Skip to content

Commit 6430a4e

Browse files
authored
Rename layer (#368)
* rename layer * update changelog * update dict when renaming a layer
1 parent 523e2fa commit 6430a4e

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ For upgrade instructions, please check the respective *Breaking Changes* section
1414

1515
### Added
1616
- Added functions to `wkcuber.api.dataset.Dataset` and `wkcuber.api.layer.Layer` to set and get the view configuration. [#344](https://github.com/scalableminds/webknossos-cuber/pull/344)
17+
- Added option to rename a layer. [#368](https://github.com/scalableminds/webknossos-cuber/pull/368)
1718

1819
### Changed
1920

tests/test_dataset.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,3 +1647,23 @@ def test_add_copy_layer(tmp_path: Path) -> None:
16471647

16481648
# Test if the changes of the properties are persisted on disk by opening it again
16491649
assert "color" in Dataset(tmp_path / "ds").layers.keys()
1650+
1651+
1652+
def test_rename_layer(tmp_path: Path) -> None:
1653+
ds = Dataset.create(tmp_path / "ds", scale=(1, 1, 1))
1654+
layer = ds.add_layer("color", LayerCategories.COLOR_TYPE)
1655+
mag = layer.add_mag(1)
1656+
write_data = (np.random.rand(10, 20, 30) * 255).astype(np.uint8)
1657+
mag.write(data=write_data)
1658+
1659+
layer.rename("color2")
1660+
1661+
assert not (tmp_path / "ds" / "color").exists()
1662+
assert (tmp_path / "ds" / "color2").exists()
1663+
assert "color2" in ds.properties.data_layers.keys()
1664+
assert "color2" == ds.properties.data_layers["color2"].name
1665+
assert "color2" in ds.layers.keys()
1666+
assert "color" not in ds.layers.keys()
1667+
1668+
# The "mag" object which was created before renaming the layer is still valid
1669+
assert np.array_equal(mag.read()[0], write_data)

wkcuber/api/layer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import math
3+
import os
34
from argparse import Namespace
45
from shutil import rmtree
56
from os.path import join
@@ -254,6 +255,31 @@ def set_bounding_box_size(self, size: Tuple[int, int, int]) -> None:
254255
def get_bounding_box(self) -> BoundingBox:
255256
return self.dataset.properties.data_layers[self.name].get_bounding_box()
256257

258+
def rename(self, layer_name: str) -> None:
259+
"""
260+
Renames the layer to `layer_name`. This changes the name of the directory on disk and updates the properties.
261+
"""
262+
assert (
263+
layer_name not in self.dataset.layers.keys()
264+
), f"Failed to rename layer {self.name} to {layer_name}: The new name already exists."
265+
os.rename(self.dataset.path / self.name, self.dataset.path / layer_name)
266+
layer_properties = self.dataset.properties.data_layers[self.name]
267+
layer_properties._name = layer_name
268+
del self.dataset.properties.data_layers[self.name]
269+
self.dataset.properties._data_layers[layer_name] = layer_properties
270+
self.dataset.properties._export_as_json()
271+
del self.dataset.layers[self.name]
272+
self.dataset.layers[layer_name] = self
273+
self.name = layer_name
274+
275+
# The MagViews need to be updated
276+
for mag in self._mags.values():
277+
mag.path = _find_mag_path_on_disk(self.dataset.path, self.name, mag.name)
278+
if mag._is_opened:
279+
# Reopen handle to dataset on disk
280+
mag.close()
281+
mag.open()
282+
257283
def downsample(
258284
self,
259285
from_mag: Optional[Mag] = None,

0 commit comments

Comments
 (0)