risk-network 0.0.5b4__tar.gz → 0.0.5b5__tar.gz
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_network-0.0.5b4 → risk_network-0.0.5b5}/PKG-INFO +1 -1
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/__init__.py +1 -1
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/annotations/annotations.py +2 -2
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/network/graph.py +44 -17
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/network/plot.py +6 -4
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk_network.egg-info/PKG-INFO +1 -1
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/LICENSE +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/MANIFEST.in +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/README.md +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/pyproject.toml +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/annotations/__init__.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/annotations/io.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/constants.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/log/__init__.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/log/console.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/log/params.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/neighborhoods/__init__.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/neighborhoods/community.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/neighborhoods/domains.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/neighborhoods/neighborhoods.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/network/__init__.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/network/geometry.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/network/io.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/risk.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/stats/__init__.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/stats/fisher_exact.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/stats/hypergeom.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/stats/permutation/__init__.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/stats/permutation/permutation.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/stats/permutation/test_functions.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk/stats/stats.py +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk_network.egg-info/SOURCES.txt +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk_network.egg-info/dependency_links.txt +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk_network.egg-info/requires.txt +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/risk_network.egg-info/top_level.txt +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/setup.cfg +0 -0
- {risk_network-0.0.5b4 → risk_network-0.0.5b5}/setup.py +0 -0
@@ -207,7 +207,7 @@ def _simplify_word_list(words: List[str], threshold: float = 0.80) -> List[str]:
|
|
207
207
|
similar_words = [
|
208
208
|
other_word
|
209
209
|
for other_word in word_counts
|
210
|
-
if
|
210
|
+
if _calculate_jaccard_index(word_set, set(other_word)) >= threshold
|
211
211
|
]
|
212
212
|
# Sort by frequency and choose the most frequent word
|
213
213
|
similar_words.sort(key=lambda w: word_counts[w], reverse=True)
|
@@ -220,7 +220,7 @@ def _simplify_word_list(words: List[str], threshold: float = 0.80) -> List[str]:
|
|
220
220
|
return final_words
|
221
221
|
|
222
222
|
|
223
|
-
def
|
223
|
+
def _calculate_jaccard_index(set1: Set[Any], set2: Set[Any]) -> float:
|
224
224
|
"""Calculate the Jaccard Index of two sets.
|
225
225
|
|
226
226
|
Args:
|
@@ -5,7 +5,7 @@ risk/network/graph
|
|
5
5
|
|
6
6
|
import random
|
7
7
|
from collections import defaultdict
|
8
|
-
from typing import Any, Dict, List, Tuple
|
8
|
+
from typing import Any, Dict, List, Tuple, Union
|
9
9
|
|
10
10
|
import networkx as nx
|
11
11
|
import numpy as np
|
@@ -100,7 +100,12 @@ class NetworkGraph:
|
|
100
100
|
self.node_coordinates = _extract_node_coordinates(G_2d)
|
101
101
|
|
102
102
|
def get_domain_colors(
|
103
|
-
self,
|
103
|
+
self,
|
104
|
+
min_scale: float = 0.8,
|
105
|
+
max_scale: float = 1.0,
|
106
|
+
scale_factor: float = 1.0,
|
107
|
+
random_seed: int = 888,
|
108
|
+
**kwargs,
|
104
109
|
) -> np.ndarray:
|
105
110
|
"""Generate composite colors for domains.
|
106
111
|
|
@@ -111,6 +116,8 @@ class NetworkGraph:
|
|
111
116
|
Args:
|
112
117
|
min_scale (float, optional): Minimum scale for color intensity. Defaults to 0.8.
|
113
118
|
max_scale (float, optional): Maximum scale for color intensity. Defaults to 1.0.
|
119
|
+
scale_factor (float, optional): Exponent for scaling, where values > 1 increase contrast by dimming small
|
120
|
+
values more. Defaults to 1.0.
|
114
121
|
random_seed (int, optional): Seed for random number generation. Defaults to 888.
|
115
122
|
**kwargs: Additional keyword arguments for color generation.
|
116
123
|
|
@@ -118,7 +125,7 @@ class NetworkGraph:
|
|
118
125
|
np.ndarray: Array of transformed colors.
|
119
126
|
"""
|
120
127
|
# Get colors for each domain
|
121
|
-
domain_colors = self._get_domain_colors(
|
128
|
+
domain_colors = self._get_domain_colors(random_seed=random_seed)
|
122
129
|
# Generate composite colors for nodes
|
123
130
|
node_colors = self._get_composite_node_colors(domain_colors)
|
124
131
|
# Transform colors to ensure proper alpha values and intensity
|
@@ -127,6 +134,7 @@ class NetworkGraph:
|
|
127
134
|
self.node_enrichment_sums,
|
128
135
|
min_scale=min_scale,
|
129
136
|
max_scale=max_scale,
|
137
|
+
scale_factor=scale_factor,
|
130
138
|
)
|
131
139
|
|
132
140
|
return transformed_colors
|
@@ -152,9 +160,15 @@ class NetworkGraph:
|
|
152
160
|
|
153
161
|
return composite_colors
|
154
162
|
|
155
|
-
def _get_domain_colors(
|
163
|
+
def _get_domain_colors(
|
164
|
+
self, color: Union[str, None] = None, random_seed: int = 888
|
165
|
+
) -> Dict[str, Any]:
|
156
166
|
"""Get colors for each domain.
|
157
167
|
|
168
|
+
Args:
|
169
|
+
color (Union[str, None], optional): Specific color to use for all domains. If specified, it will overwrite the colormap.
|
170
|
+
random_seed (int, optional): Seed for random number generation. Defaults to 888.
|
171
|
+
|
158
172
|
Returns:
|
159
173
|
dict: A dictionary mapping domain keys to their corresponding RGBA colors.
|
160
174
|
"""
|
@@ -163,20 +177,28 @@ class NetworkGraph:
|
|
163
177
|
col for col in self.domains.columns if isinstance(col, (int, np.integer))
|
164
178
|
]
|
165
179
|
domains = np.sort(numeric_domains)
|
166
|
-
domain_colors = _get_colors(
|
180
|
+
domain_colors = _get_colors(
|
181
|
+
num_colors_to_generate=len(domains), color=color, random_seed=random_seed
|
182
|
+
)
|
167
183
|
return dict(zip(self.domain_to_nodes.keys(), domain_colors))
|
168
184
|
|
169
185
|
|
170
186
|
def _transform_colors(
|
171
|
-
colors: np.ndarray,
|
187
|
+
colors: np.ndarray,
|
188
|
+
enrichment_sums: np.ndarray,
|
189
|
+
min_scale: float = 0.8,
|
190
|
+
max_scale: float = 1.0,
|
191
|
+
scale_factor: float = 1.0,
|
172
192
|
) -> np.ndarray:
|
173
|
-
"""Transform colors
|
193
|
+
"""Transform colors using power scaling to emphasize high enrichment sums more.
|
174
194
|
|
175
195
|
Args:
|
176
196
|
colors (np.ndarray): An array of RGBA colors.
|
177
197
|
enrichment_sums (np.ndarray): An array of enrichment sums corresponding to the colors.
|
178
198
|
min_scale (float, optional): Minimum scale for color intensity. Defaults to 0.8.
|
179
199
|
max_scale (float, optional): Maximum scale for color intensity. Defaults to 1.0.
|
200
|
+
scale_factor (float, optional): Exponent for scaling, where values > 1 increase contrast by dimming small
|
201
|
+
values more. Defaults to 1.0.
|
180
202
|
|
181
203
|
Returns:
|
182
204
|
np.ndarray: The transformed array of RGBA colors with adjusted intensities.
|
@@ -184,11 +206,12 @@ def _transform_colors(
|
|
184
206
|
if min_scale == max_scale:
|
185
207
|
min_scale = max_scale - 10e-6 # Avoid division by zero
|
186
208
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
209
|
+
# Normalize the enrichment sums to the range [0, 1]
|
210
|
+
normalized_sums = enrichment_sums / np.max(enrichment_sums)
|
211
|
+
# Apply power scaling to dim lower values and emphasize higher values
|
212
|
+
scaled_sums = normalized_sums**scale_factor
|
213
|
+
# Linearly scale the normalized sums to the range [min_scale, max_scale]
|
214
|
+
scaled_sums = min_scale + (max_scale - min_scale) * scaled_sums
|
192
215
|
# Adjust RGB values based on scaled sums
|
193
216
|
for i in range(3): # Only adjust RGB values
|
194
217
|
colors[:, i] = scaled_sums * colors[:, i]
|
@@ -249,7 +272,10 @@ def _extract_node_coordinates(G: nx.Graph) -> np.ndarray:
|
|
249
272
|
|
250
273
|
|
251
274
|
def _get_colors(
|
252
|
-
num_colors_to_generate: int = 10,
|
275
|
+
num_colors_to_generate: int = 10,
|
276
|
+
cmap: str = "hsv",
|
277
|
+
random_seed: int = 888,
|
278
|
+
color: Union[str, None] = None,
|
253
279
|
) -> List[Tuple]:
|
254
280
|
"""Generate a list of RGBA colors from a specified colormap or use a direct color string.
|
255
281
|
|
@@ -257,16 +283,17 @@ def _get_colors(
|
|
257
283
|
num_colors_to_generate (int): The number of colors to generate. Defaults to 10.
|
258
284
|
cmap (str): The name of the colormap to use. Defaults to "hsv".
|
259
285
|
random_seed (int): Seed for random number generation. Defaults to 888.
|
260
|
-
|
286
|
+
color (str, optional): Specific color to use for all nodes. If specified, it will overwrite the colormap.
|
287
|
+
Defaults to None.
|
261
288
|
|
262
289
|
Returns:
|
263
290
|
list of tuple: List of RGBA colors.
|
264
291
|
"""
|
265
292
|
# Set random seed for reproducibility
|
266
293
|
random.seed(random_seed)
|
267
|
-
if
|
268
|
-
# If a direct color
|
269
|
-
rgba = matplotlib.colors.to_rgba(
|
294
|
+
if color:
|
295
|
+
# If a direct color is provided, generate a list with that color
|
296
|
+
rgba = matplotlib.colors.to_rgba(color)
|
270
297
|
rgbas = [rgba] * num_colors_to_generate
|
271
298
|
else:
|
272
299
|
colormap = matplotlib.colormaps.get_cmap(cmap)
|
@@ -512,7 +512,7 @@ class NetworkPlotter:
|
|
512
512
|
self.graph.node_coordinates, radius_margin=perimeter_scale
|
513
513
|
)
|
514
514
|
# Calculate the best positions for labels around the perimeter
|
515
|
-
best_label_positions =
|
515
|
+
best_label_positions = _calculate_best_label_positions(
|
516
516
|
filtered_domain_centroids, center, radius, offset
|
517
517
|
)
|
518
518
|
# Annotate the network with labels - valid_indices is used for fontcolor and arrow_color
|
@@ -795,7 +795,7 @@ def _calculate_bounding_box(
|
|
795
795
|
return center, radius
|
796
796
|
|
797
797
|
|
798
|
-
def
|
798
|
+
def _calculate_best_label_positions(
|
799
799
|
filtered_domain_centroids: Dict[str, Any], center: np.ndarray, radius: float, offset: float
|
800
800
|
) -> Dict[str, Any]:
|
801
801
|
"""Calculate and optimize label positions for clarity.
|
@@ -811,7 +811,9 @@ def _best_label_positions(
|
|
811
811
|
"""
|
812
812
|
num_domains = len(filtered_domain_centroids)
|
813
813
|
# Calculate equidistant positions around the center for initial label placement
|
814
|
-
equidistant_positions =
|
814
|
+
equidistant_positions = _calculate_equidistant_positions_around_center(
|
815
|
+
center, radius, offset, num_domains
|
816
|
+
)
|
815
817
|
# Create a mapping of domains to their initial label positions
|
816
818
|
label_positions = {
|
817
819
|
domain: position
|
@@ -821,7 +823,7 @@ def _best_label_positions(
|
|
821
823
|
return _optimize_label_positions(label_positions, filtered_domain_centroids)
|
822
824
|
|
823
825
|
|
824
|
-
def
|
826
|
+
def _calculate_equidistant_positions_around_center(
|
825
827
|
center: np.ndarray, radius: float, label_offset: float, num_domains: int
|
826
828
|
) -> List[np.ndarray]:
|
827
829
|
"""Calculate positions around a center at equidistant angles.
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|