biocimpute
A Python port of the Bioconductor package impute (version 1.86.0): k-nearest-neighbor imputation of missing values in gene expression matrices, with recursive two-means clustering for large matrices.
Results match R's impute.knn exactly, including the random two-means split
(R's Mersenne-Twister set.seed()/sample() are reimplemented). The test
suite checks every imputed value against fixtures generated by the R package.
Installation
uv add biocimpute # or: pip install biocimpute
uv add "biocimpute[pandas]" # DataFrame support and the khanmiss dataset
Usage
import numpy as np
from biocimpute import impute_knn, load_khanmiss
# Same as R: khan.expr <- khanmiss[-1, -(1:2)]; impute.knn(as.matrix(khan.expr))
khan_expr = load_khanmiss().iloc[1:, 2:].to_numpy(dtype=float)
result = impute_knn(khan_expr)
result.data # imputed matrix
result.rng_seed # 362436069
impute_knn(data, k=10, rowmax=0.5, colmax=0.8, maxp=1500, rng_seed=362436069)
takes the same arguments as impute.knn (genes in rows, samples in columns,
missing values as NaN). A pandas DataFrame input gives a DataFrame output
with the same index and columns, as R keeps dimnames.
| R | biocimpute |
|---|---|
impute.knn(...) |
impute_knn(...) |
rng.seed |
rng_seed |
$data, $rng.seed |
.data, .rng_seed |
$rng.state |
not returned: biocimpute never touches a global RNG |
stop(...) on colmax |
ValueError |
warning(...) on rowmax |
UserWarning |
cat("Cluster size ...") |
logging.getLogger("biocimpute").info(...) |
data(khanmiss) |
load_khanmiss() |
Behavior inherited from R
For parity, these behaviors of impute.knn are kept:
- Two-means clustering (when there are more than
maxprows) runs on the matrix with missing values replaced by 0. - In a cluster with at most
krows, missing values are left as 0. - If exactly one row has more than
rowmaxmissing, its missing values are left as 0. With two or more such rows, column means are used. - A column with no observed values among the rows used for KNN stays NaN.
The one deliberate difference: when a block has k or fewer rows, R's
Fortran reads past the end of its neighbor array (undefined behavior).
biocimpute uses all other rows of the block as neighbors.
Layout
The code follows the R package's structure:
src/biocimpute/_impute.py:R/allknnimp.R, one function per R functionsrc/biocimpute/_kernels/:src/knnimpute.f, ported to Numba. The_kernelspackage is the only place a backend is imported, so another backend (e.g. Rust) can be swapped in there.src/biocimpute/_rng.py: R's default RNG andsample()reference/: the original R and Fortran sources
Development
uv sync
uv run pytest
To regenerate the R fixtures in tests/fixtures (needs R and gfortran):
tests/r/install_impute.sh # installs impute into tests/r/lib
Rscript tests/r/generate_fixtures.R
License
GPL-2.0, like the original impute package (Trevor Hastie, Robert Tibshirani, Balasubramanian Narasimhan, Gilbert Chu).
Metadata
Release files for biocimpute 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 | Provenance | Actions |
|---|---|---|---|---|
| biocimpute-0.1.0.tar.gz | 859.4 kB | Without provenance | Details for biocimpute-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| biocimpute-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for biocimpute-0.1.0-py3-none-any.whl |
Total release size: 1.7 MB
Release files / biocimpute-0.1.0.tar.gz
| Download URL | biocimpute-0.1.0.tar.gz |
|---|---|
| Size | 859.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ec7881a8507bd627b39ac6a798ab8d99ece87ca8a2cbfb9785efcc200394cac0
|
|
BLAKE2b-256 checksum How to use checksums |
6bc38f8400d150de582986dc8922a1445cc62c1fcf89a1494caed8b65308d40c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / biocimpute-0.1.0-py3-none-any.whl
| Download URL | biocimpute-0.1.0-py3-none-any.whl |
|---|---|
| Size | 860.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b6ea8c7e01239d514a13836fd0917f4cf44a3db7337d9210527b8dfb1a23b7f3
|
|
BLAKE2b-256 checksum How to use checksums |
6f5db661367d5860377b370a65ad6874c00804ff6c8d7c80dec85a298a04d73c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for biocimpute 0.1.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.