From ed217404af459dc6ffee9e1966826802ebb5ff31 Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas.kluyver@xfel.eu>
Date: Mon, 17 Feb 2025 16:31:30 +0100
Subject: [PATCH] Improve LPD darks per-memory-cell plots

---
 notebooks/LPD/LPDChar_Darks_NBC.ipynb | 93 ++++++++++++++++-----------
 1 file changed, 56 insertions(+), 37 deletions(-)

diff --git a/notebooks/LPD/LPDChar_Darks_NBC.ipynb b/notebooks/LPD/LPDChar_Darks_NBC.ipynb
index 11f1dd73a..3689c6035 100644
--- a/notebooks/LPD/LPDChar_Darks_NBC.ipynb
+++ b/notebooks/LPD/LPDChar_Darks_NBC.ipynb
@@ -1195,18 +1195,17 @@
     "    for gain in range(3):\n",
     "        display(Markdown('### Mean over pixels - {} gain'.format(gain_names[gain])))\n",
     "\n",
-    "        fig = plt.figure(figsize=(9,11))\n",
+    "        fig, axs = plt.subplots(nrows=3, figsize=(9,11))\n",
     "\n",
-    "        for iconst, const in enumerate(res[qm]):\n",
-    "\n",
-    "            ax = fig.add_subplot(311+iconst)\n",
+    "        # Offset, Noise\n",
+    "        non_mask_consts = [c for c in res[qm] if c != 'BadPixelsDark']\n",
     "\n",
+    "        for iconst, const in enumerate(non_mask_consts):\n",
     "            data = res[qm][const][:,:,:510,gain]\n",
-    "            if const == 'BadPixelsDark':\n",
-    "                data[data>0] = 1.0\n",
     "\n",
+    "            # Mask out bad pixels\n",
     "            dataBP = np.copy(data)\n",
-    "            dataBP[badpix_g[qm][:,:,:510,gain]>0] = -10\n",
+    "            dataBP[badpix_g[qm][:,:,:510,gain]>0] = np.nan\n",
     "\n",
     "            data = np.nanmean(data, axis=(0,1))\n",
     "            dataBP = np.nanmean(dataBP, axis=(0,1))\n",
@@ -1214,43 +1213,63 @@
     "            d = [{'y': data,\n",
     "                  'x': np.arange(data.shape[0]),\n",
     "                  'drawstyle': 'steps-mid',\n",
-    "                  'label' : 'All data'\n",
-    "                 }\n",
-    "                ]\n",
-    "\n",
-    "            if const != 'BadPixelsDark':\n",
-    "                d.append({'y': dataBP,\n",
+    "                  'linestyle': 'dotted',\n",
+    "                  'label' : 'All data',\n",
+    "                 },\n",
+    "                 {'y': dataBP,\n",
     "                  'x': np.arange(data.shape[0]),\n",
     "                  'drawstyle': 'steps-mid',\n",
-    "                  'label' : 'good pixels only'\n",
-    "                 })\n",
-    "                y_title = \"{} value [ADU]\".format(const)\n",
-    "                title = \"{} value, {} gain\".format(const, gain_names[gain])\n",
-    "            else:\n",
-    "                y_title = \"Fraction of Bad Pixels\"\n",
-    "                title = \"Fraction of Bad Pixels, {} gain\".format(gain_names[gain])\n",
+    "                  'label' : 'Good pixels only',\n",
+    "                 },\n",
+    "            ]\n",
+    "            data_min = np.nanmin([data, dataBP])\n",
+    "            data_max = np.nanmax([data[20:], dataBP[20:]])\n",
     "\n",
-    "            data_min = np.min([data, dataBP])if const != 'BadPixelsDark' else np.min([data])\n",
-    "            data_max = np.max([data[20:], dataBP[20:]])\n",
     "            data_dif = data_max - data_min\n",
     "\n",
-    "            local_max = np.max([data[200:300], dataBP[200:300]])\n",
+    "            # Ensure the line in the main plot isn't covered by the inset\n",
+    "            local_max = np.nanmax([data[90:310], dataBP[90:310]])\n",
     "            frac = 0.35\n",
     "            new_max = (local_max - data_min*(1-frac))/frac\n",
-    "            new_max = np.max([data_max, new_max])\n",
-    "\n",
-    "            _ = simplePlot(d, figsize=(10,10), aspect=2, xrange=(-12, 510),\n",
-    "                              x_label = 'Memory Cell ID', \n",
-    "                              y_label=y_title, use_axis=ax,\n",
-    "                              title=title,\n",
-    "                              title_position=[0.5, 1.15],  \n",
-    "                              inset='xy-coord-right', inset_x_range=(0,20), inset_indicated=True,\n",
-    "                              inset_labeled=True, inset_coord=[0.2,0.5,0.6,0.95],\n",
-    "                                inset_lw = 1.0, y_range = [data_min-data_dif*0.05, new_max+data_dif*0.05],\n",
-    "                              y_log=False, legend='outside-top-ncol2-frame', legend_size='18%',\n",
-    "                                 legend_pad=0.00)\n",
-    "\n",
-    "            plt.tight_layout(pad=1.08, h_pad=0.35)\n",
+    "            new_max = np.nanmax([data_max, new_max])\n",
+    "\n",
+    "            simplePlot(d, use_axis=axs[iconst], aspect=2,\n",
+    "                       x_range=(-12, 510),\n",
+    "                       y_range=(data_min-data_dif*0.05, new_max+data_dif*0.05),\n",
+    "                       x_label=\"Memory Cell ID\",\n",
+    "                       y_label=f\"{const} value [ADU]\",\n",
+    "                       title=f\"{const} value, {gain_names[gain]} gain\",\n",
+    "                       title_position=[0.5, 1.15],\n",
+    "                       inset='xy-coord-right', inset_x_range=(0,20), inset_indicated=True,\n",
+    "                       inset_labeled=True, inset_coord=[0.2, 0.5, 0.6, 0.95], inset_lw=1.0,\n",
+    "                       legend='top-right', legend_size='18%', legend_pad=0.00)\n",
+    "\n",
+    "        # BadPixelsDark\n",
+    "        data = res[qm]['BadPixelsDark'][:,:,:510,gain].copy()\n",
+    "        data[data > 0] = 1.0\n",
+    "        data = np.mean(data, axis=(0,1))\n",
+    "\n",
+    "        d = [{'y': data, 'x': np.arange(data.shape[0]), 'drawstyle': 'steps-mid'}]\n",
+    "        data_min, data_max = 0., 1.\n",
+    "\n",
+    "        # Ensure the line in the main plot isn't covered by the inset\n",
+    "        local_max = np.max(data[90:310])\n",
+    "        frac = 0.35\n",
+    "        new_max = (local_max - data_min*(1-frac))/frac\n",
+    "        new_max = np.max([data_max, new_max])\n",
+    "\n",
+    "        simplePlot(d, use_axis=axs[2], aspect=2,\n",
+    "                   x_label='Memory Cell ID',\n",
+    "                   y_label=\"Fraction of Bad Pixels\",\n",
+    "                   x_range=(-12, 510),\n",
+    "                   y_range=(data_min-0.05, new_max+0.05),\n",
+    "                   title=f\"Fraction of Bad Pixels, {gain_names[gain]} gain\",\n",
+    "                   title_position=[0.5, 1.15],\n",
+    "                   inset='xy-coord-right', inset_x_range=(0,20), inset_indicated=True,\n",
+    "                   inset_labeled=True, inset_coord=[0.2,0.5,0.6,0.95], inset_lw=1.0,\n",
+    "        )\n",
+    "\n",
+    "        fig.tight_layout(pad=1.08, h_pad=0.35)\n",
     "\n",
     "        plt.show()"
    ]
-- 
GitLab