risk-network 0.0.6b0__py3-none-any.whl → 0.0.6b2__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 +1 -1
- risk/annotations/io.py +15 -3
- risk/neighborhoods/community.py +7 -3
- risk/neighborhoods/neighborhoods.py +2 -2
- risk/network/graph.py +26 -21
- risk/network/io.py +11 -0
- risk/network/plot.py +430 -202
- risk/risk.py +6 -18
- {risk_network-0.0.6b0.dist-info → risk_network-0.0.6b2.dist-info}/METADATA +1 -1
- {risk_network-0.0.6b0.dist-info → risk_network-0.0.6b2.dist-info}/RECORD +13 -13
- {risk_network-0.0.6b0.dist-info → risk_network-0.0.6b2.dist-info}/LICENSE +0 -0
- {risk_network-0.0.6b0.dist-info → risk_network-0.0.6b2.dist-info}/WHEEL +0 -0
- {risk_network-0.0.6b0.dist-info → risk_network-0.0.6b2.dist-info}/top_level.txt +0 -0
risk/__init__.py
CHANGED
risk/annotations/io.py
CHANGED
@@ -36,13 +36,15 @@ class AnnotationsIO:
|
|
36
36
|
dict: A dictionary containing ordered nodes, ordered annotations, and the annotations matrix.
|
37
37
|
"""
|
38
38
|
filetype = "JSON"
|
39
|
+
# Log the loading of the JSON file
|
39
40
|
params.log_annotations(filepath=filepath, filetype=filetype)
|
40
41
|
_log_loading(filetype, filepath=filepath)
|
42
|
+
|
41
43
|
# Open and read the JSON file
|
42
44
|
with open(filepath, "r") as file:
|
43
45
|
annotations_input = json.load(file)
|
44
46
|
|
45
|
-
#
|
47
|
+
# Load the annotations into the provided network
|
46
48
|
return load_annotations(network, annotations_input)
|
47
49
|
|
48
50
|
def load_excel_annotation(
|
@@ -69,14 +71,18 @@ class AnnotationsIO:
|
|
69
71
|
linked to the provided network.
|
70
72
|
"""
|
71
73
|
filetype = "Excel"
|
74
|
+
# Log the loading of the Excel file
|
72
75
|
params.log_annotations(filepath=filepath, filetype=filetype)
|
73
76
|
_log_loading(filetype, filepath=filepath)
|
77
|
+
|
74
78
|
# Load the specified sheet from the Excel file
|
75
79
|
df = pd.read_excel(filepath, sheet_name=sheet_name)
|
76
80
|
# Split the nodes column by the specified nodes_delimiter
|
77
81
|
df[nodes_colname] = df[nodes_colname].apply(lambda x: x.split(nodes_delimiter))
|
78
82
|
# Convert the DataFrame to a dictionary pairing labels with their corresponding nodes
|
79
83
|
label_node_dict = df.set_index(label_colname)[nodes_colname].to_dict()
|
84
|
+
|
85
|
+
# Load the annotations into the provided network
|
80
86
|
return load_annotations(network, label_node_dict)
|
81
87
|
|
82
88
|
def load_csv_annotation(
|
@@ -101,13 +107,16 @@ class AnnotationsIO:
|
|
101
107
|
linked to the provided network.
|
102
108
|
"""
|
103
109
|
filetype = "CSV"
|
110
|
+
# Log the loading of the CSV file
|
104
111
|
params.log_annotations(filepath=filepath, filetype=filetype)
|
105
112
|
_log_loading(filetype, filepath=filepath)
|
113
|
+
|
106
114
|
# Load the CSV file into a dictionary
|
107
115
|
annotations_input = _load_matrix_file(
|
108
116
|
filepath, label_colname, nodes_colname, delimiter=",", nodes_delimiter=nodes_delimiter
|
109
117
|
)
|
110
|
-
|
118
|
+
|
119
|
+
# Load the annotations into the provided network
|
111
120
|
return load_annotations(network, annotations_input)
|
112
121
|
|
113
122
|
def load_tsv_annotation(
|
@@ -132,13 +141,16 @@ class AnnotationsIO:
|
|
132
141
|
linked to the provided network.
|
133
142
|
"""
|
134
143
|
filetype = "TSV"
|
144
|
+
# Log the loading of the TSV file
|
135
145
|
params.log_annotations(filepath=filepath, filetype=filetype)
|
136
146
|
_log_loading(filetype, filepath=filepath)
|
147
|
+
|
137
148
|
# Load the TSV file into a dictionary
|
138
149
|
annotations_input = _load_matrix_file(
|
139
150
|
filepath, label_colname, nodes_colname, delimiter="\t", nodes_delimiter=nodes_delimiter
|
140
151
|
)
|
141
|
-
|
152
|
+
|
153
|
+
# Load the annotations into the provided network
|
142
154
|
return load_annotations(network, annotations_input)
|
143
155
|
|
144
156
|
|
risk/neighborhoods/community.py
CHANGED
@@ -25,10 +25,14 @@ def calculate_dijkstra_neighborhoods(network: nx.Graph) -> np.ndarray:
|
|
25
25
|
|
26
26
|
# Populate the neighborhoods matrix based on Dijkstra's distances
|
27
27
|
for source, targets in all_dijkstra_paths.items():
|
28
|
+
max_length = max(targets.values()) if targets else 1 # Handle cases with no targets
|
28
29
|
for target, length in targets.items():
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
if np.isnan(length):
|
31
|
+
neighborhoods[source, target] = max_length # Use max distance for NaN
|
32
|
+
elif length == 0:
|
33
|
+
neighborhoods[source, target] = 1 # Assign 1 for zero-length paths (self-loops)
|
34
|
+
else:
|
35
|
+
neighborhoods[source, target] = 1 / length # Inverse of the distance
|
32
36
|
|
33
37
|
return neighborhoods
|
34
38
|
|
@@ -4,7 +4,7 @@ risk/neighborhoods/neighborhoods
|
|
4
4
|
"""
|
5
5
|
|
6
6
|
import warnings
|
7
|
-
from typing import Any, Dict, Tuple
|
7
|
+
from typing import Any, Dict, List, Tuple
|
8
8
|
|
9
9
|
import networkx as nx
|
10
10
|
import numpy as np
|
@@ -305,7 +305,7 @@ def _get_node_position(network: nx.Graph, node: Any) -> np.ndarray:
|
|
305
305
|
)
|
306
306
|
|
307
307
|
|
308
|
-
def _calculate_threshold(average_distances:
|
308
|
+
def _calculate_threshold(average_distances: List, distance_threshold: float) -> float:
|
309
309
|
"""Calculate the distance threshold based on the given average distances and a percentile threshold.
|
310
310
|
|
311
311
|
Args:
|
risk/network/graph.py
CHANGED
@@ -101,31 +101,32 @@ class NetworkGraph:
|
|
101
101
|
|
102
102
|
def get_domain_colors(
|
103
103
|
self,
|
104
|
+
cmap: str = "gist_rainbow",
|
105
|
+
color: Union[str, None] = None,
|
104
106
|
min_scale: float = 0.8,
|
105
107
|
max_scale: float = 1.0,
|
106
108
|
scale_factor: float = 1.0,
|
107
109
|
random_seed: int = 888,
|
108
|
-
**kwargs,
|
109
110
|
) -> np.ndarray:
|
110
|
-
"""Generate composite colors for domains.
|
111
|
-
|
112
|
-
This method generates composite colors for nodes based on their enrichment scores and transforms
|
113
|
-
them to ensure proper alpha values and intensity. For nodes with alpha == 0, it assigns new colors
|
114
|
-
based on the closest valid neighbors within a specified distance.
|
111
|
+
"""Generate composite colors for domains based on enrichment or specified colors.
|
115
112
|
|
116
113
|
Args:
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
114
|
+
cmap (str, optional): Name of the colormap to use for generating domain colors. Defaults to "gist_rainbow".
|
115
|
+
color (str or None, optional): A specific color to use for all generated colors. Defaults to None.
|
116
|
+
min_scale (float, optional): Minimum intensity scale for the colors generated by the colormap.
|
117
|
+
Controls the dimmest colors. Defaults to 0.8.
|
118
|
+
max_scale (float, optional): Maximum intensity scale for the colors generated by the colormap.
|
119
|
+
Controls the brightest colors. Defaults to 1.0.
|
120
|
+
scale_factor (float, optional): Exponent for adjusting the color scaling based on enrichment scores.
|
121
|
+
A higher value increases contrast by dimming lower scores more. Defaults to 1.0.
|
122
|
+
random_seed (int, optional): Seed for random number generation to ensure reproducibility of color assignments.
|
123
|
+
Defaults to 888.
|
123
124
|
|
124
125
|
Returns:
|
125
|
-
np.ndarray: Array of
|
126
|
+
np.ndarray: Array of RGBA colors generated for each domain, based on enrichment or the specified color.
|
126
127
|
"""
|
127
128
|
# Get colors for each domain
|
128
|
-
domain_colors = self._get_domain_colors(random_seed=random_seed)
|
129
|
+
domain_colors = self._get_domain_colors(cmap=cmap, color=color, random_seed=random_seed)
|
129
130
|
# Generate composite colors for nodes
|
130
131
|
node_colors = self._get_composite_node_colors(domain_colors)
|
131
132
|
# Transform colors to ensure proper alpha values and intensity
|
@@ -161,12 +162,16 @@ class NetworkGraph:
|
|
161
162
|
return composite_colors
|
162
163
|
|
163
164
|
def _get_domain_colors(
|
164
|
-
self,
|
165
|
+
self,
|
166
|
+
cmap: str = "gist_rainbow",
|
167
|
+
color: Union[str, None] = None,
|
168
|
+
random_seed: int = 888,
|
165
169
|
) -> Dict[str, Any]:
|
166
170
|
"""Get colors for each domain.
|
167
171
|
|
168
172
|
Args:
|
169
|
-
|
173
|
+
cmap (str, optional): The name of the colormap to use. Defaults to "gist_rainbow".
|
174
|
+
color (str or None, optional): A specific color to use for all generated colors. Defaults to None.
|
170
175
|
random_seed (int, optional): Seed for random number generation. Defaults to 888.
|
171
176
|
|
172
177
|
Returns:
|
@@ -178,7 +183,7 @@ class NetworkGraph:
|
|
178
183
|
]
|
179
184
|
domains = np.sort(numeric_domains)
|
180
185
|
domain_colors = _get_colors(
|
181
|
-
num_colors_to_generate=len(domains), color=color, random_seed=random_seed
|
186
|
+
num_colors_to_generate=len(domains), cmap=cmap, color=color, random_seed=random_seed
|
182
187
|
)
|
183
188
|
return dict(zip(self.domain_to_nodes.keys(), domain_colors))
|
184
189
|
|
@@ -273,17 +278,17 @@ def _extract_node_coordinates(G: nx.Graph) -> np.ndarray:
|
|
273
278
|
|
274
279
|
def _get_colors(
|
275
280
|
num_colors_to_generate: int = 10,
|
276
|
-
cmap: str = "
|
277
|
-
random_seed: int = 888,
|
281
|
+
cmap: str = "gist_rainbow",
|
278
282
|
color: Union[str, None] = None,
|
283
|
+
random_seed: int = 888,
|
279
284
|
) -> List[Tuple]:
|
280
285
|
"""Generate a list of RGBA colors from a specified colormap or use a direct color string.
|
281
286
|
|
282
287
|
Args:
|
283
288
|
num_colors_to_generate (int): The number of colors to generate. Defaults to 10.
|
284
|
-
cmap (str): The name of the colormap to use. Defaults to "
|
289
|
+
cmap (str, optional): The name of the colormap to use. Defaults to "gist_rainbow".
|
290
|
+
color (str or None, optional): A specific color to use for all generated colors.
|
285
291
|
random_seed (int): Seed for random number generation. Defaults to 888.
|
286
|
-
color (str, optional): Specific color to use for all nodes. If specified, it will overwrite the colormap.
|
287
292
|
Defaults to None.
|
288
293
|
|
289
294
|
Returns:
|
risk/network/io.py
CHANGED
@@ -48,6 +48,7 @@ class NetworkIO:
|
|
48
48
|
self.min_edges_per_node = min_edges_per_node
|
49
49
|
self.include_edge_weight = include_edge_weight
|
50
50
|
self.weight_label = weight_label
|
51
|
+
# Log the initialization of the NetworkIO class
|
51
52
|
params.log_network(
|
52
53
|
compute_sphere=compute_sphere,
|
53
54
|
surface_depth=surface_depth,
|
@@ -98,11 +99,14 @@ class NetworkIO:
|
|
98
99
|
nx.Graph: Loaded and processed network.
|
99
100
|
"""
|
100
101
|
filetype = "GPickle"
|
102
|
+
# Log the loading of the GPickle file
|
101
103
|
params.log_network(filetype=filetype, filepath=filepath)
|
102
104
|
self._log_loading(filetype, filepath=filepath)
|
105
|
+
|
103
106
|
with open(filepath, "rb") as f:
|
104
107
|
G = pickle.load(f)
|
105
108
|
|
109
|
+
# Initialize the graph
|
106
110
|
return self._initialize_graph(G)
|
107
111
|
|
108
112
|
@classmethod
|
@@ -147,8 +151,11 @@ class NetworkIO:
|
|
147
151
|
nx.Graph: Processed network.
|
148
152
|
"""
|
149
153
|
filetype = "NetworkX"
|
154
|
+
# Log the loading of the NetworkX graph
|
150
155
|
params.log_network(filetype=filetype)
|
151
156
|
self._log_loading(filetype)
|
157
|
+
|
158
|
+
# Initialize the graph
|
152
159
|
return self._initialize_graph(network)
|
153
160
|
|
154
161
|
@classmethod
|
@@ -213,8 +220,10 @@ class NetworkIO:
|
|
213
220
|
nx.Graph: Loaded and processed network.
|
214
221
|
"""
|
215
222
|
filetype = "Cytoscape"
|
223
|
+
# Log the loading of the Cytoscape file
|
216
224
|
params.log_network(filetype=filetype, filepath=str(filepath))
|
217
225
|
self._log_loading(filetype, filepath=filepath)
|
226
|
+
|
218
227
|
cys_files = []
|
219
228
|
tmp_dir = ".tmp_cytoscape"
|
220
229
|
# Try / finally to remove unzipped files
|
@@ -295,6 +304,7 @@ class NetworkIO:
|
|
295
304
|
node
|
296
305
|
] # Assuming you have a dict `node_y_positions` for y coordinates
|
297
306
|
|
307
|
+
# Initialize the graph
|
298
308
|
return self._initialize_graph(G)
|
299
309
|
|
300
310
|
finally:
|
@@ -354,6 +364,7 @@ class NetworkIO:
|
|
354
364
|
NetworkX graph: Loaded and processed network.
|
355
365
|
"""
|
356
366
|
filetype = "Cytoscape JSON"
|
367
|
+
# Log the loading of the Cytoscape JSON file
|
357
368
|
params.log_network(filetype=filetype, filepath=str(filepath))
|
358
369
|
self._log_loading(filetype, filepath=filepath)
|
359
370
|
|