pynem 0.6.0__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.
- pynem-0.6.0/LICENSE +21 -0
- pynem-0.6.0/PKG-INFO +152 -0
- pynem-0.6.0/README.md +115 -0
- pynem-0.6.0/pyproject.toml +67 -0
- pynem-0.6.0/setup.cfg +4 -0
- pynem-0.6.0/src/pynem/__init__.py +11 -0
- pynem-0.6.0/src/pynem/_fast.py +96 -0
- pynem-0.6.0/src/pynem/core.py +733 -0
- pynem-0.6.0/src/pynem/io.py +153 -0
- pynem-0.6.0/src/pynem/metrics.py +65 -0
- pynem-0.6.0/src/pynem/models.py +443 -0
- pynem-0.6.0/src/pynem/ppanggolin.py +287 -0
- pynem-0.6.0/src/pynem/spatial.py +99 -0
- pynem-0.6.0/src/pynem/viz.py +357 -0
- pynem-0.6.0/src/pynem/weights.py +220 -0
- pynem-0.6.0/src/pynem.egg-info/PKG-INFO +152 -0
- pynem-0.6.0/src/pynem.egg-info/SOURCES.txt +26 -0
- pynem-0.6.0/src/pynem.egg-info/dependency_links.txt +1 -0
- pynem-0.6.0/src/pynem.egg-info/requires.txt +14 -0
- pynem-0.6.0/src/pynem.egg-info/top_level.txt +1 -0
- pynem-0.6.0/tests/test_api.py +116 -0
- pynem-0.6.0/tests/test_degenerate.py +87 -0
- pynem-0.6.0/tests/test_fast.py +76 -0
- pynem-0.6.0/tests/test_mag_bias.py +148 -0
- pynem-0.6.0/tests/test_nem.py +379 -0
- pynem-0.6.0/tests/test_ppanggolin.py +214 -0
- pynem-0.6.0/tests/test_weighted.py +193 -0
- pynem-0.6.0/tests/test_weights.py +166 -0
pynem-0.6.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Christophe Ambroise
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pynem-0.6.0/PKG-INFO
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pynem
|
|
3
|
+
Version: 0.6.0
|
|
4
|
+
Summary: Neighborhood EM (NEM): spatial clustering on graphs with a hidden Markov random field
|
|
5
|
+
Author-email: Christophe Ambroise <christophe.ambroise@univ-evry.fr>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/cambroise/nem
|
|
8
|
+
Project-URL: Repository, https://github.com/cambroise/nem
|
|
9
|
+
Project-URL: Issues, https://github.com/cambroise/nem/issues
|
|
10
|
+
Keywords: clustering,expectation-maximization,hidden Markov random field,spatial clustering,Bernoulli mixture,pangenome,PPanGGOLiN,neighborhood EM
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
21
|
+
Classifier: Operating System :: OS Independent
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: numpy>=1.22
|
|
26
|
+
Requires-Dist: scipy>=1.8
|
|
27
|
+
Requires-Dist: networkx>=2.8
|
|
28
|
+
Provides-Extra: viz
|
|
29
|
+
Requires-Dist: matplotlib>=3.5; extra == "viz"
|
|
30
|
+
Provides-Extra: fast
|
|
31
|
+
Requires-Dist: numba>=0.57; extra == "fast"
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
34
|
+
Requires-Dist: ruff>=0.6; extra == "dev"
|
|
35
|
+
Requires-Dist: matplotlib>=3.5; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# pynem — Neighborhood EM for spatial clustering on graphs
|
|
39
|
+
|
|
40
|
+
[](https://github.com/cambroise/nem/actions/workflows/tests.yml)
|
|
41
|
+
[](https://www.python.org)
|
|
42
|
+
[](https://github.com/cambroise/nem/blob/main/pynem/LICENSE)
|
|
43
|
+
|
|
44
|
+
`pynem` is a standalone, pure-Python implementation of the **Neighborhood EM
|
|
45
|
+
(NEM)** algorithm: clustering that combines the EM algorithm with a **hidden
|
|
46
|
+
Markov random field**. Given a graph whose nodes carry feature vectors, NEM
|
|
47
|
+
produces a partition that accounts for both the data and the spatial structure
|
|
48
|
+
of the graph. It has a scikit-learn-style API and uses NumPy/SciPy, NetworkX and
|
|
49
|
+
(optionally) Numba.
|
|
50
|
+
|
|
51
|
+
It also faithfully reproduces the partitioning step of
|
|
52
|
+
[PPanGGOLiN](https://github.com/labgem/PPanGGOLiN) — the persistent / shell /
|
|
53
|
+
cloud partitioning of bacterial pangenomes is exactly this NEM algorithm — and
|
|
54
|
+
adds two original extensions: **per-variable weighting** (correcting genome
|
|
55
|
+
redundancy) and a **MAG-aware completeness** model (correcting genome
|
|
56
|
+
incompleteness).
|
|
57
|
+
|
|
58
|
+
## Installation
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install pynem
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Optional extras:
|
|
65
|
+
- `pynem[viz]` — **matplotlib** for the plotting utilities (`pynem.viz`).
|
|
66
|
+
Without it, `import pynem` and all clustering still work; only plotting needs it.
|
|
67
|
+
- `pynem[fast]` — **Numba** (JIT acceleration of the sequential E-step; a
|
|
68
|
+
pure-Python fallback is used otherwise).
|
|
69
|
+
- `pynem[dev]` — the test suite.
|
|
70
|
+
|
|
71
|
+
(They combine: `pip install "pynem[fast,viz]"`.)
|
|
72
|
+
|
|
73
|
+
## Quick start
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
import numpy as np
|
|
77
|
+
import networkx as nx
|
|
78
|
+
from pynem import NEM
|
|
79
|
+
|
|
80
|
+
# A graph whose nodes carry feature vectors, plus a feature matrix X (N, D)
|
|
81
|
+
G = nx.path_graph(100)
|
|
82
|
+
X = np.random.default_rng(0).normal(size=(100, 2))
|
|
83
|
+
|
|
84
|
+
model = NEM(n_clusters=3, beta=1.0, family="normal")
|
|
85
|
+
model.fit(X, graph=G)
|
|
86
|
+
|
|
87
|
+
model.labels_ # hard classification (N,)
|
|
88
|
+
model.membership_ # soft classification (N, K)
|
|
89
|
+
model.centers_ # cluster centers (K, D)
|
|
90
|
+
model.criteria_ # dict with U, D, G, L, M
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+

|
|
94
|
+
|
|
95
|
+
## Pangenome partitioning (PPanGGOLiN-faithful)
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from pynem import partition_pangenome
|
|
99
|
+
|
|
100
|
+
# presence: (n_families, n_genomes) array of {0, 1}; graph: contiguity nx.Graph
|
|
101
|
+
res = partition_pangenome(presence, graph, K=3, beta=2.5)
|
|
102
|
+
res["partition"] # array of "P" / "S" / "C" per gene family
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
On PPanGGOLiN's real *Chlamydia* test set (53 genomes, 1086 gene families),
|
|
106
|
+
`partition_pangenome` reproduces PPanGGOLiN **exactly** — identical
|
|
107
|
+
persistent / shell / cloud composition (871 / 56 / 159, agreement 1.000) and
|
|
108
|
+
soft membership matching the reference C core to ~5e-4 — while running a bit
|
|
109
|
+
faster (it stays in memory).
|
|
110
|
+
|
|
111
|
+
## Original extensions
|
|
112
|
+
|
|
113
|
+
- **Weighted NEM** — `feature_weights` (per variable) / `genome_weights` (per
|
|
114
|
+
genome) down-weight redundant features so the partition reflects biology, not
|
|
115
|
+
sampling. `pynem.genome_weights(...)` derives them automatically (Jaccard +
|
|
116
|
+
UPGMA, silhouette).
|
|
117
|
+
- **MAG-aware completeness** — `completeness=` (per genome, from CheckM or
|
|
118
|
+
`"auto"` self-estimation) forgives absences in incomplete metagenome-assembled
|
|
119
|
+
genomes, restoring a persistent class that the standard model would collapse.
|
|
120
|
+
|
|
121
|
+
Both are **opt-in**: with the defaults, `pynem` reproduces standard NEM /
|
|
122
|
+
PPanGGOLiN byte-for-byte.
|
|
123
|
+
|
|
124
|
+
## Features
|
|
125
|
+
|
|
126
|
+
| Feature | Options |
|
|
127
|
+
|---|---|
|
|
128
|
+
| Algorithm | `nem` (mean field), `ncem` (ICM), `gem` (Gibbs) |
|
|
129
|
+
| Distributions | `normal`, `laplace`, `bernoulli` |
|
|
130
|
+
| Dispersion | `s__`, `sk_`, `s_d`, `skd` |
|
|
131
|
+
| Proportions | `p_` (equal), `pk` (free) |
|
|
132
|
+
| Beta | fixed, pseudo-gradient, heuristics |
|
|
133
|
+
| Init | `sort`, `random`, `param` |
|
|
134
|
+
| Site update | `parallel` (Jacobi), `seq` (Gauss-Seidel) |
|
|
135
|
+
| Feature weights | per-variable `feature_weights` (weighted NEM) |
|
|
136
|
+
| Completeness (MAG) | per-genome `completeness` (CheckM or `"auto"`) |
|
|
137
|
+
|
|
138
|
+
## Documentation & source
|
|
139
|
+
|
|
140
|
+
Full documentation, examples and the C reference implementation:
|
|
141
|
+
**https://github.com/cambroise/nem**
|
|
142
|
+
|
|
143
|
+
## References
|
|
144
|
+
|
|
145
|
+
- Ambroise, C., Dang, V. M., & Govaert, G. (1997). *Clustering of spatial data by
|
|
146
|
+
the EM algorithm.* geoENV I — Geostatistics for Environmental Applications.
|
|
147
|
+
- Ambroise, C., & Govaert, G. (1998). *Convergence proof of an EM-type algorithm
|
|
148
|
+
for spatial clustering.* Pattern Recognition Letters.
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT
|
pynem-0.6.0/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# pynem — Neighborhood EM for spatial clustering on graphs
|
|
2
|
+
|
|
3
|
+
[](https://github.com/cambroise/nem/actions/workflows/tests.yml)
|
|
4
|
+
[](https://www.python.org)
|
|
5
|
+
[](https://github.com/cambroise/nem/blob/main/pynem/LICENSE)
|
|
6
|
+
|
|
7
|
+
`pynem` is a standalone, pure-Python implementation of the **Neighborhood EM
|
|
8
|
+
(NEM)** algorithm: clustering that combines the EM algorithm with a **hidden
|
|
9
|
+
Markov random field**. Given a graph whose nodes carry feature vectors, NEM
|
|
10
|
+
produces a partition that accounts for both the data and the spatial structure
|
|
11
|
+
of the graph. It has a scikit-learn-style API and uses NumPy/SciPy, NetworkX and
|
|
12
|
+
(optionally) Numba.
|
|
13
|
+
|
|
14
|
+
It also faithfully reproduces the partitioning step of
|
|
15
|
+
[PPanGGOLiN](https://github.com/labgem/PPanGGOLiN) — the persistent / shell /
|
|
16
|
+
cloud partitioning of bacterial pangenomes is exactly this NEM algorithm — and
|
|
17
|
+
adds two original extensions: **per-variable weighting** (correcting genome
|
|
18
|
+
redundancy) and a **MAG-aware completeness** model (correcting genome
|
|
19
|
+
incompleteness).
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install pynem
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Optional extras:
|
|
28
|
+
- `pynem[viz]` — **matplotlib** for the plotting utilities (`pynem.viz`).
|
|
29
|
+
Without it, `import pynem` and all clustering still work; only plotting needs it.
|
|
30
|
+
- `pynem[fast]` — **Numba** (JIT acceleration of the sequential E-step; a
|
|
31
|
+
pure-Python fallback is used otherwise).
|
|
32
|
+
- `pynem[dev]` — the test suite.
|
|
33
|
+
|
|
34
|
+
(They combine: `pip install "pynem[fast,viz]"`.)
|
|
35
|
+
|
|
36
|
+
## Quick start
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
import numpy as np
|
|
40
|
+
import networkx as nx
|
|
41
|
+
from pynem import NEM
|
|
42
|
+
|
|
43
|
+
# A graph whose nodes carry feature vectors, plus a feature matrix X (N, D)
|
|
44
|
+
G = nx.path_graph(100)
|
|
45
|
+
X = np.random.default_rng(0).normal(size=(100, 2))
|
|
46
|
+
|
|
47
|
+
model = NEM(n_clusters=3, beta=1.0, family="normal")
|
|
48
|
+
model.fit(X, graph=G)
|
|
49
|
+
|
|
50
|
+
model.labels_ # hard classification (N,)
|
|
51
|
+
model.membership_ # soft classification (N, K)
|
|
52
|
+
model.centers_ # cluster centers (K, D)
|
|
53
|
+
model.criteria_ # dict with U, D, G, L, M
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+

|
|
57
|
+
|
|
58
|
+
## Pangenome partitioning (PPanGGOLiN-faithful)
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from pynem import partition_pangenome
|
|
62
|
+
|
|
63
|
+
# presence: (n_families, n_genomes) array of {0, 1}; graph: contiguity nx.Graph
|
|
64
|
+
res = partition_pangenome(presence, graph, K=3, beta=2.5)
|
|
65
|
+
res["partition"] # array of "P" / "S" / "C" per gene family
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
On PPanGGOLiN's real *Chlamydia* test set (53 genomes, 1086 gene families),
|
|
69
|
+
`partition_pangenome` reproduces PPanGGOLiN **exactly** — identical
|
|
70
|
+
persistent / shell / cloud composition (871 / 56 / 159, agreement 1.000) and
|
|
71
|
+
soft membership matching the reference C core to ~5e-4 — while running a bit
|
|
72
|
+
faster (it stays in memory).
|
|
73
|
+
|
|
74
|
+
## Original extensions
|
|
75
|
+
|
|
76
|
+
- **Weighted NEM** — `feature_weights` (per variable) / `genome_weights` (per
|
|
77
|
+
genome) down-weight redundant features so the partition reflects biology, not
|
|
78
|
+
sampling. `pynem.genome_weights(...)` derives them automatically (Jaccard +
|
|
79
|
+
UPGMA, silhouette).
|
|
80
|
+
- **MAG-aware completeness** — `completeness=` (per genome, from CheckM or
|
|
81
|
+
`"auto"` self-estimation) forgives absences in incomplete metagenome-assembled
|
|
82
|
+
genomes, restoring a persistent class that the standard model would collapse.
|
|
83
|
+
|
|
84
|
+
Both are **opt-in**: with the defaults, `pynem` reproduces standard NEM /
|
|
85
|
+
PPanGGOLiN byte-for-byte.
|
|
86
|
+
|
|
87
|
+
## Features
|
|
88
|
+
|
|
89
|
+
| Feature | Options |
|
|
90
|
+
|---|---|
|
|
91
|
+
| Algorithm | `nem` (mean field), `ncem` (ICM), `gem` (Gibbs) |
|
|
92
|
+
| Distributions | `normal`, `laplace`, `bernoulli` |
|
|
93
|
+
| Dispersion | `s__`, `sk_`, `s_d`, `skd` |
|
|
94
|
+
| Proportions | `p_` (equal), `pk` (free) |
|
|
95
|
+
| Beta | fixed, pseudo-gradient, heuristics |
|
|
96
|
+
| Init | `sort`, `random`, `param` |
|
|
97
|
+
| Site update | `parallel` (Jacobi), `seq` (Gauss-Seidel) |
|
|
98
|
+
| Feature weights | per-variable `feature_weights` (weighted NEM) |
|
|
99
|
+
| Completeness (MAG) | per-genome `completeness` (CheckM or `"auto"`) |
|
|
100
|
+
|
|
101
|
+
## Documentation & source
|
|
102
|
+
|
|
103
|
+
Full documentation, examples and the C reference implementation:
|
|
104
|
+
**https://github.com/cambroise/nem**
|
|
105
|
+
|
|
106
|
+
## References
|
|
107
|
+
|
|
108
|
+
- Ambroise, C., Dang, V. M., & Govaert, G. (1997). *Clustering of spatial data by
|
|
109
|
+
the EM algorithm.* geoENV I — Geostatistics for Environmental Applications.
|
|
110
|
+
- Ambroise, C., & Govaert, G. (1998). *Convergence proof of an EM-type algorithm
|
|
111
|
+
for spatial clustering.* Pattern Recognition Letters.
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pynem"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Neighborhood EM (NEM): spatial clustering on graphs with a hidden Markov random field"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "Christophe Ambroise", email = "christophe.ambroise@univ-evry.fr" },
|
|
15
|
+
]
|
|
16
|
+
keywords = [
|
|
17
|
+
"clustering",
|
|
18
|
+
"expectation-maximization",
|
|
19
|
+
"hidden Markov random field",
|
|
20
|
+
"spatial clustering",
|
|
21
|
+
"Bernoulli mixture",
|
|
22
|
+
"pangenome",
|
|
23
|
+
"PPanGGOLiN",
|
|
24
|
+
"neighborhood EM",
|
|
25
|
+
]
|
|
26
|
+
classifiers = [
|
|
27
|
+
"Development Status :: 4 - Beta",
|
|
28
|
+
"Intended Audience :: Science/Research",
|
|
29
|
+
"Programming Language :: Python :: 3",
|
|
30
|
+
"Programming Language :: Python :: 3.9",
|
|
31
|
+
"Programming Language :: Python :: 3.10",
|
|
32
|
+
"Programming Language :: Python :: 3.11",
|
|
33
|
+
"Programming Language :: Python :: 3.12",
|
|
34
|
+
"Programming Language :: Python :: 3.13",
|
|
35
|
+
"Topic :: Scientific/Engineering :: Bio-Informatics",
|
|
36
|
+
"Topic :: Scientific/Engineering :: Mathematics",
|
|
37
|
+
"Operating System :: OS Independent",
|
|
38
|
+
]
|
|
39
|
+
dependencies = [
|
|
40
|
+
"numpy>=1.22",
|
|
41
|
+
"scipy>=1.8",
|
|
42
|
+
"networkx>=2.8",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[project.optional-dependencies]
|
|
46
|
+
viz = ["matplotlib>=3.5"] # plotting (pynem.viz); optional
|
|
47
|
+
fast = ["numba>=0.57"] # JIT of the sequential E-step; pure-Python fallback otherwise
|
|
48
|
+
dev = ["pytest>=7.0", "ruff>=0.6", "matplotlib>=3.5"] # tests exercise pynem.viz
|
|
49
|
+
|
|
50
|
+
[project.urls]
|
|
51
|
+
Homepage = "https://github.com/cambroise/nem"
|
|
52
|
+
Repository = "https://github.com/cambroise/nem"
|
|
53
|
+
Issues = "https://github.com/cambroise/nem/issues"
|
|
54
|
+
|
|
55
|
+
[tool.setuptools.dynamic]
|
|
56
|
+
version = { attr = "pynem.__version__" }
|
|
57
|
+
|
|
58
|
+
[tool.setuptools.packages.find]
|
|
59
|
+
where = ["src"]
|
|
60
|
+
|
|
61
|
+
[tool.ruff]
|
|
62
|
+
line-length = 100
|
|
63
|
+
target-version = "py39"
|
|
64
|
+
|
|
65
|
+
[tool.ruff.lint]
|
|
66
|
+
# E/W pycodestyle, F pyflakes, I import sorting. Keep it focused; widen later.
|
|
67
|
+
select = ["E", "F", "W", "I"]
|
pynem-0.6.0/setup.cfg
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""pynem — Neighborhood EM for spatial clustering on graphs."""
|
|
2
|
+
|
|
3
|
+
from . import io, metrics, models, ppanggolin, spatial, viz, weights
|
|
4
|
+
from .core import NEM
|
|
5
|
+
from .ppanggolin import partition_pangenome
|
|
6
|
+
from .weights import abundance_weights, genome_weights, jaccard_upgma_labels
|
|
7
|
+
|
|
8
|
+
__version__ = "0.6.0"
|
|
9
|
+
__all__ = ["NEM", "io", "viz", "models", "spatial", "metrics",
|
|
10
|
+
"ppanggolin", "partition_pangenome", "weights",
|
|
11
|
+
"genome_weights", "jaccard_upgma_labels", "abundance_weights"]
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Optional Numba-accelerated kernels.
|
|
2
|
+
|
|
3
|
+
The sequential (Gauss-Seidel) E-step cannot be vectorised across nodes — each
|
|
4
|
+
node reads the already-updated memberships of its earlier neighbours. This loop
|
|
5
|
+
is JIT-compiled with Numba when available; otherwise the pure-Python loop in
|
|
6
|
+
``core.py`` is used. The two paths are numerically identical (float64, same
|
|
7
|
+
operations and visit order), so PPanGGOLiN reproduction is unaffected.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
from numba import njit
|
|
14
|
+
HAS_NUMBA = True
|
|
15
|
+
except Exception: # pragma: no cover - numba is an optional dependency
|
|
16
|
+
HAS_NUMBA = False
|
|
17
|
+
|
|
18
|
+
def njit(*args, **kwargs):
|
|
19
|
+
"""No-op fallback so the module imports without numba."""
|
|
20
|
+
if args and callable(args[0]):
|
|
21
|
+
return args[0]
|
|
22
|
+
|
|
23
|
+
def wrap(func):
|
|
24
|
+
return func
|
|
25
|
+
|
|
26
|
+
return wrap
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@njit(cache=True)
|
|
30
|
+
def seq_sweep(CM, log_pkfki, indptr, indices, data, beta, harden):
|
|
31
|
+
"""One in-place sequential (Gauss-Seidel) E-step sweep.
|
|
32
|
+
|
|
33
|
+
Parameters
|
|
34
|
+
----------
|
|
35
|
+
CM : (N, K) float64 — classification, updated in place and returned.
|
|
36
|
+
log_pkfki : (N, K) float64 — precomputed log(p_k f_k(x_i)).
|
|
37
|
+
indptr, indices, data : CSR arrays of the (directed) weighted adjacency.
|
|
38
|
+
beta : float — spatial smoothing strength.
|
|
39
|
+
harden : bool — if True apply the C-step (argmax -> one-hot), i.e. NCEM.
|
|
40
|
+
|
|
41
|
+
Mirrors ``NEM._normalize_local`` / ``_harden_node`` for the mean-field and
|
|
42
|
+
ICM (ncem) variants. Gibbs (gem) needs an RNG and stays on the Python path.
|
|
43
|
+
"""
|
|
44
|
+
N = CM.shape[0]
|
|
45
|
+
K = CM.shape[1]
|
|
46
|
+
ctx = np.empty(K)
|
|
47
|
+
lognum = np.empty(K)
|
|
48
|
+
|
|
49
|
+
for i in range(N):
|
|
50
|
+
# spatial context: sum_{j in N(i)} w_ij * CM[j]
|
|
51
|
+
for k in range(K):
|
|
52
|
+
ctx[k] = 0.0
|
|
53
|
+
for p in range(indptr[i], indptr[i + 1]):
|
|
54
|
+
j = indices[p]
|
|
55
|
+
w = data[p]
|
|
56
|
+
for k in range(K):
|
|
57
|
+
ctx[k] += w * CM[j, k]
|
|
58
|
+
|
|
59
|
+
# log numerator and its max
|
|
60
|
+
maxlog = -np.inf
|
|
61
|
+
for k in range(K):
|
|
62
|
+
val = log_pkfki[i, k] + beta * ctx[k]
|
|
63
|
+
lognum[k] = val
|
|
64
|
+
if val > maxlog:
|
|
65
|
+
maxlog = val
|
|
66
|
+
|
|
67
|
+
if not np.isfinite(maxlog):
|
|
68
|
+
for k in range(K):
|
|
69
|
+
CM[i, k] = 1.0 / K
|
|
70
|
+
continue
|
|
71
|
+
|
|
72
|
+
total = 0.0
|
|
73
|
+
for k in range(K):
|
|
74
|
+
e = np.exp(lognum[k] - maxlog)
|
|
75
|
+
lognum[k] = e
|
|
76
|
+
total += e
|
|
77
|
+
|
|
78
|
+
if total <= 0.0:
|
|
79
|
+
for k in range(K):
|
|
80
|
+
CM[i, k] = 1.0 / K
|
|
81
|
+
continue
|
|
82
|
+
|
|
83
|
+
if harden:
|
|
84
|
+
best = 0
|
|
85
|
+
bestval = lognum[0]
|
|
86
|
+
for k in range(1, K):
|
|
87
|
+
if lognum[k] > bestval:
|
|
88
|
+
bestval = lognum[k]
|
|
89
|
+
best = k
|
|
90
|
+
for k in range(K):
|
|
91
|
+
CM[i, k] = 1.0 if k == best else 0.0
|
|
92
|
+
else:
|
|
93
|
+
for k in range(K):
|
|
94
|
+
CM[i, k] = lognum[k] / total
|
|
95
|
+
|
|
96
|
+
return CM
|