Calibration of the pixel position for a Pilatus detector

This tutorial summarizes the work done by Frederic Sulzman during his internship at ESRF during the summer 2015 entilted “Calibration for geometric distortion in multi- modules pixel detectors”.

The overall strategy is very similar to “CCD calibration” tutorial with some specificities due to the modular nature of the detector.

  1. Image preprocessing

  2. Peak picking

  3. Grid assignment

  4. Displacement fitting

  5. Reconstruction of the pixel position

  6. Saving into a detector definition file

  7. Validation of the geometry with a 2D integration

Each module being made by lithographic processes, the error within a module will be assumeed to be constant. We will use the name “displacement of the module” to describe the rigide movement of the module.

This tutorial uses data from the Pilatus3 2M CdTe from the ID15 beam line of the ESRF. They provided not only the intersnip subject but also the couple of images uses to calibrate the detector.

This detector contains 48 half-modules, each bound to a single CdTe monocrystal sensor and is designed for high energy X-ray radiation detection. Due to the construction procedure, these half-modules could show a misalignment within the detector plane. While the manufacturer (Dectris) garanties a precision within a pixel (172µm), the miss-alignment of certain modules can be seen while calibrating Debye-Scherrer ring using refereance sample. So the aim of this work is to provide a detector description with a better precision better than the original detector.

This work will be performed on the image of a grid available: http://www.silx.org/pub/pyFAI/detector_calibration/Pilatus2MCdTe_ID15_grid_plus_sample_0004.cbf and the scattering of ceria (CeO2) at 72.1keV available here. http://www.silx.org/pub/pyFAI/detector_calibration/Pilatus2MCdTe_ID15_CeO2_72100eV_800mm_0000.cbf

It is a good exercise to calibrate all rings of the later image using the pyFAI-calib2 tool. A calibration close to perfection is needed to visualize the module miss-alignement we aim at correcting.

[1]:
%matplotlib inline
#matplotlib notebook
#many imports which will be used all along the notebook
import time
start_time = time.perf_counter()
import os
import pyFAI
import fabio
import glob
import numpy
from numpy.lib.stride_tricks import as_strided
from math import sin, cos, sqrt
from scipy.ndimage import convolve, binary_dilation
from scipy.spatial import distance_matrix
from scipy.optimize import minimize
from matplotlib.pyplot import subplots
from pyFAI.ext.bilinear import Bilinear
from pyFAI.ext.watershed import InverseWatershed
from silx.resources import ExternalResources
from pyFAI.azimuthalIntegrator import AzimuthalIntegrator

# A couple of compound dtypes ...
dt = numpy.dtype([('y', numpy.float64),
                  ('x', numpy.float64),
                  ('i', numpy.int64),
                 ])
dl = numpy.dtype([('y', numpy.float64),
                  ('x', numpy.float64),
                  ('i', numpy.int64),
                  ('Y', numpy.int64),
                  ('X', numpy.int64),
                 ])

print("Using pyFAI verison: ", pyFAI.version)
Using pyFAI verison:  0.20.0-beta1
[2]:
#Nota: Configure here your proxy if you are behind a firewall
#os.environ["http_proxy"] = "http://proxy.comany.com:3128"
[3]:
downloader = ExternalResources("detector_calibration", "http://www.silx.org/pub/pyFAI/detector_calibration/")
ring_file = downloader.getfile("Pilatus2MCdTe_ID15_CeO2_72100eV_800mm_0000.cbf")
print(ring_file)
grid_file = downloader.getfile("Pilatus2MCdTe_ID15_grid_plus_sample_0004.cbf")
print(grid_file)
/tmp/detector_calibration_testdata_jerome/Pilatus2MCdTe_ID15_CeO2_72100eV_800mm_0000.cbf
/tmp/detector_calibration_testdata_jerome/Pilatus2MCdTe_ID15_grid_plus_sample_0004.cbf
[4]:
rings = fabio.open(ring_file).data
img = fabio.open(grid_file).data
fig,ax = subplots(1,2, figsize=(10,5))
ax[0].imshow(img.clip(0,1000), interpolation="bilinear")
ax[0].set_title("grid")
ax[1].imshow(numpy.arcsinh(rings), interpolation="bilinear")
ax[1].set_title("rings")
pass
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_4_0.png

Image processing

There are 3 pre-processing steps which are needed.

  1. Define for each module a unique identifier which will be used later on during the fitting procedure

  2. Define the proper mask: each module is the assembly of 4x2 sub-modules and there are (3) interpolated pixels between each sub-module, such “unreliable pixels should be masked out as well

  3. Correct the grid image by the smoothed image to have a constant background.

  4. Convolve the raw image with a typical hole shape to allow a precise spotting of the hole center.

[5]:
# This is the default detector as definied in pyFAI according to the specification provided by Dectris:
pilatus = pyFAI.detector_factory("Pilatus_2m_CdTe")
print(pilatus)

mask1 = pilatus.mask
module_size = pilatus.MODULE_SIZE
module_gap = pilatus.MODULE_GAP
submodule_size = (96,60)
Detector Pilatus CdTe 2M         PixelSize= 1.720e-04, 1.720e-04 m
[6]:
#1 + 2 Calculation of the module_id and the interpolated-mask:
mid = numpy.zeros(pilatus.shape, dtype=int)
mask2 = numpy.zeros(pilatus.shape, dtype=int)
idx = 1
for i in range(8):
    y_start = i*(module_gap[0] + module_size[0])
    y_stop = y_start + module_size[0]
    for j in range(3):
        x_start = j*(module_gap[1] + module_size[1])
        x_stop = x_start + module_size[1]
        mid[y_start:y_stop,x_start: x_start+module_size[1]//2] = idx
        idx+=1
        mid[y_start:y_stop,x_start+module_size[1]//2: x_stop] = idx
        idx+=1
        mask2[y_start+submodule_size[0]-1:y_start+submodule_size[0]+2,
              x_start:x_stop] = 1
        for k in range(1,8):
            mask2[y_start:y_stop,
              x_start+k*(submodule_size[1]+1)-1:x_start+k*(submodule_size[1]+1)+2] = 1
[7]:
#Extra masking
mask0 = img<0
#Those pixel are miss-behaving... they are the hot pixels next to the beam-stop
mask0[915:922,793:800] = 1
mask0[817:820,747:750] = 1
[8]:
fig,ax = subplots(1,3, figsize=(10,4))
ax[0].imshow(mid, interpolation="bilinear")
ax[0].set_title("Module Id")

ax[1].imshow(mask2+mask1+mask0, interpolation="bilinear")
ax[1].set_title("Combined mask")

nimg = img.astype(float)
nimg[numpy.where(mask0+mask1+mask2)] = numpy.nan


ax[2].imshow(nimg)#, interpolation="bilinear")
ax[2].set_title("Nan masked image")
pass
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_9_0.png
[9]:
# The Nan-masked image contains now only valid values (and Nan elsewhere). We will make a large median filter to
# build up a smooth image without gaps.
#
# This function is backported from future version of numpy ... it allows to expose a winbowed view
# to perform the nanmedian-filter

def sliding_window_view(x, shape, subok=False, readonly=True):
    """
    Creates sliding window views of the N dimensional array with the given window
    shape. Window slides across each dimension of `x` and extract subsets of `x`
    at any window position.
    Parameters
    ----------
    x : array_like
        Array to create sliding window views of.
    shape : sequence of int
        The shape of the window. Must have same length as the number of input array dimensions.
    subok : bool, optional
        If True, then sub-classes will be passed-through, otherwise the returned
        array will be forced to be a base-class array (default).
    readonly : bool, optional
        If set to True, the returned array will always be readonly view.
        Otherwise it will return writable copies(see Notes).
    Returns
    -------
    view : ndarray
        Sliding window views (or copies) of `x`. view.shape = x.shape - shape + 1
    See also
    --------
    as_strided: Create a view into the array with the given shape and strides.
    broadcast_to: broadcast an array to a given shape.
    Notes
    -----
    ``sliding_window_view`` create sliding window views of the N dimensions array
    with the given window shape and its implementation based on ``as_strided``.
    Please note that if readonly set to True, views are returned, not copies
    of array. In this case, write operations could be unpredictable, so the returned
    views are readonly. Bear in mind that returned copies (readonly=False) will
    take more memory than the original array, due to overlapping windows.
    For some cases there may be more efficient approaches to calculate transformations
    across multi-dimensional arrays, for instance `scipy.signal.fftconvolve`, where combining
    the iterating step with the calculation itself while storing partial results can result
    in significant speedups.
    Examples
    --------
    >>> i, j = np.ogrid[:3,:4]
    >>> x = 10*i + j
    >>> shape = (2,2)
    >>> np.lib.stride_tricks.sliding_window_view(x, shape)
    array([[[[ 0,  1],
             [10, 11]],
            [[ 1,  2],
             [11, 12]],
            [[ 2,  3],
             [12, 13]]],
           [[[10, 11],
             [20, 21]],
            [[11, 12],
             [21, 22]],
            [[12, 13],
             [22, 23]]]])
    """
    np = numpy
    # first convert input to array, possibly keeping subclass
    x = np.array(x, copy=False, subok=subok)

    try:
        shape = np.array(shape, np.int)
    except:
        raise TypeError('`shape` must be a sequence of integer')
    else:
        if shape.ndim > 1:
            raise ValueError('`shape` must be one-dimensional sequence of integer')
        if len(x.shape) != len(shape):
            raise ValueError("`shape` length doesn't match with input array dimensions")
        if np.any(shape <= 0):
            raise ValueError('`shape` cannot contain non-positive value')

    o = np.array(x.shape) - shape  + 1 # output shape
    if np.any(o <= 0):
        raise ValueError('window shape cannot larger than input array shape')

    if type(readonly) != bool:
        raise TypeError('readonly must be a boolean')

    strides = x.strides
    view_strides = strides

    view_shape = np.concatenate((o, shape), axis=0)
    view_strides = np.concatenate((view_strides, strides), axis=0)
    view = as_strided(x, view_shape, view_strides, subok=subok, writeable=not readonly)

    if not readonly:
        return view.copy()
    else:
        return view
[10]:
%%time
#Calculate a background image using a large median filter ... takes a while
shape = (19,11)
print(nimg.shape)
padded = numpy.pad(nimg, tuple((i//2,) for i in shape), mode="edge")
print(padded.shape)
background = numpy.nanmedian(sliding_window_view(padded,shape), axis = (-2,-1))
print(background.shape)
fig,ax = subplots()
ax.imshow(background)
ax.set_title("Background image")
pass
(1679, 1475)
(1697, 1485)
(1679, 1475)
CPU times: user 25.8 s, sys: 7.5 s, total: 33.4 s
Wall time: 33.5 s
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_11_1.png
[11]:
fig,ax = subplots(1,2, figsize=(9,5))

normalized = (nimg/background)

low = numpy.nanmin(normalized)
high = numpy.nanmax(normalized)
print(low, high)
normalized[numpy.isnan(normalized)] = 0
normalized /= high

ax[0].imshow(normalized)
ax[0].set_title("Normalized image")

ax[1].hist(normalized.ravel(), 100, range=(0,1))
ax[1].set_title("Histogram of intensities in normalized image")
pass
0.0 17.728813559322035
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_12_1.png

For a precise measurement of the peak position, one trick is to convolve the image with a pattern which looks like a hole of the grid.

[12]:
#print the profile of the normalized image: the center is difficult to measure due to the small size of the hole.
fig,ax = subplots(2)
ax[0].plot(normalized[:,545])
ax[1].plot(normalized[536,:])
pass
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_14_0.png
[13]:
#Definition of the convolution kernel
ksize = 5
y,x = numpy.ogrid[-(ksize-1)//2:ksize//2+1,-(ksize-1)//2:ksize//2+1]
d = numpy.sqrt(y*y+x*x)

#Fade out curve definition
fadeout = lambda x: 1/(1+numpy.exp(5*(x-2.5)))

kernel = fadeout(d)
mini=kernel.sum()
print(mini)

fig,ax = subplots(1,3)
ax[0].imshow(d)
ax[0].set_title("Distance array")

ax[1].plot(numpy.linspace(0,5,100),fadeout(numpy.linspace(0,5,100)))
ax[1].set_title("fade-out curve")

ax[2].imshow(kernel)
ax[2].set_title("Convolution kernel")
pass
19.63857792789662
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_15_1.png
[14]:
my_smooth = convolve(normalized, kernel, mode="constant", cval=0)/mini
print(my_smooth.shape)
fig,ax = subplots(1,2)
ax[0].imshow(normalized.clip(0,1))
ax[0].set_ylim(1050,1100)
ax[0].set_xlim(300,350)
ax[1].imshow(my_smooth.clip(0,1))
ax[1].set_ylim(1050,1100)
ax[1].set_xlim(300,350)
numpy.where(my_smooth == my_smooth.max())
(1679, 1475)
[14]:
(array([1065]), array([338]))
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_16_2.png
[15]:
#mask out all pixels too close to any masked position

all_masks = numpy.logical_or(numpy.logical_or(mask0,mask1),mask2)
print(all_masks.sum())
big_mask = binary_dilation(all_masks, iterations=ksize//2+1+1)
print(big_mask.sum())
smooth2 = my_smooth.copy()
smooth2[big_mask] = 0
fig,ax = subplots()
ax.imshow(smooth2)
pass
335009
782371
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_17_1.png
[16]:
#Display the profile of the smoothed image: the center is easy to measure thanks to the smoothness of the signal
fig,ax = subplots(2)
ax[0].plot(my_smooth[:,545])
ax[1].plot(my_smooth[536,:])
pass
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_18_0.png

Peak picking

We use the watershed module from pyFAI to retrieve all peak positions. Those regions are sieved out respectively for:

  • their size, it should be larger than the kernel itself

  • the peaks too close to masked regions are removed

  • the intensity of the peak

[17]:
iw = InverseWatershed(my_smooth)
iw.init()
iw.merge_singleton()
all_regions = set(iw.regions.values())

regions = [i for i in all_regions if i.size>mini]

print("Number of region segmented: %s"%len(all_regions))
print("Number of large enough regions : %s"%len(regions))
Number of region segmented: 82126
Number of large enough regions : 41333
[18]:
#Remove peaks on masked region
sieved_region = [i for i in regions if not big_mask[(i.index//nimg.shape[-1], i.index%nimg.shape[-1])]]
print("Number of peaks not on masked areea : %s"%len(sieved_region))
Number of peaks not on masked areea : 30001
[19]:
# Histogram of peak height:
s = numpy.array([i.maxi for i in sieved_region])

fig, ax = subplots()
ax.hist(s, 100)
pass
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_22_0.png
[20]:
#sieve-out for peak intensity
int_mini = 0.1
peaks = [(i.index//nimg.shape[-1], i.index%nimg.shape[-1]) for i in sieved_region if (i.maxi)>int_mini]
print("Number of remaining peaks with I>%s: %s"%(int_mini, len(peaks)))

peaks_raw = numpy.array(peaks)
Number of remaining peaks with I>0.1: 2075
[21]:
# Finally the peak positions are interpolated using a second order taylor expansion
# in thevinicy of the maximum value of the signal:

#Create bilinear interpolator
bl = Bilinear(my_smooth)

#Overlay raw peak coordinate and refined peak positions

ref_peaks = [bl.local_maxi(p) for p in peaks]
fig, ax = subplots()
ax.imshow(img.clip(0,1000), interpolation="nearest")
peaks_ref = numpy.array(ref_peaks)
ax.plot(peaks_raw[:,1], peaks_raw[:, 0], ".r")
ax.plot(peaks_ref[:,1],peaks_ref[:, 0], ".b")
ax.set_title("Extracted peak position (red: raw, blue: refined)")
print("Refined peak coordinate:")
print(ref_peaks[:10])
Refined peak coordinate:
[(66.386809527874, 545.4018792510033), (66.49157708883286, 516.0233984515071), (66.12943549454212, 398.44530776143074), (66.27097326517105, 457.24162001907825), (66.12287057191133, 133.89461011439562), (66.10124520212412, 163.3917499780655), (65.9386735856533, 251.47241121530533), (66.10113758593798, 192.66102123260498), (66.11095763742924, 222.20659750699997), (66.03663577884436, 339.52876299619675)]
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_24_1.png

At this stage we have about 2000 peaks (with sub-pixel precision) which are visually distributed on all modules. Some modules have their peaks located along sub-module boundaries which are masked out, hence they have fewer ontrol point for the calculation. Let’s assign each peak to a module identifier. This allows to print out the number of peaks per module:

[22]:
yxi = numpy.array([i+(mid[round(i[0]),round(i[1])],)
                   for i in ref_peaks], dtype=dt)
print("Number of keypoint per module:")
for i in range(1,mid.max()+1):
    print("Module id:",i, "cp:", (yxi[:]["i"] == i).sum())
Number of keypoint per module:
Module id: 1 cp: 48
Module id: 2 cp: 30
Module id: 3 cp: 48
Module id: 4 cp: 46
Module id: 5 cp: 42
Module id: 6 cp: 47
Module id: 7 cp: 47
Module id: 8 cp: 30
Module id: 9 cp: 48
Module id: 10 cp: 48
Module id: 11 cp: 41
Module id: 12 cp: 39
Module id: 13 cp: 48
Module id: 14 cp: 30
Module id: 15 cp: 47
Module id: 16 cp: 48
Module id: 17 cp: 42
Module id: 18 cp: 48
Module id: 19 cp: 47
Module id: 20 cp: 30
Module id: 21 cp: 48
Module id: 22 cp: 47
Module id: 23 cp: 42
Module id: 24 cp: 47
Module id: 25 cp: 48
Module id: 26 cp: 30
Module id: 27 cp: 48
Module id: 28 cp: 47
Module id: 29 cp: 41
Module id: 30 cp: 50
Module id: 31 cp: 46
Module id: 32 cp: 30
Module id: 33 cp: 48
Module id: 34 cp: 42
Module id: 35 cp: 47
Module id: 36 cp: 48
Module id: 37 cp: 48
Module id: 38 cp: 28
Module id: 39 cp: 48
Module id: 40 cp: 42
Module id: 41 cp: 44
Module id: 42 cp: 48
Module id: 43 cp: 47
Module id: 44 cp: 26
Module id: 45 cp: 46
Module id: 46 cp: 42
Module id: 47 cp: 45
Module id: 48 cp: 48

Grid assignment

The calibration is performed using a regular grid, the idea is to assign to each peak of coordinates (x,y) the integer value (X, Y) which correspond to the grid corrdinate system.

The first step is to measure the grid pitch which correspond to the distance (in pixels) from one peak to the next. This is easily obtained from a pair-wise distribution function.

[23]:
# pairwise distance calculation using scipy.spatial.distance_matrix

dist = distance_matrix(peaks_ref, peaks_ref)

fig, ax = subplots()
ax.hist(dist.ravel(), 100, range=(0,100))
ax.set_title("Pair-wise distribution function")
pass
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_28_0.png

The histogram of the pair-distribution function has a first peak at 0 and the second peak between 29 and 30. Let’s start the fit with this value

Two other parameters correspond to the offset, in pixel for the grid index (X,Y) = (0,0). The easiest is to measure the smallest x and y for the first module.

[24]:
#from pair-wise distribution histogram
step = 29
#work with the first module and fit the peak positions
first = yxi[yxi[:]["i"] == 1]
y_min = first[:]["y"].min()
x_min = first[:]["x"].min()
print("offset for the first peak: ", x_min, y_min)
offset for the first peak:  16.269793540239334 7.186804354190826

The grid looks very well aligned with the axes which makes this step easier but nothing garanties it is perfect, so the rotation of the grid has to be measured as well.

The default rotation will be zero and will be fitted later on.

Once the indexes X,Y determined for eack peak, one can fit the parameter to properly align the grid with the first module. Those 4 parameters are step-size, x_min, y_min and angle

[25]:
#Assign each peak to an index
indexed1 = numpy.zeros(len(first), dtype=dl)

for i,v in enumerate(first):
    Y = int(round((v["y"]-y_min)/step))
    X = int(round((v["x"]-x_min)/step))
    indexed1[i]["y"] = v["y"]
    indexed1[i]["x"] = v["x"]
    indexed1[i]["i"] = v["i"]
    indexed1[i]["Y"] = Y
    indexed1[i]["X"] = X
    print("peak id: %s %20s Y:%d (Δ=%.3f) X:%s (Δ=%.3f)"%
          (i,v, Y, (v["y"]-Y*step-y_min)/step, X, (v["x"]-X*step-x_min)/step))

peak id: 0 (66.12287057, 133.89461011, 1) Y:2 (Δ=0.032) X:4 (Δ=0.056)
peak id: 1 (66.1012452, 163.39174998, 1) Y:2 (Δ=0.032) X:5 (Δ=0.073)
peak id: 2 (66.10113759, 192.66102123, 1) Y:2 (Δ=0.032) X:6 (Δ=0.082)
peak id: 3 (66.11095764, 222.20659751, 1) Y:2 (Δ=0.032) X:7 (Δ=0.101)
peak id: 4 (65.89957739, 104.38019937, 1) Y:2 (Δ=0.025) X:3 (Δ=0.038)
peak id: 5 (66.03506869, 75.15414216, 1) Y:2 (Δ=0.029) X:2 (Δ=0.030)
peak id: 6 (65.94483702, 45.61336857, 1) Y:2 (Δ=0.026) X:1 (Δ=0.012)
peak id: 7 (65.94893547, 16.45467511, 1) Y:2 (Δ=0.026) X:0 (Δ=0.006)
peak id: 8 (183.49989897, 16.39247844, 1) Y:6 (Δ=0.080) X:0 (Δ=0.004)
peak id: 9 (183.50560439, 45.46551961, 1) Y:6 (Δ=0.080) X:1 (Δ=0.007)
peak id: 10 (183.60742423, 75.08415242, 1) Y:6 (Δ=0.083) X:2 (Δ=0.028)
peak id: 11 (183.51583004, 104.62201929, 1) Y:6 (Δ=0.080) X:3 (Δ=0.047)
peak id: 12 (183.66220078, 133.73585817, 1) Y:6 (Δ=0.085) X:4 (Δ=0.051)
peak id: 13 (183.65553248, 163.30831155, 1) Y:6 (Δ=0.085) X:5 (Δ=0.070)
peak id: 14 (183.60811904, 192.46339762, 1) Y:6 (Δ=0.083) X:6 (Δ=0.076)
peak id: 15 (183.75545964, 222.06266521, 1) Y:6 (Δ=0.089) X:7 (Δ=0.096)
peak id: 16 (36.49696946, 16.50531429, 1) Y:1 (Δ=0.011) X:0 (Δ=0.008)
peak id: 17 (36.54493174, 45.66864574, 1) Y:1 (Δ=0.012) X:1 (Δ=0.014)
peak id: 18 (36.57917181, 75.22501931, 1) Y:1 (Δ=0.014) X:2 (Δ=0.033)
peak id: 19 (36.48521471, 104.38776881, 1) Y:1 (Δ=0.010) X:3 (Δ=0.039)
peak id: 20 (36.5958997, 133.89308376, 1) Y:1 (Δ=0.014) X:4 (Δ=0.056)
peak id: 21 (36.56109262, 163.54238695, 1) Y:1 (Δ=0.013) X:5 (Δ=0.078)
peak id: 22 (36.62988734, 192.63348889, 1) Y:1 (Δ=0.015) X:6 (Δ=0.082)
peak id: 23 (36.66130301, 222.22433692, 1) Y:1 (Δ=0.016) X:7 (Δ=0.102)
peak id: 24 (7.18680435, 16.41273034, 1) Y:0 (Δ=0.000) X:0 (Δ=0.005)
peak id: 25 (7.23123039, 45.74501115, 1) Y:0 (Δ=0.002) X:1 (Δ=0.016)
peak id: 26 (7.29059145, 75.19019249, 1) Y:0 (Δ=0.004) X:2 (Δ=0.032)
peak id: 27 (7.19876912, 104.56766725, 1) Y:0 (Δ=0.000) X:3 (Δ=0.045)
peak id: 28 (7.28363913, 134.01557775, 1) Y:0 (Δ=0.003) X:4 (Δ=0.060)
peak id: 29 (7.25490889, 163.38282073, 1) Y:0 (Δ=0.002) X:5 (Δ=0.073)
peak id: 30 (7.36717248, 192.76562056, 1) Y:0 (Δ=0.006) X:6 (Δ=0.086)
peak id: 31 (7.37469381, 222.16684446, 1) Y:0 (Δ=0.006) X:7 (Δ=0.100)
peak id: 32 (154.21079713, 45.57556251, 1) Y:5 (Δ=0.070) X:1 (Δ=0.011)
peak id: 33 (154.28871083, 75.04928852, 1) Y:5 (Δ=0.072) X:2 (Δ=0.027)
peak id: 34 (154.21604921, 104.4366582, 1) Y:5 (Δ=0.070) X:3 (Δ=0.040)
peak id: 35 (154.37423909, 133.8660319, 1) Y:5 (Δ=0.075) X:4 (Δ=0.055)
peak id: 36 (154.35255641, 163.24378996, 1) Y:5 (Δ=0.075) X:5 (Δ=0.068)
peak id: 37 (154.32582822, 192.64246738, 1) Y:5 (Δ=0.074) X:6 (Δ=0.082)
peak id: 38 (154.46003968, 222.04596291, 1) Y:5 (Δ=0.078) X:7 (Δ=0.096)
peak id: 39 (154.25518942, 16.26979354, 1) Y:5 (Δ=0.071) X:0 (Δ=0.000)
peak id: 40 (124.85729225, 163.3060154, 1) Y:4 (Δ=0.058) X:5 (Δ=0.070)
peak id: 41 (124.75223885, 104.64793801, 1) Y:4 (Δ=0.054) X:3 (Δ=0.048)
peak id: 42 (124.84565204, 133.79748473, 1) Y:4 (Δ=0.057) X:4 (Δ=0.053)
peak id: 43 (124.96431521, 222.11254385, 1) Y:4 (Δ=0.061) X:7 (Δ=0.098)
peak id: 44 (124.74490786, 45.56257215, 1) Y:4 (Δ=0.054) X:1 (Δ=0.010)
peak id: 45 (124.69245848, 16.47663069, 1) Y:4 (Δ=0.052) X:0 (Δ=0.007)
peak id: 46 (124.81674041, 75.13362026, 1) Y:4 (Δ=0.056) X:2 (Δ=0.030)
peak id: 47 (124.89991033, 192.57832247, 1) Y:4 (Δ=0.059) X:6 (Δ=0.080)

The error in positionning each of the pixel is less than 0.1 pixel which is already excellent and will allow a straight forward fit.

The cost function for the first module is calculated as the sum of distances squared in pixel space. It uses 4 parameters which are step-size, x_min, y_min and angle

[26]:
#Calculate the center of every single module for rotation around this center.
centers = {i: numpy.array([[numpy.where(mid == i)[1].mean()], [numpy.where(mid == i)[0].mean()]]) for i in range(1, 49)}
for k,v in centers.items():
    print(k,v.ravel())
1 [121.  97.]
2 [364.5  97. ]
3 [615.  97.]
4 [858.5  97. ]
5 [1109.   97.]
6 [1352.5   97. ]
7 [121. 309.]
8 [364.5 309. ]
9 [615. 309.]
10 [858.5 309. ]
11 [1109.  309.]
12 [1352.5  309. ]
13 [121. 521.]
14 [364.5 521. ]
15 [615. 521.]
16 [858.5 521. ]
17 [1109.  521.]
18 [1352.5  521. ]
19 [121. 733.]
20 [364.5 733. ]
21 [615. 733.]
22 [858.5 733. ]
23 [1109.  733.]
24 [1352.5  733. ]
25 [121. 945.]
26 [364.5 945. ]
27 [615. 945.]
28 [858.5 945. ]
29 [1109.  945.]
30 [1352.5  945. ]
31 [ 121. 1157.]
32 [ 364.5 1157. ]
33 [ 615. 1157.]
34 [ 858.5 1157. ]
35 [1109. 1157.]
36 [1352.5 1157. ]
37 [ 121. 1369.]
38 [ 364.5 1369. ]
39 [ 615. 1369.]
40 [ 858.5 1369. ]
41 [1109. 1369.]
42 [1352.5 1369. ]
43 [ 121. 1581.]
44 [ 364.5 1581. ]
45 [ 615. 1581.]
46 [ 858.5 1581. ]
47 [1109. 1581.]
48 [1352.5 1581. ]
[27]:
# Define a rotation of a module around the center of the module ...

def rotate(angle, xy, module):
    "Perform the rotation of the xy points around the center of the given module"
    rot = [[cos(angle),-sin(angle)],
           [sin(angle), cos(angle)]]
    center = centers[module]
    return numpy.dot(rot, xy - center) + center
[28]:
guess1 = [step, y_min, x_min, 0]

def cost1(param):
    """contains: step, y_min, x_min, angle for the first module
    returns the sum of distance squared in pixel space
    """
    step = param[0]
    y_min = param[1]
    x_min = param[2]
    angle = param[3]
    XY = numpy.vstack((indexed1["X"], indexed1["Y"]))
#     rot = [[cos(angle),-sin(angle)],
#            [sin(angle), cos(angle)]]
    xy_min = [[x_min], [y_min]]
    xy_guess = rotate(angle, step * XY + xy_min, module=1)
    delta = xy_guess - numpy.vstack((indexed1["x"], indexed1["y"]))
    return (delta*delta).sum()
[29]:
print("Before optimization", guess1, "cost=", cost1(guess1))
res1 = minimize(cost1, guess1, method = "slsqp")
print(res1)
print("After optimization", res1.x, "cost=", cost1(res1.x))
print("Average displacement (pixels): ",sqrt(cost1(res1.x)/len(indexed1)))
Before optimization [29, 7.186804354190826, 16.269793540239334, 0] cost= 250.36710826038234
     fun: 0.6337541947713751
     jac: array([-1.85221434e-05, -1.28895044e-06, -5.28246164e-06,  8.85806978e-04])
 message: 'Optimization terminated successfully'
    nfev: 54
     nit: 8
    njev: 8
  status: 0
 success: True
       x: array([2.93980816e+01, 7.24220064e+00, 1.63128833e+01, 8.76927134e-04])
After optimization [2.93980816e+01 7.24220064e+00 1.63128833e+01 8.76927134e-04] cost= 0.6337541947713751
Average displacement (pixels):  0.11490523221799047

At this step, the grid is perfectly aligned with the first module. This module is used as the reference one and all other are aligned along it, using this first fit:

[30]:
#retrieve the result of the first module fit:
step, y_min, x_min, angle = res1.x
indexed = numpy.zeros(yxi.shape, dtype=dl)

# rot =  [[cos(angle),-sin(angle)],
#         [sin(angle), cos(angle)]]
# irot =  [[cos(angle), sin(angle)],
#          [-sin(angle), cos(angle)]]

print("cost1: ",cost1([step, y_min, x_min, angle]), "for:", step, y_min, x_min, angle)

xy_min = numpy.array([[x_min], [y_min]])
xy = numpy.vstack((yxi["x"], yxi["y"]))
indexed["y"] = yxi["y"]
indexed["x"] = yxi["x"]
indexed["i"] = yxi["i"]
XY_app = (rotate(-angle, xy, 1)-xy_min) / step
XY_int = numpy.round((XY_app)).astype("int")
indexed["X"] = XY_int[0]
indexed["Y"] = XY_int[1]
xy_guess = rotate(angle, step * XY_int + xy_min, 1)

thres = 1.2
delta = abs(xy_guess - xy)
print((delta>thres).sum(), "suspicious peaks:")
suspicious = indexed[numpy.where(abs(delta>thres))[1]]
print(suspicious)
cost1:  0.6337541947713751 for: 29.398081608234577 7.242200641927896 16.312883307835875 0.0008769271337167889
6 suspicious peaks:
[(1654.4202804 , 954.44135761, 46, 56, 32)
 (1624.68617365, 807.47743243, 46, 55, 27)
 (1448.66381988, 895.86545633, 40, 49, 30)
 (1448.84506877, 954.56920397, 40, 49, 32)
 (1595.5792897 , 954.49445057, 46, 54, 32)
 (1448.30209509, 748.8784325 , 40, 49, 25)]
[31]:
fig,ax = subplots()
ax.imshow(img.clip(0,1000))
ax.plot(indexed["x"], indexed["y"],".g")
ax.plot(suspicious["x"], suspicious["y"],".r")
pass
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_40_0.png

Only 6 peaks have an initial displacement of more than 1.2 pixel, all located in modules 40 and 46. The visual inspection confirms their localization is valid.

There are 48 (half-)modules which have each of them 2 translations and one rotation. In addition to the step size, this represents 145 degrees of freedom for the fit. The first module is used to align the grid, all other modules are then aligned along this grid.

[32]:
def submodule_cost(param, module=1):
    """contains: step, y_min_1, x_min_1, angle_1, y_min_2, x_min_2, angle_2, ...
    returns the sum of distance squared in pixel space
    """

    step = param[0]
    y_min1 = param[1]
    x_min1 = param[2]
    angle1 = param[3]

    mask = indexed["i"] == module
    substack = indexed[mask]

    XY = numpy.vstack((substack["X"], substack["Y"]))
#     rot1 = [[cos(angle1), -sin(angle1)],
#             [sin(angle1), cos(angle1)]]
    xy_min1 = numpy.array([[x_min1], [y_min1]])
    xy_guess1 = rotate(angle1, step * XY + xy_min1, module=1)
    #This is guessed spot position for module #1
    if module == 1:
        "Not much to do for module 1"
        delta = xy_guess1 - numpy.vstack((substack["x"], substack["y"]))
    else:
        "perform the correction for given module"
        y_min = param[(module-1)*3+1]
        x_min = param[(module-1)*3+2]
        angle = param[(module-1)*3+3]

#         rot = numpy.array([[cos(angle),-sin(angle)],
#                            [sin(angle), cos(angle)]])
        xy_min = numpy.array([[x_min], [y_min]])
        xy_guess = rotate(angle, xy_guess1+xy_min, module)
        delta = xy_guess - numpy.vstack((substack["x"], substack["y"]))

    return (delta*delta).sum()

guess145 = numpy.zeros(48*3+1)
guess145[:4] = res1.x
for i in range(1, 49):
    print("Cost for module #",i, submodule_cost(guess145, i))

Cost for module # 1 0.6337541947713751
Cost for module # 2 1.8140317981828615
Cost for module # 3 1.850856756734583
Cost for module # 4 4.9103260413343826
Cost for module # 5 1.7748767723960284
Cost for module # 6 7.482513569475628
Cost for module # 7 14.440097815045657
Cost for module # 8 5.219968567847135
Cost for module # 9 23.407239468598583
Cost for module # 10 5.689984147039398
Cost for module # 11 4.125574298327773
Cost for module # 12 8.410821203737589
Cost for module # 13 29.859823681959156
Cost for module # 14 12.628455185282027
Cost for module # 15 3.1377113547124984
Cost for module # 16 5.277312006280297
Cost for module # 17 8.497333751867275
Cost for module # 18 4.653152524578221
Cost for module # 19 6.007098218814478
Cost for module # 20 4.98026761437093
Cost for module # 21 5.136971083841102
Cost for module # 22 5.048724630612108
Cost for module # 23 1.437310407427827
Cost for module # 24 6.046285782737919
Cost for module # 25 38.254472519397154
Cost for module # 26 19.509799035699995
Cost for module # 27 2.4297067661757685
Cost for module # 28 5.488596235955263
Cost for module # 29 10.764111776022451
Cost for module # 30 14.042156288246352
Cost for module # 31 22.940124887731823
Cost for module # 32 18.49373757353179
Cost for module # 33 12.941764917266461
Cost for module # 34 15.371707605812944
Cost for module # 35 39.66703112065475
Cost for module # 36 37.28280524203717
Cost for module # 37 63.70071107788737
Cost for module # 38 23.549525058421253
Cost for module # 39 34.00429199188217
Cost for module # 40 43.80371653531938
Cost for module # 41 15.505218479088368
Cost for module # 42 8.048822312345614
Cost for module # 43 22.735369163785595
Cost for module # 44 10.79602481222893
Cost for module # 45 41.01968721513809
Cost for module # 46 46.25894986857199
Cost for module # 47 24.54657799525454
Cost for module # 48 34.7695934501684

On retrieves that the modules 40 and 46 have large errors. Module 37 as well.

The total cost funtion is hence the sum of all cost function for all modules:

[33]:
def total_cost(param):
    """contains: step, y_min_1, x_min_1, angle_1, ...
    returns the sum of distance squared in pixel space
    """
    return sum(submodule_cost(param, module=i) for i in range(1,49))
total_cost(guess145)
[33]:
778.3949928045985
[34]:
%%time
print("Before optimization", guess145[:10], "cost=", total_cost(guess145))
res_all = minimize(total_cost, guess145, method = "slsqp")
print(res_all)
print("After optimization", res_all.x[:10], "cost=", total_cost(res_all.x))
Before optimization [2.93980816e+01 7.24220064e+00 1.63128833e+01 8.76927134e-04
 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
 0.00000000e+00 0.00000000e+00] cost= 778.3949928045985
     fun: 27.18427227690945
     jac: array([ 7.38434267e+00,  1.04433775e-01,  9.29090977e-02, -2.18628788e+00,
       -1.20322704e-02,  3.66873741e-02,  3.77511978e-03,  1.75099373e-02,
       -1.40905380e-03, -1.39172077e-02, -1.53563023e-02, -2.13699341e-02,
       -7.65371323e-03, -1.52676105e-02, -2.17664242e-02,  6.58941269e-03,
       -1.36704445e-02, -2.19297409e-03,  9.23323631e-03,  2.08234787e-03,
        5.71966171e-04,  2.50947475e-02, -7.84206390e-03, -1.21035576e-02,
       -1.25281811e-02,  5.26618958e-03,  1.68561935e-03,  2.45761871e-03,
        9.60159302e-03,  6.89697266e-03, -2.14877129e-02,  2.20789909e-02,
        3.07564735e-02, -1.10065937e-02,  1.45642757e-02,  7.16280937e-03,
       -2.77924538e-02,  8.30364227e-03,  4.06193733e-03,  1.05745792e-02,
       -2.64840126e-02,  9.96899605e-03, -1.68983936e-02, -7.45224953e-03,
       -5.91397285e-03, -7.36603737e-02,  7.88259506e-03,  1.18780136e-03,
       -2.64663696e-02,  2.68146992e-02, -1.17321014e-02,  2.70805359e-02,
       -3.44514847e-03, -4.33850288e-03,  1.55889988e-02, -1.05094910e-03,
       -1.01351738e-03, -2.13294029e-02,  1.01890564e-02,  1.80752277e-02,
       -1.92263126e-02,  7.33065605e-03,  8.27121735e-03,  4.01649475e-02,
       -6.79230690e-03, -5.97000122e-04,  4.40406799e-03, -7.96771049e-03,
        3.85665894e-03,  7.52856731e-02, -6.90841675e-03, -4.83274460e-04,
       -1.12404823e-02,  3.97157669e-03,  6.04462624e-03, -1.28593445e-02,
        3.51078510e-02, -1.07290745e-02, -9.73157883e-02, -4.30011749e-03,
        7.84873962e-03, -3.68134975e-02,  3.48134041e-02,  4.10175323e-03,
       -5.11379242e-02, -9.69982147e-03,  9.55033302e-03,  3.90272141e-02,
        2.48241425e-03, -6.48999214e-03, -3.43353748e-02, -1.96874142e-02,
        1.90858841e-02, -2.22861767e-02, -1.33347511e-02, -1.84464455e-03,
        9.22870636e-03,  3.79419327e-03,  1.21061802e-02,  1.37050152e-02,
       -1.53048038e-02,  2.78959274e-02, -4.21481133e-02, -7.65275955e-03,
        1.05316639e-02,  2.70886421e-02, -7.07578659e-03,  1.23381615e-03,
        1.87616348e-02,  2.33650208e-04,  7.11202621e-03, -1.16748810e-02,
        1.47957802e-02, -7.09955692e-02, -6.33780956e-02, -6.93321228e-04,
        1.09355450e-02,  7.27152824e-03,  2.27301121e-02,  2.41034031e-02,
       -7.12938309e-02, -2.76708603e-03, -1.44281387e-02,  2.69603729e-02,
       -8.90994072e-03,  7.16495514e-03, -2.01234818e-02,  9.34982300e-03,
       -3.87001038e-03,  3.75895500e-02,  1.88112259e-04, -9.02938843e-03,
       -1.39913559e-02,  9.32765007e-03, -8.83913040e-03,  1.60174370e-02,
        2.93567181e-02,  2.37135887e-02, -7.35812187e-02,  2.66745090e-02,
       -2.40159035e-03,  3.21681499e-02, -1.19957924e-02,  7.13300705e-03,
       -1.18374825e-03])
 message: 'Optimization terminated successfully'
    nfev: 12930
     nit: 86
    njev: 86
  status: 0
 success: True
       x: array([ 2.94082896e+01,  7.21157229e+00,  1.62771761e+01,  8.41282815e-04,
       -1.52438544e-01, -1.98525966e-01,  3.10021172e-04,  1.20046857e-01,
       -2.38782702e-01,  9.58662041e-04,  2.80278029e-01, -2.25936529e-01,
        1.97335003e-03, -8.67879742e-02, -4.65551534e-01,  3.52669991e-04,
        3.00368036e-01, -6.41400557e-01,  2.11764184e-03, -5.99494608e-01,
        7.80038023e-02,  1.37883058e-03, -4.32752619e-01, -1.31670801e-01,
        1.11819466e-03, -7.05086611e-01, -4.11537693e-01,  9.30040238e-04,
       -2.18720601e-01, -4.51694629e-01,  2.14125752e-03,  2.25584610e-01,
       -2.63520537e-01,  5.05602516e-04,  3.29059578e-01, -1.69780660e-01,
        6.83046113e-04, -6.85210299e-01, -5.65693055e-01,  1.18098003e-03,
       -5.13341544e-01, -5.77336324e-01,  1.34588860e-03, -2.94982709e-01,
       -3.29345202e-01,  1.00148297e-03, -2.68656240e-02, -5.50275593e-01,
        1.30747605e-03, -3.07113543e-01, -7.58757780e-01,  4.68208253e-04,
       -4.31128716e-02, -6.78851745e-01,  1.57173271e-03, -4.81951685e-01,
       -2.21492961e-01,  7.42923592e-04, -2.20904452e-01, -4.86480784e-01,
        7.51574374e-04, -4.26536156e-01, -4.05607045e-01, -2.65028393e-04,
       -2.46639352e-01, -5.47178826e-01,  1.66020139e-03, -1.79821203e-01,
       -2.84513028e-01,  1.27447936e-03,  1.12844301e-01, -4.06087337e-01,
        1.69038810e-03, -7.41194146e-01, -7.86147804e-01,  9.80794742e-04,
       -4.00228103e-01, -8.69271123e-01,  1.78864338e-03, -9.15429471e-02,
       -2.23471806e-01, -4.89308529e-04,  4.85837968e-02, -2.55151365e-01,
        6.30417004e-04, -1.01049714e-01, -8.42346890e-01,  5.92939725e-04,
        1.61447829e-01, -6.33904822e-01,  2.36675481e-03, -5.57394081e-01,
       -6.99077086e-01, -7.11452270e-04, -6.78338392e-01, -7.99962162e-01,
        1.47179223e-03, -1.69718703e-01, -6.79418039e-01, -6.01978324e-04,
       -3.95692752e-02, -8.02418286e-01,  1.59248966e-03, -2.06612961e-01,
       -1.28702243e+00,  6.73831701e-04, -5.44539672e-02, -1.29573647e+00,
        5.94969322e-04, -1.02721132e+00, -1.03228949e+00,  1.82147608e-06,
       -9.34189982e-01, -8.87357308e-01, -3.85612590e-06, -5.82595045e-01,
       -1.03849251e+00,  1.44126359e-04, -2.59909319e-01, -1.27033057e+00,
        2.46858682e-03, -1.76048533e-01, -9.25195677e-01,  3.75310644e-04,
       -2.17865677e-01, -8.14464293e-01,  6.49197452e-04, -8.33019076e-01,
       -6.50804886e-01,  1.02294227e-03, -6.30038298e-01, -7.53545616e-01,
        5.87542938e-04, -5.45639943e-01, -1.15996788e+00, -3.20258112e-04,
       -3.80592575e-01, -1.33888907e+00,  1.31943604e-03, -9.01175376e-02,
       -1.00327530e+00,  1.18943778e-03,  2.39389657e-01, -9.06099021e-01,
        1.87856865e-03])
After optimization [ 2.94082896e+01  7.21157229e+00  1.62771761e+01  8.41282815e-04
 -1.52438544e-01 -1.98525966e-01  3.10021172e-04  1.20046857e-01
 -2.38782702e-01  9.58662041e-04] cost= 27.18427227690945
CPU times: user 32.3 s, sys: 28.7 ms, total: 32.4 s
Wall time: 32.3 s
[35]:
for i in range(1,49):
    print("Module id: %d cost: %.3f Δx: %.3f, Δy: %.3f rot: %.3f°"%
          (i, submodule_cost(res_all.x, i), res_all.x[-2+i*3], res_all.x[-1+i*3], numpy.rad2deg(res_all.x[i*3])))
Module id: 1 cost: 0.684 Δx: 7.212, Δy: 16.277 rot: 0.048°
Module id: 2 cost: 0.473 Δx: -0.152, Δy: -0.199 rot: 0.018°
Module id: 3 cost: 0.650 Δx: 0.120, Δy: -0.239 rot: 0.055°
Module id: 4 cost: 0.613 Δx: 0.280, Δy: -0.226 rot: 0.113°
Module id: 5 cost: 0.566 Δx: -0.087, Δy: -0.466 rot: 0.020°
Module id: 6 cost: 0.666 Δx: 0.300, Δy: -0.641 rot: 0.121°
Module id: 7 cost: 0.696 Δx: -0.599, Δy: 0.078 rot: 0.079°
Module id: 8 cost: 0.341 Δx: -0.433, Δy: -0.132 rot: 0.064°
Module id: 9 cost: 0.566 Δx: -0.705, Δy: -0.412 rot: 0.053°
Module id: 10 cost: 0.661 Δx: -0.219, Δy: -0.452 rot: 0.123°
Module id: 11 cost: 0.616 Δx: 0.226, Δy: -0.264 rot: 0.029°
Module id: 12 cost: 0.495 Δx: 0.329, Δy: -0.170 rot: 0.039°
Module id: 13 cost: 0.763 Δx: -0.685, Δy: -0.566 rot: 0.068°
Module id: 14 cost: 0.353 Δx: -0.513, Δy: -0.577 rot: 0.077°
Module id: 15 cost: 0.645 Δx: -0.295, Δy: -0.329 rot: 0.057°
Module id: 16 cost: 0.492 Δx: -0.027, Δy: -0.550 rot: 0.075°
Module id: 17 cost: 0.440 Δx: -0.307, Δy: -0.759 rot: 0.027°
Module id: 18 cost: 0.779 Δx: -0.043, Δy: -0.679 rot: 0.090°
Module id: 19 cost: 0.748 Δx: -0.482, Δy: -0.221 rot: 0.043°
Module id: 20 cost: 0.423 Δx: -0.221, Δy: -0.486 rot: 0.043°
Module id: 21 cost: 0.520 Δx: -0.427, Δy: -0.406 rot: -0.015°
Module id: 22 cost: 0.676 Δx: -0.247, Δy: -0.547 rot: 0.095°
Module id: 23 cost: 0.503 Δx: -0.180, Δy: -0.285 rot: 0.073°
Module id: 24 cost: 0.822 Δx: 0.113, Δy: -0.406 rot: 0.097°
Module id: 25 cost: 0.575 Δx: -0.741, Δy: -0.786 rot: 0.056°
Module id: 26 cost: 0.320 Δx: -0.400, Δy: -0.869 rot: 0.102°
Module id: 27 cost: 0.642 Δx: -0.092, Δy: -0.223 rot: -0.028°
Module id: 28 cost: 0.599 Δx: 0.049, Δy: -0.255 rot: 0.036°
Module id: 29 cost: 0.498 Δx: -0.101, Δy: -0.842 rot: 0.034°
Module id: 30 cost: 0.830 Δx: 0.161, Δy: -0.634 rot: 0.136°
Module id: 31 cost: 0.687 Δx: -0.557, Δy: -0.699 rot: -0.041°
Module id: 32 cost: 0.422 Δx: -0.678, Δy: -0.800 rot: 0.084°
Module id: 33 cost: 0.616 Δx: -0.170, Δy: -0.679 rot: -0.034°
Module id: 34 cost: 0.393 Δx: -0.040, Δy: -0.802 rot: 0.091°
Module id: 35 cost: 0.447 Δx: -0.207, Δy: -1.287 rot: 0.039°
Module id: 36 cost: 0.587 Δx: -0.054, Δy: -1.296 rot: 0.034°
Module id: 37 cost: 0.603 Δx: -1.027, Δy: -1.032 rot: 0.000°
Module id: 38 cost: 0.295 Δx: -0.934, Δy: -0.887 rot: -0.000°
Module id: 39 cost: 0.558 Δx: -0.583, Δy: -1.038 rot: 0.008°
Module id: 40 cost: 0.498 Δx: -0.260, Δy: -1.270 rot: 0.141°
Module id: 41 cost: 0.468 Δx: -0.176, Δy: -0.925 rot: 0.022°
Module id: 42 cost: 0.569 Δx: -0.218, Δy: -0.814 rot: 0.037°
Module id: 43 cost: 0.633 Δx: -0.833, Δy: -0.651 rot: 0.059°
Module id: 44 cost: 0.413 Δx: -0.630, Δy: -0.754 rot: 0.034°
Module id: 45 cost: 0.671 Δx: -0.546, Δy: -1.160 rot: -0.018°
Module id: 46 cost: 0.542 Δx: -0.381, Δy: -1.339 rot: 0.076°
Module id: 47 cost: 0.537 Δx: -0.090, Δy: -1.003 rot: 0.068°
Module id: 48 cost: 0.593 Δx: 0.239, Δy: -0.906 rot: 0.108°

Analysis: Modules 40, 46 and 48 show large displacement but the fitting precedure allowed to reduce the residual cost to the same value as other modules.

Reconstruction of the pixel position

The pixel position can be obtained from the standard Pilatus detector. Each module is then displaced according to the fitted values, except the first one which is left where it is.

[36]:
def correct(x, y, dx, dy, angle, module):
    "apply the correction dx, dy and angle to those pixels ..."
    trans = numpy.array([[dx],
                         [dy]])
    xy_guess = numpy.vstack((x.ravel(),
                             y.ravel()))
    xy_cor = rotate(-angle, xy_guess, module) - trans
    xy_cor.shape = ((2,)+x.shape)
    return xy_cor[0], xy_cor[1]
[37]:
pixel_coord = pyFAI.detector_factory("Pilatus2MCdTe").get_pixel_corners()
pixel_coord_raw = pixel_coord.copy()
for i in range(2, 49):
    # Extract the pixel corners for one module
    module_idx = numpy.where(mid == i)
    one_module = pixel_coord_raw[module_idx]
    #retrieve the fitted values
    dy, dx, angle = res_all.x[-2+i*3:1+3*i]

    y = one_module[..., 1]/pilatus.pixel1
    x = one_module[..., 2]/pilatus.pixel2

    #apply the correction the other way around
    x_cor, y_cor = correct(x, y, dx, dy, angle, i)
    one_module[...,1] = y_cor * pilatus.pixel1 #y
    one_module[...,2] = x_cor * pilatus.pixel2 #x
    #Update the array
    pixel_coord_raw[module_idx] = one_module

Update the detector and save it in HDF5

[38]:
pilatus.set_pixel_corners(pixel_coord_raw)
pilatus.mask = all_masks
pilatus.save("Pilatus_ID15_raw.h5")
[39]:
displ = numpy.sqrt(((pixel_coord - pixel_coord_raw)**2).sum(axis=-1))
displ /= pilatus.pixel1 #convert in pixel units
fig, ax = subplots()
ax.hist(displ.ravel(), 100)
ax.set_title("Displacement of pixels versus the reference representation")
ax.set_xlabel("Error in pixel size (172µm)")
pass
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_52_0.png
[40]:
misaligned = numpy.vstack((pixel_coord_raw[..., 2].ravel(), #x
                           pixel_coord_raw[..., 1].ravel())) #y

reference = numpy.vstack((pixel_coord[..., 2].ravel(), #x
                          pixel_coord[..., 1].ravel())) #y
[41]:
#Kabsch alignment of the whole detector ...

def kabsch(P, R):
    "Align P on R"
    centroid_P = P.mean(axis=0)
    centroid_R = R.mean(axis=0)
    centered_P = P - centroid_P
    centered_R = R - centroid_R
    C = numpy.dot(centered_P.T, centered_R)
    V, S, W = numpy.linalg.svd(C)
    d = (numpy.linalg.det(V) * numpy.linalg.det(W)) < 0.0
    if d:
        S[-1] = -S[-1]
        V[:, -1] = -V[:, -1]
    # Create Rotation matrix U
    U = numpy.dot(V, W)
    P = numpy.dot(centered_P, U)
    return P + centroid_R

%time aligned = kabsch(misaligned.T, reference.T).T
CPU times: user 181 ms, sys: 36.1 ms, total: 217 ms
Wall time: 216 ms
[42]:
displ = numpy.sqrt(((aligned-reference)**2).sum(axis=0))
displ /= pilatus.pixel1 #convert in pixel units
fig, ax = subplots()
ax.hist(displ.ravel(), 100)
ax.set_title("Displacement of pixels versus the reference representation")
ax.set_xlabel("Pixel size (172µm)")
pass
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_55_0.png
[43]:
pixel_coord_aligned = pixel_coord.copy()
pixel_coord_aligned[...,1] = aligned[1,:].reshape(pixel_coord.shape[:-1])
pixel_coord_aligned[...,2] = aligned[0,:].reshape(pixel_coord.shape[:-1])

pilatus.set_pixel_corners(pixel_coord_aligned)
pilatus.mask = all_masks
pilatus.save("Pilatus_ID15_Kabsch.h5")
[44]:
fig, ax = subplots(1, 2, figsize=(8, 4))
ax[0].imshow((pixel_coord_aligned[...,2].mean(axis=-1) - pixel_coord[...,2].mean(axis=-1))/pilatus.pixel2)
ax[0].set_title("Displacement x (in pixel)")
ax[1].imshow((pixel_coord_aligned[...,1].mean(axis=-1) - pixel_coord[...,1].mean(axis=-1))/pilatus.pixel1)
ax[1].set_title("Displacement y (in pixel)")
pass
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_57_0.png

Validation of the result

To validate the improvement obtained, one can perform the experiment calibration and the 2D integration of a reference compound, the 2D integration with either the reference from Dectris or this freshly refined detector.

[45]:
# The geometry has been obtained from pyFAI
geo = { "dist":  0.8001094657585498,
        "poni1": 0.14397714477803805,
        "poni2": 0.12758748978422835,
        "rot1":  0.0011165686147339689,
        "rot2":  0.0002214091645638961,
        "rot3":  0,
        "detector": "Pilatus2MCdTe"}
ai_unc = AzimuthalIntegrator(**geo)
geo["detector"] = "Pilatus_ID15_Kabsch.h5"
ai_cor = AzimuthalIntegrator(**geo)
fig, ax = subplots(1, 2, figsize=(8,4))
method = ("pseudo", "histogram", "cython")
res_unc = ai_unc.integrate2d_ng(rings, 100, 100, radial_range=(7.9, 8.2), unit="2th_deg", method=method, mask=all_masks)
res_cor = ai_cor.integrate2d_ng(rings, 100, 100, radial_range=(7.9, 8.2), unit="2th_deg", method=method, mask=all_masks)
opts = {"origin":"lower",
        "extent": [res_unc.radial.min(), res_unc.radial.max(), -180, 180],
        "aspect":"auto",
        "cmap":"inferno"}
ax[0].imshow(res_unc[0], **opts)
ax[1].imshow(res_cor[0], **opts)
ax[0].set_xlabel(res_unc.unit.label)
ax[1].set_xlabel(res_cor.unit.label)
ax[0].set_ylabel(r"Azimuthal angle $\chi$ ($^{o}$)")
ax[0].set_title("Uncorrected")
ax[1].set_title("Corrected")
pass
../../../../_images/usage_tutorial_Detector_Pilatus_Calibration_Pilatus_ID15_59_0.png

Conclusion

This tutorial presents the way to calibrate a module based detector using the Pilatus2M CdTe from ESRF-ID15. The HDF5 file generated is directly useable by any parts of pyFAI, the reader is invited in calibrating the rings images with the default definition and with this optimized definition and check the residual error is almost divided by a factor two.

To come back on the precision of the localization of the pixel: not all the pixel are within the specifications provided by Dectris which claims the misaliment of the modules is within one pixel.

[46]:
print(f"Total execution time: {time.perf_counter()-start_time:.3f}s")
Total execution time: 83.991s