-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
842 additions
and
594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import Literal, Mapping, Optional, TypeVar, Union | ||
|
||
from typing_extensions import assert_never | ||
|
||
from bioimageio.spec.model import v0_5 | ||
|
||
|
||
def _get_axis_type(a: Literal["b", "t", "i", "c", "x", "y", "z"]): | ||
if a == "b": | ||
return "batch" | ||
elif a == "t": | ||
return "time" | ||
elif a == "i": | ||
return "index" | ||
elif a == "c": | ||
return "channel" | ||
elif a in ("x", "y", "z"): | ||
return "space" | ||
else: | ||
assert_never(a) | ||
|
||
|
||
S = TypeVar("S", bound=str) | ||
|
||
|
||
def _get_axis_id(a: Union[Literal["b", "t", "i", "c"], S]): | ||
if a == "b": | ||
return AxisId("batch") | ||
elif a == "t": | ||
return AxisId("time") | ||
elif a == "i": | ||
return AxisId("index") | ||
elif a == "c": | ||
return AxisId("channel") | ||
else: | ||
return AxisId(a) | ||
|
||
|
||
AxisId = v0_5.AxisId | ||
|
||
T = TypeVar("T") | ||
PerAxis = Mapping[AxisId, T] | ||
|
||
BatchSize = int | ||
|
||
AxisLetter = Literal["b", "i", "t", "c", "z", "y", "x"] | ||
AxisLike = Union[AxisLetter, v0_5.AnyAxis, "Axis"] | ||
|
||
|
||
@dataclass | ||
class Axis: | ||
id: AxisId | ||
type: Literal["batch", "channel", "index", "space", "time"] | ||
|
||
@classmethod | ||
def create(cls, axis: AxisLike) -> Axis: | ||
if isinstance(axis, cls): | ||
return axis | ||
elif isinstance(axis, Axis): | ||
return Axis(id=axis.id, type=axis.type) | ||
elif isinstance(axis, str): | ||
return Axis(id=_get_axis_id(axis), type=_get_axis_type(axis)) | ||
elif isinstance(axis, v0_5.AxisBase): | ||
return Axis(id=AxisId(axis.id), type=axis.type) | ||
else: | ||
assert_never(axis) | ||
|
||
|
||
@dataclass | ||
class AxisInfo(Axis): | ||
maybe_singleton: bool | ||
|
||
@classmethod | ||
def create(cls, axis: AxisLike, maybe_singleton: Optional[bool] = None) -> AxisInfo: | ||
if isinstance(axis, AxisInfo): | ||
return axis | ||
|
||
axis_base = super().create(axis) | ||
if maybe_singleton is None: | ||
if isinstance(axis, Axis): | ||
maybe_singleton = False | ||
elif isinstance(axis, str): | ||
maybe_singleton = axis == "b" | ||
else: | ||
if axis.size is None: | ||
maybe_singleton = True | ||
elif isinstance(axis.size, int): | ||
maybe_singleton = axis.size == 1 | ||
elif isinstance(axis.size, v0_5.SizeReference): | ||
maybe_singleton = ( | ||
False # TODO: check if singleton is ok for a `SizeReference` | ||
) | ||
elif isinstance( | ||
axis.size, (v0_5.ParameterizedSize, v0_5.DataDependentSize) | ||
): | ||
try: | ||
maybe_size_one = axis.size.validate_size( | ||
1 | ||
) # TODO: refactor validate_size() to have boolean func here | ||
except ValueError: | ||
maybe_singleton = False | ||
else: | ||
maybe_singleton = maybe_size_one == 1 | ||
else: | ||
assert_never(axis.size) | ||
|
||
return AxisInfo( | ||
id=axis_base.id, type=axis_base.type, maybe_singleton=maybe_singleton | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,64 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Dict, | ||
Literal, | ||
Mapping, | ||
NamedTuple, | ||
Protocol, | ||
Tuple, | ||
Union, | ||
) | ||
from typing import Literal, NamedTuple, Tuple, TypeVar, Union | ||
|
||
import xarray as xr | ||
from typing_extensions import Self, assert_never | ||
|
||
from bioimageio.spec.model import v0_5 | ||
DTypeStr = Literal[ | ||
"bool", | ||
"float32", | ||
"float64", | ||
"int8", | ||
"int16", | ||
"int32", | ||
"int64", | ||
"uint8", | ||
"uint16", | ||
"uint32", | ||
"uint64", | ||
] | ||
|
||
if TYPE_CHECKING: | ||
from bioimageio.core.stat_measures import Measure, MeasureValue | ||
|
||
TensorId = v0_5.TensorId | ||
AxisId = v0_5.AxisId | ||
LeftRight_T = TypeVar("LeftRight_T", bound="LeftRight") | ||
LeftRightLike = Union[int, Tuple[int, int], LeftRight_T] | ||
|
||
|
||
@dataclass | ||
class Axis: | ||
id: AxisId | ||
type: Literal["batch", "channel", "index", "space", "time"] | ||
class LeftRight(NamedTuple): | ||
left: int | ||
right: int | ||
|
||
@classmethod | ||
def create(cls, like: LeftRightLike[Self]) -> Self: | ||
if isinstance(like, cls): | ||
return like | ||
elif isinstance(like, tuple): | ||
return cls(*like) | ||
elif isinstance(like, int): | ||
return cls(like, like) | ||
else: | ||
assert_never(like) | ||
|
||
class AxisLike(Protocol): | ||
id: str | ||
type: Literal["batch", "channel", "index", "space", "time"] | ||
|
||
class Halo(LeftRight): | ||
pass | ||
|
||
BatchSize = int | ||
Tensor = xr.DataArray | ||
|
||
Data = Dict[TensorId, Tensor] | ||
Stat = Dict["Measure", "MeasureValue"] | ||
HaloLike = LeftRightLike[Halo] | ||
|
||
|
||
class LeftRight(NamedTuple): | ||
left: int | ||
right: int | ||
class PadWidth(LeftRight): | ||
pass | ||
|
||
|
||
PadWidthLike = LeftRightLike[PadWidth] | ||
PadMode = Literal["edge", "reflect", "symmetric"] | ||
PadWhere = Literal["before", "center", "after"] | ||
|
||
|
||
class SliceInfo(NamedTuple): | ||
start: int | ||
stop: int | ||
|
||
|
||
TileNumber = int | ||
TotalNumberOfTiles = int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from pathlib import Path | ||
from typing import Optional, Sequence, Union | ||
|
||
import imageio | ||
|
||
from bioimageio.core.axis import Axis, AxisLike | ||
from bioimageio.spec.model import v0_5 | ||
from bioimageio.spec.model.v0_4 import InputTensorDescr as InputTensorDescr04 | ||
from bioimageio.spec.model.v0_4 import OutputTensorDescr as OutputTensorDescr04 | ||
from bioimageio.spec.utils import load_array | ||
|
||
from .tensor import Tensor, TensorId | ||
|
||
|
||
def load_tensor( | ||
path: Path, axes: Optional[Sequence[AxisLike]] = None, id: Optional[TensorId] = None | ||
) -> Tensor: | ||
|
||
ext = path.suffix | ||
if ext == ".npy": | ||
array = load_array(path) | ||
else: | ||
is_volume = ( | ||
True | ||
if axes is None | ||
else sum(Axis.create(a).type != "channel" for a in axes) > 2 | ||
) | ||
array = imageio.volread(path) if is_volume else imageio.imread(path) | ||
|
||
return Tensor.from_numpy(array, axes, id=TensorId(path.stem) if id is None else id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.