{ "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": [ "# Visualizing patterns\n", "\n", "The [EBSD](../reference.rst#kikuchipy.signals.EBSD) and\n", "[EBSDMasterPattern](../reference.rst#kikuchipy.signals.EBSDMasterPattern) signals\n", "have a powerful and versatile\n", "[plot()](http://hyperspy.org/hyperspy-doc/current/api/hyperspy.signal.html#hyperspy.signal.BaseSignal.plot)\n", "method provided by HyperSpy. Its uses are greatly detailed in HyperSpy's\n", "[visualisation user guide](http://hyperspy.org/hyperspy-doc/current/user_guide/visualisation.html).\n", "This section details example uses specific to EBSD and EBSDMasterPattern\n", "signals.\n", "\n", "Let's import the necessary libraries and a Nickel EBSD test data set\n", "Ă…nes et al. (2019):" ] }, { "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 kikuchipy as kp\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from orix import io, plot, quaternion, vector\n", "import pyvista\n", "import skimage.exposure as ske\n", "import skimage.transform as skt\n", "\n", "\n", "pyvista.global_theme.window_size = [700, 700]\n", "pyvista.set_jupyter_backend(\"pythreejs\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Use kp.load(\"data.h5\") to load your own data\n", "s = kp.data.nickel_ebsd_large(allow_download=True) # External download\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Navigate in custom map\n", "\n", "Correlating results from e.g. crystal and phase structure determination, i.e.\n", "indexing, with experimental patterns can inform their interpretation. When\n", "calling `plot()` without any input parameters, the navigator map is a grey scale\n", "image with pixel values corresponding to the sum of all detector intensities\n", "within that pattern:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The upper panel shows the navigation axes, in this case 2D, with the current\n", "beam position in the upper left corner shown as a red square the size of one\n", "pixel. This square can be made larger/smaller with +/-.\n", "The square can be moved either by the keyboard arrows or the mouse. The lower\n", "panel shows the image on the detector in the current beam position." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any\n", "[BaseSignal](http://hyperspy.org/hyperspy-doc/current/api/hyperspy.signal.html#hyperspy.signal.BaseSignal)\n", "signal with a 2D `signal_shape` corresponding to the scan\n", "`navigation_shape` can be passed in to the `navgiator` parameter in\n", "[plot()](http://hyperspy.org/hyperspy-doc/current/api/hyperspy.signal.html#hyperspy.signal.BaseSignal.plot),\n", "including a virtual image showing diffraction contrast, any quality metric map,\n", "or an orientation map or a phase map." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Virtual image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A virtual backscatter electron (VBSE) image created from any detector region of\n", "interest with the\n", "[get_virtual_bse_intensity()](../reference.rst#kikuchipy.signals.EBSD.get_virtual_bse_intensity)\n", "method or\n", "[get_rgb_image()](../reference.rst#kikuchipy.generators.VirtualBSEGenerator.get_rgb_image)\n", "explained in the\n", "[virtual backscatter electron imaging](virtual_backscatter_electron_imaging.rst)\n", "section, can be used as a navigator for a scan `s`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vbse_gen = kp.generators.VirtualBSEGenerator(s)\n", "print(vbse_gen)\n", "print(vbse_gen.grid_shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vbse_rgb = vbse_gen.get_rgb_image(r=(3, 1), b=(3, 2), g=(3, 3))\n", "vbse_rgb" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.plot(navigator=vbse_rgb, cmap=\"viridis\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Any image\n", "\n", "Images made into a\n", "[Signal2D](http://hyperspy.org/hyperspy-doc/current/api/hyperspy._signals.signal2d.html#hyperspy._signals.signal2d.Signal2D)\n", "can be used as navigators, like a quality metric map like the\n", "[image quality map](feature_maps.ipynb#Image-quality) calculated using\n", "[get_image_quality()](../reference.rst#kikuchipy.signals.EBSD.get_image_quality):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.remove_static_background()\n", "s.remove_dynamic_background()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "iq = s.get_image_quality()\n", "s_iq = hs.signals.Signal2D(iq)\n", "s.plot(navigator=s_iq, scalebar=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can obtain an RGB signal from an RGB image using\n", "[get_rgb_navigator()](../reference.rst#kikuchipy.draw.get_rgb_navigator).\n", "Let's load an orientation map from dictionary indexing in the\n", "[pattern matching](pattern_matching.rst) user guide" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rgb_z = plt.imread(\"../_static/image/visualizing_patterns/ni_large_rgb_z.png\")\n", "rgb_z = rgb_z[..., :3] # Drop the alpha channel\n", "s_rgb_z = kp.draw.get_rgb_navigator(rgb_z)\n", "\n", "s.plot(navigator=s_rgb_z, colorbar=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot multiple signals\n", "\n", "HyperSpy provides the function\n", "[plot_signals()](http://hyperspy.org/hyperspy-doc/current/api/hyperspy.drawing.utils.html#hyperspy.drawing.utils.plot_signals)\n", "to plot multiple signals with the same navigator, as explained in their\n", "[user guide](http://hyperspy.org/hyperspy-doc/current/user_guide/visualisation.html#plotting-several-signals).\n", "This enables e.g. plotting of experimental and best matching simulated patterns\n", "side-by-side as a visual inspection of indexing results. See the\n", "[pattern matching user guide](pattern_matching.ipynb#Analyze-indexing-results)\n", "for a demonstration." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot master patterns\n", "\n", "[EBSDMasterPattern](../reference.rst#kikuchipy.signals.EBSDMasterPattern) signals\n", "can be navigated along their energy axis and/or their northern/southern\n", "hemisphere. Let's reload the Nickel master pattern used in the previous section,\n", "but this time in the stereographic projection." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Only a single energy, 20 keV\n", "mp_stereo = kp.data.nickel_ebsd_master_pattern_small(\n", " projection=\"stereographic\", hemisphere=\"both\"\n", ")\n", "print(mp_stereo.axes_manager)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As can be seen from the axes manager, the master pattern has two navigation\n", "axes, the northern and southern hemispheres, thus, when plotting, we get a\n", "navigation slider" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbsphinx-thumbnail": { "tooltip": "Ways to visualize the navigation (scan) and signal (detector) dimensions" }, "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "mp_stereo.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can plot the master pattern on the sphere, provided it is in the\n", "stereographic projection and that both hemispheres are loaded if it is\n", "non-centrosymmetric, using\n", "[EBSDMasterPattern.plot_spherical()](../reference.rst#kikuchipy.signals.EBSDMasterPattern.plot_spherical).\n", "The initial orientation of the sphere corresponds to the orientation of the\n", "stereographic and Lambert projections." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "mp_stereo.plot_spherical(style=\"points\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This plot uses the [pythreejs](https://github.com/jupyter-widgets/pythreejs)\n", "package for plotting in the notebook, which must be installed separately. If we\n", "want to plot the master pattern in a separate window using\n", "[pyvista](https://docs.pyvista.org) instead, we can pass\n", "`plotter_kwargs={\"notebook\": False}\"` to `plot_spherical()`." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }