Calibration of a 2 theta arm with a Pilatus 100k detector

The aim of this document is to explain how to use pyFAI.goniometer for calibrating the position of the detector from the goniometer encoders.

Those data have been acquired at ROBL (ESRF-BM20 German CRG) in winter 2017 by Christoph Henning using a Pilatus 100k detector and LaB6 as calibrant. One hundred and twenty one images have been acquired with the detector moving between 5 and 65 degree with a step size of half a degree. The motor position is registered in the filename.

A prior manual calibration (using pyFAI-calib) has been performed on four images locate at 31.5, 33.5, 35 and 35.5 degrees. Those images were the first with two rings. The control points extrated during this initial calibration has been used as a starting point for this calibration. Then more images have been added to make the model more robust.


In [1]:
# Initialization of the plotting library to be used with the jupyter notebook

%pylab nbagg


Populating the interactive namespace from numpy and matplotlib

In [2]:
#Loading of a few libraries

import time
start_time = time.time()
import os
import glob
import random
import fabio
import pyFAI
from pyFAI.goniometer import GeometryTransformation, GoniometerRefinement, Goniometer
from pyFAI.gui import jupyter

In [3]:
#loading of the list of files, and display of the last one with its headers

image_files = glob.glob("*.cbf")
image_files.sort()
print("List of images: " + ", ".join(image_files) + "." + os.linesep)
fimg = fabio.open(image_files[-1])

print("Image headers:")
for key, value in  fimg.header.items():
    print("%s: %s"%(key,value))
    
jupyter.display(fimg.data, label=fimg.filename)


List of images: del_05.0_0001p.cbf, del_05.5_0001p.cbf, del_06.0_0001p.cbf, del_06.5_0001p.cbf, del_07.0_0001p.cbf, del_07.5_0001p.cbf, del_08.0_0001p.cbf, del_08.5_0001p.cbf, del_09.0_0001p.cbf, del_09.5_0001p.cbf, del_10.0_0001p.cbf, del_10.5_0001p.cbf, del_11.0_0001p.cbf, del_11.5_0001p.cbf, del_12.0_0001p.cbf, del_12.5_0001p.cbf, del_13.0_0001p.cbf, del_13.5_0001p.cbf, del_14.0_0001p.cbf, del_14.5_0001p.cbf, del_15.0_0001p.cbf, del_15.5_0001p.cbf, del_16.0_0001p.cbf, del_16.5_0001p.cbf, del_17.0_0001p.cbf, del_17.5_0001p.cbf, del_18.0_0001p.cbf, del_18.5_0001p.cbf, del_19.0_0001p.cbf, del_19.5_0001p.cbf, del_20.0_0001p.cbf, del_20.5_0001p.cbf, del_21.0_0001p.cbf, del_21.5_0001p.cbf, del_22.0_0001p.cbf, del_22.5_0001p.cbf, del_23.0_0001p.cbf, del_23.5_0001p.cbf, del_24.0_0001p.cbf, del_24.5_0001p.cbf, del_25.0_0001p.cbf, del_25.5_0001p.cbf, del_26.0_0001p.cbf, del_26.5_0001p.cbf, del_27.0_0001p.cbf, del_27.5_0001p.cbf, del_28.0_0001p.cbf, del_28.5_0001p.cbf, del_29.0_0001p.cbf, del_29.5_0001p.cbf, del_30.0_0001p.cbf, del_30.5_0001p.cbf, del_31.0_0001p.cbf, del_31.5_0001p.cbf, del_32.0_0001p.cbf, del_32.5_0001p.cbf, del_33.0_0001p.cbf, del_33.5_0001p.cbf, del_34.0_0001p.cbf, del_34.5_0001p.cbf, del_35.0_0001p.cbf, del_35.5_0001p.cbf, del_36.0_0001p.cbf, del_36.5_0001p.cbf, del_37.0_0001p.cbf, del_37.5_0001p.cbf, del_38.0_0001p.cbf, del_38.5_0001p.cbf, del_39.0_0001p.cbf, del_39.5_0001p.cbf, del_40.0_0001p.cbf, del_40.5_0001p.cbf, del_41.0_0001p.cbf, del_41.5_0001p.cbf, del_42.0_0001p.cbf, del_42.5_0001p.cbf, del_43.0_0001p.cbf, del_43.5_0001p.cbf, del_44.0_0001p.cbf, del_44.5_0001p.cbf, del_45.0_0001p.cbf, del_45.5_0001p.cbf, del_46.0_0001p.cbf, del_46.5_0001p.cbf, del_47.0_0001p.cbf, del_47.5_0001p.cbf, del_48.0_0001p.cbf, del_48.5_0001p.cbf, del_49.0_0001p.cbf, del_49.5_0001p.cbf, del_50.0_0001p.cbf, del_50.5_0001p.cbf, del_51.0_0001p.cbf, del_51.5_0001p.cbf, del_52.0_0001p.cbf, del_52.5_0001p.cbf, del_53.0_0001p.cbf, del_53.5_0001p.cbf, del_54.0_0001p.cbf, del_54.5_0001p.cbf, del_55.0_0001p.cbf, del_55.5_0001p.cbf, del_56.0_0001p.cbf, del_56.5_0001p.cbf, del_57.0_0001p.cbf, del_57.5_0001p.cbf, del_58.0_0001p.cbf, del_58.5_0001p.cbf, del_59.0_0001p.cbf, del_59.5_0001p.cbf, del_60.0_0001p.cbf, del_60.5_0001p.cbf, del_61.0_0001p.cbf, del_61.5_0001p.cbf, del_62.0_0001p.cbf, del_62.5_0001p.cbf, del_63.0_0001p.cbf, del_63.5_0001p.cbf, del_64.0_0001p.cbf, del_64.5_0001p.cbf, del_65.0_0001p.cbf.

Image headers:
_array_data.header_convention: PILATUS_1.2
_array_data.header_contents: # Detector: PILATUS 2M, 24-0111
# 2016-05-17T18:12:57.232113
# Pixel_size 172e-6 m x 172e-6 m
# Silicon sensor, thickness 0.000450 m
# Exposure_time 9.9977 s
# Exposure_period 9.9977 s
# Tau = 1.991e-07 s
# Count_cutoff 1071182 counts
# Threshold_setting: 4024.0 eV
# Gain_setting: low gain (vrf = -0.300)
# N_excluded_pixels = 1
# Excluded_pixels: badpix_mask.tif
# Flat_field: FF_p2m0111_E26000_T13000_vrf_m0p30.tif
# Trim_file: p2m0111_E26000_T13000_vrf_m0p30.bin
# Image_path: /ramdisk/
# Wavelength 0.7749 A
# Start_angle 0.00 deg.
# Angle_increment 0.00 deg.
# Omega 0.00 deg.
# Omega_increment 0.00 deg.
# Phi 0.00 deg.
# Phi_increment 0.00 deg.
# Kappa 0.0 deg.
# Oscillation_axis PHI
# Detector_distance 0.157 m
# Detector_Voffset 0.0 m
# Beam_xy (542.41, 732.4) pixels
# Flux 0 counts
# Temperature 0.00 K
# Blower 0.0 C
# Lakeshore 0.00 K
Content-Type: application/octet-stream;
conversions: x-CBF_BYTE_OFFSET
Content-Transfer-Encoding: BINARY
X-Binary-Size: 95035
X-Binary-ID: 1
X-Binary-Element-Type: signed 32-bit integer
X-Binary-Element-Byte-Order: LITTLE_ENDIAN
Content-MD5: darooj/7FEQRNdNzNDo0XQ==
X-Binary-Number-of-Elements: 94965
X-Binary-Size-Fastest-Dimension: 487
X-Binary-Size-Second-Dimension: 195
X-Binary-Size-Padding: 4095
Out[3]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f9c8f870d30>

In [4]:
#Definition of the goniometer translation function:
# The detector rotates vertically, around the horizontal axis, i.e. rot2

goniotrans = GeometryTransformation(param_names = ["dist", "poni1", "poni2", "rot1",
                                                   "rot2_offset", "rot2_scale"],
                                    dist_expr="dist", 
                                    poni1_expr="poni1",
                                    poni2_expr="poni2", 
                                    rot1_expr="rot1", 
                                    rot2_expr="rot2_scale * pos + rot2_offset", 
                                    rot3_expr="0.0")


#Definition of the function reading the goniometer angle from the filename of the image.

def get_angle(basename):
    """Takes the basename (like del_65.0_0001p ) and returns the angle of the detector"""
    return float(basename.split("_")[1])

print('filename', fimg.filename, "angle:",get_angle(fimg.filename))


filename del_65.0_0001p.cbf angle: 65.0

In [5]:
#Definition of the detector, its mask, the calibrant

mask1 = fabio.open("deviation-mask.edf").data
mask2 = fabio.open("minimum-mask.edf").data
mask = numpy.logical_or(mask1, mask2)
pilatus = pyFAI.detector_factory("Pilatus100k")
pilatus.mask = mask
    
LaB6 = pyFAI.calibrant.CALIBRANT_FACTORY("LaB6")
wavelength = 7.7490120575e-11
LaB6.wavelength = wavelength

In [6]:
#Definition of the geometry refinement: the parameter order is the same as the param_names

param = {"dist":0.8, 
         "poni1":0.02, 
         "poni2":0.04, 
         "rot1":0,
         "rot2_offset":0,
         "rot2_scale": numpy.pi/180. # rot2 is in radians, while the motor position is in degrees
        }
#Defines the bounds for some variables
bounds = {"dist": (0.79, 0.81), 
          "rot1": (-0.01, 0.01),
          "rot2_offset": (-0.01, 0.01),
          "rot2_scale": (numpy.pi/180., numpy.pi/180.) #strict bounds on the scale: we expect the gonio to be precise
         }
gonioref = GoniometerRefinement(param, #initial guess
                                bounds=bounds,
                                pos_function=get_angle,
                                trans_function=goniotrans,
                                detector=pilatus, wavelength=wavelength)
print("Empty refinement object:")
print(gonioref)


Empty refinement object:
GoniometerRefinement with 0 geometries labeled: .

In [7]:
#Let's populate the goniometer refinement object with all control point files:

ponis = glob.glob("*.poni")
ponis.sort()
for fn in ponis:
    base = os.path.splitext(fn)[0]
    fimg = fabio.open(base + ".cbf")
    sg =gonioref.new_geometry(base, image=fimg.data, metadata=base, control_points=base+".npt",
                              geometry=fn, calibrant=LaB6)
    print(base, "Angle:", sg.get_position())
    print(sg.geometry_refinement)
    print()
    

print("Filled refinement object:")
print(gonioref)


del_31.5_0001p Angle: 31.5
Detector Pilatus 100k	 PixelSize= 1.720e-04, 1.720e-04 m
Wavelength= 7.749012e-11m
SampleDetDist= 8.058209e-01m	PONI= 5.700310e-03, 4.599931e-02m	rot1=-0.000000  rot2= 0.560707  rot3= -0.000065 rad
DirectBeamDist= 951.518mm	Center: x=267.438, y=2975.017 pix	Tilt=32.126 deg  tiltPlanRotation= 90.000 deg

del_33.5_0001p Angle: 33.5
Detector Pilatus 100k	 PixelSize= 1.720e-04, 1.720e-04 m
Wavelength= 7.749012e-11m
SampleDetDist= 8.061475e-01m	PONI= 1.998418e-02, 4.623309e-02m	rot1=0.000061  rot2= 0.577898  rot3= -0.000000 rad
DirectBeamDist= 962.435mm	Center: x=268.511, y=3172.837 pix	Tilt=33.111 deg  tiltPlanRotation= 90.005 deg

del_35.0_0001p Angle: 35.0
Detector Pilatus 100k	 PixelSize= 1.720e-04, 1.720e-04 m
Wavelength= 7.749012e-11m
SampleDetDist= 8.053432e-01m	PONI= 5.551240e-03, 4.624546e-02m	rot1=-0.000016  rot2= 0.622009  rot3= 0.000012 rad
DirectBeamDist= 990.936mm	Center: x=268.944, y=3389.181 pix	Tilt=35.638 deg  tiltPlanRotation= 89.999 deg

del_35.5_0001p Angle: 35.5
Detector Pilatus 100k	 PixelSize= 1.720e-04, 1.720e-04 m
Wavelength= 7.749012e-11m
SampleDetDist= 8.061272e-01m	PONI= 5.998339e-03, 3.450065e-02m	rot1=-0.014779  rot2= 0.630136  rot3= 0.000007 rad
DirectBeamDist= 997.856mm	Center: x=269.857, y=3453.432 pix	Tilt=36.113 deg  tiltPlanRotation= 88.839 deg

Filled refinement object:
GoniometerRefinement with 4 geometries labeled: del_31.5_0001p, del_33.5_0001p, del_35.0_0001p, del_35.5_0001p.

In [8]:
#Display all images with associated calibration:

for sg in gonioref.single_geometries.values():
    jupyter.display(sg=sg)



In [9]:
# Initial refinement of the goniometer model with 5 dof

gonioref.refine2()


Cost function before refinement: 4.67628027948e-05
     fun: 3.4745458894672223e-09
     jac: array([  1.55797940e-08,   6.20443484e-08,  -8.86836424e-09,
         9.38199124e-09,   4.54174193e-08,   5.30002587e-06,
         0.00000000e+00])
 message: 'Optimization terminated successfully.'
    nfev: 145
     nit: 18
    njev: 18
  status: 0
 success: True
       x: array([ 0.80587963,  0.01626972,  0.04374852, -0.00299843, -0.00217847,
        0.01745329])
Cost function after refinement: 3.47454588947e-09
GonioParam(dist=0.80587963248790362, poni1=0.016269720058867374, poni2=0.043748524559145206, rot1=-0.0029984303217802364, rot2_offset=-0.002178469880426713, rot2_scale=0.017453292519943295)
maxdelta on: dist (0) 0.8 --> 0.805879632488
Out[9]:
array([ 0.80587963,  0.01626972,  0.04374852, -0.00299843, -0.00217847,
        0.01745329])

In [10]:
# This function adds new images to the pool of data used for the refinement.
# A set of new control points are extractred and a refinement step is performed at each iteration
# The last image of the serie is displayed 

def optimize_with_new_images(list_images, pts_per_deg=1):
    sg = None
    for fname in list_images:
        print()
        base = os.path.splitext(fname)[0]
        fimg = fabio.open(fname)
        if base in gonioref.single_geometries:
            continue
        print(base)
        sg = gonioref.new_geometry(base, image=fimg.data, metadata=base,
                                   calibrant=LaB6)
        print(sg.extract_cp(pts_per_deg=pts_per_deg))
    print("*"*50)
    gonioref.refine2()
    if sg: 
        sg.geometry_refinement.set_param(gonioref.get_ai(sg.get_position()).param)
        jupyter.display(sg=sg)

In [11]:
# Append all other images bewteen 30 and 40 degrees

images_30_40 = glob.glob("del_3?.?_0001p.cbf")
random.shuffle(images_30_40)
optimize_with_new_images(images_30_40, pts_per_deg=3)


del_37.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# c ring 10: 30 points

del_30.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# d ring 6: 36 points

del_38.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# e ring 10: 30 points


del_30.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# f ring 6: 36 points

del_38.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
# g ring 10: 30 points
# h ring 11: 30 points

del_33.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# i ring 7: 36 points

del_36.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# j ring 9: 36 points

del_39.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# k ring 11: 30 points

del_36.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# l ring 9: 36 points


del_32.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# m ring 7: 36 points

del_39.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
# n ring 10: 15 points
# o ring 11: 30 points

del_37.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
# p ring 9: 36 points
# q ring 10: 30 points

del_34.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# r ring 8: 36 points


del_34.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# s ring 8: 36 points

del_32.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# t ring 7: 36 points


del_31.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
# u ring 6: 36 points
**************************************************
Cost function before refinement: 3.92127316022e-09
     fun: 3.9197831247330188e-09
     jac: array([ -4.02475211e-08,   4.62919295e-08,  -3.15459889e-07,
         2.54695533e-07,   3.38884847e-08,  -4.44073933e-05,
         0.00000000e+00])
 message: 'Optimization terminated successfully.'
    nfev: 17
     nit: 2
    njev: 2
  status: 0
 success: True
       x: array([ 0.80587964,  0.01627031,  0.04374859, -0.00299848, -0.00217799,
        0.01745329])
Cost function after refinement: 3.91978312473e-09
GonioParam(dist=0.80587963966178366, poni1=0.01627031402756671, poni2=0.043748592032658032, rot1=-0.0029984847917287919, rot2_offset=-0.0021779904577083099, rot2_scale=0.017453292519943295)
maxdelta on: poni1 (1) 0.0162697200589 --> 0.0162703140276

In [12]:
# Append all other images

all_images = glob.glob("del_*_0001p.cbf")
random.shuffle(all_images)
optimize_with_new_images(all_images)



del_64.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
# v ring 26: 8 points
# w ring 27: 8 points

del_40.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
# x ring 11: 10 points
# y ring 12: 8 points

del_45.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
# z ring 14: 10 points
#aa ring 15: 1 points

del_20.0_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_27.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ab ring 5: 14 points

del_41.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ac ring 12: 10 points

del_47.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#ad ring 15: 10 points
#ae ring 16: 9 points

del_51.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#af ring 18: 8 points
#ag ring 19: 8 points

del_52.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ah ring 19: 8 points

del_23.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ai ring 4: 16 points

del_09.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#aj ring 0: 14 points

del_63.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ak ring 26: 8 points

del_41.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#al ring 12: 10 points

del_14.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#am ring 1: 10 points

del_43.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#an ring 13: 10 points



del_06.0_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_62.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ao ring 25: 8 points

del_07.5_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_25.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ap ring 5: 14 points

del_29.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#aq ring 6: 7 points

del_11.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ar ring 0: 29 points

del_58.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#as ring 22: 8 points
#at ring 23: 8 points

del_21.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#au ring 3: 17 points

del_46.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#av ring 14: 7 points
#aw ring 15: 10 points


del_12.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ax ring 0: 11 points

del_57.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#ay ring 22: 8 points
#az ring 23: 8 points

del_28.0_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_44.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ba ring 13: 10 points

del_40.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#bb ring 11: 10 points
#bc ring 12: 10 points

del_08.0_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_11.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#bd ring 0: 34 points

del_18.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#be ring 2: 19 points

del_59.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#bf ring 24: 8 points

del_17.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#bg ring 2: 10 points

del_62.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#bh ring 25: 8 points

del_16.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#bi ring 1: 24 points

del_50.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#bj ring 17: 9 points
#bk ring 18: 7 points


del_20.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#bl ring 3: 17 points

del_48.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#bm ring 16: 9 points
#bn ring 17: 9 points

del_18.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#bo ring 2: 19 points

del_26.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#bp ring 5: 14 points

del_56.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#bq ring 21: 8 points
#br ring 22: 8 points


del_42.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#bs ring 12: 10 points

del_05.5_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_45.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#bt ring 13: 10 points
#bu ring 14: 10 points

del_50.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#bv ring 17: 9 points
#bw ring 18: 8 points




del_09.0_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_52.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#bx ring 19: 8 points



del_07.0_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_49.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#by ring 17: 8 points
#bz ring 18: 3 points

del_53.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ca ring 20: 6 points

del_49.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#cb ring 16: 9 points
#cc ring 17: 9 points

del_54.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#cd ring 20: 5 points
#ce ring 21: 3 points

del_26.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#cf ring 5: 14 points

del_47.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#cg ring 15: 10 points
#ch ring 16: 9 points




del_59.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ci ring 23: 8 points

del_08.5_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_13.5_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_10.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#cj ring 0: 28 points

del_25.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ck ring 4: 16 points

del_48.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#cl ring 16: 9 points

del_19.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#cm ring 2: 19 points

del_56.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#cn ring 21: 8 points
#co ring 22: 7 points


del_54.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#cp ring 20: 8 points

del_63.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#cq ring 26: 8 points

del_60.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#cr ring 24: 8 points
#cs ring 25: 8 points

del_23.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ct ring 4: 11 points

del_28.5_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_19.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#cu ring 2: 19 points

del_55.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#cv ring 20: 8 points
#cw ring 21: 8 points

del_13.0_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:



del_14.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#cx ring 1: 24 points

del_16.5_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_06.5_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_58.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#cy ring 23: 8 points

del_57.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#cz ring 22: 8 points
#da ring 23: 8 points

del_15.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#db ring 1: 24 points

del_61.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#dc ring 24: 8 points
#dd ring 25: 8 points

del_51.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#de ring 18: 8 points
#df ring 19: 8 points


del_46.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#dg ring 14: 10 points
#dh ring 15: 10 points

del_05.0_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_12.5_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_22.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#di ring 3: 17 points

del_24.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#dj ring 4: 16 points

del_27.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#dk ring 5: 14 points

del_61.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#dl ring 24: 8 points
#dm ring 25: 8 points

del_10.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#dn ring 0: 33 points



del_29.0_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_60.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#do ring 24: 8 points

del_44.5_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#dp ring 13: 10 points
#dq ring 14: 10 points

del_55.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#dr ring 20: 8 points
#ds ring 21: 8 points

del_24.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#dt ring 4: 16 points

del_64.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#du ring 26: 8 points
#dv ring 27: 8 points

del_21.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#dw ring 3: 17 points

del_17.0_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:

del_65.0_0001p
ControlPoints instance containing 2 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 2 groups of points:
#dx ring 27: 8 points
#dy ring 28: 8 points

del_15.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#dz ring 1: 24 points

del_43.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ea ring 13: 10 points

del_53.0_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#eb ring 19: 8 points

del_22.5_0001p
ControlPoints instance containing 1 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 1 groups of points:
#ec ring 3: 17 points

del_42.5_0001p
ControlPoints instance containing 0 group of point:
LaB6 Calibrant with 91 reflections at wavelength 7.7490120575e-11
Containing 0 groups of points:
**************************************************
Cost function before refinement: 1.54482357991e-08
     fun: 1.2717773557396512e-08
     jac: array([ -8.67558480e-08,   9.00979583e-07,  -1.61419460e-08,
         7.72584907e-09,   7.28791957e-07,  -1.02714441e-03,
         0.00000000e+00])
 message: 'Optimization terminated successfully.'
    nfev: 49
     nit: 6
    njev: 6
  status: 0
 success: True
       x: array([ 0.80588466,  0.016274  ,  0.04327334, -0.00261441, -0.00217479,
        0.01745329])
Cost function after refinement: 1.27177735574e-08
GonioParam(dist=0.80588465517075347, poni1=0.016274004318260964, poni2=0.043273335318004272, rot1=-0.0026144144555218839, rot2_offset=-0.0021747867021798891, rot2_scale=0.017453292519943295)
maxdelta on: poni2 (2) 0.0437485920327 --> 0.043273335318
/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py:545: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots.
  warnings.warn("No labelled objects found. "

In [13]:
# Check the calibration of the first and the last image with rings

print("First & last rings")

print("Total number of images:", len(gonioref.single_geometries) )

fig = plt.figure()
for idx,lbl in enumerate(["del_10.0_0001p", "del_65.0_0001p"]):
    sg = gonioref.single_geometries[lbl]
    if sg.control_points.get_labels():
        sg.geometry_refinement.set_param(gonioref.get_ai(sg.get_position()).param)
    jupyter.display(sg=sg, ax=fig.add_subplot(2, 1, idx+1))


First & last rings
Total number of images: 121

In [14]:
# Final pass of refinement with all constrains removed, very fine refinement

gonioref.bounds = None
gonioref.refine2("slsqp", eps=1e-13, maxiter=10000, ftol=1e-12)


Cost function before refinement: 1.27177735574e-08
     fun: 1.1111654794284465e-08
     jac: array([  4.57430879e-08,  -7.22955855e-09,   5.86520685e-07,
        -4.77200495e-07,  -1.00916035e-09,   6.38583433e-09,
         0.00000000e+00])
 message: 'Optimization terminated successfully.'
    nfev: 36
     nit: 4
    njev: 4
  status: 0
 success: True
       x: array([ 0.8058848 ,  0.01622186,  0.04327261, -0.00261381, -0.00221681,
        0.01745628])
Cost function after refinement: 1.11116547943e-08
GonioParam(dist=0.80588479781690947, poni1=0.016221860052011303, poni2=0.043272606410526104, rot1=-0.0026138134046104502, rot2_offset=-0.0022168095667989851, rot2_scale=0.017456283337989313)
maxdelta on: poni1 (1) 0.0162740043183 --> 0.016221860052
Out[14]:
array([ 0.8058848 ,  0.01622186,  0.04327261, -0.00261381, -0.00221681,
        0.01745628])

In [15]:
#Create a MultiGeometry integrator from the refined geometry:

angles = []
images = []
for sg in gonioref.single_geometries.values():
    angles.append(sg.get_position())
    images.append(sg.image)
    
multigeo = gonioref.get_mg(angles)
multigeo.radial_range=(3, 68)
print(multigeo)


MultiGeometry integrator with 121 geometries on (3, 68) radial range (2th_deg) and (-180, 180) azimuthal range (deg)

In [16]:
# Integrate the whole set of images in a single run:

res = multigeo.integrate1d(images, 10000)
fig, ax = subplots()
ax.plot(*res)
ax.set_xlabel(res.unit.label)
ax.set_ylabel("Intensity")


Out[16]:
<matplotlib.text.Text at 0x7f9c8ad24588>

In [17]:
# Save the goniometer configuration with 1 angle

gonioref.save("ROBL_v1.json")

In [18]:
#Can the refinement be improved by freeing another degree of freedom ? what about rot1 ?

goniotrans2 = GeometryTransformation(param_names = ["dist", "poni1", "poni2", 
                                                    "rot1", "rot1_scale",
                                                    "rot2_offset", "rot2_scale"],
                                     dist_expr="dist", 
                                     poni1_expr="poni1",
                                     poni2_expr="poni2", 
                                     rot1_expr="rot1_scale * pos + rot1", 
                                     rot2_expr="rot2_scale * pos + rot2_offset", 
                                     rot3_expr="0.0")

param2 = (gonioref.nt_param(*gonioref.param))._asdict()
param2["rot1_scale"] = 0

gonioref2 = GoniometerRefinement(param2, 
                                 pos_function = get_angle,
                                 trans_function=goniotrans2,
                                 detector=pilatus,
                                 wavelength=wavelength)
gonioref2.single_geometries = gonioref.single_geometries.copy()

print(gonioref2.chi2(), gonioref.chi2())
gonioref2.refine2()
gonioref2.save("ROBL_v2.json")


1.11116547943e-08 1.11116547943e-08
Cost function before refinement: 1.11116547943e-08
     fun: 4.3839371423944213e-09
     jac: array([  5.25061268e-08,  -1.27521146e-07,  -7.37897327e-09,
         7.19318610e-09,   1.76603933e-08,  -1.05703885e-07,
        -4.25410142e-06,   0.00000000e+00])
 message: 'Optimization terminated successfully.'
    nfev: 121
     nit: 13
    njev: 13
  status: 0
 success: True
       x: array([  8.05864487e-01,   1.62074080e-02,   4.16536438e-02,
        -1.30537565e-03,  -1.34815359e-04,  -2.22811078e-03,
         1.74566788e-02])
Cost function after refinement: 4.38393714239e-09
GonioParam(dist=0.80586448711989489, poni1=0.016207407990379279, poni2=0.041653643845572928, rot1=-0.0013053756542407884, rot1_scale=-0.00013481535944469204, rot2_offset=-0.0022281107840006532, rot2_scale=0.01745667877925083)
maxdelta on: poni2 (2) 0.0432726064105 --> 0.0416536438456

In [19]:
# Check the calibration of the first and the last image with rings

print("First & last rings")

fig = plt.figure()
for idx,lbl in enumerate(["del_10.0_0001p", "del_65.0_0001p"]):
    sg = gonioref.single_geometries[lbl]
    if sg.control_points.get_labels():
        sg.geometry_refinement.set_param(gonioref2.get_ai(sg.get_position()).param)
    jupyter.display(sg=sg, ax=fig.add_subplot(2, 1, idx+1))


First & last rings

In [20]:
#Create a MultiGeometry integrator from the refined geometry and display the integrated image:

multigeo2 = gonioref2.get_mg(angles)
multigeo2.radial_range=(3, 68)
print(multigeo2)

res2 = multigeo2.integrate1d(images, 10000)

#Display the 2 curves with a zoom
fig = figure(figsize=(10,5))
ax = fig.add_subplot(1,2,1)
ax.plot(*res2, label="rot1 & rot2 rotation")
ax.plot(*res, label="rot2 only rotation")
ax.set_xlabel(res.unit.label)
ax.set_ylabel("Intensity")
ax.set_title("Azimuthal integration of 121 images merged")
ax.legend()
ay = fig.add_subplot(1,2,2)
ay.plot(*res2, label="rot1 & rot2 rotation")
ay.plot(*res, label="rot2 only rotation")
ay.set_xlabel(res.unit.label)
ay.set_ylabel("Intensity")
ay.set_xlim(10.5,11)
ay.set_title("Zoom on first peak")
ay.legend()


MultiGeometry integrator with 121 geometries on (3, 68) radial range (2th_deg) and (-180, 180) azimuthal range (deg)
Out[20]:
<matplotlib.legend.Legend at 0x7f9c8a892a90>

In [21]:
print("Total execution time: %.3fs"%(time.time() - start_time))


Total execution time: 16.379s

With the first model, the refinement was not perfect on the very low angles and indicates a miss-fit. Relaxing the constrains on rot1 allowed to spot a non (perfect) orthogonality between the goniometer axis and the incident beam. Releasing the distances is also possible, for example to cope with the sample not perfectly mounted on the center of the goniometer.

Conclusion

This notebook exposes the how to calibrate the goniometer for a small detector moving on a 2theta arm. Once calibrated, the geometry can be saved and restored and stays valid as long as the detector or the goniometer is not unmounted from the beam-line. This configuration can subsequently be used to integrate the data acquired with any sample, in minutes instead of hours. The resolution limit is now the pixel size. Fortunately, pixel detector with small pixel like the MaxiPix or the Lambda detector exists and offer higher resolution.