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.

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()