-
Notifications
You must be signed in to change notification settings - Fork 25
/
__init__.py
280 lines (232 loc) · 9.09 KB
/
__init__.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
# -*- coding: utf-8 -*-
# Copyright 2019-2021 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/>.
"""Test data.
Some datasets must be downloaded from the web. For more test datasets,
see :doc:`open datasets <open_datasets>`.
"""
import os
from pathlib import Path
from typing import Union
import pooch as ppooch
from kikuchipy.signals import EBSD, EBSDMasterPattern
from kikuchipy import load
from kikuchipy.release import version
from kikuchipy.data._registry import registry, registry_urls
__all__ = [
"nickel_ebsd_small",
"nickel_ebsd_large",
"nickel_ebsd_master_pattern_small",
"silicon_ebsd_moving_screen_in",
"silicon_ebsd_moving_screen_out5mm",
"silicon_ebsd_moving_screen_out10mm",
]
fetcher = ppooch.create(
path=ppooch.os_cache("kikuchipy"),
base_url="",
version=version.replace(".dev", "+"),
env="KIKUCHIPY_DATA_DIR",
registry=registry,
urls=registry_urls,
)
cache_data_path = fetcher.path.joinpath("data")
package_data_path = Path(os.path.abspath(os.path.dirname(__file__)))
def _has_hash(path, expected_hash):
"""Check if the provided path has the expected hash."""
if not os.path.exists(path):
return False
else:
return ppooch.file_hash(path) == expected_hash
def _cautious_downloader(url, output_file, pooch):
if pooch.allow_download:
delattr(pooch, "allow_download")
# HTTPDownloader() requires tqdm, a HyperSpy dependency, so
# adding it to our dependencies doesn't cost anything
download = ppooch.HTTPDownloader(progressbar=True)
download(url, output_file, pooch)
else:
raise ValueError(
"The dataset must be (re)downloaded from the kikuchipy-data "
"repository on GitHub (https://github.com/pyxem/kikuchipy-data) to "
"your local cache with the pooch Python package. Pass "
"`allow_download=True` to allow this download."
)
def _fetch(filename: str, allow_download: bool = False):
resolved_path = os.path.join(package_data_path, "..", filename)
expected_hash = registry[filename]
if _has_hash(resolved_path, expected_hash): # File already in data module
return resolved_path
else: # Pooch must download the data to the local cache
fetcher.allow_download = allow_download # Extremely ugly
resolved_path = fetcher.fetch(filename, downloader=_cautious_downloader)
return resolved_path
def _load(filename: str, **kwargs) -> Union[EBSD, EBSDMasterPattern]:
allow_download = kwargs.pop("allow_download", False)
return load(_fetch(filename, allow_download=allow_download), **kwargs)
def nickel_ebsd_small(**kwargs) -> EBSD:
"""9 EBSD patterns in a (3, 3) navigation shape of (60, 60) detector
pixels from Nickel, acquired on a NORDIF UF-1100 detector
:cite:`aanes2019electron`.
Parameters
----------
kwargs
Keyword arguments passed to :func:`~kikuchipy.io._io.load`.
Returns
-------
signal : EBSD
EBSD signal.
"""
return _load(filename="data/kikuchipy_h5ebsd/patterns.h5", **kwargs)
def nickel_ebsd_master_pattern_small(**kwargs) -> EBSDMasterPattern:
"""(401, 401) `uint8` square Lambert or stereographic projection of the
northern and southern hemisphere of a Nickel master pattern at 20
keV accelerating voltage.
Parameters
----------
kwargs
Keyword arguments passed to :func:`~kikuchipy.io._io.load`.
Returns
-------
signal : EBSDMasterPattern
EBSD master pattern signal.
Notes
-----
Initially generated using the EMsoft EMMCOpenCL and EMEBSDMaster
programs. The included file was rewritten to disk with
:mod:`h5py`, where the master patterns' data type is converted from
`float32` to `uint8` with
:meth:`~kikuchipy.signals.EBSDMasterPattern.rescale_intensity`, all
datasets were written with
:meth:`~kikuchipy.io.plugins.h5ebsd.dict2h5ebsdgroup` with
keyword arguments `compression="gzip"` and `compression_opts=9`. All
other HDF5 groups and datasets are the same as in the original file.
"""
fname = "data/emsoft_ebsd_master_pattern/ni_mc_mp_20kv_uint8_gzip_opts9.h5"
return _load(fname, **kwargs)
def nickel_ebsd_large(allow_download: bool = False, **kwargs) -> EBSD:
"""4125 EBSD patterns in a (55, 75) navigation shape of (60, 60)
detector pixels from Nickel, acquired on a NORDIF UF-1100 detector
:cite:`aanes2019electron`.
Parameters
----------
allow_download : bool
Whether to allow downloading the dataset from the kikuchipy-data
GitHub repository (https://github.com/pyxem/kikuchipy-data) to
the local cache with the pooch Python package. Default is False.
kwargs
Keyword arguments passed to :func:`~kikuchipy.io._io.load`.
Returns
-------
signal : EBSD
EBSD signal.
"""
return _load(
filename="data/nickel_ebsd_large/patterns.h5",
allow_download=allow_download,
**kwargs,
)
def silicon_ebsd_moving_screen_in(allow_download: bool = False, **kwargs) -> EBSD:
"""One EBSD pattern of (480, 480) detector pixels from a single
crystal Silicon sample, acquired on a NORDIF UF-420 detector.
This pattern and two other patterns from the same sample position
but with 5 mm and 10 mm greater sample-screen-distances were
acquired to test the moving-screen projection center estimation
technique :cite:`hjelen1991electron`.
Parameters
----------
allow_download : bool
Whether to allow downloading the dataset from the kikuchipy-data
GitHub repository (https://github.com/pyxem/kikuchipy-data) to
the local cache with the pooch Python package. Default is False.
kwargs
Keyword arguments passed to :func:`~kikuchipy.io._io.load`.
Returns
-------
signal : EBSD
EBSD signal.
See Also
--------
silicon_ebsd_moving_screen_out5mm
silicon_ebsd_moving_screen_out10mm
"""
return _load(
filename="data/silicon_ebsd_moving_screen/si_in.h5",
allow_download=allow_download,
**kwargs,
)
def silicon_ebsd_moving_screen_out5mm(allow_download: bool = False, **kwargs) -> EBSD:
"""One EBSD pattern of (480, 480) detector pixels from a single
crystal Silicon sample, acquired on a NORDIF UF-420 detector.
This pattern and two other patterns from the same sample position
but with sample-screen-distances 5 mm shorter
(:func:`silicon_ebsd_moving_screen_in`) and 5 mm greater
(:func:`silicon_ebsd_moving_screen_out10mm`) were acquired to test
the moving-screen projection center estimation technique
:cite:`hjelen1991electron`.
Parameters
----------
allow_download : bool
Whether to allow downloading the dataset from the kikuchipy-data
GitHub repository (https://github.com/pyxem/kikuchipy-data) to
the local cache with the pooch Python package. Default is False.
kwargs
Keyword arguments passed to :func:`~kikuchipy.io._io.load`.
Returns
-------
signal : EBSD
EBSD signal.
See Also
--------
silicon_ebsd_moving_screen_in
silicon_ebsd_moving_screen_out10mm
"""
return _load(
filename="data/silicon_ebsd_moving_screen/si_out5mm.h5",
allow_download=allow_download,
**kwargs,
)
def silicon_ebsd_moving_screen_out10mm(allow_download: bool = False, **kwargs) -> EBSD:
"""One EBSD pattern of (480, 480) detector pixels from a single
crystal Silicon sample, acquired on a NORDIF UF-420 detector.
This pattern and two other patterns from the same sample position
but with sample-screen-distances 10 mm shorter
(:func:`silicon_ebsd_moving_screen_in`) and 5 mm shorter
(:func:`silicon_ebsd_moving_screen_out5mm`) were acquired to test
the moving-screen projection center estimation technique
:cite:`hjelen1991electron`.
Parameters
----------
allow_download : bool
Whether to allow downloading the dataset from the kikuchipy-data
GitHub repository (https://github.com/pyxem/kikuchipy-data) to
the local cache with the pooch Python package. Default is False.
kwargs
Keyword arguments passed to :func:`~kikuchipy.io._io.load`.
Returns
-------
signal : EBSD
EBSD signal.
See Also
--------
silicon_ebsd_moving_screen_in
silicon_ebsd_moving_screen_out5mm
"""
return _load(
filename="data/silicon_ebsd_moving_screen/si_out10mm.h5",
allow_download=allow_download,
**kwargs,
)