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 and BatchNanMean support merging with +. These classes and
the top-k accumulators support versioned to_state() / from_state() and
NPZ checkpoints with save() / load(), without pickle.
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
Development
Install the development dependencies and run the local quality gates:
python -m pip install -e ".[dev]"
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.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| batchstats-0.7.tar.gz | 30.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| batchstats-0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 68.5 kB
Release files / batchstats-0.7.tar.gz
| Download URL | batchstats-0.7.tar.gz |
|---|---|
| Size | 30.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
61db1b7f95cd22a62a3cd6396d14097cb21184c8d3f330c1f019d5afcbc22e7f
|
|
BLAKE2b-256 checksum How to use checksums |
c9ff5874ccc6f505bd6fd9cfcc63593b3c2b94322993fc769e161e5ded865daf
|
| 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.7-py3-none-any.whl
| Download URL | batchstats-0.7-py3-none-any.whl |
|---|---|
| Size | 38.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
393e5197e7c1e0308c3a22e0302e85f50f6eaa2167c5d3ed8ce486d5a35a61fc
|
|
BLAKE2b-256 checksum How to use checksums |
9d11cd63bf22cc500f37e6b5415c5200ff47f37821bef9ba6d950f2afe4cdf39
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|