Notebook to combine TensorMap phases#

Written by Haixing Fang, Jon Wright and James Ball
Date: 21/02/2025

Source: ImageD11/nbGui/S3DXRD/5_combine_phases.ipynb. Shown as saved in git; the documentation build does not run it.

# this cell is tagged with 'parameters'
# to view the tag, select the cell, then find the settings gear icon (right or left sidebar) and look for Cell Tags

# python environment stuff
IMAGED11_PATH = None  # means do not use git, otherwise "ImageD11" or "ImageD11_version_xx", etc
CHECKOUT_PATH = None  # None means guess, or you can specify a folder for the checkout

# dataset file to import
dset_path = 'si_cube_test/processed/Si_cube/Si_cube_S3DXRD_nt_moves_dty/Si_cube_S3DXRD_nt_moves_dty_dataset.h5'

phase_strs = ['Fe', 'Au']

# whether or not we are combining refined tensormaps (changes where we look for them)
combine_refined = True
if IMAGED11_PATH is not None:
    exec(open('/data/id11/nanoscope/install_ImageD11_from_git.py').read())
    PYTHONPATH=setup_ImageD11_from_git(CHECKOUT_PATH, IMAGED11_PATH)
else:
    import site
    PYTHONPATH = site.getsitepackages()[0]
    print(PYTHONPATH)
import os

import ImageD11.sinograms.dataset
from ImageD11.sinograms.tensor_map import TensorMap

%matplotlib ipympl

Load data#

Dataset#

ds = ImageD11.sinograms.dataset.load(dset_path)
print(ds)

Phases#

If the parameter file was a json, we can access the unit cells via ds.phases.unitcells

ds.phases = ds.get_phases_from_disk()
ds.phases.unitcells
# what phases are we merging?
print(*[ds.phases.unitcells[phase_str].lattice_parameters for phase_str in phase_strs], sep='\n')

Choose where to import your TensorMaps from.
If you refined them, you’ll need to change the below paths to point to the separate refined tensormap h5 files.

if combine_refined:
    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]
else:
    tensor_maps = [TensorMap.from_h5(ds.grainsfile, h5group='TensorMap_' + phase_str) for phase_str in phase_strs]
try:
    for tmap in tensor_maps:
        tmap.plot('labels')
except KeyError:
    # no labels field
    pass
tensor_map_combined = TensorMap.from_combine_phases(tensor_maps)
tensor_map_combined.plot('phase_ids')
try:
    tensor_map_combined.plot('labels')
except KeyError:
    # no labels field
    pass
tensor_map_combined.plot('ipf_z')

Export#

if combine_refined:
    tensor_map_combined.to_h5(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_refined_tmap_combined.h5'))
    tensor_map_combined.to_paraview(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_refined_tmap_combined.h5'))
else:
    tensor_map_combined.to_h5(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_tmap_combined.h5'))
    tensor_map_combined.to_paraview(os.path.join(ds.analysispath, f'{ds.sample}_{ds.dset}_tmap_combined.h5'))