risk-network 0.0.6b8__tar.gz → 0.0.6b10__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/PKG-INFO +1 -1
  2. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/__init__.py +1 -1
  3. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/log/params.py +2 -0
  4. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/network/graph.py +38 -19
  5. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/network/plot.py +31 -26
  6. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/risk.py +4 -4
  7. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk_network.egg-info/PKG-INFO +1 -1
  8. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/LICENSE +0 -0
  9. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/MANIFEST.in +0 -0
  10. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/README.md +0 -0
  11. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/pyproject.toml +0 -0
  12. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/annotations/__init__.py +0 -0
  13. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/annotations/annotations.py +0 -0
  14. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/annotations/io.py +0 -0
  15. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/constants.py +0 -0
  16. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/log/__init__.py +0 -0
  17. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/log/console.py +0 -0
  18. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/neighborhoods/__init__.py +0 -0
  19. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/neighborhoods/community.py +0 -0
  20. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/neighborhoods/domains.py +0 -0
  21. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/neighborhoods/neighborhoods.py +0 -0
  22. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/network/__init__.py +0 -0
  23. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/network/geometry.py +0 -0
  24. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/network/io.py +0 -0
  25. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/stats/__init__.py +0 -0
  26. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/stats/fisher_exact.py +0 -0
  27. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/stats/hypergeom.py +0 -0
  28. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/stats/permutation/__init__.py +0 -0
  29. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/stats/permutation/permutation.py +0 -0
  30. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/stats/permutation/test_functions.py +0 -0
  31. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk/stats/stats.py +0 -0
  32. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk_network.egg-info/SOURCES.txt +0 -0
  33. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk_network.egg-info/dependency_links.txt +0 -0
  34. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk_network.egg-info/requires.txt +0 -0
  35. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/risk_network.egg-info/top_level.txt +0 -0
  36. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/setup.cfg +0 -0
  37. {risk_network-0.0.6b8 → risk_network-0.0.6b10}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: risk-network
3
- Version: 0.0.6b8
3
+ Version: 0.0.6b10
4
4
  Summary: A Python package for biological network analysis
5
5
  Author: Ira Horecka
6
6
  Author-email: Ira Horecka <ira89@icloud.com>
@@ -7,4 +7,4 @@ RISK: RISK Infers Spatial Kinships
7
7
 
8
8
  from risk.risk import RISK
9
9
 
10
- __version__ = "0.0.6-beta.8"
10
+ __version__ = "0.0.6-beta.10"
@@ -7,6 +7,7 @@ import csv
7
7
  import json
8
8
  import warnings
9
9
  from datetime import datetime
10
+ from functools import wraps
10
11
  from typing import Any, Dict
11
12
 
12
13
  import numpy as np
@@ -27,6 +28,7 @@ def _safe_param_export(func):
27
28
  function: The wrapped function with error handling.
28
29
  """
29
30
 
31
+ @wraps(func)
30
32
  def wrapper(*args, **kwargs):
31
33
  try:
32
34
  result = func(*args, **kwargs)
@@ -28,7 +28,7 @@ class NetworkGraph:
28
28
  top_annotations: pd.DataFrame,
29
29
  domains: pd.DataFrame,
30
30
  trimmed_domains: pd.DataFrame,
31
- node_label_to_id_map: Dict[str, Any],
31
+ node_label_to_node_id_map: Dict[str, Any],
32
32
  node_enrichment_sums: np.ndarray,
33
33
  ):
34
34
  """Initialize the NetworkGraph object.
@@ -38,43 +38,48 @@ class NetworkGraph:
38
38
  top_annotations (pd.DataFrame): DataFrame containing annotations data for the network nodes.
39
39
  domains (pd.DataFrame): DataFrame containing domain data for the network nodes.
40
40
  trimmed_domains (pd.DataFrame): DataFrame containing trimmed domain data for the network nodes.
41
- node_label_to_id_map (dict): A dictionary mapping node labels to their corresponding IDs.
41
+ node_label_to_node_id_map (dict): A dictionary mapping node labels to their corresponding IDs.
42
42
  node_enrichment_sums (np.ndarray): Array containing the enrichment sums for the nodes.
43
43
  """
44
44
  self.top_annotations = top_annotations
45
- self.domain_to_nodes_map = self._create_domain_to_nodes_map(domains)
45
+ self.domain_id_to_node_ids_map = self._create_domain_id_to_node_ids_map(domains)
46
46
  self.domains = domains
47
- self.trimmed_domain_to_term = self._create_domain_to_term_map(trimmed_domains)
48
- self.trimmed_domains = trimmed_domains
47
+ self.domain_id_to_domain_terms_map = self._create_domain_id_to_domain_terms_map(
48
+ trimmed_domains
49
+ )
49
50
  self.node_enrichment_sums = node_enrichment_sums
50
- self.node_id_to_label_map = {v: k for k, v in node_label_to_id_map.items()}
51
+ self.node_id_to_node_label_map = {v: k for k, v in node_label_to_node_id_map.items()}
51
52
  self.node_label_to_enrichment_map = dict(
52
- zip(node_label_to_id_map.keys(), node_enrichment_sums)
53
+ zip(node_label_to_node_id_map.keys(), node_enrichment_sums)
53
54
  )
54
- self.node_label_to_id_map = node_label_to_id_map
55
- # NOTE: self.network and self.node_coordinates are declared in _initialize_network
55
+ self.node_label_to_node_id_map = node_label_to_node_id_map
56
+ # NOTE: Below this point, instance attributes (i.e., self) will be used!
57
+ self.domain_id_to_node_labels_map = self._create_domain_id_to_node_labels_map()
58
+ # self.network and self.node_coordinates are properly declared in _initialize_network
56
59
  self.network = None
57
60
  self.node_coordinates = None
58
61
  self._initialize_network(network)
59
62
 
60
- def _create_domain_to_nodes_map(self, domains: pd.DataFrame) -> Dict[str, Any]:
61
- """Create a mapping from domains to the list of nodes belonging to each domain.
63
+ def _create_domain_id_to_node_ids_map(self, domains: pd.DataFrame) -> Dict[str, Any]:
64
+ """Create a mapping from domains to the list of node IDs belonging to each domain.
62
65
 
63
66
  Args:
64
67
  domains (pd.DataFrame): DataFrame containing domain information, including the 'primary domain' for each node.
65
68
 
66
69
  Returns:
67
- dict: A dictionary where keys are domain IDs and values are lists of nodes belonging to each domain.
70
+ dict: A dictionary where keys are domain IDs and values are lists of node IDs belonging to each domain.
68
71
  """
69
72
  cleaned_domains_matrix = domains.reset_index()[["index", "primary domain"]]
70
73
  node_to_domains_map = cleaned_domains_matrix.set_index("index")["primary domain"].to_dict()
71
- domain_to_nodes_map = defaultdict(list)
74
+ domain_id_to_node_ids_map = defaultdict(list)
72
75
  for k, v in node_to_domains_map.items():
73
- domain_to_nodes_map[v].append(k)
76
+ domain_id_to_node_ids_map[v].append(k)
74
77
 
75
- return domain_to_nodes_map
78
+ return domain_id_to_node_ids_map
76
79
 
77
- def _create_domain_to_term_map(self, trimmed_domains: pd.DataFrame) -> Dict[str, Any]:
80
+ def _create_domain_id_to_domain_terms_map(
81
+ self, trimmed_domains: pd.DataFrame
82
+ ) -> Dict[str, Any]:
78
83
  """Create a mapping from domain IDs to their corresponding terms.
79
84
 
80
85
  Args:
@@ -90,6 +95,20 @@ class NetworkGraph:
90
95
  )
91
96
  )
92
97
 
98
+ def _create_domain_id_to_node_labels_map(self) -> Dict[int, List[str]]:
99
+ """Create a map from domain IDs to node labels.
100
+
101
+ Returns:
102
+ dict: A dictionary mapping domain IDs to the corresponding node labels.
103
+ """
104
+ domain_id_to_label_map = {}
105
+ for domain_id, node_ids in self.domain_id_to_node_ids_map.items():
106
+ domain_id_to_label_map[domain_id] = [
107
+ self.node_id_to_node_label_map[node_id] for node_id in node_ids
108
+ ]
109
+
110
+ return domain_id_to_label_map
111
+
93
112
  def _initialize_network(self, G: nx.Graph) -> None:
94
113
  """Initialize the network by unfolding it and extracting node coordinates.
95
114
 
@@ -158,8 +177,8 @@ class NetworkGraph:
158
177
  # Initialize composite colors array with shape (number of nodes, 4) for RGBA
159
178
  composite_colors = np.zeros((num_nodes, 4))
160
179
  # Assign colors to nodes based on domain_colors
161
- for domain_idx, nodes in self.domain_to_nodes_map.items():
162
- color = domain_colors[domain_idx]
180
+ for domain_id, nodes in self.domain_id_to_node_ids_map.items():
181
+ color = domain_colors[domain_id]
163
182
  for node in nodes:
164
183
  composite_colors[node] = color
165
184
 
@@ -189,7 +208,7 @@ class NetworkGraph:
189
208
  domain_colors = _get_colors(
190
209
  num_colors_to_generate=len(domains), cmap=cmap, color=color, random_seed=random_seed
191
210
  )
192
- return dict(zip(self.domain_to_nodes_map.keys(), domain_colors))
211
+ return dict(zip(self.domain_id_to_node_ids_map.keys(), domain_colors))
193
212
 
194
213
 
195
214
  def _transform_colors(
@@ -308,9 +308,9 @@ class NetworkPlotter:
308
308
 
309
309
  # Filter to get node IDs and their coordinates
310
310
  node_ids = [
311
- self.graph.node_label_to_id_map.get(node)
311
+ self.graph.node_label_to_node_id_map.get(node)
312
312
  for node in nodes
313
- if node in self.graph.node_label_to_id_map
313
+ if node in self.graph.node_label_to_node_id_map
314
314
  ]
315
315
  if not node_ids:
316
316
  raise ValueError("No nodes found in the network graph.")
@@ -320,7 +320,7 @@ class NetworkPlotter:
320
320
  node_color = [
321
321
  node_color[nodes.index(node)]
322
322
  for node in nodes
323
- if node in self.graph.node_label_to_id_map
323
+ if node in self.graph.node_label_to_node_id_map
324
324
  ]
325
325
 
326
326
  # Convert colors to RGBA using the _to_rgba helper function
@@ -389,16 +389,16 @@ class NetworkPlotter:
389
389
  )
390
390
 
391
391
  # Ensure color is converted to RGBA with repetition matching the number of domains
392
- color = _to_rgba(color, alpha, num_repeats=len(self.graph.domain_to_nodes_map))
392
+ color = _to_rgba(color, alpha, num_repeats=len(self.graph.domain_id_to_node_ids_map))
393
393
  # Extract node coordinates from the network graph
394
394
  node_coordinates = self.graph.node_coordinates
395
395
  # Draw contours for each domain in the network
396
- for idx, (_, nodes) in enumerate(self.graph.domain_to_nodes_map.items()):
397
- if len(nodes) > 1:
396
+ for idx, (_, node_ids) in enumerate(self.graph.domain_id_to_node_ids_map.items()):
397
+ if len(node_ids) > 1:
398
398
  self._draw_kde_contour(
399
399
  self.ax,
400
400
  node_coordinates,
401
- nodes,
401
+ node_ids,
402
402
  color=color[idx],
403
403
  levels=levels,
404
404
  bandwidth=bandwidth,
@@ -452,9 +452,9 @@ class NetworkPlotter:
452
452
  for sublist in node_groups:
453
453
  # Filter to get node IDs and their coordinates for each sublist
454
454
  node_ids = [
455
- self.graph.node_label_to_id_map.get(node)
455
+ self.graph.node_label_to_node_id_map.get(node)
456
456
  for node in sublist
457
- if node in self.graph.node_label_to_id_map
457
+ if node in self.graph.node_label_to_node_id_map
458
458
  ]
459
459
  if not node_ids or len(node_ids) == 1:
460
460
  raise ValueError(
@@ -641,12 +641,14 @@ class NetworkPlotter:
641
641
 
642
642
  # Set max_labels to the total number of domains if not provided (None)
643
643
  if max_labels is None:
644
- max_labels = len(self.graph.domain_to_nodes_map)
644
+ max_labels = len(self.graph.domain_id_to_node_ids_map)
645
645
 
646
646
  # Convert colors to RGBA using the _to_rgba helper function
647
- fontcolor = _to_rgba(fontcolor, fontalpha, num_repeats=len(self.graph.domain_to_nodes_map))
647
+ fontcolor = _to_rgba(
648
+ fontcolor, fontalpha, num_repeats=len(self.graph.domain_id_to_node_ids_map)
649
+ )
648
650
  arrow_color = _to_rgba(
649
- arrow_color, arrow_alpha, num_repeats=len(self.graph.domain_to_nodes_map)
651
+ arrow_color, arrow_alpha, num_repeats=len(self.graph.domain_id_to_node_ids_map)
650
652
  )
651
653
 
652
654
  # Normalize words_to_omit to lowercase
@@ -655,9 +657,9 @@ class NetworkPlotter:
655
657
 
656
658
  # Calculate the center and radius of the network
657
659
  domain_centroids = {}
658
- for domain, nodes in self.graph.domain_to_nodes_map.items():
659
- if nodes: # Skip if the domain has no nodes
660
- domain_centroids[domain] = self._calculate_domain_centroid(nodes)
660
+ for domain_id, node_ids in self.graph.domain_id_to_node_ids_map.items():
661
+ if node_ids: # Skip if the domain has no nodes
662
+ domain_centroids[domain_id] = self._calculate_domain_centroid(node_ids)
661
663
 
662
664
  # Initialize dictionaries and lists for valid indices
663
665
  valid_indices = []
@@ -675,12 +677,15 @@ class NetworkPlotter:
675
677
 
676
678
  # Process the specified IDs first
677
679
  for domain in ids_to_keep:
678
- if domain in self.graph.trimmed_domain_to_term and domain in domain_centroids:
680
+ if (
681
+ domain in self.graph.domain_id_to_domain_terms_map
682
+ and domain in domain_centroids
683
+ ):
679
684
  # Handle ids_to_replace logic here for ids_to_keep
680
685
  if ids_to_replace and domain in ids_to_replace:
681
686
  terms = ids_to_replace[domain].split(" ")
682
687
  else:
683
- terms = self.graph.trimmed_domain_to_term[domain].split(" ")
688
+ terms = self.graph.domain_id_to_domain_terms_map[domain].split(" ")
684
689
 
685
690
  # Apply words_to_omit, word length constraints, and max_words
686
691
  if words_to_omit:
@@ -712,7 +717,7 @@ class NetworkPlotter:
712
717
  if ids_to_replace and domain in ids_to_replace:
713
718
  terms = ids_to_replace[domain].split(" ")
714
719
  else:
715
- terms = self.graph.trimmed_domain_to_term[domain].split(" ")
720
+ terms = self.graph.domain_id_to_domain_terms_map[domain].split(" ")
716
721
 
717
722
  # Apply words_to_omit, word length constraints, and max_words
718
723
  if words_to_omit:
@@ -835,9 +840,9 @@ class NetworkPlotter:
835
840
  for sublist in node_groups:
836
841
  # Map node labels to IDs
837
842
  node_ids = [
838
- self.graph.node_label_to_id_map.get(node)
843
+ self.graph.node_label_to_node_id_map.get(node)
839
844
  for node in sublist
840
- if node in self.graph.node_label_to_id_map
845
+ if node in self.graph.node_label_to_node_id_map
841
846
  ]
842
847
  if not node_ids or len(node_ids) == 1:
843
848
  raise ValueError(
@@ -948,10 +953,10 @@ class NetworkPlotter:
948
953
  Returns:
949
954
  np.ndarray: Array of node sizes, with enriched nodes larger than non-enriched ones.
950
955
  """
951
- # Merge all enriched nodes from the domain_to_nodes_map dictionary
956
+ # Merge all enriched nodes from the domain_id_to_node_ids_map dictionary
952
957
  enriched_nodes = set()
953
- for _, nodes in self.graph.domain_to_nodes_map.items():
954
- enriched_nodes.update(nodes)
958
+ for _, node_ids in self.graph.domain_id_to_node_ids_map.items():
959
+ enriched_nodes.update(node_ids)
955
960
 
956
961
  # Initialize all node sizes to the non-enriched size
957
962
  node_sizes = np.full(len(self.graph.network.nodes), nonenriched_size)
@@ -1065,10 +1070,10 @@ class NetworkPlotter:
1065
1070
  random_seed=random_seed,
1066
1071
  )
1067
1072
  annotated_colors = []
1068
- for _, nodes in self.graph.domain_to_nodes_map.items():
1069
- if len(nodes) > 1:
1073
+ for _, node_ids in self.graph.domain_id_to_node_ids_map.items():
1074
+ if len(node_ids) > 1:
1070
1075
  # For multi-node domains, choose the brightest color based on RGB sum
1071
- domain_colors = np.array([node_colors[node] for node in nodes])
1076
+ domain_colors = np.array([node_colors[node] for node in node_ids])
1072
1077
  brightest_color = domain_colors[
1073
1078
  np.argmax(domain_colors[:, :3].sum(axis=1)) # Sum the RGB values
1074
1079
  ]
@@ -62,7 +62,7 @@ class RISK(NetworkIO, AnnotationsIO):
62
62
 
63
63
  Args:
64
64
  network (nx.Graph): The network graph.
65
- annotations (pd.DataFrame): The matrix of annotations associated with the network.
65
+ annotations (dict): The annotations associated with the network.
66
66
  distance_metric (str, optional): Distance metric for neighborhood analysis. Defaults to "dijkstra".
67
67
  louvain_resolution (float, optional): Resolution parameter for Louvain clustering. Defaults to 0.1.
68
68
  edge_length_threshold (float, optional): Edge length threshold for neighborhood analysis. Defaults to 0.5.
@@ -131,7 +131,7 @@ class RISK(NetworkIO, AnnotationsIO):
131
131
 
132
132
  Args:
133
133
  network (nx.Graph): The network graph.
134
- annotations (pd.DataFrame): The matrix of annotations associated with the network.
134
+ annotations (dict): The annotations associated with the network.
135
135
  distance_metric (str, optional): Distance metric for neighborhood analysis. Defaults to "dijkstra".
136
136
  louvain_resolution (float, optional): Resolution parameter for Louvain clustering. Defaults to 0.1.
137
137
  edge_length_threshold (float, optional): Edge length threshold for neighborhood analysis. Defaults to 0.5.
@@ -187,7 +187,7 @@ class RISK(NetworkIO, AnnotationsIO):
187
187
 
188
188
  Args:
189
189
  network (nx.Graph): The network graph.
190
- annotations (pd.DataFrame): The matrix of annotations associated with the network.
190
+ annotations (dict): The annotations associated with the network.
191
191
  distance_metric (str, optional): Distance metric for neighborhood analysis. Defaults to "dijkstra".
192
192
  louvain_resolution (float, optional): Resolution parameter for Louvain clustering. Defaults to 0.1.
193
193
  edge_length_threshold (float, optional): Edge length threshold for neighborhood analysis. Defaults to 0.5.
@@ -343,7 +343,7 @@ class RISK(NetworkIO, AnnotationsIO):
343
343
  top_annotations=top_annotations,
344
344
  domains=domains,
345
345
  trimmed_domains=trimmed_domains,
346
- node_label_to_id_map=node_label_to_id,
346
+ node_label_to_node_id_map=node_label_to_id,
347
347
  node_enrichment_sums=node_enrichment_sums,
348
348
  )
349
349
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: risk-network
3
- Version: 0.0.6b8
3
+ Version: 0.0.6b10
4
4
  Summary: A Python package for biological network analysis
5
5
  Author: Ira Horecka
6
6
  Author-email: Ira Horecka <ira89@icloud.com>
File without changes
File without changes