-
Notifications
You must be signed in to change notification settings - Fork 28
/
_map_helper.py
325 lines (285 loc) · 10.4 KB
/
_map_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# Copyright 2019-2023 The kikuchipy developers
#
# This file is part of kikuchipy.
#
# kikuchipy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# kikuchipy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with kikuchipy. If not, see <http://www.gnu.org/licenses/>.
"""Private functions for calculating dot products between EBSD patterns
and their neighbours in a map of a 1D or 2D navigation shape.
"""
from typing import Callable, Optional, Tuple, Union
import numpy as np
from scipy.ndimage import generic_filter
from kikuchipy.filters import Window
from kikuchipy.pattern._pattern import _normalize, _zero_mean
# This private module is tested indirectly via the EBSD methods
# get_average_neighbour_dot_product_map() and
# get_neighbour_dot_product_matrices()
def _map_helper(
patterns: np.ndarray,
map_function: Callable,
window: Union[np.ndarray, Window],
nav_shape: tuple,
dtype_out: np.dtype = np.float32,
**kwargs,
) -> np.ndarray:
"""Return output of :func:`scipy.ndimage.generic_filter` after
wrapping the `map_function` to apply at each element of
`flat_index_map`.
The generic filter function will be applied at each navigation
point.
Parameters
----------
patterns
Pattern chunk.
map_function
:func:`_neighbour_dot_products`.
window
Window defining the pattern neighbours to calculate the dot
product with.
nav_shape
Navigation shape of pattern chunk.
dtype_out
Output array data dtype.
kwargs
Keyword arguments passed to `map_function`.
Returns
-------
numpy.ndarray
The generic filter output.
"""
# Array in the original navigation shape of indices into the
# flattened navigation axis
flat_index_map = np.arange(np.prod(nav_shape)).reshape(nav_shape)
def wrapped_map_function(indices):
# `indices` contain the indices to be picked out with window
# from `flat_index_map`
return map_function(
patterns=patterns,
indices=indices.astype(int),
nav_shape=nav_shape,
dtype_out=dtype_out,
**kwargs,
)
return generic_filter(
flat_index_map,
wrapped_map_function,
footprint=window,
mode="constant",
cval=-1,
output=None if "output" in kwargs else dtype_out,
)
def _neighbour_dot_products(
patterns: np.ndarray,
indices: np.ndarray,
nav_shape: tuple,
sig_size: int,
dtype_out: np.dtype,
center_index: int,
zero_mean: bool,
normalize: bool,
flat_window_truthy_indices: Optional[np.ndarray] = None,
output: Optional[np.ndarray] = None,
) -> Union[float, int, np.ndarray]:
"""Return either an average of a dot product matrix between a
pattern and it's neighbours, or the matrix.
Parameters
----------
patterns
Pattern chunk.
indices
Flat array of indices into `patterns`.
nav_shape
Navigation shape of `patterns`.
sig_size
Size of the signal, i.e. pattern or detector pixels.
dtype_out
Data type of the output dot product matrix and also of the
patterns prior to dot product calculation.
center_index
Index into `patterns` for the current pattern to calculate the
dot products for.
zero_mean
Whether to center the pattern intensities by subtracting the
mean intensity to get an average intensity of zero,
individually.
normalize
Whether to normalize the pattern intensities to a standard
deviation of 1 before calculating the dot products. This
operation is performed after centering the intensities if
`zero_mean` is True.
flat_window_truthy_indices
Flat array of indices into `patterns` for the navigation points
to calculate dot products with. If None (default), the function
assumes that both `output` and this parameter is None, and that
the `output` array of dot products is to be updated inplace.
output
A continually, inplace updated 4D array containing the dot
product matrices.
"""
# Flat navigation index corresponding to the origin of the window,
# i.e. into `patterns`, i.e. the current navigation point for which
# to calculate the dot product with it's neighbours
pat_idx = indices[center_index]
# Indices into `indices` of neighbours to compute dot product with,
# excluding neighbours outside the map and itself
neighbour_idx = np.where((indices != pat_idx) & (indices != -1))[0]
neighbours = indices[neighbour_idx]
neighbours = np.unravel_index(neighbours, nav_shape)
# Flat array of neighbour patterns
neighbour_patterns = patterns[neighbours].astype(dtype_out)
neighbour_patterns = neighbour_patterns.reshape((-1, sig_size))
# Flat pattern corresponding to the window origin, i.e. the current
# navigation point to average
pattern = patterns[np.unravel_index(pat_idx, nav_shape)]
pattern = pattern.squeeze().flatten().astype(dtype_out)
# Pre-process pattern intensities
if zero_mean:
pattern = _zero_mean(pattern, axis=0)
neighbour_patterns = _zero_mean(neighbour_patterns, axis=1)
if normalize:
pattern = _normalize(pattern, axis=0)
neighbour_patterns = _normalize(neighbour_patterns, axis=1)
# Calculate the dot products
dot_products = neighbour_patterns @ pattern
if output is None:
return np.nanmean(dot_products)
else:
center_value = (pattern**2).sum()
output[pat_idx][flat_window_truthy_indices[center_index]] = center_value
output[pat_idx][flat_window_truthy_indices[neighbour_idx]] = dot_products
# Output variable is modified in place, but `_map_helper()`
# expects a (in this case discarded) returned value
return 1
def _get_neighbour_dot_product_matrices(
patterns: np.ndarray,
window: Window,
sig_dim: int,
sig_size: int,
zero_mean: bool,
normalize: bool,
dtype_out: np.dtype,
) -> np.ndarray:
"""Return a 4D array of a pattern chunk's navigation shape, and a
matrix of dot products between a pattern and its neighbours within a
window in each navigation point in that chunk.
Parameters
----------
patterns
Pattern chunk.
window
Window defining the neighbours to calculate the average with.
sig_dim
Number of signal dimensions.
sig_size
Number of pattern pixels.
zero_mean
Whether to subtract the mean of each pattern individually to
center the intensities about zero before calculating the
dot products.
normalize
Whether to normalize the pattern intensities to a standard
deviation of 1 before calculating the dot products. This
operation is performed after centering the intensities if
`zero_mean` is True.
dtype_out
Data type of output map.
Returns
-------
adp
Map of the average dot product between each pattern and its
neighbours in a chunk of patterns.
"""
# Get a flat boolean window, a boolean array with True for True
# window coefficients, and the index of this window's origin
(boolean_window, flat_window_truthy_indices, center_index) = _setup_window_indices(
window=window
)
nav_shape = patterns.shape[:-sig_dim]
output = np.empty((np.prod(nav_shape), window.size), dtype=dtype_out)
output[:] = np.nan
_map_helper(
patterns,
_neighbour_dot_products,
window=boolean_window,
nav_shape=nav_shape,
sig_size=sig_size,
center_index=center_index,
flat_window_truthy_indices=flat_window_truthy_indices,
zero_mean=zero_mean,
normalize=normalize,
output=output,
)
output = output.reshape(nav_shape + window.shape)
return output
def _get_average_dot_product_map(
patterns: np.ndarray,
window: Window,
sig_dim: int,
sig_size: int,
zero_mean: bool,
normalize: bool,
dtype_out: np.dtype,
) -> np.ndarray:
"""Return the average dot product map for a chunk of patterns.
Parameters
----------
patterns
Pattern chunk.
window
Window defining the neighbours to calculate the average with.
sig_dim
Number of signal dimensions.
sig_size
Number of pattern pixels.
zero_mean
Whether to subtract the mean of each pattern individually to
center the intensities about zero before calculating the
dot products.
normalize
Whether to normalize the pattern intensities to a standard
deviation of 1 before calculating the dot products. This
operation is performed after centering the intensities if
`zero_mean` is True.
dtype_out
Data type of output map.
Returns
-------
adp
Average dot product map for the chunk of patterns.
"""
# Get a flat boolean window and the index of this window's origin
boolean_window, _, center_index = _setup_window_indices(window=window)
adp = _map_helper(
patterns,
_neighbour_dot_products,
window=boolean_window,
nav_shape=patterns.shape[:-sig_dim],
sig_size=sig_size,
dtype_out=dtype_out,
center_index=center_index,
zero_mean=zero_mean,
normalize=normalize,
)
return adp
def _setup_window_indices(window: Window) -> Tuple[np.ndarray, np.ndarray, int]:
# Index of window origin in flattened window
flat_window_origin = np.ravel_multi_index(window.origin, window.shape)
# Make window flat with boolean values
boolean_window = window.copy().astype(bool)
flat_window = boolean_window.flatten()
# Index of window origin in boolean array with only True values
flat_window_truthy_indices = np.nonzero(flat_window)[0]
center_index = np.where(flat_window_truthy_indices == flat_window_origin)
center_index = center_index[0][0]
return boolean_window, flat_window_truthy_indices, center_index