risk-network 0.0.8b27__py3-none-any.whl → 0.0.9__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/annotations/__init__.py +2 -2
- risk/annotations/annotations.py +195 -118
- risk/annotations/io.py +47 -31
- risk/log/__init__.py +4 -2
- risk/log/console.py +3 -1
- risk/log/{params.py → parameters.py} +17 -42
- risk/neighborhoods/__init__.py +3 -5
- risk/neighborhoods/api.py +442 -0
- risk/neighborhoods/community.py +324 -101
- risk/neighborhoods/domains.py +125 -52
- risk/neighborhoods/neighborhoods.py +177 -165
- risk/network/__init__.py +1 -3
- risk/network/geometry.py +71 -89
- risk/network/graph/__init__.py +6 -0
- risk/network/graph/api.py +200 -0
- risk/network/{graph.py → graph/graph.py} +90 -40
- risk/network/graph/summary.py +254 -0
- risk/network/io.py +103 -114
- risk/network/plotter/__init__.py +6 -0
- risk/network/plotter/api.py +54 -0
- risk/network/{plot → plotter}/canvas.py +9 -8
- risk/network/{plot → plotter}/contour.py +27 -24
- risk/network/{plot → plotter}/labels.py +73 -78
- risk/network/{plot → plotter}/network.py +45 -39
- risk/network/{plot → plotter}/plotter.py +23 -17
- risk/network/{plot/utils/color.py → plotter/utils/colors.py} +114 -122
- risk/network/{plot → plotter}/utils/layout.py +10 -7
- risk/risk.py +11 -500
- risk/stats/__init__.py +10 -4
- risk/stats/permutation/__init__.py +1 -1
- risk/stats/permutation/permutation.py +44 -38
- risk/stats/permutation/test_functions.py +26 -18
- risk/stats/{stats.py → significance.py} +17 -15
- risk/stats/stat_tests.py +267 -0
- {risk_network-0.0.8b27.dist-info → risk_network-0.0.9.dist-info}/METADATA +31 -46
- risk_network-0.0.9.dist-info/RECORD +40 -0
- {risk_network-0.0.8b27.dist-info → risk_network-0.0.9.dist-info}/WHEEL +1 -1
- risk/constants.py +0 -31
- risk/network/plot/__init__.py +0 -6
- risk/stats/hypergeom.py +0 -54
- risk/stats/poisson.py +0 -44
- risk_network-0.0.8b27.dist-info/RECORD +0 -37
- {risk_network-0.0.8b27.dist-info → risk_network-0.0.9.dist-info}/LICENSE +0 -0
- {risk_network-0.0.8b27.dist-info → risk_network-0.0.9.dist-info}/top_level.txt +0 -0
risk/annotations/io.py
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
"""
|
2
2
|
risk/annotations/io
|
3
3
|
~~~~~~~~~~~~~~~~~~~
|
4
|
-
|
5
|
-
This file contains the code for the RISK class and command-line access.
|
6
4
|
"""
|
7
5
|
|
8
6
|
import json
|
@@ -25,27 +23,32 @@ class AnnotationsIO:
|
|
25
23
|
def __init__(self):
|
26
24
|
pass
|
27
25
|
|
28
|
-
def load_json_annotation(
|
26
|
+
def load_json_annotation(
|
27
|
+
self, network: nx.Graph, filepath: str, min_nodes_per_term: int = 2
|
28
|
+
) -> Dict[str, Any]:
|
29
29
|
"""Load annotations from a JSON file and convert them to a DataFrame.
|
30
30
|
|
31
31
|
Args:
|
32
32
|
network (NetworkX graph): The network to which the annotations are related.
|
33
33
|
filepath (str): Path to the JSON annotations file.
|
34
|
+
min_nodes_per_term (int, optional): The minimum number of network nodes required for each annotation
|
35
|
+
term to be included. Defaults to 2.
|
34
36
|
|
35
37
|
Returns:
|
36
38
|
Dict[str, Any]: A dictionary containing ordered nodes, ordered annotations, and the annotations matrix.
|
37
39
|
"""
|
38
40
|
filetype = "JSON"
|
39
41
|
# Log the loading of the JSON file
|
40
|
-
params.log_annotations(
|
42
|
+
params.log_annotations(
|
43
|
+
filetype=filetype, filepath=filepath, min_nodes_per_term=min_nodes_per_term
|
44
|
+
)
|
41
45
|
_log_loading(filetype, filepath=filepath)
|
42
46
|
|
43
|
-
#
|
44
|
-
with open(filepath, "r") as file:
|
47
|
+
# Load the JSON file into a dictionary
|
48
|
+
with open(filepath, "r", encoding="utf-8") as file:
|
45
49
|
annotations_input = json.load(file)
|
46
50
|
|
47
|
-
|
48
|
-
return load_annotations(network, annotations_input)
|
51
|
+
return load_annotations(network, annotations_input, min_nodes_per_term)
|
49
52
|
|
50
53
|
def load_excel_annotation(
|
51
54
|
self,
|
@@ -55,6 +58,7 @@ class AnnotationsIO:
|
|
55
58
|
nodes_colname: str = "nodes",
|
56
59
|
sheet_name: str = "Sheet1",
|
57
60
|
nodes_delimiter: str = ";",
|
61
|
+
min_nodes_per_term: int = 2,
|
58
62
|
) -> Dict[str, Any]:
|
59
63
|
"""Load annotations from an Excel file and associate them with the network.
|
60
64
|
|
@@ -65,6 +69,8 @@ class AnnotationsIO:
|
|
65
69
|
nodes_colname (str): Name of the column containing the nodes associated with each label.
|
66
70
|
sheet_name (str, optional): The name of the Excel sheet to load (default is 'Sheet1').
|
67
71
|
nodes_delimiter (str, optional): Delimiter used to separate multiple nodes within the nodes column (default is ';').
|
72
|
+
min_nodes_per_term (int, optional): The minimum number of network nodes required for each annotation
|
73
|
+
term to be included. Defaults to 2.
|
68
74
|
|
69
75
|
Returns:
|
70
76
|
Dict[str, Any]: A dictionary where each label is paired with its respective list of nodes,
|
@@ -72,18 +78,21 @@ class AnnotationsIO:
|
|
72
78
|
"""
|
73
79
|
filetype = "Excel"
|
74
80
|
# Log the loading of the Excel file
|
75
|
-
params.log_annotations(
|
81
|
+
params.log_annotations(
|
82
|
+
filetype=filetype, filepath=filepath, min_nodes_per_term=min_nodes_per_term
|
83
|
+
)
|
76
84
|
_log_loading(filetype, filepath=filepath)
|
77
85
|
|
78
86
|
# Load the specified sheet from the Excel file
|
79
|
-
|
87
|
+
annotation = pd.read_excel(filepath, sheet_name=sheet_name)
|
80
88
|
# Split the nodes column by the specified nodes_delimiter
|
81
|
-
|
89
|
+
annotation[nodes_colname] = annotation[nodes_colname].apply(
|
90
|
+
lambda x: x.split(nodes_delimiter)
|
91
|
+
)
|
82
92
|
# Convert the DataFrame to a dictionary pairing labels with their corresponding nodes
|
83
|
-
|
93
|
+
annotations_input = annotation.set_index(label_colname)[nodes_colname].to_dict()
|
84
94
|
|
85
|
-
|
86
|
-
return load_annotations(network, label_node_dict)
|
95
|
+
return load_annotations(network, annotations_input, min_nodes_per_term)
|
87
96
|
|
88
97
|
def load_csv_annotation(
|
89
98
|
self,
|
@@ -92,6 +101,7 @@ class AnnotationsIO:
|
|
92
101
|
label_colname: str = "label",
|
93
102
|
nodes_colname: str = "nodes",
|
94
103
|
nodes_delimiter: str = ";",
|
104
|
+
min_nodes_per_term: int = 2,
|
95
105
|
) -> Dict[str, Any]:
|
96
106
|
"""Load annotations from a CSV file and associate them with the network.
|
97
107
|
|
@@ -101,6 +111,8 @@ class AnnotationsIO:
|
|
101
111
|
label_colname (str): Name of the column containing the labels (e.g., GO terms).
|
102
112
|
nodes_colname (str): Name of the column containing the nodes associated with each label.
|
103
113
|
nodes_delimiter (str, optional): Delimiter used to separate multiple nodes within the nodes column (default is ';').
|
114
|
+
min_nodes_per_term (int, optional): The minimum number of network nodes required for each annotation
|
115
|
+
term to be included. Defaults to 2.
|
104
116
|
|
105
117
|
Returns:
|
106
118
|
Dict[str, Any]: A dictionary where each label is paired with its respective list of nodes,
|
@@ -108,7 +120,9 @@ class AnnotationsIO:
|
|
108
120
|
"""
|
109
121
|
filetype = "CSV"
|
110
122
|
# Log the loading of the CSV file
|
111
|
-
params.log_annotations(
|
123
|
+
params.log_annotations(
|
124
|
+
filetype=filetype, filepath=filepath, min_nodes_per_term=min_nodes_per_term
|
125
|
+
)
|
112
126
|
_log_loading(filetype, filepath=filepath)
|
113
127
|
|
114
128
|
# Load the CSV file into a dictionary
|
@@ -116,8 +130,7 @@ class AnnotationsIO:
|
|
116
130
|
filepath, label_colname, nodes_colname, delimiter=",", nodes_delimiter=nodes_delimiter
|
117
131
|
)
|
118
132
|
|
119
|
-
|
120
|
-
return load_annotations(network, annotations_input)
|
133
|
+
return load_annotations(network, annotations_input, min_nodes_per_term)
|
121
134
|
|
122
135
|
def load_tsv_annotation(
|
123
136
|
self,
|
@@ -126,6 +139,7 @@ class AnnotationsIO:
|
|
126
139
|
label_colname: str = "label",
|
127
140
|
nodes_colname: str = "nodes",
|
128
141
|
nodes_delimiter: str = ";",
|
142
|
+
min_nodes_per_term: int = 2,
|
129
143
|
) -> Dict[str, Any]:
|
130
144
|
"""Load annotations from a TSV file and associate them with the network.
|
131
145
|
|
@@ -135,6 +149,8 @@ class AnnotationsIO:
|
|
135
149
|
label_colname (str): Name of the column containing the labels (e.g., GO terms).
|
136
150
|
nodes_colname (str): Name of the column containing the nodes associated with each label.
|
137
151
|
nodes_delimiter (str, optional): Delimiter used to separate multiple nodes within the nodes column (default is ';').
|
152
|
+
min_nodes_per_term (int, optional): The minimum number of network nodes required for each annotation
|
153
|
+
term to be included. Defaults to 2.
|
138
154
|
|
139
155
|
Returns:
|
140
156
|
Dict[str, Any]: A dictionary where each label is paired with its respective list of nodes,
|
@@ -142,7 +158,9 @@ class AnnotationsIO:
|
|
142
158
|
"""
|
143
159
|
filetype = "TSV"
|
144
160
|
# Log the loading of the TSV file
|
145
|
-
params.log_annotations(
|
161
|
+
params.log_annotations(
|
162
|
+
filetype=filetype, filepath=filepath, min_nodes_per_term=min_nodes_per_term
|
163
|
+
)
|
146
164
|
_log_loading(filetype, filepath=filepath)
|
147
165
|
|
148
166
|
# Load the TSV file into a dictionary
|
@@ -150,15 +168,18 @@ class AnnotationsIO:
|
|
150
168
|
filepath, label_colname, nodes_colname, delimiter="\t", nodes_delimiter=nodes_delimiter
|
151
169
|
)
|
152
170
|
|
153
|
-
|
154
|
-
return load_annotations(network, annotations_input)
|
171
|
+
return load_annotations(network, annotations_input, min_nodes_per_term)
|
155
172
|
|
156
|
-
def load_dict_annotation(
|
173
|
+
def load_dict_annotation(
|
174
|
+
self, network: nx.Graph, content: Dict[str, Any], min_nodes_per_term: int = 2
|
175
|
+
) -> Dict[str, Any]:
|
157
176
|
"""Load annotations from a provided dictionary and convert them to a dictionary annotation.
|
158
177
|
|
159
178
|
Args:
|
160
179
|
network (NetworkX graph): The network to which the annotations are related.
|
161
180
|
content (Dict[str, Any]): The annotations dictionary to load.
|
181
|
+
min_nodes_per_term (int, optional): The minimum number of network nodes required for each annotation
|
182
|
+
term to be included. Defaults to 2.
|
162
183
|
|
163
184
|
Returns:
|
164
185
|
Dict[str, Any]: A dictionary containing ordered nodes, ordered annotations, and the annotations matrix.
|
@@ -174,13 +195,8 @@ class AnnotationsIO:
|
|
174
195
|
params.log_annotations(filepath="In-memory dictionary", filetype=filetype)
|
175
196
|
_log_loading(filetype, "In-memory dictionary")
|
176
197
|
|
177
|
-
# Load the annotations
|
178
|
-
|
179
|
-
# Ensure the output is a dictionary
|
180
|
-
if not isinstance(annotations_dict, dict):
|
181
|
-
raise ValueError("Expected output to be a dictionary")
|
182
|
-
|
183
|
-
return annotations_dict
|
198
|
+
# Load the annotations as a dictionary from the provided dictionary
|
199
|
+
return load_annotations(network, content, min_nodes_per_term)
|
184
200
|
|
185
201
|
|
186
202
|
def _load_matrix_file(
|
@@ -203,11 +219,11 @@ def _load_matrix_file(
|
|
203
219
|
Dict[str, Any]: A dictionary where each label is paired with its respective list of nodes.
|
204
220
|
"""
|
205
221
|
# Load the CSV or TSV file into a DataFrame
|
206
|
-
|
222
|
+
annotation = pd.read_csv(filepath, delimiter=delimiter)
|
207
223
|
# Split the nodes column by the nodes_delimiter to handle multiple nodes per label
|
208
|
-
|
224
|
+
annotation[nodes_colname] = annotation[nodes_colname].apply(lambda x: x.split(nodes_delimiter))
|
209
225
|
# Create a dictionary pairing labels with their corresponding list of nodes
|
210
|
-
label_node_dict =
|
226
|
+
label_node_dict = annotation.set_index(label_colname)[nodes_colname].to_dict()
|
211
227
|
return label_node_dict
|
212
228
|
|
213
229
|
|
risk/log/__init__.py
CHANGED
@@ -3,7 +3,9 @@ risk/log
|
|
3
3
|
~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
|
-
from .console import logger, log_header, set_global_verbosity
|
7
|
-
from .
|
6
|
+
from risk.log.console import logger, log_header, set_global_verbosity
|
7
|
+
from risk.log.parameters import Params
|
8
8
|
|
9
|
+
# Initialize the global parameters logger
|
9
10
|
params = Params()
|
11
|
+
params.initialize()
|
risk/log/console.py
CHANGED
@@ -16,8 +16,10 @@ def in_jupyter():
|
|
16
16
|
shell = get_ipython().__class__.__name__
|
17
17
|
if shell == "ZMQInteractiveShell": # Jupyter Notebook or QtConsole
|
18
18
|
return True
|
19
|
-
|
19
|
+
if shell == "TerminalInteractiveShell": # Terminal running IPython
|
20
20
|
return False
|
21
|
+
|
22
|
+
return False # Other type (?)
|
21
23
|
except NameError:
|
22
24
|
return False # Not in Jupyter
|
23
25
|
|
@@ -1,50 +1,22 @@
|
|
1
1
|
"""
|
2
|
-
risk/log/
|
3
|
-
|
2
|
+
risk/log/parameters
|
3
|
+
~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
import csv
|
7
7
|
import json
|
8
8
|
import warnings
|
9
9
|
from datetime import datetime
|
10
|
-
from functools import wraps
|
11
10
|
from typing import Any, Dict
|
12
11
|
|
13
12
|
import numpy as np
|
14
13
|
|
15
|
-
from .console import logger, log_header
|
14
|
+
from risk.log.console import logger, log_header
|
16
15
|
|
17
16
|
# Suppress all warnings - this is to resolve warnings from multiprocessing
|
18
17
|
warnings.filterwarnings("ignore")
|
19
18
|
|
20
19
|
|
21
|
-
def _safe_param_export(func):
|
22
|
-
"""A decorator to wrap parameter export functions in a try-except block for safe execution.
|
23
|
-
|
24
|
-
Args:
|
25
|
-
func (function): The function to be wrapped.
|
26
|
-
|
27
|
-
Returns:
|
28
|
-
function: The wrapped function with error handling.
|
29
|
-
"""
|
30
|
-
|
31
|
-
@wraps(func)
|
32
|
-
def wrapper(*args, **kwargs):
|
33
|
-
try:
|
34
|
-
result = func(*args, **kwargs)
|
35
|
-
filepath = (
|
36
|
-
kwargs.get("filepath") or args[1]
|
37
|
-
) # Assuming filepath is always the second argument
|
38
|
-
logger.info(f"Parameters successfully exported to filepath: {filepath}")
|
39
|
-
return result
|
40
|
-
except Exception as e:
|
41
|
-
filepath = kwargs.get("filepath") or args[1]
|
42
|
-
logger.error(f"An error occurred while exporting parameters to {filepath}: {e}")
|
43
|
-
return None
|
44
|
-
|
45
|
-
return wrapper
|
46
|
-
|
47
|
-
|
48
20
|
class Params:
|
49
21
|
"""Handles the storage and logging of various parameters for network analysis.
|
50
22
|
|
@@ -106,7 +78,6 @@ class Params:
|
|
106
78
|
"""
|
107
79
|
self.plotter = {**self.plotter, **kwargs}
|
108
80
|
|
109
|
-
@_safe_param_export
|
110
81
|
def to_csv(self, filepath: str) -> None:
|
111
82
|
"""Export the parameters to a CSV file.
|
112
83
|
|
@@ -116,7 +87,7 @@ class Params:
|
|
116
87
|
# Load the parameter dictionary
|
117
88
|
params = self.load()
|
118
89
|
# Open the file in write mode
|
119
|
-
with open(filepath, "w", newline="") as csv_file:
|
90
|
+
with open(filepath, "w", encoding="utf-8", newline="") as csv_file:
|
120
91
|
writer = csv.writer(csv_file)
|
121
92
|
# Write the header
|
122
93
|
writer.writerow(["parent_key", "child_key", "value"])
|
@@ -128,17 +99,19 @@ class Params:
|
|
128
99
|
else:
|
129
100
|
writer.writerow([parent_key, "", parent_value])
|
130
101
|
|
131
|
-
|
102
|
+
logger.info(f"Parameters exported to CSV file: {filepath}")
|
103
|
+
|
132
104
|
def to_json(self, filepath: str) -> None:
|
133
105
|
"""Export the parameters to a JSON file.
|
134
106
|
|
135
107
|
Args:
|
136
108
|
filepath (str): The path where the JSON file will be saved.
|
137
109
|
"""
|
138
|
-
with open(filepath, "w") as json_file:
|
110
|
+
with open(filepath, "w", encoding="utf-8") as json_file:
|
139
111
|
json.dump(self.load(), json_file, indent=4)
|
140
112
|
|
141
|
-
|
113
|
+
logger.info(f"Parameters exported to JSON file: {filepath}")
|
114
|
+
|
142
115
|
def to_txt(self, filepath: str) -> None:
|
143
116
|
"""Export the parameters to a text file.
|
144
117
|
|
@@ -148,13 +121,15 @@ class Params:
|
|
148
121
|
# Load the parameter dictionary
|
149
122
|
params = self.load()
|
150
123
|
# Open the file in write mode
|
151
|
-
with open(filepath, "w") as txt_file:
|
124
|
+
with open(filepath, "w", encoding="utf-8") as txt_file:
|
152
125
|
for key, value in params.items():
|
153
126
|
# Write the key and its corresponding value
|
154
127
|
txt_file.write(f"{key}: {value}\n")
|
155
128
|
# Add a blank line after each entry
|
156
129
|
txt_file.write("\n")
|
157
130
|
|
131
|
+
logger.info(f"Parameters exported to text file: {filepath}")
|
132
|
+
|
158
133
|
def load(self) -> Dict[str, Any]:
|
159
134
|
"""Load and process various parameters, converting any np.ndarray values to lists.
|
160
135
|
|
@@ -186,12 +161,12 @@ def _convert_ndarray_to_list(d: Dict[str, Any]) -> Dict[str, Any]:
|
|
186
161
|
if isinstance(d, dict):
|
187
162
|
# Recursively process each value in the dictionary
|
188
163
|
return {k: _convert_ndarray_to_list(v) for k, v in d.items()}
|
189
|
-
|
164
|
+
if isinstance(d, list):
|
190
165
|
# Recursively process each item in the list
|
191
166
|
return [_convert_ndarray_to_list(v) for v in d]
|
192
|
-
|
167
|
+
if isinstance(d, np.ndarray):
|
193
168
|
# Convert numpy arrays to lists
|
194
169
|
return d.tolist()
|
195
|
-
|
196
|
-
|
197
|
-
|
170
|
+
|
171
|
+
# Return the value unchanged if it's not a dict, List, or ndarray
|
172
|
+
return d
|
risk/neighborhoods/__init__.py
CHANGED
@@ -3,8 +3,6 @@ risk/neighborhoods
|
|
3
3
|
~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
|
-
from .domains import define_domains,
|
7
|
-
from .neighborhoods import
|
8
|
-
|
9
|
-
process_neighborhoods,
|
10
|
-
)
|
6
|
+
from risk.neighborhoods.domains import define_domains, trim_domains
|
7
|
+
from risk.neighborhoods.api import NeighborhoodsAPI
|
8
|
+
from risk.neighborhoods.neighborhoods import process_neighborhoods
|