diff --git a/webknossos/Changelog.md b/webknossos/Changelog.md index 7b7577ca5..0b4cf1914 100644 --- a/webknossos/Changelog.md +++ b/webknossos/Changelog.md @@ -16,7 +16,7 @@ For upgrade instructions, please check the respective *Breaking Changes* section ### Changed - Adapt the dataset upload to new webKnossos api. [#484](https://github.com/scalableminds/webknossos-libs/pull/484) - +- `get_segmentation_layer()` and `get_color_layer()` were deprecated and should not be used, anymore, as they will fail if no or more than one layer exists for each category. Instead, `get_segmentation_layers()` and `get_color_layers()` should be used (if desired in combination with `[0]` to get the old, error-prone behavior). ### Fixed diff --git a/webknossos/webknossos/dataset/dataset.py b/webknossos/webknossos/dataset/dataset.py index 5bcccfeae..43d7beddc 100644 --- a/webknossos/webknossos/dataset/dataset.py +++ b/webknossos/webknossos/dataset/dataset.py @@ -2,6 +2,7 @@ import json import os import shutil +import warnings from argparse import Namespace from os import makedirs from os.path import basename, join, normpath @@ -345,23 +346,52 @@ def add_layer_for_existing_files( def get_segmentation_layer(self) -> SegmentationLayer: """ - Returns the only segmentation layer. + Returns the only segmentation layer. Prefer `get_segmentation_layers`, + because multiple segmentation layers can exist. Fails with a IndexError if there are multiple segmentation layers or none. """ + + warnings.warn( + "[DEPRECATION] get_segmentation_layer() fails if no or more than one segmentation layer exists. Prefer get_segmentation_layers()." + ) return cast( SegmentationLayer, self._get_layer_by_category(SEGMENTATION_CATEGORY), ) + def get_segmentation_layers(self) -> List[SegmentationLayer]: + """ + Returns all segmentation layers. + """ + return [ + cast(SegmentationLayer, layer) + for layer in self.layers.values() + if layer.category == SEGMENTATION_CATEGORY + ] + def get_color_layer(self) -> Layer: """ - Returns the only color layer. + Returns the only color layer. Prefer `get_color_layers`, because multiple + color layers can exist. Fails with a RuntimeError if there are multiple color layers or none. """ return self._get_layer_by_category(COLOR_CATEGORY) + def get_color_layers(self) -> List[Layer]: + """ + Returns all color layers. + """ + warnings.warn( + "[DEPRECATION] get_color_layer() fails if no or more than one color layer exists. Prefer get_color_layers()." + ) + return [ + cast(Layer, layer) + for layer in self.layers.values() + if layer.category == COLOR_CATEGORY + ] + def delete_layer(self, layer_name: str) -> None: """ Deletes the layer from the `datasource-properties.json` and the data from disk.