def get_data_lazy(image: ImageWrapper, c_index: int = 0) -> da.Array: """Get n-dimensional dask array, with delayed reading from OMERO image.""" size_z = image.getSizeZ() size_t = image.getSizeT() size_x = image.getSizeX() size_y = image.getSizeY() pixels = image.getPrimaryPixels() @delayed @timer def get_plane(plane_name): z, c, t = [int(n) for n in plane_name.split(",")] p = pixels.getPlane(z, c, t) return p dtype = PIXEL_TYPES.get(pixels.getPixelsType().value, None) plane_names = [ f"{z},{c_index},{t}" for t in range(size_t) for z in range(size_z) ] lazy_arrays = [get_plane(pn) for pn in plane_names] dask_arrays = [ da.from_delayed(delayed_reader, shape=(size_y, size_x), dtype=dtype) for delayed_reader in lazy_arrays ] # Stack into one large dask.array if size_z == 1 or size_t == 1: return da.stack(dask_arrays, axis=0) z_stacks = [] for t in range(size_t): z_stacks.append( da.stack(dask_arrays[t * size_z:(t + 1) * size_z], axis=0)) stack = da.stack(z_stacks, axis=0) return stack
def get_data_lazy(image: ImageWrapper) -> da.Array: """Get 5D dask array, with delayed reading from OMERO image.""" nt, nc, nz, ny, nx = [getattr(image, f'getSize{x}')() for x in 'TCZYX'] pixels = image.getPrimaryPixels() dtype = PIXEL_TYPES.get(pixels.getPixelsType().value, None) get_plane = delayed(timer(lambda idx: pixels.getPlane(*idx))) def get_lazy_plane(zct): return da.from_delayed(get_plane(zct), shape=(ny, nx), dtype=dtype) # 5D stack: TCZXY t_stacks = [] for t in range(nt): c_stacks = [] for c in range(nc): z_stack = [] for z in range(nz): z_stack.append(get_lazy_plane((z, c, t))) c_stacks.append(da.stack(z_stack)) t_stacks.append(da.stack(c_stacks)) return da.stack(t_stacks)