From 67de8cc9996774b53a36314e3a649dbcf45cf916 Mon Sep 17 00:00:00 2001
From: David Hammer <dhammer@mailbox.org>
Date: Mon, 8 Mar 2021 17:39:42 +0100
Subject: [PATCH] Moving index_to_qm to tools

---
 cal_tools/cal_tools/tools.py                  |  9 +++++++-
 .../Characterize_AGIPD_Gain_Darks_NBC.ipynb   | 22 +++++--------------
 2 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/cal_tools/cal_tools/tools.py b/cal_tools/cal_tools/tools.py
index d4015a558..a6aa3f5e8 100644
--- a/cal_tools/cal_tools/tools.py
+++ b/cal_tools/cal_tools/tools.py
@@ -78,7 +78,7 @@ def map_modules_from_folder(in_folder, run, path_template, karabo_da,
     sequences_qm = {}
     for inset in karabo_da:
         module_idx = int(inset[-2:])
-        name = f"Q{module_idx // 4 + 1}M{module_idx % 4 + 1}"
+        name = module_index_to_qm(module_idx)
         module_files[name] = Queue()
         sequences_qm[name] = 0
         mod_ids[name] = module_idx
@@ -673,6 +673,13 @@ def get_constant_from_db_and_time(karabo_id: str, karabo_da: str,
         return data, None
 
 
+def module_index_to_qm(index: int, total_modules: int = 16):
+    """Maps module index (0-indexed) to quadrant + module string (1-indexed)"""
+    modules_per_quad = total_modules // 4
+    quad, mod = divmod(index, modules_per_quad)
+    return f"Q{quad+1}M{mod+1}"
+
+
 class CalibrationMetadata(dict):
     """Convenience class: dictionary stored in metadata YAML file
 
diff --git a/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb b/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
index bec0026e2..d715b75b2 100644
--- a/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
+++ b/notebooks/AGIPD/Characterize_AGIPD_Gain_Darks_NBC.ipynb
@@ -103,7 +103,7 @@
     "                                show_overview, show_processed_modules)\n",
     "from cal_tools.tools import (get_dir_creation_date, get_from_db,\n",
     "                             get_pdu_from_db, get_random_db_interface,\n",
-    "                             get_report, map_gain_stages,\n",
+    "                             get_report, map_gain_stages, module_index_to_qm,\n",
     "                             run_prop_seq_from_path, save_const_to_h5,\n",
     "                             send_to_db)\n",
     "from iCalibrationDB import Conditions, Constants, Detectors"
@@ -251,16 +251,6 @@
     "print(f\"Will process a total of {total_sequences} files.\")"
    ]
   },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "def index_to_qm(i: int):\n",
-    "    return f\"Q{i//4+1}M{i%4+1}\""
-   ]
-  },
   {
    "cell_type": "markdown",
    "metadata": {},
@@ -410,7 +400,7 @@
     "for gg, (gain, mapped_files) in enumerate(gain_mapped_files.items()):\n",
     "    dones = []\n",
     "    for i in modules:\n",
-    "        qm = index_to_qm(i)\n",
+    "        qm = module_index_to_qm(i)\n",
     "        if qm in mapped_files and not mapped_files[qm].empty():\n",
     "            fname_in = mapped_files[qm].get()\n",
     "            print(\"Process file: \", fname_in)\n",
@@ -426,7 +416,7 @@
     "    all_cells.append(thiscell)\n",
     "    all_acq_rate.append(thisacq)\n",
     "    for i in modules:\n",
-    "        qm = index_to_qm(i)\n",
+    "        qm = module_index_to_qm(i)\n",
     "        if qm not in offset_g:\n",
     "            offset_g[qm] = np.zeros((offset.shape[0], offset.shape[1], offset.shape[2], 3))\n",
     "            noise_g[qm] = np.zeros_like(offset_g[qm])\n",
@@ -497,7 +487,7 @@
    "source": [
     "res = OrderedDict()\n",
     "for i in modules:\n",
-    "    qm = index_to_qm(i)\n",
+    "    qm = module_index_to_qm(i)\n",
     "    res[qm] = {\n",
     "        'Offset': offset_g[qm],\n",
     "        'Noise': noise_g[qm],\n",
@@ -530,7 +520,7 @@
     "# Create the modules dict of karabo_das and PDUs\n",
     "qm_dict = OrderedDict()\n",
     "for i, k_da in zip(modules, karabo_da):\n",
-    "    qm = index_to_qm(i)\n",
+    "    qm = module_index_to_qm(i)\n",
     "    qm_dict[qm] = {\n",
     "        \"karabo_da\": k_da,\n",
     "        \"db_module\": \"\"\n",
@@ -642,7 +632,7 @@
    "source": [
     "mnames=[]\n",
     "for i in modules:\n",
-    "    qm = index_to_qm(i)\n",
+    "    qm = module_index_to_qm(i)\n",
     "    mnames.append(qm)\n",
     "    display(Markdown(f'## Position of the module {qm} and its ASICs##'))\n",
     "show_processed_modules(dinstance, constants=None, mnames=mnames, mode=\"position\")"
-- 
GitLab