From 52bf88db5dd2fa2eb93b7ec717433ae59bd95577 Mon Sep 17 00:00:00 2001 From: Mark Bader Date: Mon, 22 Jul 2024 10:52:06 +0200 Subject: [PATCH] Add converter to voxel_size factor. (#1139) * Add converter to voxel_size factor. * Update changelog. * Update converter to pass typecheck. --- webknossos/Changelog.md | 1 + webknossos/webknossos/dataset/dataset.py | 2 +- webknossos/webknossos/dataset/properties.py | 15 ++++++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/webknossos/Changelog.md b/webknossos/Changelog.md index 4c8b2ebd4..9d26961f4 100644 --- a/webknossos/Changelog.md +++ b/webknossos/Changelog.md @@ -19,6 +19,7 @@ For upgrade instructions, please check the respective _Breaking Changes_ section ### Changed ### Fixed +- Add a converter to the VoxelSize field `factor`, to ensure it is a tuple. ## [0.14.25](https://github.com/scalableminds/webknossos-libs/releases/tag/v0.14.25) - 2024-07-18 diff --git a/webknossos/webknossos/dataset/dataset.py b/webknossos/webknossos/dataset/dataset.py index 89c0cd308..9db6ee9ae 100644 --- a/webknossos/webknossos/dataset/dataset.py +++ b/webknossos/webknossos/dataset/dataset.py @@ -359,7 +359,7 @@ def __init__( else: assert ( self.voxel_size_with_unit == voxel_size_with_unit - ), f"Cannot open Dataset: The dataset {dataset_path} already exists, but the voxel_sizes do not match ({self.voxel_size} != {voxel_size})" + ), f"Cannot open Dataset: The dataset {dataset_path} already exists, but the voxel_sizes do not match ({self.voxel_size_with_unit} != {voxel_size_with_unit})" if name is not None: assert ( self.name == name diff --git a/webknossos/webknossos/dataset/properties.py b/webknossos/webknossos/dataset/properties.py index ca5233cfd..9721f7b8e 100644 --- a/webknossos/webknossos/dataset/properties.py +++ b/webknossos/webknossos/dataset/properties.py @@ -4,6 +4,7 @@ Any, Callable, Dict, + Iterable, List, Optional, Tuple, @@ -65,6 +66,18 @@ def _extract_num_channels( return array.info.num_channels +def float_tpl(voxel_size: Union[List, Tuple]) -> Iterable: + # Fix for mypy bug https://github.com/python/mypy/issues/5313. + # Solution based on other issue for the same bug: https://github.com/python/mypy/issues/8389. + return tuple( + ( + voxel_size[0], + voxel_size[1], + voxel_size[2], + ) + ) + + _properties_floating_type_to_python_type: Dict[Union[str, type], np.dtype] = { "float": np.dtype("float32"), # np.float: np.dtype("float32"), # np.float is an alias for float @@ -183,7 +196,7 @@ class SegmentationLayerProperties(LayerProperties): @attr.define class VoxelSize: - factor: Tuple[float, float, float] + factor: Tuple[float, float, float] = attr.field(converter=float_tpl) unit: LengthUnit = DEFAULT_LENGTH_UNIT def to_nanometer(self) -> Tuple[float, float, float]: