Skip to content

Commit 525d4a8

Browse files
authored
Add only_setup_mag(s) parameter to downsample methods (#610)
* add only_setup_mag(s) parameter to downsample methods to allow parallel downsampling without writing outdated datasource-properties.json * update changelog * add test
1 parent 1077117 commit 525d4a8

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

webknossos/Changelog.md

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

1414
### Added
15+
- Added `only_setup_mag` parameter to downsample-related methods in `Layer`. This parameter allows creating output magnifications before parallelizing downsampling invocations to avoid outdated writes to datasource-properties.json. [#610](https://github.com/scalableminds/webknossos-libs/pull/610)
1516
- Added `Task.create()` method to create tasks by prodiving a dataset name, location, and rotation. [#605](https://github.com/scalableminds/webknossos-libs/pull/605)
1617

1718
### Changed

webknossos/webknossos/dataset/layer.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ def downsample(
515515
buffer_edge_len: Optional[int] = None,
516516
force_sampling_scheme: bool = False,
517517
args: Optional[Namespace] = None,
518+
only_setup_mags: bool = False,
518519
) -> None:
519520
"""
520521
Downsamples the data starting from `from_mag` until a magnification is `>= max(max_mag)`.
@@ -602,6 +603,7 @@ def downsample(
602603
compress=compress,
603604
buffer_edge_len=buffer_edge_len,
604605
args=args,
606+
only_setup_mag=only_setup_mags,
605607
)
606608

607609
def downsample_mag(
@@ -613,6 +615,7 @@ def downsample_mag(
613615
buffer_edge_len: Optional[int] = None,
614616
args: Optional[Namespace] = None,
615617
allow_overwrite: bool = False,
618+
only_setup_mag: bool = False,
616619
) -> None:
617620
"""
618621
Performs a single downsampling step from `from_mag` to `target_mag`.
@@ -626,6 +629,11 @@ def downsample_mag(
626629
627630
The `args` can contain information to distribute the computation.
628631
If allow_overwrite is True, an existing Mag may be overwritten.
632+
633+
If only_setup_mag is True, the magnification is created, but left
634+
empty. This parameter can be used to prepare for parallel downsampling
635+
of multiple layers while avoiding parallel writes with outdated updates
636+
to the datasource-properties.json file.
629637
"""
630638
assert (
631639
from_mag in self.mags.keys()
@@ -652,6 +660,9 @@ def downsample_mag(
652660
target_mag, prev_mag_view, compress
653661
)
654662

663+
if only_setup_mag:
664+
return
665+
655666
bb_mag1 = self.bounding_box
656667

657668
aligned_source_bb = bb_mag1.align_with_mag(target_mag, ceil=True).in_mag(
@@ -731,6 +742,7 @@ def downsample_mag_list(
731742
buffer_edge_len: Optional[int] = None,
732743
args: Optional[Namespace] = None,
733744
allow_overwrite: bool = False,
745+
only_setup_mags: bool = False,
734746
) -> None:
735747
"""
736748
Downsamples the data starting at `from_mag` to each magnification in `target_mags` iteratively.
@@ -762,6 +774,7 @@ def downsample_mag_list(
762774
buffer_edge_len=buffer_edge_len,
763775
args=args,
764776
allow_overwrite=allow_overwrite,
777+
only_setup_mag=only_setup_mags,
765778
)
766779
source_mag = target_mag
767780

wkcuber/tests/test_downsampling.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,24 @@ def test_downsample_mag_list() -> None:
347347
assert m in layer.mags
348348

349349

350+
def test_downsample_mag_list_with_only_setup_mags() -> None:
351+
ds = Dataset(TESTOUTPUT_DIR / "downsample_mag_list", scale=(1, 1, 2))
352+
layer = ds.add_layer("color", COLOR_CATEGORY)
353+
mag = layer.add_mag(1)
354+
mag.write(data=(np.random.rand(10, 20, 30) * 255).astype(np.uint8))
355+
356+
target_mags = [Mag([4, 4, 8]), Mag(2), Mag([32, 32, 8]), Mag(32)] # unsorted list
357+
358+
layer.downsample_mag_list(
359+
from_mag=Mag(1), target_mags=target_mags, only_setup_mags=True
360+
)
361+
362+
for m in target_mags:
363+
assert np.all(
364+
layer.get_mag(m).read((0, 0, 0), (10, 20, 30)) == 0
365+
), "The mags should be empty."
366+
367+
350368
def test_downsample_with_invalid_mag_list() -> None:
351369
ds = Dataset(TESTOUTPUT_DIR / "downsample_mag_list", scale=(1, 1, 2))
352370
layer = ds.add_layer("color", COLOR_CATEGORY)

0 commit comments

Comments
 (0)