Skip to content

webknossos.dataset.layer

class Layer:

A Layer consists of multiple MagViews, which store the same data in different magnifications.

Do not use this constructor manually. Instead use Dataset.add_layer() to create a Layer.

path: pathlib.Path
name: str

Renames the layer to layer_name. This changes the name of the directory on disk and updates the properties. Only layers on local file systems can be renamed.

Updates the offset and size of the bounding box of this layer in the properties.

category: Literal['color', 'segmentation']
dtype_per_layer: str
dtype_per_channel: numpy.dtype
num_channels: int
default_view_configuration: Union[webknossos.dataset.properties.LayerViewConfiguration, NoneType]
read_only: bool

Getter for dictionary containing all mags.

def get_mag( self, mag: Union[int, str, list, tuple, numpy.ndarray, webknossos.geometry.mag.Mag]) -> webknossos.dataset.mag_view.MagView:

Returns the MagView called mag of this layer. The return type is webknossos.dataset.mag_view.MagView.

This function raises an IndexError if the specified mag does not exist.

def get_finest_mag(self) -> webknossos.dataset.mag_view.MagView:
def get_best_mag(self) -> webknossos.dataset.mag_view.MagView:

Deprecated, please use get_finest_mag.

def add_mag( self, mag: Union[int, str, list, tuple, numpy.ndarray, webknossos.geometry.mag.Mag], chunk_shape: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], int, NoneType] = None, chunks_per_shard: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], int, NoneType] = None, compress: bool = False, *, chunk_size: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], int, NoneType] = None, block_len: Union[int, NoneType] = None, file_len: Union[int, NoneType] = None) -> webknossos.dataset.mag_view.MagView:

Creates a new mag called and adds it to the layer. The parameter chunk_shape, chunks_per_shard and compress can be specified to adjust how the data is stored on disk. 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. Alternatively, you can call mag.compress() after all the data was written

The return type is webknossos.dataset.mag_view.MagView.

Raises an IndexError if the specified mag already exists.

def add_mag_for_existing_files( self, mag: Union[int, str, list, tuple, numpy.ndarray, webknossos.geometry.mag.Mag]) -> webknossos.dataset.mag_view.MagView:

Creates a new mag based on already existing files.

Raises an IndexError if the specified mag does not exists.

def get_or_add_mag( self, mag: Union[int, str, list, tuple, numpy.ndarray, webknossos.geometry.mag.Mag], chunk_shape: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], int, NoneType] = None, chunks_per_shard: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], int, NoneType] = None, compress: Union[bool, NoneType] = None, *, chunk_size: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], int, NoneType] = None, block_len: Union[int, NoneType] = None, file_len: Union[int, NoneType] = None) -> webknossos.dataset.mag_view.MagView:

Creates a new mag called and adds it to the dataset, in case it did not exist before. Then, returns the mag.

See add_mag for more information.

def delete_mag( self, mag: Union[int, str, list, tuple, numpy.ndarray, webknossos.geometry.mag.Mag]) -> None:

Deletes the MagView from the datasource-properties.json and the data from disk.

This function raises an IndexError if the specified mag does not exist.

def add_copy_mag( self, foreign_mag_view_or_path: Union[os.PathLike, str, webknossos.dataset.mag_view.MagView], extend_layer_bounding_box: bool = True, chunk_shape: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], int, NoneType] = None, chunks_per_shard: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], int, NoneType] = None, compress: Union[bool, NoneType] = None, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None) -> webknossos.dataset.mag_view.MagView:

Copies the data at foreign_mag_view_or_path which can belong to another dataset to the current dataset. Additionally, the relevant information from the datasource-properties.json of the other dataset are copied, too.

def add_fs_copy_mag( self, foreign_mag_view_or_path: Union[os.PathLike, str, webknossos.dataset.mag_view.MagView], extend_layer_bounding_box: bool = True) -> webknossos.dataset.mag_view.MagView:

Copies the data at foreign_mag_view_or_path which belongs to another dataset to the current dataset via the filesystem. Additionally, the relevant information from the datasource-properties.json of the other dataset are copied, too.

def downsample( self, from_mag: Union[webknossos.geometry.mag.Mag, NoneType] = None, coarsest_mag: Union[webknossos.geometry.mag.Mag, NoneType] = None, interpolation_mode: str = 'default', compress: bool = True, sampling_mode: Union[str, webknossos.dataset.sampling_modes.SamplingModes] = <SamplingModes.ANISOTROPIC: 'anisotropic'>, align_with_other_layers: Union[bool, webknossos.dataset.dataset.Dataset] = True, buffer_shape: Union[webknossos.geometry.vec3_int.Vec3Int, NoneType] = None, force_sampling_scheme: bool = False, args: Union[argparse.Namespace, NoneType] = None, allow_overwrite: bool = False, only_setup_mags: bool = False, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None) -> None:

Downsamples the data starting from from_mag until a magnification is >= max(coarsest_mag). There are three different sampling_modes:

  • 'anisotropic' - The next magnification is chosen so that the width, height and depth of a downsampled voxel assimilate. For example, if the z resolution is worse than the x/y resolution, z won't be downsampled in the first downsampling step(s). As a basis for this method, the voxel_size from the datasource-properties.json is used.
  • 'isotropic' - Each dimension is downsampled equally.
  • 'constant_z' - The x and y dimensions are downsampled equally, but the z dimension remains the same.

See downsample_mag for more information.

Example:

from webknossos import SamplingModes

# ...
# let 'layer' be a [`Layer`](../../webknossos/dataset/layer.html#Layer) with only `Mag(1)`
assert "1" in self.mags.keys()

layer.downsample(
    coarsest_mag=Mag(4),
    sampling_mode=SamplingModes.ISOTROPIC
)

assert "2" in self.mags.keys()
assert "4" in self.mags.keys()
def downsample_mag( self, from_mag: webknossos.geometry.mag.Mag, target_mag: webknossos.geometry.mag.Mag, interpolation_mode: str = 'default', compress: bool = True, buffer_shape: Union[webknossos.geometry.vec3_int.Vec3Int, NoneType] = None, args: Union[argparse.Namespace, NoneType] = None, allow_overwrite: bool = False, only_setup_mag: bool = False, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None) -> None:

Performs a single downsampling step from from_mag to target_mag.

The supported interpolation_modes are:

  • "median"
  • "mode"
  • "nearest"
  • "bilinear"
  • "bicubic"

If allow_overwrite is True, an existing Mag may be overwritten.

If only_setup_mag is True, the magnification is created, but left empty. This parameter can be used to prepare for parallel downsampling of multiple layers while avoiding parallel writes with outdated updates to the datasource-properties.json file.

executor can be passed to allow distrubuted computation, parallelizing across chunks. args is deprecated.

def redownsample( self, interpolation_mode: str = 'default', compress: bool = True, buffer_shape: Union[webknossos.geometry.vec3_int.Vec3Int, NoneType] = None, args: Union[argparse.Namespace, NoneType] = None, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None) -> None:

Use this method to recompute downsampled magnifications after mutating data in the base magnification.

def downsample_mag_list( self, from_mag: webknossos.geometry.mag.Mag, target_mags: List[webknossos.geometry.mag.Mag], interpolation_mode: str = 'default', compress: bool = True, buffer_shape: Union[webknossos.geometry.vec3_int.Vec3Int, NoneType] = None, args: Union[argparse.Namespace, NoneType] = None, allow_overwrite: bool = False, only_setup_mags: bool = False, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None) -> None:

Downsamples the data starting at from_mag to each magnification in target_mags iteratively.

See downsample_mag for more information.

def upsample( self, from_mag: webknossos.geometry.mag.Mag, finest_mag: webknossos.geometry.mag.Mag = Mag(1), compress: bool = False, sampling_mode: Union[str, webknossos.dataset.sampling_modes.SamplingModes] = <SamplingModes.ANISOTROPIC: 'anisotropic'>, align_with_other_layers: Union[bool, webknossos.dataset.dataset.Dataset] = True, buffer_shape: Union[webknossos.geometry.vec3_int.Vec3Int, NoneType] = None, buffer_edge_len: Union[int, NoneType] = None, args: Union[argparse.Namespace, NoneType] = None, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None, *, min_mag: Union[webknossos.geometry.mag.Mag, NoneType] = None) -> None:

Upsamples the data starting from from_mag as long as the magnification is >= finest_mag. There are three different sampling_modes:

  • 'anisotropic' - The next magnification is chosen so that the width, height and depth of a downsampled voxel assimilate. For example, if the z resolution is worse than the x/y resolution, z won't be downsampled in the first downsampling step(s). As a basis for this method, the voxel_size from the datasource-properties.json is used.
  • 'isotropic' - Each dimension is downsampled equally.
  • 'constant_z' - The x and y dimensions are downsampled equally, but the z dimension remains the same.

min_mag is deprecated, please use finest_mag instead.

class SegmentationLayer(Layer):

A Layer consists of multiple MagViews, which store the same data in different magnifications.

largest_segment_id: Union[int, NoneType]
category: Literal['color', 'segmentation']
def refresh_largest_segment_id( self, chunk_shape: Union[webknossos.geometry.vec3_int.Vec3Int, NoneType] = None, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None) -> None:

Sets the largest segment id to the highest value in the data. largest_segment_id is set to None if the data is empty.