knodes 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
knodes/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from . import gauss, lowrank, hybrid, centralities_nx, spectral_deflation, generate
@@ -0,0 +1,140 @@
1
+ import numpy as np
2
+ from networkx import from_scipy_sparse_array
3
+ from networkx import (
4
+ degree_centrality,
5
+ eigenvector_centrality,
6
+ katz_centrality,
7
+ pagerank,
8
+ betweenness_centrality,
9
+ closeness_centrality,
10
+ )
11
+ from scipy.sparse import csc_matrix
12
+ from typing import Tuple
13
+
14
+
15
+ def _extract_top_nodes(centralities: dict, nnodes: int) -> Tuple[np.ndarray, np.ndarray]:
16
+ """Extract top nnodes from centralities list
17
+
18
+ Args:
19
+ centralities (dict): centrality for each node
20
+ nnodes (int): number of nodes
21
+
22
+ Returns:
23
+ Tuple[np.ndarray, np.ndarray]: ordered tuple of nnodes with their respective centralities
24
+ """
25
+ nodes = np.array(list(centralities.keys()))
26
+ values = np.array(list(centralities.values()))
27
+
28
+
29
+ sorted_order = np.argsort(values)[::-1]
30
+ top_indices = sorted_order[:nnodes]
31
+
32
+ k_nodes = nodes[top_indices]
33
+ k_centralities = values[top_indices]
34
+
35
+ return k_nodes, k_centralities
36
+
37
+
38
+ def compute_degree_centrality(network: csc_matrix, nnodes: int) -> Tuple[np.ndarray, np.ndarray]:
39
+ """Computes the degree centrality of a network
40
+
41
+ Args:
42
+ network (csc_matrix): network
43
+ nnodes (int): number of nodes
44
+
45
+ Returns:
46
+ Tuple[np.ndarray, np.ndarray]: ordered tuple of nnodes with their respective centralities
47
+ """
48
+ G = from_scipy_sparse_array(network)
49
+ centralities = degree_centrality(G)
50
+
51
+ return _extract_top_nodes(centralities, nnodes)
52
+
53
+
54
+ def compute_eigenvector_centrality(network: csc_matrix, nnodes: int, max_iter: int = 1000, tol: float = 1e-6) -> Tuple[np.ndarray, np.ndarray]:
55
+ """Computes the eigenvector centrality of a network
56
+
57
+ Args:
58
+ network (csc_matrix): network
59
+ nnodes (int): number of nodes
60
+ max_iter (int, optional): max iterations for power method. Defaults to 1000.
61
+ tol (float, optional): tolerance for power method iteration. Defaults to 1e-6.
62
+
63
+ Returns:
64
+ Tuple[np.ndarray, np.ndarray]: ordered tuple of nnodes with their respective centralities
65
+ """
66
+ G = from_scipy_sparse_array(network)
67
+ centralities = eigenvector_centrality(G, max_iter=max_iter, tol=tol)
68
+
69
+ return _extract_top_nodes(centralities, nnodes)
70
+
71
+
72
+ def compute_katz_centrality(network: csc_matrix, nnodes: int, alpha: float = 0.1, beta: float = 1.0, max_iter: int = 1000, tol: float = 1e-6) -> Tuple[np.ndarray, np.ndarray]:
73
+ """Computes the Katz centrality of a network
74
+
75
+ Args:
76
+ network (csc_matrix): network
77
+ nnodes (int): number of nodes
78
+ alpha (float, optional): damping factor for Katz centrality. Defaults to 0.1.
79
+ beta (float, optional): initial centrality. Defaults to 1.0.
80
+ max_iter (int, optional): max iterations for power method. Defaults to 1000.
81
+ tol (float, optional): tolerance for power method iteration. Defaults to 1e-6.
82
+
83
+ Returns:
84
+ Tuple[np.ndarray, np.ndarray]: ordered tuple of nnodes with their respective centralities
85
+ """
86
+ G = from_scipy_sparse_array(network)
87
+ centralities = katz_centrality(G, alpha=alpha, beta=beta, max_iter=max_iter, tol=tol)
88
+
89
+ return _extract_top_nodes(centralities, nnodes)
90
+
91
+
92
+ def compute_pagerank(network: csc_matrix, nnodes: int, alpha: float = 0.85, max_iter: int = 1000, tol: float = 1e-6) -> Tuple[np.ndarray, np.ndarray]:
93
+ """Computes the PageRank of a network
94
+
95
+ Args:
96
+ network (csc_matrix): network
97
+ nnodes (int): number of nodes
98
+ alpha (float, optional): damping parameter for PageRank. Defaults to 0.1.
99
+ max_iter (int, optional): max iterations for power method. Defaults to 1000.
100
+ tol (float, optional): tolerance for power method iteration. Defaults to 1e-6.
101
+
102
+ Returns:
103
+ Tuple[np.ndarray, np.ndarray]: ordered tuple of nnodes with their respective centralities
104
+ """
105
+ G = from_scipy_sparse_array(network)
106
+ centralities = pagerank(G, alpha, max_iter=max_iter, tol=tol)
107
+
108
+ return _extract_top_nodes(centralities, nnodes)
109
+
110
+
111
+ def compute_betweenness_centrality(network: csc_matrix, nnodes: int) -> Tuple[np.ndarray, np.ndarray]:
112
+ """Computes the betweenness centrality of a network
113
+
114
+ Args:
115
+ network (csc_matrix): network
116
+ nnodes (int): number of nodes
117
+
118
+ Returns:
119
+ Tuple[np.ndarray, np.ndarray]: ordered tuple of nnodes with their respective centralities
120
+ """
121
+ G = from_scipy_sparse_array(network)
122
+ centralities = betweenness_centrality(G)
123
+
124
+ return _extract_top_nodes(centralities, nnodes)
125
+
126
+
127
+ def compute_closeness_centrality(network: csc_matrix, nnodes: int) -> Tuple[np.ndarray, np.ndarray]:
128
+ """Computes the closeness centrality of a network
129
+
130
+ Args:
131
+ network (csc_matrix): network
132
+ nnodes (int): number of nodes
133
+
134
+ Returns:
135
+ Tuple[np.ndarray, np.ndarray]: ordered tuple of nnodes with their respective centralities
136
+ """
137
+ G = from_scipy_sparse_array(network)
138
+ centralities = closeness_centrality(G)
139
+
140
+ return _extract_top_nodes(centralities, nnodes)