From 58b86651013c9c41f16a8cffddebc4bf5ac0eae3 Mon Sep 17 00:00:00 2001
From: ahmedk <karim.ahmed@xfel.eu>
Date: Thu, 25 Apr 2024 10:07:42 +0200
Subject: [PATCH] Remove warning from agipdlib and test PRINT statement

---
 .../AGIPD/AGIPD_Correct_and_Verify.ipynb      | 11 +++++--
 src/cal_tools/agipdlib.py                     | 30 ++++++++-----------
 2 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
index e32d7f16a..9ffa2487e 100644
--- a/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
+++ b/notebooks/AGIPD/AGIPD_Correct_and_Verify.ipynb
@@ -238,9 +238,16 @@
     "\n",
     "# Dont apply any corrections if only_offset is requested\n",
     "if not only_offset:\n",
-    "    corr_bools[\"adjust_mg_baseline\"] = adjust_mg_baseline\n",
-    "    corr_bools[\"cs_corr\"] = cs_corr \n",
+    "    corr_bools[\"cs_corr\"] = cs_corr\n",
     "    corr_bools[\"pc_corr\"] = pc_corr\n",
+    "    if cs_corr:\n",
+    "        corr_bools[\"adjust_mg_baseline\"] = False\n",
+    "        warning(\n",
+    "            \"Currently, `adjust_mg_baseline` correction\"\n",
+    "            \" is not allowed with Current Source gain correction.\"\n",
+    "            \" Skipping `adjust_mg_baseline` correction step.\")\n",
+    "    else:\n",
+    "        corr_bools[\"adjust_mg_baseline\"] = adjust_mg_baseline\n",
     "    corr_bools[\"xray_corr\"] = xray_gain\n",
     "    corr_bools[\"blc_noise\"] = blc_noise\n",
     "    corr_bools[\"blc_stripes\"] = blc_stripes\n",
diff --git a/src/cal_tools/agipdlib.py b/src/cal_tools/agipdlib.py
index d28b8021b..522ee4695 100644
--- a/src/cal_tools/agipdlib.py
+++ b/src/cal_tools/agipdlib.py
@@ -1056,12 +1056,6 @@ class AgipdCorrections:
         # Adjust medium gain baseline to match highest high gain value
         # This correction is disabled in case CS gain correction is selected.
         if self.corr_bools.get("adjust_mg_baseline"):
-            if self.corr_bools.get("cs_corr"):
-                warning(
-                    "Currently, `adjust_mg_baseline` correction"
-                    " is not allowed with Current Source gain correction."
-                    " Skipping `adjust_mg_baseline` correction step.")
-            else:
                 mgbc = self.md_additional_offset[module_idx][cellid, ...]
                 data[gain == 1] += mgbc[gain == 1]
                 del mgbc
@@ -1437,20 +1431,22 @@ class AgipdCorrections:
 
             # it is not expected to have SlopesCS and SlopesPC at the sametime.
             if "SlopesCS" in cons_data:
-                rel_gain[..., 1] = rel_gain[..., 0] * cons_data["SlopesCS"][..., self.max_cells, 6]  # noqa
-                rel_gain[..., 2] = rel_gain[..., 1] * cons_data["SlopesCS"][..., self.max_cells, 7]  # noqa
-                # TODO: Decide between:
+                # TODO consider updating the SlopesCS shape instead of doing this here.
+                cons_data["SlopesCS"] = np.transpose(cons_data["SlopesCS"], (2, 3, 1, 0))
+                rel_gain[..., 1] = rel_gain[..., 0] * cons_data["SlopesCS"][..., :self.max_cells, 6]  # noqa
+                rel_gain[..., 2] = rel_gain[..., 1] * cons_data["SlopesCS"][..., :self.max_cells, 7]  # noqa
+                # TODO: Decide between: BTW both medium fractions are not the equal after testing!
                 #   - Median of ratio of high gain and medium gain slope
                 #   - Ratio of medians of high gain slope and medium gain slope
-                frac_high_med_ofratio = np.median(
-                    cons_data["SlopesCS"][..., self.max_cells, 6])
+                # frac_high_med_ofratio = np.median(
+                #     cons_data["SlopesCS"][..., :self.max_cells, 6])
                 frac_high_med = (
-                    np.median(cons_data["SlopesCS"][..., self.max_cells, 0]) /
-                    np.median(cons_data["SlopesCS"][..., self.max_cells, 1]))
-                # TODO: DECIDE AND REMOVE THIS PRINT AFTER TESTING
-                print(
-                    "CHECK IF BOTH MEDIAN FRACTIONS ARE THE SAME:",
-                    np.allclose(frac_high_med, frac_high_med_ofratio))
+                    np.median(cons_data["SlopesCS"][..., :self.max_cells, 0]) /
+                    np.median(cons_data["SlopesCS"][..., :self.max_cells, 1]))
+                # Currently, `adjust_mg_baseline` correction 
+                # is not allowed with Current Source gain correction.
+                # Skipping `adjust_mg_baseline` correction step."
+                md_additional_offset = np.zeros((128, 512, self.max_cells), np.float32)
             elif "SlopesPC" in cons_data:
                 slopesPC = cons_data["SlopesPC"].astype(np.float32, copy=False)
 
-- 
GitLab