From a1a94faa72f3fbaa5a00c560c65bb99d9be92d09 Mon Sep 17 00:00:00 2001
From: Arin Wongprommoon <arin.wongprommoon@ed.ac.uk>
Date: Fri, 2 Dec 2022 16:30:01 +0000
Subject: [PATCH] fix!(aliby): handles alternative ref channel name when
 segmenting

WHY IS THIS CHANGE NEEDED?:
- ValueError raised when pipeline is run (see ref).

HOW DOES THE CHANGE SOLVE THE PROBLEM?:
(i'm still not 100% clear how the change solved the problem, but i
think this is happening...)
- BabyRunner class in aliby.baby_client assumed that the brightfield
  channel is always called 'Brightfield'; this is not true for
  experiment staffa:470 that is responsible for this bug
- i added an argument to the class so that the brightfield channel name
  string can be specified.  this extends to the classmethod as the only
  instance this class is used in the entire codebase is via this classmethod.

WHAT SIDE EFFECTS DOES THIS CHANGE HAVE?:
- breaks: i hard-coded the name of the brightfield channel
  'brightfield1' in pipeline.py because i couldn't think of another way to
  get it to work
- tech debt: fixing this commit exposes just how many times
  'Brightfield' is hard-coded.  this is fair because up till now, we've
  only had one name for any brightfield channel.  i suggest that the name of the
  brightfield channel be taken from the tiler parameters the user specifies.

EVIDENCE THAT COMMIT WORKS:
- run script starts segmenting without abortive errors

REFERENCES:
- issue #38
- ValueError: https://git.ecdf.ed.ac.uk/swain-lab/aliby/aliby/-/issues/38#note_113221
---
 src/aliby/baby_client.py | 10 ++++++----
 src/aliby/pipeline.py    |  1 +
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/aliby/baby_client.py b/src/aliby/baby_client.py
index 5731967b..bba33e4f 100644
--- a/src/aliby/baby_client.py
+++ b/src/aliby/baby_client.py
@@ -133,7 +133,9 @@ class BabyRunner:
 
     Does segmentation one time point at a time."""
 
-    def __init__(self, tiler, parameters=None, **kwargs):
+    def __init__(
+        self, tiler, bf_channel_name="Brightfield", parameters=None, **kwargs
+    ):
         self.tiler = tiler
         # self.model_config = modelsets()[choose_model_from_params(**kwargs)]
         self.model_config = (
@@ -143,11 +145,11 @@ class BabyRunner:
         )
         self.brain = BabyBrain(**self.model_config)
         self.crawler = BabyCrawler(self.brain)
-        self.bf_channel = self.tiler.get_channel_index("Brightfield")
+        self.bf_channel = self.tiler.get_channel_index(bf_channel_name)
 
     @classmethod
-    def from_tiler(cls, parameters: BabyParameters, tiler):
-        return cls(tiler, parameters)
+    def from_tiler(cls, parameters: BabyParameters, tiler, bf_channel_name):
+        return cls(tiler, bf_channel_name, parameters)
 
     def get_data(self, tp):
         # Swap axes x and z, probably shouldn't swap, just move z
diff --git a/src/aliby/pipeline.py b/src/aliby/pipeline.py
index 70cde680..1c119418 100644
--- a/src/aliby/pipeline.py
+++ b/src/aliby/pipeline.py
@@ -387,6 +387,7 @@ class Pipeline(ProcessABC):
                     steps["baby"] = BabyRunner.from_tiler(
                         BabyParameters.from_dict(config["baby"]),
                         steps["tiler"],
+                        "brightfield1",
                     )
                     if trackers_state:
                         steps["baby"].crawler.tracker_states = trackers_state
-- 
GitLab