BatchStats
BatchStats computes statistics on data that arrives in batches, so you can stream or process large datasets without loading everything into memory. Its incremental algorithms expose a small NumPy-friendly API and support merging independently computed accumulators.
BatchStats requires Python 3.10 or newer.
Installation
pip install batchstats
Or with conda/mamba:
conda install -c conda-forge batchstats
Quick Start
import numpy as np
from batchstats import BatchMean, BatchVar
rng = np.random.default_rng(0)
data_stream = (rng.standard_normal((100, 10)) for _ in range(10))
batch_mean = BatchMean()
batch_var = BatchVar()
for batch in data_stream:
batch_mean.update_batch(batch)
batch_var.update_batch(batch)
mean = batch_mean()
variance = batch_var()
print(f"Mean shape: {mean.shape}")
print(f"Variance shape: {variance.shape}")
Available Statistics
BatchSum/BatchNanSumBatchWeightedSumBatchMean/BatchNanMeanBatchWeightedMeanBatchMin/BatchNanMinBatchMax/BatchNanMaxBatchPeakToPeak/BatchNanPeakToPeakBatchVarBatchStdBatchCovBatchCorrBatchTopK/BatchNanTopK(exact extreme ranks and linear tail quantiles)
BatchNanSum, BatchNanMean and the top-k accumulators can be saved and resumed
with save() / load() using NPZ checkpoints. Use to_state() / from_state()
to export and restore their state in memory.
from batchstats import BatchNanTopK, required_k
extremes = BatchNanTopK(required_k(0.998, 8784), axis=0)
for block in data_stream: # (time, *spatial_shape), same spatial positions
extremes.update_batch(block)
p998 = extremes.quantile(0.998) # raises if retained capacity is insufficient
extremes.save("extremes.npz")
Top-k outputs have a leading rank axis and mask unavailable ranks while preserving
the selected dtype. BatchTopK rejects NaNs; BatchNanTopK ignores them per cell.
See the streaming and checkpoint guide.
Docs: https://batchstats.readthedocs.io
Optional xarray support
pip install "batchstats[xarray]"
Use the classes in batchstats.xarray with a DataArray or a Dataset:
from batchstats.xarray import BatchNanMean
mean = BatchNanMean(dim="time")
for batch in labelled_batches:
mean.update_batch(batch)
result = mean() # DataArray or Dataset, with the remaining dimensions/coordinates
All the statistics listed above have labelled counterparts. Reduce one or more
named dimensions with dim="time" or dim=("time", "level"); dim=None
reduces all dimensions. Dataset variables are accumulated independently. The
NumPy API and its dependencies remain unchanged; importing batchstats does not
import xarray. All labelled accumulators support checkpoints, including their
retained coordinates and attributes:
mean.save("mean.npz")
mean = BatchNanMean.load("mean.npz")
mean.update_batch(next_batch)
Use keep_attrs=True to keep variable and Dataset attributes from the first
batch. See the xarray guide for coordinate
validation, weighted statistics, covariance, top-k and streaming limits.
Development
Install the development dependencies and run the local quality gates:
python -m pip install -e ".[dev,xarray]"
python -m ruff check .
python -m ruff format --check .
python -m pytest --cov=batchstats
python -m build
python -m twine check dist/*
Metadata
Release files for batchstats 0.8
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 |
|---|---|---|---|---|
| batchstats-0.8.tar.gz | 52.3 kB | Without provenance | Details for batchstats-0.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| batchstats-0.8-py3-none-any.whl | Python 3 | none | any | Details for batchstats-0.8-py3-none-any.whl |
Total release size: 104.0 kB
Release files / batchstats-0.8.tar.gz
| Download URL | batchstats-0.8.tar.gz |
|---|---|
| Size | 52.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
041ddcff6df4a2c7878d41aa58a5b8521cb9517928d705006d8c4eed98f5a0d7
|
|
BLAKE2b-256 checksum How to use checksums |
3bf781a6d2f6a35f2b49dcb41aa9b10bd89463e9479f16a7e0f8df0d9bec445b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / batchstats-0.8-py3-none-any.whl
| Download URL | batchstats-0.8-py3-none-any.whl |
|---|---|
| Size | 51.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
12e150124a135c3c0fe69e0209950786c6100b432dc20433ca64dccf717858a1
|
|
BLAKE2b-256 checksum How to use checksums |
de166bab521cc6f32bc6d92bb9f0d1e74892bc0d1e8bf4c5f15fd90eb306c0c7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Security for batchstats 0.8
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.