knodes 0.1.0__tar.gz

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-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,297 @@
1
+ Metadata-Version: 2.4
2
+ Name: knodes
3
+ Version: 0.1.0
4
+ Summary: Network oriented detection and selection of key nodes.
5
+ License-Expression: MIT
6
+ Project-URL: Repository, https://github.com/danilolecca/k-nodes-desktop
7
+ Requires-Python: >=3.9
8
+ Description-Content-Type: text/markdown
9
+ Requires-Dist: numpy<2.0,>=1.23
10
+ Requires-Dist: scipy>=1.10
11
+ Requires-Dist: networkx>=3.0
12
+ Requires-Dist: primme>=3.2; sys_platform != "win32"
13
+
14
+ # K-NODES
15
+
16
+ K-NODES is a Python library for ranking vertices in complex networks. It
17
+ provides algorithms for subgraph centrality, communicability, and starting
18
+ convenience, as well as common NetworkX centrality measures.
19
+
20
+ The package contains the calculation library only; it does not install or run
21
+ the K-NODES desktop application or its API server.
22
+
23
+ ## Installation
24
+
25
+ K-NODES requires Python 3.9 or newer. Python 3.10–3.12 is recommended.
26
+
27
+ ```bash
28
+ python -m pip install knodes
29
+ ```
30
+
31
+ For an isolated environment, create and activate a virtual environment before
32
+ installing:
33
+
34
+ ```bash
35
+ python -m venv .venv
36
+ source .venv/bin/activate
37
+ python -m pip install --upgrade pip
38
+ python -m pip install knodes
39
+ ```
40
+
41
+ In Windows PowerShell, activate the environment with:
42
+
43
+ ```powershell
44
+ .\.venv\Scripts\Activate.ps1
45
+ ```
46
+
47
+ The package installs NumPy, SciPy, and NetworkX. PRIMME is installed on
48
+ platforms other than Windows; when PRIMME is unavailable, the low-rank
49
+ algorithms use the SciPy ARPACK-based fallback.
50
+
51
+ On Linux, if pip needs to build PRIMME from source, install BLAS/LAPACK and a
52
+ Fortran compiler first. On Debian or Ubuntu:
53
+
54
+ ```bash
55
+ sudo apt-get install libblas-dev liblapack-dev gfortran
56
+ ```
57
+
58
+ ## Quick start
59
+
60
+ The main algorithms accept a symmetric, square adjacency matrix in SciPy CSC
61
+ format. This example generates an undirected scale-free network and finds the
62
+ five vertices with the highest exponential subgraph centrality:
63
+
64
+ ```python
65
+ from knodes import gauss, generate
66
+
67
+ adjacency = generate.generate_scale_free_complex_network(nnodes=100, k=5)
68
+
69
+ top_nodes, top_scores, reached_iteration_limit, iterations, _ = (
70
+ gauss.sgcengauss(adjacency, fun_name="exp", nnodes=5)
71
+ )
72
+
73
+ for node, score in zip(top_nodes, top_scores):
74
+ print(f"Vertex {node}: {score:.6f}")
75
+
76
+ if reached_iteration_limit:
77
+ print("At least one calculation reached the Gauss iteration limit.")
78
+ ```
79
+
80
+ Vertex indices returned by K-NODES are zero-based. The first two returned
81
+ arrays contain the top vertex indices and their scores, ordered from highest
82
+ to lowest score. A low-rank method may return additional near-tie candidates
83
+ when its convergence criteria request them; the first `nnodes` entries are
84
+ the requested top vertices.
85
+
86
+ ## Input graphs
87
+
88
+ The built-in generators return SciPy CSC adjacency matrices. To use a
89
+ NetworkX graph, choose and keep an explicit ordering of its vertices when
90
+ building the matrix. This lets you map K-NODES' zero-based indices back to
91
+ your original vertex labels:
92
+
93
+ ```python
94
+ import networkx as nx
95
+ from scipy.sparse import csc_matrix
96
+ from knodes import gauss
97
+
98
+ graph = nx.karate_club_graph()
99
+ vertex_labels = list(graph.nodes())
100
+ adjacency = csc_matrix(
101
+ nx.to_scipy_sparse_array(
102
+ graph,
103
+ nodelist=vertex_labels,
104
+ format="csc",
105
+ dtype=float,
106
+ )
107
+ )
108
+
109
+ top_indices, top_scores, *_ = gauss.sgcengauss(
110
+ adjacency, fun_name="exp", nnodes=5
111
+ )
112
+ top_labels = [vertex_labels[int(index)] for index in top_indices]
113
+ print(list(zip(top_labels, top_scores)))
114
+ ```
115
+
116
+ The matrix must be square and symmetric for the matrix-function algorithms.
117
+ The low-rank methods are designed for unweighted adjacency matrices. For
118
+ communicability methods, the source vertex `i` is a zero-based matrix index;
119
+ an isolated source vertex has no other vertices to rank.
120
+
121
+ ## Measures and functions
122
+
123
+ The matrix-function algorithms support two functions of the adjacency matrix
124
+ `A`:
125
+
126
+ - `"exp"`: the matrix exponential `exp(A)`; this is the default.
127
+ - `"res"`: the resolvent `(I - alpha*A)^-1`.
128
+
129
+ The resolvent requires an `alpha` value satisfying
130
+ `0 < alpha < 1 / lambda_max(A)`, where `lambda_max(A)` is the largest
131
+ eigenvalue. Pass it in the `opts` dictionary. For example:
132
+
133
+ ```python
134
+ from knodes import gauss
135
+ from scipy.sparse.linalg import eigsh
136
+
137
+ lambda_max = eigsh(adjacency, k=1, which="LA", return_eigenvectors=False)[0]
138
+ alpha = 0.9 / lambda_max
139
+ opts = {"alpha": alpha}
140
+
141
+ top_nodes, scores, *_ = gauss.sgcengauss(
142
+ adjacency, fun_name="res", nnodes=5, opts=opts
143
+ )
144
+ ```
145
+
146
+ The three measures are:
147
+
148
+ | Measure | Meaning | Matrix-function result |
149
+ | --- | --- | --- |
150
+ | Subgraph centrality | Closed-walk contribution at each vertex | `[f(A)]_ii` |
151
+ | Communicability | Connection strength from a source vertex `i` to other vertices | `[f(A)]_ij` |
152
+ | Starting convenience | Total communicability from each vertex to the network | `[f(A) 1]_i` |
153
+
154
+ Here, `f(A)` is either `exp(A)` or the resolvent specified above.
155
+
156
+ ## Algorithms
157
+
158
+ The `gauss`, `lowrank`, and `hybrid` modules implement the same three
159
+ measures:
160
+
161
+ | Measure | Gauss quadrature | Low-rank approximation | Hybrid |
162
+ | --- | --- | --- | --- |
163
+ | Subgraph centrality | `gauss.sgcengauss` | `lowrank.sgcenlr` | `hybrid.sgcenhyb` |
164
+ | Communicability | `gauss.commgauss` | `lowrank.commlr` | `hybrid.commhyb` |
165
+ | Starting convenience | `gauss.stconvgauss` | `lowrank.stconvlr` | `hybrid.stconvhyb` |
166
+
167
+ Gauss methods evaluate the measure using Lanczos quadrature. Low-rank methods
168
+ use computed eigenpairs to estimate scores and identify candidates. Hybrid
169
+ methods first select candidates using a low-rank approximation, then refine
170
+ their scores with Gauss quadrature.
171
+
172
+ All methods accept `A`, `fun_name`, `nnodes`, and an optional `opts`
173
+ dictionary. Communicability methods also require `i`, the source vertex
174
+ index. For example:
175
+
176
+ ```python
177
+ from knodes import gauss, hybrid, lowrank
178
+
179
+ top_nodes, scores, *_ = gauss.commgauss(
180
+ adjacency, i=0, fun_name="exp", nnodes=5
181
+ )
182
+
183
+ approx_nodes, approx_scores, eigenpairs_used, history = lowrank.sgcenlr(
184
+ adjacency, fun_name="exp", nnodes=5
185
+ )
186
+
187
+ hybrid_nodes, hybrid_scores, *diagnostics = hybrid.sgcenhyb(
188
+ adjacency,
189
+ fun_name="exp",
190
+ nnodes=5,
191
+ opts={"lr_nnodes": 20},
192
+ )
193
+ ```
194
+
195
+ For hybrid methods, `opts["lr_nnodes"]` is required. It controls the number
196
+ of candidates sought by the low-rank stage and should generally be greater
197
+ than or equal to `nnodes`. The returned tuple starts with the selected vertex
198
+ indices and scores; subsequent values contain convergence and diagnostic
199
+ information. Low-rank methods return the selected indices, estimated scores,
200
+ the number of eigenpairs used, and candidate-bound history.
201
+
202
+ ### Options
203
+
204
+ Options are passed as a dictionary using the names below:
205
+
206
+ ```python
207
+ opts = {
208
+ "gauss_tol": 1e-6,
209
+ "gauss_max_iterations": 200,
210
+ }
211
+ top_nodes, scores, *_ = gauss.sgcengauss(
212
+ adjacency, nnodes=5, opts=opts
213
+ )
214
+ ```
215
+
216
+ Common Gauss options:
217
+
218
+ - `gauss_tol`: quadrature convergence tolerance.
219
+ - `gauss_max_iterations`: maximum number of Lanczos iterations.
220
+ - `gauss_largest_eigenvalue`: provide an upper bound for the largest
221
+ eigenvalue; otherwise it is computed automatically.
222
+ - `alpha`: resolvent parameter when `fun_name="res"`.
223
+
224
+ Low-rank options use the `lr_` prefix. The most useful include
225
+ `lr_max_iterations`, `lr_max_eigenvectors`, `lr_approximation_tol`,
226
+ `lr_primme_batch_size`, `lr_primme_tol`, and
227
+ `lr_primme_max_iterations`. Hybrid methods accept options for both stages.
228
+ Defaults can differ by measure and algorithm; consult the function's
229
+ docstring for the full option list.
230
+
231
+ ## Network generators
232
+
233
+ The `generate` module provides three undirected graph generators. Each
234
+ returns a SciPy CSC adjacency matrix:
235
+
236
+ ```python
237
+ from knodes import generate
238
+
239
+ scale_free = generate.generate_scale_free_complex_network(
240
+ nnodes=100, k=5
241
+ )
242
+ small_world = generate.generate_small_world_complex_network(
243
+ nnodes=100, k=6, probability=0.1
244
+ )
245
+ random_graph = generate.generate_random_model_complex_network(
246
+ nnodes=100, probability=0.05
247
+ )
248
+ ```
249
+
250
+ - `generate_scale_free_complex_network(nnodes, k=5)` uses the
251
+ Barabási–Albert model.
252
+ - `generate_small_world_complex_network(nnodes, k=5, probability=0.01)`
253
+ uses the Watts–Strogatz model.
254
+ - `generate_random_model_complex_network(nnodes, probability)` uses the
255
+ Erdős–Rényi model.
256
+
257
+ ## Standard centrality measures
258
+
259
+ The `centralities_nx` module wraps NetworkX centrality functions. Each
260
+ function takes the CSC adjacency matrix and the number of top vertices to
261
+ return. It returns `(vertex_indices, scores)`, ordered by descending score:
262
+
263
+ ```python
264
+ from knodes import centralities_nx
265
+
266
+ nodes, scores = centralities_nx.compute_degree_centrality(
267
+ adjacency, nnodes=5
268
+ )
269
+ ```
270
+
271
+ Available functions are:
272
+
273
+ - `compute_degree_centrality`
274
+ - `compute_eigenvector_centrality`
275
+ - `compute_katz_centrality`
276
+ - `compute_pagerank`
277
+ - `compute_betweenness_centrality`
278
+ - `compute_closeness_centrality`
279
+
280
+ The eigenvector, Katz, and PageRank functions also accept iteration and
281
+ tolerance parameters. Katz centrality accepts `alpha` and `beta`; PageRank
282
+ accepts its damping parameter `alpha`.
283
+
284
+ ## API reference
285
+
286
+ All public functions and their detailed parameter descriptions are available
287
+ in the module docstrings:
288
+
289
+ - `knodes.generate`
290
+ - `knodes.gauss`
291
+ - `knodes.lowrank`
292
+ - `knodes.hybrid`
293
+ - `knodes.centralities_nx`
294
+
295
+ ## License
296
+
297
+ K-NODES is distributed under the MIT License.
knodes-0.1.0/README.md ADDED
@@ -0,0 +1,284 @@
1
+ # K-NODES
2
+
3
+ K-NODES is a Python library for ranking vertices in complex networks. It
4
+ provides algorithms for subgraph centrality, communicability, and starting
5
+ convenience, as well as common NetworkX centrality measures.
6
+
7
+ The package contains the calculation library only; it does not install or run
8
+ the K-NODES desktop application or its API server.
9
+
10
+ ## Installation
11
+
12
+ K-NODES requires Python 3.9 or newer. Python 3.10–3.12 is recommended.
13
+
14
+ ```bash
15
+ python -m pip install knodes
16
+ ```
17
+
18
+ For an isolated environment, create and activate a virtual environment before
19
+ installing:
20
+
21
+ ```bash
22
+ python -m venv .venv
23
+ source .venv/bin/activate
24
+ python -m pip install --upgrade pip
25
+ python -m pip install knodes
26
+ ```
27
+
28
+ In Windows PowerShell, activate the environment with:
29
+
30
+ ```powershell
31
+ .\.venv\Scripts\Activate.ps1
32
+ ```
33
+
34
+ The package installs NumPy, SciPy, and NetworkX. PRIMME is installed on
35
+ platforms other than Windows; when PRIMME is unavailable, the low-rank
36
+ algorithms use the SciPy ARPACK-based fallback.
37
+
38
+ On Linux, if pip needs to build PRIMME from source, install BLAS/LAPACK and a
39
+ Fortran compiler first. On Debian or Ubuntu:
40
+
41
+ ```bash
42
+ sudo apt-get install libblas-dev liblapack-dev gfortran
43
+ ```
44
+
45
+ ## Quick start
46
+
47
+ The main algorithms accept a symmetric, square adjacency matrix in SciPy CSC
48
+ format. This example generates an undirected scale-free network and finds the
49
+ five vertices with the highest exponential subgraph centrality:
50
+
51
+ ```python
52
+ from knodes import gauss, generate
53
+
54
+ adjacency = generate.generate_scale_free_complex_network(nnodes=100, k=5)
55
+
56
+ top_nodes, top_scores, reached_iteration_limit, iterations, _ = (
57
+ gauss.sgcengauss(adjacency, fun_name="exp", nnodes=5)
58
+ )
59
+
60
+ for node, score in zip(top_nodes, top_scores):
61
+ print(f"Vertex {node}: {score:.6f}")
62
+
63
+ if reached_iteration_limit:
64
+ print("At least one calculation reached the Gauss iteration limit.")
65
+ ```
66
+
67
+ Vertex indices returned by K-NODES are zero-based. The first two returned
68
+ arrays contain the top vertex indices and their scores, ordered from highest
69
+ to lowest score. A low-rank method may return additional near-tie candidates
70
+ when its convergence criteria request them; the first `nnodes` entries are
71
+ the requested top vertices.
72
+
73
+ ## Input graphs
74
+
75
+ The built-in generators return SciPy CSC adjacency matrices. To use a
76
+ NetworkX graph, choose and keep an explicit ordering of its vertices when
77
+ building the matrix. This lets you map K-NODES' zero-based indices back to
78
+ your original vertex labels:
79
+
80
+ ```python
81
+ import networkx as nx
82
+ from scipy.sparse import csc_matrix
83
+ from knodes import gauss
84
+
85
+ graph = nx.karate_club_graph()
86
+ vertex_labels = list(graph.nodes())
87
+ adjacency = csc_matrix(
88
+ nx.to_scipy_sparse_array(
89
+ graph,
90
+ nodelist=vertex_labels,
91
+ format="csc",
92
+ dtype=float,
93
+ )
94
+ )
95
+
96
+ top_indices, top_scores, *_ = gauss.sgcengauss(
97
+ adjacency, fun_name="exp", nnodes=5
98
+ )
99
+ top_labels = [vertex_labels[int(index)] for index in top_indices]
100
+ print(list(zip(top_labels, top_scores)))
101
+ ```
102
+
103
+ The matrix must be square and symmetric for the matrix-function algorithms.
104
+ The low-rank methods are designed for unweighted adjacency matrices. For
105
+ communicability methods, the source vertex `i` is a zero-based matrix index;
106
+ an isolated source vertex has no other vertices to rank.
107
+
108
+ ## Measures and functions
109
+
110
+ The matrix-function algorithms support two functions of the adjacency matrix
111
+ `A`:
112
+
113
+ - `"exp"`: the matrix exponential `exp(A)`; this is the default.
114
+ - `"res"`: the resolvent `(I - alpha*A)^-1`.
115
+
116
+ The resolvent requires an `alpha` value satisfying
117
+ `0 < alpha < 1 / lambda_max(A)`, where `lambda_max(A)` is the largest
118
+ eigenvalue. Pass it in the `opts` dictionary. For example:
119
+
120
+ ```python
121
+ from knodes import gauss
122
+ from scipy.sparse.linalg import eigsh
123
+
124
+ lambda_max = eigsh(adjacency, k=1, which="LA", return_eigenvectors=False)[0]
125
+ alpha = 0.9 / lambda_max
126
+ opts = {"alpha": alpha}
127
+
128
+ top_nodes, scores, *_ = gauss.sgcengauss(
129
+ adjacency, fun_name="res", nnodes=5, opts=opts
130
+ )
131
+ ```
132
+
133
+ The three measures are:
134
+
135
+ | Measure | Meaning | Matrix-function result |
136
+ | --- | --- | --- |
137
+ | Subgraph centrality | Closed-walk contribution at each vertex | `[f(A)]_ii` |
138
+ | Communicability | Connection strength from a source vertex `i` to other vertices | `[f(A)]_ij` |
139
+ | Starting convenience | Total communicability from each vertex to the network | `[f(A) 1]_i` |
140
+
141
+ Here, `f(A)` is either `exp(A)` or the resolvent specified above.
142
+
143
+ ## Algorithms
144
+
145
+ The `gauss`, `lowrank`, and `hybrid` modules implement the same three
146
+ measures:
147
+
148
+ | Measure | Gauss quadrature | Low-rank approximation | Hybrid |
149
+ | --- | --- | --- | --- |
150
+ | Subgraph centrality | `gauss.sgcengauss` | `lowrank.sgcenlr` | `hybrid.sgcenhyb` |
151
+ | Communicability | `gauss.commgauss` | `lowrank.commlr` | `hybrid.commhyb` |
152
+ | Starting convenience | `gauss.stconvgauss` | `lowrank.stconvlr` | `hybrid.stconvhyb` |
153
+
154
+ Gauss methods evaluate the measure using Lanczos quadrature. Low-rank methods
155
+ use computed eigenpairs to estimate scores and identify candidates. Hybrid
156
+ methods first select candidates using a low-rank approximation, then refine
157
+ their scores with Gauss quadrature.
158
+
159
+ All methods accept `A`, `fun_name`, `nnodes`, and an optional `opts`
160
+ dictionary. Communicability methods also require `i`, the source vertex
161
+ index. For example:
162
+
163
+ ```python
164
+ from knodes import gauss, hybrid, lowrank
165
+
166
+ top_nodes, scores, *_ = gauss.commgauss(
167
+ adjacency, i=0, fun_name="exp", nnodes=5
168
+ )
169
+
170
+ approx_nodes, approx_scores, eigenpairs_used, history = lowrank.sgcenlr(
171
+ adjacency, fun_name="exp", nnodes=5
172
+ )
173
+
174
+ hybrid_nodes, hybrid_scores, *diagnostics = hybrid.sgcenhyb(
175
+ adjacency,
176
+ fun_name="exp",
177
+ nnodes=5,
178
+ opts={"lr_nnodes": 20},
179
+ )
180
+ ```
181
+
182
+ For hybrid methods, `opts["lr_nnodes"]` is required. It controls the number
183
+ of candidates sought by the low-rank stage and should generally be greater
184
+ than or equal to `nnodes`. The returned tuple starts with the selected vertex
185
+ indices and scores; subsequent values contain convergence and diagnostic
186
+ information. Low-rank methods return the selected indices, estimated scores,
187
+ the number of eigenpairs used, and candidate-bound history.
188
+
189
+ ### Options
190
+
191
+ Options are passed as a dictionary using the names below:
192
+
193
+ ```python
194
+ opts = {
195
+ "gauss_tol": 1e-6,
196
+ "gauss_max_iterations": 200,
197
+ }
198
+ top_nodes, scores, *_ = gauss.sgcengauss(
199
+ adjacency, nnodes=5, opts=opts
200
+ )
201
+ ```
202
+
203
+ Common Gauss options:
204
+
205
+ - `gauss_tol`: quadrature convergence tolerance.
206
+ - `gauss_max_iterations`: maximum number of Lanczos iterations.
207
+ - `gauss_largest_eigenvalue`: provide an upper bound for the largest
208
+ eigenvalue; otherwise it is computed automatically.
209
+ - `alpha`: resolvent parameter when `fun_name="res"`.
210
+
211
+ Low-rank options use the `lr_` prefix. The most useful include
212
+ `lr_max_iterations`, `lr_max_eigenvectors`, `lr_approximation_tol`,
213
+ `lr_primme_batch_size`, `lr_primme_tol`, and
214
+ `lr_primme_max_iterations`. Hybrid methods accept options for both stages.
215
+ Defaults can differ by measure and algorithm; consult the function's
216
+ docstring for the full option list.
217
+
218
+ ## Network generators
219
+
220
+ The `generate` module provides three undirected graph generators. Each
221
+ returns a SciPy CSC adjacency matrix:
222
+
223
+ ```python
224
+ from knodes import generate
225
+
226
+ scale_free = generate.generate_scale_free_complex_network(
227
+ nnodes=100, k=5
228
+ )
229
+ small_world = generate.generate_small_world_complex_network(
230
+ nnodes=100, k=6, probability=0.1
231
+ )
232
+ random_graph = generate.generate_random_model_complex_network(
233
+ nnodes=100, probability=0.05
234
+ )
235
+ ```
236
+
237
+ - `generate_scale_free_complex_network(nnodes, k=5)` uses the
238
+ Barabási–Albert model.
239
+ - `generate_small_world_complex_network(nnodes, k=5, probability=0.01)`
240
+ uses the Watts–Strogatz model.
241
+ - `generate_random_model_complex_network(nnodes, probability)` uses the
242
+ Erdős–Rényi model.
243
+
244
+ ## Standard centrality measures
245
+
246
+ The `centralities_nx` module wraps NetworkX centrality functions. Each
247
+ function takes the CSC adjacency matrix and the number of top vertices to
248
+ return. It returns `(vertex_indices, scores)`, ordered by descending score:
249
+
250
+ ```python
251
+ from knodes import centralities_nx
252
+
253
+ nodes, scores = centralities_nx.compute_degree_centrality(
254
+ adjacency, nnodes=5
255
+ )
256
+ ```
257
+
258
+ Available functions are:
259
+
260
+ - `compute_degree_centrality`
261
+ - `compute_eigenvector_centrality`
262
+ - `compute_katz_centrality`
263
+ - `compute_pagerank`
264
+ - `compute_betweenness_centrality`
265
+ - `compute_closeness_centrality`
266
+
267
+ The eigenvector, Katz, and PageRank functions also accept iteration and
268
+ tolerance parameters. Katz centrality accepts `alpha` and `beta`; PageRank
269
+ accepts its damping parameter `alpha`.
270
+
271
+ ## API reference
272
+
273
+ All public functions and their detailed parameter descriptions are available
274
+ in the module docstrings:
275
+
276
+ - `knodes.generate`
277
+ - `knodes.gauss`
278
+ - `knodes.lowrank`
279
+ - `knodes.hybrid`
280
+ - `knodes.centralities_nx`
281
+
282
+ ## License
283
+
284
+ K-NODES is distributed under the MIT License.
@@ -0,0 +1 @@
1
+ from . import gauss, lowrank, hybrid, centralities_nx, spectral_deflation, generate