.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/processing/vector_finding.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_processing_vector_finding.py: Finding Diffraction Vectors =========================== .. GENERATED FROM PYTHON SOURCE LINES 7-9 This example shows how to find the diffraction vectors for a given signal and then plot them using hyperspy's markers. .. GENERATED FROM PYTHON SOURCE LINES 9-15 .. code-block:: Python import pyxem as pxm import hyperspy.api as hs s = pxm.data.tilt_boundary_data() .. GENERATED FROM PYTHON SOURCE LINES 16-19 .. code-block:: Python s.find_peaks(interactive=True) # find the peaks using the interactive peak finder .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/processing/images/sphx_glr_vector_finding_001.png :alt: vector finding :srcset: /examples/processing/images/sphx_glr_vector_finding_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/processing/images/sphx_glr_vector_finding_002.png :alt: Signal :srcset: /examples/processing/images/sphx_glr_vector_finding_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none 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.'))))) .. GENERATED FROM PYTHON SOURCE LINES 20-45 .. code-block:: Python """ 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) .. image-sg:: /examples/processing/images/sphx_glr_vector_finding_003.png :alt: Too Small, Just Right, Too Large :srcset: /examples/processing/images/sphx_glr_vector_finding_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ ] | 0% Completed | 189.81 us [ ] | 0% Completed | 100.51 ms [ ] | 0% Completed | 200.87 ms [ ] | 0% Completed | 301.22 ms [########################################] | 100% Completed | 401.53 ms [ ] | 0% Completed | 133.41 us [ ] | 0% Completed | 100.31 ms [ ] | 0% Completed | 200.57 ms [ ] | 0% Completed | 300.91 ms [########################################] | 100% Completed | 401.24 ms [ ] | 0% Completed | 135.01 us [ ] | 0% Completed | 100.38 ms [ ] | 0% Completed | 200.68 ms [ ] | 0% Completed | 300.98 ms [########################################] | 100% Completed | 401.26 ms [ ] | 0% Completed | 122.68 us [ ] | 0% Completed | 100.37 ms [ ] | 0% Completed | 200.61 ms [ ] | 0% Completed | 300.87 ms [ ] | 0% Completed | 401.11 ms [########################################] | 100% Completed | 501.45 ms [ ] | 0% Completed | 122.59 us [########################################] | 100% Completed | 100.37 ms .. GENERATED FROM PYTHON SOURCE LINES 46-54 .. code-block:: Python # 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)) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/processing/images/sphx_glr_vector_finding_004.png :alt: vector finding :srcset: /examples/processing/images/sphx_glr_vector_finding_004.png :class: sphx-glr-multi-img * .. image-sg:: /examples/processing/images/sphx_glr_vector_finding_005.png :alt: Signal :srcset: /examples/processing/images/sphx_glr_vector_finding_005.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ ] | 0% Completed | 200.22 us [########################################] | 100% Completed | 102.06 ms .. GENERATED FROM PYTHON SOURCE LINES 55-80 .. code-block:: Python # 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) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/processing/images/sphx_glr_vector_finding_006.png :alt: vector finding :srcset: /examples/processing/images/sphx_glr_vector_finding_006.png :class: sphx-glr-multi-img * .. image-sg:: /examples/processing/images/sphx_glr_vector_finding_007.png :alt: Signal :srcset: /examples/processing/images/sphx_glr_vector_finding_007.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ ] | 0% Completed | 126.95 us [########################################] | 100% Completed | 100.45 ms [ ] | 0% Completed | 109.90 us [ ] | 0% Completed | 100.42 ms [ ] | 0% Completed | 200.85 ms [ ] | 0% Completed | 301.44 ms [########################################] | 100% Completed | 401.70 ms [ ] | 0% Completed | 122.69 us [########################################] | 100% Completed | 100.39 ms [ ] | 0% Completed | 111.03 us [ ] | 0% Completed | 100.36 ms [ ] | 0% Completed | 200.61 ms [ ] | 0% Completed | 300.86 ms [ ] | 0% Completed | 401.11 ms [ ] | 0% Completed | 501.38 ms [ ] | 0% Completed | 601.80 ms [ ] | 0% Completed | 702.11 ms [ ] | 0% Completed | 802.39 ms [ ] | 0% Completed | 902.69 ms [ ] | 0% Completed | 1.00 s [ ] | 0% Completed | 1.10 s [ ] | 0% Completed | 1.20 s [ ] | 0% Completed | 1.30 s [ ] | 0% Completed | 1.40 s [ ] | 0% Completed | 1.50 s [ ] | 0% Completed | 1.60 s [ ] | 0% Completed | 1.70 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.81 s [ ] | 0% Completed | 3.91 s [ ] | 0% Completed | 4.01 s [ ] | 0% Completed | 4.11 s [ ] | 0% Completed | 4.21 s [ ] | 0% Completed | 4.31 s [ ] | 0% Completed | 4.41 s [ ] | 0% Completed | 4.51 s [ ] | 0% Completed | 4.61 s [ ] | 0% Completed | 4.71 s [ ] | 0% Completed | 4.81 s [ ] | 0% Completed | 4.91 s [ ] | 0% Completed | 5.01 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.42 s [ ] | 0% Completed | 6.52 s [ ] | 0% Completed | 6.62 s [ ] | 0% Completed | 6.72 s [ ] | 0% Completed | 6.82 s [ ] | 0% Completed | 6.92 s [ ] | 0% Completed | 7.02 s [ ] | 0% Completed | 7.12 s [ ] | 0% Completed | 7.22 s [ ] | 0% Completed | 7.32 s [ ] | 0% Completed | 7.42 s [ ] | 0% Completed | 7.52 s [ ] | 0% Completed | 7.62 s [ ] | 0% Completed | 7.72 s [ ] | 0% Completed | 7.82 s [ ] | 0% Completed | 7.92 s [ ] | 0% Completed | 8.02 s [ ] | 0% Completed | 8.12 s [ ] | 0% Completed | 8.22 s [ ] | 0% Completed | 8.32 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.53 s [ ] | 0% Completed | 9.63 s [ ] | 0% Completed | 9.73 s [ ] | 0% Completed | 9.83 s [ ] | 0% Completed | 9.93 s [ ] | 0% Completed | 10.03 s [ ] | 0% Completed | 10.13 s [ ] | 0% Completed | 10.23 s [ ] | 0% Completed | 10.33 s [ ] | 0% Completed | 10.43 s [ ] | 0% Completed | 10.53 s [ ] | 0% Completed | 10.63 s [ ] | 0% Completed | 10.73 s [ ] | 0% Completed | 10.83 s [ ] | 0% Completed | 10.93 s [ ] | 0% Completed | 11.03 s [ ] | 0% Completed | 11.13 s [ ] | 0% Completed | 11.23 s [ ] | 0% Completed | 11.33 s [ ] | 0% Completed | 11.43 s [ ] | 0% Completed | 11.53 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.64 s [ ] | 0% Completed | 12.74 s [ ] | 0% Completed | 12.84 s [ ] | 0% Completed | 12.94 s [ ] | 0% Completed | 13.04 s [ ] | 0% Completed | 13.14 s [ ] | 0% Completed | 13.24 s [ ] | 0% Completed | 13.34 s [ ] | 0% Completed | 13.44 s [ ] | 0% Completed | 13.54 s [ ] | 0% Completed | 13.64 s [ ] | 0% Completed | 13.74 s [ ] | 0% Completed | 13.84 s [ ] | 0% Completed | 13.94 s [ ] | 0% Completed | 14.04 s [ ] | 0% Completed | 14.14 s [########################################] | 100% Completed | 14.24 s [ ] | 0% Completed | 124.65 us [########################################] | 100% Completed | 100.39 ms [ ] | 0% Completed | 110.42 us [########################################] | 100% Completed | 100.36 ms [ ] | 0% Completed | 211.29 us [########################################] | 100% Completed | 101.15 ms .. GENERATED FROM PYTHON SOURCE LINES 81-83 .. code-block:: Python # sphinx_gallery_thumbnail_number = 3 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 20.506 seconds) .. _sphx_glr_download_examples_processing_vector_finding.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: vector_finding.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: vector_finding.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_