From 0b939c7dcdc6a9b02cf403e04634ee16ab2e309c Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Mon, 5 Feb 2024 15:59:28 +0100
Subject: [PATCH] remove jet color map, remove hard coded number for plot
 scales

---
 .../AGIPD/AGIPD_Correct_and_Verify.ipynb      | 92 ++++++++++---------
 1 file changed, 48 insertions(+), 44 deletions(-)

diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
index c4431088e..8c1b09452 100644
--- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
+++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
@@ -1031,10 +1031,15 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "hist, bins_x, bins_y = calgs.histogram2d(raw[:,0,...].flatten().astype(np.float32),\n",
-    "                                         raw[:,1,...].flatten().astype(np.float32),\n",
-    "                                         bins=(100, 100),\n",
-    "                                         range=[[4000, 8192], [4000, 8192]])\n",
+    "hist, bins_x, bins_y = calgs.histogram2d(\n",
+    "    raw[:,0,...].flatten().astype(np.float32),\n",
+    "    raw[:,1,...].flatten().astype(np.float32),\n",
+    "    bins=(100, 100),\n",
+    "    range=[\n",
+    "        np.percentile(raw[:, 0, ...].astype(np.float32), [0.02, 99.8]),\n",
+    "        np.percentile(raw[:, 1, ...].astype(np.float32), [0.02, 99.8]),\n",
+    "        ],\n",
+    "    )\n",
     "do_2d_plot(hist, (bins_x, bins_y), \"Signal (ADU)\", \"Analogue gain (ADU)\")\n",
     "do_3d_plot(hist, (bins_x, bins_y), \"Signal (ADU)\", \"Analogue gain (ADU)\")"
    ]
@@ -1054,9 +1059,14 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "hist, bins_x, bins_y = calgs.histogram2d(corrected.flatten().astype(np.float32),\n",
-    "                                         gains.flatten().astype(np.float32), bins=(100, 3),\n",
-    "                                         range=[[-50, 8192], [0, 3]])\n",
+    "hist, bins_x, bins_y = calgs.histogram2d(\n",
+    "    corrected.flatten().astype(np.float32),\n",
+    "    gains.flatten().astype(np.float32), bins=(100, 3),\n",
+    "    range=[\n",
+    "        np.percentile(corrected.flatten().astype(np.float32), [0, 100]),\n",
+    "        [0, 3]\n",
+    "        ],\n",
+    "    )\n",
     "do_2d_plot(hist, (bins_x, bins_y), \"Signal (ADU)\", \"Gain bit value\")"
    ]
   },
@@ -1087,25 +1097,21 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "pulse_range = [np.min(pulseId[pulseId>=0]), np.max(pulseId[pulseId>=0])]\n",
-    "\n",
+    "pulse_range = np.percentile(pulseId.astype(np.float32), [0, 100])\n",
     "# Modify pulse_range, if only one pulse is selected.\n",
     "if pulse_range[0] == pulse_range[1]:\n",
     "    pulse_range = [0, pulse_range[1]+int(acq_rate)]\n",
     "\n",
     "mean_data = np.nanmean(corrected, axis=(2, 3))\n",
-    "hist, bins_x, bins_y = calgs.histogram2d(mean_data.flatten().astype(np.float32),\n",
-    "                                      pulseId.flatten().astype(np.float32),\n",
-    "                                      bins=(100, int(pulse_range[1])),\n",
-    "                                      range=[[-50, 1000], pulse_range])\n",
-    "\n",
-    "do_2d_plot(hist, (bins_x, bins_y), \"Signal (ADU)\", \"Pulse id\")\n",
-    "do_3d_plot(hist, (bins_x, bins_y), \"Signal (ADU)\", \"Pulse id\")\n",
-    "\n",
-    "hist, bins_x, bins_y = calgs.histogram2d(mean_data.flatten().astype(np.float32),\n",
-    "                                      pulseId.flatten().astype(np.float32),\n",
-    "                                      bins=(100,  int(pulse_range[1])),\n",
-    "                                      range=[[-50, 200000], pulse_range])\n",
+    "hist, bins_x, bins_y = calgs.histogram2d(\n",
+    "    mean_data.flatten().astype(np.float32),\n",
+    "    pulseId.flatten().astype(np.float32),\n",
+    "    bins=(100, int(pulse_range[1])),\n",
+    "    range=[\n",
+    "        np.percentile(mean_data.astype(np.float32), [0, 100]),\n",
+    "        pulse_range\n",
+    "    ],\n",
+    ")\n",
     "\n",
     "do_2d_plot(hist, (bins_x, bins_y), \"Signal (ADU)\", \"Pulse id\")\n",
     "do_3d_plot(hist, (bins_x, bins_y), \"Signal (ADU)\", \"Pulse id\")"
@@ -1143,7 +1149,7 @@
     "fig = plt.figure(figsize=(10, 10))\n",
     "corrected_ave = np.nansum(corrected, axis=(2, 3))\n",
     "plt.scatter(corrected_ave.flatten()/10**6, blshift.flatten(), s=0.9)\n",
-    "plt.xlim(-1, 1000)\n",
+    "plt.xlim(np.percentile(corrected_ave.flatten()/10**6, [2, 98]))\n",
     "plt.grid()\n",
     "plt.xlabel('Illuminated corrected [MADU] ')\n",
     "_ = plt.ylabel('Estimated baseline shift [ADU]')"
@@ -1176,8 +1182,8 @@
     "    fig = plt.figure(figsize=(20, 10))\n",
     "    ax = fig.add_subplot(111)\n",
     "    data = np.mean(raw[slice(*cell_sel.crange), 0, ...], axis=0)\n",
-    "    vmin, vmax = get_range(data, 5)\n",
-    "    ax = geom.plot_data_fast(data, ax=ax, cmap=\"jet\", vmin=vmin, vmax=vmax)\n",
+    "    vmin, vmax = np.percentile(data, [5, 95])\n",
+    "    ax = geom.plot_data_fast(data, ax=ax, vmin=vmin, vmax=vmax)\n",
     "else:\n",
     "    print(\"Skipping mean RAW preview for single memory cell, \"\n",
     "          f\"see single shot image for selected cell ID {cell_id_preview}.\")"
@@ -1192,8 +1198,8 @@
     "display(Markdown(f'Single shot of the RAW data from cell {cell_id_preview} \\n'))\n",
     "fig = plt.figure(figsize=(20, 10))\n",
     "ax = fig.add_subplot(111)\n",
-    "vmin, vmax = get_range(raw[cell_idx_preview, 0, ...], 5)\n",
-    "ax = geom.plot_data_fast(raw[cell_idx_preview, 0, ...], ax=ax, cmap=\"jet\", vmin=vmin, vmax=vmax)"
+    "vmin, vmax = np.percentile(raw[cell_idx_preview, 0, ...], [5, 95])\n",
+    "ax = geom.plot_data_fast(raw[cell_idx_preview, 0, ...], ax=ax, vmin=vmin, vmax=vmax)"
    ]
   },
   {
@@ -1209,8 +1215,8 @@
     "    fig = plt.figure(figsize=(20, 10))\n",
     "    ax = fig.add_subplot(111)\n",
     "    data = np.mean(corrected, axis=0)\n",
-    "    vmin, vmax = get_range(data, 7)\n",
-    "    ax = geom.plot_data_fast(data, ax=ax, cmap=\"jet\", vmin=-50, vmax=vmax)\n",
+    "    vmin, vmax = np.percentile(data, [5, 95])\n",
+    "    ax = geom.plot_data_fast(data, ax=ax, vmin=vmin, vmax=vmax)\n",
     "else:\n",
     "    print(\"Skipping mean CORRECTED preview for single memory cell, \"\n",
     "          f\"see single shot image for selected cell ID {cell_id_preview}.\")"
@@ -1225,9 +1231,8 @@
     "display(Markdown(f'A single shot of the CORRECTED image from cell {cell_id_preview} \\n'))\n",
     "fig = plt.figure(figsize=(20, 10))\n",
     "ax = fig.add_subplot(111)\n",
-    "vmin, vmax = get_range(corrected[cell_idx_preview], 7, -50)\n",
-    "vmin = - 50\n",
-    "ax = geom.plot_data_fast(corrected[cell_idx_preview], ax=ax, cmap=\"jet\", vmin=vmin, vmax=vmax)"
+    "vmin, vmax = np.percentile(corrected[cell_idx_preview], [5, 95])\n",
+    "ax = geom.plot_data_fast(corrected[cell_idx_preview], ax=ax, vmin=vmin, vmax=vmax)"
    ]
   },
   {
@@ -1238,10 +1243,9 @@
    "source": [
     "fig = plt.figure(figsize=(20, 10))\n",
     "ax = fig.add_subplot(111)\n",
-    "vmin, vmax = get_range(corrected[cell_idx_preview], 5, -50)\n",
     "nbins = np.int((vmax + 50) / 2)\n",
     "h = ax.hist(corrected[cell_idx_preview].flatten(),\n",
-    "            bins=nbins, range=(-50, vmax),\n",
+    "            bins=nbins, range=(vmin, vmax),\n",
     "            histtype='stepfilled', log=True)\n",
     "plt.xlabel('[ADU]')\n",
     "plt.ylabel('Counts')\n",
@@ -1256,18 +1260,19 @@
    "source": [
     "fig = plt.figure(figsize=(20, 10))\n",
     "ax = fig.add_subplot(111)\n",
-    "vmin, vmax = get_range(corrected, 10, -100)\n",
+    "vmin, vmax = np.percentile(corrected[cell_idx_preview], [5, 95])\n",
     "vmax = np.nanmax(corrected)\n",
     "if vmax > 50000:\n",
     "    vmax=50000\n",
     "nbins = np.int((vmax + 100) / 5)\n",
+    "hist_range=(vmin, vmax)\n",
     "h = ax.hist(corrected.flatten(), bins=nbins,\n",
-    "            range=(-100, vmax), histtype='step', log=True, label = 'All')\n",
-    "ax.hist(corrected[gains == 0].flatten(), bins=nbins, range=(-100, vmax),\n",
+    "            range=hist_range, histtype='step', log=True, label = 'All')\n",
+    "ax.hist(corrected[gains == 0].flatten(), bins=nbins, range=hist_range,\n",
     "        alpha=0.5, log=True, label='High gain', color='green')\n",
-    "ax.hist(corrected[gains == 1].flatten(), bins=nbins, range=(-100, vmax),\n",
+    "ax.hist(corrected[gains == 1].flatten(), bins=nbins, range=hist_range,\n",
     "        alpha=0.5, log=True, label='Medium gain', color='red')\n",
-    "ax.hist(corrected[gains == 2].flatten(), bins=nbins, range=(-100, vmax),\n",
+    "ax.hist(corrected[gains == 2].flatten(), bins=nbins, range=hist_range,\n",
     "        alpha=0.5, log=True, label='Low gain', color='yellow')\n",
     "ax.legend()\n",
     "ax.grid()\n",
@@ -1293,8 +1298,8 @@
    "source": [
     "fig = plt.figure(figsize=(20, 10))\n",
     "ax = fig.add_subplot(111)\n",
-    "ax = geom.plot_data_fast(np.max(gains, axis=0), ax=ax,\n",
-    "                         cmap=\"jet\", vmin=-1, vmax=3)"
+    "vmin, vmax = np.percentile(np.mean(gains, axis=0), [0, 100])\n",
+    "ax = geom.plot_data_fast(np.mean(gains, axis=0), ax=ax, vmin=vmin, vmax=vmax)"
    ]
   },
   {
@@ -1336,7 +1341,7 @@
    "source": [
     "fig = plt.figure(figsize=(20, 10))\n",
     "ax = fig.add_subplot(111)\n",
-    "geom.plot_data_fast(np.log2(mask[cell_idx_preview]), ax=ax, vmin=0, vmax=32, cmap=\"jet\")"
+    "geom.plot_data_fast(np.log2(mask[cell_idx_preview]), ax=ax, vmin=0, vmax=32)"
    ]
   },
   {
@@ -1384,7 +1389,7 @@
    "source": [
     "fig = plt.figure(figsize=(20, 10))\n",
     "ax = fig.add_subplot(111)\n",
-    "geom.plot_data_fast(np.mean(mask>0, axis=0), vmin=0, ax=ax, vmax=1, cmap=\"jet\")"
+    "geom.plot_data_fast(np.mean(mask>0, axis=0), vmin=0, ax=ax, vmax=1)"
    ]
   },
   {
@@ -1404,8 +1409,7 @@
     "ax = fig.add_subplot(111)\n",
     "cm = np.copy(mask)\n",
     "cm[cm > BadPixels.NO_DARK_DATA.value] = 0\n",
-    "ax = geom.plot_data_fast(np.mean(cm>0, axis=0),\n",
-    "                         vmin=0, ax=ax, vmax=1, cmap=\"jet\")"
+    "ax = geom.plot_data_fast(np.mean(cm>0, axis=0), vmin=0, ax=ax, vmax=1)"
    ]
   }
  ],
-- 
GitLab