Skip to content

Commit 4af2d9f

Browse files
authored
Add pad option to cube images with differing size (#94)
* add pad option to cube images with differing size * refactor argparse and fix missing arg for main * fix dimensions in padding * bump version
1 parent 6f5df53 commit 4af2d9f

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
setup(
44
name="wkcuber",
55
packages=find_packages(exclude=("tests",)),
6-
version="0.2.12",
6+
version="0.2.13",
77
install_requires=["scipy", "numpy", "pillow", "pyyaml", "wkw", "cluster_tools>=1.19"],
88
description="A cubing tool for webKnossos",
99
author="Norman Rzepka",

wkcuber/__main__.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import logging
44

5-
from .cubing import cubing, BLOCK_LEN
5+
from .cubing import cubing, BLOCK_LEN, create_parser as create_cubing_parser
66
from .downsampling import downsample_mags, downsample_mags_anisotropic, DEFAULT_EDGE_LEN
77
from .compress import compress_mag_inplace
88
from .metadata import write_webknossos_metadata
@@ -11,35 +11,7 @@
1111

1212

1313
def create_parser():
14-
parser = ArgumentParser()
15-
16-
parser.add_argument("source_path", help="Directory containing the input images.")
17-
18-
parser.add_argument(
19-
"target_path", help="Output directory for the generated dataset."
20-
)
21-
22-
parser.add_argument(
23-
"--layer_name",
24-
"-l",
25-
help="Name of the cubed layer (color or segmentation)",
26-
default="color",
27-
)
28-
29-
parser.add_argument(
30-
"--dtype",
31-
"-d",
32-
help="Target datatype (e.g. uint8, uint16, uint32)",
33-
default="uint8",
34-
)
35-
36-
parser.add_argument(
37-
"--batch_size",
38-
"-b",
39-
type=int,
40-
help="Number of slices to buffer per job",
41-
default=BLOCK_LEN,
42-
)
14+
parser = create_cubing_parser()
4315

4416
parser.add_argument(
4517
"--max_mag",
@@ -65,8 +37,6 @@ def create_parser():
6537
default="1,1,1",
6638
)
6739

68-
add_verbose_flag(parser)
69-
add_distribution_flags(parser)
7040
add_anisotropic_flag(parser)
7141

7242
return parser

wkcuber/cubing.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ def create_parser():
5252
default=BLOCK_LEN,
5353
)
5454

55+
parser.add_argument(
56+
"--pad",
57+
help="Automatically pad image files at the bottom and right borders. "
58+
"Use this, when the input images don't have a common size, but have "
59+
"their origin at (0, 0).",
60+
default=False,
61+
action="store_true",
62+
)
63+
5564
add_verbose_flag(parser)
5665
add_distribution_flags(parser)
5766

@@ -89,6 +98,7 @@ def cubing_job(
8998
batch_size,
9099
image_size,
91100
num_channels,
101+
pad=False,
92102
):
93103
if len(z_batches) == 0:
94104
return
@@ -109,13 +119,32 @@ def cubing_job(
109119
for z, file_name in zip(z_batch, source_file_batch):
110120
# Image shape will be (x, y, channel_count, z=1) or (x, y, z=1)
111121
image = read_image_file(file_name, target_wkw_info.dtype)
112-
assert (
113-
image.shape[0:2] == image_size
114-
), "Section z={} has the wrong dimensions: {} (expected {}).".format(
115-
z, image.shape, image_size
116-
)
122+
if not pad:
123+
assert (
124+
image.shape[0:2] == image_size
125+
), "Section z={} has the wrong dimensions: {} (expected {}). Consider using --pad.".format(
126+
z, image.shape, image_size
127+
)
117128
buffer.append(image)
118129

130+
if pad:
131+
x_max = max(slice.shape[0] for slice in buffer)
132+
y_max = max(slice.shape[1] for slice in buffer)
133+
134+
buffer = [
135+
np.pad(
136+
slice,
137+
mode="constant",
138+
pad_width=[
139+
(0, x_max - slice.shape[0]),
140+
(0, y_max - slice.shape[1]),
141+
(0, 0),
142+
(0, 0),
143+
],
144+
)
145+
for slice in buffer
146+
]
147+
119148
# Write batch buffer which will have shape (x, y, channel_count, z)
120149
# since we concat along the last axis (z)
121150
buffer = np.concatenate(buffer, axis=-1)
@@ -175,6 +204,7 @@ def cubing(source_path, target_path, layer_name, dtype, batch_size, args=None) -
175204
batch_size,
176205
(num_x, num_y),
177206
num_channels,
207+
args.pad,
178208
)
179209
)
180210

0 commit comments

Comments
 (0)