{"cells": [{"cell_type": "markdown", "id": "db9fc61d", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": ["# Point-by-point map visualisation notebook \n", "__Written by Haixing Fang, Jon Wright and James Ball__ \n", "__Date: 21/02/2025__"]}, {"cell_type": "markdown", "metadata": {}, "source": "Source: [ImageD11/nbGui/S3DXRD/pbp_2_visualise.ipynb](https://github.com/FABLE-3DXRD/ImageD11/blob/master/ImageD11/nbGui/S3DXRD/pbp_2_visualise.ipynb). Shown as saved in git; the documentation build does not run it."}, {"cell_type": "markdown", "id": "5b634874", "metadata": {}, "source": ["This notebook will view the results of the point-by-point process, and to save a single-valued version of the pbp map to H5, ParaView XDMF and MTEX CTF.\n", "To get much better strains, slightly better orientations and possibly better grain shapes, you should run pbp_3_refinement\n", "Then run 4_visualise to convert the refinement results to an accurate single-valued map with good strains."]}, {"cell_type": "code", "execution_count": null, "id": "9e33e37f", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": ["parameters"]}, "outputs": [], "source": ["# this cell is tagged with 'parameters'\n", "# to view the tag, select the cell, then find the settings gear icon (right or left sidebar) and look for Cell Tags\n", "\n", "# python environment stuff\n", "IMAGED11_PATH = None # means do not use git, otherwise \"ImageD11\" or \"ImageD11_version_xx\", etc\n", "CHECKOUT_PATH = None # None means guess, or you can specify a folder for the checkout\n", "\n", "# dataset file to import\n", "dset_path = 'si_cube_test/processed/Si_cube/Si_cube_S3DXRD_nt_moves_dty/Si_cube_S3DXRD_nt_moves_dty_dataset.h5'\n", "\n", "# which phase to index\n", "phase_str = 'Si'\n", "\n", "# the minimum number of peaks you want a pixel to have to be counted\n", "min_unique = 20"]}, {"cell_type": "code", "execution_count": null, "id": "b6db12f3", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "outputs": [], "source": ["if IMAGED11_PATH is not None:\n", " exec(open('/data/id11/nanoscope/install_ImageD11_from_git.py').read())\n", " PYTHONPATH=setup_ImageD11_from_git(CHECKOUT_PATH, IMAGED11_PATH)\n", "else:\n", " import site\n", " PYTHONPATH = site.getsitepackages()[0]\n", " print(PYTHONPATH)"]}, {"cell_type": "code", "execution_count": null, "id": "e9534db0", "metadata": {"tags": []}, "outputs": [], "source": ["import os\n", "import numpy as np\n", "from matplotlib import pyplot as plt\n", "import matplotlib.cm as cm\n", "from matplotlib.colors import Normalize\n", "\n", "from ImageD11.grain import grain\n", "from ImageD11 import unitcell\n", "import ImageD11.parameters\n", "import ImageD11.sinograms.dataset\n", "from ImageD11.sinograms.point_by_point import PBPMap\n", "from ImageD11.sinograms.tensor_map import TensorMap\n", "from ImageD11.sinograms import polefigures as polefig\n", "from ImageD11.nbGui import nb_utils as utils\n", "\n", "%matplotlib ipympl"]}, {"cell_type": "markdown", "id": "f219f5ce", "metadata": {}, "source": ["# Load data\n", "## Dataset"]}, {"cell_type": "code", "execution_count": null, "id": "74098296", "metadata": {"tags": []}, "outputs": [], "source": ["ds = ImageD11.sinograms.dataset.load(dset_path)\n", "print(ds)"]}, {"cell_type": "markdown", "id": "a3bff9c9", "metadata": {}, "source": ["## Phases\n", "If the parameter file was a json, we can access the unit cells via `ds.phases.unitcells`"]}, {"cell_type": "code", "execution_count": null, "id": "e2ea3f91", "metadata": {"tags": []}, "outputs": [], "source": ["ds.phases = ds.get_phases_from_disk()\n", "ds.phases.unitcells"]}, {"cell_type": "code", "execution_count": null, "id": "10b452be", "metadata": {"tags": []}, "outputs": [], "source": ["ref_ucell = ds.phases.unitcells[phase_str]\n", "print(ref_ucell)"]}, {"cell_type": "markdown", "id": "13708879", "metadata": {}, "source": ["## Point-by-point map"]}, {"cell_type": "code", "execution_count": null, "id": "200e78dc", "metadata": {"tags": []}, "outputs": [], "source": ["pmap = PBPMap(ds.pbpfile.replace('.txt', f'_{phase_str}.txt'))"]}, {"cell_type": "markdown", "id": "2c1309ce", "metadata": {}, "source": ["# Visualise"]}, {"cell_type": "code", "execution_count": null, "id": "138370ec", "metadata": {"tags": []}, "outputs": [], "source": ["pmap.plot_nuniq_hist()"]}, {"cell_type": "code", "execution_count": null, "id": "c5e39815", "metadata": {"tags": []}, "outputs": [], "source": ["# choose the minimum number of peaks you want a pixel to have to be counted\n", "pmap.choose_best(min_unique)\n", "pmap.plot_best(min_unique)"]}, {"cell_type": "markdown", "id": "a1d519b3", "metadata": {}, "source": ["## Compare the point-by-point model to the measured data\n", "\n", "For each indexed voxel, take its best UBI matrix and use it to predict where a\n", "handful of low-angle powder rings should diffract to (calculated tth/eta/omega).\n", "Compare that prediction, ring by ring, to the peaks actually measured on the\n", "detector (`ds.get_cf_2d()`):\n", "\n", "- pole figures: for each ring, the (eta, omega) distribution of the calculated\n", " peaks next to the one measured directly\n", "- sinograms: for each ring, the (omega, dty) distribution of the calculated\n", " peaks next to the one measured directly\n", "\n", "If the point-by-point map is good, the calculated and measured plots for each\n", "ring should line up closely."]}, {"cell_type": "code", "execution_count": null, "id": "f39b7d59", "metadata": {}, "outputs": [], "source": ["# how many low-angle rings to check (keep this small - the calculated g-vector array\n", "# grows with the number of hkls)\n", "n_rings_to_check = 2\n", "hkls, mults = polefig.ring_hkls(ref_ucell, dsmax=2, n_rings=n_rings_to_check)\n", "print('hkl multiplicities for the rings used:', mults)"]}, {"cell_type": "code", "execution_count": null, "id": "9ba8babe", "metadata": {}, "outputs": [], "source": ["pars = ImageD11.parameters.read_par_file(ds.parfile)\n", "\n", "# each hkl has two solutions (a Friedel pair), hence the eta1/eta2, omega1/omega2 arrays\n", "voxel_mask, tth, eta1, eta2, omega1, omega2 = polefig.calc_gve_angles(pmap, hkls, pars)"]}, {"cell_type": "code", "execution_count": null, "id": "199b1bed", "metadata": {}, "outputs": [], "source": ["cf_2d = ds.get_cf_2d()\n", "cf_2d.updateGeometry(pars)"]}, {"cell_type": "code", "execution_count": null, "id": "994bbba9", "metadata": {}, "outputs": [], "source": ["# bin size for the eta/omega histograms below - 1 degree is a reasonable\n", "# default to actually see something; ds.ostep (the scan's angular step) is\n", "# another natural choice\n", "polefig_bin_deg = 1.0\n", "polefig.plot_ring_polefigures(ref_ucell, mults, eta1, eta2, omega1, omega2, cf_2d, bin_deg=polefig_bin_deg, ds=ds)"]}, {"cell_type": "code", "execution_count": null, "id": "e008bea0", "metadata": {}, "outputs": [], "source": ["y0 = ds.y0 # set previously by pbp_1_indexing\n", "dty1, dty2 = polefig.calc_dty(ds, voxel_mask, omega1, omega2, y0)"]}, {"cell_type": "code", "execution_count": null, "id": "30e6dace", "metadata": {}, "outputs": [], "source": ["polefig.plot_ring_sinograms(ds, cf_2d, ref_ucell, mults, omega1, omega2, dty1, dty2)"]}, {"cell_type": "markdown", "id": "99e9960d", "metadata": {}, "source": ["## Make TensorMap"]}, {"cell_type": "code", "execution_count": null, "id": "c093a8d3", "metadata": {"tags": []}, "outputs": [], "source": ["# we can choose to make a TensorMap here\n", "# which is useful for Paraview and MTEX visualisations\n", "\n", "phase_ids = TensorMap.recon_order_to_map_order(np.where(pmap.best_nuniq > min_unique, 0, -1))\n", "tmap = TensorMap.from_pbpmap(pmap, steps=(1, ds.ystep, ds.ystep), phases={0: ref_ucell})\n", "tmap['phase_ids'] = phase_ids\n", "tmap.get_ipf_maps()\n", "tmap.plot('nuniq')\n", "tmap.plot('ipf_x')\n", "tmap.plot('ipf_y')\n", "tmap.plot('ipf_z')\n", "utils.plot_all_ipfs_from_meta_orien(tmap.get_meta_orix_orien(), tmap.phases[0])"]}], "metadata": {"kernelspec": {"display_name": "Python 3 (main)", "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.12.9"}}, "nbformat": 4, "nbformat_minor": 5}