.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/processing/azimuthal_integration.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_azimuthal_integration.py: Azimuthal Integration (in Pyxem!) ================================= pyxem now includes built in azimuthal integration functionality. This is useful for extracting radial profiles from diffraction patterns in 1 or 2 dimensions. The new method will split the pixels into radial bins and then sum the intensity in each bin resulting in a `Diffraction1D` or `Polar2D` signal. In each case the total intensity of the diffraction pattern is preserved. .. GENERATED FROM PYTHON SOURCE LINES 11-26 .. code-block:: Python import pyxem as pxm import hyperspy.api as hs import numpy as np s = pxm.data.tilt_boundary_data() s.calibration( center=None ) # set the center to None to use center of the diffraction patterns s.calibration.units = "/AA^{-1}" s.calibration.scale = 0.03 # To angstroms s1d = s.get_azimuthal_integral1d(npt=100, inplace=False) s1d.sum().plot() .. image-sg:: /examples/processing/images/sphx_glr_azimuthal_integration_001.png :alt: Signal :srcset: /examples/processing/images/sphx_glr_azimuthal_integration_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ ] | 0% Completed | 175.84 us [ ] | 0% Completed | 113.72 ms [########################################] | 100% Completed | 214.14 ms .. GENERATED FROM PYTHON SOURCE LINES 27-28 Similarly, the `get_azimuthal_integral2d` method will return a `Polar2D` signal. .. GENERATED FROM PYTHON SOURCE LINES 28-32 .. code-block:: Python s_polar = s.get_azimuthal_integral2d(npt=100, npt_azim=360, inplace=False) s_polar.sum().plot() .. image-sg:: /examples/processing/images/sphx_glr_azimuthal_integration_002.png :alt: Signal :srcset: /examples/processing/images/sphx_glr_azimuthal_integration_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ ] | 0% Completed | 175.03 us [ ] | 0% Completed | 116.36 ms [ ] | 0% Completed | 216.69 ms [ ] | 0% Completed | 316.99 ms [ ] | 0% Completed | 417.29 ms [ ] | 0% Completed | 517.60 ms [ ] | 0% Completed | 617.90 ms [ ] | 0% Completed | 721.00 ms [########################################] | 100% Completed | 821.39 ms .. GENERATED FROM PYTHON SOURCE LINES 33-46 There are also other things you can account for with azimuthal integration, such as the effects of the Ewald sphere. This can be done by calibrating with a known detector distance, and beam energy. Here we just show the effect of just calibrating with the first peak vs. calibrating with the known beam energy and detector distance. For things like accurate template matching good calibration can be important when matching to high diffraction vectors. The calibration example gives more information on how to get the correct values for your microscope/setup. If you are doing x-ray diffraction please raise an issue on the pyxem github to let us know! The same assumptions should apply for each case, but it would be good to test! We only show the 1D case here, but the same applies for the 2D case as well! .. GENERATED FROM PYTHON SOURCE LINES 46-68 .. code-block:: Python s.calibration.detector( pixel_size=0.001, detector_distance=0.125, beam_energy=200, center=None, units="k_A^-1", ) # set the center= None to use the center of the diffraction patterns s1d_200 = s.get_azimuthal_integral1d(npt=100, inplace=False) s.calibration.detector( pixel_size=0.001, detector_distance=0.075, beam_energy=80, center=None, units="k_A^-1", ) # These are just made up pixel sizes and detector distances for illustration s1d_80 = s.get_azimuthal_integral1d(npt=100, inplace=False) hs.plot.plot_spectra( [s1d.sum(), s1d_200.sum(), s1d_80.sum()], legend=["Flat Ewald Sphere Assumption", "200keV Corrected", "80keV Corrected"], ) .. image-sg:: /examples/processing/images/sphx_glr_azimuthal_integration_003.png :alt: azimuthal integration :srcset: /examples/processing/images/sphx_glr_azimuthal_integration_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ ] | 0% Completed | 174.26 us [ ] | 0% Completed | 108.71 ms [########################################] | 100% Completed | 209.11 ms [ ] | 0% Completed | 138.64 us [ ] | 0% Completed | 102.00 ms [########################################] | 100% Completed | 202.40 ms .. GENERATED FROM PYTHON SOURCE LINES 69-74 At times you may want to use a mask to exclude certain pixels from the azimuthal integration or apply an affine transformation to the diffraction patterns before azimuthal integration. This can be done using the `mask` and `affine` parameters of the `Calibration` object. Here we just show a random affine transformation for illustration. .. GENERATED FROM PYTHON SOURCE LINES 74-83 .. code-block:: Python mask = s.get_direct_beam_mask(radius=20) # Mask the direct beam affine = np.array( [[0.9, 0.1, 0], [0.1, 0.9, 0], [0, 0, 1]] ) # Just a random affine transformation for illustration s.calibration(mask=mask, affine=affine) s.get_azimuthal_integral2d(npt=100, npt_azim=360, inplace=False).sum().plot() .. image-sg:: /examples/processing/images/sphx_glr_azimuthal_integration_004.png :alt: Signal :srcset: /examples/processing/images/sphx_glr_azimuthal_integration_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ ] | 0% Completed | 188.68 us [ ] | 0% Completed | 101.38 ms [ ] | 0% Completed | 201.76 ms [ ] | 0% Completed | 306.10 ms [ ] | 0% Completed | 410.10 ms [########################################] | 100% Completed | 510.51 ms .. GENERATED FROM PYTHON SOURCE LINES 84-86 The `azimuth_range`-argument lets you choose what angular range to calculate the azimuthal integral for. The range can be increasing, decreasing, and does not need to be a multiple of pi. .. GENERATED FROM PYTHON SOURCE LINES 86-102 .. code-block:: Python pol1 = s.get_azimuthal_integral2d(npt=100, azimuth_range=(-np.pi, np.pi)) pol2 = s.get_azimuthal_integral2d(npt=100, azimuth_range=(0, 1)) pol3 = s.get_azimuthal_integral2d(npt=100, npt_azim=720, azimuth_range=(0, 4 * np.pi)) pol4 = s.get_azimuthal_integral2d(npt=100, azimuth_range=(np.pi, 0)) hs.plot.plot_images( [pol1.sum(), pol2.sum(), pol3.sum(), pol4.sum()], label=["(-pi, pi) default", "(0, 1)", "(0, 4pi)", "(pi, 0)"], cmap="viridis", tight_layout=True, colorbar=None, ) .. image-sg:: /examples/processing/images/sphx_glr_azimuthal_integration_005.png :alt: (-pi, pi) default, (0, 1), (0, 4pi), (pi, 0) :srcset: /examples/processing/images/sphx_glr_azimuthal_integration_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [ ] | 0% Completed | 178.05 us [ ] | 0% Completed | 114.12 ms [ ] | 0% Completed | 214.46 ms [ ] | 0% Completed | 314.77 ms [ ] | 0% Completed | 415.08 ms [########################################] | 100% Completed | 515.46 ms [ ] | 0% Completed | 172.91 us [ ] | 0% Completed | 101.25 ms [ ] | 0% Completed | 201.59 ms [ ] | 0% Completed | 301.92 ms [ ] | 0% Completed | 402.22 ms [########################################] | 100% Completed | 502.63 ms [ ] | 0% Completed | 146.09 us [ ] | 0% Completed | 100.42 ms [ ] | 0% Completed | 200.76 ms [ ] | 0% Completed | 301.10 ms [ ] | 0% Completed | 401.43 ms [ ] | 0% Completed | 501.76 ms [ ] | 0% Completed | 602.10 ms [ ] | 0% Completed | 702.62 ms [ ] | 0% Completed | 802.95 ms [########################################] | 100% Completed | 903.35 ms [ ] | 0% Completed | 170.60 us [ ] | 0% Completed | 113.70 ms [ ] | 0% Completed | 214.05 ms [ ] | 0% Completed | 314.38 ms [ ] | 0% Completed | 414.72 ms [########################################] | 100% Completed | 515.12 ms [, , , ] .. rst-class:: sphx-glr-timing **Total running time of the script:** (2 minutes 26.928 seconds) .. _sphx_glr_download_examples_processing_azimuthal_integration.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: azimuthal_integration.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: azimuthal_integration.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_