From caea291c6582f5ae3adfb2c5101ea525832f1e0f Mon Sep 17 00:00:00 2001 From: Christopher Rhodes <christopher.rhodes@embl.de> Date: Fri, 20 Sep 2024 11:58:24 +0200 Subject: [PATCH] Can create RoiSet from bounding boxes; now working on patch-level accessors --- model_server/base/roiset.py | 41 +++++++++++++++---------------------- tests/base/test_roiset.py | 9 +++++--- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/model_server/base/roiset.py b/model_server/base/roiset.py index ed8f44f8..fa622930 100644 --- a/model_server/base/roiset.py +++ b/model_server/base/roiset.py @@ -414,39 +414,32 @@ class RoiSet(object): # deproject if zi is not specified if bbox_zi is None: - def _slice_zmax(r): - acc_raw.crop_hw( - r.y0, - r.x0, - r.y1 - r.y0, - r.x1 - r.x0 - ) - # TODO: use accessor.get_z_argmax - zmax = acc_raw.data.argmax(axis=3, keepdims=True)[:, :, 0, 0].astype('uint16') + dch = params.deproject_channel + if dch is None or dch >= acc_raw.chroma or dch < 0: + if acc_raw.chroma == 1: + dch = 0 + else: + raise NoDeprojectChannelSpecifiedError( + f'When labeling objects, either their z-coordinates or a valid deprojection channel are required.' + ) + bbox_df['zi'] = acc_raw.get_mono(dch).get_focus_vector().argmax() else: bbox_df['zi'] = bbox_zi + bbox_df['y0'] = bbox_df['y'] + bbox_df['x0'] = bbox_df['x'] + bbox_df['y1'] = bbox_df['y0'] + bbox_df['h'] + bbox_df['x1'] = bbox_df['x0'] + bbox_df['w'] + + df = df_insert_slices( - bbox_df, + bbox_df[['y0', 'x0', 'y1', 'x1', 'zi']], acc_raw.shape_dict, - params.get('expand_box_by', 0) + params.expand_box_by, ) # TODO: don't even make binary_mask column in obj det case - # def _make_binary_mask(r): - # # acc = InMemoryDataAccessor(acc_obj_ids.data == r.label) - # # cropped = acc.get_mono(0, mip=True).crop_hw((r.y0, r.x0, (r.y1 - r.y0), (r.x1 - r.x0))).data_xy - # return cropped - # - # - # - # df['binary_mask'] = df.apply( - # _make_binary_mask, - # axis=1, - # result_type='reduce', - # ) - return RoiSet(acc_raw, df, params) diff --git a/tests/base/test_roiset.py b/tests/base/test_roiset.py index 497bcb03..6a516be0 100644 --- a/tests/base/test_roiset.py +++ b/tests/base/test_roiset.py @@ -645,22 +645,25 @@ class TestRoiSetObjectDetection(unittest.TestCase): table = pd.DataFrame( regionprops_table(labels) ).rename( - columns={'bbox-0': 'y0', 'bbox-1': 'x0', 'bbox-2': 'zi', 'bbox-3': 'y1', 'bbox-4': 'x1'} + columns={'bbox-0': 'y', 'bbox-1': 'x', 'bbox-2': 'zi', 'bbox-3': 'y1', 'bbox-4': 'x1'} ).drop( columns=['bbox-5'] ) - table['w'] = table['x1'] - table['x0'] - table['h'] = table['y1'] - table['y0'] + table['w'] = table['x1'] - table['x'] + table['h'] = table['y1'] - table['y'] + bboxes = table[['y', 'x', 'h', 'w']].to_dict(orient='records') roiset = RoiSet.from_bounding_boxes( self.stack_ch_pa, + bboxes, ) # test segments reside in bounding boxes self.assertFalse(roiset.contains_segmentation) + patches = roiset.get_patches_acc() self.assertTrue(False) class TestRoiSetPolygons(BaseTestRoiSetMonoProducts, unittest.TestCase): -- GitLab