From 2491ddf1f4c2c5f80948828e3fdbdcaa00876845 Mon Sep 17 00:00:00 2001
From: Christopher Rhodes <christopher.rhodes@embl.de>
Date: Thu, 21 Dec 2023 14:50:25 +0100
Subject: [PATCH] Can make patch stack from itself

---
 extensions/chaeo/accessors.py            | 10 +++++++---
 extensions/chaeo/tests/test_accessors.py |  8 ++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/extensions/chaeo/accessors.py b/extensions/chaeo/accessors.py
index 60cc2c70..c24ab7d6 100644
--- a/extensions/chaeo/accessors.py
+++ b/extensions/chaeo/accessors.py
@@ -12,9 +12,13 @@ class MonoPatchStack(InMemoryDataAccessor):
         :param data: either np.ndarray of dimensions YXn, or a list of np.ndarrays of size YX
         """
 
-        if isinstance(data, np.ndarray): # interpret as YXZ
-            assert data.ndim == 3
-            self._data = np.expand_dims(data, 2)
+        if isinstance(data, np.ndarray):
+            if data.ndim == 3:  # interpret as YXZ
+                self._data = np.expand_dims(data, 2)
+            elif data.ndim == 4:  # interpret as a copy another patch stack
+                self._data = data
+            else:
+                raise InvalidDataForPatchStackError()
         elif isinstance(data, list): # list of YX patches
             if len(data) == 0:
                 self._data = np.ndarray([0, 0, 0, 0], dtype='uin9')
diff --git a/extensions/chaeo/tests/test_accessors.py b/extensions/chaeo/tests/test_accessors.py
index c2194581..52935598 100644
--- a/extensions/chaeo/tests/test_accessors.py
+++ b/extensions/chaeo/tests/test_accessors.py
@@ -28,6 +28,14 @@ class TestCziImageFileAccess(unittest.TestCase):
         self.assertEqual(acc.count, n)
         self.assertEqual(acc.hw, (h, w))
         self.assertEqual(acc.make_tczyx().shape, (n, 1, 1, h, w))
+        return acc
+
+    def test_make_patch_stack_clone(self):
+        w = 256
+        h = 512
+        n = 4
+        acc = MonoPatchStack([np.random.rand(h, w) for _ in range(0, n)])
+        self.assertEqual(MonoPatchStack(acc.data).shape, acc.shape)
 
     def test_make_patch_stack_from_file(self):
         h = monozstackmask['h']
-- 
GitLab