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

Background subtraction#

If your diffraction data is noisy, you might want to subtract the background from the dataset. Pyxem offers some built-in functionality for this, with the subtract_diffraction_background class method. Custom filtering is also possible, an example is shown in the ‘Filtering Data’-example.

import pyxem as pxm
import hyperspy.api as hs

s = pxm.data.tilt_boundary_data()

s_filtered = s.subtract_diffraction_background(
    "difference of gaussians",
    inplace=False,
    min_sigma=3,
    max_sigma=20,
)

s_filtered_h = s.subtract_diffraction_background("h-dome", inplace=False, h=0.7)


hs.plot.plot_images(
    [s.inav[2, 2], s_filtered.inav[2, 2], s_filtered_h.inav[2, 2]],
    label=["Original", "Difference of Gaussians", "H-Dome"],
    tight_layout=True,
    norm="symlog",
    cmap="viridis",
    colorbar=None,
)
Original, Difference of Gaussians, H-Dome
[                                        ] | 0% Completed | 118.03 us
[                                        ] | 0% Completed | 100.44 ms
[                                        ] | 0% Completed | 200.78 ms
[                                        ] | 0% Completed | 301.16 ms
[                                        ] | 0% Completed | 401.48 ms
[                                        ] | 0% Completed | 501.80 ms
[                                        ] | 0% Completed | 602.12 ms
[                                        ] | 0% Completed | 702.43 ms
[                                        ] | 0% Completed | 802.75 ms
[                                        ] | 0% Completed | 903.06 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.51 s
[########################################] | 100% Completed | 1.61 s
/home/docs/checkouts/readthedocs.org/user_builds/pyxem/envs/v0.18.0/lib/python3.10/site-packages/hyperspy/misc/utils.py:1437: UserWarning: Possible precision loss converting image of type float32 to uint8 as required by rank filters. Convert manually using skimage.util.img_as_ubyte to silence this warning.
  output = function(test_data, **kwargs)

[                                        ] | 0% Completed | 134.81 us/home/docs/checkouts/readthedocs.org/user_builds/pyxem/envs/v0.18.0/lib/python3.10/site-packages/hyperspy/misc/utils.py:1360: UserWarning: Possible precision loss converting image of type float32 to uint8 as required by rank filters. Convert manually using skimage.util.img_as_ubyte to silence this warning.
  output_array[islice] = function(data[islice], **kwargs)

[                                        ] | 0% Completed | 100.41 ms
[                                        ] | 0% Completed | 200.78 ms
[                                        ] | 0% Completed | 301.09 ms
[                                        ] | 0% Completed | 401.43 ms
[                                        ] | 0% Completed | 501.77 ms
[                                        ] | 0% Completed | 602.15 ms
[                                        ] | 0% Completed | 702.50 ms
[                                        ] | 0% Completed | 802.83 ms
[                                        ] | 0% Completed | 903.17 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.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
[########################################] | 100% Completed | 2.81 s

[<Axes: title={'center': 'Original'}>, <Axes: title={'center': 'Difference of Gaussians'}>, <Axes: title={'center': 'H-Dome'}>]

Filtering Polar Images#

The available methods differ for Diffraction2D datasets and PolarDiffraction2D datasets.

Set the center of the diffraction pattern to its default, i.e. the middle of the image

Transform to polar coordinates

s_polar = s.get_azimuthal_integral2d(npt=100, mean=True)

s_polar_filtered = s_polar.subtract_diffraction_background(
    "radial median",
    inplace=False,
)

s_polar_filtered2 = s_polar.subtract_diffraction_background(
    "radial percentile",
    percentile=70,
    inplace=False,
)

hs.plot.plot_images(
    [s_polar.inav[2, 2], s_polar_filtered.inav[2, 2], s_polar_filtered2.inav[2, 2]],
    label=["Original (polar)", "Radial Median", "Radial Percentile"],
    tight_layout=True,
    norm="symlog",
    cmap="viridis",
    colorbar=None,
)
Original (polar), Radial Median, Radial Percentile
[                                        ] | 0% Completed | 214.49 us
[                                        ] | 0% Completed | 100.95 ms
[                                        ] | 0% Completed | 201.28 ms
[                                        ] | 0% Completed | 301.63 ms
[                                        ] | 0% Completed | 401.97 ms
[                                        ] | 0% Completed | 502.33 ms
[                                        ] | 0% Completed | 602.69 ms
[########################################] | 100% Completed | 703.09 ms

[                                        ] | 0% Completed | 135.38 us
[                                        ] | 0% Completed | 101.10 ms
[########################################] | 100% Completed | 201.46 ms

[                                        ] | 0% Completed | 124.67 us
[                                        ] | 0% Completed | 100.40 ms
[                                        ] | 0% Completed | 200.78 ms
[                                        ] | 0% Completed | 301.20 ms
[                                        ] | 0% Completed | 401.49 ms
[                                        ] | 0% Completed | 501.86 ms
[                                        ] | 0% Completed | 602.18 ms
[                                        ] | 0% Completed | 702.50 ms
[                                        ] | 0% Completed | 802.87 ms
[                                        ] | 0% Completed | 903.15 ms
[########################################] | 100% Completed | 1.00 s

[<Axes: title={'center': 'Original (polar)'}>, <Axes: title={'center': 'Radial Median'}>, <Axes: title={'center': 'Radial Percentile'}>]

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

Gallery generated by Sphinx-Gallery