From a2c2fa51993c8a91a455c3c9cd50af0f32eba96b Mon Sep 17 00:00:00 2001
From: Christopher Rhodes <christopher.rhodes@embl.de>
Date: Thu, 12 Oct 2023 15:09:44 +0200
Subject: [PATCH] Corrected translational error in mask file output

---
 extensions/chaeo/products.py          | 20 ++++++++++++++++----
 extensions/chaeo/tests/test_zstack.py | 14 +++++++++++++-
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/extensions/chaeo/products.py b/extensions/chaeo/products.py
index 7c97784b..d01ceb15 100644
--- a/extensions/chaeo/products.py
+++ b/extensions/chaeo/products.py
@@ -57,6 +57,7 @@ def _write_patch_to_file(where, fname, data):
 
 def export_patch_masks_from_zstack(
     where: Path,
+    stack: GenericImageDataAccessor,
     zmask_meta: list,
     pad_to: int = 256,
     prefix='mask',
@@ -64,15 +65,26 @@ def export_patch_masks_from_zstack(
     exported = []
     for mi in zmask_meta:
         obj = mi['info']
-        mask = np.expand_dims(mi['mask'], (2, 3))
+        sl = mi['slice']
+
+        rbb = mi['relative_bounding_box']
+        x0 = rbb['x0']
+        y0 = rbb['y0']
+        x1 = rbb['x1']
+        y1 = rbb['y1']
+
+        sp_sl = np.s_[y0: y1, x0: x1, :, :]
+
+        h, w = stack.data[sl].shape[0:2]
+        patch = np.zeros((h, w, 1, 1), dtype='uint8')
+        patch[sp_sl][:, :, 0, 0] = mi['mask'] * 255
 
         if pad_to:
-            mask = pad(mask, pad_to)
+            patch = pad(patch, pad_to)
 
         ext = 'png'
         fname = f'{prefix}-la{obj.label:04d}-zi{obj.zi:04d}.{ext}'
-        mask8bit = 255 * mask.astype('uint8')
-        _write_patch_to_file(where, fname, mask8bit)
+        _write_patch_to_file(where, fname, patch)
         exported.append(fname)
     return exported
 
diff --git a/extensions/chaeo/tests/test_zstack.py b/extensions/chaeo/tests/test_zstack.py
index 35f14666..453ea4b3 100644
--- a/extensions/chaeo/tests/test_zstack.py
+++ b/extensions/chaeo/tests/test_zstack.py
@@ -5,7 +5,7 @@ import numpy as np
 from conf.testing import output_path
 
 from extensions.chaeo.conf.testing import multichannel_zstack, pixel_classifier, pipeline_params
-from extensions.chaeo.products import export_patches_from_zstack, export_multichannel_patches_from_zstack
+from extensions.chaeo.products import export_patches_from_zstack, export_multichannel_patches_from_zstack, export_patch_masks_from_zstack
 from extensions.chaeo.zmask import build_zmask_from_object_mask
 from model_server.accessors import generate_file_accessor, InMemoryDataAccessor, write_accessor_data_to_file
 from extensions.ilastik.models import IlastikObjectClassifierModel, IlastikPixelClassifierModel
@@ -164,4 +164,16 @@ class TestZStackDerivedDataProducts(unittest.TestCase):
             contour_channel=1,
             overlay_gain=(0.1, 1.0, 1.0)
         )
+        self.assertGreaterEqual(len(files), 1)
+
+    def test_make_binary_masks_from_zmask(self):
+        zmask, meta = self.test_zmask_makes_correct_boxes(
+            filters={'area': (1e3, 1e4)},
+            expand_box_by=(128, 2)
+        )
+        files = export_patch_masks_from_zstack(
+            output_path / '2d_mask_patches',
+            InMemoryDataAccessor(self.stack.data),
+            meta,
+        )
         self.assertGreaterEqual(len(files), 1)
\ No newline at end of file
-- 
GitLab