{"cells": [{"cell_type": "markdown", "id": "bbc4bc96-cbc7-436a-a174-c99388869cbb", "metadata": {"editable": true, "slideshow": {"slide_type": ""}, "tags": []}, "source": ["# Notebook to combine TensorMap phases \n", "__Written by Haixing Fang, Jon Wright and James Ball__ \n", "__Date: 21/02/2025__"]}, {"cell_type": "markdown", "metadata": {}, "source": "Source: [ImageD11/nbGui/S3DXRD/5_combine_phases.ipynb](https://github.com/FABLE-3DXRD/ImageD11/blob/master/ImageD11/nbGui/S3DXRD/5_combine_phases.ipynb). Shown as saved in git; the documentation build does not run it."}, {"cell_type": "code", "execution_count": null, "id": "94b89030-fdb2-47d2-bc26-3e5cfb0d6509", "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", "phase_strs = ['Fe', 'Au']\n", "\n", "# whether or not we are combining refined tensormaps (changes where we look for them)\n", "combine_refined = True"]}, {"cell_type": "code", "execution_count": null, "id": "dc15bdb0-cbfc-42f5-9e86-4d46d1dd1527", "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": "8f4b91e0-7a83-462b-85cb-27f65721ff93", "metadata": {}, "outputs": [], "source": ["import os\n", "\n", "import ImageD11.sinograms.dataset\n", "from ImageD11.sinograms.tensor_map import TensorMap\n", "\n", "%matplotlib ipympl"]}, {"cell_type": "markdown", "id": "3c99b07f-46f1-4265-826e-7d17cc7baf34", "metadata": {}, "source": ["# Load data\n", "## Dataset"]}, {"cell_type": "code", "execution_count": null, "id": "c3c559a5-0202-4f54-be32-20830f5fd6a1", "metadata": {}, "outputs": [], "source": ["ds = ImageD11.sinograms.dataset.load(dset_path)\n", "print(ds)"]}, {"cell_type": "markdown", "id": "93ce14c6-c69d-482f-9455-1e92dfa99976", "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": "741bb37b-8fd2-40a0-b3e8-e9ce2451fc76", "metadata": {"tags": []}, "outputs": [], "source": ["ds.phases = ds.get_phases_from_disk()\n", "ds.phases.unitcells"]}, {"cell_type": "code", "execution_count": null, "id": "58bababb-461c-41a1-898d-378418fdc4f4", "metadata": {}, "outputs": [], "source": ["# what phases are we merging?\n", "print(*[ds.phases.unitcells[phase_str].lattice_parameters for phase_str in phase_strs], sep='\\n')"]}, {"cell_type": "markdown", "id": "def6fe02-6e5f-45c8-b401-24a4533991b3", "metadata": {}, "source": ["Choose where to import your TensorMaps from. \n", "If you refined them, you'll need to change the below paths to point to the separate refined tensormap h5 files."]}, {"cell_type": "code", "execution_count": null, "id": "ce95ef6a-6267-4403-a2e4-0bcbb59350b9", "metadata": {"tags": []}, "outputs": [], "source": ["if combine_refined:\n", " tensor_maps = [TensorMap.from_h5(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_refined_tmap_{phase_str}.h5')) for phase_str in phase_strs]\n", "else:\n", " tensor_maps = [TensorMap.from_h5(ds.grainsfile, h5group='TensorMap_' + phase_str) for phase_str in phase_strs]"]}, {"cell_type": "code", "execution_count": null, "id": "1cf5e081-feda-4dee-a260-36f4f286ec1a", "metadata": {}, "outputs": [], "source": ["try:\n", " for tmap in tensor_maps:\n", " tmap.plot('labels')\n", "except KeyError:\n", " # no labels field\n", " pass"]}, {"cell_type": "code", "execution_count": null, "id": "705c9219-2c22-4db6-9c64-150fc286cb1d", "metadata": {"tags": []}, "outputs": [], "source": ["tensor_map_combined = TensorMap.from_combine_phases(tensor_maps)"]}, {"cell_type": "code", "execution_count": null, "id": "9a5baf01-2f49-4c44-bf6c-7b39a165b29c", "metadata": {"tags": []}, "outputs": [], "source": ["tensor_map_combined.plot('phase_ids')\n", "try:\n", " tensor_map_combined.plot('labels')\n", "except KeyError:\n", " # no labels field\n", " pass\n", "tensor_map_combined.plot('ipf_z')"]}, {"cell_type": "markdown", "id": "8d12bf9a-3eca-43c2-abc1-31de1ef24c85", "metadata": {}, "source": ["# Export"]}, {"cell_type": "code", "execution_count": null, "id": "68bccb3c-9338-4202-a5c1-6292183836dd", "metadata": {"tags": []}, "outputs": [], "source": ["if combine_refined:\n", " tensor_map_combined.to_h5(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_refined_tmap_combined.h5'))\n", " tensor_map_combined.to_paraview(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_refined_tmap_combined.h5'))\n", "else:\n", " tensor_map_combined.to_h5(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_tmap_combined.h5'))\n", " tensor_map_combined.to_paraview(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_tmap_combined.h5'))"]}], "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}