From 5a67eca9c24c03ac51691081f8c80cb1b9bb0e92 Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas.kluyver@xfel.eu>
Date: Fri, 16 Dec 2022 16:57:31 +0100
Subject: [PATCH] Add helper function to get LPD memory cell order

---
 notebooks/LPD/LPD_Correct_Fast.ipynb          | 30 +++++++---------
 ...LPD_retrieve_constants_precorrection.ipynb | 36 +++++++++----------
 2 files changed, 28 insertions(+), 38 deletions(-)

diff --git a/notebooks/LPD/LPD_Correct_Fast.ipynb b/notebooks/LPD/LPD_Correct_Fast.ipynb
index 3cfe25abd..443afdcdc 100644
--- a/notebooks/LPD/LPD_Correct_Fast.ipynb
+++ b/notebooks/LPD/LPD_Correct_Fast.ipynb
@@ -106,6 +106,7 @@
     "from extra_data.components import LPD1M\n",
     "\n",
     "from cal_tools.lpdalgs import correct_lpd_frames\n",
+    "from cal_tools.lpdlib import get_mem_cell_order\n",
     "from cal_tools.tools import CalibrationMetadata, calcat_creation_time\n",
     "from cal_tools.files import DataFile\n",
     "from cal_tools.restful_config import restful_config"
@@ -194,21 +195,6 @@
     "# Obtain and prepare calibration constants"
    ]
   },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "# Read the order of memory cells used\n",
-    "raw_data = xd.DataCollection.from_paths([e[1] for e in data_to_process])\n",
-    "\n",
-    "cell_ids = raw_data[det_inp_sources[0], 'image.cellId'].drop_empty_trains()\n",
-    "cell_ids_pattern = np.squeeze(cell_ids[0].ndarray())\n",
-    "cell_ids_pattern_s = \",\".join([str(c) for c in cell_ids_pattern]) + \",\"\n",
-    "print(\"Memory cells order:\", cell_ids_pattern_s)"
-   ]
-  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -277,9 +263,17 @@
     "        dict(parameter_id=13, value=256),  # Pixels X\n",
     "        dict(parameter_id=14, value=256),  # Pixels Y\n",
     "    ]\n",
-    "    dark_condition = base_condition + ([\n",
-    "        dict(parameter_id=142, value=cell_ids_pattern_s),  # Memory cell order\n",
-    "    ] if use_cell_order else [])\n",
+    "    if use_cell_order:\n",
+    "        # Read the order of memory cells used\n",
+    "        raw_data = xd.DataCollection.from_paths([e[1] 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=142, value=cell_ids_pattern_s),  # Memory cell order\n",
+    "        ]\n",
+    "    else:\n",
+    "        dark_condition = base_condition.copy()\n",
     "\n",
     "    illuminated_calibrations = {\n",
     "        20: 'BadPixelsFF',  # np.uint32\n",
diff --git a/notebooks/LPD/LPD_retrieve_constants_precorrection.ipynb b/notebooks/LPD/LPD_retrieve_constants_precorrection.ipynb
index 9066c3c46..5d2ef0579 100644
--- a/notebooks/LPD/LPD_retrieve_constants_precorrection.ipynb
+++ b/notebooks/LPD/LPD_retrieve_constants_precorrection.ipynb
@@ -56,6 +56,7 @@
     "from calibration_client.modules import CalibrationConstantVersion\n",
     "import extra_data as xd\n",
     "\n",
+    "from cal_tools.lpdlib import get_mem_cell_order\n",
     "from cal_tools.tools import (\n",
     "    CalibrationMetadata,\n",
     "    calcat_creation_time,\n",
@@ -85,23 +86,10 @@
     "    if not modules or modules == [-1]:\n",
     "        modules = list(range(16))\n",
     "\n",
-    "    karabo_da = [f'LPD{i:02d}' for i in modules]   "
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "# Read the order of memory cells used\n",
-    "raw_data = xd.RunDirectory(Path(in_folder, f'r{run:04d}'))\n",
-    "\n",
-    "src = input_source.format(karabo_id=karabo_id, module_index=int(karabo_da[0][-2:]))\n",
-    "cell_ids = raw_data[src, 'image.cellId'].drop_empty_trains()\n",
-    "cell_ids_pattern = np.squeeze(cell_ids[0].ndarray())\n",
-    "cell_ids_pattern_s = \",\".join([str(c) for c in cell_ids_pattern]) + \",\"\n",
-    "print(\"Memory cells order:\", cell_ids_pattern_s)"
+    "    karabo_da = [f'LPD{i:02d}' for i in modules]\n",
+    "\n",
+    "# List of detector sources.\n",
+    "det_inp_sources = [input_source.format(karabo_id=karabo_id, module_index=int(da[-2:])) for da in karabo_da]"
    ]
   },
   {
@@ -142,9 +130,17 @@
     "    dict(parameter_id=13, value=256),  # Pixels X\n",
     "    dict(parameter_id=14, value=256),  # Pixels Y\n",
     "]\n",
-    "dark_condition = base_condition + ([\n",
-    "    dict(parameter_id=142, value=cell_ids_pattern_s),  # Memory cell order\n",
-    "] if use_cell_order else [])\n",
+    "if use_cell_order:\n",
+    "    # Read the order of memory cells used\n",
+    "    raw_data = xd.RunDirectory(Path(in_folder, f'r{run:04d}'))\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=142, value=cell_ids_pattern_s),  # Memory cell order\n",
+    "    ]\n",
+    "else:\n",
+    "    dark_condition = base_condition.copy()\n",
     "\n",
     "illuminated_calibrations = {\n",
     "    20: 'BadPixelsFF',\n",
-- 
GitLab