From c361e0045c08b2ab0f3fafbf56cf405c7f7a1da3 Mon Sep 17 00:00:00 2001
From: Arin Wongprommoon <arin.wongprommoon@ed.ac.uk>
Date: Tue, 11 Oct 2022 18:15:57 +0100
Subject: [PATCH] fix(postproc/chainer): group signals apart from GFP

WHY IS THIS CHANGE NEEDED?:
- grouper (which usues chainer) breaks if it accesses a channel that is
  not GFP

HOW DOES THE CHANGE SOLVE THE PROBLEM?:
- chainer assumes all experiments have a GFP channel; this was
  hard-coded in line 29 in chainer.py

WHAT SIDE EFFECTS DOES THIS CHANGE HAVE?:
- yet to test

EVIDENCE THAT COMMIT WORKS:
- pipelines pending

REFERENCES:
- ...
---
 src/postprocessor/chainer.py | 66 ++++++++++++++++++++----------------
 1 file changed, 37 insertions(+), 29 deletions(-)

diff --git a/src/postprocessor/chainer.py b/src/postprocessor/chainer.py
index 25dabb1d..9400e765 100644
--- a/src/postprocessor/chainer.py
+++ b/src/postprocessor/chainer.py
@@ -22,38 +22,46 @@ class Chainer(Signal):
     """
 
     process_types = ("multisignal", "processes", "reshapers")
+    common_chains = {}
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
-        channel = [ch for ch in self.channels if re.match("GFP", ch)][0]
-        if (
-            channel == "GFPFast" and "mCherry" in self.channels
-        ):  # Use mCherry for Batman if available
-            channel = "mCherry"
-
-        equivalences = {
-            "m5m": (
-                f"extraction/{channel}/max/max5px",
-                f"extraction/{channel}/max/median",
-            )
-        }
-
-        def replace_url(url: str, bgsub: str = ""):
-            # return pattern with bgsub
-            channel = url.split("/")[1]
-            if "bgsub" in bgsub:
-                url = re.sub(channel, f"{channel}_bgsub", url)
-            return url
-
-        self.common_chains = {
-            alias
-            + bgsub: lambda **kwargs: self.get(
-                replace_url(denominator, alias + bgsub), **kwargs
-            )
-            / self.get(replace_url(numerator, alias + bgsub), **kwargs)
-            for alias, (denominator, numerator) in equivalences.items()
-            for bgsub in ("", "_bgsub")
-        }
+        for channel in self.candidate_channels:
+            try:
+                channel = [
+                    ch for ch in self.channels if re.match("channel", ch)
+                ][0]
+                break
+            except:
+                pass
+
+        try:
+            equivalences = {
+                "m5m": (
+                    f"extraction/{channel}/max/max5px",
+                    f"extraction/{channel}/max/median",
+                ),
+            }
+
+            def replace_url(url: str, bgsub: str = ""):
+                # return pattern with bgsub
+                channel = url.split("/")[1]
+                if "bgsub" in bgsub:
+                    url = re.sub(channel, f"{channel}_bgsub", url)
+                return url
+
+            self.common_chains = {
+                alias
+                + bgsub: lambda **kwargs: self.get(
+                    replace_url(denominator, alias + bgsub), **kwargs
+                )
+                / self.get(replace_url(numerator, alias + bgsub), **kwargs)
+                for alias, (denominator, numerator) in equivalences.items()
+                for bgsub in ("", "_bgsub")
+            }
+
+        except:
+            pass
 
     def get(
         self,
-- 
GitLab