{
"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/generated/kikuchipy.signals.EBSD.rst) and [EBSDMasterPattern](../reference/generated/kikuchipy.signals.EBSDMasterPattern.rst) signals have a powerful and versatile [plot()](http://hyperspy.org/hyperspy-doc/current/api/hyperspy.signal.html#hyperspy.signal.BaseSignal.plot) method provided by HyperSpy.\n",
"Its uses are greatly detailed in HyperSpy's [visualisation user guide](http://hyperspy.org/hyperspy-doc/current/user_guide/visualisation.html).\n",
"This section details example uses specific to EBSD and EBSDMasterPattern signals.\n",
"\n",
"Let's import the necessary libraries and a Nickel EBSD test data set Ă…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 matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pyvista\n",
"import skimage.exposure as ske\n",
"import skimage.transform as skt\n",
"\n",
"import hyperspy.api as hs\n",
"import kikuchipy as kp\n",
"from orix import io, plot, quaternion, vector\n",
"\n",
"\n",
"# See https://docs.pyvista.org/user-guide/jupyter/index.html\n",
"pyvista.set_jupyter_backend(\"panel\")"
]
},
{
"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. indexing, with experimental patterns can inform their interpretation.\n",
"When calling `plot()` without any input parameters, the navigator map is a grey scale image with pixel values corresponding to the sum of all detector intensities 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 beam position in the upper left corner shown as a red square the size of one pixel.\n",
"This square can be made larger/smaller with +/-.\n",
"The square can be moved either by the keyboard arrows or the mouse.\n",
"The lower panel shows the image on the detector in the current beam position.\n",
"\n",
"Any [BaseSignal](http://hyperspy.org/hyperspy-doc/current/api/hyperspy.signal.html#hyperspy.signal.BaseSignal) signal with a 2D `signal_shape` corresponding to the scan `navigation_shape` can be passed in to the `navgiator` parameter in [plot()](http://hyperspy.org/hyperspy-doc/current/api/hyperspy.signal.html#hyperspy.signal.BaseSignal.plot), including a virtual image showing diffraction contrast, any quality metric map, or an orientation map or a phase map."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Virtual image\n",
"\n",
"A virtual backscatter electron (VBSE) image created from any detector region of interest with the [get_virtual_bse_intensity()](../reference/generated/kikuchipy.signals.EBSD.get_virtual_bse_intensity.rst) method or\n",
"[get_rgb_image()](../reference/generated/kikuchipy.imaging.VirtualBSEImager.get_rgb_image.rst) explained in the [virtual backscatter electron imaging](virtual_backscatter_electron_imaging.ipynb) tutorial, can be used as a navigator for a scan `s`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vbse_imager = kp.imaging.VirtualBSEImager(s)\n",
"print(vbse_imager)\n",
"print(vbse_imager.grid_shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vbse_rgb = vbse_imager.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 [Signal2D](http://hyperspy.org/hyperspy-doc/current/api/hyperspy._signals.signal2d.html#hyperspy._signals.signal2d.Signal2D) can be used as navigators, like a quality metric map like the [image quality map](feature_maps.ipynb#Image-quality) calculated using [get_image_quality()](../reference/generated/kikuchipy.signals.EBSD.get_image_quality.rst)"
]
},
{
"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)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can obtain an RGB signal from an RGB image using [get_rgb_navigator()](../reference/generated/kikuchipy.draw.get_rgb_navigator.rst).\n",
"Let's load an orientation map from dictionary indexing in the [pattern matching](pattern_matching.ipynb) tutorial"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rgb_z = plt.imread(\n",
" \"../_static/image/visualizing_patterns/ni_large_rgb_z.png\"\n",
")\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 [plot_signals()](http://hyperspy.org/hyperspy-doc/current/api/hyperspy.drawing.utils.html#hyperspy.drawing.utils.plot_signals) to plot multiple signals with the same navigator, as explained in their [documentation](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 side-by-side as a visual inspection of indexing results.\n",
"See the [pattern matching tutorial](pattern_matching.ipynb#Validate-indexing-results) for a demonstration."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot master patterns\n",
"\n",
"[EBSDMasterPattern](../reference/generated/kikuchipy.signals.EBSDMasterPattern.rst) signals can be navigated along their energy axis and/or their northern/southern hemisphere.\n",
"Let's reload the Nickel master pattern used in the previous section, 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 axes, the northern and southern hemispheres, thus, when plotting, we get a 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 stereographic projection and that both hemispheres are loaded if it is non-centrosymmetric, using [EBSDMasterPattern.plot_spherical()](../reference/generated/kikuchipy.signals.EBSDMasterPattern.plot_spherical.rst).\n",
"The initial orientation of the sphere corresponds to the orientation of the stereographic and Lambert projections."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"nbval-skip"
]
},
"outputs": [],
"source": [
"# Interactive!\n",
"mp_stereo.plot_spherical(style=\"points\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"tags": []
},
"source": [
"This plot uses one of the [Jupyter backends supported by PyVista](https://docs.pyvista.org/user-guide/jupyter/index.html), installed separately, which we set in the first notebook cell.\n",
"If we want to plot the master pattern in a separate window, we can pass `plotter_kwargs={\"notebook\": False}\"` to `plot_spherical()`.\n",
"`PyVista`, required for this interactive plot, is an optional dependency of kikuchipy; see [the installation guide](../user/installation.rst#with-pip) for details."
]
}
],
"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.10.9"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}