From 714a2eea50db95cbccd3d5585d047b01737de7a4 Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas.kluyver@xfel.eu>
Date: Thu, 29 Jun 2023 17:39:07 +0200
Subject: [PATCH] Rework LPD Mini cell order condition to match LPD-1M again

---
 .../LPDMini/LPD_Mini_Char_Darks_NBC.ipynb     | 23 +++++-------
 notebooks/LPDMini/LPD_Mini_Correct.ipynb      | 35 ++++++++++---------
 2 files changed, 27 insertions(+), 31 deletions(-)

diff --git a/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb b/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb
index a375fe263..38706bbff 100644
--- a/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb
+++ b/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb
@@ -22,12 +22,12 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "in_folder = \"/gpfs/exfel/exp/FXE/202231/p900316/raw/\" # path to input data, required\n",
+    "in_folder = \"/gpfs/exfel/exp/FXE/202330/p900319/raw/\" # path to input data, required\n",
     "out_folder = \"/gpfs/exfel/data/scratch/kluyvert/darks-lpdmini\" # path to output to, required\n",
     "metadata_folder = \"\"  # Directory containing calibration_metadata.yml when run by xfel-calibrate\n",
-    "run_high = 10 # run number in which high gain data was recorded, required\n",
-    "run_med = 11 # run number in which medium gain data was recorded, required\n",
-    "run_low = 12 # run number in which low gain data was recorded, required\n",
+    "run_high = 3 # run number in which high gain data was recorded, required\n",
+    "run_med = 4 # run number in which medium gain data was recorded, required\n",
+    "run_low = 5 # run number in which low gain data was recorded, required\n",
     "\n",
     "karabo_id = \"FXE_DET_LPD_MINI\" # karabo karabo_id\n",
     "karabo_da = ['']  # a list of data aggregators names with module number, e.g. 'LPDMINI00/2', Default [''] for selecting all data aggregators\n",
@@ -53,7 +53,7 @@
     "min_trains = 370  # minimum number of trains needed for each gain stage\n",
     "high_res_badpix_3d = False # plot bad-pixel summary in high resolution\n",
     "test_for_normality = False # permorm normality test\n",
-    "inject_cell_order = False  # Include memory cell order as part of the detector condition\n",
+    "inject_cell_order = 'auto'  # Include memory cell order as part of the detector condition (auto = only if cells wrap around)\n",
     "operation_mode = \"\"  # This in given as a default input argument through the webservice in production. Don't remove!"
    ]
   },
@@ -88,6 +88,7 @@
     "\n",
     "from cal_tools.calcat_interface import CalCatApi\n",
     "from cal_tools.enums import BadPixels\n",
+    "from cal_tools.lpdlib import make_cell_order_condition\n",
     "from cal_tools.plotting import plot_badpix_3d\n",
     "from cal_tools.restful_config import calibration_client\n",
     "from cal_tools.tools import (\n",
@@ -207,7 +208,7 @@
     "    cell_ids = cell_ids[skip_first_ntrains : skip_first_ntrains + ntrains]\n",
     "    \n",
     "    if len(data.train_ids) < min_trains:\n",
-    "        raise Exception(f\"Run {run_path} only contains {len(data.train_ids)} trains, but {min_ntrains} required\")\n",
+    "        raise Exception(f\"Run {run_path} only contains {len(data.train_ids)} trains, but {min_trains} required\")\n",
     "\n",
     "    im = data.ndarray()\n",
     "    if im.ndim > 3:\n",
@@ -354,10 +355,7 @@
     "    old_const[mod_num] = {}\n",
     "    old_mdata[mod_num] = {}\n",
     "\n",
-    "    if inject_cell_order:\n",
-    "        mem_cell_order = \",\".join([str(c) for c in cellid_pattern]) + \",\"\n",
-    "    else:\n",
-    "        mem_cell_order = None\n",
+    "    mem_cell_order = make_cell_order_condition(inject_cell_order, cellid_pattern)\n",
     "\n",
     "    # mod_num is from 1 to 8, so b_v_0 applies to odd numbers\n",
     "    bias_voltage = bias_voltage_0 if (mod_num % 2 == 1) else bias_voltage_1\n",
@@ -432,10 +430,7 @@
     "    db_module = pdu_name_by_da[karabo_da_m]\n",
     "    print(f\"Storing constants for PDU {db_module}\")\n",
     "\n",
-    "    if inject_cell_order:\n",
-    "        mem_cell_order = \",\".join([str(c) for c in cellid_pattern]) + \",\"\n",
-    "    else:\n",
-    "        mem_cell_order = None\n",
+    "    mem_cell_order = make_cell_order_condition(inject_cell_order, cellid_pattern)\n",
     "\n",
     "    # Do not store empty constants\n",
     "    # In case of 0 trains data_g is initiated with nans and never refilled.\n",
diff --git a/notebooks/LPDMini/LPD_Mini_Correct.ipynb b/notebooks/LPDMini/LPD_Mini_Correct.ipynb
index efc8fca0c..769e5121b 100644
--- a/notebooks/LPDMini/LPD_Mini_Correct.ipynb
+++ b/notebooks/LPDMini/LPD_Mini_Correct.ipynb
@@ -45,7 +45,7 @@
     "bias_voltage_1 = -1 # bias voltage for minis 2, 4, 6, 8; Setting -1 will read the value from files\n",
     "capacitor = '5pF'  # Capacitor setting: 5pF or 50pF\n",
     "photon_energy = 9.3  # Photon energy in keV.\n",
-    "use_cell_order = False  # Whether to use memory cell order as a detector condition (not stored for older constants)\n",
+    "use_cell_order = 'auto'  # Whether to use memory cell order as a detector condition (auto = used only when memory cells wrap around)\n",
     "\n",
     "# Correction parameters\n",
     "offset_corr = True  # Offset correction.\n",
@@ -101,7 +101,7 @@
     "\n",
     "from cal_tools.calcat_interface import CalCatApi\n",
     "from cal_tools.lpdalgs import correct_lpd_frames\n",
-    "from cal_tools.lpdlib import get_mem_cell_order\n",
+    "from cal_tools.lpdlib import get_mem_cell_pattern, make_cell_order_condition\n",
     "from cal_tools.tools import CalibrationMetadata, calcat_creation_time\n",
     "from cal_tools.files import DataFile\n",
     "from cal_tools.restful_config import calibration_client"
@@ -247,22 +247,23 @@
     "\n",
     "base_condition = [\n",
     "    # Bias voltage added below as it differs by module\n",
-    "    dict(parameter_id=15, value=capacitor),  # Feedback capacitor\n",
-    "    dict(parameter_id=7, value=mem_cells),  # Memory cells\n",
-    "    dict(parameter_id=13, value=256),  # Pixels X\n",
-    "    dict(parameter_id=14, value=32),   # Pixels Y\n",
+    "    dict(parameter_name='Feedback capacitor', value=capacitor),\n",
+    "    dict(parameter_name='Memory cells', value=mem_cells),\n",
+    "    dict(parameter_name='Pixels X', value=256),\n",
+    "    dict(parameter_name='Pixels Y', value=32),\n",
     "]\n",
-    "if use_cell_order:\n",
+    "dark_condition = base_condition.copy()\n",
+    "if use_cell_order != 'never':\n",
     "    # Read the order of memory cells used\n",
     "    raw_data = xd.DataCollection.from_paths([e[0] for e in data_to_process])\n",
-    "    cell_ids_pattern_s = get_mem_cell_order(raw_data, det_inp_sources)\n",
-    "    print(\"Memory cells order:\", cell_ids_pattern_s)\n",
-    "\n",
-    "    dark_condition = base_condition + [\n",
-    "        dict(parameter_id=30, value=cell_ids_pattern_s),  # Memory cell order\n",
-    "    ]\n",
-    "else:\n",
-    "    dark_condition = base_condition.copy()\n",
+    "    cell_ids_pattern_s = make_cell_order_condition(\n",
+    "        use_cell_order, get_mem_cell_pattern(raw_data, det_inp_sources)\n",
+    "    )\n",
+    "    if cell_ids_pattern_s is not None:        \n",
+    "        print(\"Memory cells order:\", cell_ids_pattern_s)\n",
+    "        dark_condition.append(\n",
+    "            dict(parameter_name='Memory cell order', value=cell_ids_pattern_s),\n",
+    "        )\n",
     "\n",
     "illuminated_calibrations = {}\n",
     "if rel_gain:\n",
@@ -274,7 +275,7 @@
     "    illuminated_calibrations[42] = 'GainAmpMap'  # np.float32\n",
     "\n",
     "illuminated_condition = base_condition + [\n",
-    "    dict(parameter_id=3, value=photon_energy),  # Source energy\n",
+    "    dict(parameter_name='Source Energy', value=photon_energy),\n",
     "]"
    ]
   },
@@ -298,7 +299,7 @@
     "        mod_num = int(karabo_da_m.split('/')[-1])\n",
     "        # mod_num is from 1 to 8, so b_v_0 applies to odd numbers\n",
     "        bias_voltage = bias_voltage_0 if mod_num % 2 == 1 else bias_voltage_1\n",
-    "        condition_w_voltage = [dict(parameter_id=1, value=bias_voltage)] + condition\n",
+    "        condition_w_voltage = [dict(parameter_name='Sensor Bias Voltage', value=bias_voltage)] + condition\n",
     "        \n",
     "        resp = CalibrationConstantVersion.get_closest_by_time_by_detector_conditions(\n",
     "            calcat_client, karabo_id, list(calibrations.keys()),\n",
-- 
GitLab