pynem — Neighborhood EM for spatial clustering on graphs
pynem is a standalone, pure-Python implementation of the Neighborhood EM
(NEM) algorithm: clustering that combines the EM algorithm with a hidden
Markov random field. Given a graph whose nodes carry feature vectors, NEM
produces a partition that accounts for both the data and the spatial structure
of the graph. It has a scikit-learn-style API and uses NumPy/SciPy, NetworkX and
(optionally) Numba.
It also faithfully reproduces the partitioning step of PPanGGOLiN — the persistent / shell / cloud partitioning of bacterial pangenomes is exactly this NEM algorithm — and adds two original extensions: per-variable weighting (correcting genome redundancy) and a MAG-aware completeness model (correcting genome incompleteness).
Installation
pip install pynem
Optional extras:
pynem[viz]— matplotlib for the plotting utilities (pynem.viz). Without it,import pynemand all clustering still work; only plotting needs it.pynem[fast]— Numba (JIT acceleration of the sequential E-step; a pure-Python fallback is used otherwise).pynem[dev]— the test suite.
(They combine: pip install "pynem[fast,viz]".)
Quick start
import numpy as np
import networkx as nx
from pynem import NEM
# A graph whose nodes carry feature vectors, plus a feature matrix X (N, D)
G = nx.path_graph(100)
X = np.random.default_rng(0).normal(size=(100, 2))
model = NEM(n_clusters=3, beta=1.0, family="normal")
model.fit(X, graph=G)
model.labels_ # hard classification (N,)
model.membership_ # soft classification (N, K)
model.centers_ # cluster centers (K, D)
model.criteria_ # dict with U, D, G, L, M
Pangenome partitioning (PPanGGOLiN-faithful)
from pynem import partition_pangenome
# presence: (n_families, n_genomes) array of {0, 1}; graph: contiguity nx.Graph
res = partition_pangenome(presence, graph, K=3, beta=2.5)
res["partition"] # array of "P" / "S" / "C" per gene family
On PPanGGOLiN's real Chlamydia test set (53 genomes, 1086 gene families),
partition_pangenome reproduces PPanGGOLiN exactly — identical
persistent / shell / cloud composition (871 / 56 / 159, agreement 1.000) and
soft membership matching the reference C core to ~5e-4 — while running a bit
faster (it stays in memory).
Original extensions
- Weighted NEM —
feature_weights(per variable) /genome_weights(per genome) down-weight redundant features so the partition reflects biology, not sampling.pynem.genome_weights(...)derives them automatically (Jaccard + UPGMA, silhouette). - MAG-aware completeness —
completeness=(per genome, from CheckM or"auto"self-estimation) forgives absences in incomplete metagenome-assembled genomes, restoring a persistent class that the standard model would collapse.
Both are opt-in: with the defaults, pynem reproduces standard NEM /
PPanGGOLiN byte-for-byte.
Features
| Feature | Options |
|---|---|
| Algorithm | nem (mean field), ncem (ICM), gem (Gibbs) |
| Distributions | normal, laplace, bernoulli |
| Dispersion | s__, sk_, s_d, skd |
| Proportions | p_ (equal), pk (free) |
| Beta | fixed, pseudo-gradient, heuristics |
| Init | sort, random, param |
| Site update | parallel (Jacobi), seq (Gauss-Seidel) |
| Feature weights | per-variable feature_weights (weighted NEM) |
| Completeness (MAG) | per-genome completeness (CheckM or "auto") |
Documentation & source
- Documentation: https://pynem.readthedocs.io
- Source, examples and the C reference implementation: https://github.com/cambroise/nem
Citation
If you use pynem, please cite it (metadata in
CITATION.cff):
Ambroise, C. pynem: Neighborhood EM for spatial clustering on graphs. Zenodo. https://doi.org/10.5281/zenodo.20627688
The concept DOI above always resolves to the latest version; each release also gets its own version DOI.
References
- Ambroise, C., Dang, V. M., & Govaert, G. (1997). Clustering of spatial data by the EM algorithm. geoENV I — Geostatistics for Environmental Applications.
- Ambroise, C., & Govaert, G. (1998). Convergence proof of an EM-type algorithm for spatial clustering. Pattern Recognition Letters.
License
MIT
Metadata
Release files for pynem 0.6.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pynem-0.6.1.tar.gz | 49.3 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pynem-0.6.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 85.9 kB
Release files / pynem-0.6.1.tar.gz
| Download URL | pynem-0.6.1.tar.gz |
|---|---|
| Size | 49.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4d1232a7a33b2890105d880336590bd7f24102c4f55a05fb13234a4d31cd0128
|
|
BLAKE2b-256 checksum How to use checksums |
4c9d9bd040748ae0f45d589e4d822a541a5b32504e6f163fb3e3f5324e95ea97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logRelease files / pynem-0.6.1-py3-none-any.whl
| Download URL | pynem-0.6.1-py3-none-any.whl |
|---|---|
| Size | 36.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4be767f88def1ab986c539162072423155514e6b744e8d750726dc119152f904
|
|
BLAKE2b-256 checksum How to use checksums |
aac64bbfc5c7e15d59f24ac2a7e4a52eb7f5e5427c57ccb84e574a1dd959e638
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logSecurity for pynem 0.6.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for pynem-0.6.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.