risk-network 0.0.8b4__tar.gz → 0.0.8b6__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.8b4 → risk_network-0.0.8b6}/PKG-INFO +1 -1
  2. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/__init__.py +1 -1
  3. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/network/plot.py +167 -112
  4. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk_network.egg-info/PKG-INFO +1 -1
  5. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/LICENSE +0 -0
  6. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/MANIFEST.in +0 -0
  7. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/README.md +0 -0
  8. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/pyproject.toml +0 -0
  9. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/annotations/__init__.py +0 -0
  10. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/annotations/annotations.py +0 -0
  11. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/annotations/io.py +0 -0
  12. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/constants.py +0 -0
  13. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/log/__init__.py +0 -0
  14. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/log/config.py +0 -0
  15. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/log/params.py +0 -0
  16. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/neighborhoods/__init__.py +0 -0
  17. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/neighborhoods/community.py +0 -0
  18. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/neighborhoods/domains.py +0 -0
  19. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/neighborhoods/neighborhoods.py +0 -0
  20. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/network/__init__.py +0 -0
  21. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/network/geometry.py +0 -0
  22. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/network/graph.py +0 -0
  23. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/network/io.py +0 -0
  24. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/risk.py +0 -0
  25. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/stats/__init__.py +0 -0
  26. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/stats/hypergeom.py +0 -0
  27. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/stats/permutation/__init__.py +0 -0
  28. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/stats/permutation/permutation.py +0 -0
  29. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/stats/permutation/test_functions.py +0 -0
  30. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/stats/poisson.py +0 -0
  31. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk/stats/stats.py +0 -0
  32. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk_network.egg-info/SOURCES.txt +0 -0
  33. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk_network.egg-info/dependency_links.txt +0 -0
  34. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk_network.egg-info/requires.txt +0 -0
  35. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/risk_network.egg-info/top_level.txt +0 -0
  36. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/setup.cfg +0 -0
  37. {risk_network-0.0.8b4 → risk_network-0.0.8b6}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: risk-network
3
- Version: 0.0.8b4
3
+ Version: 0.0.8b6
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.8-beta.4"
10
+ __version__ = "0.0.8-beta.6"
@@ -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: Union[float, None] = 1.0,
36
37
  ) -> None:
37
38
  """Initialize the NetworkPlotter with a NetworkGraph object and plotting parameters.
38
39
 
@@ -40,16 +41,24 @@ 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, None, optional): Transparency level of the background color. If provided, it overrides
45
+ any existing alpha values found in background_color. Defaults to 1.0.
43
46
  """
44
47
  self.graph = graph
45
48
  # Initialize the plot with the specified parameters
46
- self.ax = self._initialize_plot(graph, figsize, background_color)
49
+ self.ax = self._initialize_plot(
50
+ graph=graph,
51
+ figsize=figsize,
52
+ background_color=background_color,
53
+ background_alpha=background_alpha,
54
+ )
47
55
 
48
56
  def _initialize_plot(
49
57
  self,
50
58
  graph: NetworkGraph,
51
59
  figsize: Tuple,
52
60
  background_color: Union[str, List, Tuple, np.ndarray],
61
+ background_alpha: Union[float, None],
53
62
  ) -> plt.Axes:
54
63
  """Set up the plot with figure size and background color.
55
64
 
@@ -57,6 +66,8 @@ class NetworkPlotter:
57
66
  graph (NetworkGraph): The network data and attributes to be visualized.
58
67
  figsize (tuple): Size of the figure in inches (width, height).
59
68
  background_color (str): Background color of the plot.
69
+ background_alpha (float, None, optional): Transparency level of the background color. If provided, it overrides any
70
+ existing alpha values found in background_color.
60
71
 
61
72
  Returns:
62
73
  plt.Axes: The axis object for the plot.
@@ -76,7 +87,7 @@ class NetworkPlotter:
76
87
 
77
88
  # Set the background color of the plot
78
89
  # Convert color to RGBA using the _to_rgba helper function
79
- fig.patch.set_facecolor(_to_rgba(background_color, 1.0))
90
+ fig.patch.set_facecolor(_to_rgba(color=background_color, alpha=background_alpha))
80
91
  ax.invert_yaxis() # Invert the y-axis to match typical image coordinates
81
92
  # Remove axis spines for a cleaner look
82
93
  for spine in ax.spines.values():
@@ -172,8 +183,8 @@ class NetworkPlotter:
172
183
  linestyle: str = "dashed",
173
184
  linewidth: float = 1.5,
174
185
  color: Union[str, List, Tuple, np.ndarray] = "black",
175
- outline_alpha: float = 1.0,
176
- fill_alpha: float = 0.0,
186
+ outline_alpha: Union[float, None] = 1.0,
187
+ fill_alpha: Union[float, None] = 0.0,
177
188
  ) -> None:
178
189
  """Plot a circle around the network graph to represent the network perimeter.
179
190
 
@@ -182,8 +193,10 @@ class NetworkPlotter:
182
193
  linestyle (str, optional): Line style for the network perimeter circle (e.g., dashed, solid). Defaults to "dashed".
183
194
  linewidth (float, optional): Width of the circle's outline. Defaults to 1.5.
184
195
  color (str, list, tuple, or np.ndarray, optional): Color of the network perimeter circle. Defaults to "black".
185
- outline_alpha (float, optional): Transparency level of the circle outline. Defaults to 1.0.
186
- fill_alpha (float, optional): Transparency level of the circle fill. Defaults to 0.0.
196
+ outline_alpha (float, None, optional): Transparency level of the circle outline. If provided, it overrides any existing alpha
197
+ values found in color. Defaults to 1.0.
198
+ fill_alpha (float, None, optional): Transparency level of the circle fill. If provided, it overrides any existing alpha values
199
+ found in color. Defaults to 0.0.
187
200
  """
188
201
  # Log the circle perimeter plotting parameters
189
202
  params.log_plotter(
@@ -199,7 +212,7 @@ class NetworkPlotter:
199
212
  )
200
213
 
201
214
  # Convert color to RGBA using the _to_rgba helper function - use outline_alpha for the perimeter
202
- color = _to_rgba(color, outline_alpha)
215
+ color = _to_rgba(color=color, alpha=outline_alpha)
203
216
  # Extract node coordinates from the network graph
204
217
  node_coordinates = self.graph.node_coordinates
205
218
  # Calculate the center and radius of the bounding box around the network
@@ -218,7 +231,7 @@ class NetworkPlotter:
218
231
  )
219
232
  # Set the transparency of the fill if applicable
220
233
  if fill_alpha > 0:
221
- circle.set_facecolor(_to_rgba(color, fill_alpha))
234
+ circle.set_facecolor(_to_rgba(color=color, alpha=fill_alpha))
222
235
 
223
236
  self.ax.add_artist(circle)
224
237
 
@@ -231,8 +244,8 @@ class NetworkPlotter:
231
244
  color: Union[str, List, Tuple, np.ndarray] = "black",
232
245
  linestyle: str = "solid",
233
246
  linewidth: float = 1.5,
234
- outline_alpha: float = 1.0,
235
- fill_alpha: float = 0.0,
247
+ outline_alpha: Union[float, None] = 1.0,
248
+ fill_alpha: Union[float, None] = 0.0,
236
249
  ) -> None:
237
250
  """
238
251
  Plot a KDE-based contour around the network graph to represent the network perimeter.
@@ -245,8 +258,10 @@ class NetworkPlotter:
245
258
  color (str, list, tuple, or np.ndarray, optional): Color of the network perimeter contour. Defaults to "black".
246
259
  linestyle (str, optional): Line style for the network perimeter contour (e.g., dashed, solid). Defaults to "solid".
247
260
  linewidth (float, optional): Width of the contour's outline. Defaults to 1.5.
248
- outline_alpha (float, optional): Transparency level of the contour outline. Defaults to 1.0.
249
- fill_alpha (float, optional): Transparency level of the contour fill. Defaults to 0.0.
261
+ outline_alpha (float, None, optional): Transparency level of the contour outline. If provided, it overrides any existing
262
+ alpha values found in color. Defaults to 1.0.
263
+ fill_alpha (float, None, optional): Transparency level of the contour fill. If provided, it overrides any existing alpha
264
+ values found in color. Defaults to 0.0.
250
265
  """
251
266
  # Log the contour perimeter plotting parameters
252
267
  params.log_plotter(
@@ -263,7 +278,7 @@ class NetworkPlotter:
263
278
  )
264
279
 
265
280
  # Convert color to RGBA using the _to_rgba helper function - use outline_alpha for the perimeter
266
- color = _to_rgba(color, outline_alpha)
281
+ color = _to_rgba(color=color, alpha=outline_alpha)
267
282
  # Extract node coordinates from the network graph
268
283
  node_coordinates = self.graph.node_coordinates
269
284
  # Scale the node coordinates if needed
@@ -291,8 +306,8 @@ class NetworkPlotter:
291
306
  node_color: Union[str, List, Tuple, np.ndarray] = "white",
292
307
  node_edgecolor: Union[str, List, Tuple, np.ndarray] = "black",
293
308
  edge_color: Union[str, List, Tuple, np.ndarray] = "black",
294
- node_alpha: float = 1.0,
295
- edge_alpha: float = 1.0,
309
+ node_alpha: Union[float, None] = 1.0,
310
+ edge_alpha: Union[float, None] = 1.0,
296
311
  ) -> None:
297
312
  """Plot the network graph with customizable node colors, sizes, edge widths, and node edge widths.
298
313
 
@@ -301,11 +316,14 @@ class NetworkPlotter:
301
316
  node_shape (str, optional): Shape of the nodes. Defaults to "o".
302
317
  node_edgewidth (float, optional): Width of the node edges. Defaults to 1.0.
303
318
  edge_width (float, optional): Width of the edges. Defaults to 1.0.
304
- node_color (str, list, tuple, or np.ndarray, optional): Color of the nodes. Can be a single color or an array of colors. Defaults to "white".
319
+ node_color (str, list, tuple, or np.ndarray, optional): Color of the nodes. Can be a single color or an array of colors.
320
+ Defaults to "white".
305
321
  node_edgecolor (str, list, tuple, or np.ndarray, optional): Color of the node edges. Defaults to "black".
306
322
  edge_color (str, list, tuple, or np.ndarray, optional): Color of the edges. Defaults to "black".
307
- node_alpha (float, optional): Alpha value (transparency) for the nodes. Defaults to 1.0. Annotated node_color alphas will override this value.
308
- edge_alpha (float, optional): Alpha value (transparency) for the edges. Defaults to 1.0.
323
+ node_alpha (float, None, optional): Alpha value (transparency) for the nodes. If provided, it overrides any existing alpha
324
+ values found in node_color. Defaults to 1.0. Annotated node_color alphas will override this value.
325
+ edge_alpha (float, None, optional): Alpha value (transparency) for the edges. If provided, it overrides any existing alpha
326
+ values found in edge_color. Defaults to 1.0.
309
327
  """
310
328
  # Log the plotting parameters
311
329
  params.log_plotter(
@@ -326,9 +344,15 @@ class NetworkPlotter:
326
344
 
327
345
  # Convert colors to RGBA using the _to_rgba helper function
328
346
  # 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))
347
+ node_color = _to_rgba(
348
+ color=node_color, alpha=node_alpha, num_repeats=len(self.graph.network.nodes)
349
+ )
350
+ node_edgecolor = _to_rgba(
351
+ color=node_edgecolor, alpha=1.0, num_repeats=len(self.graph.network.nodes)
352
+ )
353
+ edge_color = _to_rgba(
354
+ color=edge_color, alpha=edge_alpha, num_repeats=len(self.graph.network.edges)
355
+ )
332
356
 
333
357
  # Extract node coordinates from the network graph
334
358
  node_coordinates = self.graph.node_coordinates
@@ -363,8 +387,8 @@ class NetworkPlotter:
363
387
  node_color: Union[str, List, Tuple, np.ndarray] = "white",
364
388
  node_edgecolor: Union[str, List, Tuple, np.ndarray] = "black",
365
389
  edge_color: Union[str, List, Tuple, np.ndarray] = "black",
366
- node_alpha: float = 1.0,
367
- edge_alpha: float = 1.0,
390
+ node_alpha: Union[float, None] = None,
391
+ edge_alpha: Union[float, None] = None,
368
392
  ) -> None:
369
393
  """Plot a subnetwork of selected nodes with customizable node and edge attributes.
370
394
 
@@ -377,8 +401,10 @@ class NetworkPlotter:
377
401
  node_color (str, list, tuple, or np.ndarray, optional): Color of the nodes. Defaults to "white".
378
402
  node_edgecolor (str, list, tuple, or np.ndarray, optional): Color of the node edges. Defaults to "black".
379
403
  edge_color (str, list, tuple, or np.ndarray, optional): Color of the edges. Defaults to "black".
380
- node_alpha (float, optional): Transparency for the nodes. Defaults to 1.0.
381
- edge_alpha (float, optional): Transparency for the edges. Defaults to 1.0.
404
+ node_alpha (float, None, optional): Transparency for the nodes. If provided, it overrides any existing alpha values
405
+ found in node_color. Defaults to 1.0.
406
+ edge_alpha (float, None, optional): Transparency for the edges. If provided, it overrides any existing alpha values
407
+ found in node_color. Defaults to 1.0.
382
408
 
383
409
  Raises:
384
410
  ValueError: If no valid nodes are found in the network graph.
@@ -405,9 +431,11 @@ class NetworkPlotter:
405
431
  ]
406
432
 
407
433
  # 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))
434
+ node_color = _to_rgba(color=node_color, alpha=node_alpha, num_repeats=len(node_ids))
435
+ node_edgecolor = _to_rgba(color=node_edgecolor, alpha=1.0, num_repeats=len(node_ids))
436
+ edge_color = _to_rgba(
437
+ color=edge_color, alpha=edge_alpha, num_repeats=len(self.graph.network.edges)
438
+ )
411
439
 
412
440
  # Get the coordinates of the filtered nodes
413
441
  node_coordinates = {node_id: self.graph.node_coordinates[node_id] for node_id in node_ids}
@@ -442,8 +470,8 @@ class NetworkPlotter:
442
470
  color: Union[str, List, Tuple, np.ndarray] = "white",
443
471
  linestyle: str = "solid",
444
472
  linewidth: float = 1.5,
445
- alpha: float = 1.0,
446
- fill_alpha: float = 0.2,
473
+ alpha: Union[float, None] = 1.0,
474
+ fill_alpha: Union[float, None] = None,
447
475
  ) -> None:
448
476
  """Draw KDE contours for nodes in various domains of a network graph, highlighting areas of high density.
449
477
 
@@ -451,11 +479,14 @@ class NetworkPlotter:
451
479
  levels (int, optional): Number of contour levels to plot. Defaults to 5.
452
480
  bandwidth (float, optional): Bandwidth for KDE. Controls the smoothness of the contour. Defaults to 0.8.
453
481
  grid_size (int, optional): Resolution of the grid for KDE. Higher values create finer contours. Defaults to 250.
454
- color (str, list, tuple, or np.ndarray, optional): Color of the contours. Can be a single color or an array of colors. Defaults to "white".
482
+ color (str, list, tuple, or np.ndarray, optional): Color of the contours. Can be a single color or an array of colors.
483
+ Defaults to "white".
455
484
  linestyle (str, optional): Line style for the contours. Defaults to "solid".
456
485
  linewidth (float, optional): Line width for the contours. Defaults to 1.5.
457
- alpha (float, optional): Transparency level of the contour lines. Defaults to 1.0.
458
- fill_alpha (float, optional): Transparency level of the contour fill. Defaults to 0.2.
486
+ alpha (float, None, optional): Transparency level of the contour lines. If provided, it overrides any existing alpha values
487
+ found in color. Defaults to 1.0.
488
+ fill_alpha (float, None, optional): Transparency level of the contour fill. If provided, it overrides any existing alpha
489
+ values found in color. Defaults to None.
459
490
  """
460
491
  # Log the contour plotting parameters
461
492
  params.log_plotter(
@@ -470,7 +501,9 @@ class NetworkPlotter:
470
501
  )
471
502
 
472
503
  # 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))
504
+ color = _to_rgba(
505
+ color=color, alpha=alpha, num_repeats=len(self.graph.domain_id_to_node_ids_map)
506
+ )
474
507
  # Extract node coordinates from the network graph
475
508
  node_coordinates = self.graph.node_coordinates
476
509
  # Draw contours for each domain in the network
@@ -499,8 +532,8 @@ class NetworkPlotter:
499
532
  color: Union[str, List, Tuple, np.ndarray] = "white",
500
533
  linestyle: str = "solid",
501
534
  linewidth: float = 1.5,
502
- alpha: float = 1.0,
503
- fill_alpha: float = 0.2,
535
+ alpha: Union[float, None] = 1.0,
536
+ fill_alpha: Union[float, None] = None,
504
537
  ) -> None:
505
538
  """Plot a subcontour for a given set of nodes or a list of node sets using Kernel Density Estimation (KDE).
506
539
 
@@ -509,11 +542,14 @@ class NetworkPlotter:
509
542
  levels (int, optional): Number of contour levels to plot. Defaults to 5.
510
543
  bandwidth (float, optional): Bandwidth for KDE. Controls the smoothness of the contour. Defaults to 0.8.
511
544
  grid_size (int, optional): Resolution of the grid for KDE. Higher values create finer contours. Defaults to 250.
512
- color (str, list, tuple, or np.ndarray, optional): Color of the contour. Can be a string (e.g., 'white') or RGBA array. Defaults to "white".
545
+ color (str, list, tuple, or np.ndarray, optional): Color of the contour. Can be a string (e.g., 'white') or RGBA array.
546
+ Defaults to "white".
513
547
  linestyle (str, optional): Line style for the contour. Defaults to "solid".
514
548
  linewidth (float, optional): Line width for the contour. Defaults to 1.5.
515
- alpha (float, optional): Transparency level of the contour lines. Defaults to 1.0.
516
- fill_alpha (float, optional): Transparency level of the contour fill. Defaults to 0.2.
549
+ alpha (float, None, optional): Transparency level of the contour lines. If provided, it overrides any existing alpha values
550
+ found in color. Defaults to 1.0.
551
+ fill_alpha (float, None, optional): Transparency level of the contour fill. If provided, it overrides any existing alpha
552
+ values found in color. Defaults to None.
517
553
 
518
554
  Raises:
519
555
  ValueError: If no valid nodes are found in the network graph.
@@ -527,7 +563,7 @@ class NetworkPlotter:
527
563
  node_groups = [nodes]
528
564
 
529
565
  # Convert color to RGBA using the _to_rgba helper function
530
- color_rgba = _to_rgba(color, alpha)
566
+ color_rgba = _to_rgba(color=color, alpha=alpha)
531
567
 
532
568
  # Iterate over each group of nodes (either sublists or flat list)
533
569
  for sublist in node_groups:
@@ -569,8 +605,8 @@ class NetworkPlotter:
569
605
  color: Union[str, np.ndarray] = "white",
570
606
  linestyle: str = "solid",
571
607
  linewidth: float = 1.5,
572
- alpha: float = 1.0,
573
- fill_alpha: float = 0.2,
608
+ alpha: Union[float, None] = 1.0,
609
+ fill_alpha: Union[float, None] = 0.2,
574
610
  ) -> None:
575
611
  """Draw a Kernel Density Estimate (KDE) contour plot for a set of nodes on a given axis.
576
612
 
@@ -584,8 +620,10 @@ class NetworkPlotter:
584
620
  color (str or np.ndarray): Color for the contour. Can be a string or RGBA array. Defaults to "white".
585
621
  linestyle (str, optional): Line style for the contour. Defaults to "solid".
586
622
  linewidth (float, optional): Line width for the contour. Defaults to 1.5.
587
- alpha (float, optional): Transparency level for the contour lines. Defaults to 1.0.
588
- fill_alpha (float, optional): Transparency level for the contour fill. Defaults to 0.2.
623
+ alpha (float, None, optional): Transparency level for the contour lines. If provided, it overrides any existing alpha
624
+ values found in color. Defaults to 1.0.
625
+ fill_alpha (float, None, optional): Transparency level for the contour fill. If provided, it overrides any existing
626
+ alpha values found in color. Defaults to 0.2.
589
627
  """
590
628
  # Extract the positions of the specified nodes
591
629
  points = np.array([pos[n] for n in nodes])
@@ -672,11 +710,11 @@ class NetworkPlotter:
672
710
  font: str = "Arial",
673
711
  fontsize: int = 10,
674
712
  fontcolor: Union[str, List, Tuple, np.ndarray] = "black",
675
- fontalpha: float = 1.0,
713
+ fontalpha: Union[float, None] = 1.0,
676
714
  arrow_linewidth: float = 1,
677
715
  arrow_style: str = "->",
678
716
  arrow_color: Union[str, List, Tuple, np.ndarray] = "black",
679
- arrow_alpha: float = 1.0,
717
+ arrow_alpha: Union[float, None] = 1.0,
680
718
  arrow_base_shrink: float = 0.0,
681
719
  arrow_tip_shrink: float = 0.0,
682
720
  max_labels: Union[int, None] = None,
@@ -696,12 +734,15 @@ class NetworkPlotter:
696
734
  offset (float, optional): Offset distance for labels from the perimeter. Defaults to 0.10.
697
735
  font (str, optional): Font name for the labels. Defaults to "Arial".
698
736
  fontsize (int, optional): Font size for the labels. Defaults to 10.
699
- fontcolor (str, list, tuple, or np.ndarray, optional): Color of the label text. Can be a string or RGBA array. Defaults to "black".
700
- fontalpha (float, optional): Transparency level for the font color. Defaults to 1.0.
737
+ fontcolor (str, list, tuple, or np.ndarray, optional): Color of the label text. Can be a string or RGBA array.
738
+ Defaults to "black".
739
+ fontalpha (float, None, optional): Transparency level for the font color. If provided, it overrides any existing alpha
740
+ values found in fontcolor. Defaults to 1.0.
701
741
  arrow_linewidth (float, optional): Line width of the arrows pointing to centroids. Defaults to 1.
702
742
  arrow_style (str, optional): Style of the arrows pointing to centroids. Defaults to "->".
703
743
  arrow_color (str, list, tuple, or np.ndarray, optional): Color of the arrows. Defaults to "black".
704
- arrow_alpha (float, optional): Transparency level for the arrow color. Defaults to 1.0.
744
+ arrow_alpha (float, None, optional): Transparency level for the arrow color. If provided, it overrides any existing alpha
745
+ values found in arrow_color. Defaults to 1.0.
705
746
  arrow_base_shrink (float, optional): Distance between the text and the base of the arrow. Defaults to 0.0.
706
747
  arrow_tip_shrink (float, optional): Distance between the arrow tip and the centroid. Defaults to 0.0.
707
748
  max_labels (int, optional): Maximum number of labels to plot. Defaults to None (no limit).
@@ -713,9 +754,9 @@ class NetworkPlotter:
713
754
  overlay_ids (bool, optional): Whether to overlay domain IDs in the center of the centroids. Defaults to False.
714
755
  ids_to_keep (list, tuple, np.ndarray, or None, optional): IDs of domains that must be labeled. To discover domain IDs,
715
756
  you can set `overlay_ids=True`. Defaults to None.
716
- ids_to_replace (dict, optional): A dictionary mapping domain IDs to custom labels (strings). The labels should be space-separated words.
717
- If provided, the custom labels will replace the default domain terms. To discover domain IDs, you can set `overlay_ids=True`.
718
- Defaults to None.
757
+ ids_to_replace (dict, optional): A dictionary mapping domain IDs to custom labels (strings). The labels should be
758
+ space-separated words. If provided, the custom labels will replace the default domain terms. To discover domain IDs, you
759
+ can set `overlay_ids=True`. Defaults to None.
719
760
 
720
761
  Raises:
721
762
  ValueError: If the number of provided `ids_to_keep` exceeds `max_labels`.
@@ -816,12 +857,14 @@ class NetworkPlotter:
816
857
  best_label_positions = _calculate_best_label_positions(
817
858
  filtered_domain_centroids, center, radius, offset
818
859
  )
819
- # Convert colors to RGBA using the _to_rgba helper function
860
+ # Convert all domain colors to RGBA using the _to_rgba helper function
820
861
  fontcolor = _to_rgba(
821
- fontcolor, fontalpha, num_repeats=len(self.graph.domain_id_to_node_ids_map)
862
+ color=fontcolor, alpha=fontalpha, num_repeats=len(self.graph.domain_id_to_node_ids_map)
822
863
  )
823
864
  arrow_color = _to_rgba(
824
- arrow_color, arrow_alpha, num_repeats=len(self.graph.domain_id_to_node_ids_map)
865
+ color=arrow_color,
866
+ alpha=arrow_alpha,
867
+ num_repeats=len(self.graph.domain_id_to_node_ids_map),
825
868
  )
826
869
 
827
870
  # Annotate the network with labels
@@ -847,8 +890,11 @@ class NetworkPlotter:
847
890
  shrinkB=arrow_tip_shrink,
848
891
  ),
849
892
  )
850
- # Overlay domain ID at the centroid if requested
851
- if overlay_ids:
893
+
894
+ # Overlay domain ID at the centroid regardless of max_labels if requested
895
+ if overlay_ids:
896
+ for idx, domain in enumerate(self.graph.domain_id_to_node_ids_map):
897
+ centroid = domain_id_to_centroid_map[domain]
852
898
  self.ax.text(
853
899
  centroid[0],
854
900
  centroid[1],
@@ -871,11 +917,11 @@ class NetworkPlotter:
871
917
  font: str = "Arial",
872
918
  fontsize: int = 10,
873
919
  fontcolor: Union[str, List, Tuple, np.ndarray] = "black",
874
- fontalpha: float = 1.0,
920
+ fontalpha: Union[float, None] = 1.0,
875
921
  arrow_linewidth: float = 1,
876
922
  arrow_style: str = "->",
877
923
  arrow_color: Union[str, List, Tuple, np.ndarray] = "black",
878
- arrow_alpha: float = 1.0,
924
+ arrow_alpha: Union[float, None] = 1.0,
879
925
  arrow_base_shrink: float = 0.0,
880
926
  arrow_tip_shrink: float = 0.0,
881
927
  ) -> None:
@@ -890,11 +936,13 @@ class NetworkPlotter:
890
936
  font (str, optional): Font name for the label. Defaults to "Arial".
891
937
  fontsize (int, optional): Font size for the label. Defaults to 10.
892
938
  fontcolor (str, list, tuple, or np.ndarray, optional): Color of the label text. Defaults to "black".
893
- fontalpha (float, optional): Transparency level for the font color. Defaults to 1.0.
939
+ fontalpha (float, None, optional): Transparency level for the font color. If provided, it overrides any existing alpha values found
940
+ in fontalpha. Defaults to 1.0.
894
941
  arrow_linewidth (float, optional): Line width of the arrow pointing to the centroid. Defaults to 1.
895
942
  arrow_style (str, optional): Style of the arrows pointing to the centroid. Defaults to "->".
896
943
  arrow_color (str, list, tuple, or np.ndarray, optional): Color of the arrow. Defaults to "black".
897
- arrow_alpha (float, optional): Transparency level for the arrow color. Defaults to 1.0.
944
+ arrow_alpha (float, None, optional): Transparency level for the arrow color. If provided, it overrides any existing alpha values
945
+ found in arrow_alpha. Defaults to 1.0.
898
946
  arrow_base_shrink (float, optional): Distance between the text and the base of the arrow. Defaults to 0.0.
899
947
  arrow_tip_shrink (float, optional): Distance between the arrow tip and the centroid. Defaults to 0.0.
900
948
  """
@@ -907,8 +955,8 @@ class NetworkPlotter:
907
955
  node_groups = [nodes]
908
956
 
909
957
  # Convert fontcolor and arrow_color to RGBA
910
- fontcolor_rgba = _to_rgba(fontcolor, fontalpha)
911
- arrow_color_rgba = _to_rgba(arrow_color, arrow_alpha)
958
+ fontcolor_rgba = _to_rgba(color=fontcolor, alpha=fontalpha)
959
+ arrow_color_rgba = _to_rgba(color=arrow_color, alpha=arrow_alpha)
912
960
 
913
961
  # Calculate the bounding box around the network
914
962
  center, radius = _calculate_bounding_box(self.graph.node_coordinates, radius_margin=scale)
@@ -1251,9 +1299,9 @@ class NetworkPlotter:
1251
1299
  min_scale: float = 0.8,
1252
1300
  max_scale: float = 1.0,
1253
1301
  scale_factor: float = 1.0,
1254
- alpha: float = 1.0,
1302
+ alpha: Union[float, None] = 1.0,
1255
1303
  nonenriched_color: Union[str, List, Tuple, np.ndarray] = "white",
1256
- nonenriched_alpha: float = 1.0,
1304
+ nonenriched_alpha: Union[float, None] = 1.0,
1257
1305
  random_seed: int = 888,
1258
1306
  ) -> np.ndarray:
1259
1307
  """Adjust the colors of nodes in the network graph based on enrichment.
@@ -1264,9 +1312,11 @@ class NetworkPlotter:
1264
1312
  min_scale (float, optional): Minimum scale for color intensity. Defaults to 0.8.
1265
1313
  max_scale (float, optional): Maximum scale for color intensity. Defaults to 1.0.
1266
1314
  scale_factor (float, optional): Factor for adjusting the color scaling intensity. Defaults to 1.0.
1267
- alpha (float, optional): Alpha value for enriched nodes. Defaults to 1.0.
1315
+ alpha (float, None, optional): Alpha value for enriched nodes. If provided, it overrides any existing alpha values
1316
+ found in color. Defaults to 1.0.
1268
1317
  nonenriched_color (str, list, tuple, or np.ndarray, optional): Color for non-enriched nodes. Defaults to "white".
1269
- nonenriched_alpha (float, optional): Alpha value for non-enriched nodes. Defaults to 1.0.
1318
+ nonenriched_alpha (float, None, optional): Alpha value for non-enriched nodes. If provided, it overrides any existing
1319
+ alpha values found in nonenriched_color. Defaults to 1.0.
1270
1320
  random_seed (int, optional): Seed for random number generation. Defaults to 888.
1271
1321
 
1272
1322
  Returns:
@@ -1284,11 +1334,11 @@ class NetworkPlotter:
1284
1334
  # Apply the alpha value for enriched nodes
1285
1335
  network_colors[:, 3] = alpha # Apply the alpha value to the enriched nodes' A channel
1286
1336
  # Convert the non-enriched color to RGBA using the _to_rgba helper function
1287
- nonenriched_color = _to_rgba(nonenriched_color, nonenriched_alpha)
1337
+ nonenriched_color = _to_rgba(color=nonenriched_color, alpha=nonenriched_alpha)
1288
1338
  # Adjust node colors: replace any fully black nodes (RGB == 0) with the non-enriched color and its alpha
1289
1339
  adjusted_network_colors = np.where(
1290
1340
  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
1341
+ np.array(nonenriched_color), # Apply the non-enriched color with alpha
1292
1342
  network_colors, # Keep the original colors for enriched nodes
1293
1343
  )
1294
1344
  return adjusted_network_colors
@@ -1462,62 +1512,67 @@ class NetworkPlotter:
1462
1512
 
1463
1513
  def _to_rgba(
1464
1514
  color: Union[str, List, Tuple, np.ndarray],
1465
- alpha: float = 1.0,
1515
+ alpha: Union[float, None] = None,
1466
1516
  num_repeats: Union[int, None] = None,
1467
1517
  ) -> np.ndarray:
1468
- """Convert a color or array of colors to RGBA format, applying alpha only if the color is RGB.
1518
+ """Convert color(s) to RGBA format, applying alpha and repeating as needed.
1469
1519
 
1470
1520
  Args:
1471
1521
  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.
1522
+ alpha (float, None, optional): Alpha value (transparency) to apply. If provided, it overrides any existing alpha values
1523
+ found in color.
1524
+ num_repeats (int, None, optional): If provided, the color(s) will be repeated this many times. Defaults to None.
1474
1525
 
1475
1526
  Returns:
1476
- np.ndarray: The RGBA color or array of RGBA colors.
1527
+ np.ndarray: Array of RGBA colors repeated `num_repeats` times, if applicable.
1477
1528
  """
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
1529
 
1495
- return rgba_color
1530
+ def convert_to_rgba(c: Union[str, List, Tuple, np.ndarray]) -> np.ndarray:
1531
+ """Convert a single color to RGBA format, handling strings, hex, and RGB/RGBA lists."""
1532
+ # Note: if no alpha is provided, the default alpha value is 1.0 by mcolors.to_rgba
1533
+ if isinstance(c, str):
1534
+ # Convert color names or hex values (e.g., 'red', '#FF5733') to RGBA
1535
+ rgba = np.array(mcolors.to_rgba(c))
1536
+ elif isinstance(c, (list, tuple, np.ndarray)) and len(c) in [3, 4]:
1537
+ # Convert RGB (3) or RGBA (4) values to RGBA format
1538
+ rgba = np.array(mcolors.to_rgba(c))
1539
+ else:
1540
+ raise ValueError(
1541
+ f"Invalid color format: {c}. Must be a valid string or RGB/RGBA sequence."
1542
+ )
1496
1543
 
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
1544
+ if alpha is not None: # Override alpha if provided
1545
+ rgba[3] = alpha
1546
+ return rgba
1511
1547
 
1512
- rgba_colors.append(rgba_c)
1513
- else:
1514
- raise ValueError(f"Invalid color: {c}. Must be a valid RGB/RGBA or string color.")
1548
+ # If color is a 2D array of RGBA values, convert it to a list of lists
1549
+ if isinstance(color, np.ndarray) and color.ndim == 2 and color.shape[1] == 4:
1550
+ color = [list(c) for c in color]
1515
1551
 
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)
1552
+ # Handle a single color (string or RGB/RGBA list/tuple)
1553
+ if isinstance(color, (str, list, tuple)) and not any(
1554
+ isinstance(c, (list, tuple, np.ndarray)) for c in color
1555
+ ):
1556
+ rgba_color = convert_to_rgba(color)
1557
+ if num_repeats:
1558
+ return np.tile(
1559
+ rgba_color, (num_repeats, 1)
1560
+ ) # Repeat the color if num_repeats is provided
1561
+ return np.array([rgba_color]) # Return a single color wrapped in a numpy array
1562
+
1563
+ # Handle a list/array of colors
1564
+ elif isinstance(color, (list, tuple, np.ndarray)):
1565
+ rgba_colors = np.array(
1566
+ [convert_to_rgba(c) for c in color]
1567
+ ) # Convert each color in the list to RGBA
1568
+ # Handle repetition if num_repeats is provided
1569
+ if num_repeats:
1570
+ repeated_colors = np.array(
1571
+ [rgba_colors[i % len(rgba_colors)] for i in range(num_repeats)]
1572
+ )
1573
+ return repeated_colors
1519
1574
 
1520
- return np.array(rgba_colors)
1575
+ return rgba_colors
1521
1576
 
1522
1577
  else:
1523
1578
  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.8b6
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
File without changes
File without changes