From 1e6ceb92b9b5408b68942c4eab38280ced262391 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Le=20Guyader?= <loic.le.guyader@xfel.eu>
Date: Thu, 16 Jun 2022 12:21:00 +0200
Subject: [PATCH] Adds option to refine flat field fit from scratch

---
 src/toolbox_scs/routines/boz.py | 46 +++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 13 deletions(-)

diff --git a/src/toolbox_scs/routines/boz.py b/src/toolbox_scs/routines/boz.py
index 9d64f30..b3c60ec 100644
--- a/src/toolbox_scs/routines/boz.py
+++ b/src/toolbox_scs/routines/boz.py
@@ -121,6 +121,30 @@ class parameters():
         """Get the list of bad pixel indices."""
         return self.mask_idx
 
+    def flat_field_guess(self, guess=None):
+        """Set the flat field guess parameter for the fit and returns it.
+
+        Inputs
+        ------
+        guess: a list of 8 floats, the 4 first to define the plane
+               ax+by+cz+d=0 for 'n' beam and the 4 last for the 'p' beam
+               in case mirror symmetry is disbaled
+        """
+        if guess is not None:
+            self.plane_guess_fit = guess
+            return self.plane_guess_fit
+            
+        if self.plane_guess_fit is None:
+            if self.use_hex:
+                self.plane_guess_fit = [
+                    -20, 0.0, 1.5, -0.5, 20, 0, 1.5, -0.5 ]
+            else:
+                self.plane_guess_fit = [
+                    -0.2, -0.1, 1, -0.54, 0.2, -0.1, 1, -0.54]
+
+        return self.plane_guess_fit
+
+
     def set_flat_field(self, plane,
             prod_th=None, ratio_th=None):
         """Set the flat field plane definition."""
@@ -846,7 +870,7 @@ def inspect_flat_field_domain(avg, rois, prod_th, ratio_th, vmin=None, vmax=None
     return fig, domain
 
 
-def inspect_plane_fitting(avg, rois, domain, vmin=None, vmax=None):
+def inspect_plane_fitting(avg, rois, domain=None, vmin=None, vmax=None):
     """Extract beams roi from average image and compute the ratio.
 
     Inputs
@@ -854,6 +878,7 @@ def inspect_plane_fitting(avg, rois, domain, vmin=None, vmax=None):
     avg: module average image with no saturated shots for the flat field
          determination
     rois: dictionnary of rois
+    domain: list of domain mask for the -1st and +1st order
     vmin: imshow vmin level, default None will use 5 percentile value
     vmax: imshow vmax level, default None will use 99.8 percentile value
 
@@ -889,9 +914,10 @@ def inspect_plane_fitting(avg, rois, domain, vmin=None, vmax=None):
         v = img_rois[r]/img_rois['0']
         im2 = axs[1, k].imshow(v, vmin=0.2, vmax=1.1, cmap='RdBu_r')
 
-    n_m, p_m = domain
-    axs[1, 0].contour(n_m)
-    axs[1, 2].contour(p_m)
+    if domain is not None:
+        n_m, p_m = domain
+        axs[1, 0].contour(n_m)
+        axs[1, 2].contour(p_m)
 
     cbar = fig.colorbar(im, ax=axs[0, :], orientation="horizontal")
     cbar.ax.set_xlabel('data mean')
@@ -1014,15 +1040,7 @@ def plane_fitting(params):
 
         return 1e3*(d2_2 + d0_2)
 
-    if params.plane_guess_fit is None:
-        if params.use_hex:
-            p_guess_fit = [-20, 0.0, 1.5, -0.5, 20, 0, 1.5, -0.5 ]
-        else:
-            p_guess_fit = [-0.2, -0.1, 1, -0.54, 0.2, -0.1, 1, -0.54]
-    else:
-        p_guess_fit = params.plane_guess_fit
-
-    res = minimize(_crit, p_guess_fit)
+    res = minimize(_crit, params.flat_field_guess())
 
     return res
 
@@ -1092,6 +1110,8 @@ def ff_refine_fit(params):
         fitrois[k] = params.rois[k]
 
     p0 = params.get_flat_field()
+    if p0 is None: # flat field was not yet fitted
+        p0 = params.flat_field_guess()
 
     fixed_p = (params.ff_alpha, params, params.arr_dark, params.arr,
         params.tid, fitrois, params.get_mask(), params.sat_level)
-- 
GitLab