bioimageio.core.axis
1from __future__ import annotations 2 3from dataclasses import dataclass 4from typing import Literal, Mapping, Optional, TypeVar, Union 5 6from typing_extensions import assert_never 7 8from bioimageio.spec.model import v0_5 9 10 11def _get_axis_type(a: Literal["b", "t", "i", "c", "x", "y", "z"]): 12 if a == "b": 13 return "batch" 14 elif a == "t": 15 return "time" 16 elif a == "i": 17 return "index" 18 elif a == "c": 19 return "channel" 20 elif a in ("x", "y", "z"): 21 return "space" 22 else: 23 return "index" # return most unspecific axis 24 25 26S = TypeVar("S", bound=str) 27 28 29AxisId = v0_5.AxisId 30 31T = TypeVar("T") 32PerAxis = Mapping[AxisId, T] 33 34BatchSize = int 35 36AxisLetter = Literal["b", "i", "t", "c", "z", "y", "x"] 37AxisLike = Union[AxisId, AxisLetter, v0_5.AnyAxis, "Axis"] 38 39 40@dataclass 41class Axis: 42 id: AxisId 43 type: Literal["batch", "channel", "index", "space", "time"] 44 45 @classmethod 46 def create(cls, axis: AxisLike) -> Axis: 47 if isinstance(axis, cls): 48 return axis 49 elif isinstance(axis, Axis): 50 return Axis(id=axis.id, type=axis.type) 51 elif isinstance(axis, str): 52 return Axis(id=AxisId(axis), type=_get_axis_type(axis)) 53 elif isinstance(axis, v0_5.AxisBase): 54 return Axis(id=AxisId(axis.id), type=axis.type) 55 else: 56 assert_never(axis) 57 58 59@dataclass 60class AxisInfo(Axis): 61 maybe_singleton: bool # TODO: replace 'maybe_singleton' with size min/max for better axis guessing 62 63 @classmethod 64 def create(cls, axis: AxisLike, maybe_singleton: Optional[bool] = None) -> AxisInfo: 65 if isinstance(axis, AxisInfo): 66 return axis 67 68 axis_base = super().create(axis) 69 if maybe_singleton is None: 70 if isinstance(axis, (Axis, str)): 71 maybe_singleton = True 72 else: 73 if axis.size is None: 74 maybe_singleton = True 75 elif isinstance(axis.size, int): 76 maybe_singleton = axis.size == 1 77 elif isinstance(axis.size, v0_5.SizeReference): 78 maybe_singleton = ( 79 True # TODO: check if singleton is ok for a `SizeReference` 80 ) 81 elif isinstance( 82 axis.size, (v0_5.ParameterizedSize, v0_5.DataDependentSize) 83 ): 84 try: 85 maybe_size_one = axis.size.validate_size( 86 1 87 ) # TODO: refactor validate_size() to have boolean func here 88 except ValueError: 89 maybe_singleton = False 90 else: 91 maybe_singleton = maybe_size_one == 1 92 else: 93 assert_never(axis.size) 94 95 return AxisInfo( 96 id=axis_base.id, type=axis_base.type, maybe_singleton=maybe_singleton 97 )
class
AxisId(bioimageio.spec._internal.types.LowerCaseIdentifier):
199class AxisId(LowerCaseIdentifier): 200 root_model: ClassVar[Type[RootModel[Any]]] = RootModel[ 201 Annotated[LowerCaseIdentifierAnno, MaxLen(16)] 202 ]
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
PerAxis =
typing.Mapping[AxisId, ~T]
BatchSize =
<class 'int'>
AxisLetter =
typing.Literal['b', 'i', 't', 'c', 'z', 'y', 'x']
AxisLike =
typing.Union[AxisId, typing.Literal['b', 'i', 't', 'c', 'z', 'y', 'x'], typing.Annotated[typing.Union[bioimageio.spec.model.v0_5.BatchAxis, bioimageio.spec.model.v0_5.ChannelAxis, bioimageio.spec.model.v0_5.IndexInputAxis, bioimageio.spec.model.v0_5.TimeInputAxis, bioimageio.spec.model.v0_5.SpaceInputAxis], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], typing.Annotated[typing.Union[bioimageio.spec.model.v0_5.BatchAxis, bioimageio.spec.model.v0_5.ChannelAxis, bioimageio.spec.model.v0_5.IndexOutputAxis, typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.model.v0_5.TimeOutputAxis, Tag(tag='wo_halo')], typing.Annotated[bioimageio.spec.model.v0_5.TimeOutputAxisWithHalo, Tag(tag='with_halo')]], Discriminator(discriminator=<function _get_halo_axis_discriminator_value>, custom_error_type=None, custom_error_message=None, custom_error_context=None)], typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.model.v0_5.SpaceOutputAxis, Tag(tag='wo_halo')], typing.Annotated[bioimageio.spec.model.v0_5.SpaceOutputAxisWithHalo, Tag(tag='with_halo')]], Discriminator(discriminator=<function _get_halo_axis_discriminator_value>, custom_error_type=None, custom_error_message=None, custom_error_context=None)]], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], ForwardRef('Axis')]
@dataclass
class
Axis:
41@dataclass 42class Axis: 43 id: AxisId 44 type: Literal["batch", "channel", "index", "space", "time"] 45 46 @classmethod 47 def create(cls, axis: AxisLike) -> Axis: 48 if isinstance(axis, cls): 49 return axis 50 elif isinstance(axis, Axis): 51 return Axis(id=axis.id, type=axis.type) 52 elif isinstance(axis, str): 53 return Axis(id=AxisId(axis), type=_get_axis_type(axis)) 54 elif isinstance(axis, v0_5.AxisBase): 55 return Axis(id=AxisId(axis.id), type=axis.type) 56 else: 57 assert_never(axis)
Axis( id: AxisId, type: Literal['batch', 'channel', 'index', 'space', 'time'])
id: AxisId
@classmethod
def
create( cls, axis: Union[AxisId, Literal['b', 'i', 't', 'c', 'z', 'y', 'x'], Annotated[Union[bioimageio.spec.model.v0_5.BatchAxis, bioimageio.spec.model.v0_5.ChannelAxis, bioimageio.spec.model.v0_5.IndexInputAxis, bioimageio.spec.model.v0_5.TimeInputAxis, bioimageio.spec.model.v0_5.SpaceInputAxis], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.model.v0_5.BatchAxis, bioimageio.spec.model.v0_5.ChannelAxis, bioimageio.spec.model.v0_5.IndexOutputAxis, Annotated[Union[Annotated[bioimageio.spec.model.v0_5.TimeOutputAxis, Tag(tag='wo_halo')], Annotated[bioimageio.spec.model.v0_5.TimeOutputAxisWithHalo, Tag(tag='with_halo')]], Discriminator(discriminator=<function _get_halo_axis_discriminator_value>, custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[Annotated[bioimageio.spec.model.v0_5.SpaceOutputAxis, Tag(tag='wo_halo')], Annotated[bioimageio.spec.model.v0_5.SpaceOutputAxisWithHalo, Tag(tag='with_halo')]], Discriminator(discriminator=<function _get_halo_axis_discriminator_value>, custom_error_type=None, custom_error_message=None, custom_error_context=None)]], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Axis]) -> Axis:
46 @classmethod 47 def create(cls, axis: AxisLike) -> Axis: 48 if isinstance(axis, cls): 49 return axis 50 elif isinstance(axis, Axis): 51 return Axis(id=axis.id, type=axis.type) 52 elif isinstance(axis, str): 53 return Axis(id=AxisId(axis), type=_get_axis_type(axis)) 54 elif isinstance(axis, v0_5.AxisBase): 55 return Axis(id=AxisId(axis.id), type=axis.type) 56 else: 57 assert_never(axis)
60@dataclass 61class AxisInfo(Axis): 62 maybe_singleton: bool # TODO: replace 'maybe_singleton' with size min/max for better axis guessing 63 64 @classmethod 65 def create(cls, axis: AxisLike, maybe_singleton: Optional[bool] = None) -> AxisInfo: 66 if isinstance(axis, AxisInfo): 67 return axis 68 69 axis_base = super().create(axis) 70 if maybe_singleton is None: 71 if isinstance(axis, (Axis, str)): 72 maybe_singleton = True 73 else: 74 if axis.size is None: 75 maybe_singleton = True 76 elif isinstance(axis.size, int): 77 maybe_singleton = axis.size == 1 78 elif isinstance(axis.size, v0_5.SizeReference): 79 maybe_singleton = ( 80 True # TODO: check if singleton is ok for a `SizeReference` 81 ) 82 elif isinstance( 83 axis.size, (v0_5.ParameterizedSize, v0_5.DataDependentSize) 84 ): 85 try: 86 maybe_size_one = axis.size.validate_size( 87 1 88 ) # TODO: refactor validate_size() to have boolean func here 89 except ValueError: 90 maybe_singleton = False 91 else: 92 maybe_singleton = maybe_size_one == 1 93 else: 94 assert_never(axis.size) 95 96 return AxisInfo( 97 id=axis_base.id, type=axis_base.type, maybe_singleton=maybe_singleton 98 )
AxisInfo( id: AxisId, type: Literal['batch', 'channel', 'index', 'space', 'time'], maybe_singleton: bool)
@classmethod
def
create( cls, axis: Union[AxisId, Literal['b', 'i', 't', 'c', 'z', 'y', 'x'], Annotated[Union[bioimageio.spec.model.v0_5.BatchAxis, bioimageio.spec.model.v0_5.ChannelAxis, bioimageio.spec.model.v0_5.IndexInputAxis, bioimageio.spec.model.v0_5.TimeInputAxis, bioimageio.spec.model.v0_5.SpaceInputAxis], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.model.v0_5.BatchAxis, bioimageio.spec.model.v0_5.ChannelAxis, bioimageio.spec.model.v0_5.IndexOutputAxis, Annotated[Union[Annotated[bioimageio.spec.model.v0_5.TimeOutputAxis, Tag(tag='wo_halo')], Annotated[bioimageio.spec.model.v0_5.TimeOutputAxisWithHalo, Tag(tag='with_halo')]], Discriminator(discriminator=<function _get_halo_axis_discriminator_value>, custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[Annotated[bioimageio.spec.model.v0_5.SpaceOutputAxis, Tag(tag='wo_halo')], Annotated[bioimageio.spec.model.v0_5.SpaceOutputAxisWithHalo, Tag(tag='with_halo')]], Discriminator(discriminator=<function _get_halo_axis_discriminator_value>, custom_error_type=None, custom_error_message=None, custom_error_context=None)]], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Axis], maybe_singleton: Optional[bool] = None) -> AxisInfo:
64 @classmethod 65 def create(cls, axis: AxisLike, maybe_singleton: Optional[bool] = None) -> AxisInfo: 66 if isinstance(axis, AxisInfo): 67 return axis 68 69 axis_base = super().create(axis) 70 if maybe_singleton is None: 71 if isinstance(axis, (Axis, str)): 72 maybe_singleton = True 73 else: 74 if axis.size is None: 75 maybe_singleton = True 76 elif isinstance(axis.size, int): 77 maybe_singleton = axis.size == 1 78 elif isinstance(axis.size, v0_5.SizeReference): 79 maybe_singleton = ( 80 True # TODO: check if singleton is ok for a `SizeReference` 81 ) 82 elif isinstance( 83 axis.size, (v0_5.ParameterizedSize, v0_5.DataDependentSize) 84 ): 85 try: 86 maybe_size_one = axis.size.validate_size( 87 1 88 ) # TODO: refactor validate_size() to have boolean func here 89 except ValueError: 90 maybe_singleton = False 91 else: 92 maybe_singleton = maybe_size_one == 1 93 else: 94 assert_never(axis.size) 95 96 return AxisInfo( 97 id=axis_base.id, type=axis_base.type, maybe_singleton=maybe_singleton 98 )