Skip to content

Commit

Permalink
Fixed bugs in asdask() and GeneratorSource, pinned some versions in p…
Browse files Browse the repository at this point in the history
…ip requirements
  • Loading branch information
folterj committed Feb 10, 2024
1 parent 05f15fa commit 013c87f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### Version 0.6.7
- Fixed bugs in asdask() and GeneratorSource
- Pinned some versions in pip requirements

#### Version 0.6.6
- Rewritten TiffSource using dask/zarr functionality
- Added high level Dask function
Expand Down
20 changes: 10 additions & 10 deletions OmeSliCC/GeneratorSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ def calc_color(self, *args, **kwargs):
return np.stack(channels, axis=-1)

def get_tile(self, indices, tile_size=None):
# tile in (z,),y,x,c
# indices / tile size in x,y(,z,...)
if not tile_size:
tile_size = np.flip(self.tile_shape)
range0 = np.flip(indices)
range1 = np.min([range0 + tile_size, self.size], 0)
range0 = indices
range1 = np.min([np.array(indices) + np.array(tile_size), self.size], 0)
shape = list(reversed(range1 - range0))
tile = np.fromfunction(self.calc_color, shape, dtype=int, range0=range0)
# apply noise to each channel separately
Expand All @@ -77,14 +77,14 @@ def get_tile(self, indices, tile_size=None):
def _asarray_level(self, level: int, x0: float = 0, y0: float = 0, x1: float = -1, y1: float = -1,
c: int = None, z: int = None, t: int = None) -> np.ndarray:
# ignore c
indices = [y0, x0]
tile_size = [y1 - y0, x1 - x0]
indices = [x0, y0]
tile_size = [x1 - x0, y1 - y0]
if z:
indices = [0] + indices
tile_size = [1] + tile_size
indices += [0]
tile_size += [1]
if t:
indices = [0] + indices
tile_size = [1] + tile_size
indices += [0]
tile_size += [1]
data = self.get_tile(indices, tile_size)
if not z:
data = np.expand_dims(data, 0)
Expand All @@ -95,7 +95,7 @@ def _asarray_level(self, level: int, x0: float = 0, y0: float = 0, x1: float = -


if __name__ == '__main__':
# (tile) size in x,y(,z)
# (tile) size in x,y(,z,...)
size = 256, 256, 256
tile_size = 256, 256, 1
dtype = np.uint8
Expand Down
19 changes: 13 additions & 6 deletions OmeSliCC/OmeSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ def asdask(self, chunk_size: tuple) -> da.Array:
while len(chunk_shape) < 5:
chunk_shape = [1] + chunk_shape
chunks = np.ceil(np.flip(self.get_size_xyzct()) / chunk_shape).astype(int)
w, h = self.get_size()

delayed_reader = dask.delayed(self.asarray)
dtype = self.get_pixel_type()
Expand All @@ -365,18 +366,24 @@ def asdask(self, chunk_size: tuple) -> da.Array:
for yi in range(chunks[3]):
dask_tiles = []
for xi in range(chunks[4]):
x0, x1 = xi * chunk_shape[4], (xi + 1) * chunk_shape[4]
y0, y1 = yi * chunk_shape[3], (yi + 1) * chunk_shape[3]
z = zi * chunk_shape[2]
t = ti * chunk_shape[0]
dask_tile = da.from_delayed(delayed_reader(x0, y0, x1, y1, z=z, t=t), shape=chunk_shape,
shape = list(chunk_shape).copy()
x0, x1 = xi * shape[4], (xi + 1) * shape[4]
y0, y1 = yi * shape[3], (yi + 1) * shape[3]
if x1 > w:
x1 = w
shape[4] = w - x0
if y1 > h:
y1 = h
shape[3] = h - y0
z = zi * shape[2]
t = ti * shape[0]
dask_tile = da.from_delayed(delayed_reader(x0, y0, x1, y1, z=z, t=t), shape=shape,
dtype=dtype)
dask_tiles.append(dask_tile)
dask_rows.append(da.concatenate(dask_tiles, axis=4))
dask_planes.append(da.concatenate(dask_rows, axis=3))
dask_times.append(da.concatenate(dask_planes, axis=1))
dask_data = da.concatenate(dask_times, axis=0)

return dask_data

def clone_empty(self) -> np.ndarray:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "OmeSliCC"
version = "0.6.6"
version = "0.6.7"
description = "Ome(ro) Slide Image Conversion and Compression pipeline"
readme = "README.md"
license = {file = "LICENSE.md"}
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pandas
scikit-image
scikit-learn
opencv-python
imagecodecs
imagecodecs>=2023.3.16
numcodecs
tifffile
tifffile>=2023.9.26
zarr
aiohttp
omero-py
Expand Down
9 changes: 5 additions & 4 deletions tests/conversion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def load_as_zarr(path, x_um, y_um, w_um, h_um):


def save_source_as_ome_tiff(output, source, output_params):
data = source.asarray()
#data = source.asdask(output_params['tile_size'])
#data = source.asarray()
data = source.asdask(output_params['tile_size'])
save_image_as_tiff(source, data, output, output_params, ome=True)


Expand All @@ -50,9 +50,10 @@ def conversion_test():
path2 = 'test1.ome.zarr'
path3 = 'test2.ome.tiff'
size = [1024, 1024]
output_params = {'tile_size': [256, 256], 'npyramid_add': 3, 'pyramid_downsample': 2}
tile_size = [256, 256]
output_params = {'tile_size': tile_size, 'npyramid_add': 3, 'pyramid_downsample': 2}

source = create_source(size, [256, 256], np.uint8, [(1, 'um')])
source = create_source(size, tile_size, np.uint8, [(1, 'um')])
nchannels = source.get_nchannels()
save_source_as_ome_tiff(path1, source, output_params)

Expand Down

0 comments on commit 013c87f

Please sign in to comment.