risk-network 0.0.12b1__py3-none-any.whl → 0.0.12b3__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/annotation/__init__.py +10 -0
- risk/{annotations/annotations.py → annotation/annotation.py} +44 -44
- risk/{annotations → annotation}/io.py +46 -46
- risk/{annotations → annotation}/nltk_setup.py +4 -4
- risk/log/parameters.py +5 -5
- risk/neighborhoods/api.py +36 -36
- risk/neighborhoods/domains.py +20 -24
- risk/neighborhoods/neighborhoods.py +4 -4
- risk/neighborhoods/stats/permutation/permutation.py +17 -17
- risk/neighborhoods/stats/permutation/test_functions.py +2 -2
- risk/neighborhoods/stats/tests.py +41 -41
- risk/network/graph/api.py +17 -17
- risk/network/graph/graph.py +17 -11
- risk/network/graph/summary.py +10 -10
- risk/network/io.py +12 -12
- risk/network/plotter/canvas.py +1 -1
- risk/network/plotter/contour.py +3 -3
- risk/network/plotter/labels.py +72 -74
- risk/network/plotter/network.py +6 -6
- risk/network/plotter/plotter.py +6 -6
- risk/network/plotter/utils/colors.py +12 -8
- risk/network/plotter/utils/layout.py +3 -3
- risk/risk.py +2 -2
- {risk_network-0.0.12b1.dist-info → risk_network-0.0.12b3.dist-info}/METADATA +1 -1
- risk_network-0.0.12b3.dist-info/RECORD +40 -0
- {risk_network-0.0.12b1.dist-info → risk_network-0.0.12b3.dist-info}/WHEEL +1 -1
- risk/annotations/__init__.py +0 -10
- risk_network-0.0.12b1.dist-info/RECORD +0 -40
- {risk_network-0.0.12b1.dist-info → risk_network-0.0.12b3.dist-info}/licenses/LICENSE +0 -0
- {risk_network-0.0.12b1.dist-info → risk_network-0.0.12b3.dist-info}/top_level.txt +0 -0
risk/network/plotter/labels.py
CHANGED
@@ -134,7 +134,7 @@ class Labels:
|
|
134
134
|
max_chars_per_line = int(1e6)
|
135
135
|
# Normalize words_to_omit to lowercase
|
136
136
|
if words_to_omit:
|
137
|
-
words_to_omit = set(word.lower() for word in words_to_omit)
|
137
|
+
words_to_omit = list(set(word.lower() for word in words_to_omit))
|
138
138
|
|
139
139
|
# Calculate the center and radius of domains to position labels around the network
|
140
140
|
domain_id_to_centroid_map = {}
|
@@ -205,13 +205,11 @@ class Labels:
|
|
205
205
|
for idx, (domain, pos) in zip(valid_indices, best_label_positions.items()):
|
206
206
|
centroid = filtered_domain_centroids[domain]
|
207
207
|
# Split by special key TERM_DELIMITER to split annotation into multiple lines
|
208
|
-
|
208
|
+
terms = filtered_domain_terms[domain].split(TERM_DELIMITER)
|
209
209
|
if fontcase is not None:
|
210
|
-
|
211
|
-
words=annotations, transformation=fontcase
|
212
|
-
)
|
210
|
+
terms = self._apply_str_transformation(words=terms, transformation=fontcase)
|
213
211
|
self.ax.annotate(
|
214
|
-
"\n".join(
|
212
|
+
"\n".join(terms),
|
215
213
|
xy=centroid,
|
216
214
|
xytext=pos,
|
217
215
|
textcoords="data",
|
@@ -237,7 +235,7 @@ class Labels:
|
|
237
235
|
self.ax.text(
|
238
236
|
centroid[0],
|
239
237
|
centroid[1],
|
240
|
-
domain,
|
238
|
+
str(domain),
|
241
239
|
ha="center",
|
242
240
|
va="center",
|
243
241
|
fontsize=fontsize,
|
@@ -373,7 +371,7 @@ class Labels:
|
|
373
371
|
|
374
372
|
def _process_ids_to_keep(
|
375
373
|
self,
|
376
|
-
domain_id_to_centroid_map: Dict[
|
374
|
+
domain_id_to_centroid_map: Dict[int, np.ndarray],
|
377
375
|
ids_to_keep: Union[List[str], Tuple[str], np.ndarray],
|
378
376
|
ids_to_labels: Union[Dict[int, str], None],
|
379
377
|
words_to_omit: Union[List[str], None],
|
@@ -382,14 +380,14 @@ class Labels:
|
|
382
380
|
max_label_lines: int,
|
383
381
|
min_chars_per_line: int,
|
384
382
|
max_chars_per_line: int,
|
385
|
-
filtered_domain_centroids: Dict[
|
386
|
-
filtered_domain_terms: Dict[
|
383
|
+
filtered_domain_centroids: Dict[int, np.ndarray],
|
384
|
+
filtered_domain_terms: Dict[int, str],
|
387
385
|
valid_indices: List[int],
|
388
386
|
) -> None:
|
389
387
|
"""Process the ids_to_keep, apply filtering, and store valid domain centroids and terms.
|
390
388
|
|
391
389
|
Args:
|
392
|
-
domain_id_to_centroid_map (Dict[
|
390
|
+
domain_id_to_centroid_map (Dict[int, np.ndarray]): Mapping of domain IDs to their centroids.
|
393
391
|
ids_to_keep (List, Tuple, or np.ndarray, optional): IDs of domains that must be labeled.
|
394
392
|
ids_to_labels (Dict[int, str], None, optional): A dictionary mapping domain IDs to custom labels. Defaults to None.
|
395
393
|
words_to_omit (List, optional): List of words to omit from the labels. Defaults to None.
|
@@ -398,7 +396,7 @@ class Labels:
|
|
398
396
|
max_label_lines (int): Maximum number of lines in a label.
|
399
397
|
min_chars_per_line (int): Minimum number of characters in a line to display.
|
400
398
|
max_chars_per_line (int): Maximum number of characters in a line to display.
|
401
|
-
filtered_domain_centroids (Dict[
|
399
|
+
filtered_domain_centroids (Dict[int, np.ndarray]): Dictionary to store filtered domain centroids (output).
|
402
400
|
filtered_domain_terms (Dict[str, str]): Dictionary to store filtered domain terms (output).
|
403
401
|
valid_indices (List): List to store valid indices (output).
|
404
402
|
|
@@ -415,15 +413,15 @@ class Labels:
|
|
415
413
|
)
|
416
414
|
|
417
415
|
# Process each domain in ids_to_keep
|
418
|
-
for
|
416
|
+
for domain_id in ids_to_keep:
|
419
417
|
if (
|
420
|
-
|
421
|
-
and
|
418
|
+
domain_id in self.graph.domain_id_to_domain_terms_map
|
419
|
+
and domain_id in domain_id_to_centroid_map
|
422
420
|
):
|
423
|
-
domain_centroid = domain_id_to_centroid_map[
|
421
|
+
domain_centroid = domain_id_to_centroid_map[domain_id]
|
424
422
|
# No need to filter the domain terms if it is in ids_to_keep
|
425
423
|
_ = self._validate_and_update_domain(
|
426
|
-
|
424
|
+
domain_id=domain_id,
|
427
425
|
domain_centroid=domain_centroid,
|
428
426
|
domain_id_to_centroid_map=domain_id_to_centroid_map,
|
429
427
|
ids_to_labels=ids_to_labels,
|
@@ -439,7 +437,7 @@ class Labels:
|
|
439
437
|
|
440
438
|
def _process_remaining_domains(
|
441
439
|
self,
|
442
|
-
domain_id_to_centroid_map: Dict[
|
440
|
+
domain_id_to_centroid_map: Dict[int, np.ndarray],
|
443
441
|
ids_to_keep: Union[List[str], Tuple[str], np.ndarray],
|
444
442
|
ids_to_labels: Union[Dict[int, str], None],
|
445
443
|
words_to_omit: Union[List[str], None],
|
@@ -448,14 +446,14 @@ class Labels:
|
|
448
446
|
max_label_lines: int,
|
449
447
|
min_chars_per_line: int,
|
450
448
|
max_chars_per_line: int,
|
451
|
-
filtered_domain_centroids: Dict[
|
452
|
-
filtered_domain_terms: Dict[
|
449
|
+
filtered_domain_centroids: Dict[int, np.ndarray],
|
450
|
+
filtered_domain_terms: Dict[int, str],
|
453
451
|
valid_indices: List[int],
|
454
452
|
) -> None:
|
455
453
|
"""Process remaining domains to fill in additional labels, respecting the remaining_labels limit.
|
456
454
|
|
457
455
|
Args:
|
458
|
-
domain_id_to_centroid_map (Dict[
|
456
|
+
domain_id_to_centroid_map (Dict[int, np.ndarray]): Mapping of domain IDs to their centroids.
|
459
457
|
ids_to_keep (List, Tuple, or np.ndarray, optional): IDs of domains that must be labeled.
|
460
458
|
ids_to_labels (Dict[int, str], None, optional): A dictionary mapping domain IDs to custom labels. Defaults to None.
|
461
459
|
words_to_omit (List, optional): List of words to omit from the labels. Defaults to None.
|
@@ -464,7 +462,7 @@ class Labels:
|
|
464
462
|
max_label_lines (int): Maximum number of lines in a label.
|
465
463
|
min_chars_per_line (int): Minimum number of characters in a line to display.
|
466
464
|
max_chars_per_line (int): Maximum number of characters in a line to display.
|
467
|
-
filtered_domain_centroids (Dict[
|
465
|
+
filtered_domain_centroids (Dict[int, np.ndarray]): Dictionary to store filtered domain centroids (output).
|
468
466
|
filtered_domain_terms (Dict[str, str]): Dictionary to store filtered domain terms (output).
|
469
467
|
valid_indices (List): List to store valid indices (output).
|
470
468
|
|
@@ -485,24 +483,24 @@ class Labels:
|
|
485
483
|
return np.linalg.norm(centroid1 - centroid2)
|
486
484
|
|
487
485
|
# Domains to plot on network
|
488
|
-
|
486
|
+
selected_domain_ids = []
|
489
487
|
# Find the farthest apart domains using centroids
|
490
488
|
if remaining_domains and remaining_labels:
|
491
489
|
first_domain = next(iter(remaining_domains)) # Pick the first domain to start
|
492
|
-
|
490
|
+
selected_domain_ids.append(first_domain)
|
493
491
|
|
494
|
-
while len(
|
492
|
+
while len(selected_domain_ids) < remaining_labels:
|
495
493
|
farthest_domain = None
|
496
494
|
max_distance = -1
|
497
495
|
# Find the domain farthest from any already selected domain
|
498
496
|
for candidate_domain, candidate_centroid in remaining_domains.items():
|
499
|
-
if candidate_domain in
|
497
|
+
if candidate_domain in selected_domain_ids:
|
500
498
|
continue
|
501
499
|
|
502
500
|
# Calculate the minimum distance to any selected domain
|
503
501
|
min_distance = min(
|
504
502
|
calculate_distance(candidate_centroid, remaining_domains[dom])
|
505
|
-
for dom in
|
503
|
+
for dom in selected_domain_ids
|
506
504
|
)
|
507
505
|
# Update the farthest domain if the minimum distance is greater
|
508
506
|
if min_distance > max_distance:
|
@@ -511,15 +509,15 @@ class Labels:
|
|
511
509
|
|
512
510
|
# Add the farthest domain to the selected domains
|
513
511
|
if farthest_domain:
|
514
|
-
|
512
|
+
selected_domain_ids.append(farthest_domain)
|
515
513
|
else:
|
516
514
|
break # No more domains to select
|
517
515
|
|
518
516
|
# Process the selected domains and add to filtered lists
|
519
|
-
for
|
520
|
-
domain_centroid = remaining_domains[
|
517
|
+
for domain_id in selected_domain_ids:
|
518
|
+
domain_centroid = remaining_domains[domain_id]
|
521
519
|
is_domain_valid = self._validate_and_update_domain(
|
522
|
-
|
520
|
+
domain_id=domain_id,
|
523
521
|
domain_centroid=domain_centroid,
|
524
522
|
domain_id_to_centroid_map=domain_id_to_centroid_map,
|
525
523
|
ids_to_labels=ids_to_labels,
|
@@ -540,45 +538,45 @@ class Labels:
|
|
540
538
|
|
541
539
|
def _validate_and_update_domain(
|
542
540
|
self,
|
543
|
-
|
541
|
+
domain_id: int,
|
544
542
|
domain_centroid: np.ndarray,
|
545
|
-
domain_id_to_centroid_map: Dict[
|
543
|
+
domain_id_to_centroid_map: Dict[int, np.ndarray],
|
546
544
|
ids_to_labels: Union[Dict[int, str], None],
|
547
545
|
words_to_omit: Union[List[str], None],
|
548
546
|
min_label_lines: int,
|
549
547
|
max_label_lines: int,
|
550
548
|
min_chars_per_line: int,
|
551
549
|
max_chars_per_line: int,
|
552
|
-
filtered_domain_centroids: Dict[
|
553
|
-
filtered_domain_terms: Dict[
|
550
|
+
filtered_domain_centroids: Dict[int, np.ndarray],
|
551
|
+
filtered_domain_terms: Dict[int, str],
|
554
552
|
valid_indices: List[int],
|
555
553
|
) -> bool:
|
556
554
|
"""Validate and process the domain terms, updating relevant dictionaries if valid.
|
557
555
|
|
558
556
|
Args:
|
559
|
-
|
557
|
+
domain_id (int): Domain ID to process.
|
560
558
|
domain_centroid (np.ndarray): Centroid position of the domain.
|
561
|
-
domain_id_to_centroid_map (Dict[
|
559
|
+
domain_id_to_centroid_map (Dict[int, np.ndarray]): Mapping of domain IDs to their centroids.
|
562
560
|
ids_to_labels (Dict[int, str], None, optional): A dictionary mapping domain IDs to custom labels. Defaults to None.
|
563
561
|
words_to_omit (List[str], None, optional): List of words to omit from the labels. Defaults to None.
|
564
562
|
min_label_lines (int): Minimum number of lines required in a label.
|
565
563
|
max_label_lines (int): Maximum number of lines allowed in a label.
|
566
564
|
min_chars_per_line (int): Minimum number of characters allowed per line.
|
567
565
|
max_chars_per_line (int): Maximum number of characters allowed per line.
|
568
|
-
filtered_domain_centroids (Dict[
|
566
|
+
filtered_domain_centroids (Dict[int, np.ndarray]): Dictionary to store valid domain centroids.
|
569
567
|
filtered_domain_terms (Dict[str, str]): Dictionary to store valid domain terms.
|
570
568
|
valid_indices (List[int]): List of valid domain indices.
|
571
569
|
|
572
570
|
Returns:
|
573
571
|
bool: True if the domain is valid and added to the filtered dictionaries, False otherwise.
|
574
572
|
"""
|
575
|
-
if ids_to_labels and
|
573
|
+
if ids_to_labels and domain_id in ids_to_labels:
|
576
574
|
# Directly use custom labels without filtering
|
577
|
-
domain_terms = ids_to_labels[
|
575
|
+
domain_terms = ids_to_labels[domain_id]
|
578
576
|
else:
|
579
577
|
# Process the domain terms automatically
|
580
578
|
domain_terms = self._process_terms(
|
581
|
-
|
579
|
+
domain_id=domain_id,
|
582
580
|
words_to_omit=words_to_omit,
|
583
581
|
max_label_lines=max_label_lines,
|
584
582
|
min_chars_per_line=min_chars_per_line,
|
@@ -595,24 +593,24 @@ class Labels:
|
|
595
593
|
return False
|
596
594
|
|
597
595
|
# Store the valid terms and centroids
|
598
|
-
filtered_domain_centroids[
|
599
|
-
filtered_domain_terms[
|
600
|
-
valid_indices.append(list(domain_id_to_centroid_map.keys()).index(
|
596
|
+
filtered_domain_centroids[domain_id] = domain_centroid
|
597
|
+
filtered_domain_terms[domain_id] = domain_terms
|
598
|
+
valid_indices.append(list(domain_id_to_centroid_map.keys()).index(domain_id))
|
601
599
|
|
602
600
|
return True
|
603
601
|
|
604
602
|
def _process_terms(
|
605
603
|
self,
|
606
|
-
|
604
|
+
domain_id: int,
|
607
605
|
words_to_omit: Union[List[str], None],
|
608
606
|
max_label_lines: int,
|
609
607
|
min_chars_per_line: int,
|
610
608
|
max_chars_per_line: int,
|
611
|
-
) ->
|
609
|
+
) -> str:
|
612
610
|
"""Process terms for a domain, applying word length constraints and combining words where appropriate.
|
613
611
|
|
614
612
|
Args:
|
615
|
-
|
613
|
+
domain_id (int): Domain ID to process.
|
616
614
|
words_to_omit (List[str], None): List of words to omit from the labels.
|
617
615
|
max_label_lines (int): Maximum number of lines in a label.
|
618
616
|
min_chars_per_line (int): Minimum number of characters in a line to display.
|
@@ -622,7 +620,7 @@ class Labels:
|
|
622
620
|
str: Processed terms separated by TERM_DELIMITER, with words combined if necessary to fit within constraints.
|
623
621
|
"""
|
624
622
|
# Set custom labels from significant terms
|
625
|
-
terms = self.graph.domain_id_to_domain_terms_map[
|
623
|
+
terms = self.graph.domain_id_to_domain_terms_map[domain_id].split(" ")
|
626
624
|
# Apply words_to_omit and word length constraints
|
627
625
|
if words_to_omit:
|
628
626
|
terms = [
|
@@ -632,7 +630,7 @@ class Labels:
|
|
632
630
|
]
|
633
631
|
|
634
632
|
# Use the combine_words function directly to handle word combinations and length constraints
|
635
|
-
compressed_terms = self._combine_words(
|
633
|
+
compressed_terms = self._combine_words(list(terms), max_chars_per_line, max_label_lines)
|
636
634
|
|
637
635
|
return compressed_terms
|
638
636
|
|
@@ -647,8 +645,8 @@ class Labels:
|
|
647
645
|
scale_factor: float = 1.0,
|
648
646
|
ids_to_colors: Union[Dict[int, Any], None] = None,
|
649
647
|
random_seed: int = 888,
|
650
|
-
) ->
|
651
|
-
"""Get colors for the labels based on node
|
648
|
+
) -> List:
|
649
|
+
"""Get colors for the labels based on node annotation or a specified colormap.
|
652
650
|
|
653
651
|
Args:
|
654
652
|
cmap (str, optional): Name of the colormap to use for generating label colors. Defaults to "gist_rainbow".
|
@@ -666,7 +664,7 @@ class Labels:
|
|
666
664
|
random_seed (int, optional): Seed for random number generation to ensure reproducibility. Defaults to 888.
|
667
665
|
|
668
666
|
Returns:
|
669
|
-
|
667
|
+
List: Array of RGBA colors for label annotation.
|
670
668
|
"""
|
671
669
|
return get_annotated_domain_colors(
|
672
670
|
graph=self.graph,
|
@@ -750,21 +748,21 @@ class Labels:
|
|
750
748
|
|
751
749
|
def _calculate_best_label_positions(
|
752
750
|
self,
|
753
|
-
filtered_domain_centroids: Dict[
|
754
|
-
center:
|
751
|
+
filtered_domain_centroids: Dict[int, Any],
|
752
|
+
center: Tuple[float, float],
|
755
753
|
radius: float,
|
756
754
|
offset: float,
|
757
|
-
) -> Dict[
|
755
|
+
) -> Dict[int, Any]:
|
758
756
|
"""Calculate and optimize label positions for clarity.
|
759
757
|
|
760
758
|
Args:
|
761
|
-
filtered_domain_centroids (Dict[
|
762
|
-
center (
|
759
|
+
filtered_domain_centroids (Dict[int, Any]): Centroids of the filtered domains.
|
760
|
+
center (Tuple[float, float]): The center point around which labels are positioned.
|
763
761
|
radius (float): The radius for positioning labels around the center.
|
764
762
|
offset (float): The offset distance from the radius for positioning labels.
|
765
763
|
|
766
764
|
Returns:
|
767
|
-
Dict[
|
765
|
+
Dict[int, Any]: Optimized positions for labels.
|
768
766
|
"""
|
769
767
|
num_domains = len(filtered_domain_centroids)
|
770
768
|
# Calculate equidistant positions around the center for initial label placement
|
@@ -777,12 +775,12 @@ class Labels:
|
|
777
775
|
return self._optimize_label_positions(label_positions, filtered_domain_centroids)
|
778
776
|
|
779
777
|
def _calculate_equidistant_positions_around_center(
|
780
|
-
self, center:
|
778
|
+
self, center: Tuple[float, float], radius: float, label_offset: float, num_domains: int
|
781
779
|
) -> List[np.ndarray]:
|
782
780
|
"""Calculate positions around a center at equidistant angles.
|
783
781
|
|
784
782
|
Args:
|
785
|
-
center (
|
783
|
+
center (Tuple[float, float]): The center point around which positions are calculated.
|
786
784
|
radius (float): The radius at which positions are calculated.
|
787
785
|
label_offset (float): The offset added to the radius for label positioning.
|
788
786
|
num_domains (int): The number of positions (or domains) to calculate.
|
@@ -799,16 +797,16 @@ class Labels:
|
|
799
797
|
]
|
800
798
|
|
801
799
|
def _optimize_label_positions(
|
802
|
-
self, best_label_positions: Dict[
|
803
|
-
) -> Dict[
|
800
|
+
self, best_label_positions: Dict[int, Any], domain_centroids: Dict[int, Any]
|
801
|
+
) -> Dict[int, Any]:
|
804
802
|
"""Optimize label positions around the perimeter to minimize total distance to centroids.
|
805
803
|
|
806
804
|
Args:
|
807
|
-
best_label_positions (Dict[
|
808
|
-
domain_centroids (Dict[
|
805
|
+
best_label_positions (Dict[int, Any]): Initial positions of labels around the perimeter.
|
806
|
+
domain_centroids (Dict[int, Any]): Centroid positions of the domains.
|
809
807
|
|
810
808
|
Returns:
|
811
|
-
Dict[
|
809
|
+
Dict[int, Any]: Optimized label positions.
|
812
810
|
"""
|
813
811
|
while True:
|
814
812
|
improvement = False # Start each iteration assuming no improvement
|
@@ -838,39 +836,39 @@ class Labels:
|
|
838
836
|
return best_label_positions
|
839
837
|
|
840
838
|
def _calculate_total_distance(
|
841
|
-
self, label_positions: Dict[
|
839
|
+
self, label_positions: Dict[int, Any], domain_centroids: Dict[int, Any]
|
842
840
|
) -> float:
|
843
841
|
"""Calculate the total distance from label positions to their domain centroids.
|
844
842
|
|
845
843
|
Args:
|
846
|
-
label_positions (Dict[
|
847
|
-
domain_centroids (Dict[
|
844
|
+
label_positions (Dict[int, Any]): Positions of labels around the perimeter.
|
845
|
+
domain_centroids (Dict[int, Any]): Centroid positions of the domains.
|
848
846
|
|
849
847
|
Returns:
|
850
848
|
float: The total distance from labels to centroids.
|
851
849
|
"""
|
852
|
-
total_distance = 0
|
850
|
+
total_distance = 0.0
|
853
851
|
# Iterate through each domain and calculate the distance to its centroid
|
854
852
|
for domain, pos in label_positions.items():
|
855
853
|
centroid = domain_centroids[domain]
|
856
|
-
total_distance += np.linalg.norm(centroid - pos)
|
854
|
+
total_distance += float(np.linalg.norm(centroid - pos))
|
857
855
|
|
858
856
|
return total_distance
|
859
857
|
|
860
858
|
def _swap_and_evaluate(
|
861
859
|
self,
|
862
|
-
label_positions: Dict[
|
860
|
+
label_positions: Dict[int, Any],
|
863
861
|
i: int,
|
864
862
|
j: int,
|
865
|
-
domain_centroids: Dict[
|
863
|
+
domain_centroids: Dict[int, Any],
|
866
864
|
) -> float:
|
867
865
|
"""Swap two labels and evaluate the total distance after the swap.
|
868
866
|
|
869
867
|
Args:
|
870
|
-
label_positions (Dict[
|
868
|
+
label_positions (Dict[int, Any]): Positions of labels around the perimeter.
|
871
869
|
i (int): Index of the first label to swap.
|
872
870
|
j (int): Index of the second label to swap.
|
873
|
-
domain_centroids (Dict[
|
871
|
+
domain_centroids (Dict[int, Any]): Centroid positions of the domains.
|
874
872
|
|
875
873
|
Returns:
|
876
874
|
float: The total distance after swapping the two labels.
|
risk/network/plotter/network.py
CHANGED
@@ -94,10 +94,10 @@ class Network:
|
|
94
94
|
# Draw the nodes of the graph
|
95
95
|
nx.draw_networkx_nodes(
|
96
96
|
self.graph.network,
|
97
|
-
pos=node_coordinates,
|
97
|
+
pos=node_coordinates.tolist(),
|
98
98
|
node_size=node_size,
|
99
99
|
node_shape=node_shape,
|
100
|
-
node_color=node_color_rgba,
|
100
|
+
node_color=node_color_rgba.tolist(),
|
101
101
|
edgecolors=node_edgecolor_rgba,
|
102
102
|
linewidths=node_edgewidth,
|
103
103
|
ax=self.ax,
|
@@ -105,9 +105,9 @@ class Network:
|
|
105
105
|
# Draw the edges of the graph
|
106
106
|
nx.draw_networkx_edges(
|
107
107
|
self.graph.network,
|
108
|
-
pos=node_coordinates,
|
108
|
+
pos=node_coordinates.tolist(),
|
109
109
|
width=edge_width,
|
110
|
-
edge_color=edge_color_rgba,
|
110
|
+
edge_color=edge_color_rgba.tolist(),
|
111
111
|
ax=self.ax,
|
112
112
|
)
|
113
113
|
|
@@ -181,7 +181,7 @@ class Network:
|
|
181
181
|
nodelist=node_ids,
|
182
182
|
node_size=node_size,
|
183
183
|
node_shape=node_shape,
|
184
|
-
node_color=node_color_rgba,
|
184
|
+
node_color=node_color_rgba.tolist(),
|
185
185
|
edgecolors=node_edgecolor_rgba,
|
186
186
|
linewidths=node_edgewidth,
|
187
187
|
ax=self.ax,
|
@@ -192,7 +192,7 @@ class Network:
|
|
192
192
|
subgraph,
|
193
193
|
pos=node_coordinates,
|
194
194
|
width=edge_width,
|
195
|
-
edge_color=edge_color_rgba,
|
195
|
+
edge_color=edge_color_rgba.tolist(),
|
196
196
|
ax=self.ax,
|
197
197
|
)
|
198
198
|
|
risk/network/plotter/plotter.py
CHANGED
@@ -29,7 +29,7 @@ class Plotter(Canvas, Network, Contour, Labels):
|
|
29
29
|
def __init__(
|
30
30
|
self,
|
31
31
|
graph: Graph,
|
32
|
-
figsize: Tuple = (10, 10),
|
32
|
+
figsize: Union[List, Tuple, np.ndarray] = (10, 10),
|
33
33
|
background_color: Union[str, List, Tuple, np.ndarray] = "white",
|
34
34
|
background_alpha: Union[float, None] = 1.0,
|
35
35
|
pad: float = 0.3,
|
@@ -38,7 +38,7 @@ class Plotter(Canvas, Network, Contour, Labels):
|
|
38
38
|
|
39
39
|
Args:
|
40
40
|
graph (Graph): The network data and attributes to be visualized.
|
41
|
-
figsize (Tuple, optional): Size of the figure in inches (width, height). Defaults to (10, 10).
|
41
|
+
figsize (List, Tuple, np.ndarray, optional): Size of the figure in inches (width, height). Defaults to (10, 10).
|
42
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.
|
@@ -58,7 +58,7 @@ class Plotter(Canvas, Network, Contour, Labels):
|
|
58
58
|
def _initialize_plot(
|
59
59
|
self,
|
60
60
|
graph: Graph,
|
61
|
-
figsize: Tuple,
|
61
|
+
figsize: Union[List, Tuple, np.ndarray],
|
62
62
|
background_color: Union[str, List, Tuple, np.ndarray],
|
63
63
|
background_alpha: Union[float, None],
|
64
64
|
pad: float,
|
@@ -67,7 +67,7 @@ class Plotter(Canvas, Network, Contour, Labels):
|
|
67
67
|
|
68
68
|
Args:
|
69
69
|
graph (Graph): The network data and attributes to be visualized.
|
70
|
-
figsize (Tuple): Size of the figure in inches (width, height).
|
70
|
+
figsize (List, Tuple, np.ndarray, optional): Size of the figure in inches (width, height). Defaults to (10, 10).
|
71
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`.
|
@@ -93,8 +93,8 @@ class Plotter(Canvas, Network, Contour, Labels):
|
|
93
93
|
fig, ax = plt.subplots(figsize=figsize)
|
94
94
|
fig.tight_layout() # Adjust subplot parameters to give specified padding
|
95
95
|
# Set axis limits based on the calculated bounding box and radius
|
96
|
-
ax.set_xlim(
|
97
|
-
ax.set_ylim(
|
96
|
+
ax.set_xlim((float(center[0] - radius - pad), float(center[0] + radius + pad)))
|
97
|
+
ax.set_ylim((float(center[1] - radius - pad), float(center[1] + radius + pad)))
|
98
98
|
ax.set_aspect("equal") # Ensure the aspect ratio is equal
|
99
99
|
|
100
100
|
# Set the background color of the plot
|
@@ -23,8 +23,8 @@ def get_annotated_domain_colors(
|
|
23
23
|
scale_factor: float = 1.0,
|
24
24
|
ids_to_colors: Union[Dict[int, Any], None] = None,
|
25
25
|
random_seed: int = 888,
|
26
|
-
) ->
|
27
|
-
"""Get colors for the domains based on node
|
26
|
+
) -> List[Tuple]:
|
27
|
+
"""Get colors for the domains based on node annotation, or use a specified color.
|
28
28
|
|
29
29
|
Args:
|
30
30
|
graph (Graph): The network data and attributes to be visualized.
|
@@ -41,7 +41,7 @@ def get_annotated_domain_colors(
|
|
41
41
|
random_seed (int, optional): Seed for random number generation to ensure reproducibility. Defaults to 888.
|
42
42
|
|
43
43
|
Returns:
|
44
|
-
|
44
|
+
List[Tuple]: List of RGBA colors for each domain, based on significance or the specified color.
|
45
45
|
"""
|
46
46
|
# Generate domain colors based on the significance data
|
47
47
|
node_colors = get_domain_colors(
|
@@ -61,10 +61,12 @@ def get_annotated_domain_colors(
|
|
61
61
|
if len(node_ids) > 1:
|
62
62
|
# For multi-node domains, choose the brightest color based on RGB sum
|
63
63
|
domain_colors = np.array([node_colors[node] for node in node_ids])
|
64
|
-
color =
|
64
|
+
color = tuple(
|
65
|
+
domain_colors[np.argmax(domain_colors[:, :3].sum(axis=1))]
|
66
|
+
) # Sum the RGB values
|
65
67
|
else:
|
66
68
|
# Single-node domains default to white (RGBA)
|
67
|
-
color =
|
69
|
+
color = tuple([1.0, 1.0, 1.0, 1.0])
|
68
70
|
|
69
71
|
annotated_colors.append(color)
|
70
72
|
|
@@ -202,7 +204,7 @@ def _get_composite_node_colors(
|
|
202
204
|
significances = node_info["significances"] # List of significance values
|
203
205
|
# Filter domains and significances to keep only those with corresponding colors in domain_ids_to_colors
|
204
206
|
filtered_domains_significances = [
|
205
|
-
(domain_id, significance)
|
207
|
+
(domain_id, float(significance))
|
206
208
|
for domain_id, significance in zip(domains, significances)
|
207
209
|
if domain_id in domain_ids_to_colors
|
208
210
|
]
|
@@ -216,7 +218,9 @@ def _get_composite_node_colors(
|
|
216
218
|
colors = [domain_ids_to_colors[domain_id] for domain_id in filtered_domains]
|
217
219
|
# Blend the colors using the given gamma (default is 2.2 if None)
|
218
220
|
gamma = blend_gamma if blend_gamma is not None else 2.2
|
219
|
-
composite_color = _blend_colors_perceptually(
|
221
|
+
composite_color = _blend_colors_perceptually(
|
222
|
+
colors, list(filtered_significances), gamma
|
223
|
+
)
|
220
224
|
# Assign the composite color to the node
|
221
225
|
composite_colors[node] = composite_color
|
222
226
|
|
@@ -246,7 +250,7 @@ def _get_colors(
|
|
246
250
|
if color:
|
247
251
|
# If a single color is specified, apply it to all domains
|
248
252
|
rgba = to_rgba(color, num_repeats=num_domains)
|
249
|
-
return rgba
|
253
|
+
return list(rgba)
|
250
254
|
|
251
255
|
# Load colormap and generate a large, maximally separated set of colors
|
252
256
|
colormap = matplotlib.colormaps.get_cmap(cmap)
|
@@ -11,7 +11,7 @@ import numpy as np
|
|
11
11
|
|
12
12
|
def calculate_bounding_box(
|
13
13
|
node_coordinates: np.ndarray, radius_margin: float = 1.05
|
14
|
-
) -> Tuple[
|
14
|
+
) -> Tuple[Tuple, float]:
|
15
15
|
"""Calculate the bounding box of the network based on node coordinates.
|
16
16
|
|
17
17
|
Args:
|
@@ -19,13 +19,13 @@ def calculate_bounding_box(
|
|
19
19
|
radius_margin (float, optional): Margin factor to apply to the bounding box radius. Defaults to 1.05.
|
20
20
|
|
21
21
|
Returns:
|
22
|
-
|
22
|
+
Tuple[Tuple, float]: Center (x, y) and radius of the bounding box.
|
23
23
|
"""
|
24
24
|
# Find minimum and maximum x, y coordinates
|
25
25
|
x_min, y_min = np.min(node_coordinates, axis=0)
|
26
26
|
x_max, y_max = np.max(node_coordinates, axis=0)
|
27
27
|
# Calculate the center of the bounding box
|
28
|
-
center =
|
28
|
+
center = ((x_min + x_max) / 2, (y_min + y_max) / 2)
|
29
29
|
# Calculate the radius of the bounding box, adjusted by the margin
|
30
30
|
radius = max(x_max - x_min, y_max - y_min) / 2 * radius_margin
|
31
31
|
return center, radius
|
risk/risk.py
CHANGED
@@ -3,7 +3,7 @@ risk/risk
|
|
3
3
|
~~~~~~~~~
|
4
4
|
"""
|
5
5
|
|
6
|
-
from risk.
|
6
|
+
from risk.annotation.io import AnnotationIO
|
7
7
|
from risk.log import params, set_global_verbosity
|
8
8
|
from risk.neighborhoods.api import NeighborhoodsAPI
|
9
9
|
from risk.network.graph.api import GraphAPI
|
@@ -11,7 +11,7 @@ from risk.network.io import NetworkIO
|
|
11
11
|
from risk.network.plotter.api import PlotterAPI
|
12
12
|
|
13
13
|
|
14
|
-
class RISK(NetworkIO,
|
14
|
+
class RISK(NetworkIO, AnnotationIO, NeighborhoodsAPI, GraphAPI, PlotterAPI):
|
15
15
|
"""RISK: A class for network analysis and visualization.
|
16
16
|
|
17
17
|
The RISK class integrates functionalities for loading networks, processing annotations,
|
@@ -0,0 +1,40 @@
|
|
1
|
+
risk/__init__.py,sha256=VvDXIVi8eQIQJ1y7eWSxIK70Xj19NdNxJCNkaTn9GsM,127
|
2
|
+
risk/risk.py,sha256=Wjuxob5bI70Tpz9t71i05g94AQ3qXEMjfEcm5IV9HSY,1118
|
3
|
+
risk/annotation/__init__.py,sha256=1EbGo41ClQb5ESTtitjOhrZhaLzzwr5aT-RYDX8w-h4,185
|
4
|
+
risk/annotation/annotation.py,sha256=OE859FpVnp69hDi1cN_CQqeiG0SrJ2ZCuIdQKzY-gt0,14675
|
5
|
+
risk/annotation/io.py,sha256=iffCXePQuMluytYQDg5tbu34g9ZsCUq1H4j-z-W25lE,10695
|
6
|
+
risk/annotation/nltk_setup.py,sha256=14B6L56_dwIgAOC9Rl4dNd4-b-aEngUCoJP9L9kEilU,3572
|
7
|
+
risk/log/__init__.py,sha256=en-hKzuFtQWos4oZd8PxJ9u9Pe5bdihiqH9-qk_5ppw,217
|
8
|
+
risk/log/console.py,sha256=PgjyEvyhYLUSHXPUKEqOmxsDsfrjPICIgqo_cAHq0N8,4575
|
9
|
+
risk/log/parameters.py,sha256=FnP-kdg0Kip0SyxFEn7su7BG51FbMtTfm3Rk96cnCoc,5801
|
10
|
+
risk/neighborhoods/__init__.py,sha256=CNS0VNbh_IdxldXjDkPqeTrxgY4hqi_Tc_MTADW_tzQ,182
|
11
|
+
risk/neighborhoods/api.py,sha256=TXbjbHfQDi2eZeD9086BBsUjhDiJq2BNEYALiGs_rPc,23299
|
12
|
+
risk/neighborhoods/community.py,sha256=0YdTh6wgMLiGMdtlaD7Vu_uxOVoZ9vDBjxbkglkrTV8,17808
|
13
|
+
risk/neighborhoods/domains.py,sha256=9cWcGYGALcYruvAjWKztnEZlnYyT6DSoAPP6VsgA2wQ,14601
|
14
|
+
risk/neighborhoods/neighborhoods.py,sha256=g08IFsMjsadkkfl9FI9oIrzlQ5KgWK_BMRN0VmCzDbs,21694
|
15
|
+
risk/neighborhoods/stats/__init__.py,sha256=1evLEAa7trCWj2DapCV4vW_f0zsyKHqTsn4--E_fDPg,306
|
16
|
+
risk/neighborhoods/stats/tests.py,sha256=Z14H8G2qHzM5AOhKqp4YfCOL_wp3QwzUzfeGQJOqTTU,12060
|
17
|
+
risk/neighborhoods/stats/permutation/__init__.py,sha256=V-uLSoi4SIPKjSRl7rhcDR4HJ4tquAn3QxNTXH9KzK8,169
|
18
|
+
risk/neighborhoods/stats/permutation/permutation.py,sha256=k9-Yw-1wWBqeD9cGcTh_d2ypqN-HzoJrcCEAR5-BK0o,10673
|
19
|
+
risk/neighborhoods/stats/permutation/test_functions.py,sha256=Wuop4WDY3foGeKp-hMGc2Gdqf85s-x9BDXGB8ifp0oE,3147
|
20
|
+
risk/network/__init__.py,sha256=C9GisdyJM7fizwUIipy1vC4lbry9ceqLn1iBcW0-mZg,34
|
21
|
+
risk/network/io.py,sha256=1JViOU1YY0DgRP-dK-n-Tkj2ZrpVJJm4vq-5mzvK7kM,27856
|
22
|
+
risk/network/graph/__init__.py,sha256=iG6IlE8xtAyF6_NuCBUpsJrjrjd1vsXO1Ajsr0K8EA0,46
|
23
|
+
risk/network/graph/api.py,sha256=gXheVl1xw7vFDHOOQdRxCFwVy4ajEJ1dk4GQxm13sCI,8528
|
24
|
+
risk/network/graph/graph.py,sha256=jPNJea9JiLKedGFvSKmZ5Kf2R9go0HJkZ4wYNJwZRJI,12410
|
25
|
+
risk/network/graph/stats.py,sha256=xITodchaF8Pgngynil_GM9IeMBtxuraPX-Gjp6WBEMY,7338
|
26
|
+
risk/network/graph/summary.py,sha256=UZvZk5w2BZ5zs8IDfr-YYYBr2eM1wmXh0FKp_p9UdB4,10260
|
27
|
+
risk/network/plotter/__init__.py,sha256=9kdeONe8NaJvJ5FO7eOdZiobqL4CeR7q2MghG4z6Kt8,50
|
28
|
+
risk/network/plotter/api.py,sha256=GFP0n_0-tSdCZQd6cuaH3w55953sXCR02nwyJhAEqK4,1789
|
29
|
+
risk/network/plotter/canvas.py,sha256=i8i-ARLCNsHkAW3W_E63PpbEjmQLJplpqK6mSivgz4k,13610
|
30
|
+
risk/network/plotter/contour.py,sha256=TJA4C9G07iFBBSuSCYGZJ6NZfXGWH8zVw0LC2eqU3JA,15540
|
31
|
+
risk/network/plotter/labels.py,sha256=89Ege5NkfFMZJXw_mF-rhtIPa4-S8UKtYSkj8ty_VeQ,46805
|
32
|
+
risk/network/plotter/network.py,sha256=c9rPQ5mjil0sxVQnprRaKMAUqT6PZmKiATWz0m-TvQU,14185
|
33
|
+
risk/network/plotter/plotter.py,sha256=WZcOrBW3vBQ_aLwv8c8pXJO8ZlyswHHHfEsiLxzEYaI,6121
|
34
|
+
risk/network/plotter/utils/colors.py,sha256=xZt4877ORTQqySiMh-tUGe0sXvhLbXO04iGNeBDkbbw,19144
|
35
|
+
risk/network/plotter/utils/layout.py,sha256=Lty16T-Q-oWwo9fXqm-nnS_dMS3BMhuFt4SFqxFC3Ng,3610
|
36
|
+
risk_network-0.0.12b3.dist-info/licenses/LICENSE,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
|
37
|
+
risk_network-0.0.12b3.dist-info/METADATA,sha256=asgx3FEU8sjkJzPlyHJ676q47C1j_O9doIVq_oFwVn4,6667
|
38
|
+
risk_network-0.0.12b3.dist-info/WHEEL,sha256=ooBFpIzZCPdw3uqIQsOo4qqbA4ZRPxHnOH7peeONza0,91
|
39
|
+
risk_network-0.0.12b3.dist-info/top_level.txt,sha256=NX7C2PFKTvC1JhVKv14DFlFAIFnKc6Lpsu1ZfxvQwVw,5
|
40
|
+
risk_network-0.0.12b3.dist-info/RECORD,,
|