From 3f47771dfef96e7e7a3df748888887e3d7416cf9 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Wed, 19 Jul 2023 12:45:10 +0200
Subject: [PATCH] move back unneeded moved cell and error out for thresholds as
 well

---
 .../AGIPD/AGIPD_Correct_and_Verify.ipynb      | 80 ++++++++++---------
 src/cal_tools/agipdlib.py                     |  5 +-
 2 files changed, 44 insertions(+), 41 deletions(-)

diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
index dd59d0d1e..3717cc4e9 100644
--- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
+++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
@@ -334,6 +334,37 @@
     "    print(f'Processing all valid trains')"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# set everything up filewise\n",
+    "mapped_files, _, total_sequences, _, _ =  map_modules_from_folder(\n",
+    "    str(in_folder), run, path_template, karabo_da, sequences\n",
+    ")\n",
+    "file_list = []\n",
+    "\n",
+    "# ToDo: Split table over pages\n",
+    "print(f\"Processing a total of {total_sequences} sequence files in chunks of {n_cores_files}\")\n",
+    "table = []\n",
+    "ti = 0\n",
+    "for k, files in mapped_files.items():\n",
+    "    i = 0\n",
+    "    for f in list(files.queue):\n",
+    "        file_list.append(f)\n",
+    "        if i == 0:\n",
+    "            table.append((ti, k, i, f))\n",
+    "        else:\n",
+    "            table.append((ti, \"\", i,  f))\n",
+    "        i += 1\n",
+    "        ti += 1\n",
+    "md = display(Latex(tabulate.tabulate(table, tablefmt='latex',\n",
+    "                                     headers=[\"#\", \"module\", \"# module\", \"file\"])))\n",
+    "file_list = sorted(file_list, key=lambda name: name[-10:])"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -484,14 +515,6 @@
     "    print(f'Photon energy for rounding: {photon_energy:.3f} keV')"
    ]
   },
-  {
-   "attachments": {},
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "## Retrieving constants"
-   ]
-  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -527,34 +550,11 @@
    ]
   },
   {
-   "cell_type": "code",
-   "execution_count": null,
+   "attachments": {},
+   "cell_type": "markdown",
    "metadata": {},
-   "outputs": [],
    "source": [
-    "# set everything up filewise\n",
-    "mapped_files, _, total_sequences, _, _ =  map_modules_from_folder(\n",
-    "    str(in_folder), run, path_template, karabo_da, sequences\n",
-    ")\n",
-    "file_list = []\n",
-    "\n",
-    "# ToDo: Split table over pages\n",
-    "print(f\"Processing a total of {total_sequences} sequence files in chunks of {n_cores_files}\")\n",
-    "table = []\n",
-    "ti = 0\n",
-    "for k, files in mapped_files.items():\n",
-    "    i = 0\n",
-    "    for f in list(files.queue):\n",
-    "        file_list.append(f)\n",
-    "        if i == 0:\n",
-    "            table.append((ti, k, i, f))\n",
-    "        else:\n",
-    "            table.append((ti, \"\", i,  f))\n",
-    "        i += 1\n",
-    "        ti += 1\n",
-    "md = display(Latex(tabulate.tabulate(table, tablefmt='latex',\n",
-    "                                     headers=[\"#\", \"module\", \"# module\", \"file\"])))\n",
-    "file_list = sorted(file_list, key=lambda name: name[-10:])"
+    "## Retrieving constants"
    ]
   },
   {
@@ -567,6 +567,7 @@
     "agipd_corr.allocate_constants(modules, (3, mem_cells_db, 512, 128))\n",
     "\n",
     "step_timer.start()\n",
+    "# Instantiate agipd_cal with the read operating conditions.\n",
     "agipd_cal = AGIPD_CalibrationData(\n",
     "    detector_name=karabo_id,\n",
     "    modules=karabo_da,\n",
@@ -582,10 +583,10 @@
     "    caldb_root=Path(cal_db_root),\n",
     ")\n",
     "\n",
+    "# Prepare lists of expected calibrations\n",
     "dark_constants = [\"Offset\", \"Noise\", \"BadPixelsDark\"]\n",
     "if not gain_mode:  # Adaptive gain\n",
     "    dark_constants.append(\"ThresholdsDark\")\n",
-    "\n",
     "gain_constants = []\n",
     "if any(agipd_corr.pc_bools):\n",
     "    gain_constants += [\"SlopesPC\", \"BadPixelsPC\"]\n",
@@ -623,14 +624,19 @@
     "# Validate constants availability and exclude modules with no offsets.\n",
     "for da, calibrations in agipd_metadata.items():\n",
     "    mod = modules[karabo_da.index(da)]\n",
-    "    if {\"Offset\"} - set(calibrations):\n",
+    "    # Constants to error out for when missing.\n",
+    "    error_missing_constants = {\"Offset\"}\n",
+    "    if gain_mode:\n",
+    "        error_missing_constants |= {\"ThresholdsDark\"}\n",
+    "\n",
+    "    if error_missing_constants - set(calibrations):\n",
     "        warning(f\"Offset constant is not available to correct {da}.\")\n",
     "        mapped_files.remove(module_index_to_qm(mod))\n",
     "        karabo_da.drop(da)\n",
     "        modules.drop(mod)\n",
     "\n",
     "    warn_missing_constants = set(dark_constants + gain_constants)\n",
-    "    warn_missing_constants.remove(\"Offset\")\n",
+    "    warn_missing_constants -= error_missing_constants\n",
     "    warn_missing_constants = warn_missing_constants - set(calibrations)\n",
     "    if warn_missing_constants:\n",
     "        warning(f\"Constants {warn_missing_constants} were not retrieved for {da}.\")\n",
diff --git a/src/cal_tools/agipdlib.py b/src/cal_tools/agipdlib.py
index 2e50df04d..60e245ab4 100644
--- a/src/cal_tools/agipdlib.py
+++ b/src/cal_tools/agipdlib.py
@@ -1180,10 +1180,7 @@ class AgipdCorrections:
             calgs.transpose_constant(
                 self.noise[module_idx], cons_data["Noise"], **calgs_opts)
 
-        if (
-            self.gain_mode is AgipdGainMode.ADAPTIVE_GAIN and
-            "ThresholdsDark" in cons_data
-        ):
+        if self.gain_mode is AgipdGainMode.ADAPTIVE_GAIN:
             calgs.transpose_constant(self.thresholds[module_idx],
                                      cons_data["ThresholdsDark"][..., :3],
                                      **calgs_opts)
-- 
GitLab