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

Universal reader output base classes #132

Merged
merged 18 commits into from
Jun 13, 2023
Merged
Changes from 7 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
88 changes: 88 additions & 0 deletions iohub/dataset_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from abc import ABC, abstractmethod
from types import TracebackType
from typing import Generator, Optional, Type, Union

import numpy as np
from numpy.typing import ArrayLike


class BaseFOV(ABC):
# NOTE: not using the `data` method from ngff Position
ziw-liu marked this conversation as resolved.
Show resolved Hide resolved

@property
@abstractmethod
def channels(self) -> list[str]:
JoOkuma marked this conversation as resolved.
Show resolved Hide resolved
JoOkuma marked this conversation as resolved.
Show resolved Hide resolved
raise NotImplementedError

def channel_index(self, key: str) -> int:
"""Return index of given channel."""
return self.channels.index(key)

@property
@abstractmethod
def shape(self) -> tuple[int, int, int, int, int]:
# NOTE: suggestion, fix dimension to 5?
# We could me more restrictive than ome-zarr
ziw-liu marked this conversation as resolved.
Show resolved Hide resolved
raise NotImplementedError

@abstractmethod
def __getitem__(
self,
key: Union[int, slice, tuple[Union[int, slice], ...]],
) -> ArrayLike:
"""
Returned object must support the ``__array__`` interface,
so that ``np.asarray(...)`` will work.
"""
raise NotImplementedError

@property
@abstractmethod
def ndim(self) -> int:
raise NotImplementedError

@property
@abstractmethod
def dtype(self) -> np.dtype:
JoOkuma marked this conversation as resolved.
Show resolved Hide resolved
raise NotImplementedError

@property
JoOkuma marked this conversation as resolved.
Show resolved Hide resolved
def zyx_scale(self) -> tuple[float, float, float]:
"""Helper function for FOV spatial scale."""
raise NotImplementedError


class BaseFOVCollection(ABC):
JoOkuma marked this conversation as resolved.
Show resolved Hide resolved
@abstractmethod
def __enter__(self) -> "BaseFOVCollection":
"""Open the underlying file and return self."""
raise NotImplementedError

@abstractmethod
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> bool:
"""Close the files."""
raise NotImplementedError

@abstractmethod
def __contains__(self, position_key: str) -> bool:
"""Check if a position is present in the collection."""
raise NotImplementedError

@abstractmethod
def __len__(self) -> int:
raise NotImplementedError

@abstractmethod
def __getitem__(self, position_key: str) -> BaseFOV:
"""FOV key position to FOV object."""
raise NotImplementedError

@abstractmethod
def __iter__(self) -> Generator[tuple[str, BaseFOV], None, None]:
"""Iterates over pairs of keys and FOVs."""
raise NotImplementedError