Skip to content

Commit 7bc9773

Browse files
markbadernormanrz
andauthored
Add layer_name to kwargs of from_images (#1054)
* Add layer_name to parameters of from_images method. * Update Changelog.md * Update webknossos/webknossos/dataset/dataset.py Co-authored-by: Norman Rzepka <[email protected]> --------- Co-authored-by: Norman Rzepka <[email protected]>
1 parent 11f9931 commit 7bc9773

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
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 `layer_name` as optional argument to `Dataset.from_images` method. If the created dataset contains only a single layer, `layer_name` is used, otherwise the given `layer_name` is a common prefix for all layers. [1054](https://github.com/scalableminds/webknossos-libs/pull/1054)
2021
- The context variable of View.get_buffered_slice_writer() is a BufferedSliceWriter now instead of a Generator. Interaction with the SliceWriter does not change, but updating the offset after first initialization is possible now. [1052](https://github.com/scalableminds/webknossos-libs/pull/1052)
2122

2223
### Fixed

webknossos/tests/dataset/test_from_images.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ def test_compare_tifffile(tmp_path: Path) -> None:
2525
tmp_path,
2626
(1, 1, 1),
2727
compress=True,
28+
layer_name="tiff_stack",
2829
layer_category="segmentation",
2930
chunks_per_shard=(8, 8, 8),
3031
map_filepath_to_layer_name=wk.Dataset.ConversionLayerMapping.ENFORCE_SINGLE_LAYER,
3132
)
3233
assert len(ds.layers) == 1
33-
assert "tiff" in ds.layers
34-
data = ds.layers["tiff"].get_finest_mag().read()[0, :, :]
34+
assert "tiff_stack" in ds.layers
35+
data = ds.layers["tiff_stack"].get_finest_mag().read()[0, :, :]
3536
for z_index in range(0, data.shape[-1]):
3637
with TiffFile(TESTDATA_DIR / "tiff" / "test.0000.tiff") as tif_file:
3738
comparison_slice = tif_file.asarray().T
@@ -43,14 +44,15 @@ def test_multiple_multitiffs(tmp_path: Path) -> None:
4344
TESTDATA_DIR / "various_tiff_formats",
4445
tmp_path,
4546
(1, 1, 1),
47+
layer_name="tiffs",
4648
)
4749
assert len(ds.layers) == 4
4850

4951
expected_dtype_channels_size_per_layer = {
50-
"test_CS.tif": ("uint8", 3, (128, 128, 320)),
51-
"test_C.tif": ("uint8", 1, (128, 128, 320)),
52-
"test_I.tif": ("uint32", 1, (64, 128, 64)),
53-
"test_S.tif": ("uint16", 3, (128, 128, 64)),
52+
"tiffs_test_CS.tif": ("uint8", 3, (128, 128, 320)),
53+
"tiffs_test_C.tif": ("uint8", 1, (128, 128, 320)),
54+
"tiffs_test_I.tif": ("uint32", 1, (64, 128, 64)),
55+
"tiffs_test_S.tif": ("uint16", 3, (128, 128, 64)),
5456
}
5557

5658
for layer_name, layer in ds.layers.items():

webknossos/webknossos/cli/convert.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def main(
4545
metavar="VOXEL_SIZE",
4646
),
4747
],
48+
layer_name: Annotated[
49+
Optional[str],
50+
typer.Option(
51+
help="This name is used if only one layer is created. Otherwise this name is used as a common prefix for all layers.",
52+
),
53+
] = None,
4854
category: Annotated[
4955
Optional[LayerCategory],
5056
typer.Option(
@@ -107,5 +113,6 @@ def main(
107113
data_format=data_format,
108114
executor=executor,
109115
compress=compress,
116+
layer_name=layer_name,
110117
layer_category=category.value if category else None,
111118
)

webknossos/webknossos/dataset/dataset.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ def from_images(
566566
ConversionLayerMapping, Callable[[Path], str]
567567
] = ConversionLayerMapping.INSPECT_SINGLE_FILE,
568568
z_slices_sort_key: Callable[[Path], Any] = natsort_keygen(),
569+
layer_name: Optional[str] = None,
569570
layer_category: Optional[LayerCategoryType] = None,
570571
data_format: Union[str, DataFormat] = DEFAULT_DATA_FORMAT,
571572
chunk_shape: Optional[Union[Vec3IntLike, int]] = None,
@@ -593,6 +594,9 @@ def from_images(
593594
The order of the z-slices can be customized by setting
594595
`z_slices_sort_key`.
595596
597+
If a layer_name is set, this name is used if a single layer is created.
598+
Otherwise the layer_name is used as a common prefix for all layers.
599+
596600
The category of layers (`color` vs `segmentation`) is determined
597601
automatically by checking if `segmentation` is part of the path.
598602
Alternatively, a category can be enforced by passing `layer_category`.
@@ -634,20 +638,32 @@ def from_images(
634638

635639
filepaths_per_layer: Dict[str, List[Path]] = {}
636640
for input_file in input_files:
637-
layer_name = map_filepath_to_layer_name(input_file)
641+
layer_name_from_mapping = map_filepath_to_layer_name(input_file)
638642
# Remove characters from layer name that are not allowed
639-
layer_name = _UNALLOWED_LAYER_NAME_CHARS.sub("", layer_name)
643+
layer_name_from_mapping = _UNALLOWED_LAYER_NAME_CHARS.sub(
644+
"", layer_name_from_mapping
645+
)
640646
# Ensure layer name does not start with a dot
641-
layer_name = layer_name.lstrip(".")
647+
layer_name_from_mapping = layer_name_from_mapping.lstrip(".")
642648

643649
assert (
644-
layer_name != ""
650+
layer_name_from_mapping != ""
645651
), f"Could not determine a layer name for {input_file}."
646652

647-
filepaths_per_layer.setdefault(layer_name, []).append(
653+
filepaths_per_layer.setdefault(layer_name_from_mapping, []).append(
648654
input_path / input_file
649655
)
650656

657+
if layer_name is not None:
658+
if len(filepaths_per_layer) == 1:
659+
filepaths_per_layer[layer_name] = filepaths_per_layer.pop(
660+
layer_name_from_mapping
661+
)
662+
else:
663+
filepaths_per_layer = {
664+
f"{layer_name}_{k}": v for k, v in filepaths_per_layer.items()
665+
}
666+
651667
for layer_name, filepaths in filepaths_per_layer.items():
652668
filepaths.sort(key=z_slices_sort_key)
653669
category: LayerCategoryType

0 commit comments

Comments
 (0)