Skip to content

Commit 545f72e

Browse files
authored
Fix bounding box of mag != 1 when exporting wkw to tiff (#205)
* fix bounding box of mag != 1 when exporting wkw to tiff * fix formatting
1 parent d8e0326 commit 545f72e

File tree

4 files changed

+67
-65
lines changed

4 files changed

+67
-65
lines changed

test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
set -euxo pipefail
12
export CIRCLE_BUILD_NUM=latest
23
tests/scripts/build_docker_image.sh
34
tests/scripts/decompress_test_data.sh

tests/test_export_wkw_as_tiff.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
from wkcuber.export_wkw_as_tiff import run, wkw_name_and_bbox_to_tiff_name
2+
from wkcuber.api.bounding_box import BoundingBox
23
import os
34
from PIL import Image
45
from wkcuber.mag import Mag
56
import wkw
67
import numpy as np
78
from math import ceil
89

10+
ds_name = "simple_wk_dataset"
11+
source_path = os.path.join("testdata", ds_name)
12+
913

1014
def test_export_tiff_stack():
11-
destination_path = os.path.join("testoutput", "WT1_wkw")
15+
destination_path = os.path.join("testoutput", ds_name + "_tiff")
16+
bbox = BoundingBox((100, 100, 10), (100, 500, 50))
17+
bbox_dict = bbox.as_config()
1218
args_list = [
1319
"--source_path",
14-
os.path.join("testdata", "WT1_wkw"),
20+
source_path,
1521
"--destination_path",
1622
destination_path,
1723
"--layer_name",
1824
"color",
1925
"--name",
2026
"test_export",
2127
"--bbox",
22-
"0,0,0,100,100,5",
28+
bbox.as_csv(),
2329
"--mag",
2430
"1",
2531
]
2632

27-
bbox = {"topleft": [0, 0, 0], "size": [100, 100, 5]}
28-
2933
run(args_list)
3034

31-
test_wkw_file_path = os.path.join(
32-
"testdata", "WT1_wkw", "color", Mag(1).to_layer_name()
33-
)
35+
test_wkw_file_path = os.path.join(source_path, "color", Mag(1).to_layer_name())
3436
with wkw.Dataset.open(test_wkw_file_path) as dataset:
35-
slice_bbox = bbox
37+
slice_bbox = bbox_dict
3638
slice_bbox["size"] = [slice_bbox["size"][0], slice_bbox["size"][1], 1]
37-
for data_slice_index in range(1, bbox["size"][2] + 1):
39+
for data_slice_index in range(1, bbox_dict["size"][2] + 1):
3840
slice_bbox["topleft"] = [
3941
slice_bbox["topleft"][0],
4042
slice_bbox["topleft"][1],
41-
bbox["topleft"][2] + data_slice_index,
43+
bbox_dict["topleft"][2] + data_slice_index,
4244
]
4345
tiff_path = os.path.join(
4446
destination_path,
@@ -49,8 +51,7 @@ def test_export_tiff_stack():
4951
tiff_path
5052
), f"Expected a tiff to be written at: {tiff_path}."
5153

52-
test_image = np.array(Image.open(tiff_path))
53-
test_image.transpose((1, 0))
54+
test_image = np.array(Image.open(tiff_path)).T
5455

5556
correct_image = dataset.read(
5657
off=slice_bbox["topleft"], shape=slice_bbox["size"]
@@ -64,10 +65,10 @@ def test_export_tiff_stack():
6465

6566

6667
def test_export_tiff_stack_tile_size():
67-
destination_path = os.path.join("testoutput", "WT1_wkw_tile_size")
68+
destination_path = os.path.join("testoutput", ds_name + "_tile_size")
6869
args_list = [
6970
"--source_path",
70-
os.path.join("testdata", "WT1_wkw"),
71+
source_path,
7172
"--destination_path",
7273
destination_path,
7374
"--layer_name",
@@ -87,9 +88,7 @@ def test_export_tiff_stack_tile_size():
8788
run(args_list)
8889

8990
tile_bbox = {"topleft": bbox["topleft"], "size": [30, 30, 1]}
90-
test_wkw_file_path = os.path.join(
91-
"testdata", "WT1_wkw", "color", Mag(1).to_layer_name()
92-
)
91+
test_wkw_file_path = os.path.join(source_path, "color", Mag(1).to_layer_name())
9392
with wkw.Dataset.open(test_wkw_file_path) as dataset:
9493
slice_bbox = {"topleft": bbox["topleft"], "size": bbox["size"]}
9594
slice_bbox["size"] = [slice_bbox["size"][0], slice_bbox["size"][1], 1]
@@ -108,8 +107,7 @@ def test_export_tiff_stack_tile_size():
108107
tiff_path
109108
), f"Expected a tiff to be written at: {tiff_path}."
110109

111-
test_image = np.array(Image.open(tiff_path))
112-
test_image.transpose((1, 0))
110+
test_image = np.array(Image.open(tiff_path)).T
113111

114112
correct_image = dataset.read(
115113
off=[
@@ -131,10 +129,10 @@ def test_export_tiff_stack_tile_size():
131129

132130

133131
def test_export_tiff_stack_tiles_per_dimension():
134-
destination_path = os.path.join("testoutput", "WT1_wkw_tiles_per_dimension")
132+
destination_path = os.path.join("testoutput", ds_name + "_tiles_per_dimension")
135133
args_list = [
136134
"--source_path",
137-
os.path.join("testdata", "WT1_wkw"),
135+
source_path,
138136
"--destination_path",
139137
destination_path,
140138
"--layer_name",
@@ -154,9 +152,7 @@ def test_export_tiff_stack_tiles_per_dimension():
154152
run(args_list)
155153

156154
tile_bbox = {"topleft": bbox["topleft"], "size": [17, 17, 1]}
157-
test_wkw_file_path = os.path.join(
158-
"testdata", "WT1_wkw", "color", Mag(1).to_layer_name()
159-
)
155+
test_wkw_file_path = os.path.join(source_path, "color", Mag(1).to_layer_name())
160156
with wkw.Dataset.open(test_wkw_file_path) as dataset:
161157
slice_bbox = bbox
162158
slice_bbox["size"] = [slice_bbox["size"][0], slice_bbox["size"][1], 1]
@@ -177,8 +173,7 @@ def test_export_tiff_stack_tiles_per_dimension():
177173
tiff_path
178174
), f"Expected a tiff to be written at: {tiff_path}."
179175

180-
test_image = np.array(Image.open(tiff_path))
181-
test_image.transpose((1, 0))
176+
test_image = np.array(Image.open(tiff_path)).T
182177

183178
correct_image = dataset.read(
184179
off=[
@@ -200,4 +195,6 @@ def test_export_tiff_stack_tiles_per_dimension():
200195

201196

202197
if __name__ == "__main__":
198+
test_export_tiff_stack()
203199
test_export_tiff_stack_tile_size()
200+
test_export_tiff_stack_tiles_per_dimension()

wkcuber/api/bounding_box.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ def as_tuple6(self) -> Tuple[int, int, int, int, int, int]:
120120

121121
return tuple(self.topleft.tolist() + self.size.tolist()) # type: ignore
122122

123+
def as_csv(self) -> str:
124+
125+
return ",".join(map(str, self.as_tuple6()))
126+
123127
def __repr__(self) -> str:
124128

125129
return "BoundingBox(topleft={}, size={})".format(

wkcuber/export_wkw_as_tiff.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from argparse import ArgumentParser
2-
2+
from functools import partial
33
import logging
44
import wkw
55
import os
@@ -17,6 +17,7 @@
1717
add_batch_size_flag,
1818
)
1919
from wkcuber.mag import Mag
20+
from wkcuber.utils import wait_and_ensure_success
2021

2122

2223
def create_parser():
@@ -115,30 +116,30 @@ def wkw_slice_to_image(data_slice: np.ndarray, downsample: int = 1) -> Image:
115116

116117

117118
def export_tiff_slice(
118-
export_args: Tuple[
119-
int,
120-
Tuple[
121-
Dict[str, Tuple[int, int, int]],
122-
str,
123-
str,
124-
str,
125-
Union[None, Tuple[int, int]],
126-
int,
127-
int,
128-
],
129-
]
119+
tiff_bbox: Dict[str, Tuple[int, int, int]],
120+
dest_path: str,
121+
name: str,
122+
dataset_path: str,
123+
tiling_size: Union[None, Tuple[int, int]],
124+
batch_size: int,
125+
downsample: int,
126+
mag: Mag,
127+
batch_number: int,
130128
):
131-
(
132-
batch_number,
133-
(tiff_bbox, dest_path, name, dataset_path, tiling_size, batch_size, downsample),
134-
) = export_args
129+
135130
tiff_bbox = tiff_bbox.copy()
136-
number_of_slices = min(tiff_bbox["size"][2] - batch_number * batch_size, batch_size)
137-
tiff_bbox["size"] = [tiff_bbox["size"][0], tiff_bbox["size"][1], number_of_slices]
131+
number_of_slices = (
132+
min(tiff_bbox["size"][2] - batch_number * batch_size, batch_size) // mag.mag[2]
133+
)
134+
tiff_bbox["size"] = [
135+
tiff_bbox["size"][0] // mag.mag[0],
136+
tiff_bbox["size"][1] // mag.mag[1],
137+
number_of_slices,
138+
]
138139
tiff_bbox["topleft"] = [
139-
tiff_bbox["topleft"][0],
140-
tiff_bbox["topleft"][1],
141-
tiff_bbox["topleft"][2] + batch_number * batch_size,
140+
tiff_bbox["topleft"][0] // mag.mag[0],
141+
tiff_bbox["topleft"][1] // mag.mag[1],
142+
(tiff_bbox["topleft"][2] + batch_number * batch_size) // mag.mag[2],
142143
]
143144

144145
with wkw.Dataset.open(dataset_path) as dataset:
@@ -155,7 +156,6 @@ def export_tiff_slice(
155156
slice_name_number = batch_number * batch_size + slice_index + 1
156157
if tiling_size is None:
157158
tiff_file_name = wkw_name_and_bbox_to_tiff_name(name, slice_name_number)
158-
159159
tiff_file_path = os.path.join(dest_path, tiff_file_name)
160160

161161
image = wkw_slice_to_image(tiff_data[:, :, :, slice_index], downsample)
@@ -213,23 +213,23 @@ def export_tiff_stack(
213213
with get_executor_for_args(args) as executor:
214214
num_slices = ceil(bbox["size"][2] / batch_size)
215215
slices = range(0, num_slices)
216-
export_args = zip(
216+
217+
logging.info(f"starting jobs")
218+
futures = executor.map_to_futures(
219+
partial(
220+
export_tiff_slice,
221+
bbox,
222+
destination_path,
223+
name,
224+
dataset_path,
225+
tiling_slice_size,
226+
batch_size,
227+
downsample,
228+
mag,
229+
),
217230
slices,
218-
[
219-
(
220-
bbox,
221-
destination_path,
222-
name,
223-
dataset_path,
224-
tiling_slice_size,
225-
batch_size,
226-
downsample,
227-
)
228-
]
229-
* num_slices,
230231
)
231-
logging.info(f"starting jobs")
232-
executor.map(export_tiff_slice, export_args)
232+
wait_and_ensure_success(futures)
233233

234234

235235
def export_wkw_as_tiff(args):

0 commit comments

Comments
 (0)