From 39467625388574a8b3907e492e172b78d2f21ed9 Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas@kluyver.me.uk>
Date: Tue, 9 May 2023 18:05:42 +0100
Subject: [PATCH] Rework get_mem_cell_order

---
 src/cal_tools/lpdlib.py | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/cal_tools/lpdlib.py b/src/cal_tools/lpdlib.py
index 872a52964..db063f08f 100644
--- a/src/cal_tools/lpdlib.py
+++ b/src/cal_tools/lpdlib.py
@@ -772,21 +772,32 @@ class LpdCorrections:
                         flat_fields)
 
 
-def get_mem_cell_order(run, sources) -> str:
+def get_mem_cell_order(run, sources, use_param) -> Optional[str]:
     """Load the memory cell order to use as a condition to find constants"""
-    res = set()
+    if use_param == 'never':
+        return None
+
+    patterns = []
     for source in sources:
         cell_id_data = run[source, 'image.cellId'].drop_empty_trains()
         if len(cell_id_data.train_ids) == 0:
             continue  # No data for this module
-        cell_ids = cell_id_data[0].ndarray()
-        # Trailing comma required so e.g. "...,1" doesn't match "...,10"
-        res.add(",".join([str(c) for c in cell_ids.flatten()]) + ",")
+        cell_ids = cell_id_data[0].ndarray().flatten()
+        if not any(np.array_equal(cell_ids, p) for p in patterns):
+            patterns.append(cell_ids)
 
-    if len(res) > 1:
+    if len(patterns) > 1:
         warn("Memory cell order varies between detector modules: "
-             "; ".join([f"{s[:10]}...{s[-10:]}" for s in res]))
-    elif not res:
+             "; ".join([f"{s[:10]}...{s[-10:]}" for s in patterns]))
+    elif not patterns:
         raise ValueError("Couldn't find memory cell order for any modules")
 
-    return res.pop()
+    cellid_pattern = patterns[0]
+
+    use = (use_param == 'always') or (
+        # auto: use cell order if it wraps around (cell IDs not monotonic)
+        len(cellid_pattern) > 2 and (np.diff(cellid_pattern) < 0).any()
+    )
+
+    # Trailing comma required so e.g. "...,1" doesn't match "...,10"
+    return ",".join([str(c) for c in cellid_pattern]) + "," if use else None
-- 
GitLab