From 691a9fcef635303fecbfebfbb16824460ff95294 Mon Sep 17 00:00:00 2001
From: Christopher Rhodes <christopher.rhodes@embl.de>
Date: Thu, 28 Mar 2024 18:50:25 +0100
Subject: [PATCH] Passes, long warnings that convex hull calcs don't work

---
 model_server/base/roiset.py | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/model_server/base/roiset.py b/model_server/base/roiset.py
index 4607b72b..503c5b66 100644
--- a/model_server/base/roiset.py
+++ b/model_server/base/roiset.py
@@ -154,18 +154,17 @@ class RoiSet(object):
         return self._df.itertuples(name='Roi')
 
     @staticmethod
-    def make_df(acc_raw, acc_obj_ids, expand_box_by, deproject=True) -> pd.DataFrame:
+    def make_df(acc_raw, acc_obj_ids, expand_box_by) -> pd.DataFrame:
         """
         Build dataframe associate object IDs with summary stats
         :param acc_raw: accessor to raw image data
         :param acc_obj_ids: accessor to map of object IDs
         :param expand_box_by: number of pixels to expand bounding box in all directions (without exceeding image boundary)
-        :param deproject: assign object's z-position based on argmax of raw data if True
+        # :param deproject: assign object's z-position based on argmax of raw data if True
         :return: pd.DataFrame
         """
         # build dataframe of objects, assign z index to each object
-        if deproject:
-            assert acc_obj_ids.nz == 1, 'Can only deproject a 2D object identity map'
+        if acc_obj_ids.nz == 1:  # deproject objects' z-coordinates from argmax of raw image
             argmax = acc_raw.data.argmax(axis=3, keepdims=True)[:, :, 0, 0].astype('uint16')
             df = (
                 pd.DataFrame(
@@ -180,7 +179,7 @@ class RoiSet(object):
                 )
             )
             df['zi'] = df['intensity_mean'].round().astype('int')
-        else:
+        else:  # objects' z-coordinates come from arg of max count in object identities map
             df = (
                 pd.DataFrame(
                     regionprops_table(
@@ -192,7 +191,10 @@ class RoiSet(object):
                     columns={'bbox-0': 'y0', 'bbox-1': 'x0', 'bbox-2': 'y1', 'bbox-3': 'x1', }
                 )
             )
-            df['zi'] = ... # from somewhere in obmap floor of avg z for each label...
+            def _get_zi_from_obj_map_counts(la):
+                return (acc_obj_ids.data == la).sum(axis=(0, 1, 2)).argmax()
+
+            df['zi'] = df['label'].apply(_get_zi_from_obj_map_counts)
 
 
         # compute expanded bounding boxes
-- 
GitLab