{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "This notebook is part of the `kikuchipy` documentation https://kikuchipy.org.\n", "Links to the documentation won't work from the notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pattern processing\n", "\n", "The raw EBSD signal can be empirically evaluated as a superposition of a Kikuchi\n", "diffraction pattern and a smooth background intensity. For pattern indexing, the\n", "latter intensity is usually undesirable, while for\n", "[virtual backscatter electron VBSE) imaging](virtual_backscatter_electron_imaging.rst),\n", "this intensity can reveal topographical, compositional or diffraction contrast.\n", "This section details methods to enhance the Kikuchi diffraction pattern and\n", "manipulate detector intensities in patterns in an\n", "[EBSD](../reference.rst#kikuchipy.signals.EBSD) signal.\n", "\n", "Most of the methods operating on EBSD objects use functions that operate on the\n", "individual patterns (`numpy.ndarray`). These single pattern functions are\n", "available in the [kikuchipy.pattern](../reference.rst#pattern) module.\n", "\n", "Let's import the necessary libraries and read the Nickel EBSD test data set" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exchange inline for notebook or qt5 (from pyqt) for interactive plotting\n", "%matplotlib inline\n", "\n", "import hyperspy.api as hs\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import kikuchipy as kp\n", "\n", "\n", "s = kp.data.nickel_ebsd_small() # Use kp.load(\"data.h5\") to load your own data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most methods operate inplace (indicated in their docstrings), meaning they\n", "overwrite the patterns in the EBSD signal. If we instead want to keep the\n", "original signal and operate on a new signal, we can create a\n", "[deepcopy()](http://hyperspy.org/hyperspy-doc/current/api/hyperspy.signal.html#hyperspy.signal.BaseSignal.deepcopy)\n", "of the original signal. As an example here, we create a new EBSD signal from a\n", "small part of the original signal:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s2 = s.deepcopy()\n", "np.may_share_memory(s.data, s2.data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "## Background correction\n", "\n", "### Remove the static background\n", "\n", "Effects which are constant, like hot pixels or dirt on the detector, can be\n", "removed by either subtracting or dividing by a static background via\n", "[remove_static_background()](../reference.rst#kikuchipy.signals.EBSD.remove_static_background):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s2.remove_static_background(operation=\"subtract\", relative=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbsphinx-thumbnail": { "tooltip": "Enhancement of Kikuchi bands in EBSD patterns" }, "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(13, 6), ncols=2)\n", "ax[0].imshow(s.inav[0, 0].data, cmap=\"gray\")\n", "ax[0].set_title(\"As acquired\")\n", "ax[0].axis(\"off\")\n", "ax[1].imshow(s2.inav[0, 0].data, cmap=\"gray\")\n", "ax[1].set_title(\"Static background removed\")\n", "ax[1].axis(\"off\")\n", "fig.tight_layout(w_pad=0-8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, the static background pattern is assumed to be stored as part of the\n", "signal `metadata`, which can be loaded via\n", "[set_experimental_parameters()](../reference.rst#kikuchipy.signals.EBSD.set_experimental_parameters).\n", "The static background pattern can also be passed to the `static_bg` parameter.\n", "Passing `relative=True` (default) ensures that relative intensities between\n", "patterns are kept when they are rescaled after correction to fill the available\n", "data range. In this case, for a scan of data type `uint8` with data range\n", "[0, 255], the highest pixel intensity in a scan is stretched to 255 (and the\n", "lowest to 0), while the rest is rescaled keeping relative intensities between\n", "patterns. With `relative=False`, all patterns are stretched to [0, 255].\n", "\n", "