From 0269762e8e11f53665cd29092500d1355f75bf01 Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas.kluyver@xfel.eu>
Date: Tue, 10 Oct 2023 15:35:03 +0200
Subject: [PATCH] Add thresholding for JUNGFRAU ROI projections

---
 ...Jungfrau_Gain_Correct_and_Verify_NBC.ipynb | 27 ++++++++++++-------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
index e644dfcaa..f915003c5 100644
--- a/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
+++ b/notebooks/Jungfrau/Jungfrau_Gain_Correct_and_Verify_NBC.ipynb
@@ -17,16 +17,16 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "in_folder = \"/gpfs/exfel/exp/SPB/202130/p900204/raw\"  # the folder to read data from, required\n",
-    "out_folder =  \"/gpfs/exfel/data/scratch/ahmedk/test/remove\"  # the folder to output to, required\n",
-    "run = 91  # run to process, required\n",
+    "in_folder = \"/gpfs/exfel/exp/FXE/202301/p003279/raw\"  # the folder to read data from, required\n",
+    "out_folder =  \"/gpfs/exfel/data/scratch/kluyvert/jf-corr-p3279-r275\"  # the folder to output to, required\n",
+    "run = 275  # run to process, required\n",
     "metadata_folder = \"\"  # Directory containing calibration_metadata.yml when run by xfel-calibrate\n",
     "sequences = [-1]  # sequences to correct, set to [-1] for all, range allowed\n",
     "sequences_per_node = 1  # number of sequence files per cluster node if run as slurm job, set to 0 to not run SLURM parallel\n",
     "\n",
     "# Parameters used to access raw data.\n",
-    "karabo_id = \"SPB_IRDA_JF4M\"  # karabo prefix of Jungfrau devices\n",
-    "karabo_da = ['JNGFR01', 'JNGFR02', 'JNGFR03', 'JNGFR04', 'JNGFR05', 'JNGFR06', 'JNGFR07', 'JNGFR08']  # data aggregators\n",
+    "karabo_id = \"FXE_XAD_JF500K\"  # karabo prefix of Jungfrau devices\n",
+    "karabo_da = ['JNGFR03']  # data aggregators\n",
     "receiver_template = \"JNGFR{:02d}\"  # Detector receiver template for accessing raw data files. e.g. \"JNGFR{:02d}\"\n",
     "instrument_source_template = '{}/DET/{}:daqOutput'  # template for source name (filled with karabo_id & receiver_id). e.g. 'SPB_IRDA_JF4M/DET/JNGFR01:daqOutput'\n",
     "ctrl_source_template = '{}/DET/CONTROL'  # template for control source name (filled with karabo_id_control)\n",
@@ -60,6 +60,7 @@
     "\n",
     "# Parameters for ROI selection and reduction\n",
     "roi_definitions = [-1]  # List with groups of 6 values defining ROIs, e.g. [3, 120, 180, 200, 550, -2] for module 3 (JNGFR03), slice 120:180, 200:550, average along axis -2 (slow scan, or -1 for fast scan)\n",
+    "roi_threshold = -1  # Corrected pixels below the threshold will be excluded from ROI projections. Set to -1 to include all pixels.\n",
     "\n",
     "def balance_sequences(in_folder, run, sequences, sequences_per_node, karabo_da):\n",
     "    from xfel_calibrate.calibrate import balance_sequences as bs\n",
@@ -182,7 +183,9 @@
   {
    "cell_type": "code",
    "execution_count": null,
-   "metadata": {},
+   "metadata": {
+    "scrolled": true
+   },
    "outputs": [],
    "source": [
     "jf_cal = JUNGFRAU_CalibrationData(\n",
@@ -463,8 +466,12 @@
     "        roi_module, a1, a2, b1, b2, mean_axis = roi_definitions[i*6 : (i+1)*6]\n",
     "        if roi_module == module_no:\n",
     "            rois_defined += 1\n",
+    "            # Set pixels below the threshold to 0 (but still used in the averaging)\n",
+    "            roi_data = data_corr[..., a1:a2, b1:b2]\n",
+    "            if roi_threshold > -1:\n",
+    "                roi_data = roi_data * (roi_data > roi_threshold)\n",
     "            # Apply the mask and average remaining pixels to 1D\n",
-    "            roi_data = data_corr[..., a1:a2, b1:b2].mean(\n",
+    "            roi_data = roi_data.mean(\n",
     "                axis=mean_axis, where=(mask_corr[..., a1:a2, b1:b2] == 0)\n",
     "            )\n",
     "\n",
@@ -1016,9 +1023,9 @@
  ],
  "metadata": {
   "kernelspec": {
-   "display_name": "cal2_venv",
+   "display_name": "Offline Cal",
    "language": "python",
-   "name": "cal2_venv"
+   "name": "offline-cal"
   },
   "language_info": {
    "codemirror_mode": {
@@ -1030,7 +1037,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.8.11"
+   "version": "3.8.10"
   }
  },
  "nbformat": 4,
-- 
GitLab