From d02553fb60f83d4b5e30eb60c162fe58c090f8c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Le=20Guyader?= <loic.le.guyader@xfel.eu>
Date: Thu, 26 Aug 2021 17:20:59 +0200
Subject: [PATCH] BOZ slurm update

---
 ...ysis part I parameters determination.ipynb | 70 ++++++++++++++++---
 doc/howtos.rst                                | 13 +++-
 scripts/boz_parameters_job.sh                 | 22 ++++++
 src/toolbox_scs/routines/boz.py               | 11 +--
 4 files changed, 100 insertions(+), 16 deletions(-)
 create mode 100644 scripts/boz_parameters_job.sh

diff --git a/doc/BOZ analysis part I parameters determination.ipynb b/doc/BOZ analysis part I parameters determination.ipynb
index de0e8d0..9c89c0d 100644
--- a/doc/BOZ analysis part I parameters determination.ipynb	
+++ b/doc/BOZ analysis part I parameters determination.ipynb	
@@ -140,14 +140,31 @@
     "# Create parameters object"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "tags": [
+     "parameters"
+    ]
+   },
+   "outputs": [],
+   "source": [
+    "proposal = 2619\n",
+    "darkrun = 33\n",
+    "run = 38\n",
+    "module = 15\n",
+    "gain = 4"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
-    "params = boz.parameters(proposal=2712, darkrun=5, run=6, module=15)\n",
-    "#params = boz.parameters(proposal=2619, darkrun=33, run=38, module=15)"
+    "params = boz.parameters(proposal=proposal, darkrun=darkrun, run=run, module=module, gain=gain)\n",
+    "path = f'r{params.run}/'"
    ]
   },
   {
@@ -196,7 +213,8 @@
    "outputs": [],
    "source": [
     "mean_th = (30, 56)\n",
-    "f = boz.inspect_dark(params.arr_dark, mean_th=mean_th)"
+    "f = boz.inspect_dark(params.arr_dark, mean_th=mean_th)\n",
+    "f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-dark.png', dpi=300)"
    ]
   },
   {
@@ -233,7 +251,8 @@
    "source": [
     "h, f = boz.inspect_histogram(params.proposal, params.run, params.module,\n",
     "                             mask=params.get_mask() #, extra_lines=True\n",
-    "                         )"
+    "                         )\n",
+    "f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-histogram.png', dpi=300)"
    ]
   },
   {
@@ -268,7 +287,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "params.rois_th = 50\n",
+    "params.rois_th = 130\n",
     "params.rois = boz.find_rois_from_params(params)"
    ]
   },
@@ -298,7 +317,8 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "f = boz.inspect_rois(dataM, params.rois, params.rois_th)"
+    "f = boz.inspect_rois(dataM, params.rois, params.rois_th)\n",
+    "f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-rois.png', dpi=300)"
    ]
   },
   {
@@ -344,6 +364,15 @@
     "f = boz.inspect_plane_fitting(avg, params.rois)"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-noflatfield.png', dpi=300)"
+   ]
+  },
   {
    "cell_type": "markdown",
    "metadata": {},
@@ -397,6 +426,15 @@
     "f = boz.inspect_plane_fitting(avg/ff, params.rois)"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-withflatfield.png', dpi=300)"
+   ]
+  },
   {
    "cell_type": "markdown",
    "metadata": {},
@@ -443,6 +481,15 @@
     "params.set_Fnl(boz.nl_lut(domain, res.x))"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "print(params)"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -451,7 +498,7 @@
    },
    "outputs": [],
    "source": [
-    "f = boz.inspect_correction(params, gain=3)"
+    "f = boz.inspect_correction(params, gain=params.gain)"
    ]
   },
   {
@@ -460,7 +507,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "f.savefig(f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-correction.png', dpi=300)"
+    "f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-correction.png', dpi=300)"
    ]
   },
   {
@@ -485,7 +532,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "f.savefig(f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-Fnl.png', dpi=300)"
+    "f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-Fnl.png', dpi=300)"
    ]
   },
   {
@@ -510,7 +557,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "f.savefig(f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-nl-fit.png', dpi=300)"
+    "f.savefig(path+f'p{params.proposal}-r{params.run}-d{params.darkrun}-inspect-nl-fit.png', dpi=300)"
    ]
   },
   {
@@ -526,11 +573,12 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "params.save()"
+    "params.save(path=path)"
    ]
   }
  ],
  "metadata": {
+  "celltoolbar": "Tags",
   "kernelspec": {
    "display_name": "xfel",
    "language": "python",
diff --git a/doc/howtos.rst b/doc/howtos.rst
index 5175138..716e45b 100644
--- a/doc/howtos.rst
+++ b/doc/howtos.rst
@@ -38,6 +38,17 @@ Photo-Electron Spectrometer (PES)
 routines
 --------
 
+BOZ: Beam-Splitting Off-axis Zone plate analysis
+++++++++++++++++++++++++++++++++++++++++++++++++
+
+The BOZ analysis consists of 2 notebooks and a script. The first notebook is used to determine
+all the correction. The input are a dark run and a run with X-ray on empty sample or at the pre-edge.
+The result is a JSON file that contains the flat field and non-linearity correction. The determination
+of the non-linearity correction can take some 2 to 8 hours depending on the number of pulses in the
+train. For this reason it is possible to use a script 'scripts/boz_parameters_job.sh' in the toolbox to
+launch the first notebook via slurm. It requires 'papermill' to be installed.
+
+The second notebook then use the JSON file to load all needed correction and process an run with a
+corresponding dark run to bin data and compute a spectrum or a time resolved XAS scan.
 * :doc:`BOZ analysis part I parameters determination <BOZ analysis part I parameters determination>`.
 * :doc:`BOZ analysis part II run processing <BOZ analysis part II run processing>`.
-* *to do*
diff --git a/scripts/boz_parameters_job.sh b/scripts/boz_parameters_job.sh
new file mode 100644
index 0000000..a8c8013
--- /dev/null
+++ b/scripts/boz_parameters_job.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+#SBATCH -N 1
+#SBATCH --partition=exfel
+#SBATCH --time=12:00:00
+#SBATCH --mail-type=END,FAIL
+#SBATCH --output=logs/%j-%x.out
+
+PROPOSAL=2619
+DARK=$1
+RUN=$2
+GAIN=$3
+MODULE=15
+
+source /etc/profile.d/modules.sh
+module load exfel
+module load exfel_anaconda3/1.1
+
+echo processing run $RUN
+mkdir r$RUN
+papermill 'BOZ analysis part I parameters determination.ipynb' r$RUN/output.ipynb \
+ -p proposal $PROPOSAL -p darkrun $DARK -p run $RUN -p module $MODULE \
+ -p gain $GAIN -k xfel
diff --git a/src/toolbox_scs/routines/boz.py b/src/toolbox_scs/routines/boz.py
index 2b53ae1..61c1fbd 100644
--- a/src/toolbox_scs/routines/boz.py
+++ b/src/toolbox_scs/routines/boz.py
@@ -42,13 +42,15 @@ class parameters():
     proposal: int, proposal number
     darkrun: int, run number for the dark run
     run: int, run number for the data run
-    module: int: DSSC module number
+    module: int, DSSC module number
+    gain: float, number of ph per bin
     """
-    def __init__(self, proposal, darkrun, run, module):
+    def __init__(self, proposal, darkrun, run, module, gain):
         self.proposal = proposal
         self.darkrun = darkrun
         self.run = run
         self.module = module
+        self.gain = gain
         self.mask_idx = None
         self.mean_th = (None, None)
         self.std_th = (None, None)
@@ -143,6 +145,7 @@ class parameters():
         v['darkrun'] = self.darkrun
         v['run'] = self.run
         v['module'] = self.module
+        v['gain'] = self.gain
 
         v['mask'] = self.mask_idx
         v['mean_th'] = self.mean_th
@@ -174,7 +177,7 @@ class parameters():
         """
         with open(fname, 'r') as f:
             v = json.load(f)
-        c = cls(v['proposal'], v['darkrun'], v['run'], v['module'])
+        c = cls(v['proposal'], v['darkrun'], v['run'], v['module'], v['gain'])
 
         c.mean_th = v['mean_th']
         c.std_th = v['std_th']
@@ -194,7 +197,7 @@ class parameters():
 
     def __str__(self):
         f = f'proposal:{self.proposal} darkrun:{self.darkrun} run:{self.run}'
-        f += f' module:{self.module}\n'
+        f += f' module:{self.module} gain:{self.gain} ph/bin\n'
 
         if self.mask_idx is not None:
             f += f'mean threshold:{self.mean_th} std threshold:{self.std_th}\n'
-- 
GitLab