From d358045dde7574918b519e49f5f3cc06fb30b36c Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Mon, 15 Jan 2024 12:04:11 +0100
Subject: [PATCH] Improve dark summary plots - Improve vmin to show constants
 maps rangs using percentile - Improve BadPixels heatmaps - Add table for
 Jungfrau Badpixels

---
 .../Jungfrau/Jungfrau_darks_Summary_NBC.ipynb | 74 +++++++++++++------
 1 file changed, 50 insertions(+), 24 deletions(-)

diff --git a/notebooks/Jungfrau/Jungfrau_darks_Summary_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_darks_Summary_NBC.ipynb
index 6da1df8b9..2488be2b5 100644
--- a/notebooks/Jungfrau/Jungfrau_darks_Summary_NBC.ipynb
+++ b/notebooks/Jungfrau/Jungfrau_darks_Summary_NBC.ipynb
@@ -56,9 +56,13 @@
     "matplotlib.use(\"agg\")\n",
     "%matplotlib inline\n",
     "\n",
+    "import tabulate\n",
+    "from IPython.display import Latex, Markdown, display\n",
+    "from XFELDetAna.plotting.simpleplot import simplePlot\n",
+    "\n",
+    "from cal_tools.enums import BadPixels\n",
     "from cal_tools.plotting import init_jungfrau_geom, show_processed_modules_jungfrau\n",
-    "from cal_tools.tools import CalibrationMetadata\n",
-    "from XFELDetAna.plotting.simpleplot import simplePlot"
+    "from cal_tools.tools import CalibrationMetadata"
    ]
   },
   {
@@ -188,22 +192,7 @@
    "source": [
     "gainstages = 3\n",
     "gain_names = [\"High Gain\", \"Medium Gain\", \"Low Gain\" ]\n",
-    "const_range = {\n",
-    "    \"Offset\": [(0, 8000), (8000, 16000), (8000, 16000)],\n",
-    "    \"Noise\": [(0., 50.), (0., 50.), (0., 50.)],\n",
-    "    \"BadPixelsDark\": [(0., 5.), (0., 5.), (0., 5.)],\n",
-    "}\n",
-    "# vmin and vmax are different for Offset for fixed gain constants. \n",
-    "if fixed_gain:\n",
-    "    const_range[\"Offset\"] = [(0, 8000), (0, 8000), (0, 8000)]\n",
-    "\n",
-    "diff_const_range = {\n",
-    "    \"Offset\": [(0, 500), (0, 500), (0, 500)],\n",
-    "    \"Noise\": [(0., 5.), (0., 5.), (0., 5.)],\n",
-    "    \"BadPixelsDark\": [(0., 5.), (0., 5.), (0., 5.)],\n",
-    "}\n",
-    "percentage_range = (0, 100)\n",
-    "perc_const_range = {c: [percentage_range]*3 for c in dark_constants}\n",
+    "\n",
     "gs = gridspec.GridSpec(2, 4)\n",
     "\n",
     "axes = {\n",
@@ -214,7 +203,6 @@
     "        \"label\": \"ADCu\",\n",
     "        \"title\": \"{}\",\n",
     "        \"location\": \"right\",\n",
-    "        \"range\": const_range,\n",
     "        },\n",
     "    \"ax1\": {\n",
     "        \"gs\": gs[1, :2],\n",
@@ -223,7 +211,6 @@
     "        \"label\": \"ADCu\",\n",
     "        \"location\": \"left\",\n",
     "        \"title\": \"Difference with previous {}\",\n",
-    "        \"range\": diff_const_range,\n",
     "        },\n",
     "    \"ax2\": {\n",
     "        \"gs\": gs[1, 2:],\n",
@@ -232,11 +219,32 @@
     "        \"label\": \"%\",\n",
     "        \"location\": \"right\",\n",
     "        \"title\": \"Difference with previous {} %\",\n",
-    "        \"range\": perc_const_range,\n",
     "        },\n",
     "    }"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def badpx(constant_name):\n",
+    "    return True if \"bad\" in constant_name.lower() else False\n",
+    "\n",
+    "\n",
+    "def bp_entry(bp):\n",
+    "    return [f\"{bp.name:<30s}\", f\"{bp.value:032b}\", f\"{int(bp.value)}\"]\n",
+    "\n",
+    "\n",
+    "badpixels = [\n",
+    "        BadPixels.OFFSET_OUT_OF_THRESHOLD,\n",
+    "        BadPixels.NOISE_OUT_OF_THRESHOLD,\n",
+    "        BadPixels.OFFSET_NOISE_EVAL_ERROR,\n",
+    "        BadPixels.GAIN_THRESHOLDING_ERROR,\n",
+    "    ]"
+   ]
+  },
   {
    "cell_type": "markdown",
    "metadata": {},
@@ -253,6 +261,15 @@
    "outputs": [],
    "source": [
     "for cname, const in curr_constants.items():\n",
+    "    if badpx(cname):\n",
+    "        table = [bp_entry(bp) for bp in badpixels]\n",
+    "        display(Markdown(\"\"\"**The bad pixel** mask is encoded as a bit mask.\"\"\"))\n",
+    "        display(Latex(\n",
+    "            tabulate.tabulate(\n",
+    "                table,\n",
+    "                tablefmt='latex',\n",
+    "                headers=[\"Name\", \"bit value\", \"integer value\"]\n",
+    "            )))\n",
     "\n",
     "    # Prepare the stacked mean of constant,\n",
     "    # the difference with the previous constant\n",
@@ -282,15 +299,24 @@
     "            if cname in exculded_constants and axname != \"ax0\":\n",
     "                break\n",
     "            ax = fig.add_subplot(axv[\"gs\"])\n",
-    "            vmin, vmax = axv[\"range\"][cname][gain]\n",
+    "\n",
+    "            if axname == \"ax2\":  # Difference in percentage\n",
+    "               vmin, vmax = (0, 100)\n",
+    "            elif badpx(cname):\n",
+    "                vmin, vmax = (0, 5)\n",
+    "            else:\n",
+    "                vmin, vmax = np.percentile(data_to_plot[axname], [5, 95])\n",
+    "\n",
     "            geom.plot_data(\n",
     "                data_to_plot[axname],\n",
-    "                vmin=vmin, vmax=vmax, ax=ax, \n",
+    "                vmin=vmin,\n",
+    "                vmax=vmax,\n",
+    "                ax=ax, \n",
     "                colorbar={\n",
     "                    \"shrink\": axv[\"shrink\"],\n",
     "                    \"pad\": axv[\"pad\"],\n",
     "                    \"location\": axv[\"location\"],\n",
-    "                }\n",
+    "                },\n",
     "            )\n",
     "\n",
     "            colorbar = ax.images[0].colorbar\n",
-- 
GitLab