The following code analyzes a set of dark images taken with the AGIPD detector to deduce detector offsets , noise, bad-pixel maps and thresholding. All four types of constants are evaluated per-pixel and per-memory cell. Data for the detector's three gain stages needs to be present, separated into separate runs.
The evaluated calibration constants are stored locally and injected in the calibration data base.
%% Cell type:code id: tags:
``` python
in_folder="/gpfs/exfel/d/raw/CALLAB/202031/p900113"# path to input data, required
out_folder="/gpfs/exfel/data/scratch/hammerd/agipd-fixed-gain"# path to output to, required
sequences=[0]# sequence files to evaluate.
modules=[-1]# list of modules to evaluate, RANGE ALLOWED
run_high=9985# run number in which high gain data was recorded, required
run_med=9984# run number in which medium gain data was recorded, required
run_low=9983# run number in which low gain data was recorded, required
operation_mode="ADAPTIVE_GAIN"# Detector operation mode, optional (defaults to "ADAPTIVE_GAIN")
karabo_id="HED_DET_AGIPD500K2G"# karabo karabo_id
karabo_da=['-1']# a list of data aggregators names, Default [-1] for selecting all data aggregators
receiver_id="{}CH0"# inset for receiver devices
path_template='RAW-R{:04d}-{}-S{:05d}.h5'# the template to use to access data
h5path='/INSTRUMENT/{}/DET/{}:xtdf/image'# path in the HDF5 file to images
h5path_idx='/INDEX/{}/DET/{}:xtdf/image'# path in the HDF5 file to images
h5path_ctrl='/CONTROL/{}/MDL/FPGA_COMP'# path to control information
karabo_id_control="HED_EXP_AGIPD500K2G"# karabo-id for control device '
karabo_da_control="AGIPD500K2G00"# karabo DA for control infromation
use_dir_creation_date=True# use dir creation date as data production reference date
cal_db_interface="tcp://max-exfl016:8020"# the database interface to use
cal_db_timeout=3000000# timeout on caldb requests"
local_output=True# output constants locally
db_output=False# output constants to database
mem_cells=0# number of memory cells used, set to 0 to automatically infer
bias_voltage=0# detector bias voltage
gain_setting=0.1# the gain setting, use 0.1 to try to auto-determine
acq_rate=0.# the detector acquisition rate, use 0 to try to auto-determine
interlaced=False# assume interlaced data format, for data prior to Dec. 2017
rawversion=2# RAW file format version
thresholds_offset_sigma=3.# offset sigma thresholds for offset deduced bad pixels
thresholds_offset_hard=[0,0]# For setting the same threshold offset for the 3 gains. Left for backcompatability. Default [0, 0] to take the following parameters.
thresholds_offset_hard_hg=[3000,7000]# High-gain thresholds in absolute ADU terms for offset deduced bad pixels
thresholds_offset_hard_mg=[6000,10000]# Medium-gain thresholds in absolute ADU terms for offset deduced bad pixels
thresholds_offset_hard_lg=[6000,10000]# Low-gain thresholds in absolute ADU terms for offset deduced bad pixels
thresholds_offset_hard_hg_fixed=[3500,6500]# Same as thresholds_offset_hard_hg, but for fixed gain operation
thresholds_offset_hard_mg_fixed=[3500,6500]# Same as thresholds_offset_hard_mg, but for fixed gain operation
thresholds_offset_hard_lg_fixed=[3500,6500]# Same as thresholds_offset_hard_lg, but for fixed gain operation
thresholds_noise_sigma=5.# noise sigma thresholds for offset deduced bad pixels
thresholds_noise_hard=[0,0]# For setting the same threshold noise for the 3 gains. Left for backcompatability. Default [0, 0] to take the following parameters.
thresholds_noise_hard_hg=[4,20]# High-gain thresholds in absolute ADU terms for offset deduced bad pixels
thresholds_noise_hard_mg=[4,20]# Medium-gain thresholds in absolute ADU terms for offset deduced bad pixels
thresholds_noise_hard_lg=[4,20]# Low-gain thresholds in absolute ADU terms for offset deduced bad pixels
thresholds_gain_sigma=5.# Gain separation sigma threshold
high_res_badpix_3d=False# set this to True if you need high-resolution 3d bad pixel plots. ~7mins extra time for 64 memory cells
```
%% Cell type:code id: tags:
``` python
importos
fromcollectionsimportOrderedDict
fromdatetimeimportdatetime
fromtypingimportTuple
importdateutil.parser
importh5py
importmatplotlib
importnumpyasnp
importpashaaspsh
importtabulate
importyaml
matplotlib.use('agg')
importmatplotlib.pyplotasplt
fromIPython.displayimportLatex,Markdown,display
%matplotlibinline
importitertools
importmultiprocessing
fromcal_tools.agipdlibimport(
get_acq_rate,
get_bias_voltage,
get_gain_mode,
get_gain_setting,
get_num_cells,
)
fromcal_tools.enumsimportAgipdGainMode,BadPixels
fromcal_tools.plottingimport(
create_constant_overview,
plot_badpix_3d,
show_overview,
show_processed_modules,
)
fromcal_tools.toolsimport(
get_dir_creation_date,
get_from_db,
get_random_db_interface,
get_report,
map_gain_stages,
module_index_to_qm,
run_prop_seq_from_path,
save_const_to_h5,
send_to_db,
)
importiCalibrationDB
```
%% Cell type:code id: tags:
``` python
# insert control device if format string (does nothing otherwise)
The calculation is performed per-pixel and per-memory-cell. Offsets are simply the median value for a set of dark data taken at a given gain, noise the standard deviation, and gain-bit values the medians of the gain array.
The thresholds for gain switching are then defined as the mean value between in individual gain bit levels. Note that these thresholds need to be refined with charge induced thresholds, as the two are not the same.
Single cell overviews allow to identify potential effects on all memory cells, e.g. on sensor level. Additionally, they should serve as a first sanity check on expected behaviour, e.g. if structuring on the ASIC level is visible in the offsets, but otherwise no immediate artifacts are visible.
%% Cell type:markdown id: tags:
### High Gain ###
%% Cell type:code id: tags:
``` python
cell = 3
gain = 0
show_overview(res, cell, gain, infix="{}-{}-{}".format(*offset_runs.values()))
```
%% Cell type:markdown id: tags:
### Medium Gain ###
%% Cell type:code id: tags:
``` python
cell = 3
gain = 1
show_overview(res, cell, gain, infix="{}-{}-{}".format(*offset_runs.values()))
```
%% Cell type:markdown id: tags:
### Low Gain ###
%% Cell type:code id: tags:
``` python
cell = 3
gain = 2
show_overview(res, cell, gain, infix="{}-{}-{}".format(*offset_runs.values()))
The following tables show summary information for the evaluated module. Values for currently evaluated constants are compared with values for pre-existing constants retrieved from the calibration database.
%% Cell type:code id: tags:
``` python
# now we need the old constants
old_const = {}
old_mdata = {}
old_retrieval_res.wait()
for (qm, const), (data, timestamp) in zip(qm_x_const, old_retrieval_res.get()):
old_const.setdefault(qm, {})[const] = data
old_mdata.setdefault(qm, {})[const] = timestamp
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
display(Markdown("The following pre-existing constants are used for comparison:"))
for qm, consts in old_mdata.items():
display(Markdown(f"- {qm}"))
for const in consts:
display(Markdown(f" - {const} at {consts[const]}"))