risk-network 0.0.4b1__py3-none-any.whl → 0.0.4b3__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/stats/stats.py CHANGED
@@ -3,221 +3,10 @@ risk/stats/stats
3
3
  ~~~~~~~~~~~~~~~~
4
4
  """
5
5
 
6
- from multiprocessing import get_context, Manager
7
- from tqdm import tqdm
8
- from typing import Any, Callable, Dict, Union
6
+ from typing import Union
9
7
 
10
8
  import numpy as np
11
9
  from statsmodels.stats.multitest import fdrcorrection
12
- from threadpoolctl import threadpool_limits
13
-
14
- from risk.stats.permutation import (
15
- compute_neighborhood_score_by_sum,
16
- compute_neighborhood_score_by_stdev,
17
- compute_neighborhood_score_by_z_score,
18
- )
19
-
20
- DISPATCH_PERMUTATION_TABLE = {
21
- "sum": compute_neighborhood_score_by_sum,
22
- "stdev": compute_neighborhood_score_by_stdev,
23
- "z_score": compute_neighborhood_score_by_z_score,
24
- }
25
-
26
-
27
- def compute_permutation(
28
- neighborhoods: np.ndarray,
29
- annotations: np.ndarray,
30
- score_metric: str = "sum",
31
- null_distribution: str = "network",
32
- num_permutations: int = 1000,
33
- random_seed: int = 888,
34
- max_workers: int = 1,
35
- ) -> Dict[str, Any]:
36
- """Compute permutation test for enrichment and depletion in neighborhoods.
37
-
38
- Args:
39
- neighborhoods (np.ndarray): Binary matrix representing neighborhoods.
40
- annotations (np.ndarray): Binary matrix representing annotations.
41
- score_metric (str, optional): Metric to use for scoring ('sum', 'mean', etc.). Defaults to "sum".
42
- null_distribution (str, optional): Type of null distribution ('network' or other). Defaults to "network".
43
- num_permutations (int, optional): Number of permutations to run. Defaults to 1000.
44
- random_seed (int, optional): Seed for random number generation. Defaults to 888.
45
- max_workers (int, optional): Number of workers for multiprocessing. Defaults to 1.
46
-
47
- Returns:
48
- dict: Dictionary containing depletion and enrichment p-values.
49
- """
50
- # Ensure that the matrices are in the correct format and free of NaN values
51
- neighborhoods = neighborhoods.astype(np.float32)
52
- annotations = annotations.astype(np.float32)
53
- # Retrieve the appropriate neighborhood score function based on the metric
54
- neighborhood_score_func = DISPATCH_PERMUTATION_TABLE[score_metric]
55
-
56
- # Run the permutation test to calculate depletion and enrichment counts
57
- counts_depletion, counts_enrichment = _run_permutation_test(
58
- neighborhoods=neighborhoods,
59
- annotations=annotations,
60
- neighborhood_score_func=neighborhood_score_func,
61
- null_distribution=null_distribution,
62
- num_permutations=num_permutations,
63
- random_seed=random_seed,
64
- max_workers=max_workers,
65
- )
66
-
67
- # Compute p-values for depletion and enrichment
68
- # If counts are 0, set p-value to 1/num_permutations to avoid zero p-values
69
- depletion_pvals = np.maximum(counts_depletion, 1) / num_permutations
70
- enrichment_pvals = np.maximum(counts_enrichment, 1) / num_permutations
71
-
72
- return {
73
- "depletion_pvals": depletion_pvals,
74
- "enrichment_pvals": enrichment_pvals,
75
- }
76
-
77
-
78
- def _run_permutation_test(
79
- neighborhoods: np.ndarray,
80
- annotations: np.ndarray,
81
- neighborhood_score_func: Callable,
82
- null_distribution: str = "network",
83
- num_permutations: int = 1000,
84
- random_seed: int = 888,
85
- max_workers: int = 4,
86
- ) -> tuple:
87
- """Run a permutation test to calculate enrichment and depletion counts.
88
-
89
- Args:
90
- neighborhoods (np.ndarray): The neighborhood matrix.
91
- annotations (np.ndarray): The annotation matrix.
92
- neighborhood_score_func (Callable): Function to calculate neighborhood scores.
93
- null_distribution (str, optional): Type of null distribution. Defaults to "network".
94
- num_permutations (int, optional): Number of permutations. Defaults to 1000.
95
- random_seed (int, optional): Seed for random number generation. Defaults to 888.
96
- max_workers (int, optional): Number of workers for multiprocessing. Defaults to 4.
97
-
98
- Returns:
99
- tuple: Depletion and enrichment counts.
100
- """
101
- # Set the random seed for reproducibility
102
- np.random.seed(random_seed)
103
-
104
- # Determine the indices to use based on the null distribution type
105
- if null_distribution == "network":
106
- idxs = range(annotations.shape[0])
107
- else:
108
- idxs = np.nonzero(np.sum(~np.isnan(annotations), axis=1))[0]
109
-
110
- # Replace NaNs with zeros in the annotations matrix
111
- annotations[np.isnan(annotations)] = 0
112
- annotation_matrix_obsv = annotations[idxs]
113
- neighborhoods_matrix_obsv = neighborhoods.T[idxs].T
114
-
115
- # Calculate observed neighborhood scores
116
- with np.errstate(invalid="ignore", divide="ignore"):
117
- observed_neighborhood_scores = neighborhood_score_func(
118
- neighborhoods_matrix_obsv, annotation_matrix_obsv
119
- )
120
-
121
- # Initialize count matrices for depletion and enrichment
122
- counts_depletion = np.zeros(observed_neighborhood_scores.shape)
123
- counts_enrichment = np.zeros(observed_neighborhood_scores.shape)
124
-
125
- # Determine the number of permutations to run in each worker process
126
- subset_size = num_permutations // max_workers
127
- remainder = num_permutations % max_workers
128
-
129
- # Use the spawn context for creating a new multiprocessing pool
130
- ctx = get_context("spawn")
131
- manager = Manager()
132
- progress_counter = manager.Value("i", 0)
133
- total_progress = num_permutations
134
-
135
- # Execute the permutation test using multiprocessing
136
- with ctx.Pool(max_workers) as pool:
137
- with tqdm(total=total_progress, desc="Total progress", position=0) as progress:
138
- # Prepare parameters for multiprocessing
139
- params_list = [
140
- (
141
- annotations,
142
- np.array(idxs),
143
- neighborhoods_matrix_obsv,
144
- observed_neighborhood_scores,
145
- neighborhood_score_func,
146
- subset_size + (1 if i < remainder else 0),
147
- progress_counter,
148
- )
149
- for i in range(max_workers)
150
- ]
151
-
152
- # Start the permutation process in parallel
153
- results = pool.starmap_async(_permutation_process_subset, params_list, chunksize=1)
154
-
155
- # Update progress bar based on progress_counter
156
- # NOTE: Waiting for results to be ready while updating progress bar gives a big improvement
157
- # in performance, especially for large number of permutations and workers
158
- while not results.ready():
159
- progress.update(progress_counter.value - progress.n)
160
- results.wait(0.05) # Wait for 50ms
161
-
162
- # Ensure progress bar reaches 100%
163
- progress.update(total_progress - progress.n)
164
-
165
- # Accumulate results from each worker
166
- for local_counts_depletion, local_counts_enrichment in results.get():
167
- counts_depletion = np.add(counts_depletion, local_counts_depletion)
168
- counts_enrichment = np.add(counts_enrichment, local_counts_enrichment)
169
-
170
- return counts_depletion, counts_enrichment
171
-
172
-
173
- def _permutation_process_subset(
174
- annotation_matrix: np.ndarray,
175
- idxs: np.ndarray,
176
- neighborhoods_matrix_obsv: np.ndarray,
177
- observed_neighborhood_scores: np.ndarray,
178
- neighborhood_score_func: Callable,
179
- subset_size: int,
180
- progress_counter,
181
- ) -> tuple:
182
- """Process a subset of permutations for the permutation test.
183
-
184
- Args:
185
- annotation_matrix (np.ndarray): The annotation matrix.
186
- idxs (np.ndarray): Indices of valid rows in the matrix.
187
- neighborhoods_matrix_obsv (np.ndarray): Observed neighborhoods matrix.
188
- observed_neighborhood_scores (np.ndarray): Observed neighborhood scores.
189
- neighborhood_score_func (Callable): Function to calculate neighborhood scores.
190
- subset_size (int): Number of permutations to run in this subset.
191
- progress_counter: Shared counter for tracking progress.
192
-
193
- Returns:
194
- tuple: Local counts of depletion and enrichment.
195
- """
196
- # Initialize local count matrices for this worker
197
- local_counts_depletion = np.zeros(observed_neighborhood_scores.shape)
198
- local_counts_enrichment = np.zeros(observed_neighborhood_scores.shape)
199
-
200
- with threadpool_limits(limits=1, user_api="blas"):
201
- for _ in range(subset_size):
202
- # Permute the annotation matrix
203
- annotation_matrix_permut = annotation_matrix[np.random.permutation(idxs)]
204
- # Calculate permuted neighborhood scores
205
- with np.errstate(invalid="ignore", divide="ignore"):
206
- permuted_neighborhood_scores = neighborhood_score_func(
207
- neighborhoods_matrix_obsv, annotation_matrix_permut
208
- )
209
- # Update local depletion and enrichment counts based on permuted scores
210
- local_counts_depletion = np.add(
211
- local_counts_depletion, permuted_neighborhood_scores <= observed_neighborhood_scores
212
- )
213
- local_counts_enrichment = np.add(
214
- local_counts_enrichment,
215
- permuted_neighborhood_scores >= observed_neighborhood_scores,
216
- )
217
- # Update the shared progress counter
218
- progress_counter.value += 1
219
-
220
- return local_counts_depletion, local_counts_enrichment
221
10
 
222
11
 
223
12
  def calculate_significance_matrices(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: risk-network
3
- Version: 0.0.4b1
3
+ Version: 0.0.4b3
4
4
  Summary: A Python package for biological network analysis
5
5
  Author: Ira Horecka
6
6
  Author-email: Ira Horecka <ira89@icloud.com>
@@ -722,7 +722,7 @@ Requires-Dist: tqdm
722
722
 
723
723
  ## RISK
724
724
 
725
- #### Regional Inference of Significant Kinships
725
+ #### RISK Infers Spatial Kinships
726
726
 
727
727
  RISK is a software tool for visualizing spatial relationships in networks. It aims to enhance network analysis by integrating advanced network annotation algorithms, such as Louvain and Markov Clustering, to identify key functional modules and pathways.
728
728
 
@@ -0,0 +1,30 @@
1
+ risk/__init__.py,sha256=h9TG0BQ8u5D9jZ6S4Hr0GsilUhb1gnrisxLtvMl2mbk,111
2
+ risk/constants.py,sha256=AICk3x5qRQhls_ijTb4VdbdxU6mZ1aLGbAjLEdBwfJI,550
3
+ risk/risk.py,sha256=4Y-fViqdK9gvxEjJRYsV_7gbN0kh7eiTwD8eesd_M6o,21109
4
+ risk/annotations/__init__.py,sha256=vUpVvMRE5if01Ic8QY6M2Ae3EFGJHdugEe9PdEkAW4Y,138
5
+ risk/annotations/annotations.py,sha256=t4aLwROCFHcqk8g-viuwkFG--HqVpVN_2yVVcjhg6wI,10534
6
+ risk/annotations/io.py,sha256=TMicRACfY8bNtmbvVrxHoh8zkOVLOIhZwWrpxUlR28Q,7988
7
+ risk/log/__init__.py,sha256=xuLImfxFlKpnVhzi_gDYlr2_c9cLkrw2c_3iEsXb1as,107
8
+ risk/log/console.py,sha256=im9DRExwf6wHlcn9fewoDcKIpo3vPcorZIaNAl-0csY,355
9
+ risk/log/params.py,sha256=Tbb-sovFTptGBqPDKafUA8KOpby4zFObutAT_Iti1hE,6302
10
+ risk/neighborhoods/__init__.py,sha256=tKKEg4lsbqFukpgYlUGxU_v_9FOqK7V0uvM9T2QzoL0,206
11
+ risk/neighborhoods/community.py,sha256=eL2IGT-8sbiJIyfyb_FGngev7pEMxw7tZb8YzbzOYw8,6512
12
+ risk/neighborhoods/domains.py,sha256=HwuChmZH0RGD9eQOvk2-ezQDJRUHHn93vhVgHb-kX6I,10192
13
+ risk/neighborhoods/neighborhoods.py,sha256=SqYJaT49rUj77ts0XsPXb9cURM11aGh2Teks0nBH_4s,13939
14
+ risk/network/__init__.py,sha256=iEPeJdZfqp0toxtbElryB8jbz9_t_k4QQ3iDvKE8C_0,126
15
+ risk/network/geometry.py,sha256=H1yGVVqgbfpzBzJwEheDLfvGLSA284jGQQTn612L4Vc,6759
16
+ risk/network/graph.py,sha256=m3bFWU5528OEm246LPG5XP3l_30vaBiJv4AO4iq0pSA,11552
17
+ risk/network/io.py,sha256=otiRG6uT6HLgbbJql7X2wjYxab8OFJSgRoWJlcDoyu4,20291
18
+ risk/network/plot.py,sha256=yGmuQXcju2cWUcyJJALtNmIPPCFS2yusULJXka8263I,40080
19
+ risk/stats/__init__.py,sha256=e-BE_Dr_jgiK6hKM-T-tlG4yvHnId8e5qjnM0pdwNVc,230
20
+ risk/stats/fisher_exact.py,sha256=-bPwzu76-ob0HzrTV20mXUTot7v-MLuqFaAoab-QxPg,4966
21
+ risk/stats/hypergeom.py,sha256=lrIFdhCWRjvM4apYw1MlOKqT_IY5OjtCwrjdtJdt6Tg,4954
22
+ risk/stats/stats.py,sha256=kvShov-94W6ffgDUTb522vB9hDJQSyTsYif_UIaFfSM,7059
23
+ risk/stats/permutation/__init__.py,sha256=neJp7FENC-zg_CGOXqv-iIvz1r5XUKI9Ruxhmq7kDOI,105
24
+ risk/stats/permutation/permutation.py,sha256=qLWdwxEY6nmkYPxpM8HLDcd2mbqYv9Qr7CKtJvhLqIM,9220
25
+ risk/stats/permutation/test_functions.py,sha256=HuDIM-V1jkkfE1rlaIqrWWBSKZt3dQ1f-YEDjWpnLSE,2343
26
+ risk_network-0.0.4b3.dist-info/LICENSE,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
27
+ risk_network-0.0.4b3.dist-info/METADATA,sha256=yjXG21NrHagGZgVl-T1ySHLGsItvM4xBH9j9xiQGGJ4,43236
28
+ risk_network-0.0.4b3.dist-info/WHEEL,sha256=UvcQYKBHoFqaQd6LKyqHw9fxEolWLQnlzP0h_LgJAfI,91
29
+ risk_network-0.0.4b3.dist-info/top_level.txt,sha256=NX7C2PFKTvC1JhVKv14DFlFAIFnKc6Lpsu1ZfxvQwVw,5
30
+ risk_network-0.0.4b3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (73.0.1)
2
+ Generator: setuptools (74.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,26 +0,0 @@
1
- risk/__init__.py,sha256=mAHhUWFzKCookp6VYUp1WNQXgQStNGIUXaOZ2QfFtwc,122
2
- risk/constants.py,sha256=AICk3x5qRQhls_ijTb4VdbdxU6mZ1aLGbAjLEdBwfJI,550
3
- risk/risk.py,sha256=Kaju0siA2piDWuawSB1iZcngm9BND_R7iJAqAS_55E0,14717
4
- risk/annotations/__init__.py,sha256=vUpVvMRE5if01Ic8QY6M2Ae3EFGJHdugEe9PdEkAW4Y,138
5
- risk/annotations/annotations.py,sha256=0-Yf2XPVdMzpttLLgLxtTTnH0Eed_Uv8VXMur6E6xKw,10481
6
- risk/annotations/io.py,sha256=TMicRACfY8bNtmbvVrxHoh8zkOVLOIhZwWrpxUlR28Q,7988
7
- risk/log/__init__.py,sha256=xuLImfxFlKpnVhzi_gDYlr2_c9cLkrw2c_3iEsXb1as,107
8
- risk/log/console.py,sha256=im9DRExwf6wHlcn9fewoDcKIpo3vPcorZIaNAl-0csY,355
9
- risk/log/params.py,sha256=Tbb-sovFTptGBqPDKafUA8KOpby4zFObutAT_Iti1hE,6302
10
- risk/neighborhoods/__init__.py,sha256=tKKEg4lsbqFukpgYlUGxU_v_9FOqK7V0uvM9T2QzoL0,206
11
- risk/neighborhoods/community.py,sha256=eL2IGT-8sbiJIyfyb_FGngev7pEMxw7tZb8YzbzOYw8,6512
12
- risk/neighborhoods/domains.py,sha256=HwuChmZH0RGD9eQOvk2-ezQDJRUHHn93vhVgHb-kX6I,10192
13
- risk/neighborhoods/neighborhoods.py,sha256=benMk6ZCufhi9FKo6ByaeOzwJ8y43vkFbCD-wUNaCAU,13828
14
- risk/network/__init__.py,sha256=iEPeJdZfqp0toxtbElryB8jbz9_t_k4QQ3iDvKE8C_0,126
15
- risk/network/geometry.py,sha256=euBMOVvxpj-0WZam40IbHdsT7E4WXpTwSxuGbmAGTDg,6757
16
- risk/network/graph.py,sha256=m3bFWU5528OEm246LPG5XP3l_30vaBiJv4AO4iq0pSA,11552
17
- risk/network/io.py,sha256=Vn__hWla0EI9djIv7mWc1bFSTS2pRpUwOjPCqMWQOYI,19689
18
- risk/network/plot.py,sha256=-8LyRqeQi4cxEVxNsTDCyHhmTfFuObLLaFbKQGZwHCI,38289
19
- risk/stats/__init__.py,sha256=4s9gdJo5B1G_cQc0iMoeIBt5OrQNXkdtNXkAMFQkLxc,103
20
- risk/stats/permutation.py,sha256=xgaZbaxo57t_FzPlpcb2nsq8oCzc_wj-zAm-xj3P2dA,3404
21
- risk/stats/stats.py,sha256=7Z-1sJ5zvS3XDjG2UfOV0Y3c3xliYeB-6fk_UHNqfiE,16019
22
- risk_network-0.0.4b1.dist-info/LICENSE,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
23
- risk_network-0.0.4b1.dist-info/METADATA,sha256=YyRQOzpjmA1i506SBsBljqbz2hthqVYMfaSL5xIyz7I,43250
24
- risk_network-0.0.4b1.dist-info/WHEEL,sha256=Mdi9PDNwEZptOjTlUcAth7XJDFtKrHYaQMPulZeBCiQ,91
25
- risk_network-0.0.4b1.dist-info/top_level.txt,sha256=NX7C2PFKTvC1JhVKv14DFlFAIFnKc6Lpsu1ZfxvQwVw,5
26
- risk_network-0.0.4b1.dist-info/RECORD,,