nd.filters package¶
The main use of image filters is for noise reduction. This module implements several such filters, all of which are designed to work in an arbitrary number of dimensions.
-
nd.filters.
_expand_kernel
(kernel, kernel_dims, new_dims)[source]¶ Reshape a kernel spanning some dimensions to cover a superset of dimensions.
- Parameters
kernel (ndarray) – An n-dimensional kernel.
kernel_dims (tuple) – The dimensions corresponding to the kernel axes.
new_dims (tuple) – The dimensions of the dataset to which the kernel needs to be applied. Must be a superset of kernel_dims.
- Returns
The reshaped kernel.
- Return type
ndarray
- Raises
ValueError – Will raise a ValueError if the dimensions of the kernel don’t match kernel_dims.
ValueError – Will raise a ValueError if new_dims is not a superset of kernel_dims.
-
class
nd.filters.
BoxcarFilter
(dims='y', 'x', w=3, **kwargs)[source]¶ Bases:
nd.filters.ConvolutionFilter
A boxcar filter.
- Parameters
dims (tuple of str, optional) – The dimensions along which to apply the filter (default: (‘y’, ‘x’)).
w (int) – The width of the boxcar window. Should be an odd integer in order to ensure symmetry.
kwargs (dict, optional) – Extra keyword arguments passed on to
scipy.ndimage.filters.convolve
.
-
apply
(ds, inplace=False, *, njobs=1)[source]¶ Apply the filter to the input dataset.
- Parameters
ds (xarray.Dataset) – The input dataset
inplace (bool, optional) – If True, overwrite the input data inplace (default: False).
njobs (int, optional) – Number of jobs to run in parallel. Setting njobs to -1 uses the number of available cores. Disable parallelism by setting njobs to 1 (default).
- Returns
The filtered dataset
- Return type
xarray.Dataset
-
dims
= ()¶
-
kwargs
= {}¶
-
per_variable
= True¶
-
supports_complex
= True¶
-
class
nd.filters.
ConvolutionFilter
(dims='y', 'x', kernel=None, **kwargs)[source]¶ Bases:
nd.filters.Filter
Kernel-convolution of an xarray.Dataset.
- Parameters
dims (tuple, optional) – The dataset dimensions corresponding to the kernel axes (default: (‘y’, ‘x’)). The length of the tuple must match the number of dimensions of the kernel.
kernel (ndarray) – The convolution kernel.
kwargs (dict, optional) – Extra keyword arguments passed on to
scipy.ndimage.filters.convolve
.
-
apply
(ds, inplace=False, *, njobs=1)[source]¶ Apply the filter to the input dataset.
- Parameters
ds (xarray.Dataset) – The input dataset
inplace (bool, optional) – If True, overwrite the input data inplace (default: False).
njobs (int, optional) – Number of jobs to run in parallel. Setting njobs to -1 uses the number of available cores. Disable parallelism by setting njobs to 1 (default).
- Returns
The filtered dataset
- Return type
xarray.Dataset
-
dims
= ()¶
-
kwargs
= {}¶
-
per_variable
= True¶
-
supports_complex
= True¶
-
class
nd.filters.
Filter
(*args, **kwargs)[source]¶ Bases:
nd.algorithm.Algorithm
The base class for a generic filter.
- Parameters
dims (tuple of str) – The dimensions along which the filter is applied.
-
apply
(ds, inplace=False, *, njobs=1)[source]¶ Apply the filter to the input dataset.
- Parameters
ds (xarray.Dataset) – The input dataset
inplace (bool, optional) – If True, overwrite the input data inplace (default: False).
njobs (int, optional) – Number of jobs to run in parallel. Setting njobs to -1 uses the number of available cores. Disable parallelism by setting njobs to 1 (default).
- Returns
The filtered dataset
- Return type
xarray.Dataset
-
dims
= ()¶
-
per_variable
= True¶
-
supports_complex
= False¶
-
class
nd.filters.
GaussianFilter
(dims='y', 'x', sigma=1, **kwargs)[source]¶ Bases:
nd.filters.Filter
A Gaussian filter.
- Parameters
dims (tuple of str, optional) – The dimensions along which to apply the Gaussian filtering (default: (‘y’, ‘x’)).
sigma (float or sequence of float) – The standard deviation for the Gaussian kernel. If sequence, this is set individually for each dimension.
kwargs (dict, optional) – Extra keyword arguments passed on to
scipy.ndimage.filters.gaussian_filter
.
- Returns
The filtered dataset.
- Return type
xarray.Dataset
-
apply
(ds, inplace=False, *, njobs=1)[source]¶ Apply the filter to the input dataset.
- Parameters
ds (xarray.Dataset) – The input dataset
inplace (bool, optional) – If True, overwrite the input data inplace (default: False).
njobs (int, optional) – Number of jobs to run in parallel. Setting njobs to -1 uses the number of available cores. Disable parallelism by setting njobs to 1 (default).
- Returns
The filtered dataset
- Return type
xarray.Dataset
-
dims
= ()¶
-
per_variable
= True¶
-
supports_complex
= False¶
-
class
nd.filters.
NLMeansFilter
(dims='y', 'x', r=1, sigma=1, h=1, f=1, n_eff=- 1)[source]¶ Bases:
nd.filters.Filter
Non-Local Means (Buades2011).
Buades, A., Coll, B., & Morel, J.-M. (2011). Non-Local Means Denoising. Image Processing On Line, 1, 208–212. https://doi.org/10.5201/ipol.2011.bcm_nlm
- Parameters
dims (tuple of str) – The dataset dimensions along which to filter.
r ({int, sequence}) – The radius
sigma (float) – The standard deviation of the noise present in the data.
h (float) –
f (int) –
n_eff (float, optional) – The desired effective sample size. If given, must be greater than 1 and should be no larger than about half the pixels in the window. -1 means no fixed effective sample size (default: -1).
-
apply
(ds, inplace=False, *, njobs=1)[source]¶ Apply the filter to the input dataset.
- Parameters
ds (xarray.Dataset) – The input dataset
inplace (bool, optional) – If True, overwrite the input data inplace (default: False).
njobs (int, optional) – Number of jobs to run in parallel. Setting njobs to -1 uses the number of available cores. Disable parallelism by setting njobs to 1 (default).
- Returns
The filtered dataset
- Return type
xarray.Dataset
-
dims
= ()¶
-
per_variable
= False¶
-
supports_complex
= False¶
-
nd.filters.
boxcar
(ds, inplace=False, dims='y', 'x', w=3, *, njobs=1, **kwargs)[source]¶ Wrapper for
nd.filters.BoxcarFilter
.A boxcar filter.
- Parameters
ds (xarray.Dataset) – The input dataset
inplace (bool, optional) – If True, overwrite the input data inplace (default: False).
dims (tuple of str, optional) – The dimensions along which to apply the filter (default: (‘y’, ‘x’)).
w (int) – The width of the boxcar window. Should be an odd integer in order to ensure symmetry.
njobs (int, optional) – Number of jobs to run in parallel. Setting njobs to -1 uses the number of available cores. Disable parallelism by setting njobs to 1 (default).
kwargs (dict, optional) – Extra keyword arguments passed on to
scipy.ndimage.filters.convolve
.
- Returns
The filtered dataset
- Return type
xarray.Dataset
-
nd.filters.
convolution
(ds, inplace=False, dims='y', 'x', kernel=None, *, njobs=1, **kwargs)[source]¶ Wrapper for
nd.filters.ConvolutionFilter
.Kernel-convolution of an xarray.Dataset.
- Parameters
ds (xarray.Dataset) – The input dataset
inplace (bool, optional) – If True, overwrite the input data inplace (default: False).
dims (tuple, optional) – The dataset dimensions corresponding to the kernel axes (default: (‘y’, ‘x’)). The length of the tuple must match the number of dimensions of the kernel.
kernel (ndarray) – The convolution kernel.
njobs (int, optional) – Number of jobs to run in parallel. Setting njobs to -1 uses the number of available cores. Disable parallelism by setting njobs to 1 (default).
kwargs (dict, optional) – Extra keyword arguments passed on to
scipy.ndimage.filters.convolve
.
- Returns
The filtered dataset
- Return type
xarray.Dataset
-
nd.filters.
gaussian
(ds, inplace=False, dims='y', 'x', sigma=1, *, njobs=1, **kwargs)[source]¶ Wrapper for
nd.filters.GaussianFilter
.A Gaussian filter.
- Parameters
ds (xarray.Dataset) – The input dataset
inplace (bool, optional) – If True, overwrite the input data inplace (default: False).
dims (tuple of str, optional) – The dimensions along which to apply the Gaussian filtering (default: (‘y’, ‘x’)).
sigma (float or sequence of float) – The standard deviation for the Gaussian kernel. If sequence, this is set individually for each dimension.
njobs (int, optional) – Number of jobs to run in parallel. Setting njobs to -1 uses the number of available cores. Disable parallelism by setting njobs to 1 (default).
kwargs (dict, optional) – Extra keyword arguments passed on to
scipy.ndimage.filters.gaussian_filter
.
- Returns
The filtered dataset
- Return type
xarray.Dataset
-
nd.filters.
nlmeans
(ds, inplace=False, dims='y', 'x', r=1, sigma=1, h=1, f=1, n_eff=- 1, *, njobs=1)[source]¶ Wrapper for
nd.filters.NLMeansFilter
.Non-Local Means (Buades2011).
Buades, A., Coll, B., & Morel, J.-M. (2011). Non-Local Means Denoising. Image Processing On Line, 1, 208–212. https://doi.org/10.5201/ipol.2011.bcm_nlm
- Parameters
ds (xarray.Dataset) – The input dataset
inplace (bool, optional) – If True, overwrite the input data inplace (default: False).
dims (tuple of str) – The dataset dimensions along which to filter.
r ({int, sequence}) – The radius
sigma (float) – The standard deviation of the noise present in the data.
h (float) –
f (int) –
n_eff (float, optional) – The desired effective sample size. If given, must be greater than 1 and should be no larger than about half the pixels in the window. -1 means no fixed effective sample size (default: -1).
njobs (int, optional) – Number of jobs to run in parallel. Setting njobs to -1 uses the number of available cores. Disable parallelism by setting njobs to 1 (default).
- Returns
The filtered dataset
- Return type
xarray.Dataset