Skip to content

Commit

Permalink
Test for ImageCoordinateSystemDicomSchema load
Browse files Browse the repository at this point in the history
  • Loading branch information
erikogabrielsson committed Oct 30, 2024
1 parent 896615b commit f03283f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
43 changes: 41 additions & 2 deletions tests/metadata/dicom_schema/test_dicom_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from datetime import datetime
from typing import Optional

import pytest
from pydicom import Dataset
Expand All @@ -36,7 +37,7 @@
LenseCode,
LightPathFilterCode,
)
from wsidicom.geometry import PointMm, SizeMm
from wsidicom.geometry import Orientation, PointMm, SizeMm
from wsidicom.instance import ImageType
from wsidicom.metadata import (
Equipment,
Expand Down Expand Up @@ -65,7 +66,10 @@
)
from wsidicom.metadata.schema.dicom.defaults import Defaults
from wsidicom.metadata.schema.dicom.equipment import EquipmentDicomSchema
from wsidicom.metadata.schema.dicom.image import ImageDicomSchema
from wsidicom.metadata.schema.dicom.image import (
ImageCoordinateSystemDicomSchema,
ImageDicomSchema,
)
from wsidicom.metadata.schema.dicom.label import LabelDicomSchema
from wsidicom.metadata.schema.dicom.optical_path import (
OpticalPathDicomSchema,
Expand Down Expand Up @@ -796,3 +800,38 @@ def test_deserialize_wsi_metadata_from_empty_dataset(

# Assert
assert isinstance(deserialized, WsiMetadata)

@pytest.mark.parametrize("origin", [PointMm(20.0, 30.0), None])
@pytest.mark.parametrize("orientation", [Orientation.from_rotation(90), None])
@pytest.mark.parametrize("z_offset", [1.0, None])
def test_deserialize_image_coordinate_system(
self,
origin: Optional[PointMm],
orientation: Optional[Orientation],
z_offset: Optional[float],
):
# Arrange
dataset = Dataset()
origin_dataset = Dataset()
if origin is not None:
origin_dataset.XOffsetInSlideCoordinateSystem = origin.x
origin_dataset.YOffsetInSlideCoordinateSystem = origin.y
if z_offset is not None:
origin_dataset.ZOffsetInSlideCoordinateSystem = z_offset
if len(origin_dataset) > 0:
dataset.TotalPixelMatrixOriginSequence = [origin_dataset]
if orientation is not None:
dataset.ImageOrientationSlide = list(orientation.values)

schema = ImageCoordinateSystemDicomSchema()
# Act
deserialized = schema.load(dataset)

# Assert
if origin is not None and orientation is not None:
assert isinstance(deserialized, ImageCoordinateSystem)
assert deserialized.origin == origin
assert deserialized.rotation == orientation.rotation
assert deserialized.z_offset == z_offset
else:
assert deserialized is None
6 changes: 2 additions & 4 deletions wsidicom/metadata/schema/dicom/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,15 @@ def load_type(self) -> Type[ImageCoordinateSystem]:
def load(self, dataset: Dataset, **kwargs) -> Optional[ImageCoordinateSystem]:
try:
return super().load(dataset, **kwargs)
except (TypeError, AttributeError):
except (TypeError, AttributeError, KeyError, IndexError):
return None

@post_load
def post_load(
self, data: Dict[str, Any], **kwargs
) -> Optional[ImageCoordinateSystem]:
"""Post load hook to handle separation of xy and z offset."""
origin: Optional[Tuple[ImageCoordinateSystem, float]] = data.pop("origin", None)
if origin is None:
return None
origin: Tuple[ImageCoordinateSystem, float] = data.pop("origin")
return super().post_load(
{"origin": (origin[0]), "rotation": data["rotation"], "z_offset": origin[1]}
)
Expand Down

0 comments on commit f03283f

Please sign in to comment.