Check out the Hyperspy Workshop May 13-17, 2024 Online

Finding Diffraction Vectors#

This example shows how to find the diffraction vectors for a given signal and then plot them using hyperspy’s markers.

import pyxem as pxm
import hyperspy.api as hs

s = pxm.data.tilt_boundary_data()
s.find_peaks(interactive=True)  # find the peaks using the interactive peak finder
  • vector finding
  • Signal
VBox(children=(Accordion(children=(VBox(children=(HBox(children=(Label(value='Unnamed 0th axis', layout=Layout(width='15%')), IntSlider(value=0, description='index', max=9), BoundedFloatText(value=0.0, continuous_update=True, description='value', max=9.0, step=1.0), Label(value='', layout=Layout(width='5%')))), HBox(children=(Label(value='Unnamed 1st axis', layout=Layout(width='15%')), IntSlider(value=0, description='index', max=9), BoundedFloatText(value=0.0, continuous_update=True, description='value', max=9.0, step=1.0), Label(value='', layout=Layout(width='5%')))), Button(description='Set random navigation position.', layout=Layout(width='auto'), style=ButtonStyle(), tooltip='Set random navigation position, useful to check the method parameters.'), Checkbox(value=True, description='Continuous update'))),), titles=('Navigation sliders',)), Accordion(children=(VBox(children=(HBox(children=(Label(value='Method', layout=Layout(width='auto')), Dropdown(options=('Local max', 'Max', 'Minmax', 'Zaefferer', 'Stat', 'Laplacian of Gaussian', 'Difference of Gaussian', 'Template matching'), value='Local max')), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), VBox(children=(HBox(children=(Label(value='Distance', layout=Layout(width='auto')), IntSlider(value=3, max=20, min=1)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Threshold', layout=Layout(width='auto')), FloatSlider(value=10.0, max=20.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between'))), layout=Layout(display='')), VBox(children=(HBox(children=(Label(value='Alpha', layout=Layout(width='auto')), FloatSlider(value=3.0, max=6.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Distance', layout=Layout(width='auto')), IntSlider(value=10, max=20, min=1)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between'))), layout=Layout(display='none')), VBox(children=(HBox(children=(Label(value='Distance', layout=Layout(width='auto')), FloatSlider(value=3.0, max=6.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Threshold', layout=Layout(width='auto')), FloatSlider(value=10.0, max=20.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between'))), layout=Layout(display='none')), VBox(children=(HBox(children=(Label(value='Gradient threshold', layout=Layout(width='auto')), FloatSlider(value=0.1, max=0.2, step=0.020000000000000004)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Window size', layout=Layout(width='auto')), IntSlider(value=40, max=80, min=2)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Distance cutoff', layout=Layout(width='auto')), FloatSlider(value=50.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between'))), layout=Layout(display='none')), VBox(children=(HBox(children=(Label(value='Alpha', layout=Layout(width='auto')), FloatSlider(value=1.0, max=2.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Radius', layout=Layout(width='auto')), IntSlider(value=10, max=20, min=5)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Convergence ratio', layout=Layout(width='auto')), FloatSlider(value=0.05, max=0.1)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between'))), layout=Layout(display='none')), VBox(children=(HBox(children=(Label(value='Min sigma', layout=Layout(width='auto')), FloatSlider(value=1.0, max=2.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Max sigma', layout=Layout(width='auto')), FloatSlider(value=50.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Num sigma', layout=Layout(width='auto')), FloatSlider(value=10.0, max=20.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Threshold', layout=Layout(width='auto')), FloatSlider(value=0.2, max=0.4)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Overlap', layout=Layout(width='auto')), FloatSlider(value=0.5, max=1.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Log scale', layout=Layout(width='auto')), Checkbox(value=False)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between'))), layout=Layout(display='none')), VBox(children=(HBox(children=(Label(value='Min sigma', layout=Layout(width='auto')), FloatSlider(value=1.0, max=2.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Max sigma', layout=Layout(width='auto')), FloatSlider(value=50.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Sigma ratio', layout=Layout(width='auto')), FloatSlider(value=1.6, max=3.2)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Threshold', layout=Layout(width='auto')), FloatSlider(value=0.2, max=0.4)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Overlap', layout=Layout(width='auto')), FloatSlider(value=0.5, max=1.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between'))), layout=Layout(display='none')), VBox(children=(HBox(children=(Label(value='Distance', layout=Layout(width='auto')), FloatSlider(value=5.0, max=10.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between')), HBox(children=(Label(value='Threshold', layout=Layout(width='auto')), FloatSlider(value=0.5, max=2.0)), layout=Layout(display='flex', flex_flow='row', justify_content='space-between'))), layout=Layout(display='none')))),), titles=('Method parameters',)), HBox(children=(Button(description='Compute over navigation axes.', style=ButtonStyle(), tooltip='Find the peaks by iterating over the navigation axes.'), Button(description='Close', style=ButtonStyle(), tooltip='Close widget and close figure.')))))

<BaseSignal, title: , dimensions: (10, 10|)>
"""
Template Matching
=================

The best method for finding peaks is usually through template matching.  In this case a disk with
some radius is used as the template.  The radius of the disk should be chosen to be the same size
as the diffraction spots.  The template matching is done using the :meth:`template_match_disk` method.

This can also be done lazy, including the plotting of the markers!
"""
s.axes_manager[2].scale = 0.3


temp_small = s.template_match_disk(disk_r=3, subtract_min=False)  # Too small
temp = s.template_match_disk(disk_r=5, subtract_min=False)  # Just right
temp_large = s.template_match_disk(disk_r=7, subtract_min=False)  # Too large
ind = (5, 5)
hs.plot.plot_images(
    [temp_small.inav[ind], temp.inav[ind], temp_large.inav[ind]],
    label=["Too Small", "Just Right", "Too Large"],
)

vectors = temp.get_diffraction_vectors(threshold_abs=0.4, min_distance=5)
Too Small, Just Right, Too Large
[                                        ] | 0% Completed | 246.17 us
[                                        ] | 0% Completed | 100.57 ms
[                                        ] | 0% Completed | 201.01 ms
[                                        ] | 0% Completed | 301.49 ms
[########################################] | 100% Completed | 401.84 ms

[                                        ] | 0% Completed | 136.20 us
[                                        ] | 0% Completed | 100.46 ms
[                                        ] | 0% Completed | 200.82 ms
[                                        ] | 0% Completed | 301.19 ms
[########################################] | 100% Completed | 401.54 ms

[                                        ] | 0% Completed | 133.35 us
[                                        ] | 0% Completed | 100.52 ms
[                                        ] | 0% Completed | 200.85 ms
[                                        ] | 0% Completed | 301.19 ms
[########################################] | 100% Completed | 401.54 ms

[                                        ] | 0% Completed | 138.76 us
[                                        ] | 0% Completed | 100.42 ms
[                                        ] | 0% Completed | 200.76 ms
[                                        ] | 0% Completed | 301.07 ms
[                                        ] | 0% Completed | 401.67 ms
[########################################] | 100% Completed | 502.03 ms

[                                        ] | 0% Completed | 156.10 us
[########################################] | 100% Completed | 100.44 ms
# Plotting Peaks
# ==============
# We can plot the peaks using hyperSpy's markers and DiffractionVectors.

s.plot()
s.add_marker(vectors.to_markers(color="red", sizes=10, alpha=0.5))
  • vector finding
  • Signal
[                                        ] | 0% Completed | 219.24 us
[########################################] | 100% Completed | 103.24 ms
# Subpixel Peak Fitting
# =====================

# The template matching is done on the pixel grid.  To find the peak position more accurately the correlation
# can be up-sampled using the :func:`pyxem.signals.DiffractionVectors.subpixel_refine` method.  This method takes a
# `DiffractionSignal2D` object and uses that to refine the peak positions.
#
# This only really works up to up-sampling of 2-4. There is little improvement with increased up-sampling while
# it greatly increases the computation time.

refined_peaks_com = vectors.subpixel_refine(s, "center-of-mass", square_size=20)
refined_peaks_xc = vectors.subpixel_refine(
    s, "cross-correlation", square_size=20, upsample_factor=2, disk_r=5
)

markers2 = refined_peaks_com.to_markers(color="blue", sizes=10, alpha=0.25)
markers3 = refined_peaks_xc.to_markers(color="green", sizes=10, alpha=0.25)


s.plot()
s.add_marker(vectors.to_markers(color="red", sizes=10, alpha=0.25))
s.add_marker(markers2)
s.add_marker(markers3)
  • vector finding
  • Signal
[                                        ] | 0% Completed | 138.19 us
[########################################] | 100% Completed | 100.46 ms

[                                        ] | 0% Completed | 131.31 us
[                                        ] | 0% Completed | 101.42 ms
[                                        ] | 0% Completed | 211.85 ms
[                                        ] | 0% Completed | 316.25 ms
[########################################] | 100% Completed | 416.58 ms

[                                        ] | 0% Completed | 127.80 us
[########################################] | 100% Completed | 100.41 ms

[                                        ] | 0% Completed | 128.88 us
[                                        ] | 0% Completed | 100.39 ms
[                                        ] | 0% Completed | 200.77 ms
[                                        ] | 0% Completed | 301.08 ms
[                                        ] | 0% Completed | 401.45 ms
[                                        ] | 0% Completed | 501.93 ms
[                                        ] | 0% Completed | 602.41 ms
[                                        ] | 0% Completed | 702.73 ms
[                                        ] | 0% Completed | 803.24 ms
[                                        ] | 0% Completed | 903.59 ms
[                                        ] | 0% Completed | 1.00 s
[                                        ] | 0% Completed | 1.10 s
[                                        ] | 0% Completed | 1.21 s
[                                        ] | 0% Completed | 1.31 s
[                                        ] | 0% Completed | 1.41 s
[                                        ] | 0% Completed | 1.51 s
[                                        ] | 0% Completed | 1.61 s
[                                        ] | 0% Completed | 1.71 s
[                                        ] | 0% Completed | 1.81 s
[                                        ] | 0% Completed | 1.91 s
[                                        ] | 0% Completed | 2.01 s
[                                        ] | 0% Completed | 2.11 s
[                                        ] | 0% Completed | 2.21 s
[                                        ] | 0% Completed | 2.31 s
[                                        ] | 0% Completed | 2.41 s
[                                        ] | 0% Completed | 2.51 s
[                                        ] | 0% Completed | 2.61 s
[                                        ] | 0% Completed | 2.71 s
[                                        ] | 0% Completed | 2.81 s
[                                        ] | 0% Completed | 2.91 s
[                                        ] | 0% Completed | 3.01 s
[                                        ] | 0% Completed | 3.11 s
[                                        ] | 0% Completed | 3.21 s
[                                        ] | 0% Completed | 3.31 s
[                                        ] | 0% Completed | 3.41 s
[                                        ] | 0% Completed | 3.51 s
[                                        ] | 0% Completed | 3.61 s
[                                        ] | 0% Completed | 3.71 s
[                                        ] | 0% Completed | 3.82 s
[                                        ] | 0% Completed | 3.92 s
[                                        ] | 0% Completed | 4.02 s
[                                        ] | 0% Completed | 4.12 s
[                                        ] | 0% Completed | 4.22 s
[                                        ] | 0% Completed | 4.32 s
[                                        ] | 0% Completed | 4.42 s
[                                        ] | 0% Completed | 4.52 s
[                                        ] | 0% Completed | 4.62 s
[                                        ] | 0% Completed | 4.72 s
[                                        ] | 0% Completed | 4.82 s
[                                        ] | 0% Completed | 4.92 s
[                                        ] | 0% Completed | 5.02 s
[                                        ] | 0% Completed | 5.12 s
[                                        ] | 0% Completed | 5.22 s
[                                        ] | 0% Completed | 5.32 s
[                                        ] | 0% Completed | 5.42 s
[                                        ] | 0% Completed | 5.52 s
[                                        ] | 0% Completed | 5.62 s
[                                        ] | 0% Completed | 5.72 s
[                                        ] | 0% Completed | 5.82 s
[                                        ] | 0% Completed | 5.92 s
[                                        ] | 0% Completed | 6.02 s
[                                        ] | 0% Completed | 6.12 s
[                                        ] | 0% Completed | 6.22 s
[                                        ] | 0% Completed | 6.32 s
[                                        ] | 0% Completed | 6.43 s
[                                        ] | 0% Completed | 6.53 s
[                                        ] | 0% Completed | 6.63 s
[                                        ] | 0% Completed | 6.73 s
[                                        ] | 0% Completed | 6.83 s
[                                        ] | 0% Completed | 6.93 s
[                                        ] | 0% Completed | 7.03 s
[                                        ] | 0% Completed | 7.13 s
[                                        ] | 0% Completed | 7.23 s
[                                        ] | 0% Completed | 7.33 s
[                                        ] | 0% Completed | 7.43 s
[                                        ] | 0% Completed | 7.53 s
[                                        ] | 0% Completed | 7.63 s
[                                        ] | 0% Completed | 7.73 s
[                                        ] | 0% Completed | 7.83 s
[                                        ] | 0% Completed | 7.93 s
[                                        ] | 0% Completed | 8.03 s
[                                        ] | 0% Completed | 8.13 s
[                                        ] | 0% Completed | 8.23 s
[                                        ] | 0% Completed | 8.33 s
[                                        ] | 0% Completed | 8.43 s
[                                        ] | 0% Completed | 8.53 s
[                                        ] | 0% Completed | 8.63 s
[                                        ] | 0% Completed | 8.73 s
[                                        ] | 0% Completed | 8.83 s
[                                        ] | 0% Completed | 8.93 s
[                                        ] | 0% Completed | 9.03 s
[                                        ] | 0% Completed | 9.14 s
[                                        ] | 0% Completed | 9.24 s
[                                        ] | 0% Completed | 9.34 s
[                                        ] | 0% Completed | 9.44 s
[                                        ] | 0% Completed | 9.54 s
[                                        ] | 0% Completed | 9.64 s
[                                        ] | 0% Completed | 9.74 s
[                                        ] | 0% Completed | 9.84 s
[                                        ] | 0% Completed | 9.94 s
[                                        ] | 0% Completed | 10.04 s
[                                        ] | 0% Completed | 10.14 s
[                                        ] | 0% Completed | 10.24 s
[                                        ] | 0% Completed | 10.34 s
[                                        ] | 0% Completed | 10.44 s
[                                        ] | 0% Completed | 10.54 s
[                                        ] | 0% Completed | 10.64 s
[                                        ] | 0% Completed | 10.74 s
[                                        ] | 0% Completed | 10.84 s
[                                        ] | 0% Completed | 10.94 s
[                                        ] | 0% Completed | 11.04 s
[                                        ] | 0% Completed | 11.14 s
[                                        ] | 0% Completed | 11.24 s
[                                        ] | 0% Completed | 11.34 s
[                                        ] | 0% Completed | 11.44 s
[                                        ] | 0% Completed | 11.54 s
[                                        ] | 0% Completed | 11.65 s
[                                        ] | 0% Completed | 11.75 s
[                                        ] | 0% Completed | 11.85 s
[                                        ] | 0% Completed | 11.95 s
[                                        ] | 0% Completed | 12.05 s
[                                        ] | 0% Completed | 12.15 s
[                                        ] | 0% Completed | 12.25 s
[                                        ] | 0% Completed | 12.35 s
[                                        ] | 0% Completed | 12.45 s
[                                        ] | 0% Completed | 12.55 s
[                                        ] | 0% Completed | 12.65 s
[                                        ] | 0% Completed | 12.75 s
[                                        ] | 0% Completed | 12.85 s
[                                        ] | 0% Completed | 12.95 s
[                                        ] | 0% Completed | 13.05 s
[                                        ] | 0% Completed | 13.15 s
[                                        ] | 0% Completed | 13.25 s
[                                        ] | 0% Completed | 13.35 s
[                                        ] | 0% Completed | 13.45 s
[                                        ] | 0% Completed | 13.55 s
[                                        ] | 0% Completed | 13.65 s
[                                        ] | 0% Completed | 13.75 s
[                                        ] | 0% Completed | 13.85 s
[                                        ] | 0% Completed | 13.95 s
[                                        ] | 0% Completed | 14.05 s
[                                        ] | 0% Completed | 14.15 s
[                                        ] | 0% Completed | 14.26 s
[                                        ] | 0% Completed | 14.36 s
[                                        ] | 0% Completed | 14.46 s
[                                        ] | 0% Completed | 14.56 s
[                                        ] | 0% Completed | 14.66 s
[                                        ] | 0% Completed | 14.76 s
[                                        ] | 0% Completed | 14.86 s
[                                        ] | 0% Completed | 14.96 s
[                                        ] | 0% Completed | 15.06 s
[                                        ] | 0% Completed | 15.16 s
[                                        ] | 0% Completed | 15.26 s
[                                        ] | 0% Completed | 15.36 s
[                                        ] | 0% Completed | 15.46 s
[                                        ] | 0% Completed | 15.56 s
[                                        ] | 0% Completed | 15.66 s
[                                        ] | 0% Completed | 15.76 s
[                                        ] | 0% Completed | 15.86 s
[                                        ] | 0% Completed | 15.96 s
[########################################] | 100% Completed | 16.06 s

[                                        ] | 0% Completed | 151.32 us
[########################################] | 100% Completed | 100.47 ms

[                                        ] | 0% Completed | 143.07 us
[########################################] | 100% Completed | 100.48 ms

[                                        ] | 0% Completed | 241.59 us
[########################################] | 100% Completed | 100.99 ms
# sphinx_gallery_thumbnail_number = 3

Total running time of the script: (0 minutes 22.608 seconds)

Gallery generated by Sphinx-Gallery