pynem 0.6.0__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.
- pynem/__init__.py +11 -0
- pynem/_fast.py +96 -0
- pynem/core.py +733 -0
- pynem/io.py +153 -0
- pynem/metrics.py +65 -0
- pynem/models.py +443 -0
- pynem/ppanggolin.py +287 -0
- pynem/spatial.py +99 -0
- pynem/viz.py +357 -0
- pynem/weights.py +220 -0
- pynem-0.6.0.dist-info/METADATA +152 -0
- pynem-0.6.0.dist-info/RECORD +15 -0
- pynem-0.6.0.dist-info/WHEEL +5 -0
- pynem-0.6.0.dist-info/licenses/LICENSE +21 -0
- pynem-0.6.0.dist-info/top_level.txt +1 -0
pynem/__init__.py
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"]
|
pynem/_fast.py
ADDED
|
@@ -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
|