Note
Go to the end to download the full example code.
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
VBox(children=(Accordion(children=(VBox(children=(HBox(children=(Label(value='x', 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='px', layout=Layout(width='5%')))), HBox(children=(Label(value='y', 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='px', 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)

[ ] | 0% Completed | 171.24 us
[ ] | 0% Completed | 114.16 ms
[ ] | 0% Completed | 214.67 ms
[ ] | 0% Completed | 315.05 ms
[ ] | 0% Completed | 415.37 ms
[########################################] | 100% Completed | 515.73 ms
[ ] | 0% Completed | 153.79 us
[ ] | 0% Completed | 107.30 ms
[ ] | 0% Completed | 207.68 ms
[ ] | 0% Completed | 307.99 ms
[ ] | 0% Completed | 408.29 ms
[########################################] | 100% Completed | 508.67 ms
[ ] | 0% Completed | 160.62 us
[ ] | 0% Completed | 110.17 ms
[ ] | 0% Completed | 210.55 ms
[ ] | 0% Completed | 310.84 ms
[ ] | 0% Completed | 411.15 ms
[########################################] | 100% Completed | 511.52 ms
[ ] | 0% Completed | 183.28 us
[ ] | 0% Completed | 108.28 ms
[ ] | 0% Completed | 208.60 ms
[ ] | 0% Completed | 308.90 ms
[ ] | 0% Completed | 409.21 ms
[ ] | 0% Completed | 509.49 ms
[ ] | 0% Completed | 609.82 ms
[ ] | 0% Completed | 710.09 ms
[ ] | 0% Completed | 810.41 ms
[########################################] | 100% Completed | 910.76 ms
[ ] | 0% Completed | 151.69 us
[########################################] | 100% Completed | 103.90 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))
[ ] | 0% Completed | 169.13 us
[########################################] | 100% Completed | 102.17 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 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)
[ ] | 0% Completed | 166.02 us
[########################################] | 100% Completed | 113.72 ms
[ ] | 0% Completed | 127.48 us
[ ] | 0% Completed | 106.90 ms
[ ] | 0% Completed | 207.23 ms
[ ] | 0% Completed | 307.56 ms
[ ] | 0% Completed | 407.93 ms
[ ] | 0% Completed | 508.29 ms
[ ] | 0% Completed | 608.67 ms
[ ] | 0% Completed | 709.01 ms
[ ] | 0% Completed | 809.33 ms
[########################################] | 100% Completed | 909.70 ms
[ ] | 0% Completed | 157.13 us
[########################################] | 100% Completed | 113.87 ms
[ ] | 0% Completed | 147.76 us
[ ] | 0% Completed | 105.49 ms
[ ] | 0% Completed | 205.79 ms
[ ] | 0% Completed | 306.08 ms
[ ] | 0% Completed | 406.44 ms
[ ] | 0% Completed | 506.76 ms
[ ] | 0% Completed | 607.12 ms
[ ] | 0% Completed | 707.42 ms
[ ] | 0% Completed | 807.72 ms
[ ] | 0% Completed | 908.01 ms
[ ] | 0% Completed | 1.01 s
[ ] | 0% Completed | 1.11 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.22 s
[ ] | 0% Completed | 3.32 s
[ ] | 0% Completed | 3.42 s
[ ] | 0% Completed | 3.52 s
[ ] | 0% Completed | 3.62 s
[ ] | 0% Completed | 3.72 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.33 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.13 s
[ ] | 0% Completed | 9.23 s
[ ] | 0% Completed | 9.33 s
[ ] | 0% Completed | 9.43 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.64 s
[ ] | 0% Completed | 11.74 s
[ ] | 0% Completed | 11.84 s
[ ] | 0% Completed | 11.94 s
[ ] | 0% Completed | 12.04 s
[ ] | 0% Completed | 12.14 s
[ ] | 0% Completed | 12.24 s
[ ] | 0% Completed | 12.34 s
[ ] | 0% Completed | 12.44 s
[ ] | 0% Completed | 12.54 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.25 s
[ ] | 0% Completed | 14.35 s
[ ] | 0% Completed | 14.45 s
[ ] | 0% Completed | 14.55 s
[ ] | 0% Completed | 14.65 s
[ ] | 0% Completed | 14.75 s
[ ] | 0% Completed | 14.85 s
[ ] | 0% Completed | 14.95 s
[ ] | 0% Completed | 15.05 s
[ ] | 0% Completed | 15.15 s
[ ] | 0% Completed | 15.25 s
[ ] | 0% Completed | 15.35 s
[ ] | 0% Completed | 15.45 s
[ ] | 0% Completed | 15.55 s
[ ] | 0% Completed | 15.65 s
[ ] | 0% Completed | 15.75 s
[ ] | 0% Completed | 15.85 s
[ ] | 0% Completed | 15.96 s
[ ] | 0% Completed | 16.06 s
[ ] | 0% Completed | 16.16 s
[ ] | 0% Completed | 16.26 s
[ ] | 0% Completed | 16.36 s
[ ] | 0% Completed | 16.46 s
[ ] | 0% Completed | 16.56 s
[ ] | 0% Completed | 16.66 s
[ ] | 0% Completed | 16.76 s
[ ] | 0% Completed | 16.86 s
[ ] | 0% Completed | 16.96 s
[ ] | 0% Completed | 17.06 s
[ ] | 0% Completed | 17.16 s
[ ] | 0% Completed | 17.26 s
[ ] | 0% Completed | 17.36 s
[ ] | 0% Completed | 17.46 s
[ ] | 0% Completed | 17.56 s
[ ] | 0% Completed | 17.66 s
[ ] | 0% Completed | 17.76 s
[ ] | 0% Completed | 17.86 s
[ ] | 0% Completed | 17.96 s
[ ] | 0% Completed | 18.06 s
[ ] | 0% Completed | 18.16 s
[ ] | 0% Completed | 18.26 s
[ ] | 0% Completed | 18.36 s
[ ] | 0% Completed | 18.46 s
[ ] | 0% Completed | 18.56 s
[ ] | 0% Completed | 18.66 s
[ ] | 0% Completed | 18.76 s
[ ] | 0% Completed | 18.86 s
[ ] | 0% Completed | 18.96 s
[ ] | 0% Completed | 19.07 s
[ ] | 0% Completed | 19.17 s
[ ] | 0% Completed | 19.27 s
[ ] | 0% Completed | 19.37 s
[ ] | 0% Completed | 19.47 s
[ ] | 0% Completed | 19.57 s
[ ] | 0% Completed | 19.67 s
[ ] | 0% Completed | 19.77 s
[ ] | 0% Completed | 19.87 s
[ ] | 0% Completed | 19.97 s
[ ] | 0% Completed | 20.07 s
[ ] | 0% Completed | 20.17 s
[ ] | 0% Completed | 20.27 s
[ ] | 0% Completed | 20.37 s
[ ] | 0% Completed | 20.47 s
[ ] | 0% Completed | 20.57 s
[ ] | 0% Completed | 20.67 s
[ ] | 0% Completed | 20.77 s
[ ] | 0% Completed | 20.87 s
[ ] | 0% Completed | 20.97 s
[ ] | 0% Completed | 21.07 s
[ ] | 0% Completed | 21.17 s
[ ] | 0% Completed | 21.27 s
[ ] | 0% Completed | 21.37 s
[ ] | 0% Completed | 21.47 s
[ ] | 0% Completed | 21.57 s
[ ] | 0% Completed | 21.67 s
[ ] | 0% Completed | 21.77 s
[ ] | 0% Completed | 21.87 s
[ ] | 0% Completed | 21.97 s
[ ] | 0% Completed | 22.07 s
[ ] | 0% Completed | 22.17 s
[########################################] | 100% Completed | 22.28 s
[ ] | 0% Completed | 154.45 us
[########################################] | 100% Completed | 102.03 ms
[ ] | 0% Completed | 123.56 us
[########################################] | 100% Completed | 101.91 ms
[ ] | 0% Completed | 166.83 us
[########################################] | 100% Completed | 102.06 ms
# sphinx_gallery_thumbnail_number = 3
Total running time of the script: (0 minutes 31.822 seconds)





