risk-network 0.0.8b17__py3-none-any.whl → 0.0.8b19__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/log/params.py +1 -1
- risk/neighborhoods/domains.py +7 -3
- risk/network/graph.py +37 -0
- risk/network/io.py +1 -1
- risk/network/plot/canvas.py +8 -8
- risk/network/plot/contour.py +12 -6
- risk/network/plot/labels.py +49 -36
- risk/network/plot/network.py +19 -13
- risk/network/plot/plotter.py +4 -4
- risk/network/plot/utils/color.py +96 -25
- risk/risk.py +1 -1
- {risk_network-0.0.8b17.dist-info → risk_network-0.0.8b19.dist-info}/METADATA +1 -1
- {risk_network-0.0.8b17.dist-info → risk_network-0.0.8b19.dist-info}/RECORD +17 -17
- {risk_network-0.0.8b17.dist-info → risk_network-0.0.8b19.dist-info}/LICENSE +0 -0
- {risk_network-0.0.8b17.dist-info → risk_network-0.0.8b19.dist-info}/WHEEL +0 -0
- {risk_network-0.0.8b17.dist-info → risk_network-0.0.8b19.dist-info}/top_level.txt +0 -0
risk/__init__.py
CHANGED
risk/log/params.py
CHANGED
@@ -193,5 +193,5 @@ def _convert_ndarray_to_list(d: Any) -> Any:
|
|
193
193
|
# Convert numpy arrays to lists
|
194
194
|
return d.tolist()
|
195
195
|
else:
|
196
|
-
# Return the value unchanged if it's not a dict,
|
196
|
+
# Return the value unchanged if it's not a dict, List, or ndarray
|
197
197
|
return d
|
risk/neighborhoods/domains.py
CHANGED
@@ -76,6 +76,10 @@ def define_domains(
|
|
76
76
|
t_idxmax = node_to_domain.loc[:, 1:].idxmax(axis=1)
|
77
77
|
t_idxmax[t_max == 0] = 0
|
78
78
|
|
79
|
+
# Assign all domains where the score is greater than 0
|
80
|
+
node_to_domain["all domains"] = node_to_domain.loc[:, 1:].apply(
|
81
|
+
lambda row: list(row[row > 0].index), axis=1
|
82
|
+
)
|
79
83
|
# Assign primary domain
|
80
84
|
node_to_domain["primary domain"] = t_idxmax
|
81
85
|
|
@@ -97,7 +101,7 @@ def trim_domains_and_top_annotations(
|
|
97
101
|
max_cluster_size (int, optional): Maximum size of a cluster to be retained. Defaults to 1000.
|
98
102
|
|
99
103
|
Returns:
|
100
|
-
|
104
|
+
Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]: A tuple containing:
|
101
105
|
- Trimmed annotations (pd.DataFrame)
|
102
106
|
- Trimmed domains (pd.DataFrame)
|
103
107
|
- A DataFrame with domain labels (pd.DataFrame)
|
@@ -154,7 +158,7 @@ def _optimize_silhouette_across_linkage_and_metrics(
|
|
154
158
|
linkage_metric (str): Linkage metric for clustering.
|
155
159
|
|
156
160
|
Returns:
|
157
|
-
|
161
|
+
Tuple[str, str, float]: A tuple containing:
|
158
162
|
- Best linkage method (str)
|
159
163
|
- Best linkage metric (str)
|
160
164
|
- Best threshold (float)
|
@@ -208,7 +212,7 @@ def _find_best_silhouette_score(
|
|
208
212
|
resolution (float, optional): Desired resolution for the best threshold. Defaults to 0.001.
|
209
213
|
|
210
214
|
Returns:
|
211
|
-
|
215
|
+
Tuple[float, float]: A tuple containing:
|
212
216
|
- Best threshold (float): The threshold that yields the best silhouette score.
|
213
217
|
- Best silhouette score (float): The highest silhouette score achieved.
|
214
218
|
"""
|
risk/network/graph.py
CHANGED
@@ -46,6 +46,9 @@ class NetworkGraph:
|
|
46
46
|
trimmed_domains
|
47
47
|
)
|
48
48
|
self.node_enrichment_sums = node_enrichment_sums
|
49
|
+
self.node_id_to_domain_ids_and_enrichments_map = (
|
50
|
+
self._create_node_id_to_domain_ids_and_enrichments(domains)
|
51
|
+
)
|
49
52
|
self.node_id_to_node_label_map = {v: k for k, v in node_label_to_node_id_map.items()}
|
50
53
|
self.node_label_to_enrichment_map = dict(
|
51
54
|
zip(node_label_to_node_id_map.keys(), node_enrichment_sums)
|
@@ -92,6 +95,40 @@ class NetworkGraph:
|
|
92
95
|
)
|
93
96
|
)
|
94
97
|
|
98
|
+
def _create_node_id_to_domain_ids_and_enrichments(
|
99
|
+
self, domains: pd.DataFrame
|
100
|
+
) -> Dict[int, Dict]:
|
101
|
+
"""Creates a dictionary mapping each node ID to its corresponding domain IDs and enrichment values.
|
102
|
+
|
103
|
+
Args:
|
104
|
+
domains (pd.DataFrame): A DataFrame containing domain information for each node. Assumes the last
|
105
|
+
two columns are 'all domains' and 'primary domain', which are excluded from processing.
|
106
|
+
|
107
|
+
Returns:
|
108
|
+
dict: A dictionary where the key is the node ID (index of the DataFrame), and the value is another dictionary
|
109
|
+
with 'domain' (a list of domain IDs with non-zero enrichment) and 'enrichment'
|
110
|
+
(a dict of domain IDs and their corresponding enrichment values).
|
111
|
+
"""
|
112
|
+
# Initialize an empty dictionary to store the result
|
113
|
+
node_id_to_domain_ids_and_enrichments = {}
|
114
|
+
# Get the list of domain columns (excluding 'all domains' and 'primary domain')
|
115
|
+
domain_columns = domains.columns[
|
116
|
+
:-2
|
117
|
+
] # The last two columns are 'all domains' and 'primary domain'
|
118
|
+
# Iterate over each row in the dataframe
|
119
|
+
for idx, row in domains.iterrows():
|
120
|
+
# Get the domains (column names) where the enrichment score is greater than 0
|
121
|
+
all_domains = domain_columns[row[domain_columns] > 0].tolist()
|
122
|
+
# Get the enrichment values for those domains
|
123
|
+
enrichment_values = row[all_domains].to_dict()
|
124
|
+
# Store the result in the dictionary with index as the key
|
125
|
+
node_id_to_domain_ids_and_enrichments[idx] = {
|
126
|
+
"domains": all_domains, # The column names where enrichment > 0
|
127
|
+
"enrichments": enrichment_values, # The actual enrichment values for those columns
|
128
|
+
}
|
129
|
+
|
130
|
+
return node_id_to_domain_ids_and_enrichments
|
131
|
+
|
95
132
|
def _create_domain_id_to_node_labels_map(self) -> Dict[int, List[str]]:
|
96
133
|
"""Create a map from domain IDs to node labels.
|
97
134
|
|
risk/network/io.py
CHANGED
@@ -491,7 +491,7 @@ class NetworkIO:
|
|
491
491
|
if "x" not in attrs or "y" not in attrs:
|
492
492
|
if (
|
493
493
|
"pos" in attrs
|
494
|
-
and isinstance(attrs["pos"], (
|
494
|
+
and isinstance(attrs["pos"], (List, Tuple, np.ndarray))
|
495
495
|
and len(attrs["pos"]) >= 2
|
496
496
|
):
|
497
497
|
attrs["x"], attrs["y"] = attrs["pos"][
|
risk/network/plot/canvas.py
CHANGED
@@ -34,8 +34,8 @@ class Canvas:
|
|
34
34
|
title_fontsize: int = 20,
|
35
35
|
subtitle_fontsize: int = 14,
|
36
36
|
font: str = "Arial",
|
37
|
-
title_color: Union[str,
|
38
|
-
subtitle_color: Union[str,
|
37
|
+
title_color: Union[str, List, Tuple, np.ndarray] = "black",
|
38
|
+
subtitle_color: Union[str, List, Tuple, np.ndarray] = "gray",
|
39
39
|
title_y: float = 0.975,
|
40
40
|
title_space_offset: float = 0.075,
|
41
41
|
subtitle_offset: float = 0.025,
|
@@ -48,9 +48,9 @@ class Canvas:
|
|
48
48
|
title_fontsize (int, optional): Font size for the title. Defaults to 20.
|
49
49
|
subtitle_fontsize (int, optional): Font size for the subtitle. Defaults to 14.
|
50
50
|
font (str, optional): Font family used for both title and subtitle. Defaults to "Arial".
|
51
|
-
title_color (str,
|
51
|
+
title_color (str, List, Tuple, or np.ndarray, optional): Color of the title text. Can be a string or an array of colors.
|
52
52
|
Defaults to "black".
|
53
|
-
subtitle_color (str,
|
53
|
+
subtitle_color (str, List, Tuple, or np.ndarray, optional): Color of the subtitle text. Can be a string or an array of colors.
|
54
54
|
Defaults to "gray".
|
55
55
|
title_y (float, optional): Y-axis position of the title. Defaults to 0.975.
|
56
56
|
title_space_offset (float, optional): Fraction of figure height to leave for the space above the plot. Defaults to 0.075.
|
@@ -124,7 +124,7 @@ class Canvas:
|
|
124
124
|
scale (float, optional): Scaling factor for the perimeter diameter. Defaults to 1.0.
|
125
125
|
linestyle (str, optional): Line style for the network perimeter circle (e.g., dashed, solid). Defaults to "dashed".
|
126
126
|
linewidth (float, optional): Width of the circle's outline. Defaults to 1.5.
|
127
|
-
color (str,
|
127
|
+
color (str, List, Tuple, or np.ndarray, optional): Color of the network perimeter circle. Defaults to "black".
|
128
128
|
outline_alpha (float, None, optional): Transparency level of the circle outline. If provided, it overrides any existing alpha
|
129
129
|
values found in color. Defaults to 1.0.
|
130
130
|
fill_alpha (float, None, optional): Transparency level of the circle fill. If provided, it overrides any existing alpha values
|
@@ -137,7 +137,7 @@ class Canvas:
|
|
137
137
|
perimeter_linestyle=linestyle,
|
138
138
|
perimeter_linewidth=linewidth,
|
139
139
|
perimeter_color=(
|
140
|
-
"custom" if isinstance(color, (
|
140
|
+
"custom" if isinstance(color, (List, Tuple, np.ndarray)) else color
|
141
141
|
), # np.ndarray usually indicates custom colors
|
142
142
|
perimeter_outline_alpha=outline_alpha,
|
143
143
|
perimeter_fill_alpha=fill_alpha,
|
@@ -193,7 +193,7 @@ class Canvas:
|
|
193
193
|
levels (int, optional): Number of contour levels. Defaults to 3.
|
194
194
|
bandwidth (float, optional): Bandwidth for the KDE. Controls smoothness. Defaults to 0.8.
|
195
195
|
grid_size (int, optional): Grid resolution for the KDE. Higher values yield finer contours. Defaults to 250.
|
196
|
-
color (str,
|
196
|
+
color (str, List, Tuple, or np.ndarray, optional): Color of the network perimeter contour. Defaults to "black".
|
197
197
|
linestyle (str, optional): Line style for the network perimeter contour (e.g., dashed, solid). Defaults to "solid".
|
198
198
|
linewidth (float, optional): Width of the contour's outline. Defaults to 1.5.
|
199
199
|
outline_alpha (float, None, optional): Transparency level of the contour outline. If provided, it overrides any existing
|
@@ -210,7 +210,7 @@ class Canvas:
|
|
210
210
|
perimeter_grid_size=grid_size,
|
211
211
|
perimeter_linestyle=linestyle,
|
212
212
|
perimeter_linewidth=linewidth,
|
213
|
-
perimeter_color=("custom" if isinstance(color, (
|
213
|
+
perimeter_color=("custom" if isinstance(color, (List, Tuple, np.ndarray)) else color),
|
214
214
|
perimeter_outline_alpha=outline_alpha,
|
215
215
|
perimeter_fill_alpha=fill_alpha,
|
216
216
|
)
|
risk/network/plot/contour.py
CHANGED
@@ -46,7 +46,7 @@ class Contour:
|
|
46
46
|
levels (int, optional): Number of contour levels to plot. Defaults to 5.
|
47
47
|
bandwidth (float, optional): Bandwidth for KDE. Controls the smoothness of the contour. Defaults to 0.8.
|
48
48
|
grid_size (int, optional): Resolution of the grid for KDE. Higher values create finer contours. Defaults to 250.
|
49
|
-
color (str,
|
49
|
+
color (str, List, Tuple, or np.ndarray, optional): Color of the contours. Can be a single color or an array of colors.
|
50
50
|
Defaults to "white".
|
51
51
|
linestyle (str, optional): Line style for the contours. Defaults to "solid".
|
52
52
|
linewidth (float, optional): Line width for the contours. Defaults to 1.5.
|
@@ -105,11 +105,11 @@ class Contour:
|
|
105
105
|
"""Plot a subcontour for a given set of nodes or a list of node sets using Kernel Density Estimation (KDE).
|
106
106
|
|
107
107
|
Args:
|
108
|
-
nodes (
|
108
|
+
nodes (List, Tuple, or np.ndarray): List of node labels or list of lists of node labels to plot the contour for.
|
109
109
|
levels (int, optional): Number of contour levels to plot. Defaults to 5.
|
110
110
|
bandwidth (float, optional): Bandwidth for KDE. Controls the smoothness of the contour. Defaults to 0.8.
|
111
111
|
grid_size (int, optional): Resolution of the grid for KDE. Higher values create finer contours. Defaults to 250.
|
112
|
-
color (str,
|
112
|
+
color (str, List, Tuple, or np.ndarray, optional): Color of the contour. Can be a string (e.g., 'white') or RGBA array.
|
113
113
|
Can be a single color or an array of colors. Defaults to "white".
|
114
114
|
linestyle (str, optional): Line style for the contour. Defaults to "solid".
|
115
115
|
linewidth (float, optional): Line width for the contour. Defaults to 1.5.
|
@@ -122,7 +122,7 @@ class Contour:
|
|
122
122
|
ValueError: If no valid nodes are found in the network graph.
|
123
123
|
"""
|
124
124
|
# Check if nodes is a list of lists or a flat list
|
125
|
-
if any(isinstance(item, (
|
125
|
+
if any(isinstance(item, (List, Tuple, np.ndarray)) for item in nodes):
|
126
126
|
# If it's a list of lists, iterate over sublists
|
127
127
|
node_groups = nodes
|
128
128
|
# Convert color to RGBA arrays to match the number of groups
|
@@ -274,7 +274,9 @@ class Contour:
|
|
274
274
|
def get_annotated_contour_colors(
|
275
275
|
self,
|
276
276
|
cmap: str = "gist_rainbow",
|
277
|
-
color: Union[str,
|
277
|
+
color: Union[str, List, Tuple, np.ndarray, None] = None,
|
278
|
+
blend_colors: bool = False,
|
279
|
+
blend_gamma: float = 2.2,
|
278
280
|
min_scale: float = 0.8,
|
279
281
|
max_scale: float = 1.0,
|
280
282
|
scale_factor: float = 1.0,
|
@@ -284,8 +286,10 @@ class Contour:
|
|
284
286
|
|
285
287
|
Args:
|
286
288
|
cmap (str, optional): Name of the colormap to use for generating contour colors. Defaults to "gist_rainbow".
|
287
|
-
color (str,
|
289
|
+
color (str, List, Tuple, np.ndarray, or None, optional): Color to use for the contours. Can be a single color or an array of colors.
|
288
290
|
If None, the colormap will be used. Defaults to None.
|
291
|
+
blend_colors (bool, optional): Whether to blend colors for nodes with multiple domains. Defaults to False.
|
292
|
+
blend_gamma (float, optional): Gamma correction factor for perceptual color blending. Defaults to 2.2.
|
289
293
|
min_scale (float, optional): Minimum intensity scale for the colors generated by the colormap.
|
290
294
|
Controls the dimmest colors. Defaults to 0.8.
|
291
295
|
max_scale (float, optional): Maximum intensity scale for the colors generated by the colormap.
|
@@ -301,6 +305,8 @@ class Contour:
|
|
301
305
|
graph=self.graph,
|
302
306
|
cmap=cmap,
|
303
307
|
color=color,
|
308
|
+
blend_colors=blend_colors,
|
309
|
+
blend_gamma=blend_gamma,
|
304
310
|
min_scale=min_scale,
|
305
311
|
max_scale=max_scale,
|
306
312
|
scale_factor=scale_factor,
|
risk/network/plot/labels.py
CHANGED
@@ -66,13 +66,13 @@ class Labels:
|
|
66
66
|
- If a dictionary, maps specific cases ('lower', 'upper', 'title') to transformations (e.g., 'lower'='upper').
|
67
67
|
- If None, no transformation is applied.
|
68
68
|
fontsize (int, optional): Font size for the labels. Defaults to 10.
|
69
|
-
fontcolor (str,
|
69
|
+
fontcolor (str, List, Tuple, or np.ndarray, optional): Color of the label text. Can be a string or RGBA array.
|
70
70
|
Defaults to "black".
|
71
71
|
fontalpha (float, None, optional): Transparency level for the font color. If provided, it overrides any existing alpha
|
72
72
|
values found in fontcolor. Defaults to 1.0.
|
73
73
|
arrow_linewidth (float, optional): Line width of the arrows pointing to centroids. Defaults to 1.
|
74
74
|
arrow_style (str, optional): Style of the arrows pointing to centroids. Defaults to "->".
|
75
|
-
arrow_color (str,
|
75
|
+
arrow_color (str, List, Tuple, or np.ndarray, optional): Color of the arrows. Defaults to "black".
|
76
76
|
arrow_alpha (float, None, optional): Transparency level for the arrow color. If provided, it overrides any existing alpha
|
77
77
|
values found in arrow_color. Defaults to 1.0.
|
78
78
|
arrow_base_shrink (float, optional): Distance between the text and the base of the arrow. Defaults to 0.0.
|
@@ -82,9 +82,9 @@ class Labels:
|
|
82
82
|
max_label_lines (int, optional): Maximum number of lines in a label. Defaults to None (no limit).
|
83
83
|
min_chars_per_line (int, optional): Minimum number of characters in a line to display. Defaults to 1.
|
84
84
|
max_chars_per_line (int, optional): Maximum number of characters in a line to display. Defaults to None (no limit).
|
85
|
-
words_to_omit (
|
85
|
+
words_to_omit (List, optional): List of words to omit from the labels. Defaults to None.
|
86
86
|
overlay_ids (bool, optional): Whether to overlay domain IDs in the center of the centroids. Defaults to False.
|
87
|
-
ids_to_keep (
|
87
|
+
ids_to_keep (List, Tuple, np.ndarray, or None, optional): IDs of domains that must be labeled. To discover domain IDs,
|
88
88
|
you can set `overlay_ids=True`. Defaults to None.
|
89
89
|
ids_to_replace (dict, optional): A dictionary mapping domain IDs to custom labels (strings). The labels should be
|
90
90
|
space-separated words. If provided, the custom labels will replace the default domain terms. To discover domain IDs, you
|
@@ -263,26 +263,26 @@ class Labels:
|
|
263
263
|
"""Annotate the network graph with a label for the given nodes, with one arrow pointing to each centroid of sublists of nodes.
|
264
264
|
|
265
265
|
Args:
|
266
|
-
nodes (
|
266
|
+
nodes (List, Tuple, or np.ndarray): List of node labels or list of lists of node labels.
|
267
267
|
label (str): The label to be annotated on the network.
|
268
268
|
radial_position (float, optional): Radial angle for positioning the label, in degrees (0-360). Defaults to 0.0.
|
269
269
|
scale (float, optional): Scale factor for positioning the label around the perimeter. Defaults to 1.05.
|
270
270
|
offset (float, optional): Offset distance for the label from the perimeter. Defaults to 0.10.
|
271
271
|
font (str, optional): Font name for the label. Defaults to "Arial".
|
272
272
|
fontsize (int, optional): Font size for the label. Defaults to 10.
|
273
|
-
fontcolor (str,
|
273
|
+
fontcolor (str, List, Tuple, or np.ndarray, optional): Color of the label text. Defaults to "black".
|
274
274
|
fontalpha (float, None, optional): Transparency level for the font color. If provided, it overrides any existing alpha values found
|
275
275
|
in fontalpha. Defaults to 1.0.
|
276
276
|
arrow_linewidth (float, optional): Line width of the arrow pointing to the centroid. Defaults to 1.
|
277
277
|
arrow_style (str, optional): Style of the arrows pointing to the centroid. Defaults to "->".
|
278
|
-
arrow_color (str,
|
278
|
+
arrow_color (str, List, Tuple, or np.ndarray, optional): Color of the arrow. Defaults to "black".
|
279
279
|
arrow_alpha (float, None, optional): Transparency level for the arrow color. If provided, it overrides any existing alpha values
|
280
280
|
found in arrow_alpha. Defaults to 1.0.
|
281
281
|
arrow_base_shrink (float, optional): Distance between the text and the base of the arrow. Defaults to 0.0.
|
282
282
|
arrow_tip_shrink (float, optional): Distance between the arrow tip and the centroid. Defaults to 0.0.
|
283
283
|
"""
|
284
284
|
# Check if nodes is a list of lists or a flat list
|
285
|
-
if any(isinstance(item, (
|
285
|
+
if any(isinstance(item, (List, Tuple, np.ndarray)) for item in nodes):
|
286
286
|
# If it's a list of lists, iterate over sublists
|
287
287
|
node_groups = nodes
|
288
288
|
# Convert fontcolor and arrow_color to RGBA arrays to match the number of groups
|
@@ -383,9 +383,9 @@ class Labels:
|
|
383
383
|
|
384
384
|
Args:
|
385
385
|
domain_id_to_centroid_map (dict): Mapping of domain IDs to their centroids.
|
386
|
-
ids_to_keep (
|
386
|
+
ids_to_keep (List, Tuple, or np.ndarray, optional): IDs of domains that must be labeled.
|
387
387
|
ids_to_replace (dict, optional): A dictionary mapping domain IDs to custom labels. Defaults to None.
|
388
|
-
words_to_omit (
|
388
|
+
words_to_omit (List, optional): List of words to omit from the labels. Defaults to None.
|
389
389
|
max_labels (int, optional): Maximum number of labels allowed.
|
390
390
|
min_label_lines (int): Minimum number of lines in a label.
|
391
391
|
max_label_lines (int): Maximum number of lines in a label.
|
@@ -449,9 +449,9 @@ class Labels:
|
|
449
449
|
|
450
450
|
Args:
|
451
451
|
domain_id_to_centroid_map (dict): Mapping of domain IDs to their centroids.
|
452
|
-
ids_to_keep (
|
452
|
+
ids_to_keep (List, Tuple, or np.ndarray, optional): IDs of domains that must be labeled.
|
453
453
|
ids_to_replace (dict, optional): A dictionary mapping domain IDs to custom labels. Defaults to None.
|
454
|
-
words_to_omit (
|
454
|
+
words_to_omit (List, optional): List of words to omit from the labels. Defaults to None.
|
455
455
|
remaining_labels (int): The remaining number of labels that can be generated.
|
456
456
|
min_label_lines (int): Minimum number of lines in a label.
|
457
457
|
max_label_lines (int): Maximum number of lines in a label.
|
@@ -607,7 +607,7 @@ class Labels:
|
|
607
607
|
Args:
|
608
608
|
domain (str): The domain being processed.
|
609
609
|
ids_to_replace (dict, optional): Dictionary mapping domain IDs to custom labels.
|
610
|
-
words_to_omit (
|
610
|
+
words_to_omit (List, optional): List of words to omit from the labels.
|
611
611
|
max_label_lines (int): Maximum number of lines in a label.
|
612
612
|
min_chars_per_line (int): Minimum number of characters in a line to display.
|
613
613
|
max_chars_per_line (int): Maximum number of characters in a line to display.
|
@@ -637,7 +637,9 @@ class Labels:
|
|
637
637
|
def get_annotated_label_colors(
|
638
638
|
self,
|
639
639
|
cmap: str = "gist_rainbow",
|
640
|
-
color: Union[str,
|
640
|
+
color: Union[str, List, Tuple, np.ndarray, None] = None,
|
641
|
+
blend_colors: bool = False,
|
642
|
+
blend_gamma: float = 2.2,
|
641
643
|
min_scale: float = 0.8,
|
642
644
|
max_scale: float = 1.0,
|
643
645
|
scale_factor: float = 1.0,
|
@@ -647,8 +649,10 @@ class Labels:
|
|
647
649
|
|
648
650
|
Args:
|
649
651
|
cmap (str, optional): Name of the colormap to use for generating label colors. Defaults to "gist_rainbow".
|
650
|
-
color (str,
|
652
|
+
color (str, List, Tuple, np.ndarray, or None, optional): Color to use for the labels. Can be a single color or an array
|
651
653
|
of colors. If None, the colormap will be used. Defaults to None.
|
654
|
+
blend_colors (bool, optional): Whether to blend colors for nodes with multiple domains. Defaults to False.
|
655
|
+
blend_gamma (float, optional): Gamma correction factor for perceptual color blending. Defaults to 2.2.
|
652
656
|
min_scale (float, optional): Minimum intensity scale for the colors generated by the colormap.
|
653
657
|
Controls the dimmest colors. Defaults to 0.8.
|
654
658
|
max_scale (float, optional): Maximum intensity scale for the colors generated by the colormap.
|
@@ -664,6 +668,8 @@ class Labels:
|
|
664
668
|
graph=self.graph,
|
665
669
|
cmap=cmap,
|
666
670
|
color=color,
|
671
|
+
blend_colors=blend_colors,
|
672
|
+
blend_gamma=blend_gamma,
|
667
673
|
min_scale=min_scale,
|
668
674
|
max_scale=max_scale,
|
669
675
|
scale_factor=scale_factor,
|
@@ -768,7 +774,7 @@ def _calculate_equidistant_positions_around_center(
|
|
768
774
|
num_domains (int): The number of positions (or domains) to calculate.
|
769
775
|
|
770
776
|
Returns:
|
771
|
-
|
777
|
+
List[np.ndarray]: List of positions (as 2D numpy arrays) around the center.
|
772
778
|
"""
|
773
779
|
# Calculate equidistant angles in radians around the center
|
774
780
|
angles = np.linspace(0, 2 * np.pi, num_domains, endpoint=False)
|
@@ -878,28 +884,35 @@ def _apply_str_transformation(
|
|
878
884
|
Returns:
|
879
885
|
List[str]: A list of transformed words with no duplicates.
|
880
886
|
"""
|
887
|
+
# Initialize a list to store transformed words
|
881
888
|
transformed_words = []
|
882
889
|
for word in words:
|
883
|
-
#
|
884
|
-
|
885
|
-
|
886
|
-
#
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
890
|
+
# Split word into subwords by space
|
891
|
+
subwords = word.split(" ")
|
892
|
+
transformed_subwords = []
|
893
|
+
# Apply transformation to each subword
|
894
|
+
for subword in subwords:
|
895
|
+
transformed_subword = subword # Start with the original subword
|
896
|
+
# If transformation is a string, apply it to all subwords
|
897
|
+
if isinstance(transformation, str):
|
898
|
+
if hasattr(subword, transformation):
|
899
|
+
transformed_subword = getattr(subword, transformation)()
|
900
|
+
|
901
|
+
# If transformation is a dictionary, apply case-specific transformations
|
902
|
+
elif isinstance(transformation, dict):
|
903
|
+
for case_type, transform in transformation.items():
|
904
|
+
if case_type == "lower" and subword.islower() and transform:
|
905
|
+
transformed_subword = getattr(subword, transform)()
|
906
|
+
elif case_type == "upper" and subword.isupper() and transform:
|
907
|
+
transformed_subword = getattr(subword, transform)()
|
908
|
+
elif case_type == "title" and subword.istitle() and transform:
|
909
|
+
transformed_subword = getattr(subword, transform)()
|
910
|
+
|
911
|
+
# Append the transformed subword to the list
|
912
|
+
transformed_subwords.append(transformed_subword)
|
913
|
+
|
914
|
+
# Rejoin the transformed subwords into a single string to preserve structure
|
915
|
+
transformed_word = " ".join(transformed_subwords)
|
903
916
|
# Only append if the transformed word is not already in the list
|
904
917
|
if transformed_word not in transformed_words:
|
905
918
|
transformed_words.append(transformed_word)
|
risk/network/plot/network.py
CHANGED
@@ -45,11 +45,11 @@ class Network:
|
|
45
45
|
node_shape (str, optional): Shape of the nodes. Defaults to "o".
|
46
46
|
node_edgewidth (float, optional): Width of the node edges. Defaults to 1.0.
|
47
47
|
edge_width (float, optional): Width of the edges. Defaults to 1.0.
|
48
|
-
node_color (str,
|
48
|
+
node_color (str, List, Tuple, or np.ndarray, optional): Color of the nodes. Can be a single color or an array of colors.
|
49
49
|
Defaults to "white".
|
50
|
-
node_edgecolor (str,
|
50
|
+
node_edgecolor (str, List, Tuple, or np.ndarray, optional): Color of the node edges. Can be a single color or an array of colors.
|
51
51
|
Defaults to "black".
|
52
|
-
edge_color (str,
|
52
|
+
edge_color (str, List, Tuple, or np.ndarray, optional): Color of the edges. Can be a single color or an array of colors.
|
53
53
|
Defaults to "black".
|
54
54
|
node_alpha (float, None, optional): Alpha value (transparency) for the nodes. If provided, it overrides any existing alpha
|
55
55
|
values found in node_color. Defaults to 1.0. Annotated node_color alphas will override this value.
|
@@ -124,14 +124,14 @@ class Network:
|
|
124
124
|
"""Plot a subnetwork of selected nodes with customizable node and edge attributes.
|
125
125
|
|
126
126
|
Args:
|
127
|
-
nodes (
|
127
|
+
nodes (List, Tuple, or np.ndarray): List of node labels to include in the subnetwork. Accepts nested lists.
|
128
128
|
node_size (int or np.ndarray, optional): Size of the nodes. Can be a single integer or an array of sizes. Defaults to 50.
|
129
129
|
node_shape (str, optional): Shape of the nodes. Defaults to "o".
|
130
130
|
node_edgewidth (float, optional): Width of the node edges. Defaults to 1.0.
|
131
131
|
edge_width (float, optional): Width of the edges. Defaults to 1.0.
|
132
|
-
node_color (str,
|
133
|
-
node_edgecolor (str,
|
134
|
-
edge_color (str,
|
132
|
+
node_color (str, List, Tuple, or np.ndarray, optional): Color of the nodes. Defaults to "white".
|
133
|
+
node_edgecolor (str, List, Tuple, or np.ndarray, optional): Color of the node edges. Defaults to "black".
|
134
|
+
edge_color (str, List, Tuple, or np.ndarray, optional): Color of the edges. Defaults to "black".
|
135
135
|
node_alpha (float, None, optional): Transparency for the nodes. If provided, it overrides any existing alpha values
|
136
136
|
found in node_color. Defaults to 1.0.
|
137
137
|
edge_alpha (float, None, optional): Transparency for the edges. If provided, it overrides any existing alpha values
|
@@ -141,7 +141,7 @@ class Network:
|
|
141
141
|
ValueError: If no valid nodes are found in the network graph.
|
142
142
|
"""
|
143
143
|
# Flatten nested lists of nodes, if necessary
|
144
|
-
if any(isinstance(item, (
|
144
|
+
if any(isinstance(item, (List, Tuple, np.ndarray)) for item in nodes):
|
145
145
|
nodes = [node for sublist in nodes for node in sublist]
|
146
146
|
|
147
147
|
# Filter to get node IDs and their coordinates
|
@@ -154,7 +154,7 @@ class Network:
|
|
154
154
|
raise ValueError("No nodes found in the network graph.")
|
155
155
|
|
156
156
|
# Check if node_color is a single color or a list of colors
|
157
|
-
if not isinstance(node_color, (str,
|
157
|
+
if not isinstance(node_color, (str, Tuple, np.ndarray)):
|
158
158
|
node_color = [
|
159
159
|
node_color[nodes.index(node)]
|
160
160
|
for node in nodes
|
@@ -196,12 +196,14 @@ class Network:
|
|
196
196
|
def get_annotated_node_colors(
|
197
197
|
self,
|
198
198
|
cmap: str = "gist_rainbow",
|
199
|
-
color: Union[str,
|
199
|
+
color: Union[str, List, Tuple, np.ndarray, None] = None,
|
200
|
+
blend_colors: bool = False,
|
201
|
+
blend_gamma: float = 2.2,
|
200
202
|
min_scale: float = 0.8,
|
201
203
|
max_scale: float = 1.0,
|
202
204
|
scale_factor: float = 1.0,
|
203
205
|
alpha: Union[float, None] = 1.0,
|
204
|
-
nonenriched_color: Union[str,
|
206
|
+
nonenriched_color: Union[str, List, Tuple, np.ndarray] = "white",
|
205
207
|
nonenriched_alpha: Union[float, None] = 1.0,
|
206
208
|
random_seed: int = 888,
|
207
209
|
) -> np.ndarray:
|
@@ -209,14 +211,16 @@ class Network:
|
|
209
211
|
|
210
212
|
Args:
|
211
213
|
cmap (str, optional): Colormap to use for coloring the nodes. Defaults to "gist_rainbow".
|
212
|
-
color (str,
|
214
|
+
color (str, List, Tuple, np.ndarray, or None, optional): Color to use for the nodes. Can be a single color or an array of colors.
|
213
215
|
If None, the colormap will be used. Defaults to None.
|
216
|
+
blend_colors (bool, optional): Whether to blend colors for nodes with multiple domains. Defaults to False.
|
217
|
+
blend_gamma (float, optional): Gamma correction factor for perceptual color blending. Defaults to 2.2.
|
214
218
|
min_scale (float, optional): Minimum scale for color intensity. Defaults to 0.8.
|
215
219
|
max_scale (float, optional): Maximum scale for color intensity. Defaults to 1.0.
|
216
220
|
scale_factor (float, optional): Factor for adjusting the color scaling intensity. Defaults to 1.0.
|
217
221
|
alpha (float, None, optional): Alpha value for enriched nodes. If provided, it overrides any existing alpha values found in `color`.
|
218
222
|
Defaults to 1.0.
|
219
|
-
nonenriched_color (str,
|
223
|
+
nonenriched_color (str, List, Tuple, or np.ndarray, optional): Color for non-enriched nodes. Can be a single color or an array of colors.
|
220
224
|
Defaults to "white".
|
221
225
|
nonenriched_alpha (float, None, optional): Alpha value for non-enriched nodes. If provided, it overrides any existing alpha values found
|
222
226
|
in `nonenriched_color`. Defaults to 1.0.
|
@@ -230,6 +234,8 @@ class Network:
|
|
230
234
|
graph=self.graph,
|
231
235
|
cmap=cmap,
|
232
236
|
color=color,
|
237
|
+
blend_colors=blend_colors,
|
238
|
+
blend_gamma=blend_gamma,
|
233
239
|
min_scale=min_scale,
|
234
240
|
max_scale=max_scale,
|
235
241
|
scale_factor=scale_factor,
|
risk/network/plot/plotter.py
CHANGED
@@ -38,8 +38,8 @@ class NetworkPlotter(Canvas, Network, Contour, Labels):
|
|
38
38
|
|
39
39
|
Args:
|
40
40
|
graph (NetworkGraph): The network data and attributes to be visualized.
|
41
|
-
figsize (
|
42
|
-
background_color (str,
|
41
|
+
figsize (Tuple, optional): Size of the figure in inches (width, height). Defaults to (10, 10).
|
42
|
+
background_color (str, List, Tuple, np.ndarray, optional): Background color of the plot. Defaults to "white".
|
43
43
|
background_alpha (float, None, optional): Transparency level of the background color. If provided, it overrides
|
44
44
|
any existing alpha values found in background_color. Defaults to 1.0.
|
45
45
|
pad (float, optional): Padding value to adjust the axis limits. Defaults to 0.3.
|
@@ -59,7 +59,7 @@ class NetworkPlotter(Canvas, Network, Contour, Labels):
|
|
59
59
|
self,
|
60
60
|
graph: NetworkGraph,
|
61
61
|
figsize: Tuple,
|
62
|
-
background_color: Union[str,
|
62
|
+
background_color: Union[str, List, Tuple, np.ndarray],
|
63
63
|
background_alpha: Union[float, None],
|
64
64
|
pad: float,
|
65
65
|
) -> plt.Axes:
|
@@ -68,7 +68,7 @@ class NetworkPlotter(Canvas, Network, Contour, Labels):
|
|
68
68
|
Args:
|
69
69
|
graph (NetworkGraph): The network data and attributes to be visualized.
|
70
70
|
figsize (tuple): Size of the figure in inches (width, height).
|
71
|
-
background_color (str,
|
71
|
+
background_color (str, List, Tuple, or np.ndarray): Background color of the plot. Can be a single color or an array of colors.
|
72
72
|
background_alpha (float, None, optional): Transparency level of the background color. If provided, it overrides any existing
|
73
73
|
alpha values found in `background_color`.
|
74
74
|
pad (float, optional): Padding value to adjust the axis limits.
|
risk/network/plot/utils/color.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"""
|
2
|
-
risk/network/plot/utils/
|
3
|
-
|
2
|
+
risk/network/plot/utils/color
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
6
|
from typing import Any, Dict, List, Tuple, Union
|
@@ -16,7 +16,9 @@ from risk.network.plot.utils.layout import calculate_centroids
|
|
16
16
|
def get_annotated_domain_colors(
|
17
17
|
graph: NetworkGraph,
|
18
18
|
cmap: str = "gist_rainbow",
|
19
|
-
color: Union[str,
|
19
|
+
color: Union[str, List, Tuple, np.ndarray, None] = None,
|
20
|
+
blend_colors: bool = False,
|
21
|
+
blend_gamma: float = 2.2,
|
20
22
|
min_scale: float = 0.8,
|
21
23
|
max_scale: float = 1.0,
|
22
24
|
scale_factor: float = 1.0,
|
@@ -27,8 +29,10 @@ def get_annotated_domain_colors(
|
|
27
29
|
Args:
|
28
30
|
graph (NetworkGraph): The network data and attributes to be visualized.
|
29
31
|
cmap (str, optional): Colormap to use for generating domain colors. Defaults to "gist_rainbow".
|
30
|
-
color (str,
|
32
|
+
color (str, List, Tuple, np.ndarray, or None, optional): Color to use for the domains. Can be a single color or an array of colors.
|
31
33
|
If None, the colormap will be used. Defaults to None.
|
34
|
+
blend_colors (bool, optional): Whether to blend colors for nodes with multiple domains. Defaults to False.
|
35
|
+
blend_gamma (float, optional): Gamma correction factor for perceptual color blending. Defaults to 2.2.
|
32
36
|
min_scale (float, optional): Minimum scale for color intensity when generating domain colors. Defaults to 0.8.
|
33
37
|
max_scale (float, optional): Maximum scale for color intensity when generating domain colors. Defaults to 1.0.
|
34
38
|
scale_factor (float, optional): Factor for adjusting the contrast in the colors generated based on enrichment. Higher values
|
@@ -43,6 +47,8 @@ def get_annotated_domain_colors(
|
|
43
47
|
graph=graph,
|
44
48
|
cmap=cmap,
|
45
49
|
color=color,
|
50
|
+
blend_colors=blend_colors,
|
51
|
+
blend_gamma=blend_gamma,
|
46
52
|
min_scale=min_scale,
|
47
53
|
max_scale=max_scale,
|
48
54
|
scale_factor=scale_factor,
|
@@ -68,7 +74,9 @@ def get_annotated_domain_colors(
|
|
68
74
|
def get_domain_colors(
|
69
75
|
graph: NetworkGraph,
|
70
76
|
cmap: str = "gist_rainbow",
|
71
|
-
color: Union[str,
|
77
|
+
color: Union[str, List, Tuple, np.ndarray, None] = None,
|
78
|
+
blend_colors: bool = False,
|
79
|
+
blend_gamma: float = 2.2,
|
72
80
|
min_scale: float = 0.8,
|
73
81
|
max_scale: float = 1.0,
|
74
82
|
scale_factor: float = 1.0,
|
@@ -79,8 +87,10 @@ def get_domain_colors(
|
|
79
87
|
Args:
|
80
88
|
graph (NetworkGraph): The network data and attributes to be visualized.
|
81
89
|
cmap (str, optional): Name of the colormap to use for generating domain colors. Defaults to "gist_rainbow".
|
82
|
-
color (str,
|
90
|
+
color (str, List, Tuple, np.ndarray, or None, optional): A specific color or array of colors to use for all domains.
|
83
91
|
If None, the colormap will be used. Defaults to None.
|
92
|
+
blend_colors (bool, optional): Whether to blend colors for nodes with multiple domains. Defaults to False.
|
93
|
+
blend_gamma (float, optional): Gamma correction factor for perceptual color blending. Defaults to 2.2.
|
84
94
|
min_scale (float, optional): Minimum intensity scale for the colors generated by the colormap. Controls the dimmest colors.
|
85
95
|
Defaults to 0.8.
|
86
96
|
max_scale (float, optional): Maximum intensity scale for the colors generated by the colormap. Controls the brightest colors.
|
@@ -95,7 +105,9 @@ def get_domain_colors(
|
|
95
105
|
# Get colors for each domain
|
96
106
|
domain_colors = _get_domain_colors(graph=graph, cmap=cmap, color=color, random_seed=random_seed)
|
97
107
|
# Generate composite colors for nodes
|
98
|
-
node_colors = _get_composite_node_colors(
|
108
|
+
node_colors = _get_composite_node_colors(
|
109
|
+
graph=graph, domain_colors=domain_colors, blend_colors=blend_colors, blend_gamma=blend_gamma
|
110
|
+
)
|
99
111
|
# Transform colors to ensure proper alpha values and intensity
|
100
112
|
transformed_colors = _transform_colors(
|
101
113
|
node_colors,
|
@@ -110,7 +122,7 @@ def get_domain_colors(
|
|
110
122
|
def _get_domain_colors(
|
111
123
|
graph: NetworkGraph,
|
112
124
|
cmap: str = "gist_rainbow",
|
113
|
-
color: Union[str,
|
125
|
+
color: Union[str, List, Tuple, np.ndarray, None] = None,
|
114
126
|
random_seed: int = 888,
|
115
127
|
) -> Dict[str, Any]:
|
116
128
|
"""Get colors for each domain.
|
@@ -118,7 +130,7 @@ def _get_domain_colors(
|
|
118
130
|
Args:
|
119
131
|
graph (NetworkGraph): The network data and attributes to be visualized.
|
120
132
|
cmap (str, optional): The name of the colormap to use. Defaults to "gist_rainbow".
|
121
|
-
color (str,
|
133
|
+
color (str, List, Tuple, np.ndarray, or None, optional): A specific color or array of colors to use for the domains.
|
122
134
|
If None, the colormap will be used. Defaults to None.
|
123
135
|
random_seed (int, optional): Seed for random number generation. Defaults to 888.
|
124
136
|
|
@@ -136,12 +148,17 @@ def _get_domain_colors(
|
|
136
148
|
return dict(zip(graph.domain_id_to_node_ids_map.keys(), domain_colors))
|
137
149
|
|
138
150
|
|
139
|
-
def _get_composite_node_colors(
|
140
|
-
|
151
|
+
def _get_composite_node_colors(
|
152
|
+
graph, domain_colors: np.ndarray, blend_colors: bool = False, blend_gamma: float = 2.2
|
153
|
+
) -> np.ndarray:
|
154
|
+
"""Generate composite colors for nodes based on domain colors and enrichment values, with optional color blending.
|
141
155
|
|
142
156
|
Args:
|
143
157
|
graph (NetworkGraph): The network data and attributes to be visualized.
|
144
|
-
domain_colors (np.ndarray): Array of colors corresponding to each domain.
|
158
|
+
domain_colors (np.ndarray): Array or list of RGBA colors corresponding to each domain.
|
159
|
+
blend_colors (bool): Whether to blend colors for nodes with multiple domains. Defaults to False.
|
160
|
+
blend_gamma (float, optional): Gamma correction factor to be used for perceptual color blending.
|
161
|
+
This parameter is only relevant if blend_colors is True. Defaults to 2.2.
|
145
162
|
|
146
163
|
Returns:
|
147
164
|
np.ndarray: Array of composite colors for each node.
|
@@ -150,11 +167,38 @@ def _get_composite_node_colors(graph: NetworkGraph, domain_colors: np.ndarray) -
|
|
150
167
|
num_nodes = len(graph.node_coordinates)
|
151
168
|
# Initialize composite colors array with shape (number of nodes, 4) for RGBA
|
152
169
|
composite_colors = np.zeros((num_nodes, 4))
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
for
|
157
|
-
|
170
|
+
|
171
|
+
# If blending is not required, directly assign domain colors to nodes
|
172
|
+
if not blend_colors:
|
173
|
+
for domain_id, nodes in graph.domain_id_to_node_ids_map.items():
|
174
|
+
color = domain_colors[domain_id]
|
175
|
+
for node in nodes:
|
176
|
+
composite_colors[node] = color
|
177
|
+
|
178
|
+
# If blending is required
|
179
|
+
else:
|
180
|
+
for node, node_info in graph.node_id_to_domain_ids_and_enrichments_map.items():
|
181
|
+
domains = node_info["domains"] # List of domain IDs
|
182
|
+
enrichments = node_info["enrichments"] # List of enrichment values
|
183
|
+
# Filter domains and enrichments to keep only those with corresponding colors in domain_colors
|
184
|
+
filtered_domains_enrichments = [
|
185
|
+
(domain_id, enrichment)
|
186
|
+
for domain_id, enrichment in zip(domains, enrichments)
|
187
|
+
if domain_id in domain_colors
|
188
|
+
]
|
189
|
+
# If no valid domains exist, skip this node
|
190
|
+
if not filtered_domains_enrichments:
|
191
|
+
continue
|
192
|
+
|
193
|
+
# Unpack filtered domains and enrichments
|
194
|
+
filtered_domains, filtered_enrichments = zip(*filtered_domains_enrichments)
|
195
|
+
# Get the colors corresponding to the valid filtered domains
|
196
|
+
colors = [domain_colors[domain_id] for domain_id in filtered_domains]
|
197
|
+
# Blend the colors using the given gamma (default is 2.2 if None)
|
198
|
+
gamma = blend_gamma if blend_gamma is not None else 2.2
|
199
|
+
composite_color = _blend_colors_perceptually(colors, filtered_enrichments, gamma)
|
200
|
+
# Assign the composite color to the node
|
201
|
+
composite_colors[node] = composite_color
|
158
202
|
|
159
203
|
return composite_colors
|
160
204
|
|
@@ -163,7 +207,7 @@ def _get_colors(
|
|
163
207
|
network,
|
164
208
|
domain_id_to_node_ids_map,
|
165
209
|
cmap: str = "gist_rainbow",
|
166
|
-
color: Union[str,
|
210
|
+
color: Union[str, List, Tuple, np.ndarray, None] = None,
|
167
211
|
random_seed: int = 888,
|
168
212
|
) -> List[Tuple]:
|
169
213
|
"""Generate a list of RGBA colors based on domain centroids, ensuring that domains
|
@@ -173,7 +217,7 @@ def _get_colors(
|
|
173
217
|
network (NetworkX graph): The graph representing the network.
|
174
218
|
domain_id_to_node_ids_map (dict): Mapping from domain IDs to lists of node IDs.
|
175
219
|
cmap (str, optional): The name of the colormap to use. Defaults to "gist_rainbow".
|
176
|
-
color (str,
|
220
|
+
color (str, List, Tuple, np.ndarray, or None, optional): A specific color or array of colors to use for the domains.
|
177
221
|
If None, the colormap will be used. Defaults to None.
|
178
222
|
random_seed (int, optional): Seed for random number generation. Defaults to 888.
|
179
223
|
|
@@ -237,6 +281,33 @@ def _assign_distant_colors(dist_matrix, num_colors_to_generate):
|
|
237
281
|
return color_positions
|
238
282
|
|
239
283
|
|
284
|
+
def _blend_colors_perceptually(
|
285
|
+
colors: Union[List, Tuple, np.ndarray], enrichments: List[float], gamma: float = 2.2
|
286
|
+
) -> Tuple[float, float, float, float]:
|
287
|
+
"""Blends a list of RGBA colors using gamma correction for perceptually uniform color mixing.
|
288
|
+
|
289
|
+
Args:
|
290
|
+
colors (List, Tuple, np.ndarray): List of RGBA colors. Can be a list, tuple, or NumPy array of RGBA values.
|
291
|
+
enrichments (List[float]): Corresponding list of enrichment values.
|
292
|
+
gamma (float, optional): Gamma correction factor, default is 2.2 (typical for perceptual blending).
|
293
|
+
|
294
|
+
Returns:
|
295
|
+
Tuple[float, float, float, float]: The blended RGBA color.
|
296
|
+
"""
|
297
|
+
# Normalize enrichments so they sum up to 1 (proportions)
|
298
|
+
total_enrichment = sum(enrichments)
|
299
|
+
proportions = [enrichment / total_enrichment for enrichment in enrichments]
|
300
|
+
# Convert colors to gamma-corrected space (apply gamma correction to RGB channels)
|
301
|
+
gamma_corrected_colors = [[channel**gamma for channel in color[:3]] for color in colors]
|
302
|
+
# Blend the colors in gamma-corrected space
|
303
|
+
blended_color = np.dot(proportions, gamma_corrected_colors)
|
304
|
+
# Convert back from gamma-corrected space to linear space (by applying inverse gamma correction)
|
305
|
+
blended_color = [channel ** (1 / gamma) for channel in blended_color]
|
306
|
+
# Average the alpha channel separately (no gamma correction on alpha)
|
307
|
+
alpha = np.dot(proportions, [color[3] for color in colors])
|
308
|
+
return tuple(blended_color + [alpha])
|
309
|
+
|
310
|
+
|
240
311
|
def _transform_colors(
|
241
312
|
colors: np.ndarray,
|
242
313
|
enrichment_sums: np.ndarray,
|
@@ -283,14 +354,14 @@ def _transform_colors(
|
|
283
354
|
|
284
355
|
|
285
356
|
def to_rgba(
|
286
|
-
color: Union[str,
|
357
|
+
color: Union[str, List, Tuple, np.ndarray],
|
287
358
|
alpha: Union[float, None] = None,
|
288
359
|
num_repeats: Union[int, None] = None,
|
289
360
|
) -> np.ndarray:
|
290
361
|
"""Convert color(s) to RGBA format, applying alpha and repeating as needed.
|
291
362
|
|
292
363
|
Args:
|
293
|
-
color (str,
|
364
|
+
color (str, List, Tuple, np.ndarray): The color(s) to convert. Can be a string (e.g., 'red'), a list or tuple of RGB/RGBA values,
|
294
365
|
or an `np.ndarray` of colors.
|
295
366
|
alpha (float, None, optional): Alpha value (transparency) to apply. If provided, it overrides any existing alpha values found
|
296
367
|
in color.
|
@@ -306,7 +377,7 @@ def to_rgba(
|
|
306
377
|
if isinstance(c, str):
|
307
378
|
# Convert color names or hex values (e.g., 'red', '#FF5733') to RGBA
|
308
379
|
rgba = np.array(mcolors.to_rgba(c))
|
309
|
-
elif isinstance(c, (
|
380
|
+
elif isinstance(c, (List, Tuple, np.ndarray)) and len(c) in [3, 4]:
|
310
381
|
# Convert RGB (3) or RGBA (4) values to RGBA format
|
311
382
|
rgba = np.array(mcolors.to_rgba(c))
|
312
383
|
else:
|
@@ -325,8 +396,8 @@ def to_rgba(
|
|
325
396
|
# Handle a single color (string or RGB/RGBA list/tuple)
|
326
397
|
if (
|
327
398
|
isinstance(color, str)
|
328
|
-
or isinstance(color, (
|
329
|
-
and not any(isinstance(c, (str,
|
399
|
+
or isinstance(color, (List, Tuple, np.ndarray))
|
400
|
+
and not any(isinstance(c, (str, List, Tuple, np.ndarray)) for c in color)
|
330
401
|
):
|
331
402
|
rgba_color = convert_to_rgba(color)
|
332
403
|
if num_repeats:
|
@@ -336,7 +407,7 @@ def to_rgba(
|
|
336
407
|
return np.array([rgba_color]) # Return a single color wrapped in a numpy array
|
337
408
|
|
338
409
|
# Handle a list/array of colors
|
339
|
-
elif isinstance(color, (
|
410
|
+
elif isinstance(color, (List, Tuple, np.ndarray)):
|
340
411
|
rgba_colors = np.array(
|
341
412
|
[convert_to_rgba(c) for c in color]
|
342
413
|
) # Convert each color in the list to RGBA
|
risk/risk.py
CHANGED
@@ -362,7 +362,7 @@ class RISK(NetworkIO, AnnotationsIO):
|
|
362
362
|
|
363
363
|
Args:
|
364
364
|
graph (NetworkGraph): The graph to plot.
|
365
|
-
figsize (
|
365
|
+
figsize (Tuple, optional): Size of the figure. Defaults to (10, 10).
|
366
366
|
background_color (str, optional): Background color of the plot. Defaults to "white".
|
367
367
|
background_alpha (float, None, optional): Transparency level of the background color. If provided, it overrides
|
368
368
|
any existing alpha values found in background_color. Defaults to 1.0.
|
@@ -1,27 +1,27 @@
|
|
1
|
-
risk/__init__.py,sha256=
|
1
|
+
risk/__init__.py,sha256=jShwk2Z-jTDhGp_Y0GZIkJ1BoZFHOBTRGSKtBnS4Re0,113
|
2
2
|
risk/constants.py,sha256=XInRaH78Slnw_sWgAsBFbUHkyA0h0jL0DKGuQNbOvjM,550
|
3
|
-
risk/risk.py,sha256=
|
3
|
+
risk/risk.py,sha256=FQp5269IsCh-flmSWIpV7sBmvbGHjlrSy89SkImkxCE,21231
|
4
4
|
risk/annotations/__init__.py,sha256=vUpVvMRE5if01Ic8QY6M2Ae3EFGJHdugEe9PdEkAW4Y,138
|
5
5
|
risk/annotations/annotations.py,sha256=7ilzXxrlHqN75J3q8WeHz0n79D-jAtUQx5czvC9wfIM,11303
|
6
6
|
risk/annotations/io.py,sha256=TTXVJQgUGAlKpnGBcx7Dow146IGyozA03nSbl3S7M5M,9475
|
7
7
|
risk/log/__init__.py,sha256=aDUz5LMFQsz0UlsQI2EdXtiBKRLfml1UMeZKC7QQIGU,134
|
8
8
|
risk/log/config.py,sha256=m8pzj-hN4vI_2JdJUfyOoSvzT8_lhoIfBt27sKbnOes,4535
|
9
|
-
risk/log/params.py,sha256=
|
9
|
+
risk/log/params.py,sha256=lgwhtO_pQWLd2_Cpu0T7BMwH5NiA4GFW0aP6d1_rJTE,6363
|
10
10
|
risk/neighborhoods/__init__.py,sha256=tKKEg4lsbqFukpgYlUGxU_v_9FOqK7V0uvM9T2QzoL0,206
|
11
11
|
risk/neighborhoods/community.py,sha256=stYYBXeZlGLMV-k8ckQeIqThT6v9y-S3hETobAo9590,6817
|
12
|
-
risk/neighborhoods/domains.py,sha256=
|
12
|
+
risk/neighborhoods/domains.py,sha256=DbhUFsvbr8wuvrNr7a0PaAJO-cdv6U3-T4CXB4-j5Qw,10930
|
13
13
|
risk/neighborhoods/neighborhoods.py,sha256=M-wL4xB_BUTlSZg90swygO5NdrZ6hFUFqs6jsiZaqHk,18260
|
14
14
|
risk/network/__init__.py,sha256=iEPeJdZfqp0toxtbElryB8jbz9_t_k4QQ3iDvKE8C_0,126
|
15
15
|
risk/network/geometry.py,sha256=H1yGVVqgbfpzBzJwEheDLfvGLSA284jGQQTn612L4Vc,6759
|
16
|
-
risk/network/graph.py,sha256=
|
17
|
-
risk/network/io.py,sha256=
|
16
|
+
risk/network/graph.py,sha256=X63SNlWIov3oz0aMBMZfbHdmckbLqakZli5HP2Y5OdU,8519
|
17
|
+
risk/network/io.py,sha256=w_9fUcZUVXAPRKGhLBc7xhIJs8l83szHiBQTdaNN0gk,22942
|
18
18
|
risk/network/plot/__init__.py,sha256=MfmaXJgAZJgXZ2wrhK8pXwzETlcMaLChhWXKAozniAo,98
|
19
|
-
risk/network/plot/canvas.py,sha256=
|
20
|
-
risk/network/plot/contour.py,sha256=
|
21
|
-
risk/network/plot/labels.py,sha256=
|
22
|
-
risk/network/plot/network.py,sha256=
|
23
|
-
risk/network/plot/plotter.py,sha256=
|
24
|
-
risk/network/plot/utils/color.py,sha256=
|
19
|
+
risk/network/plot/canvas.py,sha256=hdrmGd2TCuii8wn6jDQfyJTI5YXDNGYFLiU4TyqAYbE,10778
|
20
|
+
risk/network/plot/contour.py,sha256=ecmqNpyq512Koa14OGa58Z7EP_oUOxYfS4CuU1P9ras,15027
|
21
|
+
risk/network/plot/labels.py,sha256=hoW6AL1dAUUt2WhWOEDu-Q28L_ojf13NkuxeqfHtqWc,44848
|
22
|
+
risk/network/plot/network.py,sha256=nfTmQxx1YwS3taXwq8WSCfu6nfKFOyxj7T5605qLXVM,13615
|
23
|
+
risk/network/plot/plotter.py,sha256=6534tKpOb2ZXn1imu_CDM_BkLaThi50eaRsJQyjsm84,5770
|
24
|
+
risk/network/plot/utils/color.py,sha256=Y_AUIoj_zKzChz_aC-WoYqiZ5P4qil3k7lmm4MHDaPY,19606
|
25
25
|
risk/network/plot/utils/layout.py,sha256=znssSqe2VZzzSz47hLZtTuXwMTpHR9b8lkQPL0BX7OA,1950
|
26
26
|
risk/stats/__init__.py,sha256=WcgoETQ-hS0LQqKRsAMIPtP15xZ-4eul6VUBuUx4Wzc,220
|
27
27
|
risk/stats/hypergeom.py,sha256=o6Qnj31gCAKxr2uQirXrbv7XvdDJGEq69MFW-ubx_hA,2272
|
@@ -30,8 +30,8 @@ risk/stats/stats.py,sha256=kvShov-94W6ffgDUTb522vB9hDJQSyTsYif_UIaFfSM,7059
|
|
30
30
|
risk/stats/permutation/__init__.py,sha256=neJp7FENC-zg_CGOXqv-iIvz1r5XUKI9Ruxhmq7kDOI,105
|
31
31
|
risk/stats/permutation/permutation.py,sha256=D84Rcpt6iTQniK0PfQGcw9bLcHbMt9p-ARcurUnIXZQ,10095
|
32
32
|
risk/stats/permutation/test_functions.py,sha256=lftOude6hee0pyR80HlBD32522JkDoN5hrKQ9VEbuoY,2345
|
33
|
-
risk_network-0.0.
|
34
|
-
risk_network-0.0.
|
35
|
-
risk_network-0.0.
|
36
|
-
risk_network-0.0.
|
37
|
-
risk_network-0.0.
|
33
|
+
risk_network-0.0.8b19.dist-info/LICENSE,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
|
34
|
+
risk_network-0.0.8b19.dist-info/METADATA,sha256=zRIBsg3MXtdrk10GQ2Byns8YWleTkv0taFT67hCrBl4,47498
|
35
|
+
risk_network-0.0.8b19.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
|
36
|
+
risk_network-0.0.8b19.dist-info/top_level.txt,sha256=NX7C2PFKTvC1JhVKv14DFlFAIFnKc6Lpsu1ZfxvQwVw,5
|
37
|
+
risk_network-0.0.8b19.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|