From 065185c36e05d56695cca5bf70a567a3f03df9d5 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Fri, 9 Feb 2024 09:30:51 +0100
Subject: [PATCH] Last changes after more testing - replace np.percentile(,
 [0,100]) with min(), max() - put back histogram plots with get_range for no
 need of change. - improve gain plot and add clamp function

---
 .../AGIPD/AGIPD_Correct_and_Verify.ipynb      | 38 +++++++++++--------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
index b9048f088..3fa1efccd 100644
--- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
+++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
@@ -167,6 +167,7 @@
     "    CellRange,\n",
     "    LitFrameSelection,\n",
     ")\n",
+    "from cal_tools.ana_tools import get_range\n",
     "from cal_tools.calcat_interface import (\n",
     "    AGIPD_CalibrationData,\n",
     "    CalCatError,\n",
@@ -1045,7 +1046,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "vmin, vmax = np.percentile(corrected, [0, 100])\n",
+    "vmin, vmax = np.nanmin(corrected), np.nanmax(corrected)\n",
     "hist, bins_x, bins_y = calgs.histogram2d(\n",
     "    corrected.flatten().astype(np.float32),\n",
     "    gains.flatten().astype(np.float32), bins=(100, 3),\n",
@@ -1087,17 +1088,22 @@
    "source": [
     "pulse_range = [np.min(pulseId[pulseId>=0]), np.max(pulseId[pulseId>=0])]\n",
     "\n",
+    "\n",
+    "def clamp(value, min_value, max_value):\n",
+    "    return max(min_value, min(value, max_value))\n",
+    "\n",
+    "\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",
-    "vmin, vmax = np.percentile(mean_data, [0, 100])\n",
+    "vmin, vmax = mean_data.min(), mean_data.max()\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=[[max(vmin, -50), min(vmax, 1000)], pulse_range],\n",
+    "    range=[[clamp(vmin, -50, -0.2), min(vmax, 1000)], pulse_range],\n",
     ")\n",
     "do_2d_plot(hist, (bins_x, bins_y), \"Signal (ADU)\", \"Pulse id\")\n",
     "if vmax > 1000:  # a zoom out plot.\n",
@@ -1105,7 +1111,7 @@
     "        mean_data.flatten().astype(np.float32),\n",
     "        pulseId.flatten().astype(np.float32),\n",
     "        bins=(100,  int(pulse_range[1])),\n",
-    "        range=[[max(vmin, -50), min(vmax, 20000)], pulse_range]\n",
+    "        range=[[clamp(vmin, -50, -0.2), min(vmax, 20000)], pulse_range]\n",
     "    )\n",
     "    do_2d_plot(hist, (bins_x, bins_y), \"Signal (ADU)\", \"Pulse id\")"
    ]
@@ -1242,10 +1248,10 @@
    "source": [
     "fig = plt.figure(figsize=(20, 10))\n",
     "ax = fig.add_subplot(111)\n",
-    "vmin, vmax = np.nanpercentile(corrected[cell_idx_preview], [5, 100])\n",
-    "nbins = int((vmax + 50) / 2)\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=(vmin, vmax),\n",
+    "            bins=nbins, range=(-50, vmax),\n",
     "            histtype='stepfilled', log=True)\n",
     "plt.xlabel('[ADU]')\n",
     "plt.ylabel('Counts')\n",
@@ -1262,18 +1268,18 @@
    "source": [
     "fig = plt.figure(figsize=(20, 10))\n",
     "ax = fig.add_subplot(111)\n",
-    "vmin, vmax = np.nanpercentile(corrected, [5, 100])\n",
+    "vmin, vmax = get_range(corrected, 10, -100)\n",
+    "vmax = np.nanmax(corrected)\n",
     "if vmax > 50000:\n",
     "    vmax = 50000\n",
-    "nbins = int((vmax + 100) / 5)\n",
-    "hist_range=(vmin, vmax)\n",
+    "nbins = np.int((vmax + 100) / 5)\n",
     "h = ax.hist(corrected.flatten(), bins=nbins,\n",
-    "            range=hist_range, histtype='step', log=True, label = 'All')\n",
-    "ax.hist(corrected[gains == 0].flatten(), bins=nbins, range=hist_range,\n",
+    "            range=(-100, vmax), histtype='step', log=True, label = 'All')\n",
+    "ax.hist(corrected[gains == 0].flatten(), bins=nbins, range=(-100, vmax),\n",
     "        alpha=0.5, log=True, label='High gain', color='green')\n",
-    "ax.hist(corrected[gains == 1].flatten(), bins=nbins, range=hist_range,\n",
+    "ax.hist(corrected[gains == 1].flatten(), bins=nbins, range=(-100, vmax),\n",
     "        alpha=0.5, log=True, label='Medium gain', color='red')\n",
-    "ax.hist(corrected[gains == 2].flatten(), bins=nbins, range=hist_range,\n",
+    "ax.hist(corrected[gains == 2].flatten(), bins=nbins, range=(-100, vmax),\n",
     "        alpha=0.5, log=True, label='Low gain', color='yellow')\n",
     "ax.legend()\n",
     "ax.grid()\n",
@@ -1301,9 +1307,9 @@
    "source": [
     "fig = plt.figure(figsize=(20, 10))\n",
     "ax = fig.add_subplot(111)\n",
-    "vmin, vmax = np.percentile(np.mean(gains, axis=0), [0, 100])\n",
     "ax = geom.plot_data_fast(\n",
-    "    np.mean(gains, axis=0), ax=ax, vmin=vmin, vmax=vmax, cmap=cmap)\n",
+    "    np.max(gains, axis=0), ax=ax,\n",
+    "    cmap=cmap, vmin=-0.3, vmax=2.3)  # Extend cmap for wrong gain values.\n",
     "pass"
    ]
   },
-- 
GitLab