Skip to content

Commit 4176bf5

Browse files
rschwanholdYouri K
andauthored
Use Dataset API inside cubing (#418)
* WIP: use dataset API in cubing * use Dataset API for cubing * remove unused imports * cleanup * use scale 1 for cubing * update changelog * format code * fix comment * fix typing error * fix linting * support segmentation layers for cubing * format code * compute largest_segment_id for segmentation layers * fix a bug that was introduced through the merge * use new properties of Mag and Vec3Int * use to_wkw_dict * use correct mag for comression and downsampling * change type of cli parameter to Mag * fix formatting * fix the behaviour of start_z * update changelog * fix formatting * fix lint * implement PR feedback * use better import statement * format code * add scale parameter for cubing * format code * extend existing bbox * add scale parameter to test scripts * fix interpretation of parameter 'start_z' and add parameter 'skip_first_z_slices' * fix formatting/linting * add missing parapeter to test script * add scale parameter to docker smoke test Co-authored-by: Youri K <[email protected]>
1 parent 29376eb commit 4176bf5

File tree

13 files changed

+159
-124
lines changed

13 files changed

+159
-124
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ jobs:
202202
--batch_size 8 \
203203
--layer_name color \
204204
--wkw_file_len 32 \
205+
--scale 1 \
205206
testdata/tiff testoutput/tiff
206207
207208
- name: Login to docker

webknossos/webknossos/dataset/downsampling_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ def calculate_default_max_mag(dataset_size: Vec3IntLike) -> Mag:
9999

100100

101101
def parse_interpolation_mode(
102-
interpolation_mode: str, layer_name: str
102+
interpolation_mode: str, layer_category: str
103103
) -> InterpolationModes:
104104
if interpolation_mode.upper() == "DEFAULT":
105+
from webknossos.dataset.layer import LayerCategories
106+
105107
return (
106108
InterpolationModes.MODE
107-
if layer_name == "segmentation"
109+
if layer_category == LayerCategories.SEGMENTATION_TYPE
108110
else InterpolationModes.MEDIAN
109111
)
110112
else:

webknossos/webknossos/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ def named_partial(func: F, *args: Any, **kwargs: Any) -> F:
8484
return partial_func
8585

8686

87-
def wait_and_ensure_success(futures: List[Future]) -> None:
87+
def wait_and_ensure_success(futures: List[Future]) -> List[Any]:
8888
"""Waits for all futures to complete and raises an exception
8989
as soon as a future resolves with an error."""
90+
results = []
9091
for fut in as_completed(futures):
91-
fut.result()
92+
results.append(fut.result())
93+
return results
9294

9395

9496
def snake_to_camel_case(snake_case_name: str) -> str:

wkcuber/Changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ For upgrade instructions, please check the respective *Breaking Changes* section
1010
[Commits](https://github.com/scalableminds/webknossos-libs/compare/v0.8.16...HEAD)
1111

1212
### Breaking Changes in Config & CLI
13+
- Use Dataset API inside `cubing` to automatically write metadata. Cubing does now require a scale. [#418](https://github.com/scalableminds/webknossos-libs/pull/418)
1314
### Added
1415
### Changed
1516
- Updated scikit-image dependency to 0.18.3. [#435](https://github.com/scalableminds/webknossos-libs/pull/435)
17+
- Improved the `TIFF` and `CZI` reader to work with a wider variety of formats. The module `convert_image_stack_to_wkw` is now capable of making the result webKnossos compatible. [#335](https://github.com/scalableminds/webknossos-libs/pull/335)
1618
### Fixed
1719

1820

@@ -28,7 +30,6 @@ For upgrade instructions, please check the respective *Breaking Changes* section
2830
- Adjust downsampling scheme to always try to minimize the scaled difference between the different dimensions of a mag and renamed the sampling mode `auto` to `anisotropic`. [#391](https://github.com/scalableminds/webknossos-libs/pull/391)
2931
- Make parameter `executor` optional for `View.for_each_chunk` and `View.for_zipped_chunks`. [#404](https://github.com/scalableminds/webknossos-libs/pull/404)
3032
- Add option to rename foreign layer with add_{symlink,copy}_layer. [#419](https://github.com/scalableminds/webknossos-libs/pull/419)
31-
- Improved the `TIFF` and `CZI` reader to work with a wider variety of formats. The module `convert_image_stack_to_wkw` is now capable of making the result webKnossos compatible. [#335](https://github.com/scalableminds/webknossos-libs/pull/335)
3233

3334
### Fixed
3435
- Reverted that `dataset.add_symlink_layer` and `dataset.add_copy_layer` resolved the layer path if it was a symlink. [#408](https://github.com/scalableminds/webknossos-libs/pull/408)

wkcuber/tests/scripts/in_memory_downsampled_cubing.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ python -m wkcuber.cubing \
66
--layer_name color \
77
--target_mag 2-2-1 \
88
--interpolation_mode default \
9+
--scale 1 \
910
testdata/tiff testoutput/in_memory_downsampled_tiff
1011
[ -d testoutput/in_memory_downsampled_tiff/color ]
1112
[ -d testoutput/in_memory_downsampled_tiff/color/2-2-1 ]

wkcuber/tests/scripts/tiff_cubing.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ python -m wkcuber.cubing \
55
--batch_size 8 \
66
--layer_name color \
77
--wkw_file_len 32 \
8+
--scale 1 \
89
testdata/tiff testoutput/tiff
910
[ -d testoutput/tiff/color ]
1011
[ -d testoutput/tiff/color/1 ]

wkcuber/tests/scripts/tiff_formats_cubing.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ python -m wkcuber.convert_image_stack_to_wkw \
88
--batch_size 8 \
99
--layer_name color \
1010
--max_mag 4 \
11-
--target_mag 2\
11+
--target_mag 2 \
1212
--scale 1,1,1 \
1313
--name awesome_data \
1414
--no_compress \

wkcuber/tests/scripts/tile_cubing.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ python -m wkcuber.tile_cubing \
44
--jobs 2 \
55
--batch_size 8 \
66
--layer_name color \
7+
--scale 1 \
78
testdata/temca2 testoutput/temca2
89
[ -d testoutput/temca2/color ]
910
[ -d testoutput/temca2/color/1 ]

wkcuber/tests/test_metadata.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import numpy as np
33
import wkw
44

5-
from wkcuber.cubing import ensure_wkw
6-
from wkcuber.utils import WkwDatasetInfo, open_wkw
5+
from wkcuber.utils import WkwDatasetInfo, open_wkw, ensure_wkw
76
from wkcuber.metadata import (
87
write_webknossos_metadata,
98
refresh_metadata,

wkcuber/wkcuber/convert_image_stack_to_wkw.py

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22
from argparse import Namespace, ArgumentParser
33
from typing import Sequence
44

5-
from wkcuber import downsample_mags
6-
from .compress import compress_mag_inplace
75
from .cubing import (
86
get_channel_and_sample_count_and_dtype,
97
cubing,
108
create_parser as create_cubing_parser,
119
)
1210
from .mag import Mag
13-
from .metadata import write_webknossos_metadata, refresh_metadata
1411
from .utils import (
1512
add_isotropic_flag,
1613
setup_logging,
17-
add_scale_flag,
1814
add_sampling_mode_flag,
1915
get_executor_args,
2016
is_wk_compatible_layer_format,
@@ -66,7 +62,6 @@ def create_parser() -> ArgumentParser:
6662
)
6763

6864
parser.add_argument("--name", "-n", help="Name of the dataset", default=None)
69-
add_scale_flag(parser)
7065
add_isotropic_flag(parser)
7166
add_sampling_mode_flag(parser)
7267

@@ -151,58 +146,44 @@ def main(args: Namespace) -> None:
151146
sample_iter = range(sample_count)
152147

153148
layer_count = 0
154-
bounding_box = None
149+
layers = []
155150
for channel_index in channel_iter:
156151
for sample_index in sample_iter:
157-
bounding_box = cubing(
158-
args.source_path,
159-
args.target_path,
160-
f"{args.layer_name}_{layer_count}"
161-
if len(channel_iter) * len(sample_iter) > 1
162-
else args.layer_name,
163-
arg_dict.get("batch_size"),
164-
channel_index,
165-
sample_index,
166-
arg_dict.get("dtype"),
167-
args.target_mag,
168-
args.wkw_file_len,
169-
args.interpolation_mode,
170-
args.start_z,
171-
args.pad,
172-
args,
152+
layers.append(
153+
cubing(
154+
args.source_path,
155+
args.target_path,
156+
f"{args.layer_name}_{layer_count}"
157+
if len(channel_iter) * len(sample_iter) > 1
158+
else args.layer_name,
159+
arg_dict.get("batch_size"),
160+
channel_index,
161+
sample_index,
162+
arg_dict.get("dtype"),
163+
args.target_mag,
164+
args.wkw_file_len,
165+
args.interpolation_mode,
166+
args.start_z,
167+
args.skip_first_z_slices,
168+
args.pad,
169+
args.scale,
170+
args,
171+
)
173172
)
174173
layer_count += 1
175174

176-
write_webknossos_metadata(
177-
args.target_path,
178-
args.name,
179-
args.scale,
180-
compute_max_id=False,
181-
exact_bounding_box=bounding_box,
182-
)
183-
184-
for i in range(layer_count):
175+
for layer in layers:
185176
if not args.no_compress:
186-
compress_mag_inplace(
187-
args.target_path,
188-
f"{args.layer_name}_{i}" if layer_count > 1 else args.layer_name,
189-
args.target_mag,
190-
args,
191-
)
177+
layer.get_mag(args.target_mag).compress(args=args)
192178

193-
downsample_mags(
194-
path=args.target_path,
195-
layer_name=f"{args.layer_name}_{i}" if layer_count > 1 else args.layer_name,
179+
layer.downsample(
196180
from_mag=args.target_mag,
197181
max_mag=None if args.max_mag is None else Mag(args.max_mag),
198-
interpolation_mode="default",
199182
compress=not args.no_compress,
200183
sampling_mode=args.sampling_mode,
201184
args=get_executor_args(args),
202185
)
203186

204-
refresh_metadata(args.target_path)
205-
206187

207188
if __name__ == "__main__":
208189
args = create_parser().parse_args()

0 commit comments

Comments
 (0)