Tomographic indexing notebook with minor phase adjustments#
Written by Haixing Fang, Jon Wright and James Ball
Date: 21/02/2025
Source: ImageD11/nbGui/S3DXRD/tomo_1_index_minor_phase.ipynb. Shown as saved in git; the documentation build does not run it.
This notebook will try to find good grain orientations from a 4D merge of your peak data.
This notebook is optimised for a weaker minor phase in a larger major phase sample.
This means your peaks have been merged across omega and dty.
This notebook (and the tomo route in general) works best for low levels of deformation.
If it doesn’t seem to work well, try the point-by-point route instead!
# 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 names
major_phase_strs = ['Fe']
minor_phase_str = 'Au'
# peak filtration parameters
min_frames_per_peak = 0
# whether to filter out major phase peaks before trying to index
remove_major_phase_peaks = True
major_phase_cf_dstol = 0.005
minor_phase_cf_frac = 0.9975
minor_phase_cf_dsmax = 1.594
minor_phase_cf_dstol = 0.005
# indexing
rings_for_gen = [0, 4, 5]
# now we want to decide which rings to score our found orientations against
# generally we can just exclude dodgy rings (close to other phases, only a few peaks in etc)
rings_for_scoring = [0, 2, 3, 4, 5, 6, 7, 8, 10, 12, 13]
# the sequence of hkl tolerances the indexer will iterate through
hkl_tols_seq = [0.01, 0.02, 0.03, 0.04, 0.05]
# the sequence of minpks fractions the indexer will iterate through
fracs = [0.9]
# the max number of UBIs we can find per pair of rings
max_grains = 1000
peak_assign_tol = 0.025
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 numpy as np
from matplotlib import pyplot as plt
import ImageD11.grain
import ImageD11.indexing
import ImageD11.nbGui.nb_utils as utils
from ImageD11.peakselect import select_ring_peaks_by_intensity, remove_peaks_from_phases
%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
Now we need to decide which are our ‘major’ phases, which we will remove from our peaks before indexing.
major_phase_unitcells = [ds.phases.unitcells[mps] for mps in major_phase_strs]
print(*major_phase_unitcells)
minor_phase_unitcell = ds.phases.unitcells[minor_phase_str]
print(minor_phase_unitcell)
Peaks#
cf_4d = ds.get_cf_4d()
ds.update_colfile_pars(cf_4d) # computes geometry, needed for filtration
for major_phase_unitcell in major_phase_unitcells:
major_phase_unitcell.makerings(cf_4d.ds.max())
minor_phase_unitcell.makerings(cf_4d.ds.max())
# Optionally remove some noisy peaks
if min_frames_per_peak > 0:
cf_4d.filter(cf_4d['npk2d'] > min_frames_per_peak)
Filtration#
(optional) remove major phase peaks#
if remove_major_phase_peaks:
cf_4d = remove_peaks_from_phases(cf_4d, major_phase_cf_dstol, major_phase_unitcells)
Filter by intensity#
Now we are filtering our peaks (cf_4d) to select only the strongest ones for indexing purposes only!
We first filter the peaks in \(d^{*}\) to keep only those close to the predicted peaks from the unit cell.
We then sort our peaks by intensity, and take a certain intensity-weighted fraction of them.
dstol: The tolerance in \(d^{*}\) between a peak and a predicted reflection.
dsmax: The maximum allowed peak \(d^{*}\) value. Used to limit the number of rings given to the indexer - 6-8 rings max are normally sufficient.
frac: The intensity fraction: frac=0.9 keeps 90% of the peak intensity. We recommend that you choose a value close to the ‘elbow’ of the plot.
cf_strong = select_ring_peaks_by_intensity(cf_4d, frac=minor_phase_cf_frac, dsmax=minor_phase_cf_dsmax, dstol=minor_phase_cf_dstol, ucell=minor_phase_unitcell, doplot=0.5)
minor_phase_unitcell.makerings(cf_strong.ds.max())
skip = 1 # we can skip peaks to speed up plotting if needed
fig, ax = plt.subplots(figsize=(10, 5), constrained_layout=True)
ax.plot(cf_4d.ds[::skip], cf_4d.sum_intensity[::skip],',', label='cf_4d',c='blue')
ax.plot(cf_strong.ds[::skip], cf_strong.sum_intensity[::skip],',', label='minor phase',c='orange')
ax.vlines(minor_phase_unitcell.ringds, 1e3, 3e3, color='red', label=minor_phase_unitcell.name)
for inc, major_phase_unitcell in enumerate(major_phase_unitcells):
ax.vlines(major_phase_unitcell.ringds, 3e3*(3**inc), 3e3*(2**inc)*3, color=plt.color_sequences['tab20'][inc], label=major_phase_unitcell.name)
ax.set(xlabel=r'$d^{*}~(\AA^{-1})$', ylabel='Intensity', yscale='log', title='Peak filtration')
ax.legend()
plt.show()
Indexing#
Ring assignment#
indexer = ImageD11.indexing.indexer_from_colfile_and_ucell(cf_strong, minor_phase_unitcell)
indexer.ds_tol = minor_phase_cf_dstol
ImageD11.indexing.loglevel = 1
indexer.assigntorings()
ImageD11.indexing.loglevel = 3
print(f"Indexing {cf_strong.nrows} peaks")
skip = 1 # we can skip peaks to speed up plotting if needed
fig, ax = plt.subplots(layout='constrained', figsize=(10,5))
ax.scatter(indexer.colfile.ds[::skip], indexer.colfile.eta[::skip], c=indexer.ra[::skip]%20, cmap='tab20', s=1)
ax.vlines(minor_phase_unitcell.ringds, -25, 25, color='red', label=minor_phase_unitcell.name)
for inc, major_phase_unitcell in enumerate(major_phase_unitcells):
ax.vlines(major_phase_unitcell.ringds, -25+(50*(inc+1)), 25+(50*(inc+1)), color=plt.color_sequences['tab20'][inc], label=major_phase_unitcell.name)
ax.set(xlabel=r'$d^{*}~(\AA^{-1})$', ylabel=r'$\eta~(\degree)$', xlim=(min(minor_phase_unitcell.ringds[0], cf_strong.ds.min()) - 0.02, cf_strong.ds.max() + 0.02), title='Ring assignments')
ax.legend(loc='upper right')
plt.show()
cosine_tol = np.cos(np.radians(90 - ds.ostep))
grains, indexer = utils.do_index(cf=cf_strong,
unitcell=minor_phase_unitcell,
dstol=minor_phase_cf_dstol,
forgen=rings_for_gen,
foridx=rings_for_scoring,
hkl_tols=hkl_tols_seq,
fracs=fracs,
cosine_tol=cosine_tol,
max_grains=max_grains
)
for ginc, g in enumerate(grains):
g.gid = ginc
print(f'Found {len(grains)} grains!')
Results#
unit_cell_lengths = np.array([np.sort(g.unitcell.copy()) for g in grains])
median_unit_cell = np.median(unit_cell_lengths, axis=0)
print("Median unit cell:", median_unit_cell)
fig, ax = plt.subplots(constrained_layout=True)
ax.plot(unit_cell_lengths[:,:3])
ax.hlines(median_unit_cell[:3], 0, len(unit_cell_lengths))
ax.set(xlabel='Grain ID', ylabel='Unit cell length')
plt.show()
utils.assign_peaks_to_grains(grains, cf_strong, tol=peak_assign_tol)
utils.plot_index_results(indexer, cf_strong, 'Indexing attempt')
utils.plot_grain_sinograms(grains, cf_strong, min(len(grains), 25))
Export data#
ds.save_grains_to_disk(grains, phase_name=minor_phase_str)
ds.save()