From 48d28055ddc0e50d89cf58f9054a82a80c75f7cc Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Wed, 10 Nov 2021 14:28:12 +0100
Subject: [PATCH] [AGIPD][DARK] FIX: Make sure to process darks out of sequence
 file with the highest trainIds

---
 .../Characterize_AGIPD_Gain_Darks_NBC.ipynb   | 25 +++++++++++--------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb b/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
index ff66d2a70..dd69492ab 100644
--- a/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
+++ b/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
@@ -330,15 +330,19 @@
    "source": [
     "# set everything up filewise\n",
     "os.makedirs(out_folder, exist_ok=True)\n",
-    "gain_mapped_files, total_files, total_file_size = map_gain_stages(\n",
+    "gain_mapped_files, _, total_file_size = map_gain_stages(\n",
     "    in_folder, offset_runs, path_template, karabo_da, sequences\n",
     ")\n",
-    "print(f\"Will process a total of {total_files} files ({total_file_size:.02f} GB).\")\n",
+    "# TODO: Keep this commented out to use it later again, this is false information at the moment.\n",
+    "# print(f\"Will process a total of {total_files} files ({total_file_size:.02f} GB).\")\n",
     "\n",
-    "inp = []\n",
+    "# TODO: Remove all of this nonsense with Extra-data.\n",
+    "inp = [None] * 3 * len(modules)\n",
     "for gain_index, (gain, qm_file_map) in enumerate(gain_mapped_files.items()):\n",
     "    gain_input = []\n",
     "    for module_index in modules:\n",
+    "        max_n_imgs = 0\n",
+    "        inp_idx = gain_index  * len(modules) + module_index\n",
     "        qm = module_index_to_qm(module_index)\n",
     "        if qm not in qm_file_map:\n",
     "            print(f\"Did not find files for {qm}\")\n",
@@ -349,17 +353,18 @@
     "            # TODO: remove after using EXtra-data to read files\n",
     "            # and skip empty trains.\n",
     "            with h5py.File(filename, \"r\") as fin:\n",
-    "                if fin[h5path.format(module_index)+\"/trainId\"].shape[0] != 0:\n",
-    "                    print(f\"Process {filename} for {qm}\")\n",
-    "                    gain_input.append((filename, module_index, gain_index))\n",
-    "                else:\n",
-    "                    print(f\"Do not process {filename} because it is empty.\")\n",
-    "    if not gain_input:\n",
+    "                n_imgs = fin[h5path.format(module_index)+\"/trainId\"].shape[0]\n",
+    "                if n_imgs != 0 and n_imgs > max_n_imgs:\n",
+    "                    inp[inp_idx] = (filename, module_index, gain_index)\n",
+    "                    max_n_imgs = n_imgs\n",
+    "        print(f\"Process {inp[inp_idx][0]} for {qm}\")\n",
+    "    if inp[inp_idx] is None:\n",
     "        raise ValueError(\n",
     "            \"No images to process for run: \"\n",
     "            f\"{[v for v in offset_runs.values()][gain_index]}\"\n",
     "        )\n",
-    "    inp += gain_input"
+    "inp = [x for x in inp if x is not None]\n",
+    "total_files = len(inp)"
    ]
   },
   {
-- 
GitLab