{ "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": [ "# Load and save data\n", "\n", "In this tutorial, we will load and save electron backscatter diffraction (EBSD) patterns, Kikuchi master patterns generated with *EMsoft* and virtual backscatter electron (VBSE) images from and to various formats supported by kikuchipy." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load patterns\n", "\n", "### From a file\n", "\n", "kikuchipy can read and write experimental EBSD patterns and Kikuchi master patterns from or to many formats (see [supported formats](#Supported-file-formats)).\n", "To load patterns from a file use, the [load()](../reference/generated/kikuchipy.load.rst) function.\n", "Let's import the necessary libraries and read the nickel EBSD example dataset directly from file (not via [kikuchipy.data.nickel_ebsd_small()](../reference/generated/kikuchipy.data.nickel_ebsd_small.rst))" ] }, { "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 os\n", "from pathlib import Path\n", "import tempfile\n", "\n", "import dask.array as da\n", "import imageio.v3 as iio\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "import hyperspy.api as hs\n", "import kikuchipy as kp" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "datadir = Path(\"../../kikuchipy/data\")\n", "nordif_ebsd = \"nordif/Pattern.dat\"\n", "s = kp.load(datadir / nordif_ebsd)\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or, load the stereographic projection of the upper hemisphere of an EBSD master pattern for a 20 keV beam energy from a modified version of *EMsoft*'s master pattern file, returned from their `EMEBSDmaster.f90` program" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "emsoft_master_pattern = (\n", " \"emsoft_ebsd_master_pattern/ni_mc_mp_20kv_uint8_gzip_opts9.h5\"\n", ")\n", "s_mp = kp.load(datadir / emsoft_master_pattern)\n", "s_mp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Both the stereographic and the square Lambert projections of this master pattern can be loaded with [kikuchipy.data.nickel_ebsd_master_pattern_small()](../reference/generated/kikuchipy.data.nickel_ebsd_master_pattern_small.rst).\n", "\n", "All file readers support accessing the data's metadata without loading it into memory (with the [Dask library](https://docs.dask.org/en/latest)), which can be useful when processing large data sets, one data chunk at a time, to avoid memory errors" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s_lazy = kp.load(datadir / nordif_ebsd, lazy=True)\n", "print(s_lazy)\n", "s_lazy.data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Parts or all of the data can be read into memory by calling [compute()](http://hyperspy.org/hyperspy-doc/current/api/hyperspy._signals.lazy.html#hyperspy._signals.lazy.LazySignal.compute)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s_lazy_copy = s_lazy.inav[:2, :].deepcopy()\n", "s_lazy_copy.compute()\n", "s_lazy_copy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s_lazy.compute()\n", "s_lazy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "