risk-network 0.0.16b0__py3-none-any.whl → 0.0.16b2__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 +2 -2
- risk/{_annotation → annotation}/__init__.py +2 -2
- risk/{_annotation → annotation}/_nltk_setup.py +3 -3
- risk/{_annotation/_annotation.py → annotation/annotation.py} +22 -25
- risk/{_annotation/_io.py → annotation/io.py} +4 -4
- risk/cluster/__init__.py +8 -0
- risk/{_neighborhoods → cluster}/_community.py +37 -37
- risk/cluster/api.py +273 -0
- risk/{_neighborhoods/_neighborhoods.py → cluster/cluster.py} +127 -98
- risk/{_neighborhoods/_domains.py → cluster/label.py} +18 -12
- risk/{_log → log}/__init__.py +2 -2
- risk/{_log/_console.py → log/console.py} +2 -2
- risk/{_log/_parameters.py → log/parameters.py} +20 -10
- risk/network/__init__.py +8 -0
- risk/network/graph/__init__.py +7 -0
- risk/{_network/_graph → network/graph}/_stats.py +2 -2
- risk/{_network/_graph → network/graph}/_summary.py +13 -13
- risk/{_network/_graph/_api.py → network/graph/api.py} +37 -39
- risk/{_network/_graph/_graph.py → network/graph/graph.py} +5 -5
- risk/{_network/_io.py → network/io.py} +9 -4
- risk/network/plotter/__init__.py +6 -0
- risk/{_network/_plotter → network/plotter}/_canvas.py +6 -6
- risk/{_network/_plotter → network/plotter}/_contour.py +4 -4
- risk/{_network/_plotter → network/plotter}/_labels.py +6 -6
- risk/{_network/_plotter → network/plotter}/_network.py +7 -7
- risk/{_network/_plotter → network/plotter}/_plotter.py +5 -5
- risk/network/plotter/_utils/__init__.py +7 -0
- risk/{_network/_plotter/_utils/_colors.py → network/plotter/_utils/colors.py} +3 -3
- risk/{_network/_plotter/_utils/_layout.py → network/plotter/_utils/layout.py} +2 -2
- risk/{_network/_plotter/_api.py → network/plotter/api.py} +5 -5
- risk/{_risk.py → risk.py} +9 -8
- risk/stats/__init__.py +6 -0
- risk/stats/_stats/__init__.py +11 -0
- risk/stats/_stats/permutation/__init__.py +6 -0
- risk/stats/_stats/permutation/_test_functions.py +72 -0
- risk/{_neighborhoods/_stats/_permutation/_permutation.py → stats/_stats/permutation/permutation.py} +35 -37
- risk/{_neighborhoods/_stats/_tests.py → stats/_stats/tests.py} +32 -34
- risk/stats/api.py +202 -0
- {risk_network-0.0.16b0.dist-info → risk_network-0.0.16b2.dist-info}/METADATA +2 -2
- risk_network-0.0.16b2.dist-info/RECORD +43 -0
- risk/_neighborhoods/__init__.py +0 -8
- risk/_neighborhoods/_api.py +0 -354
- risk/_neighborhoods/_stats/__init__.py +0 -11
- risk/_neighborhoods/_stats/_permutation/__init__.py +0 -6
- risk/_neighborhoods/_stats/_permutation/_test_functions.py +0 -72
- risk/_network/__init__.py +0 -8
- risk/_network/_graph/__init__.py +0 -7
- risk/_network/_plotter/__init__.py +0 -6
- risk/_network/_plotter/_utils/__init__.py +0 -7
- risk_network-0.0.16b0.dist-info/RECORD +0 -41
- {risk_network-0.0.16b0.dist-info → risk_network-0.0.16b2.dist-info}/WHEEL +0 -0
- {risk_network-0.0.16b0.dist-info → risk_network-0.0.16b2.dist-info}/licenses/LICENSE +0 -0
- {risk_network-0.0.16b0.dist-info → risk_network-0.0.16b2.dist-info}/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
|
-
risk/
|
|
3
|
-
|
|
2
|
+
risk/cluster/label
|
|
3
|
+
~~~~~~~~~~~~~~~~~~
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
from itertools import product
|
|
@@ -13,9 +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.
|
|
16
|
+
from risk.annotation import get_weighted_description
|
|
17
17
|
|
|
18
|
-
from ..
|
|
18
|
+
from ..log import logger
|
|
19
19
|
|
|
20
20
|
# Define constants for clustering
|
|
21
21
|
# fmt: off
|
|
@@ -30,7 +30,7 @@ LINKAGE_METRICS = {
|
|
|
30
30
|
|
|
31
31
|
def define_domains(
|
|
32
32
|
top_annotation: pd.DataFrame,
|
|
33
|
-
|
|
33
|
+
significant_clusters_significance: np.ndarray,
|
|
34
34
|
linkage_criterion: str,
|
|
35
35
|
linkage_method: str,
|
|
36
36
|
linkage_metric: str,
|
|
@@ -42,7 +42,7 @@ def define_domains(
|
|
|
42
42
|
|
|
43
43
|
Args:
|
|
44
44
|
top_annotation (pd.DataFrame): DataFrame of top annotations data for the network nodes.
|
|
45
|
-
|
|
45
|
+
significant_clusters_significance (np.ndarray): The binary significance matrix below alpha.
|
|
46
46
|
linkage_criterion (str): The clustering criterion for defining groups. Choose "off" to disable clustering.
|
|
47
47
|
linkage_method (str): The linkage method for clustering. Choose "auto" to optimize.
|
|
48
48
|
linkage_metric (str): The linkage metric for clustering. Choose "auto" to optimize.
|
|
@@ -66,11 +66,11 @@ def define_domains(
|
|
|
66
66
|
top_annotation["domain"] = range(1, n_rows + 1)
|
|
67
67
|
else:
|
|
68
68
|
# Transpose the matrix to cluster annotations
|
|
69
|
-
m =
|
|
69
|
+
m = significant_clusters_significance[:, top_annotation["significant_annotation"]].T
|
|
70
70
|
# Safeguard the matrix by replacing NaN, Inf, and -Inf values
|
|
71
71
|
m = _safeguard_matrix(m)
|
|
72
72
|
try:
|
|
73
|
-
# Optimize silhouette score across different linkage methods and
|
|
73
|
+
# Optimize silhouette score across different linkage methods and metrics
|
|
74
74
|
(
|
|
75
75
|
best_linkage,
|
|
76
76
|
best_metric,
|
|
@@ -99,7 +99,7 @@ def define_domains(
|
|
|
99
99
|
|
|
100
100
|
# Create DataFrames to store domain information
|
|
101
101
|
node_to_significance = pd.DataFrame(
|
|
102
|
-
data=
|
|
102
|
+
data=significant_clusters_significance,
|
|
103
103
|
columns=[top_annotation.index.values, top_annotation["domain"]],
|
|
104
104
|
)
|
|
105
105
|
node_to_domain = node_to_significance.T.groupby(level="domain").sum().T
|
|
@@ -152,9 +152,9 @@ def trim_domains(
|
|
|
152
152
|
top_annotation["domain"] = top_annotation["domain"].replace(to_remove, invalid_domain_id)
|
|
153
153
|
domains.loc[domains["primary_domain"].isin(to_remove), ["primary_domain"]] = invalid_domain_id
|
|
154
154
|
|
|
155
|
-
# Normalize "num significant
|
|
155
|
+
# Normalize "num significant clusters" by percentile for each domain and scale to 0-10
|
|
156
156
|
top_annotation["normalized_value"] = top_annotation.groupby("domain")[
|
|
157
|
-
"
|
|
157
|
+
"significant_cluster_significance_sums"
|
|
158
158
|
].transform(lambda x: (x.rank(pct=True) * 10).apply(np.ceil).astype(int))
|
|
159
159
|
# Modify the lambda function to pass both full_terms and significant_significance_score
|
|
160
160
|
top_annotation["combined_terms"] = top_annotation.apply(
|
|
@@ -245,6 +245,12 @@ def _safeguard_matrix(matrix: np.ndarray) -> np.ndarray:
|
|
|
245
245
|
Returns:
|
|
246
246
|
np.ndarray: Safeguarded data matrix.
|
|
247
247
|
"""
|
|
248
|
+
# Safety guard: handle empty or invalid matrices
|
|
249
|
+
if matrix.size == 0 or not np.isfinite(matrix).any():
|
|
250
|
+
logger.warning(
|
|
251
|
+
"Input matrix is empty or contains no finite values. Returning a zero matrix of same shape."
|
|
252
|
+
)
|
|
253
|
+
return np.zeros(matrix.shape, dtype=float)
|
|
248
254
|
# Replace NaN with column mean
|
|
249
255
|
nan_replacement = np.nanmean(matrix, axis=0)
|
|
250
256
|
matrix = np.where(np.isnan(matrix), nan_replacement, matrix)
|
|
@@ -267,7 +273,7 @@ def _optimize_silhouette_across_linkage_and_metrics(
|
|
|
267
273
|
linkage_threshold: Union[str, float],
|
|
268
274
|
) -> Tuple[str, str, float]:
|
|
269
275
|
"""
|
|
270
|
-
Optimize silhouette score across different linkage methods and
|
|
276
|
+
Optimize silhouette score across different linkage methods and metrics.
|
|
271
277
|
|
|
272
278
|
Args:
|
|
273
279
|
m (np.ndarray): Data matrix.
|
risk/{_log → log}/__init__.py
RENAMED
|
@@ -3,8 +3,8 @@ risk/_log
|
|
|
3
3
|
~~~~~~~~~
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
|
-
from .
|
|
7
|
-
from .
|
|
6
|
+
from .console import log_header, logger, set_global_verbosity
|
|
7
|
+
from .parameters import Params
|
|
8
8
|
|
|
9
9
|
# Initialize the global parameters logger
|
|
10
10
|
params = Params()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
|
-
risk/
|
|
3
|
-
|
|
2
|
+
risk/log/parameters
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
import csv
|
|
@@ -11,7 +11,7 @@ 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")
|
|
@@ -22,7 +22,7 @@ class Params:
|
|
|
22
22
|
Handles the storage and logging of various parameters for network analysis.
|
|
23
23
|
|
|
24
24
|
The Params class provides methods to log parameters related to different components of the analysis,
|
|
25
|
-
such as the network, annotation,
|
|
25
|
+
such as the network, annotation, clusters, graph, and plotter settings. It also stores
|
|
26
26
|
the current datetime when the parameters were initialized.
|
|
27
27
|
"""
|
|
28
28
|
|
|
@@ -35,7 +35,8 @@ class Params:
|
|
|
35
35
|
"""Initialize the parameter dictionaries for different components."""
|
|
36
36
|
self.network = {}
|
|
37
37
|
self.annotation = {}
|
|
38
|
-
self.
|
|
38
|
+
self.clusters = {}
|
|
39
|
+
self.stats = {}
|
|
39
40
|
self.graph = {}
|
|
40
41
|
self.plotter = {}
|
|
41
42
|
|
|
@@ -57,14 +58,23 @@ class Params:
|
|
|
57
58
|
"""
|
|
58
59
|
self.annotation = {**self.annotation, **kwargs}
|
|
59
60
|
|
|
60
|
-
def
|
|
61
|
+
def log_clusters(self, **kwargs) -> None:
|
|
61
62
|
"""
|
|
62
|
-
Log
|
|
63
|
+
Log cluster-related parameters.
|
|
63
64
|
|
|
64
65
|
Args:
|
|
65
|
-
**kwargs:
|
|
66
|
+
**kwargs: Cluster parameters to log.
|
|
66
67
|
"""
|
|
67
|
-
self.
|
|
68
|
+
self.clusters = {**self.clusters, **kwargs}
|
|
69
|
+
|
|
70
|
+
def log_stats(self, **kwargs) -> None:
|
|
71
|
+
"""
|
|
72
|
+
Log statistical test-related parameters.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
**kwargs: Statistical test parameters to log.
|
|
76
|
+
"""
|
|
77
|
+
self.stats = {**self.stats, **kwargs}
|
|
68
78
|
|
|
69
79
|
def log_graph(self, **kwargs) -> None:
|
|
70
80
|
"""
|
|
@@ -152,7 +162,7 @@ class Params:
|
|
|
152
162
|
"annotation": self.annotation,
|
|
153
163
|
"datetime": self.datetime,
|
|
154
164
|
"graph": self.graph,
|
|
155
|
-
"
|
|
165
|
+
"clusters": self.clusters,
|
|
156
166
|
"network": self.network,
|
|
157
167
|
"plotter": self.plotter,
|
|
158
168
|
}
|
risk/network/__init__.py
ADDED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
|
-
risk/
|
|
3
|
-
|
|
2
|
+
risk/network/graph/_summary
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
from typing import Any, Dict, Tuple, Union
|
|
@@ -9,35 +9,35 @@ import numpy as np
|
|
|
9
9
|
import pandas as pd
|
|
10
10
|
from statsmodels.stats.multitest import fdrcorrection
|
|
11
11
|
|
|
12
|
-
from ...
|
|
12
|
+
from ...log import log_header, logger
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
class Summary:
|
|
16
16
|
"""
|
|
17
17
|
Handles the processing, storage, and export of network analysis results.
|
|
18
18
|
|
|
19
|
-
The
|
|
20
|
-
FDR-corrected q-values, and structure information on domains and
|
|
21
|
-
DataFrame. It also offers functionality to export the
|
|
22
|
-
and text formats for analysis and reporting.
|
|
19
|
+
The Summary class provides methods to process significance and depletion data,
|
|
20
|
+
compute FDR-corrected q-values, and structure information on domains and
|
|
21
|
+
annotations into a DataFrame. It also offers functionality to export the
|
|
22
|
+
processed data in CSV, JSON, and text formats for analysis and reporting.
|
|
23
23
|
"""
|
|
24
24
|
|
|
25
25
|
def __init__(
|
|
26
26
|
self,
|
|
27
27
|
annotation: Dict[str, Any],
|
|
28
|
-
|
|
28
|
+
stats_results: Dict[str, Any],
|
|
29
29
|
graph, # Avoid type hinting Graph to prevent circular imports
|
|
30
30
|
):
|
|
31
31
|
"""
|
|
32
|
-
Initialize the
|
|
32
|
+
Initialize the Summary object with analysis components.
|
|
33
33
|
|
|
34
34
|
Args:
|
|
35
35
|
annotation (Dict[str, Any]): Annotation data, including ordered annotations and matrix of associations.
|
|
36
|
-
|
|
36
|
+
stats_results (Dict[str, Any]): Cluster data containing p-values for significance and depletion analysis.
|
|
37
37
|
graph (Graph): Graph object representing domain-to-node and node-to-label mappings.
|
|
38
38
|
"""
|
|
39
39
|
self.annotation = annotation
|
|
40
|
-
self.
|
|
40
|
+
self.stats_results = stats_results
|
|
41
41
|
self.graph = graph
|
|
42
42
|
|
|
43
43
|
def to_csv(self, filepath: str) -> None:
|
|
@@ -88,8 +88,8 @@ class Summary:
|
|
|
88
88
|
"""
|
|
89
89
|
log_header("Loading analysis summary")
|
|
90
90
|
# Calculate significance and depletion q-values from p-value matrices in annotation
|
|
91
|
-
enrichment_pvals = self.
|
|
92
|
-
depletion_pvals = self.
|
|
91
|
+
enrichment_pvals = self.stats_results["enrichment_pvals"]
|
|
92
|
+
depletion_pvals = self.stats_results["depletion_pvals"]
|
|
93
93
|
enrichment_qvals = self._calculate_qvalues(enrichment_pvals)
|
|
94
94
|
depletion_qvals = self._calculate_qvalues(depletion_pvals)
|
|
95
95
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
|
-
risk/
|
|
3
|
-
|
|
2
|
+
risk/network/graph/api
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
import copy
|
|
@@ -9,14 +9,14 @@ from typing import Any, Dict, Union
|
|
|
9
9
|
import networkx as nx
|
|
10
10
|
import pandas as pd
|
|
11
11
|
|
|
12
|
-
from ...
|
|
13
|
-
from ...
|
|
14
|
-
from ...
|
|
12
|
+
from ...annotation import define_top_annotation
|
|
13
|
+
from ...log import log_header, logger, params
|
|
14
|
+
from ...cluster import (
|
|
15
15
|
define_domains,
|
|
16
|
-
|
|
16
|
+
process_significant_clusters,
|
|
17
17
|
trim_domains,
|
|
18
18
|
)
|
|
19
|
-
from .
|
|
19
|
+
from .graph import Graph
|
|
20
20
|
from ._stats import calculate_significance_matrices
|
|
21
21
|
|
|
22
22
|
|
|
@@ -24,14 +24,14 @@ class GraphAPI:
|
|
|
24
24
|
"""
|
|
25
25
|
Handles the loading of network graphs and associated data.
|
|
26
26
|
|
|
27
|
-
The GraphAPI class provides methods to load and process network graphs, annotations, and
|
|
27
|
+
The GraphAPI class provides methods to load and process network graphs, annotations, and cluster results.
|
|
28
28
|
"""
|
|
29
29
|
|
|
30
30
|
def load_graph(
|
|
31
31
|
self,
|
|
32
32
|
network: nx.Graph,
|
|
33
33
|
annotation: Dict[str, Any],
|
|
34
|
-
|
|
34
|
+
stats_results: Dict[str, Any],
|
|
35
35
|
tail: str = "right",
|
|
36
36
|
pval_cutoff: float = 0.01,
|
|
37
37
|
fdr_cutoff: float = 0.9999,
|
|
@@ -50,7 +50,7 @@ class GraphAPI:
|
|
|
50
50
|
Args:
|
|
51
51
|
network (nx.Graph): The network graph.
|
|
52
52
|
annotation (Dict[str, Any]): The annotation associated with the network.
|
|
53
|
-
|
|
53
|
+
stats_results (Dict[str, Any]): Cluster significance data.
|
|
54
54
|
tail (str, optional): Type of significance tail ("right", "left", "both"). Defaults to "right".
|
|
55
55
|
pval_cutoff (float, optional): p-value cutoff for significance. Defaults to 0.01.
|
|
56
56
|
fdr_cutoff (float, optional): FDR cutoff for significance. Defaults to 0.9999.
|
|
@@ -62,14 +62,14 @@ class GraphAPI:
|
|
|
62
62
|
Defaults to "yule".
|
|
63
63
|
linkage_threshold (float, str, optional): Threshold for clustering. Choose "auto" to optimize.
|
|
64
64
|
Defaults to 0.2.
|
|
65
|
-
min_cluster_size (int, optional): Minimum size for clusters. Defaults to 5.
|
|
66
|
-
max_cluster_size (int, optional): Maximum size for clusters. Defaults to 1000.
|
|
65
|
+
min_cluster_size (int, optional): Minimum size for significant clusters. Defaults to 5.
|
|
66
|
+
max_cluster_size (int, optional): Maximum size for significant clusters. Defaults to 1000.
|
|
67
67
|
|
|
68
68
|
Returns:
|
|
69
69
|
Graph: A fully initialized and processed Graph object.
|
|
70
70
|
"""
|
|
71
71
|
# Log the parameters and display headers
|
|
72
|
-
log_header("Finding significant
|
|
72
|
+
log_header("Finding significant clusters")
|
|
73
73
|
params.log_graph(
|
|
74
74
|
tail=tail,
|
|
75
75
|
pval_cutoff=pval_cutoff,
|
|
@@ -92,20 +92,20 @@ class GraphAPI:
|
|
|
92
92
|
logger.debug(
|
|
93
93
|
f"Significance tail: '{tail}' ({'enrichment' if tail == 'right' else 'depletion' if tail == 'left' else 'both'})"
|
|
94
94
|
)
|
|
95
|
-
# Calculate significant
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
95
|
+
# Calculate significant clusters based on the provided parameters
|
|
96
|
+
significant_clusters = calculate_significance_matrices(
|
|
97
|
+
stats_results["depletion_pvals"],
|
|
98
|
+
stats_results["enrichment_pvals"],
|
|
99
99
|
tail=tail,
|
|
100
100
|
pval_cutoff=pval_cutoff,
|
|
101
101
|
fdr_cutoff=fdr_cutoff,
|
|
102
102
|
)
|
|
103
103
|
|
|
104
|
-
log_header("Processing
|
|
105
|
-
# Process
|
|
106
|
-
|
|
104
|
+
log_header("Processing significant clusters")
|
|
105
|
+
# Process significant clusters by imputing and pruning based on the given settings
|
|
106
|
+
processed_clusters = process_significant_clusters(
|
|
107
107
|
network=network,
|
|
108
|
-
|
|
108
|
+
significant_clusters=significant_clusters,
|
|
109
109
|
impute_depth=impute_depth,
|
|
110
110
|
prune_threshold=prune_threshold,
|
|
111
111
|
)
|
|
@@ -113,24 +113,22 @@ class GraphAPI:
|
|
|
113
113
|
log_header("Finding top annotations")
|
|
114
114
|
logger.debug(f"Min cluster size: {min_cluster_size}")
|
|
115
115
|
logger.debug(f"Max cluster size: {max_cluster_size}")
|
|
116
|
-
# Define top annotations based on processed
|
|
116
|
+
# Define top annotations based on processed significant clusters
|
|
117
117
|
top_annotation = self._define_top_annotation(
|
|
118
118
|
network=network,
|
|
119
119
|
annotation=annotation,
|
|
120
|
-
|
|
120
|
+
processed_clusters=processed_clusters,
|
|
121
121
|
min_cluster_size=min_cluster_size,
|
|
122
122
|
max_cluster_size=max_cluster_size,
|
|
123
123
|
)
|
|
124
124
|
|
|
125
|
-
log_header("
|
|
126
|
-
# Extract the significant significance matrix from the
|
|
127
|
-
|
|
128
|
-
"significant_significance_matrix"
|
|
129
|
-
]
|
|
125
|
+
log_header("Grouping clusters into domains")
|
|
126
|
+
# Extract the significant significance matrix from the processed_clusters data
|
|
127
|
+
significant_clusters_significance = processed_clusters["significant_significance_matrix"]
|
|
130
128
|
# Define domains in the network using the specified clustering settings
|
|
131
129
|
domains = define_domains(
|
|
132
130
|
top_annotation=top_annotation,
|
|
133
|
-
|
|
131
|
+
significant_clusters_significance=significant_clusters_significance,
|
|
134
132
|
linkage_criterion=linkage_criterion,
|
|
135
133
|
linkage_method=linkage_method,
|
|
136
134
|
linkage_metric=linkage_metric,
|
|
@@ -147,13 +145,13 @@ class GraphAPI:
|
|
|
147
145
|
# Prepare node mapping and significance sums for the final Graph object
|
|
148
146
|
ordered_nodes = annotation["ordered_nodes"]
|
|
149
147
|
node_label_to_id = dict(zip(ordered_nodes, range(len(ordered_nodes))))
|
|
150
|
-
node_significance_sums =
|
|
148
|
+
node_significance_sums = processed_clusters["node_significance_sums"]
|
|
151
149
|
|
|
152
150
|
# Return the fully initialized Graph object
|
|
153
151
|
return Graph(
|
|
154
152
|
network=network,
|
|
155
153
|
annotation=annotation,
|
|
156
|
-
|
|
154
|
+
stats_results=stats_results,
|
|
157
155
|
domains=domains,
|
|
158
156
|
trimmed_domains=trimmed_domains,
|
|
159
157
|
node_label_to_node_id_map=node_label_to_id,
|
|
@@ -164,7 +162,7 @@ class GraphAPI:
|
|
|
164
162
|
self,
|
|
165
163
|
network: nx.Graph,
|
|
166
164
|
annotation: Dict[str, Any],
|
|
167
|
-
|
|
165
|
+
processed_clusters: Dict[str, Any],
|
|
168
166
|
min_cluster_size: int = 5,
|
|
169
167
|
max_cluster_size: int = 1000,
|
|
170
168
|
) -> pd.DataFrame:
|
|
@@ -174,25 +172,25 @@ class GraphAPI:
|
|
|
174
172
|
Args:
|
|
175
173
|
network (nx.Graph): The network graph.
|
|
176
174
|
annotation (Dict[str, Any]): Annotation data for the network.
|
|
177
|
-
|
|
175
|
+
processed_clusters (Dict[str, Any]): Processed cluster significance data.
|
|
178
176
|
min_cluster_size (int, optional): Minimum size for clusters. Defaults to 5.
|
|
179
177
|
max_cluster_size (int, optional): Maximum size for clusters. Defaults to 1000.
|
|
180
178
|
|
|
181
179
|
Returns:
|
|
182
|
-
|
|
180
|
+
pd.DataFrame: Top annotations identified within the network.
|
|
183
181
|
"""
|
|
184
|
-
# Extract necessary data from annotation and
|
|
182
|
+
# Extract necessary data from annotation and processed_clusters
|
|
185
183
|
ordered_annotation = annotation["ordered_annotation"]
|
|
186
|
-
|
|
187
|
-
significant_significance_matrix =
|
|
188
|
-
significant_binary_significance_matrix =
|
|
184
|
+
cluster_significance_sums = processed_clusters["cluster_significance_counts"]
|
|
185
|
+
significant_significance_matrix = processed_clusters["significant_significance_matrix"]
|
|
186
|
+
significant_binary_significance_matrix = processed_clusters[
|
|
189
187
|
"significant_binary_significance_matrix"
|
|
190
188
|
]
|
|
191
189
|
# Call external function to define top annotations
|
|
192
190
|
return define_top_annotation(
|
|
193
191
|
network=network,
|
|
194
192
|
ordered_annotation_labels=ordered_annotation,
|
|
195
|
-
|
|
193
|
+
cluster_significance_sums=cluster_significance_sums,
|
|
196
194
|
significant_significance_matrix=significant_significance_matrix,
|
|
197
195
|
significant_binary_significance_matrix=significant_binary_significance_matrix,
|
|
198
196
|
min_cluster_size=min_cluster_size,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
|
-
risk/
|
|
3
|
-
|
|
2
|
+
risk/network/graph/graph
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
from collections import defaultdict
|
|
@@ -27,7 +27,7 @@ class Graph:
|
|
|
27
27
|
self,
|
|
28
28
|
network: nx.Graph,
|
|
29
29
|
annotation: Dict[str, Any],
|
|
30
|
-
|
|
30
|
+
stats_results: Dict[str, Any],
|
|
31
31
|
domains: pd.DataFrame,
|
|
32
32
|
trimmed_domains: pd.DataFrame,
|
|
33
33
|
node_label_to_node_id_map: Dict[str, Any],
|
|
@@ -40,7 +40,7 @@ class Graph:
|
|
|
40
40
|
Args:
|
|
41
41
|
network (nx.Graph): The network graph.
|
|
42
42
|
annotation (Dict[str, Any]): The annotation associated with the network.
|
|
43
|
-
|
|
43
|
+
stats_results (Dict[str, Any]): Cluster significance data.
|
|
44
44
|
domains (pd.DataFrame): DataFrame containing domain data for the network nodes.
|
|
45
45
|
trimmed_domains (pd.DataFrame): DataFrame containing trimmed domain data for the network nodes.
|
|
46
46
|
node_label_to_node_id_map (Dict[str, Any]): A dictionary mapping node labels to their corresponding IDs.
|
|
@@ -72,7 +72,7 @@ class Graph:
|
|
|
72
72
|
self.node_coordinates = self._extract_node_coordinates(self.network)
|
|
73
73
|
|
|
74
74
|
# NOTE: Only after the above attributes are initialized, we can create the summary
|
|
75
|
-
self.summary = Summary(annotation,
|
|
75
|
+
self.summary = Summary(annotation, stats_results, self)
|
|
76
76
|
|
|
77
77
|
def pop(self, domain_id: int) -> List[str]:
|
|
78
78
|
"""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
|
-
risk/
|
|
3
|
-
|
|
2
|
+
risk/network/io
|
|
3
|
+
~~~~~~~~~~~~~~~
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
import copy
|
|
@@ -15,7 +15,7 @@ import networkx as nx
|
|
|
15
15
|
import numpy as np
|
|
16
16
|
import pandas as pd
|
|
17
17
|
|
|
18
|
-
from ..
|
|
18
|
+
from ..log import log_header, logger, params
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
class NetworkAPI:
|
|
@@ -370,7 +370,7 @@ class NetworkIO:
|
|
|
370
370
|
self._log_loading_network(filetype, filepath=filepath)
|
|
371
371
|
|
|
372
372
|
# Load the Cytoscape JSON file
|
|
373
|
-
with open(filepath, "r") as f:
|
|
373
|
+
with open(filepath, "r", encoding="utf-8") as f:
|
|
374
374
|
cyjs_data = json.load(f)
|
|
375
375
|
|
|
376
376
|
# Create a graph
|
|
@@ -603,6 +603,11 @@ class NetworkIO:
|
|
|
603
603
|
distances = compute_distance_vectorized(edge_data, compute_sphere)
|
|
604
604
|
# Assign Euclidean or spherical distances to edges
|
|
605
605
|
for (u, v), distance in zip(G.edges, distances):
|
|
606
|
+
if not np.isfinite(distance) or distance <= 0:
|
|
607
|
+
logger.warning(
|
|
608
|
+
f"Edge ({u},{v}) has invalid or non-positive length ({distance}); replaced with minimal fallback 1e-12."
|
|
609
|
+
)
|
|
610
|
+
distance = 1e-12
|
|
606
611
|
G.edges[u, v]["length"] = distance
|
|
607
612
|
|
|
608
613
|
def _map_to_sphere(self, G: nx.Graph) -> None:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
|
-
risk/
|
|
3
|
-
|
|
2
|
+
risk/network/plotter/_canvas
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
from typing import List, Tuple, Union
|
|
@@ -8,10 +8,10 @@ from typing import List, Tuple, Union
|
|
|
8
8
|
import matplotlib.pyplot as plt
|
|
9
9
|
import numpy as np
|
|
10
10
|
|
|
11
|
-
from ...
|
|
12
|
-
from ..
|
|
13
|
-
from ._utils.
|
|
14
|
-
from ._utils.
|
|
11
|
+
from ...log import params
|
|
12
|
+
from ..graph import Graph
|
|
13
|
+
from ._utils.colors import to_rgba
|
|
14
|
+
from ._utils.layout import calculate_bounding_box
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
class Canvas:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
|
-
risk/
|
|
3
|
-
|
|
2
|
+
risk/network/plotter/_contour
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
from typing import Any, Dict, List, Tuple, Union
|
|
@@ -11,8 +11,8 @@ from scipy import linalg
|
|
|
11
11
|
from scipy.ndimage import label
|
|
12
12
|
from scipy.stats import gaussian_kde
|
|
13
13
|
|
|
14
|
-
from ...
|
|
15
|
-
from ..
|
|
14
|
+
from ...log import logger, params
|
|
15
|
+
from ..graph import Graph
|
|
16
16
|
from ._utils import get_annotated_domain_colors, to_rgba
|
|
17
17
|
|
|
18
18
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
|
-
risk/
|
|
3
|
-
|
|
2
|
+
risk/network/plotter/_labels
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
import copy
|
|
@@ -10,8 +10,8 @@ import matplotlib.pyplot as plt
|
|
|
10
10
|
import numpy as np
|
|
11
11
|
import pandas as pd
|
|
12
12
|
|
|
13
|
-
from ...
|
|
14
|
-
from ..
|
|
13
|
+
from ...log import params
|
|
14
|
+
from ..graph import Graph
|
|
15
15
|
from ._utils import calculate_bounding_box, get_annotated_domain_colors, to_rgba
|
|
16
16
|
|
|
17
17
|
TERM_DELIMITER = "::::" # String used to separate multiple domain terms when constructing composite domain labels
|
|
@@ -275,12 +275,12 @@ class Labels:
|
|
|
275
275
|
fontsize (int, optional): Font size for the label. Defaults to 10.
|
|
276
276
|
fontcolor (str, List, Tuple, or np.ndarray, optional): Color of the label text. Defaults to "black".
|
|
277
277
|
fontalpha (float, None, optional): Transparency level for the font color. If provided, it overrides any existing alpha values found
|
|
278
|
-
in
|
|
278
|
+
in fontcolor. Defaults to 1.0.
|
|
279
279
|
arrow_linewidth (float, optional): Line width of the arrow pointing to the centroid. Defaults to 1.
|
|
280
280
|
arrow_style (str, optional): Style of the arrows pointing to the centroid. Defaults to "->".
|
|
281
281
|
arrow_color (str, List, Tuple, or np.ndarray, optional): Color of the arrow. Defaults to "black".
|
|
282
282
|
arrow_alpha (float, None, optional): Transparency level for the arrow color. If provided, it overrides any existing alpha values
|
|
283
|
-
found in
|
|
283
|
+
found in arrow_color. Defaults to 1.0.
|
|
284
284
|
arrow_base_shrink (float, optional): Distance between the text and the base of the arrow. Defaults to 0.0.
|
|
285
285
|
arrow_tip_shrink (float, optional): Distance between the arrow tip and the centroid. Defaults to 0.0.
|
|
286
286
|
|