risk-network 0.0.8b4__py3-none-any.whl → 0.0.8b5__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 CHANGED
@@ -7,4 +7,4 @@ RISK: RISK Infers Spatial Kinships
7
7
 
8
8
  from risk.risk import RISK
9
9
 
10
- __version__ = "0.0.8-beta.4"
10
+ __version__ = "0.0.8-beta.5"
risk/network/plot.py CHANGED
@@ -33,6 +33,7 @@ class NetworkPlotter:
33
33
  graph: NetworkGraph,
34
34
  figsize: Tuple = (10, 10),
35
35
  background_color: Union[str, List, Tuple, np.ndarray] = "white",
36
+ background_alpha: float = 1.0,
36
37
  ) -> None:
37
38
  """Initialize the NetworkPlotter with a NetworkGraph object and plotting parameters.
38
39
 
@@ -40,16 +41,23 @@ class NetworkPlotter:
40
41
  graph (NetworkGraph): The network data and attributes to be visualized.
41
42
  figsize (tuple, optional): Size of the figure in inches (width, height). Defaults to (10, 10).
42
43
  background_color (str, list, tuple, np.ndarray, optional): Background color of the plot. Defaults to "white".
44
+ background_alpha (float, optional): Transparency level of the background color. Defaults to 1.0.
43
45
  """
44
46
  self.graph = graph
45
47
  # Initialize the plot with the specified parameters
46
- self.ax = self._initialize_plot(graph, figsize, background_color)
48
+ self.ax = self._initialize_plot(
49
+ graph=graph,
50
+ figsize=figsize,
51
+ background_color=background_color,
52
+ background_alpha=background_alpha,
53
+ )
47
54
 
48
55
  def _initialize_plot(
49
56
  self,
50
57
  graph: NetworkGraph,
51
58
  figsize: Tuple,
52
59
  background_color: Union[str, List, Tuple, np.ndarray],
60
+ background_alpha: float = 1.0,
53
61
  ) -> plt.Axes:
54
62
  """Set up the plot with figure size and background color.
55
63
 
@@ -57,6 +65,7 @@ class NetworkPlotter:
57
65
  graph (NetworkGraph): The network data and attributes to be visualized.
58
66
  figsize (tuple): Size of the figure in inches (width, height).
59
67
  background_color (str): Background color of the plot.
68
+ background_alpha (float, optional): Transparency level of the background color. Defaults to 1.0.
60
69
 
61
70
  Returns:
62
71
  plt.Axes: The axis object for the plot.
@@ -76,7 +85,7 @@ class NetworkPlotter:
76
85
 
77
86
  # Set the background color of the plot
78
87
  # Convert color to RGBA using the _to_rgba helper function
79
- fig.patch.set_facecolor(_to_rgba(background_color, 1.0))
88
+ fig.patch.set_facecolor(_to_rgba(color=background_color, alpha=background_alpha))
80
89
  ax.invert_yaxis() # Invert the y-axis to match typical image coordinates
81
90
  # Remove axis spines for a cleaner look
82
91
  for spine in ax.spines.values():
@@ -199,7 +208,7 @@ class NetworkPlotter:
199
208
  )
200
209
 
201
210
  # Convert color to RGBA using the _to_rgba helper function - use outline_alpha for the perimeter
202
- color = _to_rgba(color, outline_alpha)
211
+ color = _to_rgba(color=color, alpha=outline_alpha)
203
212
  # Extract node coordinates from the network graph
204
213
  node_coordinates = self.graph.node_coordinates
205
214
  # Calculate the center and radius of the bounding box around the network
@@ -218,7 +227,7 @@ class NetworkPlotter:
218
227
  )
219
228
  # Set the transparency of the fill if applicable
220
229
  if fill_alpha > 0:
221
- circle.set_facecolor(_to_rgba(color, fill_alpha))
230
+ circle.set_facecolor(_to_rgba(color=color, alpha=fill_alpha))
222
231
 
223
232
  self.ax.add_artist(circle)
224
233
 
@@ -263,7 +272,7 @@ class NetworkPlotter:
263
272
  )
264
273
 
265
274
  # Convert color to RGBA using the _to_rgba helper function - use outline_alpha for the perimeter
266
- color = _to_rgba(color, outline_alpha)
275
+ color = _to_rgba(color=color, alpha=outline_alpha)
267
276
  # Extract node coordinates from the network graph
268
277
  node_coordinates = self.graph.node_coordinates
269
278
  # Scale the node coordinates if needed
@@ -326,9 +335,15 @@ class NetworkPlotter:
326
335
 
327
336
  # Convert colors to RGBA using the _to_rgba helper function
328
337
  # If node_colors was generated using get_annotated_node_colors, its alpha values will override node_alpha
329
- node_color = _to_rgba(node_color, node_alpha, num_repeats=len(self.graph.network.nodes))
330
- node_edgecolor = _to_rgba(node_edgecolor, 1.0, num_repeats=len(self.graph.network.nodes))
331
- edge_color = _to_rgba(edge_color, edge_alpha, num_repeats=len(self.graph.network.edges))
338
+ node_color = _to_rgba(
339
+ color=node_color, alpha=node_alpha, num_repeats=len(self.graph.network.nodes)
340
+ )
341
+ node_edgecolor = _to_rgba(
342
+ color=node_edgecolor, alpha=1.0, num_repeats=len(self.graph.network.nodes)
343
+ )
344
+ edge_color = _to_rgba(
345
+ color=edge_color, alpha=edge_alpha, num_repeats=len(self.graph.network.edges)
346
+ )
332
347
 
333
348
  # Extract node coordinates from the network graph
334
349
  node_coordinates = self.graph.node_coordinates
@@ -405,9 +420,11 @@ class NetworkPlotter:
405
420
  ]
406
421
 
407
422
  # Convert colors to RGBA using the _to_rgba helper function
408
- node_color = _to_rgba(node_color, node_alpha, num_repeats=len(node_ids))
409
- node_edgecolor = _to_rgba(node_edgecolor, 1.0, num_repeats=len(node_ids))
410
- edge_color = _to_rgba(edge_color, edge_alpha, num_repeats=len(self.graph.network.edges))
423
+ node_color = _to_rgba(color=node_color, alpha=node_alpha, num_repeats=len(node_ids))
424
+ node_edgecolor = _to_rgba(color=node_edgecolor, alpha=1.0, num_repeats=len(node_ids))
425
+ edge_color = _to_rgba(
426
+ color=edge_color, alpha=edge_alpha, num_repeats=len(self.graph.network.edges)
427
+ )
411
428
 
412
429
  # Get the coordinates of the filtered nodes
413
430
  node_coordinates = {node_id: self.graph.node_coordinates[node_id] for node_id in node_ids}
@@ -470,7 +487,9 @@ class NetworkPlotter:
470
487
  )
471
488
 
472
489
  # Ensure color is converted to RGBA with repetition matching the number of domains
473
- color = _to_rgba(color, alpha, num_repeats=len(self.graph.domain_id_to_node_ids_map))
490
+ color = _to_rgba(
491
+ color=color, alpha=alpha, num_repeats=len(self.graph.domain_id_to_node_ids_map)
492
+ )
474
493
  # Extract node coordinates from the network graph
475
494
  node_coordinates = self.graph.node_coordinates
476
495
  # Draw contours for each domain in the network
@@ -527,7 +546,7 @@ class NetworkPlotter:
527
546
  node_groups = [nodes]
528
547
 
529
548
  # Convert color to RGBA using the _to_rgba helper function
530
- color_rgba = _to_rgba(color, alpha)
549
+ color_rgba = _to_rgba(color=color, alpha=alpha)
531
550
 
532
551
  # Iterate over each group of nodes (either sublists or flat list)
533
552
  for sublist in node_groups:
@@ -816,12 +835,14 @@ class NetworkPlotter:
816
835
  best_label_positions = _calculate_best_label_positions(
817
836
  filtered_domain_centroids, center, radius, offset
818
837
  )
819
- # Convert colors to RGBA using the _to_rgba helper function
838
+ # Convert all domain colors to RGBA using the _to_rgba helper function
820
839
  fontcolor = _to_rgba(
821
- fontcolor, fontalpha, num_repeats=len(self.graph.domain_id_to_node_ids_map)
840
+ color=fontcolor, alpha=fontalpha, num_repeats=len(self.graph.domain_id_to_node_ids_map)
822
841
  )
823
842
  arrow_color = _to_rgba(
824
- arrow_color, arrow_alpha, num_repeats=len(self.graph.domain_id_to_node_ids_map)
843
+ color=arrow_color,
844
+ alpha=arrow_alpha,
845
+ num_repeats=len(self.graph.domain_id_to_node_ids_map),
825
846
  )
826
847
 
827
848
  # Annotate the network with labels
@@ -847,8 +868,11 @@ class NetworkPlotter:
847
868
  shrinkB=arrow_tip_shrink,
848
869
  ),
849
870
  )
850
- # Overlay domain ID at the centroid if requested
851
- if overlay_ids:
871
+
872
+ # Overlay domain ID at the centroid regardless of max_labels if requested
873
+ if overlay_ids:
874
+ for idx, domain in enumerate(self.graph.domain_id_to_node_ids_map):
875
+ centroid = domain_id_to_centroid_map[domain]
852
876
  self.ax.text(
853
877
  centroid[0],
854
878
  centroid[1],
@@ -907,8 +931,8 @@ class NetworkPlotter:
907
931
  node_groups = [nodes]
908
932
 
909
933
  # Convert fontcolor and arrow_color to RGBA
910
- fontcolor_rgba = _to_rgba(fontcolor, fontalpha)
911
- arrow_color_rgba = _to_rgba(arrow_color, arrow_alpha)
934
+ fontcolor_rgba = _to_rgba(color=fontcolor, alpha=fontalpha)
935
+ arrow_color_rgba = _to_rgba(color=arrow_color, alpha=arrow_alpha)
912
936
 
913
937
  # Calculate the bounding box around the network
914
938
  center, radius = _calculate_bounding_box(self.graph.node_coordinates, radius_margin=scale)
@@ -1284,11 +1308,11 @@ class NetworkPlotter:
1284
1308
  # Apply the alpha value for enriched nodes
1285
1309
  network_colors[:, 3] = alpha # Apply the alpha value to the enriched nodes' A channel
1286
1310
  # Convert the non-enriched color to RGBA using the _to_rgba helper function
1287
- nonenriched_color = _to_rgba(nonenriched_color, nonenriched_alpha)
1311
+ nonenriched_color = _to_rgba(color=nonenriched_color, alpha=nonenriched_alpha)
1288
1312
  # Adjust node colors: replace any fully black nodes (RGB == 0) with the non-enriched color and its alpha
1289
1313
  adjusted_network_colors = np.where(
1290
1314
  np.all(network_colors[:, :3] == 0, axis=1, keepdims=True), # Check RGB values only
1291
- np.array([nonenriched_color]), # Apply the non-enriched color with alpha
1315
+ np.array(nonenriched_color), # Apply the non-enriched color with alpha
1292
1316
  network_colors, # Keep the original colors for enriched nodes
1293
1317
  )
1294
1318
  return adjusted_network_colors
@@ -1462,62 +1486,65 @@ class NetworkPlotter:
1462
1486
 
1463
1487
  def _to_rgba(
1464
1488
  color: Union[str, List, Tuple, np.ndarray],
1465
- alpha: float = 1.0,
1489
+ alpha: Union[float, None] = None,
1466
1490
  num_repeats: Union[int, None] = None,
1467
1491
  ) -> np.ndarray:
1468
- """Convert a color or array of colors to RGBA format, applying alpha only if the color is RGB.
1492
+ """Convert color(s) to RGBA format, applying alpha and repeating as needed.
1469
1493
 
1470
1494
  Args:
1471
1495
  color (Union[str, list, tuple, np.ndarray]): The color(s) to convert. Can be a string, list, tuple, or np.ndarray.
1472
- alpha (float, optional): Alpha value (transparency) to apply if the color is in RGB format. Defaults to 1.0.
1473
- num_repeats (int or None, optional): If provided, the color will be repeated this many times. Defaults to None.
1496
+ alpha (float, None, optional): Alpha value (transparency) to apply. If provided, it overrides any existing alpha values.
1497
+ num_repeats (int, None, optional): If provided, the color(s) will be repeated this many times. Defaults to None.
1474
1498
 
1475
1499
  Returns:
1476
- np.ndarray: The RGBA color or array of RGBA colors.
1500
+ np.ndarray: Array of RGBA colors repeated `num_repeats` times, if applicable.
1477
1501
  """
1478
- # Handle single color case (string, RGB, or RGBA)
1479
- if isinstance(color, str) or (
1480
- isinstance(color, (list, tuple, np.ndarray))
1481
- and len(color) in [3, 4]
1482
- and not any(isinstance(c, (list, tuple, np.ndarray)) for c in color)
1483
- ):
1484
- rgba_color = np.array(mcolors.to_rgba(color))
1485
- # Only set alpha if the input is an RGB color or a string (not RGBA)
1486
- if len(rgba_color) == 4 and (
1487
- len(color) == 3 or isinstance(color, str)
1488
- ): # If it's RGB or a string, set the alpha
1489
- rgba_color[3] = alpha
1490
-
1491
- # Repeat the color if num_repeats argument is provided
1492
- if num_repeats is not None:
1493
- return np.array([rgba_color] * num_repeats)
1494
1502
 
1495
- return rgba_color
1503
+ def convert_to_rgba(c: Union[str, List, Tuple, np.ndarray]) -> np.ndarray:
1504
+ """Convert a single color to RGBA format, handling strings, hex, and RGB/RGBA lists."""
1505
+ if isinstance(c, str):
1506
+ # Convert color names or hex values (e.g., 'red', '#FF5733') to RGBA
1507
+ rgba = np.array(mcolors.to_rgba(c))
1508
+ elif isinstance(c, (list, tuple, np.ndarray)) and len(c) in [3, 4]:
1509
+ # Convert RGB (3) or RGBA (4) values to RGBA format
1510
+ rgba = np.array(mcolors.to_rgba(c))
1511
+ else:
1512
+ raise ValueError(
1513
+ f"Invalid color format: {c}. Must be a valid string or RGB/RGBA sequence."
1514
+ )
1496
1515
 
1497
- # Handle array of colors case (including strings, RGB, and RGBA)
1498
- elif isinstance(color, (list, tuple, np.ndarray)):
1499
- rgba_colors = []
1500
- for i in range(num_repeats):
1501
- # Reiterate over the colors if the number of repeats exceeds the number of colors
1502
- c = color[i % len(color)]
1503
- # Ensure each element is either a valid string or a list/tuple of length 3 (RGB) or 4 (RGBA)
1504
- if isinstance(c, str) or (
1505
- isinstance(c, (list, tuple, np.ndarray)) and len(c) in [3, 4]
1506
- ):
1507
- rgba_c = np.array(mcolors.to_rgba(c))
1508
- # Apply alpha only to RGB colors (not RGBA) and strings
1509
- if len(rgba_c) == 4 and (len(c) == 3 or isinstance(c, str)):
1510
- rgba_c[3] = alpha
1516
+ if alpha is not None: # Override alpha if provided
1517
+ rgba[3] = alpha
1518
+ return rgba
1511
1519
 
1512
- rgba_colors.append(rgba_c)
1513
- else:
1514
- raise ValueError(f"Invalid color: {c}. Must be a valid RGB/RGBA or string color.")
1520
+ # If color is a 2D array of RGBA values, convert it to a list of lists
1521
+ if isinstance(color, np.ndarray) and color.ndim == 2 and color.shape[1] == 4:
1522
+ color = [list(c) for c in color]
1515
1523
 
1516
- # Repeat the colors if num_repeats argument is provided
1517
- if num_repeats is not None and len(rgba_colors) == 1:
1518
- return np.array([rgba_colors[0]] * num_repeats)
1524
+ # Handle a single color (string or RGB/RGBA list/tuple)
1525
+ if isinstance(color, (str, list, tuple)) and not any(
1526
+ isinstance(c, (list, tuple, np.ndarray)) for c in color
1527
+ ):
1528
+ rgba_color = convert_to_rgba(color)
1529
+ if num_repeats:
1530
+ return np.tile(
1531
+ rgba_color, (num_repeats, 1)
1532
+ ) # Repeat the color if num_repeats is provided
1533
+ return np.array([rgba_color]) # Return a single color wrapped in a numpy array
1534
+
1535
+ # Handle a list/array of colors
1536
+ elif isinstance(color, (list, tuple, np.ndarray)):
1537
+ rgba_colors = np.array(
1538
+ [convert_to_rgba(c) for c in color]
1539
+ ) # Convert each color in the list to RGBA
1540
+ # Handle repetition if num_repeats is provided
1541
+ if num_repeats:
1542
+ repeated_colors = np.array(
1543
+ [rgba_colors[i % len(rgba_colors)] for i in range(num_repeats)]
1544
+ )
1545
+ return repeated_colors
1519
1546
 
1520
- return np.array(rgba_colors)
1547
+ return rgba_colors
1521
1548
 
1522
1549
  else:
1523
1550
  raise ValueError("Color must be a valid RGB/RGBA or array of RGB/RGBA colors.")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: risk-network
3
- Version: 0.0.8b4
3
+ Version: 0.0.8b5
4
4
  Summary: A Python package for biological network analysis
5
5
  Author: Ira Horecka
6
6
  Author-email: Ira Horecka <ira89@icloud.com>
@@ -1,4 +1,4 @@
1
- risk/__init__.py,sha256=NEb1vT_sWtAGA_9YQ9jcZiU201G2LaBvb6-ayyoBAro,112
1
+ risk/__init__.py,sha256=2GschYTLwmGIvkQltbf0EfXkWUv5e0Y760GJNGFzDWQ,112
2
2
  risk/constants.py,sha256=XInRaH78Slnw_sWgAsBFbUHkyA0h0jL0DKGuQNbOvjM,550
3
3
  risk/risk.py,sha256=FaQhDCBZxZSAXJsScH0rSbjjCTNZA5vgf9rJj1GHW44,20924
4
4
  risk/annotations/__init__.py,sha256=vUpVvMRE5if01Ic8QY6M2Ae3EFGJHdugEe9PdEkAW4Y,138
@@ -15,7 +15,7 @@ risk/network/__init__.py,sha256=iEPeJdZfqp0toxtbElryB8jbz9_t_k4QQ3iDvKE8C_0,126
15
15
  risk/network/geometry.py,sha256=H1yGVVqgbfpzBzJwEheDLfvGLSA284jGQQTn612L4Vc,6759
16
16
  risk/network/graph.py,sha256=EwD4-1THC5YNdP6PY01Oe35k2QYYqtZpxWraPVH6wa4,16426
17
17
  risk/network/io.py,sha256=kY7HqmL3wa1NnqHu61_G8IpT21qpBijpAZ4ixmsseJA,22911
18
- risk/network/plot.py,sha256=_Z2OSNJM6vBhpwfKUwU9IGRvIbr9uZVZzaEXwfyVa9M,81981
18
+ risk/network/plot.py,sha256=dLugXFhQ5QZ2JjuLLAbEuB6FxcWqDnItT-6Cilotw5I,82982
19
19
  risk/stats/__init__.py,sha256=WcgoETQ-hS0LQqKRsAMIPtP15xZ-4eul6VUBuUx4Wzc,220
20
20
  risk/stats/hypergeom.py,sha256=o6Qnj31gCAKxr2uQirXrbv7XvdDJGEq69MFW-ubx_hA,2272
21
21
  risk/stats/poisson.py,sha256=8x9hB4DCukq4gNIlIKO-c_jYG1-BTwTX53oLauFyfj8,1793
@@ -23,8 +23,8 @@ risk/stats/stats.py,sha256=kvShov-94W6ffgDUTb522vB9hDJQSyTsYif_UIaFfSM,7059
23
23
  risk/stats/permutation/__init__.py,sha256=neJp7FENC-zg_CGOXqv-iIvz1r5XUKI9Ruxhmq7kDOI,105
24
24
  risk/stats/permutation/permutation.py,sha256=D84Rcpt6iTQniK0PfQGcw9bLcHbMt9p-ARcurUnIXZQ,10095
25
25
  risk/stats/permutation/test_functions.py,sha256=lftOude6hee0pyR80HlBD32522JkDoN5hrKQ9VEbuoY,2345
26
- risk_network-0.0.8b4.dist-info/LICENSE,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
27
- risk_network-0.0.8b4.dist-info/METADATA,sha256=wHiueFpwYZzcIRfiMlHztT4GHhZDxFTGKUuTPfXz7Jo,47450
28
- risk_network-0.0.8b4.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
29
- risk_network-0.0.8b4.dist-info/top_level.txt,sha256=NX7C2PFKTvC1JhVKv14DFlFAIFnKc6Lpsu1ZfxvQwVw,5
30
- risk_network-0.0.8b4.dist-info/RECORD,,
26
+ risk_network-0.0.8b5.dist-info/LICENSE,sha256=jOtLnuWt7d5Hsx6XXB2QxzrSe2sWWh3NgMfFRetluQM,35147
27
+ risk_network-0.0.8b5.dist-info/METADATA,sha256=Lf97YQxcZGwjylO_c21AJQz8ZmdwKWkI5EbPtnl49u8,47450
28
+ risk_network-0.0.8b5.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
29
+ risk_network-0.0.8b5.dist-info/top_level.txt,sha256=NX7C2PFKTvC1JhVKv14DFlFAIFnKc6Lpsu1ZfxvQwVw,5
30
+ risk_network-0.0.8b5.dist-info/RECORD,,