.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/processing/filtering_data.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_filtering_data.py: Filtering Data ============== If you have a low number of counts in your data, you may want to filter the data to remove noise. This can be done using the `filter` function which applies some function to the entire dataset and returns a filtered dataset of the same shape. .. GENERATED FROM PYTHON SOURCE LINES 9-33 .. code-block:: Python from scipy.ndimage import gaussian_filter from dask_image.ndfilters import gaussian_filter as dask_gaussian_filter import pyxem as pxm import hyperspy.api as hs import numpy as np s = pxm.data.mgo_nanocrystals(allow_download=True) # MgO nanocrystals dataset s_filtered = s.filter( gaussian_filter, sigma=1.0, inplace=False ) # Gaussian filter with sigma=1.0 s_filtered2 = s.filter( gaussian_filter, sigma=(1.0, 1.0, 0, 0), inplace=False ) # Only filter in real space hs.plot.plot_images( [s.inav[10, 10], s_filtered.inav[10, 10], s_filtered2.inav[10, 10]], label=["Original", "GaussFilt(all)", "GaussFilt(real space)"], tight_layout=True, vmax="99th", ) .. image-sg:: /examples/processing/images/sphx_glr_filtering_data_001.png :alt: Original, GaussFilt(all), GaussFilt(real space) :srcset: /examples/processing/images/sphx_glr_filtering_data_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0.00/104M [00:00, , ] .. GENERATED FROM PYTHON SOURCE LINES 34-53 .. code-block:: Python """ The `filter` function can also be used with a custom function as long as the function takes a numpy array as input and returns a numpy array of the same shape. """ def custom_filter(array): filtered = gaussian_filter(array, sigma=1.0) return filtered - np.mean(filtered) s_filtered3 = s.filter(custom_filter, inplace=False) # Custom filter hs.plot.plot_images( [s.inav[10, 10], s_filtered3.inav[10, 10]], label=["Original", "GaussFilt(Custom)"], tight_layout=True, vmax="99th", ) .. image-sg:: /examples/processing/images/sphx_glr_filtering_data_002.png :alt: Original, GaussFilt(Custom) :srcset: /examples/processing/images/sphx_glr_filtering_data_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [, ] .. GENERATED FROM PYTHON SOURCE LINES 54-72 .. code-block:: Python """ For lazy datasets, functions which operate on dask arrays can be used. For example, the `gaussian_filter` function from `scipy.ndimage` is replaced with the `dask_image` version which operates on dask arrays. """ s = s.as_lazy() # Convert to lazy dataset s_filtered4 = s.filter( dask_gaussian_filter, sigma=1.0, inplace=False ) # Gaussian filter with sigma=1.0 hs.plot.plot_images( [s_filtered.inav[10, 10], s_filtered4.inav[10, 10]], label=["GaussFilt", "GaussFilt(Lazy)"], tight_layout=True, vmax="99th", ) .. image-sg:: /examples/processing/images/sphx_glr_filtering_data_003.png :alt: GaussFilt, GaussFilt(Lazy) :srcset: /examples/processing/images/sphx_glr_filtering_data_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [, ] .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 45.310 seconds) .. _sphx_glr_download_examples_processing_filtering_data.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: filtering_data.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: filtering_data.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_