From 7270c069825800b98fd8bcbc75b519c286508fe7 Mon Sep 17 00:00:00 2001
From: Egor Sobolev <egor.sobolev@xfel.eu>
Date: Tue, 30 Aug 2022 20:35:48 +0200
Subject: [PATCH] Fix array reshaping in common mode correction

---
 src/cal_tools/agipdlib.py | 37 ++++++++++++++++---------------------
 1 file changed, 16 insertions(+), 21 deletions(-)

diff --git a/src/cal_tools/agipdlib.py b/src/cal_tools/agipdlib.py
index 03f9680d7..b0d9d2845 100644
--- a/src/cal_tools/agipdlib.py
+++ b/src/cal_tools/agipdlib.py
@@ -647,24 +647,19 @@ class AgipdCorrections:
         if n_img == 0:
             return
         cell_id = self.shared_dict[i_proc]['cellId'][:n_img]
-        train_id = self.shared_dict[i_proc]['trainId'][:n_img]
-        cell_ids = cell_id[train_id == train_id[0]]
-        n_cells = cell_ids.size
-        data = self.shared_dict[i_proc]['data'][:n_img].reshape(-1, n_cells,
-                                                                8, 64, 2, 64)
-
-        # Loop over iterations
-        for _ in range(n_itr):
-            # Loop over rows of cells
-            # TODO: check what occurs in case of 64 memory cells
-            #       as it will have less than 11 iterations
-            first = 0
-            for cell_row in range(11):
-                last = first + cell_ids[(cell_ids // 32) == cell_row].size
-                if first == last:
-                    continue
-                asic_data = data[:, first:last, asic % 8, :, asic // 8, :]
 
+        data_arr = self.shared_dict[i_proc]['data'][:n_img]
+        data = data_arr.reshape(-1, 32, 8, 64, 2, 64)
+        row_id = cell_id[::32] // 32
+
+        xasic, yasic = asic % 8, asic // 8
+        for cell_row in range(11):
+            irow = np.flatnonzero(row_id == cell_row)
+            if not irow.size:
+                continue
+            asic_data = data[irow, :, xasic, :, yasic, :]
+            # Loop over iterations
+            for _ in range(n_itr):
                 # Cell common mode
                 cell_cm_sum, cell_cm_count = \
                     calgs.sum_and_count_in_range_cell(asic_data, dark_min,
@@ -672,8 +667,8 @@ class AgipdCorrections:
                 cell_cm = cell_cm_sum / cell_cm_count
 
                 # TODO: check files with less 256 trains
-                cell_cm[cell_cm_count < fraction * 32 * 256] = 0
-                asic_data[...] -= cell_cm[None, None, :, :]
+                cell_cm[cell_cm_count < fraction * 32 * asic_data.shape[0]] = 0
+                asic_data -= cell_cm[None, None, :, :]
 
                 # Asics common mode
                 asic_cm_sum, asic_cm_count = \
@@ -682,9 +677,9 @@ class AgipdCorrections:
                 asic_cm = asic_cm_sum / asic_cm_count
 
                 asic_cm[asic_cm_count < fraction * 64 * 64] = 0
-                asic_data[...] -= asic_cm[:, :, None, None]
+                asic_data -= asic_cm[:, :, None, None]
 
-                first = last
+            data[irow, :, xasic, :, yasic, :] = asic_data
 
     def mask_zero_std(self, i_proc, cells):
         """
-- 
GitLab