def test_neighboring_tiles(): # fmt: off arr = Array5D(np.asarray( [[10, 11, 12, 20, 21, 22, 30], [13, 14, 15, 23, 24, 25, 33], [16, 17, 18, 26, 27, 28, 36], [40, 41, 42, 50, 51, 52, 60], [43, 44, 45, 53, 54, 55, 63], [46, 47, 48, 56, 57, 58, 66], [70, 71, 72, 80, 81, 82, 90], [73, 74, 75, 83, 84, 85, 93], [76, 77, 78, 86, 87, 88, 96], [0, 1, 2, 3, 4, 5, 6]], dtype=np.uint8), axiskeys="yx") ds = DataSource.create(create_png(arr)) fifties_slice = DataSourceSlice(ds).clamped( Slice5D(x=slice(3, 6), y=slice(3, 6))) expected_fifties_slice = Array5D(np.asarray([[50, 51, 52], [53, 54, 55], [56, 57, 58]]), axiskeys="yx") # fmt: on top_slice = DataSourceSlice(ds, x=slice(3, 6), y=slice(0, 3)) bottom_slice = DataSourceSlice(ds, x=slice(3, 6), y=slice(6, 9)) right_slice = DataSourceSlice(ds, x=slice(6, 7), y=slice(3, 6)) left_slice = DataSourceSlice(ds, x=slice(0, 3), y=slice(3, 6)) # fmt: off fifties_neighbor_data = { top_slice: Array5D(np.asarray([[20, 21, 22], [23, 24, 25], [26, 27, 28]]), axiskeys="yx"), right_slice: Array5D(np.asarray([[60], [63], [66]]), axiskeys="yx"), bottom_slice: Array5D(np.asarray([[80, 81, 82], [83, 84, 85], [86, 87, 88]]), axiskeys="yx"), left_slice: Array5D(np.asarray([[40, 41, 42], [43, 44, 45], [46, 47, 48]]), axiskeys="yx"), } expected_fifties_neighbors = {} # fmt: on assert (fifties_slice.retrieve().raw("yx") == expected_fifties_slice.raw( "yx")).all() for neighbor in fifties_slice.get_neighboring_tiles( tile_shape=Shape5D(x=3, y=3)): try: expected_slice = fifties_neighbor_data.pop(neighbor) assert (expected_slice.raw("yx") == neighbor.retrieve().raw("yx") ).all() except KeyError: print(f"\nWas searching for ", neighbor, "\n") for k in fifties_neighbor_data.keys(): print("--->>> ", k) assert len(fifties_neighbor_data) == 0
def test_h5_datasource(): data_2d = Array5D(np.arange(100).reshape(10, 10), axiskeys="yx") h5_path = create_h5(data_2d, axiskeys_style="vigra", chunk_shape=Shape5D(x=3, y=3)) ds = DataSource.create(h5_path) assert ds.shape == data_2d.shape assert ds.tile_shape == Shape5D(x=3, y=3) slc = Slice5D(x=slice(0, 3), y=slice(0, 2)) assert (ds.retrieve(slc).raw("yx") == data_2d.cut(slc).raw("yx")).all() data_3d = Array5D(np.arange(10 * 10 * 10).reshape(10, 10, 10), axiskeys="zyx") h5_path = create_h5(data_3d, axiskeys_style="vigra", chunk_shape=Shape5D(x=3, y=3)) ds = DataSource.create(h5_path) assert ds.shape == data_3d.shape assert ds.tile_shape == Shape5D(x=3, y=3) slc = Slice5D(x=slice(0, 3), y=slice(0, 2), z=3) assert (ds.retrieve(slc).raw("yxz") == data_3d.cut(slc).raw("yxz")).all()
def test_retrieve_roi_smaller_than_tile(): # fmt: off data = Array5D(np.asarray([ [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]], [[100, 200, 300, 400, 500], [600, 700, 800, 900, 1000], [1100, 1200, 1300, 1400, 1500], [1600, 1700, 1800, 1900, 2000]], ]).astype(np.uint32), axiskeys="cyx") # fmt: on path = Path(create_n5(data, chunk_size=Shape5D(c=2, y=4, x=4))) ds = DataSource.create(path) print(f"\n\n====>> tile shape: {ds.roi}") smaller_than_tile = ds.retrieve( Slice5D.all(c=1, y=slice(0, 4), x=slice(0, 4))) print(smaller_than_tile.raw("cyx"))
def test_n5_datasource(): # fmt: off data = Array5D(np.asarray([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]).astype(np.uint8), axiskeys="yx") # fmt: on path = Path(create_n5(data)) ds = DataSource.create(path) assert ds.shape == data.shape # fmt: off expected_raw_piece = Array5D(np.asarray([[1, 2, 3], [6, 7, 8]]).astype(np.uint8), axiskeys="yx") # fmt: on assert ds.retrieve(Slice5D(x=slice(0, 3), y=slice(0, 2))) == expected_raw_piece ds2 = pickle.loads(pickle.dumps(ds)) assert ds2.retrieve(Slice5D(x=slice(0, 3), y=slice(0, 2))) == expected_raw_piece
def tile_equals(tile: DataSource, axiskeys: str, raw: np.ndarray): return (tile.retrieve().raw(axiskeys) == raw).all()