Skip to content

Commit ba415a5

Browse files
authored
Add possibility to change the boundingbox of a layer (#225)
* add possibility to change the boundingbox of a layer * make offset and size optional parameter when changing the boundingbox * split changing boundingbox size and offset into different methods
1 parent 44d6e8b commit ba415a5

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

tests/test_dataset.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,3 +1146,52 @@ def test_tiled_tiff_dataset_get_or_create():
11461146
raise Exception(expected_error_msg)
11471147
except AssertionError:
11481148
pass
1149+
1150+
1151+
def test_changing_layer_bounding_box():
1152+
delete_dir("./testoutput/test_changing_layer_bounding_box/")
1153+
copytree(
1154+
"./testdata/simple_tiff_dataset/",
1155+
"./testoutput/test_changing_layer_bounding_box/",
1156+
)
1157+
1158+
ds = TiffDataset("./testoutput/test_changing_layer_bounding_box/")
1159+
layer = ds.get_layer("color")
1160+
mag = layer.get_mag("1")
1161+
1162+
bbox_size = ds.properties.data_layers["color"].get_bounding_box_size()
1163+
assert bbox_size == (265, 265, 10)
1164+
original_data = mag.read(bbox_size)
1165+
assert original_data.shape == (1, 265, 265, 10)
1166+
1167+
layer.set_bounding_box_size((100, 100, 10)) # decrease boundingbox
1168+
1169+
bbox_size = ds.properties.data_layers["color"].get_bounding_box_size()
1170+
assert bbox_size == (100, 100, 10)
1171+
less_data = mag.read(bbox_size)
1172+
assert less_data.shape == (1, 100, 100, 10)
1173+
assert np.array_equal(original_data[:, :100, :100, :10], less_data)
1174+
1175+
layer.set_bounding_box_size((300, 300, 10)) # increase the boundingbox
1176+
1177+
bbox_size = ds.properties.data_layers["color"].get_bounding_box_size()
1178+
assert bbox_size == (300, 300, 10)
1179+
more_data = mag.read(bbox_size)
1180+
assert more_data.shape == (1, 300, 300, 10)
1181+
assert np.array_equal(more_data[:, :265, :265, :10], original_data)
1182+
1183+
layer.set_bounding_box_size((300, 300, 10)) # increase the boundingbox
1184+
1185+
assert ds.properties.data_layers["color"].get_bounding_box_offset() == (0, 0, 0)
1186+
1187+
layer.set_bounding_box(
1188+
offset=(10, 10, 0), size=(255, 255, 10)
1189+
) # change offset and size
1190+
1191+
new_bbox_offset = ds.properties.data_layers["color"].get_bounding_box_offset()
1192+
new_bbox_size = ds.properties.data_layers["color"].get_bounding_box_size()
1193+
assert new_bbox_offset == (10, 10, 0)
1194+
assert new_bbox_size == (255, 255, 10)
1195+
new_data = mag.read(new_bbox_size)
1196+
assert new_data.shape == (1, 255, 255, 10)
1197+
assert np.array_equal(original_data[:, 10:, 10:, :], new_data)

wkcuber/api/Layer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from shutil import rmtree
22
from os.path import join
33
from os import makedirs
4+
from typing import Tuple
5+
6+
import numpy as np
7+
48
from wkw import wkw
59

610
from wkcuber.api.MagDataset import (
@@ -61,6 +65,28 @@ def _assert_mag_does_not_exist_yet(self, mag):
6165
)
6266
)
6367

68+
def set_bounding_box(
69+
self, offset: Tuple[int, int, int], size: Tuple[int, int, int]
70+
):
71+
self.set_bounding_box_offset(offset)
72+
self.set_bounding_box_size(size)
73+
74+
def set_bounding_box_offset(self, offset: Tuple[int, int, int]):
75+
size = self.dataset.properties.data_layers["color"].get_bounding_box_size()
76+
self.dataset.properties._set_bounding_box_of_layer(
77+
self.name, tuple(offset), tuple(size)
78+
)
79+
for _, mag in self.mags.items():
80+
mag.view.global_offset = offset
81+
82+
def set_bounding_box_size(self, size: Tuple[int, int, int]):
83+
offset = self.dataset.properties.data_layers["color"].get_bounding_box_offset()
84+
self.dataset.properties._set_bounding_box_of_layer(
85+
self.name, tuple(offset), tuple(size)
86+
)
87+
for _, mag in self.mags.items():
88+
mag.view.size = size
89+
6490

6591
class WKLayer(Layer):
6692
def add_mag(

0 commit comments

Comments
 (0)