Debian Codegraph @

dask_image/ndinterp/_affine_transform.py

raw
1 # -*- coding: utf-8 -*-
2 from itertools import product
3
4 import dask.array as da
5 import numpy as np
6 from dask.base import tokenize
7 from dask.highlevelgraph import HighLevelGraph
8 import scipy
9 from scipy.ndimage import affine_transform as ndimage_affine_transform
10
11 from ._spline_filters import spline_filter
12 from ..dispatch._dispatch_ndinterp import (
13 dispatch_affine_transform,
14 dispatch_asarray,
15 )
16
17
18 def affine_transform(
19 image,
20 matrix,
21 offset=0.0,
22 output_shape=None,
23 order=1,
24 output_chunks=None,
25 **kwargs
26 ):
27 """Apply an affine transform using Dask.
28
29 For every output chunk, only the slice containing the relevant part
30 of the image is processed. Chunkwise processing is performed
31 either using `ndimage.affine_transform` or
32 `cupyx.scipy.ndimage.affine_transform`, depending on the input type.
33
34 Notes
35 -----
36 Differences to `ndimage.affine_transformation`:
37 - currently, prefiltering is not supported
38 (affecting the output in case of interpolation `order > 1`)
39 - default order is 1
40 - modes 'reflect', 'mirror' and 'wrap' are not supported
41
42 Arguments equal to `ndimage.affine_transformation`,
43 except for `output_chunks`.
44
45 Parameters
46 ----------
47 image : array_like (Numpy Array, Cupy Array, Dask Array...)
48 The image array.
49 matrix : array (ndim,), (ndim, ndim), (ndim, ndim+1) or (ndim+1, ndim+1)
50 Transformation matrix.
51 offset : float or sequence, optional
52 The offset into the array where the transform is applied. If a float,
53 `offset` is the same for each axis. If a sequence, `offset` should
54 contain one value for each axis.
55 output_shape : tuple of ints, optional
56 The shape of the array to be returned.
57 order : int, optional
58 The order of the spline interpolation. Note that for order>1
59 scipy's affine_transform applies prefiltering, which is not
60 yet supported and skipped in this implementation.
61 output_chunks : tuple of ints, optional
62 The shape of the chunks of the output Dask Array.
63
64 Returns
65 -------
66 affine_transform : Dask Array
67 A dask array representing the transformed output
68
69 """
70
71 if not isinstance(image, da.core.Array):
72 image = da.from_array(image)
73
74 if output_shape is None:
75 output_shape = image.shape
76
77 if output_chunks is None:
78 output_chunks = image.shape
79
80 # Perform test run to ensure parameter validity.
81 ndimage_affine_transform(np.zeros([0] * image.ndim),
82 matrix,
83 offset)
84
85 # Make sure parameters contained in matrix and offset
86 # are not overlapping, i.e. that the offset is valid as
87 # it needs to be modified for each chunk.
88 # Further parameter checks are performed directly by
89 # `ndimage.affine_transform`.
90
91 matrix = np.asarray(matrix)
92 offset = np.asarray(offset).squeeze()
93
94 # these lines were copied and adapted from `ndimage.affine_transform`
95 if (matrix.ndim == 2 and matrix.shape[1] == image.ndim + 1 and
96 (matrix.shape[0] in [image.ndim, image.ndim + 1])):
97
98 # assume input is homogeneous coordinate transformation matrix
99 offset = matrix[:image.ndim, image.ndim]
100 matrix = matrix[:image.ndim, :image.ndim]
101
102 cval = kwargs.pop('cval', 0)
103 mode = kwargs.pop('mode', 'constant')
104 prefilter = kwargs.pop('prefilter', False)
105
106 supported_modes = ['constant', 'nearest']
107 if scipy.__version__ > np.lib.NumpyVersion('1.6.0'):
108 supported_modes += ['grid-constant']
109 if mode in ['wrap', 'reflect', 'mirror', 'grid-mirror', 'grid-wrap']:
110 raise NotImplementedError(
111 f"Mode {mode} is not currently supported. It must be one of "
112 f"{supported_modes}.")
113
114 # process kwargs
115 if prefilter and order > 1:
116 # prefilter is not yet supported for all modes
117 if mode in ['nearest', 'grid-constant']:
118 raise NotImplementedError(
119 f"order > 1 with mode='{mode}' is not supported. Currently "
120 f"prefilter is only supported with mode='constant'."
121 )
122 image = spline_filter(image, order, output=np.float64,
123 mode=mode)
124
125 n = image.ndim
126 image_shape = image.shape
127
128 # calculate output array properties
129 normalized_chunks = da.core.normalize_chunks(output_chunks,
130 tuple(output_shape))
131 block_indices = product(*(range(len(bds)) for bds in normalized_chunks))
132 block_offsets = [np.cumsum((0,) + bds[:-1]) for bds in normalized_chunks]
133
134 # use dispatching mechanism to determine backend
135 affine_transform_method = dispatch_affine_transform(image)
136 asarray_method = dispatch_asarray(image)
137
138 # construct dask graph for output array
139 # using unique and deterministic identifier
140 output_name = 'affine_transform-' + tokenize(image, matrix, offset,
141 output_shape, output_chunks,
142 kwargs)
143 output_layer = {}
144 rel_images = []
145 for ib, block_ind in enumerate(block_indices):
146
147 out_chunk_shape = [normalized_chunks[dim][block_ind[dim]]
148 for dim in range(n)]
149 out_chunk_offset = [block_offsets[dim][block_ind[dim]]
150 for dim in range(n)]
151
152 out_chunk_edges = np.array([i for i in np.ndindex(tuple([2] * n))])\
153 * np.array(out_chunk_shape) + np.array(out_chunk_offset)
154
155 # map output chunk edges onto input image coordinates
156 # to define the input region relevant for the current chunk
157 if matrix.ndim == 1 and len(matrix) == image.ndim:
158 rel_image_edges = matrix * out_chunk_edges + offset
159 else:
160 rel_image_edges = np.dot(matrix, out_chunk_edges.T).T + offset
161
162 rel_image_i = np.min(rel_image_edges, 0)
163 rel_image_f = np.max(rel_image_edges, 0)
164
165 # Calculate edge coordinates required for the footprint of the
166 # spline kernel according to
167 # https://github.com/scipy/scipy/blob/9c0d08d7d11fc33311a96d2ac3ad73c8f6e3df00/scipy/ndimage/src/ni_interpolation.c#L412-L419 # noqa: E501
168 # Also see this discussion:
169 # https://github.com/dask/dask-image/issues/24#issuecomment-706165593 # noqa: E501
170 for dim in range(n):
171
172 if order % 2 == 0:
173 rel_image_i[dim] += 0.5
174 rel_image_f[dim] += 0.5
175
176 rel_image_i[dim] = np.floor(rel_image_i[dim]) - order // 2
177 rel_image_f[dim] = np.floor(rel_image_f[dim]) - order // 2 + order
178
179 if order == 0: # required for consistency with scipy.ndimage
180 rel_image_i[dim] -= 1
181
182 # clip image coordinates to image extent
183 for dim, s in zip(range(n), image_shape):
184 rel_image_i[dim] = np.clip(rel_image_i[dim], 0, s - 1)
185 rel_image_f[dim] = np.clip(rel_image_f[dim], 0, s - 1)
186
187 rel_image_slice = tuple([slice(int(rel_image_i[dim]),
188 int(rel_image_f[dim]) + 2)
189 for dim in range(n)])
190
191 rel_image = image[rel_image_slice]
192
193 """Block comment for future developers explaining how `offset` is
194 transformed into `offset_prime` for each output chunk.
195 Modify offset to point into cropped image.
196 y = Mx + o
197 Coordinate substitution:
198 y' = y - y0(min_coord_px)
199 x' = x - x0(chunk_offset)
200 Then:
201 y' = Mx' + o + Mx0 - y0
202 M' = M
203 o' = o + Mx0 - y0
204 """
205
206 offset_prime = offset + np.dot(matrix, out_chunk_offset) - rel_image_i
207
208 output_layer[(output_name,) + block_ind] = (
209 affine_transform_method,
210 (da.core.concatenate3, rel_image.__dask_keys__()),
211 asarray_method(matrix),
212 offset_prime,
213 tuple(out_chunk_shape), # output_shape
214 None, # out
215 order,
216 mode,
217 cval,
218 False # prefilter
219 )
220
221 rel_images.append(rel_image)
222
223 graph = HighLevelGraph.from_collections(output_name, output_layer,
224 dependencies=[image] + rel_images)
225
226 meta = dispatch_asarray(image)([0]).astype(image.dtype)
227
228 transformed = da.Array(graph,
229 output_name,
230 shape=tuple(output_shape),
231 # chunks=output_chunks,
232 chunks=normalized_chunks,
233 meta=meta)
234
235 return transformed