From 7f49ae6ec95212eaf1e4b93896ae6ad75c88b271 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 18 Jan 2024 07:27:58 +0100
Subject: [PATCH] updated dark notebook - Different way in using receiver
 templates and affixes - Correct naming data source parameter name from
 receivers to data_sources - apply refactors based on this

---
 .../Characterize_Darks_Gotthard2_NBC.ipynb    | 24 ++++++++++---------
 .../Gotthard2/Correction_Gotthard2_NBC.ipynb  |  5 ++--
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb b/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb
index 3908e95b3..f3b3f1ea3 100644
--- a/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb
+++ b/notebooks/Gotthard2/Characterize_Darks_Gotthard2_NBC.ipynb
@@ -33,8 +33,8 @@
     "karabo_id = \"SA1_XTD9_HIREX\"  # karabo prefix of Gotthard-II devices\n",
     "karabo_da = [\"\"]  # data aggregators\n",
     "receiver_template = \"RECEIVER{}\"  # receiver template used to read INSTRUMENT keys.\n",
-    "receiver_suffixes = [\"\"]  # Exact receiver names. This can be used for GH2 25um when each receiver has a unique name. Leave empty to use the receiver_template instead.\n",
-    "control_template = \"GOTTHARD2_CTRL\"  # control template used to read CONTROL keys.\n",
+    "receiver_affixes = [\"\"]  # The affix to format into the receiver template to be able to load the correct receiver name from the data.\n",
+    "control_template = \"CONTROL\"  # control template used to read CONTROL keys.\n",
     "ctrl_source_template = '{}/DET/{}'  # template for control source name (filled with karabo_id_control)\n",
     "karabo_id_control = \"\"  # Control karabo ID. Set to empty string to use the karabo-id\n",
     "\n",
@@ -128,8 +128,8 @@
    "source": [
     "run_dc = RunDirectory(in_folder / f\"r{run_high:04d}\")\n",
     "file_loc = f\"proposal:{run_dc.run_metadata()['proposalNumber']} runs:{run_high} {run_med} {run_low}\"  # noqa\n",
-    "receivers = [\n",
-    "    list(run_dc.select(f'*{receiver_template}{r}').all_sources)[0] for r in receiver_suffixes]"
+    "receiver_names = [f\"*{receiver_template.format(x)}*\" for x in receiver_affixes]\n",
+    "data_sources = list(run_dc.select(receiver_names).all_sources)"
    ]
   },
   {
@@ -234,6 +234,8 @@
    "source": [
     "def specify_trains_to_process(\n",
     "    img_key_data: \"extra_data.KeyData\",\n",
+    "    run_num: int,\n",
+    "    src: str,\n",
     "):\n",
     "    \"\"\"Specify total number of trains to process.\n",
     "    Based on given min_trains and max_trains, if given.\n",
@@ -245,7 +247,7 @@
     "    n_trains = img_key_data.shape[0]\n",
     "    all_trains = len(img_key_data.train_ids)\n",
     "    print(\n",
-    "        f\"{receiver} has {all_trains - n_trains} \"\n",
+    "        f\"{src} for run {run} has {all_trains - n_trains}\"\n",
     "        f\"trains with empty frames out of {all_trains} trains\"\n",
     "    )\n",
     "\n",
@@ -290,7 +292,7 @@
     "def convert_train(wid, index, tid, d):\n",
     "    \"\"\"Convert a Gotthard2 train from 12bit to 10bit.\"\"\"\n",
     "    gotthard2algs.convert_to_10bit(\n",
-    "        d[receiver][\"data.adc\"], lut, data_10bit[index, ...]\n",
+    "        d[src][\"data.adc\"], lut, data_10bit[index, ...]\n",
     "    )"
    ]
   },
@@ -311,7 +313,7 @@
     "    np.uint16\n",
     ")\n",
     "empty_lut = np.stack(1280 * [np.stack([empty_lut] * 2)], axis=0)\n",
-    "for mod, receiver in zip(karabo_da, receivers):\n",
+    "for mod, src in zip(karabo_da, data_sources):\n",
     "\n",
     "    # Retrieve LUT constant\n",
     "    lut, time = get_constant_from_db_and_time(\n",
@@ -335,10 +337,10 @@
     "\n",
     "    for run_num, [gain, run_dc] in run_dcs_dict.items():\n",
     "        step_timer.start()\n",
-    "        n_trains = specify_trains_to_process(run_dc[receiver, \"data.adc\"])\n",
+    "        n_trains = specify_trains_to_process(run_dc[src, \"data.adc\"], run_num, src)\n",
     "\n",
     "        # Select requested number of trains to process.\n",
-    "        dc = run_dc.select(receiver, require_all=True).select_trains(\n",
+    "        dc = run_dc.select(src, require_all=True).select_trains(\n",
     "            np.s_[:n_trains]\n",
     "        )\n",
     "\n",
@@ -347,7 +349,7 @@
     "        step_timer.start()\n",
     "        # Convert 12bit data to 10bit\n",
     "        data_10bit = context.alloc(\n",
-    "            shape=dc[receiver, \"data.adc\"].shape, dtype=np.float32\n",
+    "            shape=dc[src, \"data.adc\"].shape, dtype=np.float32\n",
     "        )\n",
     "        context.map(convert_train, dc)\n",
     "        step_timer.done_step(\"convert to 10bit\")\n",
@@ -375,7 +377,7 @@
     "        context.map(offset_noise_cell, (even_data, odd_data))\n",
     "\n",
     "        # Split even and odd gain data.\n",
-    "        data_gain = dc[receiver, \"data.gain\"].ndarray()\n",
+    "        data_gain = dc[src, \"data.gain\"].ndarray()\n",
     "        even_gain = data_gain[:, 20::2, :]\n",
     "        odd_gain = data_gain[:, 21::2, :]\n",
     "        raw_g = 3 if gain == 2 else gain\n",
diff --git a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
index 22f829ba8..a3a53018b 100644
--- a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
+++ b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
@@ -61,7 +61,7 @@
     "karabo_id = \"DETLAB_25UM_GH2\"  # karabo prefix of Gotthard-II devices\n",
     "karabo_da = [\"\"]  # data aggregators\n",
     "receiver_template = \"RECEIVER{}\"  # receiver template used to read INSTRUMENT keys.\n",
-    "receiver_suffixes = [\"\"]\n",
+    "receiver_affixes = [\"\"]  # The affix to format into the receiver template to be able to load the correct receiver name from the data.\n",
     "control_template = \"CONTROL\"  # control template used to read CONTROL keys.\n",
     "ctrl_source_template = \"{}/DET/{}\"  # template for control source name (filled with karabo_id_control)\n",
     "karabo_id_control = \"\"  # Control karabo ID. Set to empty string to use the karabo-id\n",
@@ -236,8 +236,9 @@
     "print(f\"Process modules: {db_modules} for run {run}\")\n",
     "\n",
     "# Create the correction receiver name.\n",
+    "receiver_names = [receiver_template.format(x) for x in receiver_affixes]\n",
     "receivers = [\n",
-    "    list(run_dc.select(f'*{receiver_template}{r}').all_sources)[0] for r in receiver_suffixes]\n",
+    "    list(run_dc.select(f'*{r}').all_sources)[0] for r in receiver_names]\n",
     "if not corr_receiver:\n",
     "    if gh2_detector == \"25um\":  # For 25um use virtual karabo_das for CALCAT data mapping.\n",
     "        corr_receiver = receivers[0].split(\"/\")[-1].split(\":\")[0][:-2]\n",
-- 
GitLab