molprim
Molecular primitives: classify molecular graphs by their primitive
structures, that is, rings, branching points and bonds. The package rewrites
each molecule as a small graph whose vertices are these structures, then
compares molecules with graph kernels.
Everything is a scikit-learn transformer, so it plugs into Pipeline,
GridSearchCV and cross_val_score.
This is the implementation of:
P. Wongsriphisant, C. Lursinsap, A. Suratanee and K. Plaimas, "A classification of biochemical compounds based on their primitive structures and graph kernels", IEEE, 2020, pp. 104–109.
Install
pip install molprim
Requires Python ≥ 3.9 with networkx, numpy, scipy and scikit-learn. It needs neither graph-tool nor grakel.
Quickstart
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.svm import SVC
from molprim import GraphKernelTransformer, PrimitiveStructureExtractor, fetch_tudataset
graphs, y = fetch_tudataset("MUTAG") # list of networkx.Graph with a "label" node attribute
model = make_pipeline(
PrimitiveStructureExtractor(),
GraphKernelTransformer(kernel="wl_sp", n_iter=2),
SVC(kernel="precomputed"),
)
print(cross_val_score(model, graphs, y, cv=5).mean())
Inputs are lists of undirected networkx.Graph whose nodes carry the atom type
in a node attribute ("label" by default; change it with node_label=).
For a fuller example, see examples/quickstart.py.
How it works
- Selection (
PrimitiveStructureExtractor.fit): collects every labelled cycle (3–10 atoms), star (3–7 atoms) and bond that occurs in the training molecules, and keeps those whose frequency is at least thethresholdpercentile. Bonds are always kept. - Extraction (
PrimitiveStructureExtractor.transform): finds the primitives in each molecule, largest cycles first, then stars, then bonds. An occurrence that shares more than half of its atoms with one already taken is skipped. Each kept occurrence becomes a vertex labelled by its primitive (e.g."C6-1", the most frequent 6-ring). Two vertices are joined when their occurrences share an atom. - Similarity: either a graph kernel on the primitive graphs, or a count of adjacent primitive pairs fed to an RBF SVM.
| Component | Purpose |
|---|---|
PrimitiveStructureExtractor |
molecules → primitive graphs |
GraphKernelTransformer |
Weisfeiler-Lehman subtree ("wl_subtree"), WL shortest-path ("wl_sp") or shortest-path ("sp") kernel. fit_transform returns the training kernel matrix and transform the kernel against the training graphs, for SVC(kernel="precomputed"). Values match grakel's GraphKernel. |
LabelPairEdgeCounter |
counts edges per pair of end labels (the paper's "Edges" measure) |
fetch_tudataset, read_tudataset |
load TU Dortmund benchmark datasets (MUTAG, NCI1, BZR, COX2, …) as networkx graphs |
Tune the extraction like any other hyper-parameter, for example
GridSearchCV(model, {"primitivestructureextractor__threshold": [0, 50], "graphkerneltransformer__n_iter": [1, 2, 3]}).
Implementation notes
- Primitives are learned in
fit, from the training graphs only. - Candidate cycles, stars and bonds are enumerated directly and identified by a canonical form of their labels, rather than with general subgraph isomorphism. Extracting the primitive graphs of all 3,865 NCI1 molecules takes a few seconds.
- When occurrences overlap, the one kept is chosen deterministically: larger shapes first, then more frequent primitives, then vertex order.
Benchmarks
Scripts that evaluate the package on TU datasets (MUTAG, BZR, COX2, NCI1),
with their per-split results and runtimes, are in benchmarks/.
Repository layout
src/molprim/ the package
tests/ pytest suite
examples/ usage examples
benchmarks/ benchmark scripts and results of this implementation
legacy/ original notebooks and results from the 2020 paper (see legacy/README.md)
Development
pip install -e ".[test]"
pytest
License
BSD-3-Clause, the same as scikit-learn. See LICENSE.
Metadata
Release files for molprim 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| molprim-0.1.0.tar.gz | 17.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| molprim-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.4 kB
Release files / molprim-0.1.0.tar.gz
| Download URL | molprim-0.1.0.tar.gz |
|---|---|
| Size | 17.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
949bc36ab507a868cfc7e636bbf81301331a9ddf6509ae637cbf799639442727
|
|
BLAKE2b-256 checksum How to use checksums |
a585b1899d7cf786ab0af878dcf7e613c9813cf32d44b6770a32c01ef26ef044
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 5, 2026.
Transparency logRelease files / molprim-0.1.0-py3-none-any.whl
| Download URL | molprim-0.1.0-py3-none-any.whl |
|---|---|
| Size | 14.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0a9227eb926aa397e8b7381975d00f79da9a24be2c5911b7eb1f79596301c6ca
|
|
BLAKE2b-256 checksum How to use checksums |
cb9692c439810d221e7c351a90665f9bdbcfcf7df00da77f88087342307cac24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 5, 2026.
Transparency log