Skip to main content
Logo BatchStats

PyPI Version Python Versions conda Version Documentation Status Unit tests

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 / BatchNanSum
  • BatchWeightedSum
  • BatchMean / BatchNanMean
  • BatchWeightedMean
  • BatchMin / BatchNanMin
  • BatchMax / BatchNanMax
  • BatchPeakToPeak / BatchNanPeakToPeak
  • BatchVar
  • BatchStd
  • BatchCov
  • BatchCorr
  • BatchTopK / 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)

Source distribution for batchstats 0.7
File Size Uploaded
batchstats-0.7.tar.gz 30.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for batchstats 0.7
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

0.8

2 release files

This release

0.7 This release

2 release files

0.6

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page