Skip to content

Commit 2a79f51

Browse files
authored
Add layer_name and mag as args for compress command. (#1141)
* Add layer_name and mag as args for compress command. * Update changelog.
1 parent 366ea7a commit 2a79f51

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

webknossos/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ For upgrade instructions, please check the respective _Breaking Changes_ section
1717
### Added
1818

1919
### Changed
20+
- Added options `--layer-name` and `--mag` for compress command of the CLI. [#1141](https://github.com/scalableminds/webknossos-libs/pull/1141)
2021

2122
### Fixed
2223

webknossos/tests/test_cli.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,42 @@ def test_compress() -> None:
176176
assert result.exit_code == 0
177177

178178

179+
def test_compress_with_args() -> None:
180+
"""Tests the functionality of compress subcommand."""
181+
182+
with tmp_cwd():
183+
wkw_path = TESTDATA_DIR / "simple_wkw_dataset"
184+
copytree(wkw_path, Path("testdata") / "simple_wkw_dataset")
185+
186+
result = runner.invoke(
187+
app,
188+
[
189+
"compress",
190+
"--layer-name",
191+
"color",
192+
"--mag",
193+
"1",
194+
"testdata/simple_wkw_dataset",
195+
],
196+
)
197+
198+
assert result.exit_code == 0
199+
200+
result_with_wrong_mag = runner.invoke(
201+
app,
202+
[
203+
"compress",
204+
"--layer-name",
205+
"color",
206+
"--mag",
207+
"2",
208+
"testdata/simple_wkw_dataset",
209+
],
210+
)
211+
212+
assert result_with_wrong_mag.exit_code == 1
213+
214+
179215
@pytest.mark.filterwarnings("ignore::UserWarning")
180216
def test_convert() -> None:
181217
"""Tests the functionality of convert subcommand."""

webknossos/webknossos/cli/compress.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
from argparse import Namespace
44
from multiprocessing import cpu_count
5-
from typing import Any, Optional
5+
from typing import Any, List, Optional
66

77
import typer
88
from typing_extensions import Annotated
99

10+
from webknossos.geometry.mag import Mag
11+
1012
from ..dataset import Dataset
1113
from ..utils import get_executor_for_args
12-
from ._utils import DistributionStrategy, parse_path
14+
from ._utils import DistributionStrategy, parse_mag, parse_path
1315

1416

1517
def main(
@@ -22,6 +24,22 @@ def main(
2224
parser=parse_path,
2325
),
2426
],
27+
layer_name: Annotated[
28+
Optional[str],
29+
typer.Option(
30+
help="Name of the layer to be compressed. If not provided, all layers will be compressed.",
31+
),
32+
] = None,
33+
mag: Annotated[
34+
Optional[List[Mag]],
35+
typer.Option(
36+
help="Mags that should be compressed. "
37+
"Should be number or minus separated string (e.g. 2 or 2-2-2). "
38+
"For multiple mags type: --mag 1 --mag 2",
39+
parser=parse_mag,
40+
metavar="MAG",
41+
),
42+
] = None,
2543
jobs: Annotated[
2644
int,
2745
typer.Option(
@@ -53,5 +71,20 @@ def main(
5371
job_resources=job_resources,
5472
)
5573

74+
ds = Dataset.open(target)
75+
if layer_name is None:
76+
layers = list(ds.layers.values())
77+
else:
78+
layers = [ds.get_layer(layer_name)]
79+
5680
with get_executor_for_args(args=executor_args) as executor:
57-
Dataset.open(target).compress(executor=executor)
81+
for layer in layers:
82+
if mag is None:
83+
mags = list(layer.mags.values())
84+
else:
85+
mags = [layer.get_mag(mag) for mag in mag]
86+
for current_mag in mags:
87+
if not current_mag._is_compressed():
88+
current_mag.compress(executor=executor)
89+
else:
90+
typer.echo(f"Skipping {current_mag} as it is already compressed.")

0 commit comments

Comments
 (0)