Classic
These functions are classic graph theoretic metrics.
ccc(matrix)
Computes the classical clustering coefficient of a directed graph
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
numpy array
|
the adjaceny matrix of an unweighted graph |
required |
Returns:
| Type | Description |
|---|---|
Series
|
The index is the node index in |
References
The formula is taken from the following paper.
[1] G. Fagiolo, "Clustering in complex directed networks", 2006; DOI: 10.1103/PhysRevE.76.026107.
[2] Conceição, Pedro, et al. "An application of neighbourhoods in digraphs to the classification of binary dynamics.", 2022 DOI: 10.1162/netn_a_00228.
Source code in src/connalysis/network/classic.py
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 | |
closeness_connected_components(adj, directed=False, return_sum=True)
Compute the closeness of each connected component of more than 1 vertex
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adj
|
array_like
|
Adjacency matrix of the graph |
required |
directed
|
bool
|
If |
False
|
return_sum
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
array_like
|
A single array( if |
Source code in src/connalysis/network/classic.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
connected_components(adj, directed=True, connection='weak', return_labels=False)
Returns a list of the size of the connected components of the graph
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adj
|
array_like or sparse matrix
|
Adjacency matrix of the graph |
required |
directed
|
bool
|
If |
True
|
connection
|
str {'weak', 'strong'}
|
If |
'weak'
|
return_labels
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
array_like
|
A list of the size of the connected components of the graph. If return_labels == True, it also returns the list of labels of the connected components. |
Source code in src/connalysis/network/classic.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
connection_probability_within(m, v, cols=['x', 'y'], max_dist=100, type='directed', skip_symmetry_check=False)
Returns the average density of submatrices of nodes within distance max_dist of each node in m.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
array or sparse matrix
|
Adjacency matrix of the graph |
required |
v
|
DataFrame or tuple
|
DataFrame with the coordinates of the nodes if m is square and has the same pre and post nodes. If a tuple is passed, it should contain two DataFrames, one for the source nodes and one for the target nodes. |
required |
cols
|
list
|
Columns of the DataFrame containing the coordinates of the nodes. |
['x', 'y']
|
max_dist
|
float
|
Maximum distance between nodes to be considered connected. |
100
|
type
|
str {'directed', 'undirected', 'reciprocal'}
|
The type of the graph considered for the computation. If 'directed', the density as a directed graph is computed. If 'undirected', the density of the underlying undirected graph is computed. Only possible if the matrix is square. If 'reciprocal', the density of the underlying reciprocal graph is computed. Only possible if the matrix is square. |
'directed'
|
skip_symmetry_check
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
float
|
The average density of the submatrices. |
Source code in src/connalysis/network/classic.py
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 242 243 244 245 246 247 | |
core_number(adj)
Returns the core number for each node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adj
|
array_like or sparse matrix
|
Adjacency matrix of the graph |
required |
directed
|
bool
|
If |
required |
connection
|
str {'weak', 'strong'}
|
If |
required |
return_labels
|
bool
|
If |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary with keys the indices of the nodes of adj and values their corresponding core number. |
Notes
The k-core of a graph is the maximal subgraph that contains nodes of degree k or more in the induced subgraph. The core number of a node is the largest value k of a k-core containing that node. For directed graphs the total node degree is use, i.e., the sum of in-degree + out-degree.
Source code in src/connalysis/network/classic.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
density(adj, type='directed', skip_symmetry_check=False)
Returns the density of a matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adj
|
array_like or sparse matrix
|
Adjacency matrix of the graph |
required |
type
|
str {'directed', 'undirected', 'reciprocal'}
|
The type of the graph considered for the computation. If 'directed', the density as a directed graph is computed. If 'undirected', the density of the underlying undirected graph is computed. If 'reciprocal', the density of the underlying reciprocal graph is computed. |
'directed'
|
skip_symmetry_check
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
float
|
The density of the graph. |
Source code in src/connalysis/network/classic.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
efficient_rich_club_curve(M, direction='TOTAL', pre_calculated_filtration=None, sparse_bin_set=False)
Fast computation of the rich-club curve of the matrix M with respect to a degree filtration and possibly any filtration chosen by the user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
M
|
array_like or sparse matrix
|
Adjacency matrix of the graph. |
required |
direction
|
str {'OUT', 'IN', 'TOTAL'}
|
'OUT' : Compute the rich-club curve for the out-degree. 'IN' : Compute the rich-club curve for the in-degree. 'TOTAL' or None : Compute the rich-club curve for the total degree i.e., in-degree + out-degree. |
'TOTAL'
|
pre_calculated_filtration
|
pandas series
|
To provide user defined filtration values other than degree. |
None
|
sparse_bin_set
|
bool
|
If False, all integer values between 0 and the maximum degree/filtration value will be used to generate the bins. If True, unique values of the degrees/filtration will be used. This is useful when the degrees/filtration values are sparse over the whole range. |
False
|
Returns:
| Type | Description |
|---|---|
Pandas series
|
With index the center of the binned degrees/filtration and values the rich club coefficient at that degree. |
Notes
The rich-club coefficient is a measure of the tendency of high-degree nodes (nodes with a high filtration value) to form tightly interconnected communities.
Source code in src/connalysis/network/classic.py
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | |
get_pairs_within(m, v, cols=['x', 'y'], max_dist=100)
Returns a matrix of the paris of nodes within distance max_dist of each other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
array or sparse matrix
|
Adjacency matrix of the graph |
required |
v
|
DataFrame or tuple
|
DataFrame with the coordinates of the nodes if m is square and has the same source and target nodes. If a tuple is passed, it should contain two DataFrames, one for the source nodes and one for the target nodes. |
required |
cols
|
list
|
Columns of the DataFrame containing the coordinates of the nodes. |
['x', 'y']
|
max_dist
|
float
|
Maximum distance between nodes to be considered connected. |
100
|
Returns:
| Type | Description |
|---|---|
sparse matrix
|
Boolean matrix with 1 indicating the pairs of nodes within |
Source code in src/connalysis/network/classic.py
162 163 164 165 166 167 168 169 170 171 172 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 | |
largest_strongly_connected_component(adjacency_matrix)
Computes the largest strongly connected component of the graph with adjacency matrix adjacency_matrix, and returns the adjacency matrix of said component
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adjacency_matrix
|
numpy array
|
the adjaceny matrix of the DiGraph as a numpy array |
required |
Returns:
| Type | Description |
|---|---|
numpy array
|
The adjacency matrix of the largest strongly connected component |
Source code in src/connalysis/network/classic.py
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | |
np_to_nx(adjacency_matrix)
Converts numpy array of an adjacency matrix to a networkx digraph
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adjacency_matrix
|
numpy array
|
the adjaceny matrix of the DiGraph as a numpy array |
required |
Returns:
| Type | Description |
|---|---|
networkx DiGraph
|
a directed graph |
Source code in src/connalysis/network/classic.py
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 | |
nx_to_np(directed_graph)
Converts networkx digraph to numpy array of the adjacency matrix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directed_graph
|
networkx DiGraph
|
a directed graph |
required |
Returns:
| Type | Description |
|---|---|
numpy array
|
the adjaceny matrix of the DiGraph as a numpy array |
Source code in src/connalysis/network/classic.py
672 673 674 675 676 677 678 679 680 681 682 683 684 685 | |
rich_club_curve(m, direction='TOTAL')
Compute the rich-club curve of the matrix m.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
array_like or sparse matrix
|
Adjacency matrix of the graph. |
required |
direction
|
str {'OUT', 'IN', 'TOTAL'}
|
'OUT' : Compute the rich-club curve for the out-degree. 'IN' : Compute the rich-club curve for the in-degree. 'TOTAL' : Compute the rich-club curve for the total degree i.e., in-degree + out-degree. |
'TOTAL'
|
Returns:
| Type | Description |
|---|---|
Pandas series
|
With index the center of the binned degrees and values the rich club coefficient at that degree. |
Notes
The rich-club coefficient is a measure of the tendency of high-degree nodes to form tightly interconnected communities.
Source code in src/connalysis/network/classic.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |