/
_io.py
463 lines (404 loc) · 14.3 KB
/
_io.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# 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 glob
import os
from pathlib import Path
from typing import List, Optional, Union
from hyperspy.io_plugins import hspy
from hyperspy.misc.io.tools import overwrite as overwrite_method
from hyperspy.misc.utils import strlist2enumeration, find_subclasses
from hyperspy.signal import BaseSignal
from h5py import File, is_hdf5, Group
import numpy as np
import kikuchipy.signals
from kikuchipy.io.plugins import (
bruker_h5ebsd,
ebsd_directory,
edax_binary,
edax_h5ebsd,
emsoft_ebsd,
emsoft_ebsd_master_pattern,
emsoft_ecp_master_pattern,
emsoft_tkd_master_pattern,
kikuchipy_h5ebsd,
nordif,
nordif_calibration_patterns,
oxford_binary,
oxford_h5ebsd,
)
from kikuchipy.io._util import _get_input_bool, _ensure_directory
plugins = [
bruker_h5ebsd,
ebsd_directory,
edax_binary,
edax_h5ebsd,
emsoft_ebsd,
emsoft_ebsd_master_pattern,
emsoft_ecp_master_pattern,
emsoft_tkd_master_pattern,
hspy,
kikuchipy_h5ebsd,
nordif,
nordif_calibration_patterns,
oxford_binary,
oxford_h5ebsd,
]
default_write_ext = set()
for plugin in plugins:
if plugin.writes:
default_write_ext.add(plugin.file_extensions[plugin.default_extension])
def load(
filename: Union[str, Path], lazy: bool = False, **kwargs
) -> Union[
"EBSD",
"EBSDMasterPattern",
"ECPMasterPattern",
List["EBSD"],
List["EBSDMasterPattern"],
List["ECPMasterPattern"],
]:
"""Load an :class:`~kikuchipy.signals.EBSD`,
:class:`~kikuchipy.signals.EBSDMasterPattern` or
:class:`~kikuchipy.signals.ECPMasterPattern` signal from one of the
:ref:`/tutorials/load_save_data.ipynb#Supported-file-formats`.
This function is a modified version of :func:`hyperspy.io.load`.
Parameters
----------
filename
Name of file to load.
lazy
Open the data lazily without actually reading the data from disk
until required. Allows opening arbitrary sized datasets. Default
is ``False``.
**kwargs
Keyword arguments passed to the corresponding kikuchipy reader.
See their individual documentation for available options.
Returns
-------
out
Signal or a list of signals.
Raises
------
IOError
If the file was not found or could not be read.
Examples
--------
Import nine patterns from an HDF5 file in a directory ``DATA_DIR``
>>> import kikuchipy as kp
>>> s = kp.load(DATA_DIR + "/patterns.h5")
>>> s
<EBSD, title: patterns Scan 1, dimensions: (3, 3|60, 60)>
"""
filename = str(filename)
if not os.path.isfile(filename):
is_wildcard = False
filenames = glob.glob(filename)
if len(filenames) > 0:
is_wildcard = True
if not is_wildcard:
raise IOError(f"No filename matches '{filename}'.")
# Find matching reader for file extension
extension = os.path.splitext(filename)[1][1:]
readers = []
for plugin in plugins:
if extension.lower() in plugin.file_extensions:
readers.append(plugin)
if len(readers) == 0:
raise IOError(
f"Could not read '{filename}'. If the file format is supported, please "
"report this error"
)
elif len(readers) > 1 and is_hdf5(filename):
reader = _plugin_from_footprints(filename, plugins=readers)
else:
reader = readers[0]
# Get data and metadata (from potentially multiple signals if an h5ebsd
# file)
signal_dicts = reader.file_reader(filename, lazy=lazy, **kwargs)
out = []
for signal in signal_dicts:
out.append(_dict2signal(signal, lazy=lazy))
directory, filename = os.path.split(os.path.abspath(filename))
filename, extension = os.path.splitext(filename)
out[-1].tmp_parameters.folder = directory
out[-1].tmp_parameters.filename = filename
out[-1].tmp_parameters.extension = extension.replace(".", "")
if len(out) == 1:
out = out[0]
return out
def _dict2signal(signal_dict: dict, lazy: bool = False):
"""Create a signal instance from a dictionary.
This function is a modified version :func:`hyperspy.io.dict2signal`.
Parameters
----------
signal_dict
Signal dictionary with ``data``, ``metadata`` and
``original_metadata``.
lazy
Open the data lazily without actually reading the data from disk
until required. Allows opening arbitrary sized datasets. Default
is False.
Returns
-------
signal : EBSD, LazyEBSD, EBSDMasterPattern or LazyEBSDMasterPattern
Signal instance with ``data``, ``metadata`` and
``original_metadata`` from dictionary.
"""
signal_type = ""
if "metadata" in signal_dict:
md = signal_dict["metadata"]
if "Signal" in md and "record_by" in md["Signal"]:
record_by = md["Signal"]["record_by"]
if record_by != "image":
raise ValueError(
"kikuchipy only supports `record_by = image`, not " f"{record_by}."
)
del md["Signal"]["record_by"]
if "Signal" in md and "signal_type" in md["Signal"]:
signal_type = md["Signal"]["signal_type"]
signal = _assign_signal_subclass(
signal_dimension=2,
signal_type=signal_type,
dtype=signal_dict["data"].dtype,
lazy=lazy,
)(**signal_dict)
if signal._lazy:
signal._make_lazy()
return signal
def _plugin_from_footprints(filename: str, plugins) -> Optional[object]:
"""Get HDF5 correct plugin from a list of potential plugins based on
their unique footprints.
The unique footprint is a list of strings that can take on either of
two formats:
* group/dataset names separated by "/", indicating nested
groups/datasets
* single group/dataset name indicating that the groups/datasets
are in the top group
Parameters
----------
filename
Input file name.
plugins
Potential plugins reading HDF5 files.
Returns
-------
plugin
One of the potential plugins, or ``None`` if no footprint was
found.
"""
def _hdf5group2dict(group):
d = {}
for key, val in group.items():
key_lower = key.lstrip().lower()
if isinstance(val, Group):
d[key_lower] = _hdf5group2dict(val)
elif key_lower == "manufacturer":
d[key_lower] = key
else:
d[key_lower] = 1
return d
def _exists(obj, chain):
key = chain.pop(0)
if key in obj:
return _exists(obj[key], chain) if chain else obj[key]
with File(filename) as f:
d = _hdf5group2dict(f["/"])
plugins_with_footprints = [p for p in plugins if hasattr(p, "footprint")]
plugins_with_manufacturer = [
p for p in plugins_with_footprints if hasattr(p, "manufacturer")
]
matching_plugin = None
# Check manufacturer if possible (all h5ebsd files have this)
for key, val in d.items():
if key == "manufacturer":
# Extracting the manufacturer is finicky
man = f[val][()]
if isinstance(man, np.ndarray) and len(man) == 1:
man = man[0]
if isinstance(man, bytes):
man = man.decode("latin-1")
for p in plugins_with_manufacturer:
if man.lower() == p.manufacturer:
matching_plugin = p
break
# If no match found, continue searching
if matching_plugin is None:
for p in plugins_with_footprints:
n_matches = 0
n_desired_matches = len(p.footprint)
for fp in p.footprint:
fp = fp.lower().split("/")
if _exists(d, fp) is not None:
n_matches += 1
if n_matches == n_desired_matches:
matching_plugin = p
break
return matching_plugin
def _assign_signal_subclass(
dtype: np.dtype,
signal_dimension: int,
signal_type: str = "",
lazy: bool = False,
):
"""Given ``record_by`` and ``signal_type`` return the matching
signal subclass.
This function is a modified version of
:func:`hyperspy.io.assign_signal_subclass`.
Parameters
----------
dtype
Data type of signal data.
signal_dimension
Number of signal dimensions.
signal_type
Signal type. Options are '', 'EBSD', 'EBSDMasterPattern'.
lazy
Open the data lazily without actually reading the data from disc
until required. Allows opening arbitrary sized datasets. Default
is False.
Returns
-------
Signal or subclass
"""
# Check if parameter values are allowed
if (
"float" in dtype.name
or "int" in dtype.name
or "void" in dtype.name
or "bool" in dtype.name
or "object" in dtype.name
):
dtype = "real"
else:
raise ValueError(f"Data type '{dtype.name}' not understood")
if not isinstance(signal_dimension, int) or signal_dimension < 0:
raise ValueError(
f"Signal dimension must be a positive integer and not '{signal_dimension}'"
)
# Get possible signal classes
signals = {
key: value
for key, value in find_subclasses(kikuchipy.signals, BaseSignal).items()
if value._lazy == lazy
}
# Get signals matching both input signal's dtype and signal dimension
dtype_matches = [s for s in signals.values() if s._dtype == dtype]
dtype_dim_matches = [
s for s in dtype_matches if s._signal_dimension == signal_dimension
]
dtype_dim_type_matches = [
s
for s in dtype_dim_matches
if signal_type == s._signal_type or signal_type in s._alias_signal_types
]
if len(dtype_dim_type_matches) == 1:
matches = dtype_dim_type_matches
else:
raise ValueError(
f"No kikuchipy signals match dtype '{dtype}', signal dimension "
f"'{signal_dimension}' and signal_type '{signal_type}'."
)
return matches[0]
def _save(
filename: Union[str, Path],
signal,
overwrite: Optional[bool] = None,
add_scan: Optional[bool] = None,
**kwargs,
):
"""Write signal to a file in a supported format.
This function is a modified version of :func:`hyperspy.io.save`.
Parameters
----------
filename
File path including name of new file.
signal : EBSD or LazyEBSD
Signal instance.
overwrite
Whether to overwrite file or not if it already exists.
add_scan
Whether to add the signal to an already existing h5ebsd file or
not. If the file does not exist the signal is written to a new
file.
**kwargs :
Keyword arguments passed to the writer.
"""
filename = str(filename)
ext = os.path.splitext(filename)[1][1:]
if ext == "": # Will write to kikuchipy's h5ebsd format
ext = "h5"
filename = filename + "." + ext
writer = None
for plugin in plugins:
if ext.lower() in plugin.file_extensions and plugin.writes:
writer = plugin
break
if writer is None:
raise ValueError(
f"'{ext}' does not correspond to any supported format. Supported file "
f"extensions are: '{strlist2enumeration(default_write_ext)}'"
)
else:
sd = signal.axes_manager.signal_dimension
nd = signal.axes_manager.navigation_dimension
if writer.writes is not True and (sd, nd) not in writer.writes:
# Get writers that can write this data
writing_plugins = []
for plugin in plugins:
if (
plugin.writes is True
or plugin.writes is not False
and (sd, nd) in plugin.writes
):
writing_plugins.append(plugin)
raise ValueError(
"This file format cannot write this data. The following formats can: "
f"{strlist2enumeration(writing_plugins)}"
)
_ensure_directory(filename)
is_file = os.path.isfile(filename)
# Check if we are to add signal to an already existing h5ebsd file
if (
writer.format_name == "kikuchipy_h5ebsd"
and overwrite is not True
and is_file
):
if add_scan is None:
q = "Add scan to '{}' (y/n)?\n".format(filename)
add_scan = _get_input_bool(q)
if add_scan:
overwrite = True # So that the 2nd statement below triggers
kwargs["add_scan"] = add_scan
# Determine if signal is to be written to file or not
if overwrite is None:
write = overwrite_method(filename) # Ask what to do
elif overwrite is True or (overwrite is False and not is_file):
write = True # Write the file
elif overwrite is False and is_file:
write = False # Don't write the file
else:
raise ValueError(
"overwrite parameter can only be None, True or False, and not "
f"{overwrite}"
)
# Finally, write file
if write:
writer.file_writer(filename, signal, **kwargs)
directory, filename = os.path.split(os.path.abspath(filename))
signal.tmp_parameters.set_item("folder", directory)
signal.tmp_parameters.set_item("filename", os.path.splitext(filename)[0])
signal.tmp_parameters.set_item("extension", ext)