From 4f53ce0cb420bd026cd5734e2cc543d454b11265 Mon Sep 17 00:00:00 2001
From: Christopher Rhodes <christopher.rhodes@embl.de>
Date: Thu, 5 Oct 2023 17:46:01 +0200
Subject: [PATCH] Prototyping adaptive sobel-based projection within 3d patch
 export

---
 extensions/chaeo/examples/batch_run_patches.py |  5 +++--
 extensions/chaeo/workflows.py                  | 10 ++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/extensions/chaeo/examples/batch_run_patches.py b/extensions/chaeo/examples/batch_run_patches.py
index 2900f060..52b01490 100644
--- a/extensions/chaeo/examples/batch_run_patches.py
+++ b/extensions/chaeo/examples/batch_run_patches.py
@@ -47,7 +47,7 @@ if __name__ == '__main__':
             'where_output': where_output.__str__(),
             'mask_type': 'boxes',
             'zmask_filters': {'area': (1e3, 1e8)},
-            'zmask_expand_box_by': (128, 0),
+            'zmask_expand_box_by': (128, 3),
         }
 
         result = export_patches_from_multichannel_zstack(**export_kwargs)
@@ -65,4 +65,5 @@ if __name__ == '__main__':
             write_accessor_data_to_file(
                 where_output / k / (ff.stem + '.tif'),
                 InMemoryDataAccessor(result['interm'][k])
-            )
\ No newline at end of file
+            )
+        break
\ No newline at end of file
diff --git a/extensions/chaeo/workflows.py b/extensions/chaeo/workflows.py
index 23eab18f..85b16d8b 100644
--- a/extensions/chaeo/workflows.py
+++ b/extensions/chaeo/workflows.py
@@ -60,6 +60,15 @@ def export_patches_from_multichannel_zstack(
     ti.click('generate_zmasks')
 
     # export patches
+    import numpy as np
+    from skimage.filters import gaussian, sobel
+
+    def zs_projector(zs):
+        sigma = 1.5
+        blur = gaussian(sobel(zs), sigma)
+        argmax = np.argmax(blur, axis=3, keepdims=True)
+        return np.take_along_axis(zs, argmax, axis=3)
+
     files = export_patches_from_zstack(
         Path(where_output) / '2d_patches',
         stack.get_one_channel_data(patches_channel),
@@ -67,6 +76,7 @@ def export_patches_from_multichannel_zstack(
         prefix=fstem,
         draw_bounding_box=True,
         rescale_clip=0.0,
+        projector=zs_projector,
     )
     ti.click('export_patches')
 
-- 
GitLab