def test_n_cores(self): with make_case(np.uint16, (10, 20, 30), return_path=True) as (glob_expr, dest, volume): main([ "--source", glob_expr, "--dest", dest, "--levels", "2", "--format", "tiff", "--n-cores", "1" ])
def test_zarr(self): with make_case(np.uint16, (128, 128, 128), return_path=True) as (glob_expr, dest, volume): main([ "--source", glob_expr, "--dest", dest, "--levels", "2", "--format", "zarr" ]) store = zarr.NestedDirectoryStore(os.path.join(dest, "1_1_1")) z_arr = zarr.open(store, "r") self.assertSequenceEqual(z_arr.shape, (128, 128, 128))
def test_tiff(self): with make_case(np.uint16, (256, 256, 256), return_path=True) as (glob_expr, dest, volume): main([ "--source", glob_expr, "--dest", dest, "--levels", "2", "--format", "tiff" ]) main([ "--source", glob_expr, "--dest", dest, "--levels", "2", "--format", "tiff", "--chunk-size", "128,128,128" ])
def test_write_chunk_128(self): with make_case(np.uint16, (200, 200, 200), return_path=True) \ as (glob_expr, dest, stack): bfs = BlockfsStack(glob_expr, dest, chunk_size=(128, 128, 128)) bfs.write_level_1(silent=True, n_cores=1) directory_filename = \ os.path.join(dest, "1_1_1", BlockfsStack.DIRECTORY_FILENAME) directory = Directory.open(directory_filename) np.testing.assert_array_equal(stack[:128, :128, :128], directory.read_block(0, 0, 0)) np.testing.assert_array_equal(stack[128:, 128:, 128:], directory.read_block(128, 128, 128))
def test_write_oddly_shaped(self): with make_case(np.uint16, (100, 100, 100), return_path=True)\ as (glob_expr, dest, stack): bfs = BlockfsStack(glob_expr, dest) bfs.write_level_1(silent=True, n_cores=1) directory_filename =\ os.path.join(dest, "1_1_1", BlockfsStack.DIRECTORY_FILENAME) directory = Directory.open(directory_filename) np.testing.assert_array_equal(stack[:64, :64, :64], directory.read_block(0, 0, 0)) np.testing.assert_array_equal(stack[64:, 64:, 64:], directory.read_block(64, 64, 64))
def test_write_level_1(self): with make_case(np.uint16, (100, 200, 300)) as (stack, npstack): stack.write_level_1() block_0_64_256 = tifffile.imread( os.path.join(stack.dest, "1_1_1", "256-300_64-128_0-64.tiff")) np.testing.assert_equal(block_0_64_256, npstack[:64, 64:128, 256:]) for (x0, x1), (y0, y1), (z0, z1) in itertools.product( zip(stack.x0(1), stack.x1(1)), zip(stack.y0(1), stack.y1(1)), zip(stack.z0(1), stack.z1(1))): path = stack.fname(1, x0, x1, y0, y1, z0, z1) block = tifffile.imread(path) np.testing.assert_equal(block, npstack[z0:z1, y0:y1, x0:x1])
def test_write_level_2(self): with make_case(np.uint16, (100, 201, 300)) as (stack, npstack): stack.write_level_1() stack.write_level_n(2) block = tifffile.imread( os.path.join(stack.dest, "2_2_2", "128-150_64-101_0-50.tiff")) self.assertSequenceEqual(block.shape, (50, 101 - 64, 150 - 128)) s32 = npstack.astype(np.uint32) first = (s32[0, 128, 256] + s32[1, 128, 256] + s32[0, 129, 256] + s32[1, 129, 256] + s32[0, 128, 257] + s32[1, 128, 257] + s32[0, 129, 257] + s32[1, 129, 257]) // 8 self.assertEqual(block[0, 0, 0], first) last = (s32[-2, -1, -2] + s32[-1, -1, -2] + s32[-2, -1, -1] + s32[-1, -1, -1]) // 4 self.assertEqual(block[-1, -1, -1], last)
def test_write_level_1(self): with make_case(np.uint16, (100, 200, 300), klass=NGFFStack) \ as (stack, npstack): stack.create() stack.write_info_file(1) stack.write_level_1() z_arr = stack.zgroup["0"] block_0_64_256 = z_arr[0, 0, :64, 64:128, 256:] np.testing.assert_equal(block_0_64_256, npstack[:64, 64:128, 256:]) for (x0, x1), (y0, y1), (z0, z1) in itertools.product( zip(stack.x0(1), stack.x1(1)), zip(stack.y0(1), stack.y1(1)), zip(stack.z0(1), stack.z1(1))): block = z_arr[0, 0, z0:z1, y0:y1, x0:x1] np.testing.assert_equal(block, npstack[z0:z1, y0:y1, x0:x1])
def test_write_level_1(self): with make_case(np.uint16, (100, 200, 300), klass=ZarrStack) \ as (stack, npstack): stack.write_level_1() dest_lvl1 = os.path.join(stack.dest, "1_1_1") store = zarr.NestedDirectoryStore(dest_lvl1) z_arr = zarr.open(store, mode='r') block_0_64_256 = z_arr[:64, 64:128, 256:] np.testing.assert_equal(block_0_64_256, npstack[:64, 64:128, 256:]) for (x0, x1), (y0, y1), (z0, z1) in itertools.product( zip(stack.x0(1), stack.x1(1)), zip(stack.y0(1), stack.y1(1)), zip(stack.z0(1), stack.z1(1))): block = z_arr[z0:z1, y0:y1, x0:x1] np.testing.assert_equal(block, npstack[z0:z1, y0:y1, x0:x1])
def test_write_info_file(self): with make_case(np.uint16, (101, 200, 300)) as (stack, npstack): stack.write_info_file(2) with open(os.path.join(stack.dest, "info")) as fd: info = json.load(fd) self.assertEqual(info["data_type"], "uint16") self.assertEqual(info["num_channels"], 1) scales = info["scales"] self.assertEqual(len(scales), 2) self.assertSequenceEqual(scales[0]["chunk_sizes"][0], (64, 64, 64)) self.assertEqual(scales[0]["encoding"], "raw") self.assertEqual(scales[0]["key"], "1_1_1") self.assertSequenceEqual(scales[0]["resolution"], (1800, 1800, 2000)) self.assertSequenceEqual(scales[0]["size"], (300, 200, 101)) self.assertSequenceEqual(scales[0]["voxel_offset"], (0, 0, 0)) self.assertSequenceEqual(scales[1]["size"], (150, 100, 51))
def test_coordinates(self): with make_case(np.uint16, (100, 200, 300)) as (stack, npstack): self.assertEqual(stack.n_x(1), 300 // 64 + 1) self.assertEqual(stack.n_x(2), 3) self.assertEqual(stack.n_x(3), 2) self.assertEqual(stack.n_x(4), 1) self.assertEqual(stack.n_x(5), 1) self.assertEqual(stack.n_y(1), 4) self.assertEqual(stack.n_y(2), 2) self.assertEqual(stack.n_z(1), 2) self.assertEqual(stack.n_z(2), 1) self.assertSequenceEqual(stack.z0(1).tolist(), (0, 64)) self.assertSequenceEqual(stack.z1(1).tolist(), (64, 100)) self.assertSequenceEqual(stack.y0(1).tolist(), (0, 64, 128, 192)) self.assertSequenceEqual(stack.y1(1).tolist(), (64, 128, 192, 200)) self.assertSequenceEqual(stack.x0(2).tolist(), (0, 64, 128)) self.assertSequenceEqual(stack.x1(2).tolist(), (64, 128, 150))
def test_write_level_2(self): with make_case(np.uint16, (100, 201, 300), klass=ZarrStack) \ as (stack, npstack): stack.write_level_1() stack.write_level_n(2) dest_lvl2 = os.path.join(stack.dest, "2_2_2") store = zarr.NestedDirectoryStore(dest_lvl2) z_arr = zarr.open(store, "r") block = z_arr[0:50, 64:101, 128:150] self.assertSequenceEqual(block.shape, (50, 101 - 64, 150 - 128)) s32 = npstack.astype(np.uint32) first = (s32[0, 128, 256] + s32[1, 128, 256] + s32[0, 129, 256] + s32[1, 129, 256] + s32[0, 128, 257] + s32[1, 128, 257] + s32[0, 129, 257] + s32[1, 129, 257]) // 8 self.assertEqual(block[0, 0, 0], first) last = (s32[-2, -1, -2] + s32[-1, -1, -2] + s32[-2, -1, -1] + s32[-1, -1, -1]) // 4 self.assertEqual(block[-1, -1, -1], last)
def test_blockfs(self): with make_case(np.uint16, (256, 256, 256), return_path=True) as \ (glob_expr, dest, volume): main([ "--source", glob_expr, "--dest", dest, "--levels", "2", "--format", "blockfs" ]) directory = Directory.open( os.path.join(dest, "1_1_1", BlockfsStack.DIRECTORY_FILENAME)) block = directory.read_block(0, 0, 0) np.testing.assert_array_equal(block, volume[:64, :64, :64]) main([ "--source", glob_expr, "--dest", dest, "--levels", "2", "--format", "blockfs", "--chunk-size", "128,128,128" ]) directory = Directory.open( os.path.join(dest, "1_1_1", BlockfsStack.DIRECTORY_FILENAME)) block = directory.read_block(0, 0, 0) np.testing.assert_array_equal(block, volume[:128, :128, :128])
def test_write_info_file(self): with make_case(np.uint16, (101, 200, 300), klass=NGFFStack) \ as (stack, npstack): stack.create() stack.write_info_file(2) scale_md = stack.zgroup.attrs["multiscales"][0] for i, dataset_md in enumerate(scale_md["datasets"]): self.assertEqual(str(i), dataset_md["path"]) with open(os.path.join(stack.dest, "info")) as fd: info = json.load(fd) self.assertEqual(info["data_type"], "uint16") self.assertEqual(info["num_channels"], 1) scales = info["scales"] self.assertEqual(len(scales), 2) self.assertSequenceEqual(scales[0]["chunk_sizes"][0], (64, 64, 64)) self.assertEqual(scales[0]["encoding"], "raw") self.assertEqual(scales[0]["key"], "1_1_1") self.assertSequenceEqual(scales[0]["resolution"], (1800, 1800, 2000)) self.assertSequenceEqual(scales[0]["size"], (300, 200, 101)) self.assertSequenceEqual(scales[0]["voxel_offset"], (0, 0, 0)) self.assertSequenceEqual(scales[1]["size"], (150, 100, 51))
def test_init(self): with make_case(np.uint16, (100, 200, 300)) as (stack, npstack): self.assertEqual(stack.x_extent, 300) self.assertEqual(stack.y_extent, 200) self.assertEqual(stack.z_extent, 100)