Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tile size level matching #181

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Cache for compressed and decompressed tiles.
- Setting `strict_tile_size_check`, defaulting to `True`, to enable parsing of levels with different tile size.

### Changed

Expand Down
35 changes: 23 additions & 12 deletions wsidicom/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ class Settings:

def __init__(self) -> None:
self._strict_uid_check = False
self._strict_tile_size_check = True
self._strict_attribute_check = False
self._strict_specimen_identifier_check = True
self._focal_plane_distance_threshold = 0.000001
self._pyramids_origin_threshold = 0.02
self._prefered_decoder: Optional[str] = None
self._open_web_theads: Optional[int] = None
self._pillow_resampling_filter = Pillow.Resampling.BILINEAR
self._strict_specimen_identifier_check = True
self._ignore_specimen_preparation_step_on_validation_error = True
self._truncate_long_dicom_strings = False
self._decoded_frame_cache_size = 1000
Expand All @@ -54,6 +55,27 @@ def strict_attribute_check(self) -> bool:
def strict_attribute_check(self, value: bool) -> None:
self._strict_attribute_check = value

@property
def strict_specimen_identifier_check(self) -> bool:
"""If `True` the issuer of two specimen identifiers needs to match or both be
None for the identifiers to match. If `False` the identifiers will match also if
either issuer is None. Either way the identifier needs to match."""
return self._strict_specimen_identifier_check

@strict_specimen_identifier_check.setter
def strict_specimen_identifier_check(self, value: bool) -> None:
self._strict_specimen_identifier_check = value

@property
def strict_tile_size_check(self) -> bool:
"""If tile size need to match for levels. If `False` the tile size accross
levels are allowed to be non-uniform."""
return self._strict_tile_size_check

@strict_tile_size_check.setter
def strict_tile_size_check(self, value: bool) -> None:
self._strict_tile_size_check = value

@property
def focal_plane_distance_threshold(self) -> float:
"""Threshold in mm for which distances between focal planes are
Expand Down Expand Up @@ -104,17 +126,6 @@ def pillow_resampling_filter(self) -> Pillow.Resampling:
def pillow_resampling_filter(self, value: Pillow.Resampling) -> None:
self._pillow_resampling_filter = value

@property
def strict_specimen_identifier_check(self) -> bool:
"""If `True` the issuer of two specimen identifiers needs to match or both be
None for the identifiers to match. If `False` the identifiers will match also if
either issuer is None. Either way the identifier needs to match."""
return self._strict_specimen_identifier_check

@strict_specimen_identifier_check.setter
def strict_specimen_identifier_check(self, value: bool) -> None:
self._strict_specimen_identifier_check = value

@property
def ignore_specimen_preparation_step_on_validation_error(self) -> bool:
"""If ignore specimen preparation steps that fails to validate. If false all
Expand Down
5 changes: 4 additions & 1 deletion wsidicom/file/wsidicom_file_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from pydicom.uid import UID, MediaStorageDirectoryStorage
from upath import UPath

from wsidicom.config import settings
from wsidicom.errors import (
WsiDicomNotFoundError,
WsiDicomNotSupportedError,
Expand Down Expand Up @@ -99,7 +100,9 @@ def base_dataset(self) -> WsiDataset:
def level_instances(self) -> Iterable[WsiInstance]:
"""The level instances parsed from the source."""
return self._create_instances(
self._levels, self._slide_uids, self._base_tile_size
self._levels,
self._slide_uids,
self._base_tile_size if settings.strict_tile_size_check else None,
)

@property
Expand Down
7 changes: 5 additions & 2 deletions wsidicom/group/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

from PIL.Image import Image

from wsidicom import settings
from wsidicom.errors import WsiDicomNoResolutionError, WsiDicomOutOfBoundsError
from wsidicom.geometry import Point, Region, Size, SizeMm
from wsidicom.group.group import Group
from wsidicom.instance import WsiInstance
from wsidicom.stringprinting import dict_pretty_str
from wsidicom import settings


class Level(Group):
Expand Down Expand Up @@ -110,7 +110,10 @@ def matches(self, other_level: "Level") -> bool:
return (
self.uids.matches(other_level.uids)
and other_level.image_type == self.image_type
and other_level.tile_size == self.tile_size
and (
not settings.strict_tile_size_check
or other_level.tile_size == self.tile_size
)
)

def get_highest_level(self) -> int:
Expand Down
8 changes: 8 additions & 0 deletions wsidicom/web/wsidicom_web_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,12 @@ def create_instance(
key=lambda instance: instance.dataset.image_size.width,
)
)

except StopIteration:
WsiDicomNotFoundError(
"No level instances found", f"{study_uid}, {series_uids}"
)
self._base_tile_size = self._base_dataset.tile_size

@property
def base_dataset(self) -> WsiDataset:
Expand All @@ -174,6 +176,12 @@ def base_dataset(self) -> WsiDataset:
@property
def level_instances(self) -> List[WsiInstance]:
"""The level instances parsed from the source."""
if settings.strict_tile_size_check:
return [
level
for level in self._level_instances
if level.dataset.tile_size == self._base_tile_size
]
return self._level_instances

@property
Expand Down