-
Notifications
You must be signed in to change notification settings - Fork 28
/
_dask.py
329 lines (285 loc) · 11 KB
/
_dask.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
326
327
328
329
# 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/>.
import logging
from typing import List, Optional, Tuple, Union
import dask.array as da
import numpy as np
_logger = logging.getLogger(__name__)
def get_chunking(
signal: Optional[Union["EBSD", "LazyEBSD"]] = None,
data_shape: Optional[tuple] = None,
nav_dim: Optional[int] = None,
sig_dim: Optional[int] = None,
chunk_shape: Optional[int] = None,
chunk_bytes: Union[int, float, str, None] = 30e6,
dtype: Union[str, np.dtype, type, None] = None,
) -> tuple:
"""Get a chunk tuple based on the shape of the signal data.
The signal dimensions will not be chunked, and the navigation
dimensions will be chunked based on either ``chunk_shape``, or be
optimized based on the ``chunk_bytes``.
This function is inspired by a similar function in :mod:`pyxem`.
Parameters
----------
signal
If not given, the following must be: data shape to be chunked,
``data_shape``, the number of navigation dimensions,
``nav_dim``, the number of signal dimensions, ``sig_dim``, and
the data array data type ``dtype``.
data_shape
Data shape, must be given if ``signal`` is not.
nav_dim
Number of navigation dimensions, must be given if ``signal`` is
not.
sig_dim
Number of signal dimensions, must be given if ``signal`` is not.
chunk_shape
Shape of navigation chunks. If not given, this size is set
automatically based on ``chunk_bytes``. This is a rectangle if
``signal`` has two navigation dimensions.
chunk_bytes
Number of bytes in each chunk. Default is 30e6, i.e. 30 MB.
Only used if freedom is given to choose, i.e. if ``chunk_shape``
is not given. Various parameter types are allowed, e.g.
``30000000``, ``"30 MB"``, ``"30MiB"``, or the default ``30e6``,
all resulting in approximately 30 MB chunks.
dtype
Data type of the array to chunk. Will take precedence over the
signal data type if ``signal`` is given. Must be given if
``signal`` is not.
Returns
-------
chunks
Chunk tuple.
"""
if signal is not None:
data_shape = signal.data.shape
nav_dim = signal.axes_manager.navigation_dimension
sig_dim = signal.axes_manager.signal_dimension
if dtype is None:
dtype = signal.data.dtype
else:
dtype = np.dtype(dtype)
chunks_dict = {}
# Set the desired navigation chunk shape
for i in range(nav_dim):
if chunk_shape is None:
chunks_dict[i] = "auto"
else:
chunks_dict[i] = chunk_shape
# Don't chunk the signal shape
for i in range(nav_dim, nav_dim + sig_dim):
chunks_dict[i] = -1
chunks = da.core.normalize_chunks(
chunks=chunks_dict,
shape=data_shape,
limit=chunk_bytes,
dtype=dtype,
)
return chunks
def get_dask_array(
signal: Union["EBSD", "LazyEBSD"],
dtype: Union[str, np.dtype, type, None] = None,
**kwargs,
) -> da.Array:
"""Return dask array of patterns with appropriate chunking.
Parameters
----------
signal
Signal with data to return dask array from.
dtype
Data type of returned dask array. This is also passed on to
:func:`~kikuchipy.signals.util.get_chunking`.
**kwargs
Keyword arguments passed to
:func:`~kikuchipy.signals.util.get_chunking` to control the
number of chunks the output data array is split into. Only
``chunk_shape``, ``chunk_bytes`` and ``dtype`` are passed on.
Returns
-------
dask_array
Dask array with signal data with appropriate chunking and data
type.
"""
if dtype is None:
dtype = signal.data.dtype
else:
dtype = np.dtype(dtype)
if signal._lazy or isinstance(signal.data, da.Array):
dask_array = signal.data
if kwargs.pop("rechunk", False):
new_chunks = _reduce_chunks(
dask_array=dask_array,
chunk_bytes=kwargs.pop("chunk_bytes", 8e6),
dtype_out=dtype,
)
dask_array = dask_array.rechunk(new_chunks)
_logger.info(f"Rechunk Dask array: {dask_array}")
else:
chunks = get_chunking(
signal=signal,
dtype=dtype,
chunk_shape=kwargs.pop("chunk_shape", None),
chunk_bytes=kwargs.pop("chunk_bytes", None),
)
dask_array = da.from_array(signal.data, chunks=chunks)
return dask_array.astype(dtype)
def _reduce_chunks(
dask_array: da.Array,
chunk_bytes: Union[int, float] = 8e6,
dtype_out: Union[str, np.dtype, type] = "float32",
) -> tuple:
dtype_out = np.dtype(dtype_out)
chunksize = dask_array.chunksize
nav_chunksize = chunksize[:-2]
nav_ndim = len(nav_chunksize)
chunks_dict = {i: "auto" for i in range(nav_ndim)}
chunks_dict.update({i: -1 for i in range(nav_ndim, nav_ndim + 2)})
if nav_ndim == 2:
idx_min = np.argmin(nav_chunksize)
if nav_chunksize[idx_min] * np.prod(chunksize[-2:]) * 4 < chunk_bytes:
chunks_dict[idx_min] = -1
chunks = da.core.normalize_chunks(
chunks=chunks_dict,
shape=dask_array.shape,
limit=chunk_bytes,
dtype=dtype_out,
)
old_chunks = dask_array.chunks
new_chunks = ()
for i in range(len(chunksize)):
if chunks_dict[i] == -1:
new_chunks += (old_chunks[i],)
else:
new_chunks += (chunks[i],)
return new_chunks
def _get_chunk_overlap_depth(window, axes_manager, chunksize: tuple) -> dict:
"""Return overlap depth between navigation chunks equal to the max.
number of nearest neighbours in each navigation axis.
Parameters
----------
window : kikuchipy.filters.window.Window
axes_manager : hyperspy.axes.AxesManager
chunksize
Returns
-------
overlap_depth
"""
sig_dim = axes_manager.signal_dimension
nav_shape = axes_manager.navigation_shape[::-1]
is_chunked = ~np.equal(chunksize[:-sig_dim], nav_shape)
overlap_depth = {i: n for i, n in enumerate(window.n_neighbours) if is_chunked[i]}
return overlap_depth
def _rechunk_learning_results(
factors: Union[np.ndarray, da.Array],
loadings: Union[np.ndarray, da.Array],
mbytes_chunk: Union[int, float] = 100,
) -> list:
"""Return suggested data chunks for learning results.
It is assumed that the loadings are not transposed. The last axes of
factors and loadings are not chunked. The aims in prioritised order:
1. Limit chunks to approximately input MB (``mbytes_chunk``). 2.
Keep first axis of factors (detector pixels).
Parameters
----------
factors
Component patterns
:attr:`hyperspy.learn.mva.LearningResults.factors` in learning
results.
loadings
Component loadings
:attr:`hyperspy.learn.mva.LearningResults.loadings` in learning
results.
mbytes_chunk
Size of chunks in MB, default is 100 MB as suggested in the Dask
documentation.
Returns
-------
List of two tuples :
The first/second tuple are suggested chunks to pass to
:func:`dask.array.rechunk` for factors/loadings, respectively.
"""
# Make sure the last factors/loading axes have the same shapes
if factors.shape[-1] != loadings.shape[-1]:
raise ValueError("The last dimensions in factors and loadings are not the same")
# Get shape of learning results
learning_results_shape = factors.shape + loadings.shape
# Determine maximum number of (strictly necessary) chunks
suggested_size = mbytes_chunk * 2**20
factors_size = factors.nbytes
loadings_size = loadings.nbytes
total_size = factors_size + loadings_size
num_chunks = np.ceil(total_size / suggested_size)
# Get chunk sizes
if factors_size <= suggested_size: # Chunk first axis in loadings
chunks = [(-1, -1), (int(learning_results_shape[2] / num_chunks), -1)]
else: # Chunk both first axes
sizes = [factors_size, loadings_size]
while (sizes[0] + sizes[1]) >= suggested_size:
max_idx = int(np.argmax(sizes))
sizes[max_idx] = np.floor(sizes[max_idx] / 2)
factors_chunks = int(np.ceil(factors_size / sizes[0]))
loadings_chunks = int(np.ceil(loadings_size / sizes[1]))
chunks = [
(int(learning_results_shape[0] / factors_chunks), -1),
(int(learning_results_shape[2] / loadings_chunks), -1),
]
return chunks
def _update_learning_results(
learning_results,
components: Union[None, int, List[int]],
dtype_out: Union[str, np.dtype, type],
) -> Tuple[Union[np.ndarray, da.Array], Union[np.ndarray, da.Array]]:
"""Update learning results before calling
:meth:`hyperspy.learn.mva.MVA.get_decomposition_model` by
changing data type, keeping only desired components and rechunking
them into suitable chunks if they are lazy.
Parameters
----------
learning_results : hyperspy.learn.mva.LearningResults
Learning results with component patterns and loadings.
components
If None, rebuilds the signal from all `components`. If `int`,
rebuilds signal from `components` in range 0-given `int`. If
list of `int`, rebuilds signal from only `components` in given
list.
dtype_out
Floating data type to cast learning results to.
Returns
-------
factors
Updated component patterns in learning results.
loadings
Updated component loadings in learning results.
"""
dtype_out = np.dtype(dtype_out)
# Change data type
factors = learning_results.factors.astype(dtype_out)
loadings = learning_results.loadings.astype(dtype_out)
# Keep desired components
if hasattr(components, "__iter__"): # components is a list of ints
factors = factors[:, components]
loadings = loadings[:, components]
else: # components is an int
factors = factors[:, :components]
loadings = loadings[:, :components]
# Rechunk if learning results are lazy
if isinstance(factors, da.Array) and isinstance(loadings, da.Array):
chunks = _rechunk_learning_results(factors=factors, loadings=loadings)
factors = factors.rechunk(chunks=chunks[0])
loadings = loadings.rechunk(chunks=chunks[1])
return factors, loadings