|
1 | 1 | import numpy as np |
2 | | -from wkcuber.utils import get_chunks, get_regular_chunks |
| 2 | +from wkcuber.utils import get_chunks, get_regular_chunks, BufferedSliceWriter |
| 3 | +import wkw |
| 4 | +from wkcuber.mag import Mag |
| 5 | +import os |
3 | 6 |
|
4 | 7 | BLOCK_LEN = 32 |
5 | 8 |
|
@@ -27,3 +30,58 @@ def test_get_regular_chunks_max_inclusive(): |
27 | 30 | assert list(target[0]) == list(range(4, 5)) |
28 | 31 | # The last chunk should include 44 |
29 | 32 | assert list(target[-1]) == list(range(44, 45)) |
| 33 | + |
| 34 | + |
| 35 | +def test_buffered_slice_writer(): |
| 36 | + test_img = np.arange(24 * 24).reshape(24, 24).astype(np.uint16) + 1 |
| 37 | + dtype = test_img.dtype |
| 38 | + bbox = {'topleft': (0, 0, 0), 'size': (24, 24, 35)} |
| 39 | + origin = [0, 0, 0] |
| 40 | + dataset_dir = 'testoutput/buffered_slice_writer' |
| 41 | + layer_name = 'color' |
| 42 | + mag = Mag(1) |
| 43 | + dataset_path = os.path.join(dataset_dir, layer_name, mag.to_layer_name()) |
| 44 | + |
| 45 | + with BufferedSliceWriter(dataset_dir, layer_name, dtype, bbox, origin, mag=mag) as writer: |
| 46 | + for i in range(13): |
| 47 | + writer.write_slice(i, test_img) |
| 48 | + with wkw.Dataset.open(dataset_path, wkw.Header(dtype)) as data: |
| 49 | + try: |
| 50 | + read_data = data.read(origin, (24, 24, 13)) |
| 51 | + if read_data[read_data.nonzero()].size != 0: |
| 52 | + raise AssertionError('Nothing should be written on the disk. But found data with shape: {}' |
| 53 | + .format(read_data.shape)) |
| 54 | + except wkw.wkw.WKWException: |
| 55 | + pass |
| 56 | + |
| 57 | + for i in range(13, 32): |
| 58 | + writer.write_slice(i, test_img) |
| 59 | + with wkw.Dataset.open(dataset_path, wkw.Header(dtype)) as data: |
| 60 | + read_data = data.read(origin, (24, 24, 32)) |
| 61 | + assert np.squeeze(read_data).shape == (24, 24, 32), "The read data should have the shape: (24, 24, 32) " \ |
| 62 | + "but has a shape of: {}"\ |
| 63 | + .format(np.squeeze(read_data).shape) |
| 64 | + assert read_data.size == read_data[read_data.nonzero()].size, "The read data contains zeros while the " \ |
| 65 | + "written image has no zeros" |
| 66 | + |
| 67 | + for i in range(32, 35): |
| 68 | + writer.write_slice(i, test_img) |
| 69 | + |
| 70 | + with wkw.Dataset.open(dataset_path, wkw.Header(dtype)) as data: |
| 71 | + read_data = data.read(origin, (24, 24, 35)) |
| 72 | + read_data = np.squeeze(read_data) |
| 73 | + assert read_data.shape == (24, 24, 35), "The read data should have the shape: (24, 24, 35) " \ |
| 74 | + "but has a shape of: {}"\ |
| 75 | + .format(np.squeeze(read_data).shape) |
| 76 | + assert read_data.size == read_data[read_data.nonzero()].size, "The read data contains zeros while the " \ |
| 77 | + "written image has no zeros" |
| 78 | + test_img_3d = np.zeros((test_img.shape[0], test_img.shape[1], 35)) |
| 79 | + for i in np.arange(35): |
| 80 | + test_img_3d[:, :, i] = test_img |
| 81 | + # transpose because the slice writer takes [y, x] data and transposes it to [x, y] before writing |
| 82 | + test_img_3d = np.transpose(test_img_3d, (1, 0, 2)) |
| 83 | + # check if the data are correct |
| 84 | + assert np.array_equal(test_img_3d, read_data), "The data from the disk is not the same " \ |
| 85 | + "as the data that should be written." |
| 86 | + |
| 87 | + |
0 commit comments