From de58376ee2a7de7cfb451193a3e1171e935f967d Mon Sep 17 00:00:00 2001
From: Egor Sobolev <egor.sobolev@xfel.eu>
Date: Fri, 9 Dec 2022 20:09:26 +0100
Subject: [PATCH] Add docstring and fix the name for agipdalgs.cm_correction

---
 src/cal_tools/agipdalgs.pyx | 35 ++++++++++++++++++++++++++++++++---
 src/cal_tools/agipdlib.py   |  2 +-
 2 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/src/cal_tools/agipdalgs.pyx b/src/cal_tools/agipdalgs.pyx
index d582af17f..e905b5643 100644
--- a/src/cal_tools/agipdalgs.pyx
+++ b/src/cal_tools/agipdalgs.pyx
@@ -147,9 +147,38 @@ def gain_choose(cnp.ndarray[cnp.uint8_t, ndim=3] a, cnp.ndarray[choices_t, ndim=
 
 @boundscheck(False)
 @wraparound(False)
-def cm_correction_1pass(float[:, :, :] arr, unsigned short[:] cellid,
-                        float lower, float upper, float fraction):
-    """Apply 1 pass of common mode correction"""
+def cm_correction(float[:, :, :] arr, unsigned short[:] cellid,
+                  float lower, float upper, float fraction):
+    """Apply one interation of common-mode correction
+
+    The common-mode correction shifts the position of the noise peak for
+    a slice of pixels to zero. The position is estimated as mean signal of
+    pixels in a range between `lower` and `upper` boundaries. If the noise
+    peak located in this boundares then the mean value works as a robust
+    estimator, which converges to the true noise peak position with
+    iterations. The correction is applied only if the number of pixels in
+    the given range above the `fraction` of the total number of the pixels
+    in the slice.
+
+    This function performs consequently cells common-mode correction and
+    ASIC common-mode correction.
+    Cells correction is calculated across trains and groups of 32 cells.
+    ASIC correction is calculated across the pixes in ASIC on a single image.
+
+    To converge this function should be iterated.
+    
+    This function performs correction in-place, altering `arr`.
+
+    :param arr: array of images cropped to a single ASIC and
+        stacked together over trains and pulses in the first dimension
+    :param cellid: array of cell IDs, must have the length equal to
+        the number of images
+    :param lower: the lower signal value in ADU to consider pixel as a dark
+    :param upper: the upper signal value in ADU to consider pixel as a dark
+    :param fraction: the fraction of the dark pixels in the slice which is
+        cosidered to be enough to apply the common-mode correction to this
+        slice
+    """
     cdef long long nfrm = arr.shape[0]
     cdef long long nx = arr.shape[1]
     cdef long long ny = arr.shape[2]
diff --git a/src/cal_tools/agipdlib.py b/src/cal_tools/agipdlib.py
index 090ff0b26..862864080 100644
--- a/src/cal_tools/agipdlib.py
+++ b/src/cal_tools/agipdlib.py
@@ -651,7 +651,7 @@ class AgipdCorrections:
 
         asic_data = data[:, asic % 8, :, asic // 8, :]
         for _ in range(n_itr):
-            calgs.cm_correction_1pass(
+            calgs.cm_correction(
                 asic_data, cell_id, dark_min, dark_max, fraction)
 
     def mask_zero_std(self, i_proc, cells):
-- 
GitLab