dask_image.ndinterp package

dask_image.ndinterp package

dask_image.ndinterp.affine_transform(image, matrix, offset=0.0, output_shape=None, order=1, output_chunks=None, **kwargs)[source]

Apply an affine transform using Dask.

For every output chunk, only the slice containing the relevant part of the image is processed. Chunkwise processing is performed either using ndimage.affine_transform or cupyx.scipy.ndimage.affine_transform, depending on the input type.

Notes

Differences to ndimage.affine_transformation:
  • currently, prefiltering is not supported (affecting the output in case of interpolation order > 1)

  • default order is 1

  • modes ‘reflect’, ‘mirror’ and ‘wrap’ are not supported

Arguments equal to ndimage.affine_transformation, except for output_chunks.

Parameters
  • image (array_like (Numpy Array, Cupy Array, Dask Array...)) – The image array.

  • matrix (array (ndim,), (ndim, ndim), (ndim, ndim+1) or (ndim+1, ndim+1)) – Transformation matrix.

  • offset (float or sequence, optional) – The offset into the array where the transform is applied. If a float, offset is the same for each axis. If a sequence, offset should contain one value for each axis.

  • output_shape (tuple of ints, optional) – The shape of the array to be returned.

  • order (int, optional) – The order of the spline interpolation. Note that for order>1 scipy’s affine_transform applies prefiltering, which is not yet supported and skipped in this implementation.

  • output_chunks (tuple of ints, optional) – The shape of the chunks of the output Dask Array.

Returns

affine_transform – A dask array representing the transformed output

Return type

Dask Array

dask_image.ndinterp.map_coordinates(input, coordinates, order=3, mode='constant', cval=0.0, prefilter=False)[source]

Wraps ndimage.map_coordinates.

Both the input and coordinate arrays can be dask arrays. GPU arrays are not supported.

For each chunk in the coordinates array, the coordinates are computed and mapped onto the required slices of the input array. One task is is defined for each input array chunk that has been associated to at least one coordinate. The outputs of the tasks are then rearranged to match the input order. For more details see the docstring of ‘_map_single_coordinates_array_chunk’.

Using this function together with schedulers that support parallelism (threads, processes, distributed) makes sense in the case of either a large input array or a large coordinates array. When both arrays are large, it is recommended to use the single-threaded scheduler. A scheduler can be specified using e.g. with dask.config.set(scheduler=’threads’): ….

inputarray_like

The input array.

coordinatesarray_like

The coordinates at which to sample the input.

orderint, optional

The order of the spline interpolation, default is 3. The order has to be in the range 0-5.

mode : boundary behavior mode, optional cval : float, optional

Value to fill past edges of input if mode is ‘constant’. Default is 0.0

prefilterbool, optional

If True, prefilter the input before interpolation. Default is False. Warning: prefilter is True by default in scipy.ndimage.map_coordinates. Prefiltering here is performed on a chunk-by-chunk basis, which may lead to different results than scipy.ndimage.map_coordinates in case of chunked input arrays and order > 1. Note: prefilter is not necessary when:

  • You are using nearest neighbour interpolation, by setting order=0

  • You are using linear interpolation, by setting order=1, or

  • You have already prefiltered the input array,

using the spline_filter or spline_filter1d functions.

Comments:
  • in case of a small coordinate array, it might make sense to rechunk it into a single chunk

  • note the different default for prefilter compared to scipy.ndimage.map_coordinates, which is True by default.

dask_image.ndinterp.rotate(input_arr, angle, axes=(1, 0), reshape=True, output_chunks=None, **kwargs)[source]

Rotate an array using Dask.

The array is rotated in the plane defined by the two axes given by the axes parameter using spline interpolation of the requested order.

Chunkwise processing is performed using dask_image.ndinterp.affine_transform, for which further parameters supported by the ndimage functions can be passed as keyword arguments.

Notes

Differences to ndimage.rotate:
  • currently, prefiltering is not supported (affecting the output in case of interpolation order > 1)

  • default order is 1

  • modes ‘reflect’, ‘mirror’ and ‘wrap’ are not supported

Arguments are equal to ndimage.rotate except for - output (not present here) - output_chunks (relevant in the dask array context)

Parameters
  • input_arr (array_like (Numpy Array, Cupy Array, Dask Array...)) – The image array.

  • angle (float) – The rotation angle in degrees.

  • axes (tuple of 2 ints, optional) – The two axes that define the plane of rotation. Default is the first two axes.

  • reshape (bool, optional) – If reshape is true, the output shape is adapted so that the input array is contained completely in the output. Default is True.

  • output_chunks (tuple of ints, optional) – The shape of the chunks of the output Dask Array.

  • **kwargs (dict, optional) – Additional keyword arguments are passed to dask_image.ndinterp.affine_transform.

Returns

rotate – A dask array representing the rotated input.

Return type

Dask Array

Examples

>>> from scipy import ndimage, misc
>>> import matplotlib.pyplot as plt
>>> import dask.array as da
>>> fig = plt.figure(figsize=(10, 3))
>>> ax1, ax2, ax3 = fig.subplots(1, 3)
>>> img = da.from_array(misc.ascent(),chunks=(64,64))
>>> img_45 = dask_image.ndinterp.rotate(img, 45, reshape=False)
>>> full_img_45 = dask_image.ndinterp.rotate(img, 45, reshape=True)
>>> ax1.imshow(img, cmap='gray')
>>> ax1.set_axis_off()
>>> ax2.imshow(img_45, cmap='gray')
>>> ax2.set_axis_off()
>>> ax3.imshow(full_img_45, cmap='gray')
>>> ax3.set_axis_off()
>>> fig.set_tight_layout(True)
>>> plt.show()
>>> print(img.shape)
(512, 512)
>>> print(img_45.shape)
(512, 512)
>>> print(full_img_45.shape)
(724, 724)
dask_image.ndinterp.spline_filter(image, order=3, output=<class 'numpy.float64'>, mode='mirror', output_chunks=None, *, depth=None, **kwargs)[source]

Wrapped copy of “scipy.ndimage._interpolation.spline_filter”

Excludes the output parameter as it would not work with Dask arrays.

Original docstring:

Multidimensional spline filter.

For more details, see spline_filter1d.

See also

spline_filter1d

Calculate a 1-D spline filter along the given axis.

Notes

The multidimensional filter is implemented as a sequence of 1-D spline filters. The intermediate arrays are stored in the same data type as the output. Therefore, for output types with a limited precision, the results may be imprecise because intermediate results may be stored with insufficient precision.

For complex-valued image, this function processes the real and imaginary components independently.

New in version 1.6.0: Complex-valued support added.

dask_image.ndinterp.spline_filter1d(image, order=3, axis=-1, output=<class 'numpy.float64'>, mode='mirror', output_chunks=None, *, depth=None, **kwargs)[source]

Wrapped copy of “scipy.ndimage._interpolation.spline_filter1d”

Excludes the output parameter as it would not work with Dask arrays.

Original docstring:

Calculate a 1-D spline filter along the given axis.

The lines of the array along the given axis are filtered by a spline filter. The order of the spline must be >= 2 and <= 5.

Parameters
  • image (array_like) – The image array.

  • order (int, optional) – The order of the spline, default is 3.

  • axis (int, optional) – The axis along which the spline filter is applied. Default is the last axis.

  • mode ({'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}, optional) –

    The mode parameter determines how the image array is extended beyond its boundaries. Default is ‘mirror’. Behavior for each valid value is as follows (see additional plots and details on boundary modes):

    ’reflect’ (d c b a | a b c d | d c b a)

    The image is extended by reflecting about the edge of the last pixel. This mode is also sometimes referred to as half-sample symmetric.

    ’grid-mirror’

    This is a synonym for ‘reflect’.

    ’constant’ (k k k k | a b c d | k k k k)

    The image is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter. No interpolation is performed beyond the edges of the image.

    ’grid-constant’ (k k k k | a b c d | k k k k)

    The image is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter. Interpolation occurs for samples outside the image’s extent as well.

    ’nearest’ (a a a a | a b c d | d d d d)

    The image is extended by replicating the last pixel.

    ’mirror’ (d c b | a b c d | c b a)

    The image is extended by reflecting about the center of the last pixel. This mode is also sometimes referred to as whole-sample symmetric.

    ’grid-wrap’ (a b c d | a b c d | a b c d)

    The image is extended by wrapping around to the opposite edge.

    ’wrap’ (d b c d | a b c d | b c a b)

    The image is extended by wrapping around to the opposite edge, but in a way such that the last point and initial point exactly overlap. In this case it is not well defined which sample will be chosen at the point of overlap.

Returns

spline_filter1d – The filtered image.

Return type

ndarray

Notes

All of the interpolation functions in ndimage do spline interpolation of the image image. If using B-splines of order > 1, the image image values have to be converted to B-spline coefficients first, which is done by applying this 1-D filter sequentially along all axes of the image. All functions that require B-spline coefficients will automatically filter their images, a behavior controllable with the prefilter keyword argument. For functions that accept a mode parameter, the result will only be correct if it matches the mode used when filtering.

For complex-valued image, this function processes the real and imaginary components independently.

New in version 1.6.0: Complex-valued support added.

See also

spline_filter

Multidimensional spline filter.