Skip to main content

reducers

Reduction functions + Rust(rs), shortname rd.

Rust-backed reduction functions for NumPy arrays - plain (numpy-like) and NaN-aware.

The Goal of this toy project was:

  1. much faster than numpy in many use cases,
  2. much faster than bottleneck in many use cases, and
  3. especially maximum performance for median and variance calculations, which are often bottlenecks in data processing pipelines.

reducers might be slower than numpy or bottleneck for small arrays. However, the most time-consuming reductions like large arrays or deep stacks, median, percentile or quantile, var and std are frequently several times (>100 times for nanpercentiles) faster than numpy and bottleneck.

Install

pip install reducers

For Rust crate use:

[dependencies]
reducers = "<version>"

After Installation

Run the autotuner once on your machine where reducers will run:

python -m reducers.autotuner

It saves parallel-grain settings for that CPU and workload profile. Future import reducers calls apply those settings automatically. The built-in defaults are still valid; use python -m reducers.autotuner --reset to remove the saved tuning file and return to them.

import numpy as np
import reducers as rd

a = np.array([1.0, 2.0, np.nan, np.inf, 5.0])

rd.mean(a)                      # nan: plain reducers propagate NaN/inf
rd.nanmean(a)                   # inf: skip NaN, keep inf
rd.nanmean(a, ignore_inf=True)  # finite-only
rd.nanminmax(a)                 # one fused 1-D scan for nanmin + nanmax
rd.nanpercentile(a, [16, 50, 84])

Axis reductions cover the layouts this package optimizes:

rng = np.random.default_rng(20250311)
stack = rng.normal(size=(31, 256, 256)).astype("f4")
rows = rng.normal(size=(256, 256, 31)).astype("f4")

rd.nanmedian(stack, axis=0)      # stack reduction -> shape (256, 256)
rd.nanmean(rows, axis=-1)        # contiguous trailing-axis reduction
  • For [nan]var and [nan]std, return_mean=True returns the already-computed mean with the variance or standard deviation to avoid duplicate work when both are needed.
  • [nan]sum(a, weights=w) can do similar: return_sum_weights=True and return_unweighted_sum=True expose quantities already available during the fused weighted scan, avoiding separate sum(a * w), sum(w), or sum(a) passes when a caller needs them together.
std, mean = rd.nanstd(a, ddof=1, return_mean=True)
weighted_sum, sum_of_weights = rd.nansum(a, weights=w, return_sum_weights=True)
weighted_sum, unweighted_sum = rd.nansum(a, weights=w, return_unweighted_sum=True)
weighted_sum, unweighted_sum, sum_of_weights = rd.nansum(
    a, weights=w, return_unweighted_sum=True, return_sum_weights=True
)

Dual use: the kernel modules are pure Rust (no PyO3/NumPy) and usable as a crate.

The Rust sigma_clip module provides cumulative sigma clipping over slices, with reusable caller-owned workspace, independent rejection/dispersion centers, rollback policies, and optional clipped mean or median. Defaults are nkeep=0 and revert_on_nkeep=false, making survivor rollback opt-in. See the Rust API and performance notes.

Python exposes sigclip/sigclip_1d, mask-only variants, fused sigclip_combine/sigclip_combine_1d mean/median calls, restoration flags, grow_mask, and reusable SigClip settings. They call the same Rust engine:

values = np.array([1., 2., 3., 4., 100.])
rd.sigclip_mask_1d(values, sigma=2)  # [False, False, False, False, True]
rd.sigclip_combine_1d(values, combine="mean", sigma=2)  # 2.5

Maximum-performance Python calls

For fixed production hot loops, import the low-level Python API as rdl:

import reducers.lowlevel as rdl

The rdl functions call the same Rust kernels but skip the high-level Python normalization layer. Weighted hot loops can choose the narrow fused primitive for the output terms they actually need:

weighted_sum = rdl.weighted_sum_only_skip_nonfinite(a, w)
weighted_sum, sum_weights = rdl.weighted_sum_and_weights_skip_nonfinite(a, w)
weighted_sum, sum_weights, unweighted_sum = rdl.weighted_sum_skip_nonfinite(a, w)
average = rdl.weighted_average_skip_nonfinite(a, w)

See the documentation for details on achieving maximum performance.

Current limits

  • axis may be None (default, whole-array), 0 or -1 (identical to a.ndim - 1); other axes raise NotImplementedError. This keeps hidden transpose/copy costs out of the API and lets the Rust kernels specialize for the supported layouts.

  • NumPy-like subset: There are many unsupported parameters like out, keepdims, where, dtype, or percentile method (linear only). Adding them will not likely be considered unless there is a strong use case, as they add complexity and maintenance burden. The main focus is on the core reduction logic and, more importantly, performance.

See the documentation for detailed API semantics, performance notes, axis behavior, and release wheels: https://ysbach.github.io/reducers/.

Maintainers: see RELEASING.md for CI, dependency updates, release rehearsals, trusted-publisher setup, and recovery.

Metadata

Release files for reducers 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for reducers 0.4.0
File Size Uploaded Provenance Actions
reducers-0.4.0.tar.gz 79.4 kB Without provenance Details for reducers-0.4.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for reducers 0.4.0
File Actions
reducers-0.4.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for reducers-0.4.0-cp310-abi3-win_amd64.whl
reducers-0.4.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details for reducers-0.4.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
reducers-0.4.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details for reducers-0.4.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
reducers-0.4.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details for reducers-0.4.0-cp310-abi3-macosx_11_0_arm64.whl
reducers-0.4.0-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details for reducers-0.4.0-cp310-abi3-macosx_10_12_x86_64.whl

Total release size: 9.1 MB

Release files / reducers-0.4.0.tar.gz

Download URL reducers-0.4.0.tar.gz
Size 79.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d2ed0dbb5522f48f58f5833b9e1b41b945ac03ae4523bf3bd98a453a0dcf05a0
BLAKE2b-256 checksum
How to use checksums
c03f78323521dec1baafe11f608f0ab6a542e5239dcd2fa59f162d502051f7dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
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 / reducers-0.4.0-cp310-abi3-win_amd64.whl

Download URL reducers-0.4.0-cp310-abi3-win_amd64.whl
Size 2.0 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
e7fd3811a5b63d42e6eaad425a72d7b2f691185f0f4d615f6bc498ac85e4de5f
BLAKE2b-256 checksum
How to use checksums
c81654495ac916ee5506bca14907a716aed65d23d78ea3f62859030f39bfb586
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
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 / reducers-0.4.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL reducers-0.4.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.8 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
eb724f3a961ec3745fa47283e54882060ace45d0da36eadb7bd4b69126ecc7f7
BLAKE2b-256 checksum
How to use checksums
75803aac1a543f79b61ca4c92545b9ff4936fae05b807a088f3e5c265f88be77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
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 / reducers-0.4.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL reducers-0.4.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 1.7 MB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
14e79894912536ef551f0958a046b95b366851526cdb41e1f2b5b322b464110e
BLAKE2b-256 checksum
How to use checksums
0895b65b81052a2b3da7137361e61d5b8d794a1e36be279f9cb825ae3cd4ee3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
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 / reducers-0.4.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL reducers-0.4.0-cp310-abi3-macosx_11_0_arm64.whl
Size 1.7 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
9f25b0dc92b55b609a2cf68375fa5431c31e51a95f006e9550722dec7bcbe25c
BLAKE2b-256 checksum
How to use checksums
a8d5d610d7e6b4807ac63f636785b5e6ff22f44e7634d6c875e917aa751de63e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
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 / reducers-0.4.0-cp310-abi3-macosx_10_12_x86_64.whl

Download URL reducers-0.4.0-cp310-abi3-macosx_10_12_x86_64.whl
Size 1.8 MB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
4252c49ee1286fa18a7e48ff3c219f33c52187e8c7623b08fd72eb7fc76241fb
BLAKE2b-256 checksum
How to use checksums
79ecb6071ef94af9bd77618bd9b4c49ba23ceeff233510c9ff0ff5b4bb5fe356
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
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}

Security for reducers 0.4.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.

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