Skip to content

Commit

Permalink
Add converter to voxel_size factor. (#1139)
Browse files Browse the repository at this point in the history
* Add converter to voxel_size factor.

* Update changelog.

* Update converter to pass typecheck.
  • Loading branch information
markbader committed Jul 22, 2024
1 parent 017b02d commit 52bf88d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions webknossos/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion webknossos/webknossos/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion webknossos/webknossos/dataset/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Any,
Callable,
Dict,
Iterable,
List,
Optional,
Tuple,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down

0 comments on commit 52bf88d

Please sign in to comment.