OPFython
OPFython is a Python implementation of the Optimum-Path Forest family of classifiers. It provides supervised, semi-supervised, unsupervised, and KNN-supervised models backed by NumPy and Numba.
This implementation follows LibOPF. Please cite the original LibOPF authors as well as OPFython when using it in research.
OPFython requires Python 3.11 or newer.
Installation
uv add opfython
Quick start
import numpy as np
from opfython.models import SupervisedOPF
X_train = np.asarray([[0.0, 0.0], [0.1, 0.2], [1.0, 1.0], [1.1, 0.9]])
Y_train = np.asarray([0, 0, 1, 1])
X_test = np.asarray([[0.05, 0.1], [1.05, 1.0]])
classifier = SupervisedOPF()
classifier.fit(X_train, Y_train)
predictions = classifier.predict(X_test)
Labels must be zero-based and sequential. Pre-computed distance matrices can
be supplied through each classifier's pre_computed_distance constructor
argument.
Features, labels, and optional sample indexes must have matching sample
counts; mismatches raise opfython.utils.exception.SizeError. Supervised
and semi-supervised models also support a single labeled class. KNN models
require 1 <= k < number of training samples.
When splitting a pre-computed matrix's dataset, retain its original sample
indexes with stream.splitter.split_with_index and pass the corresponding
indexes to fit and predict. The matrix must cover those indexes, not
merely match the training subset's size. See the
pre-computed distance example.
Classifiers
| Class | Purpose |
|---|---|
SupervisedOPF |
Complete-graph supervised classification |
KNNSupervisedOPF |
Supervised classification with learned KNN adjacency |
SemiSupervisedOPF |
Learning from labeled and unlabeled samples |
UnsupervisedOPF |
Density-based clustering and label propagation |
The package also includes 47 distance metrics, random generators, OPF evaluation measures, dataset loaders and splitters, package logging and exception helpers, and converters for LibOPF binary datasets.
Clustering purity is independent of cluster numbering and supports more clusters than true classes. Evaluation measures require one prediction for each true label and handle class IDs missing from an evaluation split.
See the documentation and the
examples/applications directory for complete
workflows.
Development
uv sync --all-groups
uv run pytest
uv run pre-commit run --all-files
uv run --group docs sphinx-build -W -b html docs docs/_build/html
uv build --no-sources
Citation
@article{rosa2021simpa,
title = {OPFython: A Python implementation for Optimum-Path Forest},
author = {Gustavo H. {de Rosa} and Joao P. Papa},
journal = {Software Impacts},
pages = {100113},
year = {2021},
issn = {2665-9638},
doi = {https://doi.org/10.1016/j.simpa.2021.100113}
}
@misc{rosa2021speedup,
title = {Speeding Up OPFython with Numba},
author = {Gustavo H. de Rosa and Joao Paulo Papa},
year = {2021},
eprint = {2106.11828},
archivePrefix = {arXiv},
primaryClass = {cs.LG}
}
OPFython is licensed under the Apache License 2.0.
Release files for opfython 2.0.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 | |
|---|---|---|---|
| opfython-2.0.1.tar.gz | 28.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| opfython-2.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 65.7 kB
Release files / opfython-2.0.1.tar.gz
| Download URL | opfython-2.0.1.tar.gz |
|---|---|
| Size | 28.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a331d4752ba9d496acb776394abba47106e22b1bc6206aae2b9af1319be9212a
|
|
BLAKE2b-256 checksum How to use checksums |
1c18ba886362bda69f4f05754294169160f92833409734f4bac9ba61543cd56b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / opfython-2.0.1-py3-none-any.whl
| Download URL | opfython-2.0.1-py3-none-any.whl |
|---|---|
| Size | 37.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
54c70e5dd1587c7c406aa758a747174ff7d7da9e2fa54148010f6ef5f3840605
|
|
BLAKE2b-256 checksum How to use checksums |
210b796f006d46c913d4d47cd9c38a07f5cec864304487999332a91dd874751c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|