Skip to content

Commit

Permalink
Deprecate get_segmentation/color_layer and add plural form (#504)
Browse files Browse the repository at this point in the history
* deprecate get_segmentation/color_layer and add plural form for it instead

* fix typing

* fix typing

* update changelog
  • Loading branch information
philippotto authored Dec 7, 2021
1 parent d2b2247 commit 86432c4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion webknossos/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 32 additions & 2 deletions webknossos/webknossos/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 86432c4

Please sign in to comment.