Skip to content

Commit

Permalink
Add anisotropic parameter to downsampling tool (#88)
Browse files Browse the repository at this point in the history
* add anisotropic parameter to downsampling tool

* black

* incorporate feedback
  • Loading branch information
philippotto authored and valentin-pinkau committed May 17, 2019
1 parent 3350277 commit 12e1ede
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Created with [Python3](https://www.python.org/).
* `wkcuber.cubing`: Convert image stacks (e.g., `tiff`, `jpg`, `png`, `dm3`) to WKW cubes
* `wkcuber.tile_cubing`: Convert tiled image stacks (e.g. in `z/y/x.ext` folder structure) to WKW cubes
* `wkcuber.convert_knossos`: Convert KNOSSOS cubes to WKW cubes
* `wkcuber.downsampling`: Create downsampled magnifications (with `median`, `mode` and linear interpolation modes)
* `wkcuber.downsampling`: Create downsampled magnifications (with `median`, `mode` and linear interpolation modes). Downsampling compresses the new magnifications by default (disable via `--no-compress`).
* `wkcuber.compress`: Compress WKW cubes for efficient file storage (especially useful for segmentation data)
* `wkcuber.metadata`: Create (or refresh) metadata (with guessing of most parameters)
* Most modules support multiprocessing
Expand Down
48 changes: 43 additions & 5 deletions wkcuber/downsampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from functools import lru_cache
from enum import Enum
from .mag import Mag
from wkcuber.metadata import read_datasource_properties

from .utils import (
add_verbose_flag,
Expand All @@ -21,6 +22,7 @@
add_distribution_flags,
get_executor_for_args,
wait_and_ensure_success,
add_anisotropic_flag,
)

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

Expand All @@ -90,10 +102,14 @@ def create_parser():
)

parser.add_argument(
"--compress", action="store_true", help="Compress data during downsampling"
"--no_compress",
help="Don't compress data during downsampling",
default=False,
action="store_true",
)

add_verbose_flag(parser)
add_anisotropic_flag(parser)
add_distribution_flags(parser)

return parser
Expand Down Expand Up @@ -530,7 +546,29 @@ def detect_larger_and_smaller_dimension(scale):
anisotropic_target_mag,
args.interpolation_mode,
args.buffer_cube_size,
args.compress,
not args.no_compress,
args,
)
elif args.anisotropic:
try:
scale = read_datasource_properties(args.path)["scale"]
except Exception as exc:
logging.error(
"Could not determine scale which is necessary "
"to find target magnifications for anisotropic downsampling. "
"Does the provided dataset have a datasource-properties.json file?"
)
raise exc

downsample_mags_anisotropic(
args.path,
args.layer_name,
from_mag,
max_mag,
scale,
args.interpolation_mode,
DEFAULT_EDGE_LEN,
not args.no_compress,
args,
)
else:
Expand All @@ -541,6 +579,6 @@ def detect_larger_and_smaller_dimension(scale):
max_mag,
args.interpolation_mode,
args.buffer_cube_size,
args.compress,
not args.no_compress,
args,
)
4 changes: 2 additions & 2 deletions wkcuber/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def add_anisotropic_flag(parser):
parser.add_argument(
"--anisotropic",
"-a",
help="Activates Anisotropic downsampling. It will detect which dimension ist the smallest and the largest. "
"The largest dimension will only be down sampled by 2 if it would be smaller or equal to the smallest "
help="Activates Anisotropic downsampling. It will detect which dimension is the smallest and the largest. "
"The largest dimension will only be downsampled by 2 if it would be smaller or equal to the smallest "
"dimension in the next downsampling step.",
dest="anisotropic",
action="store_true",
Expand Down

0 comments on commit 12e1ede

Please sign in to comment.