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
b5ef7995
Commit
b5ef7995
authored
3 years ago
by
Karim Ahmed
Browse files
Options
Downloads
Patches
Plain Diff
New notebook for injecting constants from stored h5files
parent
5f4c9857
No related branches found
No related tags found
1 merge request
!475
[LPD] New notebook for injecting constants from stored h5files
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
notebooks/LPD/inject_calibration_constants_from_h5files.ipynb
+218
-0
218 additions, 0 deletions
...books/LPD/inject_calibration_constants_from_h5files.ipynb
src/cal_tools/tools.py
+2
-2
2 additions, 2 deletions
src/cal_tools/tools.py
with
220 additions
and
2 deletions
notebooks/LPD/inject_calibration_constants_from_h5files.ipynb
0 → 100644
+
218
−
0
View file @
b5ef7995
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Injecting calibration constant data to the database #\n",
"\n",
"Author: European XFEL Detector Group, Version: 1.0\n",
"\n",
"Reading h5files of calibration constants to inject them to the database. Used for LPD"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"# calibration constant parameters:\n",
"constant_names = [\"GainMap\", \"GainAmpMap\", \"FFMap\", \"BadPixelsFF\"] # calibration constant name, required.\n",
"in_folder = \"/gpfs/exfel/data/scratch/yousefh/LPD_CalinConst_Provisional/\" # calibration constant file, required.\n",
"proposal = \"\" # Add proposal number to be sent to the database as a part of Raw data location.\n",
"runs = [] # Add list of runs to be sent to the database as a part of Raw data location.\n",
"\n",
"# detector parameters:\n",
"karabo_id = \"FXE_DET_LPD1M-1\" # detector identifier, required.\n",
"karabo_da = [\"LPD00\"] # karabo data aggregators, required.\n",
"\n",
"# calibration database parameters:\n",
"cal_db_interface = \"tcp://max-exfl017:8020\"\n",
"\n",
"# calibration constant conditions:\n",
"memory_cells = 512\n",
"bias_voltage = 250\n",
"capacitor = 5\n",
"creation_time = '2020-01-20T14:12:06' # creation time for the injected constants. required format '2019-01-20T14:12:06'"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import h5py\n",
"from pathlib import Path\n",
"\n",
"from iCalibrationDB import (\n",
" Constants,\n",
" Conditions,\n",
")\n",
"from cal_tools.tools import (\n",
" get_from_db,\n",
" get_pdu_from_db,\n",
" send_to_db,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pixels_x = pixels_y = 256\n",
"\n",
"# if proposal or runs are given assign file_loc \n",
"# for calibration constant versions metadata.\n",
"file_loc = \"\"\n",
"if proposal:\n",
" file_loc += f\"proposal:{proposal}\"\n",
"if len(runs) > 0:\n",
" file_loc += f\"runs: {runs}\"\n",
"\n",
"if file_loc == \"\"\n",
" print(\n",
" \"No proposal or runs were given for constant source.\"\n",
" \" No \\\"Raw data location\\\" will be injected with the constants\"\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def validate_input_paths(in_folder):\n",
" \n",
" # Validate input parameters:\n",
" if not (in_folder):\n",
" raise ValueError(\n",
" \"ERROR: \\\"in_folder\\\" is not given.\"\n",
" \" Please provide the constants input folder.\"\n",
" )\n",
"\n",
" c_folder = Path(in_folder)\n",
" \n",
" if not c_folder.isdir():\n",
" raise ValueError(\n",
" f\"ERROR: in_folder {in_folder} directory doesn't exist.\"\n",
" )\n",
"\n",
" try:\n",
" creation_time = datetime.datetime.strptime(creation_time, '%Y-%m-%dT%H:%M:%S')\n",
" except ValueError:\n",
" raise ValueError(\"Incorrect data format, should be YYYY-MM-DDTHH:MM:SS i.e. 2019-01-20T14:12:06\")\n",
" \n",
" return c_folder, creation_time"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No proposal or runs were given for constant source. No \"Raw data location\" will be injected with the constants\n",
"Physical detector units retrieved are: {'LPD00': 'LPD_SIV1_LPDV2_SM015'}\n",
"BadPixelsFF is injected with creation-time: 2020-01-20 14:12:06\n",
"\n"
]
}
],
"source": [
"c_folder, creation_time = validate_input_paths(in_folder)\n",
"\n",
"# create a report path for calibration constant versions metadata.\n",
"report_path = get_report(\n",
" out_folder=in_folder,\n",
" default_path=f\"No_report/LPD_{datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')}\"\n",
")\n",
"\n",
"# Calibration constants condition object.\n",
"condition = Conditions.Dark.LPD(\n",
" memory_cells=memory_cells,\n",
" bias_voltage=bias_voltage,\n",
" pixels_x=pixels_x,\n",
" pixels_y=pixels_y,\n",
" capacitor=capacitor,\n",
")\n",
"\n",
"mod_mapping = {}\n",
"for const in constant_names:\n",
"\n",
" # Calibration constant object.\n",
" constant = getattr(Constants.LPD, const)()\n",
" \n",
" if len(mod_mapping.keys()) == 0\n",
" # Retrieve the detector modules if it is not known.\n",
" physical_units = get_pdu_from_db(\n",
" karabo_id=karabo_id,\n",
" karabo_da=karabo_da,\n",
" constant=constant,\n",
" condition=condition,\n",
" cal_db_interface=cal_db_interface,\n",
" snapshot_at=creation_time\n",
" )\n",
" mod_mapping = dict(zip(karabo_da, physical_units))\n",
" print(\"Physical detector units retrieved are: \", mod_mapping)\n",
"\n",
" for pdu, k_da in zip(physical_units, karabo_da): \n",
"\n",
" cfile = c_folder / f\"{const}_{k_da}\"\n",
"\n",
" # load constant data.\n",
" with h5py.File(cfile, \"r\") as f:\n",
" cdata = f[const][()]\n",
"\n",
" # Validate for only LPD at the moment.\n",
" if not cdata.shape == (pixels_x, pixels_y, memory_cells, 3):\n",
" raise ValueError(\n",
" f\"ERROR: {const} constant data shape is not as expected.\"\n",
" f\" {cdata.shape} != ({pixels_x}, {pixels_y}, {memory_cells}, 3).\"\n",
" )\n",
"\n",
" constant.data = cdata\n",
"\n",
" md = send_to_db(\n",
" db_module=pdu,\n",
" karabo_id=karabo_id,\n",
" constant=constant,\n",
" condition=condition,\n",
" file_loc=file_loc,\n",
" report_path=report_path,\n",
" cal_db_interface=cal_db_interface,\n",
" creation_time=creation_time,\n",
" )"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "cal_venv",
"language": "python",
"name": "cal_venv"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
%% Cell type:markdown id: tags:
# Injecting calibration constant data to the database #
Author: European XFEL Detector Group, Version: 1.0
Reading h5files of calibration constants to inject them to the database. Used for LPD
%% Cell type:code id: tags:
```
python
# calibration constant parameters:
constant_names
=
[
"
GainMap
"
,
"
GainAmpMap
"
,
"
FFMap
"
,
"
BadPixelsFF
"
]
# calibration constant name, required.
in_folder
=
"
/gpfs/exfel/data/scratch/yousefh/LPD_CalinConst_Provisional/
"
# calibration constant file, required.
proposal
=
""
# Add proposal number to be sent to the database as a part of Raw data location.
runs
=
[]
# Add list of runs to be sent to the database as a part of Raw data location.
# detector parameters:
karabo_id
=
"
FXE_DET_LPD1M-1
"
# detector identifier, required.
karabo_da
=
[
"
LPD00
"
]
# karabo data aggregators, required.
# calibration database parameters:
cal_db_interface
=
"
tcp://max-exfl017:8020
"
# calibration constant conditions:
memory_cells
=
512
bias_voltage
=
250
capacitor
=
5
creation_time
=
'
2020-01-20T14:12:06
'
# creation time for the injected constants. required format '2019-01-20T14:12:06'
```
%% Cell type:code id: tags:
```
python
import
datetime
import
h5py
from
pathlib
import
Path
from
iCalibrationDB
import
(
Constants
,
Conditions
,
)
from
cal_tools.tools
import
(
get_from_db
,
get_pdu_from_db
,
send_to_db
,
)
```
%% Cell type:code id: tags:
```
python
pixels_x
=
pixels_y
=
256
# if proposal or runs are given assign file_loc
# for calibration constant versions metadata.
file_loc
=
""
if
proposal
:
file_loc
+=
f
"
proposal:
{
proposal
}
"
if
len
(
runs
)
>
0
:
file_loc
+=
f
"
runs:
{
runs
}
"
if
file_loc
==
""
print
(
"
No proposal or runs were given for constant source.
"
"
No
\"
Raw data location
\"
will be injected with the constants
"
)
```
%% Cell type:code id: tags:
```
python
def
validate_input_paths
(
in_folder
):
# Validate input parameters:
if
not
(
in_folder
):
raise
ValueError
(
"
ERROR:
\"
in_folder
\"
is not given.
"
"
Please provide the constants input folder.
"
)
c_folder
=
Path
(
in_folder
)
if
not
c_folder
.
isdir
():
raise
ValueError
(
f
"
ERROR: in_folder
{
in_folder
}
directory doesn
'
t exist.
"
)
try
:
creation_time
=
datetime
.
datetime
.
strptime
(
creation_time
,
'
%Y-%m-%dT%H:%M:%S
'
)
except
ValueError
:
raise
ValueError
(
"
Incorrect data format, should be YYYY-MM-DDTHH:MM:SS i.e. 2019-01-20T14:12:06
"
)
return
c_folder
,
creation_time
```
%% Cell type:code id: tags:
```
python
c_folder
,
creation_time
=
validate_input_paths
(
in_folder
)
# create a report path for calibration constant versions metadata.
report_path
=
get_report
(
out_folder
=
in_folder
,
default_path
=
f
"
No_report/LPD_
{
datetime
.
datetime
.
now
().
strftime
(
'
%Y-%m-%dT%H
:
%
M
:
%
S
'
)
}
"
)
# Calibration constants condition object.
condition
=
Conditions
.
Dark
.
LPD
(
memory_cells
=
memory_cells
,
bias_voltage
=
bias_voltage
,
pixels_x
=
pixels_x
,
pixels_y
=
pixels_y
,
capacitor
=
capacitor
,
)
mod_mapping
=
{}
for
const
in
constant_names
:
# Calibration constant object.
constant
=
getattr
(
Constants
.
LPD
,
const
)()
if
len
(
mod_mapping
.
keys
())
==
0
# Retrieve the detector modules if it is not known.
physical_units
=
get_pdu_from_db
(
karabo_id
=
karabo_id
,
karabo_da
=
karabo_da
,
constant
=
constant
,
condition
=
condition
,
cal_db_interface
=
cal_db_interface
,
snapshot_at
=
creation_time
)
mod_mapping
=
dict
(
zip
(
karabo_da
,
physical_units
))
print
(
"
Physical detector units retrieved are:
"
,
mod_mapping
)
for
pdu
,
k_da
in
zip
(
physical_units
,
karabo_da
):
cfile
=
c_folder
/
f
"
{
const
}
_
{
k_da
}
"
# load constant data.
with
h5py
.
File
(
cfile
,
"
r
"
)
as
f
:
cdata
=
f
[
const
][()]
# Validate for only LPD at the moment.
if
not
cdata
.
shape
==
(
pixels_x
,
pixels_y
,
memory_cells
,
3
):
raise
ValueError
(
f
"
ERROR:
{
const
}
constant data shape is not as expected.
"
f
"
{
cdata
.
shape
}
!= (
{
pixels_x
}
,
{
pixels_y
}
,
{
memory_cells
}
, 3).
"
)
constant
.
data
=
cdata
md
=
send_to_db
(
db_module
=
pdu
,
karabo_id
=
karabo_id
,
constant
=
constant
,
condition
=
condition
,
file_loc
=
file_loc
,
report_path
=
report_path
,
cal_db_interface
=
cal_db_interface
,
creation_time
=
creation_time
,
)
```
%% Output
No proposal or runs were given for constant source. No "Raw data location" will be injected with the constants
Physical detector units retrieved are: {'LPD00': 'LPD_SIV1_LPDV2_SM015'}
BadPixelsFF is injected with creation-time: 2020-01-20 14:12:06
This diff is collapsed.
Click to expand it.
src/cal_tools/tools.py
+
2
−
2
View file @
b5ef7995
...
...
@@ -373,13 +373,13 @@ def get_random_db_interface(cal_db_interface):
return
cal_db_interface
def
get_report
(
out_folder
:
str
):
def
get_report
(
out_folder
:
str
,
default_path
:
str
=
""
):
"""
Get the report path from calibration_metadata.yml
stored in the out_folder.
"""
metadata
=
CalibrationMetadata
(
out_folder
)
report_path
=
metadata
.
get
(
"
report-path
"
,
""
)
report_path
=
metadata
.
get
(
"
report-path
"
,
default_path
)
if
not
report_path
:
print
(
"
WARNING: No report path will be injected
"
"
with the constants.
\n
"
)
...
...
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