risk-network 0.0.13b4__py3-none-any.whl → 0.0.14b0__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} +18 -11
- risk/{annotation/io.py → _annotation/_io.py} +22 -14
- 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} +168 -126
- 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 → _risk.py} +11 -11
- {risk_network-0.0.13b4.dist-info → risk_network-0.0.14b0.dist-info}/METADATA +1 -1
- risk_network-0.0.14b0.dist-info/RECORD +41 -0
- {risk_network-0.0.13b4.dist-info → risk_network-0.0.14b0.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.13b4.dist-info/RECORD +0 -40
- {risk_network-0.0.13b4.dist-info → risk_network-0.0.14b0.dist-info}/licenses/LICENSE +0 -0
- {risk_network-0.0.13b4.dist-info → risk_network-0.0.14b0.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_network/_graph/_stats
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
from typing import Any, Dict, Union
|
@@ -16,7 +16,8 @@ def calculate_significance_matrices(
|
|
16
16
|
pval_cutoff: float = 0.05,
|
17
17
|
fdr_cutoff: float = 0.05,
|
18
18
|
) -> Dict[str, Any]:
|
19
|
-
"""
|
19
|
+
"""
|
20
|
+
Calculate significance matrices based on p-values and specified tail.
|
20
21
|
|
21
22
|
Args:
|
22
23
|
depletion_pvals (np.ndarray): Matrix of depletion p-values.
|
@@ -89,7 +90,8 @@ def _select_significance_matrices(
|
|
89
90
|
log_enrichment_matrix: np.ndarray,
|
90
91
|
enrichment_alpha_threshold_matrix: np.ndarray,
|
91
92
|
) -> tuple:
|
92
|
-
"""
|
93
|
+
"""
|
94
|
+
Select significance matrices based on the specified tail type.
|
93
95
|
|
94
96
|
Args:
|
95
97
|
tail (str): The tail type for significance selection. Options are 'left', 'right', or 'both'.
|
@@ -143,7 +145,8 @@ def _compute_threshold_matrix(
|
|
143
145
|
pval_cutoff: float = 0.05,
|
144
146
|
fdr_cutoff: float = 0.05,
|
145
147
|
) -> np.ndarray:
|
146
|
-
"""
|
148
|
+
"""
|
149
|
+
Compute a threshold matrix indicating significance based on p-value and FDR cutoffs.
|
147
150
|
|
148
151
|
Args:
|
149
152
|
pvals (np.ndarray): Array of p-values for statistical tests.
|
@@ -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,11 +9,12 @@ 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
|
+
Handles the processing, storage, and export of network analysis results.
|
17
18
|
|
18
19
|
The Results class provides methods to process significance and depletion data, compute
|
19
20
|
FDR-corrected q-values, and structure information on domains and annotations into a
|
@@ -27,7 +28,8 @@ class Summary:
|
|
27
28
|
neighborhoods: Dict[str, Any],
|
28
29
|
graph, # Avoid type hinting Graph to prevent circular imports
|
29
30
|
):
|
30
|
-
"""
|
31
|
+
"""
|
32
|
+
Initialize the Results object with analysis components.
|
31
33
|
|
32
34
|
Args:
|
33
35
|
annotation (Dict[str, Any]): Annotation data, including ordered annotations and matrix of associations.
|
@@ -39,7 +41,8 @@ class Summary:
|
|
39
41
|
self.graph = graph
|
40
42
|
|
41
43
|
def to_csv(self, filepath: str) -> None:
|
42
|
-
"""
|
44
|
+
"""
|
45
|
+
Export significance results to a CSV file.
|
43
46
|
|
44
47
|
Args:
|
45
48
|
filepath (str): The path where the CSV file will be saved.
|
@@ -50,7 +53,8 @@ class Summary:
|
|
50
53
|
logger.info(f"Analysis summary exported to CSV file: {filepath}")
|
51
54
|
|
52
55
|
def to_json(self, filepath: str) -> None:
|
53
|
-
"""
|
56
|
+
"""
|
57
|
+
Export significance results to a JSON file.
|
54
58
|
|
55
59
|
Args:
|
56
60
|
filepath (str): The path where the JSON file will be saved.
|
@@ -61,7 +65,8 @@ class Summary:
|
|
61
65
|
logger.info(f"Analysis summary exported to JSON file: {filepath}")
|
62
66
|
|
63
67
|
def to_txt(self, filepath: str) -> None:
|
64
|
-
"""
|
68
|
+
"""
|
69
|
+
Export significance results to a text file.
|
65
70
|
|
66
71
|
Args:
|
67
72
|
filepath (str): The path where the text file will be saved.
|
@@ -74,7 +79,8 @@ class Summary:
|
|
74
79
|
logger.info(f"Analysis summary exported to text file: {filepath}")
|
75
80
|
|
76
81
|
def load(self) -> pd.DataFrame:
|
77
|
-
"""
|
82
|
+
"""
|
83
|
+
Load and process domain and annotation data into a DataFrame with significance metrics.
|
78
84
|
|
79
85
|
Returns:
|
80
86
|
pd.DataFrame: Processed DataFrame containing significance scores, p-values, q-values,
|
@@ -171,7 +177,8 @@ class Summary:
|
|
171
177
|
return results
|
172
178
|
|
173
179
|
def _calculate_qvalues(self, pvals: np.ndarray) -> np.ndarray:
|
174
|
-
"""
|
180
|
+
"""
|
181
|
+
Calculate q-values (FDR) for each row of a p-value matrix.
|
175
182
|
|
176
183
|
Args:
|
177
184
|
pvals (np.ndarray): 2D array of p-values.
|
@@ -190,7 +197,8 @@ class Summary:
|
|
190
197
|
enrichment_qvals: np.ndarray,
|
191
198
|
depletion_qvals: np.ndarray,
|
192
199
|
) -> Tuple[Union[float, None], Union[float, None], Union[float, None], Union[float, None]]:
|
193
|
-
"""
|
200
|
+
"""
|
201
|
+
Retrieve the most significant p-values and q-values (FDR) for a given annotation.
|
194
202
|
|
195
203
|
Args:
|
196
204
|
domain_id (int): The domain ID associated with the annotation.
|
@@ -226,7 +234,8 @@ class Summary:
|
|
226
234
|
)
|
227
235
|
|
228
236
|
def _get_annotation_members(self, description: str) -> str:
|
229
|
-
"""
|
237
|
+
"""
|
238
|
+
Retrieve node labels associated with a given annotation description.
|
230
239
|
|
231
240
|
Args:
|
232
241
|
description (str): The annotation description.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_network/_io
|
3
|
+
~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
import copy
|
@@ -15,14 +15,13 @@ 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
|
-
class
|
22
|
-
"""
|
23
|
-
|
24
|
-
|
25
|
-
and process the network by adjusting node coordinates, calculating edge lengths, and validating graph structure.
|
21
|
+
class NetworkAPI:
|
22
|
+
"""
|
23
|
+
Public-facing interface for loading and initializing network data.
|
24
|
+
Delegates to the NetworkIO worker class for actual I/O and processing.
|
26
25
|
"""
|
27
26
|
|
28
27
|
def __init__(
|
@@ -31,22 +30,17 @@ class NetworkIO:
|
|
31
30
|
surface_depth: float = 0.0,
|
32
31
|
min_edges_per_node: int = 0,
|
33
32
|
):
|
34
|
-
"""
|
33
|
+
"""
|
34
|
+
Initialize the NetworkAPI.
|
35
35
|
|
36
36
|
Args:
|
37
|
-
compute_sphere (bool
|
38
|
-
surface_depth (float
|
39
|
-
min_edges_per_node (int
|
37
|
+
compute_sphere (bool): Whether to map nodes to a sphere. Defaults to True.
|
38
|
+
surface_depth (float): Surface depth for the sphere. Defaults to 0.0.
|
39
|
+
min_edges_per_node (int): Minimum number of edges per node. Defaults to 0.
|
40
40
|
"""
|
41
41
|
self.compute_sphere = compute_sphere
|
42
42
|
self.surface_depth = surface_depth
|
43
43
|
self.min_edges_per_node = min_edges_per_node
|
44
|
-
# Log the initialization of the NetworkIO class
|
45
|
-
params.log_network(
|
46
|
-
compute_sphere=compute_sphere,
|
47
|
-
surface_depth=surface_depth,
|
48
|
-
min_edges_per_node=min_edges_per_node,
|
49
|
-
)
|
50
44
|
|
51
45
|
def load_network_gpickle(
|
52
46
|
self,
|
@@ -55,43 +49,23 @@ class NetworkIO:
|
|
55
49
|
surface_depth: float = 0.0,
|
56
50
|
min_edges_per_node: int = 0,
|
57
51
|
) -> nx.Graph:
|
58
|
-
"""
|
52
|
+
"""
|
53
|
+
Load a network from a GPickle file via NetworkIO.
|
59
54
|
|
60
55
|
Args:
|
61
56
|
filepath (str): Path to the GPickle file.
|
62
|
-
compute_sphere (bool, optional):
|
63
|
-
surface_depth (float, optional):
|
64
|
-
min_edges_per_node (int, optional):
|
65
|
-
|
57
|
+
compute_sphere (bool, optional): Override or use API default. Defaults to True.
|
58
|
+
surface_depth (float, optional): Override or use API default. Defaults to 0.0.
|
59
|
+
min_edges_per_node (int, optional): Override or use API default. Defaults to 0.
|
66
60
|
Returns:
|
67
61
|
nx.Graph: Loaded and processed network.
|
68
62
|
"""
|
69
|
-
|
63
|
+
io = NetworkIO(
|
70
64
|
compute_sphere=compute_sphere,
|
71
65
|
surface_depth=surface_depth,
|
72
66
|
min_edges_per_node=min_edges_per_node,
|
73
67
|
)
|
74
|
-
return
|
75
|
-
|
76
|
-
def _load_network_gpickle(self, filepath: str) -> nx.Graph:
|
77
|
-
"""Private method to load a network from a GPickle file.
|
78
|
-
|
79
|
-
Args:
|
80
|
-
filepath (str): Path to the GPickle file.
|
81
|
-
|
82
|
-
Returns:
|
83
|
-
nx.Graph: Loaded and processed network.
|
84
|
-
"""
|
85
|
-
filetype = "GPickle"
|
86
|
-
# Log the loading of the GPickle file
|
87
|
-
params.log_network(filetype=filetype, filepath=filepath)
|
88
|
-
self._log_loading_network(filetype, filepath=filepath)
|
89
|
-
|
90
|
-
with open(filepath, "rb") as f:
|
91
|
-
G = pickle.load(f)
|
92
|
-
|
93
|
-
# Initialize the graph
|
94
|
-
return self._initialize_graph(G)
|
68
|
+
return io.load_network_gpickle(filepath=filepath)
|
95
69
|
|
96
70
|
def load_network_networkx(
|
97
71
|
self,
|
@@ -100,42 +74,23 @@ class NetworkIO:
|
|
100
74
|
surface_depth: float = 0.0,
|
101
75
|
min_edges_per_node: int = 0,
|
102
76
|
) -> nx.Graph:
|
103
|
-
"""
|
77
|
+
"""
|
78
|
+
Load a NetworkX graph via NetworkIO.
|
104
79
|
|
105
80
|
Args:
|
106
81
|
network (nx.Graph): A NetworkX graph object.
|
107
|
-
compute_sphere (bool, optional):
|
108
|
-
surface_depth (float, optional):
|
109
|
-
min_edges_per_node (int, optional):
|
110
|
-
|
82
|
+
compute_sphere (bool, optional): Override or use API default. Defaults to True.
|
83
|
+
surface_depth (float, optional): Override or use API default. Defaults to 0.0.
|
84
|
+
min_edges_per_node (int, optional): Override or use API default. Defaults to 0.
|
111
85
|
Returns:
|
112
|
-
nx.Graph:
|
86
|
+
nx.Graph: Processed network.
|
113
87
|
"""
|
114
|
-
|
88
|
+
io = NetworkIO(
|
115
89
|
compute_sphere=compute_sphere,
|
116
90
|
surface_depth=surface_depth,
|
117
91
|
min_edges_per_node=min_edges_per_node,
|
118
92
|
)
|
119
|
-
return
|
120
|
-
|
121
|
-
def _load_network_networkx(self, network: nx.Graph) -> nx.Graph:
|
122
|
-
"""Private method to load a NetworkX graph.
|
123
|
-
|
124
|
-
Args:
|
125
|
-
network (nx.Graph): A NetworkX graph object.
|
126
|
-
|
127
|
-
Returns:
|
128
|
-
nx.Graph: Processed network.
|
129
|
-
"""
|
130
|
-
filetype = "NetworkX"
|
131
|
-
# Log the loading of the NetworkX graph
|
132
|
-
params.log_network(filetype=filetype)
|
133
|
-
self._log_loading_network(filetype)
|
134
|
-
|
135
|
-
# Important: Make a copy of the network to avoid modifying the original
|
136
|
-
network_copy = copy.deepcopy(network)
|
137
|
-
# Initialize the graph
|
138
|
-
return self._initialize_graph(network_copy)
|
93
|
+
return io.load_network_networkx(network=network)
|
139
94
|
|
140
95
|
def load_network_cytoscape(
|
141
96
|
self,
|
@@ -147,40 +102,148 @@ class NetworkIO:
|
|
147
102
|
surface_depth: float = 0.0,
|
148
103
|
min_edges_per_node: int = 0,
|
149
104
|
) -> nx.Graph:
|
150
|
-
"""
|
105
|
+
"""
|
106
|
+
Load a network from a Cytoscape file via NetworkIO.
|
151
107
|
|
152
108
|
Args:
|
153
109
|
filepath (str): Path to the Cytoscape file.
|
154
110
|
source_label (str, optional): Source node label. Defaults to "source".
|
155
111
|
target_label (str, optional): Target node label. Defaults to "target".
|
156
112
|
view_name (str, optional): Specific view name to load. Defaults to "".
|
157
|
-
compute_sphere (bool, optional):
|
158
|
-
surface_depth (float, optional):
|
159
|
-
min_edges_per_node (int, optional):
|
160
|
-
|
113
|
+
compute_sphere (bool, optional): Override or use API default. Defaults to True.
|
114
|
+
surface_depth (float, optional): Override or use API default. Defaults to 0.0.
|
115
|
+
min_edges_per_node (int, optional): Override or use API default. Defaults to 0.
|
161
116
|
Returns:
|
162
117
|
nx.Graph: Loaded and processed network.
|
163
118
|
"""
|
164
|
-
|
119
|
+
io = NetworkIO(
|
165
120
|
compute_sphere=compute_sphere,
|
166
121
|
surface_depth=surface_depth,
|
167
122
|
min_edges_per_node=min_edges_per_node,
|
168
123
|
)
|
169
|
-
return
|
124
|
+
return io.load_network_cytoscape(
|
170
125
|
filepath=filepath,
|
171
126
|
source_label=source_label,
|
172
127
|
target_label=target_label,
|
173
128
|
view_name=view_name,
|
174
129
|
)
|
175
130
|
|
176
|
-
def
|
131
|
+
def load_network_cyjs(
|
132
|
+
self,
|
133
|
+
filepath: str,
|
134
|
+
source_label: str = "source",
|
135
|
+
target_label: str = "target",
|
136
|
+
compute_sphere: bool = True,
|
137
|
+
surface_depth: float = 0.0,
|
138
|
+
min_edges_per_node: int = 0,
|
139
|
+
) -> nx.Graph:
|
140
|
+
"""
|
141
|
+
Load a network from a Cytoscape JSON (.cyjs) file via NetworkIO.
|
142
|
+
|
143
|
+
Args:
|
144
|
+
filepath (str): Path to the Cytoscape JSON file.
|
145
|
+
source_label (str, optional): Source node label. Defaults to "source".
|
146
|
+
target_label (str, optional): Target node label. Defaults to "target".
|
147
|
+
compute_sphere (bool, optional): Override or use API default. Defaults to True.
|
148
|
+
surface_depth (float, optional): Override or use API default. Defaults to 0.0.
|
149
|
+
min_edges_per_node (int, optional): Override or use API default. Defaults to 0.
|
150
|
+
Returns:
|
151
|
+
nx.Graph: Loaded and processed network.
|
152
|
+
"""
|
153
|
+
io = NetworkIO(
|
154
|
+
compute_sphere=compute_sphere,
|
155
|
+
surface_depth=surface_depth,
|
156
|
+
min_edges_per_node=min_edges_per_node,
|
157
|
+
)
|
158
|
+
return io.load_network_cyjs(
|
159
|
+
filepath=filepath,
|
160
|
+
source_label=source_label,
|
161
|
+
target_label=target_label,
|
162
|
+
)
|
163
|
+
|
164
|
+
|
165
|
+
class NetworkIO:
|
166
|
+
"""
|
167
|
+
A class for loading, processing, and managing network data.
|
168
|
+
|
169
|
+
The NetworkIO class provides methods to load network data from various formats (e.g., GPickle, NetworkX)
|
170
|
+
and process the network by adjusting node coordinates, calculating edge lengths, and validating graph structure.
|
171
|
+
"""
|
172
|
+
|
173
|
+
def __init__(
|
174
|
+
self,
|
175
|
+
compute_sphere: bool = True,
|
176
|
+
surface_depth: float = 0.0,
|
177
|
+
min_edges_per_node: int = 0,
|
178
|
+
):
|
179
|
+
"""
|
180
|
+
Initialize the NetworkIO class.
|
181
|
+
|
182
|
+
Args:
|
183
|
+
compute_sphere (bool, optional): Whether to map nodes to a sphere. Defaults to True.
|
184
|
+
surface_depth (float, optional): Surface depth for the sphere. Defaults to 0.0.
|
185
|
+
min_edges_per_node (int, optional): Minimum number of edges per node. Defaults to 0.
|
186
|
+
"""
|
187
|
+
self.compute_sphere = compute_sphere
|
188
|
+
self.surface_depth = surface_depth
|
189
|
+
self.min_edges_per_node = min_edges_per_node
|
190
|
+
# Log the initialization of the NetworkIO class
|
191
|
+
params.log_network(
|
192
|
+
compute_sphere=compute_sphere,
|
193
|
+
surface_depth=surface_depth,
|
194
|
+
min_edges_per_node=min_edges_per_node,
|
195
|
+
)
|
196
|
+
|
197
|
+
def load_network_gpickle(self, filepath: str) -> nx.Graph:
|
198
|
+
"""
|
199
|
+
Load a network from a GPickle file.
|
200
|
+
|
201
|
+
Args:
|
202
|
+
filepath (str): Path to the GPickle file.
|
203
|
+
|
204
|
+
Returns:
|
205
|
+
nx.Graph: Loaded and processed network.
|
206
|
+
"""
|
207
|
+
filetype = "GPickle"
|
208
|
+
# Log the loading of the GPickle file
|
209
|
+
params.log_network(filetype=filetype, filepath=filepath)
|
210
|
+
self._log_loading_network(filetype, filepath=filepath)
|
211
|
+
|
212
|
+
with open(filepath, "rb") as f:
|
213
|
+
G = pickle.load(f)
|
214
|
+
|
215
|
+
# Initialize the graph
|
216
|
+
return self._initialize_graph(G)
|
217
|
+
|
218
|
+
def load_network_networkx(self, network: nx.Graph) -> nx.Graph:
|
219
|
+
"""
|
220
|
+
Load a NetworkX graph.
|
221
|
+
|
222
|
+
Args:
|
223
|
+
network (nx.Graph): A NetworkX graph object.
|
224
|
+
|
225
|
+
Returns:
|
226
|
+
nx.Graph: Processed network.
|
227
|
+
"""
|
228
|
+
filetype = "NetworkX"
|
229
|
+
# Log the loading of the NetworkX graph
|
230
|
+
params.log_network(filetype=filetype)
|
231
|
+
self._log_loading_network(filetype)
|
232
|
+
|
233
|
+
# Important: Make a copy of the network to avoid modifying the original
|
234
|
+
network_copy = copy.deepcopy(network)
|
235
|
+
# Initialize the graph
|
236
|
+
return self._initialize_graph(network_copy)
|
237
|
+
|
238
|
+
def load_network_cytoscape(
|
177
239
|
self,
|
178
240
|
filepath: str,
|
179
241
|
source_label: str = "source",
|
180
242
|
target_label: str = "target",
|
181
243
|
view_name: str = "",
|
182
244
|
) -> nx.Graph:
|
183
|
-
"""
|
245
|
+
"""
|
246
|
+
Load a network from a Cytoscape file.
|
184
247
|
|
185
248
|
Args:
|
186
249
|
filepath (str): Path to the Cytoscape file.
|
@@ -307,41 +370,9 @@ class NetworkIO:
|
|
307
370
|
if os.path.exists(tmp_dir):
|
308
371
|
shutil.rmtree(tmp_dir)
|
309
372
|
|
310
|
-
def load_network_cyjs(
|
311
|
-
self,
|
312
|
-
filepath: str,
|
313
|
-
source_label: str = "source",
|
314
|
-
target_label: str = "target",
|
315
|
-
compute_sphere: bool = True,
|
316
|
-
surface_depth: float = 0.0,
|
317
|
-
min_edges_per_node: int = 0,
|
318
|
-
) -> nx.Graph:
|
319
|
-
"""Load a network from a Cytoscape JSON (.cyjs) file.
|
320
|
-
|
321
|
-
Args:
|
322
|
-
filepath (str): Path to the Cytoscape JSON file.
|
323
|
-
source_label (str, optional): Source node label. Default is "source".
|
324
|
-
target_label (str, optional): Target node label. Default is "target".
|
325
|
-
compute_sphere (bool, optional): Whether to map nodes to a sphere. Defaults to True.
|
326
|
-
surface_depth (float, optional): Surface depth for the sphere. Defaults to 0.0.
|
327
|
-
min_edges_per_node (int, optional): Minimum number of edges per node. Defaults to 0.
|
328
|
-
|
329
|
-
Returns:
|
330
|
-
NetworkX graph: Loaded and processed network.
|
373
|
+
def load_network_cyjs(self, filepath, source_label="source", target_label="target"):
|
331
374
|
"""
|
332
|
-
|
333
|
-
compute_sphere=compute_sphere,
|
334
|
-
surface_depth=surface_depth,
|
335
|
-
min_edges_per_node=min_edges_per_node,
|
336
|
-
)
|
337
|
-
return networkio._load_network_cyjs(
|
338
|
-
filepath=filepath,
|
339
|
-
source_label=source_label,
|
340
|
-
target_label=target_label,
|
341
|
-
)
|
342
|
-
|
343
|
-
def _load_network_cyjs(self, filepath, source_label="source", target_label="target"):
|
344
|
-
"""Private method to load a network from a Cytoscape JSON (.cyjs) file.
|
375
|
+
Load a network from a Cytoscape JSON (.cyjs) file.
|
345
376
|
|
346
377
|
Args:
|
347
378
|
filepath (str): Path to the Cytoscape JSON file.
|
@@ -396,7 +427,8 @@ class NetworkIO:
|
|
396
427
|
return self._initialize_graph(G)
|
397
428
|
|
398
429
|
def _initialize_graph(self, G: nx.Graph) -> nx.Graph:
|
399
|
-
"""
|
430
|
+
"""
|
431
|
+
Initialize the graph by processing and validating its nodes and edges.
|
400
432
|
|
401
433
|
Args:
|
402
434
|
G (nx.Graph): The input NetworkX graph.
|
@@ -414,7 +446,8 @@ class NetworkIO:
|
|
414
446
|
return G
|
415
447
|
|
416
448
|
def _remove_invalid_graph_properties(self, G: nx.Graph) -> None:
|
417
|
-
"""
|
449
|
+
"""
|
450
|
+
Remove invalid properties from the graph, including self-loops, nodes with fewer edges than
|
418
451
|
the threshold, and isolated nodes.
|
419
452
|
|
420
453
|
Args:
|
@@ -449,7 +482,8 @@ class NetworkIO:
|
|
449
482
|
logger.debug(f"Final edge count: {num_final_edges}")
|
450
483
|
|
451
484
|
def _assign_edge_weights(self, G: nx.Graph) -> None:
|
452
|
-
"""
|
485
|
+
"""
|
486
|
+
Assign default edge weights to the graph.
|
453
487
|
|
454
488
|
Args:
|
455
489
|
G (nx.Graph): A NetworkX graph object.
|
@@ -459,7 +493,8 @@ class NetworkIO:
|
|
459
493
|
nx.set_edge_attributes(G, default_weight, "weight")
|
460
494
|
|
461
495
|
def _validate_nodes(self, G: nx.Graph) -> None:
|
462
|
-
"""
|
496
|
+
"""
|
497
|
+
Validate the graph structure and attributes with attribute fallback for positions and labels.
|
463
498
|
|
464
499
|
Args:
|
465
500
|
G (nx.Graph): A NetworkX graph object.
|
@@ -519,7 +554,8 @@ class NetworkIO:
|
|
519
554
|
)
|
520
555
|
|
521
556
|
def _assign_edge_lengths(self, G: nx.Graph) -> None:
|
522
|
-
"""
|
557
|
+
"""
|
558
|
+
Prepare the network by adjusting surface depth and calculating edge lengths.
|
523
559
|
|
524
560
|
Args:
|
525
561
|
G (nx.Graph): The input network graph.
|
@@ -537,7 +573,8 @@ class NetworkIO:
|
|
537
573
|
compute_sphere: bool = True,
|
538
574
|
surface_depth: float = 0.0,
|
539
575
|
) -> nx.Graph:
|
540
|
-
"""
|
576
|
+
"""
|
577
|
+
Prepare the graph by normalizing coordinates and optionally mapping nodes to a sphere.
|
541
578
|
|
542
579
|
Args:
|
543
580
|
G (nx.Graph): The input graph.
|
@@ -558,7 +595,8 @@ class NetworkIO:
|
|
558
595
|
return G_depth
|
559
596
|
|
560
597
|
def _calculate_and_set_edge_lengths(self, G: nx.Graph, compute_sphere: bool) -> None:
|
561
|
-
"""
|
598
|
+
"""
|
599
|
+
Compute and assign edge lengths in the graph.
|
562
600
|
|
563
601
|
Args:
|
564
602
|
G (nx.Graph): The input graph.
|
@@ -592,7 +630,8 @@ class NetworkIO:
|
|
592
630
|
G.edges[u, v]["length"] = distance
|
593
631
|
|
594
632
|
def _map_to_sphere(self, G: nx.Graph) -> None:
|
595
|
-
"""
|
633
|
+
"""
|
634
|
+
Map the x and y coordinates of graph nodes onto a 3D sphere.
|
596
635
|
|
597
636
|
Args:
|
598
637
|
G (nx.Graph): The input graph with nodes having 'x' and 'y' coordinates.
|
@@ -616,7 +655,8 @@ class NetworkIO:
|
|
616
655
|
nx.set_node_attributes(G, xyz_coords)
|
617
656
|
|
618
657
|
def _normalize_graph_coordinates(self, G: nx.Graph) -> None:
|
619
|
-
"""
|
658
|
+
"""
|
659
|
+
Normalize the x and y coordinates of the nodes in the graph to the [0, 1] range.
|
620
660
|
|
621
661
|
Args:
|
622
662
|
G (nx.Graph): The input graph with nodes having 'x' and 'y' coordinates.
|
@@ -633,7 +673,8 @@ class NetworkIO:
|
|
633
673
|
G.nodes[node]["x"], G.nodes[node]["y"] = normalized_xy[i]
|
634
674
|
|
635
675
|
def _create_depth(self, G: nx.Graph, surface_depth: float = 0.0) -> nx.Graph:
|
636
|
-
"""
|
676
|
+
"""
|
677
|
+
Adjust the 'z' attribute of each node based on the subcluster strengths and normalized surface depth.
|
637
678
|
|
638
679
|
Args:
|
639
680
|
G (nx.Graph): The input graph.
|
@@ -677,7 +718,8 @@ class NetworkIO:
|
|
677
718
|
filetype: str,
|
678
719
|
filepath: str = "",
|
679
720
|
) -> None:
|
680
|
-
"""
|
721
|
+
"""
|
722
|
+
Log the loading of the network with relevant parameters.
|
681
723
|
|
682
724
|
Args:
|
683
725
|
filetype (str): The type of the file being loaded (e.g., 'CSV', 'JSON').
|
@@ -1,19 +1,20 @@
|
|
1
1
|
"""
|
2
|
-
risk/
|
3
|
-
|
2
|
+
risk/_network/_plotter/_api
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
from typing import List, Tuple, Union
|
7
7
|
|
8
8
|
import numpy as np
|
9
9
|
|
10
|
-
from
|
11
|
-
from
|
12
|
-
from
|
10
|
+
from ..._log import log_header
|
11
|
+
from .._graph import Graph
|
12
|
+
from ._plotter import Plotter
|
13
13
|
|
14
14
|
|
15
15
|
class PlotterAPI:
|
16
|
-
"""
|
16
|
+
"""
|
17
|
+
Handles the loading of network plotter objects.
|
17
18
|
|
18
19
|
The PlotterAPI class provides methods to load and configure Plotter objects for plotting network graphs.
|
19
20
|
"""
|
@@ -29,7 +30,8 @@ class PlotterAPI:
|
|
29
30
|
background_alpha: Union[float, None] = 1.0,
|
30
31
|
pad: float = 0.3,
|
31
32
|
) -> Plotter:
|
32
|
-
"""
|
33
|
+
"""
|
34
|
+
Get a Plotter object for plotting.
|
33
35
|
|
34
36
|
Args:
|
35
37
|
graph (Graph): The graph to plot.
|