Skip to content

webknossos.dataset.mag_view

A MagView contains all information about the data of a single magnification of a Layer. MagView inherits from View. The main difference is that the MagViewhas a reference to its Layer

Therefore, a MagView can write outside the specified bounding box (unlike a normal View), resizing the layer's bounding box. If necessary, the properties are automatically updated (e.g. if the bounding box changed).

MagView( layer: webknossos.dataset.layer.Layer, mag: webknossos.geometry.mag.Mag, chunk_shape: webknossos.geometry.vec3_int.Vec3Int, chunks_per_shard: webknossos.geometry.vec3_int.Vec3Int, compression_mode: bool, create: bool = False)

Do not use this constructor manually. Instead use webknossos.dataset.layer.Layer.add_mag().

⚠️ Deprecated, use Vec3Int.zeros() instead.

⚠️ Deprecated, use mag_view.bounding_box.in_mag(mag_view.mag).bottomright instead.

path: pathlib.Path
name: str
def get_zarr_array(self) -> webknossos.utils.NDArrayLike:

Directly access the underlying Zarr array. Only available for Zarr-based datasets.

def write( self, data: numpy.ndarray, offset: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], NoneType] = None, json_update_allowed: bool = True, *, relative_offset: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], NoneType] = None, absolute_offset: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], NoneType] = None, relative_bounding_box: Union[webknossos.geometry.nd_bounding_box.NDBoundingBox, NoneType] = None, absolute_bounding_box: Union[webknossos.geometry.nd_bounding_box.NDBoundingBox, NoneType] = None) -> None:

The user can specify where the data should be written. The default is to write the data to the view's bounding box. Alternatively, one can supply one of the following keywords:

  • relative_offset in Mag(1) -> only usable for 3D datasets
  • absolute_offset in Mag(1) -> only usable for 3D datasets
  • relative_bounding_box in Mag(1)
  • absolute_bounding_box in Mag(1)

⚠️ The offset parameter is deprecated. This parameter used to be relative for View and absolute for MagView, and specified in the mag of the respective view.

Writing data to a segmentation layer manually does not automatically update the largest_segment_id. To set the largest segment id properly run the refresh_largest_segment_id method on your layer or set the largest_segment_id property manually..

Example:

ds = Dataset(DS_PATH, voxel_size=(1, 1, 1))

segmentation_layer = cast(
    SegmentationLayer,
    ds.add_layer("segmentation", SEGMENTATION_CATEGORY),
)
mag = segmentation_layer.add_mag(Mag(1))

mag.write(data=MY_NP_ARRAY)

segmentation_layer.refresh_largest_segment_id()

Note that writing compressed data which is not aligned with the blocks on disk may result in diminished performance, as full blocks will automatically be read to pad the write actions.

def read( self, offset: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], NoneType] = None, size: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], NoneType] = None, *, relative_offset: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], NoneType] = None, absolute_offset: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], NoneType] = None, relative_bounding_box: Union[webknossos.geometry.nd_bounding_box.NDBoundingBox, NoneType] = None, absolute_bounding_box: Union[webknossos.geometry.nd_bounding_box.NDBoundingBox, NoneType] = None) -> numpy.ndarray:

The user can specify which data should be read. The default is to read all data of the view's bounding box. Alternatively, one can supply one of the following keyword argument combinations:

  • relative_offset and size, both in Mag(1)
  • absolute_offset and size, both in Mag(1)
  • relative_bounding_box in Mag(1)
  • absolute_bounding_box in Mag(1)
  • ⚠️ deprecated: offset and size, both in the current Mag. offset used to be relative for View and absolute for MagView

If the specified bounding box exceeds the data on disk, the rest is padded with 0.

Returns the specified data as a np.array.

Example:

import numpy as np

# ...
# let mag1 be a MagView
view = mag1.get_view(absolute_offset=(10, 20, 30), size=(100, 200, 300))

assert np.array_equal(
    view.read(absolute_offset=(0, 0, 0), size=(100, 200, 300)),
    view.read(),
)
def get_view( self, offset: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], NoneType] = None, size: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], NoneType] = None, *, relative_offset: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], NoneType] = None, absolute_offset: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], NoneType] = None, relative_bbox: Union[webknossos.geometry.nd_bounding_box.NDBoundingBox, NoneType] = None, absolute_bbox: Union[webknossos.geometry.nd_bounding_box.NDBoundingBox, NoneType] = None, read_only: Union[bool, NoneType] = None) -> webknossos.dataset.view.View:

Returns a view that is limited to the specified bounding box. The new view may exceed the bounding box of the current view only if read_only is set to True.

The default is to return the same view as the current bounding box, in case of a MagView that's the layer bounding box. One can supply one of the following keyword argument combinations:

  • relative_offset and size, both in Mag(1)
  • absolute_offset and size, both in Mag(1)
  • ⚠️ deprecated: offset and size, both in the current Mag. offset used to be relative for View and absolute for MagView

Example:

# ...
# let mag1 be a MagView
view = mag1.get_view(absolute_offset=(10, 20, 30), size=(100, 200, 300))

# works because the specified sub-view is completely in the bounding box of the view
sub_view = view.get_view(relative_offset=(50, 60, 70), size=(10, 120, 230))

# fails because the specified sub-view is not completely in the bounding box of the view
invalid_sub_view = view.get_view(relative_offset=(50, 60, 70), size=(999, 120, 230))

# works because `read_only=True`
valid_sub_view = view.get_view(relative_offset=(50, 60, 70), size=(999, 120, 230), read_only=True)
def get_bounding_boxes_on_disk(self) -> Iterator[webknossos.geometry.nd_bounding_box.NDBoundingBox]:

Returns a Mag(1) bounding box for each file on disk.

This differs from the bounding box in the properties, which is an "overall" bounding box, abstracting from the files on disk.

def get_views_on_disk( self, read_only: Union[bool, NoneType] = None) -> Iterator[webknossos.dataset.view.View]:

Yields a view for each file on disk, which can be used for efficient parallelization.

def compress( self, target_path: Union[str, pathlib.Path, NoneType] = None, args: Union[argparse.Namespace, NoneType] = None, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None) -> None:

Compresses the files on disk. This has consequences for writing data (see write).

The data gets compressed inplace, if target_path is None. Otherwise it is written to target_path/layer_name/mag.

Compressing mags on remote file systems requires a target_path.