Skip to content

Commit

Permalink
Merge pull request #10 from bioio-devs/feature/allow-setting-resoluti…
Browse files Browse the repository at this point in the history
…on-level

Feature/allow setting resolution level
  • Loading branch information
SeanLeRoy authored Dec 6, 2023
2 parents 175a4d0 + c2fbc48 commit e450672
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
14 changes: 14 additions & 0 deletions bioio_base/image_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ def current_scene_index(self) -> int:
def set_scene(self, scene_id: Union[str, int]) -> None:
pass

@property
@abstractmethod
def resolution_levels(self) -> Tuple[int, ...]:
pass

@property
@abstractmethod
def current_resolution_level(self) -> int:
pass

@abstractmethod
def set_resolution_level(self, resolution_level: int) -> None:
pass

@property
@abstractmethod
def xarray_dask_data(self) -> xr.DataArray:
Expand Down
50 changes: 50 additions & 0 deletions bioio_base/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Reader(ImageContainer, ABC):
_metadata: Optional[Any] = None
_scenes: Optional[Tuple[str, ...]] = None
_current_scene_index: int = 0
_current_resolution_level: int = 0
# Do not default because they aren't used by all readers
_fs: AbstractFileSystem
_path: str
Expand Down Expand Up @@ -194,6 +195,28 @@ def current_scene_index(self) -> int:
"""
return self._current_scene_index

@property
def resolution_levels(self) -> Tuple[int, ...]:
"""
Returns
-------
resolution_levels: Tuple[str, ...]
Return the available resolution levels for the current scene.
By default these are ordered from highest resolution to lowest
resolution.
"""
return (self._current_resolution_level,)

@property
def current_resolution_level(self) -> int:
"""
Returns
-------
resolution_level: int
The current resolution level.
"""
return self._current_resolution_level

def _reset_self(self) -> None:
# Reset the data stored in the Reader object
self._xarray_dask_data = None
Expand Down Expand Up @@ -261,6 +284,33 @@ def set_scene(self, scene_id: Union[str, int]) -> None:
f"or integer (for scene index). Provided: {scene_id} ({type(scene_id)}."
)

def set_resolution_level(self, resolution_level: int) -> None:
"""
Set the resolution level.
Parameters
----------
resolution_level: int
The resolution level to access the image at.
Raises
------
IndexError
The provided resolution level is not found in the
available resolution level list.
"""
# Validate resolution level
if resolution_level not in self.resolution_levels:
raise IndexError(
f"Resolution level: '{resolution_level}' "
"is not present in available image resolution levels: "
f"{self.resolution_levels}. Readers are not required by `bioio-base` "
"to support resolution levels and therefore may not have any "
"available besides the default of 0."
)

self._current_resolution_level = resolution_level

@abstractmethod
def _read_delayed(self) -> xr.DataArray:
"""
Expand Down
16 changes: 16 additions & 0 deletions bioio_base/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def run_image_container_checks(
Optional[float], Optional[float], Optional[float]
],
expected_metadata_type: Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]],
set_resolution_level: int = 0,
expected_current_resolution_level: int = 0,
expected_resolution_levels: Tuple[int, ...] = (0,),
) -> ImageContainer:
"""
A general suite of tests to run against readers.
Expand All @@ -82,6 +85,13 @@ def run_image_container_checks(
assert image_container.scenes == expected_scenes
assert image_container.current_scene == expected_current_scene

# Set resolution level
image_container.set_resolution_level(set_resolution_level)

# Check resolution level info
assert image_container.resolution_levels == expected_resolution_levels
assert image_container.current_resolution_level == expected_current_resolution_level

# Check basics
assert image_container.shape == expected_shape
assert image_container.dtype == expected_dtype
Expand Down Expand Up @@ -158,6 +168,9 @@ def run_image_file_checks(
Optional[float], Optional[float], Optional[float]
],
expected_metadata_type: Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]],
set_resolution_level: int = 0,
expected_current_resolution_level: int = 0,
expected_resolution_levels: Tuple[int, ...] = (0,),
) -> ImageContainer:
# Init container
image_container = ImageContainer(image, fs_kwargs=dict(anon=True))
Expand All @@ -169,8 +182,11 @@ def run_image_file_checks(
run_image_container_checks(
image_container=image_container,
set_scene=set_scene,
set_resolution_level=set_resolution_level,
expected_scenes=expected_scenes,
expected_current_scene=expected_current_scene,
expected_resolution_levels=expected_resolution_levels,
expected_current_resolution_level=expected_current_resolution_level,
expected_shape=expected_shape,
expected_dtype=expected_dtype,
expected_dims_order=expected_dims_order,
Expand Down

0 comments on commit e450672

Please sign in to comment.