risk-network 0.0.9b7__py3-none-any.whl → 0.0.9b8__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/network/plot/utils/colors.py +38 -26
- {risk_network-0.0.9b7.dist-info → risk_network-0.0.9b8.dist-info}/METADATA +1 -1
- {risk_network-0.0.9b7.dist-info → risk_network-0.0.9b8.dist-info}/RECORD +7 -7
- {risk_network-0.0.9b7.dist-info → risk_network-0.0.9b8.dist-info}/LICENSE +0 -0
- {risk_network-0.0.9b7.dist-info → risk_network-0.0.9b8.dist-info}/WHEEL +0 -0
- {risk_network-0.0.9b7.dist-info → risk_network-0.0.9b8.dist-info}/top_level.txt +0 -0
risk/__init__.py
CHANGED
@@ -9,6 +9,7 @@ import matplotlib
|
|
9
9
|
import matplotlib.colors as mcolors
|
10
10
|
import networkx as nx
|
11
11
|
import numpy as np
|
12
|
+
from sklearn.cluster import AgglomerativeClustering
|
12
13
|
|
13
14
|
from risk.network.graph import NetworkGraph
|
14
15
|
from risk.network.plot.utils.layout import calculate_centroids
|
@@ -249,34 +250,53 @@ def _get_colors(
|
|
249
250
|
"""
|
250
251
|
# Set random seed for reproducibility
|
251
252
|
np.random.seed(random_seed)
|
253
|
+
|
252
254
|
# Determine the number of colors to generate based on the number of domains
|
253
|
-
|
255
|
+
num_domains = len(domain_id_to_node_ids_map)
|
254
256
|
if color:
|
255
257
|
# Generate all colors as the same specified color
|
256
|
-
rgba = to_rgba(color, num_repeats=
|
258
|
+
rgba = to_rgba(color, num_repeats=num_domains)
|
257
259
|
return rgba
|
258
260
|
|
259
261
|
# Load colormap
|
260
262
|
colormap = matplotlib.colormaps.get_cmap(cmap)
|
261
263
|
# Step 1: Calculate centroids for each domain
|
262
264
|
centroids = calculate_centroids(network, domain_id_to_node_ids_map)
|
263
|
-
# Step 2: Calculate pairwise distances between centroids
|
264
265
|
centroid_array = np.array(centroids)
|
266
|
+
|
267
|
+
# Step 2: Cluster domains based on proximity using Agglomerative Clustering
|
265
268
|
dist_matrix = np.linalg.norm(centroid_array[:, None] - centroid_array, axis=-1)
|
266
|
-
|
267
|
-
|
268
|
-
# Step 4: Randomly shift the entire color palette while maintaining relative distances
|
269
|
-
global_shift = np.random.uniform(-0.1, 0.1) # Small global shift to change the overall palette
|
270
|
-
color_positions = (color_positions + global_shift) % 1 # Wrap around to keep within [0, 1]
|
271
|
-
# Step 5: Ensure that all positions remain between 0 and 1
|
272
|
-
color_positions = np.clip(color_positions, 0, 1)
|
269
|
+
max_distance = np.max(dist_matrix) if np.max(dist_matrix) != 0 else 1
|
270
|
+
proximity_threshold = 0.3 * max_distance
|
273
271
|
|
274
|
-
|
275
|
-
|
272
|
+
clustering_model = AgglomerativeClustering(
|
273
|
+
n_clusters=None, distance_threshold=proximity_threshold
|
274
|
+
)
|
275
|
+
cluster_labels = clustering_model.fit_predict(centroid_array)
|
276
|
+
num_clusters = len(set(cluster_labels))
|
277
|
+
|
278
|
+
# Step 3: Assign base color positions for each cluster, spaced across colormap
|
279
|
+
cluster_positions = np.linspace(0, 1, num_clusters, endpoint=False)
|
280
|
+
np.random.shuffle(cluster_positions) # Shuffle based on seed to vary color layout
|
281
|
+
cluster_id_to_position = {
|
282
|
+
cluster_id: pos for cluster_id, pos in zip(np.unique(cluster_labels), cluster_positions)
|
283
|
+
}
|
284
|
+
|
285
|
+
# Step 4: Assign colors to each domain based on cluster base color with a global shift
|
286
|
+
global_shift = np.random.uniform(-0.1, 0.1) # Small global shift for variety
|
287
|
+
colors = []
|
288
|
+
for i in range(num_domains):
|
289
|
+
cluster_idx = cluster_labels[i]
|
290
|
+
base_position = cluster_id_to_position[cluster_idx]
|
291
|
+
# Add global shift and ensure it stays within [0, 1]
|
292
|
+
color_position = (base_position + global_shift) % 1
|
293
|
+
colors.append(colormap(color_position)) # Get color from colormap
|
294
|
+
|
295
|
+
return colors
|
276
296
|
|
277
297
|
|
278
298
|
def _assign_distant_colors(dist_matrix: np.ndarray, num_colors_to_generate: int) -> np.ndarray:
|
279
|
-
"""Assign
|
299
|
+
"""Assign color positions ensuring centroids close in space are maximally separated in color.
|
280
300
|
|
281
301
|
Args:
|
282
302
|
dist_matrix (ndarray): Matrix of pairwise centroid distances.
|
@@ -285,22 +305,14 @@ def _assign_distant_colors(dist_matrix: np.ndarray, num_colors_to_generate: int)
|
|
285
305
|
Returns:
|
286
306
|
np.array: Array of color positions in the range [0, 1].
|
287
307
|
"""
|
288
|
-
|
289
|
-
# Step 1: Sort indices by centroid proximity (based on sum of distances to others)
|
308
|
+
# Step 1: Calculate proximity order based on the sum of distances
|
290
309
|
proximity_order = sorted(
|
291
310
|
range(num_colors_to_generate), key=lambda idx: np.sum(dist_matrix[idx])
|
292
311
|
)
|
293
|
-
# Step 2:
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
# Step 3: Adjust colors so that centroids close to one another are maximally distant on the color spectrum
|
298
|
-
half_spectrum = int(num_colors_to_generate / 2)
|
299
|
-
for i in range(half_spectrum):
|
300
|
-
# Split the spectrum so that close centroids are assigned distant colors
|
301
|
-
color_positions[proximity_order[i]] = (i * 2) / num_colors_to_generate
|
302
|
-
color_positions[proximity_order[-(i + 1)]] = ((i * 2) + 1) / num_colors_to_generate
|
303
|
-
|
312
|
+
# Step 2: Generate evenly spaced color positions
|
313
|
+
color_positions = np.linspace(0, 1, num_colors_to_generate, endpoint=False)
|
314
|
+
# Step 3: Shuffle color positions based on proximity
|
315
|
+
color_positions = color_positions[proximity_order]
|
304
316
|
return color_positions
|
305
317
|
|
306
318
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
risk/__init__.py,sha256=
|
1
|
+
risk/__init__.py,sha256=CF_5U_LFsoHtqjLJO9F0HRUYI0sDt4UlIWwkpkOG-no,112
|
2
2
|
risk/constants.py,sha256=XInRaH78Slnw_sWgAsBFbUHkyA0h0jL0DKGuQNbOvjM,550
|
3
3
|
risk/risk.py,sha256=kntBxYwAEpoAjXN_l6BM3yxFKyuAKmd8OMGl2P00pZ4,22416
|
4
4
|
risk/annotations/__init__.py,sha256=kXgadEXaCh0z8OyhOhTj7c3qXGmWgOhaSZ4gSzSb59U,147
|
@@ -23,7 +23,7 @@ risk/network/plot/contour.py,sha256=VONX9l6owrZvWtR0mWQ6z2GSd1YXIv5wV_sf5ROQLT4,
|
|
23
23
|
risk/network/plot/labels.py,sha256=eorP80CmAbHmt7de2qHna1tHGKL8YiHknwFW2R3tvjI,45734
|
24
24
|
risk/network/plot/network.py,sha256=_K8Am2y6zSGrm3fAgMbXxzgspbugJi3uK4_tG8qqGoI,14015
|
25
25
|
risk/network/plot/plotter.py,sha256=eS1vHqvOA2O001Rq7WiDcgqcehJ3fg4OPfvkezH4erw,5771
|
26
|
-
risk/network/plot/utils/colors.py,sha256=
|
26
|
+
risk/network/plot/utils/colors.py,sha256=lNDwlTbpXCKAbaqSHsRlxxY_MRAbogma81xyqHawl3U,21005
|
27
27
|
risk/network/plot/utils/layout.py,sha256=6o7idoWQnyzujSWOFXQykUvyPy8NuRtJV04TnlbXXBo,3647
|
28
28
|
risk/stats/__init__.py,sha256=WcgoETQ-hS0LQqKRsAMIPtP15xZ-4eul6VUBuUx4Wzc,220
|
29
29
|
risk/stats/hypergeom.py,sha256=oc39f02ViB1vQ-uaDrxG_tzAT6dxQBRjc88EK2EGn78,2282
|
@@ -32,8 +32,8 @@ risk/stats/stats.py,sha256=z8NrhiVj4BzJ250bVLfytpmfC7RzYu7mBuIZD_l0aCA,7222
|
|
32
32
|
risk/stats/permutation/__init__.py,sha256=neJp7FENC-zg_CGOXqv-iIvz1r5XUKI9Ruxhmq7kDOI,105
|
33
33
|
risk/stats/permutation/permutation.py,sha256=meBNSrbRa9P8WJ54n485l0H7VQJlMSfHqdN4aCKYCtQ,10105
|
34
34
|
risk/stats/permutation/test_functions.py,sha256=lftOude6hee0pyR80HlBD32522JkDoN5hrKQ9VEbuoY,2345
|
35
|
-
risk_network-0.0.
|
36
|
-
risk_network-0.0.
|
37
|
-
risk_network-0.0.
|
38
|
-
risk_network-0.0.
|
39
|
-
risk_network-0.0.
|
35
|
+
risk_network-0.0.9b8.dist-info/LICENSE,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
|
36
|
+
risk_network-0.0.9b8.dist-info/METADATA,sha256=JaTFHNn11mwliUNgsG6jvz2sU53goxjS3cwQXLEbcEw,47497
|
37
|
+
risk_network-0.0.9b8.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
|
38
|
+
risk_network-0.0.9b8.dist-info/top_level.txt,sha256=NX7C2PFKTvC1JhVKv14DFlFAIFnKc6Lpsu1ZfxvQwVw,5
|
39
|
+
risk_network-0.0.9b8.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|