risk-network 0.0.13b5__tar.gz → 0.0.14__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 (56) hide show
  1. risk_network-0.0.14/PKG-INFO +115 -0
  2. risk_network-0.0.14/README.md +74 -0
  3. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/__init__.py +2 -2
  4. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_annotation/__init__.py +1 -1
  5. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_annotation/_io.py +2 -2
  6. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_neighborhoods/_api.py +0 -3
  7. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_neighborhoods/_domains.py +77 -26
  8. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_neighborhoods/_neighborhoods.py +45 -23
  9. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/__init__.py +1 -1
  10. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_graph/_api.py +0 -3
  11. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_graph/_summary.py +18 -20
  12. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_io.py +133 -136
  13. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_plotter/_api.py +0 -3
  14. risk_network-0.0.13b5/src/risk/risk.py → risk_network-0.0.14/src/risk/_risk.py +5 -5
  15. risk_network-0.0.14/src/risk_network.egg-info/PKG-INFO +115 -0
  16. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk_network.egg-info/SOURCES.txt +1 -1
  17. {risk_network-0.0.13b5 → risk_network-0.0.14}/tests/test_load_graph.py +88 -0
  18. {risk_network-0.0.13b5 → risk_network-0.0.14}/tests/test_load_network.py +2 -2
  19. risk_network-0.0.13b5/PKG-INFO +0 -125
  20. risk_network-0.0.13b5/README.md +0 -84
  21. risk_network-0.0.13b5/src/risk_network.egg-info/PKG-INFO +0 -125
  22. {risk_network-0.0.13b5 → risk_network-0.0.14}/LICENSE +0 -0
  23. {risk_network-0.0.13b5 → risk_network-0.0.14}/pyproject.toml +0 -0
  24. {risk_network-0.0.13b5 → risk_network-0.0.14}/setup.cfg +0 -0
  25. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_annotation/_annotation.py +0 -0
  26. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_annotation/_nltk_setup.py +0 -0
  27. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_log/__init__.py +0 -0
  28. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_log/_console.py +0 -0
  29. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_log/_parameters.py +0 -0
  30. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_neighborhoods/__init__.py +0 -0
  31. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_neighborhoods/_community.py +0 -0
  32. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_neighborhoods/_stats/__init__.py +0 -0
  33. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_neighborhoods/_stats/_permutation/__init__.py +0 -0
  34. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_neighborhoods/_stats/_permutation/_permutation.py +0 -0
  35. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_neighborhoods/_stats/_permutation/_test_functions.py +0 -0
  36. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_neighborhoods/_stats/_tests.py +0 -0
  37. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_graph/__init__.py +0 -0
  38. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_graph/_graph.py +0 -0
  39. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_graph/_stats.py +0 -0
  40. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_plotter/__init__.py +0 -0
  41. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_plotter/_canvas.py +0 -0
  42. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_plotter/_contour.py +0 -0
  43. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_plotter/_labels.py +0 -0
  44. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_plotter/_network.py +0 -0
  45. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_plotter/_plotter.py +0 -0
  46. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_plotter/_utils/__init__.py +0 -0
  47. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_plotter/_utils/_colors.py +0 -0
  48. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk/_network/_plotter/_utils/_layout.py +0 -0
  49. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk_network.egg-info/dependency_links.txt +0 -0
  50. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk_network.egg-info/requires.txt +0 -0
  51. {risk_network-0.0.13b5 → risk_network-0.0.14}/src/risk_network.egg-info/top_level.txt +0 -0
  52. {risk_network-0.0.13b5 → risk_network-0.0.14}/tests/test_load_annotation.py +0 -0
  53. {risk_network-0.0.13b5 → risk_network-0.0.14}/tests/test_load_io_combinations.py +0 -0
  54. {risk_network-0.0.13b5 → risk_network-0.0.14}/tests/test_load_neighborhoods.py +0 -0
  55. {risk_network-0.0.13b5 → risk_network-0.0.14}/tests/test_load_plotter.py +0 -0
  56. {risk_network-0.0.13b5 → risk_network-0.0.14}/tests/test_log.py +0 -0
@@ -0,0 +1,115 @@
1
+ Metadata-Version: 2.4
2
+ Name: risk-network
3
+ Version: 0.0.14
4
+ Summary: A Python package for scalable network analysis and high-quality visualization.
5
+ Author-email: Ira Horecka <ira89@icloud.com>
6
+ License: GPL-3.0-or-later
7
+ Project-URL: Homepage, https://github.com/riskportal/network
8
+ Project-URL: Issues, https://github.com/riskportal/network/issues
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
18
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
19
+ Classifier: Topic :: Scientific/Engineering :: Visualization
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Requires-Python: >=3.8
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: ipywidgets
25
+ Requires-Dist: leidenalg
26
+ Requires-Dist: markov_clustering
27
+ Requires-Dist: matplotlib
28
+ Requires-Dist: networkx
29
+ Requires-Dist: nltk
30
+ Requires-Dist: numpy
31
+ Requires-Dist: openpyxl
32
+ Requires-Dist: pandas
33
+ Requires-Dist: python-igraph
34
+ Requires-Dist: python-louvain
35
+ Requires-Dist: scikit-learn
36
+ Requires-Dist: scipy
37
+ Requires-Dist: statsmodels
38
+ Requires-Dist: threadpoolctl
39
+ Requires-Dist: tqdm
40
+ Dynamic: license-file
41
+
42
+ # RISK Network
43
+
44
+ <p align="center">
45
+ <img src="https://i.imgur.com/8TleEJs.png" width="50%" />
46
+ </p>
47
+
48
+ <br>
49
+
50
+ ![Python](https://img.shields.io/badge/python-3.8%2B-yellow)
51
+ [![pypiv](https://img.shields.io/pypi/v/risk-network.svg)](https://pypi.python.org/pypi/risk-network)
52
+ ![License](https://img.shields.io/badge/license-GPLv3-purple)
53
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.xxxxxxx.svg)](https://doi.org/10.5281/zenodo.xxxxxxx)
54
+ ![Downloads](https://img.shields.io/pypi/dm/risk-network)
55
+ ![Tests](https://github.com/riskportal/network/actions/workflows/ci.yml/badge.svg)
56
+
57
+ **RISK** (Regional Inference of Significant Kinships) is a next-generation tool for biological network annotation and visualization. It integrates community detection algorithms, rigorous overrepresentation analysis, and a modular framework for diverse network types. RISK identifies biologically coherent relationships within networks and generates publication-ready visualizations, making it a useful tool for biological and interdisciplinary network analysis.
58
+
59
+ For a full description of RISK and its applications, see:
60
+ <br>
61
+ **Horecka and Röst (2025)**, _"RISK: a next-generation tool for biological network annotation and visualization"_.
62
+ <br>
63
+ DOI: [10.5281/zenodo.xxxxxxx](https://doi.org/10.5281/zenodo.xxxxxxx)
64
+
65
+ ## Documentation and Tutorial
66
+
67
+ Full documentation is available at:
68
+
69
+ - **Docs:** [https://riskportal.github.io/network-tutorial](https://riskportal.github.io/network-tutorial)
70
+ - **Tutorial Jupyter Notebook Repository:** [https://github.com/riskportal/network-tutorial](https://github.com/riskportal/network-tutorial)
71
+
72
+ ## Installation
73
+
74
+ RISK is compatible with Python 3.8 or later and runs on all major operating systems. To install the latest version of RISK, run:
75
+
76
+ ```bash
77
+ pip install risk-network --upgrade
78
+ ```
79
+
80
+ ## Key Features of RISK
81
+
82
+ - **Broad Data Compatibility**: Accepts multiple network formats (NetworkX, Cytoscape, GPickle) and user-provided annotations formatted as term–to–gene membership tables (JSON, CSV, TSV, Excel, or Python dictionaries).
83
+ - **Flexible Clustering**: Offers Louvain, Leiden, Markov Clustering, Greedy Modularity, Label Propagation, Spinglass, and Walktrap, with user-defined resolution parameters to detect both coarse and fine-grained modules.
84
+ - **Statistical Testing**: Provides hypergeometric, binomial, chi-squared, Poisson, z-score, and permutation tests, balancing speed with statistical rigor.
85
+ - **High-Resolution Visualization**: Generates publication-ready figures with contour overlays, customizable node/edge properties, and export to SVG, PNG, or PDF.
86
+
87
+ ## Example Usage
88
+
89
+ We applied RISK to a _Saccharomyces cerevisiae_ protein–protein interaction (PPI) network (Michaelis _et al_., 2023; 3,839 proteins, 30,955 interactions). RISK identified compact, functional modules overrepresented in Gene Ontology Biological Process (GO BP) terms (Ashburner _et al_., 2000), revealing biological organization including ribosomal assembly, mitochondrial organization, and RNA polymerase activity (P < 0.0001).
90
+
91
+ [![Yeast PPI network annotated with GO BP terms](https://i.imgur.com/jQKatLY.jpeg)](https://i.imgur.com/jQKatLY.jpeg)
92
+ **RISK workflow overview and analysis of the yeast PPI network**. GO BP terms are color-coded to represent key cellular processes—including ribosomal assembly, mitochondrial organization, and RNA polymerase activity (P < 0.0001).
93
+
94
+ ## Citation
95
+
96
+ If you use RISK in your research, please cite the following:
97
+
98
+ **Horecka and Röst (2025)**, _"RISK: a next-generation tool for biological network annotation and visualization"_.
99
+ <br>
100
+ DOI: [10.5281/zenodo.xxxxxxx](https://doi.org/10.5281/zenodo.xxxxxxx)
101
+
102
+ ## Contributing
103
+
104
+ We welcome contributions from the community:
105
+
106
+ - [Issues Tracker](https://github.com/riskportal/network/issues)
107
+ - [Source Code](https://github.com/riskportal/network/tree/main/risk)
108
+
109
+ ## Support
110
+
111
+ If you encounter issues or have suggestions for new features, please use the [Issues Tracker](https://github.com/riskportal/network/issues) on GitHub.
112
+
113
+ ## License
114
+
115
+ RISK is open source under the [GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html).
@@ -0,0 +1,74 @@
1
+ # RISK Network
2
+
3
+ <p align="center">
4
+ <img src="https://i.imgur.com/8TleEJs.png" width="50%" />
5
+ </p>
6
+
7
+ <br>
8
+
9
+ ![Python](https://img.shields.io/badge/python-3.8%2B-yellow)
10
+ [![pypiv](https://img.shields.io/pypi/v/risk-network.svg)](https://pypi.python.org/pypi/risk-network)
11
+ ![License](https://img.shields.io/badge/license-GPLv3-purple)
12
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.xxxxxxx.svg)](https://doi.org/10.5281/zenodo.xxxxxxx)
13
+ ![Downloads](https://img.shields.io/pypi/dm/risk-network)
14
+ ![Tests](https://github.com/riskportal/network/actions/workflows/ci.yml/badge.svg)
15
+
16
+ **RISK** (Regional Inference of Significant Kinships) is a next-generation tool for biological network annotation and visualization. It integrates community detection algorithms, rigorous overrepresentation analysis, and a modular framework for diverse network types. RISK identifies biologically coherent relationships within networks and generates publication-ready visualizations, making it a useful tool for biological and interdisciplinary network analysis.
17
+
18
+ For a full description of RISK and its applications, see:
19
+ <br>
20
+ **Horecka and Röst (2025)**, _"RISK: a next-generation tool for biological network annotation and visualization"_.
21
+ <br>
22
+ DOI: [10.5281/zenodo.xxxxxxx](https://doi.org/10.5281/zenodo.xxxxxxx)
23
+
24
+ ## Documentation and Tutorial
25
+
26
+ Full documentation is available at:
27
+
28
+ - **Docs:** [https://riskportal.github.io/network-tutorial](https://riskportal.github.io/network-tutorial)
29
+ - **Tutorial Jupyter Notebook Repository:** [https://github.com/riskportal/network-tutorial](https://github.com/riskportal/network-tutorial)
30
+
31
+ ## Installation
32
+
33
+ RISK is compatible with Python 3.8 or later and runs on all major operating systems. To install the latest version of RISK, run:
34
+
35
+ ```bash
36
+ pip install risk-network --upgrade
37
+ ```
38
+
39
+ ## Key Features of RISK
40
+
41
+ - **Broad Data Compatibility**: Accepts multiple network formats (NetworkX, Cytoscape, GPickle) and user-provided annotations formatted as term–to–gene membership tables (JSON, CSV, TSV, Excel, or Python dictionaries).
42
+ - **Flexible Clustering**: Offers Louvain, Leiden, Markov Clustering, Greedy Modularity, Label Propagation, Spinglass, and Walktrap, with user-defined resolution parameters to detect both coarse and fine-grained modules.
43
+ - **Statistical Testing**: Provides hypergeometric, binomial, chi-squared, Poisson, z-score, and permutation tests, balancing speed with statistical rigor.
44
+ - **High-Resolution Visualization**: Generates publication-ready figures with contour overlays, customizable node/edge properties, and export to SVG, PNG, or PDF.
45
+
46
+ ## Example Usage
47
+
48
+ We applied RISK to a _Saccharomyces cerevisiae_ protein–protein interaction (PPI) network (Michaelis _et al_., 2023; 3,839 proteins, 30,955 interactions). RISK identified compact, functional modules overrepresented in Gene Ontology Biological Process (GO BP) terms (Ashburner _et al_., 2000), revealing biological organization including ribosomal assembly, mitochondrial organization, and RNA polymerase activity (P < 0.0001).
49
+
50
+ [![Yeast PPI network annotated with GO BP terms](https://i.imgur.com/jQKatLY.jpeg)](https://i.imgur.com/jQKatLY.jpeg)
51
+ **RISK workflow overview and analysis of the yeast PPI network**. GO BP terms are color-coded to represent key cellular processes—including ribosomal assembly, mitochondrial organization, and RNA polymerase activity (P < 0.0001).
52
+
53
+ ## Citation
54
+
55
+ If you use RISK in your research, please cite the following:
56
+
57
+ **Horecka and Röst (2025)**, _"RISK: a next-generation tool for biological network annotation and visualization"_.
58
+ <br>
59
+ DOI: [10.5281/zenodo.xxxxxxx](https://doi.org/10.5281/zenodo.xxxxxxx)
60
+
61
+ ## Contributing
62
+
63
+ We welcome contributions from the community:
64
+
65
+ - [Issues Tracker](https://github.com/riskportal/network/issues)
66
+ - [Source Code](https://github.com/riskportal/network/tree/main/risk)
67
+
68
+ ## Support
69
+
70
+ If you encounter issues or have suggestions for new features, please use the [Issues Tracker](https://github.com/riskportal/network/issues) on GitHub.
71
+
72
+ ## License
73
+
74
+ RISK is open source under the [GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html).
@@ -5,7 +5,7 @@ risk
5
5
  RISK: Regional Inference of Significant Kinships
6
6
  """
7
7
 
8
- from .risk import RISK
8
+ from ._risk import RISK
9
9
 
10
10
  __all__ = ["RISK"]
11
- __version__ = "0.0.13-beta.5"
11
+ __version__ = "0.0.14"
@@ -7,4 +7,4 @@ from ._annotation import (
7
7
  define_top_annotation,
8
8
  get_weighted_description,
9
9
  )
10
- from ._io import AnnotationIO
10
+ from ._io import AnnotationHandler
@@ -13,11 +13,11 @@ from .._log import log_header, logger, params
13
13
  from ._annotation import load_annotation
14
14
 
15
15
 
16
- class AnnotationIO:
16
+ class AnnotationHandler:
17
17
  """
18
18
  Handles the loading and exporting of annotation in various file formats.
19
19
 
20
- The AnnotationIO class provides methods to load annotation from different file types (JSON, CSV, Excel, etc.)
20
+ The AnnotationHandler class provides methods to load annotation from different file types (JSON, CSV, Excel, etc.)
21
21
  and to export parameter data to various formats like JSON, CSV, and text files.
22
22
  """
23
23
 
@@ -29,9 +29,6 @@ class NeighborhoodsAPI:
29
29
  The NeighborhoodsAPI class provides methods to load neighborhood results from statistical tests.
30
30
  """
31
31
 
32
- def __init__(self) -> None:
33
- pass
34
-
35
32
  def load_neighborhoods_binom(
36
33
  self,
37
34
  network: nx.Graph,
@@ -54,37 +54,48 @@ def define_domains(
54
54
  Raises:
55
55
  ValueError: If the clustering criterion is set to "off" or if an error occurs during clustering.
56
56
  """
57
- try:
58
- if linkage_criterion == "off":
59
- raise ValueError("Clustering is turned off.")
57
+ # Validate args first; let user mistakes raise immediately
58
+ clustering_off = _validate_clustering_args(
59
+ linkage_criterion, linkage_method, linkage_metric, linkage_threshold
60
+ )
60
61
 
62
+ # If clustering is turned off, assign unique domains and skip
63
+ if clustering_off:
64
+ n_rows = len(top_annotation)
65
+ logger.warning("Clustering is turned off. Skipping clustering.")
66
+ top_annotation["domain"] = range(1, n_rows + 1)
67
+ else:
61
68
  # Transpose the matrix to cluster annotations
62
69
  m = significant_neighborhoods_significance[:, top_annotation["significant_annotation"]].T
63
70
  # Safeguard the matrix by replacing NaN, Inf, and -Inf values
64
71
  m = _safeguard_matrix(m)
65
- # Optimize silhouette score across different linkage methods and distance metrics
66
- best_linkage, best_metric, best_threshold = _optimize_silhouette_across_linkage_and_metrics(
67
- m, linkage_criterion, linkage_method, linkage_metric, linkage_threshold
68
- )
69
- # Perform hierarchical clustering
70
- Z = linkage(m, method=best_linkage, metric=best_metric)
71
- logger.warning(
72
- f"Linkage criterion: '{linkage_criterion}'\nLinkage method: '{best_linkage}'\nLinkage metric: '{best_metric}'\nLinkage threshold: {round(best_threshold, 3)}"
73
- )
74
- # Calculate the optimal threshold for clustering
75
- max_d_optimal = np.max(Z[:, 2]) * best_threshold
76
- # Assign domains to the annotation matrix
77
- domains = fcluster(Z, max_d_optimal, criterion=linkage_criterion)
78
- top_annotation["domain"] = 0
79
- top_annotation.loc[top_annotation["significant_annotation"], "domain"] = domains
80
- except (ValueError, LinAlgError):
81
- # If a ValueError is encountered, handle it by assigning unique domains
82
- n_rows = len(top_annotation)
83
- if linkage_criterion == "off":
84
- logger.warning("Clustering is turned off. Skipping clustering.")
85
- else:
86
- logger.error("Error encountered. Skipping clustering.")
87
- top_annotation["domain"] = range(1, n_rows + 1) # Assign unique domains
72
+ try:
73
+ # Optimize silhouette score across different linkage methods and distance metrics
74
+ (
75
+ best_linkage,
76
+ best_metric,
77
+ best_threshold,
78
+ ) = _optimize_silhouette_across_linkage_and_metrics(
79
+ m, linkage_criterion, linkage_method, linkage_metric, linkage_threshold
80
+ )
81
+ # Perform hierarchical clustering
82
+ Z = linkage(m, method=best_linkage, metric=best_metric)
83
+ logger.warning(
84
+ f"Linkage criterion: '{linkage_criterion}'\nLinkage method: '{best_linkage}'\nLinkage metric: '{best_metric}'\nLinkage threshold: {round(best_threshold, 3)}"
85
+ )
86
+ # Calculate the optimal threshold for clustering
87
+ max_d_optimal = np.max(Z[:, 2]) * best_threshold
88
+ # Assign domains to the annotation matrix
89
+ domains = fcluster(Z, max_d_optimal, criterion=linkage_criterion)
90
+ top_annotation["domain"] = 0
91
+ top_annotation.loc[top_annotation["significant_annotation"], "domain"] = domains
92
+ except (LinAlgError, ValueError):
93
+ # Numerical errors or degenerate input are handled gracefully (not user error)
94
+ n_rows = len(top_annotation)
95
+ logger.error(
96
+ "Clustering failed due to numerical or data degeneracy. Assigning unique domains."
97
+ )
98
+ top_annotation["domain"] = range(1, n_rows + 1)
88
99
 
89
100
  # Create DataFrames to store domain information
90
101
  node_to_significance = pd.DataFrame(
@@ -184,6 +195,46 @@ def trim_domains(
184
195
  return valid_domains, valid_trimmed_domains_matrix
185
196
 
186
197
 
198
+ def _validate_clustering_args(
199
+ linkage_criterion: str,
200
+ linkage_method: str,
201
+ linkage_metric: str,
202
+ linkage_threshold: Union[float, str],
203
+ ) -> bool:
204
+ """
205
+ Validate user-provided clustering arguments.
206
+
207
+ Returns:
208
+ bool: True if clustering is turned off (criterion == 'off'); False otherwise.
209
+
210
+ Raises:
211
+ ValueError: If any argument is invalid (user error).
212
+ """
213
+ # Allow opting out of clustering without raising
214
+ if linkage_criterion == "off":
215
+ return True
216
+ # Validate linkage method (allow "auto")
217
+ if linkage_method != "auto" and linkage_method not in LINKAGE_METHODS:
218
+ raise ValueError(
219
+ f"Invalid linkage_method '{linkage_method}'. Allowed values are 'auto' or one of: {sorted(LINKAGE_METHODS)}"
220
+ )
221
+ # Validate linkage metric (allow "auto")
222
+ if linkage_metric != "auto" and linkage_metric not in LINKAGE_METRICS:
223
+ raise ValueError(
224
+ f"Invalid linkage_metric '{linkage_metric}'. Allowed values are 'auto' or one of: {sorted(LINKAGE_METRICS)}"
225
+ )
226
+ # Validate linkage threshold (allow "auto"; otherwise must be float in (0, 1])
227
+ if linkage_threshold != "auto":
228
+ try:
229
+ lt = float(linkage_threshold)
230
+ except (TypeError, ValueError):
231
+ raise ValueError("linkage_threshold must be 'auto' or a float in the interval (0, 1].")
232
+ if not (0.0 < lt <= 1.0):
233
+ raise ValueError(f"linkage_threshold must be within (0, 1]. Received: {lt}")
234
+
235
+ return False
236
+
237
+
187
238
  def _safeguard_matrix(matrix: np.ndarray) -> np.ndarray:
188
239
  """
189
240
  Safeguard the matrix by replacing NaN, Inf, and -Inf values.
@@ -394,34 +394,33 @@ def _prune_neighbors(
394
394
  # Identify indices with non-zero rows in the binary significance matrix
395
395
  non_zero_indices = np.where(significant_binary_significance_matrix.sum(axis=1) != 0)[0]
396
396
  median_distances = []
397
+ distance_lookup = {}
397
398
  for node in non_zero_indices:
398
- neighbors = [
399
- n
400
- for n in network.neighbors(node)
401
- if significant_binary_significance_matrix[n].sum() != 0
402
- ]
403
- if neighbors:
404
- median_distance = np.median(
405
- [_get_euclidean_distance(node, n, network) for n in neighbors]
406
- )
407
- median_distances.append(median_distance)
399
+ dist = _median_distance_to_significant_neighbors(
400
+ node, network, significant_binary_significance_matrix
401
+ )
402
+ if dist is not None:
403
+ median_distances.append(dist)
404
+ distance_lookup[node] = dist
405
+
406
+ if not median_distances:
407
+ logger.warning("No significant neighbors found for pruning.")
408
+ significant_significance_matrix = np.where(
409
+ significant_binary_significance_matrix == 1, significance_matrix, 0
410
+ )
411
+ return (
412
+ significance_matrix,
413
+ significant_binary_significance_matrix,
414
+ significant_significance_matrix,
415
+ )
408
416
 
409
417
  # Calculate the distance threshold value based on rank
410
418
  distance_threshold_value = _calculate_threshold(median_distances, 1 - distance_threshold)
411
419
  # Prune nodes that are outliers based on the distance threshold
412
- for row_index in non_zero_indices:
413
- neighbors = [
414
- n
415
- for n in network.neighbors(row_index)
416
- if significant_binary_significance_matrix[n].sum() != 0
417
- ]
418
- if neighbors:
419
- median_distance = np.median(
420
- [_get_euclidean_distance(row_index, n, network) for n in neighbors]
421
- )
422
- if median_distance >= distance_threshold_value:
423
- significance_matrix[row_index] = 0
424
- significant_binary_significance_matrix[row_index] = 0
420
+ for node, dist in distance_lookup.items():
421
+ if dist >= distance_threshold_value:
422
+ significance_matrix[node] = 0
423
+ significant_binary_significance_matrix[node] = 0
425
424
 
426
425
  # Create a matrix where non-significant entries are set to zero
427
426
  significant_significance_matrix = np.where(
@@ -435,6 +434,29 @@ def _prune_neighbors(
435
434
  )
436
435
 
437
436
 
437
+ def _median_distance_to_significant_neighbors(
438
+ node, network, significance_mask
439
+ ) -> Union[float, None]:
440
+ """
441
+ Calculate the median distance from a node to its significant neighbors.
442
+
443
+ Args:
444
+ node (Any): The node for which the median distance is being calculated.
445
+ network (nx.Graph): The network graph containing the nodes.
446
+ significance_mask (np.ndarray): Binary matrix indicating significant nodes.
447
+
448
+ Returns:
449
+ Union[float, None]: The median distance to significant neighbors, or None if no significant neighbors exist.
450
+ """
451
+ neighbors = [n for n in network.neighbors(node) if significance_mask[n].sum() != 0]
452
+ if not neighbors:
453
+ return None
454
+ # Calculate distances to significant neighbors
455
+ distances = [_get_euclidean_distance(node, n, network) for n in neighbors]
456
+
457
+ return np.median(distances)
458
+
459
+
438
460
  def _get_euclidean_distance(node1: Any, node2: Any, network: nx.Graph) -> float:
439
461
  """
440
462
  Calculate the Euclidean distance between two nodes in the network.
@@ -4,5 +4,5 @@ risk/_network
4
4
  """
5
5
 
6
6
  from ._graph import GraphAPI
7
- from ._io import NetworkIO
7
+ from ._io import NetworkAPI
8
8
  from ._plotter import PlotterAPI
@@ -27,9 +27,6 @@ class GraphAPI:
27
27
  The GraphAPI class provides methods to load and process network graphs, annotations, and neighborhoods.
28
28
  """
29
29
 
30
- def __init__(self) -> None:
31
- pass
32
-
33
30
  def load_graph(
34
31
  self,
35
32
  network: nx.Graph,
@@ -84,7 +84,7 @@ class Summary:
84
84
 
85
85
  Returns:
86
86
  pd.DataFrame: Processed DataFrame containing significance scores, p-values, q-values,
87
- and annotation member information.
87
+ and matched annotation members information.
88
88
  """
89
89
  log_header("Loading analysis summary")
90
90
  # Calculate significance and depletion q-values from p-value matrices in annotation
@@ -109,9 +109,9 @@ class Summary:
109
109
  # Add minimum p-values and q-values to DataFrame
110
110
  results[
111
111
  [
112
- "Enrichment P-Value",
112
+ "Enrichment P-value",
113
113
  "Enrichment Q-value",
114
- "Depletion P-Value",
114
+ "Depletion P-value",
115
115
  "Depletion Q-value",
116
116
  ]
117
117
  ] = results.apply(
@@ -126,13 +126,13 @@ class Summary:
126
126
  axis=1,
127
127
  result_type="expand",
128
128
  )
129
- # Add annotation members and their counts
130
- results["Annotation Members in Network"] = results["Annotation"].apply(
129
+ # Add matched annotation members and their counts
130
+ results["Matched Members"] = results["Annotation"].apply(
131
131
  lambda desc: self._get_annotation_members(desc)
132
132
  )
133
- results["Annotation Members in Network Count"] = results[
134
- "Annotation Members in Network"
135
- ].apply(lambda x: len(x.split(";")) if x else 0)
133
+ results["Matched Count"] = results["Matched Members"].apply(
134
+ lambda x: len(x.split(";")) if x else 0
135
+ )
136
136
 
137
137
  # Reorder columns and drop rows with NaN values
138
138
  results = (
@@ -140,12 +140,12 @@ class Summary:
140
140
  [
141
141
  "Domain ID",
142
142
  "Annotation",
143
- "Annotation Members in Network",
144
- "Annotation Members in Network Count",
143
+ "Matched Members",
144
+ "Matched Count",
145
145
  "Summed Significance Score",
146
- "Enrichment P-Value",
146
+ "Enrichment P-value",
147
147
  "Enrichment Q-value",
148
- "Depletion P-Value",
148
+ "Depletion P-value",
149
149
  "Depletion Q-value",
150
150
  ]
151
151
  ]
@@ -159,20 +159,18 @@ class Summary:
159
159
  results = pd.merge(ordered_annotation, results, on="Annotation", how="left").fillna(
160
160
  {
161
161
  "Domain ID": -1,
162
- "Annotation Members in Network": "",
163
- "Annotation Members in Network Count": 0,
162
+ "Matched Members": "",
163
+ "Matched Count": 0,
164
164
  "Summed Significance Score": 0.0,
165
- "Enrichment P-Value": 1.0,
165
+ "Enrichment P-value": 1.0,
166
166
  "Enrichment Q-value": 1.0,
167
- "Depletion P-Value": 1.0,
167
+ "Depletion P-value": 1.0,
168
168
  "Depletion Q-value": 1.0,
169
169
  }
170
170
  )
171
- # Convert "Domain ID" and "Annotation Members in Network Count" to integers
171
+ # Convert "Domain ID" and "Matched Count" to integers
172
172
  results["Domain ID"] = results["Domain ID"].astype(int)
173
- results["Annotation Members in Network Count"] = results[
174
- "Annotation Members in Network Count"
175
- ].astype(int)
173
+ results["Matched Count"] = results["Matched Count"].astype(int)
176
174
 
177
175
  return results
178
176