This page was generated from doc/user_guide/visualizing_patterns.ipynb. Interactive online version: Binder badge.

Visualizing patterns

The EBSD and EBSDMasterPattern signals have a powerful and versatile plot() method provided by HyperSpy. Its uses are greatly detailed in HyperSpy’s visualisation user guide. This section details example uses specific to EBSD and EBSDMasterPattern signals.

Let’s import the necessary libraries and a Nickel EBSD test data set [AHvHM19]:

[1]:
# Exchange inline for notebook or qt5 (from pyqt) for interactive plotting
%matplotlib inline

import hyperspy.api as hs
import kikuchipy as kp
import matplotlib.pyplot as plt
import numpy as np
from orix import io, plot, quaternion, vector
import skimage.exposure as ske
import skimage.transform as skt


# Use kp.load("data.h5") to load your own data
s = kp.data.nickel_ebsd_large(allow_download=True)  # External download
s
WARNING:hyperspy.api:The ipywidgets GUI elements are not available, probably because the hyperspy_gui_ipywidgets package is not installed.
WARNING:hyperspy.api:The traitsui GUI elements are not available, probably because the hyperspy_gui_traitsui package is not installed.
[1]:
<EBSD, title: patterns Scan 1, dimensions: (75, 55|60, 60)>

Virtual image

A virtual backscatter electron (VBSE) image created from any detector region of interest with the get_virtual_bse_intensity() method or get_rgb_image() explained in the virtual backscatter electron imaging section, can be used as a navigator for a scan s:

[3]:
vbse_gen = kp.generators.VirtualBSEGenerator(s)
print(vbse_gen)
print(vbse_gen.grid_shape)
VirtualBSEGenerator for <EBSD, title: patterns Scan 1, dimensions: (75, 55|60, 60)>
(5, 5)
[4]:
vbse_rgb = vbse_gen.get_rgb_image(r=(3, 1), b=(3, 2), g=(3, 3))
vbse_rgb
[4]:
<VirtualBSEImage, title: , dimensions: (|75, 55)>
[5]:
s.plot(navigator=vbse_rgb, cmap="viridis")
../_images/user_guide_visualizing_patterns_11_0.png
../_images/user_guide_visualizing_patterns_11_1.png

Any image

Images loaded into a Signal2D can be used as navigators, like a quality metric map like the image quality map calculated using get_image_quality():

[6]:
s.remove_static_background()
s.remove_dynamic_background()
Removing the static background:
[########################################] | 100% Completed |  0.2s
Removing the dynamic background:
[########################################] | 100% Completed |  1.3s
[7]:
iq = s.get_image_quality()
s_iq = hs.signals.Signal2D(iq)
s.plot(navigator=s_iq, scalebar=False)
Calculating the image quality:
[########################################] | 100% Completed |  0.6s
../_images/user_guide_visualizing_patterns_14_1.png
../_images/user_guide_visualizing_patterns_14_2.png

Using colour images (apart from creating RGB virtual BSE images, as shown above), e.g. an orientation map, om, or phase map, is a bit more involved (especially when the image doesn’t have the correct pixel shape, as is the case for our orientation map below, exported from MTEX):

[8]:
om = plt.imread('../_static/image/visualizing_patterns/om.png')
print(om.shape, om.dtype)
om_resized = skt.resize(
    om,
    output_shape=s.axes_manager.navigation_shape[::-1] + (3,),
    anti_aliasing=False
)
om_scaled = ske.rescale_intensity(om_resized, out_range=np.uint8)
s_om = hs.signals.Signal2D(om_scaled)
s_om
(764, 1036, 3) float32
[8]:
<Signal2D, title: , dimensions: (55|3, 75)>
[9]:
s_om = s_om.transpose(signal_axes=1)
print(s_om, s_om.data.dtype)
<Signal1D, title: , dimensions: (75, 55|3)> uint8
[10]:
s_om.change_dtype('rgb8')
print(s_om, s_om.data.dtype)
<Signal2D, title: , dimensions: (|75, 55)> [('R', 'u1'), ('G', 'u1'), ('B', 'u1')]
[11]:
s.plot(navigator=s_om, colorbar=False)
../_images/user_guide_visualizing_patterns_19_0.png
../_images/user_guide_visualizing_patterns_19_1.png

Plot multiple signals

HyperSpy provides the function plot_signals() to plot multiple signals with the same navigator, as explained in their user guide. This enables e.g. plotting of experimental and best matching simulated patterns side-by-side as a visual inspection of the results of pattern matching. To demonstrate this, we’ll load a CrystalMap with the best matching orientations of dynamically simulated Ni patterns to Nickel test data set, and project these patterns onto our detector from a master pattern

[12]:
xmap = io.load("../_static/data/ni_large.h5")
xmap
[12]:
Phase   Orientations  Name  Space group  Point group  Proper point group     Color
    0  4125 (100.0%)    ni        Fm-3m         m-3m                 432  tab:blue
Properties: scores, simulation_indices
Scan unit: um
[13]:
mp = kp.data.nickel_ebsd_master_pattern_small(projection="lambert")
[14]:
s_best = mp.get_patterns(
    rotations=xmap.rotations,
    detector=kp.detectors.EBSDDetector(
        shape=s.axes_manager.signal_shape[::-1],
        pc=[0.421, 0.7794, 0.5049],
        sample_tilt=70,
        convention="tsl"
    ),
    energy=20,
    dtype_out=s.data.dtype,
    compute=True
)
s_best = kp.signals.EBSD(s_best.data.reshape(s.data.shape))
Creating a dictionary of (4125,) simulated patterns:
[########################################] | 100% Completed |  0.9s

Let’s create a navigator map from the normalized cross-correlation scores

[15]:
ncc = xmap.get_map_data(xmap.scores[:, 0])
s_ncc = hs.signals.Signal2D(ncc)
[16]:
hs.plot.plot_signals([s, s_best], navigator=s_ncc)
../_images/user_guide_visualizing_patterns_26_0.png
../_images/user_guide_visualizing_patterns_26_1.png
../_images/user_guide_visualizing_patterns_26_2.png

This documentation cannot do this very nice feature of HyperSpy, for quick feedback how well the experimental patterns match the simulated ones, justice: you have to try it out for yourself!

Plot master patterns

EBSDMasterPattern signals can be navigated along their energy axis and/or the their northern/southern hemisphere. Let’s reload the Nickel master pattern used in the previous section, but this time in the stereographic projection.

[17]:
# Only a single energy, 20 keV
mp_stereo = kp.data.nickel_ebsd_master_pattern_small(
    projection="stereographic", hemisphere="both"
)
print(mp_stereo.axes_manager)
<Axes manager, axes: (2|401, 401)>
            Name |   size |  index |  offset |   scale |  units
================ | ====== | ====== | ======= | ======= | ======
      hemisphere |      2 |      0 |       0 |       1 |
---------------- | ------ | ------ | ------- | ------- | ------
           width |    401 |        |  -2e+02 |       1 |     px
          height |    401 |        |  -2e+02 |       1 |     px

As can be seen from the axes manager, the master pattern has two navigation axes, a north and south hemisphere, thus, when plotting, we get a slider as a navigator when plotting:

[18]:
mp_stereo.plot()
../_images/user_guide_visualizing_patterns_31_0.png
../_images/user_guide_visualizing_patterns_31_1.png