From 15f36d0d146e044e5d0422971df4e23b2f56e053 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Le=20Guyader?= <loic.le.guyader@xfel.eu>
Date: Tue, 21 May 2024 11:45:16 +0200
Subject: [PATCH] Extends polyline norm to 2D

---
 src/toolbox_scs/routines/boz.py | 51 +++++++++++++++++++++++----------
 1 file changed, 36 insertions(+), 15 deletions(-)

diff --git a/src/toolbox_scs/routines/boz.py b/src/toolbox_scs/routines/boz.py
index 732f4b4..d280b30 100644
--- a/src/toolbox_scs/routines/boz.py
+++ b/src/toolbox_scs/routines/boz.py
@@ -952,24 +952,37 @@ def initialize_polyline_ff_correction(avg, rois, params, plot=False):
     mref = 0.5*(refn + refp)
 
     inv_signal = mref/mid # normalization
-    projection = inv_signal[5:-5, :].mean(axis=0) #skip first 5 and last 5 px
-    x = np.arange(0, len(projection))
+    H_projection = inv_signal[:, :].mean(axis=0)
+    x = np.arange(0, len(H_projection))
+    H_z = np.polyfit(x, H_projection, 6)
+    H_p = np.poly1d(H_z)
 
-    z = np.polyfit(x, projection, 6)
+    V_projection = (inv_signal/H_p(x))[:, :].mean(axis=1)
+    y = np.arange(0, len(V_projection))
+    V_z = np.polyfit(y, V_projection, 6)
 
     if plot:
-        fig, ax = plt.subplots(1, 1, figsize=(4,3))
-        ax.plot(x, projection, label='projection (n+p)/2x0')
-        p = np.poly1d(z)
-        ax.plot(x, p(x), label='poly')
-        ax.legend()
-        ax.set_xlabel('x (px)')
-        ax.set_ylabel('projection')
+        fig, axs = plt.subplots(2, 1, figsize=(4,6))
+        axs[0].plot(x, H_projection, label='data (n+p)/2x0')
+        axs[0].plot(x, H_p(x), label='poly')
+        axs[0].legend()
+        axs[0].set_xlabel('x (px)')
+        axs[0].set_ylabel('H projection')
+
+        axs[1].plot(y, V_projection, label='data (n+p)/2x0')
+        V_p = np.poly1d(V_z)
+        axs[1].plot(y, V_p(y), label='poly')
+        axs[1].legend()
+        axs[1].set_xlabel('y (px)')
+        axs[1].set_ylabel('V projection')
     else:
         fig = None
 
     # scaling on polynom coefficients for better fitting
-    params.set_flat_field(z/np.logspace(-(z.shape[0]-1), 0, z.shape[0]))
+    ff = np.array([H_z/np.logspace(-(H_z.shape[0]-1), 0, H_z.shape[0]),
+                   V_z/np.logspace(-(V_z.shape[0]-1), 0, V_z.shape[0])])
+
+    params.set_flat_field(ff.flatten())
     params.ff_type = 'polyline'
 
     return fig
@@ -990,9 +1003,15 @@ def compute_polyline_flat_field_correction(rois, params, plot=False):
     """
     flat_field = np.ones((128, 512))
 
-    z = params.get_flat_field()
-    coeffs = np.logspace(-(z.shape[0]-1), 0, z.shape[0])
-    poly = np.poly1d(z*coeffs)
+    z = np.array(params.get_flat_field()).reshape((2, -1))
+    H_z = z[0, :]
+    V_z = z[1, :]
+    
+    coeffs = np.logspace(-(H_z.shape[0]-1), 0, H_z.shape[0])
+    H_p = np.poly1d(H_z*coeffs)
+    coeffs = np.logspace(-(V_z.shape[0]-1), 0, V_z.shape[0])
+    V_p = np.poly1d(V_z*coeffs)
+
     n = rois['n']
     p = rois['p']
     wn = n['xh']-n['xl']
@@ -1002,7 +1021,9 @@ def compute_polyline_flat_field_correction(rois, params, plot=False):
         f"must have the same width {wn} and {wp}px"
     )
     x = np.arange(wn)
-    norm = poly(x)
+    wn = n['yh']-n['yl']
+    y = np.arange(wn)
+    norm = V_p(y)[:, np.newaxis]*H_p(x)
     
     n_int = flat_field[n['yl']:n['yh'], n['xl']:n['xh']]
     flat_field[n['yl']:n['yh'], n['xl']:n['xh']]  = \
-- 
GitLab