diff --git a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb index 150ae71d6ad7297a98a981b7895b673ebd012a48..89391efcddda794b8549a2a070f54c2ba9b5952a 100644 --- a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb +++ b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb @@ -33,9 +33,9 @@ "karabo_id_control = \"\" # if control is on a different ID, set to empty string if it is the same a karabo-id\n", "\n", "# Parameters for calibration database.\n", + "use_dir_creation_date = True # use the creation data of the input dir for database queries\n", "cal_db_interface = \"tcp://max-exfl016:8017#8025\" # the database interface to use\n", "cal_db_timeout = 180000 # timeout on caldb requests\n", - "creation_time = \"\" # To overwrite the measured creation_time. Required Format: YYYY-MM-DD HR:MN:SC e.g. \"2022-06-28 13:00:00\"\n", "\n", "# Parameters affecting corrected data.\n", "relative_gain = True # do relative gain correction.\n", @@ -72,10 +72,10 @@ "metadata": {}, "outputs": [], "source": [ - "import gc\n", "import multiprocessing\n", "import sys\n", "import warnings\n", + "from functools import partial\n", "from logging import warning\n", "from pathlib import Path\n", "\n", @@ -90,18 +90,19 @@ "from extra_geom import JUNGFRAUGeometry\n", "from matplotlib.colors import LogNorm\n", "\n", - "from calibration_client import CalibrationClient\n", - "from cal_tools.calcat_interface import JUNGFRAU_CalibrationData\n", + "from cal_tools import h5_copy_except\n", "from cal_tools.jungfraulib import JungfrauCtrl\n", "from cal_tools.enums import BadPixels\n", "from cal_tools.files import DataFile\n", - "from cal_tools.restful_config import restful_config\n", "from cal_tools.step_timing import StepTimer\n", "from cal_tools.tools import (\n", - " calcat_creation_time,\n", + " get_constant_from_db_and_time,\n", + " get_dir_creation_date,\n", + " get_pdu_from_db,\n", " map_seq_files,\n", " CalibrationMetadata,\n", ")\n", + "from iCalibrationDB import Conditions, Constants\n", "\n", "warnings.filterwarnings('ignore')\n", "\n", @@ -121,19 +122,16 @@ "run_dc = RunDirectory(run_folder)\n", "instrument_src = instrument_source_template.format(karabo_id, receiver_template)\n", "\n", - "metadata = CalibrationMetadata(metadata_folder or out_folder)\n", - "# NOTE: this notebook will not overwrite calibration metadata file\n", - "const_yaml = metadata.get(\"retrieved-constants\", {})\n", - "\n", "out_folder.mkdir(parents=True, exist_ok=True)\n", "\n", "print(f\"Run is: {run}\")\n", "print(f\"Instrument H5File source: {instrument_src}\")\n", "print(f\"Process modules: {karabo_da}\")\n", "\n", - "# Run's creation time:\n", - "creation_time = calcat_creation_time(in_folder, run, creation_time)\n", - "print(f\"Creation time: {creation_time}\")\n", + "creation_time = None\n", + "if use_dir_creation_date:\n", + " creation_time = get_dir_creation_date(in_folder, run)\n", + " print(f\"Using {creation_time} as creation time\")\n", "\n", "if karabo_id_control == \"\":\n", " karabo_id_control = karabo_id\n", @@ -191,6 +189,7 @@ "\n", "if mem_cells < 0:\n", " memory_cells, sc_start = ctrl_data.get_memory_cells()\n", + "\n", " mem_cells_name = \"single cell\" if memory_cells == 1 else \"burst\"\n", " print(f\"Run is in {mem_cells_name} mode.\\nStorage cell start: {sc_start:02d}\")\n", "else:\n", @@ -223,26 +222,6 @@ " print('Strixel sensor transformation enabled')" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Connect to CalCat.\n", - "calcat_config = restful_config['calcat']\n", - "client = CalibrationClient(\n", - " base_api_url=calcat_config['base-api-url'],\n", - " use_oauth2=calcat_config['use-oauth2'],\n", - " client_id=calcat_config['user-id'],\n", - " client_secret=calcat_config['user-secret'],\n", - " user_email=calcat_config['user-email'],\n", - " token_url=calcat_config['token-url'],\n", - " refresh_url=calcat_config['refresh-url'],\n", - " auth_url=calcat_config['auth-url'],\n", - " scope='')" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -256,81 +235,70 @@ "metadata": {}, "outputs": [], "source": [ - "da_to_pdu = {}\n", + "condition = Conditions.Dark.jungfrau(\n", + " memory_cells=memory_cells,\n", + " bias_voltage=bias_voltage,\n", + " integration_time=integration_time,\n", + " gain_setting=gain_setting,\n", + " gain_mode=gain_mode,\n", + ")\n", "\n", - "if const_yaml:\n", - " const_data = dict()\n", - " for mod in karabo_da:\n", - " const_data[mod] = dict()\n", - " for cname, mdata in const_yaml[mod][\"constants\"].items():\n", - " const_data[mod][cname] = dict()\n", - " if mdata[\"creation-time\"]:\n", - " with h5py.File(mdata[\"path\"], \"r\") as cf:\n", - " const_data[mod][cname] = np.copy(\n", - " cf[f\"{mdata['dataset']}/data\"])\n", - "else:\n", - " jf_cal = JUNGFRAU_CalibrationData(\n", - " detector_name=karabo_id,\n", - " sensor_bias_voltage=bias_voltage,\n", - " event_at=creation_time,\n", - " snapshot_at=creation_time,\n", - " modules=karabo_da,\n", - " memory_cells=memory_cells,\n", - " integration_time=integration_time,\n", - " gain_setting=gain_setting,\n", - " gain_mode=gain_mode,\n", - " client=client,\n", - " )\n", - " constant_names = [\n", - " \"Offset10Hz\", \"BadPixelsDark10Hz\",\n", - " \"BadPixelsFF10Hz\", \"RelativeGain10Hz\",\n", - " ]\n", - " const_data = jf_cal.ndarray_map(calibrations=constant_names)\n", - " \n", - " for mod_info in jf_cal.physical_detector_units.values():\n", - " da_to_pdu[mod_info[\"karabo_da\"]] = mod_info[\"physical_name\"]\n", - "\n", - "# Validate the constants availability and raise/warn correspondingly. \n", - "for mod, calibrations in const_data.items():\n", - " missing_dark_constants = set(\n", - " c for c in [\"Offset10Hz\", \"BadPixelsDark10Hz\"] if c not in calibrations.keys())\n", - " missing_gain_constants = set(\n", - " c for c in [\"BadPixelsFF10Hz\", \"RelativeGain10Hz\"] if relative_gain and c not in calibrations.keys()) # noqa\n", - " if missing_dark_constants:\n", - " raise KeyError(\n", - " f\"Dark constants {missing_dark_constants} are not available for correction. Module: {mod}\") # noqa\n", - " if missing_gain_constants:\n", - " warning(\n", - " f\"Gain constants {missing_gain_constants} were not retrieved. Module: {mod}\")\n", - " relative_gain = False" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def prepare_constants(module: str):\n", - " \"\"\"Prepare constant arrays.\n", + "empty_constants = {\n", + " \"Offset\": np.zeros((512, 1024, memory_cells, 3), dtype=np.float32),\n", + " \"BadPixelsDark\": np.zeros((512, 1024, memory_cells, 3), dtype=np.uint32),\n", + " \"RelativeGain\": None,\n", + " \"BadPixelsFF\": None,\n", + "}\n", + "metadata = CalibrationMetadata(metadata_folder or out_folder)\n", + "# NOTE: this notebook will not overwrite calibration metadata file\n", + "const_yaml = metadata.get(\"retrieved-constants\", {})\n", + "\n", + "def get_constants_for_module(karabo_da: str):\n", + " \"\"\" Get calibration constants for given module of Jungfrau\n", "\n", - " :param module: The module name (karabo_da)\n", " :return:\n", " offset_map (offset map),\n", " mask (mask of bad pixels),\n", " gain_map (map of relative gain factors),\n", - " module (name of module),\n", + " db_module (name of DB module),\n", + " when (dictionary: constant - creation time)\n", " \"\"\"\n", - " constant_arrays = const_data[module]\n", - " try:\n", - " offset_map = constant_arrays[\"Offset10Hz\"]\n", - " mask = constant_arrays[\"BadPixelsDark10Hz\"]\n", - " except KeyError as e:\n", - " # Fail processing if dark constants are not available.\n", - " raise KeyError(f\"Dark constant is not available for correction for {module}. {e}\")\n", - " \n", - " gain_map = constant_arrays.get(\"RelativeGain10Hz\")\n", - " mask_ff = constant_arrays.get(\"BadPixelsFF10Hz\")\n", + "\n", + " when = dict()\n", + " const_data = dict()\n", + "\n", + " if const_yaml:\n", + " for cname, mdata in const_yaml[karabo_da][\"constants\"].items():\n", + " const_data[cname] = dict()\n", + " when[cname] = mdata[\"creation-time\"]\n", + " if when[cname]:\n", + " with h5py.File(mdata[\"file-path\"], \"r\") as cf:\n", + " const_data[cname] = np.copy(\n", + " cf[f\"{mdata['dataset-name']}/data\"])\n", + " else:\n", + " const_data[cname] = empty_constants[cname]\n", + " else:\n", + " retrieval_function = partial(\n", + " get_constant_from_db_and_time,\n", + " karabo_id=karabo_id,\n", + " karabo_da=karabo_da,\n", + " cal_db_interface=cal_db_interface,\n", + " creation_time=creation_time,\n", + " timeout=cal_db_timeout,\n", + " print_once=False,\n", + " )\n", + " \n", + " for cname, cempty in empty_constants.items():\n", + " const_data[cname], when[cname] = retrieval_function(\n", + " condition=condition,\n", + " constant=getattr(Constants.jungfrau, cname)(),\n", + " empty_constant=cempty,\n", + " )\n", + "\n", + " offset_map = const_data[\"Offset\"]\n", + " mask = const_data[\"BadPixelsDark\"]\n", + " gain_map = const_data[\"RelativeGain\"]\n", + " mask_ff = const_data[\"BadPixelsFF\"]\n", "\n", " # Combine masks\n", " if mask_ff is not None:\n", @@ -343,7 +311,7 @@ " else:\n", " offset_map = np.squeeze(offset_map)\n", " mask = np.squeeze(mask)\n", - "\n", + " \n", " # masking double size pixels\n", " mask[..., [255, 256], :, :] |= BadPixels.NON_STANDARD_SIZE\n", " mask[..., [255, 256, 511, 512, 767, 768], :] |= BadPixels.NON_STANDARD_SIZE\n", @@ -357,19 +325,23 @@ " else:\n", " gain_map = np.moveaxis(np.squeeze(gain_map), 1, 0)\n", "\n", - " return offset_map, mask, gain_map, module\n", + " return offset_map, mask, gain_map, karabo_da, when\n", "\n", "with multiprocessing.Pool() as pool:\n", - " r = pool.map(prepare_constants, karabo_da)\n", + " r = pool.map(get_constants_for_module, karabo_da)\n", "\n", "# Print timestamps for the retrieved constants.\n", "constants = {}\n", - "for offset_map, mask, gain_map, k_da in r:\n", + "for offset_map, mask, gain_map, k_da, when in r:\n", + " print(f'Constants for module {k_da}:')\n", + " for const in when:\n", + " print(f' {const} injected at {when[const]}')\n", "\n", - " constants[k_da] = (offset_map, mask, gain_map)\n", + " if gain_map is None:\n", + " print(\"No gain map found\")\n", + " relative_gain = False\n", "\n", - "const_data.clear()\n", - "gc.collect()" + " constants[k_da] = (offset_map, mask, gain_map)" ] }, { @@ -742,6 +714,22 @@ ")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "db_modules = get_pdu_from_db(\n", + " karabo_id=karabo_id,\n", + " karabo_da=karabo_da,\n", + " constant=Constants.jungfrau.Offset(),\n", + " condition=condition,\n", + " cal_db_interface=cal_db_interface,\n", + " snapshot_at=creation_time,\n", + ")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -910,7 +898,7 @@ "metadata": {}, "outputs": [], "source": [ - "for i, (pdu, mod) in enumerate(da_to_pdu.items()): \n", + "for i, (pdu, mod) in enumerate(zip(db_modules, karabo_da)):\n", " h, ex, ey = np.histogram2d(\n", " raw[i].flatten(),\n", " gain[i].flatten(),\n", @@ -939,7 +927,7 @@ "metadata": {}, "outputs": [], "source": [ - "for i, (pdu, mod) in enumerate(da_to_pdu.items()): \n", + "for i, (pdu, mod) in enumerate(zip(db_modules, karabo_da)): \n", " fig, axs = plt.subplots(nrows=2, ncols=1, figsize=(18, 10))\n", " corrected_flatten = corrected[i].flatten()\n", " for ax, hist_range in zip(axs, [(-100, 1000), (-1000, 10000)]):\n", diff --git a/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb index 9b1eb9cd51a69b10c7e1d3791178e06b4decd3ed..50115ad431e642a2ad463d6d05e80da326a9ccbf 100644 --- a/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb +++ b/notebooks/Jungfrau/Jungfrau_retrieve_constants_precorrection_NBC.ipynb @@ -33,9 +33,9 @@ "karabo_id_control = \"\" # if control is on a different ID, set to empty string if it is the same a karabo-id\n", "\n", "# Parameters for calibration database.\n", + "use_dir_creation_date = True # use the creation data of the input dir for database queries\n", "cal_db_interface = \"tcp://max-exfl016:8017#8025\" # the database interface to use\n", "cal_db_timeout = 180000 # timeout on cal db requests\n", - "creation_time = \"\" # To overwrite the measured creation_time. Required Format: YYYY-MM-DD HR:MN:SC e.g. \"2022-06-28 13:00:00\"\n", "\n", "# Parameters affecting corrected data.\n", "relative_gain = True # do relative gain correction\n", @@ -55,20 +55,20 @@ "metadata": {}, "outputs": [], "source": [ - "from logging import warning\n", + "import datetime\n", + "from functools import partial\n", "\n", + "import multiprocessing\n", "from extra_data import RunDirectory\n", "from pathlib import Path\n", "\n", - "from calibration_client import CalibrationClient\n", - "from cal_tools.calcat_interface import JUNGFRAU_CalibrationData\n", "from cal_tools.jungfraulib import JungfrauCtrl\n", - "from cal_tools.restful_config import restful_config\n", - "from cal_tools.step_timing import StepTimer\n", "from cal_tools.tools import (\n", - " calcat_creation_time,\n", + " get_dir_creation_date,\n", + " get_from_db,\n", " CalibrationMetadata,\n", - ")" + ")\n", + "from iCalibrationDB import Conditions, Constants" ] }, { @@ -79,19 +79,16 @@ "source": [ "in_folder = Path(in_folder)\n", "out_folder = Path(out_folder)\n", - "\n", "metadata = CalibrationMetadata(metadata_folder or out_folder)\n", - "# Constant paths & timestamps are saved under retrieved-constants in calibration_metadata.yml\n", - "retrieved_constants = metadata[\"retrieved-constants\"] = dict()\n", - "\n", "run_folder = in_folder / f'r{run:04d}'\n", "run_dc = RunDirectory(run_folder)\n", "\n", "out_folder.mkdir(parents=True, exist_ok=True)\n", "\n", - "# Run's creation time:\n", - "creation_time = calcat_creation_time(in_folder, run, creation_time)\n", - "print(f\"Creation time: {creation_time}\")\n", + "creation_time = None\n", + "if use_dir_creation_date:\n", + " creation_time = get_dir_creation_date(in_folder, run)\n", + " print(f\"Using {creation_time} as creation time\")\n", "\n", "if karabo_id_control == \"\":\n", " karabo_id_control = karabo_id" @@ -108,6 +105,7 @@ "\n", "if mem_cells < 0:\n", " memory_cells, sc_start = ctrl_data.get_memory_cells()\n", + "\n", " mem_cells_name = \"single cell\" if memory_cells == 1 else \"burst\"\n", " print(f\"Run is in {mem_cells_name} mode.\\nStorage cell start: {sc_start:02d}\")\n", "else:\n", @@ -134,27 +132,57 @@ "metadata": {}, "outputs": [], "source": [ - "step_timer = StepTimer()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Connect to CalCat.\n", - "calcat_config = restful_config['calcat']\n", - "client = CalibrationClient(\n", - " base_api_url=calcat_config['base-api-url'],\n", - " use_oauth2=calcat_config['use-oauth2'],\n", - " client_id=calcat_config['user-id'],\n", - " client_secret=calcat_config['user-secret'],\n", - " user_email=calcat_config['user-email'],\n", - " token_url=calcat_config['token-url'],\n", - " refresh_url=calcat_config['refresh-url'],\n", - " auth_url=calcat_config['auth-url'],\n", - " scope='')" + "condition = Conditions.Dark.jungfrau(\n", + " memory_cells=memory_cells,\n", + " bias_voltage=bias_voltage,\n", + " integration_time=integration_time,\n", + " gain_setting=gain_setting,\n", + " gain_mode=gain_mode,\n", + ")\n", + "\n", + "def get_constants_for_module(mod: str):\n", + " \"\"\"Get calibration constants for given module for Jungfrau.\"\"\"\n", + " retrieval_function = partial(\n", + " get_from_db,\n", + " karabo_id=karabo_id,\n", + " karabo_da=mod,\n", + " cal_db_interface=cal_db_interface,\n", + " creation_time=creation_time,\n", + " timeout=cal_db_timeout,\n", + " verbosity=0,\n", + " meta_only=True,\n", + " load_data=False,\n", + " empty_constant=None\n", + " )\n", + "\n", + " mdata_dict = dict()\n", + " mdata_dict[\"constants\"] = dict()\n", + " constants = [\n", + " \"Offset\", \"BadPixelsDark\",\n", + " \"RelativeGain\", \"BadPixelsFF\",\n", + " ]\n", + " for cname in constants:\n", + " mdata_dict[\"constants\"][cname] = dict()\n", + " if not relative_gain and cname in [\"BadPixelsFF\", \"RelativeGain\"]:\n", + " continue\n", + " _, mdata = retrieval_function(\n", + " condition=condition,\n", + " constant=getattr(Constants.jungfrau, cname)(),\n", + " )\n", + " mdata_const = mdata.calibration_constant_version\n", + " const_mdata = mdata_dict[\"constants\"][cname]\n", + " # check if constant was successfully retrieved.\n", + " if mdata.comm_db_success:\n", + " const_mdata[\"file-path\"] = (\n", + " f\"{mdata_const.hdf5path}\" f\"{mdata_const.filename}\"\n", + " )\n", + " const_mdata[\"dataset-name\"] = mdata_const.h5path\n", + " const_mdata[\"creation-time\"] = f\"{mdata_const.begin_at}\"\n", + " mdata_dict[\"physical-detector-unit\"] = mdata_const.device_name\n", + " else:\n", + " const_mdata[\"file-path\"] = None\n", + " const_mdata[\"creation-time\"] = None\n", + " return mdata_dict, mod" ] }, { @@ -163,50 +191,20 @@ "metadata": {}, "outputs": [], "source": [ - "step_timer.start()\n", - "jf_cal = JUNGFRAU_CalibrationData(\n", - " detector_name=karabo_id,\n", - " sensor_bias_voltage=bias_voltage,\n", - " event_at=creation_time,\n", - " snapshot_at=creation_time,\n", - " modules=karabo_da,\n", - " memory_cells=memory_cells,\n", - " integration_time=integration_time,\n", - " gain_setting=gain_setting,\n", - " gain_mode=gain_mode,\n", - " client=client,\n", - ")\n", - "constant_names = [\"Offset10Hz\", \"BadPixelsDark10Hz\"]\n", - "\n", - "if relative_gain:\n", - " constant_names += [\"BadPixelsFF10Hz\", \"RelativeGain10Hz\"]\n", - "\n", - "jf_metadata = jf_cal.metadata(constant_names)\n", - "\n", - "# Add constants metadata in retrieved_constants dict.\n", - "for mod, ccv_dict in jf_metadata.items():\n", - " mod_dict = retrieved_constants.setdefault(mod, dict())\n", - " const_dict = mod_dict.setdefault(\"constants\", dict())\n", - " for cname, ccv_metadata in ccv_dict.items():\n", - " const_dict[cname] = {\n", - " \"path\": str(jf_cal.caldb_root / ccv_metadata[\"path\"]),\n", - " \"dataset\": ccv_metadata[\"dataset\"],\n", - " \"creation-time\": ccv_metadata[\"begin_validity_at\"],\n", - " }\n", - " mod_dict[\"physical-name\"] = ccv_metadata[\"physical_name\"]\n", + "# Constant paths are saved under retrieved-constants in calibration_metadata.yml\n", + "retrieved_constants = metadata.setdefault(\"retrieved-constants\", {})\n", + "# Avoid retrieving constants for available modules in calibration_metadata.yml\n", + "# This is used during reproducability.\n", + "query_karabo_da = []\n", + "for mod in karabo_da:\n", + " if mod in retrieved_constants.keys():\n", + " print(f\"Constant for {mod} already in \"\n", + " \"calibration_metadata.yml, won't query again.\")\n", + " continue\n", + " query_karabo_da.append(mod)\n", "\n", - "# Validate the constants availability and raise/warn correspondingly. \n", - "for mod, calibrations in jf_metadata.items():\n", - " missing_dark_constants = set(\n", - " c for c in [\"Offset10Hz\", \"BadPixelsDark10Hz\"] if c not in calibrations.keys())\n", - " missing_gain_constants = set(\n", - " c for c in [\"BadPixelsFF10Hz\", \"RelativeGain10Hz\"] if relative_gain and c not in calibrations.keys()) # noqa\n", - " if missing_dark_constants:\n", - " raise KeyError(\n", - " f\"Dark constants {missing_dark_constants} are not available for correction. Module: {mod}\") # noqa\n", - " if missing_gain_constants:\n", - " warning(\n", - " f\"Gain constants {missing_gain_constants} were not retrieved. Module: {mod}\")" + "with multiprocessing.Pool() as pool:\n", + " results = pool.map(get_constants_for_module, query_karabo_da)" ] }, { @@ -216,18 +214,19 @@ "outputs": [], "source": [ "timestamps = dict()\n", + "for md_dict, mod in results:\n", + " retrieved_constants[mod] = md_dict\n", "\n", - "for mod in karabo_da:\n", " module_timestamps = timestamps[mod] = dict()\n", " module_constants = retrieved_constants[mod]\n", "\n", " print(f\"Module: {mod}:\")\n", " for cname, mdata in module_constants[\"constants\"].items():\n", - " if hasattr(mdata[\"creation-time\"], \"strftime\"):\n", - " mdata[\"creation-time\"] = mdata[\"creation-time\"].strftime(\"%y-%m-%d %H:%M\")\n", - " print(f\"\\t{cname:.<12s}\", mdata[\"creation-time\"])\n", + " if hasattr(mdata[\"creation-time\"], 'strftime'):\n", + " mdata[\"creation-time\"] = mdata[\"creation-time\"].strftime('%y-%m-%d %H:%M')\n", + " print(f'{cname:.<12s}', mdata[\"creation-time\"])\n", "\n", - " for cname in constant_names:\n", + " for cname in [\"Offset\", \"BadPixelsDark\", \"RelativeGain\", \"BadPixelsFF\"]:\n", " if cname in module_constants[\"constants\"]:\n", " module_timestamps[cname] = module_constants[\"constants\"][cname][\"creation-time\"]\n", " else:\n",