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%|          | 0/33 [00:00<?, ?it/s]
 33%|███▎      | 11/33 [00:00<00:00, 89.36it/s]
 61%|██████    | 20/33 [00:00<00:00, 84.58it/s]
 88%|████████▊ | 29/33 [00:00<00:00, 71.99it/s]
100%|██████████| 33/33 [00:00<00:00, 74.20it/s]
/home/docs/checkouts/readthedocs.org/user_builds/pyxem/envs/stable/lib/python3.11/site-packages/pyxem/utils/_background_subtraction.py:99: FutureWarning: `square` is deprecated since version 0.25 and will be removed in version 0.27. Use `skimage.morphology.footprint_rectangle` instead.
  regional_filter(frame / max_value, **kwargs), footprint=square(3)
/home/docs/checkouts/readthedocs.org/user_builds/pyxem/envs/stable/lib/python3.11/site-packages/hyperspy/misc/utils.py:1439: 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%|          | 0/33 [00:00<?, ?it/s]/home/docs/checkouts/readthedocs.org/user_builds/pyxem/envs/stable/lib/python3.11/site-packages/pyxem/utils/_background_subtraction.py:99: FutureWarning: `square` is deprecated since version 0.25 and will be removed in version 0.27. Use `skimage.morphology.footprint_rectangle` instead.
  regional_filter(frame / max_value, **kwargs), footprint=square(3)
/home/docs/checkouts/readthedocs.org/user_builds/pyxem/envs/stable/lib/python3.11/site-packages/hyperspy/misc/utils.py:1362: 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)

 15%|█▌        | 5/33 [00:00<00:00, 45.11it/s]
 33%|███▎      | 11/33 [00:00<00:00, 25.01it/s]
 45%|████▌     | 15/33 [00:00<00:00, 28.82it/s]
 58%|█████▊    | 19/33 [00:00<00:00, 22.71it/s]
 67%|██████▋   | 22/33 [00:00<00:00, 22.16it/s]
 76%|███████▌  | 25/33 [00:01<00:00, 23.82it/s]
 85%|████████▍ | 28/33 [00:01<00:00, 19.42it/s]
 94%|█████████▍| 31/33 [00:01<00:00, 16.42it/s]
100%|██████████| 33/33 [00:01<00:00, 22.10it/s]

[<Axes: title={'center': 'Original'}, xlabel='kx axis (px)', ylabel='ky axis (px)'>, <Axes: title={'center': 'Difference of Gaussians'}, xlabel='kx axis (px)', ylabel='ky axis (px)'>, <Axes: title={'center': 'H-Dome'}, xlabel='kx axis (px)', ylabel='ky axis (px)'>]

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%|          | 0/17 [00:00<?, ?it/s]
  6%|▌         | 1/17 [00:03<00:55,  3.46s/it]
 59%|█████▉    | 10/17 [00:03<00:01,  3.85it/s]
100%|██████████| 17/17 [00:03<00:00,  4.67it/s]

  0%|          | 0/33 [00:00<?, ?it/s]
100%|██████████| 33/33 [00:00<00:00, 407.02it/s]

  0%|          | 0/33 [00:00<?, ?it/s]
 33%|███▎      | 11/33 [00:00<00:00, 79.37it/s]
 58%|█████▊    | 19/33 [00:00<00:00, 71.18it/s]
 82%|████████▏ | 27/33 [00:00<00:00, 67.52it/s]
100%|██████████| 33/33 [00:00<00:00, 73.30it/s]

[<Axes: title={'center': 'Original (polar)'}, xlabel='Radians axis (Rad)', ylabel='Radius axis (px)'>, <Axes: title={'center': 'Radial Median'}, xlabel='Radians axis (Rad)', ylabel='Radius axis (px)'>, <Axes: title={'center': 'Radial Percentile'}, xlabel='Radians axis (Rad)', ylabel='Radius axis (px)'>]

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

Gallery generated by Sphinx-Gallery