diff --git a/src/aliby/io/image.py b/src/aliby/io/image.py index 0bd418809d1baa352b9fcb3c9e327009195b351d..da563f3e9e357007962b9d40009110dd85dbae72 100644 --- a/src/aliby/io/image.py +++ b/src/aliby/io/image.py @@ -138,7 +138,12 @@ class ImageDummy(BaseLocalImage): # Goal: make Tiler happy. @staticmethod - def pad_array(image_array: da.Array, dim: int, n_empty_slices: int): + def pad_array( + image_array: da.Array, + dim: int, + n_empty_slices: int, + image_position: int = 0, + ): """Extends a dimension in a dask array and pads with zeros Extends a dimension in a dask array that has existing content, then pads @@ -153,19 +158,29 @@ class ImageDummy(BaseLocalImage): n_empty_slices : int Number of empty slices to extend the dask array by, in the specified dimension/axis. + image_position : int + Position within the new dimension to place the input arary, default 0 + (the beginning). Examples -------- ``` - extended_array = pad_array(my_da_array, dim = 2, n_empty_slices = 4) + extended_array = pad_array( + my_da_array, dim = 2, n_empty_slices = 4, image_position = 1) ``` Extends a dask array called `my_da_array` in the 3rd dimension - (dimensions start from 0) by 4 slices, filled with zeros. + (dimensions start from 0) by 4 slices, filled with zeros. And puts the + original content in slice 1 of the 3rd dimension """ # Concats zero arrays with same dimensions as image_array, and puts # image_array as first element in list of arrays to be concatenated + zeros_array = da.zeros_like(image_array) return da.concatenate( - [image_array, *([da.zeros_like(image_array)] * n_empty_slices)], + [ + *([zeros_array] * image_position), + image_array, + *([zeros_array] * (n_empty_slices - image_position)), + ], axis=dim, ) @@ -192,7 +207,9 @@ class ImageDummy(BaseLocalImage): img, dim=0, n_empty_slices=199 ) # 200 timepoints total img = self.pad_array(img, dim=1, n_empty_slices=2) # 3 channels - img = self.pad_array(img, dim=2, n_empty_slices=4) # 5 z-stacks + img = self.pad_array( + img, dim=2, n_empty_slices=4, image_position=self.ref_z + ) # 5 z-stacks return img def name(self): diff --git a/src/aliby/tile/tiler.py b/src/aliby/tile/tiler.py index 9de61621c8a4806fda217d880698b2aebe09e997..93ae8cc4d7c5683ab75c4011cd55e15adacfc57d 100644 --- a/src/aliby/tile/tiler.py +++ b/src/aliby/tile/tiler.py @@ -258,6 +258,7 @@ class Tiler(StepABC): """ imgdmy_obj = ImageDummy(parameters) dummy_image = imgdmy_obj.get_data_lazy() + # FIXME: TECHNICAL DEBT: hard-coding dimension orders dummy_omero_metadata = { "size_x": dummy_image.shape[3], "size_y": dummy_image.shape[4], @@ -265,8 +266,8 @@ class Tiler(StepABC): "size_c": dummy_image.shape[1], "size_t": dummy_image.shape[0], "channels": [ - *(["nil"] * (dummy_image.shape[1] - 1)), parameters["ref_channel"], + *(["nil"] * (dummy_image.shape[1] - 1)), ], "name": " ", } diff --git a/tests/aliby/pipeline/test_image.py b/tests/aliby/pipeline/test_image.py index d8343a5dd7b245856fe1cb320e8430a73ea11001..3a2114006c0bd2703bbfed159ee99201853bae05 100644 --- a/tests/aliby/pipeline/test_image.py +++ b/tests/aliby/pipeline/test_image.py @@ -17,23 +17,27 @@ sample_da = da.reshape( @pytest.mark.parametrize("sample_da", [sample_da]) @pytest.mark.parametrize("dim", [2]) @pytest.mark.parametrize("n_empty_slices", [4]) -def test_pad_array(sample_da, dim, n_empty_slices): +@pytest.mark.parametrize("image_position", [1]) +def test_pad_array(sample_da, dim, n_empty_slices, image_position): """Test ImageDummy.pad_array() method""" # create object imgdmy = ImageDummy(tiler_parameters) # pads array padded_da = imgdmy.pad_array( - sample_da, dim=dim, n_empty_slices=n_empty_slices + sample_da, + dim=dim, + n_empty_slices=n_empty_slices, + image_position=image_position, ) # select which dimension to index the multidimensional array - indices = {dim: 0} + indices = {dim: image_position} ix = [ indices.get(dim, slice(None)) for dim in range(padded_da.compute().ndim) ] - # Checks that original image array is there and is at the first index + # Checks that original image array is there and is at the correct index assert np.array_equal(padded_da.compute()[ix], sample_da.compute()[0]) # Checks that the additional axis is extended correctly assert padded_da.compute().shape[dim] == n_empty_slices + 1