Skip to content

Download Image Data in Blocks

When you read large parts of a remote dataset, the shape of your requests has a large effect on speed.

Read chunk-aligned blocks

The data of each mag is stored in chunks, which are grouped into shards. A chunk is the smallest unit that can be read: reading a single voxel transfers its whole chunk. Every request therefore costs at least the chunks it touches, plus some overhead per request.

Reading one XY plane at a time is the most common mistake: with 32-voxel-deep chunks, each plane transfers 32 planes' worth of data, and the same chunks may be fetched again for each of the next 31 planes. Instead, read 3D blocks that:

  • are a multiple of the chunk shape and start at chunk borders, so no chunk is read twice;
  • lie within a single shard, so a request doesn't need to access several shards;
  • still fit into your memory.

The chunk and shard shape of a mag are available via mag_view.info.chunk_shape and mag_view.info.shard_shape, in voxels of that mag.

Example

This example derives a block shape from the chunk and shard shape and reads the mag block by block with View.for_each_chunk(). It starts the blocks at multiples of the block shape, so they line up with the storage layout. Pass an executor (e.g. from cluster_tools.get_executor("multiprocessing")) to read several blocks in parallel. To collect a result per block, use View.map_chunk() instead.

import webknossos as wk

DATASET_NAME = "l4_sample"
LAYER_NAME = "color"
MAG = wk.Mag("4-4-1")


def process_block(args: tuple[wk.View, int]) -> None:
    block, _block_index = args
    data = block.read()  # shape: (channels, x, y, z)
    print(f"Read {block.bounding_box} with shape {data.shape}")
    # Process or save the block here, e.g. write its z-slices as images.


def main() -> None:
    dataset = wk.RemoteDataset.open(
        DATASET_NAME,
        organization_id="scalable_minds",
        webknossos_url="https://webknossos.org",
        # Usually much faster for downloads than ZARR_STREAMING
        access_mode=wk.RemoteAccessMode.PROXY_PATH,
    )
    mag_view = dataset.get_layer(LAYER_NAME).get_mag(MAG)

    # How the data is stored, in voxels of this mag
    chunk_shape = mag_view.info.chunk_shape
    shard_shape = mag_view.info.shard_shape
    print(f"Chunk shape: {chunk_shape}, shard shape: {shard_shape}")

    # Read a multiple of the chunk shape per request, but at most one shard.
    # Increase the factor if memory allows, e.g. to read a whole shard at once.
    block_shape = (chunk_shape * 8).pairmin(shard_shape)

    # Read only a part of the mag for this demo. Remove offset and size to read all.
    # Writable views only accept whole shards as blocks, so request a read-only one.
    view = mag_view.get_view(
        relative_offset=(0, 0, 0), size=(2048, 2048, 256), read_only=True
    )

    # `chunk_shape` is in mag-1 coordinates.
    # Pass an `executor` to read blocks in parallel.
    view.for_each_chunk(
        process_block,
        chunk_shape=block_shape * MAG.to_vec3_int(),
        progress_desc="Reading blocks",
    )


if __name__ == "__main__":
    main()

If you need the data slice by slice, e.g. to write an image sequence, the BufferedSliceReader reads chunk-aligned blocks of slices for you and returns them one at a time. See Download datasets as TIFF sequences.

Access mode

The example opens the dataset with the PROXY_PATH access mode. By default, RemoteDataset.open() uses ZARR_STREAMING, where the WEBKNOSSOS datastore decodes the stored data and re-serves it as small Zarr chunks. With PROXY_PATH, the datastore passes the stored chunks and shards through unchanged, which is often much faster for downloads. If your machine can reach the underlying storage itself, e.g. because you are using an enterprise WEBKNOSSOS instance running on-premise, DIRECT_PATH skips the datastore and is faster still. Annotations can only be read with ZARR_STREAMING.