risk-network 0.0.13b3__py3-none-any.whl → 0.0.13b5__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.
- risk/__init__.py +3 -2
- risk/_annotation/__init__.py +10 -0
- risk/{annotation/annotation.py → _annotation/_annotation.py} +40 -19
- risk/{annotation/io.py → _annotation/_io.py} +78 -32
- risk/{annotation/nltk_setup.py → _annotation/_nltk_setup.py} +7 -5
- risk/_log/__init__.py +11 -0
- risk/{log/console.py → _log/_console.py} +22 -12
- risk/{log/parameters.py → _log/_parameters.py} +25 -14
- risk/_neighborhoods/__init__.py +8 -0
- risk/{neighborhoods/api.py → _neighborhoods/_api.py} +23 -14
- risk/{neighborhoods/community.py → _neighborhoods/_community.py} +19 -11
- risk/{neighborhoods/domains.py → _neighborhoods/_domains.py} +15 -9
- risk/{neighborhoods/neighborhoods.py → _neighborhoods/_neighborhoods.py} +24 -35
- risk/_neighborhoods/_stats/__init__.py +13 -0
- risk/_neighborhoods/_stats/_permutation/__init__.py +6 -0
- risk/{neighborhoods/stats/permutation/permutation.py → _neighborhoods/_stats/_permutation/_permutation.py} +9 -6
- risk/{neighborhoods/stats/permutation/test_functions.py → _neighborhoods/_stats/_permutation/_test_functions.py} +6 -4
- risk/{neighborhoods/stats/tests.py → _neighborhoods/_stats/_tests.py} +12 -7
- risk/_network/__init__.py +8 -0
- risk/_network/_graph/__init__.py +7 -0
- risk/{network/graph/api.py → _network/_graph/_api.py} +13 -10
- risk/{network/graph/graph.py → _network/_graph/_graph.py} +24 -13
- risk/{network/graph/stats.py → _network/_graph/_stats.py} +8 -5
- risk/{network/graph/summary.py → _network/_graph/_summary.py} +21 -12
- risk/{network/io.py → _network/_io.py} +45 -24
- risk/_network/_plotter/__init__.py +6 -0
- risk/{network/plotter/api.py → _network/_plotter/_api.py} +9 -7
- risk/{network/plotter/canvas.py → _network/_plotter/_canvas.py} +14 -10
- risk/{network/plotter/contour.py → _network/_plotter/_contour.py} +17 -11
- risk/{network/plotter/labels.py → _network/_plotter/_labels.py} +38 -23
- risk/{network/plotter/network.py → _network/_plotter/_network.py} +17 -11
- risk/{network/plotter/plotter.py → _network/_plotter/_plotter.py} +19 -15
- risk/_network/_plotter/_utils/__init__.py +7 -0
- risk/{network/plotter/utils/colors.py → _network/_plotter/_utils/_colors.py} +19 -11
- risk/{network/plotter/utils/layout.py → _network/_plotter/_utils/_layout.py} +8 -5
- risk/risk.py +8 -8
- {risk_network-0.0.13b3.dist-info → risk_network-0.0.13b5.dist-info}/METADATA +2 -2
- risk_network-0.0.13b5.dist-info/RECORD +41 -0
- {risk_network-0.0.13b3.dist-info → risk_network-0.0.13b5.dist-info}/WHEEL +1 -1
- risk/annotation/__init__.py +0 -10
- risk/log/__init__.py +0 -11
- risk/neighborhoods/__init__.py +0 -7
- risk/neighborhoods/stats/__init__.py +0 -13
- risk/neighborhoods/stats/permutation/__init__.py +0 -6
- risk/network/__init__.py +0 -4
- risk/network/graph/__init__.py +0 -4
- risk/network/plotter/__init__.py +0 -4
- risk_network-0.0.13b3.dist-info/RECORD +0 -40
- {risk_network-0.0.13b3.dist-info → risk_network-0.0.13b5.dist-info}/licenses/LICENSE +0 -0
- {risk_network-0.0.13b3.dist-info → risk_network-0.0.13b5.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_log/_parameters
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
import csv
|
@@ -11,14 +11,15 @@ from typing import Any, Dict
|
|
11
11
|
|
12
12
|
import numpy as np
|
13
13
|
|
14
|
-
from
|
14
|
+
from ._console import log_header, logger
|
15
15
|
|
16
16
|
# Suppress all warnings - this is to resolve warnings from multiprocessing
|
17
17
|
warnings.filterwarnings("ignore")
|
18
18
|
|
19
19
|
|
20
20
|
class Params:
|
21
|
-
"""
|
21
|
+
"""
|
22
|
+
Handles the storage and logging of various parameters for network analysis.
|
22
23
|
|
23
24
|
The Params class provides methods to log parameters related to different components of the analysis,
|
24
25
|
such as the network, annotation, neighborhoods, graph, and plotter settings. It also stores
|
@@ -39,7 +40,8 @@ class Params:
|
|
39
40
|
self.plotter = {}
|
40
41
|
|
41
42
|
def log_network(self, **kwargs) -> None:
|
42
|
-
"""
|
43
|
+
"""
|
44
|
+
Log network-related parameters.
|
43
45
|
|
44
46
|
Args:
|
45
47
|
**kwargs: Network parameters to log.
|
@@ -47,7 +49,8 @@ class Params:
|
|
47
49
|
self.network = {**self.network, **kwargs}
|
48
50
|
|
49
51
|
def log_annotation(self, **kwargs) -> None:
|
50
|
-
"""
|
52
|
+
"""
|
53
|
+
Log annotation-related parameters.
|
51
54
|
|
52
55
|
Args:
|
53
56
|
**kwargs: Annotation parameters to log.
|
@@ -55,7 +58,8 @@ class Params:
|
|
55
58
|
self.annotation = {**self.annotation, **kwargs}
|
56
59
|
|
57
60
|
def log_neighborhoods(self, **kwargs) -> None:
|
58
|
-
"""
|
61
|
+
"""
|
62
|
+
Log neighborhood-related parameters.
|
59
63
|
|
60
64
|
Args:
|
61
65
|
**kwargs: Neighborhood parameters to log.
|
@@ -63,7 +67,8 @@ class Params:
|
|
63
67
|
self.neighborhoods = {**self.neighborhoods, **kwargs}
|
64
68
|
|
65
69
|
def log_graph(self, **kwargs) -> None:
|
66
|
-
"""
|
70
|
+
"""
|
71
|
+
Log graph-related parameters.
|
67
72
|
|
68
73
|
Args:
|
69
74
|
**kwargs: Graph parameters to log.
|
@@ -71,7 +76,8 @@ class Params:
|
|
71
76
|
self.graph = {**self.graph, **kwargs}
|
72
77
|
|
73
78
|
def log_plotter(self, **kwargs) -> None:
|
74
|
-
"""
|
79
|
+
"""
|
80
|
+
Log plotter-related parameters.
|
75
81
|
|
76
82
|
Args:
|
77
83
|
**kwargs: Plotter parameters to log.
|
@@ -79,7 +85,8 @@ class Params:
|
|
79
85
|
self.plotter = {**self.plotter, **kwargs}
|
80
86
|
|
81
87
|
def to_csv(self, filepath: str) -> None:
|
82
|
-
"""
|
88
|
+
"""
|
89
|
+
Export the parameters to a CSV file.
|
83
90
|
|
84
91
|
Args:
|
85
92
|
filepath (str): The path where the CSV file will be saved.
|
@@ -102,7 +109,8 @@ class Params:
|
|
102
109
|
logger.info(f"Parameters exported to CSV file: {filepath}")
|
103
110
|
|
104
111
|
def to_json(self, filepath: str) -> None:
|
105
|
-
"""
|
112
|
+
"""
|
113
|
+
Export the parameters to a JSON file.
|
106
114
|
|
107
115
|
Args:
|
108
116
|
filepath (str): The path where the JSON file will be saved.
|
@@ -113,7 +121,8 @@ class Params:
|
|
113
121
|
logger.info(f"Parameters exported to JSON file: {filepath}")
|
114
122
|
|
115
123
|
def to_txt(self, filepath: str) -> None:
|
116
|
-
"""
|
124
|
+
"""
|
125
|
+
Export the parameters to a text file.
|
117
126
|
|
118
127
|
Args:
|
119
128
|
filepath (str): The path where the text file will be saved.
|
@@ -131,7 +140,8 @@ class Params:
|
|
131
140
|
logger.info(f"Parameters exported to text file: {filepath}")
|
132
141
|
|
133
142
|
def load(self) -> Dict[str, Any]:
|
134
|
-
"""
|
143
|
+
"""
|
144
|
+
Load and process various parameters, converting any np.ndarray values to lists.
|
135
145
|
|
136
146
|
Returns:
|
137
147
|
Dict[str, Any]: A dictionary containing the processed parameters.
|
@@ -149,7 +159,8 @@ class Params:
|
|
149
159
|
)
|
150
160
|
|
151
161
|
def _convert_ndarray_to_list(self, d: Dict[str, Any]) -> Dict[str, Any]:
|
152
|
-
"""
|
162
|
+
"""
|
163
|
+
Recursively convert all np.ndarray values in the dictionary to lists.
|
153
164
|
|
154
165
|
Args:
|
155
166
|
d (Dict[str, Any]): The dictionary to process.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_neighborhoods/_api
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
import copy
|
@@ -10,9 +10,9 @@ import networkx as nx
|
|
10
10
|
import numpy as np
|
11
11
|
from scipy.sparse import csr_matrix
|
12
12
|
|
13
|
-
from
|
14
|
-
from
|
15
|
-
from
|
13
|
+
from .._log import log_header, logger, params
|
14
|
+
from ._neighborhoods import get_network_neighborhoods
|
15
|
+
from ._stats import (
|
16
16
|
compute_binom_test,
|
17
17
|
compute_chi2_test,
|
18
18
|
compute_hypergeom_test,
|
@@ -23,7 +23,8 @@ from risk.neighborhoods.stats import (
|
|
23
23
|
|
24
24
|
|
25
25
|
class NeighborhoodsAPI:
|
26
|
-
"""
|
26
|
+
"""
|
27
|
+
Handles the loading of statistical results and annotation significance for neighborhoods.
|
27
28
|
|
28
29
|
The NeighborhoodsAPI class provides methods to load neighborhood results from statistical tests.
|
29
30
|
"""
|
@@ -42,7 +43,8 @@ class NeighborhoodsAPI:
|
|
42
43
|
null_distribution: str = "network",
|
43
44
|
random_seed: int = 888,
|
44
45
|
) -> Dict[str, Any]:
|
45
|
-
"""
|
46
|
+
"""
|
47
|
+
Load significant neighborhoods for the network using the binomial test.
|
46
48
|
|
47
49
|
Args:
|
48
50
|
network (nx.Graph): The network graph.
|
@@ -87,7 +89,8 @@ class NeighborhoodsAPI:
|
|
87
89
|
null_distribution: str = "network",
|
88
90
|
random_seed: int = 888,
|
89
91
|
) -> Dict[str, Any]:
|
90
|
-
"""
|
92
|
+
"""
|
93
|
+
Load significant neighborhoods for the network using the chi-squared test.
|
91
94
|
|
92
95
|
Args:
|
93
96
|
network (nx.Graph): The network graph.
|
@@ -132,7 +135,8 @@ class NeighborhoodsAPI:
|
|
132
135
|
null_distribution: str = "network",
|
133
136
|
random_seed: int = 888,
|
134
137
|
) -> Dict[str, Any]:
|
135
|
-
"""
|
138
|
+
"""
|
139
|
+
Load significant neighborhoods for the network using the hypergeometric test.
|
136
140
|
|
137
141
|
Args:
|
138
142
|
network (nx.Graph): The network graph.
|
@@ -180,7 +184,8 @@ class NeighborhoodsAPI:
|
|
180
184
|
random_seed: int = 888,
|
181
185
|
max_workers: int = 1,
|
182
186
|
) -> Dict[str, Any]:
|
183
|
-
"""
|
187
|
+
"""
|
188
|
+
Load significant neighborhoods for the network using the permutation test.
|
184
189
|
|
185
190
|
Args:
|
186
191
|
network (nx.Graph): The network graph.
|
@@ -235,7 +240,8 @@ class NeighborhoodsAPI:
|
|
235
240
|
null_distribution: str = "network",
|
236
241
|
random_seed: int = 888,
|
237
242
|
) -> Dict[str, Any]:
|
238
|
-
"""
|
243
|
+
"""
|
244
|
+
Load significant neighborhoods for the network using the Poisson test.
|
239
245
|
|
240
246
|
Args:
|
241
247
|
network (nx.Graph): The network graph.
|
@@ -280,7 +286,8 @@ class NeighborhoodsAPI:
|
|
280
286
|
null_distribution: str = "network",
|
281
287
|
random_seed: int = 888,
|
282
288
|
) -> Dict[str, Any]:
|
283
|
-
"""
|
289
|
+
"""
|
290
|
+
Load significant neighborhoods for the network using the z-score test.
|
284
291
|
|
285
292
|
Args:
|
286
293
|
network (nx.Graph): The network graph.
|
@@ -328,7 +335,8 @@ class NeighborhoodsAPI:
|
|
328
335
|
statistical_test_function: Any = compute_hypergeom_test,
|
329
336
|
**kwargs,
|
330
337
|
):
|
331
|
-
"""
|
338
|
+
"""
|
339
|
+
Load and compute significant neighborhoods for the network using a specified statistical test.
|
332
340
|
|
333
341
|
Args:
|
334
342
|
network (nx.Graph): The input network graph.
|
@@ -398,7 +406,8 @@ class NeighborhoodsAPI:
|
|
398
406
|
fraction_shortest_edges: Union[float, List, Tuple, np.ndarray] = 0.5,
|
399
407
|
random_seed: int = 888,
|
400
408
|
) -> csr_matrix:
|
401
|
-
"""
|
409
|
+
"""
|
410
|
+
Load significant neighborhoods for the network.
|
402
411
|
|
403
412
|
Args:
|
404
413
|
network (nx.Graph): The network graph.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_neighborhoods/_community
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
import community as community_louvain
|
@@ -12,13 +12,14 @@ from leidenalg import RBConfigurationVertexPartition, find_partition
|
|
12
12
|
from networkx.algorithms.community import greedy_modularity_communities
|
13
13
|
from scipy.sparse import csr_matrix
|
14
14
|
|
15
|
-
from
|
15
|
+
from .._log import logger
|
16
16
|
|
17
17
|
|
18
18
|
def calculate_greedy_modularity_neighborhoods(
|
19
19
|
network: nx.Graph, fraction_shortest_edges: float = 1.0
|
20
20
|
) -> csr_matrix:
|
21
|
-
"""
|
21
|
+
"""
|
22
|
+
Calculate neighborhoods using the Greedy Modularity method with CSR matrix output.
|
22
23
|
|
23
24
|
Args:
|
24
25
|
network (nx.Graph): The network graph.
|
@@ -62,7 +63,8 @@ def calculate_greedy_modularity_neighborhoods(
|
|
62
63
|
def calculate_label_propagation_neighborhoods(
|
63
64
|
network: nx.Graph, fraction_shortest_edges: float = 1.0
|
64
65
|
) -> csr_matrix:
|
65
|
-
"""
|
66
|
+
"""
|
67
|
+
Apply Label Propagation to the network to detect communities.
|
66
68
|
|
67
69
|
Args:
|
68
70
|
network (nx.Graph): The network graph.
|
@@ -112,7 +114,8 @@ def calculate_leiden_neighborhoods(
|
|
112
114
|
fraction_shortest_edges: float = 1.0,
|
113
115
|
random_seed: int = 888,
|
114
116
|
) -> csr_matrix:
|
115
|
-
"""
|
117
|
+
"""
|
118
|
+
Calculate neighborhoods using the Leiden method with CSR matrix output.
|
116
119
|
|
117
120
|
Args:
|
118
121
|
network (nx.Graph): The network graph.
|
@@ -168,7 +171,8 @@ def calculate_louvain_neighborhoods(
|
|
168
171
|
fraction_shortest_edges: float = 1.0,
|
169
172
|
random_seed: int = 888,
|
170
173
|
) -> csr_matrix:
|
171
|
-
"""
|
174
|
+
"""
|
175
|
+
Calculate neighborhoods using the Louvain method.
|
172
176
|
|
173
177
|
Args:
|
174
178
|
network (nx.Graph): The network graph.
|
@@ -221,7 +225,8 @@ def calculate_louvain_neighborhoods(
|
|
221
225
|
def calculate_markov_clustering_neighborhoods(
|
222
226
|
network: nx.Graph, fraction_shortest_edges: float = 1.0
|
223
227
|
) -> csr_matrix:
|
224
|
-
"""
|
228
|
+
"""
|
229
|
+
Apply Markov Clustering (MCL) to the network and return a binary neighborhood matrix (CSR).
|
225
230
|
|
226
231
|
Args:
|
227
232
|
network (nx.Graph): The network graph.
|
@@ -291,7 +296,8 @@ def calculate_markov_clustering_neighborhoods(
|
|
291
296
|
def calculate_spinglass_neighborhoods(
|
292
297
|
network: nx.Graph, fraction_shortest_edges: float = 1.0
|
293
298
|
) -> csr_matrix:
|
294
|
-
"""
|
299
|
+
"""
|
300
|
+
Apply Spinglass Community Detection to the network, handling disconnected components.
|
295
301
|
|
296
302
|
Args:
|
297
303
|
network (nx.Graph): The network graph.
|
@@ -355,7 +361,8 @@ def calculate_spinglass_neighborhoods(
|
|
355
361
|
def calculate_walktrap_neighborhoods(
|
356
362
|
network: nx.Graph, fraction_shortest_edges: float = 1.0
|
357
363
|
) -> csr_matrix:
|
358
|
-
"""
|
364
|
+
"""
|
365
|
+
Apply Walktrap Community Detection to the network with CSR matrix output.
|
359
366
|
|
360
367
|
Args:
|
361
368
|
network (nx.Graph): The network graph.
|
@@ -399,7 +406,8 @@ def calculate_walktrap_neighborhoods(
|
|
399
406
|
|
400
407
|
|
401
408
|
def _create_percentile_limited_subgraph(G: nx.Graph, fraction_shortest_edges: float) -> nx.Graph:
|
402
|
-
"""
|
409
|
+
"""
|
410
|
+
Create a subgraph containing the shortest edges based on the specified rank fraction
|
403
411
|
of all edge lengths in the input graph.
|
404
412
|
|
405
413
|
Args:
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_neighborhoods/_domains
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
from itertools import product
|
@@ -13,8 +13,9 @@ from scipy.cluster.hierarchy import fcluster, linkage
|
|
13
13
|
from sklearn.metrics import silhouette_score
|
14
14
|
from tqdm import tqdm
|
15
15
|
|
16
|
-
from risk.
|
17
|
-
|
16
|
+
from risk._annotation import get_weighted_description
|
17
|
+
|
18
|
+
from .._log import logger
|
18
19
|
|
19
20
|
# Define constants for clustering
|
20
21
|
# fmt: off
|
@@ -35,7 +36,8 @@ def define_domains(
|
|
35
36
|
linkage_metric: str,
|
36
37
|
linkage_threshold: Union[float, str],
|
37
38
|
) -> pd.DataFrame:
|
38
|
-
"""
|
39
|
+
"""
|
40
|
+
Define domains and assign nodes to these domains based on their significance scores and clustering,
|
39
41
|
handling errors by assigning unique domains when clustering fails.
|
40
42
|
|
41
43
|
Args:
|
@@ -112,7 +114,8 @@ def trim_domains(
|
|
112
114
|
min_cluster_size: int = 5,
|
113
115
|
max_cluster_size: int = 1000,
|
114
116
|
) -> Tuple[pd.DataFrame, pd.DataFrame]:
|
115
|
-
"""
|
117
|
+
"""
|
118
|
+
Trim domains that do not meet size criteria and find outliers.
|
116
119
|
|
117
120
|
Args:
|
118
121
|
domains (pd.DataFrame): DataFrame of domain data for the network nodes.
|
@@ -182,7 +185,8 @@ def trim_domains(
|
|
182
185
|
|
183
186
|
|
184
187
|
def _safeguard_matrix(matrix: np.ndarray) -> np.ndarray:
|
185
|
-
"""
|
188
|
+
"""
|
189
|
+
Safeguard the matrix by replacing NaN, Inf, and -Inf values.
|
186
190
|
|
187
191
|
Args:
|
188
192
|
matrix (np.ndarray): Data matrix.
|
@@ -211,7 +215,8 @@ def _optimize_silhouette_across_linkage_and_metrics(
|
|
211
215
|
linkage_metric: str,
|
212
216
|
linkage_threshold: Union[str, float],
|
213
217
|
) -> Tuple[str, str, float]:
|
214
|
-
"""
|
218
|
+
"""
|
219
|
+
Optimize silhouette score across different linkage methods and distance metrics.
|
215
220
|
|
216
221
|
Args:
|
217
222
|
m (np.ndarray): Data matrix.
|
@@ -287,7 +292,8 @@ def _find_best_silhouette_score(
|
|
287
292
|
lower_bound: float = 0.001,
|
288
293
|
upper_bound: float = 1.0,
|
289
294
|
) -> Tuple[float, float]:
|
290
|
-
"""
|
295
|
+
"""
|
296
|
+
Find the best silhouette score using binary search.
|
291
297
|
|
292
298
|
Args:
|
293
299
|
Z (np.ndarray): Linkage matrix.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_neighborhoods/_neighborhoods
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
import random
|
@@ -13,8 +13,8 @@ from scipy.sparse import csr_matrix
|
|
13
13
|
from sklearn.exceptions import DataConversionWarning
|
14
14
|
from sklearn.metrics.pairwise import cosine_similarity
|
15
15
|
|
16
|
-
from
|
17
|
-
from
|
16
|
+
from .._log import logger
|
17
|
+
from ._community import (
|
18
18
|
calculate_greedy_modularity_neighborhoods,
|
19
19
|
calculate_label_propagation_neighborhoods,
|
20
20
|
calculate_leiden_neighborhoods,
|
@@ -36,7 +36,8 @@ def get_network_neighborhoods(
|
|
36
36
|
leiden_resolution: float = 1.0,
|
37
37
|
random_seed: int = 888,
|
38
38
|
) -> csr_matrix:
|
39
|
-
"""
|
39
|
+
"""
|
40
|
+
Calculate the combined neighborhoods for each node using sparse matrices.
|
40
41
|
|
41
42
|
Args:
|
42
43
|
network (nx.Graph): The network graph.
|
@@ -125,7 +126,8 @@ def get_network_neighborhoods(
|
|
125
126
|
|
126
127
|
|
127
128
|
def _set_max_row_value_to_one_sparse(matrix: csr_matrix) -> csr_matrix:
|
128
|
-
"""
|
129
|
+
"""
|
130
|
+
Set the maximum value in each row of a sparse matrix to 1.
|
129
131
|
|
130
132
|
Args:
|
131
133
|
matrix (csr_matrix): The input sparse matrix.
|
@@ -142,34 +144,14 @@ def _set_max_row_value_to_one_sparse(matrix: csr_matrix) -> csr_matrix:
|
|
142
144
|
return matrix
|
143
145
|
|
144
146
|
|
145
|
-
def _set_max_row_value_to_one(matrix: np.ndarray) -> np.ndarray:
|
146
|
-
"""For each row in the input matrix, set the maximum value(s) to 1 and all other values to 0. This is particularly
|
147
|
-
useful for neighborhood matrices that have undergone multiple neighborhood detection algorithms, where the
|
148
|
-
maximum value in each row represents the most significant relationship per node in the combined neighborhoods.
|
149
|
-
|
150
|
-
Args:
|
151
|
-
matrix (np.ndarray): A 2D numpy array representing the neighborhood matrix.
|
152
|
-
|
153
|
-
Returns:
|
154
|
-
np.ndarray: The modified matrix where only the maximum value(s) in each row is set to 1, and others are set to 0.
|
155
|
-
"""
|
156
|
-
# Find the maximum value in each row (column-wise max operation)
|
157
|
-
max_values = np.max(matrix, axis=1, keepdims=True)
|
158
|
-
# Create a boolean mask where elements are True if they are the max value in their row
|
159
|
-
max_mask = matrix == max_values
|
160
|
-
# Set all elements to 0, and then set the maximum value positions to 1
|
161
|
-
matrix[:] = 0 # Set everything to 0
|
162
|
-
matrix[max_mask] = 1 # Set only the max values to 1
|
163
|
-
return matrix
|
164
|
-
|
165
|
-
|
166
147
|
def process_neighborhoods(
|
167
148
|
network: nx.Graph,
|
168
149
|
neighborhoods: Dict[str, Any],
|
169
150
|
impute_depth: int = 0,
|
170
151
|
prune_threshold: float = 0.0,
|
171
152
|
) -> Dict[str, Any]:
|
172
|
-
"""
|
153
|
+
"""
|
154
|
+
Process neighborhoods based on the imputation and pruning settings.
|
173
155
|
|
174
156
|
Args:
|
175
157
|
network (nx.Graph): The network data structure used for imputing and pruning neighbors.
|
@@ -226,7 +208,8 @@ def _impute_neighbors(
|
|
226
208
|
significant_binary_significance_matrix: np.ndarray,
|
227
209
|
max_depth: int = 3,
|
228
210
|
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
|
229
|
-
"""
|
211
|
+
"""
|
212
|
+
Impute rows with sums of zero in the significance matrix based on the closest non-zero neighbors in the network graph.
|
230
213
|
|
231
214
|
Args:
|
232
215
|
network (nx.Graph): The network graph with nodes having IDs matching the matrix indices.
|
@@ -262,7 +245,8 @@ def _impute_neighbors_with_similarity(
|
|
262
245
|
significant_binary_significance_matrix: np.ndarray,
|
263
246
|
max_depth: int = 3,
|
264
247
|
) -> Tuple[np.ndarray, np.ndarray]:
|
265
|
-
"""
|
248
|
+
"""
|
249
|
+
Impute non-significant nodes based on the closest significant neighbors' profiles and their similarity.
|
266
250
|
|
267
251
|
Args:
|
268
252
|
network (nx.Graph): The network graph with nodes having IDs matching the matrix indices.
|
@@ -306,7 +290,8 @@ def _process_node_imputation(
|
|
306
290
|
significant_binary_significance_matrix: np.ndarray,
|
307
291
|
depth: int,
|
308
292
|
) -> Tuple[np.ndarray, np.ndarray]:
|
309
|
-
"""
|
293
|
+
"""
|
294
|
+
Process the imputation for a single node based on its significant neighbors.
|
310
295
|
|
311
296
|
Args:
|
312
297
|
row_index (int): The index of the significant node being processed.
|
@@ -391,7 +376,8 @@ def _prune_neighbors(
|
|
391
376
|
significant_binary_significance_matrix: np.ndarray,
|
392
377
|
distance_threshold: float = 0.9,
|
393
378
|
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
|
394
|
-
"""
|
379
|
+
"""
|
380
|
+
Remove outliers based on their rank for edge lengths.
|
395
381
|
|
396
382
|
Args:
|
397
383
|
network (nx.Graph): The network graph with nodes having IDs matching the matrix indices.
|
@@ -450,7 +436,8 @@ def _prune_neighbors(
|
|
450
436
|
|
451
437
|
|
452
438
|
def _get_euclidean_distance(node1: Any, node2: Any, network: nx.Graph) -> float:
|
453
|
-
"""
|
439
|
+
"""
|
440
|
+
Calculate the Euclidean distance between two nodes in the network.
|
454
441
|
|
455
442
|
Args:
|
456
443
|
node1 (Any): The first node.
|
@@ -466,7 +453,8 @@ def _get_euclidean_distance(node1: Any, node2: Any, network: nx.Graph) -> float:
|
|
466
453
|
|
467
454
|
|
468
455
|
def _get_node_position(network: nx.Graph, node: Any) -> np.ndarray:
|
469
|
-
"""
|
456
|
+
"""
|
457
|
+
Retrieve the position of a node in the network as a numpy array.
|
470
458
|
|
471
459
|
Args:
|
472
460
|
network (nx.Graph): The network graph containing node positions.
|
@@ -485,7 +473,8 @@ def _get_node_position(network: nx.Graph, node: Any) -> np.ndarray:
|
|
485
473
|
|
486
474
|
|
487
475
|
def _calculate_threshold(median_distances: List, distance_threshold: float) -> float:
|
488
|
-
"""
|
476
|
+
"""
|
477
|
+
Calculate the distance threshold based on the given median distances and a percentile threshold.
|
489
478
|
|
490
479
|
Args:
|
491
480
|
median_distances (List): An array of median distances.
|
@@ -0,0 +1,13 @@
|
|
1
|
+
"""
|
2
|
+
risk/_neighborhoods/_stats
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
|
+
"""
|
5
|
+
|
6
|
+
from ._permutation import compute_permutation_test
|
7
|
+
from ._tests import (
|
8
|
+
compute_binom_test,
|
9
|
+
compute_chi2_test,
|
10
|
+
compute_hypergeom_test,
|
11
|
+
compute_poisson_test,
|
12
|
+
compute_zscore_test,
|
13
|
+
)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_neighborhoods/_stats/_permutation/_permutation
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
from multiprocessing import Manager, get_context
|
@@ -12,7 +12,7 @@ from scipy.sparse import csr_matrix
|
|
12
12
|
from threadpoolctl import threadpool_limits
|
13
13
|
from tqdm import tqdm
|
14
14
|
|
15
|
-
from
|
15
|
+
from ._test_functions import DISPATCH_TEST_FUNCTIONS
|
16
16
|
|
17
17
|
|
18
18
|
def compute_permutation_test(
|
@@ -24,7 +24,8 @@ def compute_permutation_test(
|
|
24
24
|
random_seed: int = 888,
|
25
25
|
max_workers: int = 1,
|
26
26
|
) -> Dict[str, Any]:
|
27
|
-
"""
|
27
|
+
"""
|
28
|
+
Compute permutation test for enrichment and depletion in neighborhoods.
|
28
29
|
|
29
30
|
Args:
|
30
31
|
neighborhoods (csr_matrix): Sparse binary matrix representing neighborhoods.
|
@@ -75,7 +76,8 @@ def _run_permutation_test(
|
|
75
76
|
random_seed: int = 888,
|
76
77
|
max_workers: int = 4,
|
77
78
|
) -> tuple:
|
78
|
-
"""
|
79
|
+
"""
|
80
|
+
Run the permutation test to calculate depletion and enrichment counts.
|
79
81
|
|
80
82
|
Args:
|
81
83
|
neighborhoods (csr_matrix): Sparse binary matrix representing neighborhoods.
|
@@ -181,7 +183,8 @@ def _permutation_process_batch(
|
|
181
183
|
progress_counter: ValueProxy,
|
182
184
|
max_workers: int,
|
183
185
|
) -> tuple:
|
184
|
-
"""
|
186
|
+
"""
|
187
|
+
Process a batch of permutations in a worker process.
|
185
188
|
|
186
189
|
Args:
|
187
190
|
permutations (Union[List, Tuple, np.ndarray]): Permutation batch to process.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_neighborhoods/_stats/_permutation/_test_functions
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
import numpy as np
|
@@ -15,7 +15,8 @@ from scipy.sparse import csr_matrix
|
|
15
15
|
def compute_neighborhood_score_by_sum(
|
16
16
|
neighborhoods_matrix: csr_matrix, annotation_matrix: csr_matrix
|
17
17
|
) -> np.ndarray:
|
18
|
-
"""
|
18
|
+
"""
|
19
|
+
Compute the sum of attribute values for each neighborhood using sparse matrices.
|
19
20
|
|
20
21
|
Args:
|
21
22
|
neighborhoods_matrix (csr_matrix): Sparse binary matrix representing neighborhoods.
|
@@ -34,7 +35,8 @@ def compute_neighborhood_score_by_sum(
|
|
34
35
|
def compute_neighborhood_score_by_stdev(
|
35
36
|
neighborhoods_matrix: csr_matrix, annotation_matrix: csr_matrix
|
36
37
|
) -> np.ndarray:
|
37
|
-
"""
|
38
|
+
"""
|
39
|
+
Compute the standard deviation of neighborhood scores for sparse matrices.
|
38
40
|
|
39
41
|
Args:
|
40
42
|
neighborhoods_matrix (csr_matrix): Sparse binary matrix representing neighborhoods.
|