API Reference
An implementation of the Chinese Whispers clustering algorithm.
UnknownWeightingError
Bases: ValueError
Exception raised when an unknown weighting schema is encountered.
Source code in chinese_whispers/chinese_whispers.py
23 24 25 26 27 28 | |
__init__(weighting)
Initialize the exception with the unknown weighting.
Source code in chinese_whispers/chinese_whispers.py
26 27 28 | |
WeightDict
Bases: TypedDict
A dictionary-like class that stores weights for nodes.
Attributes:
| Name | Type | Description |
|---|---|---|
weight |
float
|
The weight value associated with a key. |
Source code in chinese_whispers/chinese_whispers.py
31 32 33 34 35 36 37 38 39 40 | |
Weighting
Bases: Enum
Available weighting schemas.
Source code in chinese_whispers/chinese_whispers.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
__call__(graph, u, v)
Resolve to the corresponding weighting function and call it.
Source code in chinese_whispers/chinese_whispers.py
118 119 120 121 122 123 124 125 126 127 | |
WeightingFunc
Bases: Protocol[T]
Callable protocol for edge weighting functions.
Source code in chinese_whispers/chinese_whispers.py
103 104 105 106 107 108 | |
__call__(graph, u, v)
Calculate the weight of an edge between two nodes in a graph.
Source code in chinese_whispers/chinese_whispers.py
106 107 108 | |
aggregate_clusters(graph, label_key='label')
Produce a dictionary with the keys being cluster IDs and the values being sets of cluster elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph[T]
|
The graph object containing the clusters. |
required |
label_key
|
str
|
The attribute key used to identify the clusters. Defaults to 'label'. |
'label'
|
Returns:
| Type | Description |
|---|---|
dict[int, set[T]]
|
A dictionary where the keys represent cluster IDs and the values are sets of cluster elements. |
Source code in chinese_whispers/chinese_whispers.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
chinese_whispers(graph, weighting=None, iterations=20, ignore=None, seed=None, label_key='label')
Perform clustering of nodes in a graph using the 'weighting' method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph[T]
|
The input graph. |
required |
weighting
|
Literal['top', 'linear', 'logarithmic', 'log'] | Weighting | WeightingFunc[T] | None
|
The weighing method to use. It can be either a string specifying one of the available schemas ('top', 'linear', 'logarithmic', 'log'), an instance of the Weighting Enum, or a custom weighting function. Defaults to Weighting.TOP. |
None
|
iterations
|
int
|
The maximum number of iterations to perform. Defaults to 20. |
20
|
ignore
|
Container[T] | None
|
The set of nodes to ignore. Defaults to an empty set. |
None
|
seed
|
int | None
|
The random seed to use. Defaults to None. |
None
|
label_key
|
str
|
The key to store the cluster labels in the graph nodes. Defaults to 'label'. |
'label'
|
Returns:
| Type | Description |
|---|---|
Graph[T]
|
The input graph with cluster labels assigned to nodes. |
Three weighing schemas are available:
top: Just use the edge weights from the input graph.lin: Normalize an edge weight by the degree of the related node.log: Normalize an edge weight by the logarithm of the related node degree.
It is possible to specify the maximum number of iterations as well as the random seed to use.
Source code in chinese_whispers/chinese_whispers.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
linear_weighting(graph, node, neighbor)
Calculate the edge weight using the linear weighting schema.
This function calculates the weight of an edge between two nodes in a graph using linear weighting, which is the edge weight divided by the degree of the destination node. If the 'weight' attribute is not present, a default weight of 1.0 is assumed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph[T]
|
The graph that contains the nodes and edges. |
required |
node
|
T
|
The source node of the edge. |
required |
neighbor
|
T
|
The destination node of the edge. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The weight of the edge. |
Source code in chinese_whispers/chinese_whispers.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
log_weighting(graph, node, neighbor)
Calculate the edge weight using the logarithm weighting schema.
This function calculates the weight of an edge between two nodes in a graph using logarithm weighting, which is the edge weight divided by the logarithm of the degree of the destination node. If the 'weight' attribute is not present, a default weight of 1.0 is assumed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph[T]
|
The graph that contains the nodes and edges. |
required |
node
|
T
|
The source node of the edge. |
required |
neighbor
|
T
|
The destination node of the edge. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The weight of the edge. |
Source code in chinese_whispers/chinese_whispers.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
random_argmax(items, choice=random.choice)
Break the ties randomly.
This is an argmax function that breaks the ties randomly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Collection[tuple[T, float]]
|
A sequence of items with their corresponding weights. |
required |
choice
|
Callable[[Sequence[T]], T]
|
A callable function that takes in a sequence of items and returns one of them. |
choice
|
Returns:
| Type | Description |
|---|---|
int | None
|
An optional integer representing the index of the maximum item, if exists. |
Source code in chinese_whispers/chinese_whispers.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
resolve_weighting(weighting)
resolve_weighting(weighting: Literal['top', 'linear', 'logarithmic', 'log']) -> WeightingFunc[T]
resolve_weighting(weighting: Weighting) -> WeightingFunc[T]
resolve_weighting(weighting: WeightingFunc[T]) -> WeightingFunc[T]
resolve_weighting(weighting: str) -> WeightingFunc[T]
Resolve the weighting function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weighting
|
str | Weighting | WeightingFunc[T]
|
The weighing method to use. It can be either a string specifying one of the available schemas ('top', 'linear', 'logarithmic'), an instance of the Weighting enum, or a custom weighting function. Defaults to 'top'. |
required |
Returns:
| Type | Description |
|---|---|
WeightingFunc[T]
|
The weighting function. |
Source code in chinese_whispers/chinese_whispers.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
score(graph, node, weighting_func, ignore, label_key)
Compute label scores in the given node neighborhood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph[T]
|
The input graph. |
required |
node
|
T
|
The node in the graph. |
required |
weighting_func
|
WeightingFunc[T]
|
A function to calculate the weight between two nodes. |
required |
ignore
|
Container[T]
|
The set of nodes to ignore. |
required |
label_key
|
str
|
The key to access the label value for each node in the graph. |
required |
Returns:
| Type | Description |
|---|---|
defaultdict[int, float]
|
A dictionary with label scores as values. |
Source code in chinese_whispers/chinese_whispers.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
top_weighting(graph, node, neighbor)
Return the weight of an edge between two nodes.
This function calculates the weight of an edge between two nodes in a graph. The weight is determined by the 'weight' attribute of the edge. If the 'weight' attribute is not present, a default weight of 1.0 is assumed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph[T]
|
The graph containing the edge. |
required |
node
|
T
|
The source node of the edge. |
required |
neighbor
|
T
|
The target node of the edge. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The weight of the edge. |
Source code in chinese_whispers/chinese_whispers.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |