{"cells": [{"cell_type": "markdown", "id": "7a9839f6-a3f2-4b34-b0d5-9095be4eac08", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": ["# Notebook to stack multiple scanning 3DXRD layers\n", "__Written by Haixing Fang, Jon Wright and James Ball__ \n", "__Date: 21/02/2025__\n", "\n", "With this notebook, we stack together multiple scanning 3DXRD layers together in Z."]}, {"cell_type": "markdown", "metadata": {}, "source": "Source: [ImageD11/nbGui/S3DXRD/7_stack_layers.ipynb](https://github.com/FABLE-3DXRD/ImageD11/blob/master/ImageD11/nbGui/S3DXRD/7_stack_layers.ipynb). Shown as saved in git; the documentation build does not run it."}, {"cell_type": "code", "execution_count": null, "id": "92baa2cd-4e85-4998-979b-88ef0a674c48", "metadata": {"tags": []}, "outputs": [], "source": ["exec(open('/data/id11/nanoscope/install_ImageD11_from_git.py').read())"]}, {"cell_type": "code", "execution_count": null, "id": "e744a4a3-53f9-4fe5-a452-90a1b8dd8d6d", "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", "# supply the path to one dataset file - we'll find the rest automatically\n", "dset_path = 'si_cube_test/processed/Si_cube/Si_cube_S3DXRD_nt_moves_dty/Si_cube_S3DXRD_nt_moves_dty_dataset.h5'\n", "# common prefix to all dataset names\n", "dset_prefix = \"top_\"\n", "\n", "# are you looking to stack phase-combined layers?\n", "stack_combined = True\n", "# otherwise what is the phase you're interested in stacking?\n", "phase_str = 'Fe'\n", "# are the layers strain refined?\n", "stack_refined = True\n", "\n", "# what's the z step in microns?\n", "zstep = None"]}, {"cell_type": "code", "execution_count": null, "id": "34acef1f-3a2b-4baa-a917-c36f37fcf0fe", "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": "74e066b0-ae13-46e9-81b0-a11ea524a469", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "outputs": [], "source": ["from collections import OrderedDict\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "import ImageD11.sinograms.dataset\n", "from ImageD11.sinograms.tensor_map import TensorMap\n", "\n", "import ImageD11.nbGui.nb_utils as utils\n", "\n", "%matplotlib ipympl"]}, {"cell_type": "markdown", "id": "7f290b77-6bd5-412f-916e-21fa6ae685ed", "metadata": {}, "source": ["# Load data\n", "## Dataset"]}, {"cell_type": "code", "execution_count": null, "id": "ec147b86-3ce5-44ed-bfaf-bc7b3fde371e", "metadata": {}, "outputs": [], "source": ["ds = ImageD11.sinograms.dataset.load(dset_path)\n", "print(ds)"]}, {"cell_type": "markdown", "id": "0b5f8dab-47cb-4dc2-84cc-287c5cccc30c", "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": "8ea038db-b86c-4ce3-9117-83060f68351e", "metadata": {}, "outputs": [], "source": ["ds.phases = ds.get_phases_from_disk()\n", "ds.phases.unitcells"]}, {"cell_type": "code", "execution_count": null, "id": "a1ff363a-8106-45c4-b4de-f557f33c852a", "metadata": {"tags": []}, "outputs": [], "source": ["skips_dict = {ds.sample: []}\n", "sample_list = [ds.sample]\n", "samples_dict = utils.find_datasets_to_process(ds.dataroot, skips_dict, dset_prefix, sample_list)"]}, {"cell_type": "code", "execution_count": null, "id": "e659e4b6-2962-45c0-a980-d8207a3479be", "metadata": {}, "outputs": [], "source": ["# pick a specific sample to continue\n", "\n", "sample = ds.sample\n", "ds_dict = OrderedDict()\n", "\n", "for dataset in samples_dict[sample]:\n", " # read the ds\n", " dset_path = os.path.join(ds.analysisroot, sample, f\"{sample}_{dataset}\", f\"{sample}_{dataset}_dataset.h5\")\n", " ds = ImageD11.sinograms.dataset.load(dset_path)\n", " \n", " # read the tensormaps\n", " # choose where to import your TensorMaps from\n", " \n", " if stack_combined and stack_refined:\n", " ds.tensor_map = TensorMap.from_h5(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_refined_tmap_combined.h5'))\n", " elif stack_combined:\n", " ds.tensor_map = TensorMap.from_h5(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_tmap_combined.h5'))\n", " elif stack_refined:\n", " ds.tensor_map = TensorMap.from_h5(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_refined_tmap_{phase_str}.h5'))\n", " else:\n", " ds.tensor_map = TensorMap.from_h5(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_tmap_{phase_str}.h5'))\n", "\n", " ds_dict[dataset] = ds"]}, {"cell_type": "code", "execution_count": null, "id": "5a67e6da-0581-4b35-ab55-27a53982d774", "metadata": {}, "outputs": [], "source": ["# in this case, first scan has the Z motor at its lowest value\n", "# you should double-check this for your data!\n", "# this means we're scanning the highest region on the sample first\n", "# then moving down in the sample as we increase the Z number\n", "# therefore we want the combined tensormaps in reverse order\n", "\n", "# choose the z step for your data\n", "# in our case we assume it's the same as the y step\n", "# this may not be true for you!!!\n", "\n", "if zstep is None:\n", " zstep = ds.ystep\n", "\n", "combined_tensormap = TensorMap.from_stack([ds.tensor_map for ds in list(ds_dict.values())][::-1], zstep=zstep)"]}, {"cell_type": "markdown", "id": "e7a2f626-258d-4c53-93f5-92e6411f9846", "metadata": {}, "source": ["# note: if a labels array makes it into the combined tensormap, it is not modified during the stacking procedure!\n", "# labels are original to their own layer"]}, {"cell_type": "code", "execution_count": null, "id": "798acf0d-c5c7-47d8-b0b5-ce8cc89a87e9", "metadata": {}, "outputs": [], "source": ["# compute and plot strains for Z slice 0\n", "\n", "fig, axs = plt.subplots(3,3, sharex=True, sharey=True, layout='constrained')\n", "for i in range(3):\n", " for j in range(3):\n", " axs[i,j].imshow(combined_tensormap.eps_sample[0, ..., i, j])\n", " axs[i,j].set_title(f'eps_{i+1}{j+1}')\n", "plt.show()"]}, {"cell_type": "code", "execution_count": null, "id": "28707736-3db4-476c-9204-0ae15ead9365", "metadata": {}, "outputs": [], "source": ["# export to file\n", "\n", "output_path = os.path.join(ds.analysisroot, f'{ds.sample}_stacked.h5')\n", "\n", "combined_tensormap.to_h5(output_path)\n", "combined_tensormap.to_paraview(output_path)"]}], "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"}, "widgets": {"application/vnd.jupyter.widget-state+json": {"state": {}, "version_major": 2, "version_minor": 0}}}, "nbformat": 4, "nbformat_minor": 5}