S3DXRD processing first step: dataset creation, segmentation and peak labelling#
Written by James Ball, Haixing Fang and Jon Wright
Last updated: 21/02/2025
Outside ESRF: download install_ImageD11_from_git.py, and update the path in the next cell:
Source: ImageD11/nbGui/S3DXRD/0_segment_and_label.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
# Experts : update these files for your detector if you need to
maskfile = "/data/id11/nanoscope/Eiger/eiger_mask_E-08-0144_20250819.edf"
# old papermill tests still use e2dx + e2dy
# if you give e2dxfile, this is used. Otherwise it takes the detector h5 below.
e2dxfile = None # "/data/id11/nanoscope/Eiger/e2dx_E-08-0144_20240205.edf"
e2dyfile = None # "/data/id11/nanoscope/Eiger/e2dy_E-08-0144_20240205.edf"
# For the future easier to have one detectorh5 (so you can't mix up x/y).
detectorh5 = "/data/id11/nanoscope/Eiger/spatial_20250826_LJ/newSpatial_20250819.h5"
detector = 'eiger'
omegamotor = 'rot_center'
dtymotor = 'dty'
# do we segment on the local machine or on slurm?
segment_local = False
ncores_local = None
# Default segmentation options
options = { 'cut' : 1, 'pixels_in_spot' : 3, 'howmany' : 100000 }
normalise_intensities_to_monitor = True
monitor_name = 'fpico6'
# EXPERTS: These can be provided as papermill parameters. Users, leave these as None for now...
dataroot = None
analysisroot = None
sample = None
dataset = None
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 needed packages
import pprint
import numpy as np
from matplotlib import pyplot as plt
import ImageD11.sinograms.dataset
import ImageD11.sinograms.lima_segmenter
import ImageD11.sinograms.assemble_label
import ImageD11.sinograms.properties
import ImageD11.nbGui.nb_utils as utils
from ImageD11.nbGui import segmenter_gui
%matplotlib ipympl
# Set up the file paths. Edit this if you are not at ESRF or not using the latest data policy.
if dataroot is None:
dataroot, analysisroot = segmenter_gui.guess_ESRF_paths()
if len(dataroot)==0:
print("Please fix in the dataroot and analysisroot folder names above!!")
print('dataroot =',repr(dataroot))
print('analysisroot =',repr(analysisroot))
# List the samples available:
segmenter_gui.printsamples(dataroot)
# USER: Decide which sample
if sample is None:
sample = 'FeAu_0p5_tR_nscope'
# List the datasets for that sample:
segmenter_gui.printdatasets( dataroot, sample )
# USER: Decide which dataset
if dataset is None:
dataset = "top_100um"
Example demo to see how a frame is segmented for one dataset:#
# create ImageD11 dataset object
ds = ImageD11.sinograms.dataset.DataSet(dataroot=dataroot,
analysisroot=analysisroot,
sample=sample,
dset=dataset,
detector=detector,
omegamotor=omegamotor,
dtymotor=dtymotor
)
ds.import_all() # Can use scans = [f'{scan}.1' for scan in range(1,102)] )
ds.maskfile = maskfile
if e2dxfile is not None:
ds.e2dxfile = e2dxfile
ds.e2dyfile = e2dyfile
else:
ds.detectorh5 = detectorh5
ds.save()
ui = segmenter_gui.SegmenterGui(ds, **options )
IMPORTANT NOTE
If you changed the segmentation parameters from the defaults, you MUST manually update them to match in the first cell under options = {'cut': ...
If you do not do this, Papermill (batch processing other layers) will not use your updated values!
options = ui.getopts()
Sparse file creation#
# create batch file to send to SLURM cluster
sbat = ImageD11.sinograms.lima_segmenter.setup(ds.dsfile, **ui.getopts(), pythonpath=PYTHONPATH)
if sbat is None:
raise ValueError("This scan has already been segmented!")
print(sbat)
if segment_local:
ImageD11.sinograms.lima_segmenter.sbatchlocal(sbat, cores=ncores_local)
else:
utils.slurm_submit_and_wait(sbat, 60)
Peak labeling#
# label sparse peaks
ImageD11.sinograms.assemble_label.main(ds.dsfile)
# generate peaks table
ImageD11.sinograms.properties.main(ds.dsfile, options={'algorithm': 'lmlabel', 'wtmax': 70000, 'save_overlaps': False})
Intensity normalisation#
You can optionally normalise your observed intensities to a monitor column, such as a pico, if you had one in the beam path.
fig, ax = plt.subplots(layout='constrained')
im, om_edges, dty_edges = ds.sinohist(np.log(ds.pk2d['sum_intensity']), ds.pk2d['omega'], ds.pk2d['dty'], return_edges=True)
pcm = ax.pcolormesh(om_edges, dty_edges, im.T)
ax.set(xlabel=r'$\omega~(\degree)$', ylabel='dty', title='Un-normalised sinogram of all 2D peaks')
cax = fig.colorbar(pcm, ax=ax, label='log(intensity)')
plt.show()
Let’s look at an image of the monitor signal across (dty, omega)
if normalise_intensities_to_monitor:
monitor_per_frame = ds.get_monitor(monitor_name)
fig, ax = plt.subplots(layout='constrained')
pcm = ax.pcolormesh(om_edges, dty_edges, monitor_per_frame)
ax.set(xlabel=r'$\omega~(\degree)$', ylabel='dty', title='Monitor column')
cax = fig.colorbar(pcm, ax=ax, label='Monitor')
plt.show()
To normalise to a monitor signal, we need to choose a “reference” monitor value that we scale to.
A good choice may be np.mean(monitor_per_frame)
We then compute scale_factor_per_frame = np.mean(monitor_per_frame) / monitor_per_frame
We then multiply the observed intensities by scale_factor_per_frame
You can choose which function to use to generate the “reference” monitor value, for example:
ds.set_monitor(monitor_name='fpico6', ref_value_func=np.mean)
if normalise_intensities_to_monitor:
# ensure no monitor currently set
ds.monitor = None
ds.monitor_ref = None
ds.reset_peaks_cache()
fig, ax = plt.subplots(layout='constrained')
im, om_edges, dty_edges = ds.sinohist(ds.pk2d['sum_intensity'], ds.pk2d['omega'], ds.pk2d['dty'], return_edges=True)
ax.plot(dty_edges, im.sum(axis=0), label='before scaling')
# here we set the monitor
ds.set_monitor(monitor_name, ref_value_func=np.mean)
im, om_edges, dty_edges = ds.sinohist(ds.pk2d['sum_intensity'], ds.pk2d['omega'], ds.pk2d['dty'], return_edges=True)
ax.plot(dty_edges, im.sum(axis=0), label='after scaling')
ax.legend()
ax.set(xlabel='dty', ylabel='intensity', title='Before vs after setting monitor')
plt.show()
# save the choice of monitor to disk
ds.save()
Finished segmenting!#
You can now choose between two different indexing routes: tomographic (tomo) and point-by-point (pbp).
Tomo gives you better grain shapes, but can’t handle highly deformed samples.
Point-by-point can only give you convex grain shapes (less accurate) but can handle high levels of deformation.
Both techniques will join back together during the strain refinement stage (notebook 3).
Therefore notebooks 4 and onwards should work from either the tomo or pbp route.