Point-by-point map visualisation notebook#

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

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

This notebook will view the results of the point-by-point process, and to save a single-valued version of the pbp map to H5, ParaView XDMF and MTEX CTF. To get much better strains, slightly better orientations and possibly better grain shapes, you should run pbp_3_refinement Then run 4_visualise to convert the refinement results to an accurate single-valued map with good strains.

# 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 index
phase_str = 'Si'

# the minimum number of peaks you want a pixel to have to be counted
min_unique = 20
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
from matplotlib import pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize

from ImageD11.grain import grain
from ImageD11 import unitcell
import ImageD11.parameters
import ImageD11.sinograms.dataset
from ImageD11.sinograms.point_by_point import PBPMap
from ImageD11.sinograms.tensor_map import TensorMap
from ImageD11.sinograms import polefigures as polefig
from ImageD11.nbGui import 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
ref_ucell = ds.phases.unitcells[phase_str]
print(ref_ucell)

Point-by-point map#

pmap = PBPMap(ds.pbpfile.replace('.txt', f'_{phase_str}.txt'))

Visualise#

pmap.plot_nuniq_hist()
# choose the minimum number of peaks you want a pixel to have to be counted
pmap.choose_best(min_unique)
pmap.plot_best(min_unique)

Compare the point-by-point model to the measured data#

For each indexed voxel, take its best UBI matrix and use it to predict where a handful of low-angle powder rings should diffract to (calculated tth/eta/omega). Compare that prediction, ring by ring, to the peaks actually measured on the detector (ds.get_cf_2d()):

  • pole figures: for each ring, the (eta, omega) distribution of the calculated peaks next to the one measured directly

  • sinograms: for each ring, the (omega, dty) distribution of the calculated peaks next to the one measured directly

If the point-by-point map is good, the calculated and measured plots for each ring should line up closely.

# how many low-angle rings to check (keep this small - the calculated g-vector array
# grows with the number of hkls)
n_rings_to_check = 2
hkls, mults = polefig.ring_hkls(ref_ucell, dsmax=2, n_rings=n_rings_to_check)
print('hkl multiplicities for the rings used:', mults)
pars = ImageD11.parameters.read_par_file(ds.parfile)

# each hkl has two solutions (a Friedel pair), hence the eta1/eta2, omega1/omega2 arrays
voxel_mask, tth, eta1, eta2, omega1, omega2 = polefig.calc_gve_angles(pmap, hkls, pars)
cf_2d = ds.get_cf_2d()
cf_2d.updateGeometry(pars)
# bin size for the eta/omega histograms below - 1 degree is a reasonable
# default to actually see something; ds.ostep (the scan's angular step) is
# another natural choice
polefig_bin_deg = 1.0
polefig.plot_ring_polefigures(ref_ucell, mults, eta1, eta2, omega1, omega2, cf_2d, bin_deg=polefig_bin_deg, ds=ds)
y0 = ds.y0  # set previously by pbp_1_indexing
dty1, dty2 = polefig.calc_dty(ds, voxel_mask, omega1, omega2, y0)
polefig.plot_ring_sinograms(ds, cf_2d, ref_ucell, mults, omega1, omega2, dty1, dty2)

Make TensorMap#

# we can choose to make a TensorMap here
# which is useful for Paraview and MTEX visualisations

phase_ids = TensorMap.recon_order_to_map_order(np.where(pmap.best_nuniq > min_unique, 0, -1))
tmap = TensorMap.from_pbpmap(pmap, steps=(1, ds.ystep, ds.ystep), phases={0: ref_ucell})
tmap['phase_ids'] = phase_ids
tmap.get_ipf_maps()
tmap.plot('nuniq')
tmap.plot('ipf_x')
tmap.plot('ipf_y')
tmap.plot('ipf_z')
utils.plot_all_ipfs_from_meta_orien(tmap.get_meta_orix_orien(), tmap.phases[0])