Tomographic mapping notebook#
Written by Haixing Fang, Jon Wright and James Ball
Date: 21/02/2025
Source: ImageD11/nbGui/S3DXRD/tomo_3_refinement.ipynb. Shown as saved in git; the documentation build does not run it.
This notebook will try to perform a point-by-point strain refinement from your tomographic-derived grain shapes.
NOTE: It is highly recommended to run this notebook on a Jupyter server with many cores and a lot of RAM.#
The compute_origins() function in particular runs locally and can be compute-intensive for large datasets.
If this is a big scan (e.g 100 million+ 2D peaks), you should definitely refine on the cluster rather than locally.
import os
os.environ['OMP_NUM_THREADS'] = '1'
os.environ['OPENBLAS_NUM_THREADS'] = '1'
os.environ['MKL_NUM_THREADS'] = '1'
# 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'
# which phase to refine
phase_str = 'Si'
# manual override of y0 (ignores any value saved in dataset)
y0_manual = None
# or y0_manual = 0.0 (typical NSCOPE) or 13.5 (typical TDXRD)
# default options for the single-valued map (shouldn't need to modify this)
default_npks = 20
default_nuniq = 20
# refinement tolerances
hkl_tol_origins = 0.05
# ^ we forgot to square this before!
hkl_tol_refine = 0.1
hkl_tol_refine_merged = 0.05
ds_tol = 0.004
ifrac = 1e-4
rings_to_refine = None # can be a list of rings
drop_input_peaks = False
# ^ for large datasets, delete cf_2d from memory after choosing peaks to refine with
use_origin_found_only=True
# use cluster for refinement or run locally?
use_cluster = False
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 numpy as np
import matplotlib.pyplot as plt
import ImageD11.sinograms.dataset
from ImageD11.sinograms.sinogram import read_h5
from ImageD11.sinograms.tensor_map import TensorMap
from ImageD11.sinograms.pbp_refine import PBPRefine
import ImageD11.nbGui.nb_utils as utils
%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
ucell = ds.phases.unitcells[phase_str]
print(ucell)
Peaks#
cf_2d = ds.get_cf_2d()
ds.update_colfile_pars(cf_2d) # computes geometry, needed for filtration
print(f"Read {cf_2d.nrows} 2D peaks")
Grains#
grainsinos = read_h5(ds.grainsfile, ds, phase_str)
grains = [gs.grain for gs in grainsinos]
TensorMap#
tensor_map = TensorMap.from_h5(ds.grainsfile, h5group='TensorMap_' + phase_str)
tensor_map.plot('phase_ids')
# make a PBPMap from our TensorMap
# fills voxels that have grains with npks = 20 and nuniq = 20
pmap = tensor_map.to_pbpmap(z_layer=0, default_npks=default_npks, default_nuniq=default_nuniq)
pmap.choose_best(1)
pmap.plot_best(1)
# Priority order (decreasing)
# manual override in first cell
# ds attribute
# fails if no y0 provided at all
if y0_manual is not None:
print('Using manually supplied y0')
y0 = y0_manual
else:
if hasattr(ds, 'y0') and ds.y0 is not None:
print('Using ds.y0')
y0 = ds.y0
else:
raise ValueError('You must manually supply a y0 value in the first cell to continue.')
print('Using y0:', y0)
# if we got here, save to ds.y0 - future refinements should use the same one
ds.y0 = y0
ds.save()
Refinement#
# set up a refinement manager object
refine = PBPRefine(dset=ds,
y0=y0, hkl_tol_origins=hkl_tol_origins, hkl_tol_refine=hkl_tol_refine, hkl_tol_refine_merged=hkl_tol_refine_merged,
ds_tol=ds_tol, ifrac=ifrac, phase_name=phase_str, forref=rings_to_refine,
beam_size=None) # use step size as beam size - important for regularisation
# change the default paths of the refinement manager to append the phase name
refine.own_filename = os.path.splitext(refine.own_filename)[0] + f'_{phase_str}.h5'
refine.icolf_filename = os.path.splitext(refine.icolf_filename)[0] + f'_{phase_str}.h5'
refine.pbpmap_filename = os.path.splitext(refine.pbpmap_filename)[0] + f'_{phase_str}.h5'
refine.refinedmap_filename = os.path.splitext(refine.refinedmap_filename)[0] + f'_{phase_str}.h5'
# tell it which point-by-point map we are refining
refine.setmap(pmap)
# or load from disk:
# refine.loadmap()
# set the mask from minimum peak values
# anything greater than 0 should be accepted
refine.mask = pmap.best_npks > 0
# generate a single-valued map to refine on
refine.setsingle(refine.pbpmap, minpeaks=1)
# choose 2D peaks to refine with
refine.setpeaks(cf_2d, keep_colf=not drop_input_peaks)
# or load from disk:
# refine.loadpeaks()
if drop_input_peaks:
del cf_2d
import gc; gc.collect()
print("released input peaks")
Setting up peaks#
# plot the peaks you selected
refine.iplot()
Compute peak diffraction origins#
# compute diffraction origins - these will be added as a column to refine.icolf
# will then save the new column to disk to avoid re-computation
refine.get_origins()
# after refine.get_origins()
if use_origin_found_only:
n0 = refine.icolf.nrows
refine.icolf = refine.icolf.copyrows(refine.icolf.origin_found == 1)
print(f"origin_found filter: kept {refine.icolf.nrows} / {n0} peaks")
Run refinement#
# run the refinement
# if compute_origins took more than a couple of minutes to run, I suggest setting use_cluster=True below
# otherwise if you asked for lots of cores and RAM on this Jupyter instance, you can run it locally (use_cluster=False)
refine.run_refine(use_cluster=use_cluster, pythonpath=PYTHONPATH)
Export#
# make sure refinement is saved to disk
if not use_cluster:
refine.to_h5(save_peaks=False)
ds.save()