risk-network 0.0.8b26__py3-none-any.whl → 0.0.9b1__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/network/graph.py CHANGED
@@ -15,9 +15,9 @@ class NetworkGraph:
15
15
  """A class to represent a network graph and process its nodes and edges.
16
16
 
17
17
  The NetworkGraph class provides functionality to handle and manipulate a network graph,
18
- including managing domains, annotations, and node enrichment data. It also includes methods
18
+ including managing domains, annotations, and node significance data. It also includes methods
19
19
  for transforming and mapping graph coordinates, as well as generating colors based on node
20
- enrichment.
20
+ significance.
21
21
  """
22
22
 
23
23
  def __init__(
@@ -27,7 +27,7 @@ class NetworkGraph:
27
27
  domains: pd.DataFrame,
28
28
  trimmed_domains: pd.DataFrame,
29
29
  node_label_to_node_id_map: Dict[str, Any],
30
- node_enrichment_sums: np.ndarray,
30
+ node_significance_sums: np.ndarray,
31
31
  ):
32
32
  """Initialize the NetworkGraph object.
33
33
 
@@ -37,7 +37,7 @@ class NetworkGraph:
37
37
  domains (pd.DataFrame): DataFrame containing domain data for the network nodes.
38
38
  trimmed_domains (pd.DataFrame): DataFrame containing trimmed domain data for the network nodes.
39
39
  node_label_to_node_id_map (Dict[str, Any]): A dictionary mapping node labels to their corresponding IDs.
40
- node_enrichment_sums (np.ndarray): Array containing the enrichment sums for the nodes.
40
+ node_significance_sums (np.ndarray): Array containing the significant sums for the nodes.
41
41
  """
42
42
  self.top_annotations = top_annotations
43
43
  self.domain_id_to_node_ids_map = self._create_domain_id_to_node_ids_map(domains)
@@ -49,13 +49,13 @@ class NetworkGraph:
49
49
  trimmed_domains
50
50
  )
51
51
  self.trimmed_domains = trimmed_domains
52
- self.node_enrichment_sums = node_enrichment_sums
53
- self.node_id_to_domain_ids_and_enrichments_map = (
54
- self._create_node_id_to_domain_ids_and_enrichments(domains)
52
+ self.node_significance_sums = node_significance_sums
53
+ self.node_id_to_domain_ids_and_significance_map = (
54
+ self._create_node_id_to_domain_ids_and_significances(domains)
55
55
  )
56
56
  self.node_id_to_node_label_map = {v: k for k, v in node_label_to_node_id_map.items()}
57
- self.node_label_to_enrichment_map = dict(
58
- zip(node_label_to_node_id_map.keys(), node_enrichment_sums)
57
+ self.node_label_to_significance_map = dict(
58
+ zip(node_label_to_node_id_map.keys(), node_significance_sums)
59
59
  )
60
60
  self.node_label_to_node_id_map = node_label_to_node_id_map
61
61
  # NOTE: Below this point, instance attributes (i.e., self) will be used!
@@ -103,25 +103,25 @@ class NetworkGraph:
103
103
  def _create_domain_id_to_domain_info_map(
104
104
  trimmed_domains: pd.DataFrame,
105
105
  ) -> Dict[int, Dict[str, Any]]:
106
- """Create a mapping from domain IDs to their corresponding full description and enrichment score.
106
+ """Create a mapping from domain IDs to their corresponding full description and significance score.
107
107
 
108
108
  Args:
109
- trimmed_domains (pd.DataFrame): DataFrame containing domain IDs, full descriptions, and enrichment scores.
109
+ trimmed_domains (pd.DataFrame): DataFrame containing domain IDs, full descriptions, and significance scores.
110
110
 
111
111
  Returns:
112
- Dict[int, Dict[str, Any]]: A dictionary mapping domain IDs (int) to a dictionary with 'full_descriptions' and 'enrichment_scores'.
112
+ Dict[int, Dict[str, Any]]: A dictionary mapping domain IDs (int) to a dictionary with 'full_descriptions' and 'significance_scores'.
113
113
  """
114
114
  return {
115
115
  int(id_): {
116
116
  "full_descriptions": trimmed_domains.at[id_, "full_descriptions"],
117
- "enrichment_scores": trimmed_domains.at[id_, "enrichment_scores"],
117
+ "significance_scores": trimmed_domains.at[id_, "significance_scores"],
118
118
  }
119
119
  for id_ in trimmed_domains.index
120
120
  }
121
121
 
122
122
  @staticmethod
123
- def _create_node_id_to_domain_ids_and_enrichments(domains: pd.DataFrame) -> Dict[int, Dict]:
124
- """Creates a dictionary mapping each node ID to its corresponding domain IDs and enrichment values.
123
+ def _create_node_id_to_domain_ids_and_significances(domains: pd.DataFrame) -> Dict[int, Dict]:
124
+ """Creates a dictionary mapping each node ID to its corresponding domain IDs and significance values.
125
125
 
126
126
  Args:
127
127
  domains (pd.DataFrame): A DataFrame containing domain information for each node. Assumes the last
@@ -129,28 +129,28 @@ class NetworkGraph:
129
129
 
130
130
  Returns:
131
131
  Dict[int, Dict]: A dictionary where the key is the node ID (index of the DataFrame), and the value is another dictionary
132
- with 'domain' (a list of domain IDs with non-zero enrichment) and 'enrichment'
133
- (a dict of domain IDs and their corresponding enrichment values).
132
+ with 'domain' (a list of domain IDs with non-zero significance) and 'significance'
133
+ (a dict of domain IDs and their corresponding significance values).
134
134
  """
135
135
  # Initialize an empty dictionary to store the result
136
- node_id_to_domain_ids_and_enrichments = {}
136
+ node_id_to_domain_ids_and_significances = {}
137
137
  # Get the list of domain columns (excluding 'all domains' and 'primary domain')
138
138
  domain_columns = domains.columns[
139
139
  :-2
140
140
  ] # The last two columns are 'all domains' and 'primary domain'
141
141
  # Iterate over each row in the dataframe
142
142
  for idx, row in domains.iterrows():
143
- # Get the domains (column names) where the enrichment score is greater than 0
143
+ # Get the domains (column names) where the significance score is greater than 0
144
144
  all_domains = domain_columns[row[domain_columns] > 0].tolist()
145
- # Get the enrichment values for those domains
146
- enrichment_values = row[all_domains].to_dict()
145
+ # Get the significance values for those domains
146
+ significance_values = row[all_domains].to_dict()
147
147
  # Store the result in the dictionary with index as the key
148
- node_id_to_domain_ids_and_enrichments[idx] = {
149
- "domains": all_domains, # The column names where enrichment > 0
150
- "enrichments": enrichment_values, # The actual enrichment values for those columns
148
+ node_id_to_domain_ids_and_significances[idx] = {
149
+ "domains": all_domains, # The column names where significance > 0
150
+ "significances": significance_values, # The actual significance values for those columns
151
151
  }
152
152
 
153
- return node_id_to_domain_ids_and_enrichments
153
+ return node_id_to_domain_ids_and_significances
154
154
 
155
155
  def _create_domain_id_to_node_labels_map(self) -> Dict[int, List[str]]:
156
156
  """Create a map from domain IDs to node labels.
@@ -36,6 +36,7 @@ class Canvas:
36
36
  font: str = "Arial",
37
37
  title_color: Union[str, List, Tuple, np.ndarray] = "black",
38
38
  subtitle_color: Union[str, List, Tuple, np.ndarray] = "gray",
39
+ title_x: float = 0.5,
39
40
  title_y: float = 0.975,
40
41
  title_space_offset: float = 0.075,
41
42
  subtitle_offset: float = 0.025,
@@ -52,6 +53,7 @@ class Canvas:
52
53
  Defaults to "black".
53
54
  subtitle_color (str, List, Tuple, or np.ndarray, optional): Color of the subtitle text. Can be a string or an array of colors.
54
55
  Defaults to "gray".
56
+ title_x (float, optional): X-axis position of the title. Defaults to 0.5.
55
57
  title_y (float, optional): Y-axis position of the title. Defaults to 0.975.
56
58
  title_space_offset (float, optional): Fraction of figure height to leave for the space above the plot. Defaults to 0.075.
57
59
  subtitle_offset (float, optional): Offset factor to position the subtitle below the title. Defaults to 0.025.
@@ -85,7 +87,7 @@ class Canvas:
85
87
  fontsize=title_fontsize,
86
88
  color=title_color,
87
89
  fontname=font,
88
- x=0.5, # Center the title horizontally
90
+ x=title_x,
89
91
  ha="center",
90
92
  va="top",
91
93
  y=title_y,
@@ -294,7 +294,7 @@ class Contour:
294
294
  Controls the dimmest colors. Defaults to 0.8.
295
295
  max_scale (float, optional): Maximum intensity scale for the colors generated by the colormap.
296
296
  Controls the brightest colors. Defaults to 1.0.
297
- scale_factor (float, optional): Exponent for adjusting color scaling based on enrichment scores.
297
+ scale_factor (float, optional): Exponent for adjusting color scaling based on significance scores.
298
298
  A higher value increases contrast by dimming lower scores more. Defaults to 1.0.
299
299
  random_seed (int, optional): Seed for random number generation to ensure reproducibility. Defaults to 888.
300
300
 
@@ -659,7 +659,7 @@ class Labels:
659
659
  Controls the dimmest colors. Defaults to 0.8.
660
660
  max_scale (float, optional): Maximum intensity scale for the colors generated by the colormap.
661
661
  Controls the brightest colors. Defaults to 1.0.
662
- scale_factor (float, optional): Exponent for adjusting color scaling based on enrichment scores.
662
+ scale_factor (float, optional): Exponent for adjusting color scaling based on significance scores.
663
663
  A higher value increases contrast by dimming lower scores more. Defaults to 1.0.
664
664
  random_seed (int, optional): Seed for random number generation to ensure reproducibility. Defaults to 888.
665
665
 
@@ -203,11 +203,11 @@ class Network:
203
203
  max_scale: float = 1.0,
204
204
  scale_factor: float = 1.0,
205
205
  alpha: Union[float, None] = 1.0,
206
- nonenriched_color: Union[str, List, Tuple, np.ndarray] = "white",
207
- nonenriched_alpha: Union[float, None] = 1.0,
206
+ nonsignificant_color: Union[str, List, Tuple, np.ndarray] = "white",
207
+ nonsignificant_alpha: Union[float, None] = 1.0,
208
208
  random_seed: int = 888,
209
209
  ) -> np.ndarray:
210
- """Adjust the colors of nodes in the network graph based on enrichment.
210
+ """Adjust the colors of nodes in the network graph based on significance.
211
211
 
212
212
  Args:
213
213
  cmap (str, optional): Colormap to use for coloring the nodes. Defaults to "gist_rainbow".
@@ -218,16 +218,16 @@ class Network:
218
218
  min_scale (float, optional): Minimum scale for color intensity. Defaults to 0.8.
219
219
  max_scale (float, optional): Maximum scale for color intensity. Defaults to 1.0.
220
220
  scale_factor (float, optional): Factor for adjusting the color scaling intensity. Defaults to 1.0.
221
- alpha (float, None, optional): Alpha value for enriched nodes. If provided, it overrides any existing alpha values found in `color`.
221
+ alpha (float, None, optional): Alpha value for significant nodes. If provided, it overrides any existing alpha values found in `color`.
222
222
  Defaults to 1.0.
223
- nonenriched_color (str, List, Tuple, or np.ndarray, optional): Color for non-enriched nodes. Can be a single color or an array of colors.
223
+ nonsignificant_color (str, List, Tuple, or np.ndarray, optional): Color for non-significant nodes. Can be a single color or an array of colors.
224
224
  Defaults to "white".
225
- nonenriched_alpha (float, None, optional): Alpha value for non-enriched nodes. If provided, it overrides any existing alpha values found
226
- in `nonenriched_color`. Defaults to 1.0.
225
+ nonsignificant_alpha (float, None, optional): Alpha value for non-significant nodes. If provided, it overrides any existing alpha values found
226
+ in `nonsignificant_color`. Defaults to 1.0.
227
227
  random_seed (int, optional): Seed for random number generation. Defaults to 888.
228
228
 
229
229
  Returns:
230
- np.ndarray: Array of RGBA colors adjusted for enrichment status.
230
+ np.ndarray: Array of RGBA colors adjusted for significance status.
231
231
  """
232
232
  # Get the initial domain colors for each node, which are returned as RGBA
233
233
  network_colors = get_domain_colors(
@@ -241,11 +241,11 @@ class Network:
241
241
  scale_factor=scale_factor,
242
242
  random_seed=random_seed,
243
243
  )
244
- # Apply the alpha value for enriched nodes
245
- network_colors[:, 3] = alpha # Apply the alpha value to the enriched nodes' A channel
246
- # Convert the non-enriched color to RGBA using the to_rgba helper function
247
- nonenriched_color_rgba = to_rgba(
248
- color=nonenriched_color, alpha=nonenriched_alpha, num_repeats=1
244
+ # Apply the alpha value for significant nodes
245
+ network_colors[:, 3] = alpha # Apply the alpha value to the significant nodes' A channel
246
+ # Convert the non-significant color to RGBA using the to_rgba helper function
247
+ nonsignificant_color_rgba = to_rgba(
248
+ color=nonsignificant_color, alpha=nonsignificant_alpha, num_repeats=1
249
249
  ) # num_repeats=1 for a single color
250
250
  # Adjust node colors: replace any nodes where all three RGB values are equal and less than 0.1
251
251
  # 0.1 is a predefined threshold for the minimum color intensity
@@ -255,34 +255,34 @@ class Network:
255
255
  & np.all(network_colors[:, :3] == network_colors[:, 0:1], axis=1)
256
256
  )[:, None],
257
257
  np.tile(
258
- np.array(nonenriched_color_rgba), (network_colors.shape[0], 1)
259
- ), # Replace with the full RGBA non-enriched color
258
+ np.array(nonsignificant_color_rgba), (network_colors.shape[0], 1)
259
+ ), # Replace with the full RGBA non-significant color
260
260
  network_colors, # Keep the original colors where no match is found
261
261
  )
262
262
  return adjusted_network_colors
263
263
 
264
264
  def get_annotated_node_sizes(
265
- self, enriched_size: int = 50, nonenriched_size: int = 25
265
+ self, significant_size: int = 50, nonsignificant_size: int = 25
266
266
  ) -> np.ndarray:
267
- """Adjust the sizes of nodes in the network graph based on whether they are enriched or not.
267
+ """Adjust the sizes of nodes in the network graph based on whether they are significant or not.
268
268
 
269
269
  Args:
270
- enriched_size (int): Size for enriched nodes. Defaults to 50.
271
- nonenriched_size (int): Size for non-enriched nodes. Defaults to 25.
270
+ significant_size (int): Size for significant nodes. Defaults to 50.
271
+ nonsignificant_size (int): Size for non-significant nodes. Defaults to 25.
272
272
 
273
273
  Returns:
274
- np.ndarray: Array of node sizes, with enriched nodes larger than non-enriched ones.
274
+ np.ndarray: Array of node sizes, with significant nodes larger than non-significant ones.
275
275
  """
276
- # Merge all enriched nodes from the domain_id_to_node_ids_map dictionary
277
- enriched_nodes = set()
276
+ # Merge all significant nodes from the domain_id_to_node_ids_map dictionary
277
+ significant_nodes = set()
278
278
  for _, node_ids in self.graph.domain_id_to_node_ids_map.items():
279
- enriched_nodes.update(node_ids)
279
+ significant_nodes.update(node_ids)
280
280
 
281
- # Initialize all node sizes to the non-enriched size
282
- node_sizes = np.full(len(self.graph.network.nodes), nonenriched_size)
283
- # Set the size for enriched nodes
284
- for node in enriched_nodes:
281
+ # Initialize all node sizes to the non-significant size
282
+ node_sizes = np.full(len(self.graph.network.nodes), nonsignificant_size)
283
+ # Set the size for significant nodes
284
+ for node in significant_nodes:
285
285
  if node in self.graph.network.nodes:
286
- node_sizes[node] = enriched_size
286
+ node_sizes[node] = significant_size
287
287
 
288
288
  return node_sizes
@@ -35,14 +35,14 @@ def get_annotated_domain_colors(
35
35
  blend_gamma (float, optional): Gamma correction factor for perceptual color blending. Defaults to 2.2.
36
36
  min_scale (float, optional): Minimum scale for color intensity when generating domain colors. Defaults to 0.8.
37
37
  max_scale (float, optional): Maximum scale for color intensity when generating domain colors. Defaults to 1.0.
38
- scale_factor (float, optional): Factor for adjusting the contrast in the colors generated based on enrichment. Higher values
38
+ scale_factor (float, optional): Factor for adjusting the contrast in the colors generated based on significance. Higher values
39
39
  increase the contrast. Defaults to 1.0.
40
40
  random_seed (int, optional): Seed for random number generation to ensure reproducibility. Defaults to 888.
41
41
 
42
42
  Returns:
43
43
  np.ndarray: Array of RGBA colors for each domain.
44
44
  """
45
- # Generate domain colors based on the enrichment data
45
+ # Generate domain colors based on the significance data
46
46
  node_colors = get_domain_colors(
47
47
  graph=graph,
48
48
  cmap=cmap,
@@ -82,7 +82,7 @@ def get_domain_colors(
82
82
  scale_factor: float = 1.0,
83
83
  random_seed: int = 888,
84
84
  ) -> np.ndarray:
85
- """Generate composite colors for domains based on enrichment or specified colors.
85
+ """Generate composite colors for domains based on significance or specified colors.
86
86
 
87
87
  Args:
88
88
  graph (NetworkGraph): The network data and attributes to be visualized.
@@ -95,12 +95,12 @@ def get_domain_colors(
95
95
  Defaults to 0.8.
96
96
  max_scale (float, optional): Maximum intensity scale for the colors generated by the colormap. Controls the brightest colors.
97
97
  Defaults to 1.0.
98
- scale_factor (float, optional): Exponent for adjusting the color scaling based on enrichment scores. Higher values increase
98
+ scale_factor (float, optional): Exponent for adjusting the color scaling based on significance scores. Higher values increase
99
99
  contrast by dimming lower scores more. Defaults to 1.0.
100
100
  random_seed (int, optional): Seed for random number generation to ensure reproducibility of color assignments. Defaults to 888.
101
101
 
102
102
  Returns:
103
- np.ndarray: Array of RGBA colors generated for each domain, based on enrichment or the specified color.
103
+ np.ndarray: Array of RGBA colors generated for each domain, based on significance or the specified color.
104
104
  """
105
105
  # Get colors for each domain
106
106
  domain_colors = _get_domain_colors(graph=graph, cmap=cmap, color=color, random_seed=random_seed)
@@ -111,7 +111,7 @@ def get_domain_colors(
111
111
  # Transform colors to ensure proper alpha values and intensity
112
112
  transformed_colors = _transform_colors(
113
113
  node_colors,
114
- graph.node_enrichment_sums,
114
+ graph.node_significance_sums,
115
115
  min_scale=min_scale,
116
116
  max_scale=max_scale,
117
117
  scale_factor=scale_factor,
@@ -151,7 +151,7 @@ def _get_domain_colors(
151
151
  def _get_composite_node_colors(
152
152
  graph, domain_colors: np.ndarray, blend_colors: bool = False, blend_gamma: float = 2.2
153
153
  ) -> np.ndarray:
154
- """Generate composite colors for nodes based on domain colors and enrichment values, with optional color blending.
154
+ """Generate composite colors for nodes based on domain colors and significance values, with optional color blending.
155
155
 
156
156
  Args:
157
157
  graph (NetworkGraph): The network data and attributes to be visualized.
@@ -177,26 +177,26 @@ def _get_composite_node_colors(
177
177
 
178
178
  # If blending is required
179
179
  else:
180
- for node, node_info in graph.node_id_to_domain_ids_and_enrichments_map.items():
180
+ for node, node_info in graph.node_id_to_domain_ids_and_significance_map.items():
181
181
  domains = node_info["domains"] # List of domain IDs
182
- enrichments = node_info["enrichments"] # List of enrichment values
183
- # Filter domains and enrichments to keep only those with corresponding colors in domain_colors
184
- filtered_domains_enrichments = [
185
- (domain_id, enrichment)
186
- for domain_id, enrichment in zip(domains, enrichments)
182
+ significances = node_info["significances"] # List of significance values
183
+ # Filter domains and significances to keep only those with corresponding colors in domain_colors
184
+ filtered_domains_significances = [
185
+ (domain_id, significance)
186
+ for domain_id, significance in zip(domains, significances)
187
187
  if domain_id in domain_colors
188
188
  ]
189
189
  # If no valid domains exist, skip this node
190
- if not filtered_domains_enrichments:
190
+ if not filtered_domains_significances:
191
191
  continue
192
192
 
193
- # Unpack filtered domains and enrichments
194
- filtered_domains, filtered_enrichments = zip(*filtered_domains_enrichments)
193
+ # Unpack filtered domains and significances
194
+ filtered_domains, filtered_significances = zip(*filtered_domains_significances)
195
195
  # Get the colors corresponding to the valid filtered domains
196
196
  colors = [domain_colors[domain_id] for domain_id in filtered_domains]
197
197
  # Blend the colors using the given gamma (default is 2.2 if None)
198
198
  gamma = blend_gamma if blend_gamma is not None else 2.2
199
- composite_color = _blend_colors_perceptually(colors, filtered_enrichments, gamma)
199
+ composite_color = _blend_colors_perceptually(colors, filtered_significances, gamma)
200
200
  # Assign the composite color to the node
201
201
  composite_colors[node] = composite_color
202
202
 
@@ -282,21 +282,21 @@ def _assign_distant_colors(dist_matrix, num_colors_to_generate):
282
282
 
283
283
 
284
284
  def _blend_colors_perceptually(
285
- colors: Union[List, Tuple, np.ndarray], enrichments: List[float], gamma: float = 2.2
285
+ colors: Union[List, Tuple, np.ndarray], significances: List[float], gamma: float = 2.2
286
286
  ) -> Tuple[float, float, float, float]:
287
287
  """Blends a list of RGBA colors using gamma correction for perceptually uniform color mixing.
288
288
 
289
289
  Args:
290
290
  colors (List, Tuple, np.ndarray): List of RGBA colors. Can be a list, tuple, or NumPy array of RGBA values.
291
- enrichments (List[float]): Corresponding list of enrichment values.
291
+ significances (List[float]): Corresponding list of significance values.
292
292
  gamma (float, optional): Gamma correction factor, default is 2.2 (typical for perceptual blending).
293
293
 
294
294
  Returns:
295
295
  Tuple[float, float, float, float]: The blended RGBA color.
296
296
  """
297
- # Normalize enrichments so they sum up to 1 (proportions)
298
- total_enrichment = sum(enrichments)
299
- proportions = [enrichment / total_enrichment for enrichment in enrichments]
297
+ # Normalize significances so they sum up to 1 (proportions)
298
+ total_significance = sum(significances)
299
+ proportions = [significance / total_significance for significance in significances]
300
300
  # Convert colors to gamma-corrected space (apply gamma correction to RGB channels)
301
301
  gamma_corrected_colors = [[channel**gamma for channel in color[:3]] for color in colors]
302
302
  # Blend the colors in gamma-corrected space
@@ -310,17 +310,17 @@ def _blend_colors_perceptually(
310
310
 
311
311
  def _transform_colors(
312
312
  colors: np.ndarray,
313
- enrichment_sums: np.ndarray,
313
+ significance_sums: np.ndarray,
314
314
  min_scale: float = 0.8,
315
315
  max_scale: float = 1.0,
316
316
  scale_factor: float = 1.0,
317
317
  ) -> np.ndarray:
318
- """Transform colors using power scaling to emphasize high enrichment sums more. Black colors are replaced with
318
+ """Transform colors using power scaling to emphasize high significance sums more. Black colors are replaced with
319
319
  very dark grey to avoid issues with color scaling (rgb(0.1, 0.1, 0.1)).
320
320
 
321
321
  Args:
322
322
  colors (np.ndarray): An array of RGBA colors.
323
- enrichment_sums (np.ndarray): An array of enrichment sums corresponding to the colors.
323
+ significance_sums (np.ndarray): An array of significance sums corresponding to the colors.
324
324
  min_scale (float, optional): Minimum scale for color intensity. Defaults to 0.8.
325
325
  max_scale (float, optional): Maximum scale for color intensity. Defaults to 1.0.
326
326
  scale_factor (float, optional): Exponent for scaling, where values > 1 increase contrast by dimming small
@@ -340,8 +340,8 @@ def _transform_colors(
340
340
  is_black = np.all(colors[:, :3] == black_color, axis=1)
341
341
  colors[is_black, :3] = dark_grey
342
342
 
343
- # Normalize the enrichment sums to the range [0, 1]
344
- normalized_sums = enrichment_sums / np.max(enrichment_sums)
343
+ # Normalize the significance sums to the range [0, 1]
344
+ normalized_sums = significance_sums / np.max(significance_sums)
345
345
  # Apply power scaling to dim lower values and emphasize higher values
346
346
  scaled_sums = normalized_sums**scale_factor
347
347
  # Linearly scale the normalized sums to the range [min_scale, max_scale]
risk/risk.py CHANGED
@@ -259,9 +259,9 @@ class RISK(NetworkIO, AnnotationsIO):
259
259
  network: nx.Graph,
260
260
  annotations: Dict[str, Any],
261
261
  neighborhoods: Dict[str, Any],
262
- tail: str = "right", # OPTIONS: "right" (enrichment), "left" (depletion), "both"
263
- pval_cutoff: float = 0.01, # OPTIONS: Any value between 0 to 1
264
- fdr_cutoff: float = 0.9999, # OPTIONS: Any value between 0 to 1
262
+ tail: str = "right",
263
+ pval_cutoff: float = 0.01,
264
+ fdr_cutoff: float = 0.9999,
265
265
  impute_depth: int = 0,
266
266
  prune_threshold: float = 0.0,
267
267
  linkage_criterion: str = "distance",
@@ -275,7 +275,7 @@ class RISK(NetworkIO, AnnotationsIO):
275
275
  Args:
276
276
  network (nx.Graph): The network graph.
277
277
  annotations (pd.DataFrame): DataFrame containing annotation data for the network.
278
- neighborhoods (Dict[str, Any]): Neighborhood enrichment data.
278
+ neighborhoods (Dict[str, Any]): Neighborhood significance data.
279
279
  tail (str, optional): Type of significance tail ("right", "left", "both"). Defaults to "right".
280
280
  pval_cutoff (float, optional): p-value cutoff for significance. Defaults to 0.01.
281
281
  fdr_cutoff (float, optional): FDR cutoff for significance. Defaults to 0.9999.
@@ -360,10 +360,10 @@ class RISK(NetworkIO, AnnotationsIO):
360
360
  max_cluster_size=max_cluster_size,
361
361
  )
362
362
 
363
- # Prepare node mapping and enrichment sums for the final NetworkGraph object
363
+ # Prepare node mapping and significance sums for the final NetworkGraph object
364
364
  ordered_nodes = annotations["ordered_nodes"]
365
365
  node_label_to_id = dict(zip(ordered_nodes, range(len(ordered_nodes))))
366
- node_enrichment_sums = processed_neighborhoods["node_enrichment_sums"]
366
+ node_significance_sums = processed_neighborhoods["node_significance_sums"]
367
367
 
368
368
  # Return the fully initialized NetworkGraph object
369
369
  return NetworkGraph(
@@ -372,7 +372,7 @@ class RISK(NetworkIO, AnnotationsIO):
372
372
  domains=domains,
373
373
  trimmed_domains=trimmed_domains,
374
374
  node_label_to_node_id_map=node_label_to_id,
375
- node_enrichment_sums=node_enrichment_sums,
375
+ node_significance_sums=node_significance_sums,
376
376
  )
377
377
 
378
378
  def load_plotter(
@@ -467,7 +467,7 @@ class RISK(NetworkIO, AnnotationsIO):
467
467
  Args:
468
468
  network (nx.Graph): The network graph.
469
469
  annotations (Dict[str, Any]): Annotations data for the network.
470
- neighborhoods (Dict[str, Any]): Neighborhood enrichment data.
470
+ neighborhoods (Dict[str, Any]): Neighborhood significance data.
471
471
  min_cluster_size (int, optional): Minimum size for clusters. Defaults to 5.
472
472
  max_cluster_size (int, optional): Maximum size for clusters. Defaults to 1000.
473
473
 
@@ -476,16 +476,18 @@ class RISK(NetworkIO, AnnotationsIO):
476
476
  """
477
477
  # Extract necessary data from annotations and neighborhoods
478
478
  ordered_annotations = annotations["ordered_annotations"]
479
- neighborhood_enrichment_sums = neighborhoods["neighborhood_enrichment_counts"]
480
- significant_enrichment_matrix = neighborhoods["significant_enrichment_matrix"]
481
- significant_binary_enrichment_matrix = neighborhoods["significant_binary_enrichment_matrix"]
479
+ neighborhood_significance_sums = neighborhoods["neighborhood_significance_counts"]
480
+ significant_significance_matrix = neighborhoods["significant_significance_matrix"]
481
+ significant_binary_significance_matrix = neighborhoods[
482
+ "significant_binary_significance_matrix"
483
+ ]
482
484
  # Call external function to define top annotations
483
485
  return define_top_annotations(
484
486
  network=network,
485
487
  ordered_annotation_labels=ordered_annotations,
486
- neighborhood_enrichment_sums=neighborhood_enrichment_sums,
487
- significant_enrichment_matrix=significant_enrichment_matrix,
488
- significant_binary_enrichment_matrix=significant_binary_enrichment_matrix,
488
+ neighborhood_significance_sums=neighborhood_significance_sums,
489
+ significant_significance_matrix=significant_significance_matrix,
490
+ significant_binary_significance_matrix=significant_binary_significance_matrix,
489
491
  min_cluster_size=min_cluster_size,
490
492
  max_cluster_size=max_cluster_size,
491
493
  )
@@ -498,7 +500,7 @@ class RISK(NetworkIO, AnnotationsIO):
498
500
  linkage_method: str,
499
501
  linkage_metric: str,
500
502
  ) -> pd.DataFrame:
501
- """Define domains in the network based on enrichment data.
503
+ """Define domains in the network based on significance data.
502
504
 
503
505
  Args:
504
506
  neighborhoods (Dict[str, Any]): Enrichment data for neighborhoods.
@@ -510,12 +512,12 @@ class RISK(NetworkIO, AnnotationsIO):
510
512
  Returns:
511
513
  pd.DataFrame: Matrix of defined domains.
512
514
  """
513
- # Extract the significant enrichment matrix from the neighborhoods data
514
- significant_neighborhoods_enrichment = neighborhoods["significant_enrichment_matrix"]
515
+ # Extract the significant significance matrix from the neighborhoods data
516
+ significant_neighborhoods_significance = neighborhoods["significant_significance_matrix"]
515
517
  # Call external function to define domains based on the extracted data
516
518
  return define_domains(
517
519
  top_annotations=top_annotations,
518
- significant_neighborhoods_enrichment=significant_neighborhoods_enrichment,
520
+ significant_neighborhoods_significance=significant_neighborhoods_significance,
519
521
  linkage_criterion=linkage_criterion,
520
522
  linkage_method=linkage_method,
521
523
  linkage_metric=linkage_metric,
risk/stats/stats.py CHANGED
@@ -44,7 +44,7 @@ def calculate_significance_matrices(
44
44
  enrichment_pvals, enrichment_qvals, pval_cutoff=pval_cutoff, fdr_cutoff=fdr_cutoff
45
45
  )
46
46
  # Compute the enrichment matrix using both q-values and p-values
47
- enrichment_matrix = (enrichment_qvals**2) * (enrichment_pvals**0.5)
47
+ enrichment_matrix = (enrichment_pvals**0.5) * (enrichment_qvals**2)
48
48
  else:
49
49
  # Compute threshold matrices based on p-value cutoffs only
50
50
  depletion_alpha_threshold_matrix = _compute_threshold_matrix(
@@ -62,7 +62,7 @@ def calculate_significance_matrices(
62
62
  log_enrichment_matrix = -np.log10(enrichment_matrix)
63
63
 
64
64
  # Select the appropriate significance matrices based on the specified tail
65
- enrichment_matrix, significant_binary_enrichment_matrix = _select_significance_matrices(
65
+ significance_matrix, significant_binary_significance_matrix = _select_significance_matrices(
66
66
  tail,
67
67
  log_depletion_matrix,
68
68
  depletion_alpha_threshold_matrix,
@@ -71,14 +71,14 @@ def calculate_significance_matrices(
71
71
  )
72
72
 
73
73
  # Filter the enrichment matrix using the binary significance matrix
74
- significant_enrichment_matrix = np.where(
75
- significant_binary_enrichment_matrix == 1, enrichment_matrix, 0
74
+ significant_significance_matrix = np.where(
75
+ significant_binary_significance_matrix == 1, significance_matrix, 0
76
76
  )
77
77
 
78
78
  return {
79
- "enrichment_matrix": enrichment_matrix,
80
- "significant_binary_enrichment_matrix": significant_binary_enrichment_matrix,
81
- "significant_enrichment_matrix": significant_enrichment_matrix,
79
+ "significance_matrix": significance_matrix,
80
+ "significant_significance_matrix": significant_significance_matrix,
81
+ "significant_binary_significance_matrix": significant_binary_significance_matrix,
82
82
  }
83
83
 
84
84
 
@@ -109,15 +109,15 @@ def _select_significance_matrices(
109
109
 
110
110
  if tail == "left":
111
111
  # Select depletion matrix and corresponding alpha threshold for left-tail analysis
112
- enrichment_matrix = -log_depletion_matrix
112
+ significance_matrix = -log_depletion_matrix
113
113
  alpha_threshold_matrix = depletion_alpha_threshold_matrix
114
114
  elif tail == "right":
115
115
  # Select enrichment matrix and corresponding alpha threshold for right-tail analysis
116
- enrichment_matrix = log_enrichment_matrix
116
+ significance_matrix = log_enrichment_matrix
117
117
  alpha_threshold_matrix = enrichment_alpha_threshold_matrix
118
118
  elif tail == "both":
119
119
  # Select the matrix with the highest absolute values while preserving the sign
120
- enrichment_matrix = np.where(
120
+ significance_matrix = np.where(
121
121
  np.abs(log_depletion_matrix) >= np.abs(log_enrichment_matrix),
122
122
  -log_depletion_matrix,
123
123
  log_enrichment_matrix,
@@ -129,10 +129,10 @@ def _select_significance_matrices(
129
129
 
130
130
  # Create a binary significance matrix where valid indices meet the alpha threshold
131
131
  valid_idxs = ~np.isnan(alpha_threshold_matrix)
132
- significant_binary_enrichment_matrix = np.zeros(alpha_threshold_matrix.shape)
133
- significant_binary_enrichment_matrix[valid_idxs] = alpha_threshold_matrix[valid_idxs]
132
+ significant_binary_significance_matrix = np.zeros(alpha_threshold_matrix.shape)
133
+ significant_binary_significance_matrix[valid_idxs] = alpha_threshold_matrix[valid_idxs]
134
134
 
135
- return enrichment_matrix, significant_binary_enrichment_matrix
135
+ return significance_matrix, significant_binary_significance_matrix
136
136
 
137
137
 
138
138
  def _compute_threshold_matrix(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: risk-network
3
- Version: 0.0.8b26
3
+ Version: 0.0.9b1
4
4
  Summary: A Python package for biological network analysis
5
5
  Author: Ira Horecka
6
6
  Author-email: Ira Horecka <ira89@icloud.com>