def test_custom_initialization() -> None:

    assert Vec3Int.zeros() == Vec3Int(0, 0, 0)
    assert Vec3Int.ones() == Vec3Int(1, 1, 1)
    assert Vec3Int.full(4) == Vec3Int(4, 4, 4)

    assert Vec3Int.ones() - Vec3Int.ones() == Vec3Int.zeros()
    assert Vec3Int.full(4) == Vec3Int.ones() * 4
Exemplo n.º 2
0
def test_buffered_slice_writer() -> None:
    test_img = np.arange(24 * 24).reshape(24, 24).astype(np.uint16) + 1
    dtype = test_img.dtype
    origin = Vec3Int.zeros()
    layer_name = "color"
    mag = Mag(1)
    dataset_dir = TESTOUTPUT_DIR / "buffered_slice_writer"
    dataset_path = str(dataset_dir / layer_name / mag.to_layer_name())

    rmtree(dataset_dir)
    ds = Dataset(dataset_dir, voxel_size=(1, 1, 1))
    mag_view = ds.add_layer("color", COLOR_CATEGORY,
                            dtype_per_channel=dtype).add_mag(mag)

    with mag_view.get_buffered_slice_writer(absolute_offset=origin) as writer:
        for i in range(13):
            writer.send(test_img)
        with wkw.Dataset.open(dataset_path, wkw.Header(dtype)) as data:
            try:
                read_data = data.read(origin, (24, 24, 13))
                if read_data[read_data.nonzero()].size != 0:
                    raise AssertionError(
                        "Nothing should be written on the disk. But found data with shape: {}"
                        .format(read_data.shape))
            except wkw.wkw.WKWException:
                pass

        for i in range(13, 32):
            writer.send(test_img)
        with wkw.Dataset.open(dataset_path, wkw.Header(dtype)) as data:
            read_data = data.read(origin, (24, 24, 32))
            assert np.squeeze(read_data).shape == (24, 24, 32), (
                "The read data should have the shape: (24, 24, 32) "
                "but has a shape of: {}".format(np.squeeze(read_data).shape))
            assert read_data.size == read_data[read_data.nonzero()].size, (
                "The read data contains zeros while the "
                "written image has no zeros")

        for i in range(32, 35):
            writer.send(test_img)

    with wkw.Dataset.open(dataset_path, wkw.Header(dtype)) as data:
        read_data = data.read(origin, (24, 24, 35))
        read_data = np.squeeze(read_data)
        assert read_data.shape == (24, 24, 35), (
            "The read data should have the shape: (24, 24, 35) "
            "but has a shape of: {}".format(np.squeeze(read_data).shape))
        assert read_data.size == read_data[read_data.nonzero()].size, (
            "The read data contains zeros while the "
            "written image has no zeros")
        test_img_3d = np.zeros((test_img.shape[0], test_img.shape[1], 35))
        for i in np.arange(35):
            test_img_3d[:, :, i] = test_img
        # check if the data are correct
        assert np.array_equal(
            test_img_3d, read_data), ("The data from the disk is not the same "
                                      "as the data that should be written.")
Exemplo n.º 3
0
    def __init__(
        self,
        view: "View",
        offset: Optional[Vec3IntLike] = None,
        # buffer_size specifies, how many slices should be aggregated until they are flushed.
        buffer_size: int = 32,
        dimension: int = 2,  # z
        *,
        relative_offset: Optional[Vec3IntLike] = None,  # in mag1
        absolute_offset: Optional[Vec3IntLike] = None,  # in mag1
        use_logging: bool = False,
    ) -> None:
        """see `View.get_buffered_slice_writer()`"""

        self.view = view
        self.buffer_size = buffer_size
        self.dtype = self.view.get_dtype()
        self.use_logging = use_logging
        if offset is None and relative_offset is None and absolute_offset is None:
            relative_offset = Vec3Int.zeros()
        if offset is not None:
            warnings.warn(
                "[DEPRECATION] Using offset for a buffered slice writer is deprecated. "
                + "Please use the parameter relative_offset or absolute_offset in Mag(1) instead.",
                DeprecationWarning,
            )
        self.offset = None if offset is None else Vec3Int(offset)
        self.relative_offset = (
            None if relative_offset is None else Vec3Int(relative_offset)
        )
        self.absolute_offset = (
            None if absolute_offset is None else Vec3Int(absolute_offset)
        )
        self.dimension = dimension

        assert 0 <= dimension <= 2

        self.buffer: List[np.ndarray] = []
        self.current_slice: Optional[int] = None
        self.buffer_start_slice: Optional[int] = None