risk-network 0.0.9b26__py3-none-any.whl → 0.0.9b28__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.
@@ -1,21 +1,16 @@
1
1
  """
2
- risk/network/plot/network
3
- ~~~~~~~~~~~~~~~~~~~~~~~~~
2
+ risk/network/plotter/network
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
4
  """
5
5
 
6
6
  from typing import Any, Dict, List, Tuple, Union
7
7
 
8
- import matplotlib.pyplot as plt
9
8
  import networkx as nx
10
9
  import numpy as np
11
10
 
12
11
  from risk.log import params
13
- from risk.network.graph.network import NetworkGraph
14
- from risk.network.plotter.canvas import Canvas
15
- from risk.network.plotter.contour import Contour
16
- from risk.network.plotter.labels import Labels
12
+ from risk.network.graph.graph import Graph
17
13
  from risk.network.plotter.utils.colors import get_domain_colors, to_rgba
18
- from risk.network.plotter.utils.layout import calculate_bounding_box
19
14
 
20
15
 
21
16
  class Network:
@@ -24,11 +19,11 @@ class Network:
24
19
  The Network class provides methods to plot network graphs with flexible node and edge properties.
25
20
  """
26
21
 
27
- def __init__(self, graph: NetworkGraph, ax: Any = None) -> None:
28
- """Initialize the NetworkPlotter class.
22
+ def __init__(self, graph: Graph, ax: Any = None) -> None:
23
+ """Initialize the Plotter class.
29
24
 
30
25
  Args:
31
- graph (NetworkGraph): The network data and attributes to be visualized.
26
+ graph (Graph): The network data and attributes to be visualized.
32
27
  ax (Any, optional): Axes object to plot the network graph. Defaults to None.
33
28
  """
34
29
  self.graph = graph
@@ -297,128 +292,3 @@ class Network:
297
292
  node_sizes[node] = significant_size
298
293
 
299
294
  return node_sizes
300
-
301
-
302
- class NetworkPlotter(Canvas, Network, Contour, Labels):
303
- """A class for visualizing network graphs with customizable options.
304
-
305
- The NetworkPlotter class uses a NetworkGraph object and provides methods to plot the network with
306
- flexible node and edge properties. It also supports plotting labels, contours, drawing the network's
307
- perimeter, and adjusting background colors.
308
- """
309
-
310
- def __init__(
311
- self,
312
- graph: NetworkGraph,
313
- figsize: Tuple = (10, 10),
314
- background_color: Union[str, List, Tuple, np.ndarray] = "white",
315
- background_alpha: Union[float, None] = 1.0,
316
- pad: float = 0.3,
317
- ) -> None:
318
- """Initialize the NetworkPlotter with a NetworkGraph object and plotting parameters.
319
-
320
- Args:
321
- graph (NetworkGraph): The network data and attributes to be visualized.
322
- figsize (Tuple, optional): Size of the figure in inches (width, height). Defaults to (10, 10).
323
- background_color (str, List, Tuple, np.ndarray, optional): Background color of the plot. Defaults to "white".
324
- background_alpha (float, None, optional): Transparency level of the background color. If provided, it overrides
325
- any existing alpha values found in background_color. Defaults to 1.0.
326
- pad (float, optional): Padding value to adjust the axis limits. Defaults to 0.3.
327
- """
328
- self.graph = graph
329
- # Initialize the plot with the specified parameters
330
- self.ax = self._initialize_plot(
331
- graph=graph,
332
- figsize=figsize,
333
- background_color=background_color,
334
- background_alpha=background_alpha,
335
- pad=pad,
336
- )
337
- super().__init__(graph=graph, ax=self.ax)
338
-
339
- def _initialize_plot(
340
- self,
341
- graph: NetworkGraph,
342
- figsize: Tuple,
343
- background_color: Union[str, List, Tuple, np.ndarray],
344
- background_alpha: Union[float, None],
345
- pad: float,
346
- ) -> plt.Axes:
347
- """Set up the plot with figure size and background color.
348
-
349
- Args:
350
- graph (NetworkGraph): The network data and attributes to be visualized.
351
- figsize (Tuple): Size of the figure in inches (width, height).
352
- background_color (str, List, Tuple, or np.ndarray): Background color of the plot. Can be a single color or an array of colors.
353
- background_alpha (float, None, optional): Transparency level of the background color. If provided, it overrides any existing
354
- alpha values found in `background_color`.
355
- pad (float, optional): Padding value to adjust the axis limits.
356
-
357
- Returns:
358
- plt.Axes: The axis object for the plot.
359
- """
360
- # Log the plotter settings
361
- params.log_plotter(
362
- figsize=figsize,
363
- background_color=background_color,
364
- background_alpha=background_alpha,
365
- pad=pad,
366
- )
367
-
368
- # Extract node coordinates from the network graph
369
- node_coordinates = graph.node_coordinates
370
- # Calculate the center and radius of the bounding box around the network
371
- center, radius = calculate_bounding_box(node_coordinates)
372
-
373
- # Create a new figure and axis for plotting
374
- fig, ax = plt.subplots(figsize=figsize)
375
- fig.tight_layout() # Adjust subplot parameters to give specified padding
376
- # Set axis limits based on the calculated bounding box and radius
377
- ax.set_xlim([center[0] - radius - pad, center[0] + radius + pad])
378
- ax.set_ylim([center[1] - radius - pad, center[1] + radius + pad])
379
- ax.set_aspect("equal") # Ensure the aspect ratio is equal
380
-
381
- # Set the background color of the plot
382
- # Convert color to RGBA using the to_rgba helper function
383
- fig.patch.set_facecolor(
384
- to_rgba(color=background_color, alpha=background_alpha, num_repeats=1)
385
- ) # num_repeats=1 for single color
386
- ax.invert_yaxis() # Invert the y-axis to match typical image coordinates
387
- # Remove axis spines for a cleaner look
388
- for spine in ax.spines.values():
389
- spine.set_visible(False)
390
-
391
- # Hide axis ticks and labels
392
- ax.set_xticks([])
393
- ax.set_yticks([])
394
- ax.patch.set_visible(False) # Hide the axis background
395
-
396
- return ax
397
-
398
- @staticmethod
399
- def savefig(*args, pad_inches: float = 0.5, dpi: int = 100, **kwargs) -> None:
400
- """Save the current plot to a file with additional export options.
401
-
402
- Args:
403
- *args: Positional arguments passed to `plt.savefig`.
404
- pad_inches (float, optional): Padding around the figure when saving. Defaults to 0.5.
405
- dpi (int, optional): Dots per inch (DPI) for the exported image. Defaults to 300.
406
- **kwargs: Keyword arguments passed to `plt.savefig`, such as filename and format.
407
- """
408
- # Ensure user-provided kwargs take precedence
409
- kwargs.setdefault("dpi", dpi)
410
- kwargs.setdefault("pad_inches", pad_inches)
411
- # Ensure the plot is saved with tight bounding box if not specified
412
- kwargs.setdefault("bbox_inches", "tight")
413
- # Call plt.savefig with combined arguments
414
- plt.savefig(*args, **kwargs)
415
-
416
- @staticmethod
417
- def show(*args, **kwargs) -> None:
418
- """Display the current plot.
419
-
420
- Args:
421
- *args: Positional arguments passed to `plt.show`.
422
- **kwargs: Keyword arguments passed to `plt.show`.
423
- """
424
- plt.show(*args, **kwargs)
@@ -0,0 +1,143 @@
1
+ """
2
+ risk/network/plotter/plotter
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
+ """
5
+
6
+ from typing import List, Tuple, Union
7
+
8
+ import matplotlib.pyplot as plt
9
+ import numpy as np
10
+
11
+ from risk.log import params
12
+ from risk.network.graph.graph import Graph
13
+ from risk.network.plotter.canvas import Canvas
14
+ from risk.network.plotter.contour import Contour
15
+ from risk.network.plotter.labels import Labels
16
+ from risk.network.plotter.network import Network
17
+ from risk.network.plotter.utils.colors import to_rgba
18
+ from risk.network.plotter.utils.layout import calculate_bounding_box
19
+
20
+
21
+ class Plotter(Canvas, Network, Contour, Labels):
22
+ """A class for visualizing network graphs with customizable options.
23
+
24
+ The Plotter class uses a Graph object and provides methods to plot the network with
25
+ flexible node and edge properties. It also supports plotting labels, contours, drawing the network's
26
+ perimeter, and adjusting background colors.
27
+ """
28
+
29
+ def __init__(
30
+ self,
31
+ graph: Graph,
32
+ figsize: Tuple = (10, 10),
33
+ background_color: Union[str, List, Tuple, np.ndarray] = "white",
34
+ background_alpha: Union[float, None] = 1.0,
35
+ pad: float = 0.3,
36
+ ) -> None:
37
+ """Initialize the Plotter with a Graph object and plotting parameters.
38
+
39
+ Args:
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).
42
+ background_color (str, List, Tuple, np.ndarray, optional): Background color of the plot. Defaults to "white".
43
+ background_alpha (float, None, optional): Transparency level of the background color. If provided, it overrides
44
+ any existing alpha values found in background_color. Defaults to 1.0.
45
+ pad (float, optional): Padding value to adjust the axis limits. Defaults to 0.3.
46
+ """
47
+ self.graph = graph
48
+ # Initialize the plot with the specified parameters
49
+ self.ax = self._initialize_plot(
50
+ graph=graph,
51
+ figsize=figsize,
52
+ background_color=background_color,
53
+ background_alpha=background_alpha,
54
+ pad=pad,
55
+ )
56
+ super().__init__(graph=graph, ax=self.ax)
57
+
58
+ def _initialize_plot(
59
+ self,
60
+ graph: Graph,
61
+ figsize: Tuple,
62
+ background_color: Union[str, List, Tuple, np.ndarray],
63
+ background_alpha: Union[float, None],
64
+ pad: float,
65
+ ) -> plt.Axes:
66
+ """Set up the plot with figure size and background color.
67
+
68
+ Args:
69
+ graph (Graph): The network data and attributes to be visualized.
70
+ figsize (Tuple): Size of the figure in inches (width, height).
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
+ background_alpha (float, None, optional): Transparency level of the background color. If provided, it overrides any existing
73
+ alpha values found in `background_color`.
74
+ pad (float, optional): Padding value to adjust the axis limits.
75
+
76
+ Returns:
77
+ plt.Axes: The axis object for the plot.
78
+ """
79
+ # Log the plotter settings
80
+ params.log_plotter(
81
+ figsize=figsize,
82
+ background_color=background_color,
83
+ background_alpha=background_alpha,
84
+ pad=pad,
85
+ )
86
+
87
+ # Extract node coordinates from the network graph
88
+ node_coordinates = graph.node_coordinates
89
+ # Calculate the center and radius of the bounding box around the network
90
+ center, radius = calculate_bounding_box(node_coordinates)
91
+
92
+ # Create a new figure and axis for plotting
93
+ fig, ax = plt.subplots(figsize=figsize)
94
+ fig.tight_layout() # Adjust subplot parameters to give specified padding
95
+ # Set axis limits based on the calculated bounding box and radius
96
+ ax.set_xlim([center[0] - radius - pad, center[0] + radius + pad])
97
+ ax.set_ylim([center[1] - radius - pad, center[1] + radius + pad])
98
+ ax.set_aspect("equal") # Ensure the aspect ratio is equal
99
+
100
+ # Set the background color of the plot
101
+ # Convert color to RGBA using the to_rgba helper function
102
+ fig.patch.set_facecolor(
103
+ to_rgba(color=background_color, alpha=background_alpha, num_repeats=1)
104
+ ) # num_repeats=1 for single color
105
+ ax.invert_yaxis() # Invert the y-axis to match typical image coordinates
106
+ # Remove axis spines for a cleaner look
107
+ for spine in ax.spines.values():
108
+ spine.set_visible(False)
109
+
110
+ # Hide axis ticks and labels
111
+ ax.set_xticks([])
112
+ ax.set_yticks([])
113
+ ax.patch.set_visible(False) # Hide the axis background
114
+
115
+ return ax
116
+
117
+ @staticmethod
118
+ def savefig(*args, pad_inches: float = 0.5, dpi: int = 100, **kwargs) -> None:
119
+ """Save the current plot to a file with additional export options.
120
+
121
+ Args:
122
+ *args: Positional arguments passed to `plt.savefig`.
123
+ pad_inches (float, optional): Padding around the figure when saving. Defaults to 0.5.
124
+ dpi (int, optional): Dots per inch (DPI) for the exported image. Defaults to 300.
125
+ **kwargs: Keyword arguments passed to `plt.savefig`, such as filename and format.
126
+ """
127
+ # Ensure user-provided kwargs take precedence
128
+ kwargs.setdefault("dpi", dpi)
129
+ kwargs.setdefault("pad_inches", pad_inches)
130
+ # Ensure the plot is saved with tight bounding box if not specified
131
+ kwargs.setdefault("bbox_inches", "tight")
132
+ # Call plt.savefig with combined arguments
133
+ plt.savefig(*args, **kwargs)
134
+
135
+ @staticmethod
136
+ def show(*args, **kwargs) -> None:
137
+ """Display the current plot.
138
+
139
+ Args:
140
+ *args: Positional arguments passed to `plt.show`.
141
+ **kwargs: Keyword arguments passed to `plt.show`.
142
+ """
143
+ plt.show(*args, **kwargs)
@@ -1,6 +1,6 @@
1
1
  """
2
- risk/network/plot/utils/color
3
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2
+ risk/network/plotter/utils/colors
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
4
  """
5
5
 
6
6
  from typing import Any, Dict, List, Tuple, Union
@@ -9,11 +9,11 @@ import matplotlib
9
9
  import matplotlib.colors as mcolors
10
10
  import numpy as np
11
11
 
12
- from risk.network.graph.network import NetworkGraph
12
+ from risk.network.graph.graph import Graph
13
13
 
14
14
 
15
15
  def get_annotated_domain_colors(
16
- graph: NetworkGraph,
16
+ graph: Graph,
17
17
  cmap: str = "gist_rainbow",
18
18
  color: Union[str, List, Tuple, np.ndarray, None] = None,
19
19
  blend_colors: bool = False,
@@ -27,7 +27,7 @@ def get_annotated_domain_colors(
27
27
  """Get colors for the domains based on node annotations, or use a specified color.
28
28
 
29
29
  Args:
30
- graph (NetworkGraph): The network data and attributes to be visualized.
30
+ graph (Graph): The network data and attributes to be visualized.
31
31
  cmap (str, optional): Colormap to use for generating domain colors. Defaults to "gist_rainbow".
32
32
  color (str, List, Tuple, np.ndarray, or None, optional): Color to use for the domains. Can be a single color or an array of colors.
33
33
  If None, the colormap will be used. Defaults to None.
@@ -72,7 +72,7 @@ def get_annotated_domain_colors(
72
72
 
73
73
 
74
74
  def get_domain_colors(
75
- graph: NetworkGraph,
75
+ graph: Graph,
76
76
  cmap: str = "gist_rainbow",
77
77
  color: Union[str, List, Tuple, np.ndarray, None] = None,
78
78
  blend_colors: bool = False,
@@ -86,7 +86,7 @@ def get_domain_colors(
86
86
  """Generate composite colors for domains based on significance or specified colors.
87
87
 
88
88
  Args:
89
- graph (NetworkGraph): The network data and attributes to be visualized.
89
+ graph (Graph): The network data and attributes to be visualized.
90
90
  cmap (str, optional): Name of the colormap to use for generating domain colors. Defaults to "gist_rainbow".
91
91
  color (str, List, Tuple, np.ndarray, or None, optional): A specific color or array of colors to use for all domains.
92
92
  If None, the colormap will be used. Defaults to None.
@@ -127,7 +127,7 @@ def get_domain_colors(
127
127
 
128
128
 
129
129
  def _get_domain_ids_to_colors(
130
- graph: NetworkGraph,
130
+ graph: Graph,
131
131
  cmap: str = "gist_rainbow",
132
132
  color: Union[str, List, Tuple, np.ndarray, None] = None,
133
133
  ids_to_colors: Union[Dict[int, Any], None] = None,
@@ -136,7 +136,7 @@ def _get_domain_ids_to_colors(
136
136
  """Get colors for each domain.
137
137
 
138
138
  Args:
139
- graph (NetworkGraph): The network data and attributes to be visualized.
139
+ graph (Graph): The network data and attributes to be visualized.
140
140
  cmap (str, optional): The name of the colormap to use. Defaults to "gist_rainbow".
141
141
  color (str, List, Tuple, np.ndarray, or None, optional): A specific color or array of colors to use for the domains.
142
142
  If None, the colormap will be used. Defaults to None.
@@ -167,7 +167,7 @@ def _get_domain_ids_to_colors(
167
167
 
168
168
 
169
169
  def _get_composite_node_colors(
170
- graph: NetworkGraph,
170
+ graph: Graph,
171
171
  domain_ids_to_colors: Dict[int, Any],
172
172
  blend_colors: bool = False,
173
173
  blend_gamma: float = 2.2,
@@ -175,7 +175,7 @@ def _get_composite_node_colors(
175
175
  """Generate composite colors for nodes based on domain colors and significance values, with optional color blending.
176
176
 
177
177
  Args:
178
- graph (NetworkGraph): The network data and attributes to be visualized.
178
+ graph (Graph): The network data and attributes to be visualized.
179
179
  domain_ids_to_colors (Dict[int, Any]): Mapping of domain IDs to RGBA colors.
180
180
  blend_colors (bool): Whether to blend colors for nodes with multiple domains. Defaults to False.
181
181
  blend_gamma (float, optional): Gamma correction factor to be used for perceptual color blending.
@@ -1,6 +1,6 @@
1
1
  """
2
- risk/network/plot/utils/layout
3
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2
+ risk/network/plotter/utils/layout
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
4
  """
5
5
 
6
6
  from typing import Any, Dict, List, Tuple
risk/stats/__init__.py CHANGED
@@ -3,11 +3,13 @@ risk/stats
3
3
  ~~~~~~~~~~
4
4
  """
5
5
 
6
- from risk.stats.binom import compute_binom_test
7
- from risk.stats.chi2 import compute_chi2_test
8
- from risk.stats.hypergeom import compute_hypergeom_test
9
6
  from risk.stats.permutation import compute_permutation_test
10
- from risk.stats.poisson import compute_poisson_test
11
- from risk.stats.zscore import compute_zscore_test
7
+ from risk.stats.stat_tests import (
8
+ compute_binom_test,
9
+ compute_chi2_test,
10
+ compute_hypergeom_test,
11
+ compute_poisson_test,
12
+ compute_zscore_test,
13
+ )
12
14
 
13
- from risk.stats.stats import calculate_significance_matrices
15
+ from risk.stats.significance import calculate_significance_matrices
@@ -1,6 +1,6 @@
1
1
  """
2
- risk/stats/stats
3
- ~~~~~~~~~~~~~~~~
2
+ risk/stats/significance
3
+ ~~~~~~~~~~~~~~~~~~~~~~~
4
4
  """
5
5
 
6
6
  from typing import Any, Dict, Union