Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pycalibration
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
calibration
pycalibration
Commits
3c59ad73
Commit
3c59ad73
authored
2 years ago
by
Karim Ahmed
Browse files
Options
Downloads
Patches
Plain Diff
MR suggestions
parent
1a1cd3ba
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!641
[AGIPD] [CORRECT] Fix / Correcting AGIPD run with no images.
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
notebooks/AGIPD/AGIPD_Correct_and_Verify_Summary_NBC.ipynb
+1
-1
1 addition, 1 deletion
notebooks/AGIPD/AGIPD_Correct_and_Verify_Summary_NBC.ipynb
notebooks/AGIPD/AGIPD_Retrieve_Constants_Precorrection.ipynb
+2
-2
2 additions, 2 deletions
notebooks/AGIPD/AGIPD_Retrieve_Constants_Precorrection.ipynb
with
3 additions
and
3 deletions
notebooks/AGIPD/AGIPD_Correct_and_Verify_Summary_NBC.ipynb
+
1
−
1
View file @
3c59ad73
...
...
@@ -101,7 +101,7 @@
" timestamp = const_data[const]\n",
" table_data.setdefault(timestamp, []).append(f\"{seq}:{mod}\")\n",
" table = []\n",
" if len(table_data)
== 0
:\n",
" if
not
len(table_data):\n",
" table.append([\"No constants retrieved\"])\n",
" elif len(table_data) == 1:\n",
" table.append([[*table_data][0], \"All modules\"])\n",
...
...
%% Cell type:markdown id: tags:
# Summary of the AGIPD offline correction #
%% Cell type:code id: tags:
```
python
run
=
11
# runs to process, required
out_folder
=
"
/gpfs/exfel/data/scratch/ahmedk/test/AGIPD_Corr
"
# path to output to, required
karabo_id
=
"
SPB_DET_AGIPD1M-1
"
# karabo karabo_id
modules
=
[
-
1
]
karabo_da
=
[
'
-1
'
]
# a list of data aggregators names, Default [-1] for selecting all data aggregators
```
%% Cell type:code id: tags:
```
python
import
re
import
warnings
from
pathlib
import
Path
import
dateutil.parser
import
numpy
as
np
import
yaml
warnings
.
filterwarnings
(
'
ignore
'
)
import
matplotlib.pyplot
as
plt
%
matplotlib
inline
import
tabulate
from
cal_tools.tools
import
CalibrationMetadata
from
IPython.display
import
Latex
,
Markdown
,
display
```
%% Cell type:code id: tags:
```
python
out_folder
=
Path
(
out_folder
)
metadata
=
CalibrationMetadata
(
out_folder
)
const_dict
=
metadata
.
setdefault
(
"
retrieved-constants
"
,
{})
time_dict
=
const_dict
.
setdefault
(
"
time-summary
"
,
{})
# Extracting Instrument string
instrument
=
karabo_id
.
split
(
"
_
"
)[
0
]
# Evaluate detector instance for mapping
if
instrument
==
"
SPB
"
:
dinstance
=
"
AGIPD1M1
"
nmods
=
16
elif
instrument
==
"
MID
"
:
dinstance
=
"
AGIPD1M2
"
nmods
=
16
elif
instrument
==
"
HED
"
:
dinstance
=
"
AGIPD500K
"
nmods
=
8
if
karabo_da
[
0
]
==
'
-1
'
:
if
modules
[
0
]
==
-
1
:
modules
=
list
(
range
(
nmods
))
karabo_da
=
[
"
AGIPD{:02d}
"
.
format
(
i
)
for
i
in
modules
]
else
:
modules
=
[
int
(
x
[
-
2
:])
for
x
in
karabo_da
]
# This is needed only if AGIPD Correction notebook had no precorrection notebooks for retrieving constants
# gather all generated sequence yml files for time summary of retrieved constant under retrieved-constants in metadata.yml
for
fn
in
sorted
(
out_folder
.
glob
(
"
retrieved_constants_*.yml
"
)):
with
fn
.
open
(
"
r
"
)
as
fd
:
fdict
=
yaml
.
safe_load
(
fd
)
# append different sequences' time summary to the main yaml
time_dict
.
update
(
fdict
[
"
time-summary
"
])
fn
.
unlink
()
metadata
.
save
()
```
%% Cell type:code id: tags:
```
python
def
print_const_table
(
const
):
print
(
f
"
{
const
}
constants were injected on:
"
)
table_data
=
{}
for
seq
,
mod_data
in
time_dict
.
items
():
for
mod
,
const_data
in
mod_data
.
items
():
timestamp
=
const_data
[
const
]
table_data
.
setdefault
(
timestamp
,
[]).
append
(
f
"
{
seq
}
:
{
mod
}
"
)
table
=
[]
if
len
(
table_data
)
==
0
:
if
not
len
(
table_data
):
table
.
append
([
"
No constants retrieved
"
])
elif
len
(
table_data
)
==
1
:
table
.
append
([[
*
table_data
][
0
],
"
All modules
"
])
else
:
for
timestamp
,
seqmod
in
table_data
.
items
():
table
.
append
([
timestamp
,
seqmod
[
0
]])
for
further_module
in
seqmod
[
1
:]:
table
.
append
([
""
,
further_module
])
display
(
Latex
(
tabulate
.
tabulate
(
table
,
tablefmt
=
"
latex
"
,
headers
=
[
"
Timestamps
"
,
"
Modules and sequences
"
])))
for
const
in
[
'
Offset
'
,
'
SlopesPC
'
,
'
SlopesFF
'
]:
print_const_table
(
const
)
```
...
...
This diff is collapsed.
Click to expand it.
notebooks/AGIPD/AGIPD_Retrieve_Constants_Precorrection.ipynb
+
2
−
2
View file @
3c59ad73
...
...
@@ -180,7 +180,7 @@
"\n",
"instr_dc = run_dc.select(instrument_src.format(\"*\"), require_all=True)\n",
"\n",
"if
len(
instr_dc.train_ids
) == 0
:\n",
"if
not
instr_dc.train_ids:\n",
" raise ValueError(f\"No images found for {in_folder / f'r{run:04d}'}\")"
]
},
...
...
@@ -192,7 +192,7 @@
"source": [
"agipd_cond = agipdlib.AgipdCtrl(\n",
" run_dc=run_dc,\n",
" image_src=None, # Not needed, as we wont read mem_cells or acq_rate.\n",
" image_src=None, # Not needed
ed
, as we wont read mem_cells or acq_rate.\n",
" ctrl_src=ctrl_src,\n",
")\n",
"\n",
...
...
%% Cell type:markdown id: tags:
# AGIPD Retrieving Constants Pre-correction #
Author: European XFEL Detector Group, Version: 1.0
Retrieving Required Constants for Offline Calibration of the AGIPD Detector
%% Cell type:code id: tags:
```
python
in_folder
=
"
/gpfs/exfel/exp/SPB/202030/p900119/raw
"
# the folder to read data from, required
out_folder
=
"
/gpfs/exfel/data/scratch/ahmedk/test/AGIPD_
"
# the folder to output to, required
sequences
=
[
-
1
]
# sequences to correct, set to -1 for all, range allowed
modules
=
[
-
1
]
# modules to correct, set to -1 for all, range allowed
run
=
80
# runs to process, required
karabo_id
=
"
SPB_DET_AGIPD1M-1
"
# karabo karabo_id
karabo_da
=
[
'
-1
'
]
# a list of data aggregators names, Default [-1] for selecting all data aggregators
path_template
=
'
RAW-R{:04d}-{}-S{:05d}.h5
'
# the template to use to access data
ctrl_source_template
=
'
{}/MDL/FPGA_COMP_TEST
'
# path to control information
instrument_source_template
=
'
{}/DET/{}:xtdf
'
# path in the HDF5 file to images
receiver_template
=
"
{}CH0
"
# inset for receiver devices
karabo_id_control
=
"
SPB_IRU_AGIPD1M1
"
# karabo-id for control device
use_dir_creation_date
=
True
# use the creation data of the input dir for database queries
cal_db_interface
=
"
tcp://max-exfl016:8015#8045
"
# the database interface to use
creation_date_offset
=
"
00:00:00
"
# add an offset to creation date, e.g. to get different constants
slopes_ff_from_files
=
""
# Path to locally stored SlopesFF and BadPixelsFF constants
calfile
=
""
# path to calibration file. Leave empty if all data should come from DB
nodb
=
False
# if set only file-based constants will be used
mem_cells
=
0
# number of memory cells used, set to 0 to automatically infer
bias_voltage
=
0
# bias voltage, set to 0 to use stored value in slow data.
acq_rate
=
0.
# the detector acquisition rate, use 0 to try to auto-determine
gain_setting
=
-
1
# the gain setting, use -1 to use value stored in slow data.
gain_mode
=
-
1
# gain mode (0: adaptive, 1-3 fixed high/med/low, -1: read from CONTROL data)
photon_energy
=
9.2
# photon energy in keV
integration_time
=
-
1
# integration time, negative values for auto-detection.
# Correction Booleans
only_offset
=
False
# Apply only Offset correction. if False, Offset is applied by Default. if True, Offset is only applied.
rel_gain
=
False
# do relative gain correction based on PC data
xray_gain
=
True
# do relative gain correction based on xray data
blc_noise
=
False
# if set, baseline correction via noise peak location is attempted
blc_stripes
=
False
# if set, baseline corrected via stripes
blc_hmatch
=
False
# if set, base line correction via histogram matching is attempted
match_asics
=
False
# if set, inner ASIC borders are matched to the same signal level
adjust_mg_baseline
=
False
# adjust medium gain baseline to match highest high gain value
```
%% Cell type:code id: tags:
```
python
# Fill dictionaries comprising bools and arguments for correction and data analysis
# Here the hierarichy and dependencies for correction booleans are defined
corr_bools
=
{}
# offset is at the bottom of AGIPD correction pyramid.
corr_bools
[
"
only_offset
"
]
=
only_offset
# Dont apply any corrections if only_offset is requested
if
not
only_offset
:
corr_bools
[
"
adjust_mg_baseline
"
]
=
adjust_mg_baseline
corr_bools
[
"
rel_gain
"
]
=
rel_gain
corr_bools
[
"
xray_corr
"
]
=
xray_gain
corr_bools
[
"
blc_noise
"
]
=
blc_noise
corr_bools
[
"
blc_hmatch
"
]
=
blc_hmatch
```
%% Cell type:code id: tags:
```
python
from
pathlib
import
Path
from
typing
import
List
,
Tuple
import
matplotlib
import
matplotlib.pyplot
as
plt
import
multiprocessing
import
numpy
as
np
from
datetime
import
timedelta
from
dateutil
import
parser
from
extra_data
import
RunDirectory
matplotlib
.
use
(
"
agg
"
)
from
cal_tools
import
agipdlib
,
tools
from
cal_tools.enums
import
AgipdGainMode
from
iCalibrationDB
import
Conditions
,
Constants
,
Detectors
```
%% Cell type:code id: tags:
```
python
# slopes_ff_from_files left as str for now
in_folder
=
Path
(
in_folder
)
out_folder
=
Path
(
out_folder
)
metadata
=
tools
.
CalibrationMetadata
(
out_folder
)
```
%% Cell type:code id: tags:
```
python
creation_time
=
None
if
use_dir_creation_date
:
creation_time
=
tools
.
get_dir_creation_date
(
str
(
in_folder
),
run
)
offset
=
parser
.
parse
(
creation_date_offset
)
delta
=
timedelta
(
hours
=
offset
.
hour
,
minutes
=
offset
.
minute
,
seconds
=
offset
.
second
)
creation_time
+=
delta
print
(
f
"
Using
{
creation_time
}
as creation time
"
)
if
sequences
[
0
]
==
-
1
:
sequences
=
None
print
(
f
"
Outputting to
{
out_folder
}
"
)
out_folder
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
melt_snow
=
False
if
corr_bools
[
"
only_offset
"
]
else
agipdlib
.
SnowResolution
.
NONE
```
%% Cell type:code id: tags:
```
python
ctrl_src
=
ctrl_source_template
.
format
(
karabo_id_control
)
print
(
f
"
Detector in use is
{
karabo_id
}
"
)
# Extracting Instrument string
instrument
=
karabo_id
.
split
(
"
_
"
)[
0
]
# Evaluate detector instance for mapping
if
instrument
==
"
SPB
"
:
nmods
=
16
elif
instrument
==
"
MID
"
:
nmods
=
16
elif
instrument
==
"
HED
"
:
nmods
=
8
print
(
f
"
Instrument
{
instrument
}
"
)
if
karabo_da
[
0
]
==
'
-1
'
:
if
modules
[
0
]
==
-
1
:
modules
=
list
(
range
(
nmods
))
karabo_da
=
[
"
AGIPD{:02d}
"
.
format
(
i
)
for
i
in
modules
]
else
:
modules
=
[
int
(
x
[
-
2
:])
for
x
in
karabo_da
]
```
%% Cell type:code id: tags:
```
python
run_dc
=
RunDirectory
(
in_folder
/
f
"
r
{
run
:
04
d
}
"
)
instrument_src
=
instrument_source_template
.
format
(
karabo_id
,
receiver_template
)
instr_dc
=
run_dc
.
select
(
instrument_src
.
format
(
"
*
"
),
require_all
=
True
)
if
len
(
instr_dc
.
train_ids
)
==
0
:
if
not
instr_dc
.
train_ids
:
raise
ValueError
(
f
"
No images found for
{
in_folder
/
f
'
r
{
run
:
04
d
}
'
}
"
)
```
%% Cell type:code id: tags:
```
python
agipd_cond
=
agipdlib
.
AgipdCtrl
(
run_dc
=
run_dc
,
image_src
=
None
,
# Not needed, as we wont read mem_cells or acq_rate.
image_src
=
None
,
# Not needed
ed
, as we wont read mem_cells or acq_rate.
ctrl_src
=
ctrl_src
,
)
if
gain_setting
==
-
1
:
gain_setting
=
agipd_cond
.
get_gain_setting
(
creation_time
)
if
bias_voltage
==
0.
:
bias_voltage
=
agipd_cond
.
get_bias_voltage
(
karabo_id_control
)
if
integration_time
==
-
1
:
integration_time
=
agipd_cond
.
get_integration_time
()
if
gain_mode
==
-
1
:
gain_mode
=
agipd_cond
.
get_gain_mode
()
else
:
gain_mode
=
AgipdGainMode
(
gain_mode
)
```
%% Cell type:markdown id: tags:
## Retrieve Constants ##
%% Cell type:code id: tags:
```
python
def
retrieve_constants
(
k_da
:
str
,
idx
:
int
)
->
Tuple
[
str
,
str
,
float
,
float
,
str
,
dict
]:
"""
Retrieve constants for a module.
:return:
k_da: karabo data aggregator.
acq_rate: acquisition rate parameter.
mem_cells: number of memory cells.
mdata_dict: (DICT) dictionary with the metadata for the retrieved constants.
"""
# check if this module has images to process.
if
instrument_src
.
format
(
idx
)
not
in
instr_dc
.
all_sources
:
print
(
"
ERROR: No raw images found for
"
f
"
{
tools
.
module_index_to_qm
(
idx
)
}
(
{
k_da
}
).
"
)
return
None
,
k_da
,
None
,
None
agipd_cond
.
image_src
=
instrument_src
.
format
(
idx
)
if
mem_cells
==
0
:
# Read value from fast data.
local_mem_cells
=
agipd_cond
.
get_num_cells
()
else
:
# or use overriding notebook parameter.
local_mem_cells
=
mem_cells
if
acq_rate
==
0.
:
local_acq_rate
=
agipd_cond
.
get_acq_rate
()
else
:
local_acq_rate
=
acq_rate
# avoid retrieving constant, if requested.
if
nodb_with_dark
:
return
None
,
k_da
,
None
,
None
const_dict
=
agipdlib
.
assemble_constant_dict
(
corr_bools
,
pc_bools
,
local_mem_cells
,
bias_voltage
,
gain_setting
,
local_acq_rate
,
photon_energy
,
gain_mode
=
gain_mode
,
beam_energy
=
None
,
only_dark
=
only_dark
,
integration_time
=
integration_time
)
# Retrieve multiple constants through an input dictionary
# to return a dict of useful metadata.
mdata_dict
=
dict
()
mdata_dict
[
"
constants
"
]
=
dict
()
mdata_dict
[
"
physical-detector-unit
"
]
=
None
# initialization
for
const_name
,
(
const_init_fun
,
const_shape
,
(
cond_type
,
cond_param
))
in
const_dict
.
items
():
# noqa
if
gain_mode
and
const_name
in
(
"
ThresholdsDark
"
,):
continue
# saving metadata in a dict
const_mdata
=
dict
()
mdata_dict
[
"
constants
"
][
const_name
]
=
const_mdata
if
slopes_ff_from_files
and
const_name
in
[
"
SlopesFF
"
,
"
BadPixelsFF
"
]:
const_mdata
[
"
file-path
"
]
=
(
f
"
{
slopes_ff_from_files
}
/slopesff_bpmask_module_
{
tools
.
module_index_to_qm
(
idx
)
}
.h5
"
)
# noqa
const_mdata
[
"
creation-time
"
]
=
"
00:00:00
"
continue
if
gain_mode
and
const_name
in
(
"
BadPixelsPC
"
,
"
SlopesPC
"
,
"
BadPixelsFF
"
,
"
SlopesFF
"
):
param_copy
=
cond_param
.
copy
()
del
param_copy
[
"
gain_mode
"
]
condition
=
getattr
(
Conditions
,
cond_type
).
AGIPD
(
**
param_copy
)
else
:
condition
=
getattr
(
Conditions
,
cond_type
).
AGIPD
(
**
cond_param
)
_
,
mdata
=
tools
.
get_from_db
(
karabo_id
,
k_da
,
getattr
(
Constants
.
AGIPD
,
const_name
)(),
condition
,
getattr
(
np
,
const_init_fun
)(
const_shape
),
cal_db_interface
,
creation_time
,
meta_only
=
True
,
verbosity
=
0
,
)
mdata_const
=
mdata
.
calibration_constant_version
# check if constant was sucessfully retrieved.
if
mdata
.
comm_db_success
:
const_mdata
[
"
file-path
"
]
=
(
f
"
{
mdata_const
.
hdf5path
}
"
f
"
{
mdata_const
.
filename
}
"
)
const_mdata
[
"
creation-time
"
]
=
f
"
{
mdata_const
.
begin_at
}
"
mdata_dict
[
"
physical-detector-unit
"
]
=
mdata_const
.
device_name
else
:
const_mdata
[
"
file-path
"
]
=
const_dict
[
const_name
][:
2
]
const_mdata
[
"
creation-time
"
]
=
None
return
mdata_dict
,
k_da
,
local_acq_rate
,
local_mem_cells
```
%% Cell type:code id: tags:
```
python
# Constant paths & timestamps are saved under retrieved-constants in calibration_metadata.yml
retrieved_constants
=
metadata
.
setdefault
(
"
retrieved-constants
"
,
{})
```
%% Cell type:code id: tags:
```
python
pc_bools
=
[
corr_bools
.
get
(
"
rel_gain
"
),
corr_bools
.
get
(
"
adjust_mg_baseline
"
),
corr_bools
.
get
(
'
blc_noise
'
),
corr_bools
.
get
(
'
blc_hmatch
'
),
corr_bools
.
get
(
'
blc_stripes
'
),
melt_snow
]
inp
=
[]
only_dark
=
False
nodb_with_dark
=
False
if
not
nodb
:
only_dark
=
(
calfile
!=
""
)
if
calfile
!=
""
and
not
corr_bools
[
"
only_offset
"
]:
nodb_with_dark
=
nodb
da_to_qm
=
dict
()
for
module_index
,
k_da
in
zip
(
modules
,
karabo_da
):
da_to_qm
[
k_da
]
=
tools
.
module_index_to_qm
(
module_index
)
if
k_da
in
retrieved_constants
:
print
(
f
"
Constant for
{
k_da
}
already in calibration_metadata.yml, won
'
t query again.
"
)
continue
inp
.
append
((
k_da
,
module_index
))
```
%% Cell type:code id: tags:
```
python
with
multiprocessing
.
Pool
(
processes
=
nmods
)
as
pool
:
results
=
pool
.
starmap
(
retrieve_constants
,
inp
)
```
%% Cell type:code id: tags:
```
python
acq_rate_mods
=
[]
mem_cells_mods
=
[]
for
md_dict
,
k_da
,
acq_rate
,
mem_cells
in
results
:
if
acq_rate
is
None
and
mem_cells
is
None
:
continue
md_dict
,
k_da
,
acq_rate
,
mem_cells
retrieved_constants
[
k_da
]
=
md_dict
mem_cells_mods
.
append
(
mem_cells
)
acq_rate_mods
.
append
(
acq_rate
)
# Validate that mem_cells and acq_rate are the same for all modules.
# TODO: Should a warning be enough?
if
len
(
set
(
mem_cells_mods
))
!=
1
or
len
(
set
(
acq_rate_mods
))
!=
1
:
print
(
"
WARNING: Number of memory cells or
"
"
acquisition rate are not identical for all modules.
\n
"
f
"
mem_cells:
{
mem_cells_mods
}
.
\n
acq_rate:
{
acq_rate_mods
}
.
"
)
# check if it is requested not to retrieve any constants from the database
if
nodb_with_dark
:
print
(
"
No constants were retrieved as calibrated files will be used.
"
)
else
:
print
(
"
\n
Retrieved constants for modules:
"
,
'
,
'
.
join
([
tools
.
module_index_to_qm
(
x
)
for
x
in
modules
]))
print
(
f
"
Operating conditions are:
"
)
print
(
f
"
• Bias voltage:
{
bias_voltage
}
"
)
print
(
f
"
• Memory cells:
{
mem_cells
}
"
)
print
(
f
"
• Acquisition rate:
{
acq_rate
}
"
)
print
(
f
"
• Gain mode:
{
gain_mode
.
name
}
"
)
print
(
f
"
• Gain setting:
{
gain_setting
}
"
)
print
(
f
"
• Integration time:
{
integration_time
}
"
)
print
(
f
"
• Photon Energy:
{
photon_energy
}
"
)
print
(
"
Constant metadata is saved under
\"
retrieved-constants
\"
in calibration_metadata.yml
\n
"
)
```
%% Cell type:code id: tags:
```
python
print
(
"
Using constants with creation times:
"
)
timestamps
=
{}
for
k_da
,
module_name
in
da_to_qm
.
items
():
if
k_da
not
in
retrieved_constants
.
keys
():
continue
module_timestamps
=
timestamps
[
module_name
]
=
{}
module_constants
=
retrieved_constants
[
k_da
]
print
(
f
"
{
module_name
}
:
"
)
for
cname
,
mdata
in
module_constants
[
"
constants
"
].
items
():
if
hasattr
(
mdata
[
"
creation-time
"
],
'
strftime
'
):
mdata
[
"
creation-time
"
]
=
mdata
[
"
creation-time
"
].
strftime
(
'
%y-%m-%d %H:%M
'
)
print
(
f
'
{
cname
:
.
<
12
s
}
'
,
mdata
[
"
creation-time
"
])
for
cname
in
[
'
Offset
'
,
'
SlopesPC
'
,
'
SlopesFF
'
]:
if
cname
in
module_constants
[
"
constants
"
]:
module_timestamps
[
cname
]
=
module_constants
[
"
constants
"
][
cname
][
"
creation-time
"
]
else
:
module_timestamps
[
cname
]
=
"
NA
"
time_summary
=
retrieved_constants
.
setdefault
(
"
time-summary
"
,
{})
time_summary
[
"
SAll
"
]
=
timestamps
metadata
.
save
()
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment