From 61130b780f5d597f89308781a062018238aeb516 Mon Sep 17 00:00:00 2001
From: Christopher Rhodes <christopher.rhodes@embl.de>
Date: Tue, 10 Oct 2023 15:45:12 +0200
Subject: [PATCH] Corrected slicing mistake that caused error in edge cases

---
 extensions/chaeo/annotators.py | 2 +-
 extensions/chaeo/products.py   | 6 ++----
 extensions/chaeo/zmask.py      | 7 +++++--
 3 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/extensions/chaeo/annotators.py b/extensions/chaeo/annotators.py
index 0c25647e..10019d2b 100644
--- a/extensions/chaeo/annotators.py
+++ b/extensions/chaeo/annotators.py
@@ -63,5 +63,5 @@ def draw_contours_on_patch(patch, contours, linewidth=1):
     pilimg = Image.fromarray(patch)  # drawing modifies array in-place
     draw = ImageDraw.Draw(pilimg)
     for co in contours:
-        draw.line([(p[1], p[0]) for p in co], width=linewidth)
+        draw.line([(p[1], p[0]) for p in co], width=linewidth, joint='curve')
     return np.array(pilimg)
\ No newline at end of file
diff --git a/extensions/chaeo/products.py b/extensions/chaeo/products.py
index bab25537..fe53dbfb 100644
--- a/extensions/chaeo/products.py
+++ b/extensions/chaeo/products.py
@@ -154,10 +154,8 @@ def export_patches_from_zstack(
         if kwargs.get('draw_contour'):
             mci = kwargs.get('contour_channel', 0)
             mask = np.zeros(patch.shape[0:2], dtype=bool)
-            try:
-                mask[sp_sl[0:2]] = mi['mask']
-            except Exception:
-                a=1
+            mask[sp_sl[0:2]] = mi['mask']
+
             for zi in range(0, patch.shape[3]):
                 patch[:, :, mci, zi] = draw_contours_on_patch(
                     patch[:, :, mci, zi],
diff --git a/extensions/chaeo/zmask.py b/extensions/chaeo/zmask.py
index f624beec..ecca8b2f 100644
--- a/extensions/chaeo/zmask.py
+++ b/extensions/chaeo/zmask.py
@@ -90,9 +90,9 @@ def build_zmask_from_object_mask(
     meta = []
     for ob in df[df['keeper']].itertuples(name='LabeledObject'):
         y0 = max(ob.y0 - ebxy, 0)
-        y1 = min(ob.y1 + ebxy, h - 1)
+        y1 = min(ob.y1 + ebxy, h)
         x0 = max(ob.x0 - ebxy, 0)
-        x1 = min(ob.x1 + ebxy, w - 1)
+        x1 = min(ob.x1 + ebxy, w)
         z0 = max(ob.zi - ebz, 0)
         z1 = min(ob.zi + ebz, nz)
 
@@ -111,6 +111,9 @@ def build_zmask_from_object_mask(
         contour = find_contours(obmask)
         mask = obmask[ob.y0: ob.y1, ob.x0: ob.x1]
 
+        assert rbb['x1'] <= (x1 - x0)
+        assert rbb['y1'] <= (y1 - y0)
+
         meta.append({
             'info': ob,
             'slice': sl,
-- 
GitLab