Skip to content

Convert 4D Tiff

This example demonstrates the basic interactions with Datasets that have more than three dimensions.

In order to manipulate 4D data in WEBKNOSSOS, we first convert the 4D Tiff dataset into a Zarr3 dataset. This conversion is achieved using the from_images method.

Once the dataset is converted, we can access specific layers and views, read data from a defined bounding box, and write data to a different position within the dataset. The NDBoundingBox is utilized to select a 4D region of the dataset.

from pathlib import Path

import webknossos as wk


def main() -> None:
    # Create a WEBKNOSSOS dataset from a 4D tiff image
    dataset = wk.Dataset.from_images(
        Path(__file__).parent.parent / "testdata" / "4D" / "4D_series",
        "testoutput/4D_series",
        voxel_size=(10, 10, 10),
        data_format="zarr3",
        use_bioformats=True,
    )

    # Access the first color layer and the Mag 1 view of this layer
    layer = dataset.get_color_layers()[0]
    mag_view = layer.get_finest_mag()

    # To get the bounding box of the dataset use layer.bounding_box
    # -> NDBoundingBox(topleft=(0, 0, 0, 0), size=(7, 5, 167, 439), axes=('t', 'z', 'y', 'x'))

    # Read all data of the dataset
    data = mag_view.read()
    # data.shape -> (1, 7, 5, 167, 439) # first value is the channel dimension

    # Read data for a specific time point (t=3) of the dataset
    data = mag_view.read(
        absolute_bounding_box=layer.bounding_box.with_bounds("t", 3, 1)
    )
    # data.shape -> (1, 1, 5, 167, 439)

    # Create a NDBoundingBox to read data from a specific region of the dataset
    read_bbox = wk.NDBoundingBox(
        topleft=(2, 0, 67, 39),
        size=(2, 5, 100, 400),
        axes=("t", "z", "y", "x"),
        index=(1, 2, 3, 4),
    )
    data = mag_view.read(absolute_bounding_box=read_bbox)
    # data.shape -> (1, 2, 5, 100, 400) # first value is the channel dimension

    # Write some data to a given position
    mag_view.write(data, absolute_bounding_box=read_bbox.offset((2, 0, 0, 0)))


if __name__ == "__main__":
    main()