Skip to content

Commit bba77ac

Browse files
authored
Adapt from images to convert single file (#1152)
* Adapt from images to convert single file. * Update changelog. * Add test for conversion of single file with CLI. * Adapt test. * Adapt docstring of from_images.
1 parent cdb2a92 commit bba77ac

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

webknossos/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ For upgrade instructions, please check the respective _Breaking Changes_ section
2020
### Changed
2121
- Added options `--layer-name` and `--mag` for compress command of the CLI. [#1141](https://github.com/scalableminds/webknossos-libs/pull/1141)
2222
- Added options `--chunk-shape` and `--chunks-per-shard` for convert command of the CLI. [#1150](https://github.com/scalableminds/webknossos-libs/pull/1150)
23+
- The `from_images` method of the `Dataset` supports directories and single files as `input_path` now. [#1152](https://github.com/scalableminds/webknossos-libs/pull/1152)
2324

2425
### Fixed
2526
- Fixed issue with webknossos URL and context URL being considered different when opening a remote dataset due to trailing slashes. [#1137](https://github.com/scalableminds/webknossos-libs/pull/1137)

webknossos/tests/test_cli.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,29 @@ def test_convert() -> None:
238238
assert (wkw_path / PROPERTIES_FILE_NAME).exists()
239239

240240

241+
@pytest.mark.filterwarnings("ignore::UserWarning")
242+
def test_convert_single_file() -> None:
243+
"""Tests the functionality of convert subcommand when given single file instead of directory."""
244+
245+
with tmp_cwd():
246+
origin_path = TESTDATA_DIR / "tiff" / "test.0000.tiff"
247+
wkw_path = Path("wkw_from_tiff_single_file")
248+
249+
result = runner.invoke(
250+
app,
251+
[
252+
"convert",
253+
"--voxel-size",
254+
"11.0,11.0,11.0",
255+
str(origin_path),
256+
str(wkw_path),
257+
],
258+
)
259+
260+
assert result.exit_code == 0
261+
assert (wkw_path / PROPERTIES_FILE_NAME).exists()
262+
263+
241264
def test_convert_with_all_params() -> None:
242265
"""Tests the functionality of convert subcommand."""
243266

webknossos/webknossos/dataset/dataset.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -598,12 +598,12 @@ def from_images(
598598
executor: Optional[Executor] = None,
599599
) -> "Dataset":
600600
"""
601-
This method imports image data in a folder as a WEBKNOSSOS dataset. The
602-
image data can be 3D images (such as multipage tiffs) or stacks of 2D
603-
images. In case of multiple 3D images or image stacks, those are mapped
604-
to different layers. The exact mapping is handled by the argument
605-
`map_filepath_to_layer_name`, which can be a pre-defined strategy from
606-
the enum `ConversionLayerMapping`, or a custom callable, taking
601+
This method imports image data in a folder or from a file as a
602+
WEBKNOSSOS dataset. The image data can be 3D images (such as multipage
603+
tiffs) or stacks of 2D images. In case of multiple 3D images or image
604+
stacks, those are mapped to different layers. The exact mapping is handled
605+
by the argument `map_filepath_to_layer_name`, which can be a pre-defined
606+
strategy from the enum `ConversionLayerMapping`, or a custom callable, taking
607607
a path of an image file and returning the corresponding layer name. All
608608
files belonging to the same layer name are then grouped. In case of
609609
multiple files per layer, those are usually mapped to the z-dimension.
@@ -615,7 +615,9 @@ def from_images(
615615
616616
The category of layers (`color` vs `segmentation`) is determined
617617
automatically by checking if `segmentation` is part of the path.
618-
Alternatively, a category can be enforced by passing `layer_category`.
618+
The category decision is evaluated and corrected after data import with a
619+
data driven approach. Alternatively, a category can be enforced by passing
620+
`layer_category`.
619621
620622
Further arguments behave as in `add_layer_from_images`, please also
621623
refer to its documentation.
@@ -630,11 +632,17 @@ def from_images(
630632
if use_bioformats is not False:
631633
valid_suffixes.update(pims_images.get_valid_bioformats_suffixes())
632634

633-
input_files = [
634-
i.relative_to(input_upath)
635-
for i in input_upath.glob("**/*")
636-
if i.is_file() and i.suffix.lstrip(".").lower() in valid_suffixes
637-
]
635+
if input_upath.is_dir():
636+
input_files = [
637+
i.relative_to(input_upath)
638+
for i in input_upath.glob("**/*")
639+
if i.is_file() and i.suffix.lstrip(".").lower() in valid_suffixes
640+
]
641+
else:
642+
if input_upath.suffix.lstrip(".").lower() in valid_suffixes:
643+
input_files = [UPath(input_upath.name)]
644+
input_upath = input_upath.parent
645+
638646
if len(input_files) == 0:
639647
raise ValueError(
640648
"Could not find any supported image data. "
@@ -683,7 +691,7 @@ def from_images(
683691
), f"Could not determine a layer name for {input_file}."
684692

685693
filepaths_per_layer.setdefault(layer_name_from_mapping, []).append(
686-
input_path / input_file
694+
input_upath / input_file
687695
)
688696

689697
if layer_name is not None:

0 commit comments

Comments
 (0)