From a3611b675307dfe5368872d349069a43d56c35f0 Mon Sep 17 00:00:00 2001 From: Norman Rzepka Date: Wed, 23 Mar 2022 15:02:16 +0100 Subject: [PATCH] fixes compression of downsampled mags (#667) * fixes compression of downsampled mags * changelog * changelog * types --- webknossos/Changelog.md | 2 ++ webknossos/tests/test_dataset.py | 16 ++++++++++++++++ webknossos/webknossos/dataset/mag_view.py | 5 +++++ 3 files changed, 23 insertions(+) diff --git a/webknossos/Changelog.md b/webknossos/Changelog.md index fdea59ac9..e68e220ad 100644 --- a/webknossos/Changelog.md +++ b/webknossos/Changelog.md @@ -18,8 +18,10 @@ For upgrade instructions, please check the respective *Breaking Changes* section ### Added ### Changed +- `MagView.compress` now skips in-place compression of already compressed mags. [#667](https://github.com/scalableminds/webknossos-libs/pull/667) ### Fixed +- Fixed compression of downsampled mags for layers with arbitrary and potentially mag-unaligned bounding boxes. [#667](https://github.com/scalableminds/webknossos-libs/pull/667) ## [0.9.12](https://github.com/scalableminds/webknossos-libs/releases/tag/v0.9.12) - 2022-03-18 diff --git a/webknossos/tests/test_dataset.py b/webknossos/tests/test_dataset.py index 14aafb1aa..7f58b238e 100644 --- a/webknossos/tests/test_dataset.py +++ b/webknossos/tests/test_dataset.py @@ -2155,3 +2155,19 @@ def test_warn_outdated_properties(tmp_path: Path) -> None: # Changing ds1 should raise a warning, since ds1 # does not know about the change in ds2 ds1.add_layer("color", COLOR_CATEGORY) + + +def test_can_compress_mag8(tmp_path: Path) -> None: + ds = Dataset(tmp_path / "ds", scale=(1, 1, 1)) + + layer = ds.add_layer("color", COLOR_CATEGORY) + layer.bounding_box = BoundingBox((0, 0, 0), (12240, 12240, 685)) + for mag in ["1", "2-2-1", "4-4-1", "8-8-2"]: + layer.add_mag(mag) + + assert layer.bounding_box == BoundingBox((0, 0, 0), (12240, 12240, 685)) + + mag_view = layer.get_mag("8-8-2") + data_to_write = (np.random.rand(1, 10, 10, 10) * 255).astype(np.uint8) + mag_view.write(data_to_write, absolute_offset=(11264, 11264, 0)) + mag_view.compress() diff --git a/webknossos/webknossos/dataset/mag_view.py b/webknossos/webknossos/dataset/mag_view.py index a54e12a9b..1a044fb9b 100644 --- a/webknossos/webknossos/dataset/mag_view.py +++ b/webknossos/webknossos/dataset/mag_view.py @@ -269,6 +269,10 @@ def compress( from webknossos.dataset.dataset import Dataset + if target_path is None and self._is_compressed(): + logging.info(f"Mag {self.name} is already compressed") + return + if target_path is not None: target_path = Path(target_path) @@ -308,6 +312,7 @@ def compress( for bbox in self.get_bounding_boxes_on_disk(): bbox = bbox.intersected_with(self.layer.bounding_box, dont_assert=True) if not bbox.is_empty(): + bbox = bbox.align_with_mag(self.mag, ceil=True) source_view = self.get_view( absolute_offset=bbox.topleft, size=bbox.size )