From ba7cb2e531c1cb4dc549848507918ffade697357 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 18 Jan 2024 07:31:29 +0100
Subject: [PATCH] update correction NB - Change how receiver templates and
 affixes are used - Refactor and correct data sources list from receivers to
 data_sources - Apply related refactors based on last item.

---
 .../Gotthard2/Correction_Gotthard2_NBC.ipynb  | 36 ++++++++++---------
 1 file changed, 19 insertions(+), 17 deletions(-)

diff --git a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
index a3a53018b..6a73b6a04 100644
--- a/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
+++ b/notebooks/Gotthard2/Correction_Gotthard2_NBC.ipynb
@@ -66,7 +66,7 @@
     "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",
     "corr_data_source = \"{}/CORR/{}:daqOutput\"  # Correction data source. filled with karabo_id and correction receiver\n",
-    "corr_receiver = \"\"  # The name of the correction receiver name. Leave empty for using the same receiver name for the 50um GH2 or the first(Master) receiver for the 25um GH2.\n",
+    "corr_receiver = \"\"  # The receiver name of the corrected data. Leave empty for using the same receiver name for the 50um GH2 or the first(Master) receiver for the 25um GH2.\\n\",\n",
     "\n",
     "# Parameters for calibration database.\n",
     "cal_db_interface = \"tcp://max-exfl-cal001:8016#8025\"  # the database interface to use.\n",
@@ -236,14 +236,16 @@
     "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'*{r}').all_sources)[0] for r in receiver_names]\n",
+    "receiver_names = [f\"*{receiver_template.format(x)}*\" for x in receiver_affixes]\n",
+    "data_sources = list(run_dc.select(receiver_names).all_sources)\n",
+    "\n",
     "if not corr_receiver:\n",
+    "    # This part assumes this data_source structure: '{karabo_id}/DET/{receiver_name}:{output_channel}'\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",
+    "        corr_receiver = data_sources[0].split(\"/\")[-1].split(\":\")[0][:-2]\n",
     "    else:\n",
-    "        corr_receiver = receivers[0].split(\"/\")[-1].split(\":\")[0]"
+    "        corr_receiver = data_sources[0].split(\"/\")[-1].split(\":\")[0]\n",
+    "    print(f\"Using {corr_receiver} as a receiver name for the corrected data.\")"
    ]
   },
   {
@@ -410,24 +412,24 @@
     "\n",
     "    out_file = out_folder / raw_file.name.replace(\"RAW\", \"CORR\")\n",
     "    # Select module INSTRUMENT sources and deselect empty trains.\n",
-    "    dc = H5File(raw_file).select(receivers, require_all=True)\n",
+    "    dc = H5File(raw_file).select(data_sources, require_all=True)\n",
     "\n",
     "    n_trains = len(dc.train_ids)\n",
     "\n",
     "    # Initialize GH2 data and gain arrays to store in corrected files.\n",
     "    if gh2_detector == \"25um\":\n",
-    "        data_stored = np.zeros((dc[receivers[0], \"data.adc\"].shape[:2] + (1280 * 2,)), dtype=np.float32)\n",
-    "        gain_stored = np.zeros((dc[receivers[0], \"data.adc\"].shape[:2] + (1280 * 2,)), dtype=np.uint8)\n",
+    "        data_stored = np.zeros((dc[data_sources[0], \"data.adc\"].shape[:2] + (1280 * 2,)), dtype=np.float32)\n",
+    "        gain_stored = np.zeros((dc[data_sources[0], \"data.adc\"].shape[:2] + (1280 * 2,)), dtype=np.uint8)\n",
     "    else:\n",
     "        data_stored = None\n",
     "        gain_stored = None\n",
     "\n",
-    "    for i, (receiver, mod) in enumerate(zip(receivers, karabo_da)):\n",
+    "    for i, (src, mod) in enumerate(zip(data_sources, karabo_da)):\n",
     "        step_timer.start()\n",
-    "        print(f\"Correcting {receiver} for {raw_file}\")\n",
+    "        print(f\"Correcting {src} for {raw_file}\")\n",
     "\n",
-    "        data = dc[receiver, \"data.adc\"].ndarray()\n",
-    "        gain = dc[receiver, \"data.gain\"].ndarray()\n",
+    "        data = dc[src, \"data.adc\"].ndarray()\n",
+    "        gain = dc[src, \"data.gain\"].ndarray()\n",
     "        step_timer.done_step(\"Preparing raw data\")\n",
     "        dshape = data.shape\n",
     "\n",
@@ -446,7 +448,7 @@
     "        data_corr[np.isinf(data_corr)] = np.nan\n",
     "\n",
     "        # Create CORR files and add corrected data sections.\n",
-    "        image_counts = dc[receiver, \"data.adc\"].data_counts(labelled=False)\n",
+    "        image_counts = dc[src, \"data.adc\"].data_counts(labelled=False)\n",
     "\n",
     "        if gh2_detector == \"25um\":\n",
     "            data_stored[..., i::2] = data_corr.copy()\n",
@@ -490,7 +492,7 @@
     "        # stored in the corrected file.\n",
     "        for field in [\"bunchId\", \"memoryCell\", \"frameNumber\", \"timestamp\"]:\n",
     "                outp_source.create_key(\n",
-    "                    f\"data.{field}\", data=dc[receiver, f\"data.{field}\"].ndarray(),\n",
+    "                    f\"data.{field}\", data=dc[src, f\"data.{field}\"].ndarray(),\n",
     "                    chunks=(chunks_data, data_corr.shape[1])\n",
     "            )\n",
     "        outp_source.create_compressed_key(f\"data.mask\", data=mask)\n",
@@ -542,9 +544,9 @@
     "    mod_dcs[corr_data_source][\"train_raw_data\"] = np.zeros((data_corr.shape[1], 1280 * 2), dtype=np.float32)\n",
     "    mod_dcs[corr_data_source][\"train_raw_gain\"] = np.zeros((data_corr.shape[1], 1280 * 2), dtype=np.uint8)\n",
     "\n",
-    "for i, rec_mod in enumerate(receivers):\n",
+    "for i, src in enumerate(data_sources):\n",
     "    with H5File(first_seq_raw) as in_dc:\n",
-    "        train_dict = in_dc.train_from_id(tid)[1][rec_mod]\n",
+    "        train_dict = in_dc.train_from_id(tid)[1][src]\n",
     "        if gh2_detector == \"25um\":\n",
     "            mod_dcs[corr_data_source][\"train_raw_data\"][..., i::2] = train_dict[\"data.adc\"]\n",
     "            mod_dcs[corr_data_source][\"train_raw_gain\"][..., i::2] = train_dict[\"data.gain\"]\n",
-- 
GitLab