risk-network 0.0.10__py3-none-any.whl → 0.0.12b0__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.
Files changed (41) hide show
  1. risk/__init__.py +1 -1
  2. risk/risk.py +5 -6
  3. {risk_network-0.0.10.dist-info → risk_network-0.0.12b0.dist-info}/METADATA +11 -13
  4. risk_network-0.0.12b0.dist-info/RECORD +7 -0
  5. {risk_network-0.0.10.dist-info → risk_network-0.0.12b0.dist-info}/WHEEL +1 -1
  6. risk/annotations/__init__.py +0 -7
  7. risk/annotations/annotations.py +0 -394
  8. risk/annotations/io.py +0 -240
  9. risk/log/__init__.py +0 -11
  10. risk/log/console.py +0 -141
  11. risk/log/parameters.py +0 -172
  12. risk/neighborhoods/__init__.py +0 -8
  13. risk/neighborhoods/api.py +0 -442
  14. risk/neighborhoods/community.py +0 -412
  15. risk/neighborhoods/domains.py +0 -358
  16. risk/neighborhoods/neighborhoods.py +0 -508
  17. risk/network/__init__.py +0 -6
  18. risk/network/geometry.py +0 -150
  19. risk/network/graph/__init__.py +0 -6
  20. risk/network/graph/api.py +0 -200
  21. risk/network/graph/graph.py +0 -269
  22. risk/network/graph/summary.py +0 -254
  23. risk/network/io.py +0 -550
  24. risk/network/plotter/__init__.py +0 -6
  25. risk/network/plotter/api.py +0 -54
  26. risk/network/plotter/canvas.py +0 -291
  27. risk/network/plotter/contour.py +0 -330
  28. risk/network/plotter/labels.py +0 -924
  29. risk/network/plotter/network.py +0 -294
  30. risk/network/plotter/plotter.py +0 -143
  31. risk/network/plotter/utils/colors.py +0 -416
  32. risk/network/plotter/utils/layout.py +0 -94
  33. risk/stats/__init__.py +0 -15
  34. risk/stats/permutation/__init__.py +0 -6
  35. risk/stats/permutation/permutation.py +0 -237
  36. risk/stats/permutation/test_functions.py +0 -69
  37. risk/stats/significance.py +0 -166
  38. risk/stats/stat_tests.py +0 -267
  39. risk_network-0.0.10.dist-info/RECORD +0 -40
  40. {risk_network-0.0.10.dist-info → risk_network-0.0.12b0.dist-info/licenses}/LICENSE +0 -0
  41. {risk_network-0.0.10.dist-info → risk_network-0.0.12b0.dist-info}/top_level.txt +0 -0
risk/stats/stat_tests.py DELETED
@@ -1,267 +0,0 @@
1
- """
2
- risk/stats/stat_tests
3
- ~~~~~~~~~~~~~~~~~~~~~
4
- """
5
-
6
- from typing import Any, Dict
7
-
8
- import numpy as np
9
- from scipy.sparse import csr_matrix
10
- from scipy.stats import binom
11
- from scipy.stats import chi2
12
- from scipy.stats import hypergeom
13
- from scipy.stats import norm
14
- from scipy.stats import poisson
15
-
16
-
17
- def compute_binom_test(
18
- neighborhoods: csr_matrix,
19
- annotations: csr_matrix,
20
- null_distribution: str = "network",
21
- ) -> Dict[str, Any]:
22
- """Compute Binomial test for enrichment and depletion in neighborhoods with selectable null distribution.
23
-
24
- Args:
25
- neighborhoods (csr_matrix): Sparse binary matrix representing neighborhoods.
26
- annotations (csr_matrix): Sparse binary matrix representing annotations.
27
- null_distribution (str, optional): Type of null distribution ('network' or 'annotations'). Defaults to "network".
28
-
29
- Returns:
30
- Dict[str, Any]: Dictionary containing depletion and enrichment p-values.
31
- """
32
- # Get the total number of nodes in the network
33
- total_nodes = neighborhoods.shape[1]
34
-
35
- # Compute sums (remain sparse here)
36
- neighborhood_sizes = neighborhoods.sum(axis=1) # Row sums
37
- annotation_totals = annotations.sum(axis=0) # Column sums
38
- # Compute probabilities (convert to dense)
39
- if null_distribution == "network":
40
- p_values = (annotation_totals / total_nodes).A.flatten() # Dense 1D array
41
- elif null_distribution == "annotations":
42
- p_values = (annotation_totals / annotations.sum()).A.flatten() # Dense 1D array
43
- else:
44
- raise ValueError(
45
- "Invalid null_distribution value. Choose either 'network' or 'annotations'."
46
- )
47
-
48
- # Observed counts (sparse matrix multiplication)
49
- annotated_counts = neighborhoods @ annotations # Sparse result
50
- annotated_counts_dense = annotated_counts.toarray() # Convert for dense operations
51
-
52
- # Compute enrichment and depletion p-values
53
- enrichment_pvals = 1 - binom.cdf(annotated_counts_dense - 1, neighborhood_sizes.A, p_values)
54
- depletion_pvals = binom.cdf(annotated_counts_dense, neighborhood_sizes.A, p_values)
55
-
56
- return {"enrichment_pvals": enrichment_pvals, "depletion_pvals": depletion_pvals}
57
-
58
-
59
- def compute_chi2_test(
60
- neighborhoods: csr_matrix,
61
- annotations: csr_matrix,
62
- null_distribution: str = "network",
63
- ) -> Dict[str, Any]:
64
- """Compute chi-squared test for enrichment and depletion in neighborhoods with selectable null distribution.
65
-
66
- Args:
67
- neighborhoods (csr_matrix): Sparse binary matrix representing neighborhoods.
68
- annotations (csr_matrix): Sparse binary matrix representing annotations.
69
- null_distribution (str, optional): Type of null distribution ('network' or 'annotations'). Defaults to "network".
70
-
71
- Returns:
72
- Dict[str, Any]: Dictionary containing depletion and enrichment p-values.
73
- """
74
- # Total number of nodes in the network
75
- total_node_count = neighborhoods.shape[0]
76
-
77
- if null_distribution == "network":
78
- # Case 1: Use all nodes as the background
79
- background_population = total_node_count
80
- neighborhood_sums = neighborhoods.sum(axis=0) # Column sums of neighborhoods
81
- annotation_sums = annotations.sum(axis=0) # Column sums of annotations
82
- elif null_distribution == "annotations":
83
- # Case 2: Only consider nodes with at least one annotation
84
- annotated_nodes = (
85
- np.ravel(annotations.sum(axis=1)) > 0
86
- ) # Row-wise sum to filter nodes with annotations
87
- background_population = annotated_nodes.sum() # Total number of annotated nodes
88
- neighborhood_sums = neighborhoods[annotated_nodes].sum(
89
- axis=0
90
- ) # Neighborhood sums for annotated nodes
91
- annotation_sums = annotations[annotated_nodes].sum(
92
- axis=0
93
- ) # Annotation sums for annotated nodes
94
- else:
95
- raise ValueError(
96
- "Invalid null_distribution value. Choose either 'network' or 'annotations'."
97
- )
98
-
99
- # Convert to dense arrays for downstream computations
100
- neighborhood_sums = np.asarray(neighborhood_sums).reshape(-1, 1) # Ensure column vector shape
101
- annotation_sums = np.asarray(annotation_sums).reshape(1, -1) # Ensure row vector shape
102
-
103
- # Observed values: number of annotated nodes in each neighborhood
104
- observed = neighborhoods.T @ annotations # Shape: (neighborhoods, annotations)
105
- # Expected values under the null
106
- expected = (neighborhood_sums @ annotation_sums) / background_population
107
- # Chi-squared statistic: sum((observed - expected)^2 / expected)
108
- with np.errstate(divide="ignore", invalid="ignore"): # Handle divide-by-zero
109
- chi2_stat = np.where(expected > 0, np.power(observed - expected, 2) / expected, 0)
110
-
111
- # Compute p-values for enrichment (upper tail) and depletion (lower tail)
112
- enrichment_pvals = chi2.sf(chi2_stat, df=1) # Survival function for upper tail
113
- depletion_pvals = chi2.cdf(chi2_stat, df=1) # Cumulative distribution for lower tail
114
-
115
- return {"depletion_pvals": depletion_pvals, "enrichment_pvals": enrichment_pvals}
116
-
117
-
118
- def compute_hypergeom_test(
119
- neighborhoods: csr_matrix,
120
- annotations: csr_matrix,
121
- null_distribution: str = "network",
122
- ) -> Dict[str, Any]:
123
- """
124
- Compute hypergeometric test for enrichment and depletion in neighborhoods with selectable null distribution.
125
-
126
- Args:
127
- neighborhoods (csr_matrix): Sparse binary matrix representing neighborhoods.
128
- annotations (csr_matrix): Sparse binary matrix representing annotations.
129
- null_distribution (str, optional): Type of null distribution ('network' or 'annotations'). Defaults to "network".
130
-
131
- Returns:
132
- Dict[str, Any]: Dictionary containing depletion and enrichment p-values.
133
- """
134
- # Get the total number of nodes in the network
135
- total_nodes = neighborhoods.shape[1]
136
-
137
- # Compute sums
138
- neighborhood_sums = neighborhoods.sum(axis=0).A.flatten() # Convert to dense array
139
- annotation_sums = annotations.sum(axis=0).A.flatten() # Convert to dense array
140
-
141
- if null_distribution == "network":
142
- background_population = total_nodes
143
- elif null_distribution == "annotations":
144
- annotated_nodes = annotations.sum(axis=1).A.flatten() > 0 # Boolean mask
145
- background_population = annotated_nodes.sum()
146
- neighborhood_sums = neighborhoods[annotated_nodes].sum(axis=0).A.flatten()
147
- annotation_sums = annotations[annotated_nodes].sum(axis=0).A.flatten()
148
- else:
149
- raise ValueError(
150
- "Invalid null_distribution value. Choose either 'network' or 'annotations'."
151
- )
152
-
153
- # Observed counts
154
- annotated_in_neighborhood = neighborhoods.T @ annotations # Sparse result
155
- annotated_in_neighborhood = annotated_in_neighborhood.toarray() # Convert to dense
156
- # Align shapes for broadcasting
157
- neighborhood_sums = neighborhood_sums.reshape(-1, 1)
158
- annotation_sums = annotation_sums.reshape(1, -1)
159
- background_population = np.array(background_population).reshape(1, 1)
160
-
161
- # Compute hypergeometric p-values
162
- depletion_pvals = hypergeom.cdf(
163
- annotated_in_neighborhood, background_population, annotation_sums, neighborhood_sums
164
- )
165
- enrichment_pvals = hypergeom.sf(
166
- annotated_in_neighborhood - 1, background_population, annotation_sums, neighborhood_sums
167
- )
168
-
169
- return {"depletion_pvals": depletion_pvals, "enrichment_pvals": enrichment_pvals}
170
-
171
-
172
- def compute_poisson_test(
173
- neighborhoods: csr_matrix,
174
- annotations: csr_matrix,
175
- null_distribution: str = "network",
176
- ) -> Dict[str, Any]:
177
- """
178
- Compute Poisson test for enrichment and depletion in neighborhoods with selectable null distribution.
179
-
180
- Args:
181
- neighborhoods (csr_matrix): Sparse binary matrix representing neighborhoods.
182
- annotations (csr_matrix): Sparse binary matrix representing annotations.
183
- null_distribution (str, optional): Type of null distribution ('network' or 'annotations'). Defaults to "network".
184
-
185
- Returns:
186
- Dict[str, Any]: Dictionary containing depletion and enrichment p-values.
187
- """
188
- # Matrix multiplication to get the number of annotated nodes in each neighborhood
189
- annotated_in_neighborhood = neighborhoods @ annotations # Sparse result
190
- # Convert annotated counts to dense for downstream calculations
191
- annotated_in_neighborhood_dense = annotated_in_neighborhood.toarray()
192
-
193
- # Compute lambda_expected based on the chosen null distribution
194
- if null_distribution == "network":
195
- # Use the mean across neighborhoods (axis=1)
196
- lambda_expected = np.mean(annotated_in_neighborhood_dense, axis=1, keepdims=True)
197
- elif null_distribution == "annotations":
198
- # Use the mean across annotations (axis=0)
199
- lambda_expected = np.mean(annotated_in_neighborhood_dense, axis=0, keepdims=True)
200
- else:
201
- raise ValueError(
202
- "Invalid null_distribution value. Choose either 'network' or 'annotations'."
203
- )
204
-
205
- # Compute p-values for enrichment and depletion using Poisson distribution
206
- enrichment_pvals = 1 - poisson.cdf(annotated_in_neighborhood_dense - 1, lambda_expected)
207
- depletion_pvals = poisson.cdf(annotated_in_neighborhood_dense, lambda_expected)
208
-
209
- return {"enrichment_pvals": enrichment_pvals, "depletion_pvals": depletion_pvals}
210
-
211
-
212
- def compute_zscore_test(
213
- neighborhoods: csr_matrix,
214
- annotations: csr_matrix,
215
- null_distribution: str = "network",
216
- ) -> Dict[str, Any]:
217
- """
218
- Compute z-score test for enrichment and depletion in neighborhoods with selectable null distribution.
219
-
220
- Args:
221
- neighborhoods (csr_matrix): Sparse binary matrix representing neighborhoods.
222
- annotations (csr_matrix): Sparse binary matrix representing annotations.
223
- null_distribution (str, optional): Type of null distribution ('network' or 'annotations'). Defaults to "network".
224
-
225
- Returns:
226
- Dict[str, Any]: Dictionary containing depletion and enrichment p-values.
227
- """
228
- # Total number of nodes in the network
229
- total_node_count = neighborhoods.shape[1]
230
-
231
- # Compute sums
232
- if null_distribution == "network":
233
- background_population = total_node_count
234
- neighborhood_sums = neighborhoods.sum(axis=0).A.flatten() # Dense column sums
235
- annotation_sums = annotations.sum(axis=0).A.flatten() # Dense row sums
236
- elif null_distribution == "annotations":
237
- annotated_nodes = annotations.sum(axis=1).A.flatten() > 0 # Dense boolean mask
238
- background_population = annotated_nodes.sum()
239
- neighborhood_sums = neighborhoods[annotated_nodes].sum(axis=0).A.flatten()
240
- annotation_sums = annotations[annotated_nodes].sum(axis=0).A.flatten()
241
- else:
242
- raise ValueError(
243
- "Invalid null_distribution value. Choose either 'network' or 'annotations'."
244
- )
245
-
246
- # Observed values
247
- observed = (neighborhoods.T @ annotations).toarray() # Convert sparse result to dense
248
- # Expected values under the null
249
- neighborhood_sums = neighborhood_sums.reshape(-1, 1) # Ensure correct shape
250
- annotation_sums = annotation_sums.reshape(1, -1) # Ensure correct shape
251
- expected = (neighborhood_sums @ annotation_sums) / background_population
252
-
253
- # Standard deviation under the null
254
- std_dev = np.sqrt(
255
- expected
256
- * (1 - annotation_sums / background_population)
257
- * (1 - neighborhood_sums / background_population)
258
- )
259
- std_dev[std_dev == 0] = np.nan # Avoid division by zero
260
- # Compute z-scores
261
- z_scores = (observed - expected) / std_dev
262
-
263
- # Convert z-scores to depletion and enrichment p-values
264
- enrichment_pvals = norm.sf(z_scores) # Upper tail
265
- depletion_pvals = norm.cdf(z_scores) # Lower tail
266
-
267
- return {"depletion_pvals": depletion_pvals, "enrichment_pvals": enrichment_pvals}
@@ -1,40 +0,0 @@
1
- risk/__init__.py,sha256=256SpbomSJ88a12A8DmDUsPOxlkwJ7cqbu7E4FHrcw4,120
2
- risk/risk.py,sha256=s827_lRknFseOP9O4zW8sP-IcCd2EzrpV_tnVY_tz5s,1104
3
- risk/annotations/__init__.py,sha256=parsbcux1U4urpUqh9AdzbDWuLj9HlMidycMPkpSQFo,179
4
- risk/annotations/annotations.py,sha256=mDgW5X4wck7oQqcMOw1T0XNkFZnSm1vx3MKIUX-FABI,16505
5
- risk/annotations/io.py,sha256=z1AJySsU-KL_IYuHa7j3nvuczmOHgK3WfaQ4TRunvrA,10499
6
- risk/log/__init__.py,sha256=7LxDysQu7doi0LAvlY2YbjN6iJH0fNknqy8lSLgeljo,217
7
- risk/log/console.py,sha256=PgjyEvyhYLUSHXPUKEqOmxsDsfrjPICIgqo_cAHq0N8,4575
8
- risk/log/parameters.py,sha256=VtwfMzLU1xI4yji3-Ch5vHjH-KdwTfwaEMmi7hFQTs0,5716
9
- risk/neighborhoods/__init__.py,sha256=Q74HwTH7okI-vaskJPy2bYwb5sNjGASTzJ6m8V8arCU,234
10
- risk/neighborhoods/api.py,sha256=ywngw2TQVV27gYlWDXcs8-qnmeepnvb-W9ov6J6VEPM,23341
11
- risk/neighborhoods/community.py,sha256=5Q_-VAJC-5SY5EUsB8gIlemeDoAL85uLjyl16pItHiQ,16699
12
- risk/neighborhoods/domains.py,sha256=INWgpAy4cLsfBb5exkh3MEAIgRreNH6d7UlKjVijHX4,14678
13
- risk/neighborhoods/neighborhoods.py,sha256=l9FhADB1C-OxM8E9QXOcA4osUDgA1vs4ud-OCGKKybc,21457
14
- risk/network/__init__.py,sha256=oVi3FA1XXKD84014Cykq-9bpX4_s0F3aAUfNOU-07Qw,73
15
- risk/network/geometry.py,sha256=c9XweJ0-DImZPU2YAJTJAwVYnW_0OGlWClyvJpeIZE4,5476
16
- risk/network/io.py,sha256=RCH4nQdgYDXcNwMfpSz7qEmPO0pJ1p9fL0rNQptsQrc,21673
17
- risk/network/graph/__init__.py,sha256=ziGJew3yhtqvrb9LUuneDu_LwW2Wa9vd4UuhoL5l1CA,91
18
- risk/network/graph/api.py,sha256=xS_rNDvZPdwIar2E9x9BKMeR0DcYuwcHiUpc_EcJ4-o,8536
19
- risk/network/graph/graph.py,sha256=qEWyZvuaGT_vvjhreBdmRPX3gst2wQFaXhFAvikPSqw,12158
20
- risk/network/graph/summary.py,sha256=Y_0rL2C1UoQeZQIPVe5LbaCO356Mcc8HisnrXwQsRm8,10289
21
- risk/network/plotter/__init__.py,sha256=4gWtQHGzQVNHmEBXi31Zf0tX0y2sTcE66J_yGnn7268,99
22
- risk/network/plotter/api.py,sha256=oJIj7vYv-3VpfN41ndCNtxcWIuhT2ULwAaPPU2f4oeM,1785
23
- risk/network/plotter/canvas.py,sha256=ifyTMyXYRzlcdSYy6C23k3dmwtbLDrOfdMvEjkW2gLg,13460
24
- risk/network/plotter/contour.py,sha256=oQDKmAKaEasnK1zqY7_bNctZ_IevZW2vxrbsnSrOSCI,15459
25
- risk/network/plotter/labels.py,sha256=k5GWvgHS8bLekJk7Gtxy6G7tDeJDZPQ-z3VxYWjAWRM,45489
26
- risk/network/plotter/network.py,sha256=0VySlJ4n3tkHsOhVVSa3yiSppT8y1dmIwa-DhRn0tcM,14131
27
- risk/network/plotter/plotter.py,sha256=4PeAeutJbgvwy4USh5RdHALLtkmeAtaxQcd48r7Zxa0,5999
28
- risk/network/plotter/utils/colors.py,sha256=EaiKsNPy_lcjrPp-QTNy3LnQdAZMjz2LegbeffMz2HM,18910
29
- risk/network/plotter/utils/layout.py,sha256=OPqV8jzV9dpnOhYU4SYMSfsIXalVzESrlBSI_Y43OGU,3640
30
- risk/stats/__init__.py,sha256=2zdLv3tUHKyAjwAo7LprVXRaak1cHgrpYMVMSik6JM4,324
31
- risk/stats/significance.py,sha256=6cKv2xBQXWTHZ6HpNWIqlNfKKS5pG_BcCUdMM3r_zw4,7336
32
- risk/stats/stat_tests.py,sha256=tj0ri9w89_1fsjGLuafTWpfBEwZXpSLn7Ej2aAQ5lxk,11776
33
- risk/stats/permutation/__init__.py,sha256=OLmYLm2uj96hPsSaUs0vUqFYw6Thwch_aHtpL7L0ZFw,127
34
- risk/stats/permutation/permutation.py,sha256=BWjgdBpLVcHvmwHy0bmD4aJFccxifNBSrrCBPppyKf4,10569
35
- risk/stats/permutation/test_functions.py,sha256=KlECWTz1EZ6EPF_OAgHb0uznaIhopiVYb_AKUKuC4no,3120
36
- risk_network-0.0.10.dist-info/LICENSE,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
37
- risk_network-0.0.10.dist-info/METADATA,sha256=MbLHMXmOv8iTUZLm8S4EKj2uDB419weRpRvWIyBKSCM,46966
38
- risk_network-0.0.10.dist-info/WHEEL,sha256=52BFRY2Up02UkjOa29eZOS2VxUrpPORXg1pkohGGUS8,91
39
- risk_network-0.0.10.dist-info/top_level.txt,sha256=NX7C2PFKTvC1JhVKv14DFlFAIFnKc6Lpsu1ZfxvQwVw,5
40
- risk_network-0.0.10.dist-info/RECORD,,