From 700abf84c789ce2f7b60dac433be191d38027184 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 7 Jul 2022 11:13:06 +0200
Subject: [PATCH] rebase to master, fix conflicts, and rearrange plots

---
 ...Jungfrau_Gain_Correct_and_Verify_NBC.ipynb | 162 +++++++++++-------
 1 file changed, 99 insertions(+), 63 deletions(-)

diff --git a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
index 492907b29..d7bf6d8b0 100644
--- a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
+++ b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
@@ -40,6 +40,7 @@
     "overwrite = True  # set to True if existing data should be overwritten\n",
     "relative_gain = True  # do relative gain correction\n",
     "limit_images = 0  # ONLY FOR TESTING. process only first N images, Use 0 to process all.\n",
+    "cell_id_preview = 15  # cell Id used for preview in single-shot plots\n",
     "\n",
     "# Parameters for retrieving calibration constants\n",
     "manual_slow_data = False  # if true, use manually entered bias_voltage and integration_time values\n",
@@ -90,6 +91,7 @@
     "from cal_tools.tools import (\n",
     "    get_constant_from_db_and_time,\n",
     "    get_dir_creation_date,\n",
+    "    get_pdu_from_db,\n",
     "    map_seq_files,\n",
     "    write_compressed_frames,\n",
     ")\n",
@@ -505,6 +507,18 @@
     "        gain = seq_dc[instrument_src_kda, \"data.gain\"].ndarray()\n",
     "        memcells = seq_dc[instrument_src_kda, \"data.memoryCell\"].ndarray()\n",
     "\n",
+    "        if memory_cells > 1:\n",
+    "            # For plotting, assuming that memory cells are sorted the same for all trains.\n",
+    "            found_cells = memcells[0] == cell_id_preview\n",
+    "            if any(found_cells):\n",
+    "                cell_idx_preview = np.where(found_cells)[0][0]\n",
+    "            else:\n",
+    "                print(f\"WARNING: The selected cell_id_preview {cell_id_preview} is not available in burst mode.\"\n",
+    "                f\"Previewing cell `{memcells[0]}`.\")\n",
+    "                cell_idx_preview = 0\n",
+    "        else:\n",
+    "            cell_idx_preview = 0\n",
+    "\n",
     "        context.map(correct_train, data)\n",
     "        step_timer.done_step(f'Correction time.')\n",
     "\n",
@@ -605,28 +619,28 @@
    "source": [
     "first_seq = 0 if sequences == [-1] else sequences[0]\n",
     "\n",
-    "# Reading CORR data for plotting.\n",
-    "jf_corr = components.JUNGFRAU(\n",
-    "    RunDirectory(out_folder, f'*{run}*S{first_seq:05d}*'),\n",
-    "    detector_name=karabo_id,\n",
-    ")\n",
-    "tid, jf_corr_data = next(iter(jf_corr.trains(require_all=True)))\n",
+    "with RunDirectory(out_folder, f'*{run}*S{first_seq:05d}*') as corr_dc:\n",
+    "    # Reading CORR data for plotting.\n",
+    "    jf_corr = components.JUNGFRAU(\n",
+    "        corr_dc,\n",
+    "        detector_name=karabo_id,\n",
+    "    )\n",
+    "    tid, jf_corr_data = next(iter(jf_corr.trains(require_all=True)))\n",
     "\n",
-    "# Show cell_idx 0 TODO: replace with a choice of cell_idx_preview\n",
     "# Shape = [modules, cells, trains, x, y]\n",
-    "corrected = jf_corr.get_array(\"data.adc\")[:, :, 0, ...].values # pick cell_idx 0 for all trains.\n",
-    "corrected_train = jf_corr_data[\"data.adc\"][:, 0, ...].values  # loose the train axis.\n",
+    "corrected = jf_corr.get_array(\"data.adc\")[:, :, cell_idx_preview, ...].values\n",
+    "corrected_train = jf_corr_data[\"data.adc\"][:, cell_idx_preview, ...].values  # loose the train axis.\n",
     "\n",
-    "mask = jf_corr.get_array(\"data.mask\")[:, :, 0, ...].values\n",
-    "mask_train = jf_corr_data[\"data.mask\"][:, 0, ...].values\n",
+    "mask = jf_corr.get_array(\"data.mask\")[:, :, cell_idx_preview, ...].values\n",
+    "mask_train = jf_corr_data[\"data.mask\"][:, cell_idx_preview, ...].values\n",
     "\n",
-    "# Reading RAW data for plotting.\n",
-    "jf_raw = components.JUNGFRAU(\n",
-    "    RunDirectory(f'{in_folder}/r{run:04d}/', f'*S{first_seq:05d}*'),\n",
-    "    detector_name=karabo_id,\n",
-    ")\n",
+    "with RunDirectory(f'{in_folder}/r{run:04d}/', f'*S{first_seq:05d}*') as raw_dc:\n",
+    "\n",
+    "    # Reading RAW data for plotting.\n",
+    "    jf_raw = components.JUNGFRAU(raw_dc, detector_name=karabo_id)\n",
+    "\n",
+    "    raw = jf_raw.get_array(\"data.adc\")[:, :, cell_idx_preview, ...].values\n",
     "\n",
-    "raw = jf_raw.get_array(\"data.adc\")[:, :, 0, ...].values\n",
     "raw_train = jf_raw.select_trains(\n",
     "    by_id[[tid]]).get_array(\"data.adc\")[:, 0, 0, ...].values\n",
     "\n",
@@ -650,47 +664,13 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "def do_2d_plot(data, edges, y_axis, x_axis, title):\n",
-    "    fig = plt.figure(figsize=(10, 10))\n",
-    "    ax = fig.add_subplot(111)\n",
-    "    extent = [\n",
-    "        np.min(edges[1]),\n",
-    "        np.max(edges[1]),\n",
-    "        np.min(edges[0]),\n",
-    "        np.max(edges[0]),\n",
-    "    ]\n",
-    "\n",
-    "    im = ax.imshow(\n",
-    "        data[::-1, :],\n",
-    "        extent=extent,\n",
-    "        aspect=\"auto\",\n",
-    "        norm=LogNorm(vmin=1, vmax=np.max(data))\n",
-    "    )\n",
-    "    ax.set_xlabel(x_axis)\n",
-    "    ax.set_ylabel(y_axis)\n",
-    "    ax.set_title(title)\n",
-    "    cb = fig.colorbar(im)\n",
-    "    cb.set_label(\"Counts\")"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": [
-    "for i, mod in enumerate(karabo_da): \n",
-    "    h, ex, ey = np.histogram2d(\n",
-    "        raw[i].flatten(),\n",
-    "        gain[i].flatten(),\n",
-    "        bins=[100, 4],\n",
-    "        range=[[0, 10000], [0, 4]],\n",
-    "    )\n",
-    "    do_2d_plot(\n",
-    "        h, (ex, ey),\n",
-    "        \"Signal (ADU)\",\n",
-    "        \"Gain Bit Value (high gain=0[00], medium gain=1[01], low gain=3[11])\",\n",
-    "        f\"Module {mod}\")"
+    "db_modules = get_pdu_from_db(\n",
+    "    karabo_id=karabo_id,\n",
+    "    karabo_da=karabo_da,\n",
+    "    constant=Constants.jungfrau.Offset(),\n",
+    "    condition=condition,\n",
+    "    cal_db_interface=cal_db_interface,\n",
+    "    snapshot_at=creation_time)"
    ]
   },
   {
@@ -718,7 +698,7 @@
     "    cmap=\"jet\",\n",
     "    colorbar={'shrink': 1, 'pad': 0.01},\n",
     ")\n",
-    "ax.set_title(f'{karabo_id}', size=18)\n",
+    "ax.set_title(f'{karabo_id} - Mean RAW', size=18)\n",
     "plt.show()"
    ]
   },
@@ -747,7 +727,7 @@
     "    cmap=\"jet\",\n",
     "    colorbar={'shrink': 1, 'pad': 0.01},\n",
     ")\n",
-    "ax.set_title(f'{karabo_id}', size=18)\n",
+    "ax.set_title(f'{karabo_id} - Mean CORRECTED', size=18)\n",
     "\n",
     "plt.show()"
    ]
@@ -769,11 +749,67 @@
     "    cmap=\"jet\",\n",
     "    colorbar={'shrink': 1, 'pad': 0.01},\n",
     ")\n",
-    "ax.set_title(f'{karabo_id}', size=18)\n",
+    "ax.set_title(f'{karabo_id} - CORRECTED train: {tid}', size=18)\n",
     "\n",
     "plt.show()"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def do_2d_plot(data, edges, y_axis, x_axis, title):\n",
+    "    fig = plt.figure(figsize=(10, 10))\n",
+    "    ax = fig.add_subplot(111)\n",
+    "    extent = [\n",
+    "        np.min(edges[1]),\n",
+    "        np.max(edges[1]),\n",
+    "        np.min(edges[0]),\n",
+    "        np.max(edges[0]),\n",
+    "    ]\n",
+    "\n",
+    "    im = ax.imshow(\n",
+    "        data[::-1, :],\n",
+    "        extent=extent,\n",
+    "        aspect=\"auto\",\n",
+    "        norm=LogNorm(vmin=1, vmax=np.max(data))\n",
+    "    )\n",
+    "    ax.set_xlabel(x_axis)\n",
+    "    ax.set_ylabel(y_axis)\n",
+    "    ax.set_title(title)\n",
+    "    cb = fig.colorbar(im)\n",
+    "    cb.set_label(\"Counts\")"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Gain Bit Value"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "for i, (pdu, mod) in enumerate(zip(db_modules, karabo_da)): \n",
+    "    h, ex, ey = np.histogram2d(\n",
+    "        raw[i].flatten(),\n",
+    "        gain[i].flatten(),\n",
+    "        bins=[100, 4],\n",
+    "        range=[[0, 10000], [0, 4]],\n",
+    "    )\n",
+    "    do_2d_plot(\n",
+    "        h, (ex, ey),\n",
+    "        \"Signal (ADU)\",\n",
+    "        \"Gain Bit Value (high gain=0[00], medium gain=1[01], low gain=3[11])\",\n",
+    "        f\"Module {mod} ({pdu})\")"
+   ]
+  },
   {
    "cell_type": "markdown",
    "metadata": {},
@@ -787,7 +823,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "for i, mod in enumerate(karabo_da): \n",
+    "for i, (pdu, mod) in enumerate(zip(db_modules, karabo_da)): \n",
     "    fig, axs = plt.subplots(nrows=2, ncols=1, figsize=(18, 10))\n",
     "    corrected_flatten = corrected[i].flatten()\n",
     "    for ax, hist_range in zip(axs, [(-100, 1000), (-1000, 10000)]):\n",
@@ -799,7 +835,7 @@
     "        )\n",
     "        l = ax.set_xlabel(\"Signal (keV)\")\n",
     "        l = ax.set_ylabel(\"Counts\")\n",
-    "        _ = ax.set_title(f'Module {mod}')"
+    "        _ = ax.set_title(f'Module {mod} ({pdu})')"
    ]
   },
   {
-- 
GitLab