Skip to main content

ndfilters

tests codecov Black Ruff Documentation Status PyPI version

ndfilters is a library of n-dimensional image filters similar to those in scipy.ndimage, but accelerated and parallelized using Numba.

Compared to their scipy.ndimage equivalents, the filters in this library offer some additional capabilities:

  • Axis selection. Every filter accepts an axis argument, so the kernel can be applied to any subset of the array's axes while the remaining axes act as batch dimensions.
  • Masking. A boolean where mask excludes selected elements of the input array from the calculation.
  • Physical units. Inputs can be either numpy.ndarray or astropy.units.Quantity instances.
  • Varying kernels. The convolution kernel is allowed to change along axes orthogonal to the convolution axes.

Differences from scipy.ndimage

Where a filter in this library has a scipy.ndimage counterpart, the two agree except in the following cases.

  • Boundary modes. Only "mirror", "nearest", and "wrap" are supported, plus "truncate", which has no scipy.ndimage equivalent and simply drops the parts of the kernel that fall outside the array. SciPy's "reflect", "constant", and "grid-*" modes raise a ValueError here.
  • Integer input. Integer arrays are promoted to floating point, so the result is a float and is not truncated. scipy.ndimage returns the dtype of the input, and for the separable filters it truncates its intermediates as well. The promotion is what lets a where mask that excludes an entire kernel footprint return NaN.
  • Even-sized median footprints. ndfilters.median_filter averages the two middle elements, like numpy.median, while scipy.ndimage.median_filter selects the element of rank size // 2, the larger of the two. SciPy's convention keeps the result in the dtype of the input and never introduces a value that was not already in the footprint, but it is a biased estimator: on unit-variance noise a size=2 filter shifts the signal by roughly 0.57. The two conventions agree exactly for odd-sized footprints.

The full documentation is hosted on Read the Docs.

Installation

ndfilters is published on PyPI and can be installed using pip.

pip install ndfilters

Quickstart

Every filter takes an array and the shape of the kernel, and returns the filtered array.

import scipy.datasets
import ndfilters

img = scipy.datasets.ascent()
img_filtered = ndfilters.median_filter(img, size=21)

Mean filter

The mean filter calculates a multidimensional rolling mean for the given kernel shape.

mean filter

Trimmed mean filter

The trimmed mean filter is like the mean filter except it ignores a given portion of the dataset before calculating the mean at each pixel.

trimmed mean filter

Median filter

The median filter calculates a multidimensional rolling median for the given kernel shape.

median filter

Variance filter

The variance filter calculates the rolling variance for the given kernel shape.

variance filter

Generic filter

The generic filter applies an arbitrary compiled function to each kernel footprint. It is the engine behind the other rolling filters in this library, and it can be used directly to build custom filters.

generic filter

Convolution

ndfilters.convolve() convolves an array with a given kernel. Unlike scipy.ndimage.convolve() or astropy.convolution.convolve(), the kernel is allowed to vary along axes orthogonal to the convolution axes.

convolve

Metadata

Release files for ndfilters 1.1.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 ndfilters 1.1.0
File Size Uploaded
ndfilters-1.1.0.tar.gz 20.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ndfilters 1.1.0
File Interpreter ABI Platform
ndfilters-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 43.3 kB

Release files / ndfilters-1.1.0.tar.gz

Download URL ndfilters-1.1.0.tar.gz
Size 20.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3a529a77bad50c48c994a87749653ff1629117e483842c3e7305c003076c095e
BLAKE2b-256 checksum
How to use checksums
4aab218223d26fcd1ae37cf59d8fb3a79daa4416b173e7d5be12302a087bf9db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / ndfilters-1.1.0-py3-none-any.whl

Download URL ndfilters-1.1.0-py3-none-any.whl
Size 22.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
317c662b5a94ab42bcc96e05a5a317c5e556ec9cad86019376f696dadf2e1bdc
BLAKE2b-256 checksum
How to use checksums
aeeb2e12267245c22ff6e062df38649cad42de89afb3c72ef8fc8d580f297c06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

1.1.1

2 release files

This release

1.1.0 This release

2 release files

1.0.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.0

2 release files

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