Skip to content

Commit 12e1ede

Browse files
philippottovalentin-pinkau
authored andcommitted
Add anisotropic parameter to downsampling tool (#88)
* add anisotropic parameter to downsampling tool * black * incorporate feedback
1 parent 3350277 commit 12e1ede

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Created with [Python3](https://www.python.org/).
1414
* `wkcuber.cubing`: Convert image stacks (e.g., `tiff`, `jpg`, `png`, `dm3`) to WKW cubes
1515
* `wkcuber.tile_cubing`: Convert tiled image stacks (e.g. in `z/y/x.ext` folder structure) to WKW cubes
1616
* `wkcuber.convert_knossos`: Convert KNOSSOS cubes to WKW cubes
17-
* `wkcuber.downsampling`: Create downsampled magnifications (with `median`, `mode` and linear interpolation modes)
17+
* `wkcuber.downsampling`: Create downsampled magnifications (with `median`, `mode` and linear interpolation modes). Downsampling compresses the new magnifications by default (disable via `--no-compress`).
1818
* `wkcuber.compress`: Compress WKW cubes for efficient file storage (especially useful for segmentation data)
1919
* `wkcuber.metadata`: Create (or refresh) metadata (with guessing of most parameters)
2020
* Most modules support multiprocessing

wkcuber/downsampling.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from functools import lru_cache
1111
from enum import Enum
1212
from .mag import Mag
13+
from wkcuber.metadata import read_datasource_properties
1314

1415
from .utils import (
1516
add_verbose_flag,
@@ -21,6 +22,7 @@
2122
add_distribution_flags,
2223
get_executor_for_args,
2324
wait_and_ensure_success,
25+
add_anisotropic_flag,
2426
)
2527

2628
DEFAULT_EDGE_LEN = 256
@@ -73,11 +75,21 @@ def create_parser():
7375
# Either provide the maximum resolution to be downsampled OR a specific, anisotropic magnification.
7476
group = parser.add_mutually_exclusive_group()
7577
group.add_argument(
76-
"--max", "-m", help="Max resolution to be downsampled", type=int, default=512
78+
"--max",
79+
"-m",
80+
help="Max resolution to be downsampled. In case of anisotropic downsampling, the process is considered "
81+
"done when max(current_mag) >= max(max_mag) where max takes the largest dimension of the mag tuple "
82+
"x, y, z. For example, a maximum mag value of 8 (or 8-8-8) will stop the downsampling as soon as a "
83+
"magnification is produced for which one dimension is equal or larger than 8.",
84+
type=int,
85+
default=512,
7786
)
7887
group.add_argument(
7988
"--anisotropic_target_mag",
80-
help="Specify an anisotropic target magnification which should be created (e.g., --anisotropic_target_mag 2-2-1)",
89+
help="Specify an explicit anisotropic target magnification which should be "
90+
"created (e.g., --anisotropic_target_mag 2-2-1). Consider using --anisotropic "
91+
"instead which automatically creates multiple anisotropic magnifications depending "
92+
"on the dataset's scale",
8193
type=str,
8294
)
8395

@@ -90,10 +102,14 @@ def create_parser():
90102
)
91103

92104
parser.add_argument(
93-
"--compress", action="store_true", help="Compress data during downsampling"
105+
"--no_compress",
106+
help="Don't compress data during downsampling",
107+
default=False,
108+
action="store_true",
94109
)
95110

96111
add_verbose_flag(parser)
112+
add_anisotropic_flag(parser)
97113
add_distribution_flags(parser)
98114

99115
return parser
@@ -530,7 +546,29 @@ def detect_larger_and_smaller_dimension(scale):
530546
anisotropic_target_mag,
531547
args.interpolation_mode,
532548
args.buffer_cube_size,
533-
args.compress,
549+
not args.no_compress,
550+
args,
551+
)
552+
elif args.anisotropic:
553+
try:
554+
scale = read_datasource_properties(args.path)["scale"]
555+
except Exception as exc:
556+
logging.error(
557+
"Could not determine scale which is necessary "
558+
"to find target magnifications for anisotropic downsampling. "
559+
"Does the provided dataset have a datasource-properties.json file?"
560+
)
561+
raise exc
562+
563+
downsample_mags_anisotropic(
564+
args.path,
565+
args.layer_name,
566+
from_mag,
567+
max_mag,
568+
scale,
569+
args.interpolation_mode,
570+
DEFAULT_EDGE_LEN,
571+
not args.no_compress,
534572
args,
535573
)
536574
else:
@@ -541,6 +579,6 @@ def detect_larger_and_smaller_dimension(scale):
541579
max_mag,
542580
args.interpolation_mode,
543581
args.buffer_cube_size,
544-
args.compress,
582+
not args.no_compress,
545583
args,
546584
)

wkcuber/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def add_anisotropic_flag(parser):
6565
parser.add_argument(
6666
"--anisotropic",
6767
"-a",
68-
help="Activates Anisotropic downsampling. It will detect which dimension ist the smallest and the largest. "
69-
"The largest dimension will only be down sampled by 2 if it would be smaller or equal to the smallest "
68+
help="Activates Anisotropic downsampling. It will detect which dimension is the smallest and the largest. "
69+
"The largest dimension will only be downsampled by 2 if it would be smaller or equal to the smallest "
7070
"dimension in the next downsampling step.",
7171
dest="anisotropic",
7272
action="store_true",

0 commit comments

Comments
 (0)