Skip to content

webknossos.dataset.dataset

logger = <Logger webknossos.dataset.dataset (WARNING)>
class Dataset:

A dataset is the entry point of the Dataset API. An existing dataset on disk can be opened or new datasets can be created.

A dataset stores the data in .wkw files on disk with metadata in datasource-properties.json. The information in those files are kept in sync with the object.

Each dataset consists of one or more layers (webknossos.dataset.layer.Layer), which themselves can comprise multiple magnifications (webknossos.dataset.mag_view.MagView).

When using Dataset.open_remote() an instance of the RemoteDataset subclass is returned.

Dataset( dataset_path: Union[str, os.PathLike], voxel_size: Union[Tuple[float, float, float], NoneType] = None, name: Union[str, NoneType] = None, exist_ok: bool = _UNSET, *, scale: Union[Tuple[float, float, float], NoneType] = None, read_only: bool = False)

Creates a new dataset and the associated datasource-properties.json. If the dataset already exists and exist_ok is set to True, it is opened (the provided voxel_size and name are asserted to match the existing dataset). Currently, exist_ok=True is the deprecated default and will change in future releases. Please use Dataset.open if you intend to open an existing dataset and don't want/need the creation behavior. scale is deprecated, please use voxel_size instead.

path: pathlib.Path
@classmethod
def open( cls, dataset_path: Union[str, os.PathLike]) -> webknossos.dataset.dataset.Dataset:

To open an existing dataset on disk, simply call Dataset.open("your_path"). This requires datasource-properties.json to exist in this folder. Based on the datasource-properties.json, a dataset object is constructed. Only layers and magnifications that are listed in the properties are loaded (even though there might exist more layers or magnifications on disk).

The dataset_path refers to the top level directory of the dataset (excluding layer or magnification names).

@classmethod
def open_remote( cls, dataset_name_or_url: str, organization_id: Union[str, NoneType] = None, sharing_token: Union[str, NoneType] = None, webknossos_url: Union[str, NoneType] = None) -> webknossos.dataset.dataset.RemoteDataset:

Opens a remote webknossos dataset. Image data is accessed via network requests. Dataset metadata such as allowed teams or the sharing token can be read and set via the respective RemoteDataset properties.

  • dataset_name_or_url may be a dataset name or a full URL to a dataset view, e.g. https://webknossos.org/datasets/scalable_minds/l4_sample_dev/view If a URL is used, organization_id, webknossos_url and sharing_token must not be set.
  • organization_id may be supplied if a dataset name was used in the previous argument, it defaults to your current organization from the webknossos_context. You can find your organization_id here.
  • sharing_token may be supplied if a dataset name was used and can specify a sharing token.
  • webknossos_url may be supplied if a dataset name was used, and allows to specifiy in which webknossos instance to search for the dataset. It defaults to the url from your current webknossos_context, using https://webknossos.org as a fallback.
@classmethod
def download( cls, dataset_name_or_url: str, organization_id: Union[str, NoneType] = None, sharing_token: Union[str, NoneType] = None, webknossos_url: Union[str, NoneType] = None, bbox: Union[webknossos.geometry.bounding_box.BoundingBox, NoneType] = None, layers: Union[List[str], str, NoneType] = None, mags: Union[List[webknossos.geometry.mag.Mag], NoneType] = None, path: Union[os.PathLike, str, NoneType] = None, exist_ok: bool = False) -> webknossos.dataset.dataset.Dataset:

Downloads a dataset and returns the Dataset instance.

  • dataset_name_or_url may be a dataset name or a full URL to a dataset view, e.g. https://webknossos.org/datasets/scalable_minds/l4_sample_dev/view If a URL is used, organization_id, webknossos_url and sharing_token must not be set.
  • organization_id may be supplied if a dataset name was used in the previous argument, it defaults to your current organization from the webknossos_context. You can find your organization_id here.
  • sharing_token may be supplied if a dataset name was used and can specify a sharing token.
  • webknossos_url may be supplied if a dataset name was used, and allows to specifiy in which webknossos instance to search for the dataset. It defaults to the url from your current webknossos_context, using https://webknossos.org as a fallback.
  • bbox, layers, and mags specify which parts of the dataset to download. If nothing is specified the whole image, all layers, and all mags are downloaded respectively.
  • path and exist_ok specify where to save the downloaded dataset and whether to overwrite if the path exists.
@classmethod
def from_images( cls, input_path: Union[str, os.PathLike], output_path: Union[str, os.PathLike], voxel_size: Tuple[float, float, float], name: Union[str, NoneType] = None, *, map_filepath_to_layer_name: Union[webknossos.dataset.dataset.Dataset.ConversionLayerMapping, Callable[[pathlib.Path], str]] = <ConversionLayerMapping.INSPECT_SINGLE_FILE: 'inspect_single_file'>, z_slices_sort_key: Callable[[pathlib.Path], Any] = functools.partial(<function natsort_key at 0x71cdb0382d30>, key=None, string_func=<function parse_string_factory.<locals>.func at 0x71cda3375e50>, bytes_func=<function parse_bytes_factory.<locals>.<lambda> at 0x71cda3375ee0>, num_func=<function parse_number_factory.<locals>.func at 0x71cda3375f70>), layer_category: Union[Literal['color', 'segmentation'], NoneType] = None, data_format: Union[str, webknossos.dataset.data_format.DataFormat] = <DataFormat.WKW: 'wkw'>, 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, swap_xy: bool = False, flip_x: bool = False, flip_y: bool = False, flip_z: bool = False, use_bioformats: Union[bool, NoneType] = None, max_layers: int = 20, batch_size: Union[int, NoneType] = None, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None) -> webknossos.dataset.dataset.Dataset:

This method imports image data in a folder as a WEBKNOSSOS dataset. The image data can be 3D images (such as multipage tiffs) or stacks of 2D images. In case of multiple 3D images or image stacks, those are mapped to different layers. The exact mapping is handled by the argument map_filepath_to_layer_name, which can be a pre-defined strategy from the enum ConversionLayerMapping, or a custom callable, taking a path of an image file and returning the corresponding layer name. All files belonging to the same layer name are then grouped. In case of multiple files per layer, those are usually mapped to the z-dimension. The order of the z-slices can be customized by setting z_slices_sort_key.

The category of layers (color vs segmentation) is determined automatically by checking if segmentation is part of the path. Alternatively, a category can be enforced by passing layer_category.

Further arguments behave as in add_layer_from_images, please also refer to its documentation.

For more fine-grained control, please create an empty dataset and use add_layer_from_images.

layers: Dict[str, webknossos.dataset.layer.Layer]

Getter for dictionary containing all layers.

voxel_size: Tuple[float, float, float]
scale: Tuple[float, float, float]

Deprecated, use voxel_size instead.

name: str
default_view_configuration: Union[webknossos.dataset.properties.DatasetViewConfiguration, NoneType]
read_only: bool
def upload( self, new_dataset_name: Union[str, NoneType] = None, layers_to_link: Union[List[Union[ForwardRef('LayerToLink'), webknossos.dataset.layer.Layer]], NoneType] = None, jobs: Union[int, NoneType] = None) -> webknossos.dataset.dataset.RemoteDataset:

Uploads this dataset to WEBKNOSSOS.

The new_dataset_name parameter allows to assign a specific name for the dataset. layers_to_link allows to add (or override) a layer in the uploaded dataset, so that it links to a layer of an existing dataset in WEBKNOSSOS. That way, already existing layers don't need to be uploaded again.

If supplied, the jobs parameter will determine the number of simultaneous chunk uploads. Defaults to 5.

Returns the RemoteDataset upon successful upload.

def get_layer(self, layer_name: str) -> webknossos.dataset.layer.Layer:

Returns the layer called layer_name of this dataset. The return type is webknossos.dataset.layer.Layer.

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

def add_layer( self, layer_name: str, category: Literal['color', 'segmentation'], dtype_per_layer: Union[numpy.dtype[Any], NoneType, Type[Any], numpy.__dtype_like._SupportsDType[numpy.dtype[Any]], str, Tuple[Any, int], Tuple[Any, Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any], numpy.__dtype_like._DTypeDict, Tuple[Any, Any]] = None, dtype_per_channel: Union[numpy.dtype[Any], NoneType, Type[Any], numpy.__dtype_like._SupportsDType[numpy.dtype[Any]], str, Tuple[Any, int], Tuple[Any, Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any], numpy.__dtype_like._DTypeDict, Tuple[Any, Any]] = None, num_channels: Union[int, NoneType] = None, data_format: Union[str, webknossos.dataset.data_format.DataFormat] = <DataFormat.WKW: 'wkw'>, bounding_box: Union[webknossos.geometry.bounding_box.BoundingBox, NoneType] = None, **kwargs: Any) -> webknossos.dataset.layer.Layer:

Creates a new layer called layer_name and adds it to the dataset. The dtype can either be specified per layer or per channel. If neither of them are specified, uint8 per channel is used as default.

Creates the folder layer_name in the directory of self.path.

WKW layers can only be added to datasets on local file systems.

The return type is webknossos.dataset.layer.Layer.

This function raises an IndexError if the specified layer_name already exists.

def get_or_add_layer( self, layer_name: str, category: Literal['color', 'segmentation'], dtype_per_layer: Union[numpy.dtype[Any], NoneType, Type[Any], numpy.__dtype_like._SupportsDType[numpy.dtype[Any]], str, Tuple[Any, int], Tuple[Any, Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any], numpy.__dtype_like._DTypeDict, Tuple[Any, Any]] = None, dtype_per_channel: Union[numpy.dtype[Any], NoneType, Type[Any], numpy.__dtype_like._SupportsDType[numpy.dtype[Any]], str, Tuple[Any, int], Tuple[Any, Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any], numpy.__dtype_like._DTypeDict, Tuple[Any, Any]] = None, num_channels: Union[int, NoneType] = None, data_format: Union[str, webknossos.dataset.data_format.DataFormat] = <DataFormat.WKW: 'wkw'>, **kwargs: Any) -> webknossos.dataset.layer.Layer:

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

For more information see add_layer.

def add_layer_like( self, other_layer: webknossos.dataset.layer.Layer, layer_name: str) -> webknossos.dataset.layer.Layer:
def add_layer_for_existing_files( self, layer_name: str, category: Literal['color', 'segmentation'], **kwargs: Any) -> webknossos.dataset.layer.Layer:
def add_layer_from_images( self, images: Union[str, ForwardRef('pims.FramesSequence'), List[Union[str, os.PathLike]]], layer_name: str, category: Literal['color', 'segmentation'] = 'color', data_format: Union[str, webknossos.dataset.data_format.DataFormat] = <DataFormat.WKW: 'wkw'>, mag: Union[int, str, list, tuple, numpy.ndarray, webknossos.geometry.mag.Mag] = Mag(1), 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, *, topleft: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int]] = Vec3Int(0,0,0), swap_xy: bool = False, flip_x: bool = False, flip_y: bool = False, flip_z: bool = False, dtype: Union[numpy.dtype[Any], NoneType, Type[Any], numpy.__dtype_like._SupportsDType[numpy.dtype[Any]], str, Tuple[Any, int], Tuple[Any, Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any], numpy.__dtype_like._DTypeDict, Tuple[Any, Any]] = None, use_bioformats: Union[bool, NoneType] = None, channel: Union[int, NoneType] = None, timepoint: Union[int, NoneType] = None, czi_channel: Union[int, NoneType] = None, batch_size: Union[int, NoneType] = None, allow_multiple_layers: bool = False, max_layers: int = 20, truncate_rgba_to_rgb: bool = True, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None, chunk_size: Union[webknossos.geometry.vec3_int.Vec3Int, Tuple[int, int, int], numpy.ndarray, Iterable[int], int, NoneType] = None) -> webknossos.dataset.layer.Layer:

Creates a new layer called layer_name with mag mag from images. images can be one of the following:

  • glob-string
  • list of paths
  • pims.FramesSequence instance

Please see the pims docs for more information.

This method needs extra packages such as pims. Please install the respective extras, e.g. using python -m pip install "webknossos[all]".

Further Arguments:

  • category: color by default, may be set to "segmentation"
  • data_format: by default wkw files are written, may be set to "zarr"
  • mag: magnification to use for the written data
  • chunk_shape, chunks_per_shard, compress: adjust how the data is stored on disk
  • topleft: set an offset in Mag(1) to start writing the data, only affecting the output
  • swap_xy: set to True to interchange x and y axis before writing to disk
  • flip_x, flip_y, flip_z: set to True to reverse the respective axis before writing to disk
  • dtype: the read image data will be convertoed to this dtype using numpy.ndarray.astype
  • use_bioformats: set to True to only use the pims bioformats adapter directly, needs a JVM, set to False to forbid using the bioformats adapter, by default it is tried as a last option
  • channel: may be used to select a single channel, if multiple are available
  • timepoint: for timeseries, select a timepoint to use by specifying it as an int, starting from 0
  • czi_channel: may be used to select a channel for .czi images, which differs from normal color-channels
  • batch_size: size to process the images, must be a multiple of the chunk-size z-axis for uncompressed and the shard-size z-axis for compressed layers, default is the chunk-size or shard-size respectively
  • allow_multiple_layers: set to True if timepoints or channels may result in multiple layers being added (only the first is returned)
  • max_layers: only applies if allow_multiple_layers=True, limits the number of layers added via different channels or timepoints
  • truncate_rgba_to_rgb: only applies if allow_multiple_layers=True, set to False to write four channels into layers instead of an RGB channel
  • executor: pass a ClusterExecutor instance to parallelize the conversion jobs across the batches
def get_segmentation_layer(self) -> webknossos.dataset.layer.SegmentationLayer:

Deprecated, please use get_segmentation_layers().

Returns the only segmentation layer. Fails with a IndexError if there are multiple segmentation layers or none.

def get_segmentation_layers(self) -> List[webknossos.dataset.layer.SegmentationLayer]:

Returns all segmentation layers.

def get_color_layer(self) -> webknossos.dataset.layer.Layer:

Deprecated, please use get_color_layers().

Returns the only color layer. Fails with a RuntimeError if there are multiple color layers or none.

def get_color_layers(self) -> List[webknossos.dataset.layer.Layer]:

Returns all color layers.

def delete_layer(self, layer_name: str) -> None:

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

def add_copy_layer( self, foreign_layer: Union[str, pathlib.Path, webknossos.dataset.layer.Layer], new_layer_name: Union[str, NoneType] = None, 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, data_format: Union[str, webknossos.dataset.data_format.DataFormat, NoneType] = None, compress: Union[bool, NoneType] = None, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None) -> webknossos.dataset.layer.Layer:

Copies the data at foreign_layer which belongs to another dataset to the current dataset. Additionally, the relevant information from the datasource-properties.json of the other dataset are copied too. If new_layer_name is None, the name of the foreign layer is used.

def add_fs_copy_layer( self, foreign_layer: Union[str, pathlib.Path, webknossos.dataset.layer.Layer], new_layer_name: Union[str, NoneType] = None) -> webknossos.dataset.layer.Layer:

Copies the files at foreign_layer 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. If new_layer_name is None, the name of the foreign layer is used.

def calculate_bounding_box(self) -> webknossos.geometry.bounding_box.BoundingBox:

Calculates and returns the enclosing bounding box of all data layers of the dataset.

def copy_dataset( self, new_dataset_path: Union[str, pathlib.Path], voxel_size: Union[Tuple[float, float, float], NoneType] = None, 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, data_format: Union[str, webknossos.dataset.data_format.DataFormat, NoneType] = None, compress: Union[bool, NoneType] = None, args: Union[argparse.Namespace, NoneType] = None, executor: Union[cluster_tools.executor_protocol.Executor, 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.dataset.Dataset:

Creates a new dataset at new_dataset_path and copies the data from the current dataset to empty_target_ds. If not specified otherwise, the voxel_size, chunk_shape, chunks_per_shard and compress of the current dataset are also used for the new dataset. WKW layers can only be copied to datasets on local file systems.

def shallow_copy_dataset( self, new_dataset_path: Union[str, os.PathLike], name: Union[str, NoneType] = None, make_relative: bool = False, layers_to_ignore: Union[Iterable[str], NoneType] = None) -> webknossos.dataset.dataset.Dataset:

Create a new dataset at the given path. Link all mags of all existing layers. In addition, link all other directories in all layer directories to make this method robust against additional files e.g. layer/mappings/agglomerate_view.hdf5. This method becomes useful when exposing a dataset to webknossos. Only datasets on local filesystems can be shallow copied.

def compress( self, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None) -> None:

Compresses all mag views in-place that are not yet compressed.

def downsample( self, sampling_mode: webknossos.dataset.sampling_modes.SamplingModes = <SamplingModes.ANISOTROPIC: 'anisotropic'>, executor: Union[cluster_tools.executor_protocol.Executor, NoneType] = None) -> None:

Downsamples all layers that are not yet downsampled.

@classmethod
def create( cls, dataset_path: Union[str, os.PathLike], voxel_size: Tuple[float, float, float], name: Union[str, NoneType] = None) -> webknossos.dataset.dataset.Dataset:

Deprecated, please use the constructor Dataset() instead.

@classmethod
def get_or_create( cls, dataset_path: Union[str, pathlib.Path], voxel_size: Tuple[float, float, float], name: Union[str, NoneType] = None) -> webknossos.dataset.dataset.Dataset:

Deprecated, please use the constructor Dataset() instead.

@staticmethod
def get_remote_datasets( organization_id: Union[str, NoneType] = None, tags: Union[str, Sequence[str], NoneType] = None) -> collections.abc.Mapping[str, webknossos.dataset.dataset.RemoteDataset]:

Returns a dict of all remote datasets visible for selected organization, or the organization of the logged in user by default. The dict contains lazy-initialized RemoteDataset values for keys indicating the dataset name.

import webknossos as wk

print(sorted(wk.Dataset.get_remote_datasets()))

ds = wk.Dataset.get_remote_datasets(
    organization_id="scalable_minds"
)["l4dense_motta_et_al_demo"]
class Dataset.ConversionLayerMapping(enum.Enum):

Strategies for mapping file paths to layers, for use in Dataset.from_images for the map_filepath_to_layer_name argument.

If none of the strategies fit, the mapping can also be specified by a callable.

INSPECT_SINGLE_FILE = <ConversionLayerMapping.INSPECT_SINGLE_FILE: 'inspect_single_file'>

The first found image file is opened. If it appears to be a 2D image, ENFORCE_LAYER_PER_FOLDER is used, if it appears to be 3D, ENFORCE_LAYER_PER_FILE is used. This is the default mapping.

INSPECT_EVERY_FILE = <ConversionLayerMapping.INSPECT_EVERY_FILE: 'inspect_every_file'>

Like INSPECT_SINGLE_FILE, but the strategy is determined for each image file separately.

ENFORCE_LAYER_PER_FILE = <ConversionLayerMapping.ENFORCE_LAYER_PER_FILE: 'enforce_layer_per_file'>

Enforce a new layer per file. This is useful for 2D images that should be converted to 2D layers each.

ENFORCE_SINGLE_LAYER = <ConversionLayerMapping.ENFORCE_SINGLE_LAYER: 'enforce_single_layer'>

Combines all found files into a single layer. This is only useful if all images are 2D.

ENFORCE_LAYER_PER_FOLDER = <ConversionLayerMapping.ENFORCE_LAYER_PER_FOLDER: 'enforce_layer_per_folder'>

Combine all files in a folder into one layer.

ENFORCE_LAYER_PER_TOPLEVEL_FOLDER = <ConversionLayerMapping.ENFORCE_LAYER_PER_TOPLEVEL_FOLDER: 'enforce_layer_per_toplevel_folder'>

The first folders of the input path are each converted to one layer. This might be useful if multiple layers have stacks of 2D images, but parts of the stacks are in different folders.

Inherited Members
enum.Enum
name
value
class RemoteDataset(Dataset):

Representation of a dataset on the webknossos server, returned from Dataset.open_remote(). Read-only image data is streamed from the webknossos server using the same interface as Dataset. Additionally, metadata can be set via the additional properties below.

RemoteDataset( dataset_path: upath.core.UPath, dataset_name: str, organization_id: str, sharing_token: Union[str, NoneType], context: AbstractContextManager)

Do not call manually, please use Dataset.open_remote() instead.

@classmethod
def open( cls, dataset_path: Union[str, os.PathLike]) -> webknossos.dataset.dataset.Dataset:

Do not call manually, please use Dataset.open_remote() instead.

url: str
display_name: Union[str, NoneType]
description: Union[str, NoneType]
tags: Tuple[str, ...]
is_public: bool
sharing_token: str
allowed_teams: Tuple[ForwardRef('Team'), ...]

Assign the teams that are allowed to access the dataset. Specify the teams like this [Team.get_by_name("Lab_A"), ...].

Move the dataset to a folder. Specify the folder like this RemoteFolder.get_by_path("Datasets/Folder_A").