Skip to content
Snippets Groups Projects
Commit a7b702c4 authored by Karim Ahmed's avatar Karim Ahmed
Browse files

reduce processes

parent 965d39ef
No related branches found
No related tags found
1 merge request!475[LPD] New notebook for injecting constants from stored h5files
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Injecting calibration constant data to the database # # Injecting calibration constant data to the database #
Author: European XFEL Detector Group, Version: 1.0 Author: European XFEL Detector Group, Version: 1.0
Reading h5files of calibration constants to inject them to the database. Used for LPD Reading h5files of calibration constants to inject them to the database. Used for LPD
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# calibration constant parameters: # calibration constant parameters:
constant_names = [""] # calibration constant name, required. constant_names = [""] # calibration constant name, required.
in_folder = "" # calibration constants folder, required. in_folder = "" # calibration constants folder, required.
out_folder = "" # output folder to store report path in case the notebook is executed by CLI, required. out_folder = "" # output folder to store report path in case the notebook is executed by CLI, required.
proposal = "" # Add proposal number to be sent to the database as a part of Raw data location. 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. runs = [""] # Add list of runs to be sent to the database as a part of Raw data location.
# detector parameters: # detector parameters:
karabo_id = "FXE_DET_LPD1M-1" # detector identifier. karabo_id = "FXE_DET_LPD1M-1" # detector identifier.
karabo_da = ["all"] # karabo data aggregators. default "all" for all 16 karabo data aggregator names. karabo_da = ["all"] # karabo data aggregators. default "all" for all 16 karabo data aggregator names.
# calibration database parameters: # calibration database parameters:
cal_db_interface = "tcp://max-exfl016:8015#8045" # calibration DB zmq address. cal_db_interface = "tcp://max-exfl016:8015#8045" # calibration DB zmq address.
# calibration constant conditions: # calibration constant conditions:
memory_cells = 512 # Number of memory cells. Used for constant conditions. memory_cells = 512 # Number of memory cells. Used for constant conditions.
bias_voltage = 250 # bias voltage value. Used for constant conditions. bias_voltage = 250 # bias voltage value. Used for constant conditions.
capacitor = 5 # capacitor value. Used for constant conditions. capacitor = 5 # capacitor value. Used for constant conditions.
category = 0 # calibration constant source category, 0 for European XFEl and 1 for RAL. Used for constant conditions. category = 0 # calibration constant source category, 0 for European XFEl and 1 for RAL. Used for constant conditions.
photon_energy = 9.2 # calibration constant photon energy. Used for constant conditions. photon_energy = 9.2 # calibration constant photon energy. Used for constant conditions.
creation_time = '2020-01-20T14:12:06' # creation time for the injected constants. required format '2019-01-20T14:12:06' 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: %% Cell type:code id: tags:
``` python ``` python
import multiprocessing import multiprocessing
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import List, Tuple from typing import List, Tuple
import h5py import h5py
from iCalibrationDB import ( from iCalibrationDB import (
Constants, Constants,
Conditions, Conditions,
) )
from cal_tools.tools import ( from cal_tools.tools import (
get_from_db, get_from_db,
get_pdu_from_db, get_pdu_from_db,
get_report, get_report,
send_to_db, send_to_db,
) )
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
pixels_x = pixels_y = 256 pixels_x = pixels_y = 256
if karabo_da[0].lower() == "all": if karabo_da[0].lower() == "all":
karabo_da = [f"LPD{i:02d}" for i in range(16)] karabo_da = [f"LPD{i:02d}" for i in range(16)]
# if proposal or runs are given assign file_loc # if proposal or runs are given assign file_loc
# for calibration constant versions metadata. # for calibration constant versions metadata.
file_loc = "" file_loc = ""
if proposal: if proposal:
file_loc += f"proposal:{proposal}" file_loc += f"proposal:{proposal}"
if runs[0] != "": if runs[0] != "":
file_loc += f"runs: {runs}" file_loc += f"runs: {runs}"
if file_loc == "": if file_loc == "":
print( print(
"No proposal or runs were given for constant source." "No proposal or runs were given for constant source."
" No \"Raw data location\" will be injected with the constants.\n" " No \"Raw data location\" will be injected with the constants.\n"
) )
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def validate_input_paths( def validate_input_paths(
in_folder: str, in_folder: str,
creation_time: str, creation_time: str,
constant_names: List[str], constant_names: List[str],
) -> Tuple[str, datetime]: ) -> Tuple[str, datetime]:
# Validate input parameters: # Validate input parameters:
if not (in_folder): if not (in_folder):
raise ValueError( raise ValueError(
"ERROR: \"in_folder\" is not given." "ERROR: \"in_folder\" is not given."
" Please provide the constants input folder." " Please provide the constants input folder."
) )
c_folder = Path(in_folder) c_folder = Path(in_folder)
if not c_folder.is_dir(): if not c_folder.is_dir():
raise ValueError( raise ValueError(
f"ERROR: in_folder {in_folder} directory doesn't exist." f"ERROR: in_folder {in_folder} directory doesn't exist."
) )
try: try:
creation_time = datetime.strptime(creation_time, '%Y-%m-%dT%H:%M:%S') creation_time = datetime.strptime(creation_time, '%Y-%m-%dT%H:%M:%S')
except ValueError: except ValueError:
raise ValueError("Incorrect data format, should be YYYY-MM-DDTHH:MM:SS i.e. 2019-01-20T14:12:06") raise ValueError("Incorrect data format, should be YYYY-MM-DDTHH:MM:SS i.e. 2019-01-20T14:12:06")
for constant in constant_names: for constant in constant_names:
if not hasattr(Constants.LPD, constant): if not hasattr(Constants.LPD, constant):
raise ValueError( raise ValueError(
f"ERROR: Constant name \"{constant}\" is not a known LPD constant. " f"ERROR: Constant name \"{constant}\" is not a known LPD constant. "
f"Available LPD Constants are {[c for c in dir(Constants.LPD) if c[0] != '_']}" f"Available LPD Constants are {[c for c in dir(Constants.LPD) if c[0] != '_']}"
) )
return c_folder, creation_time return c_folder, creation_time
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def inject_constants( def inject_constants(
constant_name: str, constant_name: str,
karabo_da: str, karabo_da: str,
physical_unit: str, physical_unit: str,
): ):
constant = getattr(Constants.LPD, constant_name)() constant = getattr(Constants.LPD, constant_name)()
cfile = c_folder / f"{constant_name}_{karabo_da}.h5" cfile = c_folder / f"{constant_name}_{karabo_da}.h5"
if not cfile.exists(): if not cfile.exists():
raise ValueError(f"ERROR: constant file {cfile} doesn't exists.\n") raise ValueError(f"ERROR: constant file {cfile} doesn't exists.\n")
# load constant data. # load constant data.
with h5py.File(cfile, "r") as f: with h5py.File(cfile, "r") as f:
cdata = f[constant_name][()] cdata = f[constant_name][()]
# Validate for only LPD at the moment. # Validate for only LPD at the moment.
if not cdata.shape == (pixels_x, pixels_y, memory_cells, 3): if not cdata.shape == (pixels_x, pixels_y, memory_cells, 3):
raise ValueError( raise ValueError(
f"ERROR: {const} constant data shape is not as expected." f"ERROR: {const} constant data shape is not as expected."
f" {cdata.shape} != ({pixels_x}, {pixels_y}, {memory_cells}, 3).\n" f" {cdata.shape} != ({pixels_x}, {pixels_y}, {memory_cells}, 3).\n"
) )
constant.data = cdata constant.data = cdata
send_to_db( send_to_db(
db_module=physical_unit, db_module=physical_unit,
karabo_id=karabo_id, karabo_id=karabo_id,
constant=constant, constant=constant,
condition=condition, condition=condition,
file_loc=file_loc, file_loc=file_loc,
report_path=report_path, report_path=report_path,
cal_db_interface=cal_db_interface, cal_db_interface=cal_db_interface,
creation_time=creation_time, creation_time=creation_time,
) )
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
c_folder, creation_time = validate_input_paths( c_folder, creation_time = validate_input_paths(
in_folder, in_folder,
creation_time, creation_time,
constant_names, constant_names,
) )
# create a report path for calibration constant versions metadata. # create a report path for calibration constant versions metadata.
report_path = get_report( report_path = get_report(
out_folder=in_folder, out_folder=in_folder,
default_path=f"No_report/LPD_{datetime.now().strftime('%Y-%m-%dT%H:%M:%S')}" default_path=f"No_report/LPD_{datetime.now().strftime('%Y-%m-%dT%H:%M:%S')}"
) )
# Calibration constants condition object. # Calibration constants condition object.
condition = Conditions.Illuminated.LPD( condition = Conditions.Illuminated.LPD(
memory_cells=memory_cells, memory_cells=memory_cells,
bias_voltage=bias_voltage, bias_voltage=bias_voltage,
photon_energy=photon_energy, photon_energy=photon_energy,
pixels_x=pixels_x, pixels_x=pixels_x,
pixels_y=pixels_y, pixels_y=pixels_y,
capacitor=capacitor, capacitor=capacitor,
category=category, category=category,
) )
# Retrieve all physical detector units for the given karabo_da list. # Retrieve all physical detector units for the given karabo_da list.
physical_units = get_pdu_from_db( physical_units = get_pdu_from_db(
karabo_id=karabo_id, karabo_id=karabo_id,
karabo_da=karabo_da, karabo_da=karabo_da,
constant=getattr(Constants.LPD, constant_names[0])(), constant=getattr(Constants.LPD, constant_names[0])(),
condition=condition, condition=condition,
cal_db_interface=cal_db_interface, cal_db_interface=cal_db_interface,
snapshot_at=creation_time snapshot_at=creation_time
) )
mod_mapping = dict(zip(karabo_da, physical_units)) mod_mapping = dict(zip(karabo_da, physical_units))
print("Physical detector units retrieved are: ", mod_mapping, "\n") print("Physical detector units retrieved are: ", mod_mapping, "\n")
inp = [] inp = []
for const in constant_names: for const in constant_names:
for k_da, pdu in mod_mapping.items(): for k_da, pdu in mod_mapping.items():
inp.append((const, k_da, pdu)) inp.append((const, k_da, pdu))
with multiprocessing.Pool(processes=16) as pool: with multiprocessing.Pool(processes=5) as pool:
results = pool.starmap(inject_constants, inp) results = pool.starmap(inject_constants, inp)
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment