MX Calibrate
Calibrate a translation table from a set of powder diffraction images taken at various sample-detector distances.
This is a notebook replacement of the MX-calibrate
tool from pyFAI with advanced features.
Start with some constant definition:
[1]:
calibrant_name = "CeO2"
detector_name = "Pilatus 2M"
file_pattern = "test-powder*.cbf"
result_file = "MX-calibrate.json"
wavelength = None # set a value to override the one in the headers
[2]:
%matplotlib widget
[3]:
import os
import glob
import logging
import numpy
from matplotlib.pyplot import subplots
import fabio
import pyFAI
from pyFAI.gui import jupyter
import pyFAI.calibrant
from pyFAI.gui.jupyter.calib import Calibration
from pyFAI.goniometer import GeometryTransformation, GoniometerRefinement
from pyFAI.gui.cli_calibration import AbstractCalibration
import pyFAI.gui.mpl_calib
pyFAI.gui.mpl_calib.logger.setLevel(logging.ERROR)
WARNING:pyFAI.gui.matplotlib:matplotlib already loaded, setting its backend may not work
[4]:
detector = pyFAI.detector_factory(detector_name)
calibrant = pyFAI.calibrant.get_calibrant(calibrant_name)
files = sorted(glob.glob(file_pattern))
print("input files: "+" ".join(files))
input files: test-powder_1_0001.cbf test-powder_2_0001.cbf test-powder_3_0001.cbf test-powder_4_0001.cbf test-powder_5_0001.cbf test-powder_6_0001.cbf test-powder_7_0001.cbf test-powder_8_0001.cbf
[5]:
first = fabio.open(files[0])
def get_dectris_headers(fimg):
"""return the dectris headers from a Pilatus detector"""
res = {}
for line in fimg.header.get("_array_data.header_contents", "").split("\n"):
words = line.split()
if len(words)>=3:
key = words[1]
for v in words[2:]:
try:
vf = float(v)
except:
continue
if not("." in v or "e" in v):
vf = int(v)
res[key] = vf
return res
get_dectris_headers(first)
[5]:
{'Silicon': 0.00045,
'Pixel_size': 0.000172,
'N_oscillations': 1,
'Chi': 0.0,
'Phi': 0.0,
'Kappa': 0.0,
'Alpha': 0.0,
'Polarization': 0.99,
'Detector_2theta': 0.0,
'Angle_increment': 1.0,
'Transmission': 100.0,
'Flux': 436215830143.2828,
'Detector_Voffset': 0.0,
'Detector_distance': 0.126474,
'Wavelength': 0.965459,
'N_excluded_pixels:': 321,
'Threshold_setting': 6421,
'Count_cutoff': 1048500,
'Tau': 0,
'Exposure_period': 0.02115,
'Exposure_time': 0.02,
'Start_angle': 0.0}
[6]:
if wavelength is None:
wavelength = get_dectris_headers(first)["Wavelength"] * 1e-10
calibrant.wavelength = wavelength
[7]:
#apply mask to the detector
mask = numpy.logical_or(detector.mask, first.data<0)
detector.mask = mask
Manual calibration of the first image
[8]:
# Important: select the ring number before right-click on the ring. Finally click on the refine button
calib = Calibration(img=first.data,
mask=mask,
detector=detector,
wavelength=wavelength,
calibrant=calibrant)
[9]:
calib.extract_cpt()
# calib.geoRef.rot1 = calib.geoRef.rot2 = calib.geoRef.rot3 = 0
calib.refine(fixed=["wavelength", "rot3"])
Before refinement, the geometry is:
Detector Pilatus 2M PixelSize= 1.720e-04, 1.720e-04 m
Wavelength= 9.654590e-11m
SampleDetDist= 1.261663e-01m PONI= 1.471352e-01, 1.212036e-01m rot1=0.006287 rot2= -0.003675 rot3= 0.000000 rad
DirectBeamDist= 126.170mm Center: x=700.060, y=852.741 pix Tilt=0.417 deg tiltPlanRotation= -149.690 deg
Detector Pilatus 2M PixelSize= 1.720e-04, 1.720e-04 m
Wavelength= 9.654590e-11m
SampleDetDist= 1.263066e-01m PONI= 1.469151e-01, 1.210761e-01m rot1=0.005276 rot2= -0.002122 rot3= 0.000000 rad
DirectBeamDist= 126.309mm Center: x=700.056, y=852.599 pix Tilt=0.326 deg tiltPlanRotation= -158.095 deg
Detector Pilatus 2M PixelSize= 1.720e-04, 1.720e-04 m
Wavelength= 9.654590e-11m
SampleDetDist= 1.263066e-01m PONI= 1.469151e-01, 1.210761e-01m rot1=0.005276 rot2= -0.002122 rot3= 0.000000 rad
DirectBeamDist= 126.309mm Center: x=700.056, y=852.599 pix Tilt=0.326 deg tiltPlanRotation= -158.095 deg
Detector Pilatus 2M PixelSize= 1.720e-04, 1.720e-04 m
Wavelength= 9.654590e-11m
SampleDetDist= 1.263066e-01m PONI= 1.469151e-01, 1.210761e-01m rot1=0.005276 rot2= -0.002122 rot3= 0.000000 rad
DirectBeamDist= 126.309mm Center: x=700.056, y=852.599 pix Tilt=0.326 deg tiltPlanRotation= -158.095 deg
Check that the beam-center and the distance is correct and how much they are off.
Calibration of all frames in automatic mode:
[10]:
# Definition of the geometry translation function:
get_distance = lambda fimg: get_dectris_headers(fimg)["Detector_distance"]
geotrans = GeometryTransformation(param_names = ["dist_offset",
"poni1", "poni2", "rot1","rot2",
"dist_scale", "poni1_scale", "poni2_scale"],
dist_expr="pos * dist_scale + dist_offset",
poni1_expr="pos * poni1_scale + poni1",
poni2_expr="pos * poni2_scale + poni2",
rot1_expr="rot1",
rot2_expr="rot2",
rot3_expr="0.0")
param = {
"dist_offset": calib.geoRef.dist-get_distance(first),
"poni1": calib.geoRef.poni1,
"poni2": calib.geoRef.poni2,
"rot1": calib.geoRef.rot1,
"rot2": calib.geoRef.rot2,
"dist_scale": 1.0,
"poni1_scale": 0.0,
"poni2_scale": 0.0,
}
print(param)
{'dist_offset': -0.00016743911038444304, 'poni1': 0.14691506424463482, 'poni2': 0.12107607213597012, 'rot1': 0.0052764302844606975, 'rot2': -0.0021215870242492665, 'dist_scale': 1.0, 'poni1_scale': 0.0, 'poni2_scale': 0.0}
[11]:
# Definition of the geometry refinement: the parameter order is the same as the param_names
gonioref = GoniometerRefinement(param, #initial guess
pos_function=get_distance,
trans_function=geotrans,
detector=detector,
wavelength=wavelength)
print("Empty refinement object:")
print(gonioref)
Empty refinement object:
GoniometerRefinement with 0 geometries labeled: .
[12]:
# Let's populate the goniometer refinement object with all geometries:
for fn in files:
base = os.path.splitext(fn)[0]
fimg = fabio.open(fn)
local_calib = AbstractCalibration(img=fimg.data, mask=mask,
detector=detector,
wavelength=wavelength,
calibrant=calibrant)
local_calib.preprocess()
local_calib.fixed = ["wavelength", "rot3"]
local_calib.ai = gonioref.get_ai(get_distance(fimg))
local_calib.extract_cpt()
sg = gonioref.new_geometry(os.path.basename(base), image=fimg.data, metadata=fimg,
control_points=local_calib.peakPicker.points,
geometry=local_calib.ai,
calibrant=calibrant)
print("Filled refinement object:")
print(gonioref)
print(os.linesep+"\tLabel \t Distance")
for k, v in gonioref.single_geometries.items():
print(k,v.get_position())
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
ERROR:root:No diffraction image available => not showing the contour
Filled refinement object:
GoniometerRefinement with 8 geometries labeled: test-powder_1_0001, test-powder_2_0001, test-powder_3_0001, test-powder_4_0001, test-powder_5_0001, test-powder_6_0001, test-powder_7_0001, test-powder_8_0001.
Label Distance
test-powder_1_0001 0.126474
test-powder_2_0001 0.141749
test-powder_3_0001 0.199249
test-powder_4_0001 0.171074
test-powder_5_0001 0.226674
test-powder_6_0001 0.293162
test-powder_7_0001 0.357899
test-powder_8_0001 0.484611
[13]:
fig, ax = subplots(len(files), figsize=(10, 10*len(files)))
for sp, sg in zip(ax, gonioref.single_geometries.values()):
jupyter.display(sg=sg, ax=sp, label=sg.label)
[14]:
gonioref.refine3(fix=["dist_scale", "poni1_scale", "poni2_scale"])
Free parameters: ['dist_offset', 'poni1', 'poni2', 'rot1', 'rot2']
Fixed: {'dist_scale': 1.0, 'poni1_scale': 0.0, 'poni2_scale': 0.0}
fun: 2.610523667293124e-07
jac: array([ 7.09961157e-09, -1.37134748e-11, -5.81689363e-10, 1.34939171e-10,
-1.94226857e-11])
message: 'Optimization terminated successfully'
nfev: 64
nit: 10
njev: 10
status: 0
success: True
x: array([-9.16210537e-05, 1.46815934e-01, 1.20862138e-01, 3.71918279e-03,
-1.46521125e-03])
Constrained Least square 5.139555719150498e-07 --> 2.610523667293124e-07
maxdelta on rot1: 0.0052764302844606975 --> 0.0037191827852943136
[14]:
2.610523667293124e-07
[15]:
gonioref.refine3(fix=[])
Free parameters: ['dist_offset', 'poni1', 'poni2', 'rot1', 'rot2', 'dist_scale', 'poni1_scale', 'poni2_scale']
Fixed: {}
fun: 1.9905995732588853e-07
jac: array([ 1.60492952e-09, 2.33169928e-09, -2.11968221e-09, -5.96764949e-09,
-1.13462825e-07, -4.41345129e-08, 1.41832727e-07, 3.02732595e-09])
message: 'Optimization terminated successfully'
nfev: 156
nit: 17
njev: 17
status: 0
success: True
x: array([-3.25212400e-04, 1.46793618e-01, 1.20788223e-01, 5.20300638e-03,
-1.71569495e-03, 1.00150026e+00, 4.72802257e-04, 2.36376405e-03])
Constrained Least square 2.610523667293124e-07 --> 1.9905995732588853e-07
maxdelta on poni2_scale: 0.0 --> 0.002363764050758662
[15]:
1.9905995732588853e-07
Interpretation of this fit:
[16]:
gonioref.get_ai(0.2)
[16]:
Detector Pilatus 2M PixelSize= 1.720e-04, 1.720e-04 m
Wavelength= 9.654590e-11m
SampleDetDist= 1.999748e-01m PONI= 1.468882e-01, 1.212610e-01m rot1=0.005203 rot2= -0.001716 rot3= 0.000000 rad
DirectBeamDist= 199.978mm Center: x=698.956, y=852.006 pix Tilt=0.314 deg tiltPlanRotation= -161.750 deg
[17]:
gonioref.get_ai(0.3)
[17]:
Detector Pilatus 2M PixelSize= 1.720e-04, 1.720e-04 m
Wavelength= 9.654590e-11m
SampleDetDist= 3.001249e-01m PONI= 1.469355e-01, 1.214974e-01m rot1=0.005203 rot2= -0.001716 rot3= 0.000000 rad
DirectBeamDist= 300.129mm Center: x=697.301, y=851.282 pix Tilt=0.314 deg tiltPlanRotation= -161.750 deg
Persistence of this fit
[18]:
gonioref.save(result_file)
[19]:
with open(result_file) as r:
print(r.read())
{
"content": "Goniometer calibration v2",
"detector": "Pilatus 2M",
"detector_config": {},
"wavelength": 9.65459e-11,
"param": [
-0.00032521239968252326,
0.14679361782615102,
0.120788222729316,
0.005203006380756238,
-0.0017156949517419815,
1.0015002644005104,
0.0004728022565169806,
0.002363764050758662
],
"param_names": [
"dist_offset",
"poni1",
"poni2",
"rot1",
"rot2",
"dist_scale",
"poni1_scale",
"poni2_scale"
],
"pos_names": [
"pos"
],
"trans_function": {
"content": "GeometryTransformation",
"param_names": [
"dist_offset",
"poni1",
"poni2",
"rot1",
"rot2",
"dist_scale",
"poni1_scale",
"poni2_scale"
],
"pos_names": [
"pos"
],
"dist_expr": "pos * dist_scale + dist_offset",
"poni1_expr": "pos * poni1_scale + poni1",
"poni2_expr": "pos * poni2_scale + poni2",
"rot1_expr": "rot1",
"rot2_expr": "rot2",
"rot3_expr": "0.0",
"constants": {
"pi": 3.141592653589793
}
}
}
Interpretation of the fit:
[20]:
distances = []
f_distances = []
f_poni1 = []
f_poni2 = []
g_distances = []
g_poni1 = []
g_poni2 = []
for sg in gonioref.single_geometries.values():
distance = sg.get_position()
distances.append(distance)
sg.geometry_refinement.refine3(fix=["wavelength", "rot3"])
f_distances.append(sg.geometry_refinement.dist)
f_poni1.append(sg.geometry_refinement.poni1)
f_poni2.append(sg.geometry_refinement.poni2)
ai = gonioref.get_ai(distance)
g_distances.append(ai.dist)
g_poni1.append(ai.poni1)
g_poni2.append(ai.poni2)
[21]:
fig,ax = subplots(2)
ax[0].plot(distances, f_distances, label="fitted individually")
ax[0].plot(distances, g_distances, label="fitted table")
ax[0].set_title("Observed deviations:")
ax[1].set_xlabel("Encoder distance (m)")
ax[1].plot(distances, f_poni1, label="poni1 fitted individually")
ax[1].plot(distances, g_poni1, label="poni1 fitted table")
ax[1].plot(distances, f_poni2, label="poni2 fitted individually")
ax[1].plot(distances, g_poni2, label="poni2 fitted table")
ax[0].set_ylabel("Fitted distance (m)")
ax[1].set_ylabel("Fitted PONIs (m)")
ax[0].legend()
ax[1].legend()
pass
Nota:
The degradation between 0.3 and 0.5m correspond to the image 6->7 and is related to the disparition of the third ring!
Conclusion:
This notebook demonstrates: * The usage of the geometry calibration in Jupyter-lab to calibrate the first image * The creation of a goniometer-refinement * The population of this goniometer-refinement with automatic control-point extraction * The fit of the table, first with the constrains of a perfecty aligned table, then with a mis-aligned table
In our case the table is miss-aligned in the horizontal direction by 2.3mm/meter (i.e. 2.3 mradian). This should be taken into account when calculating the beam-center at different distances.