def create_h5(array: Array5D, axiskeys_style: str, chunk_shape: Optional[Shape5D] = None, axiskeys: str = "xyztc"): raw_chunk_shape = (chunk_shape or Shape5D() * 2).clamped( maximum=array.shape).to_tuple(axiskeys) path = tempfile.mkstemp()[1] + ".h5" f = h5py.File(path, "w") ds = f.create_dataset("data", chunks=raw_chunk_shape, data=array.raw(axiskeys)) if axiskeys_style == "dims": for key, dim in zip(axiskeys, ds.dims): # type: ignore dim.label = key elif axiskeys_style == "vigra": type_flags = {"x": 2, "y": 2, "z": 2, "t": 2, "c": 1} axistags = [{ "key": key, "typeflags": type_flags[key], "resolution": 0, "description": "" } for key in axiskeys] ds.attrs["axistags"] = json.dumps({"axes": axistags}) else: raise Exception(f"Bad axiskeys_style: {axiskeys_style}") return Path(path)
def test_writing_to_offset_precomputed_chunks(tmp_path: Path, data: Array5D): datasource = ArrayDataSource.from_array5d(data, tile_shape=Shape5D(x=10, y=10), location=Point5D(x=1000, y=1000)) scale = PrecomputedChunksScale.from_datasource(datasource=datasource, key=Path("my_test_data"), encoding=RawEncoder()) sink_path = Path("mytest.precomputed") filesystem = OsFs(tmp_path.as_posix()) info = PrecomputedChunksInfo( data_type=datasource.dtype, type_="image", num_channels=datasource.shape.c, scales=tuple([scale]), ) datasink = PrecomputedChunksSink.create( filesystem=filesystem, base_path=sink_path, info=info, ).scale_sinks[0] for tile in datasource.roi.get_datasource_tiles(): datasink.write(tile.retrieve()) precomp_datasource = PrecomputedChunksDataSource( path=sink_path, filesystem=filesystem, resolution=scale.resolution) reloaded_data = precomp_datasource.retrieve() assert (reloaded_data.raw("xyz") == data.raw("xyz")).all() # type: ignore
def from_array5d(cls, arr: Array5D, *, tile_shape: Optional[Shape5D] = None, location: Point5D = Point5D.zero()): return cls(data=arr.raw(Point5D.LABELS), axiskeys=Point5D.LABELS, location=location, tile_shape=tile_shape)
def create_png(array: Array5D) -> Path: png_path = tempfile.mkstemp()[1] + ".png" skimage.io.imsave(png_path, array.raw("yxc")) # type: ignore return Path(png_path)
def from_array5d(data: Array5D, labels: Optional[Set[int]] = None) -> "ConnectedComponents": return ConnectedComponents( data.raw(Point5D.LABELS), axiskeys=Point5D.LABELS, location=data.location, labels=labels )
def array5d_to_vigra(arr: Array5D, axiskeys: str): return vigra.taggedView(arr.raw(axiskeys), axistags=axiskeys)