/
ebsd.py
1036 lines (898 loc) · 37.5 KB
/
ebsd.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Copyright 2019-2020 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 datetime
import gc
import logging
import numbers
import os
import sys
import dask.array as da
import dask.diagnostics as dd
from hyperspy._signals.signal2d import Signal2D
from hyperspy._lazy_signals import LazySignal2D
from hyperspy.learn.mva import LearningResults
from hyperspy.misc.utils import DictionaryTreeBrowser
from h5py import File
import numpy as np
from pyxem.signals.diffraction2d import Diffraction2D
from kikuchipy.io._io import save
import kikuchipy as kp
_logger = logging.getLogger(__name__)
class EBSD(Signal2D):
_signal_type = "EBSD"
_alias_signal_types = ["electron_backscatter_diffraction"]
_lazy = False
def __init__(self, *args, **kwargs):
"""Create an :class:`~kikuchipy.signals.ebsd.EBSD` object from a
:class:`hyperspy.signals.Signal2D` or a :class:`numpy.ndarray`.
"""
Signal2D.__init__(self, *args, **kwargs)
# Update metadata if object is initialised from numpy array
if not self.metadata.has_item(kp.util.io.metadata_nodes(sem=False)):
md = self.metadata.as_dictionary()
md.update(kp.util.io.kikuchipy_metadata().as_dictionary())
self.metadata = DictionaryTreeBrowser(md)
if not self.metadata.has_item("Sample.Phases"):
self.set_phase_parameters()
def set_experimental_parameters(
self,
detector=None,
azimuth_angle=None,
elevation_angle=None,
sample_tilt=None,
working_distance=None,
binning=None,
exposure_time=None,
grid_type=None,
gain=None,
frame_number=None,
frame_rate=None,
scan_time=None,
beam_energy=None,
xpc=None,
ypc=None,
zpc=None,
static_background=None,
manufacturer=None,
version=None,
microscope=None,
magnification=None,
):
"""Set experimental parameters in signal ``metadata``.
Parameters
----------
azimuth_angle : float, optional
Azimuth angle of the detector in degrees. If the azimuth is
zero, the detector is perpendicular to the tilt axis.
beam_energy : float, optional
Energy of the electron beam in kV.
binning : int, optional
Camera binning.
detector : str, optional
Detector manufacturer and model.
elevation_angle : float, optional
Elevation angle of the detector in degrees. If the elevation
is zero, the detector is perpendicular to the incident beam.
exposure_time : float, optional
Camera exposure time in µs.
frame_number : float, optional
Number of patterns integrated during acquisition.
frame_rate : float, optional
Frames per s.
gain : float, optional
Camera gain, typically in dB.
grid_type : str, optional
Scan grid type, only square grid is supported.
manufacturer : str, optional
Manufacturer of software used to collect patterns.
microscope : str, optional
Microscope used to collect patterns.
magnification : int, optional
Microscope magnification at which patterns were collected.
sample_tilt : float, optional
Sample tilt angle from horizontal in degrees.
scan_time : float, optional
Scan time in s.
static_background : :class:`numpy.ndarray`, optional
Static background pattern.
version : str, optional
Version of software used to collect patterns.
working_distance : float, optional
Working distance in mm.
xpc : float, optional
Pattern centre horizontal coordinate with respect to
detector centre, as viewed from the detector to the sample.
ypc : float, optional
Pattern centre vertical coordinate with respect to
detector centre, as viewed from the detector to the sample.
zpc : float, optional
Specimen to scintillator distance.
See Also
--------
set_phase_parameters
Examples
--------
>>> import kikuchipy as kp
>>> ebsd_node = kp.util.io.metadata_nodes(sem=False)
>>> print(s.metadata.get_item(ebsd_node + '.xpc')
1.0
>>> s.set_experimental_parameters(xpc=0.50726)
>>> print(s.metadata.get_item(ebsd_node + '.xpc'))
0.50726
"""
md = self.metadata
sem_node, ebsd_node = kp.util.io.metadata_nodes()
kp.util.general._write_parameters_to_dictionary(
{
"beam_energy": beam_energy,
"magnification": magnification,
"microscope": microscope,
"working_distance": working_distance,
},
md,
sem_node,
)
kp.util.general._write_parameters_to_dictionary(
{
"azimuth_angle": azimuth_angle,
"binning": binning,
"detector": detector,
"elevation_angle": elevation_angle,
"exposure_time": exposure_time,
"frame_number": frame_number,
"frame_rate": frame_rate,
"gain": gain,
"grid_type": grid_type,
"manufacturer": manufacturer,
"version": version,
"sample_tilt": sample_tilt,
"scan_time": scan_time,
"xpc": xpc,
"ypc": ypc,
"zpc": zpc,
"static_background": static_background,
},
md,
ebsd_node,
)
def set_phase_parameters(
self,
number=1,
atom_coordinates=None,
formula=None,
info=None,
lattice_constants=None,
laue_group=None,
material_name=None,
point_group=None,
setting=None,
space_group=None,
symmetry=None,
):
"""Set parameters for one phase in signal ``metadata``, using
the International Tables for Crystallography, Volume A.
A phase node with default values is created if none is present
in the ``metadata`` when this method is called.
Parameters
----------
number : int, optional
Phase number.
atom_coordinates : dict, optional
Dictionary of dictionaries with one or more of the atoms in
the unit cell, on the form `{'1': {'atom': 'Ni',
'coordinates': [0, 0, 0], 'site_occupation': 1,
'debye_waller_factor': 0}, '2': {'atom': 'O',... etc.`
`debye_waller_factor` in units of nm^2, and
`site_occupation` in range [0, 1].
formula : str, optional
Phase formula, e.g. 'Fe2' or 'Ni'.
info : str, optional
Whatever phase info the user finds relevant.
lattice_constants : :class:`numpy.ndarray` or list of\
floats, optional
Six lattice constants a, b, c, alpha, beta, gamma.
laue_group : str, optional
Phase Laue group.
material_name : str, optional
Name of material.
point_group : str, optional
Phase point group.
setting : int, optional
Space group's origin setting.
space_group : int, optional
Number between 1 and 230.
symmetry : int, optional
Phase symmetry.
See Also
--------
set_experimental_parameters
Examples
--------
>>> print(s.metadata.Sample.Phases.Number_1.atom_coordinates.
Number_1)
├── atom =
├── coordinates = array([0., 0., 0.])
├── debye_waller_factor = 0.0
└── site_occupation = 0.0
>>> s.set_phase_parameters(
number=1, atom_coordinates={
'1': {'atom': 'Ni', 'coordinates': [0, 0, 0],
'site_occupation': 1,
'debye_waller_factor': 0.0035}})
>>> print(s.metadata.Sample.Phases.Number_1.atom_coordinates.
Number_1)
├── atom = Ni
├── coordinates = array([0., 0., 0.])
├── debye_waller_factor = 0.0035
└── site_occupation = 1
"""
# Ensure atom coordinates are numpy arrays
if atom_coordinates is not None:
for phase, val in atom_coordinates.items():
atom_coordinates[phase]["coordinates"] = np.array(
atom_coordinates[phase]["coordinates"]
)
inputs = {
"atom_coordinates": atom_coordinates,
"formula": formula,
"info": info,
"lattice_constants": lattice_constants,
"laue_group": laue_group,
"material_name": material_name,
"point_group": point_group,
"setting": setting,
"space_group": space_group,
"symmetry": symmetry,
}
# Remove None values
phase = {k: v for k, v in inputs.items() if v is not None}
kp.util.phase._update_phase_info(self.metadata, phase, number)
def set_scan_calibration(self, step_x=1.0, step_y=1.0):
"""Set the step size in um.
Parameters
----------
step_x : float
Scan step size in um per pixel in horizontal direction.
step_y : float
Scan step size in um per pixel in vertical direction.
See Also
--------
set_detector_calibration
Examples
--------
>>> print(s.axes_manager.['x'].scale) # Default value
1.0
>>> s.set_scan_calibration(step_x=1.5) # Microns
>>> print(s.axes_manager['x'].scale)
1.5
"""
x, y = self.axes_manager.navigation_axes
x.name, y.name = ("x", "y")
x.scale, y.scale = (step_x, step_y)
x.units, y.units = ["\u03BC" + "m"] * 2
def set_detector_calibration(self, delta):
"""Set detector pixel size in microns. The offset is set to the
the detector centre.
Parameters
----------
delta : float
Detector pixel size in microns.
See Also
--------
set_scan_calibration
Examples
--------
>>> print(s.axes_manager['dx'].scale) # Default value
1.0
>>> s.set_detector_calibration(delta=70.)
>>> print(s.axes_manager['dx'].scale)
70.0
"""
centre = np.array(self.axes_manager.signal_shape) / 2 * delta
dx, dy = self.axes_manager.signal_axes
dx.units, dy.units = ["\u03BC" + "m"] * 2
dx.scale, dy.scale = (delta, delta)
dx.offset, dy.offset = -centre
def static_background_correction(
self, operation="subtract", relative=False, static_bg=None
):
"""Correct static background inplace by subtracting/dividing by
a static background pattern.
Resulting pattern intensities are rescaled keeping relative
intensities or not and stretched to fill the available grey
levels in the patterns' :class:`numpy.dtype` range.
Parameters
----------
operation : 'subtract' or 'divide', optional
Subtract (default) or divide by static background pattern.
relative : bool, optional
Keep relative intensities between patterns (default is
``False``).
static_bg : :class:`numpy.ndarray`,\
:class:`dask.array.Array` or None, optional
Static background pattern. If not passed we try to read it
from the signal metadata.
See Also
--------
dynamic_background_correction
Examples
--------
Assuming that a static background pattern with same shape and
data type (e.g. 8-bit unsigned integer, ``uint8``) as patterns
is available in signal metadata:
>>> import kikuchipy as kp
>>> ebsd_node = kp.util.io.metadata_nodes(sem=False)
>>> print(s.metadata.get_item(ebsd_node + '.static_background'))
[[84 87 90 ... 27 29 30]
[87 90 93 ... 27 28 30]
[92 94 97 ... 39 28 29]
...
[80 82 84 ... 36 30 26]
[79 80 82 ... 28 26 26]
[76 78 80 ... 26 26 25]]
Static background can be corrected by subtracting or dividing
this background from each pattern while keeping relative
intensities between patterns (or not).
>>> s.static_background_correction(
operation='subtract', relative=True)
If metadata has no background pattern, this must be passed in
the ``static_bg`` parameter as a numpy or dask array.
"""
dtype_out = self.data.dtype.type
# Set up background pattern
if not isinstance(static_bg, (np.ndarray, da.Array)):
try:
md = self.metadata
ebsd_node = kp.util.io.metadata_nodes(sem=False)
static_bg = da.from_array(
md.get_item(ebsd_node + ".static_background"),
chunks="auto",
)
except AttributeError:
raise OSError(
"Static background is not a numpy or dask array or could "
"not be read from signal metadata."
)
if dtype_out != static_bg.dtype:
raise ValueError(
f"Static background dtype_out {static_bg.dtype} is not the "
f"same as pattern dtype_out {dtype_out}."
)
pat_shape = self.axes_manager.signal_shape[::-1]
bg_shape = static_bg.shape
if bg_shape != pat_shape:
raise OSError(
f"Pattern {pat_shape} and static background {bg_shape} shapes "
"are not identical."
)
dtype = np.float32
static_bg = static_bg.astype(dtype)
# Get min./max. input patterns intensity after correction
if relative: # Scale relative to min./max. intensity in scan
signal_min = self.data.min(axis=(0, 1))
signal_max = self.data.max(axis=(0, 1))
if operation == "subtract":
imin = (signal_min - static_bg).astype(dtype).min()
imax = (signal_max - static_bg).astype(dtype).max()
else: # Divide
imin = (signal_min / static_bg).astype(dtype).min()
imax = (signal_max / static_bg).astype(dtype).max()
in_range = (imin, imax)
else: # Scale relative to min./max. intensity in each pattern
in_range = None
# Create dask array of signal patterns and do processing on this
dask_array = kp.util.dask._get_dask_array(signal=self, dtype=dtype)
# Correct static background and rescale intensities chunk by chunk
corrected_patterns = dask_array.map_blocks(
kp.util.experimental._static_background_correction_chunk,
static_bg=static_bg,
operation=operation,
in_range=in_range,
dtype_out=dtype_out,
dtype=dtype_out,
)
# Overwrite signal patterns
if not self._lazy:
with dd.ProgressBar():
print("Static background correction:", file=sys.stdout)
corrected_patterns.store(self.data, compute=True)
else:
self.data = corrected_patterns
def dynamic_background_correction(self, operation="subtract", sigma=None):
"""Correct dynamic background inplace by subtracting or dividing
by a blurred version of each pattern.
Resulting pattern intensities are rescaled to fill the
available grey levels in the patterns' :class:`numpy.dtype`
range.
Parameters
----------
operation : 'subtract' or 'divide', optional
Subtract (default) or divide by dynamic background pattern.
sigma : int, float or None, optional
Standard deviation of the gaussian kernel. If None
(default), a deviation of pattern width/30 is chosen.
See Also
--------
static_background_correction
Examples
--------
Traditional background correction includes static and dynamic
corrections, loosing relative intensities between patterns after
dynamic corrections (whether ``relative`` is set to ``True`` or
``False`` in :meth:`~static_background_correction`):
>>> s.static_background_correction(operation='subtract')
>>> s.dynamic_background_correction(
operation='subtract', sigma=2.0)
"""
dtype_out = self.data.dtype.type
dtype = np.float32
# Create dask array of signal patterns and do processing on this
dask_array = kp.util.dask._get_dask_array(signal=self, dtype=dtype)
if sigma is None:
sigma = self.axes_manager.signal_axes[0].size / 30
corrected_patterns = dask_array.map_blocks(
kp.util.experimental._dynamic_background_correction_chunk,
operation=operation,
sigma=sigma,
dtype_out=dtype_out,
dtype=dtype_out,
)
# Overwrite signal patterns
if not self._lazy:
with dd.ProgressBar():
print("Dynamic background correction:", file=sys.stdout)
corrected_patterns.store(self.data, compute=True)
else:
self.data = corrected_patterns
def rescale_intensities(self, relative=False, dtype_out=None):
"""Rescale pattern intensities inplace to desired
:class:`numpy.dtype` range specified by ``dtype_out`` keeping
relative intensities or not.
This method makes use of
:func:`skimage.exposure.rescale_intensity`.
Parameters
----------
relative : bool, optional
Keep relative intensities between patterns, default is
``False``.
dtype_out : numpy.dtype, optional
Data type of rescaled patterns, default is input patterns'
data type.
See Also
--------
adaptive_histogram_equalization
Examples
--------
Pattern intensities are stretched to fill the available grey
levels in the input patterns' data type range or any
:class:`numpy.dtype` range passed to ``dtype_out``, either
keeping relative intensities between patterns or not:
>>> print(s.data.dtype_out, s.data.min(), s.data.max(),
s.inav[0, 0].data.min(), s.inav[0, 0].data.max())
uint8 20 254 24 233
>>> s2 = s.deepcopy()
>>> s.rescale_intensities(dtype_out=np.uint16)
>>> print(s.data.dtype_out, s.data.min(), s.data.max(),
s.inav[0, 0].data.min(), s.inav[0, 0].data.max())
uint16 0 65535 0 65535
>>> s2.rescale_intensities(relative=True)
>>> print(s2.data.dtype_out, s2.data.min(), s2.data.max(),
s2.inav[0, 0].data.min(), s2.inav[0, 0].data.max())
uint8 0 255 4 232
"""
if dtype_out is None:
dtype_out = self.data.dtype.type
# Determine min./max. intensity of input pattern to rescale to
if relative: # Scale relative to min./max. intensity in scan
in_range = (self.data.min(), self.data.max())
else: # Scale relative to min./max. intensity in each pattern
in_range = None
# Create dask array of signal patterns and do processing on this
dask_array = kp.util.dask._get_dask_array(signal=self)
# Rescale patterns
rescaled_patterns = dask_array.map_blocks(
kp.util.experimental._rescale_pattern_chunk,
in_range=in_range,
dtype_out=dtype_out,
dtype=dtype_out,
)
# Overwrite signal patterns
if not self._lazy:
with dd.ProgressBar():
if self.data.dtype != rescaled_patterns.dtype:
self.data = self.data.astype(dtype_out)
print("Rescaling patterns:", file=sys.stdout)
rescaled_patterns.store(self.data, compute=True)
else:
self.data = rescaled_patterns
def adaptive_histogram_equalization(
self, kernel_size=None, clip_limit=0, nbins=128
):
"""Local contrast enhancement inplace with adaptive histogram
equalization.
This method makes use of
:func:`skimage.exposure.equalize_adapthist`.
Parameters
----------
kernel_size : int or list-like, optional
Shape of contextual regions for adaptive histogram
equalization, default is 1/4 of pattern height and 1/4 of
pattern width.
clip_limit : float, optional
Clipping limit, normalized between 0 and 1 (higher values
give more contrast). Default is 0.
nbins : int, optional
Number of gray bins for histogram ("data range"), default is
128.
See also
--------
dynamic_background_correction, rescale_intensities,
static_background_correction
Examples
--------
To best understand how adaptive histogram equalization works,
we plot the histogram of the same pattern before and after
equalization:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> s2 = s.inav[0, 0]
>>> s2.adaptive_histogram_equalization()
>>> imin = np.iinfo(s.data.dtype_out).min
>>> imax = np.iinfo(s.data.dtype_out).max + 1
>>> hist, _ = np.histogram(s.inav[0, 0].data, bins=imax,
range=(imin, imax))
>>> hist2, _ = np.histogram(s2.inav[0, 0].data, bins=imax,
range=(imin, imax))
>>> fig, ax = plt.subplots(nrows=2, ncols=2)
>>> ax[0, 0].imshow(s.inav[0, 0].data)
>>> ax[1, 0].plot(hist)
>>> ax[0, 1].imshow(s2.inav[0, 0].data)
>>> ax[1, 1].plot(hist2)
Notes
-----
* It is recommended to perform adaptive histogram equalization
only *after* static and dynamic background corrections,
otherwise some unwanted darkening towards the edges might
occur.
* The default kernel size might not fit all pattern sizes, so it
may be necessary to search for the optimal kernel size.
"""
# Determine kernel size (shape of contextual region)
sig_shape = self.axes_manager.signal_shape
if kernel_size is None:
kernel_size = (sig_shape[0] // 4, sig_shape[1] // 4)
elif isinstance(kernel_size, numbers.Number):
kernel_size = (kernel_size,) * self.axes_manager.signal_dimension
elif len(kernel_size) != self.axes_manager.signal_dimension:
raise ValueError(f"Incorrect value of `kernel_size`: {kernel_size}")
kernel_size = [int(k) for k in kernel_size]
# Create dask array of signal patterns and do processing on this
dask_array = kp.util.dask._get_dask_array(signal=self)
# Local contrast enhancement
equalized_patterns = dask_array.map_blocks(
kp.util.experimental._adaptive_histogram_equalization_chunk,
kernel_size=kernel_size,
clip_limit=clip_limit,
nbins=nbins,
dtype=self.data.dtype,
)
# Overwrite signal patterns
if not self._lazy:
with dd.ProgressBar():
print("Adaptive histogram equalization:", file=sys.stdout)
equalized_patterns.store(self.data, compute=True)
else:
self.data = equalized_patterns
def virtual_backscatter_electron_imaging(self, roi, **kwargs):
"""Plot an interactive virtual backscatter electron (VBSE)
image formed from detector intensities within a specified and
adjustable region of interest (ROI).
Adapted from
meth:`pyxem.signals.diffraction2d.Diffraction2D.plot_interactive_virtual_image`.
Parameters
----------
roi : hyperspy.roi.BaseInteractiveROI
Any interactive ROI detailed in HyperSpy.
**kwargs:
Keyword arguments to be passed to
:meth:`hyperspy.signal.BaseSignal.plot`.
Examples
--------
>>> import hyperspy.api as hs
>>> roi = hs.roi.RectangularROI(
left=0, right=5, top=0, bottom=5)
>>> s.virtual_backscatter_electron_imaging(roi)
"""
return Diffraction2D.plot_interactive_virtual_image(self, roi, **kwargs)
def get_virtual_image(self, roi):
"""Return a virtual backscatter electron (VBSE) image
formed from detector intensities within a region of interest
(ROI) on the detector.
Adapted from
:meth:`pyxem.signals.diffraction2d.Diffraction2D.get_virtual_image`.
Parameters
----------
roi : hyperspy.roi.BaseInteractiveROI
Any interactive ROI detailed in HyperSpy.
Returns
-------
virtual_image : hyperspy.signal.BaseSignal
VBSE image formed from detector intensities within an ROI
on the detector.
Examples
--------
>>> import hyperspy.api as hs
>>> roi = hs.roi.RectangularROI(
left=0, right=5, top=0, bottom=5)
>>> vbse_image = s.get_virtual_image(roi)
"""
return Diffraction2D.get_virtual_image(self, roi)
def save(self, filename=None, overwrite=None, extension=None, **kwargs):
"""Write signal to the specified format.
The function gets the format from the extension: `h5`, `hdf5` or
`h5ebsd` for KikuchiPy's specification of the the h5ebsd
format, `dat` for the NORDIF binary format or `hspy` for
HyperSpy's HDF5 specification. If no extension is provided the
signal is written to a file in KikuchiPy's h5ebsd format. Each
format accepts a different set of parameters.
For details see the specific format documentation under "See
Also" below.
This method is a modified version of HyperSpy's function
:meth:`hyperspy.signal.BaseSignal.save`.
Parameters
----------
filename : str or None, optional
If ``None`` (default) and ``tmp_parameters.filename`` and
``tmp_parameters.folder`` in signal metadata are defined,
the filename and path will be taken from there. A valid
extension can be provided e.g. "data.h5", see ``extension``.
overwrite : None or bool, optional
If ``None`` and the file exists, it will query the user. If
``True`` (``False``) it (does not) overwrite the file if it
exists.
extension : None, 'h5', 'hdf5', 'h5ebsd', 'dat' or 'hspy',\
optional
Extension of the file that defines the file format. 'h5',
'hdf5' and 'h5ebsd' are equivalent. If ``None``, the
extension is determined from the following list in this
order: i) the filename, ii) ``tmp_parameters.extension`` or
iii) 'h5' (KikuchiPy's h5ebsd format).
**kwargs :
Keyword arguments passed to writer.
See Also
--------
kikuchipy.io.plugins.h5ebsd.file_writer,\
kikuchipy.io.plugins.nordif.file_writer
"""
if filename is None:
if self.tmp_parameters.has_item(
"filename"
) and self.tmp_parameters.has_item("folder"):
filename = os.path.join(
self.tmp_parameters.folder, self.tmp_parameters.filename
)
extension = (
self.tmp_parameters.extension
if not extension
else extension
)
elif self.metadata.has_item("General.original_filename"):
filename = self.metadata.General.original_filename
else:
raise ValueError("Filename not defined.")
if extension is not None:
basename, ext = os.path.splitext(filename)
filename = basename + "." + extension
save(filename, self, overwrite=overwrite, **kwargs)
def decomposition(self, *args, **kwargs):
super().decomposition(*args, **kwargs)
self.__class__ = EBSD
def get_decomposition_model(self, components=None, dtype_out=np.float16):
"""Return the model signal generated with the selected number of
principal components from a decomposition.
Calls HyperSpy's
:meth:`hyperspy.learn.mva.MVA.get_decomposition_model`.
Learning results are preconditioned before this call, doing the
following: (1) set :class:`numpy.dtype` to desired
``dtype_out``, (2) remove unwanted components, and (3) rechunk,
if :class:`dask.array.Array`, to suitable chunks.
Parameters
----------
components : None, int or list of ints, optional
If ``None`` (default), rebuilds the signal from all
components. If ``int``, rebuilds signal from ``components``
in range 0-given ``int``. If list of ``ints``, rebuilds
signal from only ``components`` in given list.
dtype_out : numpy.float16, numpy.float32, numpy.float64,\
optional
Data to cast learning results to (default is
:class:`numpy.float16`). Note that HyperSpy casts them
to :class:`numpy.float64`.
Returns
-------
s_model : :class:`~kikuchipy.signals.ebsd.EBSD` or \
:class:`~kikuchipy.signals.ebsd.LazyEBSD`
"""
# Keep original results to revert back after updating
factors_orig = self.learning_results.factors.copy()
loadings_orig = self.learning_results.loadings.copy()
# Change data type, keep desired components and rechunk if lazy
(
self.learning_results.factors,
self.learning_results.loadings,
) = kp.util.decomposition._update_learning_results(
learning_results=self.learning_results,
dtype_out=dtype_out,
components=components,
)
# Call HyperSpy's function
s_model = super().get_decomposition_model()
# Revert learning results to original results
self.learning_results.factors = factors_orig
self.learning_results.loadings = loadings_orig
# Revert class
assign_class = EBSD
if self._lazy:
assign_class = LazyEBSD
self.__class__ = assign_class
s_model.__class__ = assign_class
# Remove learning results from model signal
s_model.learning_results = LearningResults()
return s_model
def rebin(self, new_shape=None, scale=None, crop=True, out=None):
s_out = super().rebin(
new_shape=new_shape, scale=scale, crop=crop, out=out
)
return_signal = True
if s_out is None:
s_out = out
return_signal = False
# Update binning in metadata to signal dimension with largest or lowest
# binning if downscaling or upscaling, respectively
md = s_out.metadata
ebsd_node = kp.util.io.metadata_nodes(sem=False)
if scale is None:
sx, sy = self.axes_manager.signal_shape
signal_idx = self.axes_manager.signal_indices_in_array
scale = (
sx / new_shape[signal_idx[0]],
sy / new_shape[signal_idx[1]],
)
upscaled_dimensions = np.where(np.array(scale) < 1)[0]
if len(upscaled_dimensions):
new_binning = np.min(scale)
else:
new_binning = np.max(scale)
original_binning = md.get_item(ebsd_node + ".binning")
md.set_item(ebsd_node + ".binning", original_binning * new_binning)
if return_signal:
return s_out
def as_lazy(self, *args, **kwargs):
"""Create a :class:`~kikuchipy.signals.ebsd.LazyEBSD` object
from an :class:`~kikuchipy.signals.ebsd.EBSD` object.
Returns
-------
lazy_signal : :class:`~kikuchipy.signals.ebsd.LazyEBSD`
Lazy signal.
"""
lazy_signal = super().as_lazy(*args, **kwargs)
lazy_signal.__class__ = LazyEBSD
lazy_signal.__init__(**lazy_signal._to_dictionary())
return lazy_signal
def change_dtype(self, dtype, rechunk=True):
super().change_dtype(dtype=dtype, rechunk=rechunk)
self.__class__ = EBSD
class LazyEBSD(EBSD, LazySignal2D):
_lazy = True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def change_dtype(self, dtype, rechunk=True):
super().change_dtype(dtype=dtype, rechunk=rechunk)
self.__class__ = LazyEBSD
def compute(self, *args, **kwargs):
with dd.ProgressBar(*args, **kwargs):
self.data = self.data.compute(*args, **kwargs)
gc.collect()
self.__class__ = EBSD
self._lazy = False
def decomposition(self, *args, **kwargs):
super().decomposition(*args, **kwargs)
self.__class__ = LazyEBSD
def get_decomposition_model_write(
self,
components=None,
dtype_learn=np.float16,
mbytes_chunk=100,
dir_out=None,
fname_out=None,
):
"""Write the model signal generated from the selected number of
principal components directly to a .hspy file.
The model signal intensities are rescaled to the original
signals' data type range, keeping relative intensities.
Parameters
----------
components : None, int or list of ints, optional
If ``None`` (default), rebuilds the signal from all
``components``. If ``int``, rebuilds signal from
``components`` in range 0-given ``int``. If list of ints,
rebuilds signal from only ``components`` in given list.
dtype_learn : :class:`numpy.float16`,\
:class:`numpy.float32` or :class:`numpy.float64`,\
optional
Data type to set learning results to (default is
:class:`numpy.float16`) before multiplication.
mbytes_chunk : int, optional
Size of learning results chunks in MB, default is 100 MB as
suggested in the Dask documentation.
dir_out : str, optional
Directory to place output signal in.
fname_out : str, optional
Name of output signal file.
Notes
-----
Multiplying the learning results' factors and loadings in memory
to create the model signal cannot sometimes be done due to too
large matrices. Here, instead, learning results are written to
file, read into dask arrays and multiplied using
:func:`dask.array.matmul`, out of core.
"""
# Change data type, keep desired components and rechunk if lazy
factors, loadings = kp.util.decomposition._update_learning_results(
self.learning_results, components=components, dtype_out=dtype_learn
)
# Write learning results to HDF5 file
if dir_out is None:
try:
dir_out = self.original_metadata.General.original_filepath
except AttributeError:
raise AttributeError("Output directory has to be specified")
t_str = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
file_learn = os.path.join(dir_out, "learn_" + t_str + ".h5")
with File(file_learn, mode="w") as f:
f.create_dataset(name="factors", data=factors)