From b1bfd608701827c7af7d8c28db4160e04a5ba274 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 28 Jul 2026 14:04:56 +0200 Subject: [PATCH 01/85] reuse spec.utils.load_image --- src/bioimageio/core/io.py | 44 ++++----------------------------------- 1 file changed, 4 insertions(+), 40 deletions(-) diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index 82cb512f..a21a5e1b 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -8,7 +8,6 @@ from shutil import copyfileobj from typing import ( TYPE_CHECKING, - Any, Dict, List, Mapping, @@ -19,25 +18,22 @@ ) import xarray as xr -from imageio.v3 import imread, imwrite # type: ignore +from imageio.v3 import imwrite # type: ignore from loguru import logger -from numpy.typing import NDArray from pydantic import BaseModel, RootModel from typing_extensions import TypeAlias from typing_extensions import TypeAliasType as _TypeAliasType -from bioimageio.spec._internal.io import get_reader, interprete_file_source -from bioimageio.spec._internal.type_guards import is_ndarray +from bioimageio.spec._internal.io import get_reader from bioimageio.spec.common import ( BytesReader, - FileDescr, FileSource, HttpUrl, PermissiveFileSource, - RelativeFilePath, ZipPath, ) -from bioimageio.spec.utils import download, load_array, save_array +from bioimageio.spec.utils import load_image as load_image +from bioimageio.spec.utils import save_array from .axis import AxisId, AxisLike from .common import PerMember @@ -60,38 +56,6 @@ ) -def load_image( - source: Union[PermissiveFileSource, ZipPath], is_volume: Optional[bool] = None -) -> NDArray[Any]: - """load a single image as numpy array - - Args: - source: image source - is_volume: deprecated - """ - if is_volume is not None: - warnings.warn("**is_volume** is deprecated and will be removed soon.") - - if isinstance(source, (FileDescr, ZipPath)): - parsed_source = source - else: - parsed_source = interprete_file_source(source) - - if isinstance(parsed_source, RelativeFilePath): - parsed_source = parsed_source.absolute() - - if parsed_source.suffix == ".npy": - image = load_array(parsed_source) - else: - reader = download(parsed_source) - image = imread( # pyright: ignore[reportUnknownVariableType] - reader.read(), extension=parsed_source.suffix - ) - - assert is_ndarray(image) - return image - - def load_tensor( source: Union[PermissiveFileSource, ZipPath], /, From e049644a5b1dcfc80d9f8b388ca4c24f63308dd7 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 28 Jul 2026 14:52:42 +0200 Subject: [PATCH 02/85] wip load zarr images --- changelog.md | 7 +++ src/bioimageio/core/io.py | 2 +- src/bioimageio/core/tensor.py | 99 +++++++++++++++++++++-------------- 3 files changed, 69 insertions(+), 39 deletions(-) diff --git a/changelog.md b/changelog.md index 385facc3..0a4c62f4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,10 @@ +### 0.12.0 (not yet released) + +- bump spec to 0.5.12.1 (wip) + - supports loading zarr images +- dropped `is_volume` flag from `load_image()` + + ### 0.11.0 - bump spec to 0.5.12.0 diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index a21a5e1b..b29b78a3 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -64,7 +64,7 @@ def load_tensor( # TODO: load axis meta data array = load_image(source) - return Tensor.from_numpy(array, dims=axes) + return Tensor.from_array(array, dims=axes) _SourceT = TypeVar("_SourceT", Path, HttpUrl, ZipPath) diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index 99b95752..6ad8df41 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -24,8 +24,9 @@ import xarray as xr from loguru import logger from numpy.typing import DTypeLike, NDArray -from typing_extensions import Self, assert_never +from typing_extensions import Self, TypeAlias, assert_never +from bioimageio.spec._internal.type_guards import is_dict from bioimageio.spec.model import v0_5 from ._magic_tensor_ops import MagicTensorOpsMixin @@ -42,10 +43,11 @@ ) if TYPE_CHECKING: - from numpy.typing import ArrayLike, NDArray + import dask.array as da + from numpy.typing import NDArray + from xarray.core.types import DaCompatible - -_ScalarOrArray = Union["ArrayLike", np.generic, "NDArray[Any]"] # TODO: add "DaskArray" +Array: TypeAlias = "NDArray[Any] | da.Array" def _resolve_pad_mode(mode: PadMode): @@ -71,11 +73,11 @@ class Tensor(MagicTensorOpsMixin): """A wrapper around an xr.DataArray for better integration with bioimageio.spec and improved type annotations.""" - _Compatible = Union["Tensor", xr.DataArray, _ScalarOrArray] + _Compatible: TypeAlias = Union["Tensor", xr.DataArray, DaCompatible] def __init__( self, - array: Union[NDArray[Any], xr.DataArray], + array: Union[Array, xr.DataArray], dims: Sequence[Union[AxisId, AxisLike]], ) -> None: super().__init__() @@ -83,14 +85,15 @@ def __init__( a if isinstance(a, AxisId) else AxisInfo.create(a).id for a in dims ) if isinstance(array, xr.DataArray): - self._data = array.transpose(*axes) + self._data: xr.DataArray = array.transpose(*axes) + assert isinstance(self._data, xr.DataArray) else: self._data = xr.DataArray(array, dims=axes) def __repr__(self) -> str: return f"" - def __array__(self, dtype: DTypeLike = None): + def __array__(self, dtype: Optional[DTypeLike] = None): return np.asarray(self._data, dtype=dtype) def __getitem__( @@ -151,11 +154,12 @@ def _binary_op( f: Callable[[Any, Any], Any], reflexive: bool = False, ) -> Self: - data = self._data._binary_op( # pyright: ignore[reportPrivateUsage] + data: xr.DataArray = self._data._binary_op( # pyright: ignore[reportPrivateUsage] (other._data if isinstance(other, Tensor) else other), f, reflexive, ) + assert isinstance(data, xr.DataArray) return self.__class__.from_xarray(data) def _inplace_binary_op( @@ -178,9 +182,10 @@ def _inplace_binary_op( return self def _unary_op(self, f: Callable[[Any], Any], *args: Any, **kwargs: Any) -> Self: - data = self._data._unary_op( # pyright: ignore[reportPrivateUsage] + data: xr.DataArray = self._data._unary_op( # pyright: ignore[reportPrivateUsage] f, *args, **kwargs ) + assert isinstance(data, xr.DataArray) return self.__class__.from_xarray(data) @classmethod @@ -198,6 +203,15 @@ def from_numpy( array: NDArray[Any], *, dims: Optional[Union[AxisLike, Sequence[AxisLike]]], + ) -> Tensor: + return cls.from_array(array, dims=dims) + + @classmethod + def from_array( + cls, + array: Array, + *, + dims: Optional[Union[AxisLike, Sequence[AxisLike]]], ) -> Tensor: """create a `Tensor` from a numpy array @@ -239,7 +253,7 @@ def dims(self): # TODO: rename to `axes`? @property def dtype(self) -> DTypeStr: - dt = str(self.data.dtype) # pyright: ignore[reportUnknownArgumentType] + dt = str(self.data.dtype) assert dt in get_args(DTypeStr) return dt # pyright: ignore[reportReturnType] @@ -278,11 +292,11 @@ def tagged_shape(self): def to_numpy(self) -> NDArray[Any]: """Return the data of this tensor as a numpy array.""" - return self.data.to_numpy() # pyright: ignore[reportUnknownVariableType] + return self.data.to_numpy() def argmax(self) -> Mapping[AxisId, int]: ret = self._data.argmax(...) - assert isinstance(ret, dict) + assert is_dict(ret) return {cast(AxisId, k): cast(int, v.item()) for k, v in ret.items()} def astype(self, dtype: DTypeStr, *, copy: bool = False): @@ -534,6 +548,7 @@ def unstack_batch_multi_index( old_dims = self.dims array = self._data.unstack(AxisId("batch")) + assert isinstance(array, xr.DataArray) added_dims = [AxisId(d) for d in array.dims if d not in self._data.dims] # restore expected axis order, replace batch dim with added dims @@ -550,6 +565,7 @@ def unstack_batch_multi_index( if AxisId("original_batch") in array.dims: array = array.rename({AxisId("original_batch"): AxisId("batch")}) + assert isinstance(array, xr.DataArray) return self.__class__.from_xarray(array) def transpose( @@ -605,7 +621,7 @@ def transpose( lets_unstack = False if lets_unstack: - array = array.unstack(AxisId("batch")) + array: xr.DataArray = array.unstack(AxisId("batch")) if AxisId("original_batch") in array.dims: if AxisId("batch") in axes: @@ -625,6 +641,7 @@ def transpose( raise ValueError(f"Missing dimensions {unhandled_missing_dims}.") + assert isinstance(array, xr.DataArray) unhandled_extra_dims = [a for a in array.dims if a not in axes] if unhandled_extra_dims and extra_dims == "raise": @@ -666,56 +683,62 @@ def var(self, dim: Optional[Union[AxisId, Sequence[AxisId]]] = None) -> Self: return self.__class__.from_xarray(self._data.var(dim=dim)) @classmethod - def _interprete_array_wo_known_axes(cls, array: NDArray[Any]): + def _interprete_array_wo_known_axes(cls, array: Array) -> Tensor: ndim = array.ndim + shape = [s if isinstance(s, int) else -1 for s in array.shape] + if ndim == 2: current_axes = ( - v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=array.shape[0]), - v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=array.shape[1]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[0]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[1]), ) - elif ndim == 3 and any(s <= 3 for s in array.shape): + elif ndim == 3 and any(s <= 3 for s in shape): current_axes = ( v0_5.ChannelAxis( channel_names=[ - v0_5.Identifier(f"channel{i}") for i in range(array.shape[0]) + v0_5.Identifier(f"channel{i}") for i in range(shape[0]) ] ), - v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=array.shape[1]), - v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=array.shape[2]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[1]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[2]), ) elif ndim == 3: current_axes = ( - v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=array.shape[0]), - v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=array.shape[1]), - v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=array.shape[2]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=shape[0]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[1]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[2]), ) elif ndim == 4: current_axes = ( v0_5.ChannelAxis( channel_names=[ - v0_5.Identifier(f"channel{i}") for i in range(array.shape[0]) + v0_5.Identifier(f"channel{i}") for i in range(shape[0]) ] ), - v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=array.shape[1]), - v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=array.shape[2]), - v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=array.shape[3]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=shape[1]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[2]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[3]), ) elif ndim == 5: current_axes = ( v0_5.BatchAxis(), v0_5.ChannelAxis( channel_names=[ - v0_5.Identifier(f"channel{i}") for i in range(array.shape[1]) + v0_5.Identifier(f"channel{i}") for i in range(shape[1]) ] ), - v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=array.shape[2]), - v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=array.shape[3]), - v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=array.shape[4]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=shape[2]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[3]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[4]), ) else: - raise ValueError(f"Could not guess an axis mapping for {array.shape}") + raise ValueError(f"Could not guess an axis mapping for {shape}") - return cls(array, dims=tuple(a.id for a in current_axes)) + dims = tuple(a.id for a in current_axes) + if isinstance(array, da.Array): + return cls.from_xarray(xr.DataArray(array, dims=dims)) + else: + return cls(array, dims=dims) def _add_singletons(arr: NDArray[Any], axis_infos: Sequence[AxisInfo]): @@ -739,13 +762,13 @@ def _add_singletons(arr: NDArray[Any], axis_infos: Sequence[AxisInfo]): def _get_array_view( - original_array: NDArray[Any], axis_infos: Sequence[AxisInfo] -) -> Optional[NDArray[Any]]: + original_array: Array, axis_infos: Sequence[AxisInfo] +) -> Optional[Array]: perms = list(permutations(range(len(original_array.shape)))) for perm in perms: - view = original_array.transpose(perm) - view = _add_singletons(view, axis_infos) + view: Array = original_array.transpose(perm) # pyright: ignore + view = _add_singletons(view, axis_infos) # pyright: ignore if len(view.shape) != len(axis_infos): return None From c2f9126d1b2493ff20efe0748b9b83f3bc6d55b2 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 28 Jul 2026 18:51:33 +0200 Subject: [PATCH 03/85] bump pyright --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 45d8fb39..c58dc82a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -72,7 +72,7 @@ dev = [ 'onnx_ir!=0.1.14;python_version<"3.10"', # uses typing.Concatentate which requires py>=3.10 "packaging>=17.0", "pre-commit", - "pyright==1.1.410", + "pyright==1.1.411", "pytest-cov", "pytest", "python-dotenv", From 919c40735681b3716162ae8ff6e4f360296fb803 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Thu, 30 Jul 2026 14:29:48 +0200 Subject: [PATCH 04/85] fix optional dask.array import --- src/bioimageio/core/tensor.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index 6ad8df41..5f70a7c5 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -42,12 +42,17 @@ SliceInfo, ) -if TYPE_CHECKING: +try: import dask.array as da +except ImportError: + da = None + +if TYPE_CHECKING: + import dask.array from numpy.typing import NDArray from xarray.core.types import DaCompatible -Array: TypeAlias = "NDArray[Any] | da.Array" +Array: TypeAlias = "NDArray[Any] | dask.array.Array" def _resolve_pad_mode(mode: PadMode): @@ -73,7 +78,7 @@ class Tensor(MagicTensorOpsMixin): """A wrapper around an xr.DataArray for better integration with bioimageio.spec and improved type annotations.""" - _Compatible: TypeAlias = Union["Tensor", xr.DataArray, DaCompatible] + _Compatible: TypeAlias = Union["Tensor", xr.DataArray, "DaCompatible"] def __init__( self, @@ -735,7 +740,7 @@ def _interprete_array_wo_known_axes(cls, array: Array) -> Tensor: raise ValueError(f"Could not guess an axis mapping for {shape}") dims = tuple(a.id for a in current_axes) - if isinstance(array, da.Array): + if da is not None and isinstance(array, da.Array): return cls.from_xarray(xr.DataArray(array, dims=dims)) else: return cls(array, dims=dims) From d099ea2e4dda46c0650862895c35efc4879417bd Mon Sep 17 00:00:00 2001 From: fynnbe Date: Fri, 31 Jul 2026 11:27:25 +0200 Subject: [PATCH 05/85] add dask[array] as dev dependency --- conda-recipe/meta.yaml | 2 +- pyproject.toml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/conda-recipe/meta.yaml b/conda-recipe/meta.yaml index 89d98362..1e1b211d 100644 --- a/conda-recipe/meta.yaml +++ b/conda-recipe/meta.yaml @@ -55,7 +55,7 @@ test: requires: {% for dep in pyproject['project']['optional-dependencies']['dev'] %} {% if 'torch' not in dep %} # can't install pytorch>=2.8 from conda-forge smh - - {{ dep.replace('[mcp]', '').replace(';python_version>="3.10"', '').replace(';python_version<"3.10"', '').lower().replace('_', '-') }} + - {{ dep.replace('[array]', '').replace('[mcp]', '').replace(';python_version>="3.10"', '').replace(';python_version<"3.10"', '').lower().replace('_', '-') }} {% endif %} {% endfor %} commands: diff --git a/pyproject.toml b/pyproject.toml index c58dc82a..431511e8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,6 +57,7 @@ gradio-client = ['gradio_client;python_version>="3.10"'] dev = [ "cellpose<4.2", # for model testing "crick", + "dask[array]", 'gradio[mcp];python_version>="3.10"', 'gradio_client;python_version>="3.10"', "httpx", From f1cd356a5e5d91c00a47dad785ac9dce9cfb261a Mon Sep 17 00:00:00 2001 From: fynnbe Date: Fri, 31 Jul 2026 13:06:43 +0200 Subject: [PATCH 06/85] CI pyright fixes --- src/bioimageio/core/tensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index 5f70a7c5..ffa5ee56 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -258,7 +258,7 @@ def dims(self): # TODO: rename to `axes`? @property def dtype(self) -> DTypeStr: - dt = str(self.data.dtype) + dt = str(self.data.dtype) # pyright: ignore[reportUnknownArgumentType] assert dt in get_args(DTypeStr) return dt # pyright: ignore[reportReturnType] @@ -297,7 +297,7 @@ def tagged_shape(self): def to_numpy(self) -> NDArray[Any]: """Return the data of this tensor as a numpy array.""" - return self.data.to_numpy() + return self.data.to_numpy() # pyright: ignore[reportUnknownVariableType] def argmax(self) -> Mapping[AxisId, int]: ret = self._data.argmax(...) From 6c42ef1c1c1a562ae1ad5478160e93d4aec4a3d3 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Mon, 3 Aug 2026 11:13:09 +0200 Subject: [PATCH 07/85] WIP support non-path sources in CLI --- src/bioimageio/core/cli.py | 35 ++++++++++++++++++++---------- src/bioimageio/core/digest_spec.py | 4 ++-- src/bioimageio/core/tensor.py | 3 ++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/bioimageio/core/cli.py b/src/bioimageio/core/cli.py index 0a8dd8a0..3e02b02d 100644 --- a/src/bioimageio/core/cli.py +++ b/src/bioimageio/core/cli.py @@ -73,6 +73,7 @@ from bioimageio.spec._internal.io import is_yaml_value from bioimageio.spec._internal.io_utils import open_bioimageio_yaml from bioimageio.spec._internal.types import FormatVersionPlaceholder, NotEmpty +from bioimageio.spec.common import PermissiveFileSource from bioimageio.spec.dataset import DatasetDescr from bioimageio.spec.model import ModelDescr, v0_4, v0_5 from bioimageio.spec.notebook import NotebookDescr @@ -431,6 +432,10 @@ def updated(self): class PredictCmd(CmdBase, WithSource): """Run inference on your data with a bioimage.io model.""" + @cached_property + def descr(self): + return load_description(self.source, perform_io_checks=False) + inputs: NotEmpty[List[Union[str, NotEmpty[List[str]]]]] = Field( default_factory=lambda: ["{input_id}/001.tif"], validation_alias=AliasChoices("inputs", "input"), @@ -708,7 +713,11 @@ def expand_inputs(i: int, ipt: Union[str, Sequence[str]]) -> Tuple[str, ...]: inputs = [expand_inputs(i, ipt) for i, ipt in enumerate(self.inputs, start=1)] sample_paths_in = [ - {t: Path(p) for t, p in zip(input_ids, ipts)} for ipts in inputs + { + t: Path(p) if isinstance(p, str) and Path(p).exists() else p + for t, p in zip(input_ids, ipts) + } + for ipts in inputs ] sample_ids = _get_sample_ids(sample_paths_in) @@ -759,7 +768,11 @@ def expand_outputs(): outputs = expand_outputs() sample_paths_out = [ - {MemberId(t): Path(p) for t, p in zip(output_ids, out)} for out in outputs + { + MemberId(t): Path(p) if isinstance(p, str) and Path(p).exists() else p + for t, p in zip(output_ids, out) + } + for out in outputs ] if not self.overwrite: @@ -773,19 +786,16 @@ def expand_outputs(): print("🛈 bioimageio prediction preview structure:") pprint( { - "{sample_id}": dict( - inputs={"{input_id}": ""}, - outputs={"{output_id}": ""}, - ) + "{sample_id}": { + "inputs": {"{input_id}": ""}, + "outputs": {"{output_id}": ""}, + } } ) print("🔎 bioimageio prediction preview output:") pprint( { - s: dict( - inputs={t: p.as_posix() for t, p in sp_in.items()}, - outputs={t: p.as_posix() for t, p in sp_out.items()}, - ) + s: {"inputs": sp_in, "outputs": sp_out} for s, sp_in, sp_out in zip( sample_ids, sample_paths_in, sample_paths_out ) @@ -1089,7 +1099,7 @@ def cli_cmd(self) -> None: def _get_sample_ids( - input_paths: Sequence[Mapping[MemberId, Path]], + input_paths: Sequence[Mapping[MemberId, PermissiveFileSource]], ) -> Sequence[SampleId]: """Get sample ids for given input paths, based on the common path per sample. @@ -1146,7 +1156,8 @@ def get_shorter_diff(seqs: Sequence[Sequence[str]]) -> List[Sequence[str]]: full_tensor_ids = [ sorted( - p.resolve().with_suffix("").as_posix() for p in input_sample_paths.values() + p.resolve().with_suffix("").as_posix() if isinstance(p, Path) else str(p) + for p in input_sample_paths.values() ) for input_sample_paths in input_paths ] diff --git a/src/bioimageio/core/digest_spec.py b/src/bioimageio/core/digest_spec.py index d691e65f..963911d8 100644 --- a/src/bioimageio/core/digest_spec.py +++ b/src/bioimageio/core/digest_spec.py @@ -550,7 +550,7 @@ def create_sample_for_model( def load_sample_for_model( *, model: AnyModelDescr, - paths: PerMember[Path], + paths: PerMember[PermissiveFileSource], axes: Optional[PerMember[Sequence[AxisLike]]] = None, stat: Optional[Stat] = None, sample_id: Optional[SampleId] = None, @@ -587,7 +587,7 @@ def load_sample_for_model( return Sample( members=members, stat={} if stat is None else stat, - id=sample_id or tuple(sorted(paths.values())), + id=sample_id or tuple(sorted(str(p) for p in paths.values())), ) diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index ffa5ee56..49cfd5d8 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -241,8 +241,9 @@ def from_array( successful_view = _get_array_view(array, axis_infos) if successful_view is None: + dims_formatted = "".join(f"\n{d}" for d in dim_seq) raise ValueError( - f"Array shape {original_shape} does not map to axes {dims}" + f"Array shape {original_shape} does not map to axes: {dims_formatted}" ) return Tensor(successful_view, dims=tuple(a.id for a in axis_infos)) From 91c0ffe5e8f5b4f8abca611c5f97c8ded21809a4 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Mon, 3 Aug 2026 11:13:32 +0200 Subject: [PATCH 08/85] ruff check --fix --- example/dataset_statistics_demo.ipynb | 2 -- example/model_usage.ipynb | 15 +++++++-------- scripts/generate_api_doc_pages.py | 3 +-- src/bioimageio/core/__init__.py | 1 - src/bioimageio/core/__main__.py | 2 +- src/bioimageio/core/_collection.py | 3 ++- src/bioimageio/core/_common_annotations.py | 3 +-- src/bioimageio/core/_model_adapter.py | 3 +-- src/bioimageio/core/_op_base.py | 3 ++- src/bioimageio/core/_ops_cellpose.py | 5 +++-- src/bioimageio/core/_ops_stardist.py | 5 +++-- src/bioimageio/core/_prediction_pipeline.py | 6 ++---- src/bioimageio/core/_resource_tests.py | 3 +-- src/bioimageio/core/_sample_serializer.py | 2 +- src/bioimageio/core/_settings.py | 3 +-- src/bioimageio/core/axis.py | 2 +- src/bioimageio/core/backends/__init__.py | 2 +- src/bioimageio/core/backends/keras_backend.py | 3 ++- src/bioimageio/core/backends/onnx_backend.py | 3 ++- src/bioimageio/core/backends/pytorch_backend.py | 3 ++- .../core/backends/tensorflow_backend.py | 3 ++- .../core/backends/torchscript_backend.py | 3 ++- src/bioimageio/core/block.py | 3 +-- src/bioimageio/core/block_meta.py | 4 +--- src/bioimageio/core/cli.py | 8 +++----- src/bioimageio/core/commands.py | 3 ++- src/bioimageio/core/common.py | 3 +-- src/bioimageio/core/dataset.py | 2 +- src/bioimageio/core/digest_spec.py | 4 +--- src/bioimageio/core/io.py | 4 +--- src/bioimageio/core/prediction.py | 5 +---- src/bioimageio/core/proc_ops.py | 4 +--- src/bioimageio/core/proc_setup.py | 4 +--- .../core/remote_backends/gradio/client.py | 3 ++- .../core/remote_backends/gradio/serializer.py | 3 ++- .../core/remote_backends/gradio/server.py | 2 +- src/bioimageio/core/sample.py | 14 ++++++-------- src/bioimageio/core/stat_calculators.py | 8 ++------ src/bioimageio/core/stat_measures.py | 5 +++-- src/bioimageio/core/tensor.py | 12 +++++------- src/bioimageio/core/utils/_compare.py | 2 +- .../core/weight_converters/_utils_torch_onnx.py | 6 +++--- .../core/weight_converters/pytorch_to_onnx.py | 3 ++- .../weight_converters/pytorch_to_torchscript.py | 3 ++- .../core/weight_converters/torchscript_to_onnx.py | 3 ++- tests/test_bioimageio_collection.py | 10 +++++----- tests/test_bioimageio_spec_version.py | 3 ++- tests/test_cli.py | 3 ++- tests/test_prediction.py | 3 ++- tests/test_proc_ops.py | 3 ++- tests/test_tensor.py | 2 +- tests/utils.py | 3 ++- 52 files changed, 99 insertions(+), 114 deletions(-) diff --git a/example/dataset_statistics_demo.ipynb b/example/dataset_statistics_demo.ipynb index 7e9aff6b..6cba4205 100644 --- a/example/dataset_statistics_demo.ipynb +++ b/example/dataset_statistics_demo.ipynb @@ -26,13 +26,11 @@ "outputs": [], "source": [ "import warnings\n", - "\n", "from pprint import pprint\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import xarray as xr\n", - "\n", "from matplotlib.patches import Rectangle\n", "\n", "warnings.filterwarnings(\"ignore\")\n", diff --git a/example/model_usage.ipynb b/example/model_usage.ipynb index 2e3db5d1..34e3560e 100644 --- a/example/model_usage.ipynb +++ b/example/model_usage.ipynb @@ -87,16 +87,17 @@ } ], "source": [ - "from typing import Mapping, Union, Any\n", - "from bioimageio.core import Tensor\n", - "from numpy.typing import NDArray\n", + "from collections.abc import Mapping\n", "from pprint import pprint\n", + "from typing import Any, Union\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", + "from numpy.typing import NDArray\n", "\n", + "from bioimageio.core import Tensor\n", + "from bioimageio.spec.model.v0_5 import ModelDescr, TensorId\n", "\n", - "from bioimageio.spec.model.v0_5 import TensorId, ModelDescr\n", "\n", "# Function to display input and prediction output images\n", "def show_images(\n", @@ -187,8 +188,7 @@ "metadata": {}, "outputs": [], "source": [ - "from bioimageio.core import load_model_description\n", - "from bioimageio.core import test_model\n", + "from bioimageio.core import load_model_description, test_model\n", "\n", "model = load_model_description(model_source, format_version=\"latest\")\n", "test_summary = test_model(model) # Test model to check environment compatibility\n", @@ -277,8 +277,7 @@ } ], "source": [ - "from bioimageio.core import predict\n", - "from bioimageio.core import Sample\n", + "from bioimageio.core import Sample, predict\n", "\n", "prediction: Sample = predict(model=model, inputs=input_image)\n", "\n", diff --git a/scripts/generate_api_doc_pages.py b/scripts/generate_api_doc_pages.py index becf0f37..44369132 100644 --- a/scripts/generate_api_doc_pages.py +++ b/scripts/generate_api_doc_pages.py @@ -70,8 +70,7 @@ with mkdocs_gen_files.open(full_doc_path, "w") as fd: # Reconstruct the full identifier from the original module_path ident = ".".join(module_path.parts) - if ident.endswith(".__init__"): - ident = ident[:-9] # Remove .__init__ + ident = ident.removesuffix(".__init__") # Remove .__init__ fd.write(f"::: {ident}") print(f"Written {full_doc_path}") diff --git a/src/bioimageio/core/__init__.py b/src/bioimageio/core/__init__.py index 85b84a23..d9a1d8b1 100644 --- a/src/bioimageio/core/__init__.py +++ b/src/bioimageio/core/__init__.py @@ -14,7 +14,6 @@ - Creating and manipulating `Sample`s consisting of tensors with associated statistics. - Computing statistics on datasets (represented as sequences of samples), see `compute_dataset_measures`. """ -# ruff: noqa: E402 __version__ = "0.11.0" from loguru import logger diff --git a/src/bioimageio/core/__main__.py b/src/bioimageio/core/__main__.py index edcdea06..123b6a9c 100644 --- a/src/bioimageio/core/__main__.py +++ b/src/bioimageio/core/__main__.py @@ -14,7 +14,7 @@ + "{module} - {message}", ) -from .cli import Bioimageio # noqa: E402 +from .cli import Bioimageio def main(): diff --git a/src/bioimageio/core/_collection.py b/src/bioimageio/core/_collection.py index 5751e01b..b06de7f1 100644 --- a/src/bioimageio/core/_collection.py +++ b/src/bioimageio/core/_collection.py @@ -2,8 +2,9 @@ import json import warnings +from collections.abc import Mapping, Sequence from functools import cache -from typing import Literal, Mapping, Sequence, TypedDict +from typing import Literal, TypedDict from bioimageio.spec.common import Sha256 from bioimageio.spec.utils import get_reader diff --git a/src/bioimageio/core/_common_annotations.py b/src/bioimageio/core/_common_annotations.py index bb255e35..ba4756be 100644 --- a/src/bioimageio/core/_common_annotations.py +++ b/src/bioimageio/core/_common_annotations.py @@ -1,11 +1,10 @@ from __future__ import annotations +from collections.abc import Hashable, Mapping from types import MappingProxyType from typing import ( Annotated, Any, - Hashable, - Mapping, TypeVar, ) diff --git a/src/bioimageio/core/_model_adapter.py b/src/bioimageio/core/_model_adapter.py index 72f7a59d..a6f13aeb 100644 --- a/src/bioimageio/core/_model_adapter.py +++ b/src/bioimageio/core/_model_adapter.py @@ -1,15 +1,14 @@ import gc import warnings from abc import ABC, abstractmethod +from collections.abc import Iterable, Sequence from queue import LifoQueue from typing import ( Any, Dict, Generic, - Iterable, List, Optional, - Sequence, Tuple, Union, ) diff --git a/src/bioimageio/core/_op_base.py b/src/bioimageio/core/_op_base.py index 9aa3a558..002d0580 100644 --- a/src/bioimageio/core/_op_base.py +++ b/src/bioimageio/core/_op_base.py @@ -1,6 +1,7 @@ from abc import ABC, abstractmethod +from collections.abc import Collection from dataclasses import dataclass -from typing import Collection, Generic, Union +from typing import Generic, Union from typing_extensions import TypeVar, assert_never diff --git a/src/bioimageio/core/_ops_cellpose.py b/src/bioimageio/core/_ops_cellpose.py index 424ac76d..b25a0773 100644 --- a/src/bioimageio/core/_ops_cellpose.py +++ b/src/bioimageio/core/_ops_cellpose.py @@ -1,9 +1,10 @@ +from collections.abc import Collection from dataclasses import dataclass -from typing import Any, Collection +from typing import Any, cast import numpy as np from numpy.typing import NDArray -from typing_extensions import Literal, cast +from typing_extensions import Literal from bioimageio.spec.model.v0_5 import CellposeFlowDynamicsDescr diff --git a/src/bioimageio/core/_ops_stardist.py b/src/bioimageio/core/_ops_stardist.py index 27b863bd..f70df70d 100644 --- a/src/bioimageio/core/_ops_stardist.py +++ b/src/bioimageio/core/_ops_stardist.py @@ -1,18 +1,19 @@ from abc import ABC, abstractmethod +from collections.abc import Collection from dataclasses import dataclass from typing import ( Any, - Collection, Generic, List, Optional, Tuple, Union, + cast, ) import numpy as np from numpy.typing import NDArray -from typing_extensions import Self, TypeVar, cast +from typing_extensions import Self, TypeVar from bioimageio.spec.model import v0_5 diff --git a/src/bioimageio/core/_prediction_pipeline.py b/src/bioimageio/core/_prediction_pipeline.py index 87e0fd1a..cec741e2 100644 --- a/src/bioimageio/core/_prediction_pipeline.py +++ b/src/bioimageio/core/_prediction_pipeline.py @@ -1,16 +1,14 @@ import warnings from abc import ABC, abstractmethod +from collections.abc import Iterable, Mapping, Sequence from itertools import chain from types import MappingProxyType from typing import ( Any, - Iterable, List, Literal, - Mapping, NamedTuple, Optional, - Sequence, Set, Tuple, TypeVar, @@ -135,7 +133,7 @@ def _get_preceding_prediction_pipelines_for_sample( required_inputs = set(self.input_ids) sample_members = set(sample.members.keys()) - preceding_pipelines: List["PredictionPipeline | RemotePredictionPipeline"] = [] + preceding_pipelines: List[PredictionPipeline | RemotePredictionPipeline] = [] for pp in self._preceding_prediction_pipelines[::-1]: preceding_pipelines.insert(0, pp) sample_members.update(pp.output_ids) diff --git a/src/bioimageio/core/_resource_tests.py b/src/bioimageio/core/_resource_tests.py index 51ae745d..9c728e09 100644 --- a/src/bioimageio/core/_resource_tests.py +++ b/src/bioimageio/core/_resource_tests.py @@ -4,6 +4,7 @@ import subprocess import sys import warnings +from collections.abc import Hashable, Sequence from contextlib import nullcontext from copy import deepcopy from io import StringIO @@ -14,11 +15,9 @@ Any, Callable, Dict, - Hashable, List, Literal, Optional, - Sequence, Set, Tuple, Union, diff --git a/src/bioimageio/core/_sample_serializer.py b/src/bioimageio/core/_sample_serializer.py index 2d3f461c..a25f80ca 100644 --- a/src/bioimageio/core/_sample_serializer.py +++ b/src/bioimageio/core/_sample_serializer.py @@ -1,7 +1,7 @@ from abc import ABC, abstractmethod +from collections.abc import Iterable from typing import ( Generic, - Iterable, Tuple, TypeVar, Union, diff --git a/src/bioimageio/core/_settings.py b/src/bioimageio/core/_settings.py index 16b46068..589e6d3a 100644 --- a/src/bioimageio/core/_settings.py +++ b/src/bioimageio/core/_settings.py @@ -1,10 +1,9 @@ import os import platform -from typing import Literal, Optional +from typing import Annotated, Literal, Optional from loguru import logger from pydantic import Field, field_validator -from typing_extensions import Annotated from bioimageio.spec._internal._settings import Settings as SpecSettings diff --git a/src/bioimageio/core/axis.py b/src/bioimageio/core/axis.py index 6c85b8b3..071c69b2 100644 --- a/src/bioimageio/core/axis.py +++ b/src/bioimageio/core/axis.py @@ -1,9 +1,9 @@ from __future__ import annotations +from collections.abc import Mapping from dataclasses import dataclass from typing import ( Literal, - Mapping, Optional, TypeVar, Union, diff --git a/src/bioimageio/core/backends/__init__.py b/src/bioimageio/core/backends/__init__.py index 7fc53d58..a687f07a 100644 --- a/src/bioimageio/core/backends/__init__.py +++ b/src/bioimageio/core/backends/__init__.py @@ -1,7 +1,7 @@ +from collections.abc import Sequence from typing import ( List, Optional, - Sequence, Tuple, Union, ) diff --git a/src/bioimageio/core/backends/keras_backend.py b/src/bioimageio/core/backends/keras_backend.py index fbdfe498..1d02e0d7 100644 --- a/src/bioimageio/core/backends/keras_backend.py +++ b/src/bioimageio/core/backends/keras_backend.py @@ -1,8 +1,9 @@ import os import shutil +from collections.abc import Sequence from pathlib import Path from tempfile import TemporaryDirectory -from typing import Any, Optional, Sequence, Tuple +from typing import Any, Optional, Tuple from keras.src.legacy.saving import ( # pyright: ignore[reportMissingTypeStubs] legacy_h5_format, diff --git a/src/bioimageio/core/backends/onnx_backend.py b/src/bioimageio/core/backends/onnx_backend.py index 0dab80c1..7d9e269d 100644 --- a/src/bioimageio/core/backends/onnx_backend.py +++ b/src/bioimageio/core/backends/onnx_backend.py @@ -1,9 +1,10 @@ # pyright: reportUnknownVariableType=false import shutil import tempfile +from collections.abc import Sequence from contextlib import contextmanager, nullcontext from pathlib import Path -from typing import Any, List, Optional, Sequence, Union, cast +from typing import Any, List, Optional, Union, cast import onnxruntime as rt # pyright: ignore[reportMissingTypeStubs] from loguru import logger diff --git a/src/bioimageio/core/backends/pytorch_backend.py b/src/bioimageio/core/backends/pytorch_backend.py index 2d463452..97df6a43 100644 --- a/src/bioimageio/core/backends/pytorch_backend.py +++ b/src/bioimageio/core/backends/pytorch_backend.py @@ -1,9 +1,10 @@ import gc from abc import abstractmethod +from collections.abc import Mapping, Sequence from contextlib import nullcontext from io import BytesIO, TextIOWrapper from pathlib import Path -from typing import Any, List, Literal, Mapping, Optional, Sequence, Tuple, Union +from typing import Any, List, Literal, Optional, Tuple, Union import torch from loguru import logger diff --git a/src/bioimageio/core/backends/tensorflow_backend.py b/src/bioimageio/core/backends/tensorflow_backend.py index 263dc878..da4e677c 100644 --- a/src/bioimageio/core/backends/tensorflow_backend.py +++ b/src/bioimageio/core/backends/tensorflow_backend.py @@ -1,5 +1,6 @@ +from collections.abc import Sequence from pathlib import Path -from typing import Any, List, Optional, Sequence, Tuple, Union +from typing import Any, List, Optional, Tuple, Union import numpy as np import tensorflow as tf diff --git a/src/bioimageio/core/backends/torchscript_backend.py b/src/bioimageio/core/backends/torchscript_backend.py index f1f81ed3..67eec433 100644 --- a/src/bioimageio/core/backends/torchscript_backend.py +++ b/src/bioimageio/core/backends/torchscript_backend.py @@ -1,6 +1,7 @@ # pyright: reportUnknownVariableType=false import gc -from typing import Any, List, Optional, Sequence, Union +from collections.abc import Sequence +from typing import Any, List, Optional, Union import torch from loguru import logger diff --git a/src/bioimageio/core/block.py b/src/bioimageio/core/block.py index 69af9118..fb573560 100644 --- a/src/bioimageio/core/block.py +++ b/src/bioimageio/core/block.py @@ -1,8 +1,7 @@ +from collections.abc import Generator, Iterable from dataclasses import dataclass from typing import ( Any, - Generator, - Iterable, Optional, Tuple, Union, diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py index d4ab3c87..8e512803 100644 --- a/src/bioimageio/core/block_meta.py +++ b/src/bioimageio/core/block_meta.py @@ -1,4 +1,5 @@ import itertools +from collections.abc import Collection, Generator, Iterable from dataclasses import dataclass from functools import cached_property from math import floor, prod @@ -6,10 +7,7 @@ from typing import ( Any, Callable, - Collection, Dict, - Generator, - Iterable, List, Optional, Tuple, diff --git a/src/bioimageio/core/cli.py b/src/bioimageio/core/cli.py index 3e02b02d..7d08dbd6 100644 --- a/src/bioimageio/core/cli.py +++ b/src/bioimageio/core/cli.py @@ -10,6 +10,7 @@ import sys from abc import ABC from argparse import RawTextHelpFormatter +from collections.abc import Iterable, Mapping, Sequence from difflib import SequenceMatcher from functools import cached_property, partial from io import StringIO @@ -19,12 +20,9 @@ Annotated, Any, Dict, - Iterable, List, Literal, - Mapping, Optional, - Sequence, Set, Tuple, Type, @@ -150,7 +148,7 @@ def descr_id(self) -> str: (replacing legacy ids with their nicknames) """ if isinstance(self.descr, InvalidDescr): - return str(getattr(self.descr, "id", getattr(self.descr, "name"))) + return str(getattr(self.descr, "id", self.descr.name)) nickname = None if ( @@ -638,7 +636,7 @@ def get_example_command(preview: bool, escape: bool = False): "🎉 Sucessfully ran example prediction!\n" + "To predict the example input using the CLI example config file" + f" {example_path / YAML_FILE}, execute `bioimageio predict` from {example_path}:\n" - + f"$ cd {str(example_path)}\n" + + f"$ cd {example_path!s}\n" + f'$ bioimageio predict "{source_escaped}"\n\n' + "Alternatively run the following command" + " in the current workind directory, not the example folder:\n$ " diff --git a/src/bioimageio/core/commands.py b/src/bioimageio/core/commands.py index 025f177b..ea2fade1 100644 --- a/src/bioimageio/core/commands.py +++ b/src/bioimageio/core/commands.py @@ -1,8 +1,9 @@ """These functions are used in the bioimageio command line interface defined in `bioimageio.core.cli`.""" +from collections.abc import Sequence from pathlib import Path -from typing import Optional, Sequence, Union +from typing import Optional, Union from typing_extensions import Literal diff --git a/src/bioimageio/core/common.py b/src/bioimageio/core/common.py index 2c4af41e..eb45f98d 100644 --- a/src/bioimageio/core/common.py +++ b/src/bioimageio/core/common.py @@ -1,9 +1,8 @@ from __future__ import annotations +from collections.abc import Hashable, Mapping from typing import ( - Hashable, Literal, - Mapping, NamedTuple, Tuple, TypeVar, diff --git a/src/bioimageio/core/dataset.py b/src/bioimageio/core/dataset.py index e7740504..3077967f 100644 --- a/src/bioimageio/core/dataset.py +++ b/src/bioimageio/core/dataset.py @@ -1,4 +1,4 @@ -from typing import Iterable +from collections.abc import Iterable from .sample import Sample diff --git a/src/bioimageio/core/digest_spec.py b/src/bioimageio/core/digest_spec.py index 963911d8..7101bd77 100644 --- a/src/bioimageio/core/digest_spec.py +++ b/src/bioimageio/core/digest_spec.py @@ -3,6 +3,7 @@ import collections.abc import importlib.util import sys +from collections.abc import Iterable, Mapping, Sequence from itertools import chain from pathlib import Path from tempfile import TemporaryDirectory @@ -10,13 +11,10 @@ Any, Callable, Dict, - Iterable, List, Literal, - Mapping, NamedTuple, Optional, - Sequence, Tuple, Union, ) diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index b29b78a3..f89da242 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -2,6 +2,7 @@ import json import warnings import zipfile +from collections.abc import Mapping, Sequence from contextlib import nullcontext from io import BytesIO from pathlib import Path @@ -10,9 +11,7 @@ TYPE_CHECKING, Dict, List, - Mapping, Optional, - Sequence, TypeVar, Union, ) @@ -169,7 +168,6 @@ class _StatEntry(BaseModel, frozen=True, arbitrary_types_allowed=True): class _StatList(RootModel[List[_StatEntry]]): """Serializable stat mapping""" - pass def serialize_stat( diff --git a/src/bioimageio/core/prediction.py b/src/bioimageio/core/prediction.py index d00585be..f3098d46 100644 --- a/src/bioimageio/core/prediction.py +++ b/src/bioimageio/core/prediction.py @@ -1,10 +1,7 @@ import collections.abc +from collections.abc import Hashable, Iterable, Iterator, Mapping from pathlib import Path from typing import ( - Hashable, - Iterable, - Iterator, - Mapping, Optional, Tuple, Union, diff --git a/src/bioimageio/core/proc_ops.py b/src/bioimageio/core/proc_ops.py index cce52970..b7c29825 100644 --- a/src/bioimageio/core/proc_ops.py +++ b/src/bioimageio/core/proc_ops.py @@ -1,14 +1,12 @@ import collections.abc +from collections.abc import Collection, Mapping, Sequence from dataclasses import InitVar, dataclass, field from functools import partial from typing import ( Any, Callable, - Collection, Literal, - Mapping, Optional, - Sequence, Set, Tuple, Union, diff --git a/src/bioimageio/core/proc_setup.py b/src/bioimageio/core/proc_setup.py index 67a37731..4a13ab15 100644 --- a/src/bioimageio/core/proc_setup.py +++ b/src/bioimageio/core/proc_setup.py @@ -1,12 +1,10 @@ +from collections.abc import Iterable, Mapping, Sequence from itertools import chain from typing import ( Callable, - Iterable, List, - Mapping, NamedTuple, Optional, - Sequence, Set, Union, ) diff --git a/src/bioimageio/core/remote_backends/gradio/client.py b/src/bioimageio/core/remote_backends/gradio/client.py index 9637c6da..1c4d8f90 100644 --- a/src/bioimageio/core/remote_backends/gradio/client.py +++ b/src/bioimageio/core/remote_backends/gradio/client.py @@ -1,5 +1,6 @@ +from collections.abc import Iterable, Mapping from types import MappingProxyType -from typing import Dict, Iterable, Literal, Mapping, Optional, Tuple, Union +from typing import Dict, Literal, Optional, Tuple, Union from gradio_client import Client from loguru import logger diff --git a/src/bioimageio/core/remote_backends/gradio/serializer.py b/src/bioimageio/core/remote_backends/gradio/serializer.py index 3800b3a6..3f20acd4 100644 --- a/src/bioimageio/core/remote_backends/gradio/serializer.py +++ b/src/bioimageio/core/remote_backends/gradio/serializer.py @@ -1,6 +1,7 @@ import tempfile +from collections.abc import Mapping from pathlib import Path -from typing import Dict, List, Mapping, Union +from typing import Dict, List, Union import numpy as np from gradio_client import handle_file diff --git a/src/bioimageio/core/remote_backends/gradio/server.py b/src/bioimageio/core/remote_backends/gradio/server.py index cf1cc236..06c6a361 100644 --- a/src/bioimageio/core/remote_backends/gradio/server.py +++ b/src/bioimageio/core/remote_backends/gradio/server.py @@ -1,8 +1,8 @@ +from collections.abc import Iterable from itertools import chain from typing import ( Any, Dict, - Iterable, Literal, Optional, Union, diff --git a/src/bioimageio/core/sample.py b/src/bioimageio/core/sample.py index 1e4a7788..e9c4a580 100644 --- a/src/bioimageio/core/sample.py +++ b/src/bioimageio/core/sample.py @@ -1,6 +1,7 @@ from __future__ import annotations import collections.abc +from collections.abc import Iterable, Mapping, Sequence from dataclasses import dataclass from math import ceil, floor from types import MappingProxyType @@ -9,11 +10,8 @@ Callable, Dict, Generic, - Iterable, Literal, - Mapping, Optional, - Sequence, Tuple, TypeVar, Union, @@ -91,7 +89,7 @@ def __getitem__( ) @property - def batch_multi_index(self) -> Optional["pd.MultiIndex"]: + def batch_multi_index(self) -> Optional[pd.MultiIndex]: """Return the batch multi-index of the sample, if it has one. Returns: @@ -297,7 +295,7 @@ def transpose( ValueError: If not all batch dimensions have the same length after transposition (and possibly stacking/unstacking extra dimensions). """ - if any((unknown := [m not in self.members for m in axes])): + if any(unknown := [m not in self.members for m in axes]): raise ValueError(f"Axes specified for unknown members: {unknown}") members = { @@ -313,13 +311,13 @@ def transpose( if ( len( - ( + batch_lengths := { t.sizes[AxisId("batch")] for t in members.values() if AxisId("batch") in t.dims } - ) + ) > 1 ): @@ -329,7 +327,7 @@ def transpose( return self.__class__(members=members, stat=dict(self.stat), id=self.id) - def assign_batch_multi_index(self, multi_index: "pd.MultiIndex") -> Self: + def assign_batch_multi_index(self, multi_index: pd.MultiIndex) -> Self: """Return a new sample with the batch multi-index assigned to all sample members. Raises: diff --git a/src/bioimageio/core/stat_calculators.py b/src/bioimageio/core/stat_calculators.py index c85c2f17..2408c552 100644 --- a/src/bioimageio/core/stat_calculators.py +++ b/src/bioimageio/core/stat_calculators.py @@ -2,18 +2,14 @@ import collections import warnings +from collections import OrderedDict +from collections.abc import Collection, Iterable, Iterator, Mapping, Sequence from itertools import product from typing import ( Any, - Collection, Dict, - Iterable, - Iterator, List, - Mapping, Optional, - OrderedDict, - Sequence, Set, Tuple, Type, diff --git a/src/bioimageio/core/stat_measures.py b/src/bioimageio/core/stat_measures.py index 478af000..798d9a3c 100644 --- a/src/bioimageio/core/stat_measures.py +++ b/src/bioimageio/core/stat_measures.py @@ -1,11 +1,12 @@ from __future__ import annotations from abc import ABC, abstractmethod +from collections.abc import Mapping from typing import ( + Annotated, Any, Dict, Literal, - Mapping, Optional, Protocol, Tuple, @@ -20,7 +21,7 @@ Discriminator, PlainSerializer, ) -from typing_extensions import Annotated, TypeAlias +from typing_extensions import TypeAlias from .axis import AxisId from .common import MemberId, PerMember, QuantileMethod diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index 49cfd5d8..b42d0a2f 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -1,18 +1,16 @@ from __future__ import annotations import collections.abc +from collections.abc import Iterator, Mapping, Sequence from itertools import permutations from typing import ( TYPE_CHECKING, Any, Callable, Dict, - Iterator, List, Literal, - Mapping, Optional, - Sequence, Tuple, Union, cast, @@ -96,7 +94,7 @@ def __init__( self._data = xr.DataArray(array, dims=axes) def __repr__(self) -> str: - return f"" + return f"" def __array__(self, dtype: Optional[DTypeLike] = None): return np.asarray(self._data, dtype=dtype) @@ -127,7 +125,7 @@ def __getitem__( def __setitem__( self, key: Union[PerAxis[Union[SliceInfo, slice]], Tensor, xr.DataArray], - value: Union[Tensor, xr.DataArray, float, int], + value: Union[Tensor, xr.DataArray, float], ) -> None: if isinstance(key, Tensor): key = key._data @@ -175,7 +173,7 @@ def _inplace_binary_op( _ = self._data._inplace_binary_op( # pyright: ignore[reportPrivateUsage] ( other_d - if (other_d := getattr(other, "data")) is not None + if (other_d := other.data) is not None and isinstance( other_d, xr.DataArray, @@ -509,7 +507,7 @@ def sum(self, dim: Optional[Union[AxisId, Sequence[AxisId]]] = None) -> Self: """Reduce this Tensor's data by applying sum along some dimension(s).""" return self.__class__.from_xarray(self._data.sum(dim=dim)) - def assign_batch_multi_index(self, multi_index: "pd.MultiIndex") -> Self: + def assign_batch_multi_index(self, multi_index: pd.MultiIndex) -> Self: """Set the batch multi-index for this tensor. Args: diff --git a/src/bioimageio/core/utils/_compare.py b/src/bioimageio/core/utils/_compare.py index d57336dd..7be79b22 100644 --- a/src/bioimageio/core/utils/_compare.py +++ b/src/bioimageio/core/utils/_compare.py @@ -1,5 +1,5 @@ +from collections.abc import Sequence from difflib import HtmlDiff, unified_diff -from typing import Sequence from loguru import logger from ruyaml import Optional diff --git a/src/bioimageio/core/weight_converters/_utils_torch_onnx.py b/src/bioimageio/core/weight_converters/_utils_torch_onnx.py index 6016fe3a..45d25307 100644 --- a/src/bioimageio/core/weight_converters/_utils_torch_onnx.py +++ b/src/bioimageio/core/weight_converters/_utils_torch_onnx.py @@ -1,6 +1,7 @@ """helper to export both TorchScript or PytorchStateDict to ONNX""" from collections import defaultdict +from collections.abc import Sequence from itertools import chain from pathlib import Path from typing import ( @@ -10,7 +11,6 @@ List, Literal, Optional, - Sequence, Tuple, Union, ) @@ -56,7 +56,7 @@ def get_torch_sample_inputs(model_descr: ModelDescr) -> Tuple[torch.Tensor, ...] def _get_dynamic_axes_noop(model_descr: ModelDescr): """noop for dynamo=True which uses `get_dynamic_shapes` instead""" - return None + return def _get_dynamic_axes_impl(model_descr: ModelDescr): @@ -83,7 +83,7 @@ def _get_dynamic_axes_impl(model_descr: ModelDescr): def _get_dynamic_shapes_noop(model_descr: ModelDescr): """noop for dynamo=False which uses `get_dynamic_axes` instead""" - return None + return get_dynamic_shapes = _get_dynamic_shapes_noop get_dynamic_axes = _get_dynamic_axes_impl diff --git a/src/bioimageio/core/weight_converters/pytorch_to_onnx.py b/src/bioimageio/core/weight_converters/pytorch_to_onnx.py index 4569748b..be3e3fdf 100644 --- a/src/bioimageio/core/weight_converters/pytorch_to_onnx.py +++ b/src/bioimageio/core/weight_converters/pytorch_to_onnx.py @@ -1,5 +1,6 @@ +from collections.abc import Sequence from pathlib import Path -from typing import Optional, Sequence +from typing import Optional from bioimageio.spec.model.v0_5 import ModelDescr, OnnxWeightsDescr diff --git a/src/bioimageio/core/weight_converters/pytorch_to_torchscript.py b/src/bioimageio/core/weight_converters/pytorch_to_torchscript.py index cda2734d..00854cb8 100644 --- a/src/bioimageio/core/weight_converters/pytorch_to_torchscript.py +++ b/src/bioimageio/core/weight_converters/pytorch_to_torchscript.py @@ -1,5 +1,6 @@ +from collections.abc import Sequence from pathlib import Path -from typing import Any, Optional, Sequence, Tuple, Union +from typing import Any, Optional, Tuple, Union import torch from torch.jit import ScriptModule diff --git a/src/bioimageio/core/weight_converters/torchscript_to_onnx.py b/src/bioimageio/core/weight_converters/torchscript_to_onnx.py index 3da2cdbe..b2ac2f92 100644 --- a/src/bioimageio/core/weight_converters/torchscript_to_onnx.py +++ b/src/bioimageio/core/weight_converters/torchscript_to_onnx.py @@ -1,5 +1,6 @@ +from collections.abc import Sequence from pathlib import Path -from typing import Optional, Sequence, Union +from typing import Optional, Union import torch.jit from loguru import logger diff --git a/tests/test_bioimageio_collection.py b/tests/test_bioimageio_collection.py index 112d762b..a41c1984 100644 --- a/tests/test_bioimageio_collection.py +++ b/tests/test_bioimageio_collection.py @@ -1,13 +1,14 @@ import os +from collections.abc import Iterable, Mapping from itertools import chain from pathlib import Path -from typing import Iterable, Mapping, Tuple +from typing import Tuple import pytest -from bioimageio.spec import InvalidDescr, settings -from bioimageio.spec.common import Sha256 from pydantic import HttpUrl +from bioimageio.spec import InvalidDescr, settings +from bioimageio.spec.common import Sha256 from tests.utils import ParameterSet, expensive_test TEST_RDF_SOURCES: Mapping[str, Tuple[HttpUrl, Sha256]] = { @@ -72,11 +73,10 @@ def test_rdf( sha: Sha256, key: str, ): + from bioimageio.core import load_description, load_description_and_test from bioimageio.spec import get_conda_env from bioimageio.spec.model import ModelDescr - from bioimageio.core import load_description, load_description_and_test - descr = load_description( descr_url, sha256=sha, format_version="latest", perform_io_checks=True ) diff --git a/tests/test_bioimageio_spec_version.py b/tests/test_bioimageio_spec_version.py index cf10007b..dea63430 100644 --- a/tests/test_bioimageio_spec_version.py +++ b/tests/test_bioimageio_spec_version.py @@ -3,9 +3,10 @@ from typing import Optional import pytest -from bioimageio.spec._internal.gh_utils import set_github_warning from packaging.version import Version +from bioimageio.spec._internal.gh_utils import set_github_warning + def test_bioimageio_spec_version(conda_cmd: Optional[str]): if conda_cmd is None: diff --git a/tests/test_cli.py b/tests/test_cli.py index ddfd92b2..fa815cb5 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,6 +1,7 @@ import subprocess +from collections.abc import Sequence from pathlib import Path -from typing import Any, List, Sequence +from typing import Any, List import numpy as np import pytest diff --git a/tests/test_prediction.py b/tests/test_prediction.py index f6c3ff82..3037ea6c 100644 --- a/tests/test_prediction.py +++ b/tests/test_prediction.py @@ -1,5 +1,6 @@ +from collections.abc import Mapping from pathlib import Path -from typing import Literal, Mapping, NamedTuple +from typing import Literal, NamedTuple import numpy as np import pandas as pd diff --git a/tests/test_proc_ops.py b/tests/test_proc_ops.py index 396859a6..16a40988 100644 --- a/tests/test_proc_ops.py +++ b/tests/test_proc_ops.py @@ -1,5 +1,6 @@ +from collections.abc import Iterable from pathlib import Path -from typing import Any, Iterable, Optional, Tuple, Type, TypeVar +from typing import Any, Optional, Tuple, Type, TypeVar import numpy as np import pytest diff --git a/tests/test_tensor.py b/tests/test_tensor.py index 86578294..92d8b5ea 100644 --- a/tests/test_tensor.py +++ b/tests/test_tensor.py @@ -1,4 +1,4 @@ -from typing import Sequence +from collections.abc import Sequence import numpy as np import pandas as pd diff --git a/tests/utils.py b/tests/utils.py index f9116fa5..c8b90530 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,7 +1,8 @@ """utils to test bioimageio.core""" import os -from typing import Any, Protocol, Sequence +from collections.abc import Sequence +from typing import Any, Protocol import pytest From b36e81b4824acdfddd1968fa6ef470966933c7e8 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Mon, 3 Aug 2026 11:14:12 +0200 Subject: [PATCH 09/85] ruff check --fix --unsafe-fixes --- example/model_usage.ipynb | 9 +- scripts/setup_dev_env.py | 2 +- .../core/_description_serializer.py | 3 +- src/bioimageio/core/_model_adapter.py | 35 ++-- src/bioimageio/core/_op_base.py | 6 +- src/bioimageio/core/_ops_stardist.py | 30 ++- src/bioimageio/core/_prediction_pipeline.py | 104 ++++------ src/bioimageio/core/_resource_tests.py | 194 +++++++++--------- .../core/_restore_batch_multi_index.py | 6 +- src/bioimageio/core/_sample_serializer.py | 8 +- src/bioimageio/core/_settings.py | 10 +- src/bioimageio/core/axis.py | 11 +- src/bioimageio/core/backends/__init__.py | 12 +- src/bioimageio/core/backends/keras_backend.py | 8 +- src/bioimageio/core/backends/onnx_backend.py | 26 +-- .../core/backends/pytorch_backend.py | 32 +-- .../core/backends/tensorflow_backend.py | 34 +-- .../core/backends/torchscript_backend.py | 14 +- src/bioimageio/core/block.py | 11 +- src/bioimageio/core/block_meta.py | 27 ++- src/bioimageio/core/cli.py | 76 ++++--- src/bioimageio/core/commands.py | 22 +- src/bioimageio/core/common.py | 5 +- src/bioimageio/core/digest_spec.py | 103 +++------- src/bioimageio/core/io.py | 43 ++-- src/bioimageio/core/prediction.py | 55 ++--- src/bioimageio/core/proc_ops.py | 87 ++++---- src/bioimageio/core/proc_setup.py | 29 ++- .../core/remote_backends/__init__.py | 8 +- .../core/remote_backends/gradio/client.py | 24 +-- .../core/remote_backends/gradio/serializer.py | 12 +- .../core/remote_backends/gradio/server.py | 15 +- src/bioimageio/core/sample.py | 39 ++-- src/bioimageio/core/stat_calculators.py | 112 +++++----- src/bioimageio/core/stat_measures.py | 17 +- src/bioimageio/core/tensor.py | 78 +++---- .../core/weight_converters/_add_weights.py | 9 +- .../weight_converters/_utils_torch_onnx.py | 20 +- .../weight_converters/keras_to_tensorflow.py | 8 +- .../core/weight_converters/pytorch_to_onnx.py | 5 +- .../pytorch_to_torchscript.py | 10 +- .../weight_converters/torchscript_to_onnx.py | 5 +- tests/conftest.py | 5 +- tests/test_bioimageio_collection.py | 3 +- tests/test_bioimageio_spec_version.py | 5 +- tests/test_cli.py | 10 +- tests/test_commands.py | 6 +- tests/test_io.py | 3 +- tests/test_proc_ops.py | 10 +- tests/test_remote_backends/test_gradio.py | 3 +- tests/test_stat_calculators.py | 8 +- tests/test_stat_measures.py | 8 +- tests/test_weight_converters.py | 2 +- 53 files changed, 630 insertions(+), 797 deletions(-) diff --git a/example/model_usage.ipynb b/example/model_usage.ipynb index 34e3560e..9dddc388 100644 --- a/example/model_usage.ipynb +++ b/example/model_usage.ipynb @@ -58,6 +58,8 @@ "metadata": {}, "outputs": [], "source": [ + "from __future__ import annotations\n", + "\n", "import os\n", "\n", "# install dependencies if running in colab\n", @@ -89,7 +91,7 @@ "source": [ "from collections.abc import Mapping\n", "from pprint import pprint\n", - "from typing import Any, Union\n", + "from typing import Any\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", @@ -101,9 +103,7 @@ "\n", "# Function to display input and prediction output images\n", "def show_images(\n", - " images: Union[\n", - " Mapping[str, NDArray[Any]], Mapping[TensorId, Union[Tensor, NDArray[Any]]]\n", - " ],\n", + " images: Mapping[str, NDArray[Any]] | Mapping[TensorId, Tensor | NDArray[Any]],\n", ") -> None:\n", " for title, image in images.items():\n", " if isinstance(image, Tensor):\n", @@ -449,7 +449,6 @@ } ], "source": [ - "from bioimageio.core import Sample\n", "from bioimageio.core.stat_measures import DatasetMean\n", "\n", "sample = Sample(\n", diff --git a/scripts/setup_dev_env.py b/scripts/setup_dev_env.py index b1df230f..c3dbe1d9 100644 --- a/scripts/setup_dev_env.py +++ b/scripts/setup_dev_env.py @@ -10,7 +10,7 @@ def run(prompt: str): if __name__ == "__main__": repo_dir = Path(__file__).parent.parent.parent - cur_dir = Path().resolve() + cur_dir = Path.cwd() chdir(str(repo_dir)) try: run("mamba env create --file core-bioimage-io/dev/env.yaml") diff --git a/src/bioimageio/core/_description_serializer.py b/src/bioimageio/core/_description_serializer.py index 516cda2c..9656c7ef 100644 --- a/src/bioimageio/core/_description_serializer.py +++ b/src/bioimageio/core/_description_serializer.py @@ -1,7 +1,6 @@ import base64 import hashlib from io import BytesIO -from typing import Tuple from zipfile import ZipFile from bioimageio.spec import ( @@ -57,7 +56,7 @@ def deserialize(serialized: bytes) -> ResourceDescr: return descr @classmethod - def serialize_to_string_and_hash(cls, rd: ResourceDescr) -> Tuple[str, Sha256]: + def serialize_to_string_and_hash(cls, rd: ResourceDescr) -> tuple[str, Sha256]: package_bytes = cls.serialize(rd) safe_bytes = cls._get_safe_bytes(package_bytes) serialized_str = safe_bytes.decode(cls.STRING_ENCODING) diff --git a/src/bioimageio/core/_model_adapter.py b/src/bioimageio/core/_model_adapter.py index a6f13aeb..cab9132a 100644 --- a/src/bioimageio/core/_model_adapter.py +++ b/src/bioimageio/core/_model_adapter.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import gc import warnings from abc import ABC, abstractmethod @@ -5,12 +7,7 @@ from queue import LifoQueue from typing import ( Any, - Dict, Generic, - List, - Optional, - Tuple, - Union, ) from exceptiongroup import ExceptionGroup @@ -50,7 +47,7 @@ class ModelAdapter(ABC): """ def __init__( - self, model_description: AnyModelDescr, devices: Optional[Sequence[str]] + self, model_description: AnyModelDescr, devices: Sequence[str] | None ): super().__init__() self._model_descr = model_description @@ -80,8 +77,8 @@ def load(self) -> None: @abstractmethod def forward( - self, inputs: PerMember[Optional[Tensor]] - ) -> PerMember[Optional[Tensor]]: ... + self, inputs: PerMember[Tensor | None] + ) -> PerMember[Tensor | None]: ... @abstractmethod def unload(self): @@ -108,12 +105,12 @@ def close(self): class LocalModelAdapter(ModelAdapter, ABC, Generic[DeviceType, ModelType]): def load(self) -> None: devices = self._devices - self._model_queue: LifoQueue[Tuple[DeviceType, ModelType]] = LifoQueue() + self._model_queue: LifoQueue[tuple[DeviceType, ModelType]] = LifoQueue() parsed_devices = self._parse_devices(devices) assert parsed_devices # prioritize devices by order specified by user - device_exceptions: Dict[str, Exception] = {} - self._initialized_devices: List[str] = [] + device_exceptions: dict[str, Exception] = {} + self._initialized_devices: list[str] = [] for d in parsed_devices[::-1]: try: model = self._init_model_on_device(d) @@ -142,7 +139,7 @@ def load(self) -> None: super().load() @abstractmethod - def _parse_devices(self, devices: Optional[Sequence[str]]) -> Sequence[DeviceType]: + def _parse_devices(self, devices: Sequence[str] | None) -> Sequence[DeviceType]: """Parse devices Note: @@ -155,8 +152,8 @@ def _parse_devices(self, devices: Optional[Sequence[str]]) -> Sequence[DeviceTyp def _init_model_on_device(self, device: DeviceType) -> ModelType: ... def forward( - self, inputs: PerMember[Optional[Tensor]] - ) -> PerMember[Optional[Tensor]]: + self, inputs: PerMember[Tensor | None] + ) -> PerMember[Tensor | None]: """ Run forward pass of model to get model predictions @@ -221,8 +218,8 @@ def _forward_impl( self, device: DeviceType, model: ModelType, - input_arrays: Sequence[Optional[NDArray[Any]]], - ) -> Union[List[Optional[NDArray[Any]]], Tuple[Optional[NDArray[Any]], ...]]: + input_arrays: Sequence[NDArray[Any] | None], + ) -> list[NDArray[Any] | None] | tuple[NDArray[Any] | None, ...]: """framework specific forward implementation""" def unload(self): @@ -273,8 +270,8 @@ def server(self) -> str: return self._server def forward( - self, inputs: PerMember[Optional[Tensor]] - ) -> PerMember[Optional[Tensor]]: + self, inputs: PerMember[Tensor | None] + ) -> PerMember[Tensor | None]: serialized_input = self._serializer.serialize_sample( Sample( members={k: v for k, v in inputs.items() if v is not None}, @@ -293,5 +290,5 @@ def _forward_impl( ) -> Iterable[SerializedSampleBlockType]: ... @abstractmethod - def test(self) -> Optional[ValidationSummary]: + def test(self) -> ValidationSummary | None: """Run the bioimageio model test.""" diff --git a/src/bioimageio/core/_op_base.py b/src/bioimageio/core/_op_base.py index 002d0580..bc85c7cb 100644 --- a/src/bioimageio/core/_op_base.py +++ b/src/bioimageio/core/_op_base.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from abc import ABC, abstractmethod from collections.abc import Collection from dataclasses import dataclass @@ -20,7 +22,7 @@ @dataclass -class Operator(Generic[SampleT], ABC): +class Operator(ABC, Generic[SampleT]): """Base class for all operators.""" @abstractmethod @@ -51,7 +53,7 @@ class SimpleOperator(BlockwiseOperator): @abstractmethod def get_output_shape(self, input_shape: PerAxis[int]) -> PerAxis[int]: ... - def __call__(self, sample: Union[Sample, SampleBlock]) -> None: + def __call__(self, sample: Sample | SampleBlock) -> None: if self.input not in sample.members: return # TODO: raise? diff --git a/src/bioimageio/core/_ops_stardist.py b/src/bioimageio/core/_ops_stardist.py index f70df70d..7e001e73 100644 --- a/src/bioimageio/core/_ops_stardist.py +++ b/src/bioimageio/core/_ops_stardist.py @@ -1,13 +1,11 @@ +from __future__ import annotations + from abc import ABC, abstractmethod from collections.abc import Collection from dataclasses import dataclass from typing import ( Any, Generic, - List, - Optional, - Tuple, - Union, cast, ) @@ -26,16 +24,16 @@ ) from .tensor import Tensor -NdTuple = TypeVar("NdTuple", Tuple[int, int], Tuple[int, int, int]) +NdTuple = TypeVar("NdTuple", tuple[int, int], tuple[int, int, int]) NdBorder = TypeVar( "NdBorder", - Tuple[Tuple[int, int], Tuple[int, int]], - Tuple[Tuple[int, int], Tuple[int, int], Tuple[int, int]], + tuple[tuple[int, int], tuple[int, int]], + tuple[tuple[int, int], tuple[int, int], tuple[int, int]], ) @dataclass -class _StardistPostprocessingBase(SamplewiseOperator, Generic[NdTuple, NdBorder], ABC): +class _StardistPostprocessingBase(SamplewiseOperator, ABC, Generic[NdTuple, NdBorder]): prob_dist_input_id: MemberId instance_labels_output_id: MemberId @@ -48,7 +46,7 @@ class _StardistPostprocessingBase(SamplewiseOperator, Generic[NdTuple, NdBorder] nms_threshold: float """The IoU threshold for non-maximum suppression.""" - b: Union[int, NdBorder] + b: int | NdBorder """Border region in which object probability is set to zero.""" n_rays: int @@ -87,7 +85,7 @@ def __call__(self, sample: Sample) -> None: prob_dist = prob_dist.transpose( (AxisId("batch"), *allowed_spatial, AxisId("channel")) ) - labels: List[NDArray[Any]] = [] + labels: list[NDArray[Any]] = [] for batch_idx in range(prob_dist.sizes[AxisId("batch")]): prob = prob_dist[ {AxisId("batch"): batch_idx, AxisId("channel"): 0} @@ -118,11 +116,11 @@ def _impl( @dataclass class StardistPostprocessing2D( _StardistPostprocessingBase[ - Tuple[int, int], Tuple[Tuple[int, int], Tuple[int, int]] + tuple[int, int], tuple[tuple[int, int], tuple[int, int]] ] ): def _impl( - self, prob: NDArray[Any], dist: NDArray[Any], spatial_shape: Tuple[int, int] + self, prob: NDArray[Any], dist: NDArray[Any], spatial_shape: tuple[int, int] ) -> NDArray[np.int32]: from stardist import ( non_maximum_suppression, # pyright: ignore[reportUnknownVariableType] @@ -164,20 +162,20 @@ def from_proc_descr( @dataclass class StardistPostprocessing3D( _StardistPostprocessingBase[ - Tuple[int, int, int], Tuple[Tuple[int, int], Tuple[int, int], Tuple[int, int]] + tuple[int, int, int], tuple[tuple[int, int], tuple[int, int], tuple[int, int]] ] ): - anisotropy: Tuple[float, float, float] + anisotropy: tuple[float, float, float] """Anisotropy factors for 3D star-convex polyhedra, i.e. the physical pixel size along each spatial axis.""" - overlap_label: Optional[int] = None + overlap_label: int | None = None """Optional label to apply to any area of overlapping predicted objects.""" def _impl( self, prob: NDArray[Any], dist: NDArray[Any], - spatial_shape: Tuple[int, int, int], + spatial_shape: tuple[int, int, int], ) -> NDArray[np.int32]: from stardist import ( Rays_GoldenSpiral, diff --git a/src/bioimageio/core/_prediction_pipeline.py b/src/bioimageio/core/_prediction_pipeline.py index cec741e2..8321b9fb 100644 --- a/src/bioimageio/core/_prediction_pipeline.py +++ b/src/bioimageio/core/_prediction_pipeline.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import warnings from abc import ABC, abstractmethod from collections.abc import Iterable, Mapping, Sequence @@ -5,14 +7,9 @@ from types import MappingProxyType from typing import ( Any, - List, Literal, NamedTuple, - Optional, - Set, - Tuple, TypeVar, - Union, ) from loguru import logger @@ -68,9 +65,7 @@ def __init__( *, default_blocksize_parameter: BlocksizeParameter, default_batch_size: int, - preceding_prediction_pipelines: Optional[ - Sequence[Union["PredictionPipeline", "RemotePredictionPipeline"]] - ], + preceding_prediction_pipelines: Sequence[PredictionPipeline | RemotePredictionPipeline] | None, ) -> None: super().__init__() self._model_descr = model_descr @@ -126,14 +121,14 @@ def model_description(self) -> AnyModelDescr: def _get_preceding_prediction_pipelines_for_sample( self, sample: Sample - ) -> Sequence["PredictionPipeline | RemotePredictionPipeline"]: + ) -> Sequence[PredictionPipeline | RemotePredictionPipeline]: """Get preceding prediction pipelines for a sample based on the sample's input member ids.""" if not self._preceding_prediction_pipelines: return () required_inputs = set(self.input_ids) sample_members = set(sample.members.keys()) - preceding_pipelines: List[PredictionPipeline | RemotePredictionPipeline] = [] + preceding_pipelines: list[PredictionPipeline | RemotePredictionPipeline] = [] for pp in self._preceding_prediction_pipelines[::-1]: preceding_pipelines.insert(0, pp) sample_members.update(pp.output_ids) @@ -211,13 +206,8 @@ def predict_sample_with_blocking( sample: Sample, skip_preprocessing: bool = False, skip_postprocessing: bool = False, - ns: Optional[ - Union[ - v0_5.ParameterizedSize_N, - Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N], - ] - ] = None, - batch_size: Optional[int] = None, + ns: v0_5.ParameterizedSize_N | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] | None = None, + batch_size: int | None = None, ) -> Sample: """Predict a sample by predicting sample blocks. @@ -288,14 +278,9 @@ def predict_sample_with_blocking_yield_intermediates( sample: Sample, skip_preprocessing: bool = False, skip_postprocessing: bool = False, - ns: Optional[ - Union[ - v0_5.ParameterizedSize_N, - Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N], - ] - ] = None, - batch_size: Optional[int] = None, - ) -> Tuple[int, Iterable[IntermediatePrediction]]: + ns: v0_5.ParameterizedSize_N | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] | None = None, + batch_size: int | None = None, + ) -> tuple[int, Iterable[IntermediatePrediction]]: """Predict `sample` by predicting sample blocks and yield intermediate predictions if no samplewise postprocessing is included. Also yields intermediate predictions if there are preceding prediction pipelines (model inputs depend on another model's outputs). For preceding prediction pipelines `ns` and `batch_size` are shared, but pre- and postprocessing are never skipped in preceding pipelines. @@ -361,7 +346,7 @@ def predict_sample_with_fixed_blocking_yield_intermediates( skip_preprocessing: bool = False, skip_postprocessing: bool = False, fill_value: float = float("nan"), - ) -> Tuple[int, Iterable[IntermediatePrediction]]: + ) -> tuple[int, Iterable[IntermediatePrediction]]: """Predict `sample` by predicting sample blocks of `input_block_shape` and yield intermediate predictions if no samplewise postprocessing is included. Also yields intermediate predictions if there are preceding prediction pipelines (model inputs depend on another model's outputs). For preceding prediction pipelines `input_block_shape` and `fill_value` are shared, but pre- and postprocessing are never skipped in preceding pipelines. @@ -409,7 +394,7 @@ def _predict_sample_with_fixed_blocking_yield_intermediates_impl( skip_preprocessing: bool = False, skip_postprocessing: bool = False, fill_value: float = float("nan"), - ) -> Tuple[int, Iterable[IntermediatePrediction]]: ... + ) -> tuple[int, Iterable[IntermediatePrediction]]: ... @abstractmethod def predict_sample_block( @@ -441,14 +426,12 @@ def __init__( *, name: str, model_description: AnyModelDescr, - preprocessing: List[Processing], - postprocessing: List[Processing], + preprocessing: list[Processing], + postprocessing: list[Processing], model_adapter: ModelAdapter, default_blocksize_parameter: BlocksizeParameter = 10, default_batch_size: int = 1, - preceding_prediction_pipelines: Optional[ - Sequence[Union["PredictionPipeline", "RemotePredictionPipeline"]] - ] = None, + preceding_prediction_pipelines: Sequence[PredictionPipeline | RemotePredictionPipeline] | None = None, ) -> None: """Consider using `create_prediction_pipeline` to create a `PredictionPipeline` with sensible defaults.""" super().__init__( @@ -466,19 +449,19 @@ def __init__( self.name = name # split preprocessing into samplewise and blockwise. samplewise preprocessing is all preprocessing up to including the last samplewise operator, blockwise preprocessing are the remaining blockwise operators. # I.e. some samplewise preprocessing may be a blockwise op (at some point followed by a samplewise op). - self._samplewise_preprocessing: List[ - Union[SamplewiseOperator, BlockwiseOperator] + self._samplewise_preprocessing: list[ + SamplewiseOperator | BlockwiseOperator ] = [] - self._blockwise_preprocessing: List[BlockwiseOperator] = [] + self._blockwise_preprocessing: list[BlockwiseOperator] = [] for op in preprocessing[::-1]: if isinstance(op, BlockwiseOperator) and not self._samplewise_preprocessing: self._blockwise_preprocessing.insert(0, op) else: self._samplewise_preprocessing.insert(0, op) # split postprocessing analougly, but here we start blockwise and switch to samplewise at the first samplewise operator. - self._blockwise_postprocessing: List[BlockwiseOperator] = [] - self._samplewise_postprocessing: List[ - Union[BlockwiseOperator, SamplewiseOperator] + self._blockwise_postprocessing: list[BlockwiseOperator] = [] + self._samplewise_postprocessing: list[ + BlockwiseOperator | SamplewiseOperator ] = [] for op in postprocessing: if ( @@ -621,13 +604,8 @@ def predict_sample_with_blocking( sample: Sample, skip_preprocessing: bool = False, skip_postprocessing: bool = False, - ns: Optional[ - Union[ - v0_5.ParameterizedSize_N, - Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N], - ] - ] = None, - batch_size: Optional[int] = None, + ns: v0_5.ParameterizedSize_N | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] | None = None, + batch_size: int | None = None, ) -> Sample: output = None for output in self.predict_sample_with_blocking_yield_intermediates( @@ -652,7 +630,7 @@ def _predict_sample_with_fixed_blocking_yield_intermediates_impl( skip_preprocessing: bool = False, skip_postprocessing: bool = False, fill_value: float = float("nan"), - ) -> Tuple[int, Iterable[IntermediatePrediction]]: + ) -> tuple[int, Iterable[IntermediatePrediction]]: """Predict `sample` with given `input_block_shape` and yield the full sample with intermediate results. Note: @@ -734,7 +712,7 @@ def _apply_samplewise_preprocessing(self, sample: Sample, /) -> None: op(sample) def _apply_blockwise_preprocessing( - self, sample_block: Union[Sample, SampleBlock], / + self, sample_block: Sample | SampleBlock, / ) -> None: """Apply blockwise preprocessing operators in-place. @@ -743,7 +721,7 @@ def _apply_blockwise_preprocessing( for op in self._blockwise_preprocessing: op(sample_block) - def apply_preprocessing(self, sample: Union[Sample, SampleBlock]) -> None: + def apply_preprocessing(self, sample: Sample | SampleBlock) -> None: """Apply preprocessing in-place, also may updates sample stats""" if isinstance(sample, Sample): @@ -754,7 +732,7 @@ def apply_preprocessing(self, sample: Union[Sample, SampleBlock]) -> None: self._apply_blockwise_preprocessing(sample) def _apply_blockwise_postprocessing( - self, sample_block: Union[Sample, SampleBlock], / + self, sample_block: Sample | SampleBlock, / ) -> None: """Apply in-place blockwise postprocessing operators @@ -774,7 +752,7 @@ def _apply_samplewise_postprocessing(self, sample: Sample, /) -> None: for op in self._samplewise_postprocessing: op(sample) - def apply_postprocessing(self, sample: Union[Sample, SampleBlock]) -> None: + def apply_postprocessing(self, sample: Sample | SampleBlock) -> None: """apply postprocessing in-place, also may updates samples stats""" self._apply_blockwise_postprocessing(sample) if isinstance(sample, Sample): @@ -822,9 +800,7 @@ def __init__( server: str, default_blocksize_parameter: BlocksizeParameter, default_batch_size: int, - preceding_prediction_pipelines: Optional[ - Sequence[Union["PredictionPipeline", "RemotePredictionPipeline"]] - ] = None, + preceding_prediction_pipelines: Sequence[PredictionPipeline | RemotePredictionPipeline] | None = None, ) -> None: super().__init__( model_descr, @@ -842,18 +818,16 @@ def server(self) -> str: def create_prediction_pipeline( bioimageio_model: AnyModelDescr, *, - devices: Optional[Sequence[str]] = None, - weight_format: Optional[SupportedWeightsFormat] = None, - weights_format: Optional[SupportedWeightsFormat] = None, - dataset_for_initial_statistics: Iterable[Union[Sample, Sequence[Tensor]]] = tuple(), + devices: Sequence[str] | None = None, + weight_format: SupportedWeightsFormat | None = None, + weights_format: SupportedWeightsFormat | None = None, + dataset_for_initial_statistics: Iterable[Sample | Sequence[Tensor]] = (), keep_updating_initial_dataset_statistics: bool = False, fixed_dataset_statistics: Mapping[Measure, MeasureValue] = MappingProxyType({}), - model_adapter: Optional[ModelAdapter] = None, - ns: Optional[BlocksizeParameter] = None, + model_adapter: ModelAdapter | None = None, + ns: BlocksizeParameter | None = None, default_blocksize_parameter: BlocksizeParameter = 10, # TODO: default to None and find smart blocksize params per axis to reduce overlap of blocks with large halo - preceding_prediction_pipelines: Optional[ - Sequence[Union[PredictionPipeline, RemotePredictionPipeline]] - ] = None, + preceding_prediction_pipelines: Sequence[PredictionPipeline | RemotePredictionPipeline] | None = None, **deprecated_kwargs: Any, ) -> PredictionPipeline: """ @@ -924,7 +898,7 @@ def dataset(): fixed_dataset_stats=fixed_dataset_statistics, ) - def _get_preceding_model_ids(model: AnyModelDescr) -> Set[v0_5.ModelId]: + def _get_preceding_model_ids(model: AnyModelDescr) -> set[v0_5.ModelId]: return { input_descr.output_of for input_descr in model.inputs @@ -972,8 +946,8 @@ def _get_preceding_model_ids(model: AnyModelDescr) -> Set[v0_5.ModelId]: def create_remote_prediction_pipeline( model_description: AnyModelDescr, *, - server: Optional[str] = None, - server_type: Optional[Literal["gradio"]] = "gradio", + server: str | None = None, + server_type: Literal["gradio"] | None = "gradio", precomputed_statistics: Mapping[Measure, MeasureValue] = MappingProxyType({}), default_blocksize_parameter: BlocksizeParameter = 10, # TODO: default to None and find smart blocksize params per axis to reduce overlap of blocks with large halo default_batch_size: int = 1, diff --git a/src/bioimageio/core/_resource_tests.py b/src/bioimageio/core/_resource_tests.py index 9c728e09..fcb0327a 100644 --- a/src/bioimageio/core/_resource_tests.py +++ b/src/bioimageio/core/_resource_tests.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import hashlib import os import platform @@ -14,13 +16,7 @@ from typing import ( Any, Callable, - Dict, - List, Literal, - Optional, - Set, - Tuple, - Union, overload, ) @@ -84,12 +80,12 @@ class DeprecatedKwargs(TypedDict): absolute_tolerance: NotRequired[AbsoluteTolerance] relative_tolerance: NotRequired[RelativeTolerance] - decimal: NotRequired[Optional[int]] + decimal: NotRequired[int | None] def enable_determinism( mode: Literal["seed_only", "full"] = "full", - weight_formats: Optional[Sequence[SupportedWeightsFormat]] = None, + weight_formats: Sequence[SupportedWeightsFormat] | None = None, ): """Seed and configure ML frameworks for maximum reproducibility. May degrade performance. Only recommended for testing reproducibility! @@ -173,14 +169,14 @@ def enable_determinism( def test_model( - source: Union[v0_4.ModelDescr, v0_5.ModelDescr, PermissiveFileSource], - weight_format: Optional[SupportedWeightsFormat] = None, - devices: Optional[List[str]] = None, + source: v0_4.ModelDescr | v0_5.ModelDescr | PermissiveFileSource, + weight_format: SupportedWeightsFormat | None = None, + devices: list[str] | None = None, *, determinism: Literal["seed_only", "full"] = "seed_only", - sha256: Optional[Sha256] = None, + sha256: Sha256 | None = None, stop_early: bool = False, - working_dir: Optional[Union[os.PathLike[str], str]] = None, + working_dir: os.PathLike[str] | str | None = None, **deprecated: Unpack[DeprecatedKwargs], ) -> ValidationSummary: """Test model inference""" @@ -203,20 +199,18 @@ def default_run_command(args: Sequence[str]): def test_description( - source: Union[ResourceDescr, PermissiveFileSource, BioimageioYamlContent], + source: ResourceDescr | PermissiveFileSource | BioimageioYamlContent, *, - format_version: Union[FormatVersionPlaceholder, str] = "discover", - weight_format: Optional[SupportedWeightsFormat] = None, - devices: Optional[Sequence[str]] = None, + format_version: FormatVersionPlaceholder | str = "discover", + weight_format: SupportedWeightsFormat | None = None, + devices: Sequence[str] | None = None, determinism: Literal["seed_only", "full"] = "seed_only", - expected_type: Optional[str] = None, - sha256: Optional[Sha256] = None, + expected_type: str | None = None, + sha256: Sha256 | None = None, stop_early: bool = False, - runtime_env: Union[ - Literal["currently-active", "as-described"], Path, BioimageioCondaEnv - ] = ("currently-active"), + runtime_env: Literal["currently-active", "as-described"] | Path | BioimageioCondaEnv = ("currently-active"), run_command: Callable[[Sequence[str]], None] = default_run_command, - working_dir: Optional[Union[os.PathLike[str], str]] = None, + working_dir: os.PathLike[str] | str | None = None, **deprecated: Unpack[DeprecatedKwargs], ) -> ValidationSummary: """Test a bioimage.io resource dynamically, @@ -282,8 +276,8 @@ def test_description( verbose = working_dir is not None if working_dir is None: - td_kwargs: Dict[str, Any] = ( - dict(ignore_cleanup_errors=True) if sys.version_info >= (3, 10) else {} + td_kwargs: dict[str, Any] = ( + {"ignore_cleanup_errors": True} if sys.version_info >= (3, 10) else {} ) working_dir_ctxt = TemporaryDirectory(**td_kwargs) else: @@ -355,14 +349,14 @@ def _test_in_env( *, descr: ResourceDescr, working_dir: Path, - weight_format: Optional[SupportedWeightsFormat], - conda_env: Optional[BioimageioCondaEnv], - devices: Optional[Sequence[str]], + weight_format: SupportedWeightsFormat | None, + conda_env: BioimageioCondaEnv | None, + devices: Sequence[str] | None, determinism: Literal["seed_only", "full"], run_command: Callable[[Sequence[str]], None], stop_early: bool, - expected_type: Optional[str], - sha256: Optional[Sha256], + expected_type: str | None, + sha256: Sha256 | None, verbose: bool, **deprecated: Unpack[DeprecatedKwargs], ): @@ -570,113 +564,113 @@ def _test_in_env( @overload def load_description_and_test( - source: Union[ResourceDescr, PermissiveFileSource, BioimageioYamlContent], + source: ResourceDescr | PermissiveFileSource | BioimageioYamlContent, *, format_version: Literal["latest"], - weight_format: Optional[SupportedWeightsFormat] = None, - devices: Optional[Sequence[str]] = None, + weight_format: SupportedWeightsFormat | None = None, + devices: Sequence[str] | None = None, determinism: Literal["seed_only", "full"] = "seed_only", expected_type: Literal["model"], - sha256: Optional[Sha256] = None, + sha256: Sha256 | None = None, stop_early: bool = False, - working_dir: Optional[Union[os.PathLike[str], str]] = None, + working_dir: os.PathLike[str] | str | None = None, **deprecated: Unpack[DeprecatedKwargs], -) -> Union[ModelDescr, InvalidDescr]: ... +) -> ModelDescr | InvalidDescr: ... @overload def load_description_and_test( - source: Union[ResourceDescr, PermissiveFileSource, BioimageioYamlContent], + source: ResourceDescr | PermissiveFileSource | BioimageioYamlContent, *, format_version: Literal["latest"], - weight_format: Optional[SupportedWeightsFormat] = None, - devices: Optional[Sequence[str]] = None, + weight_format: SupportedWeightsFormat | None = None, + devices: Sequence[str] | None = None, determinism: Literal["seed_only", "full"] = "seed_only", expected_type: Literal["dataset"], - sha256: Optional[Sha256] = None, + sha256: Sha256 | None = None, stop_early: bool = False, - working_dir: Optional[Union[os.PathLike[str], str]] = None, + working_dir: os.PathLike[str] | str | None = None, **deprecated: Unpack[DeprecatedKwargs], -) -> Union[DatasetDescr, InvalidDescr]: ... +) -> DatasetDescr | InvalidDescr: ... @overload def load_description_and_test( - source: Union[ResourceDescr, PermissiveFileSource, BioimageioYamlContent], + source: ResourceDescr | PermissiveFileSource | BioimageioYamlContent, *, format_version: Literal["latest"], - weight_format: Optional[SupportedWeightsFormat] = None, - devices: Optional[Sequence[str]] = None, + weight_format: SupportedWeightsFormat | None = None, + devices: Sequence[str] | None = None, determinism: Literal["seed_only", "full"] = "seed_only", - expected_type: Optional[str] = None, - sha256: Optional[Sha256] = None, + expected_type: str | None = None, + sha256: Sha256 | None = None, stop_early: bool = False, - working_dir: Optional[Union[os.PathLike[str], str]] = None, + working_dir: os.PathLike[str] | str | None = None, **deprecated: Unpack[DeprecatedKwargs], -) -> Union[LatestResourceDescr, InvalidDescr]: ... +) -> LatestResourceDescr | InvalidDescr: ... @overload def load_description_and_test( - source: Union[ResourceDescr, PermissiveFileSource, BioimageioYamlContent], + source: ResourceDescr | PermissiveFileSource | BioimageioYamlContent, *, - format_version: Union[FormatVersionPlaceholder, str] = DISCOVER, - weight_format: Optional[SupportedWeightsFormat] = None, - devices: Optional[Sequence[str]] = None, + format_version: FormatVersionPlaceholder | str = DISCOVER, + weight_format: SupportedWeightsFormat | None = None, + devices: Sequence[str] | None = None, determinism: Literal["seed_only", "full"] = "seed_only", expected_type: Literal["model"], - sha256: Optional[Sha256] = None, + sha256: Sha256 | None = None, stop_early: bool = False, - working_dir: Optional[Union[os.PathLike[str], str]] = None, + working_dir: os.PathLike[str] | str | None = None, **deprecated: Unpack[DeprecatedKwargs], -) -> Union[AnyModelDescr, InvalidDescr]: ... +) -> AnyModelDescr | InvalidDescr: ... @overload def load_description_and_test( - source: Union[ResourceDescr, PermissiveFileSource, BioimageioYamlContent], + source: ResourceDescr | PermissiveFileSource | BioimageioYamlContent, *, - format_version: Union[FormatVersionPlaceholder, str] = DISCOVER, - weight_format: Optional[SupportedWeightsFormat] = None, - devices: Optional[Sequence[str]] = None, + format_version: FormatVersionPlaceholder | str = DISCOVER, + weight_format: SupportedWeightsFormat | None = None, + devices: Sequence[str] | None = None, determinism: Literal["seed_only", "full"] = "seed_only", expected_type: Literal["dataset"], - sha256: Optional[Sha256] = None, + sha256: Sha256 | None = None, stop_early: bool = False, - working_dir: Optional[Union[os.PathLike[str], str]] = None, + working_dir: os.PathLike[str] | str | None = None, **deprecated: Unpack[DeprecatedKwargs], -) -> Union[AnyDatasetDescr, InvalidDescr]: ... +) -> AnyDatasetDescr | InvalidDescr: ... @overload def load_description_and_test( - source: Union[ResourceDescr, PermissiveFileSource, BioimageioYamlContent], + source: ResourceDescr | PermissiveFileSource | BioimageioYamlContent, *, - format_version: Union[FormatVersionPlaceholder, str] = DISCOVER, - weight_format: Optional[SupportedWeightsFormat] = None, - devices: Optional[Sequence[str]] = None, + format_version: FormatVersionPlaceholder | str = DISCOVER, + weight_format: SupportedWeightsFormat | None = None, + devices: Sequence[str] | None = None, determinism: Literal["seed_only", "full"] = "seed_only", - expected_type: Optional[str] = None, - sha256: Optional[Sha256] = None, + expected_type: str | None = None, + sha256: Sha256 | None = None, stop_early: bool = False, - working_dir: Optional[Union[os.PathLike[str], str]] = None, + working_dir: os.PathLike[str] | str | None = None, **deprecated: Unpack[DeprecatedKwargs], -) -> Union[ResourceDescr, InvalidDescr]: ... +) -> ResourceDescr | InvalidDescr: ... def load_description_and_test( - source: Union[ResourceDescr, PermissiveFileSource, BioimageioYamlContent], + source: ResourceDescr | PermissiveFileSource | BioimageioYamlContent, *, - format_version: Union[FormatVersionPlaceholder, str] = DISCOVER, - weight_format: Optional[SupportedWeightsFormat] = None, - devices: Optional[Sequence[str]] = None, + format_version: FormatVersionPlaceholder | str = DISCOVER, + weight_format: SupportedWeightsFormat | None = None, + devices: Sequence[str] | None = None, determinism: Literal["seed_only", "full"] = "seed_only", - expected_type: Optional[str] = None, - sha256: Optional[Sha256] = None, + expected_type: str | None = None, + sha256: Sha256 | None = None, stop_early: bool = False, - working_dir: Optional[Union[os.PathLike[str], str]] = None, + working_dir: os.PathLike[str] | str | None = None, **deprecated: Unpack[DeprecatedKwargs], -) -> Union[ResourceDescr, InvalidDescr]: +) -> ResourceDescr | InvalidDescr: """Test a bioimage.io resource dynamically, for example run prediction of test tensors for models. @@ -747,7 +741,7 @@ def load_description_and_test( if isinstance(rd, (v0_4.ModelDescr, v0_5.ModelDescr)): if weight_format is None: - weight_formats: List[SupportedWeightsFormat] = [ + weight_formats: list[SupportedWeightsFormat] = [ w for w, we in rd.weights if we is not None ] # pyright: ignore[reportAssignmentType] else: @@ -782,11 +776,11 @@ def load_description_and_test( def _get_tolerance( - model: Union[v0_4.ModelDescr, v0_5.ModelDescr], + model: v0_4.ModelDescr | v0_5.ModelDescr, wf: SupportedWeightsFormat, m: MemberId, **deprecated: Unpack[DeprecatedKwargs], -) -> Tuple[RelativeTolerance, AbsoluteTolerance, MismatchedElementsPerMillion]: +) -> tuple[RelativeTolerance, AbsoluteTolerance, MismatchedElementsPerMillion]: if isinstance(model, v0_5.ModelDescr): applicable = v0_5.ReproducibilityTolerance() @@ -836,7 +830,7 @@ def _get_tolerance( def evaluate_mismatched_elements( actual: Tensor, expected: Tensor, rtol: float, atol: float, name: str -) -> Tuple[float, str, Optional[str]]: +) -> tuple[float, str, str | None]: try: expected_np = expected.data.to_numpy().astype(np.float32) dims = expected.dims @@ -899,19 +893,19 @@ def evaluate_mismatched_elements( def _test_recreate_test_outputs( - model: Union[v0_4.ModelDescr, v0_5.ModelDescr], + model: v0_4.ModelDescr | v0_5.ModelDescr, weight_format: SupportedWeightsFormat, - devices: Optional[Sequence[str]], + devices: Sequence[str] | None, stop_early: bool, *, - working_dir: Optional[Union[os.PathLike[str], str]], + working_dir: os.PathLike[str] | str | None, verbose: bool, **deprecated: Unpack[DeprecatedKwargs], ) -> bool: test_name = f"Reproduce test outputs from test inputs ({weight_format})" logger.debug("starting '{}'", test_name) - error_entries: List[ErrorEntry] = [] - warning_entries: List[WarningEntry] = [] + error_entries: list[ErrorEntry] = [] + warning_entries: list[WarningEntry] = [] def add_error_entry(msg: str, with_traceback: bool = False): error_entries.append( @@ -933,8 +927,8 @@ def add_warning_entry(msg: str, severity: WarningSeverity): ) ) - def save_to_working_dir(name: str, tensor: Tensor) -> List[Path]: - saved_paths: List[Path] = [] + def save_to_working_dir(name: str, tensor: Tensor) -> list[Path]: + saved_paths: list[Path] = [] if working_dir is not None and verbose: for p in [ Path(working_dir) / f"{name}_{weight_format}{suffix}" @@ -980,7 +974,7 @@ def save_to_working_dir(name: str, tensor: Tensor) -> List[Path]: ) else: - intermediate_paths: List[Path] = [] + intermediate_paths: list[Path] = [] for m, t in test_input_preprocessed.members.items(): intermediate_paths.extend( save_to_working_dir(f"test_input_preprocessed_{m}", t) @@ -1053,7 +1047,7 @@ def save_to_working_dir(name: str, tensor: Tensor) -> List[Path]: except Exception as e: if get_validation_context().raise_errors: - raise e + raise add_error_entry(str(e), with_traceback=True) @@ -1073,7 +1067,7 @@ def save_to_working_dir(name: str, tensor: Tensor) -> List[Path]: def _test_parametrized_inference( model: v0_5.ModelDescr, weight_format: SupportedWeightsFormat, - devices: Optional[Sequence[str]], + devices: Sequence[str] | None, *, stop_early: bool, ) -> None: @@ -1083,7 +1077,7 @@ def _test_parametrized_inference( for a in ipt.axes ): # no parameterized sizes => set n=0 - ns: Set[v0_5.ParameterizedSize_N] = {0} + ns: set[v0_5.ParameterizedSize_N] = {0} else: ns = {0, 1, 2} @@ -1102,7 +1096,7 @@ def _test_parametrized_inference( # no batch axis batch_sizes = {1} - test_cases: Set[Tuple[BatchSize, v0_5.ParameterizedSize_N]] = { + test_cases: set[tuple[BatchSize, v0_5.ParameterizedSize_N]] = { (b, n) for b, n in product(sorted(batch_sizes), sorted(ns)) } logger.info( @@ -1113,7 +1107,7 @@ def _test_parametrized_inference( ) def generate_test_cases(): - tested: Set[Hashable] = set() + tested: set[Hashable] = set() def get_ns(n: int): return { @@ -1168,7 +1162,7 @@ def get_ns(n: int): bioimageio_model=model, devices=devices, weight_format=weight_format ) as prediction_pipeline: for n, batch_size, inputs, exptected_output_shape in generate_test_cases(): - error: Optional[str] = None + error: str | None = None try: result = prediction_pipeline.predict_sample_without_blocking( inputs, skip_input_padding=True, skip_output_cropping=True @@ -1189,7 +1183,7 @@ def get_ns(n: int): error = "Output tensors may not be None for test case" break - diff: Dict[AxisId, int] = {} + diff: dict[AxisId, int] = {} for a, s in res.sizes.items(): if isinstance((e_aid := exp[AxisId(a)]), int): if s != e_aid: @@ -1230,7 +1224,7 @@ def get_ns(n: int): break except Exception as e: if get_validation_context().raise_errors: - raise e + raise model.validation_summary.add_detail( ValidationDetail( @@ -1250,7 +1244,7 @@ def get_ns(n: int): def _test_expected_resource_type( - rd: Union[InvalidDescr, ResourceDescr], expected_type: str + rd: InvalidDescr | ResourceDescr, expected_type: str ): has_expected_type = rd.type is expected_type rd.validation_summary.details.append( diff --git a/src/bioimageio/core/_restore_batch_multi_index.py b/src/bioimageio/core/_restore_batch_multi_index.py index 8d05b1fe..d1e3380c 100644 --- a/src/bioimageio/core/_restore_batch_multi_index.py +++ b/src/bioimageio/core/_restore_batch_multi_index.py @@ -1,4 +1,4 @@ -from typing import Optional +from __future__ import annotations import pandas as pd @@ -8,8 +8,8 @@ def restore_batch_multi_index( - inputs: PerMember[Optional[Tensor]], outputs: PerMember[Optional[Tensor]] -) -> PerMember[Optional[Tensor]]: + inputs: PerMember[Tensor | None], outputs: PerMember[Tensor | None] +) -> PerMember[Tensor | None]: """Restore the first batch multi-index found in the inputs to all outputs with batch dimension.""" for tensor in inputs.values(): if tensor is None: diff --git a/src/bioimageio/core/_sample_serializer.py b/src/bioimageio/core/_sample_serializer.py index a25f80ca..2269e419 100644 --- a/src/bioimageio/core/_sample_serializer.py +++ b/src/bioimageio/core/_sample_serializer.py @@ -1,10 +1,10 @@ +from __future__ import annotations + from abc import ABC, abstractmethod from collections.abc import Iterable from typing import ( Generic, - Tuple, TypeVar, - Union, ) from bioimageio.spec.model import v0_5 @@ -22,7 +22,7 @@ class SampleSerializer(ABC, Generic[SerializedSampleBlockType]): def serialize_sample( cls, sample: Sample, - ) -> Tuple[SerializedSampleBlockType]: + ) -> tuple[SerializedSampleBlockType]: """Serialize a sample as a single block""" return (cls.serialize_sample_block(sample.as_single_block()),) @@ -62,7 +62,7 @@ def serialize_sample_with_fixed_blocking( *, block_shapes: PerMember[PerAxis[int]], halo: PerMember[PerAxis[HaloLike]], - pad_mode: Union[PadMode, PerMember[PadMode]] = "symmetric", + pad_mode: PadMode | PerMember[PadMode] = "symmetric", ) -> Iterable[SerializedSampleBlockType]: _n_blocks, input_blocks = sample.split_into_blocks( diff --git a/src/bioimageio/core/_settings.py b/src/bioimageio/core/_settings.py index 589e6d3a..4f6917f9 100644 --- a/src/bioimageio/core/_settings.py +++ b/src/bioimageio/core/_settings.py @@ -1,6 +1,8 @@ +from __future__ import annotations + import os import platform -from typing import Annotated, Literal, Optional +from typing import Annotated, Literal from loguru import logger from pydantic import Field, field_validator @@ -16,12 +18,12 @@ class Settings(SpecSettings): ] = "torch" pytorch_enable_mps_fallback: Annotated[ - Optional[bool], Field(alias="PYTORCH_ENABLE_MPS_FALLBACK") + bool | None, Field(alias="PYTORCH_ENABLE_MPS_FALLBACK") ] = None @field_validator("pytorch_enable_mps_fallback", mode="after") @classmethod - def _set_default_mps_fallback(cls, value: Optional[bool]): + def _set_default_mps_fallback(cls, value: bool | None): # pytorch versions up to the 2.6 don't support all operations (esp 3d) on MPS # this env variable allows falling back to CPU for those networks instead of failing # see for current status https://github.com/pytorch/pytorch/issues/141287 @@ -44,7 +46,7 @@ def _set_default_mps_fallback(cls, value: Optional[bool]): ) """URL to the bioimageio collection config""" - gradio_server: Optional[str] = None + gradio_server: str | None = None """URL or Hugging Face space name to connect to with the remote gradio model adapter or remote gradio prediction pipeline. Example: "bioimage-io/bioimage-io-gradio-server" diff --git a/src/bioimageio/core/axis.py b/src/bioimageio/core/axis.py index 071c69b2..10619c42 100644 --- a/src/bioimageio/core/axis.py +++ b/src/bioimageio/core/axis.py @@ -4,7 +4,6 @@ from dataclasses import dataclass from typing import ( Literal, - Optional, TypeVar, Union, ) @@ -29,9 +28,7 @@ def _guess_axis_type(a: str): raise ValueError( f"Failed to infer axis type for axis id '{a}'." + " Consider using one of: '" - + "', '".join( - ["b", "batch", "t", "time", "i", "index", "c", "channel", "x", "y", "z"] - ) + + "b', 'batch', 't', 'time', 'i', 'index', 'c', 'channel', 'x', 'y', 'z" + "'. Or creating an `Axis` object instead." ) @@ -97,8 +94,8 @@ def create(cls, axis: AxisLike) -> Axis: @dataclass class AxisSize: min: int - max: Optional[int] = None - step: Optional[int] = None + max: int | None = None + step: int | None = None @dataclass @@ -107,7 +104,7 @@ class AxisInfo(Axis): @classmethod def create( - cls, axis: AxisLike, size: Optional[Union[int, AxisSize]] = None + cls, axis: AxisLike, size: int | AxisSize | None = None ) -> AxisInfo: if isinstance(axis, AxisInfo): return axis diff --git a/src/bioimageio/core/backends/__init__.py b/src/bioimageio/core/backends/__init__.py index a687f07a..a8c44e01 100644 --- a/src/bioimageio/core/backends/__init__.py +++ b/src/bioimageio/core/backends/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from collections.abc import Sequence from typing import ( List, @@ -15,7 +17,7 @@ # Known weight formats in order of priority # First match wins -DEFAULT_WEIGHT_FORMAT_PRIORITY_ORDER: Tuple[SupportedWeightsFormat, ...] = ( +DEFAULT_WEIGHT_FORMAT_PRIORITY_ORDER: tuple[SupportedWeightsFormat, ...] = ( "pytorch_state_dict", "tensorflow_saved_model_bundle", "torchscript", @@ -26,10 +28,10 @@ def create_model_adapter( - model_description: Union[v0_4.ModelDescr, v0_5.ModelDescr], + model_description: v0_4.ModelDescr | v0_5.ModelDescr, *, - devices: Optional[Sequence[str]] = None, - weight_format_priority_order: Optional[Sequence[SupportedWeightsFormat]] = None, + devices: Sequence[str] | None = None, + weight_format_priority_order: Sequence[SupportedWeightsFormat] | None = None, ): """Creates model adapter for `model_descritption`""" if not isinstance(model_description, (v0_4.ModelDescr, v0_5.ModelDescr)): @@ -38,7 +40,7 @@ def create_model_adapter( ) weights = model_description.weights - errors: List[Exception] = [] + errors: list[Exception] = [] weight_format_priority_order = ( DEFAULT_WEIGHT_FORMAT_PRIORITY_ORDER if weight_format_priority_order is None diff --git a/src/bioimageio/core/backends/keras_backend.py b/src/bioimageio/core/backends/keras_backend.py index 1d02e0d7..991d592a 100644 --- a/src/bioimageio/core/backends/keras_backend.py +++ b/src/bioimageio/core/backends/keras_backend.py @@ -1,9 +1,11 @@ +from __future__ import annotations + import os import shutil from collections.abc import Sequence from pathlib import Path from tempfile import TemporaryDirectory -from typing import Any, Optional, Tuple +from typing import Any from keras.src.legacy.saving import ( # pyright: ignore[reportMissingTypeStubs] legacy_h5_format, @@ -36,7 +38,7 @@ class KerasModelAdapter(LocalModelAdapter[None, Any]): - def _parse_devices(self, devices: Optional[Sequence[str]]) -> Tuple[None]: + def _parse_devices(self, devices: Sequence[str] | None) -> tuple[None]: # TODO keras device management if devices is not None: logger.warning( @@ -100,7 +102,7 @@ def _forward_impl( self, device: None, model: Any, - input_arrays: Sequence[Optional[NDArray[Any]]], + input_arrays: Sequence[NDArray[Any] | None], ): network_output = model.predict(*input_arrays) if is_list(network_output) or is_tuple(network_output): diff --git a/src/bioimageio/core/backends/onnx_backend.py b/src/bioimageio/core/backends/onnx_backend.py index 7d9e269d..9ccf2493 100644 --- a/src/bioimageio/core/backends/onnx_backend.py +++ b/src/bioimageio/core/backends/onnx_backend.py @@ -1,10 +1,12 @@ # pyright: reportUnknownVariableType=false +from __future__ import annotations + import shutil import tempfile from collections.abc import Sequence from contextlib import contextmanager, nullcontext from pathlib import Path -from typing import Any, List, Optional, Union, cast +from typing import Any, Optional, cast import onnxruntime as rt # pyright: ignore[reportMissingTypeStubs] from loguru import logger @@ -19,20 +21,20 @@ class ONNXModelAdapter(LocalModelAdapter[Optional[str], rt.InferenceSession]): def __init__( self, - model_description: Union[v0_4.ModelDescr, v0_5.ModelDescr], - devices: Optional[Sequence[str]] = None, + model_description: v0_4.ModelDescr | v0_5.ModelDescr, + devices: Sequence[str] | None = None, ): onnx_descr = model_description.weights.onnx if onnx_descr is None: raise ValueError("No ONNX weights specified for {model_description.name}") self._onnx_descr = onnx_descr - self._input_names: Optional[List[str]] = None + self._input_names: list[str] | None = None super().__init__(model_description=model_description, devices=devices) def _parse_devices( - self, devices: Optional[Sequence[str]] - ) -> Sequence[Optional[str]]: + self, devices: Sequence[str] | None + ) -> Sequence[str | None]: available_providers: Any = None if hasattr(rt, "get_available_providers"): available_providers = cast(Any, rt.get_available_providers()) @@ -70,7 +72,7 @@ def _parse_devices( ) return providers - def _init_model_on_device(self, device: Optional[str]) -> rt.InferenceSession: + def _init_model_on_device(self, device: str | None) -> rt.InferenceSession: onnx_descr = self._onnx_descr if ( isinstance(onnx_descr, v0_5.OnnxWeightsDescr) @@ -136,10 +138,10 @@ def source_context_func(): def _forward_impl( self, - device: Optional[str], + device: str | None, model: rt.InferenceSession, - input_arrays: Sequence[Optional[NDArray[Any]]], - ) -> List[Optional[NDArray[Any]]]: + input_arrays: Sequence[NDArray[Any] | None], + ) -> list[NDArray[Any] | None]: assert self._input_names is not None, "set during model initialization" result: Any = model.run(None, dict(zip(self._input_names, input_arrays))) if is_list(result) or is_tuple(result): @@ -150,9 +152,9 @@ def _forward_impl( return result_seq def _cleanup_pre_model_deletion( - self, device: Optional[str], model: rt.InferenceSession + self, device: str | None, model: rt.InferenceSession ) -> None: return - def _cleanup_post_model_deletion(self, device: Optional[str]) -> None: + def _cleanup_post_model_deletion(self, device: str | None) -> None: return diff --git a/src/bioimageio/core/backends/pytorch_backend.py b/src/bioimageio/core/backends/pytorch_backend.py index 97df6a43..3323f784 100644 --- a/src/bioimageio/core/backends/pytorch_backend.py +++ b/src/bioimageio/core/backends/pytorch_backend.py @@ -1,10 +1,12 @@ +from __future__ import annotations + import gc from abc import abstractmethod from collections.abc import Mapping, Sequence from contextlib import nullcontext from io import BytesIO, TextIOWrapper from pathlib import Path -from typing import Any, List, Literal, Optional, Tuple, Union +from typing import Any, Literal import torch from loguru import logger @@ -33,15 +35,15 @@ def load_state_dict( def to( self, *, - device: Optional[torch.device] = None, - dtype: Optional[torch.dtype] = None, + device: torch.device | None = None, + dtype: torch.dtype | None = None, non_blocking: bool = False, ) -> Self: ... @abstractmethod def forward( self, *input: torch.Tensor - ) -> Union[torch.Tensor, Tuple[torch.Tensor, ...], List[torch.Tensor]]: ... + ) -> torch.Tensor | tuple[torch.Tensor, ...] | list[torch.Tensor]: ... def eval(self) -> Self: """Set model to eval mode""" @@ -53,7 +55,7 @@ def __init__( self, model_description: AnyModelDescr, mode: Literal["eval", "train"] = "eval", - devices: Optional[Sequence[str]] = None, + devices: Sequence[str] | None = None, ): weights = model_description.weights.pytorch_state_dict if weights is None: @@ -64,7 +66,7 @@ def __init__( super().__init__(model_description=model_description, devices=devices) def _parse_devices( - self, devices: Optional[Sequence[str]] + self, devices: Sequence[str] | None ) -> Sequence[torch.device]: return get_devices(devices) @@ -84,8 +86,8 @@ def _forward_impl( self, device: torch.device, model: nn.Module, - input_arrays: Sequence[Optional[NDArray[Any]]], - ) -> List[Optional[NDArray[Any]]]: + input_arrays: Sequence[NDArray[Any] | None], + ) -> list[NDArray[Any] | None]: tensors = [ None if a is None else torch.from_numpy(a).to(device) for a in input_arrays ] @@ -105,7 +107,7 @@ def _forward_impl( else: model_out_seq = model_out = [model_out] - result: List[Optional[NDArray[Any]]] = [] + result: list[NDArray[Any] | None] = [] for i, r in enumerate(model_out_seq): if r is None: result.append(None) @@ -133,12 +135,10 @@ def _cleanup_post_model_deletion(self, device: torch.device) -> None: def load_torch_model( - weight_spec: Union[ - v0_4.PytorchStateDictWeightsDescr, v0_5.PytorchStateDictWeightsDescr - ], + weight_spec: v0_4.PytorchStateDictWeightsDescr | v0_5.PytorchStateDictWeightsDescr, *, load_state: bool = True, - devices: Optional[Sequence[Union[str, torch.device]]] = None, + devices: Sequence[str | torch.device] | None = None, ) -> nn.Module: custom_callable = import_callable( weight_spec.architecture, @@ -183,7 +183,7 @@ def load_torch_model( def load_torch_state_dict( model: nn.Module, - path: Union[Path, ZipPath, BytesReader], + path: Path | ZipPath | BytesReader, devices: Sequence[torch.device], strict: bool = True, ) -> nn.Module: @@ -241,8 +241,8 @@ def load_torch_state_dict( def get_devices( - devices: Optional[Sequence[Union[torch.device, str]]] = None, -) -> List[torch.device]: + devices: Sequence[torch.device | str] | None = None, +) -> list[torch.device]: if not devices: if torch.cuda.is_available(): torch_devices = [ diff --git a/src/bioimageio/core/backends/tensorflow_backend.py b/src/bioimageio/core/backends/tensorflow_backend.py index da4e677c..374b99e1 100644 --- a/src/bioimageio/core/backends/tensorflow_backend.py +++ b/src/bioimageio/core/backends/tensorflow_backend.py @@ -1,6 +1,8 @@ +from __future__ import annotations + from collections.abc import Sequence from pathlib import Path -from typing import Any, List, Optional, Tuple, Union +from typing import Any import numpy as np import tensorflow as tf @@ -20,8 +22,8 @@ class TensorflowModelAdapter(LocalModelAdapter[None, Any]): def __init__( self, - model_description: Union[v0_4.ModelDescr, v0_5.ModelDescr], - devices: Optional[Sequence[str]] = None, + model_description: v0_4.ModelDescr | v0_5.ModelDescr, + devices: Sequence[str] | None = None, ): if model_description.weights.tensorflow_saved_model_bundle is None: @@ -35,17 +37,17 @@ def __init__( self._weight_src = model_description.weights.tensorflow_saved_model_bundle self._graph = None - self._io_names: Optional[Tuple[List[str], List[str]]] = None + self._io_names: tuple[list[str], list[str]] | None = None super().__init__(model_description=model_description, devices=devices) - def _parse_devices(self, devices: Optional[Sequence[str]]) -> Tuple[None]: + def _parse_devices(self, devices: Sequence[str] | None) -> tuple[None]: if devices is not None: logger.warning( f"Device management is not implemented for tensorflow yet, ignoring the devices {devices}" ) return (None,) - def _init_model_on_device(self, device: Optional[str]) -> Any: + def _init_model_on_device(self, device: str | None) -> Any: # TODO: check how to load tf weights without unzipping weight_file = ensure_unzipped( @@ -83,7 +85,7 @@ def _init_model_on_device(self, device: Optional[str]) -> Any: return sess # pyright: ignore[reportUnknownVariableType] def _forward_impl( - self, device: None, model: Any, input_arrays: Sequence[Optional[NDArray[Any]]] + self, device: None, model: Any, input_arrays: Sequence[NDArray[Any] | None] ): assert self._io_names is not None assert self._graph is not None @@ -102,18 +104,18 @@ def _forward_impl( return res - def _cleanup_pre_model_deletion(self, device: Optional[str], model: Any) -> None: + def _cleanup_pre_model_deletion(self, device: str | None, model: Any) -> None: return - def _cleanup_post_model_deletion(self, device: Optional[str]) -> None: + def _cleanup_post_model_deletion(self, device: str | None) -> None: return class KerasModelAdapter(LocalModelAdapter[None, Any]): def __init__( self, - model_description: Union[v0_4.ModelDescr, v0_5.ModelDescr], - devices: Optional[Sequence[str]] = None, + model_description: v0_4.ModelDescr | v0_5.ModelDescr, + devices: Sequence[str] | None = None, ): if model_description.weights.tensorflow_saved_model_bundle is None: raise ValueError("No `tensorflow_saved_model_bundle` weights found") @@ -127,7 +129,7 @@ def __init__( super().__init__(model_description=model_description, devices=devices) - def _parse_devices(self, devices: Optional[Sequence[str]]) -> Tuple[None]: + def _parse_devices(self, devices: Sequence[str] | None) -> tuple[None]: if devices is not None: logger.warning( f"Device management is not implemented for tensorflow yet, ignoring the devices {devices}" @@ -159,7 +161,7 @@ def _init_model_on_device(self, device: None) -> Any: return tfsm_layer # pyright: ignore[reportUnknownVariableType] def _forward_impl( # pyright: ignore[reportUnknownParameterType] - self, device: None, model: Any, input_arrays: Sequence[Optional[NDArray[Any]]] + self, device: None, model: Any, input_arrays: Sequence[NDArray[Any] | None] ): assert tf is not None tf_tensor = [ @@ -178,15 +180,15 @@ def _forward_impl( # pyright: ignore[reportUnknownParameterType] for r in result # pyright: ignore[reportUnknownVariableType] ] - def _cleanup_pre_model_deletion(self, device: Optional[str], model: Any) -> None: + def _cleanup_pre_model_deletion(self, device: str | None, model: Any) -> None: return - def _cleanup_post_model_deletion(self, device: Optional[str]) -> None: + def _cleanup_post_model_deletion(self, device: str | None) -> None: return def create_tf_model_adapter( - model_description: AnyModelDescr, devices: Optional[Sequence[str]] = None + model_description: AnyModelDescr, devices: Sequence[str] | None = None ): tf_version = v0_5.Version(tf.__version__) # type: ignore[reportUnknownVariableType] weights = model_description.weights.tensorflow_saved_model_bundle diff --git a/src/bioimageio/core/backends/torchscript_backend.py b/src/bioimageio/core/backends/torchscript_backend.py index 67eec433..ca7b4620 100644 --- a/src/bioimageio/core/backends/torchscript_backend.py +++ b/src/bioimageio/core/backends/torchscript_backend.py @@ -1,7 +1,9 @@ # pyright: reportUnknownVariableType=false +from __future__ import annotations + import gc from collections.abc import Sequence -from typing import Any, List, Optional, Union +from typing import Any import torch from loguru import logger @@ -17,8 +19,8 @@ class TorchscriptModelAdapter(LocalModelAdapter[torch.device, Any]): def __init__( self, - model_description: Union[v0_4.ModelDescr, v0_5.ModelDescr], - devices: Optional[Sequence[str]] = None, + model_description: v0_4.ModelDescr | v0_5.ModelDescr, + devices: Sequence[str] | None = None, ): if model_description.weights.torchscript is None: raise ValueError( @@ -29,7 +31,7 @@ def __init__( super().__init__(model_description=model_description, devices=devices) def _parse_devices( - self, devices: Optional[Sequence[str]] + self, devices: Sequence[str] | None ) -> Sequence[torch.device]: return get_devices(devices) @@ -47,8 +49,8 @@ def _forward_impl( self, device: torch.device, model: Any, - input_arrays: Sequence[Optional[NDArray[Any]]], - ) -> List[Optional[NDArray[Any]]]: + input_arrays: Sequence[NDArray[Any] | None], + ) -> list[NDArray[Any] | None]: with torch.no_grad(): torch_tensor = [ None if a is None else torch.from_numpy(a).to(device) diff --git a/src/bioimageio/core/block.py b/src/bioimageio/core/block.py index fb573560..b3c3936e 100644 --- a/src/bioimageio/core/block.py +++ b/src/bioimageio/core/block.py @@ -1,10 +1,9 @@ +from __future__ import annotations + from collections.abc import Generator, Iterable from dataclasses import dataclass from typing import ( Any, - Optional, - Tuple, - Union, ) from typing_extensions import Self @@ -61,7 +60,7 @@ def from_sample_member( ) def get_transformed( - self, new_axes: PerAxis[Union[LinearAxisTransform, int]] + self, new_axes: PerAxis[LinearAxisTransform | int] ) -> Self: raise NotImplementedError @@ -101,9 +100,9 @@ def split_tensor_into_blocks( block_shape: PerAxis[int], *, halo: PerAxis[HaloLike], - stride: Optional[PerAxis[int]] = None, + stride: PerAxis[int] | None = None, pad_mode: PadMode, -) -> Tuple[TotalNumberOfBlocks, Generator[Block, Any, None]]: +) -> tuple[TotalNumberOfBlocks, Generator[Block, Any, None]]: """divide a sample tensor into tensor blocks.""" n_blocks, block_gen = split_shape_into_blocks( tensor.tagged_shape, block_shape=block_shape, halo=halo, stride=stride diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py index 8e512803..c9328e33 100644 --- a/src/bioimageio/core/block_meta.py +++ b/src/bioimageio/core/block_meta.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import itertools from collections.abc import Collection, Generator, Iterable from dataclasses import dataclass @@ -7,11 +9,6 @@ from typing import ( Any, Callable, - Dict, - List, - Optional, - Tuple, - Union, ) import pydantic @@ -209,7 +206,7 @@ def __post_init__(self): ) def get_transformed( - self, new_axes: PerAxis[Union[LinearAxisTransform, int]] + self, new_axes: PerAxis[LinearAxisTransform | int] ) -> Self: return self.__class__( sample_shape={ @@ -248,8 +245,8 @@ def split_shape_into_blocks( shape: PerAxis[int], block_shape: PerAxis[int], halo: PerAxis[HaloLike], - stride: Optional[PerAxis[int]] = None, -) -> Tuple[TotalNumberOfBlocks, Generator[BlockMeta, Any, None]]: + stride: PerAxis[int] | None = None, +) -> tuple[TotalNumberOfBlocks, Generator[BlockMeta, Any, None]]: unknown_axes = [a for a in block_shape if a not in shape] if unknown_axes: raise ValueError( @@ -268,7 +265,7 @@ def split_shape_into_blocks( if stride is None: stride = {} - inner_1d_slices: Dict[AxisId, List[SliceInfo]] = {} + inner_1d_slices: dict[AxisId, list[SliceInfo]] = {} for a, s in shape.items(): inner_size = block_shape[a] - sum(halo[a]) stride_1d = stride.get(a, inner_size) @@ -291,7 +288,7 @@ def _block_meta_generator( sample_shape: PerAxis[int], *, blocks_in_sample: int, - inner_1d_slices: Dict[AxisId, List[SliceInfo]], + inner_1d_slices: dict[AxisId, list[SliceInfo]], halo: PerAxis[HaloLike], ): assert all(a in sample_shape for a in halo) @@ -314,9 +311,9 @@ def split_multiple_shapes_into_blocks( block_shapes: PerMember[PerAxis[int]], *, halo: PerMember[PerAxis[HaloLike]], - strides: Optional[PerMember[PerAxis[int]]] = None, + strides: PerMember[PerAxis[int]] | None = None, broadcast: bool = False, -) -> Tuple[TotalNumberOfBlocks, Iterable[PerMember[BlockMeta]]]: +) -> tuple[TotalNumberOfBlocks, Iterable[PerMember[BlockMeta]]]: if unknown_blocks := [t for t in block_shapes if t not in shapes]: raise ValueError( f"block shape specified for unknown tensors: {unknown_blocks}." @@ -346,8 +343,8 @@ def split_multiple_shapes_into_blocks( f"`stride` specified for tensors without block shape: {unknown_block}" ) - blocks: Dict[MemberId, Iterable[BlockMeta]] = {} - n_blocks: Dict[MemberId, TotalNumberOfBlocks] = {} + blocks: dict[MemberId, Iterable[BlockMeta]] = {} + n_blocks: dict[MemberId, TotalNumberOfBlocks] = {} for t in block_shapes: n_blocks[t], blocks[t] = split_shape_into_blocks( shape=shapes[t], @@ -380,7 +377,7 @@ def split_multiple_shapes_into_blocks( def _aligned_blocks_generator( - n: TotalNumberOfBlocks, blocks: Dict[MemberId, Iterable[BlockMeta]] + n: TotalNumberOfBlocks, blocks: dict[MemberId, Iterable[BlockMeta]] ): iterators = {t: iter(gen) for t, gen in blocks.items()} for _ in range(n): diff --git a/src/bioimageio/core/cli.py b/src/bioimageio/core/cli.py index 7d08dbd6..8782c9ce 100644 --- a/src/bioimageio/core/cli.py +++ b/src/bioimageio/core/cli.py @@ -3,6 +3,7 @@ Note: Some docstrings use a hair space ' ' to place the added '(default: ...)' on a new line. """ +from __future__ import annotations import json import shutil @@ -19,14 +20,7 @@ from typing import ( Annotated, Any, - Dict, - List, Literal, - Optional, - Set, - Tuple, - Type, - Union, ) import numpy as np @@ -115,7 +109,7 @@ class ArgMixin(BaseModel, use_attribute_docstrings=True, cli_implicit_flags=True class WithSummaryLogging(ArgMixin): - summary: List[Union[Literal["display"], Path]] = Field( + summary: list[Literal["display"] | Path] = Field( default_factory=lambda: ["display"], examples=[ Path("summary.md"), @@ -129,7 +123,7 @@ class WithSummaryLogging(ArgMixin): Choose/add `"display"` to render the validation summary to the terminal. """ - def log(self, descr: Union[ResourceDescr, InvalidDescr]): + def log(self, descr: ResourceDescr | InvalidDescr): _ = descr.validation_summary.log(self.summary) @@ -196,12 +190,12 @@ class TestCmd(CmdBase, WithSource, WithSummaryLogging): (only relevant for model resources)""" - devices: Optional[List[str]] = Field( + devices: list[str] | None = Field( None, validation_alias=AliasChoices("devices", "device") ) """Device(s) to use""" - runtime_env: Union[Literal["currently-active", "as-described"], Path] = Field( + runtime_env: Literal["currently-active", "as-described"] | Path = Field( "currently-active", alias="runtime-env" ) """The python environment to run the tests in @@ -212,7 +206,7 @@ class TestCmd(CmdBase, WithSource, WithSummaryLogging): Note: The `bioimageio.core` dependency will be added automatically if not present. """ - working_dir: Optional[Path] = Field(None, alias="working-dir") + working_dir: Path | None = Field(None, alias="working-dir") """(for debugging) Directory to save any temporary files.""" determinism: Literal["seed_only", "full"] = "seed_only" @@ -223,7 +217,7 @@ class TestCmd(CmdBase, WithSource, WithSummaryLogging): ) """Do not run further subtests after a failed one.""" - format_version: Union[FormatVersionPlaceholder, str] = Field( + format_version: FormatVersionPlaceholder | str = Field( "discover", alias="format-version" ) """The format version to use for testing. @@ -304,13 +298,13 @@ def _get_stat( ): stats_calc.update(sample) - stat: Dict[Measure, MeasureValue] = {k: v for k, v in stats_calc.finalize().items()} + stat: dict[Measure, MeasureValue] = {k: v for k, v in stats_calc.finalize().items()} save_stat(stat, stats_path) return stat class UpdateCmdBase(CmdBase, WithSource, ABC): - output: Union[Literal["display", "stdout"], Path] = "display" + output: Literal["display", "stdout"] | Path = "display" """Output updated bioimageio.yaml to the terminal or write to a file. Notes: - `"display"`: Render to the terminal with syntax highlighting. @@ -318,7 +312,7 @@ class UpdateCmdBase(CmdBase, WithSource, ABC): (More convenient for copying the updated bioimageio.yaml from the terminal.) """ - diff: Union[bool, Path] = Field(True, alias="diff") + diff: bool | Path = Field(True, alias="diff") """Output a diff of original and updated bioimageio.yaml. If a given path has an `.html` extension, a standalone HTML file is written, otherwise the diff is saved in unified diff format (pure text). @@ -331,7 +325,7 @@ class UpdateCmdBase(CmdBase, WithSource, ABC): """Exclude fields that have the default value (even if set explicitly).""" @cached_property - def updated(self) -> Union[ResourceDescr, InvalidDescr]: + def updated(self) -> ResourceDescr | InvalidDescr: raise NotImplementedError def cli_cmd(self): @@ -434,7 +428,7 @@ class PredictCmd(CmdBase, WithSource): def descr(self): return load_description(self.source, perform_io_checks=False) - inputs: NotEmpty[List[Union[str, NotEmpty[List[str]]]]] = Field( + inputs: NotEmpty[list[str | NotEmpty[list[str]]]] = Field( default_factory=lambda: ["{input_id}/001.tif"], validation_alias=AliasChoices("inputs", "input"), ) @@ -470,7 +464,7 @@ def descr(self):   """ - outputs: Union[str, NotEmpty[Tuple[str, ...]]] = Field( + outputs: str | NotEmpty[tuple[str, ...]] = Field( "outputs_{model_id}/{output_id}/{sample_id}.tif", validation_alias=AliasChoices("outputs", "output"), ) @@ -486,7 +480,7 @@ def descr(self): overwrite: bool = False """allow overwriting existing output files""" - blockwise: Union[bool, int] = False + blockwise: bool | int = False """Process inputs blockwise - If an integer is given, it is used as the blocksize parameter 'n' for blockwise processing. @@ -518,12 +512,12 @@ def descr(self): ) """The weight format to use.""" - devices: Optional[List[str]] = Field( + devices: list[str] | None = Field( None, validation_alias=AliasChoices("devices", "device") ) """Device(s) to use""" - server: Optional[str] = None + server: str | None = None """The URL or Hugging Face space name of a running bioimageio (gradio) server instance to use as a remote backend for prediction.""" pre_post_processing_location: Literal["local", "remote"] = Field( @@ -564,7 +558,7 @@ def _example(self): if not example_inputs: raise ValueError(f"{self.descr_id} does not specify any example inputs.") - inputs001: List[str] = [] + inputs001: list[str] = [] example_path = Path(f"{self.descr_id}_example") example_path.mkdir(exist_ok=True) @@ -582,12 +576,12 @@ def _example(self): bioimageio_cli_path = example_path / YAML_FILE stats_file = "precomputed_statistics.json" stats = (example_path / stats_file).as_posix() - cli_example_args = dict( - inputs=inputs, - outputs=output_pattern, - stats=stats_file, - blockwise=self.blockwise, - ) + cli_example_args = { + "inputs": inputs, + "outputs": output_pattern, + "stats": stats_file, + "blockwise": self.blockwise, + } assert is_yaml_value(cli_example_args), cli_example_args write_yaml( cli_example_args, @@ -654,9 +648,9 @@ def cli_cmd(self): f"Prediction failed ({e}).\nConsider using blockwise processing, " + "e.g. with `--blockwise=10` to process inputs in blocks." ) from e - raise e + raise - def _yield_predictions(self, blockwise: Union[bool, int]): + def _yield_predictions(self, blockwise: bool | int): if self.example: return self._example() @@ -675,7 +669,7 @@ def _yield_predictions(self, blockwise: Union[bool, int]): for ipt in model_descr.inputs ) - def expand_inputs(i: int, ipt: Union[str, Sequence[str]]) -> Tuple[str, ...]: + def expand_inputs(i: int, ipt: str | Sequence[str]) -> tuple[str, ...]: if isinstance(ipt, str): ipts = tuple( ipt.format(model_id=self.descr_id, input_id=t) for t in input_ids @@ -810,7 +804,7 @@ def input_dataset(stat: Stat): sample_id=s, ) - stat: Dict[Measure, MeasureValue] = dict( + stat: dict[Measure, MeasureValue] = dict( _get_stat( model_descr, input_dataset({}), len(sample_ids), self.stats ).items() @@ -878,7 +872,7 @@ class PredictBlockArtifactsCmd(PredictCmd): - Normalization layers inside the network cannot aggregate statistics over the whole sample. """ - blockwise: Union[Literal[True], int] = 1 + blockwise: Literal[True] | int = 1 """Process inputs blockwise - If an integer is given, it is used as the blocksize parameter 'n' for blockwise processing. @@ -925,10 +919,10 @@ class AddWeightsCmd(CmdBase, WithSource, WithSummaryLogging): output: CliPositionalArg[Path] """The path to write the updated model package to.""" - source_format: Optional[SupportedWeightsFormat] = Field(None, alias="source-format") + source_format: SupportedWeightsFormat | None = Field(None, alias="source-format") """Exclusively use these weights to convert to other formats.""" - target_format: Optional[SupportedWeightsFormat] = Field(None, alias="target-format") + target_format: SupportedWeightsFormat | None = Field(None, alias="target-format") """Exclusively add this weight format.""" verbose: bool = False @@ -969,7 +963,7 @@ class ServerCmd(CmdBase): backend: Literal["gradio"] = "gradio" """The remote backend to use.""" - port: Optional[int] = None + port: int | None = None """The port to start the server on. If not given, a free port will be used.""" def cli_cmd(self) -> None: @@ -1044,12 +1038,12 @@ class Bioimageio( @classmethod def settings_customise_sources( cls, - settings_cls: Type[BaseSettings], + settings_cls: type[BaseSettings], init_settings: PydanticBaseSettingsSource, env_settings: PydanticBaseSettingsSource, dotenv_settings: PydanticBaseSettingsSource, file_secret_settings: PydanticBaseSettingsSource, - ) -> Tuple[PydanticBaseSettingsSource, ...]: + ) -> tuple[PydanticBaseSettingsSource, ...]: cli: CliSettingsSource[BaseSettings] = CliSettingsSource( settings_cls, cli_parse_args=True, @@ -1128,7 +1122,7 @@ def get_common_seq(seqs: Sequence[Sequence[str]]) -> Sequence[str]: return common - def get_shorter_diff(seqs: Sequence[Sequence[str]]) -> List[Sequence[str]]: + def get_shorter_diff(seqs: Sequence[Sequence[str]]) -> list[Sequence[str]]: """get a shorter sequence whose entries are still unique (order sensitive, not minimal sequence) """ @@ -1140,7 +1134,7 @@ def get_shorter_diff(seqs: Sequence[Sequence[str]]) -> List[Sequence[str]]: min_seq_len -= start break else: - seen: Set[Sequence[str]] = set() + seen: set[Sequence[str]] = set() dupes = [s for s in seqs if s in seen or seen.add(s)] raise ValueError(f"Found duplicate entries {dupes}") diff --git a/src/bioimageio/core/commands.py b/src/bioimageio/core/commands.py index ea2fade1..cac8e2ad 100644 --- a/src/bioimageio/core/commands.py +++ b/src/bioimageio/core/commands.py @@ -1,9 +1,9 @@ """These functions are used in the bioimageio command line interface defined in `bioimageio.core.cli`.""" +from __future__ import annotations from collections.abc import Sequence from pathlib import Path -from typing import Optional, Union from typing_extensions import Literal @@ -41,19 +41,15 @@ def test( - descr: Union[ResourceDescr, InvalidDescr], + descr: ResourceDescr | InvalidDescr, *, weight_format: WeightFormatArgAll = "all", - devices: Optional[Union[str, Sequence[str]]] = None, - summary: Union[ - Literal["display"], Path, Sequence[Union[Literal["display"], Path]] - ] = "display", - runtime_env: Union[ - Literal["currently-active", "as-described"], Path - ] = "currently-active", + devices: str | Sequence[str] | None = None, + summary: Literal["display"] | Path | Sequence[Literal["display"] | Path] = "display", + runtime_env: Literal["currently-active", "as-described"] | Path = "currently-active", determinism: Literal["seed_only", "full"] = "seed_only", - format_version: Union[FormatVersionPlaceholder, str] = "discover", - working_dir: Optional[Path] = None, + format_version: FormatVersionPlaceholder | str = "discover", + working_dir: Path | None = None, ) -> int: """Test a bioimageio resource. @@ -77,8 +73,8 @@ def test( def validate_format( - descr: Union[ResourceDescr, InvalidDescr], - summary: Union[Path, Sequence[Path]] = (), + descr: ResourceDescr | InvalidDescr, + summary: Path | Sequence[Path] = (), ): """DEPRECATED; Access the existing `validation_summary` attribute instead. validate the meta data format of a bioimageio resource diff --git a/src/bioimageio/core/common.py b/src/bioimageio/core/common.py index eb45f98d..dd31ca74 100644 --- a/src/bioimageio/core/common.py +++ b/src/bioimageio/core/common.py @@ -4,7 +4,6 @@ from typing import ( Literal, NamedTuple, - Tuple, TypeVar, Union, ) @@ -63,7 +62,7 @@ _LeftRight_T = TypeVar("_LeftRight_T", bound="_LeftRight") -_LeftRightLike = Union[int, Tuple[int, int], _LeftRight_T] +_LeftRightLike = Union[int, tuple[int, int], _LeftRight_T] class _LeftRight(NamedTuple): @@ -126,7 +125,7 @@ class SliceInfo(NamedTuple): BlocksizeParameter: TypeAlias = Union[ v0_5.ParameterizedSize_N, - Mapping[Tuple[MemberId, v0_5.AxisId], v0_5.ParameterizedSize_N], + Mapping[tuple[MemberId, v0_5.AxisId], v0_5.ParameterizedSize_N], ] """ Parameter to determine a concrete size for paramtrized axis sizes defined by diff --git a/src/bioimageio/core/digest_spec.py b/src/bioimageio/core/digest_spec.py index 7101bd77..e8881bf5 100644 --- a/src/bioimageio/core/digest_spec.py +++ b/src/bioimageio/core/digest_spec.py @@ -10,12 +10,8 @@ from typing import ( Any, Callable, - Dict, - List, Literal, NamedTuple, - Optional, - Tuple, Union, ) from zipfile import ZipFile, is_zipfile @@ -57,13 +53,7 @@ def import_callable( - node: Union[ - ArchitectureFromFileDescr, - ArchitectureFromLibraryDescr, - CallableFromDepencency, - CallableFromFile, - v0_5.CustomProcessingDescr, - ], + node: ArchitectureFromFileDescr | ArchitectureFromLibraryDescr | CallableFromDepencency | CallableFromFile | v0_5.CustomProcessingDescr, /, **kwargs: Unpack[HashKwargs], ) -> Callable[..., Any]: @@ -87,7 +77,7 @@ def import_callable( return c -tmp_dirs_in_use: List[TemporaryDirectory[str]] = [] +tmp_dirs_in_use: list[TemporaryDirectory[str]] = [] """keep global reference to temporary directories created during import to delay cleanup""" @@ -115,8 +105,8 @@ def _import_from_file_impl( module = sys.modules.get(module_name) if module is None: try: - td_kwargs: Dict[str, Any] = ( - dict(ignore_cleanup_errors=True) if sys.version_info >= (3, 10) else {} + td_kwargs: dict[str, Any] = ( + {"ignore_cleanup_errors": True} if sys.version_info >= (3, 10) else {} ) if sys.version_info >= (3, 12): td_kwargs["delete"] = False @@ -170,24 +160,14 @@ def _import_from_file_impl( def get_axes_infos( - io_descr: Union[ - v0_4.InputTensorDescr, - v0_4.OutputTensorDescr, - v0_5.InputTensorDescr, - v0_5.OutputTensorDescr, - ], -) -> List[AxisInfo]: + io_descr: v0_4.InputTensorDescr | v0_4.OutputTensorDescr | v0_5.InputTensorDescr | v0_5.OutputTensorDescr, +) -> list[AxisInfo]: """get a unified, simplified axis representation from spec axes""" return [AxisInfo.create(a) for a in io_descr.axes] def get_member_id( - tensor_description: Union[ - v0_4.InputTensorDescr, - v0_4.OutputTensorDescr, - v0_5.InputTensorDescr, - v0_5.OutputTensorDescr, - ], + tensor_description: v0_4.InputTensorDescr | v0_4.OutputTensorDescr | v0_5.InputTensorDescr | v0_5.OutputTensorDescr, ) -> MemberId: """get the normalized tensor ID, usable as a sample member ID""" @@ -203,14 +183,9 @@ def get_member_id( def get_member_ids( tensor_descriptions: Iterable[ - Union[ - v0_4.InputTensorDescr, - v0_4.OutputTensorDescr, - v0_5.InputTensorDescr, - v0_5.OutputTensorDescr, - ] + v0_4.InputTensorDescr | v0_4.OutputTensorDescr | v0_5.InputTensorDescr | v0_5.OutputTensorDescr ], -) -> List[MemberId]: +) -> list[MemberId]: """get normalized tensor IDs to be used as sample member IDs""" return [get_member_id(descr) for descr in tensor_descriptions] @@ -247,25 +222,9 @@ def get_test_output_sample(model: AnyModelDescr) -> Sample: def _get_test_sample( - info: Union[ - Mapping[MemberId, Union[v0_5.InputTensorDescr, v0_5.OutputTensorDescr]], - Mapping[ - MemberId, - Tuple[ - v0_4.InputTensorDescr, - FileSource, - ], - ], - Mapping[ - MemberId, - Tuple[ - v0_4.OutputTensorDescr, - FileSource, - ], - ], - ], + info: Mapping[MemberId, v0_5.InputTensorDescr | v0_5.OutputTensorDescr] | Mapping[MemberId, tuple[v0_4.InputTensorDescr, FileSource]] | Mapping[MemberId, tuple[v0_4.OutputTensorDescr, FileSource]], ) -> Sample: - arrays: Dict[MemberId, NDArray[Any]] = {} + arrays: dict[MemberId, NDArray[Any]] = {} for m, src in info.items(): if isinstance(src, tuple): arrays[m] = load_array(src[1]) @@ -294,11 +253,11 @@ class IO_SampleBlockMeta(NamedTuple): def get_input_halo( - model: v0_5.ModelDescr, output_halo: Optional[PerMember[PerAxis[Halo]]] = None + model: v0_5.ModelDescr, output_halo: PerMember[PerAxis[Halo]] | None = None ): """returns which halo input tensors need to be divided into blocks with, such that `output_halo` can be cropped from their outputs without introducing gaps.""" - input_halo: Dict[MemberId, Dict[AxisId, Halo]] = {} + input_halo: dict[MemberId, dict[AxisId, Halo]] = {} outputs = {t.id: t for t in model.outputs} all_tensors = {**{t.id: t for t in model.inputs}, **outputs} if output_halo is None: @@ -340,9 +299,9 @@ def get_input_halo( def get_block_transform( model: v0_5.ModelDescr, -) -> PerMember[PerAxis[Union[LinearSampleAxisTransform, int]]]: +) -> PerMember[PerAxis[LinearSampleAxisTransform | int]]: """returns how a model's output tensor shapes relates to its input shapes""" - ret: Dict[MemberId, Dict[AxisId, Union[LinearSampleAxisTransform, int]]] = {} + ret: dict[MemberId, dict[AxisId, LinearSampleAxisTransform | int]] = {} batch_axis_trf = None for ipt in model.inputs: for a in ipt.axes: @@ -358,7 +317,7 @@ def get_block_transform( for t in chain(model.inputs, model.outputs) } for out in model.outputs: - new_axes: Dict[AxisId, Union[LinearSampleAxisTransform, int]] = {} + new_axes: dict[AxisId, LinearSampleAxisTransform | int] = {} for a in out.axes: if a.size is None: assert a.type == "batch" @@ -405,9 +364,9 @@ def get_block_transform( def get_io_sample_block_metas( model: v0_5.ModelDescr, input_sample_shape: PerMember[PerAxis[int]], - ns: Mapping[Tuple[MemberId, AxisId], ParameterizedSize_N], + ns: Mapping[tuple[MemberId, AxisId], ParameterizedSize_N], batch_size: int = 1, -) -> Tuple[TotalNumberOfBlocks, Iterable[IO_SampleBlockMeta]]: +) -> tuple[TotalNumberOfBlocks, Iterable[IO_SampleBlockMeta]]: """returns an iterable yielding meta data for corresponding input and output samples""" if not isinstance(model, v0_5.ModelDescr): raise TypeError(f"get_block_meta() not implemented for {type(model)}") @@ -439,13 +398,7 @@ def get_io_sample_block_metas( def get_tensor( src: TensorSource, - descr: Union[ - v0_4.InputTensorDescr, - v0_5.InputTensorDescr, - v0_4.OutputTensorDescr, - v0_5.OutputTensorDescr, - Sequence[AxisInfo], - ], + descr: v0_4.InputTensorDescr | v0_5.InputTensorDescr | v0_4.OutputTensorDescr | v0_5.OutputTensorDescr | Sequence[AxisInfo], *, extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "squeeze", missing_dims: Literal["raise", "expand", "unstack", "unstack_or_expand"] = "raise", @@ -496,9 +449,9 @@ def get_tensor( def create_sample_for_model( model: AnyModelDescr, *, - stat: Optional[Stat] = None, + stat: Stat | None = None, sample_id: SampleId = None, - inputs: Union[PerMember[TensorSource], TensorSource], + inputs: PerMember[TensorSource] | TensorSource, extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "stack", ) -> Sample: """Create a sample from a single set of input(s) for a specific bioimage.io model @@ -518,7 +471,7 @@ def create_sample_for_model( if isinstance(inputs, collections.abc.Mapping): inputs = {MemberId(k): v for k, v in inputs.items()} elif len(model_inputs) == 1: - inputs = {list(model_inputs)[0]: inputs} + inputs = {next(iter(model_inputs)): inputs} else: raise TypeError( f"Expected `inputs` to be a mapping with keys {tuple(model_inputs)}" @@ -549,9 +502,9 @@ def load_sample_for_model( *, model: AnyModelDescr, paths: PerMember[PermissiveFileSource], - axes: Optional[PerMember[Sequence[AxisLike]]] = None, - stat: Optional[Stat] = None, - sample_id: Optional[SampleId] = None, + axes: PerMember[Sequence[AxisLike]] | None = None, + stat: Stat | None = None, + sample_id: SampleId | None = None, ): """load a single sample from `paths` that can be processed by `model`""" @@ -570,7 +523,7 @@ def load_sample_for_model( if unknown := {k for k in axes if k not in model_inputs}: raise ValueError(f"Got unexpected axes hints for: {unknown}") - members: Dict[MemberId, Tensor] = {} + members: dict[MemberId, Tensor] = {} for m, p in paths.items(): if m not in axes: axes[m] = get_axes_infos(model_inputs[m]) @@ -594,7 +547,7 @@ def split_sample_into_blocks_for_model( model: v0_5.ModelDescr, blocksize_parameter: int, batch_size: int = 1, -) -> Tuple[TotalNumberOfBlocks, Iterable[SampleBlockWithOrigin]]: +) -> tuple[TotalNumberOfBlocks, Iterable[SampleBlockWithOrigin]]: if isinstance(model, v0_4.ModelDescr): raise NotImplementedError( "`predict_sample_with_blocking` not implemented for v0_4.ModelDescr" @@ -634,7 +587,7 @@ def transpose_sample_for_model( for m in axes: if m not in sample.members and not ( isinstance(model, v0_5.ModelDescr) - and [d for d in model.inputs if get_member_id(d) == m][0].optional + and next(d for d in model.inputs if get_member_id(d) == m).optional ): raise ValueError( f"Sample is missing non-optional member {m} required by model" diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index f89da242..02110516 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import collections.abc import json import warnings @@ -9,10 +11,6 @@ from shutil import copyfileobj from typing import ( TYPE_CHECKING, - Dict, - List, - Optional, - TypeVar, Union, ) @@ -27,12 +25,10 @@ from bioimageio.spec.common import ( BytesReader, FileSource, - HttpUrl, PermissiveFileSource, ZipPath, ) -from bioimageio.spec.utils import load_image as load_image -from bioimageio.spec.utils import save_array +from bioimageio.spec.utils import load_image, save_array from .axis import AxisId, AxisLike from .common import PerMember @@ -42,7 +38,7 @@ if TYPE_CHECKING: JsonValue: TypeAlias = Union[ - bool, int, float, str, None, List["JsonValue"], Dict[str, "JsonValue"] + bool, int, float, str, None, list["JsonValue"], dict[str, "JsonValue"] ] # note: order relevant for deserializing else: @@ -51,14 +47,14 @@ # however this results in a partially unknown type with the current pyright 1.1.388 JsonValue: TypeAlias = _TypeAliasType( "JsonValue", - Union[bool, int, float, str, None, List["JsonValue"], Dict[str, "JsonValue"]], + Union[bool, int, float, str, None, list["JsonValue"], dict[str, "JsonValue"]], ) def load_tensor( - source: Union[PermissiveFileSource, ZipPath], + source: PermissiveFileSource | ZipPath, /, - axes: Optional[Sequence[AxisLike]] = None, + axes: Sequence[AxisLike] | None = None, ) -> Tensor: # TODO: load axis meta data array = load_image(source) @@ -66,12 +62,11 @@ def load_tensor( return Tensor.from_array(array, dims=axes) -_SourceT = TypeVar("_SourceT", Path, HttpUrl, ZipPath) Suffix = str -def save_tensor(path: Union[Path, str], tensor: Tensor) -> None: +def save_tensor(path: Path | str, tensor: Tensor) -> None: # TODO: save axis meta data path = Path(path) @@ -85,7 +80,7 @@ def save_tensor(path: Union[Path, str], tensor: Tensor) -> None: elif extension in (".h5", ".hdf", ".hdf5"): raise NotImplementedError("Saving to h5 with dataset path is not implemented.") else: - removed_singleton_axes: List[AxisId] = [] + removed_singleton_axes: list[AxisId] = [] remove_singletons = { AxisId("batch"): [ ".tif", @@ -130,7 +125,7 @@ def save_tensor(path: Union[Path, str], tensor: Tensor) -> None: def save_sample( - path: Union[Path, str, PerMember[Union[Path, str]]], sample: Sample + path: Path | str | PerMember[Path | str], sample: Sample ) -> None: """Save a **sample** to a **path** pattern or all sample members in the **path** mapping. @@ -161,26 +156,26 @@ def save_sample( class _StatEntry(BaseModel, frozen=True, arbitrary_types_allowed=True): """Serializable stat entry""" - measure: Union[DatasetMeasure, SampleMeasure] + measure: DatasetMeasure | SampleMeasure value: MeasureValue -class _StatList(RootModel[List[_StatEntry]]): +class _StatList(RootModel[list[_StatEntry]]): """Serializable stat mapping""" def serialize_stat( - stat: Mapping[Union[DatasetMeasure, SampleMeasure], MeasureValue], -) -> List[JsonValue]: + stat: Mapping[DatasetMeasure | SampleMeasure, MeasureValue], +) -> list[JsonValue]: """Serialize a stat mapping to a JSON string""" stat_list = _StatList([_StatEntry(measure=k, value=v) for k, v in stat.items()]) return stat_list.model_dump(mode="json") def save_stat( - stat: Mapping[Union[DatasetMeasure, SampleMeasure], MeasureValue], - output: Union[Path, BytesIO], + stat: Mapping[DatasetMeasure | SampleMeasure, MeasureValue], + output: Path | BytesIO, ) -> None: """Save sample and dataset statistics as a JSON file""" @@ -193,7 +188,7 @@ def save_stat( _ = out.write(json.dumps(serialize_stat(stat), indent=2).encode("utf-8")) -def load_stat(source: Union[Path, str, Sequence[JsonValue]]) -> Stat: +def load_stat(source: Path | str | Sequence[JsonValue]) -> Stat: """Load sample and dataset statistics from JSON""" if isinstance(source, Path): source = source.read_text(encoding="utf-8") @@ -219,7 +214,7 @@ def load_dataset_stat(path: Path) -> Stat: def ensure_unzipped( - source: Union[PermissiveFileSource, ZipPath, BytesReader], folder: Path + source: PermissiveFileSource | ZipPath | BytesReader, folder: Path ): """unzip a (downloaded) **source** to a file in **folder** if source is a zip archive otherwise copy **source** to a file in **folder**.""" @@ -247,6 +242,6 @@ def ensure_unzipped( return out_path -def get_suffix(source: Union[ZipPath, FileSource]) -> Suffix: +def get_suffix(source: ZipPath | FileSource) -> Suffix: """DEPRECATED: use source.suffix instead.""" return source.suffix diff --git a/src/bioimageio/core/prediction.py b/src/bioimageio/core/prediction.py index f3098d46..0998c449 100644 --- a/src/bioimageio/core/prediction.py +++ b/src/bioimageio/core/prediction.py @@ -1,11 +1,8 @@ +from __future__ import annotations + import collections.abc from collections.abc import Hashable, Iterable, Iterator, Mapping from pathlib import Path -from typing import ( - Optional, - Tuple, - Union, -) from loguru import logger from tqdm import tqdm @@ -24,16 +21,14 @@ def predict( *, - model: Union[ - PermissiveFileSource, v0_4.ModelDescr, v0_5.ModelDescr, PredictionPipeline - ], - inputs: Union[Sample, PerMember[TensorSource], TensorSource], + model: PermissiveFileSource | v0_4.ModelDescr | v0_5.ModelDescr | PredictionPipeline, + inputs: Sample | PerMember[TensorSource] | TensorSource, sample_id: Hashable = "sample", - blocksize_parameter: Optional[BlocksizeParameter] = None, - input_block_shape: Optional[Mapping[MemberId, Mapping[AxisId, int]]] = None, + blocksize_parameter: BlocksizeParameter | None = None, + input_block_shape: Mapping[MemberId, Mapping[AxisId, int]] | None = None, skip_preprocessing: bool = False, skip_postprocessing: bool = False, - save_output_path: Optional[Union[Path, str]] = None, + save_output_path: Path | str | None = None, ) -> Sample: """Run prediction for a single set of input(s) with a bioimage.io model @@ -77,17 +72,16 @@ def predict( with pp: model = pp.model_descr - if save_output_path is not None: - if ( - "{output_id}" not in str(save_output_path) - and "{member_id}" not in str(save_output_path) - and len(model.outputs) > 1 - ): - raise ValueError( - f"Missing `{{output_id}}` in save_output_path={save_output_path} to " - + "distinguish model outputs " - + str([get_member_id(d) for d in model.outputs]) - ) + if save_output_path is not None and ( + "{output_id}" not in str(save_output_path) + and "{member_id}" not in str(save_output_path) + and len(model.outputs) > 1 + ): + raise ValueError( + f"Missing `{{output_id}}` in save_output_path={save_output_path} to " + + "distinguish model outputs " + + str([get_member_id(d) for d in model.outputs]) + ) if isinstance(inputs, Sample): sample = inputs @@ -131,20 +125,13 @@ def predict( def predict_many( *, - model: Union[ - PermissiveFileSource, v0_4.ModelDescr, v0_5.ModelDescr, PredictionPipeline - ], - inputs: Union[Iterable[PerMember[TensorSource]], Iterable[TensorSource]], + model: PermissiveFileSource | v0_4.ModelDescr | v0_5.ModelDescr | PredictionPipeline, + inputs: Iterable[PerMember[TensorSource]] | Iterable[TensorSource], sample_id: str = "sample{i:03}", - blocksize_parameter: Optional[ - Union[ - v0_5.ParameterizedSize_N, - Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N], - ] - ] = None, + blocksize_parameter: v0_5.ParameterizedSize_N | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] | None = None, skip_preprocessing: bool = False, skip_postprocessing: bool = False, - save_output_path: Optional[Union[Path, str]] = None, + save_output_path: Path | str | None = None, ) -> Iterator[Sample]: """Run prediction for a multiple sets of inputs with a bioimage.io model diff --git a/src/bioimageio/core/proc_ops.py b/src/bioimageio/core/proc_ops.py index b7c29825..ff4a1545 100644 --- a/src/bioimageio/core/proc_ops.py +++ b/src/bioimageio/core/proc_ops.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import collections.abc from collections.abc import Collection, Mapping, Sequence from dataclasses import InitVar, dataclass, field @@ -6,9 +8,6 @@ Any, Callable, Literal, - Optional, - Set, - Tuple, Union, ) @@ -25,8 +24,7 @@ from ._op_base import BlockwiseOperator, SamplewiseOperator, SimpleOperator from ._ops_cellpose import CellposeFlowDynamics -from ._ops_stardist import StardistPostprocessing2D as StardistPostprocessing2D -from ._ops_stardist import StardistPostprocessing3D as StardistPostprocessing3D +from ._ops_stardist import StardistPostprocessing2D, StardistPostprocessing3D from .axis import AxisId from .common import DTypeStr, MemberId from .digest_spec import get_member_id, import_callable @@ -51,7 +49,7 @@ def _convert_axis_ids( axes: v0_4.AxesInCZYX, mode: Literal["per_sample", "per_dataset"], -) -> Tuple[AxisId, ...]: +) -> tuple[AxisId, ...]: if not isinstance(axes, str): return tuple(axes) @@ -80,7 +78,7 @@ def __post_init__(self): def required_measures(self) -> Collection[Measure]: return set() - def __call__(self, sample: Union[Sample, SampleBlock]) -> None: + def __call__(self, sample: Sample | SampleBlock) -> None: sample.stat.update(self.dataset_stats) @@ -154,8 +152,8 @@ def __call__(self, sample: Sample) -> None: class Binarize(SimpleOperator): """'output = tensor > threshold'.""" - threshold: Union[float, Sequence[float]] - axis: Optional[AxisId] = None + threshold: float | Sequence[float] + axis: AxisId | None = None def _apply(self, x: Tensor, stat: Stat) -> Tensor: return x > self.threshold @@ -171,7 +169,7 @@ def get_output_shape( @classmethod def from_proc_descr( - cls, descr: Union[v0_4.BinarizeDescr, v0_5.BinarizeDescr], member_id: MemberId + cls, descr: v0_4.BinarizeDescr | v0_5.BinarizeDescr, member_id: MemberId ) -> Self: if isinstance(descr.kwargs, (v0_4.BinarizeKwargs, v0_5.BinarizeKwargs)): return cls( @@ -190,9 +188,9 @@ def from_proc_descr( @dataclass class Clip(SimpleOperator): - min: Optional[Union[float, SampleQuantile, DatasetQuantile]] = None + min: float | SampleQuantile | DatasetQuantile | None = None """minimum value for clipping""" - max: Optional[Union[float, SampleQuantile, DatasetQuantile]] = None + max: float | SampleQuantile | DatasetQuantile | None = None """maximum value for clipping""" def __post_init__(self): @@ -267,7 +265,7 @@ def get_output_shape( @classmethod def from_proc_descr( - cls, descr: Union[v0_4.ClipDescr, v0_5.ClipDescr], member_id: MemberId + cls, descr: v0_4.ClipDescr | v0_5.ClipDescr, member_id: MemberId ) -> Self: if isinstance(descr, v0_5.ClipDescr): dataset_mode, axes = _get_axes(descr.kwargs) @@ -338,10 +336,10 @@ def required_measures(self) -> Collection[Measure]: @dataclass class ScaleLinear(SimpleOperator): - gain: Union[float, xr.DataArray] = 1.0 + gain: float | xr.DataArray = 1.0 """multiplicative factor""" - offset: Union[float, xr.DataArray] = 0.0 + offset: float | xr.DataArray = 0.0 """additive term""" def _apply(self, x: Tensor, stat: Stat) -> Tensor: @@ -359,7 +357,7 @@ def get_output_shape( @classmethod def from_proc_descr( cls, - descr: Union[v0_4.ScaleLinearDescr, v0_5.ScaleLinearDescr], + descr: v0_4.ScaleLinearDescr | v0_5.ScaleLinearDescr, member_id: MemberId, ) -> Self: kwargs = descr.kwargs @@ -398,13 +396,13 @@ def from_proc_descr( @dataclass class ScaleMeanVariance(SimpleOperator): - axes: Optional[Sequence[AxisId]] = None - reference_tensor: Optional[MemberId] = None + axes: Sequence[AxisId] | None = None + reference_tensor: MemberId | None = None eps: float = 1e-6 - mean: Union[SampleMean, DatasetMean] = field(init=False) - std: Union[SampleStd, DatasetStd] = field(init=False) - ref_mean: Union[SampleMean, DatasetMean] = field(init=False) - ref_std: Union[SampleStd, DatasetStd] = field(init=False) + mean: SampleMean | DatasetMean = field(init=False) + std: SampleStd | DatasetStd = field(init=False) + ref_mean: SampleMean | DatasetMean = field(init=False) + ref_std: SampleStd | DatasetStd = field(init=False) @property def required_measures(self): @@ -440,7 +438,7 @@ def get_output_shape( @classmethod def from_proc_descr( cls, - descr: Union[v0_4.ScaleMeanVarianceDescr, v0_5.ScaleMeanVarianceDescr], + descr: v0_4.ScaleMeanVarianceDescr | v0_5.ScaleMeanVarianceDescr, member_id: MemberId, ) -> Self: kwargs = descr.kwargs @@ -456,16 +454,8 @@ def from_proc_descr( def _get_axes( - kwargs: Union[ - v0_4.ZeroMeanUnitVarianceKwargs, - v0_5.ZeroMeanUnitVarianceKwargs, - v0_4.ScaleRangeKwargs, - v0_5.ScaleRangeKwargs, - v0_4.ScaleMeanVarianceKwargs, - v0_5.ScaleMeanVarianceKwargs, - v0_5.ClipKwargs, - ], -) -> Tuple[bool, Optional[Tuple[AxisId, ...]]]: + kwargs: v0_4.ZeroMeanUnitVarianceKwargs | v0_5.ZeroMeanUnitVarianceKwargs | v0_4.ScaleRangeKwargs | v0_5.ScaleRangeKwargs | v0_4.ScaleMeanVarianceKwargs | v0_5.ScaleMeanVarianceKwargs | v0_5.ClipKwargs, +) -> tuple[bool, tuple[AxisId, ...] | None]: if kwargs.axes is None: return True, None elif isinstance(kwargs.axes, str): @@ -480,17 +470,17 @@ def _get_axes( @dataclass class ScaleRange(SimpleOperator): - lower_quantile: InitVar[Optional[Union[SampleQuantile, DatasetQuantile]]] = None - upper_quantile: InitVar[Optional[Union[SampleQuantile, DatasetQuantile]]] = None - lower: Union[SampleQuantile, DatasetQuantile] = field(init=False) - upper: Union[SampleQuantile, DatasetQuantile] = field(init=False) + lower_quantile: InitVar[SampleQuantile | DatasetQuantile | None] = None + upper_quantile: InitVar[SampleQuantile | DatasetQuantile | None] = None + lower: SampleQuantile | DatasetQuantile = field(init=False) + upper: SampleQuantile | DatasetQuantile = field(init=False) eps: float = 1e-6 def __post_init__( self, - lower_quantile: Optional[Union[SampleQuantile, DatasetQuantile]], - upper_quantile: Optional[Union[SampleQuantile, DatasetQuantile]], + lower_quantile: SampleQuantile | DatasetQuantile | None, + upper_quantile: SampleQuantile | DatasetQuantile | None, ): if lower_quantile is None: tid = self.input if upper_quantile is None else upper_quantile.member_id @@ -519,7 +509,7 @@ def get_output_shape( @classmethod def from_proc_descr( cls, - descr: Union[v0_4.ScaleRangeDescr, v0_5.ScaleRangeDescr], + descr: v0_4.ScaleRangeDescr | v0_5.ScaleRangeDescr, member_id: MemberId, ): kwargs = descr.kwargs @@ -589,7 +579,7 @@ def get_output_shape( @classmethod def from_proc_descr( - cls, descr: Union[v0_4.SigmoidDescr, v0_5.SigmoidDescr], member_id: MemberId + cls, descr: v0_4.SigmoidDescr | v0_5.SigmoidDescr, member_id: MemberId ) -> Self: assert isinstance(descr, (v0_4.SigmoidDescr, v0_5.SigmoidDescr)) return cls(input=member_id, output=member_id) @@ -641,7 +631,7 @@ def __post_init__(self): assert self.mean.axes == self.std.axes @property - def required_measures(self) -> Set[Union[MeanMeasure, StdMeasure]]: + def required_measures(self) -> set[MeanMeasure | StdMeasure]: return {self.mean, self.std} def get_output_shape( @@ -652,7 +642,7 @@ def get_output_shape( @classmethod def from_proc_descr( cls, - descr: Union[v0_4.ZeroMeanUnitVarianceDescr, v0_5.ZeroMeanUnitVarianceDescr], + descr: v0_4.ZeroMeanUnitVarianceDescr | v0_5.ZeroMeanUnitVarianceDescr, member_id: MemberId, ): dataset_mode, axes = _get_axes(descr.kwargs) @@ -686,8 +676,8 @@ def get_descr(self): class FixedZeroMeanUnitVariance(SimpleOperator): """normalize to zero mean, unit variance with precomputed values.""" - mean: Union[float, xr.DataArray] - std: Union[float, xr.DataArray] + mean: float | xr.DataArray + std: float | xr.DataArray eps: float = 1e-6 @@ -840,12 +830,7 @@ def from_proc_descr( def get_proc( proc_descr: ProcDescr, - tensor_descr: Union[ - v0_4.InputTensorDescr, - v0_4.OutputTensorDescr, - v0_5.InputTensorDescr, - v0_5.OutputTensorDescr, - ], + tensor_descr: v0_4.InputTensorDescr | v0_4.OutputTensorDescr | v0_5.InputTensorDescr | v0_5.OutputTensorDescr, ) -> Processing: member_id = get_member_id(tensor_descr) diff --git a/src/bioimageio/core/proc_setup.py b/src/bioimageio/core/proc_setup.py index 4a13ab15..ef8a43d7 100644 --- a/src/bioimageio/core/proc_setup.py +++ b/src/bioimageio/core/proc_setup.py @@ -1,11 +1,10 @@ +from __future__ import annotations + from collections.abc import Iterable, Mapping, Sequence from itertools import chain from typing import ( Callable, - List, NamedTuple, - Optional, - Set, Union, ) @@ -41,8 +40,8 @@ class PreAndPostprocessing(NamedTuple): - pre: List[Processing] - post: List[Processing] + pre: list[Processing] + post: list[Processing] class _ProcessingCallables(NamedTuple): @@ -65,7 +64,7 @@ def get_pre_and_postprocessing( *, dataset_for_initial_statistics: Iterable[Sample], keep_updating_initial_dataset_stats: bool = False, - fixed_dataset_stats: Optional[Mapping[Measure, MeasureValue]] = None, + fixed_dataset_stats: Mapping[Measure, MeasureValue] | None = None, ) -> _ProcessingCallables: """Creates callables to apply pre- and postprocessing in-place to a sample""" @@ -82,7 +81,7 @@ def setup_pre_and_postprocessing( model: AnyModelDescr, dataset_for_initial_statistics: Iterable[Sample], keep_updating_initial_dataset_stats: bool = False, - fixed_dataset_stats: Optional[Mapping[Measure, MeasureValue]] = None, + fixed_dataset_stats: Mapping[Measure, MeasureValue] | None = None, ) -> PreAndPostprocessing: """Get pre- and postprocessing operators for a `model` description. @@ -118,18 +117,18 @@ def setup_pre_and_postprocessing( class RequiredMeasures(NamedTuple): - pre: Set[Measure] - post: Set[Measure] + pre: set[Measure] + post: set[Measure] class RequiredDatasetMeasures(NamedTuple): - pre: Set[DatasetMeasure] - post: Set[DatasetMeasure] + pre: set[DatasetMeasure] + post: set[DatasetMeasure] class RequiredSampleMeasures(NamedTuple): - pre: Set[SampleMeasure] - post: Set[SampleMeasure] + pre: set[SampleMeasure] + post: set[SampleMeasure] def get_requried_measures(model: AnyModelDescr) -> RequiredMeasures: @@ -159,9 +158,9 @@ def get_requried_sample_measures(model: AnyModelDescr) -> RequiredSampleMeasures def _get_described_procs( tensor_descrs: Iterable[TensorDescr], -) -> List[Processing]: +) -> list[Processing]: tensor_descrs = list(tensor_descrs) - procs: List[Processing] = [] + procs: list[Processing] = [] for t_descr in tensor_descrs: if isinstance(t_descr, (v0_4.InputTensorDescr, v0_4.OutputTensorDescr)): member_id = get_member_id(t_descr) diff --git a/src/bioimageio/core/remote_backends/__init__.py b/src/bioimageio/core/remote_backends/__init__.py index 1e2be1d0..e6f52b31 100644 --- a/src/bioimageio/core/remote_backends/__init__.py +++ b/src/bioimageio/core/remote_backends/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import TYPE_CHECKING, Literal, Optional from typing_extensions import assert_never @@ -10,9 +12,9 @@ def create_remote_model_adapter( model_description: AnyModelDescr, - server: Optional[str] = None, - server_type: Optional[Literal["gradio"]] = None, -) -> "GradioModelAdapter": + server: str | None = None, + server_type: Literal["gradio"] | None = None, +) -> GradioModelAdapter: """Create a remote model adapter Args: diff --git a/src/bioimageio/core/remote_backends/gradio/client.py b/src/bioimageio/core/remote_backends/gradio/client.py index 1c4d8f90..4590511f 100644 --- a/src/bioimageio/core/remote_backends/gradio/client.py +++ b/src/bioimageio/core/remote_backends/gradio/client.py @@ -1,6 +1,8 @@ +from __future__ import annotations + from collections.abc import Iterable, Mapping from types import MappingProxyType -from typing import Dict, Literal, Optional, Tuple, Union +from typing import Literal from gradio_client import Client from loguru import logger @@ -8,7 +10,7 @@ from bioimageio.spec import AnyModelDescr, ValidationSummary from bioimageio.spec.model import v0_4 -from ..._description_serializer import DescriptionSerializer as DescriptionSerializer +from ..._description_serializer import DescriptionSerializer from ..._model_adapter import RemoteModelAdapter from ..._prediction_pipeline import IntermediatePrediction, RemotePredictionPipeline from ..._settings import settings @@ -19,14 +21,14 @@ from ...stat_measures import Measure, MeasureValue from .serializer import GradioSampleSerializer -SerializedSampleBlock = Dict[str, JsonValue] +SerializedSampleBlock = dict[str, JsonValue] class GradioModelAdapter(RemoteModelAdapter[SerializedSampleBlock]): """Model adapter to use the bioimage-io-gradio-runner as a backend for model inference.""" def __init__( - self, model_description: AnyModelDescr, *, server: Optional[str] = None + self, model_description: AnyModelDescr, *, server: str | None = None ): """Initialize the GradioModelAdapter. @@ -87,7 +89,7 @@ def load(self) -> None: if result: break - def test(self) -> Optional[ValidationSummary]: + def test(self) -> ValidationSummary | None: for model_data in ("", self._serialized_model): try: result = self._client.submit( @@ -114,7 +116,7 @@ def __init__( self, model_description: AnyModelDescr, *, - server: Optional[str] = None, + server: str | None = None, precomputed_statistics: Mapping[Measure, MeasureValue] = MappingProxyType({}), default_blocksize_parameter: BlocksizeParameter = 10, default_batch_size: int = 1, @@ -211,7 +213,7 @@ def _predict_sample_with_fixed_blocking_yield_intermediates_impl( skip_preprocessing: bool = False, skip_postprocessing: bool = False, fill_value: float = float("nan"), - ) -> Tuple[int, Iterable[IntermediatePrediction]]: + ) -> tuple[int, Iterable[IntermediatePrediction]]: sample.stat.update(self._precomputed_statistics) # blocking for serialization is not really important, but we might as well block @@ -264,14 +266,12 @@ def _call_predict_api( serialized_model: str, sha256: str, serialized_input_sample: Iterable[SerializedSampleBlock], - blocksize: Optional[ - Union[int, Literal["blockwise_as_serialized"], PerMember[PerAxis[int]]] - ], + blocksize: int | Literal["blockwise_as_serialized"] | PerMember[PerAxis[int]] | None, skip_preprocessing: bool, skip_postprocessing: bool, skip_input_padding: bool, skip_output_cropping: bool, - batch_size: Optional[int], + batch_size: int | None, ) -> Iterable[SerializedSampleBlock]: def submit(model: str): return client.submit( @@ -309,7 +309,7 @@ def submit(model: str): e, ) else: - raise e + raise if try_with_model_upload: job = submit(serialized_model) diff --git a/src/bioimageio/core/remote_backends/gradio/serializer.py b/src/bioimageio/core/remote_backends/gradio/serializer.py index 3f20acd4..d5b0966c 100644 --- a/src/bioimageio/core/remote_backends/gradio/serializer.py +++ b/src/bioimageio/core/remote_backends/gradio/serializer.py @@ -1,7 +1,8 @@ +from __future__ import annotations + import tempfile from collections.abc import Mapping from pathlib import Path -from typing import Dict, List, Union import numpy as np from gradio_client import handle_file @@ -9,7 +10,6 @@ from typing_extensions import Self from ..._common_annotations import PerMemberAnno -from ..._description_serializer import DescriptionSerializer as DescriptionSerializer from ..._sample_serializer import SampleSerializer from ...common import MemberId from ...io import JsonValue, load_stat, save_tensor, serialize_stat @@ -33,17 +33,17 @@ def from_tensor(cls, tensor: Tensor) -> Self: class _SerializableSampleBlock(BaseModel, frozen=True): meta: SampleBlockMeta - data: PerMemberAnno[Union[_SerializableBlock, Path]] - serialized_stat: List[JsonValue] + data: PerMemberAnno[_SerializableBlock | Path] + serialized_stat: list[JsonValue] -SerializedSampleBlock = Dict[str, JsonValue] +SerializedSampleBlock = dict[str, JsonValue] class GradioSampleSerializer(SampleSerializer[SerializedSampleBlock]): @staticmethod def serialize_sample_block(sample_block: SampleBlock) -> SerializedSampleBlock: - handled_members: Dict[MemberId, _SerializableBlock] = {} + handled_members: dict[MemberId, _SerializableBlock] = {} for m, t in sample_block.members.items(): handled_members[m] = _SerializableBlock.from_tensor(t) diff --git a/src/bioimageio/core/remote_backends/gradio/server.py b/src/bioimageio/core/remote_backends/gradio/server.py index 06c6a361..7755ddda 100644 --- a/src/bioimageio/core/remote_backends/gradio/server.py +++ b/src/bioimageio/core/remote_backends/gradio/server.py @@ -1,11 +1,10 @@ +from __future__ import annotations + from collections.abc import Iterable from itertools import chain from typing import ( Any, - Dict, Literal, - Optional, - Union, ) import gradio as gr @@ -47,14 +46,12 @@ def predict( model: str, sha256: str, input_sample: Iterable[SerializedSampleBlock], - blocksize: Optional[ - Union[int, Literal["blockwise_as_serialized"], PerMember[PerAxis[int]]] - ] = None, + blocksize: int | Literal["blockwise_as_serialized"] | PerMember[PerAxis[int]] | None = None, skip_preprocessing: bool = False, skip_postprocessing: bool = False, skip_input_padding: bool = False, skip_output_cropping: bool = False, - batch_size: Optional[int] = None, + batch_size: int | None = None, ) -> Iterable[SerializedSampleBlock]: """Run prediction on a sample @@ -187,7 +184,7 @@ def test_model( return summary.model_dump_json() -def _cache_key(kwargs: Dict[str, Any]) -> str: +def _cache_key(kwargs: dict[str, Any]) -> str: return kwargs["sha256"] @@ -238,7 +235,7 @@ def root(): } -def main(port: Optional[int] = None) -> str: +def main(port: int | None = None) -> str: _app, local_url, _share_url = app.launch( mcp_server=True, show_error=True, server_port=port ) diff --git a/src/bioimageio/core/sample.py b/src/bioimageio/core/sample.py index e9c4a580..2ac24f99 100644 --- a/src/bioimageio/core/sample.py +++ b/src/bioimageio/core/sample.py @@ -8,13 +8,9 @@ from typing import ( Any, Callable, - Dict, Generic, Literal, - Optional, - Tuple, TypeVar, - Union, ) import numpy as np @@ -60,7 +56,7 @@ class Sample: `MemberId("raw")` and `MemberId("mask")` image. """ - members: Dict[MemberId, Tensor] + members: dict[MemberId, Tensor] """The sample's tensors""" stat: Stat @@ -72,14 +68,7 @@ class Sample: def __getitem__( self, key: PerMember[ - Union[ - SliceInfo, - slice, - int, - PerAxis[Union[SliceInfo, slice, int]], - Tensor, - xr.DataArray, - ] + SliceInfo | slice | int | PerAxis[SliceInfo | slice | int] | Tensor | xr.DataArray ], ) -> Self: return self.__class__( @@ -89,7 +78,7 @@ def __getitem__( ) @property - def batch_multi_index(self) -> Optional[pd.MultiIndex]: + def batch_multi_index(self) -> pd.MultiIndex | None: """Return the batch multi-index of the sample, if it has one. Returns: @@ -132,7 +121,7 @@ def set_block(self, block: SampleBlock) -> None: def shape(self) -> PerMember[PerAxis[int]]: return {tid: t.sizes for tid, t in self.members.items()} - def as_arrays(self) -> Dict[MemberId, NDArray[Any]]: + def as_arrays(self) -> dict[MemberId, NDArray[Any]]: """Return sample as dictionary of arrays.""" return {m: t.to_numpy() for m, t in self.members.items()} @@ -140,9 +129,9 @@ def split_into_blocks( self, block_shapes: PerMember[PerAxis[int]], halo: PerMember[PerAxis[HaloLike]], - pad_mode: Union[PadMode, PerMember[PadMode]], + pad_mode: PadMode | PerMember[PadMode], broadcast: bool = False, - ) -> Tuple[TotalNumberOfBlocks, Iterable[SampleBlockWithOrigin]]: + ) -> tuple[TotalNumberOfBlocks, Iterable[SampleBlockWithOrigin]]: assert not (missing := [m for m in block_shapes if m not in self.members]), ( f"`block_shapes` specified for unknown members: {missing}" ) @@ -158,7 +147,7 @@ def split_into_blocks( ) return n_blocks, sample_block_generator(blocks, origin=self, pad_mode=pad_mode) - def as_single_block(self, halo: Optional[PerMember[PerAxis[Halo]]] = None): + def as_single_block(self, halo: PerMember[PerAxis[Halo]] | None = None): if halo is None: halo = {} return SampleBlockWithOrigin( @@ -257,8 +246,8 @@ def from_blocks_yield_intermediates( def pad( self, - pad_width: PerMember[PerAxis[Union[int, PadWidthLike]]], - mode: Union[PerMember[PadMode], PadMode], + pad_width: PerMember[PerAxis[int | PadWidthLike]], + mode: PerMember[PadMode] | PadMode, ) -> Self: """Convenience method to pad sample members.""" default_mode = "symmetric" @@ -447,7 +436,7 @@ class SampleBlockMeta(SampleBlockBase[BlockMeta]): """Meta data of a dataset sample block""" def get_transformed( - self, new_axes: PerMember[PerAxis[Union[LinearSampleAxisTransform, int]]] + self, new_axes: PerMember[PerAxis[LinearSampleAxisTransform | int]] ) -> Self: sample_shape = { m: { @@ -475,7 +464,7 @@ def get_member_halo(m: MemberId, round: Callable[[float], int]): for a, trf in new_axes[m].items() } - halo: Dict[MemberId, Dict[AxisId, Halo]] = {} + halo: dict[MemberId, dict[AxisId, Halo]] = {} for m in new_axes: halo[m] = get_member_halo(m, floor) if halo[m] != get_member_halo(m, ceil): @@ -540,7 +529,7 @@ def with_data(self, data: PerMember[Tensor], *, stat: Stat) -> SampleBlock: class SampleBlock(SampleBlockBase[Block]): """A block of a dataset sample""" - blocks: Dict[MemberId, Block] + blocks: dict[MemberId, Block] """Individual tensor blocks comprising this sample block""" stat: Stat @@ -552,7 +541,7 @@ def members(self) -> PerMember[Tensor]: return {m: b.data for m, b in self.blocks.items()} def get_transformed_meta( - self, new_axes: PerMember[PerAxis[Union[LinearSampleAxisTransform, int]]] + self, new_axes: PerMember[PerAxis[LinearSampleAxisTransform | int]] ) -> SampleBlockMeta: return SampleBlockMeta( sample_id=self.sample_id, @@ -640,7 +629,7 @@ def sample_block_generator( blocks: Iterable[PerMember[BlockMeta]], *, origin: Sample, - pad_mode: Union[PadMode, PerMember[PadMode]], + pad_mode: PadMode | PerMember[PadMode], ) -> Iterable[SampleBlockWithOrigin]: for member_blocks in blocks: cons = _ConsolidatedMemberBlocks(member_blocks) diff --git a/src/bioimageio/core/stat_calculators.py b/src/bioimageio/core/stat_calculators.py index 2408c552..18875ce7 100644 --- a/src/bioimageio/core/stat_calculators.py +++ b/src/bioimageio/core/stat_calculators.py @@ -7,12 +7,6 @@ from itertools import product from typing import ( Any, - Dict, - List, - Optional, - Set, - Tuple, - Type, Union, ) @@ -64,16 +58,16 @@ def quantile(self, q: Any) -> Any: class MeanCalculator: """to calculate sample and dataset mean for in-memory samples""" - def __init__(self, member_id: MemberId, axes: Optional[Sequence[AxisId]]): + def __init__(self, member_id: MemberId, axes: Sequence[AxisId] | None): super().__init__() self._n: int = 0 - self._mean: Optional[Tensor] = None + self._mean: Tensor | None = None self._axes = None if axes is None else tuple(axes) self._member_id = member_id self._sample_mean = SampleMean(member_id=self._member_id, axes=self._axes) self._dataset_mean = DatasetMean(member_id=self._member_id, axes=self._axes) - def compute(self, sample: Sample) -> Dict[SampleMean, MeasureValue]: + def compute(self, sample: Sample) -> dict[SampleMean, MeasureValue]: return {self._sample_mean: self._compute_impl(sample)} def _compute_impl(self, sample: Sample) -> Tensor: @@ -84,7 +78,7 @@ def update(self, sample: Sample) -> None: mean = self._compute_impl(sample) self._update_impl(sample.members[self._member_id], mean) - def compute_and_update(self, sample: Sample) -> Dict[SampleMean, MeasureValue]: + def compute_and_update(self, sample: Sample) -> dict[SampleMean, MeasureValue]: mean = self._compute_impl(sample) self._update_impl(sample.members[self._member_id], mean) return {self._sample_mean: mean} @@ -106,7 +100,7 @@ def _update_impl(self, tensor: Tensor, tensor_mean: Tensor): self._mean = (n_a * mean_old + n_b * tensor_mean) / self._n assert self._mean.dtype == "float64" - def finalize(self) -> Dict[DatasetMean, MeasureValue]: + def finalize(self) -> dict[DatasetMean, MeasureValue]: if self._mean is None: return {} else: @@ -116,17 +110,17 @@ def finalize(self) -> Dict[DatasetMean, MeasureValue]: class MeanVarStdCalculator: """to calculate sample and dataset mean, variance or standard deviation""" - def __init__(self, member_id: MemberId, axes: Optional[Sequence[AxisId]]): + def __init__(self, member_id: MemberId, axes: Sequence[AxisId] | None): super().__init__() self._axes = None if axes is None else tuple(map(AxisId, axes)) self._member_id = member_id self._n: int = 0 - self._mean: Optional[Tensor] = None - self._m2: Optional[Tensor] = None + self._mean: Tensor | None = None + self._m2: Tensor | None = None def compute( self, sample: Sample - ) -> Dict[Union[SampleMean, SampleVar, SampleStd], MeasureValue]: + ) -> dict[SampleMean | SampleVar | SampleStd, MeasureValue]: tensor = sample.members[self._member_id] mean = tensor.mean(dim=self._axes) c = (tensor - mean).data @@ -182,7 +176,7 @@ def update(self, sample: Sample): def finalize( self, - ) -> Dict[Union[DatasetMean, DatasetVar, DatasetStd], MeasureValue]: + ) -> dict[DatasetMean | DatasetVar | DatasetStd, MeasureValue]: if ( self._axes is not None and BATCH_AXIS_ID not in self._axes @@ -211,7 +205,7 @@ class SampleQuantilesCalculator: def __init__( self, member_id: MemberId, - axes: Optional[Sequence[AxisId]], + axes: Sequence[AxisId] | None, qs: Collection[float], method: QuantileMethod = "linear", ): @@ -222,7 +216,7 @@ def __init__( self._member_id = member_id self._method: QuantileMethod = method - def compute(self, sample: Sample) -> Dict[SampleQuantile, MeasureValue]: + def compute(self, sample: Sample) -> dict[SampleQuantile, MeasureValue]: tensor = sample.members[self._member_id] ps = tensor.quantile(self._qs, dim=self._axes, method=self._method) return { @@ -243,7 +237,7 @@ class MeanQuantilesCalculator: def __init__( self, member_id: MemberId, - axes: Optional[Sequence[AxisId]], + axes: Sequence[AxisId] | None, qs: Collection[float], ): super().__init__() @@ -252,7 +246,7 @@ def __init__( self._axes = None if axes is None else tuple(axes) self._member_id = member_id self._n: int = 0 - self._estimates: Optional[Tensor] = None + self._estimates: Tensor | None = None def update(self, sample: Sample): tensor = sample.members[self._member_id] @@ -274,7 +268,7 @@ def update(self, sample: Sample): self._n += n - def finalize(self) -> Dict[DatasetQuantile, MeasureValue]: + def finalize(self) -> dict[DatasetQuantile, MeasureValue]: if self._estimates is None: return {} else: @@ -293,7 +287,7 @@ class CrickQuantilesCalculator: def __init__( self, member_id: MemberId, - axes: Optional[Sequence[AxisId]], + axes: Sequence[AxisId] | None, qs: Collection[float], ): warnings.warn("Computing dataset quantiles with experimental 'crick' library.") @@ -303,10 +297,10 @@ def __init__( self._qs = sorted(set(qs)) self._axes = None if axes is None else tuple(axes) self._member_id = member_id - self._digest: Optional[List[TDigest]] = None - self._dims: Optional[Tuple[AxisId, ...]] = None - self._indices: Optional[Iterator[Tuple[int, ...]]] = None - self._shape: Optional[Tuple[int, ...]] = None + self._digest: list[TDigest] | None = None + self._dims: tuple[AxisId, ...] | None = None + self._indices: Iterator[tuple[int, ...]] | None = None + self._shape: tuple[int, ...] | None = None def _initialize(self, tensor_sizes: PerAxis[int]): assert crick is not None @@ -340,7 +334,7 @@ def update(self, part: Sample): for i, idx in enumerate(self._indices): self._digest[i].update(tensor[dict(zip(self._dims[1:], idx))]) - def finalize(self) -> Dict[DatasetQuantile, MeasureValue]: + def finalize(self) -> dict[DatasetQuantile, MeasureValue]: if self._digest is None: return {} else: @@ -359,8 +353,8 @@ def finalize(self) -> Dict[DatasetQuantile, MeasureValue]: if crick is None: - DatasetQuantilesCalculator: Type[ - Union[MeanQuantilesCalculator, CrickQuantilesCalculator] + DatasetQuantilesCalculator: type[ + MeanQuantilesCalculator | CrickQuantilesCalculator ] = MeanQuantilesCalculator else: DatasetQuantilesCalculator = CrickQuantilesCalculator @@ -374,7 +368,7 @@ def __init__(self, member_id: MemberId, measure: SampleMeasure): self.tensor_name = member_id self.measure = measure - def compute(self, sample: Sample) -> Dict[SampleMeasure, MeasureValue]: + def compute(self, sample: Sample) -> dict[SampleMeasure, MeasureValue]: return {self.measure: self.measure.compute(sample)} @@ -395,9 +389,7 @@ class StatsCalculator: def __init__( self, measures: Collection[Measure], - initial_dataset_measures: Optional[ - Mapping[DatasetMeasure, MeasureValue] - ] = None, + initial_dataset_measures: Mapping[DatasetMeasure, MeasureValue] | None = None, ): super().__init__() self.sample_count = 0 @@ -405,9 +397,7 @@ def __init__( measures ) if not initial_dataset_measures: - self._current_dataset_measures: Optional[ - Dict[DatasetMeasure, MeasureValue] - ] = None + self._current_dataset_measures: dict[DatasetMeasure, MeasureValue] | None = None else: missing_dataset_meas = { m @@ -429,11 +419,11 @@ def has_dataset_measures(self): def update( self, - sample: Union[Sample, Iterable[Sample]], + sample: Sample | Iterable[Sample], ) -> None: _ = self._update(sample) - def finalize(self) -> Dict[DatasetMeasure, MeasureValue]: + def finalize(self) -> dict[DatasetMeasure, MeasureValue]: """returns aggregated dataset statistics""" if self._current_dataset_measures is None: self._current_dataset_measures = {} @@ -445,8 +435,8 @@ def finalize(self) -> Dict[DatasetMeasure, MeasureValue]: def update_and_get_all( self, - sample: Union[Sample, Iterable[Sample]], - ) -> Dict[Measure, MeasureValue]: + sample: Sample | Iterable[Sample], + ) -> dict[Measure, MeasureValue]: """Returns sample as well as updated dataset statistics""" last_sample = self._update(sample) if last_sample is None: @@ -454,19 +444,19 @@ def update_and_get_all( return {**self._compute(last_sample), **self.finalize()} - def skip_update_and_get_all(self, sample: Sample) -> Dict[Measure, MeasureValue]: + def skip_update_and_get_all(self, sample: Sample) -> dict[Measure, MeasureValue]: """Returns sample as well as previously computed dataset statistics""" return {**self._compute(sample), **self.finalize()} - def _compute(self, sample: Sample) -> Dict[SampleMeasure, MeasureValue]: - ret: Dict[SampleMeasure, MeasureValue] = {} + def _compute(self, sample: Sample) -> dict[SampleMeasure, MeasureValue]: + ret: dict[SampleMeasure, MeasureValue] = {} for calc in self.sample_calculators: values = calc.compute(sample) ret.update(values.items()) return ret - def _update(self, sample: Union[Sample, Iterable[Sample]]) -> Optional[Sample]: + def _update(self, sample: Sample | Iterable[Sample]) -> Sample | None: self.sample_count += 1 samples = [sample] if isinstance(sample, Sample) else sample last_sample = None @@ -481,24 +471,24 @@ def _update(self, sample: Union[Sample, Iterable[Sample]]) -> Optional[Sample]: def get_measure_calculators( required_measures: Iterable[Measure], -) -> Tuple[List[SampleMeasureCalculator], List[DatasetMeasureCalculator]]: +) -> tuple[list[SampleMeasureCalculator], list[DatasetMeasureCalculator]]: """determines which calculators are needed to compute the required measures efficiently""" - sample_calculators: List[SampleMeasureCalculator] = [] - dataset_calculators: List[DatasetMeasureCalculator] = [] + sample_calculators: list[SampleMeasureCalculator] = [] + dataset_calculators: list[DatasetMeasureCalculator] = [] # split required measures into groups - required_sample_means: Set[SampleMean] = set() - required_dataset_means: Set[DatasetMean] = set() - required_sample_mean_var_std: Set[Union[SampleMean, SampleVar, SampleStd]] = set() - required_dataset_mean_var_std: Set[Union[DatasetMean, DatasetVar, DatasetStd]] = ( + required_sample_means: set[SampleMean] = set() + required_dataset_means: set[DatasetMean] = set() + required_sample_mean_var_std: set[SampleMean | SampleVar | SampleStd] = set() + required_dataset_mean_var_std: set[DatasetMean | DatasetVar | DatasetStd] = ( set() ) - required_sample_quantiles: Dict[ - Tuple[MemberId, Optional[Tuple[AxisId, ...]], QuantileMethod], Set[float] + required_sample_quantiles: dict[ + tuple[MemberId, tuple[AxisId, ...] | None, QuantileMethod], set[float] ] = {} - required_dataset_quantiles: Dict[ - Tuple[MemberId, Optional[Tuple[AxisId, ...]]], Set[float] + required_dataset_quantiles: dict[ + tuple[MemberId, tuple[AxisId, ...] | None], set[float] ] = {} for rm in required_measures: @@ -572,12 +562,12 @@ def get_measure_calculators( def compute_dataset_measures( measures: Iterable[DatasetMeasure], dataset: Iterable[Sample] -) -> Dict[DatasetMeasure, MeasureValue]: +) -> dict[DatasetMeasure, MeasureValue]: """compute all dataset `measures` for the given `dataset`""" sample_calculators, calculators = get_measure_calculators(measures) assert not sample_calculators - ret: Dict[DatasetMeasure, MeasureValue] = {} + ret: dict[DatasetMeasure, MeasureValue] = {} for sample in dataset: for calc in calculators: @@ -591,11 +581,11 @@ def compute_dataset_measures( def compute_sample_measures( measures: Iterable[SampleMeasure], sample: Sample -) -> Dict[SampleMeasure, MeasureValue]: +) -> dict[SampleMeasure, MeasureValue]: """compute all sample `measures` for the given `sample`""" calculators, dataset_calculators = get_measure_calculators(measures) assert not dataset_calculators - ret: Dict[SampleMeasure, MeasureValue] = {} + ret: dict[SampleMeasure, MeasureValue] = {} for calc in calculators: ret.update(calc.compute(sample).items()) @@ -605,11 +595,11 @@ def compute_sample_measures( def compute_measures( measures: Iterable[Measure], dataset: Iterable[Sample] -) -> Dict[Measure, MeasureValue]: +) -> dict[Measure, MeasureValue]: """compute all `measures` for the given `dataset` sample measures are computed for the last sample in `dataset`""" sample_calculators, dataset_calculators = get_measure_calculators(measures) - ret: Dict[Measure, MeasureValue] = {} + ret: dict[Measure, MeasureValue] = {} sample = None for sample in dataset: for calc in dataset_calculators: diff --git a/src/bioimageio/core/stat_measures.py b/src/bioimageio/core/stat_measures.py index 798d9a3c..b9475297 100644 --- a/src/bioimageio/core/stat_measures.py +++ b/src/bioimageio/core/stat_measures.py @@ -5,11 +5,8 @@ from typing import ( Annotated, Any, - Dict, Literal, - Optional, Protocol, - Tuple, TypeVar, Union, ) @@ -28,7 +25,7 @@ from .tensor import Tensor -def tensor_custom_before_validator(data: Union[Tensor, Mapping[str, Any]]): +def tensor_custom_before_validator(data: Tensor | Mapping[str, Any]): if isinstance(data, Tensor): return data @@ -36,7 +33,7 @@ def tensor_custom_before_validator(data: Union[Tensor, Mapping[str, Any]]): return Tensor(np.asarray(data["data"]), dims=data["dims"]) -def tensor_custom_serializer(t: Tensor) -> Dict[str, Any]: +def tensor_custom_serializer(t: Tensor) -> dict[str, Any]: # custome serialization logic return {"data": t.data.data.tolist(), "dims": list(map(str, t.dims))} @@ -76,7 +73,7 @@ class DatasetMeasureBase(MeasureBase, ABC, frozen=True): class _Mean(BaseModel, frozen=True): name: Literal["mean"] = "mean" - axes: Optional[Tuple[AxisId, ...]] = None + axes: tuple[AxisId, ...] | None = None """`axes` to reduce""" @@ -100,7 +97,7 @@ def model_post_init(self, __context: Any): class _Std(BaseModel, frozen=True): name: Literal["std"] = "std" - axes: Optional[Tuple[AxisId, ...]] = None + axes: tuple[AxisId, ...] | None = None """`axes` to reduce""" @@ -124,7 +121,7 @@ def model_post_init(self, __context: Any): class _Var(BaseModel, frozen=True): name: Literal["var"] = "var" - axes: Optional[Tuple[AxisId, ...]] = None + axes: tuple[AxisId, ...] | None = None """`axes` to reduce""" @@ -149,7 +146,7 @@ def model_post_init(self, __context: Any): # TODO: turn into @model_validator class _Quantile(BaseModel, frozen=True): name: Literal["quantile"] = "quantile" q: float - axes: Optional[Tuple[AxisId, ...]] = None + axes: tuple[AxisId, ...] | None = None """`axes` to reduce""" def model_post_init(self, __context: Any): @@ -190,7 +187,7 @@ def model_post_init(self, __context: Any): Measure: TypeAlias = Annotated[ Union[SampleMeasure, DatasetMeasure], Discriminator("scope") ] -Stat: TypeAlias = Dict[Measure, MeasureValue] +Stat: TypeAlias = dict[Measure, MeasureValue] MeanMeasure: TypeAlias = Union[SampleMean, DatasetMean] StdMeasure: TypeAlias = Union[SampleStd, DatasetStd] diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index b42d0a2f..4bdbc54e 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -7,11 +7,7 @@ TYPE_CHECKING, Any, Callable, - Dict, - List, Literal, - Optional, - Tuple, Union, cast, get_args, @@ -80,8 +76,8 @@ class Tensor(MagicTensorOpsMixin): def __init__( self, - array: Union[Array, xr.DataArray], - dims: Sequence[Union[AxisId, AxisLike]], + array: Array | xr.DataArray, + dims: Sequence[AxisId | AxisLike], ) -> None: super().__init__() axes = tuple( @@ -96,19 +92,12 @@ def __init__( def __repr__(self) -> str: return f"" - def __array__(self, dtype: Optional[DTypeLike] = None): + def __array__(self, dtype: DTypeLike | None = None): return np.asarray(self._data, dtype=dtype) def __getitem__( self, - key: Union[ - SliceInfo, - slice, - int, - PerAxis[Union[SliceInfo, slice, int]], - Tensor, - xr.DataArray, - ], + key: SliceInfo | slice | int | PerAxis[SliceInfo | slice | int] | Tensor | xr.DataArray, ) -> Self: if isinstance(key, SliceInfo): key = slice(*key) @@ -124,8 +113,8 @@ def __getitem__( def __setitem__( self, - key: Union[PerAxis[Union[SliceInfo, slice]], Tensor, xr.DataArray], - value: Union[Tensor, xr.DataArray, float], + key: PerAxis[SliceInfo | slice] | Tensor | xr.DataArray, + value: Tensor | xr.DataArray | float, ) -> None: if isinstance(key, Tensor): key = key._data @@ -205,7 +194,7 @@ def from_numpy( cls, array: NDArray[Any], *, - dims: Optional[Union[AxisLike, Sequence[AxisLike]]], + dims: AxisLike | Sequence[AxisLike] | None, ) -> Tensor: return cls.from_array(array, dims=dims) @@ -214,7 +203,7 @@ def from_array( cls, array: Array, *, - dims: Optional[Union[AxisLike, Sequence[AxisLike]]], + dims: AxisLike | Sequence[AxisLike] | None, ) -> Tensor: """create a `Tensor` from a numpy array @@ -253,7 +242,7 @@ def data(self): @property def dims(self): # TODO: rename to `axes`? """Tuple of dimension names associated with this tensor.""" - return cast(Tuple[AxisId, ...], self._data.dims) + return cast(tuple[AxisId, ...], self._data.dims) @property def dtype(self) -> DTypeStr: @@ -309,7 +298,7 @@ def astype(self, dtype: DTypeStr, *, copy: bool = False): note: if dtype is already satisfied copy if `copy`""" return self.__class__.from_xarray(self._data.astype(dtype, copy=copy)) - def clip(self, min: Optional[float] = None, max: Optional[float] = None): + def clip(self, min: float | None = None, max: float | None = None): """Return a tensor whose values are limited to [min, max]. At least one of max or min must be given.""" return self.__class__.from_xarray(self._data.clip(min, max)) @@ -317,10 +306,7 @@ def clip(self, min: Optional[float] = None, max: Optional[float] = None): def crop_to( self, sizes: PerAxis[int], - crop_where: Union[ - CropWhere, - PerAxis[CropWhere], - ] = "left_and_right", + crop_where: CropWhere | PerAxis[CropWhere] = "left_and_right", ) -> Self: """crop to match `sizes`""" if isinstance(crop_where, str): @@ -328,7 +314,7 @@ def crop_to( else: crop_axis_where = crop_where - slices: Dict[AxisId, SliceInfo] = {} + slices: dict[AxisId, SliceInfo] = {} for a, s_is in self.sizes.items(): if a not in sizes or sizes[a] == s_is: @@ -359,14 +345,12 @@ def crop_to( return self[slices] - def expand_dims(self, dims: Union[Sequence[AxisId], PerAxis[int]]) -> Self: + def expand_dims(self, dims: Sequence[AxisId] | PerAxis[int]) -> Self: return self.__class__.from_xarray(self._data.expand_dims(dims=dims)) def item( self, - key: Union[ - None, SliceInfo, slice, int, PerAxis[Union[SliceInfo, slice, int]] - ] = None, + key: None | SliceInfo | slice | int | PerAxis[SliceInfo | slice | int] = None, ): """Copy a tensor element to a standard Python scalar and return it.""" if key is None: @@ -377,7 +361,7 @@ def item( assert isinstance(ret, (bool, float, int)) return ret - def mean(self, dim: Optional[Union[AxisId, Sequence[AxisId]]] = None) -> Self: + def mean(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: return self.__class__.from_xarray(self._data.mean(dim=dim)) def pad( @@ -396,7 +380,7 @@ def pad( def pad_to( self, sizes: PerAxis[int], - pad_where: Union[PadWhere, PerAxis[PadWhere]] = "left_and_right", + pad_where: PadWhere | PerAxis[PadWhere] = "left_and_right", mode: PadMode = "symmetric", ) -> Self: """pad `tensor` to match `sizes`""" @@ -405,7 +389,7 @@ def pad_to( else: pad_axis_where = pad_where - pad_width: Dict[AxisId, PadWidth] = {} + pad_width: dict[AxisId, PadWidth] = {} for a, s_is in self.sizes.items(): if a not in sizes or sizes[a] == s_is: pad_width[a] = PadWidth(0, 0) @@ -437,8 +421,8 @@ def pad_to( def quantile( self, - q: Union[float, Sequence[float]], - dim: Optional[Union[AxisId, Sequence[AxisId]]] = None, + q: float | Sequence[float], + dim: AxisId | Sequence[AxisId] | None = None, method: QuantileMethod = "linear", ) -> Self: assert ( @@ -464,19 +448,13 @@ def resize_to( self, sizes: PerAxis[int], *, - pad_where: Union[ - PadWhere, - PerAxis[PadWhere], - ] = "left_and_right", - crop_where: Union[ - CropWhere, - PerAxis[CropWhere], - ] = "left_and_right", + pad_where: PadWhere | PerAxis[PadWhere] = "left_and_right", + crop_where: CropWhere | PerAxis[CropWhere] = "left_and_right", pad_mode: PadMode = "symmetric", ): """return cropped/padded tensor with `sizes`""" - crop_to_sizes: Dict[AxisId, int] = {} - pad_to_sizes: Dict[AxisId, int] = {} + crop_to_sizes: dict[AxisId, int] = {} + pad_to_sizes: dict[AxisId, int] = {} new_axes = dict(sizes) for a, s_is in self.sizes.items(): a = AxisId(str(a)) @@ -500,10 +478,10 @@ def resize_to( return tensor - def std(self, dim: Optional[Union[AxisId, Sequence[AxisId]]] = None) -> Self: + def std(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: return self.__class__.from_xarray(self._data.std(dim=dim)) - def sum(self, dim: Optional[Union[AxisId, Sequence[AxisId]]] = None) -> Self: + def sum(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: """Reduce this Tensor's data by applying sum along some dimension(s).""" return self.__class__.from_xarray(self._data.sum(dim=dim)) @@ -556,7 +534,7 @@ def unstack_batch_multi_index( added_dims = [AxisId(d) for d in array.dims if d not in self._data.dims] # restore expected axis order, replace batch dim with added dims - new_dims: List[AxisId] = [] + new_dims: list[AxisId] = [] for d in old_dims: if d in array.dims: new_dims.append(d) @@ -683,7 +661,7 @@ def transpose( # transpose to the correct axis order return self.__class__.from_xarray(array.transpose(*axes)) - def var(self, dim: Optional[Union[AxisId, Sequence[AxisId]]] = None) -> Self: + def var(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: return self.__class__.from_xarray(self._data.var(dim=dim)) @classmethod @@ -767,7 +745,7 @@ def _add_singletons(arr: NDArray[Any], axis_infos: Sequence[AxisInfo]): def _get_array_view( original_array: Array, axis_infos: Sequence[AxisInfo] -) -> Optional[Array]: +) -> Array | None: perms = list(permutations(range(len(original_array.shape)))) for perm in perms: diff --git a/src/bioimageio/core/weight_converters/_add_weights.py b/src/bioimageio/core/weight_converters/_add_weights.py index b85a5cac..b9c0371b 100644 --- a/src/bioimageio/core/weight_converters/_add_weights.py +++ b/src/bioimageio/core/weight_converters/_add_weights.py @@ -1,5 +1,6 @@ +from __future__ import annotations + import traceback -from typing import Optional, Union from loguru import logger from pydantic import DirectoryPath @@ -18,11 +19,11 @@ def add_weights( model_descr: ModelDescr, *, output_path: DirectoryPath, - source_format: Optional[WeightsFormat] = None, - target_format: Optional[WeightsFormat] = None, + source_format: WeightsFormat | None = None, + target_format: WeightsFormat | None = None, verbose: bool = False, allow_tracing: bool = True, -) -> Union[ModelDescr, InvalidDescr]: +) -> ModelDescr | InvalidDescr: """Convert model weights to other formats and add them to the model description Args: diff --git a/src/bioimageio/core/weight_converters/_utils_torch_onnx.py b/src/bioimageio/core/weight_converters/_utils_torch_onnx.py index 45d25307..7803d91e 100644 --- a/src/bioimageio/core/weight_converters/_utils_torch_onnx.py +++ b/src/bioimageio/core/weight_converters/_utils_torch_onnx.py @@ -1,4 +1,5 @@ """helper to export both TorchScript or PytorchStateDict to ONNX""" +from __future__ import annotations from collections import defaultdict from collections.abc import Sequence @@ -6,12 +7,7 @@ from pathlib import Path from typing import ( TYPE_CHECKING, - DefaultDict, - Dict, - List, Literal, - Optional, - Tuple, Union, ) @@ -41,7 +37,7 @@ ) -def get_torch_sample_inputs(model_descr: ModelDescr) -> Tuple[torch.Tensor, ...]: +def get_torch_sample_inputs(model_descr: ModelDescr) -> tuple[torch.Tensor, ...]: sample = get_test_input_sample(model_descr) procs = get_pre_and_postprocessing( model_descr, dataset_for_initial_statistics=[sample] @@ -61,7 +57,7 @@ def _get_dynamic_axes_noop(model_descr: ModelDescr): def _get_dynamic_axes_impl(model_descr: ModelDescr): """dynamic axes for (old) onnx export with dynamo=False""" - dynamic_axes: DefaultDict[str, Dict[int, str]] = defaultdict(dict) + dynamic_axes: defaultdict[str, dict[int, str]] = defaultdict(dict) for d in chain(model_descr.inputs, model_descr.outputs): for i, ax in enumerate(d.axes): if not isinstance(ax.size, int): @@ -96,11 +92,11 @@ def _get_dynamic_shapes_impl(model_descr: ModelDescr): # dynamic shapes as list to match the source code which may have # different arg names than the tensor ids in the model description - dynamic_shapes: List[Dict[int, Union[int, TensorDim]]] = [] - potential_ref_axes: Dict[str, Tuple[InputAxis, int]] = {} + dynamic_shapes: list[dict[int, int | TensorDim]] = [] + potential_ref_axes: dict[str, tuple[InputAxis, int]] = {} # add dynamic dims from parameterized input sizes (and fixed sizes as None) for d in model_descr.inputs: - dynamic_tensor_dims: Dict[int, Union[int, TensorDim]] = {} + dynamic_tensor_dims: dict[int, int | TensorDim] = {} for i, ax in enumerate(d.axes): dim_name = f"{d.id}_{ax.id}" if isinstance(ax.size, int): @@ -145,12 +141,12 @@ def _get_dynamic_shapes_impl(model_descr: ModelDescr): def export_to_onnx( model_descr: ModelDescr, - model: Union[ExportedProgram, torch.nn.Module], + model: ExportedProgram | torch.nn.Module, output_path: Path, verbose: bool, opset_version: int, parent: Literal["torchscript", "pytorch_state_dict"], - devices: Optional[Sequence[Union[str, torch.device]]] = None, + devices: Sequence[str | torch.device] | None = None, ) -> OnnxWeightsDescr: inputs_torch = get_torch_sample_inputs( model_descr diff --git a/src/bioimageio/core/weight_converters/keras_to_tensorflow.py b/src/bioimageio/core/weight_converters/keras_to_tensorflow.py index e75dc979..ed6028a7 100644 --- a/src/bioimageio/core/weight_converters/keras_to_tensorflow.py +++ b/src/bioimageio/core/weight_converters/keras_to_tensorflow.py @@ -1,9 +1,11 @@ +from __future__ import annotations + import os import shutil import sys from pathlib import Path from tempfile import TemporaryDirectory -from typing import Any, Union, no_type_check +from typing import Any, no_type_check from zipfile import ZipFile import tensorflow @@ -81,7 +83,7 @@ def convert( ) td_kwargs: dict[str, Any] = ( - dict(ignore_cleanup_errors=True) if sys.version_info >= (3, 10) else {} + {"ignore_cleanup_errors": True} if sys.version_info >= (3, 10) else {} ) with TemporaryDirectory(**td_kwargs) as temp_dir: local_weights = ensure_unzipped( @@ -114,7 +116,7 @@ def convert( def _convert_tf2( - keras_weight_path: Union[Path, ZipPath], output_path: Path + keras_weight_path: Path | ZipPath, output_path: Path ) -> TensorflowSavedModelBundleWeightsDescr: model = keras.models.load_model(keras_weight_path) # type: ignore model.export(output_path) # type: ignore diff --git a/src/bioimageio/core/weight_converters/pytorch_to_onnx.py b/src/bioimageio/core/weight_converters/pytorch_to_onnx.py index be3e3fdf..9c3b873a 100644 --- a/src/bioimageio/core/weight_converters/pytorch_to_onnx.py +++ b/src/bioimageio/core/weight_converters/pytorch_to_onnx.py @@ -1,6 +1,7 @@ +from __future__ import annotations + from collections.abc import Sequence from pathlib import Path -from typing import Optional from bioimageio.spec.model.v0_5 import ModelDescr, OnnxWeightsDescr @@ -14,7 +15,7 @@ def convert( *, verbose: bool = False, opset_version: int = 18, - devices: Optional[Sequence[str]] = None, + devices: Sequence[str] | None = None, ) -> OnnxWeightsDescr: """ Convert model weights from the Torchscript state_dict format to the ONNX format. diff --git a/src/bioimageio/core/weight_converters/pytorch_to_torchscript.py b/src/bioimageio/core/weight_converters/pytorch_to_torchscript.py index 00854cb8..e1c7c171 100644 --- a/src/bioimageio/core/weight_converters/pytorch_to_torchscript.py +++ b/src/bioimageio/core/weight_converters/pytorch_to_torchscript.py @@ -1,6 +1,8 @@ +from __future__ import annotations + from collections.abc import Sequence from pathlib import Path -from typing import Any, Optional, Tuple, Union +from typing import Any import torch from torch.jit import ScriptModule @@ -17,7 +19,7 @@ def convert( output_path: Path, *, use_tracing: bool = True, - devices: Optional[Sequence[Union[str, torch.device]]] = None, + devices: Sequence[str | torch.device] | None = None, ) -> TorchscriptWeightsDescr: """ Convert model weights from the PyTorch `state_dict` format to TorchScript. @@ -53,9 +55,7 @@ def convert( model = load_torch_model( state_dict_weights_descr, load_state=True, devices=devices ) - scripted_model: Union[ # pyright: ignore[reportUnknownVariableType] - ScriptModule, Tuple[Any, ...] - ] = ( + scripted_model: ScriptModule | tuple[Any, ...] = ( torch.jit.trace(model, input_data) if use_tracing else torch.jit.script(model) diff --git a/src/bioimageio/core/weight_converters/torchscript_to_onnx.py b/src/bioimageio/core/weight_converters/torchscript_to_onnx.py index b2ac2f92..b66b3832 100644 --- a/src/bioimageio/core/weight_converters/torchscript_to_onnx.py +++ b/src/bioimageio/core/weight_converters/torchscript_to_onnx.py @@ -1,6 +1,7 @@ +from __future__ import annotations + from collections.abc import Sequence from pathlib import Path -from typing import Optional, Union import torch.jit from loguru import logger @@ -18,7 +19,7 @@ def convert( *, verbose: bool = False, opset_version: int = 18, - devices: Optional[Sequence[Union[str, torch.device]]] = None, + devices: Sequence[str | torch.device] | None = None, ) -> OnnxWeightsDescr: """ Convert model weights from the PyTorch state_dict format to the ONNX format. diff --git a/tests/conftest.py b/tests/conftest.py index 7675c93e..df9b79ea 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,6 @@ import subprocess from itertools import chain from pathlib import Path -from typing import Dict, List from dotenv import load_dotenv from loguru import logger @@ -61,7 +60,7 @@ ) # TODO: use models from new collection on S3 -MODEL_SOURCES: Dict[str, str] = { +MODEL_SOURCES: dict[str, str] = { "hpa_densenet": "polite-pig", "stardist": ( EXAMPLE_DESCRIPTIONS + "models/stardist_example_model/v0_4.bioimageio.yaml" @@ -143,7 +142,7 @@ if tf_major_version == 1 else ["unet2d_keras_tf2"] ) -TENSORFLOW_JS_MODELS: List[str] = [] # TODO: add a tensorflow_js example model +TENSORFLOW_JS_MODELS: list[str] = [] # TODO: add a tensorflow_js example model ALL_MODELS = sorted( set( diff --git a/tests/test_bioimageio_collection.py b/tests/test_bioimageio_collection.py index a41c1984..1bd67be8 100644 --- a/tests/test_bioimageio_collection.py +++ b/tests/test_bioimageio_collection.py @@ -2,7 +2,6 @@ from collections.abc import Iterable, Mapping from itertools import chain from pathlib import Path -from typing import Tuple import pytest from pydantic import HttpUrl @@ -11,7 +10,7 @@ from bioimageio.spec.common import Sha256 from tests.utils import ParameterSet, expensive_test -TEST_RDF_SOURCES: Mapping[str, Tuple[HttpUrl, Sha256]] = { +TEST_RDF_SOURCES: Mapping[str, tuple[HttpUrl, Sha256]] = { # "affable-shark": ( # TODO: enable when updated with fixed torchscript and onnx weights # HttpUrl( # "https://hypha.aicell.io/bioimage-io/artifacts/affable-shark/files/rdf.yaml?version=v0" diff --git a/tests/test_bioimageio_spec_version.py b/tests/test_bioimageio_spec_version.py index dea63430..6a4ddf16 100644 --- a/tests/test_bioimageio_spec_version.py +++ b/tests/test_bioimageio_spec_version.py @@ -1,6 +1,7 @@ +from __future__ import annotations + import json import subprocess -from typing import Optional import pytest from packaging.version import Version @@ -8,7 +9,7 @@ from bioimageio.spec._internal.gh_utils import set_github_warning -def test_bioimageio_spec_version(conda_cmd: Optional[str]): +def test_bioimageio_spec_version(conda_cmd: str | None): if conda_cmd is None: pytest.skip("requires conda") diff --git a/tests/test_cli.py b/tests/test_cli.py index fa815cb5..f4e229a9 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,7 +1,7 @@ import subprocess from collections.abc import Sequence from pathlib import Path -from typing import Any, List +from typing import Any import numpy as np import pytest @@ -49,7 +49,7 @@ def run_subprocess( ], ) def test_cli( - args: List[str], + args: list[str], unet2d_nuclei_broad_model: str, unet2d_nuclei_broad_model_old: str, tmp_path: Path, @@ -94,7 +94,7 @@ def test_empty_cache(tmp_path: Path, unet2d_nuclei_broad_model: str): @pytest.mark.parametrize("args", [["test", "stardist_wrong_shape"]]) -def test_cli_fails(args: List[str], stardist_wrong_shape: FilePath): +def test_cli_fails(args: list[str], stardist_wrong_shape: FilePath): resolved_args = [ str(stardist_wrong_shape) if arg == "stardist_wrong_shape" else arg for arg in args @@ -157,8 +157,8 @@ def _test_cli_predict_multiple( out_folder = tmp_path / "outputs" out_folder.mkdir() out_file_pattern = "im-{sample_id}.npy" - inputs: List[str] = [] - expected_outputs: List[Path] = [] + inputs: list[str] = [] + expected_outputs: list[Path] = [] for i in range(n_images): input_path = in_folder / f"im-{i}.npy" im = np.random.randint(0, 255, size=shape).astype("uint8") diff --git a/tests/test_commands.py b/tests/test_commands.py index 54981f00..d0c500b4 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1,5 +1,7 @@ +from __future__ import annotations + from pathlib import Path -from typing import Literal, Optional +from typing import Literal import pytest @@ -38,7 +40,7 @@ def test_validate_format(model: AnyModelDescr): ) def test_test( weight_format: Literal["all", "pytorch_state_dict"], - devices: Optional[str], + devices: str | None, model: AnyModelDescr, ): assert command_tst(model, weight_format=weight_format, devices=devices) == 0 diff --git a/tests/test_io.py b/tests/test_io.py index 3ff16693..25904666 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,5 +1,4 @@ from pathlib import Path -from typing import Tuple import numpy as np import pytest @@ -23,7 +22,7 @@ (5, 3, 4), ], ) -def test_tensor_io(name: str, shape: Tuple[int, ...], tmp_path: Path): +def test_tensor_io(name: str, shape: tuple[int, ...], tmp_path: Path): from bioimageio.core import Tensor from bioimageio.core.io import load_tensor, save_tensor diff --git a/tests/test_proc_ops.py b/tests/test_proc_ops.py index 16a40988..9efdab93 100644 --- a/tests/test_proc_ops.py +++ b/tests/test_proc_ops.py @@ -1,6 +1,8 @@ +from __future__ import annotations + from collections.abc import Iterable from pathlib import Path -from typing import Any, Optional, Tuple, Type, TypeVar +from typing import Any, TypeVar import numpy as np import pytest @@ -54,7 +56,7 @@ def test_scale_linear_no_channel(tid: MemberId): T = TypeVar("T") -def is_iterable(val: Iterable[T], inner: Type[T]) -> TypeGuard[Iterable[T]]: +def is_iterable(val: Iterable[T], inner: type[T]) -> TypeGuard[Iterable[T]]: """Determines whether all objects in the list are strings""" return all(isinstance(x, inner) for x in val) @@ -287,7 +289,7 @@ def test_combination_of_op_steps_with_dims_specified(tid: MemberId): tuple(map(AxisId, "x")), ], ) -def test_scale_mean_variance(tid: MemberId, axes: Optional[Tuple[AxisId, ...]]): +def test_scale_mean_variance(tid: MemberId, axes: tuple[AxisId, ...] | None): from bioimageio.core.proc_ops import ScaleMeanVariance shape = (3, 32, 46) @@ -314,7 +316,7 @@ def test_scale_mean_variance(tid: MemberId, axes: Optional[Tuple[AxisId, ...]]): "axes_str", [None, "cy", "y", "yx"], ) -def test_scale_mean_variance_per_channel(tid: MemberId, axes_str: Optional[str]): +def test_scale_mean_variance_per_channel(tid: MemberId, axes_str: str | None): from bioimageio.core.proc_ops import ScaleMeanVariance axes = None if axes_str is None else tuple(map(AxisId, axes_str)) diff --git a/tests/test_remote_backends/test_gradio.py b/tests/test_remote_backends/test_gradio.py index 48fee4d4..5b2140f5 100644 --- a/tests/test_remote_backends/test_gradio.py +++ b/tests/test_remote_backends/test_gradio.py @@ -3,7 +3,6 @@ import time from concurrent.futures import ThreadPoolExecutor from multiprocessing import Process -from typing import List, Tuple import pytest from loguru import logger @@ -46,7 +45,7 @@ def test_gradio_backend(): raise TimeoutError(f"gradio server did not become ready on port {port}") server_url = f"http://localhost:{port}/" - prepared: List[Tuple[str, GradioModelAdapter, PerMember[Tensor]]] = [] + prepared: list[tuple[str, GradioModelAdapter, PerMember[Tensor]]] = [] for model_id in ("affable-shark", "ambitious-sloth"): model = load_model( model_id, format_version="latest", perform_io_checks=False diff --git a/tests/test_stat_calculators.py b/tests/test_stat_calculators.py index dd2823b6..e2494f28 100644 --- a/tests/test_stat_calculators.py +++ b/tests/test_stat_calculators.py @@ -1,4 +1,4 @@ -from typing import Optional, Tuple +from __future__ import annotations import numpy as np import pytest @@ -19,7 +19,7 @@ from bioimageio.core.tensor import Tensor -def create_random_dataset(tid: MemberId, axes: Tuple[AxisId, ...]): +def create_random_dataset(tid: MemberId, axes: tuple[AxisId, ...]): n = 3 sizes = list(range(n, len(axes) + n)) data = np.asarray(np.random.rand(*sizes)) @@ -37,7 +37,7 @@ def create_random_dataset(tid: MemberId, axes: Tuple[AxisId, ...]): (AxisId("channel"), AxisId("y")), ], ) -def test_sample_mean_var_std_calculator(axes: Optional[Tuple[AxisId, ...]]): +def test_sample_mean_var_std_calculator(axes: tuple[AxisId, ...] | None): tid = MemberId("tensor") d_axes = tuple(map(AxisId, ("batch", "channel", "x", "y"))) data, ds = create_random_dataset(tid, d_axes) @@ -73,7 +73,7 @@ def test_sample_mean_var_std_calculator(axes: Optional[Tuple[AxisId, ...]]): (AxisId("batch"), AxisId("channel"), AxisId("x"), AxisId("y")), ], ) -def test_dataset_mean_var_std_calculator(axes: Optional[Tuple[AxisId, ...]]): +def test_dataset_mean_var_std_calculator(axes: tuple[AxisId, ...] | None): tid = MemberId("tensor") d_axes = tuple(map(AxisId, ("batch", "channel", "x", "y"))) data, ds = create_random_dataset(tid, d_axes) diff --git a/tests/test_stat_measures.py b/tests/test_stat_measures.py index 5437dd53..86001b00 100644 --- a/tests/test_stat_measures.py +++ b/tests/test_stat_measures.py @@ -1,5 +1,7 @@ +from __future__ import annotations + from itertools import product -from typing import Literal, Optional, Tuple +from typing import Literal import numpy as np import pytest @@ -28,7 +30,7 @@ ) def test_individual_normal_measure( name: str, - axes: Optional[Tuple[AxisId, AxisId]], + axes: tuple[AxisId, AxisId] | None, ): data_id = MemberId("test_data") measure = getattr(stat_measures, "Sample" + name.title())( @@ -47,7 +49,7 @@ def test_individual_normal_measure( @pytest.mark.parametrize("method", ["inverted_cdf", "linear"]) @pytest.mark.parametrize("axes", [None, (AxisId("x"), AxisId("y"))]) def test_individual_percentile_measure( - axes: Optional[Tuple[AxisId, ...]], method: Literal["inverted_cdf", "linear"] + axes: tuple[AxisId, ...] | None, method: Literal["inverted_cdf", "linear"] ): qs = [0, 0.1, 0.5, 1.0] tid = MemberId("tensor") diff --git a/tests/test_weight_converters.py b/tests/test_weight_converters.py index 42647e98..f7a1ecda 100644 --- a/tests/test_weight_converters.py +++ b/tests/test_weight_converters.py @@ -93,7 +93,7 @@ def test_keras_to_tensorflow(any_keras_model: Path, tmp_path: Path): expected_names = {"saved_model.pb", "variables/variables.index"} with zipfile.ZipFile(out_path, "r") as f: - names = set([name for name in f.namelist()]) + names = {name for name in f.namelist()} assert len(expected_names - names) == 0 model_descr.weights.keras = ret_val From 8a813d2117747ff5cee0465db496e26db2b806ac Mon Sep 17 00:00:00 2001 From: fynnbe Date: Mon, 3 Aug 2026 11:29:43 +0200 Subject: [PATCH 10/85] manual ruff fixes --- pyproject.toml | 1 + scripts/show_diff.py | 4 ++- .../core/_description_serializer.py | 2 +- src/bioimageio/core/_ops_cellpose.py | 4 ++- src/bioimageio/core/_resource_tests.py | 24 ++++++------- src/bioimageio/core/backends/__init__.py | 6 ---- .../core/backends/pytorch_backend.py | 6 ++-- src/bioimageio/core/cli.py | 3 +- src/bioimageio/core/commands.py | 10 ++++-- src/bioimageio/core/digest_spec.py | 35 ++++++++++++++----- src/bioimageio/core/prediction.py | 18 +++++++--- src/bioimageio/core/proc_ops.py | 25 ++++++++----- .../core/remote_backends/__init__.py | 2 +- src/bioimageio/core/stat_measures.py | 18 +++++----- tests/test_cli.py | 1 + 15 files changed, 99 insertions(+), 60 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 431511e8..91c162a7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -164,6 +164,7 @@ exclude = [ [tool.ruff.lint] extend-select = ["NPY201"] +ignore = ["BLE001"] [tool.ruff.lint.isort] known-first-party = ["bioimageio"] diff --git a/scripts/show_diff.py b/scripts/show_diff.py index d1f4affe..1f536d04 100644 --- a/scripts/show_diff.py +++ b/scripts/show_diff.py @@ -19,4 +19,6 @@ latest = Path(tmp) / "latest.bioimageio.yaml" save_bioimageio_yaml_only(model_latest, file=latest) - _ = subprocess.run(f"git diff --no-index --ignore-all-space {as_is} {latest}") + _ = subprocess.run( + f"git diff --no-index --ignore-all-space {as_is} {latest}", check=True + ) diff --git a/src/bioimageio/core/_description_serializer.py b/src/bioimageio/core/_description_serializer.py index 9656c7ef..f25e3494 100644 --- a/src/bioimageio/core/_description_serializer.py +++ b/src/bioimageio/core/_description_serializer.py @@ -51,7 +51,7 @@ def deserialize_from_string(cls, serialized: str) -> ResourceDescr: def deserialize(serialized: bytes) -> ResourceDescr: descr = load_description(ZipFile(BytesIO(serialized)), perform_io_checks=False) if isinstance(descr, InvalidDescr): - raise ValueError(f"invalid serialized model package: {descr.get_reason()}") + raise TypeError(f"invalid serialized model package: {descr.get_reason()}") return descr diff --git a/src/bioimageio/core/_ops_cellpose.py b/src/bioimageio/core/_ops_cellpose.py index b25a0773..e8750a12 100644 --- a/src/bioimageio/core/_ops_cellpose.py +++ b/src/bioimageio/core/_ops_cellpose.py @@ -15,6 +15,8 @@ from .stat_measures import Measure from .tensor import Tensor +DEFAULT_LABELS_ID = MemberId("labels") + @dataclass class CellposeFlowDynamics(SamplewiseOperator): @@ -30,7 +32,7 @@ class CellposeFlowDynamics(SamplewiseOperator): do_3D: bool = False min_size: int = 15 """Minimum size of objects to keep, in pixels. Default is 15, which is the default in Cellpose. Set to 0 to disable filtering by size.""" - labels_id: MemberId = MemberId("labels") + labels_id: MemberId = DEFAULT_LABELS_ID output_dtype: Literal["uint16", "uint32"] = "uint16" @classmethod diff --git a/src/bioimageio/core/_resource_tests.py b/src/bioimageio/core/_resource_tests.py index fcb0327a..64b87553 100644 --- a/src/bioimageio/core/_resource_tests.py +++ b/src/bioimageio/core/_resource_tests.py @@ -208,7 +208,9 @@ def test_description( expected_type: str | None = None, sha256: Sha256 | None = None, stop_early: bool = False, - runtime_env: Literal["currently-active", "as-described"] | Path | BioimageioCondaEnv = ("currently-active"), + runtime_env: Literal["currently-active", "as-described"] + | Path + | BioimageioCondaEnv = ("currently-active"), run_command: Callable[[Sequence[str]], None] = default_run_command, working_dir: os.PathLike[str] | str | None = None, **deprecated: Unpack[DeprecatedKwargs], @@ -267,8 +269,8 @@ def test_description( if run_command is not default_run_command: try: run_command(["thiscommandshouldalwaysfail", "please"]) - except Exception: - pass + except Exception as e: + logger.trace("run_command failed successfully :) {}", e) else: raise RuntimeError( "given run_command does not raise an exception for a failing command" @@ -949,7 +951,7 @@ def save_to_working_dir(name: str, tensor: Tensor) -> list[Path]: try: test_input = get_test_input_sample(model) - expected = get_test_output_sample(model) + expected_sample = get_test_output_sample(model) with create_prediction_pipeline( bioimageio_model=model, devices=devices, weight_format=weight_format @@ -968,21 +970,21 @@ def save_to_working_dir(name: str, tensor: Tensor) -> list[Path]: results = deepcopy(results_not_postprocessed) prediction_pipeline.apply_postprocessing(results) - if len(results.members) != len(expected.members): + if len(results.members) != len(expected_sample.members): add_error_entry( - f"Expected {len(expected.members)} outputs, but got {len(results.members)}" + f"Expected {len(expected_sample.members)} outputs, but got {len(results.members)}" ) else: intermediate_paths: list[Path] = [] - for m, t in test_input_preprocessed.members.items(): + for m, expected in test_input_preprocessed.members.items(): intermediate_paths.extend( - save_to_working_dir(f"test_input_preprocessed_{m}", t) + save_to_working_dir(f"test_input_preprocessed_{m}", expected) ) if intermediate_paths: logger.debug("Saved preprocessed test inputs to {}", intermediate_paths) - for m, expected in expected.members.items(): + for m, expected in expected_sample.members.items(): actual = results.members.get(m) if actual is None: add_error_entry("Output tensors for test case may not be None") @@ -1243,9 +1245,7 @@ def get_ns(n: int): ) -def _test_expected_resource_type( - rd: InvalidDescr | ResourceDescr, expected_type: str -): +def _test_expected_resource_type(rd: InvalidDescr | ResourceDescr, expected_type: str): has_expected_type = rd.type is expected_type rd.validation_summary.details.append( ValidationDetail( diff --git a/src/bioimageio/core/backends/__init__.py b/src/bioimageio/core/backends/__init__.py index a8c44e01..d431df00 100644 --- a/src/bioimageio/core/backends/__init__.py +++ b/src/bioimageio/core/backends/__init__.py @@ -1,12 +1,6 @@ from __future__ import annotations from collections.abc import Sequence -from typing import ( - List, - Optional, - Tuple, - Union, -) from exceptiongroup import ExceptionGroup from typing_extensions import assert_never diff --git a/src/bioimageio/core/backends/pytorch_backend.py b/src/bioimageio/core/backends/pytorch_backend.py index 3323f784..efb9b933 100644 --- a/src/bioimageio/core/backends/pytorch_backend.py +++ b/src/bioimageio/core/backends/pytorch_backend.py @@ -65,9 +65,7 @@ def __init__( self._mode: Literal["eval", "train"] = mode super().__init__(model_description=model_description, devices=devices) - def _parse_devices( - self, devices: Sequence[str] | None - ) -> Sequence[torch.device]: + def _parse_devices(self, devices: Sequence[str] | None) -> Sequence[torch.device]: return get_devices(devices) def _init_model_on_device(self, device: torch.device) -> nn.Module: @@ -164,7 +162,7 @@ def load_torch_model( else: callable_name = weight_spec.architecture.callable - raise ValueError(f"Calling {callable_name} did not return a torch.nn.Module.") + raise TypeError(f"Calling {callable_name} did not return a torch.nn.Module.") if load_state or devices: use_devices = get_devices(devices) diff --git a/src/bioimageio/core/cli.py b/src/bioimageio/core/cli.py index 8782c9ce..c2e62111 100644 --- a/src/bioimageio/core/cli.py +++ b/src/bioimageio/core/cli.py @@ -3,6 +3,7 @@ Note: Some docstrings use a hair space ' ' to place the added '(default: ...)' on a new line. """ + from __future__ import annotations import json @@ -259,7 +260,7 @@ class PackageCmd(CmdBase, WithSource, WithSummaryLogging): def cli_cmd(self): if isinstance(self.descr, InvalidDescr): self.log(self.descr) - raise ValueError(f"Invalid {self.descr.type} description.") + raise TypeError(f"Invalid {self.descr.type} description.") sys.exit( package( diff --git a/src/bioimageio/core/commands.py b/src/bioimageio/core/commands.py index cac8e2ad..c348ecb8 100644 --- a/src/bioimageio/core/commands.py +++ b/src/bioimageio/core/commands.py @@ -1,5 +1,6 @@ """These functions are used in the bioimageio command line interface defined in `bioimageio.core.cli`.""" + from __future__ import annotations from collections.abc import Sequence @@ -45,8 +46,11 @@ def test( *, weight_format: WeightFormatArgAll = "all", devices: str | Sequence[str] | None = None, - summary: Literal["display"] | Path | Sequence[Literal["display"] | Path] = "display", - runtime_env: Literal["currently-active", "as-described"] | Path = "currently-active", + summary: Literal["display"] + | Path + | Sequence[Literal["display"] | Path] = "display", + runtime_env: Literal["currently-active", "as-described"] + | Path = "currently-active", determinism: Literal["seed_only", "full"] = "seed_only", format_version: FormatVersionPlaceholder | str = "discover", working_dir: Path | None = None, @@ -109,7 +113,7 @@ def package( if logged: msg += f" Details saved to {logged}." - raise ValueError(msg) + raise TypeError(msg) if weight_format == "all": weights_priority_order = None diff --git a/src/bioimageio/core/digest_spec.py b/src/bioimageio/core/digest_spec.py index e8881bf5..ceb2a8b8 100644 --- a/src/bioimageio/core/digest_spec.py +++ b/src/bioimageio/core/digest_spec.py @@ -53,7 +53,11 @@ def import_callable( - node: ArchitectureFromFileDescr | ArchitectureFromLibraryDescr | CallableFromDepencency | CallableFromFile | v0_5.CustomProcessingDescr, + node: ArchitectureFromFileDescr + | ArchitectureFromLibraryDescr + | CallableFromDepencency + | CallableFromFile + | v0_5.CustomProcessingDescr, /, **kwargs: Unpack[HashKwargs], ) -> Callable[..., Any]: @@ -72,7 +76,7 @@ def import_callable( assert_never(node) if not callable(c): - raise ValueError(f"{node} (imported: {c}) is not callable") + raise TypeError(f"{node} (imported: {c}) is not callable") return c @@ -160,14 +164,20 @@ def _import_from_file_impl( def get_axes_infos( - io_descr: v0_4.InputTensorDescr | v0_4.OutputTensorDescr | v0_5.InputTensorDescr | v0_5.OutputTensorDescr, + io_descr: v0_4.InputTensorDescr + | v0_4.OutputTensorDescr + | v0_5.InputTensorDescr + | v0_5.OutputTensorDescr, ) -> list[AxisInfo]: """get a unified, simplified axis representation from spec axes""" return [AxisInfo.create(a) for a in io_descr.axes] def get_member_id( - tensor_description: v0_4.InputTensorDescr | v0_4.OutputTensorDescr | v0_5.InputTensorDescr | v0_5.OutputTensorDescr, + tensor_description: v0_4.InputTensorDescr + | v0_4.OutputTensorDescr + | v0_5.InputTensorDescr + | v0_5.OutputTensorDescr, ) -> MemberId: """get the normalized tensor ID, usable as a sample member ID""" @@ -183,7 +193,10 @@ def get_member_id( def get_member_ids( tensor_descriptions: Iterable[ - v0_4.InputTensorDescr | v0_4.OutputTensorDescr | v0_5.InputTensorDescr | v0_5.OutputTensorDescr + v0_4.InputTensorDescr + | v0_4.OutputTensorDescr + | v0_5.InputTensorDescr + | v0_5.OutputTensorDescr ], ) -> list[MemberId]: """get normalized tensor IDs to be used as sample member IDs""" @@ -222,7 +235,9 @@ def get_test_output_sample(model: AnyModelDescr) -> Sample: def _get_test_sample( - info: Mapping[MemberId, v0_5.InputTensorDescr | v0_5.OutputTensorDescr] | Mapping[MemberId, tuple[v0_4.InputTensorDescr, FileSource]] | Mapping[MemberId, tuple[v0_4.OutputTensorDescr, FileSource]], + info: Mapping[MemberId, v0_5.InputTensorDescr | v0_5.OutputTensorDescr] + | Mapping[MemberId, tuple[v0_4.InputTensorDescr, FileSource]] + | Mapping[MemberId, tuple[v0_4.OutputTensorDescr, FileSource]], ) -> Sample: arrays: dict[MemberId, NDArray[Any]] = {} for m, src in info.items(): @@ -276,7 +291,7 @@ def get_input_halo( for a, ah in th.items(): s = axes[a].size if not isinstance(s, v0_5.SizeReference): - raise ValueError( + raise TypeError( f"Unable to map output halo for {t}.{a} to an input axis" ) @@ -398,7 +413,11 @@ def get_io_sample_block_metas( def get_tensor( src: TensorSource, - descr: v0_4.InputTensorDescr | v0_5.InputTensorDescr | v0_4.OutputTensorDescr | v0_5.OutputTensorDescr | Sequence[AxisInfo], + descr: v0_4.InputTensorDescr + | v0_5.InputTensorDescr + | v0_4.OutputTensorDescr + | v0_5.OutputTensorDescr + | Sequence[AxisInfo], *, extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "squeeze", missing_dims: Literal["raise", "expand", "unstack", "unstack_or_expand"] = "raise", diff --git a/src/bioimageio/core/prediction.py b/src/bioimageio/core/prediction.py index 0998c449..6596436e 100644 --- a/src/bioimageio/core/prediction.py +++ b/src/bioimageio/core/prediction.py @@ -21,7 +21,10 @@ def predict( *, - model: PermissiveFileSource | v0_4.ModelDescr | v0_5.ModelDescr | PredictionPipeline, + model: PermissiveFileSource + | v0_4.ModelDescr + | v0_5.ModelDescr + | PredictionPipeline, inputs: Sample | PerMember[TensorSource] | TensorSource, sample_id: Hashable = "sample", blocksize_parameter: BlocksizeParameter | None = None, @@ -62,7 +65,7 @@ def predict( if not isinstance(model, (v0_4.ModelDescr, v0_5.ModelDescr)): loaded = load_description(model) if not isinstance(loaded, (v0_4.ModelDescr, v0_5.ModelDescr)): - raise ValueError(f"expected model description, but got {loaded}") + raise TypeError(f"expected model description, but got {loaded}") model = loaded pp = create_prediction_pipeline( @@ -125,10 +128,15 @@ def predict( def predict_many( *, - model: PermissiveFileSource | v0_4.ModelDescr | v0_5.ModelDescr | PredictionPipeline, + model: PermissiveFileSource + | v0_4.ModelDescr + | v0_5.ModelDescr + | PredictionPipeline, inputs: Iterable[PerMember[TensorSource]] | Iterable[TensorSource], sample_id: str = "sample{i:03}", - blocksize_parameter: v0_5.ParameterizedSize_N | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] | None = None, + blocksize_parameter: v0_5.ParameterizedSize_N + | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] + | None = None, skip_preprocessing: bool = False, skip_postprocessing: bool = False, save_output_path: Path | str | None = None, @@ -164,7 +172,7 @@ def predict_many( if not isinstance(model, (v0_4.ModelDescr, v0_5.ModelDescr)): loaded = load_description(model) if not isinstance(loaded, (v0_4.ModelDescr, v0_5.ModelDescr)): - raise ValueError(f"expected model description, but got {loaded}") + raise TypeError(f"expected model description, but got {loaded}") model = loaded pp = create_prediction_pipeline(model) diff --git a/src/bioimageio/core/proc_ops.py b/src/bioimageio/core/proc_ops.py index ff4a1545..2ac88a2d 100644 --- a/src/bioimageio/core/proc_ops.py +++ b/src/bioimageio/core/proc_ops.py @@ -454,7 +454,13 @@ def from_proc_descr( def _get_axes( - kwargs: v0_4.ZeroMeanUnitVarianceKwargs | v0_5.ZeroMeanUnitVarianceKwargs | v0_4.ScaleRangeKwargs | v0_5.ScaleRangeKwargs | v0_4.ScaleMeanVarianceKwargs | v0_5.ScaleMeanVarianceKwargs | v0_5.ClipKwargs, + kwargs: v0_4.ZeroMeanUnitVarianceKwargs + | v0_5.ZeroMeanUnitVarianceKwargs + | v0_4.ScaleRangeKwargs + | v0_5.ScaleRangeKwargs + | v0_4.ScaleMeanVarianceKwargs + | v0_5.ScaleMeanVarianceKwargs + | v0_5.ClipKwargs, ) -> tuple[bool, tuple[AxisId, ...] | None]: if kwargs.axes is None: return True, None @@ -588,11 +594,13 @@ def get_descr(self): return v0_5.SigmoidDescr() +CHANNEL_AXIS_ID = AxisId("channel") + @dataclass class Softmax(SimpleOperator): """Softmax activation function.""" - axis: AxisId = AxisId("channel") + axis: AxisId = CHANNEL_AXIS_ID def _apply(self, x: Tensor, stat: Stat) -> Tensor: axis_idx = x.dims.index(self.axis) @@ -830,7 +838,10 @@ def from_proc_descr( def get_proc( proc_descr: ProcDescr, - tensor_descr: v0_4.InputTensorDescr | v0_4.OutputTensorDescr | v0_5.InputTensorDescr | v0_5.OutputTensorDescr, + tensor_descr: v0_4.InputTensorDescr + | v0_4.OutputTensorDescr + | v0_5.InputTensorDescr + | v0_5.OutputTensorDescr, ) -> Processing: member_id = get_member_id(tensor_descr) @@ -873,14 +884,12 @@ def get_proc( elif isinstance(proc_descr, v0_5.SoftmaxDescr): return Softmax.from_proc_descr(proc_descr, member_id) elif isinstance(proc_descr, v0_5.StardistPostprocessingDescr): - if isinstance(proc_descr.kwargs, v0_5.StardistPostprocessingKwargs2D): + if len(proc_descr.kwargs.grid) == 2: return StardistPostprocessing2D.from_proc_descr(proc_descr, member_id) - elif isinstance(proc_descr.kwargs, v0_5.StardistPostprocessingKwargs3D): + elif len(proc_descr.kwargs.grid) == 3: return StardistPostprocessing3D.from_proc_descr(proc_descr, member_id) else: - raise ValueError( - f"expected ndim 2 or 3 for stardist postprocessing, but got {proc_descr.kwargs.ndim}" - ) + assert_never(proc_descr.kwargs.grid) elif isinstance( proc_descr, (v0_4.ZeroMeanUnitVarianceDescr, v0_5.ZeroMeanUnitVarianceDescr), diff --git a/src/bioimageio/core/remote_backends/__init__.py b/src/bioimageio/core/remote_backends/__init__.py index e6f52b31..03169b78 100644 --- a/src/bioimageio/core/remote_backends/__init__.py +++ b/src/bioimageio/core/remote_backends/__init__.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Literal, Optional +from typing import TYPE_CHECKING, Literal from typing_extensions import assert_never diff --git a/src/bioimageio/core/stat_measures.py b/src/bioimageio/core/stat_measures.py index b9475297..461ad057 100644 --- a/src/bioimageio/core/stat_measures.py +++ b/src/bioimageio/core/stat_measures.py @@ -84,14 +84,14 @@ def compute(self, sample: SampleLike) -> MeasureValue: tensor = sample.members[self.member_id] return tensor.mean(dim=self.axes) - def model_post_init(self, __context: Any): + def model_post_init(self, __context: Any, /): assert self.axes is None or AxisId("batch") not in self.axes class DatasetMean(_Mean, DatasetMeasureBase, frozen=True): """The mean value across multiple samples""" - def model_post_init(self, __context: Any): + def model_post_init(self, __context: Any, /): assert self.axes is None or AxisId("batch") in self.axes @@ -108,14 +108,14 @@ def compute(self, sample: SampleLike) -> MeasureValue: tensor = sample.members[self.member_id] return tensor.std(dim=self.axes) - def model_post_init(self, __context: Any): + def model_post_init(self, __context: Any, /): assert self.axes is None or AxisId("batch") not in self.axes class DatasetStd(_Std, DatasetMeasureBase, frozen=True): """The standard deviation across multiple samples""" - def model_post_init(self, __context: Any): + def model_post_init(self, __context: Any, /): assert self.axes is None or AxisId("batch") in self.axes @@ -132,14 +132,14 @@ def compute(self, sample: SampleLike) -> MeasureValue: tensor = sample.members[self.member_id] return tensor.var(dim=self.axes) - def model_post_init(self, __context: Any): + def model_post_init(self, __context: Any, /): assert self.axes is None or AxisId("batch") not in self.axes class DatasetVar(_Var, DatasetMeasureBase, frozen=True): """The variance across multiple samples""" - def model_post_init(self, __context: Any): # TODO: turn into @model_validator + def model_post_init(self, __context: Any, /): # TODO: turn into @model_validator assert self.axes is None or AxisId("batch") in self.axes @@ -149,7 +149,7 @@ class _Quantile(BaseModel, frozen=True): axes: tuple[AxisId, ...] | None = None """`axes` to reduce""" - def model_post_init(self, __context: Any): + def model_post_init(self, __context: Any, /): assert self.q >= 0.0 assert self.q <= 1.0 @@ -165,7 +165,7 @@ def compute(self, sample: SampleLike) -> MeasureValue: tensor = sample.members[self.member_id] return tensor.quantile(self.q, dim=self.axes, method=self.method) - def model_post_init(self, __context: Any): + def model_post_init(self, __context: Any, /): super().model_post_init(__context) assert self.axes is None or AxisId("batch") not in self.axes @@ -173,7 +173,7 @@ def model_post_init(self, __context: Any): class DatasetQuantile(_Quantile, DatasetMeasureBase, frozen=True): """The `q`th quantile across multiple samples""" - def model_post_init(self, __context: Any): + def model_post_init(self, __context: Any, /): super().model_post_init(__context) assert self.axes is None or AxisId("batch") in self.axes diff --git a/tests/test_cli.py b/tests/test_cli.py index f4e229a9..8081ad63 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -18,6 +18,7 @@ def run_subprocess( stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8", + check=False, **kwargs, ) From d6b252177c94589e9763f0d132eb2666c59a44f8 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Mon, 3 Aug 2026 13:48:03 +0200 Subject: [PATCH 11/85] allow creating singleton block shapes along missing axes --- src/bioimageio/core/_prediction_pipeline.py | 42 ++++++++++++++++----- src/bioimageio/core/block_meta.py | 8 ++-- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/bioimageio/core/_prediction_pipeline.py b/src/bioimageio/core/_prediction_pipeline.py index 8321b9fb..ee641646 100644 --- a/src/bioimageio/core/_prediction_pipeline.py +++ b/src/bioimageio/core/_prediction_pipeline.py @@ -65,7 +65,10 @@ def __init__( *, default_blocksize_parameter: BlocksizeParameter, default_batch_size: int, - preceding_prediction_pipelines: Sequence[PredictionPipeline | RemotePredictionPipeline] | None, + preceding_prediction_pipelines: Sequence[ + PredictionPipeline | RemotePredictionPipeline + ] + | None, ) -> None: super().__init__() self._model_descr = model_descr @@ -206,7 +209,9 @@ def predict_sample_with_blocking( sample: Sample, skip_preprocessing: bool = False, skip_postprocessing: bool = False, - ns: v0_5.ParameterizedSize_N | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] | None = None, + ns: v0_5.ParameterizedSize_N + | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] + | None = None, batch_size: int | None = None, ) -> Sample: """Predict a sample by predicting sample blocks. @@ -278,7 +283,9 @@ def predict_sample_with_blocking_yield_intermediates( sample: Sample, skip_preprocessing: bool = False, skip_postprocessing: bool = False, - ns: v0_5.ParameterizedSize_N | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] | None = None, + ns: v0_5.ParameterizedSize_N + | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] + | None = None, batch_size: int | None = None, ) -> tuple[int, Iterable[IntermediatePrediction]]: """Predict `sample` by predicting sample blocks and yield intermediate predictions if no samplewise postprocessing is included. @@ -323,7 +330,7 @@ def predict_sample_with_blocking_yield_intermediates( if isinstance(a.size, v0_5.ParameterizedSize) } input_block_shape = self._model_descr.get_tensor_sizes( - ns, batch_size or self._default_batch_size + ns, batch_size or self._default_batch_size, sample.shape ).inputs steps, intermediates = ( @@ -431,7 +438,10 @@ def __init__( model_adapter: ModelAdapter, default_blocksize_parameter: BlocksizeParameter = 10, default_batch_size: int = 1, - preceding_prediction_pipelines: Sequence[PredictionPipeline | RemotePredictionPipeline] | None = None, + preceding_prediction_pipelines: Sequence[ + PredictionPipeline | RemotePredictionPipeline + ] + | None = None, ) -> None: """Consider using `create_prediction_pipeline` to create a `PredictionPipeline` with sensible defaults.""" super().__init__( @@ -604,7 +614,9 @@ def predict_sample_with_blocking( sample: Sample, skip_preprocessing: bool = False, skip_postprocessing: bool = False, - ns: v0_5.ParameterizedSize_N | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] | None = None, + ns: v0_5.ParameterizedSize_N + | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] + | None = None, batch_size: int | None = None, ) -> Sample: output = None @@ -800,7 +812,10 @@ def __init__( server: str, default_blocksize_parameter: BlocksizeParameter, default_batch_size: int, - preceding_prediction_pipelines: Sequence[PredictionPipeline | RemotePredictionPipeline] | None = None, + preceding_prediction_pipelines: Sequence[ + PredictionPipeline | RemotePredictionPipeline + ] + | None = None, ) -> None: super().__init__( model_descr, @@ -827,7 +842,10 @@ def create_prediction_pipeline( model_adapter: ModelAdapter | None = None, ns: BlocksizeParameter | None = None, default_blocksize_parameter: BlocksizeParameter = 10, # TODO: default to None and find smart blocksize params per axis to reduce overlap of blocks with large halo - preceding_prediction_pipelines: Sequence[PredictionPipeline | RemotePredictionPipeline] | None = None, + preceding_prediction_pipelines: Sequence[ + PredictionPipeline | RemotePredictionPipeline + ] + | None = None, **deprecated_kwargs: Any, ) -> PredictionPipeline: """ @@ -932,7 +950,7 @@ def _get_preceding_model_ids(model: AnyModelDescr) -> set[v0_5.ModelId]: ), ) - return PredictionPipeline( + pp = PredictionPipeline( name=bioimageio_model.name, model_description=bioimageio_model, model_adapter=model_adapter, @@ -941,6 +959,12 @@ def _get_preceding_model_ids(model: AnyModelDescr) -> set[v0_5.ModelId]: default_blocksize_parameter=default_blocksize_parameter, preceding_prediction_pipelines=preceding_prediction_pipelines, ) + logger.info( + "Created prediction pipeline for '{}' with {} adapter", + bioimageio_model.name, + model_adapter.__class__.__name__, + ) + return pp def create_remote_prediction_pipeline( diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py index c9328e33..dc4584ea 100644 --- a/src/bioimageio/core/block_meta.py +++ b/src/bioimageio/core/block_meta.py @@ -205,9 +205,7 @@ def __post_init__(self): "block {} larger than sample {}", self.shape, self.sample_shape ) - def get_transformed( - self, new_axes: PerAxis[LinearAxisTransform | int] - ) -> Self: + def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: return self.__class__( sample_shape={ a: ( @@ -247,13 +245,13 @@ def split_shape_into_blocks( halo: PerAxis[HaloLike], stride: PerAxis[int] | None = None, ) -> tuple[TotalNumberOfBlocks, Generator[BlockMeta, Any, None]]: - unknown_axes = [a for a in block_shape if a not in shape] + unknown_axes = [a for a in block_shape if a not in shape and block_shape[a] != 1] if unknown_axes: raise ValueError( f"unknown axes in block_shape: {unknown_axes} for shape {shape}" ) - if any(shape[a] < block_shape[a] for a in block_shape): + if any(shape.get(a, 1) < block_shape[a] for a in block_shape): # TODO: allow larger blockshape raise ValueError(f"shape {shape} is smaller than block shape {block_shape}") From 4b0bd09c39d7464a8ef853361ca34425f7d4fc1d Mon Sep 17 00:00:00 2001 From: fynnbe Date: Thu, 6 Aug 2026 16:42:55 +0200 Subject: [PATCH 12/85] add eval-type-backport --- pyproject.toml | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 91c162a7..009dd560 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -55,34 +55,35 @@ stardist = ["stardist"] # for stardist postprocessing gradio-server = ['gradio[mcp];python_version>="3.10"'] gradio-client = ['gradio_client;python_version>="3.10"'] dev = [ - "cellpose<4.2", # for model testing + "cellpose<4.2", # for model testing "crick", "dask[array]", - 'gradio[mcp];python_version>="3.10"', - 'gradio_client;python_version>="3.10"', "httpx", "jax", "jupyter", "keras>=3.0,<4", "matplotlib", - "monai", # for model testing + "monai", # for model testing "numpy", "onnx", "onnxruntime", "onnxscript", - 'onnx_ir!=0.1.14;python_version<"3.10"', # uses typing.Concatentate which requires py>=3.10 "packaging>=17.0", "pre-commit", "pyright==1.1.411", - "pytest-cov", "pytest", + "pytest-cov", "python-dotenv", - "segment-anything", # for model testing + "segment-anything", # for model testing "stardist", "tensorflow", - "timm", # for model testing + "timm", # for model testing "torch>=1.6,<3", "torchvision>=0.21", + 'eval-type-backport;python_version<"3.10"', # TODO: remove when py3.10 is lowest supported version + 'gradio[mcp];python_version>="3.10"', + 'gradio_client;python_version>="3.10"', + 'onnx_ir!=0.1.14;python_version<"3.10"', # uses typing.Concatentate which requires py>=3.10 ] docs = [ "griffe-pydantic", From faab1766bbafc4b4b83cd179922d8e46927fcd91 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Fri, 14 Aug 2026 11:15:01 +0200 Subject: [PATCH 13/85] load_tensor bioio/imageio/clearscale/numpy --- pyproject.toml | 14 +- src/bioimageio/core/axis.py | 49 +- src/bioimageio/core/io.py | 580 +++++++++++++++--- .../core/remote_backends/gradio/server.py | 7 +- src/bioimageio/core/tensor.py | 168 ++++- src/bioimageio/core/utils/_io_zarr.py | 136 ++++ src/bioimageio/core/utils/_type_guards.py | 2 + tests/test_io.py | 107 +++- 8 files changed, 955 insertions(+), 108 deletions(-) create mode 100644 src/bioimageio/core/utils/_io_zarr.py diff --git a/pyproject.toml b/pyproject.toml index 009dd560..c0a666a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ requires-python = ">=3.9" readme = "README.md" dynamic = ["version"] dependencies = [ - "bioimageio.spec ==0.5.12.0", + "bioimageio.spec ==0.5.13.0", "imagecodecs", "imageio>=2.10", "loguru", @@ -54,7 +54,19 @@ partners = [ stardist = ["stardist"] # for stardist postprocessing gradio-server = ['gradio[mcp];python_version>="3.10"'] gradio-client = ['gradio_client;python_version>="3.10"'] +bioio = [ + "bioio-bioformats", + "bioio-imageio", + "bioio-ome-tiff", + "bioio-ome-zarr", + "dask[array]", +] +# clearscale = ["clearscale", "dask[array]", "zarr"] # TODO: add clearscale extra deps once clearscale is available on pypi dev = [ + "bioio-bioformats", + "bioio-imageio", + "bioio-ome-tiff", + "bioio-ome-zarr", "cellpose<4.2", # for model testing "crick", "dask[array]", diff --git a/src/bioimageio/core/axis.py b/src/bioimageio/core/axis.py index 10619c42..af254fdf 100644 --- a/src/bioimageio/core/axis.py +++ b/src/bioimageio/core/axis.py @@ -14,22 +14,24 @@ def _guess_axis_type(a: str): + a = a.lower() if a in ("b", "batch"): return "batch" elif a in ("t", "time"): return "time" - elif a in ("i", "index"): + elif a in ("s", "sample", "i", "index"): return "index" elif a in ("c", "channel"): return "channel" - elif a in ("x", "y", "z"): + elif a in ("x", "y", "z") or a.startswith("space"): return "space" else: raise ValueError( f"Failed to infer axis type for axis id '{a}'." - + " Consider using one of: '" - + "b', 'batch', 't', 'time', 'i', 'index', 'c', 'channel', 'x', 'y', 'z" - + "'. Or creating an `Axis` object instead." + + " Consider using one of:" + + " 'b', 'batch', 't', 'time', 's', 'sample', 'i'," + + " 'index', 'c', 'channel', 'x', 'y', 'z'," + + " 'space*'. Or create an `Axis` object instead.", ) @@ -103,9 +105,7 @@ class AxisInfo(Axis): size: AxisSize @classmethod - def create( - cls, axis: AxisLike, size: int | AxisSize | None = None - ) -> AxisInfo: + def create(cls, axis: AxisLike, size: int | AxisSize | None = None) -> AxisInfo: if isinstance(axis, AxisInfo): return axis @@ -139,3 +139,36 @@ def create( size = AxisSize(min=size, max=size) return AxisInfo(id=axis_base.id, type=axis_base.type, size=size) + + +def single_letter_dims_if_possible( + dims: tuple[AxisId, ...], +) -> tuple[str, ...]: + """Return a tuple of single-letter dimension names if possible, otherwise return the original dimension names.""" + single_letter_dims: list[str] = [] + + def add_letter(d: str): + assert len(d) == 1 + not_unique = d in single_letter_dims + single_letter_dims.append(d) + return not_unique + + for d in dims: + d = str(d).lower() + if d in ("batch", "b"): + not_unique = add_letter("b") + elif d in ("time", "t"): + not_unique = add_letter("t") + elif d in ("index", "i"): + not_unique = add_letter("i") + elif d in ("channel", "c"): + not_unique = add_letter("c") + elif d in "zyx": + not_unique = add_letter(d) + else: + return dims # Return original dims if any dim cannot be converted to a single letter + + if not_unique: + return dims # Return original dims if any single letter dim is not unique + + return tuple(single_letter_dims) diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index 02110516..e0298a61 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -2,39 +2,50 @@ import collections.abc import json +import os import warnings import zipfile from collections.abc import Mapping, Sequence from contextlib import nullcontext from io import BytesIO -from pathlib import Path +from itertools import chain +from pathlib import Path, PosixPath, PurePath from shutil import copyfileobj -from typing import ( - TYPE_CHECKING, - Union, -) +from typing import TYPE_CHECKING, TypedDict, Union +import numpy as np import xarray as xr -from imageio.v3 import imwrite # type: ignore +from exceptiongroup import ExceptionGroup +from imageio.v3 import ( + imread, # pyright: ignore[reportUnknownVariableType] + imwrite, # type: ignore +) from loguru import logger -from pydantic import BaseModel, RootModel -from typing_extensions import TypeAlias +from pydantic import BaseModel, FilePath, RootModel +from typing_extensions import Literal, TypeAlias, assert_never from typing_extensions import TypeAliasType as _TypeAliasType -from bioimageio.spec._internal.io import get_reader +from bioimageio.spec import get_validation_context +from bioimageio.spec._internal.io import get_reader, interprete_file_source from bioimageio.spec.common import ( BytesReader, + FileDescr, FileSource, + HttpUrl, PermissiveFileSource, + RelativeFilePath, + RootHttpUrl, ZipPath, ) -from bioimageio.spec.utils import load_image, save_array +from bioimageio.spec.model import v0_5 +from bioimageio.spec.utils import load_array, save_array -from .axis import AxisId, AxisLike +from .axis import AxisId, AxisLike, single_letter_dims_if_possible from .common import PerMember from .sample import Sample from .stat_measures import DatasetMeasure, MeasureValue, SampleMeasure, Stat from .tensor import Tensor +from .utils._io_zarr import open_zarr_multiscale_array if TYPE_CHECKING: JsonValue: TypeAlias = Union[ @@ -50,83 +61,517 @@ Union[bool, int, float, str, None, list["JsonValue"], dict[str, "JsonValue"]], ) +JsonValueReadOnly: TypeAlias = Union[ + bool, + int, + float, + str, + None, + Sequence["JsonValueReadOnly"], + Mapping[str, "JsonValueReadOnly"], +] + +IO_Lib: TypeAlias = Literal["bioio", "imageio", "clearscale", "numpy"] + def load_tensor( - source: PermissiveFileSource | ZipPath, + source: FileDescr | PermissiveFileSource | ZipPath, /, axes: Sequence[AxisLike] | None = None, + io_lib: IO_Lib | None = None, ) -> Tensor: - # TODO: load axis meta data - array = load_image(source) + """Load an image tensor - return Tensor.from_array(array, dims=axes) + Args: + io_lib: library to use for loading the image. + Defaults to trying various libraries depending on file extension. + """ + file_source, extension, subdir = _interprete_tensor_source(source) + if io_lib is None: + try_io_libs = _select_io_libs_based_on_extension(extension) + + exceptions: list[Exception] = [] + for auto_io_lib in try_io_libs: + try: + return load_tensor(source, axes=axes, io_lib=auto_io_lib) + except Exception as e: + exceptions.append(e) + logger.opt(exception=e).warning( + "Failed to load tensor with io_lib={}", auto_io_lib + ) + + raise ExceptionGroup( + "Failed to load tensor with any available io_lib", exceptions + ) + elif io_lib == "bioio": + t = _load_tensor_bioio(file_source, None if subdir is None else str(subdir)) + elif io_lib == "clearscale": + t = _load_tensor_clearscale(source) + elif io_lib == "numpy": + array = load_array(source) + t = Tensor.from_array(array, dims=axes) + axes = None + elif io_lib == "imageio": + reader = get_reader(source) + array = imread(reader.read(), extension=extension) # pyright: ignore[reportArgumentType] + t = Tensor.from_array(array, dims=axes) + axes = None + else: + assert_never(io_lib) -Suffix = str + if axes is not None: + t = t.transpose(axes) + return t -def save_tensor(path: Path | str, tensor: Tensor) -> None: - # TODO: save axis meta data - path = Path(path) - if not path.suffix: - raise ValueError(f"No suffix (needed to decide file format) found in {path}") +def _select_io_libs_based_on_extension(extension: str | None) -> tuple[IO_Lib, ...]: + if extension is None: + return ("bioio", "imageio", "clearscale") - extension = path.suffix.lower() - path.parent.mkdir(exist_ok=True, parents=True) + extension = extension.lower() if extension == ".npy": - save_array(path, tensor.to_numpy()) - elif extension in (".h5", ".hdf", ".hdf5"): - raise NotImplementedError("Saving to h5 with dataset path is not implemented.") + return ("numpy",) + elif extension == ".zarr": + return ("bioio", "clearscale") else: - removed_singleton_axes: list[AxisId] = [] - remove_singletons = { - AxisId("batch"): [ - ".tif", - ".tiff", - ], # remove singleton batch dim for tiff files - **{ - a: [".png", ".jpg", ".jpeg"] for a in tensor.dims - }, # remove any singleton axis for png and jpg files - } - for rm_a, rm_ext in remove_singletons.items(): - if extension in rm_ext and tensor.tagged_shape.get(rm_a) == 1: - tensor = tensor[{rm_a: 0}] - removed_singleton_axes.append(rm_a) + return ("bioio", "imageio", "clearscale") - if removed_singleton_axes: - singleton_axes_msg = f"(with removed singleton axes {list(map(str, removed_singleton_axes))}) " - else: - singleton_axes_msg = "" - logger.info( - "writing tensor {} {}to {}", - dict(tensor.tagged_shape), - singleton_axes_msg, - path, +def _load_tensor_bioio( + source: HttpUrl | FilePath | ZipPath, + subdir: str | None, +) -> Tensor: + """Load an image tensor using bioio""" + + import bioio + + if isinstance(source, FileDescr): + src = source.source + else: + src = source + + if isinstance(src, RelativeFilePath): + src = src.absolute() + + del source + + img = bioio.BioImage(src) + if subdir is not None: + img.set_scene(subdir) + + dataarray_bioio = img.xarray_dask_data.squeeze() # pyright: ignore[reportUnknownVariableType] + assert isinstance(dataarray_bioio, xr.DataArray) + + # map bioio TCZYX/YXS axes to AxisId objects + # (e.g. "T" -> AxisId("time"), "C" -> AxisId("channel"), "S" -> AxisId("channel")) + axes_map = { + d: AxisId("channel") if d == "S" else AxisId(d) for d in dataarray_bioio.dims + } + dataarray = dataarray_bioio.rename(axes_map) # pyright: ignore[reportUnknownVariableType] + assert isinstance(dataarray, xr.DataArray) + + t = Tensor.from_xarray(dataarray) + + # add physical scale + scale_bioio: dict[str, float | None] = { + "T": img.scale.T, + # "C": img.scale.C, # should be 'dimensionless' + "Z": img.scale.Z, + "Y": img.scale.Y, + "X": img.scale.X, + } + unit_bioio: dict[str, str | None] = { + "T": img.dimension_properties.T.unit, + # "C": img.dimension_properties.C.unit, # should be 'dimensionless' + "Z": img.dimension_properties.Z.unit, + "Y": img.dimension_properties.Y.unit, + "X": img.dimension_properties.X.unit, + } + scale = {axes_map[k]: v for k, v in scale_bioio.items() if v is not None} + unit = {axes_map[k]: str(v) for k, v in unit_bioio.items() if v is not None} + t.set_physical_scale(scale) + t.set_physical_scale_unit(unit) + return t + + +def _load_tensor_clearscale(source: FileDescr | PermissiveFileSource) -> Tensor: + """load a single image as numpy array + + Args: + source: image source + subdir: image name + """ + import dask.array as da + + if isinstance(source, FileDescr): + source = source.source.absolute() + + if isinstance(source, RelativeFilePath): + source = source.absolute() + + if isinstance(source, ZipPath): + raise NotImplementedError( + "clearscale: Loading from zip files is not implemented." ) - if extension in (".png", ".jpg", ".jpeg") and tensor.dtype in ( - "float32", - "float64", - ): - logger.warning( - "converting tensor of dtype {} to uint8 for saving as {}", - tensor.dtype, - extension, + + array, ms = open_zarr_multiscale_array( + source.as_posix() if isinstance(source, Path) else str(source) + ) + image = da.from_zarr(array) + axes = tuple(str(a) for a in ms.axes()) + + return Tensor.from_xarray(xr.DataArray(image, dims=axes)) + + +Suffix = str + + +def save_tensor( + path: Path | str, + tensor: Tensor, + io_lib: IO_Lib | None = None, +) -> None: + output_path, extension, subdir = _interprete_tensor_source(path) + if isinstance(output_path, RootHttpUrl): + raise NotImplementedError( + "Saving tensors to HTTP URLs is not implemented. Please save to a local file path." + ) + + if io_lib is None: + try_io_libs = _select_io_libs_based_on_extension(extension) + exceptions: list[Exception] = [] + for try_io_lib in try_io_libs: + try: + return save_tensor(path, tensor, io_lib=try_io_lib) + except Exception as e: + exceptions.append(e) + logger.opt(exception=e).warning( + "Failed to save tensor with io_lib={}", try_io_lib + ) + + raise ExceptionGroup( + "Failed to save tensor with any available io_lib", exceptions + ) + elif io_lib == "bioio": + return _save_tensor_bioio(output_path, tensor, subdir=subdir) + elif io_lib == "imageio": + return _save_tensor_imageio(output_path, tensor) + elif io_lib == "clearscale": + return _save_tensor_clearscale(output_path, tensor, subdir=subdir) + elif io_lib == "numpy": + return _save_tensor_numpy(output_path, tensor) + else: + assert_never(io_lib) + + +def _save_tensor_bioio( + path: Path | ZipPath, tensor: Tensor, subdir: PurePath | None +) -> None: + """Save an image tensor using bioio""" + + bioio_axes_map = { + "batch": "S", + "channel": "C", + "index": "S", + "time": "T", + } + if path.suffix.lower() in (".tif", ".tiff"): + from bioio_ome_tiff.writers import OmeTiffWriter + + save = OmeTiffWriter.save # pyright: ignore[reportUnknownVariableType] + write = None + bioio_axis_letters = "TCZYX" + elif path.suffix.lower() == ".zarr": + import bioio_ome_zarr.writers + + axes_names = single_letter_dims_if_possible(tensor.dims) + axes_types = [ + {"time": "time", "t": "time", "channel": "channel", "c": "channel"}.get( + str(a).lower(), "space" + ) + for a in tensor.dims + ] + unit = tensor.get_physical_scale_unit() + axes_units = [unit.get(a) for a in tensor.dims] + + scale = tensor.get_physical_scale() + physical_pixel_size = [scale.get(a, 1.0) for a in tensor.dims] + + channel_names = tensor.channel_names + channel_colors = tensor.channel_colors + if channel_colors is None and channel_names is not None: + # use spec's default channel colors + channel_colors = [ + c.as_hex(format="long")[:6] + for c in v0_5.ChannelAxis(channel_names=channel_names).channel_colors + ] + + channels = [ + bioio_ome_zarr.writers.Channel( + label=f"channel{i}" if channel_names is None else channel_names[i], + color="#FF0000" if channel_colors is None else channel_colors[i], + ) + for i in range(tensor.tagged_shape.get(AxisId("channel"), 0)) + ] + zf = int(os.getenv("ZARR_FORMAT", "3")) + if zf not in (2, 3): + raise NotImplementedError( + f"ZARR_FORMAT={zf} is not supported. (expected 2 or 3)" + ) + + save = None + write = bioio_ome_zarr.writers.OMEZarrWriter( + store=path, + level_shapes=[tensor.shape], + dtype=tensor.dtype, + zarr_format=zf, + image_name="Image" if subdir is None else str(subdir), + axes_names=axes_names, + axes_types=axes_types, + axes_units=axes_units, + physical_pixel_size=physical_pixel_size, + channels=channels, + ).write_full_volume + bioio_axis_letters = "TCZYX" + elif path.suffix.lower() in (".gif", ".mp4", ".mkv"): + from bioio_imageio.writers import TimeseriesWriter + + tensor = tensor.squeeze() + save = TimeseriesWriter.save # pyright: ignore[reportUnknownVariableType] + write = None + bioio_axes_map["channel"] = "S" + bioio_axis_letters = "YXS" + elif path.suffix.lower() in ( + ".png", + ".bmp", + ".jpg", + ".mov", + ".avi", + ".mpg", + ".mpeg", + ".mp4", + ".mkv", + ".wmv", + ".ogg", + ): + from bioio_imageio.writers import TwoDWriter + + tensor = tensor.squeeze() + save = TwoDWriter.save # pyright: ignore[reportUnknownVariableType] + write = None + # channel dim denoted as 'S' + bioio_axes_map["channel"] = "S" + bioio_axis_letters = "YXS" + else: + raise RuntimeError( + f"Failed to identify a suitable writer for {path} with suffix={path.suffix}." + ) + + if save is not None: + assert write is None + bioio_data = tensor.data.rename( # pyright: ignore[reportUnknownVariableType] + { + a: bioio_axes_map.get(str(a).lower(), str(a)[:1].upper()) + for a in tensor.dims + } + ) + assert isinstance(bioio_data, xr.DataArray) + + if not all(str(d) in bioio_axis_letters for d in bioio_data.dims): + raise ValueError( + f"Failed to save tensor with bioio: dimensions {bioio_data.dims} not in '{bioio_axis_letters}'." ) - tensor = ( - (tensor - (t_min := tensor.data.min())) - / xr.ufuncs.maximum(tensor.data.max() - t_min, 1e-8) - * 255 - ).astype("uint8") - imwrite(path, tensor, extension=extension) + dim_order = "".join(str(d) for d in bioio_data.dims) + + image_name = None if subdir is None else str(subdir) + save( + bioio_data.data, + path, + dim_order=dim_order, + image_name=image_name, + channel_names=tensor.channel_names, + ) + + if write is not None: + assert save is None + write(tensor.data.data) + + +def _save_tensor_numpy(path: Path | ZipPath, tensor: Tensor) -> None: + if isinstance(path, ZipPath): + folder = path.filename.parent + else: + folder = path.parent + + folder.mkdir(exist_ok=True, parents=True) + save_array(path, tensor.to_numpy()) + + +def _save_tensor_imageio(path: Path | ZipPath, tensor: Tensor) -> None: + if isinstance(path, ZipPath): + raise NotImplementedError( + "Saving tensors to zip files is not implemented for imageio. Please save to an unzipped file path." + ) + + path.parent.mkdir(exist_ok=True, parents=True) + + extension = path.suffix.lower() + removed_singleton_axes: list[AxisId] = [] + remove_singletons = { + AxisId("batch"): [ + ".tif", + ".tiff", + ], # remove singleton batch dim for tiff files + **{ + a: [".png", ".jpg", ".jpeg"] for a in tensor.dims + }, # remove any singleton axis for png and jpg files + } + for rm_a, rm_ext in remove_singletons.items(): + if extension in rm_ext and tensor.tagged_shape.get(rm_a) == 1: + tensor = tensor[{rm_a: 0}] + removed_singleton_axes.append(rm_a) + + if removed_singleton_axes: + singleton_axes_msg = ( + f"(with removed singleton axes {list(map(str, removed_singleton_axes))}) " + ) + else: + singleton_axes_msg = "" + + logger.info( + "writing tensor {} {}to {}", + dict(tensor.tagged_shape), + singleton_axes_msg, + path, + ) + if extension in (".png", ".jpg", ".jpeg") and tensor.dtype in ( + "float32", + "float64", + ): + logger.warning( + "converting tensor of dtype {} to uint8 for saving as {}", + tensor.dtype, + extension, + ) + tensor = ( + (tensor - (t_min := tensor.data.min())) + / xr.ufuncs.maximum(tensor.data.max() - t_min, 1e-8) + * 255 + ).astype("uint8") + + imwrite(path, tensor, extension=extension) + + +class CreateArrayKwargs(TypedDict): + name: str + overwrite: bool + dimension_names: tuple[str, ...] | None + shards: Literal["auto"] | tuple[int, ...] | None -def save_sample( - path: Path | str | PerMember[Path | str], sample: Sample +def _save_tensor_clearscale( + path: Path | ZipPath, tensor: Tensor, subdir: PurePath | None ) -> None: + import clearscale + import dask.array + import zarr + + ZARR_FORMAT: int = int(os.getenv("ZARR_FORMAT", "3")) + SCALE_KEY = "s0" if subdir is None else str(subdir) + multiscale = clearscale.Multiscale( + { + SCALE_KEY: clearscale.Scale( + clearscale.Shape(**{str(k): v for k, v in tensor.tagged_shape.items()}) + ) + } + ) + if ZARR_FORMAT == 2: + zarr_attrs: dict[str, JsonValueReadOnly] = { + "multiscales": [multiscale.to_ome_zarr(version="0.4")] + } + elif ZARR_FORMAT == 3: + zarr_attrs: dict[str, JsonValueReadOnly] = { + "ome": { + "version": "0.5", + "multiscales": [multiscale.to_ome_zarr(version="0.5")], + } + } + else: + raise NotImplementedError(f"ZARR_FORMAT={ZARR_FORMAT} is not supported.") + + zarr_group = zarr.open_group( + str(path), + mode="w", + zarr_format=ZARR_FORMAT, + attributes=zarr_attrs, + ) + create_array_kwargs = CreateArrayKwargs( + name=SCALE_KEY, + overwrite=True, + dimension_names=None + if ZARR_FORMAT == 2 + else single_letter_dims_if_possible(tensor.dims), + shards=None if ZARR_FORMAT == 2 else "auto", + ) + if isinstance(tensor.data.data, np.ndarray): + _ = zarr_group.create_array( + **create_array_kwargs, data=tensor.to_numpy(), write_data=True + ) + elif isinstance(tensor.data.data, dask.array.Array): + dask.array.to_zarr( + tensor.data.data, zarr_group, compute=True, **create_array_kwargs + ) + else: + assert_never(tensor.data.data) + + +def _interprete_tensor_source(source: PermissiveFileSource | ZipPath | FileDescr): + if isinstance(source, FileDescr): + source = source.source + + if not isinstance(source, ZipPath): + source = interprete_file_source(source) + + if isinstance(source, RelativeFilePath): + source = source.absolute() + + subdir = None + if isinstance(source, HttpUrl): + original_source_path = PosixPath(source.path or "") + elif isinstance(source, ZipPath): + return source, None, None + else: + original_source_path = PosixPath(source) + + file_source = source + for parent in chain([source], source.parents): + extension = parent.suffix.lower() + if extension: + if isinstance(parent, RootHttpUrl): + with get_validation_context().replace(perform_io_checks=False): + file_source = HttpUrl(parent) + + parent_path = PosixPath(parent.path or "") + else: + file_source = parent + parent_path = PosixPath(parent) + + subdir = original_source_path.relative_to(parent_path) + if subdir == PosixPath("."): + subdir = None + + break + else: + extension = None + + return file_source, extension, subdir + + +def save_sample(path: Path | str | PerMember[Path | str], sample: Sample) -> None: """Save a **sample** to a **path** pattern or all sample members in the **path** mapping. @@ -164,7 +609,6 @@ class _StatList(RootModel[list[_StatEntry]]): """Serializable stat mapping""" - def serialize_stat( stat: Mapping[DatasetMeasure | SampleMeasure, MeasureValue], ) -> list[JsonValue]: @@ -213,9 +657,7 @@ def load_dataset_stat(path: Path) -> Stat: return load_stat(path) -def ensure_unzipped( - source: PermissiveFileSource | ZipPath | BytesReader, folder: Path -): +def ensure_unzipped(source: PermissiveFileSource | ZipPath | BytesReader, folder: Path): """unzip a (downloaded) **source** to a file in **folder** if source is a zip archive otherwise copy **source** to a file in **folder**.""" if isinstance(source, BytesReader): diff --git a/src/bioimageio/core/remote_backends/gradio/server.py b/src/bioimageio/core/remote_backends/gradio/server.py index 7755ddda..1122003e 100644 --- a/src/bioimageio/core/remote_backends/gradio/server.py +++ b/src/bioimageio/core/remote_backends/gradio/server.py @@ -12,11 +12,11 @@ import bioimageio.core from bioimageio.core import AxisId, Stat +from bioimageio.core._description_serializer import DescriptionSerializer from bioimageio.core.axis import PerAxis from bioimageio.core.backends import create_model_adapter from bioimageio.core.common import PerMember from bioimageio.core.remote_backends.gradio.serializer import ( - DescriptionSerializer, GradioSampleSerializer, SerializedSampleBlock, ) @@ -46,7 +46,10 @@ def predict( model: str, sha256: str, input_sample: Iterable[SerializedSampleBlock], - blocksize: int | Literal["blockwise_as_serialized"] | PerMember[PerAxis[int]] | None = None, + blocksize: int + | Literal["blockwise_as_serialized"] + | PerMember[PerAxis[int]] + | None = None, skip_preprocessing: bool = False, skip_postprocessing: bool = False, skip_input_padding: bool = False, diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index 4bdbc54e..3ef3aab8 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -20,8 +20,8 @@ from numpy.typing import DTypeLike, NDArray from typing_extensions import Self, TypeAlias, assert_never -from bioimageio.spec._internal.type_guards import is_dict from bioimageio.spec.model import v0_5 +from bioimageio.spec.model.v0_5 import SpaceUnit, TimeUnit from ._magic_tensor_ops import MagicTensorOpsMixin from .axis import AxisId, AxisInfo, AxisLike, PerAxis @@ -35,6 +35,7 @@ QuantileMethod, SliceInfo, ) +from .utils._type_guards import is_dict, is_mapping, is_sequence try: import dask.array as da @@ -97,7 +98,12 @@ def __array__(self, dtype: DTypeLike | None = None): def __getitem__( self, - key: SliceInfo | slice | int | PerAxis[SliceInfo | slice | int] | Tensor | xr.DataArray, + key: SliceInfo + | slice + | int + | PerAxis[SliceInfo | slice | int] + | Tensor + | xr.DataArray, ) -> Self: if isinstance(key, SliceInfo): key = slice(*key) @@ -154,6 +160,112 @@ def _binary_op( assert isinstance(data, xr.DataArray) return self.__class__.from_xarray(data) + def get_physical_scale(self) -> PerAxis[float]: + """Return the physical scale of each axis in units (see `get_physical_scale_unit`).""" + _scale = self._data.attrs.get("physical_scale", None) + scale: Mapping[Any, Any] = _scale if is_mapping(_scale) else {} + del _scale + return { + a if isinstance(a, AxisId) else AxisId(a): float(s) + for a, s in scale.items() + if s is not None + } + + def set_physical_scale(self, scale: PerAxis[float | None]) -> None: + """Set the physical scale of each axis in units (see `set_physical_scale_unit`).""" + self._data.attrs["physical_scale"] = { + a if isinstance(a, AxisId) else AxisId(a): float(s) + for a, s in scale.items() + if s is not None + } + + def get_physical_scale_unit(self) -> PerAxis[SpaceUnit | TimeUnit | str]: + """Return the physical unit of each axis.""" + _unit = self._data.attrs.get("physical_scale_unit", None) + unit: Mapping[Any, Any] = _unit if is_mapping(_unit) else {} + del _unit + return { + a if isinstance(a, AxisId) else AxisId(a): str(u) + for a, u in unit.items() + if u is not None + } + + def set_physical_scale_unit( + self, unit: PerAxis[SpaceUnit | TimeUnit | str | None] + ) -> None: + """Set the physical unit of each axis.""" + self._data.attrs["physical_scale_unit"] = { + a if isinstance(a, AxisId) else AxisId(a): str(u) + for a, u in unit.items() + if u is not None + } + + @property + def channel_names(self) -> list[str] | None: + """Return the channel names if available, otherwise None.""" + + names = self._data.attrs.get("channel_names", None) + if not is_sequence(names): + return None + + if len(names) != self.sizes.get(AxisId("channel"), 0): + logger.warning( + "Number of channel names ({}) does not match number of channels ({})", + len(names), + self.sizes.get(AxisId("channel"), 0), + ) + return None + + return [str(name) for name in names] + + @channel_names.setter + def channel_names(self, names: list[str] | None) -> None: + """Set the channel names.""" + if names is not None and len(names) != self.sizes.get(AxisId("channel"), 0): + logger.warning( + "Number of channel names ({}) does not match number of channels ({})", + len(names), + self.sizes.get(AxisId("c"), 0), + ) + self._data.attrs["channel_names"] = names + + @property + def channel_colors(self) -> list[str] | None: + """Return the channel colors if available, otherwise None.""" + + colors = self._data.attrs.get("channel_colors", None) + if not is_sequence(colors): + return None + + n_channels = self.sizes.get(AxisId("channel"), 0) + if len(colors) < n_channels: + logger.warning( + "Number of channel colors ({}) lower than number of channels ({})", + len(colors), + n_channels, + ) + return None + + return [str(name) for name in colors[:n_channels]] + + @channel_colors.setter + def channel_colors(self, colors: list[str] | None) -> None: + """Set the channel colors.""" + n_channels = self.sizes.get(AxisId("channel"), 0) + if colors is not None and len(colors) < n_channels: + logger.warning( + "Number of channel colors ({}) lower than number of channels ({})", + len(colors), + n_channels, + ) + self._data.attrs["channel_colors"] = colors + + def squeeze(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: + """Return a tensor with all the singleton dimensions removed.""" + squeezed = self._data.squeeze(dim=dim) # pyright: ignore[reportUnknownVariableType] + assert isinstance(squeezed, xr.DataArray) + return self.__class__.from_xarray(squeezed) + def _inplace_binary_op( self, other: _Compatible, @@ -187,7 +299,13 @@ def from_xarray(cls, data_array: xr.DataArray) -> Self: note for internal use: this factory method is round-trip save for any `Tensor`'s `data` property (an xarray.DataArray). """ - return cls(array=data_array, dims=tuple(AxisId(d) for d in data_array.dims)) + dims = [AxisId(d) for d in data_array.dims] + dim_map = {d: a for d, a in zip(data_array.dims, dims) if d != a} + if dim_map: + data_array = data_array.rename(dim_map) # pyright: ignore[reportUnknownVariableType] + assert isinstance(data_array, xr.DataArray) + + return cls(array=data_array, dims=dims) @classmethod def from_numpy( @@ -552,7 +670,7 @@ def unstack_batch_multi_index( def transpose( self, - axes: Sequence[AxisId], + axes: Sequence[AxisLike], *, extra_dims: Literal[ "raise", "squeeze", "stack", "squeeze_or_stack" @@ -578,6 +696,7 @@ def transpose( If "unstack", any missing dimensions will be unstacked from the batch dimension. For this option a batch dimension with a multi-index must be present from previous stacking operations or assigned by `Tensor.assign_batch_multi_index()`. If "unstack_or_expand", any missing dimensions will be unstacked from the batch dimension if it has a multi-index, otherwise they will be added as singleton dimensions. """ + axes = [AxisInfo.create(a).id for a in axes] array = self._data unhandled_missing_dims = [a for a in axes if a not in array.dims] @@ -669,41 +788,48 @@ def _interprete_array_wo_known_axes(cls, array: Array) -> Tensor: ndim = array.ndim shape = [s if isinstance(s, int) else -1 for s in array.shape] + current_axes: list[v0_5.InputAxis] if ndim == 2: - current_axes = ( + current_axes = [ v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[0]), v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[1]), - ) - elif ndim == 3 and any(s <= 3 for s in shape): - current_axes = ( + ] + elif ndim == 3 and any(s <= 10 for s in shape): + current_axes = [ + v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[1]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[2]), + ] + current_axes.insert( + np.argmin(shape), v0_5.ChannelAxis( channel_names=[ - v0_5.Identifier(f"channel{i}") for i in range(shape[0]) + v0_5.Identifier(f"channel{i}") for i in range(min(shape)) ] ), - v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[1]), - v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[2]), ) elif ndim == 3: - current_axes = ( + current_axes = [ v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=shape[0]), v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[1]), v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[2]), - ) + ] elif ndim == 4: - current_axes = ( + current_axes = [ + v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=shape[1]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[2]), + v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[3]), + ] + current_axes.insert( + np.argmin(shape), v0_5.ChannelAxis( channel_names=[ - v0_5.Identifier(f"channel{i}") for i in range(shape[0]) + v0_5.Identifier(f"channel{i}") for i in range(min(shape)) ] ), - v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=shape[1]), - v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[2]), - v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[3]), ) elif ndim == 5: - current_axes = ( - v0_5.BatchAxis(), + current_axes = [ + v0_5.TimeInputAxis(size=shape[1]), v0_5.ChannelAxis( channel_names=[ v0_5.Identifier(f"channel{i}") for i in range(shape[1]) @@ -712,7 +838,7 @@ def _interprete_array_wo_known_axes(cls, array: Array) -> Tensor: v0_5.SpaceInputAxis(id=v0_5.AxisId("z"), size=shape[2]), v0_5.SpaceInputAxis(id=v0_5.AxisId("y"), size=shape[3]), v0_5.SpaceInputAxis(id=v0_5.AxisId("x"), size=shape[4]), - ) + ] else: raise ValueError(f"Could not guess an axis mapping for {shape}") diff --git a/src/bioimageio/core/utils/_io_zarr.py b/src/bioimageio/core/utils/_io_zarr.py new file mode 100644 index 00000000..d8fc4e9c --- /dev/null +++ b/src/bioimageio/core/utils/_io_zarr.py @@ -0,0 +1,136 @@ +from __future__ import annotations + +from collections.abc import Mapping, Sequence +from typing import Any + +import zarr +from loguru import logger +from torch import TYPE_CHECKING + +if TYPE_CHECKING: + import clearscale + + +def open_zarr_multiscale_array( + uri: str, +) -> tuple[zarr.Array[Any], clearscale.Multiscale]: + """Open a zarr multiscale array from a URI""" + ms, _uri_to_parent, _sub_uri = _recurse_url_to_find_group(uri) + + zarr_array_or_group = zarr.open(uri) + if isinstance(zarr_array_or_group, zarr.Group): + keys = list(ms.keys()) + if len(keys) == 1: + return open_zarr_multiscale_array(f"{uri}/{keys[0]}") + else: + raise ValueError( + f"{uri} does not point to a zarr array, but a group with keys: {keys}." + + " Append '/' to the URI to open a specific array." + ) + + return zarr_array_or_group, ms + + +def _recurse_url_to_find_group(uri: str) -> tuple[clearscale.Multiscale, str, str]: + """URI may point to an OME-Zarr "multiscales" root or to a specific scale. + Try to find OME-Zarr spec first at URI, then search parent directories. + Returns + - the valid Multiscale + - root URI of the multiscales group + - scale sub-URI""" + uri = uri.rstrip("/") + i = len(uri) + parent_group = None + uri_to_parent = uri + sub_uri = "" + for _ in range(uri.count("/")): + try: + parent_group = zarr.open(uri_to_parent) + if isinstance(parent_group, zarr.Group): + break + except Exception: + # stop looking once we checked a ".zarr" directory + if uri_to_parent.split("/")[-1].endswith(".zarr"): + break + + i = uri.rfind("/", 0, i) + uri_to_parent = uri[:i] + sub_uri = uri[i + 1 :] + + if isinstance(parent_group, zarr.Group): + return ( + _ms_from_group(parent_group, sub_uri), + uri_to_parent, + sub_uri, + ) + + raise ValueError("No multiscale meta found anywhere along the URI") + + +def _ms_from_group( + zarr_group: zarr.Group, must_have_dataset: str | None = None +) -> clearscale.Multiscale: + import clearscale + + # 1. Discover the "multiscales" list + try: + ome = zarr_group.attrs["ome"] + assert isinstance(ome, Mapping) + ome_multiscales = ome["multiscales"] + except Exception: + try: + ome_multiscales = zarr_group.attrs["multiscales"] + except Exception: + raise ValueError("No multiscale metadata found in zarr group") + + if not isinstance(ome_multiscales, Sequence) or isinstance(ome_multiscales, str): + raise TypeError( + "Multiscale metadata in this zarr group is not a valid sequence" + ) + + # 2. Convert all entries of the "multiscales" list + # (There will practically never be more than one) + valid_multiscales: list[clearscale.Multiscale] = [] + for ome_ms in ome_multiscales: + try: + assert isinstance(ome_ms, Mapping) + valid_multiscales.append( + clearscale.Multiscale.from_ome_zarr( + ome_ms, shape_source=_get_shape_callable(zarr_group) + ) + ) + except Exception as e: + logger.debug("Encountered invalid multiscale metadata: {}", e) + continue # Invalid multiscale - maybe warn, or just skip + + if not valid_multiscales: + raise ValueError("Multiscale metadata in this zarr group was all invalid") + + if must_have_dataset: + selected_ms = next( + (ms for ms in valid_multiscales if must_have_dataset in ms), None + ) + if selected_ms is None: + raise ValueError( + f"No Multiscale containing a dataset named {must_have_dataset!r} found" + ) + return selected_ms + + # 3. Handle the off-chance that there is more than one for some reason. + if len(valid_multiscales) > 1: + # ilastik bounces back group URLs in this case and forces selection by passing a direct URL to a dataset. + # Many tools just default to valid_multiscales[0] and ignore further multiscale definitions. + raise ValueError( + f"{len(valid_multiscales)} Multiscale definitions found. Selection required." + ) + else: + return valid_multiscales[0] + + +def _get_shape_callable(zarr_group: zarr.Group): + def get_shape(path: str): + zarr_array = zarr_group[path] + assert isinstance(zarr_array, zarr.Array) + return zarr_array.shape + + return get_shape diff --git a/src/bioimageio/core/utils/_type_guards.py b/src/bioimageio/core/utils/_type_guards.py index ad61c691..0648c120 100644 --- a/src/bioimageio/core/utils/_type_guards.py +++ b/src/bioimageio/core/utils/_type_guards.py @@ -8,3 +8,5 @@ is_tuple = type_guards.is_tuple is_dict = type_guards.is_dict is_kwargs = type_guards.is_kwargs +is_mapping = type_guards.is_mapping +is_sequence = type_guards.is_sequence diff --git a/tests/test_io.py b/tests/test_io.py index 25904666..c22d4b49 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,4 +1,5 @@ from pathlib import Path +from typing import Literal import numpy as np import pytest @@ -7,11 +8,15 @@ @pytest.mark.parametrize( "name", [ - "img.png", - "img.tiff", "img.npy", ], ) +@pytest.mark.parametrize( + "io_lib", + [ + "numpy", + ], +) @pytest.mark.parametrize( "shape", [ @@ -22,14 +27,102 @@ (5, 3, 4), ], ) -def test_tensor_io(name: str, shape: tuple[int, ...], tmp_path: Path): +def test_tensor_io_numpy( + name: str, + io_lib: Literal["numpy"], + shape: tuple[int, ...], + tmp_path: Path, +): from bioimageio.core import Tensor from bioimageio.core.io import load_tensor, save_tensor path = tmp_path / name - data = Tensor.from_numpy( + expected = Tensor.from_numpy( np.arange(np.prod(shape), dtype=np.uint8).reshape(shape), dims=None ) - save_tensor(path, data) - actual = load_tensor(path) - assert actual == data + save_tensor(path, expected, io_lib=io_lib) + actual = load_tensor(path, io_lib=io_lib) + assert (actual == expected).to_numpy().all() + + +@pytest.mark.parametrize( + "name", + [ + "img.png", + "img.tiff", + "img.zarr", + "img.zarr/0", + "img.zarr/s2", + ], +) +@pytest.mark.parametrize( + "io_lib", + [ + "bioio", + "imageio", + "clearscale", + ], +) +@pytest.mark.parametrize( + "shape", + [ + (6, 5), + (1, 6, 5), + (3, 6, 5), + (4, 5, 6), + ], +) +def test_tensor_io( + name: str, + io_lib: Literal["bioio", "imageio", "clearscale"], + shape: tuple[int, ...], + tmp_path: Path, +): + from bioimageio.core import Tensor + from bioimageio.core.io import load_tensor, save_tensor + + if io_lib == "clearscale": + try: + import clearscale as _ + except ImportError: + pytest.skip("clearscale not installed") + + if name.find(".zarr") == -1: + pytest.skip("clearscale only supports zarr") + elif io_lib == "imageio": + if name.find(".zarr") != -1: + pytest.skip("imageio does not support zarr") + + path = tmp_path / name + c = "S" if name.endswith(".png") else "C" + expected = Tensor.from_numpy( + np.arange(np.prod(shape), dtype=np.uint8).reshape(shape), + dims=f"{c}YX"[-len(shape) :], + ) + # if io_lib == "bioio" and path.suffix == ".zarr": + # # rename axes to enable roundtrip with bioio + # expected._data = expected.data.rename( + # {a: str(a)[0].upper() for a in expected.dims} + # ) + + save_tensor(path, expected, io_lib=io_lib) + + actual = load_tensor(path, io_lib=io_lib) + assert (actual == expected).to_numpy().all() + + +def test_load_tensor_zarr(): + source = "https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0062A/6001240.zarr/0" + from bioimageio.core.io import load_tensor + + img = load_tensor(source) + assert img.shape + + +def test_load_tensor_zarr_group(): + source = "https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0062A/6001240.zarr" + + from bioimageio.core.io import load_tensor + + tensor = load_tensor(source) + assert tensor.dims == ("T", "C", "Z", "Y", "X") From 8e0429ef64318711d9d9ee6efdc4cb828387b4f9 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 18 Aug 2026 13:27:49 +0200 Subject: [PATCH 14/85] expose missing_dims in create_sample_for_model --- src/bioimageio/core/digest_spec.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/bioimageio/core/digest_spec.py b/src/bioimageio/core/digest_spec.py index ceb2a8b8..b8fa2bd8 100644 --- a/src/bioimageio/core/digest_spec.py +++ b/src/bioimageio/core/digest_spec.py @@ -433,6 +433,12 @@ def get_tensor( If "squeeze", any extra singleton dimensions will be squeezed, non-singleton dimensions will raise an error. If "stack", any extra dimensions will be stacked to the batch dimension. Such a stacked batch dimension then has a multi-index that can be unstacked using `Tensor.unstack_batch_multi_index()`. If "squeeze_or_stack", any extra singleton dimensions will be squeezed, non-singleton dimensions will be stacked to the batch dimension. + missing_dims: + Missing dimensions are any dimensions specified in `axes` that are not present in the tensor. + If "raise", any missing dimensions will raise an error. + If "expand", any missing dimensions will be added as singleton dimensions. + If "unstack", any missing dimensions will be unstacked from the batch dimension. For this option a batch dimension with a multi-index must be present from previous stacking operations or assigned by `Tensor.assign_batch_multi_index()`. + If "unstack_or_expand", any missing dimensions will be unstacked from the batch dimension if it has a multi-index, otherwise they will be added as singleton dimensions. """ if isinstance( @@ -472,6 +478,9 @@ def create_sample_for_model( sample_id: SampleId = None, inputs: PerMember[TensorSource] | TensorSource, extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "stack", + missing_dims: Literal[ + "raise", "expand", "unstack", "unstack_or_expand" + ] = "unstack_or_expand", ) -> Sample: """Create a sample from a single set of input(s) for a specific bioimage.io model @@ -484,6 +493,13 @@ def create_sample_for_model( If "squeeze", any extra singleton dimensions will be squeezed, non-singleton dimensions will raise an error. If "stack", any extra dimensions will be stacked to the batch dimension. Such a stacked batch dimension then has a multi-index that can be unstacked using `Tensor.unstack_batch_multi_index()`. If "squeeze_or_stack", any extra singleton dimensions will be squeezed, non-singleton dimensions will be stacked to the batch dimension. + missing_dims: + Missing dimensions are any dimensions specified in `axes` that are not present in the tensor. + If "raise", any missing dimensions will raise an error. + If "expand", any missing dimensions will be added as singleton dimensions. + If "unstack", any missing dimensions will be unstacked from the batch dimension. For this option a batch dimension with a multi-index must be present from previous stacking operations or assigned by `Tensor.assign_batch_multi_index()`. + If "unstack_or_expand", any missing dimensions will be unstacked from the batch dimension if it has a multi-index, otherwise they will be added as singleton dimensions. + """ model_inputs = {get_member_id(d): d for d in model.inputs} @@ -508,7 +524,9 @@ def create_sample_for_model( return Sample( members={ - m: get_tensor(inputs[m], ipt, extra_dims=extra_dims) + m: get_tensor( + inputs[m], ipt, extra_dims=extra_dims, missing_dims=missing_dims + ) for m, ipt in model_inputs.items() if m in inputs }, From bfc70422f0c05f9b0a765058b1e5c5ab8e0dd928 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 18 Aug 2026 13:28:34 +0200 Subject: [PATCH 15/85] add argument devices to predict --- src/bioimageio/core/prediction.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/bioimageio/core/prediction.py b/src/bioimageio/core/prediction.py index 6596436e..625ca3a8 100644 --- a/src/bioimageio/core/prediction.py +++ b/src/bioimageio/core/prediction.py @@ -32,6 +32,7 @@ def predict( skip_preprocessing: bool = False, skip_postprocessing: bool = False, save_output_path: Path | str | None = None, + devices: Sequence[str] | None = None, ) -> Sample: """Run prediction for a single set of input(s) with a bioimage.io model @@ -71,6 +72,7 @@ def predict( pp = create_prediction_pipeline( model, fixed_dataset_statistics=inputs.stat if isinstance(inputs, Sample) else {}, + devices=devices, ) with pp: From f95474c579e766b288d4e3b8fcb0aa29e1bcbe2f Mon Sep 17 00:00:00 2001 From: fynnbe Date: Wed, 19 Aug 2026 12:49:33 +0200 Subject: [PATCH 16/85] load/save zarr arrays --- .vscode/launch.json | 15 ++ .vscode/settings.json | 14 +- changelog.md | 7 +- pyproject.toml | 4 +- src/bioimageio/core/__init__.py | 2 +- src/bioimageio/core/_prediction_pipeline.py | 2 +- src/bioimageio/core/io.py | 195 +++++++++++++++----- src/bioimageio/core/prediction.py | 71 +++++-- tests/test_io.py | 4 +- 9 files changed, 239 insertions(+), 75 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..dac597d7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python Debugger: Current File", + "type": "debugpy", + "request": "launch", + "program": "${file}", + "justMyCode": false + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index a61bc3d9..590cef58 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,16 +10,18 @@ ], "python.testing.pytestEnabled": true, "python.testing.unittestEnabled": false, + "editor.formatOnSave": true, + "editor.codeActionsOnSave": { + "source.unusedImports": "explicit", + "source.organizeImports": "explicit" + }, + "python-envs.defaultEnvManager": "ms-python.python:conda", + "python-envs.defaultPackageManager": "ms-python.python:conda", "python-envs.pythonProjects": [ { - "path": "", + "path": ".", "envManager": "ms-python.python:conda", "packageManager": "ms-python.python:conda" } ], - "editor.formatOnSave": true, - "editor.codeActionsOnSave": { - "source.unusedImports": "explicit", - "source.organizeImports": "explicit" - }, } diff --git a/changelog.md b/changelog.md index 0a4c62f4..d36c5d02 100644 --- a/changelog.md +++ b/changelog.md @@ -1,10 +1,9 @@ -### 0.12.0 (not yet released) +### 0.12.0 -- bump spec to 0.5.12.1 (wip) - - supports loading zarr images +- bump spec to 0.5.13.1 +- support loading zarr images - dropped `is_volume` flag from `load_image()` - ### 0.11.0 - bump spec to 0.5.12.0 diff --git a/pyproject.toml b/pyproject.toml index c0a666a6..0429d2b3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ requires-python = ">=3.9" readme = "README.md" dynamic = ["version"] dependencies = [ - "bioimageio.spec ==0.5.13.0", + "bioimageio.spec ==0.5.13.1", "imagecodecs", "imageio>=2.10", "loguru", @@ -55,6 +55,7 @@ stardist = ["stardist"] # for stardist postprocessing gradio-server = ['gradio[mcp];python_version>="3.10"'] gradio-client = ['gradio_client;python_version>="3.10"'] bioio = [ + "bioio", "bioio-bioformats", "bioio-imageio", "bioio-ome-tiff", @@ -63,6 +64,7 @@ bioio = [ ] # clearscale = ["clearscale", "dask[array]", "zarr"] # TODO: add clearscale extra deps once clearscale is available on pypi dev = [ + "bioio", "bioio-bioformats", "bioio-imageio", "bioio-ome-tiff", diff --git a/src/bioimageio/core/__init__.py b/src/bioimageio/core/__init__.py index d9a1d8b1..d5085264 100644 --- a/src/bioimageio/core/__init__.py +++ b/src/bioimageio/core/__init__.py @@ -15,7 +15,7 @@ - Computing statistics on datasets (represented as sequences of samples), see `compute_dataset_measures`. """ -__version__ = "0.11.0" +__version__ = "0.12.0" from loguru import logger logger.disable("bioimageio.core") diff --git a/src/bioimageio/core/_prediction_pipeline.py b/src/bioimageio/core/_prediction_pipeline.py index ee641646..ea7b4b33 100644 --- a/src/bioimageio/core/_prediction_pipeline.py +++ b/src/bioimageio/core/_prediction_pipeline.py @@ -682,7 +682,7 @@ def _predict_blocks(): for i, b in enumerate( tqdm( input_blocks, - desc=f"predict sample {sample.id or ''} with {self._model_descr.id or self._model_descr.name}", + desc=f"predict sample '{sample.id or ''}' with {self._model_descr.id or self._model_descr.name}", unit="block", unit_divisor=1, total=n_blocks, diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index e0298a61..a572c44a 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -1,11 +1,10 @@ from __future__ import annotations -import collections.abc import json import os import warnings import zipfile -from collections.abc import Mapping, Sequence +from collections.abc import Iterable, Mapping, Sequence from contextlib import nullcontext from io import BytesIO from itertools import chain @@ -14,6 +13,7 @@ from typing import TYPE_CHECKING, TypedDict, Union import numpy as np +import pydantic import xarray as xr from exceptiongroup import ExceptionGroup from imageio.v3 import ( @@ -21,12 +21,16 @@ imwrite, # type: ignore ) from loguru import logger -from pydantic import BaseModel, FilePath, RootModel +from pydantic import BaseModel, RootModel from typing_extensions import Literal, TypeAlias, assert_never from typing_extensions import TypeAliasType as _TypeAliasType from bioimageio.spec import get_validation_context -from bioimageio.spec._internal.io import get_reader, interprete_file_source +from bioimageio.spec._internal.io import ( + RelativeDirectory, + get_reader, + interprete_file_source, +) from bioimageio.spec.common import ( BytesReader, FileDescr, @@ -40,9 +44,9 @@ from bioimageio.spec.model import v0_5 from bioimageio.spec.utils import load_array, save_array -from .axis import AxisId, AxisLike, single_letter_dims_if_possible -from .common import PerMember -from .sample import Sample +from .axis import AxisId, AxisLike, PerAxis, single_letter_dims_if_possible +from .common import PerMember, SliceInfo +from .sample import Sample, SampleBlock from .stat_measures import DatasetMeasure, MeasureValue, SampleMeasure, Stat from .tensor import Tensor from .utils._io_zarr import open_zarr_multiscale_array @@ -137,11 +141,11 @@ def _select_io_libs_based_on_extension(extension: str | None) -> tuple[IO_Lib, . elif extension == ".zarr": return ("bioio", "clearscale") else: - return ("bioio", "imageio", "clearscale") + return ("bioio", "imageio") def _load_tensor_bioio( - source: HttpUrl | FilePath | ZipPath, + source: FileSource | ZipPath, subdir: str | None, ) -> Tensor: """Load an image tensor using bioio""" @@ -153,7 +157,7 @@ def _load_tensor_bioio( else: src = source - if isinstance(src, RelativeFilePath): + if isinstance(src, (RelativeFilePath, RelativeDirectory)): src = src.absolute() del source @@ -217,6 +221,7 @@ def _load_tensor_clearscale(source: FileDescr | PermissiveFileSource) -> Tensor: "clearscale: Loading from zip files is not implemented." ) + logger.debug("loading tensor from {} with clearscale", source) array, ms = open_zarr_multiscale_array( source.as_posix() if isinstance(source, Path) else str(source) ) @@ -232,7 +237,9 @@ def _load_tensor_clearscale(source: FileDescr | PermissiveFileSource) -> Tensor: def save_tensor( path: Path | str, tensor: Tensor, + *, io_lib: IO_Lib | None = None, + roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None = None, ) -> None: output_path, extension, subdir = _interprete_tensor_source(path) if isinstance(output_path, RootHttpUrl): @@ -245,7 +252,12 @@ def save_tensor( exceptions: list[Exception] = [] for try_io_lib in try_io_libs: try: - return save_tensor(path, tensor, io_lib=try_io_lib) + return save_tensor( + path, + tensor, + io_lib=try_io_lib, + roi_and_tensor_shape=roi_and_tensor_shape, + ) except Exception as e: exceptions.append(e) logger.opt(exception=e).warning( @@ -256,19 +268,40 @@ def save_tensor( "Failed to save tensor with any available io_lib", exceptions ) elif io_lib == "bioio": - return _save_tensor_bioio(output_path, tensor, subdir=subdir) + return _save_tensor_bioio( + output_path, + tensor, + subdir=subdir, + roi_and_tensor_shape=roi_and_tensor_shape, + ) elif io_lib == "imageio": + if roi_and_tensor_shape is not None: + raise NotImplementedError( + "Saving a region of interest (roi) is not implemented for imageio." + ) return _save_tensor_imageio(output_path, tensor) elif io_lib == "clearscale": - return _save_tensor_clearscale(output_path, tensor, subdir=subdir) + return _save_tensor_clearscale( + output_path, + tensor, + subdir=subdir, + roi_and_tensor_shape=roi_and_tensor_shape, + ) elif io_lib == "numpy": + if roi_and_tensor_shape is not None: + raise NotImplementedError( + "Saving a region of interest (roi) is not implemented for numpy." + ) return _save_tensor_numpy(output_path, tensor) else: assert_never(io_lib) def _save_tensor_bioio( - path: Path | ZipPath, tensor: Tensor, subdir: PurePath | None + path: Path | ZipPath, + tensor: Tensor, + subdir: PurePath | None, + roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None, ) -> None: """Save an image tensor using bioio""" @@ -279,12 +312,24 @@ def _save_tensor_bioio( "time": "T", } if path.suffix.lower() in (".tif", ".tiff"): + if roi_and_tensor_shape is not None: + raise NotImplementedError( + "Saving a region of interest (roi) is not implemented for saving tiff files with io_lib='bioio'." + ) + from bioio_ome_tiff.writers import OmeTiffWriter save = OmeTiffWriter.save # pyright: ignore[reportUnknownVariableType] write = None bioio_axis_letters = "TCZYX" elif path.suffix.lower() == ".zarr": + if roi_and_tensor_shape is None: + tensor_shape = tensor.tagged_shape + else: + raise NotImplementedError( + "Saving a region of interest (roi) is not implemented for saving zarr files with io_lib='bioio'." + ) + import bioio_ome_zarr.writers axes_names = single_letter_dims_if_possible(tensor.dims) @@ -314,7 +359,7 @@ def _save_tensor_bioio( label=f"channel{i}" if channel_names is None else channel_names[i], color="#FF0000" if channel_colors is None else channel_colors[i], ) - for i in range(tensor.tagged_shape.get(AxisId("channel"), 0)) + for i in range(tensor_shape.get(AxisId("channel"), 0)) ] zf = int(os.getenv("ZARR_FORMAT", "3")) if zf not in (2, 3): @@ -325,18 +370,27 @@ def _save_tensor_bioio( save = None write = bioio_ome_zarr.writers.OMEZarrWriter( store=path, - level_shapes=[tensor.shape], + level_shapes=[[tensor_shape[d] for d in tensor.dims]], dtype=tensor.dtype, zarr_format=zf, image_name="Image" if subdir is None else str(subdir), - axes_names=axes_names, + axes_names=list(axes_names), axes_types=axes_types, axes_units=axes_units, physical_pixel_size=physical_pixel_size, channels=channels, ).write_full_volume - bioio_axis_letters = "TCZYX" + bioio_axis_letters = None + bioio_axes_map = None + # bioio_axis_letters = "TCZYX" + # bioio_axes_map["batch"] = "T" + # bioio_axes_map["index"] = "T" elif path.suffix.lower() in (".gif", ".mp4", ".mkv"): + if roi_and_tensor_shape is not None: + raise NotImplementedError( + "Saving a region of interest (roi) is not implemented for saving video files with io_lib='bioio'." + ) + from bioio_imageio.writers import TimeseriesWriter tensor = tensor.squeeze() @@ -357,6 +411,11 @@ def _save_tensor_bioio( ".wmv", ".ogg", ): + if roi_and_tensor_shape is not None: + raise NotImplementedError( + "Saving a region of interest (roi) is not implemented for saving 2d images with io_lib='bioio'." + ) + from bioio_imageio.writers import TwoDWriter tensor = tensor.squeeze() @@ -372,15 +431,19 @@ def _save_tensor_bioio( if save is not None: assert write is None - bioio_data = tensor.data.rename( # pyright: ignore[reportUnknownVariableType] - { - a: bioio_axes_map.get(str(a).lower(), str(a)[:1].upper()) - for a in tensor.dims - } - ) - assert isinstance(bioio_data, xr.DataArray) + bioio_data = tensor.data + if bioio_axes_map is not None: + bioio_data = bioio_data.rename( # pyright: ignore[reportUnknownVariableType] + { + a: bioio_axes_map.get(str(a).lower(), str(a)[:1].upper()) + for a in tensor.dims + } + ) + assert isinstance(bioio_data, xr.DataArray) - if not all(str(d) in bioio_axis_letters for d in bioio_data.dims): + if bioio_axis_letters is not None and not all( + str(d) in bioio_axis_letters for d in bioio_data.dims + ): raise ValueError( f"Failed to save tensor with bioio: dimensions {bioio_data.dims} not in '{bioio_axis_letters}'." ) @@ -474,18 +537,31 @@ class CreateArrayKwargs(TypedDict): def _save_tensor_clearscale( - path: Path | ZipPath, tensor: Tensor, subdir: PurePath | None + path: Path | ZipPath, + tensor: Tensor, + subdir: PurePath | None, + roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None, ) -> None: import clearscale import dask.array import zarr + if roi_and_tensor_shape is None: + roi = slice(None) + tensor_shape = tensor.tagged_shape + else: + roi_map, tensor_shape = roi_and_tensor_shape + roi = tuple( + slice(None) if (s := roi_map.get(d)) is None else slice(s.start, s.stop) + for d in tensor.dims + ) + ZARR_FORMAT: int = int(os.getenv("ZARR_FORMAT", "3")) SCALE_KEY = "s0" if subdir is None else str(subdir) multiscale = clearscale.Multiscale( { SCALE_KEY: clearscale.Scale( - clearscale.Shape(**{str(k): v for k, v in tensor.tagged_shape.items()}) + clearscale.Shape(**{str(k): v for k, v in tensor_shape.items()}) ) } ) @@ -517,14 +593,17 @@ def _save_tensor_clearscale( else single_letter_dims_if_possible(tensor.dims), shards=None if ZARR_FORMAT == 2 else "auto", ) + zarr_array = zarr_group.create_array( + **create_array_kwargs, + shape=[tensor_shape[d] for d in tensor.dims], + dtype=tensor.dtype, + write_data=True, + ) + if isinstance(tensor.data.data, np.ndarray): - _ = zarr_group.create_array( - **create_array_kwargs, data=tensor.to_numpy(), write_data=True - ) + zarr_array[roi] = tensor.data.data elif isinstance(tensor.data.data, dask.array.Array): - dask.array.to_zarr( - tensor.data.data, zarr_group, compute=True, **create_array_kwargs - ) + dask.array.to_zarr(tensor.data.data, zarr_array, compute=True, region=roi) else: assert_never(tensor.data.data) @@ -533,10 +612,10 @@ def _interprete_tensor_source(source: PermissiveFileSource | ZipPath | FileDescr if isinstance(source, FileDescr): source = source.source - if not isinstance(source, ZipPath): + if isinstance(source, (str, pydantic.AnyUrl)): source = interprete_file_source(source) - if isinstance(source, RelativeFilePath): + if isinstance(source, (RelativeFilePath, RelativeDirectory)): source = source.absolute() subdir = None @@ -571,7 +650,9 @@ def _interprete_tensor_source(source: PermissiveFileSource | ZipPath | FileDescr return file_source, extension, subdir -def save_sample(path: Path | str | PerMember[Path | str], sample: Sample) -> None: +def save_sample( + path: Path | str | PerMember[Path | str], sample: Sample | Iterable[SampleBlock] +) -> None: """Save a **sample** to a **path** pattern or all sample members in the **path** mapping. @@ -580,22 +661,42 @@ def save_sample(path: Path | str | PerMember[Path | str], sample: Sample) -> Non (Each) **path** may contain `{sample_id}` to be formatted with the **sample** object. """ - if not isinstance(path, collections.abc.Mapping): - if len(sample.members) < 2 or any( + + if isinstance(sample, Sample): + first_block = sample + remaining_blocks = () + else: + remaining_blocks = iter(sample) + first_block = next(remaining_blocks) + + if not isinstance(path, Mapping): + if len(first_block.members) < 2 or any( m in str(path) for m in ("{member_id}", "{input_id}", "{output_id}") ): - path = {m: path for m in sample.members} + path = {m: path for m in first_block.members} else: raise ValueError( - f"path {path} must contain '{{member_id}}' for sample with multiple members {list(sample.members)}." + f"path {path} must contain '{{member_id}}' for sample with multiple members {list(first_block.members)}." ) - for m, p in path.items(): - t = sample.members[m] - p_formatted = Path( - str(p).format(sample_id=sample.id, member_id=m, input_id=m, output_id=m) - ) - save_tensor(p_formatted, t) + for sample_block in chain([first_block], remaining_blocks): + for m, p in path.items(): + if isinstance(sample_block, Sample): + sample_id = sample_block.id + t = sample_block.members[m] + roi_and_tensor_shape = None + else: + sample_id = sample_block.sample_id + roi_and_tensor_shape = ( + sample_block.blocks[m].inner_slice, + sample_block.sample_shape[m], + ) + t = sample_block.blocks[m].inner_data + + p_formatted = Path( + str(p).format(sample_id=sample_id, member_id=m, input_id=m, output_id=m) + ) + save_tensor(p_formatted, t, roi_and_tensor_shape=roi_and_tensor_shape) class _StatEntry(BaseModel, frozen=True, arbitrary_types_allowed=True): diff --git a/src/bioimageio/core/prediction.py b/src/bioimageio/core/prediction.py index 625ca3a8..645f97c4 100644 --- a/src/bioimageio/core/prediction.py +++ b/src/bioimageio/core/prediction.py @@ -1,7 +1,7 @@ from __future__ import annotations import collections.abc -from collections.abc import Hashable, Iterable, Iterator, Mapping +from collections.abc import Hashable, Iterable, Iterator, Mapping, Sequence from pathlib import Path from loguru import logger @@ -103,18 +103,22 @@ def predict( input_block_shape, ) - output = pp.predict_sample_with_fixed_blocking( - sample, - input_block_shape=input_block_shape, - skip_preprocessing=skip_preprocessing, - skip_postprocessing=skip_postprocessing, + _n_intermediates, output = ( + pp.predict_sample_with_fixed_blocking_yield_intermediates( + sample, + input_block_shape=input_block_shape, + skip_preprocessing=skip_preprocessing, + skip_postprocessing=skip_postprocessing, + ) ) elif blocksize_parameter is not None: - output = pp.predict_sample_with_blocking( - sample, - skip_preprocessing=skip_preprocessing, - skip_postprocessing=skip_postprocessing, - ns=blocksize_parameter, + _n_intermediates, output = ( + pp.predict_sample_with_blocking_yield_intermediates( + sample, + skip_preprocessing=skip_preprocessing, + skip_postprocessing=skip_postprocessing, + ns=blocksize_parameter, + ) ) else: output = pp.predict_sample_without_blocking( @@ -122,10 +126,51 @@ def predict( skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, ) + if save_output_path: - save_sample(save_output_path, output) + output_ref: list[Sample | None] = [ + None + ] # to store final output when saving blockwise + if isinstance(output, Sample): + save_output = output + return_output = output + else: + if pp.has_non_blockwise_postprocessing: + final_output = None + for out in output: + final_output = out + + assert final_output is not None + save_output = final_output.sample + return_output = final_output.sample + else: + iterable_output = output + + def blockwise_output(): + for out in iterable_output: + output_ref[0] = out.sample + yield out.last_block + + save_output = blockwise_output() + return_output = None + + save_sample(save_output_path, save_output) + if return_output is None: + return_output = output_ref[0] + assert return_output is not None, ( + "saving output blockwise failed to produce a final output sample" + ) + else: + if isinstance(output, Sample): + return_output = output + else: + return_output = None + for out in output: + return_output = out.sample + + assert return_output is not None - return output + return return_output def predict_many( diff --git a/tests/test_io.py b/tests/test_io.py index c22d4b49..1846c80b 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -112,7 +112,7 @@ def test_tensor_io( def test_load_tensor_zarr(): - source = "https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0062A/6001240.zarr/0" + source = "https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0062A/6001240.zarr/2" from bioimageio.core.io import load_tensor img = load_tensor(source) @@ -125,4 +125,4 @@ def test_load_tensor_zarr_group(): from bioimageio.core.io import load_tensor tensor = load_tensor(source) - assert tensor.dims == ("T", "C", "Z", "Y", "X") + assert tensor.dims == ("channel", "z", "y", "x") From e1002d5635d8a660e5e1fb63dd4b04447869b0da Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 25 Aug 2026 11:23:47 +0200 Subject: [PATCH 17/85] load/save tiff files with metadata --- changelog.md | 2 +- pyproject.toml | 2 +- src/bioimageio/core/axis.py | 28 +- src/bioimageio/core/cli.py | 5 +- src/bioimageio/core/io.py | 841 +++++++++++++++----- src/bioimageio/core/sample.py | 25 +- src/bioimageio/core/tensor.py | 62 +- src/bioimageio/core/utils/_color.py | 74 ++ src/bioimageio/core/utils/_tiff_metadata.py | 54 ++ src/bioimageio/core/utils/_type_guards.py | 15 +- src/bioimageio/core/utils/_xml_utils.py | 31 + tests/test_io.py | 26 +- 12 files changed, 904 insertions(+), 261 deletions(-) create mode 100644 src/bioimageio/core/utils/_color.py create mode 100644 src/bioimageio/core/utils/_tiff_metadata.py create mode 100644 src/bioimageio/core/utils/_xml_utils.py diff --git a/changelog.md b/changelog.md index d36c5d02..40dccc07 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ ### 0.12.0 -- bump spec to 0.5.13.1 +- bump spec to 0.5.14.0 - support loading zarr images - dropped `is_volume` flag from `load_image()` diff --git a/pyproject.toml b/pyproject.toml index 0429d2b3..8d8c1898 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ requires-python = ">=3.9" readme = "README.md" dynamic = ["version"] dependencies = [ - "bioimageio.spec ==0.5.13.1", + "bioimageio.spec ==0.5.14.1", "imagecodecs", "imageio>=2.10", "loguru", diff --git a/src/bioimageio/core/axis.py b/src/bioimageio/core/axis.py index af254fdf..147035b3 100644 --- a/src/bioimageio/core/axis.py +++ b/src/bioimageio/core/axis.py @@ -1,6 +1,6 @@ from __future__ import annotations -from collections.abc import Mapping +from collections.abc import Mapping, Sequence from dataclasses import dataclass from typing import ( Literal, @@ -142,33 +142,29 @@ def create(cls, axis: AxisLike, size: int | AxisSize | None = None) -> AxisInfo: def single_letter_dims_if_possible( - dims: tuple[AxisId, ...], + dims: Sequence[AxisId], ) -> tuple[str, ...]: """Return a tuple of single-letter dimension names if possible, otherwise return the original dimension names.""" single_letter_dims: list[str] = [] - def add_letter(d: str): - assert len(d) == 1 - not_unique = d in single_letter_dims - single_letter_dims.append(d) - return not_unique - for d in dims: d = str(d).lower() if d in ("batch", "b"): - not_unique = add_letter("b") + single_letter_dims.append("b") elif d in ("time", "t"): - not_unique = add_letter("t") + single_letter_dims.append("t") elif d in ("index", "i"): - not_unique = add_letter("i") + single_letter_dims.append("i") elif d in ("channel", "c"): - not_unique = add_letter("c") + single_letter_dims.append("c") elif d in "zyx": - not_unique = add_letter(d) + single_letter_dims.append(d) else: - return dims # Return original dims if any dim cannot be converted to a single letter + # Return original dims if any dim cannot be converted to a single letter + return tuple(dims) - if not_unique: - return dims # Return original dims if any single letter dim is not unique + if len(set(single_letter_dims)) != len(single_letter_dims): + # Return original dims if any single letter dim is not unique + return tuple(dims) return tuple(single_letter_dims) diff --git a/src/bioimageio/core/cli.py b/src/bioimageio/core/cli.py index c2e62111..289584c5 100644 --- a/src/bioimageio/core/cli.py +++ b/src/bioimageio/core/cli.py @@ -143,7 +143,10 @@ def descr_id(self) -> str: (replacing legacy ids with their nicknames) """ if isinstance(self.descr, InvalidDescr): - return str(getattr(self.descr, "id", self.descr.name)) + return str( + (self.descr.model_extra or {}).get("id") + or (self.descr.model_extra or {}).get("name", "unknown") + ) nickname = None if ( diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index a572c44a..1694fe70 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -6,9 +6,9 @@ import zipfile from collections.abc import Iterable, Mapping, Sequence from contextlib import nullcontext -from io import BytesIO +from io import BytesIO, TextIOWrapper from itertools import chain -from pathlib import Path, PosixPath, PurePath +from pathlib import Path, PosixPath, PurePath, PurePosixPath from shutil import copyfileobj from typing import TYPE_CHECKING, TypedDict, Union @@ -21,13 +21,20 @@ imwrite, # type: ignore ) from loguru import logger -from pydantic import BaseModel, RootModel +from pydantic import BaseModel, RootModel, TypeAdapter from typing_extensions import Literal, TypeAlias, assert_never from typing_extensions import TypeAliasType as _TypeAliasType -from bioimageio.spec import get_validation_context +from bioimageio.core.utils._tiff_metadata import ( + ImageJMetadata, + OmeChannelMetadata, + OmeMetadata, + create_ome_metadata_from_xml_string, +) from bioimageio.spec._internal.io import ( RelativeDirectory, + ZarrSource, + ZarrUrl, get_reader, interprete_file_source, ) @@ -35,6 +42,7 @@ BytesReader, FileDescr, FileSource, + FtpUrl, HttpUrl, PermissiveFileSource, RelativeFilePath, @@ -49,7 +57,9 @@ from .sample import Sample, SampleBlock from .stat_measures import DatasetMeasure, MeasureValue, SampleMeasure, Stat from .tensor import Tensor +from .utils._color import hex_to_rgb from .utils._io_zarr import open_zarr_multiscale_array +from .utils._type_guards import is_tuple if TYPE_CHECKING: JsonValue: TypeAlias = Union[ @@ -75,7 +85,7 @@ Mapping[str, "JsonValueReadOnly"], ] -IO_Lib: TypeAlias = Literal["bioio", "imageio", "clearscale", "numpy"] +IO_Lib: TypeAlias = Literal["bioio", "imageio", "clearscale", "numpy", "tifffile"] def load_tensor( @@ -91,9 +101,9 @@ def load_tensor( Defaults to trying various libraries depending on file extension. """ - file_source, extension, subdir = _interprete_tensor_source(source) + file_source, extensions, internal_path = _interprete_tensor_source(source) if io_lib is None: - try_io_libs = _select_io_libs_based_on_extension(extension) + try_io_libs = _select_io_libs_based_on_extension(extensions) exceptions: list[Exception] = [] for auto_io_lib in try_io_libs: @@ -110,18 +120,25 @@ def load_tensor( ) elif io_lib == "bioio": - t = _load_tensor_bioio(file_source, None if subdir is None else str(subdir)) + t = _load_tensor_bioio( + file_source, None if internal_path is None else str(internal_path) + ) elif io_lib == "clearscale": t = _load_tensor_clearscale(source) - elif io_lib == "numpy": - array = load_array(source) - t = Tensor.from_array(array, dims=axes) - axes = None elif io_lib == "imageio": reader = get_reader(source) - array = imread(reader.read(), extension=extension) # pyright: ignore[reportArgumentType] + array = imread( + reader.read(), + extension=extensions[-1] if extensions else None, # pyright: ignore[reportArgumentType] + ) + t = Tensor.from_array(array, dims=axes) + axes = None + elif io_lib == "numpy": + array = load_array(source) t = Tensor.from_array(array, dims=axes) axes = None + elif io_lib == "tifffile": + t = _load_tensor_tifffile(source) else: assert_never(io_lib) @@ -131,27 +148,34 @@ def load_tensor( return t -def _select_io_libs_based_on_extension(extension: str | None) -> tuple[IO_Lib, ...]: - if extension is None: +def _select_io_libs_based_on_extension( + extensions: list[str] | None, +) -> tuple[IO_Lib, ...]: + if not extensions: return ("bioio", "imageio", "clearscale") - extension = extension.lower() - if extension == ".npy": + if extensions[-1] == ".npy": return ("numpy",) - elif extension == ".zarr": + elif extensions[-1] == ".zarr": return ("bioio", "clearscale") + elif extensions[-2:] in ([".ome", ".tif"], [".ome", ".tiff"]): + return ("tifffile", "bioio") + elif extensions[-1] in (".tif", ".tiff"): + return ("tifffile", "imageio") else: return ("bioio", "imageio") def _load_tensor_bioio( - source: FileSource | ZipPath, - subdir: str | None, + source: ZarrSource | FileSource | ZipPath, + internal_path: str | None, ) -> Tensor: """Load an image tensor using bioio""" import bioio + logger.debug("Loading tensor from {} with bioio", source) + if isinstance(source, FileDescr): src = source.source else: @@ -163,8 +187,34 @@ def _load_tensor_bioio( del source img = bioio.BioImage(src) - if subdir is not None: - img.set_scene(subdir) + if internal_path is not None: + internal_path_found = False + try: + import bioio_ome_zarr + + except ImportError: + pass + else: + if isinstance(img.reader, bioio_ome_zarr.Reader): + multiscale = img.reader._multiscales_metadata[img.current_scene_index] # pyright: ignore + datasets = multiscale.get("datasets", []) # pyright: ignore + data_paths = [datasets[i].get("path") for i in img.resolution_levels] # pyright: ignore + if internal_path not in data_paths: + raise ValueError( + f"Resolution level {internal_path} not found in multiscales datasets: {data_paths}" + ) + resolution_level = data_paths.index(internal_path) + img.set_resolution_level(resolution_level) + internal_path_found = True + else: + raise NotImplementedError( + f"Reading from internal path {internal_path} is not implemented for bioio reader {type(img.reader)}" # pyright: ignore[reportUnknownArgumentType] + ) + + if not internal_path_found: + raise ValueError( + f"Internal path {internal_path} not found with bioio in {src}" + ) dataarray_bioio = img.xarray_dask_data.squeeze() # pyright: ignore[reportUnknownVariableType] assert isinstance(dataarray_bioio, xr.DataArray) @@ -182,32 +232,41 @@ def _load_tensor_bioio( # add physical scale scale_bioio: dict[str, float | None] = { "T": img.scale.T, - # "C": img.scale.C, # should be 'dimensionless' + "C": img.scale.C, "Z": img.scale.Z, "Y": img.scale.Y, "X": img.scale.X, } + if (raw_c_unit := img.dimension_properties.C.unit) is None or str( # pyright: ignore[reportUnknownVariableType] + raw_c_unit # pyright: ignore[reportUnknownArgumentType] + ) == "dimensionless": + c_unit = None + else: + c_unit = str(raw_c_unit) # pyright: ignore[reportUnknownArgumentType] unit_bioio: dict[str, str | None] = { "T": img.dimension_properties.T.unit, - # "C": img.dimension_properties.C.unit, # should be 'dimensionless' + "C": c_unit, "Z": img.dimension_properties.Z.unit, "Y": img.dimension_properties.Y.unit, "X": img.dimension_properties.X.unit, } - scale = {axes_map[k]: v for k, v in scale_bioio.items() if v is not None} - unit = {axes_map[k]: str(v) for k, v in unit_bioio.items() if v is not None} + scale = { + axes_map[k]: v + for k, v in scale_bioio.items() + if k in axes_map and v is not None + } + unit = { + axes_map[k]: str(v) + for k, v in unit_bioio.items() + if k in axes_map and v is not None + } t.set_physical_scale(scale) t.set_physical_scale_unit(unit) return t def _load_tensor_clearscale(source: FileDescr | PermissiveFileSource) -> Tensor: - """load a single image as numpy array - - Args: - source: image source - subdir: image name - """ + """load a single image as numpy array""" import dask.array as da if isinstance(source, FileDescr): @@ -231,6 +290,145 @@ def _load_tensor_clearscale(source: FileDescr | PermissiveFileSource) -> Tensor: return Tensor.from_xarray(xr.DataArray(image, dims=axes)) +def _load_tensor_tifffile(source: FileDescr | PermissiveFileSource) -> Tensor: + import tifffile + + logger.debug("loading tensor from {} with tifffile", source) + if isinstance(source, FileDescr): + source = source.source.absolute() + + if isinstance(source, RelativeFilePath): + source = source.absolute() + + if isinstance(source, ZipPath): + ctxt = source.open("rb") + name = source.name + suffixes = source.suffixes + else: + if isinstance(source, (RootHttpUrl, pydantic.AnyUrl)): + source = str(source) + + if isinstance(source, Path): + suffixes = source.suffixes + else: + suffixes = PurePosixPath(source).suffixes + + ctxt = nullcontext(source) + name = None + + with ctxt as f: + assert not isinstance(f, TextIOWrapper) + + tif = tifffile.TiffFile(f, name=name) + try: + xr_data = tif.asxarray() + page = tif.pages[0] + if isinstance(page, tifffile.TiffFrame): + page = None if page.pages is None else page.pages[0] + if isinstance(page, tifffile.TiffFrame): + page = None + + if page is None: + x_res = None + y_res = None + unit = None + else: + x_res = page.tags["XResolution"].value + y_res = page.tags["YResolution"].value + + try: + x_res = x_res[0] / x_res[1] if is_tuple(x_res) else x_res + x_res = None if x_res is None else float(x_res) + except Exception as e: + logger.opt(exception=e).warning( + "Failed to interprete tiff XResolution tag: {}", x_res + ) + x_res = None + + try: + y_res = y_res[0] / y_res[1] if is_tuple(y_res) else y_res + y_res = None if y_res is None else float(y_res) + except Exception as e: + logger.opt(exception=e).warning( + "Failed to interprete tiff YResolution tag: {}", y_res + ) + y_res = None + + unit_tag = page.tags.get("ResolutionUnit") + unit = unit_tag.value if unit_tag else None + + ij_metadata = ImageJMetadata(**(tif.imagej_metadata or {})) + ome_metadata = None + if ( + suffixes[-2:] in ([".ome", ".tif"], [".ome", ".tiff"]) + or not ij_metadata + ) and tif.ome_metadata is not None: + try: + ome_metadata = create_ome_metadata_from_xml_string(tif.ome_metadata) + except Exception as e: + logger.opt(exception=e).warning( + "Failed to parse OME-XML metadata from tif.ome_metadata" + ) + + if ome_metadata is None: + ome_metadata = OmeMetadata() + + finally: + tif.close() + + t = Tensor.from_xarray(xr_data) + + # set physical scale + x_scale = ome_metadata.PhysicalSizeX or ( + 1 / x_res if x_res is not None and x_res != 0 else None + ) + y_scale = ome_metadata.PhysicalSizeY or ( + 1 / y_res if y_res is not None and y_res != 0 else x_scale + ) + z_scale = ome_metadata.PhysicalSizeZ or ij_metadata.spacing or x_scale + t_scale = ( + ome_metadata.TimeIncrement or ij_metadata.finterval or None + if ij_metadata.fps is None + else 1 / ij_metadata.fps + ) + scale = { + AxisId("x"): x_scale, + AxisId("y"): y_scale, + AxisId("z"): z_scale, + AxisId("time"): t_scale, + } + t.set_physical_scale({k: v for k, v in scale.items() if k in t.dims}) + + # set physical scale unit + x_unit = ome_metadata.PhysicalSizeXUnit or ij_metadata.unit + if x_unit is None and unit is not None: + try: + x_unit = tifffile.RESUNIT(unit).name.lower() + except Exception as e: + logger.opt(exception=e).warning( + "Failed to interprete tiff ResolutionUnit tag: {}", unit + ) + x_unit = None + + y_unit = ome_metadata.PhysicalSizeYUnit or ij_metadata.yunit or x_unit + z_unit = ome_metadata.PhysicalSizeZUnit or ij_metadata.zunit or x_unit + t_unit = ome_metadata.TimeIncrementUnit or "s" + + scale_unit = { + AxisId("x"): x_unit, + AxisId("y"): y_unit, + AxisId("z"): z_unit, + AxisId("time"): t_unit, + } + t.set_physical_scale_unit({k: v for k, v in scale_unit.items() if k in t.dims}) + + # set channel names + if ome_metadata.Channel is not None: + t.channel_names = ome_metadata.Channel.Name + + return t + + Suffix = str @@ -241,14 +439,26 @@ def save_tensor( io_lib: IO_Lib | None = None, roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None = None, ) -> None: - output_path, extension, subdir = _interprete_tensor_source(path) + try: + output_path, extensions, internal_path = _interprete_tensor_source(path) + except Exception: + output_path, extensions, internal_path = _interprete_tensor_target(path) + if isinstance(output_path, RootHttpUrl): raise NotImplementedError( "Saving tensors to HTTP URLs is not implemented. Please save to a local file path." ) + if ( + (v0_5.BATCH_AXIS_ID in tensor.dims) + and tensor.tagged_shape.get(v0_5.BATCH_AXIS_ID, 0) == 1 + and (extensions is None or extensions[-1] != ".npy") + ): + # avoid saving tensors with a singleton batch dimension + tensor = tensor.squeeze(v0_5.BATCH_AXIS_ID) + if io_lib is None: - try_io_libs = _select_io_libs_based_on_extension(extension) + try_io_libs = _select_io_libs_based_on_extension(extensions) exceptions: list[Exception] = [] for try_io_lib in try_io_libs: try: @@ -271,58 +481,75 @@ def save_tensor( return _save_tensor_bioio( output_path, tensor, - subdir=subdir, + internal_path=internal_path, roi_and_tensor_shape=roi_and_tensor_shape, ) - elif io_lib == "imageio": - if roi_and_tensor_shape is not None: - raise NotImplementedError( - "Saving a region of interest (roi) is not implemented for imageio." - ) - return _save_tensor_imageio(output_path, tensor) elif io_lib == "clearscale": return _save_tensor_clearscale( output_path, tensor, - subdir=subdir, + internal_path=internal_path, roi_and_tensor_shape=roi_and_tensor_shape, ) + elif io_lib == "imageio": + if roi_and_tensor_shape is not None: + raise NotImplementedError( + "Saving a region of interest (roi) is not implemented for imageio." + ) + return _save_tensor_imageio(output_path, tensor, internal_path=internal_path) elif io_lib == "numpy": if roi_and_tensor_shape is not None: raise NotImplementedError( "Saving a region of interest (roi) is not implemented for numpy." ) + if internal_path: + raise NotImplementedError( + "Saving to an internal path is not implemented for numpy." + ) return _save_tensor_numpy(output_path, tensor) + elif io_lib == "tifffile": + if roi_and_tensor_shape is not None: + raise NotImplementedError( + "Saving a region of interest (roi) is not implemented for tifffile." + ) + if internal_path is not None: + raise NotImplementedError( + "Saving to an internal path is not implemented for tifffile." + ) + + return _save_tensor_tifffile(output_path, extensions, tensor) else: assert_never(io_lib) def _save_tensor_bioio( - path: Path | ZipPath, + path: Path | ZipPath | FtpUrl, tensor: Tensor, - subdir: PurePath | None, + internal_path: PurePath | None, roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None, ) -> None: """Save an image tensor using bioio""" - bioio_axes_map = { + import bioio_base.types + + logger.debug( + "Saving tensor to {}{} with bioio", + path, + f"/{internal_path}" if internal_path else "", + ) + bioio_axis_map = { "batch": "S", "channel": "C", "index": "S", "time": "T", } - if path.suffix.lower() in (".tif", ".tiff"): - if roi_and_tensor_shape is not None: + scale = tensor.get_physical_scale() + + if path.suffix.lower() == ".zarr": + if internal_path and str(internal_path) != "0": raise NotImplementedError( - "Saving a region of interest (roi) is not implemented for saving tiff files with io_lib='bioio'." + "Saving to an internal path that is not '0' is not implemented for zarr files with io_lib='bioio'." ) - - from bioio_ome_tiff.writers import OmeTiffWriter - - save = OmeTiffWriter.save # pyright: ignore[reportUnknownVariableType] - write = None - bioio_axis_letters = "TCZYX" - elif path.suffix.lower() == ".zarr": if roi_and_tensor_shape is None: tensor_shape = tensor.tagged_shape else: @@ -339,20 +566,9 @@ def _save_tensor_bioio( ) for a in tensor.dims ] - unit = tensor.get_physical_scale_unit() - axes_units = [unit.get(a) for a in tensor.dims] - - scale = tensor.get_physical_scale() - physical_pixel_size = [scale.get(a, 1.0) for a in tensor.dims] channel_names = tensor.channel_names channel_colors = tensor.channel_colors - if channel_colors is None and channel_names is not None: - # use spec's default channel colors - channel_colors = [ - c.as_hex(format="long")[:6] - for c in v0_5.ChannelAxis(channel_names=channel_names).channel_colors - ] channels = [ bioio_ome_zarr.writers.Channel( @@ -367,24 +583,32 @@ def _save_tensor_bioio( f"ZARR_FORMAT={zf} is not supported. (expected 2 or 3)" ) - save = None - write = bioio_ome_zarr.writers.OMEZarrWriter( + unit = tensor.get_physical_scale_unit() + writer = bioio_ome_zarr.writers.OMEZarrWriter( store=path, level_shapes=[[tensor_shape[d] for d in tensor.dims]], dtype=tensor.dtype, zarr_format=zf, - image_name="Image" if subdir is None else str(subdir), + image_name=tensor.name or "Image", axes_names=list(axes_names), axes_types=axes_types, - axes_units=axes_units, - physical_pixel_size=physical_pixel_size, + axes_units=[unit.get(a) for a in tensor.dims], + physical_pixel_size=[scale.get(a, 1.0) for a in tensor.dims], channels=channels, - ).write_full_volume - bioio_axis_letters = None - bioio_axes_map = None - # bioio_axis_letters = "TCZYX" - # bioio_axes_map["batch"] = "T" - # bioio_axes_map["index"] = "T" + ) + logger.info("OME-Zarr metadata: {}", writer.preview_metadata()) + writer.write_full_volume(tensor.data.data) + return + elif path.suffix.lower() in (".tif", ".tiff"): + if roi_and_tensor_shape is not None: + raise NotImplementedError( + "Saving a region of interest (roi) is not implemented for saving tiff files with io_lib='bioio'." + ) + + from bioio_ome_tiff.writers import OmeTiffWriter + + save = OmeTiffWriter.save # pyright: ignore[reportUnknownVariableType] + bioio_axis_letters = "TCZYX" elif path.suffix.lower() in (".gif", ".mp4", ".mkv"): if roi_and_tensor_shape is not None: raise NotImplementedError( @@ -395,8 +619,7 @@ def _save_tensor_bioio( tensor = tensor.squeeze() save = TimeseriesWriter.save # pyright: ignore[reportUnknownVariableType] - write = None - bioio_axes_map["channel"] = "S" + bioio_axis_map["channel"] = "S" bioio_axis_letters = "YXS" elif path.suffix.lower() in ( ".png", @@ -420,67 +643,154 @@ def _save_tensor_bioio( tensor = tensor.squeeze() save = TwoDWriter.save # pyright: ignore[reportUnknownVariableType] - write = None # channel dim denoted as 'S' - bioio_axes_map["channel"] = "S" + bioio_axis_map["channel"] = "S" bioio_axis_letters = "YXS" else: raise RuntimeError( f"Failed to identify a suitable writer for {path} with suffix={path.suffix}." ) - if save is not None: - assert write is None - bioio_data = tensor.data - if bioio_axes_map is not None: - bioio_data = bioio_data.rename( # pyright: ignore[reportUnknownVariableType] - { - a: bioio_axes_map.get(str(a).lower(), str(a)[:1].upper()) - for a in tensor.dims - } - ) - assert isinstance(bioio_data, xr.DataArray) + bioio_data = tensor.data + dim_map = { + d: bioio_axis_map.get(str(d).lower(), str(d)[:1].upper()) for d in tensor.dims + } - if bioio_axis_letters is not None and not all( - str(d) in bioio_axis_letters for d in bioio_data.dims - ): - raise ValueError( - f"Failed to save tensor with bioio: dimensions {bioio_data.dims} not in '{bioio_axis_letters}'." - ) + bioio_data = bioio_data.rename(dim_map) # pyright: ignore[reportUnknownVariableType] + assert isinstance(bioio_data, xr.DataArray) + + if not all(str(d) in bioio_axis_letters for d in bioio_data.dims): + logger.warning( + "Attempting to save tensor with dimensions {} not in '{}' using bioio.", + bioio_data.dims, + bioio_axis_letters, + ) + + dim_order = "".join(str(d) for d in bioio_data.dims) + + image_name = None if internal_path is None else str(internal_path) + physical_scale = tensor.get_physical_scale() + save( + bioio_data.data, + path, + dim_order=dim_order, + image_name=image_name, + channel_names=tensor.channel_names, + channel_colors=None + if (channel_colors := tensor.channel_colors) is None + else [list(hex_to_rgb(c)) for c in channel_colors], + physical_pixel_sizes=bioio_base.types.PhysicalPixelSizes( + physical_scale.get(AxisId("z")), + physical_scale.get(AxisId("y")), + physical_scale.get(AxisId("x")), + ), + ) + + +def _save_tensor_clearscale( + path: Path | ZipPath | FtpUrl, + tensor: Tensor, + internal_path: PurePath | None, + roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None, +) -> None: + import clearscale + import dask.array + import zarr - dim_order = "".join(str(d) for d in bioio_data.dims) + logger.debug("Saving tensor to {} with clearscale", path) - image_name = None if subdir is None else str(subdir) - save( - bioio_data.data, - path, - dim_order=dim_order, - image_name=image_name, - channel_names=tensor.channel_names, + if roi_and_tensor_shape is None: + roi = (slice(None),) * len(tensor.dims) + tensor_shape = tensor.tagged_shape + else: + roi_map, tensor_shape = roi_and_tensor_shape + roi = tuple( + slice(None) if (s := roi_map.get(d)) is None else slice(s.start, s.stop) + for d in tensor.dims ) - if write is not None: - assert save is None - write(tensor.data.data) + ZARR_FORMAT: int = int(os.getenv("ZARR_FORMAT", "3")) + SCALE_KEY = "s0" if internal_path is None else str(internal_path) + # Prefer single dimension names for compatibility with bioio + dim_map = dict(zip(tensor.dims, single_letter_dims_if_possible(tensor.dims))) + multiscale = clearscale.Multiscale( + { + SCALE_KEY: clearscale.Scale( + shape=clearscale.Shape(**dict(zip(dim_map.values(), tensor.shape))), + pixel_size={ + dim_map[k]: v for k, v in tensor.get_physical_scale().items() + } + or None, + unit={ + dim_map[k]: v for k, v in tensor.get_physical_scale_unit().items() + } + or None, + ) + } + ) + if ZARR_FORMAT == 2: + zarr_attrs: dict[str, JsonValueReadOnly] = { + "multiscales": [multiscale.to_ome_zarr(version="0.4")] + } + elif ZARR_FORMAT == 3: + zarr_attrs: dict[str, JsonValueReadOnly] = { + "ome": { + "version": "0.5", + "multiscales": [multiscale.to_ome_zarr(version="0.5")], + } + } + else: + raise NotImplementedError(f"ZARR_FORMAT={ZARR_FORMAT} is not supported.") -def _save_tensor_numpy(path: Path | ZipPath, tensor: Tensor) -> None: - if isinstance(path, ZipPath): - folder = path.filename.parent + zarr_group = zarr.open_group( + str(path), + mode="w", + zarr_format=ZARR_FORMAT, + attributes=zarr_attrs, + ) + + create_array_kwargs = _CreateArrayKwargs( + name=SCALE_KEY, + overwrite=True, + dimension_names=None if ZARR_FORMAT == 2 else tuple(dim_map.values()), + shards=None if ZARR_FORMAT == 2 else "auto", + ) + zarr_array = zarr_group.create_array( + **create_array_kwargs, + shape=[tensor_shape[d] for d in tensor.dims], + dtype=tensor.dtype, + write_data=True, + ) + xr_array = tensor.data + if dm := {k: v for k, v in dim_map.items() if k != v}: + xr_array = xr_array.rename(dm) # pyright: ignore[reportUnknownVariableType] + assert isinstance(xr_array, xr.DataArray) + + if isinstance(xr_array.data, np.ndarray): + zarr_array[roi] = xr_array.data + elif isinstance(xr_array.data, dask.array.Array): + dask.array.to_zarr(xr_array.data, zarr_array, compute=True, region=roi) else: - folder = path.parent + assert_never(xr_array.data) - folder.mkdir(exist_ok=True, parents=True) - save_array(path, tensor.to_numpy()) +def _save_tensor_imageio( + path: Path | ZipPath | FtpUrl, tensor: Tensor, internal_path: PurePath | None +) -> None: + if internal_path is not None: + raise NotImplementedError( + "Saving tensors to subdirectories is not implemented for imageio. Please save to a file path without internal path." + ) -def _save_tensor_imageio(path: Path | ZipPath, tensor: Tensor) -> None: if isinstance(path, ZipPath): raise NotImplementedError( "Saving tensors to zip files is not implemented for imageio. Please save to an unzipped file path." ) - path.parent.mkdir(exist_ok=True, parents=True) + logger.debug("Saving tensor to {} with imageio", path) + if not isinstance(path, FtpUrl): + path.parent.mkdir(exist_ok=True, parents=True) extension = path.suffix.lower() removed_singleton_axes: list[AxisId] = [] @@ -529,125 +839,252 @@ def _save_tensor_imageio(path: Path | ZipPath, tensor: Tensor) -> None: imwrite(path, tensor, extension=extension) -class CreateArrayKwargs(TypedDict): - name: str - overwrite: bool - dimension_names: tuple[str, ...] | None - shards: Literal["auto"] | tuple[int, ...] | None +def _save_tensor_numpy(path: Path | ZipPath | FtpUrl, tensor: Tensor) -> None: + if isinstance(path, FtpUrl): + raise NotImplementedError( + "Saving numpy arrays to FTP URLs is not implemented. Please save to a local file path instead." + ) + logger.debug("Saving tensor to {} with numpy", path) -def _save_tensor_clearscale( - path: Path | ZipPath, - tensor: Tensor, - subdir: PurePath | None, - roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None, + if isinstance(path, ZipPath): + folder = path.filename.parent + else: + folder = path.parent + + folder.mkdir(exist_ok=True, parents=True) + + save_array(path, tensor.to_numpy()) + + +def _save_tensor_tifffile( + path: Path | ZipPath | FtpUrl, extensions: list[str] | None, tensor: Tensor ) -> None: - import clearscale - import dask.array - import zarr + if isinstance(path, FtpUrl): + raise NotImplementedError( + "Saving tiff files to FTP URLs is not implemented. Please save to a local file path instead." + ) - if roi_and_tensor_shape is None: - roi = slice(None) - tensor_shape = tensor.tagged_shape - else: - roi_map, tensor_shape = roi_and_tensor_shape - roi = tuple( - slice(None) if (s := roi_map.get(d)) is None else slice(s.start, s.stop) - for d in tensor.dims + import tifffile + + logger.trace("tensor dims {}", tensor.dims) + tensor = tensor.transpose("TZCYX") # tifffile expects TZCYX order + logger.trace("tensor dims transposed {}", tensor.dims) + if len(tensor.dims) > 5: + logger.warning( + "Saving tensor with more than 5 dimensions ({}) will likely fail." ) - ZARR_FORMAT: int = int(os.getenv("ZARR_FORMAT", "3")) - SCALE_KEY = "s0" if subdir is None else str(subdir) - multiscale = clearscale.Multiscale( - { - SCALE_KEY: clearscale.Scale( - clearscale.Shape(**{str(k): v for k, v in tensor_shape.items()}) - ) - } - ) - if ZARR_FORMAT == 2: - zarr_attrs: dict[str, JsonValueReadOnly] = { - "multiscales": [multiscale.to_ome_zarr(version="0.4")] - } - elif ZARR_FORMAT == 3: - zarr_attrs: dict[str, JsonValueReadOnly] = { - "ome": { - "version": "0.5", - "multiscales": [multiscale.to_ome_zarr(version="0.5")], - } - } + logger.debug("Saving tensor to {} with tifffile", path) + + scale = tensor.get_physical_scale() + x_scale = scale.get(AxisId("x")) + y_scale = scale.get(AxisId("y")) + z_scale = scale.get(AxisId("z")) + t_scale = scale.get(AxisId("time")) + if x_scale is not None and y_scale is not None: + resolution = (1 / x_scale, 1 / y_scale) else: - raise NotImplementedError(f"ZARR_FORMAT={ZARR_FORMAT} is not supported.") + resolution = None + + scale_unit = tensor.get_physical_scale_unit() + x_unit = scale_unit.get(AxisId("x")) + y_unit = scale_unit.get(AxisId("y")) + z_unit = scale_unit.get(AxisId("z")) + t_unit = scale_unit.get(AxisId("time")) + if x_unit == "inch": + resolution_unit = tifffile.RESUNIT.INCH + elif x_unit in ("centimeter", "cm"): + resolution_unit = tifffile.RESUNIT.CENTIMETER + elif x_unit in ("millimeter", "mm"): + resolution_unit = tifffile.RESUNIT.MILLIMETER + elif x_unit in ("micrometer", "um", "µm"): + resolution_unit = tifffile.RESUNIT.MICROMETER + else: + resolution_unit = None - zarr_group = zarr.open_group( - str(path), - mode="w", - zarr_format=ZARR_FORMAT, - attributes=zarr_attrs, - ) - create_array_kwargs = CreateArrayKwargs( - name=SCALE_KEY, - overwrite=True, - dimension_names=None - if ZARR_FORMAT == 2 - else single_letter_dims_if_possible(tensor.dims), - shards=None if ZARR_FORMAT == 2 else "auto", - ) - zarr_array = zarr_group.create_array( - **create_array_kwargs, - shape=[tensor_shape[d] for d in tensor.dims], - dtype=tensor.dtype, - write_data=True, - ) + if isinstance(path, ZipPath): + folder = path.filename.parent + else: + folder = path.parent + + folder.mkdir(exist_ok=True, parents=True) + + if extensions is not None and extensions[-2:] in ( + [".ome", ".tif"], + [".ome", ".tiff"], + ): + metadata = OmeMetadata( + # "DimensionOrder": "TZCYX", # [::-1]?? # TODO: check if this is needed + TimeIncrement=t_scale, + TimeIncrementUnit=t_unit, + PhysicalSizeX=x_scale, + PhysicalSizeXUnit=x_unit, + PhysicalSizeY=y_scale, + PhysicalSizeYUnit=y_unit, + PhysicalSizeZ=z_scale, + PhysicalSizeZUnit=z_unit, + Channel=None + if tensor.channel_names is None + else OmeChannelMetadata(Name=tensor.channel_names), + ) + kind = "ome" + else: + if not t_unit or t_scale is None: + pass + elif isinstance(t_scale, (int, float)): + if t_unit in ("nanosecond", "nanosecond(s)", "ns"): + t_scale /= 1_000_000_000 + elif t_unit in ("microsecond", "microsecond(s)", "us", "µsec", "µs"): + t_scale /= 1_000_000 + elif t_unit in ("millisecond", "millisecond(s)", "ms"): + t_scale /= 1_000 + elif t_unit in ("minute", "minute(s)", "min"): + t_scale *= 60 + elif t_unit in ("hour", "hour(s)", "h"): + t_scale *= 3_600 + elif t_unit in ("day", "day(s)", "d"): + t_scale *= 86_400 + elif t_unit in ("week", "week(s)", "wk"): + t_scale *= 604_800 + elif t_unit in ("month", "month(s)", "mo"): + t_scale *= 2_629_746 # average month length in seconds + elif t_unit in ("year", "year(s)", "yr"): + t_scale *= 31_556_952 # average year length in seconds + elif t_unit not in ("second", "s"): + logger.warning( + "Unknown time unit '{}' encountered while saving an imagej tiff with tifffile", + t_unit, + ) + else: + logger.warning( + "Failed to convert time interval '{}' of type {} (with time unit '{}') to seconds while saving an imagej tiff with tifffile", + t_scale, + type(t_scale), + t_unit, + ) + + metadata = ImageJMetadata( + unit=x_unit, + yunit=y_unit, + zunit=z_unit, + spacing=z_scale, + finterval=t_scale, + fps=None if not isinstance(t_scale, (int, float)) else 1 / t_scale, + ) + kind = "imagej" - if isinstance(tensor.data.data, np.ndarray): - zarr_array[roi] = tensor.data.data - elif isinstance(tensor.data.data, dask.array.Array): - dask.array.to_zarr(tensor.data.data, zarr_array, compute=True, region=roi) + if isinstance(path, ZipPath): + ctxt = path.open("wb") else: - assert_never(tensor.data.data) + ctxt = nullcontext(path) + + with ctxt as f: + assert not isinstance(f, TextIOWrapper) + _ = tifffile.imwrite( + f, + tensor.data, + kind=kind, + software="bioimageio.core", + resolution=resolution, + metadata={ + k: v + for k, v in metadata.model_dump(mode="json", exclude_none=True).items() + }, + resolutionunit=resolution_unit, + ) + + +class _CreateArrayKwargs(TypedDict): + name: str + overwrite: bool + dimension_names: tuple[str, ...] | None + shards: Literal["auto"] | tuple[int, ...] | None -def _interprete_tensor_source(source: PermissiveFileSource | ZipPath | FileDescr): +_zarr_url_adapter: TypeAdapter[ZarrUrl] = TypeAdapter(ZarrUrl) + + +def _interprete_tensor_target( + source: PermissiveFileSource | ZarrSource | ZipPath | FileDescr, +): if isinstance(source, FileDescr): source = source.source if isinstance(source, (str, pydantic.AnyUrl)): - source = interprete_file_source(source) + try: + source = _zarr_url_adapter.validate_python(str(source)) + except Exception: + if ( + str(source).startswith("http://") + or str(source).startswith("https://") + or str(source).startswith("ftp://") + ): + raise + else: + source = Path(str(source)) if isinstance(source, (RelativeFilePath, RelativeDirectory)): source = source.absolute() - subdir = None - if isinstance(source, HttpUrl): + return _get_file_source_extension_and_internal_path(source) + + +def _interprete_tensor_source( + source: PermissiveFileSource | ZarrSource | ZipPath | FileDescr, +): + + if isinstance(source, FileDescr): + source = source.source + + if isinstance(source, (str, pydantic.AnyUrl)): + source = interprete_file_source(source, allow_zarr=True) + + if isinstance(source, (RelativeFilePath, RelativeDirectory)): + source = source.absolute() + + return _get_file_source_extension_and_internal_path(source) + + +def _get_file_source_extension_and_internal_path( + source: Path | ZipPath | RootHttpUrl | FtpUrl, +): + internal_path = None + if isinstance(source, (HttpUrl, RootHttpUrl, FtpUrl)): original_source_path = PosixPath(source.path or "") + parents: Iterable[FtpUrl | RootHttpUrl | Path | ZipPath] = source.parents elif isinstance(source, ZipPath): - return source, None, None + original_source_path = PosixPath(source.filename) + parents = [] + p = source + while p != p.parent: + parents.append(p.parent) + p = p.parent else: original_source_path = PosixPath(source) + parents = source.parents file_source = source - for parent in chain([source], source.parents): - extension = parent.suffix.lower() - if extension: - if isinstance(parent, RootHttpUrl): - with get_validation_context().replace(perform_io_checks=False): - file_source = HttpUrl(parent) - + for parent in chain([source], parents): + extensions = [s.lower() for s in parent.suffixes] + if extensions: + file_source = parent + if isinstance(parent, (RootHttpUrl, FtpUrl)): parent_path = PosixPath(parent.path or "") + elif isinstance(parent, ZipPath): + parent_path = PosixPath(parent.filename) else: - file_source = parent parent_path = PosixPath(parent) - subdir = original_source_path.relative_to(parent_path) - if subdir == PosixPath("."): - subdir = None + internal_path = original_source_path.relative_to(parent_path) + if internal_path == PosixPath("."): + internal_path = None break else: - extension = None + extensions = None - return file_source, extension, subdir + return file_source, extensions, internal_path def save_sample( diff --git a/src/bioimageio/core/sample.py b/src/bioimageio/core/sample.py index 2ac24f99..056c8c88 100644 --- a/src/bioimageio/core/sample.py +++ b/src/bioimageio/core/sample.py @@ -68,7 +68,12 @@ class Sample: def __getitem__( self, key: PerMember[ - SliceInfo | slice | int | PerAxis[SliceInfo | slice | int] | Tensor | xr.DataArray + SliceInfo + | slice + | int + | PerAxis[SliceInfo | slice | int] + | Tensor + | xr.DataArray ], ) -> Self: return self.__class__( @@ -77,6 +82,12 @@ def __getitem__( id=self.id, ) + def __postinit__(self): + # default tensor names to member ids + for m, t in self.members.items(): + if t.name is None: + t.name = str(m) + @property def batch_multi_index(self) -> pd.MultiIndex | None: """Return the batch multi-index of the sample, if it has one. @@ -300,13 +311,11 @@ def transpose( if ( len( - - batch_lengths := { - t.sizes[AxisId("batch")] - for t in members.values() - if AxisId("batch") in t.dims - } - + batch_lengths := { + t.sizes[AxisId("batch")] + for t in members.values() + if AxisId("batch") in t.dims + } ) > 1 ): diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index 3ef3aab8..8e9a43f9 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -35,6 +35,7 @@ QuantileMethod, SliceInfo, ) +from .utils._color import get_default_channel_colors from .utils._type_guards import is_dict, is_mapping, is_sequence try: @@ -166,17 +167,19 @@ def get_physical_scale(self) -> PerAxis[float]: scale: Mapping[Any, Any] = _scale if is_mapping(_scale) else {} del _scale return { - a if isinstance(a, AxisId) else AxisId(a): float(s) + a if isinstance(a, AxisId) else AxisId(a): 1.0 if s is None else float(s) for a, s in scale.items() - if s is not None } def set_physical_scale(self, scale: PerAxis[float | None]) -> None: """Set the physical scale of each axis in units (see `set_physical_scale_unit`).""" + if unknown_axes := [d for d in scale if d not in self.dims]: + raise ValueError( + f"Cannot set physical scale for axes {unknown_axes} not in tensor axes {self.dims}" + ) + self._data.attrs["physical_scale"] = { - a if isinstance(a, AxisId) else AxisId(a): float(s) - for a, s in scale.items() - if s is not None + d: 1.0 if (s := scale.get(d)) is None else float(s) for d in self.dims } def get_physical_scale_unit(self) -> PerAxis[SpaceUnit | TimeUnit | str]: @@ -184,20 +187,18 @@ def get_physical_scale_unit(self) -> PerAxis[SpaceUnit | TimeUnit | str]: _unit = self._data.attrs.get("physical_scale_unit", None) unit: Mapping[Any, Any] = _unit if is_mapping(_unit) else {} del _unit - return { - a if isinstance(a, AxisId) else AxisId(a): str(u) - for a, u in unit.items() - if u is not None - } + return {d: "" if (u := unit.get(d)) is None else str(u) for d in self.dims} def set_physical_scale_unit( self, unit: PerAxis[SpaceUnit | TimeUnit | str | None] ) -> None: """Set the physical unit of each axis.""" + if unknown_axes := [d for d in unit if d not in self.dims]: + raise ValueError( + f"Cannot set physical scale unit for axes {unknown_axes} not in tensor axes {self.dims}" + ) self._data.attrs["physical_scale_unit"] = { - a if isinstance(a, AxisId) else AxisId(a): str(u) - for a, u in unit.items() - if u is not None + d: "" if (u := unit.get(d)) is None else str(u) for d in self.dims } @property @@ -225,28 +226,36 @@ def channel_names(self, names: list[str] | None) -> None: logger.warning( "Number of channel names ({}) does not match number of channels ({})", len(names), - self.sizes.get(AxisId("c"), 0), + self.sizes.get(AxisId("channel"), 0), ) self._data.attrs["channel_names"] = names @property def channel_colors(self) -> list[str] | None: - """Return the channel colors if available, otherwise None.""" + """Return channel colors .""" + + n_channels = self.sizes.get(AxisId("channel"), 0) + if n_channels == 0: + return None colors = self._data.attrs.get("channel_colors", None) if not is_sequence(colors): - return None + colors = None - n_channels = self.sizes.get(AxisId("channel"), 0) - if len(colors) < n_channels: + if colors is not None and len(colors) < n_channels: logger.warning( - "Number of channel colors ({}) lower than number of channels ({})", + "Overwriting existing channel colors with default colors due to too few channel colors: {} colors for {} channels", len(colors), n_channels, ) - return None + colors = None - return [str(name) for name in colors[:n_channels]] + if colors is None: + default_channel_colors = get_default_channel_colors(n_channels) + self.channel_colors = default_channel_colors + return default_channel_colors + else: + return [str(c) for c in colors[:n_channels]] @channel_colors.setter def channel_colors(self, colors: list[str] | None) -> None: @@ -260,6 +269,17 @@ def channel_colors(self, colors: list[str] | None) -> None: ) self._data.attrs["channel_colors"] = colors + @property + def name(self) -> str | None: + """Return the name of the tensor if available, otherwise None.""" + name = self._data.attrs.get("name") + return None if name is None else str(name) + + @name.setter + def name(self, name: str | None) -> None: + """Set the name of the tensor.""" + self._data.attrs["name"] = name + def squeeze(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: """Return a tensor with all the singleton dimensions removed.""" squeezed = self._data.squeeze(dim=dim) # pyright: ignore[reportUnknownVariableType] diff --git a/src/bioimageio/core/utils/_color.py b/src/bioimageio/core/utils/_color.py new file mode 100644 index 00000000..df4e90a9 --- /dev/null +++ b/src/bioimageio/core/utils/_color.py @@ -0,0 +1,74 @@ +from __future__ import annotations + +from typing import cast + + +def hex_to_rgb(value: str): + value = value.lstrip("#") + if len(value) == 3: + value = "".join(v * 2 for v in value) + + if len(value) not in (6, 8): + raise ValueError(f"Invalid hex color: {value}") + + ret = tuple(int(value[i : i + 2], 16) for i in range(0, len(value), 2)) + assert len(ret) in (3, 4) + return cast(tuple[int, int, int] | tuple[int, int, int, int], ret) + + +def get_default_channel_colors(n_channels: int) -> list[str]: + """Get default channel colors for visualization purposes. + + - For < 8 channels: colorblind-friendly palette from https://www.nature.com/articles/nmeth.1618 (without black) + - For < 21 channels: discrete matplotlib colormap 'tab20b' (redistributed for more even color distribution < 20 channels) + - For >= 21 channels: sample colors from continuous matplotlib colormap 'cividis' + + Returns: + List of hex color strings. + """ + if n_channels < 8: + # use colorblind-friendly palette from https://www.nature.com/articles/nmeth.1618 + # (without black) + channel_colors = [ + "#E69F00", + "#56B4E9", + "#009E73", + "#F0E442", + "#0072B2", + "#D55E00", + "#CC79A7", + ][:n_channels] + elif n_channels < 21: + # use discrete matplotlib colormap 'tab20b' + # (redistributed for more even color distribution < 20 channels) + channel_colors = [ + "#393b79", + "#8ca252", + "#e7ba52", + "#e7969c", + "#7b4173", + "#5254a3", + "#b5cf6b", + "#e7cb94", + "#843c39", + "#a55194", + "#6b6ecf", + "#cedb9c", + "#8c6d31", + "#d6616b", + "#ce6dbd", + "#9c9ede", + "#637939", + "#bd9e39", + "#ad494a", + "#de9ed6", + ][:n_channels] + else: + # sample colors from continuous matplotlib colormap 'cividis' + import matplotlib.colors + import matplotlib.pyplot as plt + + cmap = plt.colormaps["cividis"].resampled(n_channels) + channel_colors = [matplotlib.colors.to_hex(cmap(i)) for i in range(n_channels)] + + return channel_colors diff --git a/src/bioimageio/core/utils/_tiff_metadata.py b/src/bioimageio/core/utils/_tiff_metadata.py new file mode 100644 index 00000000..4106e93a --- /dev/null +++ b/src/bioimageio/core/utils/_tiff_metadata.py @@ -0,0 +1,54 @@ +from __future__ import annotations + +from pydantic import BaseModel + +from ._xml_utils import xml_to_dict + + +class ImageJMetadata(BaseModel, extra="allow"): + """(Partial) Metadata for ImageJ TIFF files.""" + + unit: str | None = None + """The unit of measurement for the image (e.g., "micron", "pixel").""" + + yunit: str | None = None + """The unit of measurement for the Y dimension (e.g., "micron", "pixel").""" + + zunit: str | None = None + """The unit of measurement for the Z dimension (e.g., "micron", "pixel").""" + + spacing: float | None = None + """The physical spacing between slices in the Z dimension.""" + + finterval: float | None = None + """Time interval between frames in seconds.""" + + fps: float | None = None + """Frames per second for time-lapse images.""" + + +class OmeChannelMetadata(BaseModel, extra="allow"): + """Partial Metadata for OME-TIFF channel information.""" + + Name: list[str] + """The name of the channel.""" + + +class OmeMetadata(BaseModel, extra="allow"): + """Partial Metadata for ImageJ TIFF files.""" + + Channel: OmeChannelMetadata | None = None + DimensionOrder: str | None = None + PhysicalSizeX: float | None = None + PhysicalSizeXUnit: str | None = None + PhysicalSizeY: float | None = None + PhysicalSizeYUnit: str | None = None + PhysicalSizeZ: float | None = None + PhysicalSizeZUnit: str | None = None + TimeIncrement: float | None = None + TimeIncrementUnit: str | None = None + + +def create_ome_metadata_from_xml_string(xml_string: str) -> OmeMetadata: + raw_ome_metadata = xml_to_dict(xml_string) + return OmeMetadata.model_validate(raw_ome_metadata) diff --git a/src/bioimageio/core/utils/_type_guards.py b/src/bioimageio/core/utils/_type_guards.py index 0648c120..026f49c2 100644 --- a/src/bioimageio/core/utils/_type_guards.py +++ b/src/bioimageio/core/utils/_type_guards.py @@ -1,12 +1,21 @@ """use these type guards with caution! They widen the type to T[Any], which is not always correct.""" +from typing import Any + +from typing_extensions import TypeGuard + from bioimageio.spec._internal import type_guards -is_list = type_guards.is_list -is_ndarray = type_guards.is_ndarray -is_tuple = type_guards.is_tuple is_dict = type_guards.is_dict is_kwargs = type_guards.is_kwargs +is_list = type_guards.is_list is_mapping = type_guards.is_mapping +is_ndarray = type_guards.is_ndarray is_sequence = type_guards.is_sequence +is_tuple = type_guards.is_tuple + + +def is_list_of_string(v: Any) -> TypeGuard[list[str]]: + """to avoid List[Unknown]""" + return is_list(v) and all(isinstance(x, str) for x in v) diff --git a/src/bioimageio/core/utils/_xml_utils.py b/src/bioimageio/core/utils/_xml_utils.py new file mode 100644 index 00000000..13ed7d72 --- /dev/null +++ b/src/bioimageio/core/utils/_xml_utils.py @@ -0,0 +1,31 @@ +from __future__ import annotations + +from collections import defaultdict +from typing import Any +from xml.etree import ElementTree + + +def xml_to_dict(xml_string: str): + return etree_to_dict(ElementTree.XML(xml_string)) + + +def etree_to_dict(t: ElementTree.Element): + # adapted from https://stackoverflow.com/a/10077069 + d: dict[str, Any] = {t.tag: {} if t.attrib else None} + children = list(t) + if children: + dd: defaultdict[str, list[Any]] = defaultdict(list) + for dc in map(etree_to_dict, children): + for k, v in dc.items(): + dd[k].append(v) + d = {t.tag: {k: v[0] if len(v) == 1 else v for k, v in dd.items()}} + if t.attrib: + d[t.tag].update(("@" + k, v) for k, v in t.attrib.items()) + if t.text: + text = t.text.strip() + if children or t.attrib: + if text: + d[t.tag]["#text"] = text + else: + d[t.tag] = text + return d diff --git a/tests/test_io.py b/tests/test_io.py index 1846c80b..ecb61fb2 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,3 +1,4 @@ +import sys from pathlib import Path from typing import Literal @@ -5,6 +6,10 @@ import pytest +@pytest.mark.skipif( + sys.version_info < (3, 10), + reason="various bioio writer modules are only available for py>=3.10.", +) @pytest.mark.parametrize( "name", [ @@ -99,16 +104,21 @@ def test_tensor_io( np.arange(np.prod(shape), dtype=np.uint8).reshape(shape), dims=f"{c}YX"[-len(shape) :], ) - # if io_lib == "bioio" and path.suffix == ".zarr": - # # rename axes to enable roundtrip with bioio - # expected._data = expected.data.rename( - # {a: str(a)[0].upper() for a in expected.dims} - # ) - save_tensor(path, expected, io_lib=io_lib) + def save(): + save_tensor(path, expected, io_lib=io_lib) - actual = load_tensor(path, io_lib=io_lib) - assert (actual == expected).to_numpy().all() + if io_lib == "bioio" and name.endswith(".zarr/s2"): + with pytest.raises( + NotImplementedError, + match="Saving to an internal path that is not '0' is not implemented for zarr files with io_lib='bioio'.", + ): + save() + else: + save() + + actual = load_tensor(path, io_lib=io_lib) + assert (actual == expected).to_numpy().all() def test_load_tensor_zarr(): From 571c8beb9425e2ed72f44895e5da569693f04854 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 25 Aug 2026 23:54:42 +0200 Subject: [PATCH 18/85] unstack while saving blockwise --- changelog.md | 2 +- src/bioimageio/core/_prediction_pipeline.py | 1 + src/bioimageio/core/io.py | 96 +++++++++++++++++---- src/bioimageio/core/sample.py | 15 +++- src/bioimageio/core/tensor.py | 20 +++-- 5 files changed, 103 insertions(+), 31 deletions(-) diff --git a/changelog.md b/changelog.md index 40dccc07..3dcbdec7 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ ### 0.12.0 -- bump spec to 0.5.14.0 +- bump spec to 0.5.14.1 - support loading zarr images - dropped `is_volume` flag from `load_image()` diff --git a/src/bioimageio/core/_prediction_pipeline.py b/src/bioimageio/core/_prediction_pipeline.py index ea7b4b33..3e4eac03 100644 --- a/src/bioimageio/core/_prediction_pipeline.py +++ b/src/bioimageio/core/_prediction_pipeline.py @@ -319,6 +319,7 @@ def predict_sample_with_blocking_yield_intermediates( "`predict_sample_with_blocking` not implemented for v0_4.ModelDescr" + f" {self._model_descr.name}." + " Consider using `predict_sample_with_fixed_blocking`" + + " or load_model_description(..., format_version='0.5')." ) ns = ns or self._default_blocksize_parameter diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index 1694fe70..10667946 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -131,19 +131,36 @@ def load_tensor( reader.read(), extension=extensions[-1] if extensions else None, # pyright: ignore[reportArgumentType] ) - t = Tensor.from_array(array, dims=axes) - axes = None + try: + t = Tensor.from_array(array, dims=axes) + except Exception: + if axes is None: + raise + + t = Tensor.from_array(array, dims=None) + else: + axes = None elif io_lib == "numpy": array = load_array(source) - t = Tensor.from_array(array, dims=axes) - axes = None + try: + t = Tensor.from_array(array, dims=axes) + except Exception: + if axes is None: + raise + + t = Tensor.from_array(array, dims=None) + else: + axes = None + elif io_lib == "tifffile": t = _load_tensor_tifffile(source) else: assert_never(io_lib) if axes is not None: - t = t.transpose(axes) + t = t.transpose( + axes, extra_dims="squeeze_or_stack", missing_dims="unstack_or_expand" + ) return t @@ -439,6 +456,18 @@ def save_tensor( io_lib: IO_Lib | None = None, roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None = None, ) -> None: + _ = _save_tensor_impl( + path, tensor, io_lib=io_lib, roi_and_tensor_shape=roi_and_tensor_shape + ) + + +def _save_tensor_impl( + path: Path | str, + tensor: Tensor, + *, + io_lib: IO_Lib | None = None, + roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None = None, +) -> IO_Lib: try: output_path, extensions, internal_path = _interprete_tensor_source(path) except Exception: @@ -449,20 +478,40 @@ def save_tensor( "Saving tensors to HTTP URLs is not implemented. Please save to a local file path." ) - if ( - (v0_5.BATCH_AXIS_ID in tensor.dims) - and tensor.tagged_shape.get(v0_5.BATCH_AXIS_ID, 0) == 1 - and (extensions is None or extensions[-1] != ".npy") + if (v0_5.BATCH_AXIS_ID in tensor.dims) and ( + not extensions or extensions[-1] != ".npy" ): - # avoid saving tensors with a singleton batch dimension - tensor = tensor.squeeze(v0_5.BATCH_AXIS_ID) + if roi_and_tensor_shape is None: + batch_len = tensor.tagged_shape.get(v0_5.BATCH_AXIS_ID, 0) + else: + roi, tensor_shape = roi_and_tensor_shape + batch_len = tensor_shape.get(v0_5.BATCH_AXIS_ID, 0) + + if batch_len > 1: + tensor = tensor.unstack_batch_multi_index(errors="warn") + if roi_and_tensor_shape is not None: + # TODO: use tensor.data.multi_index to unstack the roi and tensor_shape as well + raise NotImplementedError( + "Saving a region of interest (roi) is not implemented for tensors with a batch dimension." + ) + + if batch_len == 1: + # avoid saving tensors with a singleton batch dimension + tensor = tensor.squeeze(v0_5.BATCH_AXIS_ID) + if roi_and_tensor_shape is not None: + roi, tensor_shape = roi_and_tensor_shape + roi = {k: v for k, v in roi.items() if k != v0_5.BATCH_AXIS_ID} + tensor_shape = { + k: v for k, v in tensor_shape.items() if k != v0_5.BATCH_AXIS_ID + } + roi_and_tensor_shape = (roi, tensor_shape) if io_lib is None: try_io_libs = _select_io_libs_based_on_extension(extensions) exceptions: list[Exception] = [] for try_io_lib in try_io_libs: try: - return save_tensor( + return _save_tensor_impl( path, tensor, io_lib=try_io_lib, @@ -478,14 +527,14 @@ def save_tensor( "Failed to save tensor with any available io_lib", exceptions ) elif io_lib == "bioio": - return _save_tensor_bioio( + _save_tensor_bioio( output_path, tensor, internal_path=internal_path, roi_and_tensor_shape=roi_and_tensor_shape, ) elif io_lib == "clearscale": - return _save_tensor_clearscale( + _save_tensor_clearscale( output_path, tensor, internal_path=internal_path, @@ -496,7 +545,7 @@ def save_tensor( raise NotImplementedError( "Saving a region of interest (roi) is not implemented for imageio." ) - return _save_tensor_imageio(output_path, tensor, internal_path=internal_path) + _save_tensor_imageio(output_path, tensor, internal_path=internal_path) elif io_lib == "numpy": if roi_and_tensor_shape is not None: raise NotImplementedError( @@ -506,7 +555,7 @@ def save_tensor( raise NotImplementedError( "Saving to an internal path is not implemented for numpy." ) - return _save_tensor_numpy(output_path, tensor) + _save_tensor_numpy(output_path, tensor) elif io_lib == "tifffile": if roi_and_tensor_shape is not None: raise NotImplementedError( @@ -517,10 +566,12 @@ def save_tensor( "Saving to an internal path is not implemented for tifffile." ) - return _save_tensor_tifffile(output_path, extensions, tensor) + _save_tensor_tifffile(output_path, extensions, tensor) else: assert_never(io_lib) + return io_lib + def _save_tensor_bioio( path: Path | ZipPath | FtpUrl, @@ -702,12 +753,14 @@ def _save_tensor_clearscale( if roi_and_tensor_shape is None: roi = (slice(None),) * len(tensor.dims) tensor_shape = tensor.tagged_shape + tensor_shape_tuple = tensor.shape else: roi_map, tensor_shape = roi_and_tensor_shape roi = tuple( slice(None) if (s := roi_map.get(d)) is None else slice(s.start, s.stop) for d in tensor.dims ) + tensor_shape_tuple = tuple(tensor_shape.get(d, 1) for d in tensor.dims) ZARR_FORMAT: int = int(os.getenv("ZARR_FORMAT", "3")) SCALE_KEY = "s0" if internal_path is None else str(internal_path) @@ -717,7 +770,9 @@ def _save_tensor_clearscale( multiscale = clearscale.Multiscale( { SCALE_KEY: clearscale.Scale( - shape=clearscale.Shape(**dict(zip(dim_map.values(), tensor.shape))), + shape=clearscale.Shape( + **dict(zip(dim_map.values(), tensor_shape_tuple)) + ), pixel_size={ dim_map[k]: v for k, v in tensor.get_physical_scale().items() } @@ -1116,6 +1171,7 @@ def save_sample( f"path {path} must contain '{{member_id}}' for sample with multiple members {list(first_block.members)}." ) + io_lib = None for sample_block in chain([first_block], remaining_blocks): for m, p in path.items(): if isinstance(sample_block, Sample): @@ -1133,7 +1189,9 @@ def save_sample( p_formatted = Path( str(p).format(sample_id=sample_id, member_id=m, input_id=m, output_id=m) ) - save_tensor(p_formatted, t, roi_and_tensor_shape=roi_and_tensor_shape) + io_lib = _save_tensor_impl( + p_formatted, t, roi_and_tensor_shape=roi_and_tensor_shape, io_lib=io_lib + ) class _StatEntry(BaseModel, frozen=True, arbitrary_types_allowed=True): diff --git a/src/bioimageio/core/sample.py b/src/bioimageio/core/sample.py index 056c8c88..32609beb 100644 --- a/src/bioimageio/core/sample.py +++ b/src/bioimageio/core/sample.py @@ -17,8 +17,9 @@ import pandas as pd import pydantic import xarray as xr +from loguru import logger from numpy.typing import NDArray -from typing_extensions import Self +from typing_extensions import Self, assert_never from ._common_annotations import PerMemberAnno from .axis import AxisId, PerAxis @@ -350,7 +351,7 @@ def assign_batch_multi_index(self, multi_index: pd.MultiIndex) -> Self: ) def unstack_batch_multi_index( - self, *, errors: Literal["raise", "ignore"] = "raise" + self, *, errors: Literal["raise", "warn", "ignore"] = "raise" ) -> Self: """Unstack the batch multi-index of all sample members. @@ -367,9 +368,15 @@ def unstack_batch_multi_index( ] ) == len(self.members) - and errors == "raise" + and errors != "ignore" ): - raise ValueError(f"No member has a batch dimension: {no_batch}") + error_msg = f"No member has a batch dimension: {no_batch}" + if errors == "raise": + raise ValueError(error_msg) + elif errors == "warn": + logger.warning(error_msg) + else: + assert_never(errors) members = { m: t diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index 8e9a43f9..41abc181 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -639,7 +639,7 @@ def assign_batch_multi_index(self, multi_index: pd.MultiIndex) -> Self: ) def unstack_batch_multi_index( - self, *, errors: Literal["raise", "ignore"] = "raise" + self, *, errors: Literal["raise", "warn", "ignore"] = "raise" ) -> Self: """Unstack the batch multi-index of this tensor. @@ -647,20 +647,26 @@ def unstack_batch_multi_index( A new tensor with the batch multi-index unstacked into separate axes. """ if AxisId("batch") not in self.dims: + error_msg = ( + "Cannot unstack batch multi-index on a tensor without a 'batch' axis." + ) if errors == "raise": - raise ValueError( - "Cannot unstack batch multi-index on a tensor without a 'batch' axis." - ) + raise ValueError(error_msg) + elif errors == "warn": + logger.warning(error_msg) + return self elif errors == "ignore": return self else: assert_never(errors) if not isinstance(self._data.indexes.get(AxisId("batch")), pd.MultiIndex): + error_msg = "Cannot unstack batch multi-index on a tensor whose 'batch' axis does not have a MultiIndex." if errors == "raise": - raise ValueError( - "Cannot unstack batch multi-index on a tensor whose 'batch' axis does not have a MultiIndex." - ) + raise ValueError(error_msg) + elif errors == "warn": + logger.warning(error_msg) + return self elif errors == "ignore": return self else: From 8dcc6c18566702cedd56f9a8a13c826360b38d18 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Thu, 27 Aug 2026 11:32:25 +0200 Subject: [PATCH 19/85] WIP unstacked shapes --- src/bioimageio/core/_op_base.py | 22 ++++++- src/bioimageio/core/axis.py | 2 + src/bioimageio/core/block.py | 50 +++++++++++--- src/bioimageio/core/block_meta.py | 62 ++++++++++++----- src/bioimageio/core/io.py | 48 ++++++-------- src/bioimageio/core/sample.py | 106 ++++++++++++++++++++++++++++-- src/bioimageio/core/tensor.py | 61 ++++++++++++----- 7 files changed, 273 insertions(+), 78 deletions(-) diff --git a/src/bioimageio/core/_op_base.py b/src/bioimageio/core/_op_base.py index bc85c7cb..3eb96942 100644 --- a/src/bioimageio/core/_op_base.py +++ b/src/bioimageio/core/_op_base.py @@ -8,7 +8,7 @@ from typing_extensions import TypeVar, assert_never from ._restore_batch_multi_index import restore_batch_multi_index -from .axis import PerAxis +from .axis import BATCH_AXIS_ID, PerAxis from .block import Block from .common import MemberId from .sample import Sample, SampleBlock, SampleBlockWithOrigin @@ -72,10 +72,28 @@ def __call__(self, sample: Sample | SampleBlock) -> None: sample.members[self.output] = output_tensor elif isinstance(sample, SampleBlock): b = sample.blocks[self.input] + sample_shape_batch_unstacked = { + a: s + for a, s in sample.sample_shape_batch_unstacked[self.input].items() + if a not in sample.sample_shape[self.input] + } + sample_shape = self.get_output_shape(sample.sample_shape[self.input]) + for a, s in sample_shape.items(): + if a == BATCH_AXIS_ID: + continue + elif a in sample_shape_batch_unstacked: + raise ValueError( + f"Output axis {a} already exists in sample_shape_batch_unstacked" + ) + + sample_shape_batch_unstacked[a] = s + sample.blocks[self.output] = Block( - sample_shape=self.get_output_shape(sample.sample_shape[self.input]), + sample_shape=sample_shape, + sample_shape_batch_unstacked=sample_shape_batch_unstacked, data=output_tensor, inner_slice=b.inner_slice, + inner_slice_batch_unstacked=b.inner_slice_batch_unstacked, halo=b.halo, block_index=b.block_index, blocks_in_sample=b.blocks_in_sample, diff --git a/src/bioimageio/core/axis.py b/src/bioimageio/core/axis.py index 147035b3..bc2785b0 100644 --- a/src/bioimageio/core/axis.py +++ b/src/bioimageio/core/axis.py @@ -41,6 +41,8 @@ def _guess_axis_type(a: str): AxisId: TypeAlias = v0_5.AxisId """An axis identifier, e.g. 'batch', 'channel', 'z', 'y', 'x'""" +BATCH_AXIS_ID = v0_5.BATCH_AXIS_ID + _T = TypeVar("_T") PerAxis = Mapping[AxisId, _T] diff --git a/src/bioimageio/core/block.py b/src/bioimageio/core/block.py index b3c3936e..a3b65ada 100644 --- a/src/bioimageio/core/block.py +++ b/src/bioimageio/core/block.py @@ -6,9 +6,10 @@ Any, ) +import pandas as pd from typing_extensions import Self -from .axis import PerAxis +from .axis import BATCH_AXIS_ID, AxisId, PerAxis from .block_meta import BlockMeta, LinearAxisTransform, split_shape_into_blocks from .common import ( Halo, @@ -50,27 +51,57 @@ def from_sample_member( *, pad_mode: PadMode, ) -> Self: + if ( + block.inner_slice_batch_unstacked == block.inner_slice + and block.sample_shape == block.sample_shape_batch_unstacked + and isinstance( + idx := sample_member.data.indexes.get(BATCH_AXIS_ID), # pyright: ignore[reportUnknownVariableType] + pd.MultiIndex, + ) + and block.inner_slice[BATCH_AXIS_ID] == block.outer_slice[BATCH_AXIS_ID] + ): + # if sample_member has a multi index for the batch axis, we keep track of the unstacked axes of this block + # start with inner_slice without the batch axis, and add the unstacked slice for each axis in the multi index + sample_shape_batch_unstacked = dict(block.sample_shape) + inner_slice_batch_unstacked = dict(block.inner_slice) + b_slice = block.inner_slice[BATCH_AXIS_ID] + for a, s, c in zip(idx.names, idx.levshape, idx.codes): + axis = BATCH_AXIS_ID if a == "original_batch" else AxisId(a) + sample_shape_batch_unstacked[axis] = s + codes = c[b_slice.start : b_slice.stop] + si = SliceInfo(int(codes.min()), int(codes.max()) + 1) + inner_slice_batch_unstacked[axis] = si + else: + inner_slice_batch_unstacked = block.inner_slice_batch_unstacked + sample_shape_batch_unstacked = block.sample_shape_batch_unstacked + return cls( data=sample_member[block.outer_slice].pad(block.padding, pad_mode), sample_shape=sample_member.tagged_shape, + sample_shape_batch_unstacked=sample_shape_batch_unstacked, inner_slice=block.inner_slice, + inner_slice_batch_unstacked=inner_slice_batch_unstacked, halo=block.halo, block_index=block.block_index, blocks_in_sample=block.blocks_in_sample, ) - def get_transformed( - self, new_axes: PerAxis[LinearAxisTransform | int] - ) -> Self: + def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: raise NotImplementedError @classmethod def from_meta(cls, meta: BlockMeta, data: Tensor) -> Self: + sample_shape = { + k: data.tagged_shape[k] if v == -1 else v + for k, v in meta.sample_shape.items() + } + sample_shape_batch_unstacked = { + k: data.tagged_shape[k] if v == -1 else v + for k, v in meta.sample_shape_batch_unstacked.items() + } return cls( - sample_shape={ - k: data.tagged_shape[k] if v == -1 else v - for k, v in meta.sample_shape.items() - }, + sample_shape=sample_shape, + sample_shape_batch_unstacked=sample_shape_batch_unstacked, inner_slice={ k: ( SliceInfo(start=v.start, stop=data.tagged_shape[k]) @@ -79,6 +110,7 @@ def from_meta(cls, meta: BlockMeta, data: Tensor) -> Self: ) for k, v in meta.inner_slice.items() }, + inner_slice_batch_unstacked=meta.inner_slice_batch_unstacked, halo=meta.halo, block_index=meta.block_index, blocks_in_sample=meta.blocks_in_sample, @@ -88,7 +120,9 @@ def from_meta(cls, meta: BlockMeta, data: Tensor) -> Self: def get_meta(self) -> BlockMeta: return BlockMeta( sample_shape=self.sample_shape, + sample_shape_batch_unstacked=self.sample_shape_batch_unstacked, inner_slice=self.inner_slice, + inner_slice_batch_unstacked=self.inner_slice_batch_unstacked, halo=self.halo, block_index=self.block_index, blocks_in_sample=self.blocks_in_sample, diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py index dc4584ea..7a83b482 100644 --- a/src/bioimageio/core/block_meta.py +++ b/src/bioimageio/core/block_meta.py @@ -16,7 +16,7 @@ from typing_extensions import Self from ._axis_annotations import PerAxisAnno -from .axis import AxisId, PerAxis +from .axis import BATCH_AXIS_ID, AxisId, PerAxis from .common import ( BlockIndex, Halo, @@ -76,9 +76,15 @@ class BlockMeta: sample_shape: PerAxisAnno[int] """the axis sizes of the whole (unblocked) sample""" + sample_shape_batch_unstacked: PerAxisAnno[int] + """the axis sizes of the whole (unblocked) sample, with unstacked batch axes (if any)""" + inner_slice: PerAxisAnno[SliceInfo] """inner region (without halo) wrt the sample""" + inner_slice_batch_unstacked: PerAxisAnno[SliceInfo] + """inner slice (without halo) wrt the sample unstacked along the batch axis (if present)""" + halo: PerAxisAnno[Halo] """halo enlarging the inner region to the block's sizes""" @@ -206,25 +212,39 @@ def __post_init__(self): ) def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: + assert BATCH_AXIS_ID not in new_axes, "cannot transform the batch axis" + unstacked_dims = { + k for k in self.inner_slice_batch_unstacked if k not in self.inner_slice + } + return self.__class__( - sample_shape={ - a: ( - trf - if isinstance(trf, int) - else trf.compute(self.sample_shape[trf.axis]) - ) - for a, trf in new_axes.items() - }, - inner_slice={ - a: ( - SliceInfo(0, trf) - if isinstance(trf, int) - else SliceInfo( - trf.compute(self.inner_slice[trf.axis].start), - trf.compute(self.inner_slice[trf.axis].stop), + sample_shape=( + sample_shape := { + a: ( + trf + if isinstance(trf, int) + else trf.compute(self.sample_shape[trf.axis]) ) - ) - for a, trf in new_axes.items() + for a, trf in new_axes.items() + } + ), + sample_shape_batch_unstacked=sample_shape, + inner_slice=( + transformed_inner_slice := { + a: ( + SliceInfo(0, trf) + if isinstance(trf, int) + else SliceInfo( + trf.compute(self.inner_slice[trf.axis].start), + trf.compute(self.inner_slice[trf.axis].stop), + ) + ) + for a, trf in new_axes.items() + } + ), + inner_slice_batch_unstacked={ + **{k: self.inner_slice_batch_unstacked[k] for k in unstacked_dims}, + **transformed_inner_slice, }, halo={ a: ( @@ -241,6 +261,7 @@ def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: def split_shape_into_blocks( shape: PerAxis[int], + shape_unstacked: PerAxis[int], block_shape: PerAxis[int], halo: PerAxis[HaloLike], stride: PerAxis[int] | None = None, @@ -276,6 +297,7 @@ def split_shape_into_blocks( return n_blocks, _block_meta_generator( shape, + shape_unstacked, blocks_in_sample=n_blocks, inner_1d_slices=inner_1d_slices, halo=halo, @@ -284,6 +306,7 @@ def split_shape_into_blocks( def _block_meta_generator( sample_shape: PerAxis[int], + shape_unstacked: PerAxis[int], *, blocks_in_sample: int, inner_1d_slices: dict[AxisId, list[SliceInfo]], @@ -297,7 +320,9 @@ def _block_meta_generator( yield BlockMeta( sample_shape=sample_shape, + sample_shape_batch_unstacked=shape_unstacked, inner_slice=inner_slice, + inner_slice_batch_unstacked=inner_slice, halo=halo, block_index=i, blocks_in_sample=blocks_in_sample, @@ -346,6 +371,7 @@ def split_multiple_shapes_into_blocks( for t in block_shapes: n_blocks[t], blocks[t] = split_shape_into_blocks( shape=shapes[t], + shape_unstacked= block_shape=block_shapes[t], halo=halo.get(t, {}), stride=strides.get(t), diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index 10667946..60091d15 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -49,10 +49,15 @@ RootHttpUrl, ZipPath, ) -from bioimageio.spec.model import v0_5 from bioimageio.spec.utils import load_array, save_array -from .axis import AxisId, AxisLike, PerAxis, single_letter_dims_if_possible +from .axis import ( + BATCH_AXIS_ID, + AxisId, + AxisLike, + PerAxis, + single_letter_dims_if_possible, +) from .common import PerMember, SliceInfo from .sample import Sample, SampleBlock from .stat_measures import DatasetMeasure, MeasureValue, SampleMeasure, Stat @@ -478,33 +483,19 @@ def _save_tensor_impl( "Saving tensors to HTTP URLs is not implemented. Please save to a local file path." ) - if (v0_5.BATCH_AXIS_ID in tensor.dims) and ( - not extensions or extensions[-1] != ".npy" - ): - if roi_and_tensor_shape is None: - batch_len = tensor.tagged_shape.get(v0_5.BATCH_AXIS_ID, 0) - else: + if (BATCH_AXIS_ID in tensor.dims) and (not extensions or extensions[-1] != ".npy"): + if roi_and_tensor_shape is not None: roi, tensor_shape = roi_and_tensor_shape - batch_len = tensor_shape.get(v0_5.BATCH_AXIS_ID, 0) - - if batch_len > 1: - tensor = tensor.unstack_batch_multi_index(errors="warn") - if roi_and_tensor_shape is not None: - # TODO: use tensor.data.multi_index to unstack the roi and tensor_shape as well + if BATCH_AXIS_ID in roi or BATCH_AXIS_ID in tensor_shape: raise NotImplementedError( - "Saving a region of interest (roi) is not implemented for tensors with a batch dimension." + "Saving a region of interest (roi) with a batch dimension to non-NPY formats is not supported." ) - if batch_len == 1: - # avoid saving tensors with a singleton batch dimension - tensor = tensor.squeeze(v0_5.BATCH_AXIS_ID) - if roi_and_tensor_shape is not None: - roi, tensor_shape = roi_and_tensor_shape - roi = {k: v for k, v in roi.items() if k != v0_5.BATCH_AXIS_ID} - tensor_shape = { - k: v for k, v in tensor_shape.items() if k != v0_5.BATCH_AXIS_ID - } - roi_and_tensor_shape = (roi, tensor_shape) + if tensor.batch_multi_index is not None: + tensor = tensor.unstack_batch_multi_index(errors="raise") + + if BATCH_AXIS_ID in tensor.dims: + tensor = tensor.squeeze(BATCH_AXIS_ID) if io_lib is None: try_io_libs = _select_io_libs_based_on_extension(extensions) @@ -1181,10 +1172,13 @@ def save_sample( else: sample_id = sample_block.sample_id roi_and_tensor_shape = ( - sample_block.blocks[m].inner_slice, + sample_block.blocks[m].inner_slice_batch_unstacked + or sample_block.blocks[m].inner_slice, sample_block.sample_shape[m], ) - t = sample_block.blocks[m].inner_data + t = sample_block.blocks[m].inner_data.unstack_batch_multi_index( + errors="ignore" + ) p_formatted = Path( str(p).format(sample_id=sample_id, member_id=m, input_id=m, output_id=m) diff --git a/src/bioimageio/core/sample.py b/src/bioimageio/core/sample.py index 32609beb..b6b53eba 100644 --- a/src/bioimageio/core/sample.py +++ b/src/bioimageio/core/sample.py @@ -22,7 +22,7 @@ from typing_extensions import Self, assert_never from ._common_annotations import PerMemberAnno -from .axis import AxisId, PerAxis +from .axis import BATCH_AXIS_ID, AxisId, PerAxis from .block import Block from .block_meta import ( BlockMeta, @@ -100,7 +100,7 @@ def batch_multi_index(self) -> pd.MultiIndex | None: return None for tensor in self.members.values(): - idx = tensor.data.indexes.get(AxisId("batch")) # pyright: ignore[reportUnknownVariableType] + idx = tensor.batch_multi_index if isinstance(idx, pd.MultiIndex): return idx @@ -133,6 +133,24 @@ def set_block(self, block: SampleBlock) -> None: def shape(self) -> PerMember[PerAxis[int]]: return {tid: t.sizes for tid, t in self.members.items()} + @property + def shape_batch_unstacked(self) -> PerMember[PerAxis[int]]: + """Return the sample shape with unstacked batch axes (if any).""" + if (idx := self.batch_multi_index) is None: + return self.shape + + ret = {m: dict(s) for m, s in self.shape.items()} + unstacked_axes = tuple( + BATCH_AXIS_ID if name == "original_batch" else AxisId(name) + for name in idx.names + ) + for m in self.members: + if BATCH_AXIS_ID in ret[m]: + for a, s in zip(unstacked_axes, idx.levshape): + ret[m][a] = s + + return ret + def as_arrays(self) -> dict[MemberId, NDArray[Any]]: """Return sample as dictionary of arrays.""" return {m: t.to_numpy() for m, t in self.members.items()} @@ -162,16 +180,40 @@ def split_into_blocks( def as_single_block(self, halo: PerMember[PerAxis[Halo]] | None = None): if halo is None: halo = {} + + inner_slice = { + m: {a: SliceInfo(0, s) for a, s in data.tagged_shape.items()} + for m, data in self.members.items() + } + inner_slice_batch_unstacked = { + m: {k: v for k, v in inner_slice[m].items()} for m in self.members + } + + for m, t in self.members.items(): + if isinstance( + idx := t.data.indexes.get(BATCH_AXIS_ID), # pyright: ignore[reportUnknownVariableType] + pd.MultiIndex, + ): + # add slice for each unstacked axis in the multi index + b_slice = inner_slice[m][BATCH_AXIS_ID] + for a, s, c in zip(idx.names, idx.levshape, idx.codes): + axis = BATCH_AXIS_ID if a == "original_batch" else AxisId(a) + # sample_shape_batch_unstacked[m][axis] = s + codes = c[b_slice.start : b_slice.stop] + s = SliceInfo(int(codes.min()), int(codes.max()) + 1) + inner_slice_batch_unstacked[m][axis] = s + return SampleBlockWithOrigin( sample_shape=self.shape, + sample_shape_batch_unstacked=self.shape_batch_unstacked, sample_id=self.id, blocks={ m: Block( sample_shape=self.shape[m], + sample_shape_batch_unstacked=self.shape_batch_unstacked[m], data=data, - inner_slice={ - a: SliceInfo(0, s) for a, s in data.tagged_shape.items() - }, + inner_slice=inner_slice[m], + inner_slice_batch_unstacked=inner_slice_batch_unstacked[m], halo=halo.get(m, {}), block_index=0, blocks_in_sample=1, @@ -421,6 +463,9 @@ class SampleBlockBase(Generic[BlockT]): sample_shape: PerMemberAnno[PerAxis[int]] """the sample shape this block represents a part of""" + sample_shape_batch_unstacked: PerMemberAnno[PerAxis[int]] + """the sample shape this block represents a part of, with unstacked batch axes (if any)""" + sample_id: SampleId """identifier for the sample within its dataset""" @@ -505,11 +550,47 @@ def get_member_halo(m: MemberId, round: Callable[[float], int]): } for m in new_axes } + # sample_shape_batch_unstacked/inner_slice_batch_unstacked undergoes the same transform as inner_slice, but keeps the unstacked batch slices of the original block + sample_shape_batch_unstacked = {m: dict(s) for m, s in sample_shape.items()} + inner_slice_batch_unstacked = {m: dict(s) for m, s in inner_slice.items()} + for m in list(inner_slice_batch_unstacked): + sh = sample_shape_batch_unstacked[m] + for a, sh_u in self.sample_shape_batch_unstacked[m].items(): + if a == BATCH_AXIS_ID or (a not in sh): + sh[a] = sh_u # keep unstacked axis size sh_u for stacked axis a + elif a not in self.sample_shape[m]: + raise ValueError( + f"Stacked axis {a} was overwritten by an axis transform." + + " This happens for example when transforming a tensor byx into zyx," + + " but actually starting with a tensor bzyx that was stacked to byx" + + " (to apply a yx->zyx model)." + + " The resulting bzyx output tensor cannot be unstacked due to the colliding z dim." + ) + + sl = inner_slice_batch_unstacked[m] + for a, sl_u in ( + self.blocks[m].inner_slice_batch_unstacked or {} + if m in self.blocks + else {} + ).items(): + if a == BATCH_AXIS_ID or (a not in sl): + sl[a] = sl_u # keep unstacked axis slice sl_u for stacked axis a + elif a not in self.blocks[m].inner_slice: + raise ValueError( + f"Stacked axis {a} was overwritten by an axis transform." + + " This happens for example when transforming a tensor byx into zyx," + + " but actually starting with a tensor bzyx that was stacked to byx" + + " (to apply a yx->zyx model)." + + " The resulting bzyx output tensor cannot be unstacked due to the colliding z dim." + ) + return self.__class__( blocks={ m: BlockMeta( sample_shape=sample_shape[m], + sample_shape_batch_unstacked=sample_shape_batch_unstacked[m], inner_slice=inner_slice[m], + inner_slice_batch_unstacked=inner_slice_batch_unstacked[m], halo=halo[m], block_index=self.block_index, blocks_in_sample=self.blocks_in_sample, @@ -517,6 +598,7 @@ def get_member_halo(m: MemberId, round: Callable[[float], int]): for m in new_axes }, sample_shape=sample_shape, + sample_shape_batch_unstacked=sample_shape_batch_unstacked, sample_id=self.sample_id, block_index=self.block_index, blocks_in_sample=self.blocks_in_sample, @@ -531,6 +613,13 @@ def with_data(self, data: PerMember[Tensor], *, stat: Stat) -> SampleBlock: } for m, member_shape in self.sample_shape.items() }, + sample_shape_batch_unstacked={ + m: { + a: data[m].tagged_shape[a] if s == -1 else s + for a, s in member_shape.items() + } + for m, member_shape in self.sample_shape_batch_unstacked.items() + }, sample_id=self.sample_id, blocks={ m: Block.from_meta(b, data=data[m]) for m, b in self.blocks.items() @@ -563,6 +652,7 @@ def get_transformed_meta( sample_id=self.sample_id, blocks=dict(self.blocks), sample_shape=self.sample_shape, + sample_shape_batch_unstacked=self.sample_shape_batch_unstacked, block_index=self.block_index, blocks_in_sample=self.blocks_in_sample, ).get_transformed(new_axes) @@ -573,6 +663,7 @@ def from_meta( ) -> Self: return cls( sample_shape=meta.sample_shape, + sample_shape_batch_unstacked=meta.sample_shape_batch_unstacked, sample_id=meta.sample_id, blocks={ m: Block.from_meta(b, data=data[m]) for m, b in meta.blocks.items() @@ -587,6 +678,7 @@ def get_meta(self) -> SampleBlockMeta: sample_id=self.sample_id, blocks={m: b.get_meta() for m, b in self.blocks.items()}, sample_shape=self.sample_shape, + sample_shape_batch_unstacked=self.sample_shape_batch_unstacked, block_index=self.block_index, blocks_in_sample=self.blocks_in_sample, ) @@ -632,6 +724,9 @@ def sample_block_meta_generator( ): for member_blocks in blocks: cons = _ConsolidatedMemberBlocks(member_blocks) + for m, b in member_blocks.items(): + assert b.sample_shape == sample_shape[m] + yield SampleBlockMeta( blocks=dict(member_blocks), sample_shape=sample_shape, @@ -661,6 +756,7 @@ def sample_block_generator( for m in origin.members }, sample_shape=origin.shape, + sample_shape_batch_unstacked=origin.shape_batch_unstacked, origin=origin, stat=origin.stat, sample_id=origin.id, diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index 41abc181..4ba3b683 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -24,7 +24,7 @@ from bioimageio.spec.model.v0_5 import SpaceUnit, TimeUnit from ._magic_tensor_ops import MagicTensorOpsMixin -from .axis import AxisId, AxisInfo, AxisLike, PerAxis +from .axis import BATCH_AXIS_ID, AxisId, AxisInfo, AxisLike, PerAxis from .common import ( CropWhere, DTypeStr, @@ -509,12 +509,25 @@ def pad( ) -> Self: pad_width = {a: PadWidth.create(p) for a, p in pad_width.items()} mode_name, constant_value = _resolve_pad_mode(mode) - return self.__class__.from_xarray( + + padded_tensor = self.__class__.from_xarray( self._data.pad( pad_width=pad_width, mode=mode_name, constant_values=constant_value ) ) + if ( + BATCH_AXIS_ID in self.dims + # maybe future xarray versions will keep a MultiIndex when padding, but currently they don't + and padded_tensor.batch_multi_index is not None + and pad_width.get(BATCH_AXIS_ID, 0) in (0, (0, 0), PadWidth(0, 0)) + and (idx := self.batch_multi_index) is not None + ): + # add multiindex removed by padding when batch axis is not padded + padded_tensor = padded_tensor.assign_batch_multi_index(idx) + + return padded_tensor + def pad_to( self, sizes: PerAxis[int], @@ -623,19 +636,31 @@ def sum(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: """Reduce this Tensor's data by applying sum along some dimension(s).""" return self.__class__.from_xarray(self._data.sum(dim=dim)) + @property + def batch_multi_index(self) -> pd.MultiIndex | None: + """Return the batch multi-index if available, otherwise None.""" + if BATCH_AXIS_ID not in self.dims: + return None + + idx = self._data.indexes.get(BATCH_AXIS_ID) # pyright: ignore[reportUnknownVariableType] + if not isinstance(idx, pd.MultiIndex): + return None + + return idx + def assign_batch_multi_index(self, multi_index: pd.MultiIndex) -> Self: """Set the batch multi-index for this tensor. Args: multi_index: The multi-index to set. """ - if AxisId("batch") not in self.dims: + if BATCH_AXIS_ID not in self.dims: raise ValueError( "Cannot set batch multi-index on a tensor without a 'batch' axis." ) return self.__class__.from_xarray( - self._data.assign_coords({AxisId("batch"): multi_index}) + self._data.assign_coords({BATCH_AXIS_ID: multi_index}) ) def unstack_batch_multi_index( @@ -646,7 +671,7 @@ def unstack_batch_multi_index( Returns: A new tensor with the batch multi-index unstacked into separate axes. """ - if AxisId("batch") not in self.dims: + if BATCH_AXIS_ID not in self.dims: error_msg = ( "Cannot unstack batch multi-index on a tensor without a 'batch' axis." ) @@ -660,7 +685,7 @@ def unstack_batch_multi_index( else: assert_never(errors) - if not isinstance(self._data.indexes.get(AxisId("batch")), pd.MultiIndex): + if not isinstance(self._data.indexes.get(BATCH_AXIS_ID), pd.MultiIndex): error_msg = "Cannot unstack batch multi-index on a tensor whose 'batch' axis does not have a MultiIndex." if errors == "raise": raise ValueError(error_msg) @@ -673,7 +698,7 @@ def unstack_batch_multi_index( assert_never(errors) old_dims = self.dims - array = self._data.unstack(AxisId("batch")) + array = self._data.unstack(BATCH_AXIS_ID) assert isinstance(array, xr.DataArray) added_dims = [AxisId(d) for d in array.dims if d not in self._data.dims] @@ -682,14 +707,14 @@ def unstack_batch_multi_index( for d in old_dims: if d in array.dims: new_dims.append(d) - elif d == AxisId("batch"): + elif d == BATCH_AXIS_ID: new_dims.extend(added_dims) else: raise ValueError(f"Expected axis {d} not found in unstacked array.") array = array.transpose(*new_dims) if AxisId("original_batch") in array.dims: - array = array.rename({AxisId("original_batch"): AxisId("batch")}) + array = array.rename({AxisId("original_batch"): BATCH_AXIS_ID}) assert isinstance(array, xr.DataArray) return self.__class__.from_xarray(array) @@ -731,14 +756,14 @@ def transpose( unstack_error = None if unhandled_missing_dims and missing_dims in ("unstack", "unstack_or_expand"): - lets_unstack = AxisId("batch") in array.dims + lets_unstack = BATCH_AXIS_ID in array.dims if not lets_unstack: unstack_error = f"Missing dimensions {unhandled_missing_dims} found, but 'batch' axis is not in the tensor. Cannot unstack missing dimensions from batch." if missing_dims == "unstack": raise ValueError(unstack_error) if lets_unstack and not isinstance( - array.indexes.get(AxisId("batch")), pd.MultiIndex + array.indexes.get(BATCH_AXIS_ID), pd.MultiIndex ): lets_unstack = False unstack_error = f"Missing dimensions {unhandled_missing_dims} found, but 'batch' axis does not have a MultiIndex. Cannot unstack missing dimensions from non-multi-index batch." @@ -748,11 +773,11 @@ def transpose( lets_unstack = False if lets_unstack: - array: xr.DataArray = array.unstack(AxisId("batch")) + array: xr.DataArray = array.unstack(BATCH_AXIS_ID) if AxisId("original_batch") in array.dims: - if AxisId("batch") in axes: - array = array.rename({AxisId("original_batch"): AxisId("batch")}) + if BATCH_AXIS_ID in axes: + array = array.rename({AxisId("original_batch"): BATCH_AXIS_ID}) else: array = array.squeeze(AxisId("original_batch")) @@ -785,17 +810,17 @@ def transpose( ) if unhandled_extra_dims and extra_dims in ("stack", "squeeze_or_stack"): - if AxisId("batch") not in axes: + if BATCH_AXIS_ID not in axes: raise ValueError( f"Extra dimensions {unhandled_extra_dims} found but 'batch' axis is not in the desired axes {axes}." + " Cannot stack extra dimensions to batch." ) - if AxisId("batch") in array.dims: - array = array.rename({AxisId("batch"): AxisId("original_batch")}) + if BATCH_AXIS_ID in array.dims: + array = array.rename({BATCH_AXIS_ID: AxisId("original_batch")}) unhandled_extra_dims.insert(0, AxisId("original_batch")) - array = array.stack({AxisId("batch"): unhandled_extra_dims}) + array = array.stack({BATCH_AXIS_ID: unhandled_extra_dims}) unhandled_extra_dims = [] if unhandled_extra_dims: From 409fc9fd1b437f002b151a27f8d7e98a6569723f Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 1 Sep 2026 16:25:43 +0200 Subject: [PATCH 20/85] WIP handle multi-index batch axis --- src/bioimageio/core/_multi_index.py | 3 + src/bioimageio/core/_op_base.py | 38 +-- .../core/_restore_batch_multi_index.py | 5 +- src/bioimageio/core/block.py | 90 +++---- src/bioimageio/core/block_meta.py | 119 ++++++--- src/bioimageio/core/digest_spec.py | 127 ++++++++- src/bioimageio/core/io.py | 205 +++++++++++--- src/bioimageio/core/sample.py | 252 ++++++++++-------- src/bioimageio/core/tensor.py | 85 ++++-- tests/test_prediction.py | 5 +- tests/test_tensor.py | 5 +- 11 files changed, 631 insertions(+), 303 deletions(-) create mode 100644 src/bioimageio/core/_multi_index.py diff --git a/src/bioimageio/core/_multi_index.py b/src/bioimageio/core/_multi_index.py new file mode 100644 index 00000000..a4580a40 --- /dev/null +++ b/src/bioimageio/core/_multi_index.py @@ -0,0 +1,3 @@ +import pandas as pd + +MultiIndex = pd.MultiIndex diff --git a/src/bioimageio/core/_op_base.py b/src/bioimageio/core/_op_base.py index 3eb96942..6d28e4a2 100644 --- a/src/bioimageio/core/_op_base.py +++ b/src/bioimageio/core/_op_base.py @@ -8,7 +8,7 @@ from typing_extensions import TypeVar, assert_never from ._restore_batch_multi_index import restore_batch_multi_index -from .axis import BATCH_AXIS_ID, PerAxis +from .axis import PerAxis from .block import Block from .common import MemberId from .sample import Sample, SampleBlock, SampleBlockWithOrigin @@ -72,28 +72,28 @@ def __call__(self, sample: Sample | SampleBlock) -> None: sample.members[self.output] = output_tensor elif isinstance(sample, SampleBlock): b = sample.blocks[self.input] - sample_shape_batch_unstacked = { - a: s - for a, s in sample.sample_shape_batch_unstacked[self.input].items() - if a not in sample.sample_shape[self.input] - } - sample_shape = self.get_output_shape(sample.sample_shape[self.input]) - for a, s in sample_shape.items(): - if a == BATCH_AXIS_ID: - continue - elif a in sample_shape_batch_unstacked: - raise ValueError( - f"Output axis {a} already exists in sample_shape_batch_unstacked" - ) - - sample_shape_batch_unstacked[a] = s + output_shape = self.get_output_shape(sample.sample_shape[self.input]) + if ( + b.unstacked_dim_order is None + or b.unstacked_batch_shape is None + or any(a not in b.unstacked_dim_order for a in output_shape) + or any(a not in b.unstacked_dim_order for a in b.unstacked_batch_shape) + ): + unstacked_dims = None + else: + unstacked_dims = [ + d + for d in b.unstacked_dim_order + if d in output_shape or d in b.unstacked_batch_shape + ] sample.blocks[self.output] = Block( - sample_shape=sample_shape, - sample_shape_batch_unstacked=sample_shape_batch_unstacked, + sample_shape=output_shape, + unstacked_batch_shape=sample.unstacked_batch_shape, + unstacked_dim_order=unstacked_dims, data=output_tensor, inner_slice=b.inner_slice, - inner_slice_batch_unstacked=b.inner_slice_batch_unstacked, + unstacked_batch_slice=b.unstacked_batch_slice, halo=b.halo, block_index=b.block_index, blocks_in_sample=b.blocks_in_sample, diff --git a/src/bioimageio/core/_restore_batch_multi_index.py b/src/bioimageio/core/_restore_batch_multi_index.py index d1e3380c..3d05791f 100644 --- a/src/bioimageio/core/_restore_batch_multi_index.py +++ b/src/bioimageio/core/_restore_batch_multi_index.py @@ -1,7 +1,6 @@ from __future__ import annotations -import pandas as pd - +from ._multi_index import MultiIndex from .axis import AxisId from .common import PerMember from .tensor import Tensor @@ -16,7 +15,7 @@ def restore_batch_multi_index( continue idx = tensor.data.indexes.get(AxisId("batch")) # pyright: ignore[reportUnknownVariableType] - if isinstance(idx, pd.MultiIndex): + if isinstance(idx, MultiIndex): outputs = { k: v.assign_batch_multi_index(idx) if v is not None and AxisId("batch") in v.dims diff --git a/src/bioimageio/core/block.py b/src/bioimageio/core/block.py index a3b65ada..5ce85c01 100644 --- a/src/bioimageio/core/block.py +++ b/src/bioimageio/core/block.py @@ -6,16 +6,15 @@ Any, ) -import pandas as pd from typing_extensions import Self +from ._multi_index import MultiIndex from .axis import BATCH_AXIS_ID, AxisId, PerAxis from .block_meta import BlockMeta, LinearAxisTransform, split_shape_into_blocks from .common import ( Halo, HaloLike, PadMode, - SliceInfo, TotalNumberOfBlocks, ) from .tensor import Tensor @@ -51,36 +50,27 @@ def from_sample_member( *, pad_mode: PadMode, ) -> Self: - if ( - block.inner_slice_batch_unstacked == block.inner_slice - and block.sample_shape == block.sample_shape_batch_unstacked - and isinstance( - idx := sample_member.data.indexes.get(BATCH_AXIS_ID), # pyright: ignore[reportUnknownVariableType] - pd.MultiIndex, - ) - and block.inner_slice[BATCH_AXIS_ID] == block.outer_slice[BATCH_AXIS_ID] - ): - # if sample_member has a multi index for the batch axis, we keep track of the unstacked axes of this block - # start with inner_slice without the batch axis, and add the unstacked slice for each axis in the multi index - sample_shape_batch_unstacked = dict(block.sample_shape) - inner_slice_batch_unstacked = dict(block.inner_slice) - b_slice = block.inner_slice[BATCH_AXIS_ID] - for a, s, c in zip(idx.names, idx.levshape, idx.codes): - axis = BATCH_AXIS_ID if a == "original_batch" else AxisId(a) - sample_shape_batch_unstacked[axis] = s - codes = c[b_slice.start : b_slice.stop] - si = SliceInfo(int(codes.min()), int(codes.max()) + 1) - inner_slice_batch_unstacked[axis] = si - else: - inner_slice_batch_unstacked = block.inner_slice_batch_unstacked - sample_shape_batch_unstacked = block.sample_shape_batch_unstacked + block_data = sample_member[block.outer_slice] + padded_block_data = block_data.pad(block.padding, pad_mode) + + if block.unstacked_batch_slice is not None: + if not isinstance( + idx := block_data.data.indexes.get(BATCH_AXIS_ID), # pyright: ignore[reportUnknownVariableType] + MultiIndex, + ): + raise ValueError("Expected a MultiIndex for the batch axis") + + padded_block_data = padded_block_data.assign_batch_multi_index(idx) + assert block.unstacked_batch_shape is not None + assert block.inner_slice[BATCH_AXIS_ID] == block.outer_slice[BATCH_AXIS_ID] return cls( - data=sample_member[block.outer_slice].pad(block.padding, pad_mode), + data=padded_block_data, sample_shape=sample_member.tagged_shape, - sample_shape_batch_unstacked=sample_shape_batch_unstacked, + unstacked_batch_shape=block.unstacked_batch_shape, + unstacked_dim_order=block.unstacked_dim_order, inner_slice=block.inner_slice, - inner_slice_batch_unstacked=inner_slice_batch_unstacked, + unstacked_batch_slice=block.unstacked_batch_slice, halo=block.halo, block_index=block.block_index, blocks_in_sample=block.blocks_in_sample, @@ -91,26 +81,12 @@ def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: @classmethod def from_meta(cls, meta: BlockMeta, data: Tensor) -> Self: - sample_shape = { - k: data.tagged_shape[k] if v == -1 else v - for k, v in meta.sample_shape.items() - } - sample_shape_batch_unstacked = { - k: data.tagged_shape[k] if v == -1 else v - for k, v in meta.sample_shape_batch_unstacked.items() - } return cls( - sample_shape=sample_shape, - sample_shape_batch_unstacked=sample_shape_batch_unstacked, - inner_slice={ - k: ( - SliceInfo(start=v.start, stop=data.tagged_shape[k]) - if v.stop == -1 - else v - ) - for k, v in meta.inner_slice.items() - }, - inner_slice_batch_unstacked=meta.inner_slice_batch_unstacked, + sample_shape=meta.sample_shape, + unstacked_batch_shape=meta.unstacked_batch_shape, + unstacked_dim_order=meta.unstacked_dim_order, + inner_slice=meta.inner_slice, + unstacked_batch_slice=meta.unstacked_batch_slice, halo=meta.halo, block_index=meta.block_index, blocks_in_sample=meta.blocks_in_sample, @@ -120,9 +96,10 @@ def from_meta(cls, meta: BlockMeta, data: Tensor) -> Self: def get_meta(self) -> BlockMeta: return BlockMeta( sample_shape=self.sample_shape, - sample_shape_batch_unstacked=self.sample_shape_batch_unstacked, + unstacked_batch_shape=self.unstacked_batch_shape, + unstacked_dim_order=self.unstacked_dim_order, inner_slice=self.inner_slice, - inner_slice_batch_unstacked=self.inner_slice_batch_unstacked, + unstacked_batch_slice=self.unstacked_batch_slice, halo=self.halo, block_index=self.block_index, blocks_in_sample=self.blocks_in_sample, @@ -138,8 +115,21 @@ def split_tensor_into_blocks( pad_mode: PadMode, ) -> tuple[TotalNumberOfBlocks, Generator[Block, Any, None]]: """divide a sample tensor into tensor blocks.""" + if (idx := tensor.batch_multi_index) is None: + unstacked_batch_shape = None + else: + unstacked_batch_shape = { + BATCH_AXIS_ID if a == "original_batch" else AxisId(a): s + for a, s in zip(idx.names, idx.levshape) + } + n_blocks, block_gen = split_shape_into_blocks( - tensor.tagged_shape, block_shape=block_shape, halo=halo, stride=stride + tensor.tagged_shape, + unstacked_dim_order=None, + unstacked_batch_shape=unstacked_batch_shape, + block_shape=block_shape, + halo=halo, + stride=stride, ) return n_blocks, _block_generator(tensor, block_gen, pad_mode=pad_mode) diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py index 7a83b482..178a2e65 100644 --- a/src/bioimageio/core/block_meta.py +++ b/src/bioimageio/core/block_meta.py @@ -1,7 +1,7 @@ from __future__ import annotations import itertools -from collections.abc import Collection, Generator, Iterable +from collections.abc import Collection, Generator, Iterable, Sequence from dataclasses import dataclass from functools import cached_property from math import floor, prod @@ -16,6 +16,7 @@ from typing_extensions import Self from ._axis_annotations import PerAxisAnno +from ._multi_index import MultiIndex from .axis import BATCH_AXIS_ID, AxisId, PerAxis from .common import ( BlockIndex, @@ -76,14 +77,17 @@ class BlockMeta: sample_shape: PerAxisAnno[int] """the axis sizes of the whole (unblocked) sample""" - sample_shape_batch_unstacked: PerAxisAnno[int] - """the axis sizes of the whole (unblocked) sample, with unstacked batch axes (if any)""" + unstacked_batch_shape: PerAxisAnno[int] | None + """the axis sizes of the stacked axis (that make up the batch axis)""" + + unstacked_dim_order: Sequence[AxisId] | None + """The order of the fully unstacked tensor, if the sample has a multi-index""" inner_slice: PerAxisAnno[SliceInfo] """inner region (without halo) wrt the sample""" - inner_slice_batch_unstacked: PerAxisAnno[SliceInfo] - """inner slice (without halo) wrt the sample unstacked along the batch axis (if present)""" + unstacked_batch_slice: PerAxisAnno[SliceInfo] | None + """inner/outer slice along the unstacked batch dimension wrt the (unstacked) sample""" halo: PerAxisAnno[Halo] """halo enlarging the inner region to the block's sizes""" @@ -213,39 +217,40 @@ def __post_init__(self): def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: assert BATCH_AXIS_ID not in new_axes, "cannot transform the batch axis" - unstacked_dims = { - k for k in self.inner_slice_batch_unstacked if k not in self.inner_slice + + sample_shape = { + a: ( + trf + if isinstance(trf, int) + else trf.compute(self.sample_shape[trf.axis]) + ) + for a, trf in new_axes.items() + } + assert sample_shape.get(BATCH_AXIS_ID) == self.sample_shape.get( + BATCH_AXIS_ID + ), "block transform must not change the batch axis size" + + inner_slice = { + a: ( + SliceInfo(0, trf) + if isinstance(trf, int) + else SliceInfo( + trf.compute(self.inner_slice[trf.axis].start), + trf.compute(self.inner_slice[trf.axis].stop), + ) + ) + for a, trf in new_axes.items() } + assert inner_slice.get(BATCH_AXIS_ID) == self.inner_slice.get(BATCH_AXIS_ID), ( + "block transform must not change the batch axis slice" + ) return self.__class__( - sample_shape=( - sample_shape := { - a: ( - trf - if isinstance(trf, int) - else trf.compute(self.sample_shape[trf.axis]) - ) - for a, trf in new_axes.items() - } - ), - sample_shape_batch_unstacked=sample_shape, - inner_slice=( - transformed_inner_slice := { - a: ( - SliceInfo(0, trf) - if isinstance(trf, int) - else SliceInfo( - trf.compute(self.inner_slice[trf.axis].start), - trf.compute(self.inner_slice[trf.axis].stop), - ) - ) - for a, trf in new_axes.items() - } - ), - inner_slice_batch_unstacked={ - **{k: self.inner_slice_batch_unstacked[k] for k in unstacked_dims}, - **transformed_inner_slice, - }, + sample_shape=sample_shape, + unstacked_batch_shape=self.unstacked_batch_shape, + unstacked_dim_order=self.unstacked_dim_order, + inner_slice=inner_slice, + unstacked_batch_slice=self.unstacked_batch_slice, halo={ a: ( Halo(0, 0) @@ -261,10 +266,12 @@ def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: def split_shape_into_blocks( shape: PerAxis[int], - shape_unstacked: PerAxis[int], + *, + unstacked_batch_shape: PerAxis[int] | None, block_shape: PerAxis[int], halo: PerAxis[HaloLike], stride: PerAxis[int] | None = None, + unstacked_dim_order: Sequence[AxisId] | None, ) -> tuple[TotalNumberOfBlocks, Generator[BlockMeta, Any, None]]: unknown_axes = [a for a in block_shape if a not in shape and block_shape[a] != 1] if unknown_axes: @@ -297,8 +304,9 @@ def split_shape_into_blocks( return n_blocks, _block_meta_generator( shape, - shape_unstacked, + unstacked_batch_shape, blocks_in_sample=n_blocks, + unstacked_dim_order=unstacked_dim_order, inner_1d_slices=inner_1d_slices, halo=halo, ) @@ -306,8 +314,9 @@ def split_shape_into_blocks( def _block_meta_generator( sample_shape: PerAxis[int], - shape_unstacked: PerAxis[int], + unstacked_batch_shape: PerAxis[int] | None, *, + unstacked_dim_order: Sequence[AxisId] | None, blocks_in_sample: int, inner_1d_slices: dict[AxisId, list[SliceInfo]], halo: PerAxis[HaloLike], @@ -315,14 +324,35 @@ def _block_meta_generator( assert all(a in sample_shape for a in halo) halo = {a: Halo.create(halo.get(a, 0)) for a in inner_1d_slices} + if unstacked_batch_shape is None: + idx: MultiIndex | None = None + else: + idx = MultiIndex.from_product( # pyright: ignore[reportUnknownVariableType] + [range(unstacked_batch_shape[a]) for a in unstacked_batch_shape], + names=list(unstacked_batch_shape), + ) + assert isinstance(idx, MultiIndex) + for i, nd_tile in enumerate(itertools.product(*inner_1d_slices.values())): - inner_slice: PerAxis[SliceInfo] = dict(zip(inner_1d_slices, nd_tile)) + inner_1d_slices[BATCH_AXIS_ID] + inner_slice = dict(zip(inner_1d_slices, nd_tile)) + if idx is None: + unstacked_batch_slice = None + else: + batch_slice = inner_slice[BATCH_AXIS_ID] + unstacked_batch_slice: dict[AxisId, SliceInfo] | None = {} + for a, c in zip(idx.names, idx.codes): + a = BATCH_AXIS_ID if a == "original_batch" else AxisId(a) + a_start = int(c[batch_slice.start]) + a_stop = int(c[batch_slice.stop - 1]) + 1 + unstacked_batch_slice[a] = SliceInfo(a_start, a_stop) yield BlockMeta( sample_shape=sample_shape, - sample_shape_batch_unstacked=shape_unstacked, + unstacked_batch_shape=unstacked_batch_shape, + unstacked_dim_order=unstacked_dim_order, inner_slice=inner_slice, - inner_slice_batch_unstacked=inner_slice, + unstacked_batch_slice=unstacked_batch_slice, halo=halo, block_index=i, blocks_in_sample=blocks_in_sample, @@ -333,6 +363,8 @@ def split_multiple_shapes_into_blocks( shapes: PerMember[PerAxis[int]], block_shapes: PerMember[PerAxis[int]], *, + unstacked_batch_shape: PerAxis[int] | None, + unstacked_dim_order: PerMember[Sequence[AxisId] | None] | None, halo: PerMember[PerAxis[HaloLike]], strides: PerMember[PerAxis[int]] | None = None, broadcast: bool = False, @@ -371,7 +403,10 @@ def split_multiple_shapes_into_blocks( for t in block_shapes: n_blocks[t], blocks[t] = split_shape_into_blocks( shape=shapes[t], - shape_unstacked= + unstacked_batch_shape=unstacked_batch_shape, + unstacked_dim_order=unstacked_dim_order[t] + if unstacked_dim_order is not None + else None, block_shape=block_shapes[t], halo=halo.get(t, {}), stride=strides.get(t), diff --git a/src/bioimageio/core/digest_spec.py b/src/bioimageio/core/digest_spec.py index b8fa2bd8..1476ebcd 100644 --- a/src/bioimageio/core/digest_spec.py +++ b/src/bioimageio/core/digest_spec.py @@ -3,7 +3,7 @@ import collections.abc import importlib.util import sys -from collections.abc import Iterable, Mapping, Sequence +from collections.abc import Collection, Iterable, Mapping, Sequence from itertools import chain from pathlib import Path from tempfile import TemporaryDirectory @@ -36,7 +36,7 @@ from .axis import AxisId, AxisInfo, AxisLike, PerAxis from .block_meta import split_multiple_shapes_into_blocks from .common import Halo, MemberId, PerMember, SampleId, TotalNumberOfBlocks -from .io import load_tensor +from .io import load_tensor_and_return_unstacked_dim_order from .sample import ( LinearSampleAxisTransform, Sample, @@ -259,6 +259,7 @@ def _get_test_sample( members={m: Tensor.from_numpy(arrays[m], dims=axes[m]) for m in info}, stat={}, id="test-sample", + unstacked_dim_order=None, ) @@ -267,6 +268,39 @@ class IO_SampleBlockMeta(NamedTuple): output: SampleBlockMeta +def get_output_unstacked_dim_order( + model: v0_5.ModelDescr, + unstacked_batch_dims: Collection[AxisId] | None, + input_unstacked_dim_order: PerMember[Sequence[AxisId]] | None, +) -> PerMember[Sequence[AxisId]] | None: + if input_unstacked_dim_order is None or unstacked_batch_dims is None: + return None + + output_dim_order: dict[MemberId, Sequence[AxisId]] = {} + for out in model.outputs: + out_axes = {ax.id for ax in out.axes} + for a in out.axes: + if ( + isinstance(a.size, v0_5.SizeReference) + and a.size.tensor_id in input_unstacked_dim_order + ): + in_order = input_unstacked_dim_order[a.size.tensor_id] + if all( + a in out_axes or a in unstacked_batch_dims for a in in_order + ) and [a for a in in_order if a in out_axes] == [ + a.id for a in out.axes if a.id in in_order + ]: + # all input axes (and their order) are preserved in the output (or stacked) + break + else: + # not all unstacked output axes could be mapped to unstacked input axes + return None + + output_dim_order[out.id] = in_order + + return output_dim_order + + def get_input_halo( model: v0_5.ModelDescr, output_halo: PerMember[PerAxis[Halo]] | None = None ): @@ -379,6 +413,9 @@ def get_block_transform( def get_io_sample_block_metas( model: v0_5.ModelDescr, input_sample_shape: PerMember[PerAxis[int]], + *, + unstacked_batch_shape: PerAxis[int] | None = None, + unstacked_dim_order_input: PerMember[Sequence[AxisId]] | None = None, ns: Mapping[tuple[MemberId, AxisId], ParameterizedSize_N], batch_size: int = 1, ) -> tuple[TotalNumberOfBlocks, Iterable[IO_SampleBlockMeta]]: @@ -386,6 +423,19 @@ def get_io_sample_block_metas( if not isinstance(model, v0_5.ModelDescr): raise TypeError(f"get_block_meta() not implemented for {type(model)}") + if ( + unstacked_batch_shape is not None + and batch_size != 1 + and ( + not_divisible := { + a for a, s in unstacked_batch_shape.items() if s % batch_size != 0 + } + ) + ): + raise ValueError( + f"unstacked_batch_shape={unstacked_batch_shape} is not divisible by batch_size={batch_size} for axes {not_divisible}" + ) + block_axis_sizes = model.get_axis_sizes(ns=ns, batch_size=batch_size) input_block_shape = { t: {aa: s for (tt, aa), s in block_axis_sizes.inputs.items() if tt == t} @@ -398,9 +448,16 @@ def get_io_sample_block_metas( for t in model.outputs } input_halo = get_input_halo(model, output_halo) + unstacked_dim_order = get_output_unstacked_dim_order( + model, unstacked_batch_shape, unstacked_dim_order_input + ) n_input_blocks, input_blocks = split_multiple_shapes_into_blocks( - input_sample_shape, input_block_shape, halo=input_halo + input_sample_shape, + input_block_shape, + unstacked_batch_shape=unstacked_batch_shape, + unstacked_dim_order=unstacked_dim_order, + halo=input_halo, ) block_transform = get_block_transform(model) return n_input_blocks, ( @@ -440,6 +497,31 @@ def get_tensor( If "unstack", any missing dimensions will be unstacked from the batch dimension. For this option a batch dimension with a multi-index must be present from previous stacking operations or assigned by `Tensor.assign_batch_multi_index()`. If "unstack_or_expand", any missing dimensions will be unstacked from the batch dimension if it has a multi-index, otherwise they will be added as singleton dimensions. """ + tensor, _unstacked_dims = get_tensor_and_unstacked_dims( + src, + descr, + extra_dims=extra_dims, + missing_dims=missing_dims, + ) + return tensor + + +def get_tensor_and_unstacked_dims( + src: TensorSource, + descr: v0_4.InputTensorDescr + | v0_5.InputTensorDescr + | v0_4.OutputTensorDescr + | v0_5.OutputTensorDescr + | Sequence[AxisInfo], + *, + extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "squeeze", + missing_dims: Literal["raise", "expand", "unstack", "unstack_or_expand"] = "raise", +): + """See `get_tensor()` for details. Returns a tuple of (tensor, unstacked_dim_order) + + unstacked_dim_order is None if the tensor has no unstacked batch dimension, + otherwise it is a list of axis IDs in the order they appear in the fully unstacked tensor. + """ if isinstance( descr, @@ -456,19 +538,19 @@ def get_tensor( if isinstance(src, Tensor): output_dims = [a.id for a in axes] - return src.transpose( + return src.transpose_and_return_unstacked_dim_order( output_dims, extra_dims=extra_dims, missing_dims=missing_dims ) elif isinstance(src, xr.DataArray): output_dims = [a.id for a in axes] - return Tensor.from_xarray(src).transpose( + return Tensor.from_xarray(src).transpose_and_return_unstacked_dim_order( axes=output_dims, extra_dims=extra_dims, missing_dims=missing_dims ) elif isinstance(src, np.ndarray): - return Tensor.from_numpy(src, dims=axes) + return Tensor.from_numpy(src, dims=axes), None else: - return load_tensor(src, axes=axes) + return load_tensor_and_return_unstacked_dim_order(src, axes=axes) def create_sample_for_model( @@ -522,16 +604,25 @@ def create_sample_for_model( }: raise ValueError(f"Missing non-optional model inputs: {missing}") - return Sample( - members={ - m: get_tensor( + members: dict[MemberId, Tensor] = {} + unstacked_dim_order: dict[MemberId, Sequence[AxisId]] | None = {} + for m, ipt in model_inputs.items(): + if m in inputs: + tensor, unstacked_dims = get_tensor_and_unstacked_dims( inputs[m], ipt, extra_dims=extra_dims, missing_dims=missing_dims ) - for m, ipt in model_inputs.items() - if m in inputs - }, + members[m] = tensor + if unstacked_dim_order is not None: + if unstacked_dims is None: + unstacked_dim_order = None + else: + unstacked_dim_order[m] = unstacked_dims + + return Sample( + members=members, stat={} if stat is None else stat, id=sample_id, + unstacked_dim_order=unstacked_dim_order, ) @@ -561,6 +652,7 @@ def load_sample_for_model( raise ValueError(f"Got unexpected axes hints for: {unknown}") members: dict[MemberId, Tensor] = {} + unstacked_dim_order: dict[MemberId, Sequence[AxisId]] | None = {} for m, p in paths.items(): if m not in axes: axes[m] = get_axes_infos(model_inputs[m]) @@ -570,12 +662,19 @@ def load_sample_for_model( p, axes[m], ) - members[m] = load_tensor(p, axes[m]) + t, dim_order = load_tensor_and_return_unstacked_dim_order(p, axes[m]) + members[m] = t + if unstacked_dim_order is not None: + if dim_order is None: + unstacked_dim_order = None + else: + unstacked_dim_order[m] = dim_order return Sample( members=members, stat={} if stat is None else stat, id=sample_id or tuple(sorted(str(p) for p in paths.values())), + unstacked_dim_order=unstacked_dim_order, ) diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index 60091d15..45017af6 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -5,6 +5,7 @@ import warnings import zipfile from collections.abc import Iterable, Mapping, Sequence +from concurrent.futures import ThreadPoolExecutor from contextlib import nullcontext from io import BytesIO, TextIOWrapper from itertools import chain @@ -58,7 +59,7 @@ PerAxis, single_letter_dims_if_possible, ) -from .common import PerMember, SliceInfo +from .common import MemberId, PerMember, SliceInfo from .sample import Sample, SampleBlock from .stat_measures import DatasetMeasure, MeasureValue, SampleMeasure, Stat from .tensor import Tensor @@ -102,9 +103,37 @@ def load_tensor( """Load an image tensor Args: + source: The source of the image data. + axes: The (desired) axes of the image data. io_lib: library to use for loading the image. Defaults to trying various libraries depending on file extension. + Returns: + The loaded tensor. + """ + tensor, _unstacked_dim_order = load_tensor_and_return_unstacked_dim_order( + source, axes=axes, io_lib=io_lib + ) + return tensor + + +def load_tensor_and_return_unstacked_dim_order( + source: FileDescr | PermissiveFileSource | ZipPath, + /, + axes: Sequence[AxisLike] | None = None, + io_lib: IO_Lib | None = None, +) -> tuple[Tensor, Sequence[AxisId] | None]: + """Load an image tensor + + Args: + source: The source of the image data. + axes: The (desired) axes of the image data. + io_lib: library to use for loading the image. + Defaults to trying various libraries depending on file extension. + + Returns: + A tuple of the loaded tensor and the unstacked dimension order if the initially loaded + tensor had extra dimensions that were stacked to a multi-index batch dimension. """ file_source, extensions, internal_path = _interprete_tensor_source(source) if io_lib is None: @@ -113,7 +142,9 @@ def load_tensor( exceptions: list[Exception] = [] for auto_io_lib in try_io_libs: try: - return load_tensor(source, axes=axes, io_lib=auto_io_lib) + return load_tensor_and_return_unstacked_dim_order( + source, axes=axes, io_lib=auto_io_lib + ) except Exception as e: exceptions.append(e) logger.opt(exception=e).warning( @@ -162,12 +193,15 @@ def load_tensor( else: assert_never(io_lib) - if axes is not None: - t = t.transpose( + if axes is None: + unstacked_dim_order = None + else: + t, unstacked_dim_order = t.transpose_and_return_unstacked_dim_order( axes, extra_dims="squeeze_or_stack", missing_dims="unstack_or_expand" ) - return t + logger.debug("Loaded tensor {} with {}", t.tagged_shape, io_lib) + return t, unstacked_dim_order def _select_io_libs_based_on_extension( @@ -302,13 +336,12 @@ def _load_tensor_clearscale(source: FileDescr | PermissiveFileSource) -> Tensor: "clearscale: Loading from zip files is not implemented." ) - logger.debug("loading tensor from {} with clearscale", source) + logger.debug("loading tensor from {} with clearscale+zarr-python", source) array, ms = open_zarr_multiscale_array( source.as_posix() if isinstance(source, Path) else str(source) ) image = da.from_zarr(array) axes = tuple(str(a) for a in ms.axes()) - return Tensor.from_xarray(xr.DataArray(image, dims=axes)) @@ -341,8 +374,8 @@ def _load_tensor_tifffile(source: FileDescr | PermissiveFileSource) -> Tensor: with ctxt as f: assert not isinstance(f, TextIOWrapper) - tif = tifffile.TiffFile(f, name=name) - try: + with tifffile.TiffFile(f, name=name) as tif: # pyright: ignore[reportUnknownVariableType] + assert isinstance(tif, tifffile.TiffFile) xr_data = tif.asxarray() page = tif.pages[0] if isinstance(page, tifffile.TiffFrame): @@ -395,9 +428,6 @@ def _load_tensor_tifffile(source: FileDescr | PermissiveFileSource) -> Tensor: if ome_metadata is None: ome_metadata = OmeMetadata() - finally: - tif.close() - t = Tensor.from_xarray(xr_data) # set physical scale @@ -486,9 +516,22 @@ def _save_tensor_impl( if (BATCH_AXIS_ID in tensor.dims) and (not extensions or extensions[-1] != ".npy"): if roi_and_tensor_shape is not None: roi, tensor_shape = roi_and_tensor_shape + if tensor_shape.get(BATCH_AXIS_ID, 0) == 1: + # remove singleton batch dimension from tensor shape, roi and the given tensor if present + tensor_shape = { + k: v for k, v in tensor_shape.items() if k != BATCH_AXIS_ID + } + if BATCH_AXIS_ID in tensor.dims: + tensor = tensor.squeeze(BATCH_AXIS_ID) + if BATCH_AXIS_ID in roi: + assert ( + roi[BATCH_AXIS_ID].start == 0 and roi[BATCH_AXIS_ID].stop == 1 + ), roi[BATCH_AXIS_ID] + roi = {k: v for k, v in roi.items() if k != BATCH_AXIS_ID} + if BATCH_AXIS_ID in roi or BATCH_AXIS_ID in tensor_shape: raise NotImplementedError( - "Saving a region of interest (roi) with a batch dimension to non-NPY formats is not supported." + "Saving a region of interest (roi) with a non-singleton batch dimension to non-NPY formats is not supported." ) if tensor.batch_multi_index is not None: @@ -739,10 +782,35 @@ def _save_tensor_clearscale( import dask.array import zarr - logger.debug("Saving tensor to {} with clearscale", path) + # we parallelize at a higher level, so here we write single-threaded. + _ = zarr.config.set({"threading.max_workers": 1}) + + single_letter_dims = single_letter_dims_if_possible(tensor.dims) + + # prefer canonical order for standard volumes + canonical_axes = "tczyx" + if all(d in canonical_axes for d in single_letter_dims): + canonical_single_letter_dims = tuple( + a for a in canonical_axes if a in single_letter_dims + ) + canonical_order = tuple( + tensor.dims[single_letter_dims.index(a)] + for a in canonical_single_letter_dims + ) + if tensor.dims != canonical_order: + logger.debug( + "Transposing tensor from {} to canonical order {} for saving with clearscale+zarr-python", + tensor.dims, + canonical_order, + ) + tensor = tensor.transpose( + canonical_order, missing_dims="raise", extra_dims="raise" + ) + single_letter_dims = canonical_single_letter_dims if roi_and_tensor_shape is None: roi = (slice(None),) * len(tensor.dims) + roi_map = None tensor_shape = tensor.tagged_shape tensor_shape_tuple = tensor.shape else: @@ -753,11 +821,23 @@ def _save_tensor_clearscale( ) tensor_shape_tuple = tuple(tensor_shape.get(d, 1) for d in tensor.dims) + logger.debug( + "Saving tensor {} to {} with clearscale+zarr-python", + tensor.tagged_shape + if roi_map is None + else [ + f"{d}[:{s}]" + if (r := roi_map.get(d)) is None + else f"{d}[{r.start}:{r.stop}]" + for d, s in tensor.tagged_shape.items() + ], + path, + ) + ZARR_FORMAT: int = int(os.getenv("ZARR_FORMAT", "3")) SCALE_KEY = "s0" if internal_path is None else str(internal_path) - # Prefer single dimension names for compatibility with bioio - dim_map = dict(zip(tensor.dims, single_letter_dims_if_possible(tensor.dims))) + dim_map = dict(zip(tensor.dims, single_letter_dims)) multiscale = clearscale.Multiscale( { SCALE_KEY: clearscale.Scale( @@ -791,7 +871,7 @@ def _save_tensor_clearscale( zarr_group = zarr.open_group( str(path), - mode="w", + mode="a", zarr_format=ZARR_FORMAT, attributes=zarr_attrs, ) @@ -802,11 +882,10 @@ def _save_tensor_clearscale( dimension_names=None if ZARR_FORMAT == 2 else tuple(dim_map.values()), shards=None if ZARR_FORMAT == 2 else "auto", ) - zarr_array = zarr_group.create_array( + zarr_array = zarr_group.require_array( **create_array_kwargs, shape=[tensor_shape[d] for d in tensor.dims], dtype=tensor.dtype, - write_data=True, ) xr_array = tensor.data if dm := {k: v for k, v in dim_map.items() if k != v}: @@ -1134,7 +1213,10 @@ def _get_file_source_extension_and_internal_path( def save_sample( - path: Path | str | PerMember[Path | str], sample: Sample | Iterable[SampleBlock] + path: Path | str | PerMember[Path | str], + sample: Sample | Iterable[SampleBlock], + *, + max_workers: int | None = None, ) -> None: """Save a **sample** to a **path** pattern or all sample members in the **path** mapping. @@ -1162,30 +1244,73 @@ def save_sample( f"path {path} must contain '{{member_id}}' for sample with multiple members {list(first_block.members)}." ) - io_lib = None - for sample_block in chain([first_block], remaining_blocks): - for m, p in path.items(): - if isinstance(sample_block, Sample): - sample_id = sample_block.id - t = sample_block.members[m] - roi_and_tensor_shape = None - else: - sample_id = sample_block.sample_id - roi_and_tensor_shape = ( - sample_block.blocks[m].inner_slice_batch_unstacked - or sample_block.blocks[m].inner_slice, - sample_block.sample_shape[m], + def save_block_member( + sample_block: Sample | SampleBlock, + m: MemberId, + p: Path | str, + io_lib: IO_Lib | None = None, + ) -> IO_Lib: + if isinstance(sample_block, Sample): + sample_id = sample_block.id + t = sample_block.members[m] + roi_and_tensor_shape = None + else: + sample_id = sample_block.sample_id + roi = dict(sample_block.blocks[m].inner_slice) + t = sample_block.blocks[m].inner_data + if ( + unstacked_batch_slice := sample_block.blocks[m].unstacked_batch_slice + ) is not None: + _ = roi.pop(BATCH_AXIS_ID, None) + assert not any(d in unstacked_batch_slice for d in roi), ( + unstacked_batch_slice, + roi, + ) + roi = {**unstacked_batch_slice, **roi} + t = t.unstack_batch_multi_index(errors="raise") + assert t.dims == tuple(roi.keys()), (t.dims, tuple(roi.keys())) + assert t.tagged_shape == {a: s.stop - s.start for a, s in roi.items()} + + tensor_shape = dict(sample_block.sample_shape[m]) + if ( + unstacked_batch_shape := sample_block.unstacked_batch_shape + ) is not None: + _ = tensor_shape.pop(BATCH_AXIS_ID, None) + assert not any(d in unstacked_batch_shape for d in tensor_shape), ( + unstacked_batch_shape, + tensor_shape, ) - t = sample_block.blocks[m].inner_data.unstack_batch_multi_index( - errors="ignore" + tensor_shape = {**unstacked_batch_shape, **tensor_shape} + assert t.dims == tuple(tensor_shape.keys()), ( + t.dims, + tuple(tensor_shape.keys()), ) - p_formatted = Path( - str(p).format(sample_id=sample_id, member_id=m, input_id=m, output_id=m) - ) - io_lib = _save_tensor_impl( - p_formatted, t, roi_and_tensor_shape=roi_and_tensor_shape, io_lib=io_lib + assert ( + unstacked_batch_shape is None + and unstacked_batch_slice is None + or ( + unstacked_batch_shape is not None + and unstacked_batch_slice is not None + ) ) + roi_and_tensor_shape = (roi, tensor_shape) + + p_formatted = Path( + str(p).format(sample_id=sample_id, member_id=m, input_id=m, output_id=m) + ) + return _save_tensor_impl( + p_formatted, t, roi_and_tensor_shape=roi_and_tensor_shape, io_lib=io_lib + ) + + io_lib = None + with ThreadPoolExecutor(max_workers=max_workers) as executor: + for sample_block in chain([first_block], remaining_blocks): + for m, p in path.items(): + if io_lib is None: + io_lib = save_block_member(sample_block, m, p, io_lib) + else: + _ = executor.submit(save_block_member, sample_block, m, p, io_lib) class _StatEntry(BaseModel, frozen=True, arbitrary_types_allowed=True): diff --git a/src/bioimageio/core/sample.py b/src/bioimageio/core/sample.py index b6b53eba..9b199422 100644 --- a/src/bioimageio/core/sample.py +++ b/src/bioimageio/core/sample.py @@ -14,7 +14,6 @@ ) import numpy as np -import pandas as pd import pydantic import xarray as xr from loguru import logger @@ -22,6 +21,7 @@ from typing_extensions import Self, assert_never from ._common_annotations import PerMemberAnno +from ._multi_index import MultiIndex from .axis import BATCH_AXIS_ID, AxisId, PerAxis from .block import Block from .block_meta import ( @@ -66,6 +66,9 @@ class Sample: id: SampleId """Identifies the `Sample` within the dataset -- typically a number or a string.""" + unstacked_dim_order: PerMember[Sequence[AxisId] | None] | None + """The order of the fully unstacked tensor""" + def __getitem__( self, key: PerMember[ @@ -78,6 +81,7 @@ def __getitem__( ], ) -> Self: return self.__class__( + unstacked_dim_order=self.unstacked_dim_order, members={m: t[key[m]] for m, t in self.members.items() if m in key}, stat=self.stat, id=self.id, @@ -90,7 +94,7 @@ def __postinit__(self): t.name = str(m) @property - def batch_multi_index(self) -> pd.MultiIndex | None: + def batch_multi_index(self) -> MultiIndex | None: """Return the batch multi-index of the sample, if it has one. Returns: @@ -101,7 +105,7 @@ def batch_multi_index(self) -> pd.MultiIndex | None: for tensor in self.members.values(): idx = tensor.batch_multi_index - if isinstance(idx, pd.MultiIndex): + if isinstance(idx, MultiIndex): return idx return None @@ -134,22 +138,17 @@ def shape(self) -> PerMember[PerAxis[int]]: return {tid: t.sizes for tid, t in self.members.items()} @property - def shape_batch_unstacked(self) -> PerMember[PerAxis[int]]: - """Return the sample shape with unstacked batch axes (if any).""" + def unstacked_batch_shape(self) -> PerAxis[int] | None: + """The shape of the unstacked batch axis + For example `{"t": 10, "z": 5}` for a sample with stacked "t" and "z" dimensions resulting in a batch length of 50). + Note: As the batch axis has to match across members this is the same for all members.""" if (idx := self.batch_multi_index) is None: - return self.shape - - ret = {m: dict(s) for m, s in self.shape.items()} - unstacked_axes = tuple( - BATCH_AXIS_ID if name == "original_batch" else AxisId(name) - for name in idx.names - ) - for m in self.members: - if BATCH_AXIS_ID in ret[m]: - for a, s in zip(unstacked_axes, idx.levshape): - ret[m][a] = s + return None - return ret + return { + BATCH_AXIS_ID if a == "original_batch" else AxisId(a): s + for a, s in zip(idx.names, idx.levshape) + } def as_arrays(self) -> dict[MemberId, NDArray[Any]]: """Return sample as dictionary of arrays.""" @@ -171,6 +170,8 @@ def split_into_blocks( n_blocks, blocks = split_multiple_shapes_into_blocks( shapes=self.shape, + unstacked_batch_shape=self.unstacked_batch_shape, + unstacked_dim_order=self.unstacked_dim_order, block_shapes=block_shapes, halo=halo, broadcast=broadcast, @@ -181,39 +182,24 @@ def as_single_block(self, halo: PerMember[PerAxis[Halo]] | None = None): if halo is None: halo = {} - inner_slice = { - m: {a: SliceInfo(0, s) for a, s in data.tagged_shape.items()} - for m, data in self.members.items() - } - inner_slice_batch_unstacked = { - m: {k: v for k, v in inner_slice[m].items()} for m in self.members - } - - for m, t in self.members.items(): - if isinstance( - idx := t.data.indexes.get(BATCH_AXIS_ID), # pyright: ignore[reportUnknownVariableType] - pd.MultiIndex, - ): - # add slice for each unstacked axis in the multi index - b_slice = inner_slice[m][BATCH_AXIS_ID] - for a, s, c in zip(idx.names, idx.levshape, idx.codes): - axis = BATCH_AXIS_ID if a == "original_batch" else AxisId(a) - # sample_shape_batch_unstacked[m][axis] = s - codes = c[b_slice.start : b_slice.stop] - s = SliceInfo(int(codes.min()), int(codes.max()) + 1) - inner_slice_batch_unstacked[m][axis] = s - return SampleBlockWithOrigin( sample_shape=self.shape, - sample_shape_batch_unstacked=self.shape_batch_unstacked, sample_id=self.id, blocks={ m: Block( sample_shape=self.shape[m], - sample_shape_batch_unstacked=self.shape_batch_unstacked[m], + unstacked_batch_shape=self.unstacked_batch_shape, + unstacked_dim_order=self.unstacked_dim_order[m] + if self.unstacked_dim_order is not None + else None, data=data, - inner_slice=inner_slice[m], - inner_slice_batch_unstacked=inner_slice_batch_unstacked[m], + inner_slice={a: SliceInfo(0, s) for a, s in self.shape[m].items()}, + unstacked_batch_slice=None + if self.unstacked_batch_shape is None + else { + a: SliceInfo(0, s) + for a, s in self.unstacked_batch_shape.items() + }, halo=halo.get(m, {}), block_index=0, blocks_in_sample=1, @@ -266,7 +252,7 @@ def from_blocks_yield_intermediates( sample_blocks: The blocks to create the sample from. fill_value: The value to fill missing values with (default: `nan`). """ - output = cls(members={}, stat={}, id=None) + output = cls(members={}, stat={}, id=None, unstacked_dim_order=None) for sample_block in sample_blocks: if output.id is None: output.id = sample_block.sample_id @@ -321,6 +307,7 @@ def pad( }, stat=self.stat, id=self.id, + unstacked_dim_order=self.unstacked_dim_order, ) def transpose( @@ -341,16 +328,29 @@ def transpose( if any(unknown := [m not in self.members for m in axes]): raise ValueError(f"Axes specified for unknown members: {unknown}") - members = { - m: t - if m not in axes - else t.transpose( - axes=axes[m], - extra_dims=extra_dims, - missing_dims=missing_dims, - ) - for m, t in self.members.items() - } + members: dict[MemberId, Tensor] = {} + individual_unstacked_dim_order: dict[MemberId, list[AxisId] | None] = {} + for m, t in self.members.items(): + if m in axes: + members[m], individual_unstacked_dim_order[m] = ( + t.transpose_and_return_unstacked_dim_order( + axes=axes[m], + extra_dims=extra_dims, + missing_dims=missing_dims, + ) + ) + else: + members[m] = t + individual_unstacked_dim_order[m] = None + + if any(order is None for order in individual_unstacked_dim_order.values()): + unstacked_dim_order = None + else: + unstacked_dim_order = { + m: order + for m, order in individual_unstacked_dim_order.items() + if order is not None + } if ( len( @@ -366,9 +366,14 @@ def transpose( f"Transposed sample members have incompatible batch lengths: {batch_lengths}." ) - return self.__class__(members=members, stat=dict(self.stat), id=self.id) + return self.__class__( + members=members, + stat=dict(self.stat), + id=self.id, + unstacked_dim_order=unstacked_dim_order, + ) - def assign_batch_multi_index(self, multi_index: pd.MultiIndex) -> Self: + def assign_batch_multi_index(self, multi_index: MultiIndex) -> Self: """Return a new sample with the batch multi-index assigned to all sample members. Raises: @@ -390,10 +395,14 @@ def assign_batch_multi_index(self, multi_index: pd.MultiIndex) -> Self: }, stat=dict(self.stat), id=self.id, + unstacked_dim_order=self.unstacked_dim_order, ) def unstack_batch_multi_index( - self, *, errors: Literal["raise", "warn", "ignore"] = "raise" + self, + *, + errors: Literal["raise", "warn", "ignore"] = "raise", + unstacked_dim_order: PerMember[Sequence[AxisId]] | None = None, ) -> Self: """Unstack the batch multi-index of all sample members. @@ -423,7 +432,12 @@ def unstack_batch_multi_index( members = { m: t if AxisId("batch") not in t.dims - else t.unstack_batch_multi_index(errors=errors) + else t.unstack_batch_multi_index( + errors=errors, + unstacked_dim_order=unstacked_dim_order[m] + if unstacked_dim_order is not None + else None, + ) for m, t in self.members.items() } if ( @@ -441,7 +455,7 @@ def unstack_batch_multi_index( ) stat: Stat = { - k: v.unstack_batch_multi_index(errors="ignore") + k: v.unstack_batch_multi_index(errors="ignore", unstacked_dim_order=None) if isinstance(v, Tensor) else float(v) for k, v in self.stat.items() @@ -450,6 +464,7 @@ def unstack_batch_multi_index( members=members, stat=stat, id=self.id, + unstacked_dim_order=None, ) @@ -463,9 +478,6 @@ class SampleBlockBase(Generic[BlockT]): sample_shape: PerMemberAnno[PerAxis[int]] """the sample shape this block represents a part of""" - sample_shape_batch_unstacked: PerMemberAnno[PerAxis[int]] - """the sample shape this block represents a part of, with unstacked batch axes (if any)""" - sample_id: SampleId """identifier for the sample within its dataset""" @@ -486,6 +498,15 @@ def shape(self) -> PerMember[PerAxis[int]]: def inner_shape(self) -> PerMember[PerAxis[int]]: return MappingProxyType({mid: b.inner_shape for mid, b in self.blocks.items()}) + @property + def unstacked_batch_shape(self) -> PerAxis[int] | None: + """The shape of the unstacked batch axis, if the sample has a multi-index batch axis""" + for b in self.blocks.values(): + if b.unstacked_batch_shape is not None: + return b.unstacked_batch_shape + + return None + @dataclass class LinearSampleAxisTransform(LinearAxisTransform): @@ -550,47 +571,60 @@ def get_member_halo(m: MemberId, round: Callable[[float], int]): } for m in new_axes } - # sample_shape_batch_unstacked/inner_slice_batch_unstacked undergoes the same transform as inner_slice, but keeps the unstacked batch slices of the original block - sample_shape_batch_unstacked = {m: dict(s) for m, s in sample_shape.items()} - inner_slice_batch_unstacked = {m: dict(s) for m, s in inner_slice.items()} - for m in list(inner_slice_batch_unstacked): - sh = sample_shape_batch_unstacked[m] - for a, sh_u in self.sample_shape_batch_unstacked[m].items(): - if a == BATCH_AXIS_ID or (a not in sh): - sh[a] = sh_u # keep unstacked axis size sh_u for stacked axis a - elif a not in self.sample_shape[m]: - raise ValueError( - f"Stacked axis {a} was overwritten by an axis transform." - + " This happens for example when transforming a tensor byx into zyx," - + " but actually starting with a tensor bzyx that was stacked to byx" - + " (to apply a yx->zyx model)." - + " The resulting bzyx output tensor cannot be unstacked due to the colliding z dim." - ) + unstacked_batch_slice = None + for b in self.blocks.values(): + if b.unstacked_batch_slice is None: + continue + elif unstacked_batch_slice is None: + unstacked_batch_slice = b.unstacked_batch_slice + else: + assert unstacked_batch_slice == b.unstacked_batch_slice, ( + "Incompatible unstacked batch slices across sample block members" + ) - sl = inner_slice_batch_unstacked[m] - for a, sl_u in ( - self.blocks[m].inner_slice_batch_unstacked or {} - if m in self.blocks - else {} - ).items(): - if a == BATCH_AXIS_ID or (a not in sl): - sl[a] = sl_u # keep unstacked axis slice sl_u for stacked axis a - elif a not in self.blocks[m].inner_slice: - raise ValueError( - f"Stacked axis {a} was overwritten by an axis transform." - + " This happens for example when transforming a tensor byx into zyx," - + " but actually starting with a tensor bzyx that was stacked to byx" - + " (to apply a yx->zyx model)." - + " The resulting bzyx output tensor cannot be unstacked due to the colliding z dim." - ) + unstacked_dim_order: dict[MemberId, list[AxisId] | None] = {} + for m, trfs in new_axes.items(): + for trf in trfs.values(): + if isinstance(trf, LinearSampleAxisTransform): + in_order = self.blocks[trf.member].unstacked_dim_order + if in_order is None or not all(a in in_order for a in trfs): + # transform added new axes, so we cannot preserve the unstacked input order + pass + else: + stacked_dims = ( + self.blocks[trf.member].unstacked_batch_shape or () + ) + if [d for d in trfs if d in in_order] != [ + d for d in in_order if d in trfs + ]: + # transform changed order of the non-stacking axes, so we cannot preserve the unstacked axis order + pass + else: + transformed_dim_oder = [ + d for d in in_order if d in trfs or d in stacked_dims + ] + assert ( + m not in unstacked_dim_order + or unstacked_dim_order[m] == transformed_dim_oder + ), ( + f"unstacked dim order mismatch for {m}: " + + f"{unstacked_dim_order[m]} != {transformed_dim_oder}" + ) + + unstacked_dim_order[m] = transformed_dim_oder + elif isinstance(trf, int): + pass + else: + assert_never(trf) return self.__class__( blocks={ m: BlockMeta( sample_shape=sample_shape[m], - sample_shape_batch_unstacked=sample_shape_batch_unstacked[m], + unstacked_batch_shape=self.unstacked_batch_shape, + unstacked_dim_order=unstacked_dim_order[m], inner_slice=inner_slice[m], - inner_slice_batch_unstacked=inner_slice_batch_unstacked[m], + unstacked_batch_slice=unstacked_batch_slice, halo=halo[m], block_index=self.block_index, blocks_in_sample=self.blocks_in_sample, @@ -598,7 +632,6 @@ def get_member_halo(m: MemberId, round: Callable[[float], int]): for m in new_axes }, sample_shape=sample_shape, - sample_shape_batch_unstacked=sample_shape_batch_unstacked, sample_id=self.sample_id, block_index=self.block_index, blocks_in_sample=self.blocks_in_sample, @@ -613,13 +646,6 @@ def with_data(self, data: PerMember[Tensor], *, stat: Stat) -> SampleBlock: } for m, member_shape in self.sample_shape.items() }, - sample_shape_batch_unstacked={ - m: { - a: data[m].tagged_shape[a] if s == -1 else s - for a, s in member_shape.items() - } - for m, member_shape in self.sample_shape_batch_unstacked.items() - }, sample_id=self.sample_id, blocks={ m: Block.from_meta(b, data=data[m]) for m, b in self.blocks.items() @@ -652,7 +678,6 @@ def get_transformed_meta( sample_id=self.sample_id, blocks=dict(self.blocks), sample_shape=self.sample_shape, - sample_shape_batch_unstacked=self.sample_shape_batch_unstacked, block_index=self.block_index, blocks_in_sample=self.blocks_in_sample, ).get_transformed(new_axes) @@ -663,7 +688,6 @@ def from_meta( ) -> Self: return cls( sample_shape=meta.sample_shape, - sample_shape_batch_unstacked=meta.sample_shape_batch_unstacked, sample_id=meta.sample_id, blocks={ m: Block.from_meta(b, data=data[m]) for m, b in meta.blocks.items() @@ -678,7 +702,6 @@ def get_meta(self) -> SampleBlockMeta: sample_id=self.sample_id, blocks={m: b.get_meta() for m, b in self.blocks.items()}, sample_shape=self.sample_shape, - sample_shape_batch_unstacked=self.sample_shape_batch_unstacked, block_index=self.block_index, blocks_in_sample=self.blocks_in_sample, ) @@ -694,6 +717,9 @@ def as_sample(self) -> Sample: members=dict(self.members), stat=dict(self.stat), id=self.sample_id, + unstacked_dim_order={ + m: b.unstacked_dim_order for m, b in self.blocks.items() + }, ) @@ -719,12 +745,21 @@ def __init__(self, blocks: PerMember[BlockMeta]): def sample_block_meta_generator( blocks: Iterable[PerMember[BlockMeta]], *, - sample_shape: PerMember[PerAxis[int]], + sample_shape: PerMember[PerAxis[int]], # TODO: remove redundant sample_shape arg? sample_id: SampleId, ): for member_blocks in blocks: cons = _ConsolidatedMemberBlocks(member_blocks) + unstacked_batch_shape = None for m, b in member_blocks.items(): + if b.unstacked_batch_shape is not None: + if unstacked_batch_shape is None: + unstacked_batch_shape = b.unstacked_batch_shape + else: + assert unstacked_batch_shape == b.unstacked_batch_shape, ( + f"unstacked batch shape mismatch for member {m}: {unstacked_batch_shape} != {b.unstacked_batch_shape}" + ) + assert b.sample_shape == sample_shape[m] yield SampleBlockMeta( @@ -756,7 +791,6 @@ def sample_block_generator( for m in origin.members }, sample_shape=origin.shape, - sample_shape_batch_unstacked=origin.shape_batch_unstacked, origin=origin, stat=origin.stat, sample_id=origin.id, diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index 4ba3b683..222e1f0b 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -14,7 +14,6 @@ ) import numpy as np -import pandas as pd import xarray as xr from loguru import logger from numpy.typing import DTypeLike, NDArray @@ -24,6 +23,7 @@ from bioimageio.spec.model.v0_5 import SpaceUnit, TimeUnit from ._magic_tensor_ops import MagicTensorOpsMixin +from ._multi_index import MultiIndex from .axis import BATCH_AXIS_ID, AxisId, AxisInfo, AxisLike, PerAxis from .common import ( CropWhere, @@ -637,18 +637,18 @@ def sum(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: return self.__class__.from_xarray(self._data.sum(dim=dim)) @property - def batch_multi_index(self) -> pd.MultiIndex | None: + def batch_multi_index(self) -> MultiIndex | None: """Return the batch multi-index if available, otherwise None.""" if BATCH_AXIS_ID not in self.dims: return None idx = self._data.indexes.get(BATCH_AXIS_ID) # pyright: ignore[reportUnknownVariableType] - if not isinstance(idx, pd.MultiIndex): + if not isinstance(idx, MultiIndex): return None return idx - def assign_batch_multi_index(self, multi_index: pd.MultiIndex) -> Self: + def assign_batch_multi_index(self, multi_index: MultiIndex) -> Self: """Set the batch multi-index for this tensor. Args: @@ -659,12 +659,26 @@ def assign_batch_multi_index(self, multi_index: pd.MultiIndex) -> Self: "Cannot set batch multi-index on a tensor without a 'batch' axis." ) - return self.__class__.from_xarray( - self._data.assign_coords({BATCH_AXIS_ID: multi_index}) + # # TODO: check if we really need to create/assign coordinates object + # alternatively create new multi-index: self._data.set_xindex(multi_index.names) + data = self._data.drop_vars( # pyright: ignore[reportUnknownVariableType] + list(multi_index.names) + [BATCH_AXIS_ID], errors="ignore" + ) + assert isinstance(data, xr.DataArray) + mindex_coords = xr.Coordinates.from_pandas_multiindex( # pyright: ignore[reportUnknownVariableType] + multi_index, BATCH_AXIS_ID ) + assert isinstance(mindex_coords, xr.Coordinates) + data = data.assign_coords(mindex_coords) # pyright: ignore[reportUnknownVariableType] + assert isinstance(data, xr.DataArray) + + return self.__class__.from_xarray(data) def unstack_batch_multi_index( - self, *, errors: Literal["raise", "warn", "ignore"] = "raise" + self, + *, + errors: Literal["raise", "warn", "ignore"] = "raise", + unstacked_dim_order: Sequence[AxisId] | None = None, ) -> Self: """Unstack the batch multi-index of this tensor. @@ -685,7 +699,7 @@ def unstack_batch_multi_index( else: assert_never(errors) - if not isinstance(self._data.indexes.get(BATCH_AXIS_ID), pd.MultiIndex): + if not isinstance(self._data.indexes.get(BATCH_AXIS_ID), MultiIndex): error_msg = "Cannot unstack batch multi-index on a tensor whose 'batch' axis does not have a MultiIndex." if errors == "raise": raise ValueError(error_msg) @@ -700,22 +714,30 @@ def unstack_batch_multi_index( old_dims = self.dims array = self._data.unstack(BATCH_AXIS_ID) assert isinstance(array, xr.DataArray) - added_dims = [AxisId(d) for d in array.dims if d not in self._data.dims] - - # restore expected axis order, replace batch dim with added dims - new_dims: list[AxisId] = [] - for d in old_dims: - if d in array.dims: - new_dims.append(d) - elif d == BATCH_AXIS_ID: - new_dims.extend(added_dims) - else: - raise ValueError(f"Expected axis {d} not found in unstacked array.") + added_dims = [ + BATCH_AXIS_ID if d == "original_batch" else AxisId(d) + for d in array.dims + if d not in self._data.dims + ] + + if unstacked_dim_order is None: + # restore expected axis order, replace batch dim with added dims + new_dims: Sequence[AxisId] = [] + for d in old_dims: + if d in array.dims: + new_dims.append(d) + elif d == BATCH_AXIS_ID: + new_dims.extend(added_dims) + else: + raise ValueError(f"Expected axis {d} not found in unstacked array.") + else: + new_dims = unstacked_dim_order - array = array.transpose(*new_dims) if AxisId("original_batch") in array.dims: array = array.rename({AxisId("original_batch"): BATCH_AXIS_ID}) + array = array.transpose(*new_dims) + assert isinstance(array, xr.DataArray) return self.__class__.from_xarray(array) @@ -747,6 +769,22 @@ def transpose( If "unstack", any missing dimensions will be unstacked from the batch dimension. For this option a batch dimension with a multi-index must be present from previous stacking operations or assigned by `Tensor.assign_batch_multi_index()`. If "unstack_or_expand", any missing dimensions will be unstacked from the batch dimension if it has a multi-index, otherwise they will be added as singleton dimensions. """ + transposed, _dim_order = self.transpose_and_return_unstacked_dim_order( + axes, extra_dims=extra_dims, missing_dims=missing_dims + ) + return transposed + + def transpose_and_return_unstacked_dim_order( + self, + axes: Sequence[AxisLike], + *, + extra_dims: Literal[ + "raise", "squeeze", "stack", "squeeze_or_stack" + ] = "squeeze", + missing_dims: Literal[ + "raise", "expand", "unstack", "unstack_or_expand" + ] = "unstack_or_expand", + ) -> tuple[Self, list[AxisId] | None]: axes = [AxisInfo.create(a).id for a in axes] array = self._data @@ -763,7 +801,7 @@ def transpose( raise ValueError(unstack_error) if lets_unstack and not isinstance( - array.indexes.get(BATCH_AXIS_ID), pd.MultiIndex + array.indexes.get(BATCH_AXIS_ID), MultiIndex ): lets_unstack = False unstack_error = f"Missing dimensions {unhandled_missing_dims} found, but 'batch' axis does not have a MultiIndex. Cannot unstack missing dimensions from non-multi-index batch." @@ -794,6 +832,7 @@ def transpose( raise ValueError(f"Missing dimensions {unhandled_missing_dims}.") assert isinstance(array, xr.DataArray) + unstacked_dim_order = None unhandled_extra_dims = [a for a in array.dims if a not in axes] if unhandled_extra_dims and extra_dims == "raise": @@ -803,6 +842,7 @@ def transpose( for d in list(unhandled_extra_dims): if array.sizes[d] == 1: array = array.squeeze(d) + assert isinstance(array, xr.DataArray) unhandled_extra_dims.remove(d) elif extra_dims == "squeeze": raise ValueError( @@ -816,6 +856,7 @@ def transpose( + " Cannot stack extra dimensions to batch." ) + unstacked_dim_order = [AxisId(d) for d in array.dims] if BATCH_AXIS_ID in array.dims: array = array.rename({BATCH_AXIS_ID: AxisId("original_batch")}) unhandled_extra_dims.insert(0, AxisId("original_batch")) @@ -829,7 +870,7 @@ def transpose( ) # transpose to the correct axis order - return self.__class__.from_xarray(array.transpose(*axes)) + return self.__class__.from_xarray(array.transpose(*axes)), unstacked_dim_order def var(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: return self.__class__.from_xarray(self._data.var(dim=dim)) diff --git a/tests/test_prediction.py b/tests/test_prediction.py index 3037ea6c..c313d2a1 100644 --- a/tests/test_prediction.py +++ b/tests/test_prediction.py @@ -3,7 +3,6 @@ from typing import Literal, NamedTuple import numpy as np -import pandas as pd import pytest import xarray as xr from typing_extensions import assert_never @@ -174,6 +173,8 @@ def test_predict_z_as_batch(unet2d_nuclei_broad_model: str): def test_transpose_sample_for_model_stacks_extra_z_to_batch( unet2d_nuclei_broad_model: str, ): + from bioimageio.core._multi_index import MultiIndex + model = load_model( unet2d_nuclei_broad_model, perform_io_checks=False, format_version="latest" ) @@ -193,4 +194,4 @@ def test_transpose_sample_for_model_stacks_extra_z_to_batch( raw = transposed.members[MemberId("raw")] assert raw.dims == (AxisId("batch"), AxisId("channel"), AxisId("y"), AxisId("x")) assert raw.sizes[AxisId("batch")] == 2 - assert isinstance(raw.data.indexes[AxisId("batch")], pd.MultiIndex) + assert isinstance(raw.data.indexes[AxisId("batch")], MultiIndex) diff --git a/tests/test_tensor.py b/tests/test_tensor.py index 92d8b5ea..b7126c70 100644 --- a/tests/test_tensor.py +++ b/tests/test_tensor.py @@ -1,7 +1,6 @@ from collections.abc import Sequence import numpy as np -import pandas as pd import pytest import xarray as xr from xarray.testing import assert_equal # pyright: ignore[reportUnknownVariableType] @@ -68,6 +67,8 @@ def test_shape_attributes(): def test_transpose_stack_extra_dims_to_batch_creates_multi_index(): + from bioimageio.core._multi_index import MultiIndex + tensor = Tensor.from_numpy( np.arange(2 * 3 * 4).reshape(2, 3, 4), dims=("batch", "z", "x"), @@ -83,7 +84,7 @@ def test_transpose_stack_extra_dims_to_batch_creates_multi_index(): assert transposed.sizes[AxisId("batch")] == 6 assert transposed.sizes[AxisId("x")] == 4 batch_index = transposed.data.indexes[AxisId("batch")] # pyright: ignore[reportUnknownVariableType] - assert isinstance(batch_index, pd.MultiIndex) + assert isinstance(batch_index, MultiIndex) assert tuple(str(level) for level in batch_index.names) == ("original_batch", "z") From 41aa51fa7932ae6e2eb8d14da74c9fe7778c911a Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 1 Sep 2026 18:29:24 +0200 Subject: [PATCH 21/85] fix predict_many --- src/bioimageio/core/prediction.py | 51 ++++++++++++++++++------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/bioimageio/core/prediction.py b/src/bioimageio/core/prediction.py index 645f97c4..8a019e2f 100644 --- a/src/bioimageio/core/prediction.py +++ b/src/bioimageio/core/prediction.py @@ -1,7 +1,7 @@ from __future__ import annotations -import collections.abc -from collections.abc import Hashable, Iterable, Iterator, Mapping, Sequence +import itertools +from collections.abc import Hashable, Iterable, Iterator, Mapping, Sequence, Sized from pathlib import Path from loguru import logger @@ -180,7 +180,7 @@ def predict_many( | v0_5.ModelDescr | PredictionPipeline, inputs: Iterable[PerMember[TensorSource]] | Iterable[TensorSource], - sample_id: str = "sample{i:03}", + sample_id: str | Iterable[str] = "sample{i:03}", blocksize_parameter: v0_5.ParameterizedSize_N | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] | None = None, @@ -193,11 +193,10 @@ def predict_many( Args: model: Model to predict with. May be given as RDF source, model description or prediction pipeline. - inputs: An iterable of the named input(s) for this model as a dictionary. - sample_id: The sample id. - note: `{i}` will be formatted as the i-th sample. - If `{i}` (or `{i:`) is not present and `inputs` is not an iterable `{i:03}` - is appended. + inputs: An iterable of the (named) input(s) for this model. + sample_id: The sample id pattern or iterable that is not a string (specify samples a,b,c as ['a', 'b', 'c'], not 'abc'). + Note: If the `sample_id` (item) contains `{i}`, it will be formatted as the i-th sample starting with i=0. + If `{i}` (or `{i:`) is not present and `sample_id` is a single string, `{i:03}` is appended. blocksize_parameter: (optional) Tile the input into blocks parametrized by blocksize according to any parametrized axis sizes defined in the model RDF. skip_preprocessing: Flag to skip the model's preprocessing. @@ -224,19 +223,29 @@ def predict_many( pp = create_prediction_pipeline(model) - if not isinstance(inputs, collections.abc.Mapping): + if isinstance(sample_id, str): if "{i}" not in sample_id and "{i:" not in sample_id: sample_id += "{i:03}" - total = len(inputs) if isinstance(inputs, collections.abc.Sized) else None - - for i, ipts in tqdm(enumerate(inputs), total=total): - yield predict( - model=pp, - inputs=ipts, - sample_id=sample_id.format(i=i), - blocksize_parameter=blocksize_parameter, - skip_preprocessing=skip_preprocessing, - skip_postprocessing=skip_postprocessing, - save_output_path=save_output_path, - ) + sample_ids = itertools.repeat(sample_id) + else: + sample_ids = sample_id + + total = ( + len(inputs) + if isinstance(inputs, Sized) + else len(sample_ids) + if not isinstance(sample_ids, str) and isinstance(sample_ids, Sized) + else None + ) + + for i, (s_id, ipts) in tqdm(enumerate(zip(sample_ids, inputs)), total=total): + yield predict( + model=pp, + inputs=ipts, + sample_id=s_id.format(i=i), + blocksize_parameter=blocksize_parameter, + skip_preprocessing=skip_preprocessing, + skip_postprocessing=skip_postprocessing, + save_output_path=save_output_path, + ) From b6ceaa6e81ef93eb3da0cf351a5213d47b1e4af9 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Wed, 2 Sep 2026 00:28:15 +0200 Subject: [PATCH 22/85] patch likely omitted size references for blockwise processing --- src/bioimageio/core/axis.py | 1 + src/bioimageio/core/block.py | 2 +- src/bioimageio/core/digest_spec.py | 95 +++++++++++++++++++++++------- src/bioimageio/core/proc_ops.py | 4 +- 4 files changed, 77 insertions(+), 25 deletions(-) diff --git a/src/bioimageio/core/axis.py b/src/bioimageio/core/axis.py index bc2785b0..00819a22 100644 --- a/src/bioimageio/core/axis.py +++ b/src/bioimageio/core/axis.py @@ -42,6 +42,7 @@ def _guess_axis_type(a: str): """An axis identifier, e.g. 'batch', 'channel', 'z', 'y', 'x'""" BATCH_AXIS_ID = v0_5.BATCH_AXIS_ID +CHANNEL_AXIS_ID = v0_5.CHANNEL_AXIS_ID _T = TypeVar("_T") PerAxis = Mapping[AxisId, _T] diff --git a/src/bioimageio/core/block.py b/src/bioimageio/core/block.py index 5ce85c01..582185e9 100644 --- a/src/bioimageio/core/block.py +++ b/src/bioimageio/core/block.py @@ -39,7 +39,7 @@ def __post_init__(self): halo = self.halo.get(a, Halo(0, 0)) if s != (s_exp := halo.left + (slice_.stop - slice_.start) + halo.right): raise ValueError( - f"Block data shape does not match block meta information for axis {a}: data size is {s} but expected {s_exp} (halo: {halo}, slice: {slice_})", + f"Block data shape does not match block meta information for {a} axis: data size is {s} but expected {s_exp} (halo: {halo}, slice: {slice_})", ) @classmethod diff --git a/src/bioimageio/core/digest_spec.py b/src/bioimageio/core/digest_spec.py index 1476ebcd..b50799d8 100644 --- a/src/bioimageio/core/digest_spec.py +++ b/src/bioimageio/core/digest_spec.py @@ -33,7 +33,7 @@ ) from bioimageio.spec.utils import load_array -from .axis import AxisId, AxisInfo, AxisLike, PerAxis +from .axis import CHANNEL_AXIS_ID, AxisId, AxisInfo, AxisLike, PerAxis from .block_meta import split_multiple_shapes_into_blocks from .common import Halo, MemberId, PerMember, SampleId, TotalNumberOfBlocks from .io import load_tensor_and_return_unstacked_dim_order @@ -365,10 +365,65 @@ def get_block_transform( t.id: {a.id: a.scale for a in t.axes} for t in chain(model.inputs, model.outputs) } + # a common inaccuracy in bioimage.io model specs is to define a fixed output size equal to a correspoding fixed input size, while the output size should actually be a size reference to the fixed input size to encode the relationship between input and output sizes. The following dict is used to patch such cases, by replacing the fixed output size with a size reference to the corresponding fixed input size. + patched_size_ref_for_fixed_output_size_matching_fixed_input_size: dict[ + MemberId, dict[AxisId, v0_5.SizeReference] + ] = {} + inconsistet_outputs: set[MemberId] = set() + for out in model.outputs: + for a in out.axes: + if not isinstance(a.size, int): + continue + + for ipt in model.inputs: + for ia in ipt.axes: + if ( + a.type not in ("space", "time") + or a.type != ia.type + or a.id != ia.id + or a.size != ia.size + or a.scale != ia.scale + or a.unit != ia.unit + ): + continue + + previous_size_refs = list( + patched_size_ref_for_fixed_output_size_matching_fixed_input_size.get( + out.id, {} + ).values() + ) + if previous_size_refs and ipt.id != previous_size_refs[0].tensor_id: + inconsistet_outputs.add(out.id) + else: + patched_size_ref_for_fixed_output_size_matching_fixed_input_size.setdefault( + out.id, {} + )[a.id] = v0_5.SizeReference(tensor_id=ipt.id, axis_id=ia.id) + + # remove inconsistent outputs from the patch dict + patched_size_ref_for_fixed_output_size_matching_fixed_input_size = { + m: {a: sr for a, sr in srs.items()} + for m, srs in patched_size_ref_for_fixed_output_size_matching_fixed_input_size.items() + if m not in inconsistet_outputs + } + if patched_size_ref_for_fixed_output_size_matching_fixed_input_size: + logger.warning( + "Patched output size references: {}\nThe fixed output sizes were likely intended to be defined as size references to the corresponding fixed input sizes.", + { + (k, kk): (sr.tensor_id, sr.axis_id) + for k, v in patched_size_ref_for_fixed_output_size_matching_fixed_input_size.items() + for kk, sr in v.items() + }, + ) + for out in model.outputs: new_axes: dict[AxisId, LinearSampleAxisTransform | int] = {} for a in out.axes: - if a.size is None: + a_size = ( + patched_size_ref_for_fixed_output_size_matching_fixed_input_size.get( + out.id, {} + ).get(a.id, a.size) + ) + if a_size is None: assert a.type == "batch" if batch_axis_trf is None: raise ValueError( @@ -376,34 +431,32 @@ def get_block_transform( + f" '{out.id}' has one." ) s = batch_axis_trf - elif isinstance(a.size, int): - s = a.size - elif isinstance(a.size, v0_5.DataDependentSize): + elif isinstance(a_size, int): + s = a_size + elif isinstance(a_size, v0_5.DataDependentSize): s = -1 - elif isinstance(a.size, v0_5.SizeReference): + elif isinstance(a_size, v0_5.SizeReference): s = LinearSampleAxisTransform( - axis=a.size.axis_id, - scale=axis_scales[a.size.tensor_id][a.size.axis_id] / a.scale, - offset=a.size.offset, - member=a.size.tensor_id, + axis=a_size.axis_id, + scale=axis_scales[a_size.tensor_id][a_size.axis_id] / a.scale, + offset=a_size.offset, + member=a_size.tensor_id, ) else: - assert_never(a.size) + assert_never(a_size) new_axes[a.id] = s # account for postprocessing that changes the nubmer of output channels by # overwriting described output shape by the intermediate output shape - c = AxisId("channel") - if c not in new_axes: - continue - for post in out.postprocessing: - if post.id == "cellpose_flow_dynamics": - new_axes[c] = 3 - break - elif post.id == "stardist_postprocessing": - new_axes[c] = post.kwargs.n_rays + 1 - break + if CHANNEL_AXIS_ID in new_axes: + for post in out.postprocessing: + if post.id == "cellpose_flow_dynamics": + new_axes[CHANNEL_AXIS_ID] = 3 + break + elif post.id == "stardist_postprocessing": + new_axes[CHANNEL_AXIS_ID] = post.kwargs.n_rays + 1 + break ret[out.id] = new_axes diff --git a/src/bioimageio/core/proc_ops.py b/src/bioimageio/core/proc_ops.py index 2ac88a2d..fbea430d 100644 --- a/src/bioimageio/core/proc_ops.py +++ b/src/bioimageio/core/proc_ops.py @@ -25,7 +25,7 @@ from ._op_base import BlockwiseOperator, SamplewiseOperator, SimpleOperator from ._ops_cellpose import CellposeFlowDynamics from ._ops_stardist import StardistPostprocessing2D, StardistPostprocessing3D -from .axis import AxisId +from .axis import CHANNEL_AXIS_ID, AxisId from .common import DTypeStr, MemberId from .digest_spec import get_member_id, import_callable from .sample import Sample, SampleBlock @@ -594,8 +594,6 @@ def get_descr(self): return v0_5.SigmoidDescr() -CHANNEL_AXIS_ID = AxisId("channel") - @dataclass class Softmax(SimpleOperator): """Softmax activation function.""" From bed1bda66875193c6aaae60bef323f800042dfc7 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Wed, 9 Sep 2026 14:57:03 +0200 Subject: [PATCH 23/85] keep track of unstacked axis order --- changelog.md | 2 +- pyproject.toml | 2 +- src/bioimageio/core/_prediction_pipeline.py | 35 +++++-- src/bioimageio/core/_resource_tests.py | 103 ++++++++++++++++---- src/bioimageio/core/block_meta.py | 2 +- src/bioimageio/core/cli.py | 1 + src/bioimageio/core/digest_spec.py | 68 ++++++------- src/bioimageio/core/io.py | 31 +++--- src/bioimageio/core/sample.py | 99 ++++++++++++------- tests/test_prediction.py | 2 + tests/test_prediction_pipeline.py | 3 +- tests/test_proc_ops.py | 1 + tests/test_resource_tests.py | 6 -- tests/test_stat_calculators.py | 7 +- tests/test_stat_measures.py | 4 +- 15 files changed, 251 insertions(+), 115 deletions(-) diff --git a/changelog.md b/changelog.md index 3dcbdec7..218969dc 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ ### 0.12.0 -- bump spec to 0.5.14.1 +- bump spec to 0.5.14.2 - support loading zarr images - dropped `is_volume` flag from `load_image()` diff --git a/pyproject.toml b/pyproject.toml index 8d8c1898..5b77c225 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ requires-python = ">=3.9" readme = "README.md" dynamic = ["version"] dependencies = [ - "bioimageio.spec ==0.5.14.1", + "bioimageio.spec ==0.5.14.2", "imagecodecs", "imageio>=2.10", "loguru", diff --git a/src/bioimageio/core/_prediction_pipeline.py b/src/bioimageio/core/_prediction_pipeline.py index 3e4eac03..342c97fc 100644 --- a/src/bioimageio/core/_prediction_pipeline.py +++ b/src/bioimageio/core/_prediction_pipeline.py @@ -38,7 +38,7 @@ ) from .proc_ops import Processing from .proc_setup import setup_pre_and_postprocessing -from .sample import Sample, SampleBlock +from .sample import Sample, SampleBlock, get_unstacked_output_dim_order from .stat_measures import Measure, MeasureValue, Stat from .tensor import Tensor @@ -575,14 +575,37 @@ def _predict_sample_without_blocking_impl( if not skip_preprocessing: self.apply_preprocessing(sample) + output_members = { + k: v + for k, v in self._adapter.forward(sample.members).items() + if v is not None + } + + unstacked_dim_order: PerMember[Sequence[AxisId] | None] | None = {} + for m, t in output_members.items(): + if (udo := sample.unstacked_dim_order) is None: + unstacked_in_order = None + else: + udo_m = udo.get(m) + if udo_m is None: + unstacked_in_order = None + else: + unstacked_in_order = list(udo_m) + + if (sd := sample.unstacked_batch_shape) is None: + stacked_dims = None + else: + stacked_dims = list(sd) + + unstacked_dim_order[m] = get_unstacked_output_dim_order( + t.dims, unstacked_in_order=unstacked_in_order, stacked_dims=stacked_dims + ) + output = Sample( - members={ - k: v - for k, v in self._adapter.forward(sample.members).items() - if v is not None - }, + members=output_members, stat=sample.stat, id=sample.id, + unstacked_dim_order=unstacked_dim_order, ) if not skip_postprocessing: self.apply_postprocessing(output) diff --git a/src/bioimageio/core/_resource_tests.py b/src/bioimageio/core/_resource_tests.py index 64b87553..9ee74e35 100644 --- a/src/bioimageio/core/_resource_tests.py +++ b/src/bioimageio/core/_resource_tests.py @@ -69,7 +69,7 @@ from ._settings import settings from .axis import AxisId, BatchSize from .common import MemberId, SupportedWeightsFormat -from .digest_spec import get_test_input_sample, get_test_output_sample +from .digest_spec import get_member_id, get_test_input_sample, get_test_output_sample from .io import save_tensor from .sample import Sample from .tensor import Tensor @@ -783,9 +783,11 @@ def _get_tolerance( m: MemberId, **deprecated: Unpack[DeprecatedKwargs], ) -> tuple[RelativeTolerance, AbsoluteTolerance, MismatchedElementsPerMillion]: + out_descr = next((out for out in model.outputs if get_member_id(out) == m), None) + if out_descr is None: + raise ValueError(f"Output '{m}' not found in model outputs") if isinstance(model, v0_5.ModelDescr): - applicable = v0_5.ReproducibilityTolerance() - + applicable = None # check legacy test kwargs for weight format specific tolerance if model.config.bioimageio.model_extra is not None: for weights_format, test_kwargs in model.config.bioimageio.model_extra.get( @@ -801,14 +803,51 @@ def _get_tolerance( # check for weights format and output tensor specific tolerance for a in model.config.bioimageio.reproducibility_tolerance: if (not a.weights_formats or wf in a.weights_formats) and ( - not a.output_ids or m in a.output_ids + not a.output_ids + or m in a.output_ids + or ( + a.output_ids == "any_continuous_output" + and next( + ( + o.dtype.startswith("float") + for o in model.outputs + if o.id == m + ), + False, + ) + ) + or ( + a.output_ids == "any_discrete_output" + and next( + ( + o.dtype.startswith("int") + or o.dtype.startswith("uint") + or o.dtype.startswith("bool") + for o in model.outputs + if o.id == m + ), + False, + ) + ) ): applicable = a break + if applicable is None: + if out_descr.dtype.startswith("float"): + applicable = v0_5.ContinuousReproducibilityTolerance.model_construct() + else: + applicable = v0_5.DiscreteReproducibilityTolerance.model_construct() + rtol = applicable.relative_tolerance atol = applicable.absolute_tolerance mismatched_tol = applicable.mismatched_elements_per_million + logger.debug( + "Using reproducibility tolerance: rtol={}, atol={}, mismatched_tol_ppm={}", + rtol, + atol, + mismatched_tol, + ) elif (decimal := deprecated.get("decimal")) is not None: warnings.warn( "The argument `decimal` has been deprecated in favour of" @@ -821,17 +860,32 @@ def _get_tolerance( atol = 1.5 * 10 ** (-decimal) rtol = 0 mismatched_tol = 0 + logger.debug( + "Using given (deprecated) reproducibility tolerance from 'decimal': atol={}", + atol, + ) else: - # use given (deprecated) test kwargs - atol = deprecated.get("absolute_tolerance", 1e-3) - rtol = deprecated.get("relative_tolerance", 1e-3) - mismatched_tol = 0 + is_continuous = out_descr.dtype.startswith("float") + rtol = deprecated.get("relative_tolerance", 1e-3 if is_continuous else 0) + atol = deprecated.get("absolute_tolerance", 1e-3 if is_continuous else 0) + mismatched_tol = 0 if is_continuous else 1000 + logger.debug( + "Using reproducibility tolerance: rtol={}, atol={}, mismatched_tol_ppm={}", + rtol, + atol, + mismatched_tol, + ) return rtol, atol, mismatched_tol def evaluate_mismatched_elements( - actual: Tensor, expected: Tensor, rtol: float, atol: float, name: str + actual: Tensor, + expected: Tensor, + rtol: float, + atol: float, + mismatched_tol: float, + name: str, ) -> tuple[float, str, str | None]: try: expected_np = expected.data.to_numpy().astype(np.float32) @@ -872,24 +926,34 @@ def evaluate_mismatched_elements( if mismatched_elements: msg = ( f"Output '{name}': {mismatched_elements} of " - + f"{expected_np.size} elements disagree with expected values (" + + f"{expected_np.size} elements (" + ( f"{mismatched_ppm / 10_000:.1f}%" if mismatched_ppm >= 1_000 else f"{mismatched_ppm:.1f} ppm" ) - + "). " + + ") disagree with expected values (tolerance: " + + ( + f"{mismatched_tol / 10_000:.1f}%" + if mismatched_tol >= 1_000 + else f"{mismatched_tol:.1f} ppm" + ) + + ")." ) else: msg = f"Output `{name}`: all elements agree with expected values. " - msg += ( - f"\nMax relative difference not accounted for by absolute tolerance ({atol:.2e}):\n{r_max:.2e}" - + rf" (= \|{r_actual:.2e} - {r_expected:.2e}\|/\|{r_expected:.2e} + 1e-6\|)" - + f" at {dict(zip(dims, r_max_idx))} " - + f"\nMax absolute difference not accounted for by relative tolerance ({rtol:.2e}):\n{a_max:.2e}" - + rf" (= \|{a_actual:.7e} - {a_expected:.7e}\|) at {dict(zip(dims, a_max_idx))}" - ) + if rtol > 0 and r_max != 0: + msg += ( + f"\nMax relative difference not accounted for by absolute tolerance ({atol:.2e}): {r_max:.2e} " + + rf"(= \|{r_actual:.2e} - {r_expected:.2e}\|/\|{r_expected:.2e} + 1e-6\|)" + + f" at {dict(zip(dims, map(int, r_max_idx)))} " + ) + if atol > 0 and a_max != 0: + msg += ( + f"\nMax absolute difference not accounted for by relative tolerance ({rtol:.2e}): {a_max:.2e}" + + rf" (= \|{a_actual:.7e} - {a_expected:.7e}\|) at {dict(zip(dims, map(int, a_max_idx)))}" + ) return mismatched_ppm, msg, error_msg @@ -1028,7 +1092,7 @@ def save_to_working_dir(name: str, tensor: Tensor) -> list[Path]: model, wf=weight_format, m=m, **deprecated ) mismatched_ppm, msg, error_msg = evaluate_mismatched_elements( - actual, expected, rtol, atol, m + actual, expected, rtol, atol, mismatched_tol, m ) if error_msg is not None: add_error_entry(error_msg) @@ -1146,6 +1210,7 @@ def get_ns(n: int): }, stat=test_input.stat, id=test_input.id, + unstacked_dim_order=None, ) expected_output_shapes = { t.id: { diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py index 178a2e65..cfbd9567 100644 --- a/src/bioimageio/core/block_meta.py +++ b/src/bioimageio/core/block_meta.py @@ -81,7 +81,7 @@ class BlockMeta: """the axis sizes of the stacked axis (that make up the batch axis)""" unstacked_dim_order: Sequence[AxisId] | None - """The order of the fully unstacked tensor, if the sample has a multi-index""" + """The order of dimensions of the fully unstacked tensor, if the sample has a multi-index""" inner_slice: PerAxisAnno[SliceInfo] """inner region (without halo) wrt the sample""" diff --git a/src/bioimageio/core/cli.py b/src/bioimageio/core/cli.py index 289584c5..e4a050e4 100644 --- a/src/bioimageio/core/cli.py +++ b/src/bioimageio/core/cli.py @@ -914,6 +914,7 @@ def _subtract_samples(a: Sample, b: Sample) -> Sample: members={t: a.members[t] - b.members[t] for t in a.members}, id=a.id, stat=a.stat, + unstacked_dim_order=a.unstacked_dim_order, ) diff --git a/src/bioimageio/core/digest_spec.py b/src/bioimageio/core/digest_spec.py index b50799d8..d491b90b 100644 --- a/src/bioimageio/core/digest_spec.py +++ b/src/bioimageio/core/digest_spec.py @@ -3,7 +3,7 @@ import collections.abc import importlib.util import sys -from collections.abc import Collection, Iterable, Mapping, Sequence +from collections.abc import Iterable, Mapping, Sequence from itertools import chain from pathlib import Path from tempfile import TemporaryDirectory @@ -42,6 +42,7 @@ Sample, SampleBlockMeta, SampleBlockWithOrigin, + get_unstacked_output_dim_order, sample_block_meta_generator, ) from .stat_measures import Stat @@ -268,37 +269,42 @@ class IO_SampleBlockMeta(NamedTuple): output: SampleBlockMeta -def get_output_unstacked_dim_order( - model: v0_5.ModelDescr, - unstacked_batch_dims: Collection[AxisId] | None, - input_unstacked_dim_order: PerMember[Sequence[AxisId]] | None, -) -> PerMember[Sequence[AxisId]] | None: - if input_unstacked_dim_order is None or unstacked_batch_dims is None: - return None - - output_dim_order: dict[MemberId, Sequence[AxisId]] = {} +def get_unstacked_output_dim_orders_for_model( + model: v0_4.ModelDescr | v0_5.ModelDescr, + unstacked_input_order: PerMember[Sequence[AxisId]] | None, + stacked_dims: Sequence[AxisId] | None, +) -> PerMember[Sequence[AxisId]]: + """Returns the unstacked output dim order for each output tensor of a model, + given optional unstacked input dim order and optional stacked dims.""" + output_orders: dict[MemberId, Sequence[AxisId]] = {} for out in model.outputs: - out_axes = {ax.id for ax in out.axes} - for a in out.axes: - if ( - isinstance(a.size, v0_5.SizeReference) - and a.size.tensor_id in input_unstacked_dim_order - ): - in_order = input_unstacked_dim_order[a.size.tensor_id] - if all( - a in out_axes or a in unstacked_batch_dims for a in in_order - ) and [a for a in in_order if a in out_axes] == [ - a.id for a in out.axes if a.id in in_order - ]: - # all input axes (and their order) are preserved in the output (or stacked) - break + in_order = None + if isinstance(out, v0_5.OutputTensorDescr): + out_id = out.id + stacked_out_order = [ax.id for ax in out.axes] + if unstacked_input_order is not None: + for ax in out.axes: + if isinstance(ax.size, v0_5.SizeReference): + in_order = unstacked_input_order.get(ax.size.tensor_id) + break + elif isinstance(out, v0_4.OutputTensorDescr): + out_id = MemberId(out.name) + stacked_out_order = [AxisId(ax) for ax in out.axes] + if unstacked_input_order is not None: + for s in out.shape: + if isinstance(s, v0_4.ImplicitOutputShape): + in_order = unstacked_input_order.get( + MemberId(s.reference_tensor) + ) + break else: - # not all unstacked output axes could be mapped to unstacked input axes - return None + assert_never(out) - output_dim_order[out.id] = in_order + output_orders[out_id] = get_unstacked_output_dim_order( + stacked_out_order, unstacked_in_order=in_order, stacked_dims=stacked_dims + ) - return output_dim_order + return output_orders def get_input_halo( @@ -501,15 +507,11 @@ def get_io_sample_block_metas( for t in model.outputs } input_halo = get_input_halo(model, output_halo) - unstacked_dim_order = get_output_unstacked_dim_order( - model, unstacked_batch_shape, unstacked_dim_order_input - ) - n_input_blocks, input_blocks = split_multiple_shapes_into_blocks( input_sample_shape, input_block_shape, unstacked_batch_shape=unstacked_batch_shape, - unstacked_dim_order=unstacked_dim_order, + unstacked_dim_order=unstacked_dim_order_input, halo=input_halo, ) block_transform = get_block_transform(model) diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index 45017af6..b0abc241 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -26,12 +26,6 @@ from typing_extensions import Literal, TypeAlias, assert_never from typing_extensions import TypeAliasType as _TypeAliasType -from bioimageio.core.utils._tiff_metadata import ( - ImageJMetadata, - OmeChannelMetadata, - OmeMetadata, - create_ome_metadata_from_xml_string, -) from bioimageio.spec._internal.io import ( RelativeDirectory, ZarrSource, @@ -65,6 +59,12 @@ from .tensor import Tensor from .utils._color import hex_to_rgb from .utils._io_zarr import open_zarr_multiscale_array +from .utils._tiff_metadata import ( + ImageJMetadata, + OmeChannelMetadata, + OmeMetadata, + create_ome_metadata_from_xml_string, +) from .utils._type_guards import is_tuple if TYPE_CHECKING: @@ -208,12 +208,12 @@ def _select_io_libs_based_on_extension( extensions: list[str] | None, ) -> tuple[IO_Lib, ...]: if not extensions: - return ("bioio", "imageio", "clearscale") + return ("clearscale", "bioio", "imageio") if extensions[-1] == ".npy": return ("numpy",) elif extensions[-1] == ".zarr": - return ("bioio", "clearscale") + return ("clearscale", "bioio") elif extensions[-2:] in ([".ome", ".tif"], [".ome", ".tiff"]): return ("tifffile", "bioio") elif extensions[-1] in (".tif", ".tiff"): @@ -857,13 +857,17 @@ def _save_tensor_clearscale( ) if ZARR_FORMAT == 2: zarr_attrs: dict[str, JsonValueReadOnly] = { - "multiscales": [multiscale.to_ome_zarr(version="0.4")] + "multiscales": [ + multiscale.to_ome_zarr(version="0.4") + ], # TODO: add axis semantics } elif ZARR_FORMAT == 3: zarr_attrs: dict[str, JsonValueReadOnly] = { "ome": { "version": "0.5", - "multiscales": [multiscale.to_ome_zarr(version="0.5")], + "multiscales": [ + multiscale.to_ome_zarr(version="0.5") + ], # TODO: add axis semantics } } else: @@ -1304,7 +1308,12 @@ def save_block_member( ) io_lib = None - with ThreadPoolExecutor(max_workers=max_workers) as executor: + with ( + ThreadPoolExecutor( + max_workers=1 # TODO: Fix missing blocks when saving blocks with clearscale+zarr-python in parallel + # TODO: check if all io_libs (with roi support) can handle parallel writing to the same file + ) as executor + ): for sample_block in chain([first_block], remaining_blocks): for m, p in path.items(): if io_lib is None: diff --git a/src/bioimageio/core/sample.py b/src/bioimageio/core/sample.py index 9b199422..0b3efa12 100644 --- a/src/bioimageio/core/sample.py +++ b/src/bioimageio/core/sample.py @@ -44,8 +44,6 @@ from .stat_measures import Stat from .tensor import Tensor -# TODO: allow for lazy samples to read/write to disk - @dataclass class Sample: @@ -66,7 +64,7 @@ class Sample: id: SampleId """Identifies the `Sample` within the dataset -- typically a number or a string.""" - unstacked_dim_order: PerMember[Sequence[AxisId] | None] | None + unstacked_dim_order: PerMember[Sequence[AxisId] | None] | None = None """The order of the fully unstacked tensor""" def __getitem__( @@ -252,8 +250,15 @@ def from_blocks_yield_intermediates( sample_blocks: The blocks to create the sample from. fill_value: The value to fill missing values with (default: `nan`). """ - output = cls(members={}, stat={}, id=None, unstacked_dim_order=None) + output = None for sample_block in sample_blocks: + if output is None: + output = cls( + members={}, + stat={}, + id=None, + unstacked_dim_order=sample_block.unstacked_dim_order, + ) if output.id is None: output.id = sample_block.sample_id else: @@ -582,41 +587,27 @@ def get_member_halo(m: MemberId, round: Callable[[float], int]): "Incompatible unstacked batch slices across sample block members" ) - unstacked_dim_order: dict[MemberId, list[AxisId] | None] = {} + unstacked_dim_order: dict[MemberId, list[AxisId]] = {} for m, trfs in new_axes.items(): + stacked_out_order = list(trfs) + in_order = None + stacked_dims = None for trf in trfs.values(): if isinstance(trf, LinearSampleAxisTransform): - in_order = self.blocks[trf.member].unstacked_dim_order - if in_order is None or not all(a in in_order for a in trfs): - # transform added new axes, so we cannot preserve the unstacked input order - pass - else: - stacked_dims = ( - self.blocks[trf.member].unstacked_batch_shape or () - ) - if [d for d in trfs if d in in_order] != [ - d for d in in_order if d in trfs - ]: - # transform changed order of the non-stacking axes, so we cannot preserve the unstacked axis order - pass - else: - transformed_dim_oder = [ - d for d in in_order if d in trfs or d in stacked_dims - ] - assert ( - m not in unstacked_dim_order - or unstacked_dim_order[m] == transformed_dim_oder - ), ( - f"unstacked dim order mismatch for {m}: " - + f"{unstacked_dim_order[m]} != {transformed_dim_oder}" - ) - - unstacked_dim_order[m] = transformed_dim_oder + in_order = list(self.blocks[trf.member].tagged_shape) + stacked_dims = self.blocks[trf.member].unstacked_dim_order + break elif isinstance(trf, int): pass else: assert_never(trf) + unstacked_dim_order[m] = get_unstacked_output_dim_order( + stacked_out_order, + unstacked_in_order=in_order, + stacked_dims=stacked_dims, + ) + return self.__class__( blocks={ m: BlockMeta( @@ -717,11 +708,13 @@ def as_sample(self) -> Sample: members=dict(self.members), stat=dict(self.stat), id=self.sample_id, - unstacked_dim_order={ - m: b.unstacked_dim_order for m, b in self.blocks.items() - }, + unstacked_dim_order=self.unstacked_dim_order, ) + @property + def unstacked_dim_order(self) -> PerMember[Sequence[AxisId] | None]: + return {m: b.unstacked_dim_order for m, b in self.blocks.items()} + @dataclass(frozen=True) class SampleBlockWithOrigin(SampleBlock): @@ -797,3 +790,41 @@ def sample_block_generator( block_index=cons.block_index, blocks_in_sample=cons.blocks_in_sample, ) + + +def get_unstacked_output_dim_order( + stacked_out_order: Sequence[AxisId], + *, + unstacked_in_order: Sequence[AxisId] | None, + stacked_dims: Sequence[AxisId] | None, +) -> list[AxisId]: + if not stacked_dims: + return list(stacked_out_order) + + if BATCH_AXIS_ID not in stacked_out_order: + raise ValueError( + f"stacked_out_order={stacked_out_order} does not contain a batch axis, but stacked_dims={stacked_dims} are provided" + ) + + if unstacked_in_order is not None and list(stacked_out_order) == [ + d for d in unstacked_in_order if d in stacked_out_order + ]: + # no differences in non-stacking axes, so we can preserve the unstacked axis order + return [ + d for d in unstacked_in_order if d in stacked_out_order or d in stacked_dims + ] + else: + # unstack batch axis in its place + ret = list(stacked_out_order) + b_idx = ret.index(AxisId("batch")) + ret[b_idx : b_idx + 1] = ( + [ + d for d in unstacked_in_order or () if d in stacked_dims + ] # keep unstacked axis order from input + + [ + d + for d in stacked_dims + if unstacked_in_order is None or d not in unstacked_in_order + ] # add any remaining stacked dims that are not in the unstacked input order + ) + return ret diff --git a/tests/test_prediction.py b/tests/test_prediction.py index c313d2a1..7484cc77 100644 --- a/tests/test_prediction.py +++ b/tests/test_prediction.py @@ -154,6 +154,7 @@ def test_predict_z_as_batch(unet2d_nuclei_broad_model: str): MemberId("raw"): Tensor(data, dims=["batch", "channel", "z", "y", "x"]) }, stat=input_sample.stat, + unstacked_dim_order=input_sample.unstacked_dim_order, ) input_sample = transpose_sample_for_model(input_sample, model) out = predict(model=unet2d_nuclei_broad_model, inputs=input_sample) @@ -188,6 +189,7 @@ def test_transpose_sample_for_model_stacks_extra_z_to_batch( MemberId("raw"): Tensor(data, dims=["batch", "channel", "z", "y", "x"]) }, stat=input_sample.stat, + unstacked_dim_order=input_sample.unstacked_dim_order, ) transposed = transpose_sample_for_model(sample_with_z, model) diff --git a/tests/test_prediction_pipeline.py b/tests/test_prediction_pipeline.py index fd7077ab..62d38582 100644 --- a/tests/test_prediction_pipeline.py +++ b/tests/test_prediction_pipeline.py @@ -16,6 +16,7 @@ def _alter_sample(sample: Sample, offset: float) -> Sample: id=f"{sample.id}_altered", members={m: t + offset for m, t in sample.members.items()}, stat=sample.stat, + unstacked_dim_order=sample.unstacked_dim_order, ) @@ -62,7 +63,7 @@ def _test_prediction_pipeline( assert out is not None exp = expected_outputs.members[m] mismatched_ppm, msg, error_msg = evaluate_mismatched_elements( - out, exp, rtol=0.01, atol=0.1, name=m + out, exp, rtol=0.01, atol=0.1, mismatched_tol=50_000, name=m ) if error_msg is not None: raise AssertionError(error_msg) diff --git a/tests/test_proc_ops.py b/tests/test_proc_ops.py index 9efdab93..53cf9c67 100644 --- a/tests/test_proc_ops.py +++ b/tests/test_proc_ops.py @@ -528,6 +528,7 @@ def test_softmax_from_spec_descr(tid: MemberId, tmp_path: Path): out_data = sample.members[tid].data assert out_data is not None channel_sums = out_data.sum(dim="channel") + assert isinstance(channel_sums, xr.DataArray) xr.testing.assert_allclose( channel_sums, xr.ones_like(channel_sums), atol=1e-6, rtol=0 ) diff --git a/tests/test_resource_tests.py b/tests/test_resource_tests.py index 892fbe77..5b09176b 100644 --- a/tests/test_resource_tests.py +++ b/tests/test_resource_tests.py @@ -1,9 +1,7 @@ from pathlib import Path import numpy as np -import pytest -from bioimageio.core import __version__ from bioimageio.spec import InvalidDescr, ValidationContext @@ -50,10 +48,6 @@ def test_loading_description_multiple_times(unet2d_nuclei_broad_model: str): assert not isinstance(model_descr, InvalidDescr) -@pytest.mark.skipif( - __version__ == "0.11.0", - reason="Previously released bioimageio.core 0.10.4 is incompatible with updated unet2d nuclei broad model description 0.5.11", -) def test_test_description_runtime_env(unet2d_nuclei_broad_model: str): from bioimageio.core._resource_tests import test_description diff --git a/tests/test_stat_calculators.py b/tests/test_stat_calculators.py index e2494f28..e0941dd3 100644 --- a/tests/test_stat_calculators.py +++ b/tests/test_stat_calculators.py @@ -24,7 +24,12 @@ def create_random_dataset(tid: MemberId, axes: tuple[AxisId, ...]): sizes = list(range(n, len(axes) + n)) data = np.asarray(np.random.rand(*sizes)) ds = [ - Sample(members={tid: Tensor(data[i : i + 1], dims=axes)}, stat={}, id=None) + Sample( + members={tid: Tensor(data[i : i + 1], dims=axes)}, + stat={}, + id=None, + unstacked_dim_order=None, + ) for i in range(n) ] return Tensor(data, dims=axes), ds diff --git a/tests/test_stat_measures.py b/tests/test_stat_measures.py index 86001b00..c108aec1 100644 --- a/tests/test_stat_measures.py +++ b/tests/test_stat_measures.py @@ -65,7 +65,9 @@ def test_individual_percentile_measure( data = Tensor( np.random.random((5, 6, 3)), dims=(AxisId("x"), AxisId("y"), AxisId("c")) ) - actual = calc.compute(Sample(members={tid: data}, stat={}, id=None)) + actual = calc.compute( + Sample(members={tid: data}, stat={}, id=None, unstacked_dim_order=None) + ) for m in measures: expected = data.quantile(q=m.q, dim=m.axes, method=m.method) actual_data = actual[m] From 1aec384b243743b0342df89a7b4219d89ddbaebe Mon Sep 17 00:00:00 2001 From: fynnbe Date: Thu, 10 Sep 2026 21:31:46 +0200 Subject: [PATCH 24/85] add test_description_serializer_roundtrip --- tests/test_description_serializer.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/test_description_serializer.py diff --git a/tests/test_description_serializer.py b/tests/test_description_serializer.py new file mode 100644 index 00000000..98c4127e --- /dev/null +++ b/tests/test_description_serializer.py @@ -0,0 +1,19 @@ +def test_description_serializer_roundtrip(unet2d_nuclei_broad_model: str): + from bioimageio.core._description_serializer import DescriptionSerializer + from bioimageio.spec import load_model_description + + rd1 = load_model_description(unet2d_nuclei_broad_model, perform_io_checks=False) + + serialized1, hash1 = DescriptionSerializer.serialize_to_string_and_hash(rd1) + assert len(serialized1) > 2083 + + rd2 = DescriptionSerializer.deserialize_from_string(serialized1) + assert rd2.name == rd1.name + assert rd2.type == rd1.type + assert rd2.format_version == rd1.format_version + for k, v in rd1: + assert str(getattr(rd2, k)) == str(v), (k, v, getattr(rd2, k)) + + serialized2, hash2 = DescriptionSerializer.serialize_to_string_and_hash(rd2) + assert serialized2 == serialized1 + assert hash2 == hash1 From b2aa74543d891cf7aab04ddfab712627b03aa2ba Mon Sep 17 00:00:00 2001 From: fynnbe Date: Fri, 11 Sep 2026 16:32:00 +0200 Subject: [PATCH 25/85] WIP blockwise multi-index support --- pyproject.toml | 2 +- src/bioimageio/core/_model_adapter.py | 18 +- src/bioimageio/core/_multi_index.py | 3 - src/bioimageio/core/_op_base.py | 13 +- src/bioimageio/core/_prediction_pipeline.py | 9 +- .../core/_restore_batch_multi_index.py | 27 -- src/bioimageio/core/axis.py | 21 +- src/bioimageio/core/block.py | 10 +- src/bioimageio/core/block_meta.py | 49 +++- src/bioimageio/core/digest_spec.py | 97 ++++++- src/bioimageio/core/io.py | 15 +- .../core/remote_backends/gradio/client.py | 63 ++-- .../core/remote_backends/gradio/server.py | 24 +- src/bioimageio/core/sample.py | 197 ++++++++----- src/bioimageio/core/stat_measures.py | 2 +- src/bioimageio/core/tensor.py | 4 +- src/bioimageio/core/utils/_multi_index.py | 196 +++++++++++++ .../core/utils/_resolve_per_member_key.py | 54 ++++ src/bioimageio/core/utils/_type_guards.py | 18 +- tests/test_multi_index.py | 102 +++++++ tests/test_prediction.py | 2 +- tests/test_prediction_pipeline.py | 51 +++- tests/test_remote_backends/test_gradio.py | 270 +++++++++++++++++- tests/test_stat_serialization.py | 80 ++++++ tests/test_tensor.py | 2 +- 25 files changed, 1116 insertions(+), 213 deletions(-) delete mode 100644 src/bioimageio/core/_multi_index.py delete mode 100644 src/bioimageio/core/_restore_batch_multi_index.py create mode 100644 src/bioimageio/core/utils/_multi_index.py create mode 100644 src/bioimageio/core/utils/_resolve_per_member_key.py create mode 100644 tests/test_multi_index.py create mode 100644 tests/test_stat_serialization.py diff --git a/pyproject.toml b/pyproject.toml index 5b77c225..32d54b85 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -179,7 +179,7 @@ exclude = [ [tool.ruff.lint] extend-select = ["NPY201"] -ignore = ["BLE001"] +ignore = ["BLE001", "PLC0414"] [tool.ruff.lint.isort] known-first-party = ["bioimageio"] diff --git a/src/bioimageio/core/_model_adapter.py b/src/bioimageio/core/_model_adapter.py index cab9132a..009e5880 100644 --- a/src/bioimageio/core/_model_adapter.py +++ b/src/bioimageio/core/_model_adapter.py @@ -18,12 +18,12 @@ from bioimageio.spec import ValidationSummary from bioimageio.spec.model import AnyModelDescr, v0_4 -from ._restore_batch_multi_index import restore_batch_multi_index from ._sample_serializer import SampleSerializer, SerializedSampleBlockType from .common import PerMember from .digest_spec import get_axes_infos, get_member_ids from .sample import Sample from .tensor import Tensor +from .utils._multi_index import restore_batch_multi_index class ModelAdapter(ABC): @@ -46,9 +46,7 @@ class ModelAdapter(ABC): ``` """ - def __init__( - self, model_description: AnyModelDescr, devices: Sequence[str] | None - ): + def __init__(self, model_description: AnyModelDescr, devices: Sequence[str] | None): super().__init__() self._model_descr = model_description self._input_ids = get_member_ids(model_description.inputs) @@ -76,9 +74,7 @@ def load(self) -> None: self._loaded = True @abstractmethod - def forward( - self, inputs: PerMember[Tensor | None] - ) -> PerMember[Tensor | None]: ... + def forward(self, inputs: PerMember[Tensor | None]) -> PerMember[Tensor | None]: ... @abstractmethod def unload(self): @@ -151,9 +147,7 @@ def _parse_devices(self, devices: Sequence[str] | None) -> Sequence[DeviceType]: @abstractmethod def _init_model_on_device(self, device: DeviceType) -> ModelType: ... - def forward( - self, inputs: PerMember[Tensor | None] - ) -> PerMember[Tensor | None]: + def forward(self, inputs: PerMember[Tensor | None]) -> PerMember[Tensor | None]: """ Run forward pass of model to get model predictions @@ -269,9 +263,7 @@ def __init__( def server(self) -> str: return self._server - def forward( - self, inputs: PerMember[Tensor | None] - ) -> PerMember[Tensor | None]: + def forward(self, inputs: PerMember[Tensor | None]) -> PerMember[Tensor | None]: serialized_input = self._serializer.serialize_sample( Sample( members={k: v for k, v in inputs.items() if v is not None}, diff --git a/src/bioimageio/core/_multi_index.py b/src/bioimageio/core/_multi_index.py deleted file mode 100644 index a4580a40..00000000 --- a/src/bioimageio/core/_multi_index.py +++ /dev/null @@ -1,3 +0,0 @@ -import pandas as pd - -MultiIndex = pd.MultiIndex diff --git a/src/bioimageio/core/_op_base.py b/src/bioimageio/core/_op_base.py index 6d28e4a2..3043de2a 100644 --- a/src/bioimageio/core/_op_base.py +++ b/src/bioimageio/core/_op_base.py @@ -7,7 +7,6 @@ from typing_extensions import TypeVar, assert_never -from ._restore_batch_multi_index import restore_batch_multi_index from .axis import PerAxis from .block import Block from .common import MemberId @@ -17,6 +16,7 @@ Stat, ) from .tensor import Tensor +from .utils._multi_index import restore_batch_multi_index SampleT = TypeVar("SampleT", bound=Union[Sample, SampleBlock, SampleBlockWithOrigin]) @@ -75,21 +75,24 @@ def __call__(self, sample: Sample | SampleBlock) -> None: output_shape = self.get_output_shape(sample.sample_shape[self.input]) if ( b.unstacked_dim_order is None - or b.unstacked_batch_shape is None + or b.unstacked_sample_batch_shape is None or any(a not in b.unstacked_dim_order for a in output_shape) - or any(a not in b.unstacked_dim_order for a in b.unstacked_batch_shape) + or any( + a not in b.unstacked_dim_order + for a in b.unstacked_sample_batch_shape + ) ): unstacked_dims = None else: unstacked_dims = [ d for d in b.unstacked_dim_order - if d in output_shape or d in b.unstacked_batch_shape + if d in output_shape or d in b.unstacked_sample_batch_shape ] sample.blocks[self.output] = Block( sample_shape=output_shape, - unstacked_batch_shape=sample.unstacked_batch_shape, + unstacked_sample_batch_shape=sample.unstacked_sample_batch_shape, unstacked_dim_order=unstacked_dims, data=output_tensor, inner_slice=b.inner_slice, diff --git a/src/bioimageio/core/_prediction_pipeline.py b/src/bioimageio/core/_prediction_pipeline.py index 342c97fc..e73ce532 100644 --- a/src/bioimageio/core/_prediction_pipeline.py +++ b/src/bioimageio/core/_prediction_pipeline.py @@ -38,9 +38,10 @@ ) from .proc_ops import Processing from .proc_setup import setup_pre_and_postprocessing -from .sample import Sample, SampleBlock, get_unstacked_output_dim_order +from .sample import Sample, SampleBlock from .stat_measures import Measure, MeasureValue, Stat from .tensor import Tensor +from .utils._multi_index import get_unstacked_dim_order Predict_IO = TypeVar( "Predict_IO", @@ -597,8 +598,10 @@ def _predict_sample_without_blocking_impl( else: stacked_dims = list(sd) - unstacked_dim_order[m] = get_unstacked_output_dim_order( - t.dims, unstacked_in_order=unstacked_in_order, stacked_dims=stacked_dims + unstacked_dim_order[m] = get_unstacked_dim_order( + t.dims, + unstacked_reference_order=unstacked_in_order, + stacked_dims=stacked_dims, ) output = Sample( diff --git a/src/bioimageio/core/_restore_batch_multi_index.py b/src/bioimageio/core/_restore_batch_multi_index.py deleted file mode 100644 index 3d05791f..00000000 --- a/src/bioimageio/core/_restore_batch_multi_index.py +++ /dev/null @@ -1,27 +0,0 @@ -from __future__ import annotations - -from ._multi_index import MultiIndex -from .axis import AxisId -from .common import PerMember -from .tensor import Tensor - - -def restore_batch_multi_index( - inputs: PerMember[Tensor | None], outputs: PerMember[Tensor | None] -) -> PerMember[Tensor | None]: - """Restore the first batch multi-index found in the inputs to all outputs with batch dimension.""" - for tensor in inputs.values(): - if tensor is None: - continue - - idx = tensor.data.indexes.get(AxisId("batch")) # pyright: ignore[reportUnknownVariableType] - if isinstance(idx, MultiIndex): - outputs = { - k: v.assign_batch_multi_index(idx) - if v is not None and AxisId("batch") in v.dims - else v - for k, v in outputs.items() - } - break - - return outputs diff --git a/src/bioimageio/core/axis.py b/src/bioimageio/core/axis.py index 00819a22..1f11bd53 100644 --- a/src/bioimageio/core/axis.py +++ b/src/bioimageio/core/axis.py @@ -8,6 +8,7 @@ Union, ) +from loguru import logger from typing_extensions import Protocol, TypeAlias, assert_never, runtime_checkable from bioimageio.spec.model import v0_5 @@ -26,13 +27,15 @@ def _guess_axis_type(a: str): elif a in ("x", "y", "z") or a.startswith("space"): return "space" else: - raise ValueError( - f"Failed to infer axis type for axis id '{a}'." + logger.warning( + "Failed to infer axis type for axis id '{}'." + " Consider using one of:" + " 'b', 'batch', 't', 'time', 's', 'sample', 'i'," + " 'index', 'c', 'channel', 'x', 'y', 'z'," + " 'space*'. Or create an `Axis` object instead.", + a, ) + return "unknown" S = TypeVar("S", bound=str) @@ -66,13 +69,17 @@ class AxisDescrLike(Protocol): @dataclass class Axis: id: AxisId - type: Literal["batch", "channel", "index", "space", "time"] + type: Literal["batch", "channel", "index", "space", "time", "unknown"] def __post_init__(self): - if self.type == "batch": - self.id = AxisId("batch") - elif self.type == "channel": - self.id = AxisId("channel") + if self.type == "batch" and self.id != BATCH_AXIS_ID: + raise ValueError( + f"Axis with type 'batch' must have id '{BATCH_AXIS_ID}', but got '{self.id}'" + ) + if self.type == "channel" and self.id != CHANNEL_AXIS_ID: + raise ValueError( + f"Axis with type 'channel' must have id '{CHANNEL_AXIS_ID}', but got '{self.id}'" + ) @classmethod def create(cls, axis: AxisLike) -> Axis: diff --git a/src/bioimageio/core/block.py b/src/bioimageio/core/block.py index 582185e9..6d6bd34f 100644 --- a/src/bioimageio/core/block.py +++ b/src/bioimageio/core/block.py @@ -8,7 +8,6 @@ from typing_extensions import Self -from ._multi_index import MultiIndex from .axis import BATCH_AXIS_ID, AxisId, PerAxis from .block_meta import BlockMeta, LinearAxisTransform, split_shape_into_blocks from .common import ( @@ -18,6 +17,7 @@ TotalNumberOfBlocks, ) from .tensor import Tensor +from .utils._multi_index import MultiIndex @dataclass(frozen=True) @@ -61,13 +61,13 @@ def from_sample_member( raise ValueError("Expected a MultiIndex for the batch axis") padded_block_data = padded_block_data.assign_batch_multi_index(idx) - assert block.unstacked_batch_shape is not None + assert block.unstacked_sample_batch_shape is not None assert block.inner_slice[BATCH_AXIS_ID] == block.outer_slice[BATCH_AXIS_ID] return cls( data=padded_block_data, sample_shape=sample_member.tagged_shape, - unstacked_batch_shape=block.unstacked_batch_shape, + unstacked_sample_batch_shape=block.unstacked_sample_batch_shape, unstacked_dim_order=block.unstacked_dim_order, inner_slice=block.inner_slice, unstacked_batch_slice=block.unstacked_batch_slice, @@ -83,7 +83,7 @@ def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: def from_meta(cls, meta: BlockMeta, data: Tensor) -> Self: return cls( sample_shape=meta.sample_shape, - unstacked_batch_shape=meta.unstacked_batch_shape, + unstacked_sample_batch_shape=meta.unstacked_sample_batch_shape, unstacked_dim_order=meta.unstacked_dim_order, inner_slice=meta.inner_slice, unstacked_batch_slice=meta.unstacked_batch_slice, @@ -96,7 +96,7 @@ def from_meta(cls, meta: BlockMeta, data: Tensor) -> Self: def get_meta(self) -> BlockMeta: return BlockMeta( sample_shape=self.sample_shape, - unstacked_batch_shape=self.unstacked_batch_shape, + unstacked_sample_batch_shape=self.unstacked_sample_batch_shape, unstacked_dim_order=self.unstacked_dim_order, inner_slice=self.inner_slice, unstacked_batch_slice=self.unstacked_batch_slice, diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py index cfbd9567..d5a6c4ff 100644 --- a/src/bioimageio/core/block_meta.py +++ b/src/bioimageio/core/block_meta.py @@ -16,7 +16,6 @@ from typing_extensions import Self from ._axis_annotations import PerAxisAnno -from ._multi_index import MultiIndex from .axis import BATCH_AXIS_ID, AxisId, PerAxis from .common import ( BlockIndex, @@ -28,6 +27,11 @@ SliceInfo, TotalNumberOfBlocks, ) +from .utils._multi_index import ( + MultiIndex, + get_unstacked_dim_order, + get_unstacked_shape, +) @dataclass @@ -77,11 +81,11 @@ class BlockMeta: sample_shape: PerAxisAnno[int] """the axis sizes of the whole (unblocked) sample""" - unstacked_batch_shape: PerAxisAnno[int] | None - """the axis sizes of the stacked axis (that make up the batch axis)""" + unstacked_sample_batch_shape: PerAxisAnno[int] | None + """the axis sizes of the stacked axes (that make up the sample batch axis)""" unstacked_dim_order: Sequence[AxisId] | None - """The order of dimensions of the fully unstacked tensor, if the sample has a multi-index""" + """The order of dimensions of the unstacked tensor, if the sample has a multi-index""" inner_slice: PerAxisAnno[SliceInfo] """inner region (without halo) wrt the sample""" @@ -108,6 +112,15 @@ def shape(self) -> PerAxis[int]: } ) + @property + def unstacked_sample_shape(self) -> PerAxis[int]: + """combined (stacked) sample shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" + return get_unstacked_shape( + self.sample_shape, + unstacked_dim_order=self.unstacked_dim_order, + unstacked_batch_shape=self.unstacked_sample_batch_shape, + ) + @cached_property def padding(self) -> PerAxis[PadWidth]: """padding to realize the halo at the sample edge @@ -247,8 +260,14 @@ def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: return self.__class__( sample_shape=sample_shape, - unstacked_batch_shape=self.unstacked_batch_shape, - unstacked_dim_order=self.unstacked_dim_order, + unstacked_sample_batch_shape=self.unstacked_sample_batch_shape, + unstacked_dim_order=get_unstacked_dim_order( + tuple(sample_shape), + unstacked_reference_order=self.unstacked_dim_order, + stacked_dims=None + if self.unstacked_sample_batch_shape is None + else tuple(self.unstacked_sample_batch_shape), + ), inner_slice=inner_slice, unstacked_batch_slice=self.unstacked_batch_slice, halo={ @@ -273,8 +292,9 @@ def split_shape_into_blocks( stride: PerAxis[int] | None = None, unstacked_dim_order: Sequence[AxisId] | None, ) -> tuple[TotalNumberOfBlocks, Generator[BlockMeta, Any, None]]: - unknown_axes = [a for a in block_shape if a not in shape and block_shape[a] != 1] - if unknown_axes: + if unknown_axes := [ + a for a in block_shape if a not in shape and block_shape[a] != 1 + ]: raise ValueError( f"unknown axes in block_shape: {unknown_axes} for shape {shape}" ) @@ -314,7 +334,7 @@ def split_shape_into_blocks( def _block_meta_generator( sample_shape: PerAxis[int], - unstacked_batch_shape: PerAxis[int] | None, + unstacked_sample_batch_shape: PerAxis[int] | None, *, unstacked_dim_order: Sequence[AxisId] | None, blocks_in_sample: int, @@ -324,12 +344,15 @@ def _block_meta_generator( assert all(a in sample_shape for a in halo) halo = {a: Halo.create(halo.get(a, 0)) for a in inner_1d_slices} - if unstacked_batch_shape is None: + if unstacked_sample_batch_shape is None: idx: MultiIndex | None = None else: idx = MultiIndex.from_product( # pyright: ignore[reportUnknownVariableType] - [range(unstacked_batch_shape[a]) for a in unstacked_batch_shape], - names=list(unstacked_batch_shape), + [ + range(unstacked_sample_batch_shape[a]) + for a in unstacked_sample_batch_shape + ], + names=list(unstacked_sample_batch_shape), ) assert isinstance(idx, MultiIndex) @@ -349,7 +372,7 @@ def _block_meta_generator( yield BlockMeta( sample_shape=sample_shape, - unstacked_batch_shape=unstacked_batch_shape, + unstacked_sample_batch_shape=unstacked_sample_batch_shape, unstacked_dim_order=unstacked_dim_order, inner_slice=inner_slice, unstacked_batch_slice=unstacked_batch_slice, diff --git a/src/bioimageio/core/digest_spec.py b/src/bioimageio/core/digest_spec.py index d491b90b..d06d2f45 100644 --- a/src/bioimageio/core/digest_spec.py +++ b/src/bioimageio/core/digest_spec.py @@ -42,11 +42,11 @@ Sample, SampleBlockMeta, SampleBlockWithOrigin, - get_unstacked_output_dim_order, sample_block_meta_generator, ) from .stat_measures import Stat from .tensor import Tensor +from .utils._multi_index import get_unstacked_dim_order TensorSource: TypeAlias = Union[ Tensor, xr.DataArray, NDArray[Any], PermissiveFileSource @@ -204,7 +204,20 @@ def get_member_ids( return [get_member_id(descr) for descr in tensor_descriptions] -def get_test_input_sample(model: AnyModelDescr) -> Sample: +def get_test_input_sample( + model: AnyModelDescr, + *, + repetitions: Sequence[tuple[int, AxisInfo, Sequence[float]]] = (), +) -> Sample: + """returns a model's test input sample + + Args: + model: The model for which to create a test input sample. + repetitions: A mapping of additional axes (keyed by their index and ID) to sequences of value factors. This adds new dimensions with scaled repetitions of the original test sample that will be stacked into a/the batch dimension. + + Returns: + A test input sample for the given model. + """ if isinstance(model, v0_4.ModelDescr): info = { MemberId(d.name): (d, t) for d, t in zip(model.inputs, model.test_inputs) @@ -212,14 +225,18 @@ def get_test_input_sample(model: AnyModelDescr) -> Sample: else: info = {d.id: d for d in model.inputs} - return _get_test_sample(info) + return _get_test_sample(info, repetitions=repetitions) get_test_inputs = get_test_input_sample """DEPRECATED: use `get_test_input_sample` instead""" -def get_test_output_sample(model: AnyModelDescr) -> Sample: +def get_test_output_sample( + model: AnyModelDescr, + *, + repetitions: Sequence[tuple[int, AxisInfo, Sequence[float]]] = (), +) -> Sample: """returns a model's test output sample""" if isinstance(model, v0_4.ModelDescr): info = { @@ -228,7 +245,7 @@ def get_test_output_sample(model: AnyModelDescr) -> Sample: else: info = {d.id: d for d in model.outputs} - return _get_test_sample(info) + return _get_test_sample(info, repetitions=repetitions) get_test_outputs = get_test_output_sample @@ -239,6 +256,8 @@ def _get_test_sample( info: Mapping[MemberId, v0_5.InputTensorDescr | v0_5.OutputTensorDescr] | Mapping[MemberId, tuple[v0_4.InputTensorDescr, FileSource]] | Mapping[MemberId, tuple[v0_4.OutputTensorDescr, FileSource]], + *, + repetitions: Sequence[tuple[int, AxisInfo, Sequence[float]]] = (), ) -> Sample: arrays: dict[MemberId, NDArray[Any]] = {} for m, src in info.items(): @@ -256,11 +275,43 @@ def _get_test_sample( axes = { m: get_axes_infos(t[0] if isinstance(t, tuple) else t) for m, t in info.items() } + tensors: dict[MemberId, Tensor] = {} + unstacked_dim_order: PerMember[Sequence[AxisId]] = {} + for m in info: + a = arrays[m] + ax = axes[m] + axis_order_wo_repititions = [ax_.id for ax_ in ax] + if repetitions: + for rep_idx, rep_ax, factors in repetitions: + if rep_idx < -1 or rep_idx >= len(ax): + raise ValueError( + f"Invalid repetition index {rep_idx} for tensor '{m}' with axes {ax}" + ) + # Repeat the array along the specified axis according to the factors + a = np.stack([a * factor for factor in factors], axis=rep_idx) + if len(factors) < rep_ax.size.min: + raise ValueError( + f"Invalid number of repetitions {len(factors)} for axis '{rep_ax.id}' with min size {rep_ax.size.min}" + ) + if rep_ax.size.max is not None and len(factors) > rep_ax.size.max: + raise ValueError( + f"Invalid number of repetitions {len(factors)} for axis '{rep_ax.id}' with max size {rep_ax.size.max}" + ) + ax.insert(rep_idx, rep_ax) + + tensors[m] = Tensor.from_numpy(a, dims=ax) + if repetitions: + tensors[m], udo = tensors[m].transpose_and_return_unstacked_dim_order( + axis_order_wo_repititions, extra_dims="stack" + ) + assert udo is not None + unstacked_dim_order[m] = udo + return Sample( - members={m: Tensor.from_numpy(arrays[m], dims=axes[m]) for m in info}, + members=tensors, stat={}, id="test-sample", - unstacked_dim_order=None, + unstacked_dim_order=unstacked_dim_order or None, ) @@ -300,8 +351,10 @@ def get_unstacked_output_dim_orders_for_model( else: assert_never(out) - output_orders[out_id] = get_unstacked_output_dim_order( - stacked_out_order, unstacked_in_order=in_order, stacked_dims=stacked_dims + output_orders[out_id] = get_unstacked_dim_order( + stacked_out_order, + unstacked_reference_order=in_order, + stacked_dims=stacked_dims, ) return output_orders @@ -438,10 +491,22 @@ def get_block_transform( ) s = batch_axis_trf elif isinstance(a_size, int): - s = a_size + if a.type == "batch": + if batch_axis_trf is None: + raise ValueError( + "no batch axis found in any input tensor, but output tensor" + + f" '{out.id}' has one." + ) + s = batch_axis_trf + else: + s = a_size elif isinstance(a_size, v0_5.DataDependentSize): + assert a.type != "batch", ( + "data dependent size cannot be used for batch axis" + ) s = -1 elif isinstance(a_size, v0_5.SizeReference): + assert a.type != "batch", "size reference cannot be used for batch axis" s = LinearSampleAxisTransform( axis=a_size.axis_id, scale=axis_scales[a_size.tensor_id][a_size.axis_id] / a.scale, @@ -473,7 +538,7 @@ def get_io_sample_block_metas( model: v0_5.ModelDescr, input_sample_shape: PerMember[PerAxis[int]], *, - unstacked_batch_shape: PerAxis[int] | None = None, + unstacked_sample_batch_shape: PerAxis[int] | None = None, unstacked_dim_order_input: PerMember[Sequence[AxisId]] | None = None, ns: Mapping[tuple[MemberId, AxisId], ParameterizedSize_N], batch_size: int = 1, @@ -483,16 +548,18 @@ def get_io_sample_block_metas( raise TypeError(f"get_block_meta() not implemented for {type(model)}") if ( - unstacked_batch_shape is not None + unstacked_sample_batch_shape is not None and batch_size != 1 and ( not_divisible := { - a for a, s in unstacked_batch_shape.items() if s % batch_size != 0 + a + for a, s in unstacked_sample_batch_shape.items() + if s % batch_size != 0 } ) ): raise ValueError( - f"unstacked_batch_shape={unstacked_batch_shape} is not divisible by batch_size={batch_size} for axes {not_divisible}" + f"unstacked_batch_shape={unstacked_sample_batch_shape} is not divisible by batch_size={batch_size} for axes {not_divisible}" ) block_axis_sizes = model.get_axis_sizes(ns=ns, batch_size=batch_size) @@ -510,7 +577,7 @@ def get_io_sample_block_metas( n_input_blocks, input_blocks = split_multiple_shapes_into_blocks( input_sample_shape, input_block_shape, - unstacked_batch_shape=unstacked_batch_shape, + unstacked_batch_shape=unstacked_sample_batch_shape, unstacked_dim_order=unstacked_dim_order_input, halo=input_halo, ) diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index b0abc241..c4870174 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -1277,24 +1277,27 @@ def save_block_member( tensor_shape = dict(sample_block.sample_shape[m]) if ( - unstacked_batch_shape := sample_block.unstacked_batch_shape + unstacked_sample_batch_shape + := sample_block.unstacked_sample_batch_shape ) is not None: _ = tensor_shape.pop(BATCH_AXIS_ID, None) - assert not any(d in unstacked_batch_shape for d in tensor_shape), ( - unstacked_batch_shape, + assert not any( + d in unstacked_sample_batch_shape for d in tensor_shape + ), ( + unstacked_sample_batch_shape, tensor_shape, ) - tensor_shape = {**unstacked_batch_shape, **tensor_shape} + tensor_shape = {**unstacked_sample_batch_shape, **tensor_shape} assert t.dims == tuple(tensor_shape.keys()), ( t.dims, tuple(tensor_shape.keys()), ) assert ( - unstacked_batch_shape is None + unstacked_sample_batch_shape is None and unstacked_batch_slice is None or ( - unstacked_batch_shape is not None + unstacked_sample_batch_shape is not None and unstacked_batch_slice is not None ) ) diff --git a/src/bioimageio/core/remote_backends/gradio/client.py b/src/bioimageio/core/remote_backends/gradio/client.py index 4590511f..e9f3402a 100644 --- a/src/bioimageio/core/remote_backends/gradio/client.py +++ b/src/bioimageio/core/remote_backends/gradio/client.py @@ -27,9 +27,7 @@ class GradioModelAdapter(RemoteModelAdapter[SerializedSampleBlock]): """Model adapter to use the bioimage-io-gradio-runner as a backend for model inference.""" - def __init__( - self, model_description: AnyModelDescr, *, server: str | None = None - ): + def __init__(self, model_description: AnyModelDescr, *, server: str | None = None): """Initialize the GradioModelAdapter. Note: @@ -148,6 +146,13 @@ def __init__( self._serializer = GradioSampleSerializer self._precomputed_statistics = dict(precomputed_statistics) + def close(self): + logger.info( + "Closing GradioPredictionPipeline client connection to server {}", + self.server, + ) + self._client.close() + def predict_sample_block( self, sample_block: SampleBlock, @@ -260,13 +265,23 @@ def _intermediate_predictions() -> Iterable[IntermediatePrediction]: _intermediate_predictions(), ) + def get_status(self) -> dict[Literal["message"], str]: + """Check the status of the server.""" + logger.info( + "Checking status of GradioPredictionPipeline server {}", self.server + ) + return self._client.submit(api_name="/get_status").result(timeout=30) + def _call_predict_api( client: Client, serialized_model: str, sha256: str, serialized_input_sample: Iterable[SerializedSampleBlock], - blocksize: int | Literal["blockwise_as_serialized"] | PerMember[PerAxis[int]] | None, + blocksize: int + | Literal["blockwise_as_serialized"] + | PerMember[PerAxis[int]] + | None, skip_preprocessing: bool, skip_postprocessing: bool, skip_input_padding: bool, @@ -293,25 +308,27 @@ def submit(model: str): ) try_with_model_upload = True - try: - job = submit("") - for block in job: # pyright: ignore[reportUnknownVariableType] - yield block # pyright: ignore[reportReturnType] - # we got one response, so the model cache was hit... - try_with_model_upload = False - except Exception as e: - # A raised exception on the server seems to simply return an empty response sequence, - # so this except is likely not triggered at all. - # Below we retry on empty return value, too. - if try_with_model_upload: - logger.warning( - "Failed to submit job without model upload, trying with model upload, error was: {}", - e, - ) - else: - raise + # try: + # job = submit("") + # return list(job) # TOOD: reactivate yielding + # # for block in job: # pyright: ignore[reportUnknownVariableType] + # # yield block # pyright: ignore[reportReturnType] + # # # we got one response, so the model cache was hit... + # # try_with_model_upload = False + # except Exception as e: + # # A raised exception on the server seems to simply return an empty response sequence, + # # so this except is likely not triggered at all. + # # Below we retry on empty return value, too. + # if try_with_model_upload: + # logger.warning( + # "Failed to submit job without model upload, trying with model upload, error was: {}", + # e, + # ) + # else: + # raise if try_with_model_upload: job = submit(serialized_model) - for block in job: # pyright: ignore[reportUnknownVariableType] - yield block # pyright: ignore[reportReturnType] + return list(job) # TOOD: reactivate yielding + # for block in job: # pyright: ignore[reportUnknownVariableType] + # yield block # pyright: ignore[reportReturnType] diff --git a/src/bioimageio/core/remote_backends/gradio/server.py b/src/bioimageio/core/remote_backends/gradio/server.py index 1122003e..6fdbc3cc 100644 --- a/src/bioimageio/core/remote_backends/gradio/server.py +++ b/src/bioimageio/core/remote_backends/gradio/server.py @@ -2,10 +2,7 @@ from collections.abc import Iterable from itertools import chain -from typing import ( - Any, - Literal, -) +from typing import Any, Literal import gradio as gr from loguru import logger @@ -78,6 +75,11 @@ def predict( batch_size: Optional batch size only applicable to predicting input samples with batch dimension. """ + logger.info( + "Received prediction request for model {}", + model[:10] + "..." if len(model) > 10 else model, + ) + def setup(stat: Stat): model_adapter = _get_model_adapter(model, sha256=sha256) return bioimageio.core.create_prediction_pipeline( @@ -176,6 +178,13 @@ def load_model( return {"message": "Model loaded successfully"} +@app.api(name="get_status") # pyright: ignore[reportUntypedFunctionDecorator] +def get_status() -> dict[Literal["message"], str]: + """Check the status of the server.""" + logger.info("Received status request") + return {"message": "alive"} + + @app.api(name="test_model") # pyright: ignore[reportUntypedFunctionDecorator] def test_model( model: str, @@ -239,8 +248,13 @@ def root(): def main(port: int | None = None) -> str: + logger.info("Starting bioimageio.core gradio server on port {}", port) _app, local_url, _share_url = app.launch( - mcp_server=True, show_error=True, server_port=port + mcp_server=False, + show_error=True, + server_name="localhost", + server_port=port, + debug=False, ) return local_url diff --git a/src/bioimageio/core/sample.py b/src/bioimageio/core/sample.py index 0b3efa12..1306bb34 100644 --- a/src/bioimageio/core/sample.py +++ b/src/bioimageio/core/sample.py @@ -11,6 +11,7 @@ Generic, Literal, TypeVar, + Union, ) import numpy as np @@ -20,8 +21,9 @@ from numpy.typing import NDArray from typing_extensions import Self, assert_never +from bioimageio.core.utils._multi_index import get_unstacked_dim_order + from ._common_annotations import PerMemberAnno -from ._multi_index import MultiIndex from .axis import BATCH_AXIS_ID, AxisId, PerAxis from .block import Block from .block_meta import ( @@ -43,6 +45,12 @@ ) from .stat_measures import Stat from .tensor import Tensor +from .utils._multi_index import ( + MultiIndex, + create_tensor_with_multiindex, + get_unstacked_shape, +) +from .utils._resolve_per_member_key import resolve_per_member_var @dataclass @@ -76,11 +84,30 @@ def __getitem__( | PerAxis[SliceInfo | slice | int] | Tensor | xr.DataArray - ], + ] + | PerAxis[SliceInfo | slice | int], ) -> Self: + per_member_key = resolve_per_member_var( + key, + valid_member_index_types=( + SliceInfo, + slice, + int, + Tensor, + xr.DataArray, + Mapping[AxisId, Union[SliceInfo, slice, int]], + ), + valid_axis_index_types=(SliceInfo, slice, int), + members=self.members, + ) + return self.__class__( unstacked_dim_order=self.unstacked_dim_order, - members={m: t[key[m]] for m, t in self.members.items() if m in key}, + members={ + m: t[per_member_key[m]] + for m, t in self.members.items() + if m in per_member_key + }, stat=self.stat, id=self.id, ) @@ -148,6 +175,22 @@ def unstacked_batch_shape(self) -> PerAxis[int] | None: for a, s in zip(idx.names, idx.levshape) } + @property + def unstacked_shape(self) -> PerMember[PerAxis[int]]: + """combined (stacked) shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" + return { + m: get_unstacked_shape( + sh, + unstacked_dim_order=self.unstacked_dim_order[m] + if self.unstacked_dim_order is not None + else None, + unstacked_batch_shape=self.unstacked_batch_shape[m] + if self.unstacked_batch_shape is not None + else None, + ) + for m, sh in self.shape.items() + } + def as_arrays(self) -> dict[MemberId, NDArray[Any]]: """Return sample as dictionary of arrays.""" return {m: t.to_numpy() for m, t in self.members.items()} @@ -186,7 +229,7 @@ def as_single_block(self, halo: PerMember[PerAxis[Halo]] | None = None): blocks={ m: Block( sample_shape=self.shape[m], - unstacked_batch_shape=self.unstacked_batch_shape, + unstacked_sample_batch_shape=self.unstacked_batch_shape, unstacked_dim_order=self.unstacked_dim_order[m] if self.unstacked_dim_order is not None else None, @@ -275,14 +318,34 @@ def from_blocks_yield_intermediates( "merging blocks with data dependent axis not yet implemented" ) - output.members[m] = Tensor( - np.full( - tuple(block.sample_shape[a] for a in block.data.dims), - fill_value, - dtype=block.data.dtype, - ), - dims=block.data.dims, - ) + if ( + block.unstacked_sample_batch_shape is not None + and block.unstacked_dim_order is not None + ): + sizes = { + **block.sample_shape, + **block.unstacked_sample_batch_shape, + } + full_unstacked_shape = { + a: sizes[a] for a in block.unstacked_dim_order + } + t = create_tensor_with_multiindex( + full_unstacked_shape, + fill_value=fill_value, + dtype=np.dtype(block.data.dtype), + transpose_to=tuple(block.sample_shape.keys()), + ) + else: + t = Tensor( + np.full( + shape=tuple(block.sample_shape.values()), + fill_value=fill_value, + dtype=block.data.dtype, + ), + dims=tuple(block.sample_shape.keys()), + ) + + output.members[m] = t output.members[m][block.inner_slice] = block.inner_data yield output @@ -504,14 +567,32 @@ def inner_shape(self) -> PerMember[PerAxis[int]]: return MappingProxyType({mid: b.inner_shape for mid, b in self.blocks.items()}) @property - def unstacked_batch_shape(self) -> PerAxis[int] | None: + def unstacked_sample_batch_shape(self) -> PerAxis[int] | None: """The shape of the unstacked batch axis, if the sample has a multi-index batch axis""" for b in self.blocks.values(): - if b.unstacked_batch_shape is not None: - return b.unstacked_batch_shape + if b.unstacked_sample_batch_shape is not None: + return b.unstacked_sample_batch_shape return None + @property + def unstacked_dim_order(self) -> PerMemberAnno[Sequence[AxisId] | None] | None: + """The dim order of the unstacked tensors, if the sample has a multi-index batch axis""" + for b in self.blocks.values(): + if b.unstacked_dim_order is not None: + return MappingProxyType( + {mid: bb.unstacked_dim_order for mid, bb in self.blocks.items()} + ) + + return None + + @property + def unstacked_sample_shape(self) -> PerMemberAnno[PerAxis[int]]: + """combined (stacked) sample shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" + return MappingProxyType( + {mid: b.unstacked_sample_shape for mid, b in self.blocks.items()} + ) + @dataclass class LinearSampleAxisTransform(LinearAxisTransform): @@ -587,32 +668,32 @@ def get_member_halo(m: MemberId, round: Callable[[float], int]): "Incompatible unstacked batch slices across sample block members" ) - unstacked_dim_order: dict[MemberId, list[AxisId]] = {} + unstacked_dim_order: dict[MemberId, list[AxisId] | None] = {} for m, trfs in new_axes.items(): - stacked_out_order = list(trfs) - in_order = None - stacked_dims = None for trf in trfs.values(): if isinstance(trf, LinearSampleAxisTransform): - in_order = list(self.blocks[trf.member].tagged_shape) - stacked_dims = self.blocks[trf.member].unstacked_dim_order + unstacked_in_order = self.blocks[trf.member].unstacked_dim_order + stacked_dims = list( + self.blocks[trf.member].unstacked_sample_batch_shape or () + ) + unstacked_dim_order[m] = get_unstacked_dim_order( + list(trfs), + unstacked_reference_order=unstacked_in_order, + stacked_dims=stacked_dims, + ) break elif isinstance(trf, int): pass else: assert_never(trf) - - unstacked_dim_order[m] = get_unstacked_output_dim_order( - stacked_out_order, - unstacked_in_order=in_order, - stacked_dims=stacked_dims, - ) + else: + unstacked_dim_order[m] = None return self.__class__( blocks={ m: BlockMeta( sample_shape=sample_shape[m], - unstacked_batch_shape=self.unstacked_batch_shape, + unstacked_sample_batch_shape=self.unstacked_sample_batch_shape, unstacked_dim_order=unstacked_dim_order[m], inner_slice=inner_slice[m], unstacked_batch_slice=unstacked_batch_slice, @@ -715,6 +796,20 @@ def as_sample(self) -> Sample: def unstacked_dim_order(self) -> PerMember[Sequence[AxisId] | None]: return {m: b.unstacked_dim_order for m, b in self.blocks.items()} + @property + def batch_multi_index(self) -> MultiIndex | None: + """Return the batch multi-index of the sample block, if it has one. + + Returns: + The batch multi-index of the sample block, or `None` if the sample block does not have a multi-index batch dimension. + """ + for tensor in self.members.values(): + idx = tensor.batch_multi_index + if idx is not None: + return idx + + return None + @dataclass(frozen=True) class SampleBlockWithOrigin(SampleBlock): @@ -745,12 +840,12 @@ def sample_block_meta_generator( cons = _ConsolidatedMemberBlocks(member_blocks) unstacked_batch_shape = None for m, b in member_blocks.items(): - if b.unstacked_batch_shape is not None: + if b.unstacked_sample_batch_shape is not None: if unstacked_batch_shape is None: - unstacked_batch_shape = b.unstacked_batch_shape + unstacked_batch_shape = b.unstacked_sample_batch_shape else: - assert unstacked_batch_shape == b.unstacked_batch_shape, ( - f"unstacked batch shape mismatch for member {m}: {unstacked_batch_shape} != {b.unstacked_batch_shape}" + assert unstacked_batch_shape == b.unstacked_sample_batch_shape, ( + f"unstacked batch shape mismatch for member {m}: {unstacked_batch_shape} != {b.unstacked_sample_batch_shape}" ) assert b.sample_shape == sample_shape[m] @@ -790,41 +885,3 @@ def sample_block_generator( block_index=cons.block_index, blocks_in_sample=cons.blocks_in_sample, ) - - -def get_unstacked_output_dim_order( - stacked_out_order: Sequence[AxisId], - *, - unstacked_in_order: Sequence[AxisId] | None, - stacked_dims: Sequence[AxisId] | None, -) -> list[AxisId]: - if not stacked_dims: - return list(stacked_out_order) - - if BATCH_AXIS_ID not in stacked_out_order: - raise ValueError( - f"stacked_out_order={stacked_out_order} does not contain a batch axis, but stacked_dims={stacked_dims} are provided" - ) - - if unstacked_in_order is not None and list(stacked_out_order) == [ - d for d in unstacked_in_order if d in stacked_out_order - ]: - # no differences in non-stacking axes, so we can preserve the unstacked axis order - return [ - d for d in unstacked_in_order if d in stacked_out_order or d in stacked_dims - ] - else: - # unstack batch axis in its place - ret = list(stacked_out_order) - b_idx = ret.index(AxisId("batch")) - ret[b_idx : b_idx + 1] = ( - [ - d for d in unstacked_in_order or () if d in stacked_dims - ] # keep unstacked axis order from input - + [ - d - for d in stacked_dims - if unstacked_in_order is None or d not in unstacked_in_order - ] # add any remaining stacked dims that are not in the unstacked input order - ) - return ret diff --git a/src/bioimageio/core/stat_measures.py b/src/bioimageio/core/stat_measures.py index 461ad057..0e807e04 100644 --- a/src/bioimageio/core/stat_measures.py +++ b/src/bioimageio/core/stat_measures.py @@ -35,7 +35,7 @@ def tensor_custom_before_validator(data: Tensor | Mapping[str, Any]): def tensor_custom_serializer(t: Tensor) -> dict[str, Any]: # custome serialization logic - return {"data": t.data.data.tolist(), "dims": list(map(str, t.dims))} + return {"data": t.data.to_numpy().tolist(), "dims": list(map(str, t.dims))} MeasureValue: TypeAlias = Union[ diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index 222e1f0b..32ec4026 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -23,7 +23,6 @@ from bioimageio.spec.model.v0_5 import SpaceUnit, TimeUnit from ._magic_tensor_ops import MagicTensorOpsMixin -from ._multi_index import MultiIndex from .axis import BATCH_AXIS_ID, AxisId, AxisInfo, AxisLike, PerAxis from .common import ( CropWhere, @@ -36,6 +35,7 @@ SliceInfo, ) from .utils._color import get_default_channel_colors +from .utils._multi_index import MultiIndex from .utils._type_guards import is_dict, is_mapping, is_sequence try: @@ -523,7 +523,7 @@ def pad( and pad_width.get(BATCH_AXIS_ID, 0) in (0, (0, 0), PadWidth(0, 0)) and (idx := self.batch_multi_index) is not None ): - # add multiindex removed by padding when batch axis is not padded + # add multi-index removed by padding when batch axis is not padded padded_tensor = padded_tensor.assign_batch_multi_index(idx) return padded_tensor diff --git a/src/bioimageio/core/utils/_multi_index.py b/src/bioimageio/core/utils/_multi_index.py new file mode 100644 index 00000000..4677ac5f --- /dev/null +++ b/src/bioimageio/core/utils/_multi_index.py @@ -0,0 +1,196 @@ +from __future__ import annotations + +from collections.abc import Sequence +from typing import TYPE_CHECKING, Any + +import dask.array as da +import numpy as np +import pandas as pd +from numpy.typing import NDArray +from typing_extensions import assert_never + +from bioimageio.core.axis import BATCH_AXIS_ID, AxisId + +from ..axis import AxisId, PerAxis +from ..common import PerMember +from ._type_guards import is_ndarray + +if TYPE_CHECKING: + from ..tensor import Tensor + +MultiIndex = pd.MultiIndex + + +def create_tensor_with_multiindex( + unstacked_shape: PerAxis[int], + transpose_to: Sequence[AxisId], + *, + fill_value: float | None = None, + dtype: np.dtype | None = None, + unstacked_data: NDArray[Any] | da.Array | None = None, + stacked_data: NDArray[Any] | da.Array | None = None, +) -> Tensor: + """Create a tensor potentially with a MultiIndex in the batch dimension. + + The tensor is created with the given unstacked shape and then transposed to `transpose_to`. + If `transpose_to` is missing some of the unstacked_shape dimensions a MultiIndex will be created. + Otherwise the tensor will be created without a batch MultiIndex. + + Args: + unstacked_shape: The shape of the tensor before stacking. + transpose_to: The order of the axes after transposing. + fill_value: The value to fill the tensor with. If None, the tensor will be empty (ignored if unstacked_data or stacked_data are not None). + dtype: The data type of the tensor. If None, the data type will be inferred from unstacked_data or stacked_data (must be specified if neither unstacked_data nor stacked_data is provided). + unstacked_data: The data to use for the unstacked tensor. If provided, fill_value will be ignored. May not be provided together with stacked_data. + stacked_data: The data to use for the stacked tensor. If provided, fill_value will be ignored. May not be provided together with unstacked_data. + + Returns: + A tensor (with a MultiIndex in the batch dimension). + """ + from ..tensor import Tensor + + if unstacked_data is not None and stacked_data is not None: + raise ValueError("unstacked_data and stacked_data may not be provided together") + + if unstacked_data is None: + if stacked_data is not None: + dtype = stacked_data.dtype # pyright: ignore[reportUnknownVariableType] + elif dtype is None: + raise ValueError( + "dtype must be specified if neither unstacked_data nor stacked_data is provided" + ) + + if fill_value is None or stacked_data is not None: + unstacked_data = da.zeros( # pyright: ignore[reportUnknownVariableType] + shape=tuple(unstacked_shape.values()), dtype=dtype + ) + else: + unstacked_data = da.full( # pyright: ignore[reportUnknownVariableType] + shape=tuple(unstacked_shape.values()), + fill_value=fill_value, + dtype=dtype, + ) + + assert isinstance(unstacked_data, da.Array) + elif dtype is not None: + unstacked_data = unstacked_data.astype(dtype) # pyright: ignore[reportUnknownVariableType] + assert isinstance(unstacked_data, (np.ndarray, da.Array)) + else: + dtype = unstacked_data.dtype # pyright: ignore[reportUnknownVariableType] + assert isinstance(dtype, np.dtype) + + if isinstance(unstacked_data, da.Array): + tensor = Tensor.from_array(unstacked_data, dims=tuple(unstacked_shape.keys())) + elif isinstance(unstacked_data, np.ndarray): + assert is_ndarray(unstacked_data) + tensor = Tensor.from_numpy(unstacked_data, dims=tuple(unstacked_shape.keys())) + else: + assert_never(unstacked_data) + + stacked_tensor = tensor.transpose( + transpose_to, extra_dims="stack", missing_dims="expand" + ) + if stacked_data is not None: + if stacked_data.shape != stacked_tensor.data.shape: + raise ValueError( + f"stacked_data shape {stacked_data.shape} does not match the stacked tensor shape {stacked_tensor.data.shape}" + ) + stacked_tensor.data.data = stacked_data + + return stacked_tensor + + +def restore_batch_multi_index( + inputs: PerMember[Tensor | None], outputs: PerMember[Tensor | None] +) -> PerMember[Tensor | None]: + """Restore the first batch multi-index found in the inputs to all outputs with batch dimension.""" + for tensor in inputs.values(): + if tensor is None: + continue + + idx = tensor.data.indexes.get(AxisId("batch")) # pyright: ignore[reportUnknownVariableType] + if isinstance(idx, MultiIndex): + outputs = { + k: v.assign_batch_multi_index(idx) + if v is not None and AxisId("batch") in v.dims + else v + for k, v in outputs.items() + } + break + + return outputs + + +def get_unstacked_dim_order( + stacked_order: Sequence[AxisId], + *, + unstacked_reference_order: Sequence[AxisId] | None, + stacked_dims: Sequence[AxisId] | None, +) -> list[AxisId]: + if not stacked_dims and unstacked_reference_order is None: + return list(stacked_order) + if unstacked_reference_order is not None and list(stacked_order) == [ + d for d in unstacked_reference_order if d in stacked_order + ]: + # no differences in non-stacking axes, so we can preserve the unstacked axis order + return [ + d + for d in unstacked_reference_order + if d in stacked_order or (stacked_dims is not None and d in stacked_dims) + ] + elif stacked_dims is None: + # no stacked dims provided, so we can just return the stacked_out_order + return list(stacked_order) + elif BATCH_AXIS_ID not in stacked_order: + raise ValueError( + f"stacked_order={stacked_order} does not contain a batch axis, but stacked_dims={stacked_dims} are provided" + ) + else: + # unstack batch axis in its place + ret = list(stacked_order) + b_idx = ret.index(AxisId("batch")) + ret[b_idx : b_idx + 1] = ( + [ + d for d in unstacked_reference_order or () if d in stacked_dims + ] # keep unstacked axis order from reference + + [ + d + for d in stacked_dims + if unstacked_reference_order is None + or d not in unstacked_reference_order + ] # add any remaining stacked dims that are not in the unstacked reference order + ) + return ret + + +def get_unstacked_shape( + stacked_shape: PerAxis[int], + *, + unstacked_dim_order: Sequence[AxisId] | None, + unstacked_batch_shape: PerAxis[int] | None, +) -> PerAxis[int]: + """Combine a shape with an optional unstacked batch shape and apply an optional unstacked dim order to get the final unstacked shape. + + Returns `stacked_shape` if both `unstacked_dim_order` and `unstacked_batch_shape` are None. + """ + unstacked_shape_unordered = {**stacked_shape, **(unstacked_batch_shape or {})} + + if unstacked_dim_order is not None: + assert all(a in unstacked_dim_order for a in unstacked_shape_unordered), ( + f"unstacked_dim_order={unstacked_dim_order} does not contain all axes of the sample shape {stacked_shape} and unstacked sample batch shape {unstacked_batch_shape}" + ) + order = unstacked_dim_order + else: + # if no explicit unstacked dim order is provided we insert the unstacked batch axes at the position of the batch axis in the sample shape + order = list(stacked_shape) + if unstacked_batch_shape: + assert BATCH_AXIS_ID in stacked_shape, ( + f"unstacked sample batch shape {unstacked_batch_shape} is provided but the sample shape {stacked_shape} does not contain a batch axis" + ) + b_idx = order.index(BATCH_AXIS_ID) + order[b_idx : b_idx + 1] = list(unstacked_batch_shape) + + assert all(a in order for a in unstacked_shape_unordered), ( + f"unstacked_dim_order={unstacked_dim_order} does not contain all axes of the sample shape {stacked_shape} and unstacked sample batch shape {unstacked_batch_shape}" + ) + return {a: unstacked_shape_unordered[a] for a in order} diff --git a/src/bioimageio/core/utils/_resolve_per_member_key.py b/src/bioimageio/core/utils/_resolve_per_member_key.py new file mode 100644 index 00000000..463551c1 --- /dev/null +++ b/src/bioimageio/core/utils/_resolve_per_member_key.py @@ -0,0 +1,54 @@ +from __future__ import annotations + +from collections.abc import Collection +from typing import TypeVar + +from typing_extensions import assert_never + +from ..axis import AxisId, PerAxis +from ..common import MemberId, PerMember + +T_pm = TypeVar("T_pm") +T_pa = TypeVar("T_pa") + + +def resolve_per_member_var( + var: PerMember[T_pm | PerAxis[T_pa]] | PerAxis[T_pa], + /, + *, + valid_member_index_types: tuple[type[T_pm], ...], + valid_axis_index_types: tuple[type[T_pa], ...], + members: Collection[MemberId], +): + """Resolve a key that may be a per-member mapping or a per-axis mapping into a per-member mapping. + + If the key is a per-axis mapping, it will be expanded into a per-member mapping + with the same per-axis mapping for each member in `members`. + """ + + per_member_key: PerMember[T_pm | PerAxis[T_pa]] = {} + per_axis_key: PerAxis[T_pa] = {} + for k, v in var.items(): + if isinstance(k, MemberId): + if not isinstance(v, valid_member_index_types): + raise TypeError( + f"Per-member indexing only supports value types {valid_member_index_types}, but got {v} of type {type(v)})" + ) + per_member_key[k] = v + elif isinstance(k, AxisId): + if not isinstance(v, valid_axis_index_types): + raise TypeError( + f"Per-axis indexing only supports value types {valid_axis_index_types}, but got {v} of type {type(v)})" + ) + per_axis_key[k] = v + else: + assert_never(k) + + if per_member_key and per_axis_key: + raise ValueError( + f"Indexing a sample with both MemberId and AxisId keys is not supported, got keys: {list(per_member_key)} and {list(per_axis_key)}" + ) + elif per_axis_key: + per_member_key = {m: per_axis_key for m in members} + + return per_member_key diff --git a/src/bioimageio/core/utils/_type_guards.py b/src/bioimageio/core/utils/_type_guards.py index 026f49c2..035da5d6 100644 --- a/src/bioimageio/core/utils/_type_guards.py +++ b/src/bioimageio/core/utils/_type_guards.py @@ -1,19 +1,19 @@ """use these type guards with caution! They widen the type to T[Any], which is not always correct.""" +from __future__ import annotations + from typing import Any from typing_extensions import TypeGuard -from bioimageio.spec._internal import type_guards - -is_dict = type_guards.is_dict -is_kwargs = type_guards.is_kwargs -is_list = type_guards.is_list -is_mapping = type_guards.is_mapping -is_ndarray = type_guards.is_ndarray -is_sequence = type_guards.is_sequence -is_tuple = type_guards.is_tuple +from bioimageio.spec._internal.type_guards import is_dict as is_dict +from bioimageio.spec._internal.type_guards import is_kwargs as is_kwargs +from bioimageio.spec._internal.type_guards import is_list as is_list +from bioimageio.spec._internal.type_guards import is_mapping as is_mapping +from bioimageio.spec._internal.type_guards import is_ndarray as is_ndarray +from bioimageio.spec._internal.type_guards import is_sequence as is_sequence +from bioimageio.spec._internal.type_guards import is_tuple as is_tuple def is_list_of_string(v: Any) -> TypeGuard[list[str]]: diff --git a/tests/test_multi_index.py b/tests/test_multi_index.py new file mode 100644 index 00000000..bac43f82 --- /dev/null +++ b/tests/test_multi_index.py @@ -0,0 +1,102 @@ +from __future__ import annotations + +from typing import Any, Literal, cast + +import dask.array as da +import numpy as np +import pytest +from numpy.typing import NDArray + +from bioimageio.core import AxisId +from bioimageio.core.utils._multi_index import MultiIndex, create_tensor_with_multiindex + +UNSTACKED_SHAPE = {AxisId("batch"): 2, AxisId("z"): 3, AxisId("x"): 4} +TRANSPOSE_TO = (AxisId("batch"), AxisId("x")) +ARRAY_BACKENDS = pytest.mark.parametrize( + "backend", + ["numpy", "dask"], +) +ArrayBackend = Literal["numpy", "dask"] + + +def _array(backend: ArrayBackend, data: NDArray[Any]) -> NDArray[Any] | da.Array: + return ( + cast(da.Array, da.from_array(data, chunks="auto")) + if backend == "dask" + else data + ) + + +@ARRAY_BACKENDS +def test_create_tensor_with_multiindex_from_unstacked_data(backend: ArrayBackend): + unstacked_data = _array( + backend, + np.arange(2 * 3 * 4).reshape(2, 3, 4), + ) + + tensor = create_tensor_with_multiindex( + UNSTACKED_SHAPE, + TRANSPOSE_TO, + unstacked_data=unstacked_data, + ) + + assert tensor.dims == TRANSPOSE_TO + assert tensor.shape == (6, 4) + np.testing.assert_array_equal( + tensor.to_numpy(), np.asarray(unstacked_data).reshape(6, 4) + ) + batch_index = tensor.data.indexes[AxisId("batch")] # pyright: ignore[reportUnknownVariableType] + assert isinstance(batch_index, MultiIndex) + assert tuple(str(level) for level in batch_index.names) == ("original_batch", "z") + + +def test_create_tensor_with_multiindex_fills_unstacked_tensor(): + tensor = create_tensor_with_multiindex( + UNSTACKED_SHAPE, + TRANSPOSE_TO, + fill_value=7, + dtype=np.dtype(np.int16), + ) + + assert tensor.dtype == "int16" + np.testing.assert_array_equal(tensor.to_numpy(), np.full((6, 4), 7, dtype=np.int16)) + + +@ARRAY_BACKENDS +def test_create_tensor_with_multiindex_from_stacked_data(backend: ArrayBackend): + stacked_data = _array( + backend, + np.arange(6 * 4, dtype=np.float32).reshape(6, 4), + ) + + tensor = create_tensor_with_multiindex( + UNSTACKED_SHAPE, + TRANSPOSE_TO, + stacked_data=stacked_data, + ) + + assert tensor.dtype == "float32" + np.testing.assert_array_equal(tensor.to_numpy(), stacked_data) + + +@ARRAY_BACKENDS +def test_create_tensor_with_multiindex_rejects_incompatible_inputs( + backend: ArrayBackend, +): + with pytest.raises(ValueError, match="may not be provided together"): + _ = create_tensor_with_multiindex( + UNSTACKED_SHAPE, + TRANSPOSE_TO, + unstacked_data=_array(backend, np.zeros((2, 3, 4))), + stacked_data=_array(backend, np.zeros((6, 4))), + ) + + with pytest.raises(ValueError, match="dtype must be specified"): + _ = create_tensor_with_multiindex(UNSTACKED_SHAPE, TRANSPOSE_TO) + + with pytest.raises(ValueError, match="does not match"): + _ = create_tensor_with_multiindex( + UNSTACKED_SHAPE, + TRANSPOSE_TO, + stacked_data=_array(backend, np.zeros((2, 3, 4))), + ) diff --git a/tests/test_prediction.py b/tests/test_prediction.py index 7484cc77..60abf176 100644 --- a/tests/test_prediction.py +++ b/tests/test_prediction.py @@ -174,7 +174,7 @@ def test_predict_z_as_batch(unet2d_nuclei_broad_model: str): def test_transpose_sample_for_model_stacks_extra_z_to_batch( unet2d_nuclei_broad_model: str, ): - from bioimageio.core._multi_index import MultiIndex + from bioimageio.core.utils._multi_index import MultiIndex model = load_model( unet2d_nuclei_broad_model, perform_io_checks=False, format_version="latest" diff --git a/tests/test_prediction_pipeline.py b/tests/test_prediction_pipeline.py index 62d38582..2f460795 100644 --- a/tests/test_prediction_pipeline.py +++ b/tests/test_prediction_pipeline.py @@ -2,7 +2,7 @@ from functools import partial from pathlib import Path -from bioimageio.core import Sample +from bioimageio.core import AxisId, Sample from bioimageio.core._resource_tests import evaluate_mismatched_elements from bioimageio.core.common import SupportedWeightsFormat from bioimageio.spec import load_description @@ -89,3 +89,52 @@ def test_prediction_pipeline_tensorflow(any_tensorflow_model: Path): def test_prediction_pipeline_keras(any_keras_model: Path): _test_prediction_pipeline(any_keras_model, "keras_hdf5") + + +def test_blockwise_prediction_with_stacked_dims(unet2d_nuclei_broad_model: Path): + from bioimageio.core import load_model_description + from bioimageio.core._prediction_pipeline import create_prediction_pipeline + from bioimageio.core.axis import AxisInfo, AxisSize + from bioimageio.core.digest_spec import ( + get_test_input_sample, + get_test_output_sample, + ) + + bio_model = load_model_description( + unet2d_nuclei_broad_model, format_version="latest" + ) + pp = create_prediction_pipeline( + bioimageio_model=bio_model, + weight_format="pytorch_state_dict", + devices=["cpu", "cpu"], + ) + + inputs = get_test_input_sample( + bio_model, + repetitions=[ + ( + 2, + AxisInfo( + AxisId("extra_dim"), type="index", size=AxisSize(min=2, max=2) + ), + [1.0, 0.8], + ) + ], + ) + outputs = pp.predict_sample_with_blocking(inputs).unstack_batch_multi_index()[ + {AxisId("extra_dim"): 0} + ] + + expected_outputs = get_test_output_sample(bio_model) + assert len(outputs.shape) == len(expected_outputs.shape) + for m in expected_outputs.members: + out = outputs.members[m] + assert out is not None + exp = expected_outputs.members[m] + mismatched_ppm, msg, error_msg = evaluate_mismatched_elements( + out, exp, rtol=0.01, atol=0.1, mismatched_tol=50_000, name=m + ) + if error_msg is not None: + raise AssertionError(error_msg) + elif mismatched_ppm > 50_000: + raise AssertionError(msg) diff --git a/tests/test_remote_backends/test_gradio.py b/tests/test_remote_backends/test_gradio.py index 5b2140f5..4f7a878a 100644 --- a/tests/test_remote_backends/test_gradio.py +++ b/tests/test_remote_backends/test_gradio.py @@ -1,14 +1,30 @@ +from __future__ import annotations + +import multiprocessing import socket import sys import time from concurrent.futures import ThreadPoolExecutor from multiprocessing import Process +from typing import Literal import pytest from loguru import logger -from bioimageio.core import Tensor +from bioimageio.core import Sample, Tensor, load_model +from bioimageio.core._description_serializer import DescriptionSerializer +from bioimageio.core.axis import AxisId, AxisInfo, AxisSize, PerAxis from bioimageio.core.common import PerMember +from bioimageio.core.digest_spec import get_test_input_sample, get_test_output_sample +from bioimageio.core.remote_backends.gradio.client import GradioPredictionPipeline +from bioimageio.core.remote_backends.gradio.serializer import ( + GradioSampleSerializer, +) +from bioimageio.core.remote_backends.gradio.server import ( + main as gradio_server_main, +) +from bioimageio.core.remote_backends.gradio.server import predict as server_predict +from bioimageio.spec import AnyModelDescr def _is_port_in_use(port: int) -> bool: @@ -17,7 +33,7 @@ def _is_port_in_use(port: int) -> bool: @pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python 3.10 or higher") -def test_gradio_backend(): +def test_gradio_adapter(): from bioimageio.core import load_model from bioimageio.core.digest_spec import get_test_input_sample from bioimageio.core.remote_backends.gradio.client import GradioModelAdapter @@ -74,3 +90,253 @@ def test_gradio_backend(): finally: server_process.terminate() server_process.join() + + +# @pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python 3.10 or higher") +# def test_gradio_pipeline(): +# from bioimageio.core import Sample, load_model +# from bioimageio.core.axis import AxisId, AxisInfo, AxisSize +# from bioimageio.core.digest_spec import ( +# get_test_input_sample, +# get_test_output_sample, +# ) +# from bioimageio.core.remote_backends.gradio.client import GradioPredictionPipeline +# from bioimageio.core.remote_backends.gradio.server import main as gradio_server_main + +# port = 7860 +# if _is_port_in_use(port): +# sock = socket.socket() +# sock.bind(("", 0)) +# _host, port = sock.getsockname() + +# server_process = Process(target=gradio_server_main, kwargs={"port": port}) +# server_process.start() + +# try: +# deadline = time.monotonic() + 30 +# while time.monotonic() < deadline: +# try: +# with socket.create_connection(("localhost", port), timeout=1): +# break +# except OSError: +# pass +# time.sleep(0.2) +# else: +# raise TimeoutError(f"gradio server did not become ready on port {port}") + +# server_url = f"http://localhost:{port}/" +# prepared: list[tuple[str, GradioPredictionPipeline, Sample, Sample]] = [] +# for model_id in ("affable-shark", "ambitious-sloth"): +# model = load_model( +# model_id, format_version="latest", perform_io_checks=False +# ) +# sample = get_test_input_sample( +# model, +# repetitions=[ +# ( +# 2, +# AxisInfo( +# id=AxisId("extra_dim"), +# type="index", +# size=AxisSize(min=2, max=2), +# ), +# [1.0, 0.8], +# ) +# ], +# ) + +# logger.debug( +# "connecting prediction pipeline to {} for {}", server_url, model_id +# ) +# pipeline = GradioPredictionPipeline(model, server=server_url) +# prepared.append((model_id, pipeline, sample, get_test_output_sample(model))) + +# # Exercise concurrent requests pooled across both loaded models. +# with ThreadPoolExecutor(max_workers=4) as executor: +# futures = [ +# ( +# model_id, +# executor.submit(pp.predict_sample_with_blocking, sample_in), +# expected_out, +# ) +# for model_id, pp, sample_in, expected_out in prepared +# ] +# for model_id, future, expected_out in futures: +# actual_out = future.result(timeout=20)[{AxisId("extra_dim"): 0}] +# assert actual_out == expected_out # refine sample comparison + +# pipeline.close() +# finally: +# server_process.terminate() +# server_process.join(timeout=20) + + +@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python 3.10 or higher") +@pytest.mark.parametrize("with_blocking", [True]) # TODO: add True +def test_gradio_pipeline(with_blocking: bool): + + port = 7861 + + def predict_direct( + model_descr: AnyModelDescr, + sample: Sample, + *, + blocksize: int + | Literal["blockwise_as_serialized"] + | PerMember[PerAxis[int]] + | None = None, + skip_preprocessing: bool = False, + skip_postprocessing: bool = False, + skip_input_padding: bool = False, + skip_output_cropping: bool = False, + batch_size: int | None = None, + ) -> Sample: + """helper to use server predict function directly without going through the gradio server process""" + serialized_model, sha256 = DescriptionSerializer.serialize_to_string_and_hash( + model_descr + ) + serialized_input_sample = GradioSampleSerializer.serialize_sample(sample) + return GradioSampleSerializer.deserialize_sample( + server_predict( + model=serialized_model, + sha256=str(sha256), + input_sample=serialized_input_sample, + blocksize=blocksize, + skip_preprocessing=skip_preprocessing, + skip_postprocessing=skip_postprocessing, + skip_input_padding=skip_input_padding, + skip_output_cropping=skip_output_cropping, + batch_size=batch_size, + ) + ) + + models: list[tuple[str, AnyModelDescr, Sample, Sample]] = [] + # for model_id in ("affable-shark", "ambitious-sloth"): + for model_id in ("ambitious-sloth",): + model = load_model(model_id, format_version="latest", perform_io_checks=False) + sample = get_test_input_sample( + model, + repetitions=[ + ( + 2, + AxisInfo( + id=AxisId("extra_dim"), + type="index", + size=AxisSize(min=2, max=2), + ), + [1.0, 0.8], + ) + ], + ) + expected_out = get_test_output_sample(model) + + direct_out = predict_direct( + model, + sample, + blocksize=10 if with_blocking else None, + batch_size=1 if with_blocking else None, + ) + direct_out = direct_out.unstack_batch_multi_index()[{AxisId("extra_dim"): 0}] + logger.info("direct prediction check for model {}", model_id) + assert direct_out.members == expected_out.members, ( + direct_out.members, + expected_out.members, + ) + models.append((model_id, model, sample, expected_out)) + + if _is_port_in_use(port): + with socket.socket() as s: + s.bind(("", 0)) + _host, port = s.getsockname() + + server_process = multiprocessing.Process( + target=gradio_server_main, kwargs={"port": port} + ) + server_process.start() + prepared: list[tuple[str, GradioPredictionPipeline, Sample, Sample]] = [] + try: + deadline = time.monotonic() + 30 + while time.monotonic() < deadline: + try: + with socket.create_connection(("localhost", port), timeout=1): + break + except OSError: + pass + time.sleep(0.2) + else: + raise TimeoutError(f"gradio server did not become ready on port {port}") + + server_url = f"http://localhost:{port}/" + for model_id, model, sample, expected_out in models: + logger.debug( + "connecting prediction pipeline to {} for {}", server_url, model_id + ) + pipeline = GradioPredictionPipeline(model, server=server_url) + server_status = pipeline.get_status() + logger.info("Server status for model {}: {}", model_id, server_status) + prepared.append((model_id, pipeline, sample, expected_out)) + + for model_id_loop, pp, sample_in, expected_out_loop in prepared: + if with_blocking: + predict = pp.predict_sample_with_blocking + else: + predict = pp.predict_sample_without_blocking + + actual_out = predict(sample_in).unstack_batch_multi_index()[ + {AxisId("extra_dim"): 0} + ] + logger.info( + "comparing actual output to expected output for model {}", + model_id_loop, + ) + assert actual_out.members == expected_out_loop.members, ( + actual_out.members, + expected_out_loop.members, + ) + + executor = ThreadPoolExecutor(max_workers=4) + try: + futures = [ + ( + model_id_loop, + ( + executor.submit(pp.predict_sample_with_blocking, sample_in) + if with_blocking + else executor.submit( + pp.predict_sample_without_blocking, sample_in + ) + ), + expected_out_loop, + ) + for model_id_loop, pp, sample_in, expected_out_loop in prepared + ] + for model_id_loop, future, expected_out_loop in futures: + actual_out = future.result(timeout=20).unstack_batch_multi_index()[ + {AxisId("extra_dim"): 0} + ] + assert actual_out.members == expected_out_loop.members + + finally: + executor.shutdown(wait=False, cancel_futures=True) + finally: + logger.info("Closing prediction pipelines before shutting down gradio server") + for _, pp, _, _ in prepared: + try: + pp.close() + except Exception: + logger.exception("Failed to close prediction pipeline") + + logger.info("Terminating gradio server process on port {}", port) + if server_process.is_alive(): + server_process.terminate() + server_process.join(timeout=10) + + if server_process.is_alive(): + logger.warning( + "Gradio server on port {} did not exit cleanly; force killing.", port + ) + server_process.kill() + server_process.join(timeout=10) + + assert not server_process.is_alive(), "gradio server still alive after shutdown" + logger.info("gradio server exited with code {}", server_process.exitcode) diff --git a/tests/test_stat_serialization.py b/tests/test_stat_serialization.py new file mode 100644 index 00000000..c42e3484 --- /dev/null +++ b/tests/test_stat_serialization.py @@ -0,0 +1,80 @@ +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any, Literal, cast + +import dask.array as da +import numpy as np +import pytest +import xarray as xr +from numpy.typing import NDArray + +from bioimageio.core.axis import AxisId +from bioimageio.core.common import MemberId +from bioimageio.core.io import load_stat, save_stat, serialize_stat +from bioimageio.core.stat_measures import DatasetMean, SampleQuantile, Stat +from bioimageio.core.tensor import Tensor + +ArrayBackend = Literal["numpy", "dask"] +ARRAY_BACKENDS = pytest.mark.parametrize("backend", ["numpy", "dask"]) + + +def _array(backend: ArrayBackend, data: NDArray[Any]) -> NDArray[Any] | da.Array: + return ( + cast(da.Array, da.from_array(data, chunks="auto")) + if backend == "dask" + else data + ) + + +def _make_stat_with_tensor_value(backend: ArrayBackend) -> Stat: + member_id = MemberId("test_tensor") + mean_measure = DatasetMean(member_id=member_id, axes=(AxisId("batch"),)) + quantile_measure = SampleQuantile(member_id=member_id, q=0.5) + + tensor_value = Tensor( + _array(backend, np.arange(6, dtype=np.float32).reshape(2, 3)), + dims=(AxisId("x"), AxisId("y")), + ) + float_value = 3.14 + + return {mean_measure: tensor_value, quantile_measure: float_value} + + +@ARRAY_BACKENDS +def test_serialize_stat_with_tensor_value(backend: ArrayBackend): + stat = _make_stat_with_tensor_value(backend) + + serialized = serialize_stat(stat) + # should be plain JSON-compatible data, e.g. round-trippable through json + json_str = json.dumps(serialized) + assert json.loads(json_str) == serialized + + loaded = load_stat(serialized) + assert loaded.keys() == stat.keys() + for measure, expected_value in stat.items(): + actual_value = loaded[measure] + if isinstance(expected_value, Tensor): + assert isinstance(actual_value, Tensor) + xr.testing.assert_allclose(expected_value.data, actual_value.data) + else: + assert actual_value == expected_value + + +@ARRAY_BACKENDS +def test_save_and_load_stat_with_tensor_value(backend: ArrayBackend, tmp_path: Path): + stat = _make_stat_with_tensor_value(backend) + path = tmp_path / "stat.json" + + save_stat(stat, path) + loaded = load_stat(path) + + assert loaded.keys() == stat.keys() + for measure, expected_value in stat.items(): + actual_value = loaded[measure] + if isinstance(expected_value, Tensor): + assert isinstance(actual_value, Tensor) + xr.testing.assert_allclose(expected_value.data, actual_value.data) + else: + assert actual_value == expected_value diff --git a/tests/test_tensor.py b/tests/test_tensor.py index b7126c70..04c1b962 100644 --- a/tests/test_tensor.py +++ b/tests/test_tensor.py @@ -67,7 +67,7 @@ def test_shape_attributes(): def test_transpose_stack_extra_dims_to_batch_creates_multi_index(): - from bioimageio.core._multi_index import MultiIndex + from bioimageio.core.utils._multi_index import MultiIndex tensor = Tensor.from_numpy( np.arange(2 * 3 * 4).reshape(2, 3, 4), From e9787ab119fb7f4ac40b8eb88e04e53e1c3073be Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 15 Sep 2026 11:20:28 +0200 Subject: [PATCH 26/85] gradio prediction step timeout and multi-index gradio tests --- src/bioimageio/core/_sample_serializer.py | 5 +- .../core/remote_backends/gradio/client.py | 186 +++++++++++++++--- .../core/remote_backends/gradio/server.py | 10 +- src/bioimageio/core/sample.py | 6 +- src/bioimageio/core/utils/_multi_index.py | 4 +- tests/conftest.py | 6 + tests/test_description_serializer.py | 10 +- tests/test_remote_backends/test_gradio.py | 140 +++++++------ 8 files changed, 256 insertions(+), 111 deletions(-) diff --git a/src/bioimageio/core/_sample_serializer.py b/src/bioimageio/core/_sample_serializer.py index 2269e419..217f376b 100644 --- a/src/bioimageio/core/_sample_serializer.py +++ b/src/bioimageio/core/_sample_serializer.py @@ -65,12 +65,13 @@ def serialize_sample_with_fixed_blocking( pad_mode: PadMode | PerMember[PadMode] = "symmetric", ) -> Iterable[SerializedSampleBlockType]: - _n_blocks, input_blocks = sample.split_into_blocks( + _n_blocks, blocks = sample.split_into_blocks( block_shapes=block_shapes, halo=halo, pad_mode=pad_mode, ) - for block in input_blocks: + assert _n_blocks > 0, "sample has no blocks" + for block in blocks: yield cls.serialize_sample_block(block) @staticmethod diff --git a/src/bioimageio/core/remote_backends/gradio/client.py b/src/bioimageio/core/remote_backends/gradio/client.py index e9f3402a..2761c9c7 100644 --- a/src/bioimageio/core/remote_backends/gradio/client.py +++ b/src/bioimageio/core/remote_backends/gradio/client.py @@ -1,10 +1,11 @@ from __future__ import annotations -from collections.abc import Iterable, Mapping +import time +from collections.abc import Iterable, Iterator, Mapping from types import MappingProxyType -from typing import Literal +from typing import Any, Generic, Literal, TypeVar, overload -from gradio_client import Client +from gradio_client.client import Client, Job from loguru import logger from bioimageio.spec import AnyModelDescr, ValidationSummary @@ -19,6 +20,7 @@ from ...io import JsonValue from ...sample import Sample, SampleBlock from ...stat_measures import Measure, MeasureValue +from ...utils._type_guards import is_sequence from .serializer import GradioSampleSerializer SerializedSampleBlock = dict[str, JsonValue] @@ -107,6 +109,85 @@ def test(self) -> ValidationSummary | None: return None +T = TypeVar("T") + + +class _DummyJob(Generic[T]): + def __init__(self, result: T): + super().__init__() + self._result = result + + def result(self, timeout: float | None = None) -> T: + return self._result + + def __iter__(self): + yield self._result + + +class _DummyJobIterable(Generic[T]): + def __init__(self, result: Iterable[T]): + assert isinstance(result, Iterable), "result must be iterable" + super().__init__() + self._count = 0 + self._result = result + self._outputs: list[T] = [] + self._done = False + + def status(self) -> dict[Literal["status"], str]: + return {"status": "dummy done" if self._done else "dummy running"} + + def result(self, timeout: float | None = None) -> T: + self._outputs = list(self) + return self._outputs[-1] + + def done(self) -> bool: + return self._done + + def cancel(self) -> bool: + self._result = () + return True + + def outputs(self) -> list[T]: + return list(self._outputs) + + def __iter__(self) -> Iterator[T]: + for res in self._outputs: + yield res + + for res in self._result: + self._outputs.append(res) + yield res + self._done = True + + +class _DummyClient: + @overload + def submit( + self, api_name: Literal["/get_status"], **kwargs: Any + ) -> _DummyJob[dict[Literal["message"], str]]: ... + @overload + def submit( + self, api_name: Literal["/predict"], **kwargs: Any + ) -> _DummyJobIterable[SerializedSampleBlock]: ... + def submit( + self, api_name: Literal["/get_status", "/predict"], **kwargs: Any + ) -> ( + _DummyJob[dict[Literal["message"], str]] + | _DummyJobIterable[SerializedSampleBlock] + ): + if api_name == "/get_status": + return _DummyJob({"message": "dummy client"}) + elif api_name == "/predict": + from .server import predict as server_predict + + return _DummyJobIterable(server_predict(**kwargs)) + else: + raise ValueError(f"Unknown api_name: {api_name}") + + def close(self): + pass + + class GradioPredictionPipeline(RemotePredictionPipeline): """Prediction pipeline to use the bioimage-io-gradio-runner as a fully remote prediction pipeline.""" @@ -114,7 +195,7 @@ def __init__( self, model_description: AnyModelDescr, *, - server: str | None = None, + server: str | None | Literal["run_in_client_process"] = None, precomputed_statistics: Mapping[Measure, MeasureValue] = MappingProxyType({}), default_blocksize_parameter: BlocksizeParameter = 10, default_batch_size: int = 1, @@ -139,7 +220,12 @@ def __init__( default_blocksize_parameter=default_blocksize_parameter, default_batch_size=default_batch_size, ) - self._client = Client(self.server, httpx_kwargs={"timeout": 60}) + if server == "run_in_client_process": + # run via dummy client (intended for debugging) + self._client = _DummyClient() + else: + self._client = Client(self.server, httpx_kwargs={"timeout": 60}) + self._serialized_model, self._sha256 = ( DescriptionSerializer.serialize_to_string_and_hash(model_description) ) @@ -172,8 +258,8 @@ def predict_sample_block( self._client, self._serialized_model, self._sha256, - serialized_input_sample=self._serializer.serialize_sample( - sample_block.as_sample() + serialized_input_sample=( + self._serializer.serialize_sample_block(sample_block), ), blocksize=None, skip_preprocessing=skip_preprocessing, @@ -274,7 +360,7 @@ def get_status(self) -> dict[Literal["message"], str]: def _call_predict_api( - client: Client, + client: Client | _DummyClient, serialized_model: str, sha256: str, serialized_input_sample: Iterable[SerializedSampleBlock], @@ -288,12 +374,12 @@ def _call_predict_api( skip_output_cropping: bool, batch_size: int | None, ) -> Iterable[SerializedSampleBlock]: - def submit(model: str): + def submit(model: str, input_sample: list[SerializedSampleBlock]): return client.submit( api_name="/predict", model=model, sha256=sha256, - input_sample=serialized_input_sample, + input_sample=input_sample, blocksize={ str(k): {str(kk): vv for kk, vv in v.items()} for k, v in blocksize.items() @@ -307,28 +393,64 @@ def submit(model: str): batch_size=batch_size, ) + def iterate_job( + job: _DummyJobIterable[SerializedSampleBlock] | Job, + ) -> Iterable[SerializedSampleBlock]: + seconds_between_polls: float = 1.0 + timeout_seconds: float = 300.0 + output_idx = 0 + started = time.time() + seconds_to_sleep = 0.01 # first sleep is shorter for faster initial responses + while not job.done(): + if (time.time() - started) > timeout_seconds: + _ = job.cancel() + raise TimeoutError( + f"Timeout of {timeout_seconds} seconds exceeded while waiting for prediciton job {job.status()}." + ) + + logger.debug("Waiting for submitted prediction job") + time.sleep(seconds_to_sleep) + seconds_to_sleep = seconds_between_polls + if isinstance(job, _DummyJobIterable): + # dummy job runs in the same thread, so we need to call result to let it run + _ = job.result(timeout_seconds) + + outs: Any = job.outputs() # pyright: ignore[reportUnknownVariableType] + assert is_sequence(outs) + outs = outs[output_idx:] + for out in outs: + yield out + output_idx += 1 + + # cannot json serialize any iterable, so we need to convert to a list first + # this also makes it trivial to not exhaust the iterable in case we need to retry with model upload + serialized_input_sample_list = list(serialized_input_sample) + try_with_model_upload = True - # try: - # job = submit("") - # return list(job) # TOOD: reactivate yielding - # # for block in job: # pyright: ignore[reportUnknownVariableType] - # # yield block # pyright: ignore[reportReturnType] - # # # we got one response, so the model cache was hit... - # # try_with_model_upload = False - # except Exception as e: - # # A raised exception on the server seems to simply return an empty response sequence, - # # so this except is likely not triggered at all. - # # Below we retry on empty return value, too. - # if try_with_model_upload: - # logger.warning( - # "Failed to submit job without model upload, trying with model upload, error was: {}", - # e, - # ) - # else: - # raise + try: + job = submit("", input_sample=serialized_input_sample_list) + for block in iterate_job(job): + yield block + # we got one response, so the model cache was hit... + try_with_model_upload = False + except Exception as e: + # A raised exception on the server seems to simply return an empty response sequence, + # so this except is likely not triggered at all. + # Below we retry on empty return value, too. + if try_with_model_upload: + logger.warning( + "Failed to submit job without model upload, trying with model upload, error was: {}", + e, + ) + else: + # An exception occured somehow after successfully obtaining the first block of the result + # even though the server should only return a single block as we only submitted a single block. + raise if try_with_model_upload: - job = submit(serialized_model) - return list(job) # TOOD: reactivate yielding - # for block in job: # pyright: ignore[reportUnknownVariableType] - # yield block # pyright: ignore[reportReturnType] + job = submit( + serialized_model, + input_sample=serialized_input_sample_list, + ) + for block in iterate_job(job): + yield block diff --git a/src/bioimageio/core/remote_backends/gradio/server.py b/src/bioimageio/core/remote_backends/gradio/server.py index 6fdbc3cc..9d2c4628 100644 --- a/src/bioimageio/core/remote_backends/gradio/server.py +++ b/src/bioimageio/core/remote_backends/gradio/server.py @@ -34,7 +34,7 @@ def GPU(func: Any): logger.enable("bioimageio") -app = gr.Server() +app = gr.Server(version="0.1") @app.api(name="predict") # pyright: ignore[reportUntypedFunctionDecorator] @@ -250,11 +250,11 @@ def root(): def main(port: int | None = None) -> str: logger.info("Starting bioimageio.core gradio server on port {}", port) _app, local_url, _share_url = app.launch( - mcp_server=False, - show_error=True, - server_name="localhost", + debug=True, + mcp_server=True, + server_name="0.0.0.0", server_port=port, - debug=False, + show_error=True, ) return local_url diff --git a/src/bioimageio/core/sample.py b/src/bioimageio/core/sample.py index 1306bb34..a264dc84 100644 --- a/src/bioimageio/core/sample.py +++ b/src/bioimageio/core/sample.py @@ -184,9 +184,7 @@ def unstacked_shape(self) -> PerMember[PerAxis[int]]: unstacked_dim_order=self.unstacked_dim_order[m] if self.unstacked_dim_order is not None else None, - unstacked_batch_shape=self.unstacked_batch_shape[m] - if self.unstacked_batch_shape is not None - else None, + unstacked_batch_shape=self.unstacked_batch_shape, ) for m, sh in self.shape.items() } @@ -522,7 +520,7 @@ def unstack_batch_multi_index( f"Different batch lengths after unstacking: {batch_lengths}" ) - stat: Stat = { + stat: Stat = { # TODO: update stat calculations to maintain any multi-index batch index. k: v.unstack_batch_multi_index(errors="ignore", unstacked_dim_order=None) if isinstance(v, Tensor) else float(v) diff --git a/src/bioimageio/core/utils/_multi_index.py b/src/bioimageio/core/utils/_multi_index.py index 4677ac5f..4deec4a4 100644 --- a/src/bioimageio/core/utils/_multi_index.py +++ b/src/bioimageio/core/utils/_multi_index.py @@ -9,9 +9,7 @@ from numpy.typing import NDArray from typing_extensions import assert_never -from bioimageio.core.axis import BATCH_AXIS_ID, AxisId - -from ..axis import AxisId, PerAxis +from ..axis import BATCH_AXIS_ID, AxisId, PerAxis from ..common import PerMember from ._type_guards import is_ndarray diff --git a/tests/conftest.py b/tests/conftest.py index df9b79ea..ef48ec2a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -275,6 +275,12 @@ def unet2d_expand_output_shape(request: FixtureRequest): return MODEL_SOURCES[request.param] +# written as model group to automatically skip on missing torch +@fixture(scope="session", params=[] if skip_torch else ["unet2d_multi_tensor"]) +def unet2d_multi_tensor(request: FixtureRequest): + return MODEL_SOURCES[request.param] + + # written as model group to automatically skip on missing torch @fixture(scope="session", params=[] if skip_torch else ["unet2d_fixed_shape"]) def unet2d_fixed_shape(request: FixtureRequest): diff --git a/tests/test_description_serializer.py b/tests/test_description_serializer.py index 98c4127e..f636e7d1 100644 --- a/tests/test_description_serializer.py +++ b/tests/test_description_serializer.py @@ -4,16 +4,12 @@ def test_description_serializer_roundtrip(unet2d_nuclei_broad_model: str): rd1 = load_model_description(unet2d_nuclei_broad_model, perform_io_checks=False) - serialized1, hash1 = DescriptionSerializer.serialize_to_string_and_hash(rd1) - assert len(serialized1) > 2083 + serialized = DescriptionSerializer.serialize_to_string(rd1) + assert len(serialized) > 2083 - rd2 = DescriptionSerializer.deserialize_from_string(serialized1) + rd2 = DescriptionSerializer.deserialize_from_string(serialized) assert rd2.name == rd1.name assert rd2.type == rd1.type assert rd2.format_version == rd1.format_version for k, v in rd1: assert str(getattr(rd2, k)) == str(v), (k, v, getattr(rd2, k)) - - serialized2, hash2 = DescriptionSerializer.serialize_to_string_and_hash(rd2) - assert serialized2 == serialized1 - assert hash2 == hash1 diff --git a/tests/test_remote_backends/test_gradio.py b/tests/test_remote_backends/test_gradio.py index 4f7a878a..1372ba6e 100644 --- a/tests/test_remote_backends/test_gradio.py +++ b/tests/test_remote_backends/test_gradio.py @@ -172,11 +172,8 @@ def test_gradio_adapter(): @pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python 3.10 or higher") -@pytest.mark.parametrize("with_blocking", [True]) # TODO: add True -def test_gradio_pipeline(with_blocking: bool): - - port = 7861 - +@pytest.mark.parametrize("with_blocking", [False, True]) +def test_gradio_pipeline_direct(with_blocking: bool): def predict_direct( model_descr: AnyModelDescr, sample: Sample, @@ -210,9 +207,7 @@ def predict_direct( ) ) - models: list[tuple[str, AnyModelDescr, Sample, Sample]] = [] - # for model_id in ("affable-shark", "ambitious-sloth"): - for model_id in ("ambitious-sloth",): + for model_id in ("affable-shark", "ambitious-sloth"): model = load_model(model_id, format_version="latest", perform_io_checks=False) sample = get_test_input_sample( model, @@ -242,63 +237,87 @@ def predict_direct( direct_out.members, expected_out.members, ) - models.append((model_id, model, sample, expected_out)) - if _is_port_in_use(port): - with socket.socket() as s: - s.bind(("", 0)) - _host, port = s.getsockname() - server_process = multiprocessing.Process( - target=gradio_server_main, kwargs={"port": port} - ) - server_process.start() +@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python 3.10 or higher") +@pytest.mark.parametrize("with_blocking", [False, True]) +@pytest.mark.parametrize("with_server", [False, True]) +def test_gradio_pipeline( + with_blocking: bool, + with_server: bool, + shape_change_model: str, + unet2d_expand_output_shape: str, + unet2d_multi_tensor: str, +): + port = 7860 + + if with_server: + if _is_port_in_use(port): + with socket.socket() as s: + s.bind(("", 0)) + _host, port = s.getsockname() + + server_process = multiprocessing.Process( + target=gradio_server_main, kwargs={"port": port} + ) + server_process.start() + else: + server_process = None + prepared: list[tuple[str, GradioPredictionPipeline, Sample, Sample]] = [] try: - deadline = time.monotonic() + 30 - while time.monotonic() < deadline: - try: - with socket.create_connection(("localhost", port), timeout=1): - break - except OSError: - pass - time.sleep(0.2) + if with_server: + deadline = time.monotonic() + 30 + while time.monotonic() < deadline: + try: + with socket.create_connection(("localhost", port), timeout=1): + break + except OSError: + pass + time.sleep(0.2) + else: + raise TimeoutError(f"gradio server did not become ready on port {port}") + + server_url = f"http://localhost:{port}/" else: - raise TimeoutError(f"gradio server did not become ready on port {port}") + server_url = "run_in_client_process" - server_url = f"http://localhost:{port}/" - for model_id, model, sample, expected_out in models: + for model_source in ( + shape_change_model, + unet2d_expand_output_shape, + unet2d_multi_tensor, + ): + model = load_model( + model_source, format_version="latest", perform_io_checks=False + ) + input_sample = get_test_input_sample( + model, + repetitions=[ + ( + 2, + AxisInfo( + id=AxisId("extra_dim"), + type="index", + size=AxisSize(min=2, max=2), + ), + [1.0, 0.8], + ) + ], + ) + expected_out = get_test_output_sample(model) logger.debug( - "connecting prediction pipeline to {} for {}", server_url, model_id + "connecting prediction pipeline to {} for {}", server_url, model_source ) pipeline = GradioPredictionPipeline(model, server=server_url) server_status = pipeline.get_status() - logger.info("Server status for model {}: {}", model_id, server_status) - prepared.append((model_id, pipeline, sample, expected_out)) - - for model_id_loop, pp, sample_in, expected_out_loop in prepared: - if with_blocking: - predict = pp.predict_sample_with_blocking - else: - predict = pp.predict_sample_without_blocking - - actual_out = predict(sample_in).unstack_batch_multi_index()[ - {AxisId("extra_dim"): 0} - ] - logger.info( - "comparing actual output to expected output for model {}", - model_id_loop, - ) - assert actual_out.members == expected_out_loop.members, ( - actual_out.members, - expected_out_loop.members, - ) + logger.info("Server status for model {}: {}", model_source, server_status) + prepared.append((model_source, pipeline, input_sample, expected_out)) executor = ThreadPoolExecutor(max_workers=4) try: futures = [ ( - model_id_loop, + model_source, ( executor.submit(pp.predict_sample_with_blocking, sample_in) if with_blocking @@ -308,9 +327,9 @@ def predict_direct( ), expected_out_loop, ) - for model_id_loop, pp, sample_in, expected_out_loop in prepared + for model_source, pp, sample_in, expected_out_loop in prepared ] - for model_id_loop, future, expected_out_loop in futures: + for _model_source, future, expected_out_loop in futures: actual_out = future.result(timeout=20).unstack_batch_multi_index()[ {AxisId("extra_dim"): 0} ] @@ -320,23 +339,28 @@ def predict_direct( executor.shutdown(wait=False, cancel_futures=True) finally: logger.info("Closing prediction pipelines before shutting down gradio server") - for _, pp, _, _ in prepared: + for model_source, pp, _, _ in prepared: try: pp.close() except Exception: - logger.exception("Failed to close prediction pipeline") + logger.exception( + "Failed to close prediction pipeline for {}", model_source + ) logger.info("Terminating gradio server process on port {}", port) - if server_process.is_alive(): + if server_process is not None and server_process.is_alive(): server_process.terminate() server_process.join(timeout=10) - if server_process.is_alive(): + if server_process is not None and server_process.is_alive(): logger.warning( "Gradio server on port {} did not exit cleanly; force killing.", port ) server_process.kill() server_process.join(timeout=10) - assert not server_process.is_alive(), "gradio server still alive after shutdown" - logger.info("gradio server exited with code {}", server_process.exitcode) + assert server_process is None or not server_process.is_alive(), ( + "gradio server still alive after shutdown" + ) + if server_process is not None: + logger.info("gradio server exited with code {}", server_process.exitcode) From ca31fea7a7d13ce082e771acfba68d1720dd1cd4 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 15 Sep 2026 22:39:04 +0200 Subject: [PATCH 27/85] limit block shape to shape instead of failing --- src/bioimageio/core/block_meta.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py index d5a6c4ff..72c4026f 100644 --- a/src/bioimageio/core/block_meta.py +++ b/src/bioimageio/core/block_meta.py @@ -299,15 +299,20 @@ def split_shape_into_blocks( f"unknown axes in block_shape: {unknown_axes} for shape {shape}" ) - if any(shape.get(a, 1) < block_shape[a] for a in block_shape): - # TODO: allow larger blockshape - raise ValueError(f"shape {shape} is smaller than block shape {block_shape}") + if large_block_dims := [ + (a, b, s) for a in block_shape if (s := shape.get(a, 1)) < (b := block_shape[a]) + ]: + logger.warning( + "Block shape dimensions {} larger than shape dimensions {} are limiting the shape size", + {a: b for a, b, _s in large_block_dims}, + {a: s for a, _b, s in large_block_dims}, + ) + block_shape = {a: min(s, block_shape.get(a, s)) for a, s in shape.items()} assert all(a in shape for a in halo), (tuple(shape), set(halo)) # fill in default halo (0) and block axis length (from tensor shape) halo = {a: Halo.create(halo.get(a, 0)) for a in shape} - block_shape = {a: block_shape.get(a, s) for a, s in shape.items()} if stride is None: stride = {} From a6d3046ea038fee0e2be9865d9d66006345a20a2 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 15 Sep 2026 23:11:17 +0200 Subject: [PATCH 28/85] remove stride arguments --- src/bioimageio/core/block.py | 2 -- src/bioimageio/core/block_meta.py | 12 ------------ 2 files changed, 14 deletions(-) diff --git a/src/bioimageio/core/block.py b/src/bioimageio/core/block.py index 6d6bd34f..9988c7c8 100644 --- a/src/bioimageio/core/block.py +++ b/src/bioimageio/core/block.py @@ -111,7 +111,6 @@ def split_tensor_into_blocks( block_shape: PerAxis[int], *, halo: PerAxis[HaloLike], - stride: PerAxis[int] | None = None, pad_mode: PadMode, ) -> tuple[TotalNumberOfBlocks, Generator[Block, Any, None]]: """divide a sample tensor into tensor blocks.""" @@ -129,7 +128,6 @@ def split_tensor_into_blocks( unstacked_batch_shape=unstacked_batch_shape, block_shape=block_shape, halo=halo, - stride=stride, ) return n_blocks, _block_generator(tensor, block_gen, pad_mode=pad_mode) diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py index 72c4026f..be4701ff 100644 --- a/src/bioimageio/core/block_meta.py +++ b/src/bioimageio/core/block_meta.py @@ -289,7 +289,6 @@ def split_shape_into_blocks( unstacked_batch_shape: PerAxis[int] | None, block_shape: PerAxis[int], halo: PerAxis[HaloLike], - stride: PerAxis[int] | None = None, unstacked_dim_order: Sequence[AxisId] | None, ) -> tuple[TotalNumberOfBlocks, Generator[BlockMeta, Any, None]]: if unknown_axes := [ @@ -313,8 +312,6 @@ def split_shape_into_blocks( # fill in default halo (0) and block axis length (from tensor shape) halo = {a: Halo.create(halo.get(a, 0)) for a in shape} - if stride is None: - stride = {} inner_1d_slices: dict[AxisId, list[SliceInfo]] = {} for a, s in shape.items(): @@ -394,7 +391,6 @@ def split_multiple_shapes_into_blocks( unstacked_batch_shape: PerAxis[int] | None, unstacked_dim_order: PerMember[Sequence[AxisId] | None] | None, halo: PerMember[PerAxis[HaloLike]], - strides: PerMember[PerAxis[int]] | None = None, broadcast: bool = False, ) -> tuple[TotalNumberOfBlocks, Iterable[PerMember[BlockMeta]]]: if unknown_blocks := [t for t in block_shapes if t not in shapes]: @@ -419,13 +415,6 @@ def split_multiple_shapes_into_blocks( f"`halo` specified for tensors without block shape: {extra_halo}." ) - if strides is None: - strides = {} - - assert not (unknown_block := [t for t in strides if t not in block_shapes]), ( - f"`stride` specified for tensors without block shape: {unknown_block}" - ) - blocks: dict[MemberId, Iterable[BlockMeta]] = {} n_blocks: dict[MemberId, TotalNumberOfBlocks] = {} for t in block_shapes: @@ -437,7 +426,6 @@ def split_multiple_shapes_into_blocks( else None, block_shape=block_shapes[t], halo=halo.get(t, {}), - stride=strides.get(t), ) assert n_blocks[t] > 0, n_blocks From 12c2aeab759344b295e244c788dbecb11c5557e7 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Wed, 16 Sep 2026 21:38:34 +0200 Subject: [PATCH 29/85] add test_save_sample --- src/bioimageio/core/block_meta.py | 3 +-- tests/test_io.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py index be4701ff..4fbbe58e 100644 --- a/src/bioimageio/core/block_meta.py +++ b/src/bioimageio/core/block_meta.py @@ -316,10 +316,9 @@ def split_shape_into_blocks( inner_1d_slices: dict[AxisId, list[SliceInfo]] = {} for a, s in shape.items(): inner_size = block_shape[a] - sum(halo[a]) - stride_1d = stride.get(a, inner_size) inner_1d_slices[a] = [ SliceInfo(min(p, s - inner_size), min(p + inner_size, s)) - for p in range(0, s, stride_1d) + for p in range(0, s, inner_size) ] n_blocks = prod(map(len, inner_1d_slices.values())) diff --git a/tests/test_io.py b/tests/test_io.py index ecb61fb2..bfb385c1 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -50,6 +50,31 @@ def test_tensor_io_numpy( assert (actual == expected).to_numpy().all() +@pytest.mark.parametrize("suffix", [".png", ".tiff", ".zarr"]) +def test_save_sample(tmp_path: Path, suffix: str): + from bioimageio.core import Sample, Tensor + from bioimageio.core.common import MemberId + from bioimageio.core.io import load_tensor, save_sample + + members = { + MemberId("raw"): np.arange(6, dtype=np.uint8).reshape(2, 3), + MemberId("mask"): np.ones((2, 3), dtype=np.uint8), + } + sample = Sample( + members={ + m: Tensor.from_numpy(data, dims=("y", "x")) for m, data in members.items() + }, + stat={}, + id="sample", + ) + + save_sample(tmp_path / f"{sample.id}_{{member_id}}{suffix}", sample) + + for member_id, expected in members.items(): + actual = load_tensor(tmp_path / f"sample_{member_id}{suffix}") + np.testing.assert_array_equal(actual, expected) + + @pytest.mark.parametrize( "name", [ From d4f2ada4fd69824342eeb1ce79c1c8c1f59d68d3 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Wed, 7 Oct 2026 11:35:52 +0200 Subject: [PATCH 30/85] WIP lazy forward --- changelog.md | 2 +- pyproject.toml | 2 +- src/bioimageio/core/__init__.py | 4 - src/bioimageio/core/_axis_annotations.py | 5 +- src/bioimageio/core/_model_adapter.py | 212 ++- src/bioimageio/core/_op_base.py | 68 +- src/bioimageio/core/_prediction_pipeline.py | 672 ++++----- src/bioimageio/core/_resource_tests.py | 16 +- src/bioimageio/core/_sample_serializer.py | 29 +- src/bioimageio/core/_utils/__init__.py | 1 + src/bioimageio/core/_utils/chunking.py | 159 ++ .../core/{utils/_color.py => _utils/color.py} | 0 .../{utils/_compare.py => _utils/compare.py} | 0 .../{utils/_io_zarr.py => _utils/io_zarr.py} | 0 src/bioimageio/core/_utils/multi_index.py | 113 ++ .../core/_utils/resolve_per_member_key.py | 70 + .../tiff_metadata.py} | 2 +- .../_type_guards.py => _utils/type_guards.py} | 0 src/bioimageio/core/_utils/type_hints.py | 9 + .../_xml_utils.py => _utils/xml_utils.py} | 0 src/bioimageio/core/axis.py | 3 +- src/bioimageio/core/backends/keras_backend.py | 4 +- src/bioimageio/core/backends/onnx_backend.py | 6 +- .../core/backends/pytorch_backend.py | 2 +- .../core/backends/torchscript_backend.py | 6 +- src/bioimageio/core/block.py | 1082 ++++++++++++-- src/bioimageio/core/block_meta.py | 1009 +++++++------ src/bioimageio/core/cli.py | 6 +- src/bioimageio/core/common.py | 61 +- src/bioimageio/core/digest_spec.py | 403 ++---- src/bioimageio/core/io.py | 343 ++--- src/bioimageio/core/prediction.py | 28 +- src/bioimageio/core/proc_ops.py | 4 +- .../core/remote_backends/gradio/client.py | 14 +- .../core/remote_backends/gradio/serializer.py | 6 +- .../core/remote_backends/gradio/server.py | 10 +- src/bioimageio/core/sample.py | 1280 ++++++++--------- src/bioimageio/core/sample_meta.py | 287 ++++ src/bioimageio/core/tensor.py | 424 +++++- src/bioimageio/core/utils/__init__.py | 1 - src/bioimageio/core/utils/_multi_index.py | 194 --- .../core/utils/_resolve_per_member_key.py | 54 - tests/test_digest_spec.py | 48 +- tests/test_io.py | 4 +- tests/test_model_adapter.py | 38 + tests/test_multi_index.py | 2 +- tests/test_ops_cellpose.py | 2 +- tests/test_prediction.py | 3 +- tests/test_prediction_pipeline.py | 5 +- ...t_prediction_pipeline_device_management.py | 40 +- tests/test_remote_backends/test_gradio.py | 27 +- tests/test_tensor.py | 2 +- 52 files changed, 3989 insertions(+), 2773 deletions(-) create mode 100644 src/bioimageio/core/_utils/__init__.py create mode 100644 src/bioimageio/core/_utils/chunking.py rename src/bioimageio/core/{utils/_color.py => _utils/color.py} (100%) rename src/bioimageio/core/{utils/_compare.py => _utils/compare.py} (100%) rename src/bioimageio/core/{utils/_io_zarr.py => _utils/io_zarr.py} (100%) create mode 100644 src/bioimageio/core/_utils/multi_index.py create mode 100644 src/bioimageio/core/_utils/resolve_per_member_key.py rename src/bioimageio/core/{utils/_tiff_metadata.py => _utils/tiff_metadata.py} (97%) rename src/bioimageio/core/{utils/_type_guards.py => _utils/type_guards.py} (100%) create mode 100644 src/bioimageio/core/_utils/type_hints.py rename src/bioimageio/core/{utils/_xml_utils.py => _utils/xml_utils.py} (100%) create mode 100644 src/bioimageio/core/sample_meta.py delete mode 100644 src/bioimageio/core/utils/__init__.py delete mode 100644 src/bioimageio/core/utils/_multi_index.py delete mode 100644 src/bioimageio/core/utils/_resolve_per_member_key.py create mode 100644 tests/test_model_adapter.py diff --git a/changelog.md b/changelog.md index 218969dc..362bc470 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ ### 0.12.0 -- bump spec to 0.5.14.2 +- bump spec to 0.5.14.3 - support loading zarr images - dropped `is_volume` flag from `load_image()` diff --git a/pyproject.toml b/pyproject.toml index 32d54b85..c8ae6a23 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ requires-python = ">=3.9" readme = "README.md" dynamic = ["version"] dependencies = [ - "bioimageio.spec ==0.5.14.2", + "bioimageio.spec ==0.5.14.3", "imagecodecs", "imageio>=2.10", "loguru", diff --git a/src/bioimageio/core/__init__.py b/src/bioimageio/core/__init__.py index d5085264..d70788d5 100644 --- a/src/bioimageio/core/__init__.py +++ b/src/bioimageio/core/__init__.py @@ -53,7 +53,6 @@ from . import stat_measures as stat_measures from . import tensor as tensor from . import weight_converters as weight_converters -from ._prediction_pipeline import IntermediatePrediction as IntermediatePrediction from ._prediction_pipeline import PredictionPipeline as PredictionPipeline from ._prediction_pipeline import RemotePredictionPipeline as RemotePredictionPipeline from ._prediction_pipeline import ( @@ -74,13 +73,10 @@ from .axis import Axis as Axis from .axis import AxisId as AxisId from .backends import create_model_adapter as create_model_adapter -from .block_meta import BlockMeta as BlockMeta from .common import MemberId as MemberId from .prediction import predict as predict from .prediction import predict_many as predict_many from .sample import Sample as Sample -from .sample import SampleBlock as SampleBlock -from .sample import SampleBlockMeta as SampleBlockMeta from .stat_calculators import compute_dataset_measures as compute_dataset_measures from .stat_calculators import compute_measures as compute_measures from .stat_calculators import compute_sample_measures as compute_sample_measures diff --git a/src/bioimageio/core/_axis_annotations.py b/src/bioimageio/core/_axis_annotations.py index 8bd1a7e3..16062900 100644 --- a/src/bioimageio/core/_axis_annotations.py +++ b/src/bioimageio/core/_axis_annotations.py @@ -1,9 +1,10 @@ +from collections.abc import Mapping from typing import Annotated, TypeVar from ._common_annotations import PydanticMappingProxyAnnotation -from .axis import PerAxis +from .axis import AxisId _T = TypeVar("_T") -PerAxisAnno = Annotated[PerAxis[_T], PydanticMappingProxyAnnotation] +PerAxisAnno = Annotated[Mapping[AxisId, _T], PydanticMappingProxyAnnotation] """PerAxis annotated with `PydanticMappingProxyAnnotation` to be compatible with pydantic models.""" diff --git a/src/bioimageio/core/_model_adapter.py b/src/bioimageio/core/_model_adapter.py index 009e5880..a1008f9f 100644 --- a/src/bioimageio/core/_model_adapter.py +++ b/src/bioimageio/core/_model_adapter.py @@ -8,22 +8,30 @@ from typing import ( Any, Generic, + Literal, + cast, ) +import dask.array as da +from dask.delayed import Delayed, delayed from exceptiongroup import ExceptionGroup from loguru import logger from numpy.typing import NDArray -from typing_extensions import TypeVar +from typing_extensions import TypeVar, assert_never +from bioimageio.core.axis import AxisId, PerAxis from bioimageio.spec import ValidationSummary from bioimageio.spec.model import AnyModelDescr, v0_4 from ._sample_serializer import SampleSerializer, SerializedSampleBlockType from .common import PerMember -from .digest_spec import get_axes_infos, get_member_ids +from .digest_spec import ( + get_axes_infos, + get_member_ids, + get_unstacked_output_dim_orders_for_model, +) from .sample import Sample -from .tensor import Tensor -from .utils._multi_index import restore_batch_multi_index +from .tensor import Array, MultiIndex, Tensor, restore_batch_multi_index class ModelAdapter(ABC): @@ -46,7 +54,12 @@ class ModelAdapter(ABC): ``` """ - def __init__(self, model_description: AnyModelDescr, devices: Sequence[str] | None): + def __init__( + self, + model_description: AnyModelDescr, + devices: Sequence[str] | None, + computation_mode: Literal["eager", "lazy"] = "lazy", + ): super().__init__() self._model_descr = model_description self._input_ids = get_member_ids(model_description.inputs) @@ -58,13 +71,36 @@ def __init__(self, model_description: AnyModelDescr, devices: Sequence[str] | No tuple(a.id for a in get_axes_infos(t)) for t in model_description.outputs ] if isinstance(model_description, v0_4.ModelDescr): + if computation_mode == "lazy": + warnings.warn( + "'lazy' computation mode is not supported for v0.4 model descriptions. Falling back to 'eager' computation mode." + ) + computation_mode = "eager" + self._input_is_optional = [False] * len(model_description.inputs) else: self._input_is_optional = [ipt.optional for ipt in model_description.inputs] + self.computation_mode: Literal["eager", "lazy"] = computation_mode + self._devices = devices self.load() + def get_output_shape( + self, + input_shape: PerMember[PerAxis[int]], + ) -> PerMember[PerAxis[int]]: + if isinstance(self._model_descr, v0_4.ModelDescr): + raise NotImplementedError( + "v0_4.ModelDescr.get_output_shape is not implemented. Please update the model description to v0_5 or later." + ) + return { + m: {d: s if isinstance(s, int) else -1 for d, s in s_per_a.items()} + for m, s_per_a in self._model_descr.get_output_tensor_sizes( + input_shape + ).items() + } + @property def model_descr(self) -> AnyModelDescr: return self._model_descr @@ -74,14 +110,22 @@ def load(self) -> None: self._loaded = True @abstractmethod - def forward(self, inputs: PerMember[Tensor | None]) -> PerMember[Tensor | None]: ... + def forward( + self, inputs: PerMember[Tensor | Array | None] + ) -> PerMember[Tensor | None]: + """ + Run forward pass of model to get model predictions + + Args: + inputs: input tensors for the model, keyed by member id + """ @abstractmethod def unload(self): """Unload model from any devices, freeing their memory. Note: - The moder adapter should be considered unusable afterwards. + The model adapter should be considered unusable afterwards. """ self._loaded = False @@ -147,15 +191,16 @@ def _parse_devices(self, devices: Sequence[str] | None) -> Sequence[DeviceType]: @abstractmethod def _init_model_on_device(self, device: DeviceType) -> ModelType: ... - def forward(self, inputs: PerMember[Tensor | None]) -> PerMember[Tensor | None]: + def forward( + self, inputs: PerMember[Tensor | Array | None] + ) -> PerMember[Tensor | None]: """ Run forward pass of model to get model predictions - Note: sample id and stample stat attributes are passed through - Args: inputs: input tensors for the model, keyed by member id """ + if not self._loaded: raise RuntimeError("Model must be `.load()`ed before calling forward()") @@ -163,50 +208,131 @@ def forward(self, inputs: PerMember[Tensor | None]) -> PerMember[Tensor | None]: if unexpected: warnings.warn(f"Got unexpected input tensor IDs: {unexpected}") - input_arrays = [ - ( - None - if (a := inputs.get(in_id)) is None - else a.transpose(in_order).to_numpy() - ) - for in_id, in_order in zip(self._input_ids, self._input_axes) - ] + multi_batch_indices: dict[str, MultiIndex] = {} + input_arrays: list[Array | None] = [] + for in_id, in_order in zip(self._input_ids, self._input_axes): + if isinstance((a := inputs.get(in_id)), Tensor): + a_transposed = a.transpose( + in_order, + extra_dims="stack" if a.batch_multi_index is None else "raise", + ) + if a_transposed.batch_multi_index is not None: + multi_batch_indices[in_id] = a_transposed.batch_multi_index + + a = a_transposed.data.data + + input_arrays.append(a) logger.debug( "NN input shapes: {}", [a.shape if a is not None else None for a in input_arrays], ) - device, model = self._model_queue.get() - try: - output_arrays = self._forward_impl(device, model, input_arrays) - finally: - self._model_queue.put((device, model)) - logger.debug( - "NN output shapes: {}", - [a.shape if a is not None else None for a in output_arrays], - ) - if len(output_arrays) > len(self._output_ids): - warnings.warn( - f"Model produced more outputs ({len(output_arrays)}) than specified in the model description ({len(self._output_ids)}). Extra outputs will be ignored." + if self.computation_mode == "eager": + output_arrays = self._forward_arrays(input_arrays) + logger.debug( + "NN output shapes: {}", + [a.shape if a is not None else None for a in output_arrays], ) + if len(output_arrays) > len(self._output_ids): + warnings.warn( + f"Model produced more outputs ({len(output_arrays)}) than specified in the model description ({len(self._output_ids)}). Extra outputs will be ignored." + ) output_arrays = output_arrays[: len(self._output_ids)] - output_tensors = [ - None if a is None else Tensor(a, dims=d) - for a, d in zip(output_arrays, self._output_axes) - ] - outputs = { - tid: out - for tid, out in zip( - self._output_ids, - output_tensors, + output_tensors = [ + None if a is None else Tensor(a, dims=d) + for a, d in zip(output_arrays, self._output_axes) + ] + outputs = { + tid: out + for tid, out in zip( + self._output_ids, + output_tensors, + ) + if out is not None + } + elif self.computation_mode == "lazy": + output_arrays = cast( + Sequence[Delayed | None], + delayed( + self._forward_arrays, + pure=False, + nout=len(self._output_ids), + # TODO: add name to avoid hashing inputs? + )(input_arrays), ) - if out is not None - } - outputs = restore_batch_multi_index(inputs, outputs) + in_shape = { + m: {d: int(s) for d, s in zip(dims, a.shape)} + for m, a, dims in zip(self._input_ids, input_arrays, self._input_axes) + if a is not None + } + out_shape = self.get_output_shape(in_shape) + outputs = { + m: Tensor( + da.from_delayed( + a, shape=[out_shape[m][d] for d in dims], dtype=dtype + ), + dims=dims, + ) + for m, a, dims, dtype in zip( + self._output_ids, + output_arrays, + self._output_axes, + [out.dtype for out in self._model_descr.outputs], + ) + if a is not None + } + else: + assert_never(self.computation_mode) + + if multi_batch_indices: + outputs = restore_batch_multi_index(multi_batch_indices.values(), outputs) + # Reorder unstacked output dims to match the order of the unstacked input dims, if possible + unstacked_output_dim_orders = get_unstacked_output_dim_orders_for_model( + self._model_descr, + { + m: in_tensor.dims + for m, in_tensor in inputs.items() + if isinstance(in_tensor, Tensor) + }, + stacked_output_dims={ + m: [ + AxisId(name) + for name in out.batch_multi_index.names + if name is not None + ] + for m, out in outputs.items() + if out.batch_multi_index is not None + }, + ) + outputs = { + m: out.transpose(unstacked_output_dim_orders[m]) + for m, out in outputs.items() + } + return outputs + def _forward_arrays(self, input_arrays: Sequence[Array | None]): + if not self._loaded: + raise RuntimeError( + "Cannot compute model outputs because the model adapter is unloaded. " + + "Compute lazy outputs inside the prediction pipeline context " + + "(`with pred_pipe as pp:`), before the model is unloaded." + ) + device, model = self._model_queue.get() + try: + return self._forward_impl( + device, + model, + [ + cast(NDArray[Any], a.compute()) if isinstance(a, da.Array) else a + for a in input_arrays + ], + ) + finally: + self._model_queue.put((device, model)) + @abstractmethod def _forward_impl( self, @@ -273,7 +399,7 @@ def forward(self, inputs: PerMember[Tensor | None]) -> PerMember[Tensor | None]: ) serialized_output = self._forward_impl(serialized_input) output = self._serializer.deserialize_sample(serialized_output).members - output = restore_batch_multi_index(inputs, output) + output = restore_batch_multi_index(inputs.values(), output) return output @abstractmethod diff --git a/src/bioimageio/core/_op_base.py b/src/bioimageio/core/_op_base.py index 3043de2a..a574133a 100644 --- a/src/bioimageio/core/_op_base.py +++ b/src/bioimageio/core/_op_base.py @@ -3,22 +3,20 @@ from abc import ABC, abstractmethod from collections.abc import Collection from dataclasses import dataclass -from typing import Generic, Union +from typing import Generic -from typing_extensions import TypeVar, assert_never +from typing_extensions import TypeVar from .axis import PerAxis -from .block import Block from .common import MemberId -from .sample import Sample, SampleBlock, SampleBlockWithOrigin +from .sample import Sample from .stat_measures import ( Measure, Stat, ) from .tensor import Tensor -from .utils._multi_index import restore_batch_multi_index -SampleT = TypeVar("SampleT", bound=Union[Sample, SampleBlock, SampleBlockWithOrigin]) +SampleT = TypeVar("SampleT", bound=Sample) @dataclass @@ -39,7 +37,7 @@ class SamplewiseOperator(Operator[Sample]): @dataclass -class BlockwiseOperator(Operator[Union[Sample, SampleBlock]]): +class BlockwiseOperator(Operator[Sample]): # Union[Sample, SampleBlock] """Base class for operators that can be applied to whole sample or blockwise.""" @@ -53,56 +51,22 @@ class SimpleOperator(BlockwiseOperator): @abstractmethod def get_output_shape(self, input_shape: PerAxis[int]) -> PerAxis[int]: ... - def __call__(self, sample: Sample | SampleBlock) -> None: + def __call__(self, sample: Sample) -> None: # | SampleBlock if self.input not in sample.members: - return # TODO: raise? + raise ValueError( + f"Input {self.input} not found in sample members: {list(sample.members)}" + ) input_tensor = sample.members[self.input] output_tensor = self._apply(input_tensor, sample.stat) - output_tensor = restore_batch_multi_index( - {self.input: input_tensor}, {self.output: output_tensor} - )[self.output] - assert output_tensor is not None - if self.output in sample.members: - assert ( - sample.members[self.output].tagged_shape == output_tensor.tagged_shape - ) + if (multi_index := input_tensor.batch_multi_index) is not None: + output_tensor = output_tensor.assign_batch_multi_index(multi_index) - if isinstance(sample, Sample): - sample.members[self.output] = output_tensor - elif isinstance(sample, SampleBlock): - b = sample.blocks[self.input] - output_shape = self.get_output_shape(sample.sample_shape[self.input]) - if ( - b.unstacked_dim_order is None - or b.unstacked_sample_batch_shape is None - or any(a not in b.unstacked_dim_order for a in output_shape) - or any( - a not in b.unstacked_dim_order - for a in b.unstacked_sample_batch_shape - ) - ): - unstacked_dims = None - else: - unstacked_dims = [ - d - for d in b.unstacked_dim_order - if d in output_shape or d in b.unstacked_sample_batch_shape - ] - - sample.blocks[self.output] = Block( - sample_shape=output_shape, - unstacked_sample_batch_shape=sample.unstacked_sample_batch_shape, - unstacked_dim_order=unstacked_dims, - data=output_tensor, - inner_slice=b.inner_slice, - unstacked_batch_slice=b.unstacked_batch_slice, - halo=b.halo, - block_index=b.block_index, - blocks_in_sample=b.blocks_in_sample, - ) - else: - assert_never(sample) + assert self.output not in sample.members or ( + sample.members[self.output].tagged_shape == output_tensor.tagged_shape + ), "overwriting existing output member with different shape" + + sample.members[self.output] = output_tensor @abstractmethod def _apply(self, x: Tensor, stat: Stat) -> Tensor: ... diff --git a/src/bioimageio/core/_prediction_pipeline.py b/src/bioimageio/core/_prediction_pipeline.py index e73ce532..9067af5d 100644 --- a/src/bioimageio/core/_prediction_pipeline.py +++ b/src/bioimageio/core/_prediction_pipeline.py @@ -3,17 +3,12 @@ import warnings from abc import ABC, abstractmethod from collections.abc import Iterable, Mapping, Sequence -from itertools import chain from types import MappingProxyType from typing import ( - Any, Literal, - NamedTuple, - TypeVar, ) from loguru import logger -from tqdm import tqdm from typing_extensions import assert_never from bioimageio.spec import load_model_description @@ -23,40 +18,25 @@ from ._op_base import BlockwiseOperator, SamplewiseOperator from .axis import AxisId, PerAxis from .backends import create_model_adapter +from .block import apply_blockwise, get_chunk_shape from .common import ( + PROCESSING_CHUNK_MIN_BYTES, BlocksizeParameter, Halo, MemberId, PerMember, - SampleId, SupportedWeightsFormat, ) from .digest_spec import ( - get_block_transform, get_input_halo, get_member_ids, + transpose_sample_for_model, ) from .proc_ops import Processing from .proc_setup import setup_pre_and_postprocessing -from .sample import Sample, SampleBlock +from .sample import Sample from .stat_measures import Measure, MeasureValue, Stat from .tensor import Tensor -from .utils._multi_index import get_unstacked_dim_order - -Predict_IO = TypeVar( - "Predict_IO", - Sample, - Iterable[Sample], -) - - -class IntermediatePrediction(NamedTuple): - """Represents an intermediate prediction of a sample with blocking, including the predicted sample so far and the last predicted block. - - The final `IntermediatePrediction` in a sequence holds the complete predicted (and postprocessed if applicable) sample.""" - - sample: Sample - last_block: SampleBlock class _PredictionPipelineBase(ABC): @@ -80,7 +60,7 @@ def __init__( if isinstance(model_descr, v0_4.ModelDescr): self._default_output_halo: PerMember[PerAxis[Halo]] = {} self._default_input_halo: PerMember[PerAxis[Halo]] = {} - self._block_transform = None + # self._block_transform = None else: self._default_output_halo = { t.id: { @@ -93,14 +73,13 @@ def __init__( self._default_input_halo = get_input_halo( model_descr, self._default_output_halo ) - self._block_transform = get_block_transform(model_descr) + # self._block_transform = get_block_transform(model_descr) - self.pad_mode = ( + self._pad_mode = ( {} if isinstance(model_descr, v0_4.ModelDescr) else { - descr.id: descr.pad or v0_5.SymmetricPadding() - for descr in model_descr.inputs + ipt.id: ipt.pad or v0_5.SymmetricPadding() for ipt in model_descr.inputs } ) @@ -123,30 +102,7 @@ def model_descr(self) -> AnyModelDescr: def model_description(self) -> AnyModelDescr: return self._model_descr - def _get_preceding_prediction_pipelines_for_sample( - self, sample: Sample - ) -> Sequence[PredictionPipeline | RemotePredictionPipeline]: - """Get preceding prediction pipelines for a sample based on the sample's input member ids.""" - if not self._preceding_prediction_pipelines: - return () - - required_inputs = set(self.input_ids) - sample_members = set(sample.members.keys()) - preceding_pipelines: list[PredictionPipeline | RemotePredictionPipeline] = [] - for pp in self._preceding_prediction_pipelines[::-1]: - preceding_pipelines.insert(0, pp) - sample_members.update(pp.output_ids) - required_inputs.update(pp.input_ids) - required_inputs.difference_update(sample_members) - - if not required_inputs: - return preceding_pipelines - - raise KeyError( - f"Sample is missing required inputs {required_inputs} for the prediction pipeline or its preceding pipelines." - ) - - def predict_sample_without_blocking( + def predict_without_blocking( self, sample: Sample, skip_preprocessing: bool = False, @@ -168,13 +124,15 @@ def predict_sample_without_blocking( skip_output_cropping: if `True`, skip cropping any output halos from the model output. """ for pp in self._get_preceding_prediction_pipelines_for_sample(sample): - sample = pp._predict_sample_without_blocking_impl( + sample = pp.predict_without_blocking( sample, + skip_preprocessing=False, + skip_postprocessing=False, skip_input_padding=skip_input_padding, skip_output_cropping=skip_output_cropping, ) - return self._predict_sample_without_blocking_impl( + return self._predict_without_blocking_impl( sample, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, @@ -183,13 +141,13 @@ def predict_sample_without_blocking( ) @abstractmethod - def _predict_sample_without_blocking_impl( + def _predict_without_blocking_impl( self, sample: Sample, - skip_preprocessing: bool = False, - skip_postprocessing: bool = False, - skip_input_padding: bool = False, - skip_output_cropping: bool = False, + skip_preprocessing: bool, + skip_postprocessing: bool, + skip_input_padding: bool, + skip_output_cropping: bool, ) -> Sample: """Predict a whole sample at once. @@ -205,116 +163,54 @@ def _predict_sample_without_blocking_impl( skip_output_cropping: if `True`, skip cropping any output halos from the model output. """ - def predict_sample_with_blocking( + def predict_with_blocking( self, sample: Sample, - skip_preprocessing: bool = False, - skip_postprocessing: bool = False, ns: v0_5.ParameterizedSize_N | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] | None = None, + *, + skip_preprocessing: bool = False, + skip_postprocessing: bool = False, batch_size: int | None = None, + skip_input_padding: bool = False, + skip_output_cropping: bool = False, ) -> Sample: - """Predict a sample by predicting sample blocks. - - Note: For fixed/known blocksizes use `predict_sample_with_fixed_blocking`. - Args: - sample: The sample to predict on. - skip_preprocessing: If `True`, skip all preprocessing steps. - skip_postprocessing: If `True`, skip all postprocessing steps. - ns: Block size parameter(s) allows scaling the model's default input block size. - Blocksize parameters are only applied to parameterized input axes, all other axis sizes are fixed/derived or (for output axes) data dependent. - Unapplicable blocksize parameters are ignored. - batch_size: Batch size to use for prediction. - """ + for pp in self._get_preceding_prediction_pipelines_for_sample(sample): + sample = pp._predict_with_blocking_impl( + sample, + ns=ns, + skip_preprocessing=False, + skip_postprocessing=False, + batch_size=batch_size, + skip_input_padding=skip_input_padding, + skip_output_cropping=skip_output_cropping, + ) - output = None - for output in self.predict_sample_with_blocking_yield_intermediates( + return self._predict_with_blocking_impl( sample, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, ns=ns, batch_size=batch_size, - )[1]: - pass - - assert output is not None, ( - "No blocks were predicted, cannot return final sample." - ) - return output.sample - - def predict_sample_with_fixed_blocking( - self, - sample: Sample, - input_block_shape: PerMember[PerAxis[int]], - skip_preprocessing: bool = False, - skip_postprocessing: bool = False, - ) -> Sample: - """Predict `sample` with given `input_block_shape`. - - Note: - - `input_block_shape` is expected to be a valid input shape for the model. - - Use `predict_sample_with_blocking` if you want to control block sizes via generic block size parameters rather than fixed block shapes. - - Args: - sample: The sample to predict on. - input_block_shape: Mapping of input member id to mapping of axis id to block size for that axis. - skip_preprocessing: If `True`, skip all preprocessing steps. - skip_postprocessing: If `True`, skip all postprocessing steps. - """ - intermediate = None - for ( - intermediate - ) in self._predict_sample_with_fixed_blocking_yield_intermediates_impl( - sample, - input_block_shape=input_block_shape, - skip_preprocessing=skip_preprocessing, - skip_postprocessing=skip_postprocessing, - )[1]: - pass - - assert intermediate is not None, ( - "No blocks were predicted, cannot return final sample." + skip_input_padding=skip_input_padding, + skip_output_cropping=skip_output_cropping, ) - return intermediate.sample - def predict_sample_with_blocking_yield_intermediates( + def _predict_with_blocking_impl( self, sample: Sample, - skip_preprocessing: bool = False, - skip_postprocessing: bool = False, ns: v0_5.ParameterizedSize_N | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] - | None = None, - batch_size: int | None = None, - ) -> tuple[int, Iterable[IntermediatePrediction]]: - """Predict `sample` by predicting sample blocks and yield intermediate predictions if no samplewise postprocessing is included. - Also yields intermediate predictions if there are preceding prediction pipelines (model inputs depend on another model's outputs). - For preceding prediction pipelines `ns` and `batch_size` are shared, but pre- and postprocessing are never skipped in preceding pipelines. - - Returns: - Tuple of number of prediction steps and an iterator of predicted intermediate samples with the last predicted block, - All samples, but the last one, are intermediate samples with more and more blocks predicted. - In case samplewise postprocessing needs to be applied, no intermediate results are yielded, but only the final sample after all blocks are predicted and postprocessed. - In case of preceding prediction pipelines (model inputs depend on another model's outputs), intermediate results initially do not include the final output tensors at all. - """ - - total_prediction_steps = 0 - iterable_intermediates = () - for pp in self._get_preceding_prediction_pipelines_for_sample(sample): - pp_steps, pp_intermediates = ( - pp.predict_sample_with_blocking_yield_intermediates( - sample, - ns=ns, - batch_size=batch_size, - skip_preprocessing=False, - skip_postprocessing=False, - ) - ) - total_prediction_steps += pp_steps - iterable_intermediates = chain(iterable_intermediates, pp_intermediates) - + | None, + *, + skip_preprocessing: bool, + skip_postprocessing: bool, + batch_size: int | None, + skip_input_padding: bool, + skip_output_cropping: bool, + ) -> Sample: if isinstance(self._model_descr, v0_4.ModelDescr): raise NotImplementedError( "`predict_sample_with_blocking` not implemented for v0_4.ModelDescr" @@ -335,92 +231,91 @@ def predict_sample_with_blocking_yield_intermediates( ns, batch_size or self._default_batch_size, sample.shape ).inputs - steps, intermediates = ( - self._predict_sample_with_fixed_blocking_yield_intermediates_impl( - sample, - input_block_shape=input_block_shape, - skip_preprocessing=skip_preprocessing, - skip_postprocessing=skip_postprocessing, - ) + return self._predict_with_fixed_blocking_impl( + sample, + input_block_shape=input_block_shape, + skip_preprocessing=skip_preprocessing, + skip_postprocessing=skip_postprocessing, + skip_input_padding=skip_input_padding, + skip_output_cropping=skip_output_cropping, ) - total_prediction_steps += steps - iterable_intermediates = chain(iterable_intermediates, intermediates) - return total_prediction_steps, iterable_intermediates - def predict_sample_with_fixed_blocking_yield_intermediates( + def predict_with_fixed_blocking( self, sample: Sample, input_block_shape: PerMember[PerAxis[int]], - *, skip_preprocessing: bool = False, skip_postprocessing: bool = False, - fill_value: float = float("nan"), - ) -> tuple[int, Iterable[IntermediatePrediction]]: - """Predict `sample` by predicting sample blocks of `input_block_shape` and yield intermediate predictions if no samplewise postprocessing is included. - Also yields intermediate predictions if there are preceding prediction pipelines (model inputs depend on another model's outputs). - For preceding prediction pipelines `input_block_shape` and `fill_value` are shared, but pre- and postprocessing are never skipped in preceding pipelines. - - Returns: - Tuple of number of prediction steps and an iterator of predicted intermediate samples with the last predicted block, - All samples, but the last one, are intermediate samples with more and more blocks predicted. - In case samplewise postprocessing needs to be applied, no intermediate results are yielded, but only the final sample after all blocks are predicted and postprocessed. - In case of preceding prediction pipelines (model inputs depend on another model's outputs), intermediate results initially do not include the final output tensors at all. + skip_input_padding: bool = False, + skip_output_cropping: bool = False, + ) -> Sample: + """Predict `sample` with given `input_block_shape`. + + Note: + - `input_block_shape` is expected to be a valid input shape for the model. + - Use `predict_sample_with_blocking` if you want to control block sizes via generic block size parameters rather than fixed block shapes. + + Args: + sample: The sample to predict on. + input_block_shape: Mapping of input member id to mapping of axis id to block size for that axis. + skip_preprocessing: If `True`, skip all preprocessing steps. + skip_postprocessing: If `True`, skip all postprocessing steps. + skip_input_padding: If `True`, skip input padding; output might correspond to a smaller region within the input. """ - total_prediction_steps = 0 - iterable_intermediates = () - for pp in self._get_preceding_prediction_pipelines_for_sample(sample): - pp_steps, pp_intermediates = ( - pp._predict_sample_with_fixed_blocking_yield_intermediates_impl( - sample, - input_block_shape=input_block_shape, - skip_preprocessing=False, - skip_postprocessing=False, - fill_value=fill_value, - ) - ) - total_prediction_steps += pp_steps - iterable_intermediates = chain(iterable_intermediates, pp_intermediates) - pp_steps, pp_intermediates = ( - self._predict_sample_with_fixed_blocking_yield_intermediates_impl( + for pp in self._get_preceding_prediction_pipelines_for_sample(sample): + sample = pp._predict_with_fixed_blocking_impl( sample, input_block_shape=input_block_shape, - skip_preprocessing=skip_preprocessing, - skip_postprocessing=skip_postprocessing, - fill_value=fill_value, + skip_preprocessing=False, + skip_postprocessing=False, + skip_input_padding=False, + skip_output_cropping=False, ) + + return self._predict_with_fixed_blocking_impl( + sample, + input_block_shape=input_block_shape, + skip_preprocessing=skip_preprocessing, + skip_postprocessing=skip_postprocessing, + skip_input_padding=skip_input_padding, + skip_output_cropping=skip_output_cropping, ) - total_prediction_steps += pp_steps - iterable_intermediates = chain(iterable_intermediates, pp_intermediates) - return total_prediction_steps, iterable_intermediates @abstractmethod - def _predict_sample_with_fixed_blocking_yield_intermediates_impl( + def _predict_with_fixed_blocking_impl( self, sample: Sample, input_block_shape: PerMember[PerAxis[int]], *, - skip_preprocessing: bool = False, - skip_postprocessing: bool = False, - fill_value: float = float("nan"), - ) -> tuple[int, Iterable[IntermediatePrediction]]: ... + skip_preprocessing: bool, + skip_postprocessing: bool, + skip_input_padding: bool, + skip_output_cropping: bool, + ) -> Sample: ... - @abstractmethod - def predict_sample_block( - self, - sample_block: SampleBlock, - skip_preprocessing: bool = False, - skip_postprocessing: bool = False, - ) -> SampleBlock: - """Predict a single sample block. + def _get_preceding_prediction_pipelines_for_sample( + self, sample: Sample + ) -> Sequence[PredictionPipeline | RemotePredictionPipeline]: + """Get preceding prediction pipelines for a sample based on the sample's input member ids.""" + if not self._preceding_prediction_pipelines: + return () - Note that this does not apply samplewise preprocessing or postprocessing steps, but only blockwise ones. + required_inputs = set(self.input_ids) + sample_members = set(sample.members.keys()) + preceding_pipelines: list[PredictionPipeline | RemotePredictionPipeline] = [] + for pp in self._preceding_prediction_pipelines[::-1]: + preceding_pipelines.insert(0, pp) + sample_members.update(pp.output_ids) + required_inputs.update(pp.input_ids) + required_inputs.difference_update(sample_members) - Args: - sample_block: The sample block to predict on. - skip_preprocessing: If `True`, skip blockwise preprocessing steps. - skip_postprocessing: If `True`, skip blockwise postprocessing steps. - """ + if not required_inputs: + return preceding_pipelines + + raise KeyError( + f"Sample is missing required inputs {required_inputs} for the prediction pipeline or its preceding pipelines." + ) class PredictionPipeline(_PredictionPipelineBase): @@ -495,30 +390,57 @@ def __exit__(self, exc_type, exc_val, exc_tb): # type: ignore return False @property - def has_non_blockwise_preprocessing(self) -> bool: + def has_samplewise_preprocessing(self) -> bool: """`True` if any preprocessing operators in the pipeline are not applicable blockwise.""" return bool(self._samplewise_preprocessing) @property - def has_non_blockwise_postprocessing(self) -> bool: + def has_samplewise_postprocessing(self) -> bool: """`True` if any postprocessing operators in the pipeline are not applicable blockwise.""" return bool(self._samplewise_postprocessing) - def _raise_for_non_blockwise_processing( - self, proc_type: Literal["preprocessing", "postprocessing"] - ): - ops = ( - self._samplewise_preprocessing - if proc_type == "preprocessing" - else self._samplewise_postprocessing - ) - non_blockwise = [ - op.__class__.__name__ for op in ops if not isinstance(op, BlockwiseOperator) - ] - if non_blockwise: - raise NotImplementedError( - f"Blockwise {proc_type} for {non_blockwise} not implemented." - ) + def apply_preprocessing(self, sample: Sample) -> None: + """Apply all preprocessing in-place, also may updates sample stats""" + + self.apply_samplewise_preprocessing(sample) + self.apply_blockwise_preprocessing(sample) + + def apply_samplewise_preprocessing(self, sample: Sample, /) -> None: + """Apply preprocessing operators up to and including the last samplewise operator in-place. + + Note: This skips all blockwise preprocessing steps after the last samplewise operator. + """ + for op in self._samplewise_preprocessing: + op(sample) + + def apply_blockwise_preprocessing(self, sample: Sample, /) -> None: + """Apply blockwise preprocessing operators that follow the last samplewise preprocessing operator in-place. + + Note: This skips all preprocessing operators up to and including the last samplewise one. + """ + for op in self._blockwise_preprocessing: + op(sample) + + def apply_postprocessing(self, sample: Sample) -> None: + """Apply all postprocessing in-place, also may updates samples stats""" + self.apply_blockwise_postprocessing(sample) + self.apply_samplewise_postprocessing(sample) + + def apply_blockwise_postprocessing(self, sample: Sample, /) -> None: + """Apply blockwise postprocessing operators that precede the first samplewise postprocessing operator in-place. + + Note: This does not apply all postprocessing operators from the first samplewise one onwards. + """ + for op in self._blockwise_postprocessing: + op(sample) + + def apply_samplewise_postprocessing(self, sample: Sample, /) -> None: + """Apply postprocessing operators starting from and including the first samplewise operator in-place. + + Note: This skips all blockwise postprocessing steps before the first samplewise one. + """ + for op in self._samplewise_postprocessing: + op(sample) def raise_for_non_blockwise_preprocessing(self): """ @@ -534,35 +456,23 @@ def raise_for_non_blockwise_postprocessing(self): """ self._raise_for_non_blockwise_processing("postprocessing") - def predict_sample_block( - self, - sample_block: SampleBlock, - skip_preprocessing: bool = False, - skip_postprocessing: bool = False, - ) -> SampleBlock: - if isinstance(self._model_descr, v0_4.ModelDescr): + def _raise_for_non_blockwise_processing( + self, proc_type: Literal["preprocessing", "postprocessing"] + ): + ops = ( + self._samplewise_preprocessing + if proc_type == "preprocessing" + else self._samplewise_postprocessing + ) + non_blockwise = [ + op.__class__.__name__ for op in ops if not isinstance(op, BlockwiseOperator) + ] + if non_blockwise: raise NotImplementedError( - f"predict_sample_block not implemented for model {self._model_descr.format_version}" + f"Blockwise {proc_type} for {non_blockwise} not implemented." ) - else: - assert self._block_transform is not None - - if not skip_preprocessing: - self._apply_blockwise_preprocessing(sample_block) - - output_meta = sample_block.get_transformed_meta(self._block_transform) - local_output = self._adapter.forward(sample_block.members) - - output = output_meta.with_data( - {k: v for k, v in local_output.items() if v is not None}, - stat=sample_block.stat, - ) - if not skip_postprocessing: - self._apply_blockwise_postprocessing(output) - - return output - def _predict_sample_without_blocking_impl( + def _predict_without_blocking_impl( self, sample: Sample, skip_preprocessing: bool = False, @@ -570,8 +480,11 @@ def _predict_sample_without_blocking_impl( skip_input_padding: bool = False, skip_output_cropping: bool = False, ) -> Sample: + sample = transpose_sample_for_model( + sample, self._model_descr, extra_dims="stack" + ) if not skip_input_padding: - sample = sample.pad(pad_width=self._default_input_halo, mode=self.pad_mode) + sample = sample.pad(pad_width=self._default_input_halo, mode=self._pad_mode) if not skip_preprocessing: self.apply_preprocessing(sample) @@ -582,34 +495,7 @@ def _predict_sample_without_blocking_impl( if v is not None } - unstacked_dim_order: PerMember[Sequence[AxisId] | None] | None = {} - for m, t in output_members.items(): - if (udo := sample.unstacked_dim_order) is None: - unstacked_in_order = None - else: - udo_m = udo.get(m) - if udo_m is None: - unstacked_in_order = None - else: - unstacked_in_order = list(udo_m) - - if (sd := sample.unstacked_batch_shape) is None: - stacked_dims = None - else: - stacked_dims = list(sd) - - unstacked_dim_order[m] = get_unstacked_dim_order( - t.dims, - unstacked_reference_order=unstacked_in_order, - stacked_dims=stacked_dims, - ) - - output = Sample( - members=output_members, - stat=sample.stat, - id=sample.id, - unstacked_dim_order=unstacked_dim_order, - ) + output = Sample(members=output_members, stat=sample.stat, id=sample.id) if not skip_postprocessing: self.apply_postprocessing(output) @@ -626,51 +512,19 @@ def _predict_sample_without_blocking_impl( for m, t in output.members.items() } - return output - - def get_output_sample_id(self, input_sample_id: SampleId): - warnings.warn( - "`PredictionPipeline.get_output_sample_id()` is deprecated and will be" - + " removed soon. Output sample id is equal to input sample id, hence this" - + " function is not needed." - ) - return input_sample_id - - def predict_sample_with_blocking( - self, - sample: Sample, - skip_preprocessing: bool = False, - skip_postprocessing: bool = False, - ns: v0_5.ParameterizedSize_N - | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N] - | None = None, - batch_size: int | None = None, - ) -> Sample: - output = None - for output in self.predict_sample_with_blocking_yield_intermediates( - sample, - skip_preprocessing=skip_preprocessing, - skip_postprocessing=skip_postprocessing, - ns=ns, - batch_size=batch_size, - )[1]: - pass - - assert output is not None, ( - "No blocks were predicted, cannot return final sample." - ) - return output.sample + return output.unstack_batch_multi_index(errors="ignore") - def _predict_sample_with_fixed_blocking_yield_intermediates_impl( + def _predict_with_fixed_blocking_impl( self, sample: Sample, - input_block_shape: Mapping[MemberId, Mapping[AxisId, int]], + input_block_shape: PerMember[PerAxis[int]], *, - skip_preprocessing: bool = False, - skip_postprocessing: bool = False, - fill_value: float = float("nan"), - ) -> tuple[int, Iterable[IntermediatePrediction]]: - """Predict `sample` with given `input_block_shape` and yield the full sample with intermediate results. + skip_preprocessing: bool, + skip_postprocessing: bool, + skip_input_padding: bool, + skip_output_cropping: bool, + ) -> Sample: + """Predict `sample` with given `input_block_shape`. Note: - `input_block_shape` is expected to be a valid input shape for the model. @@ -684,120 +538,79 @@ def _predict_sample_with_fixed_blocking_yield_intermediates_impl( input_block_shape: Mapping of input member id to mapping of axis id to block size for that axis. skip_preprocessing: If `True`, skip all preprocessing steps. skip_postprocessing: If `True`, skip all postprocessing steps. + skip_input_padding: If `True`, skip input padding. + skip_output_cropping: If `True`, skip output cropping (of the halo). - Returns: - Tuple of number of blocks and an iterable of predicted intermediate samples with the last predicted block, - All samples, but the last one, are intermediate samples with more and more blocks predicted. """ + if not skip_input_padding: + sample = sample.pad(pad_width=self._default_input_halo, mode=self._pad_mode) + if not skip_preprocessing: - self._apply_samplewise_preprocessing(sample) + self.apply_preprocessing(sample) - n_blocks, input_blocks = sample.split_into_blocks( - input_block_shape, - halo=self._default_input_halo, - pad_mode=self.pad_mode, + chunk_shape = get_chunk_shape( + sample.members, input_block_shape, PROCESSING_CHUNK_MIN_BYTES ) logger.info( - "split sample shape {} into {} blocks of {}.", + "split sample shape {} into chunks of shape {}.", {k: dict(v) for k, v in sample.shape.items()}, - n_blocks, - {k: dict(v) for k, v in input_block_shape.items()}, + {k: dict(v) for k, v in chunk_shape.items()}, ) - def _predict_blocks(): - predicted_sample = None - for i, b in enumerate( - tqdm( - input_blocks, - desc=f"predict sample '{sample.id or ''}' with {self._model_descr.id or self._model_descr.name}", - unit="block", - unit_divisor=1, - total=n_blocks, - ) - ): - if not skip_preprocessing: - self._apply_blockwise_preprocessing(b) - - predicted_block = self.predict_sample_block( - b, skip_preprocessing=True, skip_postprocessing=True - ) - - if not skip_postprocessing: - self._apply_blockwise_postprocessing(predicted_block) - - if predicted_sample is None: - predicted_sample = Sample.from_blocks( - [predicted_block], fill_value=fill_value - ) - else: - predicted_sample.set_block(predicted_block) - - if not skip_postprocessing and i == n_blocks - 1: - self._apply_samplewise_postprocessing(predicted_sample) - - yield IntermediatePrediction(predicted_sample, predicted_block) - - return n_blocks, _predict_blocks() - - def _apply_samplewise_preprocessing(self, sample: Sample, /) -> None: - """Apply preprocessing operators up to and including the last samplewise operator in-place. - - Note: This skips all blockwise preprocessing steps after the last samplewise operator. - """ - if isinstance(sample, SampleBlock): - self.raise_for_non_blockwise_preprocessing() - - for op in self._samplewise_preprocessing: - op(sample) - - def _apply_blockwise_preprocessing( - self, sample_block: Sample | SampleBlock, / - ) -> None: - """Apply blockwise preprocessing operators in-place. - - Note: This skips all preprocessing operators up to and including the last samplewise one. - """ - for op in self._blockwise_preprocessing: - op(sample_block) - - def apply_preprocessing(self, sample: Sample | SampleBlock) -> None: - """Apply preprocessing in-place, also may updates sample stats""" - - if isinstance(sample, Sample): - self._apply_samplewise_preprocessing(sample) - else: - self.raise_for_non_blockwise_preprocessing() - - self._apply_blockwise_preprocessing(sample) - - def _apply_blockwise_postprocessing( - self, sample_block: Sample | SampleBlock, / - ) -> None: - """Apply in-place blockwise postprocessing operators + output_members = self._forward_with_fixed_blocking( + sample.members, + input_chunk_shape=chunk_shape, + input_block_shape=input_block_shape, + ) + sample = Sample(id=sample.id, members=output_members, stat=sample.stat) - Note: This does not apply all postprocessing operators from the first samplewise one onwards. - """ - for op in self._blockwise_postprocessing: - op(sample_block) + if not skip_postprocessing: + self.apply_postprocessing(sample) - def _apply_samplewise_postprocessing(self, sample: Sample, /) -> None: - """Apply in-place postprocessing operators starting from and including the first samplewise operator. + if not skip_output_cropping: + roi = { + m: { + d: slice(None) + if (h := self._default_output_halo.get(m, {}).get(d)) is None + else slice(h.left, s - h.right) + for d, s in shape.items() + } + for m, shape in sample.shape.items() + } + sample = sample[roi] - Note: This skips all blockwise postprocessing steps before the first samplewise one. - """ - if isinstance(sample, SampleBlock): - self.raise_for_non_blockwise_postprocessing() + return sample - for op in self._samplewise_postprocessing: - op(sample) + def _forward_with_fixed_blocking( + self, + sample_members: PerMember[Tensor], + input_chunk_shape: PerMember[PerAxis[int]] | int, + input_block_shape: PerMember[PerAxis[int]], + ) -> dict[MemberId, Tensor]: + def get_output_shape( + input_shape: PerMember[PerAxis[int]], + ) -> PerMember[PerAxis[int]]: + assert not isinstance(self._model_descr, v0_4.ModelDescr), ( + "forward_with_fixed_chunking_impl_dask is not implemented for v0_4.ModelDescr" + ) + return { + m: {d: s if isinstance(s, int) else -1 for d, s in s_per_a.items()} + for m, s_per_a in self._model_descr.get_output_tensor_sizes( + input_shape + ).items() + } - def apply_postprocessing(self, sample: Sample | SampleBlock) -> None: - """apply postprocessing in-place, also may updates samples stats""" - self._apply_blockwise_postprocessing(sample) - if isinstance(sample, Sample): - self._apply_samplewise_postprocessing(sample) - else: - self.raise_for_non_blockwise_postprocessing() + return apply_blockwise( + self._adapter.forward, + sample_members, + chunk_shape=input_chunk_shape, + block_shape=input_block_shape, + pad_mode=self._pad_mode, + input_halo=self._default_input_halo, + output_halo=self._default_output_halo, + get_output_shape=get_output_shape, + block_shape_factors=input_block_shape, + ) def load(self): """Prepare prediction pipeline for use. @@ -873,7 +686,6 @@ def create_prediction_pipeline( PredictionPipeline | RemotePredictionPipeline ] | None = None, - **deprecated_kwargs: Any, ) -> PredictionPipeline: """ Creates prediction pipeline which includes: @@ -915,10 +727,6 @@ def create_prediction_pipeline( del weight_format default_blocksize_parameter = ns or default_blocksize_parameter del ns - if deprecated_kwargs: - warnings.warn( - f"deprecated create_prediction_pipeline kwargs: {set(deprecated_kwargs)}" - ) model_adapter = model_adapter or create_model_adapter( model_description=bioimageio_model, diff --git a/src/bioimageio/core/_resource_tests.py b/src/bioimageio/core/_resource_tests.py index 9ee74e35..162e1e32 100644 --- a/src/bioimageio/core/_resource_tests.py +++ b/src/bioimageio/core/_resource_tests.py @@ -1022,14 +1022,12 @@ def save_to_working_dir(name: str, tensor: Tensor) -> list[Path]: ) as prediction_pipeline: prediction_pipeline.apply_preprocessing(test_input) test_input_preprocessed = deepcopy(test_input) - results_not_postprocessed = ( - prediction_pipeline.predict_sample_without_blocking( - test_input, - skip_postprocessing=True, - skip_preprocessing=True, - skip_input_padding=True, - skip_output_cropping=True, - ) + results_not_postprocessed = prediction_pipeline.predict_without_blocking( + test_input, + skip_postprocessing=True, + skip_preprocessing=True, + skip_input_padding=True, + skip_output_cropping=True, ) results = deepcopy(results_not_postprocessed) prediction_pipeline.apply_postprocessing(results) @@ -1231,7 +1229,7 @@ def get_ns(n: int): for n, batch_size, inputs, exptected_output_shape in generate_test_cases(): error: str | None = None try: - result = prediction_pipeline.predict_sample_without_blocking( + result = prediction_pipeline.predict_without_blocking( inputs, skip_input_padding=True, skip_output_cropping=True ) except Exception as e: diff --git a/src/bioimageio/core/_sample_serializer.py b/src/bioimageio/core/_sample_serializer.py index 217f376b..29ad9f37 100644 --- a/src/bioimageio/core/_sample_serializer.py +++ b/src/bioimageio/core/_sample_serializer.py @@ -9,10 +9,10 @@ from bioimageio.spec.model import v0_5 +from ._utils.type_hints import NestedList from .axis import PerAxis from .common import HaloLike, PadMode, PerMember -from .digest_spec import split_sample_into_blocks_for_model -from .sample import Sample, SampleBlock +from .sample import Sample SerializedSampleBlockType = TypeVar("SerializedSampleBlockType") @@ -22,19 +22,18 @@ class SampleSerializer(ABC, Generic[SerializedSampleBlockType]): def serialize_sample( cls, sample: Sample, - ) -> tuple[SerializedSampleBlockType]: - """Serialize a sample as a single block""" - return (cls.serialize_sample_block(sample.as_single_block()),) + ) -> NestedList[SerializedSampleBlockType]: + """Serialize a sample (chunkwise for chunked samples)""" + + # for block in sample.blockwise + return cls.serialize_sample_block(block) @classmethod def deserialize_sample( cls, serialized: Iterable[SerializedSampleBlockType], - fill_value: float = float("nan"), ) -> Sample: - return Sample.from_blocks( - (cls.deserialize_sample_block(s) for s in serialized), fill_value=fill_value - ) + return Sample.from_blocks((cls.deserialize_sample_block(s) for s in serialized)) def serialize_sample_blockwise( self, @@ -46,14 +45,10 @@ def serialize_sample_blockwise( ) -> Iterable[SerializedSampleBlockType]: """Split a sample into blocks according to the model's input specifications and `blocksize_parameter` and serialize each block.""" - _n_blocks, blocks = split_sample_into_blocks_for_model( - sample, - model=model, - blocksize_parameter=blocksize_parameter, - batch_size=batch_size, - ) - for block in blocks: - yield self.serialize_sample_block(block) + block_shape = model.get_tensor_sizes( + blocksize_parameter, batch_size, sample.shape + ).inputs + sample = sample.chunk(block_shape) @classmethod def serialize_sample_with_fixed_blocking( diff --git a/src/bioimageio/core/_utils/__init__.py b/src/bioimageio/core/_utils/__init__.py new file mode 100644 index 00000000..61584900 --- /dev/null +++ b/src/bioimageio/core/_utils/__init__.py @@ -0,0 +1 @@ +from .compare import compare as compare diff --git a/src/bioimageio/core/_utils/chunking.py b/src/bioimageio/core/_utils/chunking.py new file mode 100644 index 00000000..e4d0d976 --- /dev/null +++ b/src/bioimageio/core/_utils/chunking.py @@ -0,0 +1,159 @@ +from __future__ import annotations + +from collections.abc import Callable, Mapping +from typing import Literal, cast + +import dask.array as da + +from bioimageio.core._utils.type_hints import NestedList + +from ..axis import PerAxis +from ..common import HaloLike, MemberId, PadMode, PerMember +from ..tensor import Tensor + + +def chunk_tensors( + tensors: PerMember[Tensor], + chunk_shapes: PerMember[PerAxis[int]], + *, + halo: PerMember[PerAxis[HaloLike]] | None = None, + pad_mode: PadMode | PerMember[PadMode] | Literal["none"] = "none", +) -> dict[MemberId, Tensor]: + if unknown_chunks := [m for m in chunk_shapes if m not in tensors]: + raise ValueError( + f"chunk sizes specified for unknown tensors: {unknown_chunks}." + ) + return { + m: t.chunk( + chunk_shapes[m], + halo=halo.get(m) if halo is not None else None, + pad_mode=pad_mode + if isinstance(pad_mode, str) + else pad_mode.get(m, "none") + if isinstance(pad_mode, Mapping) + else pad_mode, + ) + for m, t in tensors.items() + } + + +def chunkwise_to_nested_list_prechunked( + tensors: PerMember[Tensor], func: Callable[[PerMember[Tensor]], PerMember[Tensor]] +) -> NestedList[PerMember[Tensor]]: + """Apply a function to each chunk of the chunked tensors and return a nested list of processed chunks. + + See [chunk_tensors][] for chunking the tensors beforehand. + + Args: + func: The function to apply to each sample chunk. + """ + + if not all(t.data.data.numblocks for t in tensors.values()): + raise ValueError( + "Sample is not chunked and no `chunk_shapes` provided. Please provide `chunk_shapes` or chunk the sample first." + ) + + chunk_ndims = cast( + PerMember[tuple[int, ...]], + {m: t.data.data.numblocks for m, t in tensors.items()}, + ) + if len(set(chunk_ndims.values())) != 1: + raise ValueError(f"Input chunk grids do not match: {chunk_ndims}") + + chunk_grid_shape = next(iter(chunk_ndims.values())) + + def func_chunks( + axis: int = 0, loc: tuple[int, ...] = () + ) -> NestedList[PerMember[Tensor]]: + if axis < len(chunk_grid_shape): + grid_size = chunk_grid_shape[axis] + return [func_chunks(axis + 1, (*loc, i)) for i in range(grid_size)] + + return func( + { + m: Tensor(t.data.data.blocks[loc], dims=t.dims) + for m, t in tensors.items() + } + ) + + return func_chunks() + + +# def blockwise_to_nested_list( +# tensors: PerMember[Tensor], func: Callable[[PerMember[Tensor]], PerMember[Tensor]], block_shapes: PerMember[PerAxis[int]], halo: PerMember[PerAxis[HaloLike]], pad_mode: PadMode | PerMember[PadMode] +# ) -> NestedList[PerMember[Tensor]]: +# """Apply a function to each block and return a nested list of processed blocks. +# Each block will have the same shape, the last two blocks along each dimension may overlap by more than the halo. + +# See [chunk_tensors][] for chunking the tensors beforehand. + +# Args: +# func: The function to apply to each sample chunk. +# """ + +# if not all(t.data.data.numblocks for t in tensors.values()): +# raise ValueError( +# "Sample is not chunked and no `chunk_shapes` provided. Please provide `chunk_shapes` or chunk the sample first." +# ) + +# chunk_ndims = cast( +# PerMember[tuple[int, ...]], +# {m: t.data.data.numblocks for m, t in tensors.items()}, +# ) +# if len(set(chunk_ndims.values())) != 1: +# raise ValueError(f"Input chunk grids do not match: {chunk_ndims}") + +# chunk_grid_shape = next(iter(chunk_ndims.values())) + +# def func_chunks( +# axis: int = 0, loc: tuple[int, ...] = () +# ) -> NestedList[PerMember[Tensor]]: +# if axis < len(chunk_grid_shape): +# grid_size = chunk_grid_shape[axis] +# return [func_chunks(axis + 1, (*loc, i)) for i in range(grid_size)] + +# return func( +# { +# m: Tensor(t.data.data.blocks[loc], dims=t.dims) +# for m, t in tensors.items() +# } +# ) + +# return func_chunks() + + +def chunkwise( + tensors: PerMember[Tensor], func: Callable[[PerMember[Tensor]], PerMember[Tensor]] +) -> dict[MemberId, Tensor]: + """Apply a function to each chunk of the chunked tensors. + + See [chunk_tensors][] for chunking the tensors beforehand. + + Args: + func: The function to apply to each sample chunk. + """ + nested_outputs = chunkwise_to_nested_list_prechunked(tensors, func) + + def select_output( + nested: NestedList[PerMember[Tensor]], output_id: MemberId + ) -> NestedList[da.Array]: + if isinstance(nested, list): + return [select_output(item, output_id) for item in nested] + else: + a = nested[output_id].data.data + assert isinstance(a, da.Array) + return a + + first_block = nested_outputs + while isinstance(first_block, list): + first_block = first_block[0] + + return { + output_id: Tensor( + da.block( + select_output(nested_outputs, output_id), + ), + dims=first_block[output_id].dims, + ) + for output_id in first_block + } diff --git a/src/bioimageio/core/utils/_color.py b/src/bioimageio/core/_utils/color.py similarity index 100% rename from src/bioimageio/core/utils/_color.py rename to src/bioimageio/core/_utils/color.py diff --git a/src/bioimageio/core/utils/_compare.py b/src/bioimageio/core/_utils/compare.py similarity index 100% rename from src/bioimageio/core/utils/_compare.py rename to src/bioimageio/core/_utils/compare.py diff --git a/src/bioimageio/core/utils/_io_zarr.py b/src/bioimageio/core/_utils/io_zarr.py similarity index 100% rename from src/bioimageio/core/utils/_io_zarr.py rename to src/bioimageio/core/_utils/io_zarr.py diff --git a/src/bioimageio/core/_utils/multi_index.py b/src/bioimageio/core/_utils/multi_index.py new file mode 100644 index 00000000..a0fea01b --- /dev/null +++ b/src/bioimageio/core/_utils/multi_index.py @@ -0,0 +1,113 @@ +from __future__ import annotations + + +# def create_tensor_with_multiindex( +# unstacked_shape: PerAxis[int], +# transpose_to: Sequence[AxisId], +# *, +# fill_value: float | None = None, +# dtype: np.dtype | None = None, +# unstacked_data: NDArray[Any] | da.Array | None = None, +# stacked_data: NDArray[Any] | da.Array | None = None, +# ) -> Tensor: +# """Create a tensor potentially with a MultiIndex in the batch dimension. + +# The tensor is created with the given unstacked shape and then transposed to `transpose_to`. +# If `transpose_to` is missing some of the unstacked_shape dimensions a MultiIndex will be created. +# Otherwise the tensor will be created without a batch MultiIndex. + +# Args: +# unstacked_shape: The shape of the tensor before stacking. +# transpose_to: The order of the axes after transposing. +# fill_value: The value to fill the tensor with. If None, the tensor will be empty (ignored if unstacked_data or stacked_data are not None). +# dtype: The data type of the tensor. If None, the data type will be inferred from unstacked_data or stacked_data (must be specified if neither unstacked_data nor stacked_data is provided). +# unstacked_data: The data to use for the unstacked tensor. If provided, fill_value will be ignored. May not be provided together with stacked_data. +# stacked_data: The data to use for the stacked tensor. If provided, fill_value will be ignored. May not be provided together with unstacked_data. + +# Returns: +# A tensor (with a MultiIndex in the batch dimension). +# """ +# from ..tensor import Tensor + +# if unstacked_data is not None and stacked_data is not None: +# raise ValueError("unstacked_data and stacked_data may not be provided together") + +# if unstacked_data is None: +# if stacked_data is not None: +# dtype = stacked_data.dtype # pyright: ignore[reportUnknownVariableType] +# elif dtype is None: +# raise ValueError( +# "dtype must be specified if neither unstacked_data nor stacked_data is provided" +# ) + +# if fill_value is None or stacked_data is not None: +# unstacked_data = da.zeros( # pyright: ignore[reportUnknownVariableType] +# shape=tuple(unstacked_shape.values()), dtype=dtype +# ) +# else: +# unstacked_data = da.full( # pyright: ignore[reportUnknownVariableType] +# shape=tuple(unstacked_shape.values()), +# fill_value=fill_value, +# dtype=dtype, +# ) + +# assert isinstance(unstacked_data, da.Array) +# elif dtype is not None: +# unstacked_data = unstacked_data.astype(dtype) # pyright: ignore[reportUnknownVariableType] +# assert isinstance(unstacked_data, (np.ndarray, da.Array)) +# else: +# dtype = unstacked_data.dtype # pyright: ignore[reportUnknownVariableType] +# assert isinstance(dtype, np.dtype) + +# if isinstance(unstacked_data, da.Array): +# tensor = Tensor.from_array(unstacked_data, dims=tuple(unstacked_shape.keys())) +# elif isinstance(unstacked_data, np.ndarray): +# assert is_ndarray(unstacked_data) +# tensor = Tensor.from_numpy(unstacked_data, dims=tuple(unstacked_shape.keys())) +# else: +# assert_never(unstacked_data) + +# stacked_tensor = tensor.transpose( +# transpose_to, extra_dims="stack", missing_dims="expand" +# ) +# if stacked_data is not None: +# if stacked_data.shape != stacked_tensor.data.shape: +# raise ValueError( +# f"stacked_data shape {stacked_data.shape} does not match the stacked tensor shape {stacked_tensor.data.shape}" +# ) +# stacked_tensor.data.data = stacked_data + +# return stacked_tensor + + +# def get_unstacked_shape( +# stacked_shape: PerAxis[int], +# *, +# unstacked_dim_order: Sequence[AxisId] | None, +# unstacked_batch_shape: PerAxis[int] | None, +# ) -> PerAxis[int]: +# """Combine a shape with an optional unstacked batch shape and apply an optional unstacked dim order to get the final unstacked shape. + +# Returns `stacked_shape` if both `unstacked_dim_order` and `unstacked_batch_shape` are None. +# """ +# unstacked_shape_unordered = {**stacked_shape, **(unstacked_batch_shape or {})} + +# if unstacked_dim_order is not None: +# assert all(a in unstacked_dim_order for a in unstacked_shape_unordered), ( +# f"unstacked_dim_order={unstacked_dim_order} does not contain all axes of the sample shape {stacked_shape} and unstacked sample batch shape {unstacked_batch_shape}" +# ) +# order = unstacked_dim_order +# else: +# # if no explicit unstacked dim order is provided we insert the unstacked batch axes at the position of the batch axis in the sample shape +# order = list(stacked_shape) +# if unstacked_batch_shape: +# assert BATCH_AXIS_ID in stacked_shape, ( +# f"unstacked sample batch shape {unstacked_batch_shape} is provided but the sample shape {stacked_shape} does not contain a batch axis" +# ) +# b_idx = order.index(BATCH_AXIS_ID) +# order[b_idx : b_idx + 1] = list(unstacked_batch_shape) + +# assert all(a in order for a in unstacked_shape_unordered), ( +# f"unstacked_dim_order={unstacked_dim_order} does not contain all axes of the sample shape {stacked_shape} and unstacked sample batch shape {unstacked_batch_shape}" +# ) +# return {a: unstacked_shape_unordered[a] for a in order} diff --git a/src/bioimageio/core/_utils/resolve_per_member_key.py b/src/bioimageio/core/_utils/resolve_per_member_key.py new file mode 100644 index 00000000..2a69c6f7 --- /dev/null +++ b/src/bioimageio/core/_utils/resolve_per_member_key.py @@ -0,0 +1,70 @@ +from __future__ import annotations + +from collections.abc import Collection, Mapping +from typing import Any, TypeVar, cast + +from ..axis import AxisId, PerAxis +from ..common import MemberId, PerMember +from .type_guards import is_mapping + +T_pm = TypeVar("T_pm") +T_pa = TypeVar("T_pa") + + +def resolve_per_member_var( + var: PerMember[T_pm | PerAxis[T_pa]] | T_pm | PerAxis[T_pa], + /, + *, + valid_per_member_types: tuple[type[T_pm], ...], + valid_per_axis_types: tuple[type[T_pa], ...], + members: Collection[MemberId], +): + """Resolve a key that may be a per-member mapping or a per-axis mapping into a per-member mapping. + + If the key is a per-axis mapping, it will be expanded into a per-member mapping + with the same per-axis mapping for each member in `members`. + """ + if not isinstance(var, Mapping): + var = {m: cast(Any, var) for m in members} + + var = cast(Mapping[Any, Any], var) + per_member_key: PerMember[T_pm | PerAxis[T_pa]] = {} + per_axis_key: PerAxis[T_pa] = {} + for k, v in var.items(): + if isinstance(k, MemberId): + if valid_per_axis_types and is_mapping(v): + if not all(isinstance(ak, AxisId) for ak in v): + raise TypeError( + f"Per-axis mappings only support keys of type AxisId, but got {list(v)}" + ) + if not all(isinstance(av, valid_per_axis_types) for av in v.values()): + raise TypeError( + f"Per-axis mappings only support value types {valid_per_axis_types}, but got {list(v.values())}" + ) + elif not isinstance(v, valid_per_member_types): + raise TypeError( + f"Per-member keys only support value types {valid_per_member_types}" + + f", but got {v} of type {type(v)})" + ) + per_member_key[k] = v + elif isinstance(k, AxisId): + if not isinstance(v, valid_per_axis_types): + raise TypeError( + f"Per-axis keys only supports value types {valid_per_axis_types}, but got {v} of type {type(v)})" + ) + per_axis_key[k] = v + else: + raise TypeError( + "Only keys of type MemberId or AxisId are supported," + + f" but got {k} of type {type(k)}" + ) + + if per_member_key and per_axis_key: + raise ValueError( + "Mixing both MemberId and AxisId keys in the same mapping is not supported," + + f" got member keys: {list(per_member_key)} and axis keys: {list(per_axis_key)}" + ) + elif per_axis_key: + per_member_key = {m: per_axis_key for m in members} + + return per_member_key diff --git a/src/bioimageio/core/utils/_tiff_metadata.py b/src/bioimageio/core/_utils/tiff_metadata.py similarity index 97% rename from src/bioimageio/core/utils/_tiff_metadata.py rename to src/bioimageio/core/_utils/tiff_metadata.py index 4106e93a..9c34fe1b 100644 --- a/src/bioimageio/core/utils/_tiff_metadata.py +++ b/src/bioimageio/core/_utils/tiff_metadata.py @@ -2,7 +2,7 @@ from pydantic import BaseModel -from ._xml_utils import xml_to_dict +from .xml_utils import xml_to_dict class ImageJMetadata(BaseModel, extra="allow"): diff --git a/src/bioimageio/core/utils/_type_guards.py b/src/bioimageio/core/_utils/type_guards.py similarity index 100% rename from src/bioimageio/core/utils/_type_guards.py rename to src/bioimageio/core/_utils/type_guards.py diff --git a/src/bioimageio/core/_utils/type_hints.py b/src/bioimageio/core/_utils/type_hints.py new file mode 100644 index 00000000..589fe1c7 --- /dev/null +++ b/src/bioimageio/core/_utils/type_hints.py @@ -0,0 +1,9 @@ +from typing import Any, TypeAlias, TypeVar, Union + +_T = TypeVar("_T") +NestedList: TypeAlias = Union[list["NestedList[_T]"], _T] + + +def cast_and_assert(typ: type[_T], x: Any) -> _T: + assert isinstance(x, typ), (x, typ) + return x diff --git a/src/bioimageio/core/utils/_xml_utils.py b/src/bioimageio/core/_utils/xml_utils.py similarity index 100% rename from src/bioimageio/core/utils/_xml_utils.py rename to src/bioimageio/core/_utils/xml_utils.py diff --git a/src/bioimageio/core/axis.py b/src/bioimageio/core/axis.py index 1f11bd53..7f248e2c 100644 --- a/src/bioimageio/core/axis.py +++ b/src/bioimageio/core/axis.py @@ -32,7 +32,7 @@ def _guess_axis_type(a: str): + " Consider using one of:" + " 'b', 'batch', 't', 'time', 's', 'sample', 'i'," + " 'index', 'c', 'channel', 'x', 'y', 'z'," - + " 'space*'. Or create an `Axis` object instead.", + + " 'space*'. Or create an `Axis` object with an explicity type instead.", a, ) return "unknown" @@ -46,6 +46,7 @@ def _guess_axis_type(a: str): BATCH_AXIS_ID = v0_5.BATCH_AXIS_ID CHANNEL_AXIS_ID = v0_5.CHANNEL_AXIS_ID +TIME_AXIS_ID = v0_5.DEFAULT_TIME_AXIS_ID _T = TypeVar("_T") PerAxis = Mapping[AxisId, _T] diff --git a/src/bioimageio/core/backends/keras_backend.py b/src/bioimageio/core/backends/keras_backend.py index 991d592a..9ed83b54 100644 --- a/src/bioimageio/core/backends/keras_backend.py +++ b/src/bioimageio/core/backends/keras_backend.py @@ -18,8 +18,8 @@ from .._model_adapter import LocalModelAdapter from .._settings import settings -from ..utils._compare import warn_about_version -from ..utils._type_guards import is_list, is_tuple +from .._utils.compare import warn_about_version +from .._utils.type_guards import is_list, is_tuple os.environ["KERAS_BACKEND"] = settings.keras_backend diff --git a/src/bioimageio/core/backends/onnx_backend.py b/src/bioimageio/core/backends/onnx_backend.py index 9ccf2493..57959b5f 100644 --- a/src/bioimageio/core/backends/onnx_backend.py +++ b/src/bioimageio/core/backends/onnx_backend.py @@ -15,7 +15,7 @@ from bioimageio.spec.model import v0_4, v0_5 from .._model_adapter import LocalModelAdapter -from ..utils._type_guards import is_list, is_tuple +from .._utils.type_guards import is_list, is_tuple class ONNXModelAdapter(LocalModelAdapter[Optional[str], rt.InferenceSession]): @@ -32,9 +32,7 @@ def __init__( self._input_names: list[str] | None = None super().__init__(model_description=model_description, devices=devices) - def _parse_devices( - self, devices: Sequence[str] | None - ) -> Sequence[str | None]: + def _parse_devices(self, devices: Sequence[str] | None) -> Sequence[str | None]: available_providers: Any = None if hasattr(rt, "get_available_providers"): available_providers = cast(Any, rt.get_available_providers()) diff --git a/src/bioimageio/core/backends/pytorch_backend.py b/src/bioimageio/core/backends/pytorch_backend.py index efb9b933..9d6e7fa3 100644 --- a/src/bioimageio/core/backends/pytorch_backend.py +++ b/src/bioimageio/core/backends/pytorch_backend.py @@ -20,8 +20,8 @@ from bioimageio.spec.utils import download from .._model_adapter import LocalModelAdapter +from .._utils.type_guards import is_list, is_ndarray, is_tuple from ..digest_spec import import_callable -from ..utils._type_guards import is_list, is_ndarray, is_tuple @runtime_checkable diff --git a/src/bioimageio/core/backends/torchscript_backend.py b/src/bioimageio/core/backends/torchscript_backend.py index ca7b4620..fdcdeb30 100644 --- a/src/bioimageio/core/backends/torchscript_backend.py +++ b/src/bioimageio/core/backends/torchscript_backend.py @@ -12,7 +12,7 @@ from bioimageio.spec.model import v0_4, v0_5 from .._model_adapter import LocalModelAdapter -from ..utils._type_guards import is_list, is_tuple +from .._utils.type_guards import is_list, is_tuple from .pytorch_backend import get_devices @@ -30,9 +30,7 @@ def __init__( self._weight_descr = model_description.weights.torchscript super().__init__(model_description=model_description, devices=devices) - def _parse_devices( - self, devices: Sequence[str] | None - ) -> Sequence[torch.device]: + def _parse_devices(self, devices: Sequence[str] | None) -> Sequence[torch.device]: return get_devices(devices) def _init_model_on_device(self, device: torch.device) -> Any: diff --git a/src/bioimageio/core/block.py b/src/bioimageio/core/block.py index 9988c7c8..be404e62 100644 --- a/src/bioimageio/core/block.py +++ b/src/bioimageio/core/block.py @@ -1,137 +1,1013 @@ from __future__ import annotations -from collections.abc import Generator, Iterable +import itertools +from collections.abc import Collection, Mapping, Sequence +from copy import deepcopy from dataclasses import dataclass +from functools import partial +from math import ceil, floor, prod +from types import MappingProxyType from typing import ( + Annotated, Any, + Callable, + Literal, ) -from typing_extensions import Self +import dask.array as da +import numpy as np +from loguru import logger +from numpy.typing import NDArray -from .axis import BATCH_AXIS_ID, AxisId, PerAxis -from .block_meta import BlockMeta, LinearAxisTransform, split_shape_into_blocks +from bioimageio.core._utils.type_hints import NestedList, cast_and_assert +from bioimageio.spec._internal.utils import SLOTS + +from ._axis_annotations import PerAxisAnno +from .axis import TIME_AXIS_ID, AxisId, PerAxis from .common import ( + PROCESSING_CHUNK_MIN_BYTES, + BlockLoc, Halo, HaloLike, + MemberId, PadMode, - TotalNumberOfBlocks, + PadWidth, + PerMember, + SliceInfo, + SliceInfoWithOverlap, ) from .tensor import Tensor -from .utils._multi_index import MultiIndex -@dataclass(frozen=True) -class Block(BlockMeta): - """A block/tile of a (larger) tensor""" +@dataclass +class LinearAxisTransform: + member: MemberId + axis: AxisId + scale: float + offset: int + + def compute(self, s: int, round: Callable[[float], int] = floor) -> int: + return round(s * self.scale) + self.offset + + +@dataclass(frozen=True, **SLOTS) +class BlockMeta: + """A block of data within a sample + + Figure for illustration: + The first 2d block (dashed) of a sample member (**bold**). + The inner slice (thin) is expanded by a halo in both dimensions on both sides. + The outer slice reaches from the sample member origin (0, 0) to the right halo point. + + ``` + first block (at the sample origin) + ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┐ + ╷ halo(left) ╷ + ╷ padding outside the sample ╷ + ╷ (0, 0)┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━┯━━━➔ + ╷ ┃ │ ╷ sample member + ╷ ┃ inner │ outer ╷ + ╷ ┃ region │ region ╷ + ╷ ┃ /slice │ /slice ╷ + ╷ ┃ │ ╷ + ╷ ┣─────────────────┘ ╷ + ╷ ┃ outer region/slice ╷ + ╷ ┃ halo(right) ╷ + └ ─ ─ ─ ─┃─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┘ + ⬇ + ``` + + Note: + - Inner and outer slices are specified in sample member coordinates. + - The outer_slice of a block at the sample edge may overlap by more than the + halo with the neighboring block (the inner slices will not overlap though). + + """ + + parent: PerAxis[int] | Tensor + """the full tensor this block is a part of, or its shape if the full tensor is not available""" + + inner_slice: PerAxisAnno[SliceInfo | SliceInfoWithOverlap] + """inner region (without halo) wrt the full tensor""" + + halo: PerAxisAnno[Halo] + """halo enlarging the inner region to the block's sizes""" - data: Tensor - """the block's tensor, e.g. a (padded) slice of some larger, original tensor""" + # block_loc: BlockLoc + # """the block index within the full tensor""" + + # block_loc_shape: BlockLocShape # TODO: rename block_loc_shape + # """The full tensor shape in numbers of blocks""" @property - def inner_data(self): - return self.data[self.local_slice] + def full_shape(self) -> PerAxis[int]: + """shape of the full tensor this block is a part of""" + if isinstance(self.parent, Tensor): + return self.parent.tagged_shape + else: + return self.parent - def __post_init__(self): - super().__post_init__() - assert not any(v == -1 for v in self.sample_shape.values()), self.sample_shape - for a, s in self.data.sizes.items(): - slice_ = self.local_slice[a] - halo = self.halo.get(a, Halo(0, 0)) - if s != (s_exp := halo.left + (slice_.stop - slice_.start) + halo.right): - raise ValueError( - f"Block data shape does not match block meta information for {a} axis: data size is {s} but expected {s_exp} (halo: {halo}, slice: {slice_})", + @property + def shape(self) -> PerAxis[int]: + """shape of the block (including halo)""" + return MappingProxyType( + { + a: s.stop - s.start + (sum(self.halo[a]) if a in self.halo else 0) + for a, s in self.inner_slice.items() + } + ) + + @property + def padding(self) -> PerAxis[PadWidth]: + """padding to realize the halo at the sample edge + where we cannot simply enlarge the inner slice""" + return MappingProxyType( + { + a: p + for a in self.inner_slice + if ( + p := PadWidth( + ( + self.halo[a].left + - (self.inner_slice[a].start - self.outer_slice[a].start) + if a in self.halo + else 0 + ), + ( + self.halo[a].right + - (self.outer_slice[a].stop - self.inner_slice[a].stop) + if a in self.halo + else 0 + ), + ) + ) + != PadWidth(0, 0) + } + ) + + @property + def outer_slice(self) -> PerAxis[SliceInfo]: + """slice of the outer block (including halo, excluding padding) wrt the sample""" + return MappingProxyType( + { + a: SliceInfo( + max( + 0, + self.inner_slice[a].start + - (self.halo[a].left if a in self.halo else 0), + ), + min( + self.full_shape[a], + self.inner_slice[a].stop + + (self.halo[a].right if a in self.halo else 0), + ), ) + for a in self.inner_slice + } + ) + + @property + def inner_shape(self) -> PerAxis[int]: + """axis lengths of the inner region (without halo)""" + return MappingProxyType( + {a: s.stop - s.start for a, s in self.inner_slice.items()} + ) + + @property + def local_slice(self) -> PerAxis[SliceInfo]: + """inner slice wrt the block, **not** the sample""" + return MappingProxyType( + { + a: ( + SliceInfo( + h.left, + h.left + self.inner_shape[a], + ) + if (h := self.halo.get(a)) is not None + else SliceInfo(0, self.inner_shape[a]) + ) + for a in self.inner_slice + } + ) + + @property + def dims(self) -> Collection[AxisId]: + return set(self.inner_shape) + + @property + def tagged_shape(self) -> PerAxis[int]: + """alias for shape""" + return self.shape + + @property + def inner_slice_wo_overlap(self) -> PerAxis[SliceInfo]: + """subslice of the inner slice, such that all `inner_slice_wo_overlap` can be + stiched together trivially to form the original sample. + + This can also be used to calculate statistics + without overrepresenting block edge regions.""" + return { + a: SliceInfo(s.start_wo_overlap, s.stop_wo_overlap) + if isinstance(s, SliceInfoWithOverlap) + else SliceInfo(s.start, s.stop) + for a, s in self.inner_slice.items() + } + + def __post_init__(self): + assert all(a in self.full_shape for a in self.inner_slice), ( + "block has axes not present in its full tensor shape" + ) + + assert all(a in self.inner_slice for a in self.halo), ( + "halo has axes not present in block" + ) + + if any( + s > self.full_shape[a] + sum(self.padding.get(a, PadWidth(0, 0))) + for a, s in self.shape.items() + ): + logger.warning( + "block {} larger than its full tensor shape {}", + self.shape, + self.full_shape, + ) - @classmethod - def from_sample_member( - cls, - sample_member: Tensor, - block: BlockMeta, - *, - pad_mode: PadMode, - ) -> Self: - block_data = sample_member[block.outer_slice] - padded_block_data = block_data.pad(block.padding, pad_mode) - - if block.unstacked_batch_slice is not None: - if not isinstance( - idx := block_data.data.indexes.get(BATCH_AXIS_ID), # pyright: ignore[reportUnknownVariableType] - MultiIndex, - ): - raise ValueError("Expected a MultiIndex for the batch axis") - - padded_block_data = padded_block_data.assign_batch_multi_index(idx) - assert block.unstacked_sample_batch_shape is not None - assert block.inner_slice[BATCH_AXIS_ID] == block.outer_slice[BATCH_AXIS_ID] - - return cls( - data=padded_block_data, - sample_shape=sample_member.tagged_shape, - unstacked_sample_batch_shape=block.unstacked_sample_batch_shape, - unstacked_dim_order=block.unstacked_dim_order, - inner_slice=block.inner_slice, - unstacked_batch_slice=block.unstacked_batch_slice, - halo=block.halo, - block_index=block.block_index, - blocks_in_sample=block.blocks_in_sample, - ) - - def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: - raise NotImplementedError - - @classmethod - def from_meta(cls, meta: BlockMeta, data: Tensor) -> Self: - return cls( - sample_shape=meta.sample_shape, - unstacked_sample_batch_shape=meta.unstacked_sample_batch_shape, - unstacked_dim_order=meta.unstacked_dim_order, - inner_slice=meta.inner_slice, - unstacked_batch_slice=meta.unstacked_batch_slice, - halo=meta.halo, - block_index=meta.block_index, - blocks_in_sample=meta.blocks_in_sample, - data=data, - ) - - def get_meta(self) -> BlockMeta: - return BlockMeta( - sample_shape=self.sample_shape, - unstacked_sample_batch_shape=self.unstacked_sample_batch_shape, - unstacked_dim_order=self.unstacked_dim_order, - inner_slice=self.inner_slice, - unstacked_batch_slice=self.unstacked_batch_slice, - halo=self.halo, - block_index=self.block_index, - blocks_in_sample=self.blocks_in_sample, + # def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: + # assert BATCH_AXIS_ID not in new_axes, "cannot transform the batch axis" + + # full_shape = { + # a: (trf if isinstance(trf, int) else trf.compute(self.full_shape[trf.axis])) + # for a, trf in new_axes.items() + # } + # assert full_shape.get(BATCH_AXIS_ID) == self.full_shape.get(BATCH_AXIS_ID), ( + # "block transform must not change the batch axis size" + # ) + + # inner_slice = { + # a: ( + # SliceInfo(0, trf) + # if isinstance(trf, int) + # else SliceInfo( + # trf.compute(self.inner_slice[trf.axis].start), + # trf.compute(self.inner_slice[trf.axis].stop), + # ) + # ) + # for a, trf in new_axes.items() + # } + # assert inner_slice.get(BATCH_AXIS_ID) == self.inner_slice.get(BATCH_AXIS_ID), ( + # "block transform must not change the batch axis slice" + # ) + + # return self.__class__( + # parent=full_shape, + # inner_slice=inner_slice, + # halo={ + # a: ( + # Halo(0, 0) + # if isinstance(trf, int) + # else Halo(self.halo[trf.axis].left, self.halo[trf.axis].right) + # ) + # for a, trf in new_axes.items() + # }, + # ) + + # def get_transformed( + # self, transformed_tensor: Tensor, new_axes: PerAxis[LinearAxisTransform | int] + # ) -> Self: + # assert BATCH_AXIS_ID not in new_axes, "cannot transform the batch axis" + + # full_shape = { + # a: (trf if isinstance(trf, int) else trf.compute(self.full_shape[trf.axis])) + # for a, trf in new_axes.items() + # } + # assert full_shape.get(BATCH_AXIS_ID) == self.full_shape.get(BATCH_AXIS_ID), ( + # "block transform must not change the batch axis size" + # ) + + # inner_slice = { + # a: ( + # SliceInfo(0, trf) + # if isinstance(trf, int) + # else SliceInfo( + # trf.compute(self.inner_slice[trf.axis].start), + # trf.compute(self.inner_slice[trf.axis].stop), + # ) + # ) + # for a, trf in new_axes.items() + # } + # assert inner_slice.get(BATCH_AXIS_ID) == self.inner_slice.get(BATCH_AXIS_ID), ( + # "block transform must not change the batch axis slice" + # ) + + # return self.__class__( + # full_tensor=transformed_tensor, + # inner_slice=inner_slice, + # halo={ + # a: ( + # Halo(0, 0) + # if isinstance(trf, int) + # else Halo(self.halo[trf.axis].left, self.halo[trf.axis].right) + # ) + # for a, trf in new_axes.items() + # }, + # block_loc=self.block_loc, + # block_loc_shape=self.block_loc_shape, + # ) + + +@dataclass(frozen=True, **SLOTS) +class Block(BlockMeta): + """A block of data within a sample + + Figure for illustration: + The first 2d block (dashed) of a sample member (**bold**). + The inner slice (thin) is expanded by a halo in both dimensions on both sides. + The outer slice reaches from the sample member origin (0, 0) to the right halo point. + + ``` + first block (at the sample origin) + ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┐ + ╷ halo(left) ╷ + ╷ padding outside the sample ╷ + ╷ (0, 0)┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━┯━━━➔ + ╷ ┃ │ ╷ sample member + ╷ ┃ inner │ outer ╷ + ╷ ┃ region │ region ╷ + ╷ ┃ /slice │ /slice ╷ + ╷ ┃ │ ╷ + ╷ ┣─────────────────┘ ╷ + ╷ ┃ outer region/slice ╷ + ╷ ┃ halo(right) ╷ + └ ─ ─ ─ ─┃─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┘ + ⬇ + ``` + + Note: + - Inner and outer slices are specified in sample member coordinates. + - The outer_slice of a block at the sample edge may overlap by more than the + halo with the neighboring block (the inner slices will not overlap though). + + """ + + parent: Tensor + """the full tensor this block is a part of""" + + pad_mode: PadMode | Literal["none"] + """The padding mode to use when the halo extends beyond the tensor boundary. + + A ValueError is raised when the halo extends beyond the tensor boundary and `pad_mode` is "none".""" + + def __post_init__(self): + super().__post_init__() + assert not self.padding or self.pad_mode != "none", ( + "block requires padding but `pad_mode` is 'none'" ) + @property + def outer_tensor(self) -> Tensor: + """the outer tensor of the block (including halo and possibly padding)""" + outer_tensor = self.parent[self.outer_slice] + if self.padding: + if self.pad_mode == "none": + raise ValueError("block requires padding but `pad_mode` is 'none'") + outer_tensor = outer_tensor.pad(self.padding, mode=self.pad_mode) + + return outer_tensor + + +BlockArray = Annotated[NDArray[np.object_], "NDArray[Block]"] + def split_tensor_into_blocks( tensor: Tensor, - block_shape: PerAxis[int], *, + block_shape: PerAxis[int], halo: PerAxis[HaloLike], - pad_mode: PadMode, -) -> tuple[TotalNumberOfBlocks, Generator[Block, Any, None]]: - """divide a sample tensor into tensor blocks.""" - if (idx := tensor.batch_multi_index) is None: - unstacked_batch_shape = None - else: - unstacked_batch_shape = { - BATCH_AXIS_ID if a == "original_batch" else AxisId(a): s - for a, s in zip(idx.names, idx.levshape) - } + pad_mode: PadMode | Literal["none"], + block_shape_factors: PerAxis[int] | None, +) -> BlockArray: + """ + Args: + item_size: size of a single element in bytes, used to find shard and chunk sizes + """ + if unknown_dims := [ + a for a in block_shape if a not in tensor.dims and block_shape[a] != 1 + ]: + raise ValueError( + f"unknown dims in block_shape: {unknown_dims} for tensor {tensor.dims}" + ) + + if unknown_halo_dims := [a for a in halo if a not in tensor.dims]: + raise ValueError( + f"unknown dims in halo: {unknown_halo_dims} for tensor {tensor.dims}" + ) + + if isinstance(pad_mode, Mapping) and ( + unknown_pad_dims := [a for a in pad_mode if a not in tensor.dims] + ): + raise ValueError( + f"unknown dims in pad_mode: {unknown_pad_dims} for tensor {tensor.dims}" + ) + + if large_block_dims := [ + (a, b, shape_size_limit) + for a in block_shape + if (shape_size_limit := tensor.tagged_shape.get(a, 1)) < (b := block_shape[a]) + ]: + logger.warning( + "Block shape dimensions {} larger than shape dimensions {} are limiting the shape size", + {a: b for a, b, _s in large_block_dims}, + {a: s for a, _b, s in large_block_dims}, + ) + + block_shape = {d: block_shape.get(d, s) for d, s in tensor.tagged_shape.items()} + if any(block_shape[d] > tensor.tagged_shape[d] for d in tensor.dims): + raise ValueError( + f"block_shape {block_shape} larger than tensor shape {tensor.tagged_shape}." + ) + + if block_shape_factors is None and any( + tensor.tagged_shape[d] % block_shape[d] != 0 for d in tensor.dims + ): + raise ValueError( + f"tensor shape {tensor.tagged_shape} not divisible by block_shape {block_shape} and `block_shape_factors` is None." + ) + + # fill in default halo (0) + halo = {d: Halo.create(halo.get(d, 0)) for d in tensor.dims} + + inner_1d_slices: dict[AxisId, list[SliceInfo | SliceInfoWithOverlap]] = {} + inner_sizes = {d: block_shape[d] - sum(halo[d]) for d in tensor.dims} + for d in tensor.dims: + valid_edge_left = halo[d].left if pad_mode == "none" else 0 + valid_edge_right = halo[d].right if pad_mode == "none" else 0 + shape_size_limit = tensor.tagged_shape[d] - valid_edge_right + inner_size = inner_sizes[d] + block_shape_factor = ( + block_shape_factors.get(d, block_shape[d]) + if block_shape_factors is not None + else None + ) + inner_1d_slices[d] = [ + SliceInfo(p, p + inner_size) + if p + inner_size <= shape_size_limit + or block_shape_factor + is None # redundant for typing as we check for divisiblity above for `block_shape_factors is None` + else SliceInfo(p, shape_size_limit) + if (shape_size_limit - p) % block_shape_factor == 0 + else SliceInfoWithOverlap( + shape_size_limit + - ( + block_shape_factor + * ceil((shape_size_limit - p) / block_shape_factor) + ) + + sum(halo[d]), + shape_size_limit, + p, + shape_size_limit, + ) + for p in range(valid_edge_left, shape_size_limit, inner_size) + ] + + out_shape = tuple(len(slices) for slices in inner_1d_slices.values()) + out: BlockArray = np.empty(out_shape, dtype=object) + for idx in itertools.product(*(range(s) for s in out_shape)): + block_loc: BlockLoc = idx + inner_slice = {d: inner_1d_slices[d][i] for d, i in zip(tensor.dims, block_loc)} + out[idx] = Block( + parent=tensor, + inner_slice=inner_slice, + halo=halo, + # block_loc=block_loc, + # block_loc_shape=out_shape, + pad_mode=pad_mode, + ) - n_blocks, block_gen = split_shape_into_blocks( - tensor.tagged_shape, - unstacked_dim_order=None, - unstacked_batch_shape=unstacked_batch_shape, + return out + + +def split_multiple_tensors_into_blocks( + tensors: PerMember[Tensor], + block_shape: PerMember[PerAxis[int]], + *, + halo: PerMember[PerAxis[HaloLike]], + broadcast: bool = False, + pad_mode: PadMode | PerMember[PadMode] | Literal["none"], + block_shape_factors: PerMember[PerAxis[int] | None] | None, +) -> dict[MemberId, BlockArray]: + """Split multiple tensors (e.g. sample members) into aligned blocks. + + Args: + tensors: the tensors to split into blocks + block_shape: the block shape per sample member (depending on `broaadcast` the block shapes must result in the same number of blocks for all tensors) + halo: the halo around each block per sample member + broadcast: whether to allow broadcasting the resulting tensor blocks to each other, e.g. if one tensor does not have a dimension along which another is blocked up. + pad_mode: the padding mode (optionally per sample member) to use when the halo extends beyond the tensor boundary + block_shape_factors: The factors by which the block shape has to be divisible to allow smaller blocks. + Any axis not included in a given mapping is assumed to be set to the block shape. + If the tensor shape does not divide evenly by a block shape factor, the last two blocks will overlap along that axis. + If the tensor shape is smaller than a block shape factor, an exception is raised. + `None` means no smaller blocks are allowed, nor is overlap allowed; an exception is raised if the shape does not divide evenly. + To allow arbitrary small blocks, set the block shape factor to 1 for all axes, e.g. `defaultdict(lambda: defaultdict(lambda: 1))`. + """ + + if unknown_blocks := [m for m in block_shape if m not in tensors]: + raise ValueError( + f"block shape specified for unknown tensors: {unknown_blocks}." + ) + + if not block_shape: + block_shape = {m: t.tagged_shape for m, t in tensors.items()} + + if not broadcast and ( + missing_blocks := [m for m in tensors if m not in block_shape] + ): + raise ValueError( + f"no block shape specified for {missing_blocks}." + + " Set `broadcast` to True if these tensors should be repeated" + + " as a whole for each block." + ) + + if extra_halo := [m for m in halo if m not in block_shape]: + raise ValueError( + f"`halo` specified for tensors without block shape: {extra_halo}." + ) + + blocks: PerMember[BlockArray] = {} + for m in block_shape: + blocks[m] = split_tensor_into_blocks( + tensor=tensors[m], + block_shape=block_shape[m], + halo=halo.get(m, {}), + pad_mode=pad_mode.get(m, "symmetric") + if isinstance(pad_mode, Mapping) + else pad_mode, + block_shape_factors=block_shape_factors.get(m, {}) + if block_shape_factors is not None + else None, + ) + + assert len(blocks) > 0, blocks + unique_block_ndims = {b.shape for b in blocks.values()} + + if len(unique_block_ndims) != 1: + if not broadcast: + raise ValueError( + f"Mismatch for block layout across members: { {t: b.shape for t, b in blocks.items()} }. Set `broadcast` to True if this is expected." + ) + + blocks = dict(zip(blocks.keys(), np.broadcast_arrays(*blocks.values()))) + block_locs = {m: b.shape for m, b in blocks.items()} + unique_block_locs = set(block_locs.values()) + assert len(unique_block_locs) == 1, ( + f"Mismatch for total number of blocks (after braodcasting?!): {block_locs}" + ) + + return blocks + + +def apply_blockwise( + func: Callable[ + [PerMember[Tensor]], + PerMember[Tensor | None], # TODO: return non-optional outputs only? + ], + inputs: PerMember[Tensor], + *, + block_shape: PerMember[PerAxis[int]], + pad_mode: PadMode | PerMember[PadMode], + input_halo: PerMember[PerAxis[Halo]], + output_halo: PerMember[PerAxis[Halo]], + get_output_shape: Callable[[PerMember[PerAxis[int]]], PerMember[PerAxis[int]]], + block_shape_factors: PerMember[PerAxis[int]] | None = MappingProxyType({}), + chunk_shape: PerMember[PerAxis[int]] | int = PROCESSING_CHUNK_MIN_BYTES, + blockwise_processing_mode: Literal["parallel", "sequential"] = "parallel", + chunkwise_processing_mode: Literal["parallel", "sequential"] = "parallel", +) -> dict[MemberId, Tensor]: + """Apply `func` to `inputs` blockwise. + + + Args: + func: the function to apply to each block + inputs: the input tensors to process + block_shape: the shape of the blocks to be processed by func + pad_mode: the padding mode (optionally per input member) to use when the halo extends beyond the tensor boundary + if "none" and `output_halo` is not empty, the output will be smaller by the `output_halo` + input_halo: the halo around each input block (e.g. to compenstate for `output_halo` such that cropped output blocks can be stitched together) + Within the sample halo is realized as overlap between neighboring blocks, outside the sample halo is realized as padding. + output_halo: the halo to be cropped from each output block + get_output_shape: a function to compute the output shape based on the input shape. + May return values <1 for output axes without halo, which means the output will be concatenated along those axes. + (Only used if the input shape does not evenly divide by the block shape to correct overlapping outputs.) + block_shape_factors: The factors by which the block shape has to be divisible to allow smaller blocks. + Any axis not included in a given mapping is assumed to be set to the block shape. + If the tensor shape does not divide evenly by a block shape factor, the last two blocks will overlap along that axis. + If the tensor shape is smaller than a block shape factor, an exception is raised. + `None` means no smaller blocks are allowed, nor is overlap allowed; an exception is raised if the shape does not divide evenly. + To allow arbitrary small blocks, set the block shape factor to 1 for all axes, e.g. `defaultdict(lambda: defaultdict(lambda: 1))`. + chunk_shape: the shape of the chunks to process. + Chunks are larger than blocks and help to reduce the number of tasks in the dask graph for 'parallel' processing. + Must be larger than `block_shape`. + If an integer is given, it is interpreted as the minimum number of bytes of any input chunk tensor. + chunkwise_processing_mode: (WIP) 'parallel' (via dask graph) or 'sequential' + blockwise_processing_mode: (WIP) 'parallel' (via dask graph; nested inside chunk task if chunksiwe_processing_mode='parallel') or 'sequential' + """ + + if isinstance(chunk_shape, int): + chunk_shape = get_chunk_shape( + inputs, block_shape=block_shape, chunk_min_bytes=chunk_shape + ) + if any( + chunk_shape[m][d] < block_shape[m][d] + for m in chunk_shape + for d in chunk_shape[m] + if m in block_shape and d in block_shape[m] + ): + raise ValueError( + f"chunk_shape {chunk_shape} must not be smaller than block_shape {block_shape}" + ) + + process_chunk = partial( + _apply_blockwise_impl, + func, + block_shape=block_shape, + pad_mode=pad_mode, + input_halo=input_halo, + output_halo=output_halo, + processing_mode=blockwise_processing_mode, + get_output_shape=get_output_shape, + block_shape_factors=block_shape_factors, + ) + + return _apply_blockwise_impl( + process_chunk, + inputs, + block_shape=chunk_shape, + pad_mode="none", + input_halo=input_halo, + output_halo=output_halo, + processing_mode=chunkwise_processing_mode, + block_shape_factors=block_shape_factors, + get_output_shape=get_output_shape, + ) + + +def _apply_blockwise_impl( + func: Callable[[PerMember[Tensor]], PerMember[Tensor | None]], + inputs: PerMember[Tensor], + *, + block_shape: PerMember[PerAxis[int]], + block_shape_factors: PerMember[PerAxis[int]] | None = MappingProxyType({}), + pad_mode: PadMode | PerMember[PadMode] | Literal["none"], + input_halo: PerMember[PerAxis[HaloLike]], + output_halo: PerMember[PerAxis[HaloLike]], + get_output_shape: Callable[[PerMember[PerAxis[int]]], PerMember[PerAxis[int]]], + processing_mode: Literal["parallel", "sequential"] = "parallel", +) -> dict[MemberId, Tensor]: + """ + Apply a function to a mapping (dict) of tensors, processing them in blocks. + + Args: + func: the function to apply to each block + inputs: the input tensors to process + block_shape: the shape of the blocks to be processed by func + block_shape_factors: The factors by which the block shape has to be divisible to allow smaller blocks. + Any axis not included in a given mapping is assumed to be set to the block shape. + If the tensor shape does not divide evenly by a block shape factor, the last two blocks will overlap along that axis. + If the tensor shape is smaller than a block shape factor, an exception is raised. + `None` means no smaller blocks are allowed, nor is overlap allowed; an exception is raised if the shape does not divide evenly. + To allow arbitrary small blocks, set the block shape factor to 1 for all axes, e.g. `defaultdict(lambda: defaultdict(lambda: 1))`. + pad_mode: the padding mode (optionally per input member) to use when the halo extends beyond the tensor boundary + if "none" and `output_halo` is not empty, the output will be smaller by the `output_halo` + input_halo: the halo around each input block (e.g. to compenstate for `output_halo` such that cropped output blocks can be stitched together) + Within the sample halo is realized as overlap between neighboring blocks, outside the sample halo is realized as padding. + output_halo: the halo to be cropped from each output block + get_output_shape: a function to compute the output shape based on the input shape + (only used if the input shape does not evenly divide by the block shape to correct overlapping outputs) + processing_mode: the mode of blockwise processing ('parallel' (via dask graph) or 'sequential') + """ + input_blocks = split_multiple_tensors_into_blocks( + inputs, block_shape=block_shape, - halo=halo, + halo=input_halo, + broadcast=True, + pad_mode=pad_mode, + block_shape_factors=block_shape_factors, + ) + + return apply_to_block_array( + func, + input_blocks, + output_halo=output_halo, + processing_mode=processing_mode, + get_output_shape=get_output_shape, ) - return n_blocks, _block_generator(tensor, block_gen, pad_mode=pad_mode) -def _block_generator(sample: Tensor, blocks: Iterable[BlockMeta], *, pad_mode: PadMode): - for block in blocks: - yield Block.from_sample_member(sample, block, pad_mode=pad_mode) +def apply_to_block_array( + func: Callable[[PerMember[Tensor]], PerMember[Tensor | None]], + input_blocks: PerMember[BlockArray], + *, + output_halo: PerMember[PerAxis[HaloLike]], + processing_mode: Literal["parallel", "sequential"] = "parallel", + get_output_shape: Callable[[PerMember[PerAxis[int]]], PerMember[PerAxis[int]]], +) -> dict[MemberId, Tensor]: + + nested_outputs = apply_to_blocks( + func, + input_blocks, + output_halo=output_halo, + processing_mode=processing_mode, + get_output_shape=get_output_shape, + ) + + def select_arrays( + nested: NestedList[dict[MemberId, Tensor]], member: MemberId + ) -> NestedList[NDArray[Any] | da.Array]: + if isinstance(nested, list): + return [select_arrays(item, member) for item in nested] + else: + return nested[member].data.data + + first_block = nested_outputs + while isinstance(first_block, list): + first_block = first_block[0] + + return { + output_id: Tensor( + da.block( + select_arrays(nested_outputs, output_id), + ), + dims=first_block[output_id].dims, + ) + for output_id in first_block + } + + +def apply_to_blocks( + func: Callable[[PerMember[Tensor]], PerMember[Tensor | None]], + blocks: PerMember[BlockArray], + *, + output_halo: PerMember[PerAxis[HaloLike]], + processing_mode: Literal["parallel", "sequential"] = "parallel", + get_output_shape: Callable[[PerMember[PerAxis[int]]], PerMember[PerAxis[int]]], +) -> NestedList[dict[MemberId, Tensor]]: + if not all(b.size > 0 for b in blocks.values()): + raise ValueError( + "Sample is not split into blocks. Please provide `block_shapes` or split the sample first." + ) + + block_ndims = {m: b.shape for m, b in blocks.items()} + if len(set(block_ndims.values())) != 1: + raise ValueError(f"Input block grids do not match: {block_ndims}") + + block_grid_shape = next(iter(block_ndims.values())) + + output_halo = { + m: {a: Halo.create(h) for a, h in h_per_a.items()} + for m, h_per_a in output_halo.items() + } + + # TODO: differentiate between parallel and sequential processing modes + def func_blocks( + axis: int = 0, loc: tuple[int, ...] = () + ) -> NestedList[dict[MemberId, Tensor]]: + if axis < len(block_grid_shape): + grid_size = block_grid_shape[axis] + return [func_blocks(axis + 1, (*loc, i)) for i in range(grid_size)] + + left_input_overlap = { + a + for b in blocks.values() + for a, s in cast_and_assert(Block, b[loc]).inner_slice.items() + if isinstance(s, SliceInfoWithOverlap) and s.start_wo_overlap != s.start + } + right_input_overlap = { + a + for b in blocks.values() + for a, s in cast_and_assert(Block, b[loc]).inner_slice.items() + if isinstance(s, SliceInfoWithOverlap) and s.stop_wo_overlap != s.stop + } + if right_input_overlap: + # we currently do not use overlap to the right... + raise NotImplementedError( + "Output block overlap to the right is not implemented." + ) + if left_input_overlap: + outer_input_shape_wo_overlap = { + m: { + a: s.stop + - s.start + + sum(cast_and_assert(Block, b[loc]).halo.get(a, (0,))) + for a, s in cast_and_assert( + Block, b[loc] + ).inner_slice_wo_overlap.items() + } + for m, b in blocks.items() + } + inner_output_shape_no_overlap_left = { + m: { + a: None if ss < 1 else ss - sum(output_halo.get(m, {}).get(a, (0,))) + for a, ss in s.items() + } + for m, s in get_output_shape(outer_input_shape_wo_overlap).items() + } + else: + inner_output_shape_no_overlap_left = None + + ret: dict[MemberId, Tensor] = {} + for m, t in func({m: b[loc].outer_tensor for m, b in blocks.items()}).items(): + if t is None: + continue + + # crop halo to keep inner output + t = t.crop_halo(output_halo.get(m)) + + # crop inner output overlap + if inner_output_shape_no_overlap_left is not None: + output_slice_no_overlap_left = { + a: SliceInfo(start=t.tagged_shape[a] - s, stop=t.tagged_shape[a]) + for a, s in inner_output_shape_no_overlap_left[m].items() + if s is not None + } + t = t[output_slice_no_overlap_left] + + ret[m] = t + + return ret + + return func_blocks() + + +def get_chunk_shape( + tensors: PerMember[Tensor], + block_shape: PerMember[PerAxis[int]], + chunk_min_bytes: int = PROCESSING_CHUNK_MIN_BYTES, +) -> PerMember[PerAxis[int]]: + if unknown_members := [m for m in block_shape if m not in tensors]: + raise ValueError( + f"Block shape specified for unknown tensors: {unknown_members}." + ) + + # default to 1 for missing block dimensions, and map -1 to tensor shape + block_shape = { + m: { + d: t.tagged_shape[d] + if (b_s := block_shape.get(m, {}).get(d, 1)) == -1 + else b_s + for d in t.dims + } + for m, t in tensors.items() + } + dim_sizes = {d: d_s for b_s in block_shape.values() for d, d_s in b_s.items()} + + if not any( + a in tensors[m].tagged_shape for m, b_s in block_shape.items() for a in b_s + ): + raise ValueError( + f"Block shape {block_shape} has dimensions not present in sample shape { {m: tensors[m].tagged_shape for m in tensors} }" + ) + + chunk_shape = deepcopy(block_shape) + item_sizes = {m: np.dtype(t.dtype).itemsize for m, t in tensors.items()} + + def get_max_chunk_size(): + return max(prod(chunk_shape[m].values()) * item_sizes[m] for m in chunk_shape) + + def increment_chunk_dims(incr_dims: Sequence[AxisId]): + incr_dims = list(incr_dims) + while incr_dims: + for a in incr_dims: + if get_max_chunk_size() >= chunk_min_bytes: + return + for m in tensors: + chunk_shape[m][a] = min( + tensors[m].tagged_shape[a], + chunk_shape[m][a] + block_shape[m][a], + ) + + if all( + chunk_shape[m][a] == tensors[m].tagged_shape[a] + for m in chunk_shape + if a in chunk_shape[m] + ): + incr_dims.remove(a) + + # increment chunk dimensions in order of non-singleton spatial, time, other, then any singleton dim + non_singleton_spatial_dims = [ + d for d in (AxisId("x"), AxisId("y"), AxisId("z")) if dim_sizes.get(d, 0) > 1 + ] + increment_chunk_dims(non_singleton_spatial_dims) + + non_singleton_time_dims = [d for d in (TIME_AXIS_ID,) if dim_sizes.get(d, 0) > 1] + increment_chunk_dims(non_singleton_time_dims) + + other_non_singleton_dims = [d for d, s in dim_sizes.items() if s > 1] + increment_chunk_dims(other_non_singleton_dims[::-1]) + + singleton_dims = [d for d, s in dim_sizes.items() if s <= 1] + increment_chunk_dims(singleton_dims[::-1]) + + logger.info( + "Calculated chunk shape {} ({} MiB) for block shape {} and sample shape {}", + chunk_shape, + get_max_chunk_size(), + block_shape, + {m: dict(t.tagged_shape) for m, t in tensors.items()}, + ) + return chunk_shape + + +# def get_transformed_block_arrays( +# input_blocks: PerMember[BlockArray], output_axes: PerMember[PerAxis[LinearAxisTransform | int]] +# ) -> PerMember[BlockArray]: +# first_block = {m: next(iter(cast(Iterator[Block], b.flat))) for m, b in input_blocks.items()} + +# all_input_blocks_ndims = {m: b.shape for m, b in input_blocks.items()} +# input_blocks_ndims = next(iter(all_input_blocks_ndims.values())) +# assert all(s == input_blocks_ndims for s in all_input_blocks_ndims.values()), ( +# f"Input block grids do not match: {all_input_blocks_ndims}" +# ) + +# def transform_shape(input_shape: PerMember[PerAxis[int]]): +# return { +# m: { +# a: ( +# trf +# if isinstance(trf, int) +# else trf.compute(input_shape[trf.member][trf.axis]) +# ) +# for a, trf in output_axes[m].items() +# } +# for m in output_axes +# } +# output_shape = transform_shape({m: first_block[m].full_shape for m in input_blocks}) + +# # output shape in number of blocks +# output_blocks_ndims = transform_shape({m: dict(zip(first_block[m].dims, b.shape)) for m, b in input_blocks.items()}) + +# output_blocks: PerMember[BlockArray] = {m: np.empty(tuple(v.values()), dtype=object) for m, v in output_blocks_ndims.items()} +# assert prod(output_blocks_ndims.values()) +# def get_member_halo(block_idx: tuple[int, ...], m: MemberId, round: Callable[[float], int]): +# return { +# a: ( +# Halo(0, 0) +# if isinstance(trf, int) +# or trf.axis not in input_blocks[trf.member][block_idx].halo +# else Halo( +# round(input_blocks[trf.member][block_idx].halo[trf.axis].left * trf.scale), +# round(input_blocks[trf.member][block_idx].halo[trf.axis].right * trf.scale), +# ) +# ) +# for a, trf in output_axes[m].items() +# } + +# example_input_key = next(iter(first_block)) +# example_input_ +# for idx in itertools.product(*(range(s) for s in output_blocks_ndims)): +# pass + + +# halo: dict[MemberId, dict[AxisId, Halo]] = {} +# for m in output_axes: +# halo[m] = get_member_halo(input_blocks[m], m, floor) +# if halo[m] != get_member_halo(input_blocks[m], m, ceil): +# raise ValueError( +# f"failed to unambiguously scale halo {halo[m]} with {output_axes[m]}" +# + f" for {m}." +# ) + +# inner_slice = { +# m: { +# a: ( +# SliceInfo(0, trf) +# if isinstance(trf, int) +# else SliceInfo( +# trf.compute( +# self.blocks[trf.member].inner_slice[trf.axis].start +# ), +# trf.compute(self.blocks[trf.member].inner_slice[trf.axis].stop), +# ) +# ) +# for a, trf in output_axes[m].items() +# } +# for m in output_axes +# } +# unstacked_batch_slice = None +# for b in self.blocks.values(): +# if b.unstacked_batch_slice is None: +# continue +# elif unstacked_batch_slice is None: +# unstacked_batch_slice = b.unstacked_batch_slice +# else: +# assert unstacked_batch_slice == b.unstacked_batch_slice, ( +# "Incompatible unstacked batch slices across sample block members" +# ) + +# return { +# m: Block( +# parent=output_shape[m], +# inner_slice=inner_slice[m], +# halo=halo[m], +# ) +# for m in output_axes +# } diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py index 4fbbe58e..914afa1e 100644 --- a/src/bioimageio/core/block_meta.py +++ b/src/bioimageio/core/block_meta.py @@ -1,468 +1,541 @@ -from __future__ import annotations - -import itertools -from collections.abc import Collection, Generator, Iterable, Sequence -from dataclasses import dataclass -from functools import cached_property -from math import floor, prod -from types import MappingProxyType -from typing import ( - Any, - Callable, -) - -import pydantic -from loguru import logger -from typing_extensions import Self - -from ._axis_annotations import PerAxisAnno -from .axis import BATCH_AXIS_ID, AxisId, PerAxis -from .common import ( - BlockIndex, - Halo, - HaloLike, - MemberId, - PadWidth, - PerMember, - SliceInfo, - TotalNumberOfBlocks, -) -from .utils._multi_index import ( - MultiIndex, - get_unstacked_dim_order, - get_unstacked_shape, -) - - -@dataclass -class LinearAxisTransform: - axis: AxisId - scale: float - offset: int - - def compute(self, s: int, round: Callable[[float], int] = floor) -> int: - return round(s * self.scale) + self.offset - - -@pydantic.dataclasses.dataclass(frozen=True) -class BlockMeta: - """Block meta data of a sample member (a tensor in a sample) - - Figure for illustration: - The first 2d block (dashed) of a sample member (**bold**). - The inner slice (thin) is expanded by a halo in both dimensions on both sides. - The outer slice reaches from the sample member origin (0, 0) to the right halo point. - - ``` - first block (at the sample origin) - ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┐ - ╷ halo(left) ╷ - ╷ padding outside the sample ╷ - ╷ (0, 0)┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━┯━━━➔ - ╷ ┃ │ ╷ sample member - ╷ ┃ inner │ outer ╷ - ╷ ┃ region │ region ╷ - ╷ ┃ /slice │ /slice ╷ - ╷ ┃ │ ╷ - ╷ ┣─────────────────┘ ╷ - ╷ ┃ outer region/slice ╷ - ╷ ┃ halo(right) ╷ - └ ─ ─ ─ ─┃─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┘ - ⬇ - ``` - - Note: - - Inner and outer slices are specified in sample member coordinates. - - The outer_slice of a block at the sample edge may overlap by more than the - halo with the neighboring block (the inner slices will not overlap though). - - """ - - sample_shape: PerAxisAnno[int] - """the axis sizes of the whole (unblocked) sample""" - - unstacked_sample_batch_shape: PerAxisAnno[int] | None - """the axis sizes of the stacked axes (that make up the sample batch axis)""" - - unstacked_dim_order: Sequence[AxisId] | None - """The order of dimensions of the unstacked tensor, if the sample has a multi-index""" - - inner_slice: PerAxisAnno[SliceInfo] - """inner region (without halo) wrt the sample""" - - unstacked_batch_slice: PerAxisAnno[SliceInfo] | None - """inner/outer slice along the unstacked batch dimension wrt the (unstacked) sample""" - - halo: PerAxisAnno[Halo] - """halo enlarging the inner region to the block's sizes""" - - block_index: BlockIndex - """the i-th block of the sample""" - - blocks_in_sample: TotalNumberOfBlocks - """total number of blocks in the sample""" - - @cached_property - def shape(self) -> PerAxis[int]: - """axis lengths of the block""" - return MappingProxyType( - { - a: s.stop - s.start + (sum(self.halo[a]) if a in self.halo else 0) - for a, s in self.inner_slice.items() - } - ) - - @property - def unstacked_sample_shape(self) -> PerAxis[int]: - """combined (stacked) sample shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" - return get_unstacked_shape( - self.sample_shape, - unstacked_dim_order=self.unstacked_dim_order, - unstacked_batch_shape=self.unstacked_sample_batch_shape, - ) - - @cached_property - def padding(self) -> PerAxis[PadWidth]: - """padding to realize the halo at the sample edge - where we cannot simply enlarge the inner slice""" - return MappingProxyType( - { - a: PadWidth( - ( - self.halo[a].left - - (self.inner_slice[a].start - self.outer_slice[a].start) - if a in self.halo - else 0 - ), - ( - self.halo[a].right - - (self.outer_slice[a].stop - self.inner_slice[a].stop) - if a in self.halo - else 0 - ), - ) - for a in self.inner_slice - } - ) - - @cached_property - def outer_slice(self) -> PerAxis[SliceInfo]: - """slice of the outer block (without padding) wrt the sample""" - return MappingProxyType( - { - a: SliceInfo( - max( - 0, - min( - self.inner_slice[a].start - - (self.halo[a].left if a in self.halo else 0), - self.sample_shape[a] - - self.inner_shape[a] - - (self.halo[a].left if a in self.halo else 0), - ), - ), - min( - self.sample_shape[a], - self.inner_slice[a].stop - + (self.halo[a].right if a in self.halo else 0), - ), - ) - for a in self.inner_slice - } - ) - - @cached_property - def inner_shape(self) -> PerAxis[int]: - """axis lengths of the inner region (without halo)""" - return MappingProxyType( - {a: s.stop - s.start for a, s in self.inner_slice.items()} - ) - - @cached_property - def local_slice(self) -> PerAxis[SliceInfo]: - """inner slice wrt the block, **not** the sample""" - return MappingProxyType( - { - a: ( - SliceInfo( - h.left, - h.left + self.inner_shape[a], - ) - if (h := self.halo.get(a)) is not None - else SliceInfo(0, self.inner_shape[a]) - ) - for a in self.inner_slice - } - ) - - @property - def dims(self) -> Collection[AxisId]: - return set(self.inner_shape) - - @property - def tagged_shape(self) -> PerAxis[int]: - """alias for shape""" - return self.shape - - @property - def inner_slice_wo_overlap(self) -> PerAxis[SliceInfo]: - """subslice of the inner slice, such that all `inner_slice_wo_overlap` can be - stiched together trivially to form the original sample. - - This can also be used to calculate statistics - without overrepresenting block edge regions.""" - # TODO: update inner_slice_wo_overlap when adding block overlap - return self.inner_slice - - def __post_init__(self): - assert all(a in self.sample_shape for a in self.inner_slice), ( - "block has axes not present in sample" - ) - - assert all(a in self.inner_slice for a in self.halo), ( - "halo has axes not present in block" - ) - - if any(s > self.sample_shape[a] for a, s in self.shape.items()): - logger.warning( - "block {} larger than sample {}", self.shape, self.sample_shape - ) - - def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: - assert BATCH_AXIS_ID not in new_axes, "cannot transform the batch axis" - - sample_shape = { - a: ( - trf - if isinstance(trf, int) - else trf.compute(self.sample_shape[trf.axis]) - ) - for a, trf in new_axes.items() - } - assert sample_shape.get(BATCH_AXIS_ID) == self.sample_shape.get( - BATCH_AXIS_ID - ), "block transform must not change the batch axis size" - - inner_slice = { - a: ( - SliceInfo(0, trf) - if isinstance(trf, int) - else SliceInfo( - trf.compute(self.inner_slice[trf.axis].start), - trf.compute(self.inner_slice[trf.axis].stop), - ) - ) - for a, trf in new_axes.items() - } - assert inner_slice.get(BATCH_AXIS_ID) == self.inner_slice.get(BATCH_AXIS_ID), ( - "block transform must not change the batch axis slice" - ) - - return self.__class__( - sample_shape=sample_shape, - unstacked_sample_batch_shape=self.unstacked_sample_batch_shape, - unstacked_dim_order=get_unstacked_dim_order( - tuple(sample_shape), - unstacked_reference_order=self.unstacked_dim_order, - stacked_dims=None - if self.unstacked_sample_batch_shape is None - else tuple(self.unstacked_sample_batch_shape), - ), - inner_slice=inner_slice, - unstacked_batch_slice=self.unstacked_batch_slice, - halo={ - a: ( - Halo(0, 0) - if isinstance(trf, int) - else Halo(self.halo[trf.axis].left, self.halo[trf.axis].right) - ) - for a, trf in new_axes.items() - }, - block_index=self.block_index, - blocks_in_sample=self.blocks_in_sample, - ) - - -def split_shape_into_blocks( - shape: PerAxis[int], - *, - unstacked_batch_shape: PerAxis[int] | None, - block_shape: PerAxis[int], - halo: PerAxis[HaloLike], - unstacked_dim_order: Sequence[AxisId] | None, -) -> tuple[TotalNumberOfBlocks, Generator[BlockMeta, Any, None]]: - if unknown_axes := [ - a for a in block_shape if a not in shape and block_shape[a] != 1 - ]: - raise ValueError( - f"unknown axes in block_shape: {unknown_axes} for shape {shape}" - ) - - if large_block_dims := [ - (a, b, s) for a in block_shape if (s := shape.get(a, 1)) < (b := block_shape[a]) - ]: - logger.warning( - "Block shape dimensions {} larger than shape dimensions {} are limiting the shape size", - {a: b for a, b, _s in large_block_dims}, - {a: s for a, _b, s in large_block_dims}, - ) - - block_shape = {a: min(s, block_shape.get(a, s)) for a, s in shape.items()} - assert all(a in shape for a in halo), (tuple(shape), set(halo)) - - # fill in default halo (0) and block axis length (from tensor shape) - halo = {a: Halo.create(halo.get(a, 0)) for a in shape} - - inner_1d_slices: dict[AxisId, list[SliceInfo]] = {} - for a, s in shape.items(): - inner_size = block_shape[a] - sum(halo[a]) - inner_1d_slices[a] = [ - SliceInfo(min(p, s - inner_size), min(p + inner_size, s)) - for p in range(0, s, inner_size) - ] - - n_blocks = prod(map(len, inner_1d_slices.values())) - - return n_blocks, _block_meta_generator( - shape, - unstacked_batch_shape, - blocks_in_sample=n_blocks, - unstacked_dim_order=unstacked_dim_order, - inner_1d_slices=inner_1d_slices, - halo=halo, - ) - - -def _block_meta_generator( - sample_shape: PerAxis[int], - unstacked_sample_batch_shape: PerAxis[int] | None, - *, - unstacked_dim_order: Sequence[AxisId] | None, - blocks_in_sample: int, - inner_1d_slices: dict[AxisId, list[SliceInfo]], - halo: PerAxis[HaloLike], -): - assert all(a in sample_shape for a in halo) - - halo = {a: Halo.create(halo.get(a, 0)) for a in inner_1d_slices} - if unstacked_sample_batch_shape is None: - idx: MultiIndex | None = None - else: - idx = MultiIndex.from_product( # pyright: ignore[reportUnknownVariableType] - [ - range(unstacked_sample_batch_shape[a]) - for a in unstacked_sample_batch_shape - ], - names=list(unstacked_sample_batch_shape), - ) - assert isinstance(idx, MultiIndex) - - for i, nd_tile in enumerate(itertools.product(*inner_1d_slices.values())): - inner_1d_slices[BATCH_AXIS_ID] - inner_slice = dict(zip(inner_1d_slices, nd_tile)) - if idx is None: - unstacked_batch_slice = None - else: - batch_slice = inner_slice[BATCH_AXIS_ID] - unstacked_batch_slice: dict[AxisId, SliceInfo] | None = {} - for a, c in zip(idx.names, idx.codes): - a = BATCH_AXIS_ID if a == "original_batch" else AxisId(a) - a_start = int(c[batch_slice.start]) - a_stop = int(c[batch_slice.stop - 1]) + 1 - unstacked_batch_slice[a] = SliceInfo(a_start, a_stop) - - yield BlockMeta( - sample_shape=sample_shape, - unstacked_sample_batch_shape=unstacked_sample_batch_shape, - unstacked_dim_order=unstacked_dim_order, - inner_slice=inner_slice, - unstacked_batch_slice=unstacked_batch_slice, - halo=halo, - block_index=i, - blocks_in_sample=blocks_in_sample, - ) - - -def split_multiple_shapes_into_blocks( - shapes: PerMember[PerAxis[int]], - block_shapes: PerMember[PerAxis[int]], - *, - unstacked_batch_shape: PerAxis[int] | None, - unstacked_dim_order: PerMember[Sequence[AxisId] | None] | None, - halo: PerMember[PerAxis[HaloLike]], - broadcast: bool = False, -) -> tuple[TotalNumberOfBlocks, Iterable[PerMember[BlockMeta]]]: - if unknown_blocks := [t for t in block_shapes if t not in shapes]: - raise ValueError( - f"block shape specified for unknown tensors: {unknown_blocks}." - ) - - if not block_shapes: - block_shapes = shapes - - if not broadcast and ( - missing_blocks := [t for t in shapes if t not in block_shapes] - ): - raise ValueError( - f"no block shape specified for {missing_blocks}." - + " Set `broadcast` to True if these tensors should be repeated" - + " as a whole for each block." - ) - - if extra_halo := [t for t in halo if t not in block_shapes]: - raise ValueError( - f"`halo` specified for tensors without block shape: {extra_halo}." - ) - - blocks: dict[MemberId, Iterable[BlockMeta]] = {} - n_blocks: dict[MemberId, TotalNumberOfBlocks] = {} - for t in block_shapes: - n_blocks[t], blocks[t] = split_shape_into_blocks( - shape=shapes[t], - unstacked_batch_shape=unstacked_batch_shape, - unstacked_dim_order=unstacked_dim_order[t] - if unstacked_dim_order is not None - else None, - block_shape=block_shapes[t], - halo=halo.get(t, {}), - ) - assert n_blocks[t] > 0, n_blocks - - assert len(blocks) > 0, blocks - assert len(n_blocks) > 0, n_blocks - unique_n_blocks = set(n_blocks.values()) - n = max(unique_n_blocks) - if len(unique_n_blocks) == 2 and 1 in unique_n_blocks: - if not broadcast: - raise ValueError( - "Mismatch for total number of blocks due to unsplit (single block)" - + f" tensors: {n_blocks}. Set `broadcast` to True if you want to" - + " repeat unsplit (single block) tensors." - ) - - blocks = { - t: _repeat_single_block(block_gen, n) if n_blocks[t] == 1 else block_gen - for t, block_gen in blocks.items() - } - elif len(unique_n_blocks) != 1: - raise ValueError(f"Mismatch for total number of blocks: {n_blocks}") - - return n, _aligned_blocks_generator(n, blocks) - - -def _aligned_blocks_generator( - n: TotalNumberOfBlocks, blocks: dict[MemberId, Iterable[BlockMeta]] -): - iterators = {t: iter(gen) for t, gen in blocks.items()} - for _ in range(n): - yield {t: next(it) for t, it in iterators.items()} - - -def _repeat_single_block(block_generator: Iterable[BlockMeta], n: TotalNumberOfBlocks): - round_two = False - for block in block_generator: - assert not round_two - for _ in range(n): - yield block - - round_two = True +# from __future__ import annotations + +# import itertools +# from collections.abc import Collection, Iterator +# from dataclasses import dataclass +# from functools import cached_property +# from math import floor +# from types import MappingProxyType +# from typing import ( +# Annotated, +# Callable, +# Iterable, +# Iterator, +# ) + +# import numpy as np +# import pydantic +# from loguru import logger +# from numpy.typing import NDArray +# from typing_extensions import Self + +# from ._axis_annotations import PerAxisAnno +# from .axis import BATCH_AXIS_ID, AxisId, PerAxis +# from .common import ( +# BlockLoc, +# BlockLocShape, +# Halo, +# HaloLike, +# PadWidth, +# PerMember, +# SliceInfo, +# SliceInfoWithOverlap, +# ) + + +# @dataclass +# class LinearAxisTransform: +# axis: AxisId +# scale: float +# offset: int + +# def compute(self, s: int, round: Callable[[float], int] = floor) -> int: +# return round(s * self.scale) + self.offset + + +# @pydantic.dataclasses.dataclass(frozen=True) +# class BlockMeta: +# """Block meta data of a sample member (a tensor in a sample) + +# Figure for illustration: +# The first 2d block (dashed) of a sample member (**bold**). +# The inner slice (thin) is expanded by a halo in both dimensions on both sides. +# The outer slice reaches from the sample member origin (0, 0) to the right halo point. + +# ``` +# first block (at the sample origin) +# ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┐ +# ╷ halo(left) ╷ +# ╷ padding outside the sample ╷ +# ╷ (0, 0)┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━┯━━━➔ +# ╷ ┃ │ ╷ sample member +# ╷ ┃ inner │ outer ╷ +# ╷ ┃ region │ region ╷ +# ╷ ┃ /slice │ /slice ╷ +# ╷ ┃ │ ╷ +# ╷ ┣─────────────────┘ ╷ +# ╷ ┃ outer region/slice ╷ +# ╷ ┃ halo(right) ╷ +# └ ─ ─ ─ ─┃─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┘ +# ⬇ +# ``` + +# Note: +# - Inner and outer slices are specified in sample member coordinates. +# - The outer_slice of a block at the sample edge may overlap by more than the +# halo with the neighboring block (the inner slices will not overlap though). + +# """ + +# sample_shape: PerAxisAnno[int] +# """the axis sizes of the whole (unblocked) sample""" + +# inner_slice: PerAxisAnno[SliceInfo | SliceInfoWithOverlap] +# """inner region (without halo) wrt the sample""" + +# halo: PerAxisAnno[Halo] +# """halo enlarging the inner region to the block's sizes""" + +# block_loc: BlockLoc +# """the block index within the sample""" + +# block_loc_shape: BlockLocShape +# """The sample shape in numbers of blocks""" + +# @cached_property +# def shape(self) -> PerAxis[int]: +# """axis lengths of the block""" +# return MappingProxyType( +# { +# a: s.stop - s.start + (sum(self.halo[a]) if a in self.halo else 0) +# for a, s in self.inner_slice.items() +# } +# ) + +# # @property +# # def unstacked_sample_shape(self) -> PerAxis[int]: +# # """combined (stacked) sample shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" +# # return get_unstacked_shape( +# # self.sample_shape, +# # unstacked_dim_order=self.unstacked_dim_order, +# # unstacked_batch_shape=self.unstacked_sample_batch_shape, +# # ) + +# @cached_property +# def padding(self) -> PerAxis[PadWidth]: +# """padding to realize the halo at the sample edge +# where we cannot simply enlarge the inner slice""" +# return MappingProxyType( +# { +# a: p +# for a in self.inner_slice +# if ( +# p := PadWidth( +# ( +# self.halo[a].left +# - (self.inner_slice[a].start - self.outer_slice[a].start) +# if a in self.halo +# else 0 +# ), +# ( +# self.halo[a].right +# - (self.outer_slice[a].stop - self.inner_slice[a].stop) +# if a in self.halo +# else 0 +# ), +# ) +# ) +# != PadWidth(0, 0) +# } +# ) + +# @cached_property +# def outer_slice(self) -> PerAxis[SliceInfo]: +# """slice of the outer block (without padding) wrt the sample""" +# return MappingProxyType( +# { +# a: SliceInfo( +# max( +# 0, +# min( +# self.inner_slice[a].start +# - (self.halo[a].left if a in self.halo else 0), +# self.sample_shape[a] +# - self.inner_shape[a] +# - (self.halo[a].left if a in self.halo else 0), +# ), +# ), +# min( +# self.sample_shape[a], +# self.inner_slice[a].stop +# + (self.halo[a].right if a in self.halo else 0), +# ), +# ) +# for a in self.inner_slice +# } +# ) + +# @cached_property +# def inner_shape(self) -> PerAxis[int]: +# """axis lengths of the inner region (without halo)""" +# return MappingProxyType( +# {a: s.stop - s.start for a, s in self.inner_slice.items()} +# ) + +# @cached_property +# def local_slice(self) -> PerAxis[SliceInfo]: +# """inner slice wrt the block, **not** the sample""" +# return MappingProxyType( +# { +# a: ( +# SliceInfo( +# h.left, +# h.left + self.inner_shape[a], +# ) +# if (h := self.halo.get(a)) is not None +# else SliceInfo(0, self.inner_shape[a]) +# ) +# for a in self.inner_slice +# } +# ) + +# @property +# def dims(self) -> Collection[AxisId]: +# return set(self.inner_shape) + +# @property +# def tagged_shape(self) -> PerAxis[int]: +# """alias for shape""" +# return self.shape + +# @property +# def inner_slice_wo_overlap(self) -> PerAxis[SliceInfo]: +# """subslice of the inner slice, such that all `inner_slice_wo_overlap` can be +# stiched together trivially to form the original sample. + +# This can also be used to calculate statistics +# without overrepresenting block edge regions.""" +# return { +# a: SliceInfo(s.start_wo_overlap, s.stop_wo_overlap) +# if isinstance(s, SliceInfoWithOverlap) +# else SliceInfo(s.start, s.stop) +# for a, s in self.inner_slice.items() +# } + +# def __post_init__(self): +# assert all(a in self.sample_shape for a in self.inner_slice), ( +# "block has axes not present in sample" +# ) + +# assert all(a in self.inner_slice for a in self.halo), ( +# "halo has axes not present in block" +# ) + +# if any(s > self.sample_shape[a] for a, s in self.shape.items()): +# logger.warning( +# "block {} larger than sample {}", self.shape, self.sample_shape +# ) + +# def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: +# assert BATCH_AXIS_ID not in new_axes, "cannot transform the batch axis" + +# sample_shape = { +# a: ( +# trf +# if isinstance(trf, int) +# else trf.compute(self.sample_shape[trf.axis]) +# ) +# for a, trf in new_axes.items() +# } +# assert sample_shape.get(BATCH_AXIS_ID) == self.sample_shape.get( +# BATCH_AXIS_ID +# ), "block transform must not change the batch axis size" + +# inner_slice = { +# a: ( +# SliceInfo(0, trf) +# if isinstance(trf, int) +# else SliceInfo( +# trf.compute(self.inner_slice[trf.axis].start), +# trf.compute(self.inner_slice[trf.axis].stop), +# ) +# ) +# for a, trf in new_axes.items() +# } +# assert inner_slice.get(BATCH_AXIS_ID) == self.inner_slice.get(BATCH_AXIS_ID), ( +# "block transform must not change the batch axis slice" +# ) + +# return self.__class__( +# sample_shape=sample_shape, +# # sample_id=self.sample_id, +# inner_slice=inner_slice, +# halo={ +# a: ( +# Halo(0, 0) +# if isinstance(trf, int) +# else Halo(self.halo[trf.axis].left, self.halo[trf.axis].right) +# ) +# for a, trf in new_axes.items() +# }, +# block_loc=self.block_loc, +# block_loc_shape=self.block_loc_shape, +# ) + + +# BlockArray = Annotated[NDArray[np.object_], "NDArray[Block]"] + + +# def split_shape_into_blocks( +# shape: PerAxis[int], +# *, +# block_shape: PerAxis[int], +# halo: PerAxis[HaloLike], +# # sample_id: SampleId, +# ) -> tuple[BlockLocShape, BlockArray]: +# """ +# Args: +# item_size: size of a single element in bytes, used to find shard and chunk sizes +# """ +# if unknown_axes := [ +# a for a in block_shape if a not in shape and block_shape[a] != 1 +# ]: +# raise ValueError( +# f"unknown axes in block_shape: {unknown_axes} for shape {shape}" +# ) + +# if large_block_dims := [ +# (a, b, shape_size) +# for a in block_shape +# if (shape_size := shape.get(a, 1)) < (b := block_shape[a]) +# ]: +# logger.warning( +# "Block shape dimensions {} larger than shape dimensions {} are limiting the shape size", +# {a: b for a, b, _s in large_block_dims}, +# {a: s for a, _b, s in large_block_dims}, +# ) + +# block_shape = {a: min(s, block_shape.get(a, s)) for a, s in shape.items()} +# if unknown_halo_dims := [a for a in halo if a not in shape]: +# raise ValueError(f"unknown dims in halo: {unknown_halo_dims} for shape {shape}") + +# # fill in default halo (0) and block axis length (from tensor shape) +# halo = {a: Halo.create(halo.get(a, 0)) for a in shape} + +# inner_1d_slices: dict[AxisId, list[SliceInfo | SliceInfoWithOverlap]] = {} +# inner_sizes = {a: block_shape[a] - sum(halo[a]) for a in shape} +# for a, shape_size in shape.items(): +# inner_size = inner_sizes[a] +# inner_1d_slices[a] = [ +# SliceInfo(p, min(p + inner_size, shape_size)) +# if p <= shape_size - inner_size +# else SliceInfoWithOverlap( +# shape_size - inner_size, +# (stop := min(p + inner_size, shape_size)), +# shape_size - inner_size, +# stop, +# ) +# for p in range(0, shape_size, inner_size) +# ] + +# # n_blocks = prod(map(len, inner_1d_slices.values())) + +# # return n_blocks, _block_meta_generator( +# # shape, +# # unstacked_batch_shape, +# # blocks_in_sample=n_blocks, +# # unstacked_dim_order=unstacked_dim_order, +# # inner_1d_slices=inner_1d_slices, +# # halo=halo, +# # ) + +# # def _block_meta_generator( +# # sample_shape: PerAxis[int], +# # *, +# # unstacked_dim_order: Sequence[AxisId] | None, +# # blocks_in_sample: int, +# # inner_1d_slices: dict[AxisId, list[SliceInfo | SliceInfoWithOverlap]], +# # halo: PerAxis[HaloLike], +# # sample_id: SampleId, +# # ) -> BlockMetaArray: +# assert all(a in shape for a in halo) + +# halo = {a: Halo.create(halo.get(a, 0)) for a in inner_1d_slices} +# # if unstacked_batch_shape is None: +# # idx: MultiIndex | None = None +# # else: +# # idx = MultiIndex.from_product( # pyright: ignore[reportUnknownVariableType] +# # [range(unstacked_batch_shape[a]) for a in unstacked_batch_shape], +# # names=list(unstacked_batch_shape), +# # ) +# # assert isinstance(idx, MultiIndex) + +# out: list[BlockMeta] = [] +# out_shape = tuple(len(slices) for slices in inner_1d_slices.values()) +# for i, nd_tile in enumerate(itertools.product(*inner_1d_slices.values())): +# inner_1d_slices[BATCH_AXIS_ID] +# inner_slice = dict(zip(inner_1d_slices, nd_tile)) +# # if idx is None: +# # unstacked_batch_slice = None +# # else: +# # batch_slice = inner_slice[BATCH_AXIS_ID] +# # unstacked_batch_slice: dict[AxisId, SliceInfo] | None = {} +# # for a, c in zip(idx.names, idx.codes): +# # a = BATCH_AXIS_ID if a == "original_batch" else AxisId(a) +# # a_start = int(c[batch_slice.start]) +# # a_stop = int(c[batch_slice.stop - 1]) + 1 +# # unstacked_batch_slice[a] = SliceInfo(a_start, a_stop) +# out.append( +# BlockMeta( +# sample_shape=shape, +# sample_id=sample_id, +# # unstacked_sample_batch_shape=unstacked_sample_batch_shape, +# # unstacked_dim_order=unstacked_dim_order, +# inner_slice=inner_slice, +# # unstacked_batch_slice=unstacked_batch_slice, +# halo=halo, +# block_loc=i, # TODO: update +# block_loc_shape=blocks_in_sample, +# ) +# ) +# return np.asarray(out, dtype=object).reshape(out_shape) + + +# def split_multiple_shapes_into_blocks( +# shapes: PerMember[PerAxis[int]], +# block_shapes: PerMember[PerAxis[int]], +# *, +# halo: PerMember[PerAxis[HaloLike]], +# broadcast: bool = False, +# # sample_id: SampleId, +# ) -> tuple[BlockLocShape, PerMember[BlockArray]]: +# """Split multiple shapes (e.g. sample member shapes) into aligned blocks. + +# Args: +# shapes: the shapes of the sample members to split into blocks +# block_shapes: the block shape per sample member (depending on `broaadcast` the block shapes must result in the same number of blocks for all sample members) +# unstacked_batch_shape: the shape of the unstacked batch +# unstacked_dim_order: the order of the dimensions of the unstacked tensor dimensions (unstacked batch dimensions plus the other tensor dimensions) +# halo: the halo around each block per sample member +# broadcast: whether to broadcast block shapes of members resulting in fewer blocks to match other sample members. +# """ + +# if unknown_blocks := [t for t in block_shapes if t not in shapes]: +# raise ValueError( +# f"block shape specified for unknown tensors: {unknown_blocks}." +# ) + +# if not block_shapes: +# block_shapes = shapes + +# if not broadcast and ( +# missing_blocks := [t for t in shapes if t not in block_shapes] +# ): +# raise ValueError( +# f"no block shape specified for {missing_blocks}." +# + " Set `broadcast` to True if these tensors should be repeated" +# + " as a whole for each block." +# ) + +# if extra_halo := [t for t in halo if t not in block_shapes]: +# raise ValueError( +# f"`halo` specified for tensors without block shape: {extra_halo}." +# ) + +# blocks: PerMember[BlockArray] = {} +# block_locs: PerMember[BlockLocShape] = {} +# for t in block_shapes: +# block_locs[t], blocks[t] = split_shape_into_blocks( +# shape=shapes[t], +# # sample_id=sample_id, +# # unstacked_batch_shape=unstacked_batch_shape, +# # unstacked_dim_order=unstacked_dim_order[t] +# # if unstacked_dim_order is not None +# # else None, +# block_shape=block_shapes[t], +# halo=halo.get(t, {}), +# ) +# assert all(b > 0 for b in block_locs[t]), block_locs + +# assert len(blocks) > 0, blocks +# assert len(block_locs) > 0, block_locs +# unique_block_locs = set(block_locs.values()) + +# if len(unique_block_locs) != 1: +# if not broadcast: +# raise ValueError( +# f"Mismatch for number of blocks per dimension: { {t: b.shape for t, b in blocks.items()} }. Set `broadcast` to True if this is expected." +# ) + +# blocks = dict(zip(blocks.keys(), np.broadcast_arrays(*blocks.values()))) +# block_locs = {m: b.shape for m, b in blocks.items()} +# unique_block_locs = set(block_locs.values()) +# assert len(unique_block_locs) == 1, ( +# f"Mismatch for total number of blocks (after braodcasting?!): {block_locs}" +# ) + +# return next(iter(unique_block_locs)), blocks +# # def foo(*bs: BlockMeta) -> SampleBlockMeta: +# # return SampleBlockMeta.from_blocks(dict(zip(blocks.keys(), bs))) + +# # foo_op = np.frompyfunc(foo, len(blocks), 1) +# # out = foo_op(*blocks.values()) +# # assert out.shape == next(iter(blocks.values())).shape +# # return n, out + + +# # def _consolidate_blocks(blocks: PerMember[BlockMetaArray]) -> PerMember[BlockMetaArray]: +# # sample_ids = {b.sample_id for b in blocks.values() if b.sample_id is not None} +# # if len(sample_ids) > 1: +# # raise ValueError(f"Mismatch for sample_id across members: {sample_ids}") +# # block_loc_shapes = {b.block_loc_shape for b in blocks.values()} +# # if len(block_loc_shapes) != 1: +# # raise ValueError( +# # f"Mismatch for block_loc_shape (sample shape in blocks) across members: {block_loc_shapes}" +# # ) +# # block_locs = {b.block_loc for b in blocks.values()} +# # if len(block_locs) != 1: +# # raise ValueError( +# # f"Mismatch for block_loc (block location within sample) across members: {block_locs}" +# # ) +# # return blocks + + +# @dataclass +# class Chunk: +# blocks: Iterable[PerMember[BlockMeta]] +# n_blocks: int + +# def __iter__(self) -> Iterator[PerMember[BlockMeta]]: +# yield from self.blocks + + +# @dataclass +# class Shard: +# chunks: Iterable[Chunk] +# n_chunks: int + +# def __iter__(self) -> Iterator[Chunk]: +# yield from self.chunks + + +# def split_multiple_shapes_into_shards( +# shapes: PerMember[PerAxis[int]], +# *, +# block_shapes: PerMember[PerAxis[int]], +# halo: PerMember[PerAxis[HaloLike]], +# broadcast: bool = False, +# item_size: int | None, +# chunk_shapes: PerMember[PerAxis[int]] | None = None, +# shard_shapes: PerMember[PerAxis[int]] | None = None, +# ) -> tuple[BlockLocShape, Iterable[Shard]]: +# """Split sample member shapes into aligned blocks. + +# Shards/chunks will be aligned across all sample members, which might lead to sup-optimal chunking/sharding for some sample members, but simplifies keeping the blocks aligned across all sample members. + +# Args: +# shapes: the shapes of the sample members to split into blocks +# block_shapes: the block shape per sample member (depending on `broaadcast` the block shapes must result in the same number of blocks for all sample members) +# halo: the halo around each block per sample member +# broadcast: whether to broadcast block shapes of members resulting in fewer blocks to match other sample members. +# item_size: the size in bytes of the largest data type of all members. This is used to determine shard and chunk sizes. May be None if `chunk_shapes` and `shard_shapes` are given. +# chunk_shapes: the chunk shape for at least one sample member; must be a multiple of the respective block shape. May be None if `item_size` is given. +# shard_shapes: the shard shape for at leasst one sample member; must be a multiple of the respective chunk shape. May be None if `item_size` is given. + + +# Returns: +# A tuple of the total number of blocks and an iterable of shards. + +# """ + +# raise NotImplementedError("sharding is not yet implemented") diff --git a/src/bioimageio/core/cli.py b/src/bioimageio/core/cli.py index e4a050e4..e13351eb 100644 --- a/src/bioimageio/core/cli.py +++ b/src/bioimageio/core/cli.py @@ -81,6 +81,7 @@ create_prediction_pipeline, create_remote_prediction_pipeline, ) +from ._utils import compare from .commands import WeightFormatArgAll, WeightFormatArgAny, package, test from .common import MemberId, SampleId, SupportedWeightsFormat from .digest_spec import get_member_ids, load_sample_for_model @@ -90,7 +91,6 @@ from .sample import Sample from .stat_calculators import StatsCalculator from .stat_measures import Measure, MeasureValue, Stat -from .utils import compare from .weight_converters._add_weights import add_weights WEIGHT_FORMAT_ALIASES = AliasChoices( @@ -836,11 +836,11 @@ def input_dataset(stat: Stat): if blockwise: predict_method = partial( - pp.predict_sample_with_blocking, + pp.predict_with_blocking, ns=None if isinstance(blockwise, bool) else blockwise, ) else: - predict_method = pp.predict_sample_without_blocking + predict_method = pp.predict_without_blocking for sample_in, sp_out in tqdm( zip(input_dataset(dict(stat)), sample_paths_out), diff --git a/src/bioimageio/core/common.py b/src/bioimageio/core/common.py index dd31ca74..56de3a6c 100644 --- a/src/bioimageio/core/common.py +++ b/src/bioimageio/core/common.py @@ -1,6 +1,7 @@ from __future__ import annotations from collections.abc import Hashable, Mapping +from dataclasses import dataclass from typing import ( Literal, NamedTuple, @@ -10,9 +11,14 @@ from typing_extensions import Self, TypeAlias, assert_never +from bioimageio.spec._internal.utils import SLOTS from bioimageio.spec.model import v0_5 -SupportedWeightsFormat = Literal[ +# SHARDED_INNER_CHUNK_MAX_BYTES: int = 1048576 # 1 MiB +# NON_SHARDED_CHUNK_MAX_BYTES: int = 67_108_864 # 64MiB +PROCESSING_CHUNK_MIN_BYTES: int = 104_857_600 # 100 MiB + +SupportedWeightsFormat: TypeAlias = Literal[ "keras_hdf5", "keras_v3", "onnx", @@ -21,7 +27,7 @@ "torchscript", ] -QuantileMethod = Literal[ +QuantileMethod: TypeAlias = Literal[ "inverted_cdf", # "averaged_inverted_cdf", # "closest_observation", @@ -46,7 +52,7 @@ """ -DTypeStr = Literal[ +DTypeStr: TypeAlias = Literal[ "bool", "float32", "float64", @@ -81,22 +87,22 @@ def create(cls, like: _LeftRightLike[Self]) -> Self: assert_never(like) -_Where = Literal["left", "right", "left_and_right"] +_Where: TypeAlias = Literal["left", "right", "left_and_right"] class CropWidth(_LeftRight): pass -CropWidthLike = _LeftRightLike[CropWidth] -CropWhere = _Where +CropWidthLike: TypeAlias = _LeftRightLike[CropWidth] +CropWhere: TypeAlias = _Where class Halo(_LeftRight): pass -HaloLike = _LeftRightLike[Halo] +HaloLike: TypeAlias = _LeftRightLike[Halo] class OverlapWidth(_LeftRight): @@ -113,11 +119,20 @@ class PadWidth(_LeftRight): PadWhere: TypeAlias = _Where -class SliceInfo(NamedTuple): +@dataclass(frozen=True, **SLOTS) +class SliceInfo: start: int stop: int +@dataclass(frozen=True, **SLOTS) +class SliceInfoWithOverlap(SliceInfo): + """SliceInfo aware of the fact that it overlaps with neighboring blocks.""" + + start_wo_overlap: int + stop_wo_overlap: int + + SampleId = Hashable """ID of a sample, see `bioimageio.core.sample.Sample`""" MemberId = v0_5.TensorId @@ -133,7 +148,31 @@ class SliceInfo(NamedTuple): """ _T = TypeVar("_T") +# PerMember = Union[ +# Mapping[MemberId, _T], Mapping[str, _T], Mapping[Union[MemberId, str], _T] +# ] PerMember = Mapping[MemberId, _T] - -BlockIndex = int -TotalNumberOfBlocks = int +BlockLoc = tuple[int, ...] +BlockLocShape = tuple[int, ...] + + +class DataDepSize(NamedTuple): + min: int + max: int | None + + +def resolve_pad_mode(mode: PadMode): + constant_value = None + if isinstance(mode, str): + mode_name = mode + elif isinstance(mode, v0_5.ConstantPadding): + mode_name = mode.mode + constant_value = mode.value + elif isinstance( + mode, (v0_5.EdgePadding, v0_5.ReflectPadding, v0_5.SymmetricPadding) + ): + mode_name = mode.mode + else: + assert_never(mode) + + return mode_name, constant_value diff --git a/src/bioimageio/core/digest_spec.py b/src/bioimageio/core/digest_spec.py index d06d2f45..af6d42f2 100644 --- a/src/bioimageio/core/digest_spec.py +++ b/src/bioimageio/core/digest_spec.py @@ -11,17 +11,18 @@ Any, Callable, Literal, - NamedTuple, Union, ) from zipfile import ZipFile, is_zipfile +import dask.array as da import numpy as np import xarray as xr from loguru import logger from numpy.typing import NDArray from typing_extensions import TypeAlias, Unpack, assert_never +from bioimageio.core._utils.type_hints import cast_and_assert from bioimageio.spec._internal.io import HashKwargs, PermissiveFileSource from bioimageio.spec.common import FileDescr, FileSource from bioimageio.spec.model import AnyModelDescr, v0_4, v0_5 @@ -29,24 +30,18 @@ from bioimageio.spec.model.v0_5 import ( ArchitectureFromFileDescr, ArchitectureFromLibraryDescr, - ParameterizedSize_N, ) from bioimageio.spec.utils import load_array -from .axis import CHANNEL_AXIS_ID, AxisId, AxisInfo, AxisLike, PerAxis -from .block_meta import split_multiple_shapes_into_blocks -from .common import Halo, MemberId, PerMember, SampleId, TotalNumberOfBlocks -from .io import load_tensor_and_return_unstacked_dim_order +from .axis import CHANNEL_AXIS_ID, Axis, AxisId, AxisInfo, PerAxis +from .block import LinearAxisTransform +from .common import Halo, MemberId, PerMember, SampleId +from .io import load_tensor from .sample import ( - LinearSampleAxisTransform, Sample, - SampleBlockMeta, - SampleBlockWithOrigin, - sample_block_meta_generator, ) from .stat_measures import Stat -from .tensor import Tensor -from .utils._multi_index import get_unstacked_dim_order +from .tensor import Tensor, get_unstacked_dim_order TensorSource: TypeAlias = Union[ Tensor, xr.DataArray, NDArray[Any], PermissiveFileSource @@ -207,7 +202,7 @@ def get_member_ids( def get_test_input_sample( model: AnyModelDescr, *, - repetitions: Sequence[tuple[int, AxisInfo, Sequence[float]]] = (), + repetitions: Sequence[tuple[int, Axis | AxisInfo, Sequence[float]]] = (), ) -> Sample: """returns a model's test input sample @@ -235,7 +230,7 @@ def get_test_input_sample( def get_test_output_sample( model: AnyModelDescr, *, - repetitions: Sequence[tuple[int, AxisInfo, Sequence[float]]] = (), + repetitions: Sequence[tuple[int, Axis | AxisInfo, Sequence[float]]] = (), ) -> Sample: """returns a model's test output sample""" if isinstance(model, v0_4.ModelDescr): @@ -257,9 +252,9 @@ def _get_test_sample( | Mapping[MemberId, tuple[v0_4.InputTensorDescr, FileSource]] | Mapping[MemberId, tuple[v0_4.OutputTensorDescr, FileSource]], *, - repetitions: Sequence[tuple[int, AxisInfo, Sequence[float]]] = (), + repetitions: Sequence[tuple[int, Axis | AxisInfo, Sequence[float]]] = (), ) -> Sample: - arrays: dict[MemberId, NDArray[Any]] = {} + arrays: dict[MemberId, NDArray[Any] | da.Array] = {} for m, src in info.items(): if isinstance(src, tuple): arrays[m] = load_array(src[1]) @@ -272,15 +267,14 @@ def _get_test_sample( else: assert_never(src) - axes = { - m: get_axes_infos(t[0] if isinstance(t, tuple) else t) for m, t in info.items() + axes: dict[MemberId, list[Axis | AxisInfo]] = { + m: list(get_axes_infos(t[0] if isinstance(t, tuple) else t)) + for m, t in info.items() } tensors: dict[MemberId, Tensor] = {} - unstacked_dim_order: PerMember[Sequence[AxisId]] = {} for m in info: a = arrays[m] ax = axes[m] - axis_order_wo_repititions = [ax_.id for ax_ in ax] if repetitions: for rep_idx, rep_ax, factors in repetitions: if rep_idx < -1 or rep_idx >= len(ax): @@ -288,45 +282,48 @@ def _get_test_sample( f"Invalid repetition index {rep_idx} for tensor '{m}' with axes {ax}" ) # Repeat the array along the specified axis according to the factors - a = np.stack([a * factor for factor in factors], axis=rep_idx) - if len(factors) < rep_ax.size.min: - raise ValueError( - f"Invalid number of repetitions {len(factors)} for axis '{rep_ax.id}' with min size {rep_ax.size.min}" - ) - if rep_ax.size.max is not None and len(factors) > rep_ax.size.max: - raise ValueError( - f"Invalid number of repetitions {len(factors)} for axis '{rep_ax.id}' with max size {rep_ax.size.max}" - ) - ax.insert(rep_idx, rep_ax) + repitition_arrays = { + factor: cast_and_assert(da.Array, da.from_array(a) * factor) + for factor in set(factors) + } + a = cast_and_assert( + da.Array, + da.stack( + [repitition_arrays[factor] for factor in factors], axis=rep_idx + ), + ) - tensors[m] = Tensor.from_numpy(a, dims=ax) - if repetitions: - tensors[m], udo = tensors[m].transpose_and_return_unstacked_dim_order( - axis_order_wo_repititions, extra_dims="stack" - ) - assert udo is not None - unstacked_dim_order[m] = udo + if len(factors) > 100: + # max 100 chunks along repition axis + chunk_size = max(1, len(factors) // 100) + chunk_shape = list(a.shape) + chunk_shape.insert(rep_idx, chunk_size) + a = cast_and_assert(da.Array, a.rechunk(chunk_shape)) # pyright: ignore[reportArgumentType] - return Sample( - members=tensors, - stat={}, - id="test-sample", - unstacked_dim_order=unstacked_dim_order or None, - ) + if isinstance(rep_ax, AxisInfo): + if len(factors) < rep_ax.size.min: + raise ValueError( + f"Invalid number of repetitions {len(factors)} for axis '{rep_ax.id}' with min size {rep_ax.size.min}" + ) + if rep_ax.size.max is not None and len(factors) > rep_ax.size.max: + raise ValueError( + f"Invalid number of repetitions {len(factors)} for axis '{rep_ax.id}' with max size {rep_ax.size.max}" + ) + ax.insert(rep_idx, rep_ax) -class IO_SampleBlockMeta(NamedTuple): - input: SampleBlockMeta - output: SampleBlockMeta + tensors[m] = Tensor.from_array(a, dims=ax) + + return Sample(members=tensors, stat={}, id="test-sample") def get_unstacked_output_dim_orders_for_model( model: v0_4.ModelDescr | v0_5.ModelDescr, unstacked_input_order: PerMember[Sequence[AxisId]] | None, - stacked_dims: Sequence[AxisId] | None, + stacked_output_dims: PerMember[Sequence[AxisId]] | None, ) -> PerMember[Sequence[AxisId]]: """Returns the unstacked output dim order for each output tensor of a model, - given optional unstacked input dim order and optional stacked dims.""" + given optional unstacked input dim order and optional output stacked dims.""" output_orders: dict[MemberId, Sequence[AxisId]] = {} for out in model.outputs: in_order = None @@ -354,7 +351,9 @@ def get_unstacked_output_dim_orders_for_model( output_orders[out_id] = get_unstacked_dim_order( stacked_out_order, unstacked_reference_order=in_order, - stacked_dims=stacked_dims, + stacked_dims=stacked_output_dims.get(out_id) + if stacked_output_dims is not None + else None, ) return output_orders @@ -407,15 +406,15 @@ def get_input_halo( def get_block_transform( model: v0_5.ModelDescr, -) -> PerMember[PerAxis[LinearSampleAxisTransform | int]]: - """returns how a model's output tensor shapes relates to its input shapes""" - ret: dict[MemberId, dict[AxisId, LinearSampleAxisTransform | int]] = {} +) -> PerMember[PerAxis[LinearAxisTransform | int]]: + """returns how a model's output tensor shapes relates to given input shapes""" + ret: dict[MemberId, dict[AxisId, LinearAxisTransform | int]] = {} batch_axis_trf = None for ipt in model.inputs: for a in ipt.axes: if a.type == "batch": - batch_axis_trf = LinearSampleAxisTransform( - axis=a.id, scale=1, offset=0, member=ipt.id + batch_axis_trf = LinearAxisTransform( + member=ipt.id, axis=a.id, scale=1, offset=0 ) break if batch_axis_trf is not None: @@ -475,7 +474,7 @@ def get_block_transform( ) for out in model.outputs: - new_axes: dict[AxisId, LinearSampleAxisTransform | int] = {} + new_axes: dict[AxisId, LinearAxisTransform | int] = {} for a in out.axes: a_size = ( patched_size_ref_for_fixed_output_size_matching_fixed_input_size.get( @@ -507,7 +506,7 @@ def get_block_transform( s = -1 elif isinstance(a_size, v0_5.SizeReference): assert a.type != "batch", "size reference cannot be used for batch axis" - s = LinearSampleAxisTransform( + s = LinearAxisTransform( axis=a_size.axis_id, scale=axis_scales[a_size.tensor_id][a_size.axis_id] / a.scale, offset=a_size.offset, @@ -534,62 +533,6 @@ def get_block_transform( return ret -def get_io_sample_block_metas( - model: v0_5.ModelDescr, - input_sample_shape: PerMember[PerAxis[int]], - *, - unstacked_sample_batch_shape: PerAxis[int] | None = None, - unstacked_dim_order_input: PerMember[Sequence[AxisId]] | None = None, - ns: Mapping[tuple[MemberId, AxisId], ParameterizedSize_N], - batch_size: int = 1, -) -> tuple[TotalNumberOfBlocks, Iterable[IO_SampleBlockMeta]]: - """returns an iterable yielding meta data for corresponding input and output samples""" - if not isinstance(model, v0_5.ModelDescr): - raise TypeError(f"get_block_meta() not implemented for {type(model)}") - - if ( - unstacked_sample_batch_shape is not None - and batch_size != 1 - and ( - not_divisible := { - a - for a, s in unstacked_sample_batch_shape.items() - if s % batch_size != 0 - } - ) - ): - raise ValueError( - f"unstacked_batch_shape={unstacked_sample_batch_shape} is not divisible by batch_size={batch_size} for axes {not_divisible}" - ) - - block_axis_sizes = model.get_axis_sizes(ns=ns, batch_size=batch_size) - input_block_shape = { - t: {aa: s for (tt, aa), s in block_axis_sizes.inputs.items() if tt == t} - for t in {tt for tt, _ in block_axis_sizes.inputs} - } - output_halo = { - t.id: { - a.id: Halo(a.halo, a.halo) for a in t.axes if isinstance(a, v0_5.WithHalo) - } - for t in model.outputs - } - input_halo = get_input_halo(model, output_halo) - n_input_blocks, input_blocks = split_multiple_shapes_into_blocks( - input_sample_shape, - input_block_shape, - unstacked_batch_shape=unstacked_sample_batch_shape, - unstacked_dim_order=unstacked_dim_order_input, - halo=input_halo, - ) - block_transform = get_block_transform(model) - return n_input_blocks, ( - IO_SampleBlockMeta(ipt, ipt.get_transformed(block_transform)) - for ipt in sample_block_meta_generator( - input_blocks, sample_shape=input_sample_shape, sample_id=None - ) - ) - - def get_tensor( src: TensorSource, descr: v0_4.InputTensorDescr @@ -597,52 +540,13 @@ def get_tensor( | v0_4.OutputTensorDescr | v0_5.OutputTensorDescr | Sequence[AxisInfo], - *, - extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "squeeze", - missing_dims: Literal["raise", "expand", "unstack", "unstack_or_expand"] = "raise", ): """helper to cast/load various tensor sources Args: src: the tensor source to load/cast descr: the tensor description or a sequence of axis infos - extra_dims: - How to handle extra dimensions in the input tensors. - If "raise", any extra dimensions will raise an error. - If "squeeze", any extra singleton dimensions will be squeezed, non-singleton dimensions will raise an error. - If "stack", any extra dimensions will be stacked to the batch dimension. Such a stacked batch dimension then has a multi-index that can be unstacked using `Tensor.unstack_batch_multi_index()`. - If "squeeze_or_stack", any extra singleton dimensions will be squeezed, non-singleton dimensions will be stacked to the batch dimension. - missing_dims: - Missing dimensions are any dimensions specified in `axes` that are not present in the tensor. - If "raise", any missing dimensions will raise an error. - If "expand", any missing dimensions will be added as singleton dimensions. - If "unstack", any missing dimensions will be unstacked from the batch dimension. For this option a batch dimension with a multi-index must be present from previous stacking operations or assigned by `Tensor.assign_batch_multi_index()`. - If "unstack_or_expand", any missing dimensions will be unstacked from the batch dimension if it has a multi-index, otherwise they will be added as singleton dimensions. - """ - tensor, _unstacked_dims = get_tensor_and_unstacked_dims( - src, - descr, - extra_dims=extra_dims, - missing_dims=missing_dims, - ) - return tensor - -def get_tensor_and_unstacked_dims( - src: TensorSource, - descr: v0_4.InputTensorDescr - | v0_5.InputTensorDescr - | v0_4.OutputTensorDescr - | v0_5.OutputTensorDescr - | Sequence[AxisInfo], - *, - extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "squeeze", - missing_dims: Literal["raise", "expand", "unstack", "unstack_or_expand"] = "raise", -): - """See `get_tensor()` for details. Returns a tuple of (tensor, unstacked_dim_order) - - unstacked_dim_order is None if the tensor has no unstacked batch dimension, - otherwise it is a list of axis IDs in the order they appear in the fully unstacked tensor. """ if isinstance( @@ -659,51 +563,38 @@ def get_tensor_and_unstacked_dims( axes = descr if isinstance(src, Tensor): - output_dims = [a.id for a in axes] - return src.transpose_and_return_unstacked_dim_order( - output_dims, extra_dims=extra_dims, missing_dims=missing_dims - ) + output = src elif isinstance(src, xr.DataArray): - output_dims = [a.id for a in axes] + output = Tensor.from_xarray(src) - return Tensor.from_xarray(src).transpose_and_return_unstacked_dim_order( - axes=output_dims, extra_dims=extra_dims, missing_dims=missing_dims - ) elif isinstance(src, np.ndarray): - return Tensor.from_numpy(src, dims=axes), None + output = Tensor.from_numpy(src, dims=axes) else: - return load_tensor_and_return_unstacked_dim_order(src, axes=axes) + output = load_tensor(src, axes=axes) + + for a in axes: + if a.id not in output.dims: + raise ValueError( + f"Missing axis '{a.id}' in tensor with dims {output.dims}. Expected axes: {axes}" + ) + + return output def create_sample_for_model( model: AnyModelDescr, + inputs: PerMember[TensorSource] | TensorSource, *, stat: Stat | None = None, sample_id: SampleId = None, - inputs: PerMember[TensorSource] | TensorSource, - extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "stack", - missing_dims: Literal[ - "raise", "expand", "unstack", "unstack_or_expand" - ] = "unstack_or_expand", ) -> Sample: """Create a sample from a single set of input(s) for a specific bioimage.io model Args: model: a bioimage.io model description + inputs: the input(s) constituting a single sample, e.g. {'raw': ..., 'mask': ...}. stat: dictionary with sample and dataset statistics (may be updated in-place!) - inputs: the input(s) constituting a single sample. - extra_dims: How to handle extra dimensions in the input tensors. - If "raise", any extra dimensions will raise an error. - If "squeeze", any extra singleton dimensions will be squeezed, non-singleton dimensions will raise an error. - If "stack", any extra dimensions will be stacked to the batch dimension. Such a stacked batch dimension then has a multi-index that can be unstacked using `Tensor.unstack_batch_multi_index()`. - If "squeeze_or_stack", any extra singleton dimensions will be squeezed, non-singleton dimensions will be stacked to the batch dimension. - missing_dims: - Missing dimensions are any dimensions specified in `axes` that are not present in the tensor. - If "raise", any missing dimensions will raise an error. - If "expand", any missing dimensions will be added as singleton dimensions. - If "unstack", any missing dimensions will be unstacked from the batch dimension. For this option a batch dimension with a multi-index must be present from previous stacking operations or assigned by `Tensor.assign_batch_multi_index()`. - If "unstack_or_expand", any missing dimensions will be unstacked from the batch dimension if it has a multi-index, otherwise they will be added as singleton dimensions. - + sample_id: sample ID """ model_inputs = {get_member_id(d): d for d in model.inputs} @@ -727,123 +618,49 @@ def create_sample_for_model( raise ValueError(f"Missing non-optional model inputs: {missing}") members: dict[MemberId, Tensor] = {} - unstacked_dim_order: dict[MemberId, Sequence[AxisId]] | None = {} for m, ipt in model_inputs.items(): if m in inputs: - tensor, unstacked_dims = get_tensor_and_unstacked_dims( - inputs[m], ipt, extra_dims=extra_dims, missing_dims=missing_dims - ) + tensor = get_tensor(inputs[m], ipt) members[m] = tensor - if unstacked_dim_order is not None: - if unstacked_dims is None: - unstacked_dim_order = None - else: - unstacked_dim_order[m] = unstacked_dims - return Sample( - members=members, - stat={} if stat is None else stat, - id=sample_id, - unstacked_dim_order=unstacked_dim_order, - ) + return Sample(members=members, stat={} if stat is None else stat, id=sample_id) -def load_sample_for_model( - *, - model: AnyModelDescr, - paths: PerMember[PermissiveFileSource], - axes: PerMember[Sequence[AxisLike]] | None = None, - stat: Stat | None = None, - sample_id: SampleId | None = None, -): - """load a single sample from `paths` that can be processed by `model`""" - - if axes is None: - axes = {} - - # make sure members are keyed by MemberId, not string - paths = {MemberId(k): v for k, v in paths.items()} - axes = {MemberId(k): v for k, v in axes.items()} - - model_inputs = {get_member_id(d): d for d in model.inputs} - - if unknown := {k for k in paths if k not in model_inputs}: - raise ValueError(f"Got unexpected paths for {unknown}") - - if unknown := {k for k in axes if k not in model_inputs}: - raise ValueError(f"Got unexpected axes hints for: {unknown}") - - members: dict[MemberId, Tensor] = {} - unstacked_dim_order: dict[MemberId, Sequence[AxisId]] | None = {} - for m, p in paths.items(): - if m not in axes: - axes[m] = get_axes_infos(model_inputs[m]) - logger.info( - "loading '{}' from {} with default input axes {} ", - m, - p, - axes[m], - ) - t, dim_order = load_tensor_and_return_unstacked_dim_order(p, axes[m]) - members[m] = t - if unstacked_dim_order is not None: - if dim_order is None: - unstacked_dim_order = None - else: - unstacked_dim_order[m] = dim_order - - return Sample( - members=members, - stat={} if stat is None else stat, - id=sample_id or tuple(sorted(str(p) for p in paths.values())), - unstacked_dim_order=unstacked_dim_order, - ) - - -def split_sample_into_blocks_for_model( - sample: Sample, - model: v0_5.ModelDescr, - blocksize_parameter: int, - batch_size: int = 1, -) -> tuple[TotalNumberOfBlocks, Iterable[SampleBlockWithOrigin]]: - if isinstance(model, v0_4.ModelDescr): - raise NotImplementedError( - "`predict_sample_with_blocking` not implemented for v0_4.ModelDescr" - + f" {model.name}." - + " Consider using `predict_sample_with_fixed_blocking` or update the model description to format version 0.5." - ) - - ns = { - (ipt.id, a.id): blocksize_parameter - for ipt in model.inputs - for a in ipt.axes - if isinstance(a.size, v0_5.ParameterizedSize) - } - halo = get_input_halo(model) - - input_block_shape = model.get_tensor_sizes(ns, batch_size=batch_size).inputs - - return sample.split_into_blocks( - block_shapes=input_block_shape, - halo=halo, - pad_mode={ipt.id: ipt.pad or "symmetric" for ipt in model.inputs}, - ) +load_sample_for_model = create_sample_for_model def transpose_sample_for_model( sample: Sample, model: AnyModelDescr, + *, extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "stack", - missing_dims: Literal["raise", "expand", "unstack", "unstack_or_expand"] = "raise", + missing_dims: Literal[ + "raise", "expand", "unstack", "unstack_or_expand" + ] = "unstack", ) -> Sample: - """Transpose sample members to the order expected as inputs by the model. + return Sample( + members=transpose_inputs_for_model( + sample.members, model, extra_dims=extra_dims, missing_dims=missing_dims + ), + stat=sample.stat, + id=sample.id, + ) - Unexpected dimensions are stacked to the batch dimension, missing dimensions are added as singletons. - """ + +def transpose_inputs_for_model( + inputs: PerMember[Tensor], + model: AnyModelDescr, + *, + extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "stack", + missing_dims: Literal[ + "raise", "expand", "unstack", "unstack_or_expand" + ] = "unstack", +) -> dict[MemberId, Tensor]: + """Transpose sample members to the order expected as inputs by the model.""" axes = {get_member_id(d): [a.id for a in get_axes_infos(d)] for d in model.inputs} for m in axes: - if m not in sample.members and not ( + if m not in inputs and not ( isinstance(model, v0_5.ModelDescr) and next(d for d in model.inputs if get_member_id(d) == m).optional ): @@ -851,8 +668,26 @@ def transpose_sample_for_model( f"Sample is missing non-optional member {m} required by model" ) - return sample.transpose( - axes=axes, - extra_dims=extra_dims, - missing_dims=missing_dims, - ) + if any(unknown := [m not in inputs for m in axes]): + raise ValueError(f"Axes specified for unknown members: {unknown}") + + transposed = { + m: t.transpose(axes[m], extra_dims=extra_dims, missing_dims=missing_dims) + for m, t in inputs.items() + if m in axes + } + if ( + len( + batch_lengths := { + t.sizes[AxisId("batch")] + for t in transposed.values() + if AxisId("batch") in t.dims + } + ) + > 1 + ): + raise ValueError( + f"Transposed tensors have incompatible batch lengths: {batch_lengths}." + ) + + return transposed diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index c4870174..8105edd5 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -26,6 +26,7 @@ from typing_extensions import Literal, TypeAlias, assert_never from typing_extensions import TypeAliasType as _TypeAliasType +from bioimageio.core._utils.type_hints import cast_and_assert from bioimageio.spec._internal.io import ( RelativeDirectory, ZarrSource, @@ -46,26 +47,25 @@ ) from bioimageio.spec.utils import load_array, save_array +from ._utils.color import hex_to_rgb +from ._utils.io_zarr import open_zarr_multiscale_array +from ._utils.tiff_metadata import ( + ImageJMetadata, + OmeChannelMetadata, + OmeMetadata, + create_ome_metadata_from_xml_string, +) +from ._utils.type_guards import is_tuple from .axis import ( - BATCH_AXIS_ID, AxisId, AxisLike, PerAxis, single_letter_dims_if_possible, ) -from .common import MemberId, PerMember, SliceInfo -from .sample import Sample, SampleBlock +from .common import PerMember +from .sample import Sample from .stat_measures import DatasetMeasure, MeasureValue, SampleMeasure, Stat from .tensor import Tensor -from .utils._color import hex_to_rgb -from .utils._io_zarr import open_zarr_multiscale_array -from .utils._tiff_metadata import ( - ImageJMetadata, - OmeChannelMetadata, - OmeMetadata, - create_ome_metadata_from_xml_string, -) -from .utils._type_guards import is_tuple if TYPE_CHECKING: JsonValue: TypeAlias = Union[ @@ -489,10 +489,15 @@ def save_tensor( tensor: Tensor, *, io_lib: IO_Lib | None = None, - roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None = None, + chunk_shape: PerAxis[int] | None = None, + shard_shape: PerAxis[int] | None = None, ) -> None: _ = _save_tensor_impl( - path, tensor, io_lib=io_lib, roi_and_tensor_shape=roi_and_tensor_shape + path, + tensor, + io_lib=io_lib, + chunk_shape=chunk_shape, + shard_shape=shard_shape, ) @@ -500,8 +505,9 @@ def _save_tensor_impl( path: Path | str, tensor: Tensor, *, - io_lib: IO_Lib | None = None, - roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None = None, + io_lib: IO_Lib | None, + chunk_shape: PerAxis[int] | None, + shard_shape: PerAxis[int] | None, ) -> IO_Lib: try: output_path, extensions, internal_path = _interprete_tensor_source(path) @@ -513,33 +519,6 @@ def _save_tensor_impl( "Saving tensors to HTTP URLs is not implemented. Please save to a local file path." ) - if (BATCH_AXIS_ID in tensor.dims) and (not extensions or extensions[-1] != ".npy"): - if roi_and_tensor_shape is not None: - roi, tensor_shape = roi_and_tensor_shape - if tensor_shape.get(BATCH_AXIS_ID, 0) == 1: - # remove singleton batch dimension from tensor shape, roi and the given tensor if present - tensor_shape = { - k: v for k, v in tensor_shape.items() if k != BATCH_AXIS_ID - } - if BATCH_AXIS_ID in tensor.dims: - tensor = tensor.squeeze(BATCH_AXIS_ID) - if BATCH_AXIS_ID in roi: - assert ( - roi[BATCH_AXIS_ID].start == 0 and roi[BATCH_AXIS_ID].stop == 1 - ), roi[BATCH_AXIS_ID] - roi = {k: v for k, v in roi.items() if k != BATCH_AXIS_ID} - - if BATCH_AXIS_ID in roi or BATCH_AXIS_ID in tensor_shape: - raise NotImplementedError( - "Saving a region of interest (roi) with a non-singleton batch dimension to non-NPY formats is not supported." - ) - - if tensor.batch_multi_index is not None: - tensor = tensor.unstack_batch_multi_index(errors="raise") - - if BATCH_AXIS_ID in tensor.dims: - tensor = tensor.squeeze(BATCH_AXIS_ID) - if io_lib is None: try_io_libs = _select_io_libs_based_on_extension(extensions) exceptions: list[Exception] = [] @@ -549,7 +528,8 @@ def _save_tensor_impl( path, tensor, io_lib=try_io_lib, - roi_and_tensor_shape=roi_and_tensor_shape, + chunk_shape=chunk_shape, + shard_shape=shard_shape, ) except Exception as e: exceptions.append(e) @@ -565,36 +545,26 @@ def _save_tensor_impl( output_path, tensor, internal_path=internal_path, - roi_and_tensor_shape=roi_and_tensor_shape, + chunk_shape=chunk_shape, + shard_shape=shard_shape, ) elif io_lib == "clearscale": _save_tensor_clearscale( output_path, tensor, internal_path=internal_path, - roi_and_tensor_shape=roi_and_tensor_shape, + chunk_shape=chunk_shape, + shard_shape=shard_shape, ) elif io_lib == "imageio": - if roi_and_tensor_shape is not None: - raise NotImplementedError( - "Saving a region of interest (roi) is not implemented for imageio." - ) _save_tensor_imageio(output_path, tensor, internal_path=internal_path) elif io_lib == "numpy": - if roi_and_tensor_shape is not None: - raise NotImplementedError( - "Saving a region of interest (roi) is not implemented for numpy." - ) if internal_path: raise NotImplementedError( "Saving to an internal path is not implemented for numpy." ) _save_tensor_numpy(output_path, tensor) elif io_lib == "tifffile": - if roi_and_tensor_shape is not None: - raise NotImplementedError( - "Saving a region of interest (roi) is not implemented for tifffile." - ) if internal_path is not None: raise NotImplementedError( "Saving to an internal path is not implemented for tifffile." @@ -611,7 +581,8 @@ def _save_tensor_bioio( path: Path | ZipPath | FtpUrl, tensor: Tensor, internal_path: PurePath | None, - roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None, + chunk_shape: PerAxis[int] | None = None, + shard_shape: PerAxis[int] | None = None, ) -> None: """Save an image tensor using bioio""" @@ -635,12 +606,6 @@ def _save_tensor_bioio( raise NotImplementedError( "Saving to an internal path that is not '0' is not implemented for zarr files with io_lib='bioio'." ) - if roi_and_tensor_shape is None: - tensor_shape = tensor.tagged_shape - else: - raise NotImplementedError( - "Saving a region of interest (roi) is not implemented for saving zarr files with io_lib='bioio'." - ) import bioio_ome_zarr.writers @@ -660,7 +625,7 @@ def _save_tensor_bioio( label=f"channel{i}" if channel_names is None else channel_names[i], color="#FF0000" if channel_colors is None else channel_colors[i], ) - for i in range(tensor_shape.get(AxisId("channel"), 0)) + for i in range(tensor.tagged_shape.get(AxisId("channel"), 0)) ] zf = int(os.getenv("ZARR_FORMAT", "3")) if zf not in (2, 3): @@ -671,7 +636,7 @@ def _save_tensor_bioio( unit = tensor.get_physical_scale_unit() writer = bioio_ome_zarr.writers.OMEZarrWriter( store=path, - level_shapes=[[tensor_shape[d] for d in tensor.dims]], + level_shapes=[[tensor.tagged_shape[d] for d in tensor.dims]], dtype=tensor.dtype, zarr_format=zf, image_name=tensor.name or "Image", @@ -680,26 +645,22 @@ def _save_tensor_bioio( axes_units=[unit.get(a) for a in tensor.dims], physical_pixel_size=[scale.get(a, 1.0) for a in tensor.dims], channels=channels, + chunk_shape=[chunk_shape[a] for a in tensor.dims] + if chunk_shape is not None + else None, + shard_shape=[shard_shape[a] for a in tensor.dims] + if shard_shape is not None + else None, ) logger.info("OME-Zarr metadata: {}", writer.preview_metadata()) writer.write_full_volume(tensor.data.data) return elif path.suffix.lower() in (".tif", ".tiff"): - if roi_and_tensor_shape is not None: - raise NotImplementedError( - "Saving a region of interest (roi) is not implemented for saving tiff files with io_lib='bioio'." - ) - from bioio_ome_tiff.writers import OmeTiffWriter save = OmeTiffWriter.save # pyright: ignore[reportUnknownVariableType] bioio_axis_letters = "TCZYX" elif path.suffix.lower() in (".gif", ".mp4", ".mkv"): - if roi_and_tensor_shape is not None: - raise NotImplementedError( - "Saving a region of interest (roi) is not implemented for saving video files with io_lib='bioio'." - ) - from bioio_imageio.writers import TimeseriesWriter tensor = tensor.squeeze() @@ -719,11 +680,6 @@ def _save_tensor_bioio( ".wmv", ".ogg", ): - if roi_and_tensor_shape is not None: - raise NotImplementedError( - "Saving a region of interest (roi) is not implemented for saving 2d images with io_lib='bioio'." - ) - from bioio_imageio.writers import TwoDWriter tensor = tensor.squeeze() @@ -776,7 +732,8 @@ def _save_tensor_clearscale( path: Path | ZipPath | FtpUrl, tensor: Tensor, internal_path: PurePath | None, - roi_and_tensor_shape: tuple[PerAxis[SliceInfo], PerAxis[int]] | None, + chunk_shape: PerAxis[int] | None = None, + shard_shape: PerAxis[int] | None = None, ) -> None: import clearscale import dask.array @@ -808,29 +765,11 @@ def _save_tensor_clearscale( ) single_letter_dims = canonical_single_letter_dims - if roi_and_tensor_shape is None: - roi = (slice(None),) * len(tensor.dims) - roi_map = None - tensor_shape = tensor.tagged_shape - tensor_shape_tuple = tensor.shape - else: - roi_map, tensor_shape = roi_and_tensor_shape - roi = tuple( - slice(None) if (s := roi_map.get(d)) is None else slice(s.start, s.stop) - for d in tensor.dims - ) - tensor_shape_tuple = tuple(tensor_shape.get(d, 1) for d in tensor.dims) + tensor_shape_tuple = tuple(tensor.tagged_shape[d] for d in tensor.dims) logger.debug( "Saving tensor {} to {} with clearscale+zarr-python", - tensor.tagged_shape - if roi_map is None - else [ - f"{d}[:{s}]" - if (r := roi_map.get(d)) is None - else f"{d}[{r.start}:{r.stop}]" - for d, s in tensor.tagged_shape.items() - ], + tensor.tagged_shape, path, ) @@ -884,22 +823,28 @@ def _save_tensor_clearscale( name=SCALE_KEY, overwrite=True, dimension_names=None if ZARR_FORMAT == 2 else tuple(dim_map.values()), - shards=None if ZARR_FORMAT == 2 else "auto", + chunks="auto" + if chunk_shape is None + else tuple(chunk_shape[d] for d in tensor.dims), + shards=None + if ZARR_FORMAT == 2 + else "auto" + if shard_shape is None + else tuple(shard_shape[d] for d in tensor.dims), ) zarr_array = zarr_group.require_array( **create_array_kwargs, - shape=[tensor_shape[d] for d in tensor.dims], + shape=[tensor.tagged_shape[d] for d in tensor.dims], dtype=tensor.dtype, ) xr_array = tensor.data if dm := {k: v for k, v in dim_map.items() if k != v}: - xr_array = xr_array.rename(dm) # pyright: ignore[reportUnknownVariableType] - assert isinstance(xr_array, xr.DataArray) + xr_array = cast_and_assert(xr.DataArray, xr_array.rename(dm)) if isinstance(xr_array.data, np.ndarray): - zarr_array[roi] = xr_array.data + zarr_array[:] = xr_array.data elif isinstance(xr_array.data, dask.array.Array): - dask.array.to_zarr(xr_array.data, zarr_array, compute=True, region=roi) + dask.array.to_zarr(xr_array.data, zarr_array, compute=True) else: assert_never(xr_array.data) @@ -1129,6 +1074,7 @@ class _CreateArrayKwargs(TypedDict): overwrite: bool dimension_names: tuple[str, ...] | None shards: Literal["auto"] | tuple[int, ...] | None + chunks: Literal["auto"] | tuple[int, ...] _zarr_url_adapter: TypeAdapter[ZarrUrl] = TypeAdapter(ZarrUrl) @@ -1218,111 +1164,48 @@ def _get_file_source_extension_and_internal_path( def save_sample( path: Path | str | PerMember[Path | str], - sample: Sample | Iterable[SampleBlock], + sample: Sample, *, - max_workers: int | None = None, + chunk_shape: PerMember[PerAxis[int]] | None = None, + shard_shape: PerMember[PerAxis[int]] | None = None, ) -> None: """Save a **sample** to a **path** pattern or all sample members in the **path** mapping. - If **path** is a pathlib.Path or a string and the **sample** has multiple members, - **path** it must contain `{member_id}` (or `{input_id}` or `{output_id}`). + Args: + path: A path pattern or a mapping of member_id to path pattern. + If **path** is a pathlib.Path or a string and the **sample** has multiple members, + **path** it must contain `{member_id}` (or `{input_id}` or `{output_id}`). + sample: Sample to save + chunk_shape: Optional (re-)chunking of the sample data for applicable image formats (zarr). + shard_shape: Optional sharding of the sample data for applicable image formats (zarr). (Each) **path** may contain `{sample_id}` to be formatted with the **sample** object. """ - if isinstance(sample, Sample): - first_block = sample - remaining_blocks = () - else: - remaining_blocks = iter(sample) - first_block = next(remaining_blocks) - if not isinstance(path, Mapping): - if len(first_block.members) < 2 or any( + if len(sample.members) < 2 or any( m in str(path) for m in ("{member_id}", "{input_id}", "{output_id}") ): - path = {m: path for m in first_block.members} + path = {m: path for m in sample.members} else: raise ValueError( - f"path {path} must contain '{{member_id}}' for sample with multiple members {list(first_block.members)}." + f"path {path} must contain '{{member_id}}' for sample with multiple members {list(sample.members)}." ) - def save_block_member( - sample_block: Sample | SampleBlock, - m: MemberId, - p: Path | str, - io_lib: IO_Lib | None = None, - ) -> IO_Lib: - if isinstance(sample_block, Sample): - sample_id = sample_block.id - t = sample_block.members[m] - roi_and_tensor_shape = None - else: - sample_id = sample_block.sample_id - roi = dict(sample_block.blocks[m].inner_slice) - t = sample_block.blocks[m].inner_data - if ( - unstacked_batch_slice := sample_block.blocks[m].unstacked_batch_slice - ) is not None: - _ = roi.pop(BATCH_AXIS_ID, None) - assert not any(d in unstacked_batch_slice for d in roi), ( - unstacked_batch_slice, - roi, - ) - roi = {**unstacked_batch_slice, **roi} - t = t.unstack_batch_multi_index(errors="raise") - assert t.dims == tuple(roi.keys()), (t.dims, tuple(roi.keys())) - assert t.tagged_shape == {a: s.stop - s.start for a, s in roi.items()} - - tensor_shape = dict(sample_block.sample_shape[m]) - if ( - unstacked_sample_batch_shape - := sample_block.unstacked_sample_batch_shape - ) is not None: - _ = tensor_shape.pop(BATCH_AXIS_ID, None) - assert not any( - d in unstacked_sample_batch_shape for d in tensor_shape - ), ( - unstacked_sample_batch_shape, - tensor_shape, - ) - tensor_shape = {**unstacked_sample_batch_shape, **tensor_shape} - assert t.dims == tuple(tensor_shape.keys()), ( - t.dims, - tuple(tensor_shape.keys()), - ) - - assert ( - unstacked_sample_batch_shape is None - and unstacked_batch_slice is None - or ( - unstacked_sample_batch_shape is not None - and unstacked_batch_slice is not None - ) + with ThreadPoolExecutor(max_workers=min(len(sample.members), 4)) as executor: + for m, p in path.items(): + _ = executor.submit( + save_tensor, + Path( + str(p).format( + sample_id=sample.id, member_id=m, input_id=m, output_id=m + ) + ), + sample.members[m], + chunk_shape=chunk_shape[m] if chunk_shape is not None else None, + shard_shape=shard_shape[m] if shard_shape is not None else None, ) - roi_and_tensor_shape = (roi, tensor_shape) - - p_formatted = Path( - str(p).format(sample_id=sample_id, member_id=m, input_id=m, output_id=m) - ) - return _save_tensor_impl( - p_formatted, t, roi_and_tensor_shape=roi_and_tensor_shape, io_lib=io_lib - ) - - io_lib = None - with ( - ThreadPoolExecutor( - max_workers=1 # TODO: Fix missing blocks when saving blocks with clearscale+zarr-python in parallel - # TODO: check if all io_libs (with roi support) can handle parallel writing to the same file - ) as executor - ): - for sample_block in chain([first_block], remaining_blocks): - for m, p in path.items(): - if io_lib is None: - io_lib = save_block_member(sample_block, m, p, io_lib) - else: - _ = executor.submit(save_block_member, sample_block, m, p, io_lib) class _StatEntry(BaseModel, frozen=True, arbitrary_types_allowed=True): @@ -1414,3 +1297,69 @@ def ensure_unzipped(source: PermissiveFileSource | ZipPath | BytesReader, folder def get_suffix(source: ZipPath | FileSource) -> Suffix: """DEPRECATED: use source.suffix instead.""" return source.suffix + + +# def _determine_chunk_and_shard_shapes( +# shape: PerAxis[int], +# block_shape: PerAxis[int], +# item_size: int, +# ) -> tuple[PerAxis[int], PerAxis[int]]: +# def increase_shard_size(shard_size: int, shape_size: int, chunk_size: int) -> int: +# """increase shard size by chunk_size (or a multiple of chunk_size such that the last two chunks fall into the same shard). +# Will not increase shard size beyond shape_size. +# """ +# while ( +# shard_size < shape_size +# and shape_size % shard_size +# != 0 # last shard is smaller than the shard size +# and shape_size % shard_size +# < 2 * chunk_size # last shard is smaller than 2*inner_size, +# # but the last two blocks may overlap and must land in the same shard +# ): +# shard_size = min(shard_size + chunk_size, shape_size) + +# return shard_size + +# shard_shape: dict[AxisId, int] | None = {} +# # create smallest possible shard shape +# for a, shape_size in shape.items(): +# # The smallest possible shard shape is made up of the inner sizes (that will make up the chunks) +# block_size = block_shape[a] +# shard_shape[a] = increase_shard_size(block_size, shape_size, block_size) + +# shard_byte_size = prod(shard_shape.values()) * item_size +# if ( +# shard_byte_size < SHARDED_INNER_CHUNK_MAX_BYTES // 2 +# and BATCH_AXIS_ID in shape +# and unstacked_batch_shape is not None +# ): +# # increase shard shape along a stacked multi-index batch dimension if present (to optimize writing) +# shard_shape[BATCH_AXIS_ID] = max( +# shard_shape[BATCH_AXIS_ID], prod(unstacked_batch_shape.values()) +# ) + +# shard_byte_size = prod(shard_shape.values()) * item_size +# increase_order = [ +# a +# for a in ( +# CHANNEL_AXIS_ID, +# AxisId("x"), +# AxisId("y"), +# AxisId("z"), +# AxisId("t"), +# AxisId("i"), +# ) +# if a in shape +# ] +# increase_order += [a for a in shape if a not in increase_order] +# while ( +# any(shard_shape[a] < s for a, s in shape.items()) +# and shard_byte_size < SHARDED_INNER_CHUNK_MAX_BYTES // 2 +# ): +# for a in increase_order: +# shard_shape[a] = increase_shard_size( +# shard_shape[a], shape[a], inner_sizes[a] +# ) +# shard_byte_size = prod(shard_shape.values()) * item_size +# if shard_byte_size >= SHARDED_INNER_CHUNK_MAX_BYTES // 2: +# break diff --git a/src/bioimageio/core/prediction.py b/src/bioimageio/core/prediction.py index 8a019e2f..ed9ce95f 100644 --- a/src/bioimageio/core/prediction.py +++ b/src/bioimageio/core/prediction.py @@ -103,25 +103,21 @@ def predict( input_block_shape, ) - _n_intermediates, output = ( - pp.predict_sample_with_fixed_blocking_yield_intermediates( - sample, - input_block_shape=input_block_shape, - skip_preprocessing=skip_preprocessing, - skip_postprocessing=skip_postprocessing, - ) + _n_intermediates, output = pp.predict_with_fixed_blocking( + sample, + input_block_shape=input_block_shape, + skip_preprocessing=skip_preprocessing, + skip_postprocessing=skip_postprocessing, ) elif blocksize_parameter is not None: - _n_intermediates, output = ( - pp.predict_sample_with_blocking_yield_intermediates( - sample, - skip_preprocessing=skip_preprocessing, - skip_postprocessing=skip_postprocessing, - ns=blocksize_parameter, - ) + _n_intermediates, output = pp._predict_with_blocking_impl( + sample, + skip_preprocessing=skip_preprocessing, + skip_postprocessing=skip_postprocessing, + ns=blocksize_parameter, ) else: - output = pp.predict_sample_without_blocking( + output = pp.predict_without_blocking( sample, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, @@ -135,7 +131,7 @@ def predict( save_output = output return_output = output else: - if pp.has_non_blockwise_postprocessing: + if pp.has_samplewise_postprocessing: final_output = None for out in output: final_output = out diff --git a/src/bioimageio/core/proc_ops.py b/src/bioimageio/core/proc_ops.py index fbea430d..e081662f 100644 --- a/src/bioimageio/core/proc_ops.py +++ b/src/bioimageio/core/proc_ops.py @@ -28,7 +28,7 @@ from .axis import CHANNEL_AXIS_ID, AxisId from .common import DTypeStr, MemberId from .digest_spec import get_member_id, import_callable -from .sample import Sample, SampleBlock +from .sample import Sample from .stat_calculators import StatsCalculator from .stat_measures import ( DatasetMean, @@ -78,7 +78,7 @@ def __post_init__(self): def required_measures(self) -> Collection[Measure]: return set() - def __call__(self, sample: Sample | SampleBlock) -> None: + def __call__(self, sample: Sample) -> None: # | SampleBlock sample.stat.update(self.dataset_stats) diff --git a/src/bioimageio/core/remote_backends/gradio/client.py b/src/bioimageio/core/remote_backends/gradio/client.py index 2761c9c7..6e880edf 100644 --- a/src/bioimageio/core/remote_backends/gradio/client.py +++ b/src/bioimageio/core/remote_backends/gradio/client.py @@ -13,14 +13,14 @@ from ..._description_serializer import DescriptionSerializer from ..._model_adapter import RemoteModelAdapter -from ..._prediction_pipeline import IntermediatePrediction, RemotePredictionPipeline +from ..._prediction_pipeline import RemotePredictionPipeline from ..._settings import settings +from ..._utils.type_guards import is_sequence from ...axis import PerAxis from ...common import BlocksizeParameter, PerMember from ...io import JsonValue -from ...sample import Sample, SampleBlock +from ...sample import Sample from ...stat_measures import Measure, MeasureValue -from ...utils._type_guards import is_sequence from .serializer import GradioSampleSerializer SerializedSampleBlock = dict[str, JsonValue] @@ -239,7 +239,7 @@ def close(self): ) self._client.close() - def predict_sample_block( + def predict_block( self, sample_block: SampleBlock, skip_preprocessing: bool = False, @@ -272,7 +272,7 @@ def predict_sample_block( output_meta = sample_block.get_transformed_meta(self._block_transform) return output_meta.with_data(output_block.members, stat=sample_block.stat) - def _predict_sample_without_blocking_impl( + def _predict_without_blocking_impl( self, sample: Sample, skip_preprocessing: bool = False, @@ -296,7 +296,7 @@ def _predict_sample_without_blocking_impl( ) ) - def _predict_sample_with_fixed_blocking_yield_intermediates_impl( + def _predict_with_fixed_blocking_impl( self, sample: Sample, input_block_shape: PerMember[PerAxis[int]], @@ -347,7 +347,7 @@ def _intermediate_predictions() -> Iterable[IntermediatePrediction]: yield from block_iterator return ( - first_intermediate.last_block.blocks_in_sample, + first_intermediate.last_block.blocks_loc_shape, _intermediate_predictions(), ) diff --git a/src/bioimageio/core/remote_backends/gradio/serializer.py b/src/bioimageio/core/remote_backends/gradio/serializer.py index d5b0966c..a3e8e01a 100644 --- a/src/bioimageio/core/remote_backends/gradio/serializer.py +++ b/src/bioimageio/core/remote_backends/gradio/serializer.py @@ -3,6 +3,7 @@ import tempfile from collections.abc import Mapping from pathlib import Path +from typing import Any import numpy as np from gradio_client import handle_file @@ -13,7 +14,6 @@ from ..._sample_serializer import SampleSerializer from ...common import MemberId from ...io import JsonValue, load_stat, save_tensor, serialize_stat -from ...sample import SampleBlock, SampleBlockMeta from ...tensor import Tensor @@ -32,7 +32,7 @@ def from_tensor(cls, tensor: Tensor) -> Self: class _SerializableSampleBlock(BaseModel, frozen=True): - meta: SampleBlockMeta + meta: Any # SampleBlockMeta data: PerMemberAnno[_SerializableBlock | Path] serialized_stat: list[JsonValue] @@ -42,7 +42,7 @@ class _SerializableSampleBlock(BaseModel, frozen=True): class GradioSampleSerializer(SampleSerializer[SerializedSampleBlock]): @staticmethod - def serialize_sample_block(sample_block: SampleBlock) -> SerializedSampleBlock: + def serialize_sample_block(sample_block: Sample) -> SerializedSampleBlock: handled_members: dict[MemberId, _SerializableBlock] = {} for m, t in sample_block.members.items(): handled_members[m] = _SerializableBlock.from_tensor(t) diff --git a/src/bioimageio/core/remote_backends/gradio/server.py b/src/bioimageio/core/remote_backends/gradio/server.py index 9d2c4628..ae66accd 100644 --- a/src/bioimageio/core/remote_backends/gradio/server.py +++ b/src/bioimageio/core/remote_backends/gradio/server.py @@ -99,7 +99,7 @@ def setup(stat: Stat): for b in sample_block_iterator ), ): - output_block = pp.predict_sample_block( + output_block = pp.predict_block( block, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, @@ -114,8 +114,8 @@ def setup(stat: Stat): output_sample = None if isinstance(blocksize, int): try: - if pp.has_non_blockwise_postprocessing and not skip_postprocessing: - output_sample = pp.predict_sample_with_blocking( + if pp.has_samplewise_postprocessing and not skip_postprocessing: + output_sample = pp.predict_with_blocking( deserialized_input_sample, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, @@ -123,7 +123,7 @@ def setup(stat: Stat): batch_size=batch_size, ) else: - for output in pp.predict_sample_with_blocking_yield_intermediates( + for output in pp._predict_with_blocking_impl( deserialized_input_sample, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, @@ -145,7 +145,7 @@ def setup(stat: Stat): e, ) if output_sample is None: - output_sample = pp.predict_sample_without_blocking( + output_sample = pp.predict_without_blocking( deserialized_input_sample, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, diff --git a/src/bioimageio/core/sample.py b/src/bioimageio/core/sample.py index a264dc84..336963c5 100644 --- a/src/bioimageio/core/sample.py +++ b/src/bioimageio/core/sample.py @@ -1,56 +1,30 @@ from __future__ import annotations import collections.abc -from collections.abc import Iterable, Mapping, Sequence +from collections.abc import Mapping, Sequence from dataclasses import dataclass -from math import ceil, floor -from types import MappingProxyType from typing import ( Any, - Callable, - Generic, Literal, - TypeVar, - Union, ) -import numpy as np -import pydantic import xarray as xr from loguru import logger from numpy.typing import NDArray from typing_extensions import Self, assert_never -from bioimageio.core.utils._multi_index import get_unstacked_dim_order - -from ._common_annotations import PerMemberAnno -from .axis import BATCH_AXIS_ID, AxisId, PerAxis -from .block import Block -from .block_meta import ( - BlockMeta, - LinearAxisTransform, - split_multiple_shapes_into_blocks, -) +from ._utils.resolve_per_member_key import resolve_per_member_var +from .axis import AxisId, PerAxis from .common import ( - BlockIndex, - Halo, - HaloLike, MemberId, PadMode, PadWidthLike, PerMember, SampleId, SliceInfo, - TotalNumberOfBlocks, ) from .stat_measures import Stat from .tensor import Tensor -from .utils._multi_index import ( - MultiIndex, - create_tensor_with_multiindex, - get_unstacked_shape, -) -from .utils._resolve_per_member_key import resolve_per_member_var @dataclass @@ -63,46 +37,39 @@ class Sample: `MemberId("raw")` and `MemberId("mask")` image. """ + id: SampleId + """Identifies the `Sample` within the dataset -- typically a number or a string.""" + members: dict[MemberId, Tensor] """The sample's tensors""" stat: Stat """Sample and dataset statistics""" - id: SampleId - """Identifies the `Sample` within the dataset -- typically a number or a string.""" - - unstacked_dim_order: PerMember[Sequence[AxisId] | None] | None = None - """The order of the fully unstacked tensor""" + # unstacked_dim_order: PerMember[Sequence[AxisId] | None] | None = None + # """The order of the fully unstacked tensor""" def __getitem__( self, - key: PerMember[ - SliceInfo - | slice - | int - | PerAxis[SliceInfo | slice | int] - | Tensor - | xr.DataArray - ] - | PerAxis[SliceInfo | slice | int], + key: PerMember[PerAxis[SliceInfo | slice | int] | Tensor | xr.DataArray] + | PerAxis[SliceInfo | slice | int] + | Tensor + | xr.DataArray, ) -> Self: per_member_key = resolve_per_member_var( key, - valid_member_index_types=( + valid_per_member_types=( SliceInfo, slice, int, Tensor, xr.DataArray, - Mapping[AxisId, Union[SliceInfo, slice, int]], ), - valid_axis_index_types=(SliceInfo, slice, int), + valid_per_axis_types=(SliceInfo, slice, int), members=self.members, ) return self.__class__( - unstacked_dim_order=self.unstacked_dim_order, members={ m: t[per_member_key[m]] for m, t in self.members.items() @@ -118,237 +85,379 @@ def __postinit__(self): if t.name is None: t.name = str(m) - @property - def batch_multi_index(self) -> MultiIndex | None: - """Return the batch multi-index of the sample, if it has one. - - Returns: - The batch multi-index of the sample, or `None` if the sample does not have a batch dimension. - """ - if not self.members: - return None - - for tensor in self.members.values(): - idx = tensor.batch_multi_index - if isinstance(idx, MultiIndex): - return idx - - return None - - def set_block(self, block: SampleBlock) -> None: - """Set values of `block`. - - Note: - - Updates only existing sample members (extra block members are ignored) - - Ignores missing block members (i.e. members in the sample but not in the block are not modified) - - Raises: - ValueError if block and sample members do not overlap at all. - """ - no_overlap = True - for m in self.members: - if m not in block.blocks: - continue - b = block.blocks[m] - self.members[m][b.inner_slice] = b.inner_data - no_overlap = False - - if no_overlap: - raise ValueError( - f"block with members {list(block.blocks)} does not overlap with sample members {list(self.members)}" - ) + # def chunk( + # self, + # chunk_shapes: PerMember[PerAxis[int]], + # *, + # halo: PerMember[PerAxis[HaloLike]] | None = None, + # pad_mode: PadMode | PerMember[PadMode] = "symmetric", + # ) -> Self: + # """Return a new sample with chunked sample members. + + # Args: + # chunk_shapes: The chunk shapes per member and axis. + # """ + # return self.__class__( + # members=chunk_tensors( + # self.members, chunk_shapes=chunk_shapes, halo=halo, pad_mode=pad_mode + # ), + # stat=self.stat, + # id=self.id, + # ) + + # def chunkwise( + # self, + # func: Callable[[Sample], Sample], + # ) -> Self: + # """Apply a function to each chunk of the chunked sample. + + # See [chunk][] for chunking a sample. + + # Args: + # func: The function to apply to each sample chunk. + # """ + # return self.__class__( + # id=self.id, + # members=chunkwise(self.members, func), + # stat=self.stat, + # ) + + # def split_into_blocks( + # self, + # block_shapes: PerMember[PerAxis[int]] | PerAxis[int], + # *, + # halo: PerMember[PerAxis[HaloLike]], + # broadcast: bool = False, + # ) -> tuple[BlockLocShape, PerMember[BlockArray]]: + # """Split multiple shapes (e.g. sample member shapes) into aligned blocks. + + # Args: + # block_shapes: the block shape per sample member (depending on `broaadcast` the block shapes must result in the same number of blocks for all sample members) + # halo: the halo around each block per sample member + # broadcast: whether to broadcast block shapes of members resulting in fewer blocks to match other sample members. + # """ + + # if unknown_blocks := [t for t in block_shapes if t not in self.shape]: + # raise ValueError( + # f"block shape specified for unknown tensors: {unknown_blocks}." + # ) + + # if not block_shapes: + # b_shapes = self.shape + # elif is_mapping(block_shapes, v_type=int): + # b_shapes = {m: block_shapes for m in self.shape} + # else: + # assert not any(isinstance(v, int) for v in block_shapes.values()) + # b_shapes: PerMember[PerAxis[int]] = block_shapes # pyright: ignore[reportAssignmentType] + + # del block_shapes + + # if not broadcast and ( + # missing_blocks := [t for t in self.shape if t not in b_shapes] + # ): + # raise ValueError( + # f"No block shape specified for {missing_blocks}." + # + " Set `broadcast` to True if these tensors should be repeated" + # + " as a whole for each block." + # ) + + # if extra_halo := [t for t in halo if t not in b_shapes]: + # raise ValueError( + # f"`halo` specified for tensors without block shape: {extra_halo}." + # ) + + # blocks: PerMember[BlockArray] = {} + # block_locs: PerMember[BlockLocShape] = {} + # for m in b_shapes: + # block_locs[m], blocks[m] = self._split_member_into_blocks( + # member=m, + # block_shape=b_shapes[m], + # halo=halo.get(m, {}), + # ) + # assert all(b > 0 for b in block_locs[m]), block_locs + + # assert len(blocks) > 0, blocks + # assert len(block_locs) > 0, block_locs + # unique_block_locs = set(block_locs.values()) + + # if len(unique_block_locs) != 1: + # if not broadcast: + # raise ValueError( + # f"Mismatch for number of blocks per dimension: { {t: b.shape for t, b in blocks.items()} }. Set `broadcast` to True if this is expected." + # ) + + # blocks = dict(zip(blocks.keys(), np.broadcast_arrays(*blocks.values()))) + # block_locs = {m: b.shape for m, b in blocks.items()} + # unique_block_locs = set(block_locs.values()) + # assert len(unique_block_locs) == 1, ( + # f"Mismatch for total number of blocks (after braodcasting?!): {block_locs}" + # ) + + # return next(iter(unique_block_locs)), blocks + + # def foo(*bs: BlockMeta) -> SampleBlockMeta: + # return SampleBlockMeta.from_blocks(dict(zip(blocks.keys(), bs))) + + # foo_op = np.frompyfunc(foo, len(blocks), 1) + # out = foo_op(*blocks.values()) + # assert out.shape == next(iter(blocks.values())).shape + # return n, out + + # @property + # def batch_multi_index(self) -> MultiIndex | None: + # """Return the batch multi-index of the sample, if it has one. + + # Returns: + # The batch multi-index of the sample, or `None` if the sample does not have a batch dimension. + # """ + # if not self.members: + # return None + + # for tensor in self.members.values(): + # idx = tensor.batch_multi_index + # if isinstance(idx, MultiIndex): + # return idx + + # return None + + # def set_block(self, block: SampleBlock) -> None: + # """Set values of `block`. + + # Note: + # - Updates only existing sample members (extra block members are ignored) + # - Ignores missing block members (i.e. members in the sample but not in the block are not modified) + + # Raises: + # ValueError if block and sample members do not overlap at all. + # """ + # no_overlap = True + # for m in self.members: + # if m not in block.blocks: + # continue + # b = block.blocks[m] + # self.members[m][b.inner_slice] = b.inner_data + # no_overlap = False + + # if no_overlap: + # raise ValueError( + # f"block with members {list(block.blocks)} does not overlap with sample members {list(self.members)}" + # ) @property def shape(self) -> PerMember[PerAxis[int]]: return {tid: t.sizes for tid, t in self.members.items()} - @property - def unstacked_batch_shape(self) -> PerAxis[int] | None: - """The shape of the unstacked batch axis - For example `{"t": 10, "z": 5}` for a sample with stacked "t" and "z" dimensions resulting in a batch length of 50). - Note: As the batch axis has to match across members this is the same for all members.""" - if (idx := self.batch_multi_index) is None: - return None - - return { - BATCH_AXIS_ID if a == "original_batch" else AxisId(a): s - for a, s in zip(idx.names, idx.levshape) - } - - @property - def unstacked_shape(self) -> PerMember[PerAxis[int]]: - """combined (stacked) shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" - return { - m: get_unstacked_shape( - sh, - unstacked_dim_order=self.unstacked_dim_order[m] - if self.unstacked_dim_order is not None - else None, - unstacked_batch_shape=self.unstacked_batch_shape, - ) - for m, sh in self.shape.items() - } + # @property + # def unstacked_batch_shape(self) -> PerAxis[int] | None: + # """The shape of the unstacked batch axis + # For example `{"t": 10, "z": 5}` for a sample with stacked "t" and "z" dimensions resulting in a batch length of 50). + # Note: As the batch axis has to match across members this is the same for all members.""" + # if (idx := self.batch_multi_index) is None: + # return None + + # return { + # BATCH_AXIS_ID if a == "original_batch" else AxisId(a): s + # for a, s in zip(idx.names, idx.levshape) + # } + + # @property + # def unstacked_shape(self) -> PerMember[PerAxis[int]]: + # """combined (stacked) shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" + # return { + # m: get_unstacked_shape( + # sh, + # unstacked_dim_order=self.unstacked_dim_order[m] + # if self.unstacked_dim_order is not None + # else None, + # unstacked_batch_shape=self.unstacked_batch_shape, + # ) + # for m, sh in self.shape.items() + # } def as_arrays(self) -> dict[MemberId, NDArray[Any]]: """Return sample as dictionary of arrays.""" return {m: t.to_numpy() for m, t in self.members.items()} - def split_into_blocks( - self, - block_shapes: PerMember[PerAxis[int]], - halo: PerMember[PerAxis[HaloLike]], - pad_mode: PadMode | PerMember[PadMode], - broadcast: bool = False, - ) -> tuple[TotalNumberOfBlocks, Iterable[SampleBlockWithOrigin]]: - assert not (missing := [m for m in block_shapes if m not in self.members]), ( - f"`block_shapes` specified for unknown members: {missing}" - ) - assert not (missing := [m for m in halo if m not in block_shapes]), ( - f"`halo` specified for members without `block_shape`: {missing}" - ) - - n_blocks, blocks = split_multiple_shapes_into_blocks( - shapes=self.shape, - unstacked_batch_shape=self.unstacked_batch_shape, - unstacked_dim_order=self.unstacked_dim_order, - block_shapes=block_shapes, - halo=halo, - broadcast=broadcast, - ) - return n_blocks, sample_block_generator(blocks, origin=self, pad_mode=pad_mode) - - def as_single_block(self, halo: PerMember[PerAxis[Halo]] | None = None): - if halo is None: - halo = {} - - return SampleBlockWithOrigin( - sample_shape=self.shape, - sample_id=self.id, - blocks={ - m: Block( - sample_shape=self.shape[m], - unstacked_sample_batch_shape=self.unstacked_batch_shape, - unstacked_dim_order=self.unstacked_dim_order[m] - if self.unstacked_dim_order is not None - else None, - data=data, - inner_slice={a: SliceInfo(0, s) for a, s in self.shape[m].items()}, - unstacked_batch_slice=None - if self.unstacked_batch_shape is None - else { - a: SliceInfo(0, s) - for a, s in self.unstacked_batch_shape.items() - }, - halo=halo.get(m, {}), - block_index=0, - blocks_in_sample=1, - ) - for m, data in self.members.items() - }, - stat=self.stat, - origin=self, - block_index=0, - blocks_in_sample=1, - ) - - @classmethod - def from_blocks( - cls, - sample_blocks: Iterable[SampleBlock], - *, - fill_value: float = float("nan"), - ) -> Self: - """Create a `Sample` from an iterable of `SampleBlock`s. - - Note: - All sample blocks must have the same `sample_id`. - - Args: - sample_blocks: The blocks to create the sample from. - fill_value: The value to fill missing values with (default: `nan`). - """ - output = None - for output in cls.from_blocks_yield_intermediates( - sample_blocks, fill_value=fill_value - ): - pass - - if output is None: - raise ValueError("no sample blocks provided") - - return output - - @classmethod - def from_blocks_yield_intermediates( - cls, - sample_blocks: Iterable[SampleBlock], - *, - fill_value: float = float("nan"), - ): - """Create a `Sample` from an iterable of `SampleBlock`s, yielding the intermediate sample after each block. - - Args: - sample_blocks: The blocks to create the sample from. - fill_value: The value to fill missing values with (default: `nan`). - """ - output = None - for sample_block in sample_blocks: - if output is None: - output = cls( - members={}, - stat={}, - id=None, - unstacked_dim_order=sample_block.unstacked_dim_order, - ) - if output.id is None: - output.id = sample_block.sample_id - else: - assert output.id == sample_block.sample_id, ( - "sample id changed between sample blocks" - ) - - output.stat = sample_block.stat - - for m, block in sample_block.blocks.items(): - if m not in output.members: - if -1 in block.sample_shape.values(): - raise NotImplementedError( - "merging blocks with data dependent axis not yet implemented" - ) - - if ( - block.unstacked_sample_batch_shape is not None - and block.unstacked_dim_order is not None - ): - sizes = { - **block.sample_shape, - **block.unstacked_sample_batch_shape, - } - full_unstacked_shape = { - a: sizes[a] for a in block.unstacked_dim_order - } - t = create_tensor_with_multiindex( - full_unstacked_shape, - fill_value=fill_value, - dtype=np.dtype(block.data.dtype), - transpose_to=tuple(block.sample_shape.keys()), - ) - else: - t = Tensor( - np.full( - shape=tuple(block.sample_shape.values()), - fill_value=fill_value, - dtype=block.data.dtype, - ), - dims=tuple(block.sample_shape.keys()), - ) - - output.members[m] = t - - output.members[m][block.inner_slice] = block.inner_data - yield output - - yield output + # def split_into_blocks_meta( + # self, + # block_shapes: PerMember[PerAxis[int]], + # halo: PerMember[PerAxis[HaloLike]], + # broadcast: bool = False, + # ) -> tuple[BlockLocShape, NDArray[Any]]: # really NDArray[SampleBlockMeta] + # assert not (missing := [m for m in block_shapes if m not in self.members]), ( + # f"`block_shapes` specified for unknown members: {missing}" + # ) + # assert not (missing := [m for m in halo if m not in block_shapes]), ( + # f"`halo` specified for members without `block_shape`: {missing}" + # ) + + # n, blocks = split_multiple_shapes_into_blocks( + # shapes=self.shape, + # unstacked_batch_shape=self.unstacked_batch_shape, + # unstacked_dim_order=self.unstacked_dim_order, + # block_shapes=block_shapes, + # halo=halo, + # broadcast=broadcast, + # ) + # return n, sample_block_meta_generator(blocks, sample_id=self.id) + + # def split_into_blocks( + # self, + # block_shapes: PerMember[PerAxis[int]], + # halo: PerMember[PerAxis[HaloLike]], + # pad_mode: PadMode | PerMember[PadMode], + # broadcast: bool = False, + # ) -> tuple[BlockLocShape, Iterable[SampleBlockWithOrigin]]: + # n_blocks, blocks = self.split_into_blocks_meta(block_shapes, halo, broadcast) + # return n_blocks, sample_block_generator( + # blocks.flatten(), origin=self, pad_mode=pad_mode + # ) + + # def get_blocks( + # self, + # block_shapes: PerMember[PerAxis[int]], + # *, + # halo: PerMember[PerAxis[HaloLike]], + # pad_mode: PadMode | PerMember[PadMode], + # broadcast: bool = False, + # ) -> tuple[BlockLocShape, PerMember[BlockMetaArray]]: + # n_blocks, blocks = self.split_into_blocks_meta(block_shapes, halo, broadcast) + # return n_blocks, sample_block_generator( + # blocks.flatten(), origin=self, pad_mode=pad_mode + # ) + + # def as_single_block(self, halo: PerMember[PerAxis[Halo]] | None = None): + # if halo is None: + # halo = {} + + # return SampleBlockWithOrigin( + # sample_shape=self.shape, + # sample_id=self.id, + # blocks={ + # m: Block( + # sample_shape=self.shape[m], + # unstacked_sample_batch_shape=self.unstacked_batch_shape, + # unstacked_dim_order=self.unstacked_dim_order[m] + # if self.unstacked_dim_order is not None + # else None, + # data=data, + # inner_slice={a: SliceInfo(0, s) for a, s in self.shape[m].items()}, + # unstacked_batch_slice=None + # if self.unstacked_batch_shape is None + # else { + # a: SliceInfo(0, s) + # for a, s in self.unstacked_batch_shape.items() + # }, + # halo=halo.get(m, {}), + # block_loc=0, + # block_loc_shape=1, + # ) + # for m, data in self.members.items() + # }, + # stat=self.stat, + # origin=self, + # block_loc=0, + # blocks_loc_shape=1, + # ) + + # @classmethod + # def from_blocks( + # cls, + # sample_blocks: Iterable[SampleBlock], + # *, + # fill_value: float = float("nan"), + # ) -> Self: + # """Create a `Sample` from an iterable of `SampleBlock`s. + + # Note: + # All sample blocks must have the same `sample_id`. + + # Args: + # sample_blocks: The blocks to create the sample from. + # fill_value: The value to fill missing values with (default: `nan`). + # """ + # output = None + # for output in cls.from_blocks_yield_intermediates( + # sample_blocks, fill_value=fill_value + # ): + # pass + + # if output is None: + # raise ValueError("no sample blocks provided") + + # return output + + # @classmethod + # def from_blocks_yield_intermediates( + # cls, + # sample_blocks: Iterable[SampleBlock], + # *, + # fill_value: float = float("nan"), + # ): + # """Create a `Sample` from an iterable of `SampleBlock`s, yielding the intermediate sample after each block. + + # Args: + # sample_blocks: The blocks to create the sample from. + # fill_value: The value to fill missing values with (default: `nan`). + # """ + # output = None + # for sample_block in sample_blocks: + # if output is None: + # output = cls( + # members={}, + # stat={}, + # id=None, + # unstacked_dim_order=sample_block.unstacked_dim_order, + # ) + # if output.id is None: + # output.id = sample_block.sample_id + # else: + # assert output.id == sample_block.sample_id, ( + # "sample id changed between sample blocks" + # ) + + # output.stat = sample_block.stat + + # for m, block in sample_block.blocks.items(): + # if m not in output.members: + # if -1 in block.sample_shape.values(): + # raise NotImplementedError( + # "merging blocks with data dependent axis not yet implemented" + # ) + + # if ( + # block.unstacked_sample_batch_shape is not None + # and block.unstacked_dim_order is not None + # ): + # sizes = { + # **block.sample_shape, + # **block.unstacked_sample_batch_shape, + # } + # full_unstacked_shape = { + # a: sizes[a] for a in block.unstacked_dim_order + # } + # t = create_tensor_with_multiindex( + # full_unstacked_shape, + # fill_value=fill_value, + # dtype=np.dtype(block.data.dtype), + # transpose_to=tuple(block.sample_shape.keys()), + # ) + # else: + # t = Tensor( + # np.full( + # shape=tuple(block.sample_shape.values()), + # fill_value=fill_value, + # dtype=block.data.dtype, + # ), + # dims=tuple(block.sample_shape.keys()), + # ) + + # output.members[m] = t + + # output.members[m][block.inner_slice] = block.inner_data + # yield output + + # yield output def pad( self, @@ -373,96 +482,90 @@ def pad( }, stat=self.stat, id=self.id, - unstacked_dim_order=self.unstacked_dim_order, - ) - - def transpose( - self, - axes: PerMember[Sequence[AxisId]], - *, - extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "raise", - missing_dims: Literal[ - "raise", "expand", "unstack", "unstack_or_expand" - ] = "raise", - ) -> Self: - """Return a new sample with transposed sample members. - - Raises: - ValueError: If not all batch dimensions have the same length after transposition (and possibly stacking/unstacking extra dimensions). - - """ - if any(unknown := [m not in self.members for m in axes]): - raise ValueError(f"Axes specified for unknown members: {unknown}") - - members: dict[MemberId, Tensor] = {} - individual_unstacked_dim_order: dict[MemberId, list[AxisId] | None] = {} - for m, t in self.members.items(): - if m in axes: - members[m], individual_unstacked_dim_order[m] = ( - t.transpose_and_return_unstacked_dim_order( - axes=axes[m], - extra_dims=extra_dims, - missing_dims=missing_dims, - ) - ) - else: - members[m] = t - individual_unstacked_dim_order[m] = None - - if any(order is None for order in individual_unstacked_dim_order.values()): - unstacked_dim_order = None - else: - unstacked_dim_order = { - m: order - for m, order in individual_unstacked_dim_order.items() - if order is not None - } - - if ( - len( - batch_lengths := { - t.sizes[AxisId("batch")] - for t in members.values() - if AxisId("batch") in t.dims - } - ) - > 1 - ): - raise ValueError( - f"Transposed sample members have incompatible batch lengths: {batch_lengths}." - ) - - return self.__class__( - members=members, - stat=dict(self.stat), - id=self.id, - unstacked_dim_order=unstacked_dim_order, ) - def assign_batch_multi_index(self, multi_index: MultiIndex) -> Self: - """Return a new sample with the batch multi-index assigned to all sample members. - - Raises: - ValueError: If not all sample members have a batch dimension. - """ - if len( - no_batch := [ - m for m, t in self.members.items() if AxisId("batch") not in t.dims - ] - ) == len(self.members): - raise ValueError(f"No member has a batch dimension: {no_batch}") - - return self.__class__( - members={ - m: t - if AxisId("batch") not in t.dims - else t.assign_batch_multi_index(multi_index) - for m, t in self.members.items() - }, - stat=dict(self.stat), - id=self.id, - unstacked_dim_order=self.unstacked_dim_order, - ) + # def transpose( + # self, + # axes: PerMember[Sequence[AxisId]], + # *, + # extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "raise", + # missing_dims: Literal[ + # "raise", "expand", "unstack", "unstack_or_expand" + # ] = "raise", + # ) -> Self: + # """Return a new sample with transposed sample members. + + # Raises: + # ValueError: If not all batch dimensions have the same length after transposition (and possibly stacking/unstacking extra dimensions). + + # """ + # if any(unknown := [m not in self.members for m in axes]): + # raise ValueError(f"Axes specified for unknown members: {unknown}") + + # members: dict[MemberId, Tensor] = {} + # individual_unstacked_dim_order: dict[MemberId, list[AxisId] | None] = {} + # for m, t in self.members.items(): + # if m in axes: + # members[m], individual_unstacked_dim_order[m] = ( + # t.transpose_and_return_unstacked_dim_order( + # axes=axes[m], + # extra_dims=extra_dims, + # missing_dims=missing_dims, + # ) + # ) + # else: + # members[m] = t + # individual_unstacked_dim_order[m] = None + + # # if any(order is None for order in individual_unstacked_dim_order.values()): + # # unstacked_dim_order = None + # # else: + # # unstacked_dim_order = { + # # m: order + # # for m, order in individual_unstacked_dim_order.items() + # # if order is not None + # # } + + # if ( + # len( + # batch_lengths := { + # t.sizes[AxisId("batch")] + # for t in members.values() + # if AxisId("batch") in t.dims + # } + # ) + # > 1 + # ): + # raise ValueError( + # f"Transposed sample members have incompatible batch lengths: {batch_lengths}." + # ) + + # return self.__class__(members=members, stat=dict(self.stat), id=self.id) + + # def assign_batch_multi_index(self, multi_index: MultiIndex) -> Self: + # """Return a new sample with the batch multi-index assigned to all sample members. + + # Raises: + # ValueError: If not all sample members have a batch dimension. + # """ + # if len( + # no_batch := [ + # m for m, t in self.members.items() if AxisId("batch") not in t.dims + # ] + # ) == len(self.members): + # raise ValueError(f"No member has a batch dimension: {no_batch}") + + # return self.__class__( + # members={ + # m: t + # if AxisId("batch") not in t.dims + # else t.assign_batch_multi_index(multi_index) + # for m, t in self.members.items() + # }, + # stat=dict(self.stat), + # id=self.id, + # unstacked_dim_order=self.unstacked_dim_order, + # ) def unstack_batch_multi_index( self, @@ -530,356 +633,121 @@ def unstack_batch_multi_index( members=members, stat=stat, id=self.id, - unstacked_dim_order=None, - ) - - -BlockT = TypeVar("BlockT", bound=BlockMeta) - - -@pydantic.dataclasses.dataclass(frozen=True) -class SampleBlockBase(Generic[BlockT]): - """base class for `SampleBlockMeta` and `SampleBlock`""" - - sample_shape: PerMemberAnno[PerAxis[int]] - """the sample shape this block represents a part of""" - - sample_id: SampleId - """identifier for the sample within its dataset""" - - blocks: PerMemberAnno[BlockT] - """Individual tensor blocks comprising this sample block""" - - block_index: BlockIndex - """the n-th block of the sample""" - - blocks_in_sample: TotalNumberOfBlocks - """total number of blocks in the sample""" - - @property - def shape(self) -> PerMember[PerAxis[int]]: - return MappingProxyType({mid: b.shape for mid, b in self.blocks.items()}) - - @property - def inner_shape(self) -> PerMember[PerAxis[int]]: - return MappingProxyType({mid: b.inner_shape for mid, b in self.blocks.items()}) - - @property - def unstacked_sample_batch_shape(self) -> PerAxis[int] | None: - """The shape of the unstacked batch axis, if the sample has a multi-index batch axis""" - for b in self.blocks.values(): - if b.unstacked_sample_batch_shape is not None: - return b.unstacked_sample_batch_shape - - return None - - @property - def unstacked_dim_order(self) -> PerMemberAnno[Sequence[AxisId] | None] | None: - """The dim order of the unstacked tensors, if the sample has a multi-index batch axis""" - for b in self.blocks.values(): - if b.unstacked_dim_order is not None: - return MappingProxyType( - {mid: bb.unstacked_dim_order for mid, bb in self.blocks.items()} - ) - - return None - - @property - def unstacked_sample_shape(self) -> PerMemberAnno[PerAxis[int]]: - """combined (stacked) sample shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" - return MappingProxyType( - {mid: b.unstacked_sample_shape for mid, b in self.blocks.items()} - ) - - -@dataclass -class LinearSampleAxisTransform(LinearAxisTransform): - member: MemberId - - -@pydantic.dataclasses.dataclass(frozen=True) -class SampleBlockMeta(SampleBlockBase[BlockMeta]): - """Meta data of a dataset sample block""" - - def get_transformed( - self, new_axes: PerMember[PerAxis[LinearSampleAxisTransform | int]] - ) -> Self: - sample_shape = { - m: { - a: ( - trf - if isinstance(trf, int) - else trf.compute(self.sample_shape[trf.member][trf.axis]) - ) - for a, trf in new_axes[m].items() - } - for m in new_axes - } - - def get_member_halo(m: MemberId, round: Callable[[float], int]): - return { - a: ( - Halo(0, 0) - if isinstance(trf, int) - or trf.axis not in self.blocks[trf.member].halo - else Halo( - round(self.blocks[trf.member].halo[trf.axis].left * trf.scale), - round(self.blocks[trf.member].halo[trf.axis].right * trf.scale), - ) - ) - for a, trf in new_axes[m].items() - } - - halo: dict[MemberId, dict[AxisId, Halo]] = {} - for m in new_axes: - halo[m] = get_member_halo(m, floor) - if halo[m] != get_member_halo(m, ceil): - raise ValueError( - f"failed to unambiguously scale halo {halo[m]} with {new_axes[m]}" - + f" for {m}." - ) - - inner_slice = { - m: { - a: ( - SliceInfo(0, trf) - if isinstance(trf, int) - else SliceInfo( - trf.compute( - self.blocks[trf.member].inner_slice[trf.axis].start - ), - trf.compute(self.blocks[trf.member].inner_slice[trf.axis].stop), - ) - ) - for a, trf in new_axes[m].items() - } - for m in new_axes - } - unstacked_batch_slice = None - for b in self.blocks.values(): - if b.unstacked_batch_slice is None: - continue - elif unstacked_batch_slice is None: - unstacked_batch_slice = b.unstacked_batch_slice - else: - assert unstacked_batch_slice == b.unstacked_batch_slice, ( - "Incompatible unstacked batch slices across sample block members" - ) - - unstacked_dim_order: dict[MemberId, list[AxisId] | None] = {} - for m, trfs in new_axes.items(): - for trf in trfs.values(): - if isinstance(trf, LinearSampleAxisTransform): - unstacked_in_order = self.blocks[trf.member].unstacked_dim_order - stacked_dims = list( - self.blocks[trf.member].unstacked_sample_batch_shape or () - ) - unstacked_dim_order[m] = get_unstacked_dim_order( - list(trfs), - unstacked_reference_order=unstacked_in_order, - stacked_dims=stacked_dims, - ) - break - elif isinstance(trf, int): - pass - else: - assert_never(trf) - else: - unstacked_dim_order[m] = None - - return self.__class__( - blocks={ - m: BlockMeta( - sample_shape=sample_shape[m], - unstacked_sample_batch_shape=self.unstacked_sample_batch_shape, - unstacked_dim_order=unstacked_dim_order[m], - inner_slice=inner_slice[m], - unstacked_batch_slice=unstacked_batch_slice, - halo=halo[m], - block_index=self.block_index, - blocks_in_sample=self.blocks_in_sample, - ) - for m in new_axes - }, - sample_shape=sample_shape, - sample_id=self.sample_id, - block_index=self.block_index, - blocks_in_sample=self.blocks_in_sample, - ) - - def with_data(self, data: PerMember[Tensor], *, stat: Stat) -> SampleBlock: - return SampleBlock( - sample_shape={ - m: { - a: data[m].tagged_shape[a] if s == -1 else s - for a, s in member_shape.items() - } - for m, member_shape in self.sample_shape.items() - }, - sample_id=self.sample_id, - blocks={ - m: Block.from_meta(b, data=data[m]) for m, b in self.blocks.items() - }, - stat=stat, - block_index=self.block_index, - blocks_in_sample=self.blocks_in_sample, - ) - - -@dataclass(frozen=True) -class SampleBlock(SampleBlockBase[Block]): - """A block of a dataset sample""" - - blocks: dict[MemberId, Block] - """Individual tensor blocks comprising this sample block""" - - stat: Stat - """computed statistics""" - - @property - def members(self) -> PerMember[Tensor]: - """the sample block's tensors""" - return {m: b.data for m, b in self.blocks.items()} - - def get_transformed_meta( - self, new_axes: PerMember[PerAxis[LinearSampleAxisTransform | int]] - ) -> SampleBlockMeta: - return SampleBlockMeta( - sample_id=self.sample_id, - blocks=dict(self.blocks), - sample_shape=self.sample_shape, - block_index=self.block_index, - blocks_in_sample=self.blocks_in_sample, - ).get_transformed(new_axes) - - @classmethod - def from_meta( - cls, meta: SampleBlockMeta, data: PerMember[Tensor], stat: Stat - ) -> Self: - return cls( - sample_shape=meta.sample_shape, - sample_id=meta.sample_id, - blocks={ - m: Block.from_meta(b, data=data[m]) for m, b in meta.blocks.items() - }, - stat=stat, - block_index=meta.block_index, - blocks_in_sample=meta.blocks_in_sample, - ) - - def get_meta(self) -> SampleBlockMeta: - return SampleBlockMeta( - sample_id=self.sample_id, - blocks={m: b.get_meta() for m, b in self.blocks.items()}, - sample_shape=self.sample_shape, - block_index=self.block_index, - blocks_in_sample=self.blocks_in_sample, - ) - - def as_sample(self) -> Sample: - """Convert this sample block to a `Sample` with the shape of this block. - - Note: - If you want to convert one or more sample block to a sample with the shape of the original, whole sample, - use `Sample.from_blocks()` instead. - """ - return Sample( - members=dict(self.members), - stat=dict(self.stat), - id=self.sample_id, - unstacked_dim_order=self.unstacked_dim_order, ) - @property - def unstacked_dim_order(self) -> PerMember[Sequence[AxisId] | None]: - return {m: b.unstacked_dim_order for m, b in self.blocks.items()} - - @property - def batch_multi_index(self) -> MultiIndex | None: - """Return the batch multi-index of the sample block, if it has one. - Returns: - The batch multi-index of the sample block, or `None` if the sample block does not have a multi-index batch dimension. - """ - for tensor in self.members.values(): - idx = tensor.batch_multi_index - if idx is not None: - return idx - - return None - - -@dataclass(frozen=True) -class SampleBlockWithOrigin(SampleBlock): - """A `SampleBlock` with a reference (`origin`) to the whole `Sample`""" - - origin: Sample - """the sample this sample block was taken from""" - - -class _ConsolidatedMemberBlocks: - def __init__(self, blocks: PerMember[BlockMeta]): - super().__init__() - block_indices = {b.block_index for b in blocks.values()} - assert len(block_indices) == 1 - self.block_index = block_indices.pop() - blocks_in_samples = {b.blocks_in_sample for b in blocks.values()} - assert len(blocks_in_samples) == 1 - self.blocks_in_sample = blocks_in_samples.pop() - - -def sample_block_meta_generator( - blocks: Iterable[PerMember[BlockMeta]], - *, - sample_shape: PerMember[PerAxis[int]], # TODO: remove redundant sample_shape arg? - sample_id: SampleId, -): - for member_blocks in blocks: - cons = _ConsolidatedMemberBlocks(member_blocks) - unstacked_batch_shape = None - for m, b in member_blocks.items(): - if b.unstacked_sample_batch_shape is not None: - if unstacked_batch_shape is None: - unstacked_batch_shape = b.unstacked_sample_batch_shape - else: - assert unstacked_batch_shape == b.unstacked_sample_batch_shape, ( - f"unstacked batch shape mismatch for member {m}: {unstacked_batch_shape} != {b.unstacked_sample_batch_shape}" - ) - - assert b.sample_shape == sample_shape[m] - - yield SampleBlockMeta( - blocks=dict(member_blocks), - sample_shape=sample_shape, - sample_id=sample_id, - block_index=cons.block_index, - blocks_in_sample=cons.blocks_in_sample, - ) - - -def sample_block_generator( - blocks: Iterable[PerMember[BlockMeta]], - *, - origin: Sample, - pad_mode: PadMode | PerMember[PadMode], -) -> Iterable[SampleBlockWithOrigin]: - for member_blocks in blocks: - cons = _ConsolidatedMemberBlocks(member_blocks) - yield SampleBlockWithOrigin( - blocks={ - m: Block.from_sample_member( - origin.members[m], - block=member_blocks[m], - pad_mode=pad_mode.get(m, "symmetric") - if isinstance(pad_mode, collections.abc.Mapping) - else pad_mode, - ) - for m in origin.members - }, - sample_shape=origin.shape, - origin=origin, - stat=origin.stat, - sample_id=origin.id, - block_index=cons.block_index, - blocks_in_sample=cons.blocks_in_sample, - ) +# @dataclass(frozen=True) +# class SampleBlock(SampleBlockBase[Block]): +# """A block of a dataset sample""" + +# blocks: dict[MemberId, Block] +# """Individual tensor blocks comprising this sample block""" + +# stat: Stat +# """computed statistics""" + +# @property +# def members(self) -> PerMember[Tensor]: +# """the sample block's tensors""" +# return {m: b.data for m, b in self.blocks.items()} + +# @classmethod +# def from_meta( +# cls, meta: SampleBlockMeta, data: PerMember[Tensor], *, stat: Stat +# ) -> SampleBlock: +# return cls( +# sample_shape={ +# m: { +# a: data[m].tagged_shape[a] if s == -1 else s +# for a, s in member_shape.items() +# } +# for m, member_shape in meta.sample_shape.items() +# }, +# sample_id=meta.sample_id, +# blocks={ +# m: Block.from_meta(b, data=data[m]) for m, b in meta.blocks.items() +# }, +# stat=stat, +# block_loc=meta.block_loc, +# blocks_loc_shape=meta.blocks_loc_shape, +# ) + +# def get_meta(self) -> SampleBlockMeta: +# return SampleBlockMeta( +# sample_id=self.sample_id, +# blocks={m: b.get_meta() for m, b in self.blocks.items()}, +# sample_shape=self.sample_shape, +# block_loc=self.block_loc, +# blocks_loc_shape=self.blocks_loc_shape, +# ) + +# def as_sample(self) -> Sample: +# """Convert this sample block to a `Sample` with the shape of this block. + +# Note: +# If you want to convert one or more sample block to a sample with the shape of the original, whole sample, +# use `Sample.from_blocks()` instead. +# """ +# return Sample( +# members=dict(self.members), +# stat=dict(self.stat), +# id=self.sample_id, +# unstacked_dim_order=self.unstacked_dim_order, +# ) + +# @property +# def unstacked_dim_order(self) -> PerMember[Sequence[AxisId] | None]: +# return {m: b.unstacked_dim_order for m, b in self.blocks.items()} + +# @property +# def batch_multi_index(self) -> MultiIndex | None: +# """Return the batch multi-index of the sample block, if it has one. + +# Returns: +# The batch multi-index of the sample block, or `None` if the sample block does not have a multi-index batch dimension. +# """ +# for tensor in self.members.values(): +# idx = tensor.batch_multi_index +# if idx is not None: +# return idx + +# return None + + +# @dataclass(frozen=True) +# class SampleBlockWithOrigin(SampleBlock): +# """A `SampleBlock` with a reference (`origin`) to the whole `Sample`""" + +# origin: Sample +# """the sample this sample block was taken from""" + + +# def sample_block_generator( +# blocks: Iterable[PerMember[BlockMeta] | SampleBlockMeta], +# *, +# origin: Sample, +# pad_mode: PadMode | PerMember[PadMode], +# ) -> Iterable[SampleBlockWithOrigin]: +# for member_blocks in blocks: +# if isinstance(member_blocks, SampleBlockMeta): +# member_blocks = member_blocks.blocks + +# cons = _ConsolidatedMemberBlocks(member_blocks) +# yield SampleBlockWithOrigin( +# blocks={ +# m: Block.from_sample_member( +# origin.members[m], +# block=member_blocks[m], +# pad_mode=pad_mode.get(m, "symmetric") +# if isinstance(pad_mode, collections.abc.Mapping) +# else pad_mode, +# ) +# for m in origin.members +# }, +# sample_shape=origin.shape, +# origin=origin, +# stat=origin.stat, +# sample_id=origin.id, +# block_loc=cons.block_index, +# blocks_loc_shape=cons.blocks_in_sample, +# ) diff --git a/src/bioimageio/core/sample_meta.py b/src/bioimageio/core/sample_meta.py new file mode 100644 index 00000000..3e434be9 --- /dev/null +++ b/src/bioimageio/core/sample_meta.py @@ -0,0 +1,287 @@ +# from __future__ import annotations + +# from collections.abc import Sequence +# from dataclasses import dataclass +# from math import ceil, floor +# from types import MappingProxyType +# from typing import ( +# Any, +# Callable, +# Generic, +# TypeVar, +# ) + +# import numpy as np +# import pydantic +# from numpy.typing import NDArray +# from typing_extensions import Self, assert_never + +# from bioimageio.core._utils.multi_index import get_unstacked_dim_order +# from bioimageio.core._utils.type_guards import is_mapping + +# from ._common_annotations import PerMemberAnno +# from .axis import AxisId, PerAxis +# from .block_meta import ( +# BlockMeta, +# LinearAxisTransform, +# ) +# from .common import ( +# BlockLoc, +# BlockLocShape, +# Halo, +# MemberId, +# PerMember, +# SampleId, +# SliceInfo, +# ) + +# BlockT = TypeVar("BlockT", bound=BlockMeta) + + +# @pydantic.dataclasses.dataclass(frozen=True) +# class SampleBlockBase(Generic[BlockT]): +# """base class for `SampleBlockMeta` and `SampleBlock`""" + +# sample_shape: PerMemberAnno[PerAxis[int]] +# """the sample shape this block represents a part of""" + +# sample_id: SampleId +# """identifier for the sample within its dataset""" + +# blocks: PerMemberAnno[BlockT] +# """Individual tensor blocks comprising this sample block""" + +# block_loc: BlockLoc +# """the location of the block within the sample""" + +# blocks_loc_shape: BlockLocShape +# """the sample shape in number of blocks""" + +# @property +# def shape(self) -> PerMember[PerAxis[int]]: +# return MappingProxyType({mid: b.shape for mid, b in self.blocks.items()}) + +# @property +# def inner_shape(self) -> PerMember[PerAxis[int]]: +# return MappingProxyType({mid: b.inner_shape for mid, b in self.blocks.items()}) + +# @property +# def unstacked_sample_batch_shape(self) -> PerAxis[int] | None: +# """The shape of the unstacked batch axis, if the sample has a multi-index batch axis""" +# for b in self.blocks.values(): +# if b.unstacked_sample_batch_shape is not None: +# return b.unstacked_sample_batch_shape + +# return None + +# @property +# def unstacked_dim_order(self) -> PerMemberAnno[Sequence[AxisId] | None] | None: +# """The dim order of the unstacked tensors, if the sample has a multi-index batch axis""" +# for b in self.blocks.values(): +# if b.unstacked_dim_order is not None: +# return MappingProxyType( +# {mid: bb.unstacked_dim_order for mid, bb in self.blocks.items()} +# ) + +# return None + +# @property +# def unstacked_sample_shape(self) -> PerMemberAnno[PerAxis[int]]: +# """combined (stacked) sample shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" +# return MappingProxyType( +# {mid: b.unstacked_sample_shape for mid, b in self.blocks.items()} +# ) + + +# @dataclass +# class LinearSampleAxisTransform(LinearAxisTransform): +# member: MemberId + + +# @pydantic.dataclasses.dataclass(frozen=True) +# class SampleBlockMeta(SampleBlockBase[BlockMeta]): +# """Meta data of a dataset sample block""" + +# # @classmethod +# # def from_blocks(cls, meta: PerMember[BlockMeta]) -> Self: +# # sample_ids = { +# # b.sample_id for b in meta.values() if b.sample_id is not None +# # } or {"unknown"} +# # block_loc_shapes = {b.block_loc_shape for b in meta.values()} +# # if len(block_loc_shapes) != 1: +# # raise ValueError( +# # f"Mismatch for block_loc_shape (sample shape in blocks) across members: {block_loc_shapes}" +# # ) +# # block_locs = {b.block_loc for b in meta.values()} +# # if len(block_locs) != 1: +# # raise ValueError( +# # f"Mismatch for block_loc (block location within sample) across members: {block_locs}" +# # ) +# # return cls( +# # sample_shape={m: b.sample_shape for m, b in meta.items()}, +# # sample_id=next(iter(sample_ids)), +# # blocks=meta, +# # block_loc=next(iter(block_locs)), +# # blocks_loc_shape=next(iter(block_loc_shapes)), +# # ) + +# def get_transformed( +# self, new_axes: PerMember[PerAxis[LinearSampleAxisTransform | int]] +# ) -> Self: +# sample_shape = { +# m: { +# a: ( +# trf +# if isinstance(trf, int) +# else trf.compute(self.sample_shape[trf.member][trf.axis]) +# ) +# for a, trf in new_axes[m].items() +# } +# for m in new_axes +# } + +# def get_member_halo(m: MemberId, round: Callable[[float], int]): +# return { +# a: ( +# Halo(0, 0) +# if isinstance(trf, int) +# or trf.axis not in self.blocks[trf.member].halo +# else Halo( +# round(self.blocks[trf.member].halo[trf.axis].left * trf.scale), +# round(self.blocks[trf.member].halo[trf.axis].right * trf.scale), +# ) +# ) +# for a, trf in new_axes[m].items() +# } + +# halo: dict[MemberId, dict[AxisId, Halo]] = {} +# for m in new_axes: +# halo[m] = get_member_halo(m, floor) +# if halo[m] != get_member_halo(m, ceil): +# raise ValueError( +# f"failed to unambiguously scale halo {halo[m]} with {new_axes[m]}" +# + f" for {m}." +# ) + +# inner_slice = { +# m: { +# a: ( +# SliceInfo(0, trf) +# if isinstance(trf, int) +# else SliceInfo( +# trf.compute( +# self.blocks[trf.member].inner_slice[trf.axis].start +# ), +# trf.compute(self.blocks[trf.member].inner_slice[trf.axis].stop), +# ) +# ) +# for a, trf in new_axes[m].items() +# } +# for m in new_axes +# } +# unstacked_batch_slice = None +# for b in self.blocks.values(): +# if b.unstacked_batch_slice is None: +# continue +# elif unstacked_batch_slice is None: +# unstacked_batch_slice = b.unstacked_batch_slice +# else: +# assert unstacked_batch_slice == b.unstacked_batch_slice, ( +# "Incompatible unstacked batch slices across sample block members" +# ) + +# unstacked_dim_order: dict[MemberId, list[AxisId] | None] = {} +# for m, trfs in new_axes.items(): +# for trf in trfs.values(): +# if isinstance(trf, LinearSampleAxisTransform): +# unstacked_in_order = self.blocks[trf.member].unstacked_dim_order +# stacked_dims = list( +# self.blocks[trf.member].unstacked_sample_batch_shape or () +# ) +# unstacked_dim_order[m] = get_unstacked_dim_order( +# list(trfs), +# unstacked_reference_order=unstacked_in_order, +# stacked_dims=stacked_dims, +# ) +# break +# elif isinstance(trf, int): +# pass +# else: +# assert_never(trf) +# else: +# unstacked_dim_order[m] = None + +# return self.__class__( +# blocks={ +# m: BlockMeta( +# sample_shape=sample_shape[m], +# sample_id=self.sample_id, +# unstacked_sample_batch_shape=self.unstacked_sample_batch_shape, +# unstacked_dim_order=unstacked_dim_order[m], +# inner_slice=inner_slice[m], +# unstacked_batch_slice=unstacked_batch_slice, +# halo=halo[m], +# block_loc=self.block_loc, +# block_loc_shape=self.blocks_loc_shape, +# ) +# for m in new_axes +# }, +# sample_shape=sample_shape, +# sample_id=self.sample_id, +# block_loc=self.block_loc, +# blocks_loc_shape=self.blocks_loc_shape, +# ) + + +# class _ConsolidatedMemberBlocks: +# def __init__(self, blocks: PerMember[BlockMeta]): +# super().__init__() +# block_indices = {b.block_loc for b in blocks.values()} +# assert len(block_indices) == 1 +# self.block_index = block_indices.pop() +# blocks_in_samples = {b.block_loc_shape for b in blocks.values()} +# assert len(blocks_in_samples) == 1 +# self.blocks_in_sample = blocks_in_samples.pop() + + +# def sample_block_meta_generator( +# blocks: NDArray[Any], # NDArray[PerMember[BlockMeta]], +# *, +# sample_id: SampleId, +# ): +# sample_shape: dict[MemberId, PerAxis[int]] = {} +# # out = np.empty(blocks.shape, dtype=object).flatten() +# out: list[SampleBlockMeta] = [] + +# # member_blocks: PerMember[BlockMeta] +# for member_blocks in blocks.flat: # pyright: ignore[reportUnknownVariableType] +# assert is_mapping(member_blocks, MemberId, BlockMeta) +# cons = _ConsolidatedMemberBlocks(member_blocks) +# unstacked_batch_shape = None +# for m, b in member_blocks.items(): +# if b.unstacked_sample_batch_shape is not None: +# if unstacked_batch_shape is None: +# unstacked_batch_shape = b.unstacked_sample_batch_shape +# else: +# if unstacked_batch_shape != b.unstacked_sample_batch_shape: +# raise ValueError( +# f"Unstacked batch shape mismatch for member {m}: " +# + f"{unstacked_batch_shape} != {b.unstacked_sample_batch_shape}" +# ) + +# if m not in sample_shape: +# sample_shape[m] = b.sample_shape +# else: +# assert b.sample_shape == sample_shape[m] + +# out.append( +# SampleBlockMeta( +# blocks=dict(member_blocks), +# sample_shape=sample_shape, +# sample_id=sample_id, +# block_loc=cons.block_index, +# blocks_loc_shape=cons.blocks_in_sample, +# ) +# ) + +# return np.asarray(out, dtype=object).reshape(blocks.shape) diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index 32ec4026..e394d6b6 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -1,7 +1,7 @@ from __future__ import annotations import collections.abc -from collections.abc import Iterator, Mapping, Sequence +from collections.abc import Collection, Iterator, Mapping, Sequence from itertools import permutations from typing import ( TYPE_CHECKING, @@ -13,7 +13,10 @@ get_args, ) +import dask.array +import dask.array as da import numpy as np +import pandas as pd import xarray as xr from loguru import logger from numpy.typing import DTypeLike, NDArray @@ -23,51 +26,35 @@ from bioimageio.spec.model.v0_5 import SpaceUnit, TimeUnit from ._magic_tensor_ops import MagicTensorOpsMixin +from ._utils.color import get_default_channel_colors +from ._utils.type_guards import is_dict, is_mapping, is_sequence +from ._utils.type_hints import cast_and_assert from .axis import BATCH_AXIS_ID, AxisId, AxisInfo, AxisLike, PerAxis from .common import ( CropWhere, DTypeStr, + Halo, + HaloLike, + MemberId, PadMode, PadWhere, PadWidth, PadWidthLike, + PerMember, QuantileMethod, SliceInfo, + resolve_pad_mode, ) -from .utils._color import get_default_channel_colors -from .utils._multi_index import MultiIndex -from .utils._type_guards import is_dict, is_mapping, is_sequence -try: - import dask.array as da -except ImportError: - da = None +MultiIndex = pd.MultiIndex if TYPE_CHECKING: - import dask.array from numpy.typing import NDArray from xarray.core.types import DaCompatible Array: TypeAlias = "NDArray[Any] | dask.array.Array" -def _resolve_pad_mode(mode: PadMode): - constant_value = None - if isinstance(mode, str): - mode_name = mode - elif isinstance(mode, v0_5.ConstantPadding): - mode_name = mode.mode - constant_value = mode.value - elif isinstance( - mode, (v0_5.EdgePadding, v0_5.ReflectPadding, v0_5.SymmetricPadding) - ): - mode_name = mode.mode - else: - assert_never(mode) - - return mode_name, constant_value - - # TODO: complete docstrings # TODO: in the long run---with improved typing in xarray---we should probably replace `Tensor` with xr.DataArray class Tensor(MagicTensorOpsMixin): @@ -107,16 +94,21 @@ def __getitem__( | xr.DataArray, ) -> Self: if isinstance(key, SliceInfo): - key = slice(*key) + key = slice(key.start, key.stop) elif isinstance(key, collections.abc.Mapping): key = { - a: s if isinstance(s, int) else s if isinstance(s, slice) else slice(*s) + a: s + if isinstance(s, int) + else s + if isinstance(s, slice) + else slice(s.start, s.stop) for a, s in key.items() } elif isinstance(key, Tensor): key = key._data - return self.__class__.from_xarray(self._data[key]) + selected = cast_and_assert(xr.DataArray, self._data[key]) + return self.__class__.from_xarray(selected) def __setitem__( self, @@ -128,7 +120,10 @@ def __setitem__( elif isinstance(key, xr.DataArray): pass else: - key = {a: s if isinstance(s, slice) else slice(*s) for a, s in key.items()} + key = { + a: s if isinstance(s, slice) else slice(s.start, s.stop) + for a, s in key.items() + } if isinstance(value, Tensor): value = value._data @@ -153,12 +148,14 @@ def _binary_op( f: Callable[[Any, Any], Any], reflexive: bool = False, ) -> Self: - data: xr.DataArray = self._data._binary_op( # pyright: ignore[reportPrivateUsage] - (other._data if isinstance(other, Tensor) else other), - f, - reflexive, + data = cast_and_assert( + xr.DataArray, + self._data._binary_op( # pyright: ignore[reportPrivateUsage] + (other._data if isinstance(other, Tensor) else other), + f, + reflexive, + ), ) - assert isinstance(data, xr.DataArray) return self.__class__.from_xarray(data) def get_physical_scale(self) -> PerAxis[float]: @@ -333,7 +330,7 @@ def from_numpy( array: NDArray[Any], *, dims: AxisLike | Sequence[AxisLike] | None, - ) -> Tensor: + ) -> Self: return cls.from_array(array, dims=dims) @classmethod @@ -342,7 +339,7 @@ def from_array( array: Array, *, dims: AxisLike | Sequence[AxisLike] | None, - ) -> Tensor: + ) -> Self: """create a `Tensor` from a numpy array Args: @@ -371,7 +368,7 @@ def from_array( f"Array shape {original_shape} does not map to axes: {dims_formatted}" ) - return Tensor(successful_view, dims=tuple(a.id for a in axis_infos)) + return cls(successful_view, dims=tuple(a.id for a in axis_infos)) @property def data(self): @@ -422,8 +419,121 @@ def tagged_shape(self): return self.sizes def to_numpy(self) -> NDArray[Any]: - """Return the data of this tensor as a numpy array.""" - return self.data.to_numpy() # pyright: ignore[reportUnknownVariableType] + """Return the data of this tensor as a numpy array. + + Use `as_numpy()` to get a new `Tensor` instance with the data as a numpy array (wrapped in an xarray DataArray).""" + return self.data.to_numpy() + + def as_numpy(self) -> Self: + """Return a new Tensor instance with this tensor's data as a numpy array (wrapped in an xarray DataArray). + Use `to_numpy()` to get the raw numpy array.""" + return self.from_xarray(cast_and_assert(xr.DataArray, self.data.as_numpy())) + + def crop_halo(self, halo: PerAxis[HaloLike] | None) -> Self: + """helper to crop a halo from the tensor""" + if not halo: + return self + + return self[ + { + d: slice(h[0], self.tagged_shape[d] - h[1]) + if (h := Halo.create(halo.get(d, 0))) != Halo(0, 0) + else slice(None) + for d in self.dims + } + ] + + def chunk( + self, + chunk_sizes: PerAxis[int], + halo: PerAxis[HaloLike] | None = None, + pad_mode: PadMode | Literal["none"] = "none", + ) -> Self: + """Return a tensor with the same data but chunked according to `chunk_sizes`.""" + if da is None: + raise ImportError( + "Dask is not installed. Please install dask to use the chunking functionality." + ) + + if pad_mode != "none" and halo is not None: + data = self.pad(pad_width=halo, mode=pad_mode).data + else: + data = self.data + + chunked = cast_and_assert(xr.DataArray, data.chunk(chunk_sizes)) + if halo is not None: + for a, h in halo.items(): + if a not in self.dims: + raise ValueError(f"Axis {a} not in tensor dimensions {self.dims}") + + if isinstance(h, int): + if h < 0: + raise ValueError( + f"Halo size for axis {a} must be non-negative, got {h}" + ) + elif isinstance(h, tuple): + if len(h) != 2 or any(x < 0 for x in h): + raise ValueError( + f"Halo size for axis {a} must be a non-negative integer or a tuple of two non-negative integers, got {h}" + ) + + if len(set(h)) != 1 and pad_mode != "none": + raise ValueError( + f"Halo size for chunking with pad_mode {pad_mode} must be symmetric, got {h}" + ) + + else: + raise TypeError( + f"Halo size for axis {a} must be a non-negative integer or a tuple of two non-negative integers, got {h}" + ) + + depth = [ + h + if isinstance((h := halo.get(d, 0)), int) + else h[0] + if len(set(h)) == 1 + else {"-": h[0], "+": h[1]} + for d in self.dims + ] + + enlarged_chunk_shape = tuple( + chunk_sizes.get(a, self.tagged_shape[a]) + + (2 * h if isinstance(h, int) else h["-"] + h["+"]) + for a, h in zip(self.dims, depth) + ) + + def map_overlap_chunk(x: NDArray[Any]) -> NDArray[Any]: + return x + + overlapping = cast_and_assert( + da.Array, + da.map_overlap( + map_overlap_chunk, + chunked.data, + depth=depth, + boundary="none", # we explicitly pad above to avoid mapping `pad_mode` to `boundary` + dtype=chunked.data.dtype, + chunks=enlarged_chunk_shape, + trim=False, + allow_rechunk=False, + # meta=np.array((), dtype=chunked.data.dtype), # possibly not a numpy array + ), + ) + ret = self.__class__.from_array(overlapping, dims=self.dims) + if self.batch_multi_index is not None: + ret = ret.assign_batch_multi_index(self.batch_multi_index) + else: + ret = self.__class__.from_xarray(chunked) + + assert ret.name == self.name + assert ret.channel_names == self.channel_names + assert ret.channel_colors == self.channel_colors + assert ( + ret.batch_multi_index is None + and self.batch_multi_index is None + or (ret.batch_multi_index == self.batch_multi_index).all() + ) + return ret def argmax(self) -> Mapping[AxisId, int]: ret = self._data.argmax(...) @@ -434,12 +544,16 @@ def astype(self, dtype: DTypeStr, *, copy: bool = False): """Return tensor cast to `dtype` note: if dtype is already satisfied copy if `copy`""" - return self.__class__.from_xarray(self._data.astype(dtype, copy=copy)) + return self.__class__.from_xarray( + cast_and_assert(xr.DataArray, self._data.astype(dtype, copy=copy)) + ) def clip(self, min: float | None = None, max: float | None = None): """Return a tensor whose values are limited to [min, max]. At least one of max or min must be given.""" - return self.__class__.from_xarray(self._data.clip(min, max)) + return self.__class__.from_xarray( + cast_and_assert(xr.DataArray, self._data.clip(min, max)) + ) def crop_to( self, @@ -484,7 +598,9 @@ def crop_to( return self[slices] def expand_dims(self, dims: Sequence[AxisId] | PerAxis[int]) -> Self: - return self.__class__.from_xarray(self._data.expand_dims(dims=dims)) + return self.__class__.from_xarray( + cast_and_assert(xr.DataArray, self._data.expand_dims(dims=dims)) + ) def item( self, @@ -508,11 +624,17 @@ def pad( mode: PadMode = "symmetric", ) -> Self: pad_width = {a: PadWidth.create(p) for a, p in pad_width.items()} - mode_name, constant_value = _resolve_pad_mode(mode) + mode_name, constant_value = resolve_pad_mode(mode) padded_tensor = self.__class__.from_xarray( - self._data.pad( - pad_width=pad_width, mode=mode_name, constant_values=constant_value + cast_and_assert( + xr.DataArray, + self._data.pad( + pad_width=pad_width, + mode=mode_name, + constant_values=constant_value, + keep_attrs=True, + ), ) ) @@ -592,7 +714,9 @@ def quantile( (quantile_dim := AxisId("quantile")) != dim and quantile_dim not in set(dim) ) return self.__class__.from_xarray( - self._data.quantile(q, dim=dim, method=method) + cast_and_assert( + xr.DataArray, self._data.quantile(q, dim=dim, method=method) + ) ) def resize_to( @@ -630,11 +754,15 @@ def resize_to( return tensor def std(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: - return self.__class__.from_xarray(self._data.std(dim=dim)) + return self.__class__.from_xarray( + cast_and_assert(xr.DataArray, self._data.std(dim=dim)) + ) def sum(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: """Reduce this Tensor's data by applying sum along some dimension(s).""" - return self.__class__.from_xarray(self._data.sum(dim=dim)) + return self.__class__.from_xarray( + cast_and_assert(xr.DataArray, self._data.sum(dim=dim)) + ) @property def batch_multi_index(self) -> MultiIndex | None: @@ -661,17 +789,17 @@ def assign_batch_multi_index(self, multi_index: MultiIndex) -> Self: # # TODO: check if we really need to create/assign coordinates object # alternatively create new multi-index: self._data.set_xindex(multi_index.names) - data = self._data.drop_vars( # pyright: ignore[reportUnknownVariableType] - list(multi_index.names) + [BATCH_AXIS_ID], errors="ignore" + data = cast_and_assert( + xr.DataArray, + self._data.drop_vars( + list(multi_index.names) + [BATCH_AXIS_ID], errors="ignore" + ), ) - assert isinstance(data, xr.DataArray) - mindex_coords = xr.Coordinates.from_pandas_multiindex( # pyright: ignore[reportUnknownVariableType] - multi_index, BATCH_AXIS_ID + mindex_coords = cast_and_assert( + xr.Coordinates, + xr.Coordinates.from_pandas_multiindex(multi_index, BATCH_AXIS_ID), ) - assert isinstance(mindex_coords, xr.Coordinates) - data = data.assign_coords(mindex_coords) # pyright: ignore[reportUnknownVariableType] - assert isinstance(data, xr.DataArray) - + data = cast_and_assert(xr.DataArray, data.assign_coords(mindex_coords)) return self.__class__.from_xarray(data) def unstack_batch_multi_index( @@ -712,8 +840,7 @@ def unstack_batch_multi_index( assert_never(errors) old_dims = self.dims - array = self._data.unstack(BATCH_AXIS_ID) - assert isinstance(array, xr.DataArray) + array = cast_and_assert(xr.DataArray, self._data.unstack(BATCH_AXIS_ID)) added_dims = [ BATCH_AXIS_ID if d == "original_batch" else AxisId(d) for d in array.dims @@ -734,11 +861,11 @@ def unstack_batch_multi_index( new_dims = unstacked_dim_order if AxisId("original_batch") in array.dims: - array = array.rename({AxisId("original_batch"): BATCH_AXIS_ID}) - - array = array.transpose(*new_dims) + array = cast_and_assert( + xr.DataArray, array.rename({AxisId("original_batch"): BATCH_AXIS_ID}) + ) - assert isinstance(array, xr.DataArray) + array = cast_and_assert(xr.DataArray, array.transpose(*new_dims)) return self.__class__.from_xarray(array) def transpose( @@ -785,10 +912,10 @@ def transpose_and_return_unstacked_dim_order( "raise", "expand", "unstack", "unstack_or_expand" ] = "unstack_or_expand", ) -> tuple[Self, list[AxisId] | None]: - axes = [AxisInfo.create(a).id for a in axes] + axes = [a if isinstance(a, AxisId) else AxisId(a) for a in axes] array = self._data - unhandled_missing_dims = [a for a in axes if a not in array.dims] + unhandled_missing_dims = [a for a in axes if a not in self.dims] if unhandled_missing_dims and missing_dims == "raise": raise ValueError(f"Found missing dimensions {unhandled_missing_dims}.") @@ -811,18 +938,25 @@ def transpose_and_return_unstacked_dim_order( lets_unstack = False if lets_unstack: - array: xr.DataArray = array.unstack(BATCH_AXIS_ID) + array = cast_and_assert(xr.DataArray, array.unstack(BATCH_AXIS_ID)) if AxisId("original_batch") in array.dims: if BATCH_AXIS_ID in axes: - array = array.rename({AxisId("original_batch"): BATCH_AXIS_ID}) + array = cast_and_assert( + xr.DataArray, + array.rename({AxisId("original_batch"): BATCH_AXIS_ID}), + ) else: - array = array.squeeze(AxisId("original_batch")) + array = cast_and_assert( + xr.DataArray, array.squeeze(AxisId("original_batch")) + ) unhandled_missing_dims = [a for a in axes if a not in array.dims] if unhandled_missing_dims and missing_dims in ("expand", "unstack_or_expand"): - array = array.expand_dims(unhandled_missing_dims) + array = cast_and_assert( + xr.DataArray, array.expand_dims(unhandled_missing_dims) + ) unhandled_missing_dims = [] if unhandled_missing_dims: @@ -841,7 +975,7 @@ def transpose_and_return_unstacked_dim_order( if unhandled_extra_dims and extra_dims in ("squeeze", "squeeze_or_stack"): for d in list(unhandled_extra_dims): if array.sizes[d] == 1: - array = array.squeeze(d) + array = cast_and_assert(xr.DataArray, array.squeeze(d)) assert isinstance(array, xr.DataArray) unhandled_extra_dims.remove(d) elif extra_dims == "squeeze": @@ -858,10 +992,15 @@ def transpose_and_return_unstacked_dim_order( unstacked_dim_order = [AxisId(d) for d in array.dims] if BATCH_AXIS_ID in array.dims: - array = array.rename({BATCH_AXIS_ID: AxisId("original_batch")}) + array = cast_and_assert( + xr.DataArray, + array.rename({BATCH_AXIS_ID: AxisId("original_batch")}), + ) unhandled_extra_dims.insert(0, AxisId("original_batch")) - array = array.stack({BATCH_AXIS_ID: unhandled_extra_dims}) + array = cast_and_assert( + xr.DataArray, array.stack({BATCH_AXIS_ID: unhandled_extra_dims}) + ) unhandled_extra_dims = [] if unhandled_extra_dims: @@ -870,13 +1009,17 @@ def transpose_and_return_unstacked_dim_order( ) # transpose to the correct axis order - return self.__class__.from_xarray(array.transpose(*axes)), unstacked_dim_order + return self.__class__.from_xarray( + cast_and_assert(xr.DataArray, array.transpose(*axes)) + ), unstacked_dim_order def var(self, dim: AxisId | Sequence[AxisId] | None = None) -> Self: - return self.__class__.from_xarray(self._data.var(dim=dim)) + return self.__class__.from_xarray( + cast_and_assert(xr.DataArray, self._data.var(dim=dim)) + ) @classmethod - def _interprete_array_wo_known_axes(cls, array: Array) -> Tensor: + def _interprete_array_wo_known_axes(cls, array: Array) -> Self: ndim = array.ndim shape = [s if isinstance(s, int) else -1 for s in array.shape] @@ -935,10 +1078,133 @@ def _interprete_array_wo_known_axes(cls, array: Array) -> Tensor: raise ValueError(f"Could not guess an axis mapping for {shape}") dims = tuple(a.id for a in current_axes) - if da is not None and isinstance(array, da.Array): - return cls.from_xarray(xr.DataArray(array, dims=dims)) + return cls(array, dims=dims) + + +def restore_batch_multi_index( + inputs: Collection[Tensor | MultiIndex | None], outputs: PerMember[Tensor] +) -> dict[MemberId, Tensor]: + """Restore any batch multi-index found in the inputs to any output with a batch dimension of matching length.""" + not_processed_outputs = dict(outputs) + ret: dict[MemberId, Tensor] = {} + for m in tuple(outputs): + if BATCH_AXIS_ID not in outputs[m].dims: + ret[m] = not_processed_outputs.pop(m) + + input_has_multi_index = False + for ipt in inputs: + if isinstance(ipt, Tensor): + idx = ipt.batch_multi_index else: - return cls(array, dims=dims) + idx = ipt + + if not isinstance(idx, MultiIndex): + continue + + input_has_multi_index = True + for m in tuple(not_processed_outputs): + if len(idx) == not_processed_outputs[m].tagged_shape.get(BATCH_AXIS_ID): + ret[m] = not_processed_outputs.pop(m).assign_batch_multi_index(idx) + + if not_processed_outputs: + if input_has_multi_index: + logger.warning( + "Could not restore batch multi-index for outputs: {}", + list(not_processed_outputs), + ) + ret.update(not_processed_outputs) + + return ret + + +def get_unstacked_dim_order( + stacked_order: Sequence[AxisId], + *, + unstacked_reference_order: Sequence[AxisId] | None, + stacked_dims: Sequence[AxisId] | None, +) -> list[AxisId]: + if stacked_dims is not None: + # make sure that any "original_batch" axis id in the stacked_dims is replaced with the actual batch axis id + stacked_dims = [ + BATCH_AXIS_ID if d == "original_batch" else d for d in stacked_dims + ] + + if not stacked_dims and unstacked_reference_order is None: + return list(stacked_order) + if unstacked_reference_order is not None and list(stacked_order) == [ + d for d in unstacked_reference_order if d in stacked_order + ]: + # no differences in non-stacking axes, so we can preserve the unstacked axis order + return [ + d + for d in unstacked_reference_order + if d in stacked_order or (stacked_dims is not None and d in stacked_dims) + ] + elif stacked_dims is None: + # no stacked dims provided, so we can just return the stacked_out_order + return list(stacked_order) + elif BATCH_AXIS_ID not in stacked_order: + raise ValueError( + f"stacked_order={stacked_order} does not contain a batch axis, but stacked_dims={stacked_dims} are provided" + ) + else: + # unstack batch axis in its place + ret = list(stacked_order) + b_idx = ret.index(AxisId("batch")) + ret[b_idx : b_idx + 1] = ( + [ + d for d in unstacked_reference_order or () if d in stacked_dims + ] # keep unstacked axis order from reference + + [ + d + for d in stacked_dims + if unstacked_reference_order is None + or d not in unstacked_reference_order + ] # add any remaining stacked dims that are not in the unstacked reference order + ) + return ret + + +# def transpose_multiple_tensors( +# tensors: PerMember[Tensor], +# axes: PerMember[Sequence[AxisId]], +# *, +# extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "raise", +# missing_dims: Literal["raise", "expand", "unstack", "unstack_or_expand"] = "raise", +# ) -> PerMember[Tensor]: +# """Transpose multiple tensors and make +# """ +# if any(unknown := [m not in tensors for m in axes]): +# raise ValueError(f"Axes specified for unknown members: {unknown}") + +# transposed_tensors: dict[MemberId, Tensor] = {} +# for m, t in tensors.items(): +# if m in axes: +# transposed_tensors[m] = ( +# t.transpose( +# axes=axes[m], +# extra_dims=extra_dims, +# missing_dims=missing_dims, +# ) +# ) +# else: +# transposed_tensors[m] = t + +# if ( +# len( +# batch_lengths := { +# t.sizes[AxisId("batch")] +# for t in transposed_tensors.values() +# if AxisId("batch") in t.dims +# } +# ) +# > 1 +# ): +# raise ValueError( +# f"Transposed sample members have incompatible batch lengths: {batch_lengths}." +# ) + +# return transposed_tensors def _add_singletons(arr: NDArray[Any], axis_infos: Sequence[AxisInfo]): diff --git a/src/bioimageio/core/utils/__init__.py b/src/bioimageio/core/utils/__init__.py deleted file mode 100644 index 62120894..00000000 --- a/src/bioimageio/core/utils/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from ._compare import compare as compare diff --git a/src/bioimageio/core/utils/_multi_index.py b/src/bioimageio/core/utils/_multi_index.py deleted file mode 100644 index 4deec4a4..00000000 --- a/src/bioimageio/core/utils/_multi_index.py +++ /dev/null @@ -1,194 +0,0 @@ -from __future__ import annotations - -from collections.abc import Sequence -from typing import TYPE_CHECKING, Any - -import dask.array as da -import numpy as np -import pandas as pd -from numpy.typing import NDArray -from typing_extensions import assert_never - -from ..axis import BATCH_AXIS_ID, AxisId, PerAxis -from ..common import PerMember -from ._type_guards import is_ndarray - -if TYPE_CHECKING: - from ..tensor import Tensor - -MultiIndex = pd.MultiIndex - - -def create_tensor_with_multiindex( - unstacked_shape: PerAxis[int], - transpose_to: Sequence[AxisId], - *, - fill_value: float | None = None, - dtype: np.dtype | None = None, - unstacked_data: NDArray[Any] | da.Array | None = None, - stacked_data: NDArray[Any] | da.Array | None = None, -) -> Tensor: - """Create a tensor potentially with a MultiIndex in the batch dimension. - - The tensor is created with the given unstacked shape and then transposed to `transpose_to`. - If `transpose_to` is missing some of the unstacked_shape dimensions a MultiIndex will be created. - Otherwise the tensor will be created without a batch MultiIndex. - - Args: - unstacked_shape: The shape of the tensor before stacking. - transpose_to: The order of the axes after transposing. - fill_value: The value to fill the tensor with. If None, the tensor will be empty (ignored if unstacked_data or stacked_data are not None). - dtype: The data type of the tensor. If None, the data type will be inferred from unstacked_data or stacked_data (must be specified if neither unstacked_data nor stacked_data is provided). - unstacked_data: The data to use for the unstacked tensor. If provided, fill_value will be ignored. May not be provided together with stacked_data. - stacked_data: The data to use for the stacked tensor. If provided, fill_value will be ignored. May not be provided together with unstacked_data. - - Returns: - A tensor (with a MultiIndex in the batch dimension). - """ - from ..tensor import Tensor - - if unstacked_data is not None and stacked_data is not None: - raise ValueError("unstacked_data and stacked_data may not be provided together") - - if unstacked_data is None: - if stacked_data is not None: - dtype = stacked_data.dtype # pyright: ignore[reportUnknownVariableType] - elif dtype is None: - raise ValueError( - "dtype must be specified if neither unstacked_data nor stacked_data is provided" - ) - - if fill_value is None or stacked_data is not None: - unstacked_data = da.zeros( # pyright: ignore[reportUnknownVariableType] - shape=tuple(unstacked_shape.values()), dtype=dtype - ) - else: - unstacked_data = da.full( # pyright: ignore[reportUnknownVariableType] - shape=tuple(unstacked_shape.values()), - fill_value=fill_value, - dtype=dtype, - ) - - assert isinstance(unstacked_data, da.Array) - elif dtype is not None: - unstacked_data = unstacked_data.astype(dtype) # pyright: ignore[reportUnknownVariableType] - assert isinstance(unstacked_data, (np.ndarray, da.Array)) - else: - dtype = unstacked_data.dtype # pyright: ignore[reportUnknownVariableType] - assert isinstance(dtype, np.dtype) - - if isinstance(unstacked_data, da.Array): - tensor = Tensor.from_array(unstacked_data, dims=tuple(unstacked_shape.keys())) - elif isinstance(unstacked_data, np.ndarray): - assert is_ndarray(unstacked_data) - tensor = Tensor.from_numpy(unstacked_data, dims=tuple(unstacked_shape.keys())) - else: - assert_never(unstacked_data) - - stacked_tensor = tensor.transpose( - transpose_to, extra_dims="stack", missing_dims="expand" - ) - if stacked_data is not None: - if stacked_data.shape != stacked_tensor.data.shape: - raise ValueError( - f"stacked_data shape {stacked_data.shape} does not match the stacked tensor shape {stacked_tensor.data.shape}" - ) - stacked_tensor.data.data = stacked_data - - return stacked_tensor - - -def restore_batch_multi_index( - inputs: PerMember[Tensor | None], outputs: PerMember[Tensor | None] -) -> PerMember[Tensor | None]: - """Restore the first batch multi-index found in the inputs to all outputs with batch dimension.""" - for tensor in inputs.values(): - if tensor is None: - continue - - idx = tensor.data.indexes.get(AxisId("batch")) # pyright: ignore[reportUnknownVariableType] - if isinstance(idx, MultiIndex): - outputs = { - k: v.assign_batch_multi_index(idx) - if v is not None and AxisId("batch") in v.dims - else v - for k, v in outputs.items() - } - break - - return outputs - - -def get_unstacked_dim_order( - stacked_order: Sequence[AxisId], - *, - unstacked_reference_order: Sequence[AxisId] | None, - stacked_dims: Sequence[AxisId] | None, -) -> list[AxisId]: - if not stacked_dims and unstacked_reference_order is None: - return list(stacked_order) - if unstacked_reference_order is not None and list(stacked_order) == [ - d for d in unstacked_reference_order if d in stacked_order - ]: - # no differences in non-stacking axes, so we can preserve the unstacked axis order - return [ - d - for d in unstacked_reference_order - if d in stacked_order or (stacked_dims is not None and d in stacked_dims) - ] - elif stacked_dims is None: - # no stacked dims provided, so we can just return the stacked_out_order - return list(stacked_order) - elif BATCH_AXIS_ID not in stacked_order: - raise ValueError( - f"stacked_order={stacked_order} does not contain a batch axis, but stacked_dims={stacked_dims} are provided" - ) - else: - # unstack batch axis in its place - ret = list(stacked_order) - b_idx = ret.index(AxisId("batch")) - ret[b_idx : b_idx + 1] = ( - [ - d for d in unstacked_reference_order or () if d in stacked_dims - ] # keep unstacked axis order from reference - + [ - d - for d in stacked_dims - if unstacked_reference_order is None - or d not in unstacked_reference_order - ] # add any remaining stacked dims that are not in the unstacked reference order - ) - return ret - - -def get_unstacked_shape( - stacked_shape: PerAxis[int], - *, - unstacked_dim_order: Sequence[AxisId] | None, - unstacked_batch_shape: PerAxis[int] | None, -) -> PerAxis[int]: - """Combine a shape with an optional unstacked batch shape and apply an optional unstacked dim order to get the final unstacked shape. - - Returns `stacked_shape` if both `unstacked_dim_order` and `unstacked_batch_shape` are None. - """ - unstacked_shape_unordered = {**stacked_shape, **(unstacked_batch_shape or {})} - - if unstacked_dim_order is not None: - assert all(a in unstacked_dim_order for a in unstacked_shape_unordered), ( - f"unstacked_dim_order={unstacked_dim_order} does not contain all axes of the sample shape {stacked_shape} and unstacked sample batch shape {unstacked_batch_shape}" - ) - order = unstacked_dim_order - else: - # if no explicit unstacked dim order is provided we insert the unstacked batch axes at the position of the batch axis in the sample shape - order = list(stacked_shape) - if unstacked_batch_shape: - assert BATCH_AXIS_ID in stacked_shape, ( - f"unstacked sample batch shape {unstacked_batch_shape} is provided but the sample shape {stacked_shape} does not contain a batch axis" - ) - b_idx = order.index(BATCH_AXIS_ID) - order[b_idx : b_idx + 1] = list(unstacked_batch_shape) - - assert all(a in order for a in unstacked_shape_unordered), ( - f"unstacked_dim_order={unstacked_dim_order} does not contain all axes of the sample shape {stacked_shape} and unstacked sample batch shape {unstacked_batch_shape}" - ) - return {a: unstacked_shape_unordered[a] for a in order} diff --git a/src/bioimageio/core/utils/_resolve_per_member_key.py b/src/bioimageio/core/utils/_resolve_per_member_key.py deleted file mode 100644 index 463551c1..00000000 --- a/src/bioimageio/core/utils/_resolve_per_member_key.py +++ /dev/null @@ -1,54 +0,0 @@ -from __future__ import annotations - -from collections.abc import Collection -from typing import TypeVar - -from typing_extensions import assert_never - -from ..axis import AxisId, PerAxis -from ..common import MemberId, PerMember - -T_pm = TypeVar("T_pm") -T_pa = TypeVar("T_pa") - - -def resolve_per_member_var( - var: PerMember[T_pm | PerAxis[T_pa]] | PerAxis[T_pa], - /, - *, - valid_member_index_types: tuple[type[T_pm], ...], - valid_axis_index_types: tuple[type[T_pa], ...], - members: Collection[MemberId], -): - """Resolve a key that may be a per-member mapping or a per-axis mapping into a per-member mapping. - - If the key is a per-axis mapping, it will be expanded into a per-member mapping - with the same per-axis mapping for each member in `members`. - """ - - per_member_key: PerMember[T_pm | PerAxis[T_pa]] = {} - per_axis_key: PerAxis[T_pa] = {} - for k, v in var.items(): - if isinstance(k, MemberId): - if not isinstance(v, valid_member_index_types): - raise TypeError( - f"Per-member indexing only supports value types {valid_member_index_types}, but got {v} of type {type(v)})" - ) - per_member_key[k] = v - elif isinstance(k, AxisId): - if not isinstance(v, valid_axis_index_types): - raise TypeError( - f"Per-axis indexing only supports value types {valid_axis_index_types}, but got {v} of type {type(v)})" - ) - per_axis_key[k] = v - else: - assert_never(k) - - if per_member_key and per_axis_key: - raise ValueError( - f"Indexing a sample with both MemberId and AxisId keys is not supported, got keys: {list(per_member_key)} and {list(per_axis_key)}" - ) - elif per_axis_key: - per_member_key = {m: per_axis_key for m in members} - - return per_member_key diff --git a/tests/test_digest_spec.py b/tests/test_digest_spec.py index 8c7b8bb5..b5645e96 100644 --- a/tests/test_digest_spec.py +++ b/tests/test_digest_spec.py @@ -1,42 +1,28 @@ -from bioimageio.spec import load_description -from bioimageio.spec.model import v0_5 +from bioimageio.spec import load_model_description -# TODO: don't just test with unet2d_nuclei_broad_model def test_get_block_transform(unet2d_nuclei_broad_model: str): from bioimageio.core.axis import AxisId + from bioimageio.core.block import LinearAxisTransform from bioimageio.core.common import MemberId - from bioimageio.core.digest_spec import ( - get_block_transform, - get_io_sample_block_metas, - ) + from bioimageio.core.digest_spec import get_block_transform - model = load_description(unet2d_nuclei_broad_model) - assert isinstance(model, v0_5.ModelDescr) + model = load_model_description( + unet2d_nuclei_broad_model, perform_io_checks=False, format_version="latest" + ) block_transform = get_block_transform(model) - ns = { - (ipt.id, a.id): 1 - for ipt in model.inputs - for a in ipt.axes - if isinstance(a.size, v0_5.ParameterizedSize) - } - - input_sample_shape = { - MemberId("raw"): { - AxisId("batch"): 3, + assert block_transform == { + MemberId("probability"): { + AxisId("batch"): LinearAxisTransform( + member=MemberId("raw"), axis=AxisId("batch"), scale=1, offset=0 + ), AxisId("channel"): 1, - AxisId("x"): 4000, - AxisId("y"): 3000, + AxisId("x"): LinearAxisTransform( + member=MemberId("raw"), axis=AxisId("x"), scale=1, offset=0 + ), + AxisId("y"): LinearAxisTransform( + member=MemberId("raw"), axis=AxisId("y"), scale=1, offset=0 + ), } } - - _, blocks = get_io_sample_block_metas( - model, - input_sample_shape=input_sample_shape, - ns=ns, - ) - - for ipt_block, out_block in blocks: - trf_block = ipt_block.get_transformed(block_transform) - assert out_block == trf_block diff --git a/tests/test_io.py b/tests/test_io.py index bfb385c1..dbfe10b7 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -57,8 +57,8 @@ def test_save_sample(tmp_path: Path, suffix: str): from bioimageio.core.io import load_tensor, save_sample members = { - MemberId("raw"): np.arange(6, dtype=np.uint8).reshape(2, 3), - MemberId("mask"): np.ones((2, 3), dtype=np.uint8), + MemberId("raw"): np.arange(30, dtype=np.uint8).reshape(5, 6), + MemberId("mask"): np.ones((5, 6), dtype=np.uint8), } sample = Sample( members={ diff --git a/tests/test_model_adapter.py b/tests/test_model_adapter.py new file mode 100644 index 00000000..812d0b06 --- /dev/null +++ b/tests/test_model_adapter.py @@ -0,0 +1,38 @@ +from unittest.mock import Mock + +import pytest +from dask.delayed import ( + Delayed, + delayed, # pyright: ignore[reportUnknownVariableType] +) + +from bioimageio.core._model_adapter import LocalModelAdapter, ModelAdapter +from bioimageio.core._utils.type_hints import cast_and_assert + + +def test_lazy_forward_after_unload(): + adapter = Mock(spec=LocalModelAdapter) + adapter._loaded = True + adapter._model_queue = Mock() + adapter._model_queue.get.return_value = ("cpu", object()) + adapter._forward_impl.return_value = [] + output = cast_and_assert( + Delayed, + delayed( + LocalModelAdapter._forward_arrays # pyright: ignore[reportPrivateUsage] + )(adapter, []), + ) + + assert output.compute() == [] + adapter._model_queue.put.assert_called_once_with( + adapter._model_queue.get.return_value + ) + + ModelAdapter.unload(adapter) + adapter._model_queue.get.reset_mock() + # Fail immediately rather than hang if the unloaded adapter accesses the queue. + adapter._model_queue.get.side_effect = AssertionError("Accessed unloaded queue") + with pytest.raises(RuntimeError, match="Compute lazy outputs inside"): + output.compute() + + adapter._model_queue.get.assert_not_called() diff --git a/tests/test_multi_index.py b/tests/test_multi_index.py index bac43f82..950a5709 100644 --- a/tests/test_multi_index.py +++ b/tests/test_multi_index.py @@ -8,7 +8,7 @@ from numpy.typing import NDArray from bioimageio.core import AxisId -from bioimageio.core.utils._multi_index import MultiIndex, create_tensor_with_multiindex +from bioimageio.core.tensor import MultiIndex UNSTACKED_SHAPE = {AxisId("batch"): 2, AxisId("z"): 3, AxisId("x"): 4} TRANSPOSE_TO = (AxisId("batch"), AxisId("x")) diff --git a/tests/test_ops_cellpose.py b/tests/test_ops_cellpose.py index a5193988..4ca91ca2 100644 --- a/tests/test_ops_cellpose.py +++ b/tests/test_ops_cellpose.py @@ -1,6 +1,6 @@ from pathlib import Path -from bioimageio.core.utils._type_guards import is_list, is_ndarray +from bioimageio.core._utils.type_guards import is_list, is_ndarray from tests.test_bioimageio_collection import expensive_test diff --git a/tests/test_prediction.py b/tests/test_prediction.py index 60abf176..a9bed59b 100644 --- a/tests/test_prediction.py +++ b/tests/test_prediction.py @@ -154,7 +154,6 @@ def test_predict_z_as_batch(unet2d_nuclei_broad_model: str): MemberId("raw"): Tensor(data, dims=["batch", "channel", "z", "y", "x"]) }, stat=input_sample.stat, - unstacked_dim_order=input_sample.unstacked_dim_order, ) input_sample = transpose_sample_for_model(input_sample, model) out = predict(model=unet2d_nuclei_broad_model, inputs=input_sample) @@ -174,7 +173,7 @@ def test_predict_z_as_batch(unet2d_nuclei_broad_model: str): def test_transpose_sample_for_model_stacks_extra_z_to_batch( unet2d_nuclei_broad_model: str, ): - from bioimageio.core.utils._multi_index import MultiIndex + from bioimageio.core._utils.multi_index import MultiIndex model = load_model( unet2d_nuclei_broad_model, perform_io_checks=False, format_version="latest" diff --git a/tests/test_prediction_pipeline.py b/tests/test_prediction_pipeline.py index 2f460795..85edb581 100644 --- a/tests/test_prediction_pipeline.py +++ b/tests/test_prediction_pipeline.py @@ -16,7 +16,6 @@ def _alter_sample(sample: Sample, offset: float) -> Sample: id=f"{sample.id}_altered", members={m: t + offset for m, t in sample.members.items()}, stat=sample.stat, - unstacked_dim_order=sample.unstacked_dim_order, ) @@ -46,7 +45,7 @@ def _test_prediction_pipeline( multiple_outputs = list( executor.map( partial( - pp.predict_sample_without_blocking, + pp.predict_without_blocking, skip_input_padding=True, skip_output_cropping=True, ), @@ -121,7 +120,7 @@ def test_blockwise_prediction_with_stacked_dims(unet2d_nuclei_broad_model: Path) ) ], ) - outputs = pp.predict_sample_with_blocking(inputs).unstack_batch_multi_index()[ + outputs = pp.predict_with_blocking(inputs).unstack_batch_multi_index()[ {AxisId("extra_dim"): 0} ] diff --git a/tests/test_prediction_pipeline_device_management.py b/tests/test_prediction_pipeline_device_management.py index ad84e97b..061f513a 100644 --- a/tests/test_prediction_pipeline_device_management.py +++ b/tests/test_prediction_pipeline_device_management.py @@ -1,7 +1,7 @@ from pathlib import Path import pytest -from numpy.testing import assert_array_almost_equal +from xarray.testing import assert_allclose # pyright: ignore[reportUnknownVariableType] from bioimageio.core.common import SupportedWeightsFormat from bioimageio.spec.model.v0_4 import ModelDescr as ModelDescr04 @@ -33,55 +33,53 @@ def _test_device_management( inputs = get_test_input_sample(bio_model) with pred_pipe as pp: - outputs = pp.predict_sample_without_blocking( + outputs = pp.predict_without_blocking( inputs, skip_input_padding=True, skip_output_cropping=True ) - expected_outputs = get_test_output_sample(bio_model) + expected_outputs = get_test_output_sample(bio_model) - assert len(outputs.shape) == len(expected_outputs.shape) - for m in expected_outputs.members: - out = outputs.members[m].data - assert out is not None - exp = expected_outputs.members[m].data - assert_array_almost_equal(out, exp, decimal=2) + assert len(outputs.shape) == len(expected_outputs.shape) + for m in expected_outputs.members: + out = outputs.members[m].as_numpy().data + exp = expected_outputs.members[m].as_numpy().data + assert_allclose(out, exp, rtol=1e-2, atol=1e-2) # repeat inference with context manager to test load/predict/unload/load/predict with pred_pipe as pp: - outputs = pp.predict_sample_without_blocking( + outputs = pp.predict_without_blocking( inputs, skip_input_padding=True, skip_output_cropping=True ) - assert len(outputs.shape) == len(expected_outputs.shape) - for m in expected_outputs.members: - out = outputs.members[m].data - assert out is not None - exp = expected_outputs.members[m].data - assert_array_almost_equal(out, exp, decimal=2) + assert len(outputs.shape) == len(expected_outputs.shape) + for m in expected_outputs.members: + out = outputs.members[m].as_numpy().data + exp = expected_outputs.members[m].as_numpy().data + assert_allclose(out, exp, rtol=1e-2, atol=1e-2) -@pytest.mark.parametrize("device", ["cpu", "cuda"]) +@pytest.mark.parametrize("device", ["cpu", "cuda", "mps"]) def test_device_management_torch(any_torch_model: Path, device: str): _test_device_management(any_torch_model, "pytorch_state_dict", device=device) -@pytest.mark.parametrize("device", ["cpu", "cuda"]) +@pytest.mark.parametrize("device", ["cpu", "cuda", "mps"]) def test_device_management_torchscript(any_torchscript_model: Path, device: str): _test_device_management(any_torchscript_model, "torchscript", device=device) -@pytest.mark.parametrize("device", ["cpu", "cuda"]) +@pytest.mark.parametrize("device", ["cpu", "cuda", "mps"]) def test_device_management_onnx(any_onnx_model: Path, device: str): _test_device_management(any_onnx_model, "onnx", device=device) -@pytest.mark.parametrize("device", ["cpu", "cuda"]) +@pytest.mark.parametrize("device", ["cpu", "cuda", "mps"]) def test_device_management_tensorflow(any_tensorflow_model: Path, device: str): _test_device_management( any_tensorflow_model, "tensorflow_saved_model_bundle", device=device ) -@pytest.mark.parametrize("device", ["cpu", "cuda"]) +@pytest.mark.parametrize("device", ["cpu", "cuda", "mps"]) def test_device_management_keras(any_keras_model: Path, device: str): _test_device_management(any_keras_model, "keras_hdf5", device=device) diff --git a/tests/test_remote_backends/test_gradio.py b/tests/test_remote_backends/test_gradio.py index 1372ba6e..b00279f6 100644 --- a/tests/test_remote_backends/test_gradio.py +++ b/tests/test_remote_backends/test_gradio.py @@ -27,6 +27,27 @@ from bioimageio.spec import AnyModelDescr +def test_gradio_sample_serializer(unet2d_diff_output_shape: str): + model = load_model( + unet2d_diff_output_shape, format_version="latest", perform_io_checks=False + ) + sample = get_test_input_sample(model) + serialized = GradioSampleSerializer.serialize_sample(sample) + deserialized = GradioSampleSerializer.deserialize_sample(serialized) + assert deserialized.members == sample.members + assert deserialized == sample + + # blockwise + serialized_blockwise = GradioSampleSerializer.serialize_sample_blockwise( + sample, model=model, blocksize_parameter=10 + ) + deserialized_blockwise = GradioSampleSerializer.deserialize_sample( + serialized_blockwise + ) + assert deserialized_blockwise.members == sample.members + assert deserialized_blockwise == sample + + def _is_port_in_use(port: int) -> bool: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: return s.connect_ex(("localhost", port)) == 0 @@ -319,11 +340,9 @@ def test_gradio_pipeline( ( model_source, ( - executor.submit(pp.predict_sample_with_blocking, sample_in) + executor.submit(pp.predict_with_blocking, sample_in) if with_blocking - else executor.submit( - pp.predict_sample_without_blocking, sample_in - ) + else executor.submit(pp.predict_without_blocking, sample_in) ), expected_out_loop, ) diff --git a/tests/test_tensor.py b/tests/test_tensor.py index 04c1b962..76e1a087 100644 --- a/tests/test_tensor.py +++ b/tests/test_tensor.py @@ -67,7 +67,7 @@ def test_shape_attributes(): def test_transpose_stack_extra_dims_to_batch_creates_multi_index(): - from bioimageio.core.utils._multi_index import MultiIndex + from bioimageio.core._utils.multi_index import MultiIndex tensor = Tensor.from_numpy( np.arange(2 * 3 * 4).reshape(2, 3, 4), From f03e99bf4c15229f41647d8c5d8d0acf60e364af Mon Sep 17 00:00:00 2001 From: fynnbe Date: Fri, 9 Oct 2026 22:51:01 +0200 Subject: [PATCH 31/85] fix halo treatment at sample edge --- .github/workflows/build.yaml | 2 +- pyproject.toml | 3 +- src/bioimageio/core/_prediction_pipeline.py | 53 ++++---- src/bioimageio/core/axis.py | 17 +++ src/bioimageio/core/block.py | 142 +++++++++++++------- src/bioimageio/core/digest_spec.py | 3 +- src/bioimageio/core/prediction.py | 76 +++-------- src/bioimageio/core/tensor.py | 15 ++- tests/test_multi_index.py | 134 +++++++++--------- tests/test_prediction.py | 2 +- tests/test_prediction_pipeline.py | 4 +- 11 files changed, 241 insertions(+), 210 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index e0be2de4..c78533e4 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -173,7 +173,7 @@ jobs: - uses: actions/download-artifact@v4 with: name: coverage.xml - - uses: orgoro/coverage@v3.2 + - uses: orgoro/coverage@v3.3 with: coverageFile: coverage.xml token: ${{ secrets.GITHUB_TOKEN }} diff --git a/pyproject.toml b/pyproject.toml index c8ae6a23..606cedcc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,6 +7,7 @@ readme = "README.md" dynamic = ["version"] dependencies = [ "bioimageio.spec ==0.5.14.3", + "dask[array]", "imagecodecs", "imageio>=2.10", "loguru", @@ -60,7 +61,6 @@ bioio = [ "bioio-imageio", "bioio-ome-tiff", "bioio-ome-zarr", - "dask[array]", ] # clearscale = ["clearscale", "dask[array]", "zarr"] # TODO: add clearscale extra deps once clearscale is available on pypi dev = [ @@ -71,7 +71,6 @@ dev = [ "bioio-ome-zarr", "cellpose<4.2", # for model testing "crick", - "dask[array]", "httpx", "jax", "jupyter", diff --git a/src/bioimageio/core/_prediction_pipeline.py b/src/bioimageio/core/_prediction_pipeline.py index 9067af5d..223ddf44 100644 --- a/src/bioimageio/core/_prediction_pipeline.py +++ b/src/bioimageio/core/_prediction_pipeline.py @@ -18,7 +18,7 @@ from ._op_base import BlockwiseOperator, SamplewiseOperator from .axis import AxisId, PerAxis from .backends import create_model_adapter -from .block import apply_blockwise, get_chunk_shape +from .block import apply_blockwise from .common import ( PROCESSING_CHUNK_MIN_BYTES, BlocksizeParameter, @@ -227,13 +227,16 @@ def _predict_with_blocking_impl( for a in ipt.axes if isinstance(a.size, v0_5.ParameterizedSize) } - input_block_shape = self._model_descr.get_tensor_sizes( + block_shape = self._model_descr.get_tensor_sizes( ns, batch_size or self._default_batch_size, sample.shape ).inputs - + block_shape_factors = self._model_descr.get_tensor_sizes( + 0, 1, sample.shape + ).inputs return self._predict_with_fixed_blocking_impl( sample, - input_block_shape=input_block_shape, + block_shape=block_shape, + block_shape_factors=block_shape_factors, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, skip_input_padding=skip_input_padding, @@ -244,6 +247,7 @@ def predict_with_fixed_blocking( self, sample: Sample, input_block_shape: PerMember[PerAxis[int]], + input_block_shape_factors: PerMember[PerAxis[int]] | None = None, skip_preprocessing: bool = False, skip_postprocessing: bool = False, skip_input_padding: bool = False, @@ -266,7 +270,8 @@ def predict_with_fixed_blocking( for pp in self._get_preceding_prediction_pipelines_for_sample(sample): sample = pp._predict_with_fixed_blocking_impl( sample, - input_block_shape=input_block_shape, + block_shape=input_block_shape, + block_shape_factors=input_block_shape_factors or input_block_shape, skip_preprocessing=False, skip_postprocessing=False, skip_input_padding=False, @@ -275,7 +280,8 @@ def predict_with_fixed_blocking( return self._predict_with_fixed_blocking_impl( sample, - input_block_shape=input_block_shape, + block_shape=input_block_shape, + block_shape_factors=input_block_shape_factors or input_block_shape, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, skip_input_padding=skip_input_padding, @@ -286,8 +292,9 @@ def predict_with_fixed_blocking( def _predict_with_fixed_blocking_impl( self, sample: Sample, - input_block_shape: PerMember[PerAxis[int]], + block_shape: PerMember[PerAxis[int]], *, + block_shape_factors: PerMember[PerAxis[int]], skip_preprocessing: bool, skip_postprocessing: bool, skip_input_padding: bool, @@ -512,13 +519,14 @@ def _predict_without_blocking_impl( for m, t in output.members.items() } - return output.unstack_batch_multi_index(errors="ignore") + return output def _predict_with_fixed_blocking_impl( self, sample: Sample, - input_block_shape: PerMember[PerAxis[int]], + block_shape: PerMember[PerAxis[int]], *, + block_shape_factors: PerMember[PerAxis[int]], skip_preprocessing: bool, skip_postprocessing: bool, skip_input_padding: bool, @@ -548,19 +556,11 @@ def _predict_with_fixed_blocking_impl( if not skip_preprocessing: self.apply_preprocessing(sample) - chunk_shape = get_chunk_shape( - sample.members, input_block_shape, PROCESSING_CHUNK_MIN_BYTES - ) - logger.info( - "split sample shape {} into chunks of shape {}.", - {k: dict(v) for k, v in sample.shape.items()}, - {k: dict(v) for k, v in chunk_shape.items()}, - ) - output_members = self._forward_with_fixed_blocking( sample.members, - input_chunk_shape=chunk_shape, - input_block_shape=input_block_shape, + chunk_shape=PROCESSING_CHUNK_MIN_BYTES, + block_shape=block_shape, + block_shape_factors=block_shape_factors, ) sample = Sample(id=sample.id, members=output_members, stat=sample.stat) @@ -584,8 +584,9 @@ def _predict_with_fixed_blocking_impl( def _forward_with_fixed_blocking( self, sample_members: PerMember[Tensor], - input_chunk_shape: PerMember[PerAxis[int]] | int, - input_block_shape: PerMember[PerAxis[int]], + chunk_shape: PerMember[PerAxis[int]] | int, + block_shape: PerMember[PerAxis[int]], + block_shape_factors: PerMember[PerAxis[int]], ) -> dict[MemberId, Tensor]: def get_output_shape( input_shape: PerMember[PerAxis[int]], @@ -603,13 +604,13 @@ def get_output_shape( return apply_blockwise( self._adapter.forward, sample_members, - chunk_shape=input_chunk_shape, - block_shape=input_block_shape, - pad_mode=self._pad_mode, + chunk_shape=chunk_shape, + block_shape=block_shape, + pad_mode="none", input_halo=self._default_input_halo, output_halo=self._default_output_halo, get_output_shape=get_output_shape, - block_shape_factors=input_block_shape, + block_shape_factors=block_shape_factors, ) def load(self): diff --git a/src/bioimageio/core/axis.py b/src/bioimageio/core/axis.py index 7f248e2c..e32d1ee2 100644 --- a/src/bioimageio/core/axis.py +++ b/src/bioimageio/core/axis.py @@ -67,6 +67,23 @@ class AxisDescrLike(Protocol): AxisLike = Union[_AxisLikePlain, AxisDescrLike, v0_5.AnyAxis, "Axis", "AxisInfo"] +def axis_id_from_axis_like(a: AxisLike) -> AxisId: + if isinstance( + a, + (Axis, v0_5.AxisBase), + ): + return a.id + elif isinstance(a, AxisDescrLike): + a = a.id + + if isinstance(a, AxisId): + return a + elif isinstance(a, str): + return AxisId(a) + else: + assert_never(a) + + @dataclass class Axis: id: AxisId diff --git a/src/bioimageio/core/block.py b/src/bioimageio/core/block.py index be404e62..9ff43487 100644 --- a/src/bioimageio/core/block.py +++ b/src/bioimageio/core/block.py @@ -1,7 +1,7 @@ from __future__ import annotations import itertools -from collections.abc import Collection, Mapping, Sequence +from collections.abc import Mapping, Sequence from copy import deepcopy from dataclasses import dataclass from functools import partial @@ -12,12 +12,14 @@ Any, Callable, Literal, + cast, ) import dask.array as da import numpy as np from loguru import logger from numpy.typing import NDArray +from typing_extensions import assert_never from bioimageio.core._utils.type_hints import NestedList, cast_and_assert from bioimageio.spec._internal.utils import SLOTS @@ -36,7 +38,7 @@ SliceInfo, SliceInfoWithOverlap, ) -from .tensor import Tensor +from .tensor import Array, Tensor @dataclass @@ -191,8 +193,8 @@ def local_slice(self) -> PerAxis[SliceInfo]: ) @property - def dims(self) -> Collection[AxisId]: - return set(self.inner_shape) + def dims(self) -> tuple[AxisId, ...]: + return tuple(self.inner_shape) @property def tagged_shape(self) -> PerAxis[int]: @@ -213,6 +215,13 @@ def inner_slice_wo_overlap(self) -> PerAxis[SliceInfo]: for a, s in self.inner_slice.items() } + @property + def inner_shape_wo_overlap(self) -> PerAxis[int]: + """axis lengths of the inner region (without halo and without overlap)""" + return MappingProxyType( + {a: s.stop - s.start for a, s in self.inner_slice_wo_overlap.items()} + ) + def __post_init__(self): assert all(a in self.full_shape for a in self.inner_slice), ( "block has axes not present in its full tensor shape" @@ -574,14 +583,13 @@ def apply_blockwise( inputs: PerMember[Tensor], *, block_shape: PerMember[PerAxis[int]], - pad_mode: PadMode | PerMember[PadMode], + pad_mode: PadMode | PerMember[PadMode] | Literal["none"], input_halo: PerMember[PerAxis[Halo]], output_halo: PerMember[PerAxis[Halo]], get_output_shape: Callable[[PerMember[PerAxis[int]]], PerMember[PerAxis[int]]], block_shape_factors: PerMember[PerAxis[int]] | None = MappingProxyType({}), chunk_shape: PerMember[PerAxis[int]] | int = PROCESSING_CHUNK_MIN_BYTES, - blockwise_processing_mode: Literal["parallel", "sequential"] = "parallel", - chunkwise_processing_mode: Literal["parallel", "sequential"] = "parallel", + blockwise_processing_mode: Literal["parallel", "sequential", "auto"] = "auto", ) -> dict[MemberId, Tensor]: """Apply `func` to `inputs` blockwise. @@ -591,7 +599,7 @@ def apply_blockwise( inputs: the input tensors to process block_shape: the shape of the blocks to be processed by func pad_mode: the padding mode (optionally per input member) to use when the halo extends beyond the tensor boundary - if "none" and `output_halo` is not empty, the output will be smaller by the `output_halo` + if "none" and `output_halo` is not empty, the output will be smaller by `output_halo` input_halo: the halo around each input block (e.g. to compenstate for `output_halo` such that cropped output blocks can be stitched together) Within the sample halo is realized as overlap between neighboring blocks, outside the sample halo is realized as padding. output_halo: the halo to be cropped from each output block @@ -608,9 +616,19 @@ def apply_blockwise( Chunks are larger than blocks and help to reduce the number of tasks in the dask graph for 'parallel' processing. Must be larger than `block_shape`. If an integer is given, it is interpreted as the minimum number of bytes of any input chunk tensor. - chunkwise_processing_mode: (WIP) 'parallel' (via dask graph) or 'sequential' - blockwise_processing_mode: (WIP) 'parallel' (via dask graph; nested inside chunk task if chunksiwe_processing_mode='parallel') or 'sequential' + blockwise_processing_mode: (WIP) processing mode wihin a (parallelly processed) dask chunk. + Options are + - 'parallel': nest blockwise tasks inside the chunk task) + - 'sequential': (not yet implemented) full chunk is a single task + - 'auto': automatically choose between 'parallel' and 'sequential' based on the number of blocks per chunk and the total number of chunks to avoid large task graphs. """ + if blockwise_processing_mode == "auto": + blockwise_processing_mode = "parallel" # TODO: implement auto mode + logger.info("using blockwise_processing_mode='parallel' for ") + elif blockwise_processing_mode == "sequential": + raise NotImplementedError( + "blockwise_processing_mode='sequential' is not yet implemented" + ) if isinstance(chunk_shape, int): chunk_shape = get_chunk_shape( @@ -630,7 +648,7 @@ def apply_blockwise( _apply_blockwise_impl, func, block_shape=block_shape, - pad_mode=pad_mode, + pad_mode="none", input_halo=input_halo, output_halo=output_halo, processing_mode=blockwise_processing_mode, @@ -642,10 +660,10 @@ def apply_blockwise( process_chunk, inputs, block_shape=chunk_shape, - pad_mode="none", + pad_mode=pad_mode, input_halo=input_halo, output_halo=output_halo, - processing_mode=chunkwise_processing_mode, + processing_mode="parallel", block_shape_factors=block_shape_factors, get_output_shape=get_output_shape, ) @@ -698,6 +716,7 @@ def _apply_blockwise_impl( func, input_blocks, output_halo=output_halo, + keep_output_halo_at_sample_edge=pad_mode == "none", processing_mode=processing_mode, get_output_shape=get_output_shape, ) @@ -708,6 +727,7 @@ def apply_to_block_array( input_blocks: PerMember[BlockArray], *, output_halo: PerMember[PerAxis[HaloLike]], + keep_output_halo_at_sample_edge: bool, processing_mode: Literal["parallel", "sequential"] = "parallel", get_output_shape: Callable[[PerMember[PerAxis[int]]], PerMember[PerAxis[int]]], ) -> dict[MemberId, Tensor]: @@ -716,8 +736,8 @@ def apply_to_block_array( func, input_blocks, output_halo=output_halo, - processing_mode=processing_mode, get_output_shape=get_output_shape, + keep_output_halo_at_sample_edge=keep_output_halo_at_sample_edge, ) def select_arrays( @@ -732,9 +752,16 @@ def select_arrays( while isinstance(first_block, list): first_block = first_block[0] + if processing_mode == "parallel": + block_impl = cast(Callable[[NestedList[Array]], da.Array], da.block) + elif processing_mode == "sequential": + block_impl = cast(Callable[[NestedList[Array]], NDArray[Any]], np.block) + else: + assert_never(processing_mode) + return { output_id: Tensor( - da.block( + block_impl( select_arrays(nested_outputs, output_id), ), dims=first_block[output_id].dims, @@ -748,8 +775,8 @@ def apply_to_blocks( blocks: PerMember[BlockArray], *, output_halo: PerMember[PerAxis[HaloLike]], - processing_mode: Literal["parallel", "sequential"] = "parallel", get_output_shape: Callable[[PerMember[PerAxis[int]]], PerMember[PerAxis[int]]], + keep_output_halo_at_sample_edge: bool, ) -> NestedList[dict[MemberId, Tensor]]: if not all(b.size > 0 for b in blocks.values()): raise ValueError( @@ -761,13 +788,14 @@ def apply_to_blocks( raise ValueError(f"Input block grids do not match: {block_ndims}") block_grid_shape = next(iter(block_ndims.values())) + first_block = cast(Sequence[Block], next(iter(blocks.values())).flat)[0] + block_grid_dims = first_block.dims output_halo = { m: {a: Halo.create(h) for a, h in h_per_a.items()} for m, h_per_a in output_halo.items() } - # TODO: differentiate between parallel and sequential processing modes def func_blocks( axis: int = 0, loc: tuple[int, ...] = () ) -> NestedList[dict[MemberId, Tensor]]: @@ -792,44 +820,60 @@ def func_blocks( raise NotImplementedError( "Output block overlap to the right is not implemented." ) - if left_input_overlap: - outer_input_shape_wo_overlap = { - m: { - a: s.stop - - s.start - + sum(cast_and_assert(Block, b[loc]).halo.get(a, (0,))) - for a, s in cast_and_assert( - Block, b[loc] - ).inner_slice_wo_overlap.items() - } - for m, b in blocks.items() - } - inner_output_shape_no_overlap_left = { - m: { - a: None if ss < 1 else ss - sum(output_halo.get(m, {}).get(a, (0,))) - for a, ss in s.items() - } - for m, s in get_output_shape(outer_input_shape_wo_overlap).items() - } - else: - inner_output_shape_no_overlap_left = None + valid_input_shape = { + m: b[loc].inner_shape_wo_overlap for m, b in blocks.items() + } + valid_output_shape = get_output_shape(valid_input_shape) + input_members = {m: b[loc].outer_tensor for m, b in blocks.items()} ret: dict[MemberId, Tensor] = {} - for m, t in func({m: b[loc].outer_tensor for m, b in blocks.items()}).items(): + for m, t in func(input_members).items(): if t is None: continue # crop halo to keep inner output - t = t.crop_halo(output_halo.get(m)) - - # crop inner output overlap - if inner_output_shape_no_overlap_left is not None: - output_slice_no_overlap_left = { - a: SliceInfo(start=t.tagged_shape[a] - s, stop=t.tagged_shape[a]) - for a, s in inner_output_shape_no_overlap_left[m].items() - if s is not None - } - t = t[output_slice_no_overlap_left] + keep_halo_here: dict[AxisId, Halo] = {} + if (crop_halo_here := output_halo.get(m)) is not None: + crop_halo_here = dict(crop_halo_here) + if keep_output_halo_at_sample_edge: + assert len(block_grid_dims) == len(block_grid_shape) == len(loc) + for a, grid_size, loc_idx in zip( + block_grid_dims, block_grid_shape, loc + ): + if a not in crop_halo_here: + continue + + if loc_idx == 0 and loc_idx == grid_size - 1: + # single block is first and last block + keep_halo_here[a] = crop_halo_here[a] + crop_halo_here[a] = Halo(0, 0) + elif loc_idx == 0: + # first block + keep_halo_here[a] = Halo(crop_halo_here[a].left, 0) + crop_halo_here[a] = Halo(0, crop_halo_here[a].right) + elif loc_idx == grid_size - 1: + # last block + keep_halo_here[a] = Halo(0, crop_halo_here[a].right) + crop_halo_here[a] = Halo(crop_halo_here[a].left, 0) + + t = t.crop_halo(crop_halo_here) + + overlap_halo = { + a: Halo(overlap, 0) + for a, vs in valid_output_shape[m].items() + if ( + overlap := t.tagged_shape[a] + - sum( + keep_halo_here.get(a, Halo(0, 0)) + ) # consider the edge halo we explicitly keep to be valid, so we do not error consider it overlap + - vs + ) + != 0 + } + assert set(overlap_halo) == left_input_overlap, ( + f"need to crop overlap {overlap_halo} but expected overlap in {left_input_overlap}" + ) + t = t.crop_halo(overlap_halo) ret[m] = t diff --git a/src/bioimageio/core/digest_spec.py b/src/bioimageio/core/digest_spec.py index af6d42f2..90d322e0 100644 --- a/src/bioimageio/core/digest_spec.py +++ b/src/bioimageio/core/digest_spec.py @@ -10,6 +10,7 @@ from typing import ( Any, Callable, + Final, Literal, Union, ) @@ -626,7 +627,7 @@ def create_sample_for_model( return Sample(members=members, stat={} if stat is None else stat, id=sample_id) -load_sample_for_model = create_sample_for_model +load_sample_for_model: Final = create_sample_for_model def transpose_sample_for_model( diff --git a/src/bioimageio/core/prediction.py b/src/bioimageio/core/prediction.py index ed9ce95f..dbdaef9c 100644 --- a/src/bioimageio/core/prediction.py +++ b/src/bioimageio/core/prediction.py @@ -31,7 +31,7 @@ def predict( input_block_shape: Mapping[MemberId, Mapping[AxisId, int]] | None = None, skip_preprocessing: bool = False, skip_postprocessing: bool = False, - save_output_path: Path | str | None = None, + output_path: Path | str | None = None, devices: Sequence[str] | None = None, ) -> Sample: """Run prediction for a single set of input(s) with a bioimage.io model @@ -41,7 +41,7 @@ def predict( May be given as RDF source, model description or prediction pipeline. inputs: the input sample or the named input(s) for this model as a dictionary sample_id: the sample id. - The **sample_id** is used to format **save_output_path** + The **sample_id** is used to format **output_path** and to distinguish sample specific log messages. blocksize_parameter: (optional) Tile the input into blocks parametrized by **blocksize_parameter** according to any parametrized axis sizes defined @@ -53,7 +53,7 @@ def predict( run prediction independent of the exact block shape. skip_preprocessing: Flag to skip the model's preprocessing. skip_postprocessing: Flag to skip the model's postprocessing. - save_output_path: A path with to save the output to. M + output_path: A path with to save the output to. Must contain: - `{output_id}` (or `{member_id}`) if the model has multiple output tensors May contain: @@ -77,13 +77,13 @@ def predict( with pp: model = pp.model_descr - if save_output_path is not None and ( - "{output_id}" not in str(save_output_path) - and "{member_id}" not in str(save_output_path) + if output_path is not None and ( + "{output_id}" not in str(output_path) + and "{member_id}" not in str(output_path) and len(model.outputs) > 1 ): raise ValueError( - f"Missing `{{output_id}}` in save_output_path={save_output_path} to " + f"Missing `{{output_id}}` in output_path={output_path} to " + "distinguish model outputs " + str([get_member_id(d) for d in model.outputs]) ) @@ -103,18 +103,18 @@ def predict( input_block_shape, ) - _n_intermediates, output = pp.predict_with_fixed_blocking( + output = pp.predict_with_fixed_blocking( sample, input_block_shape=input_block_shape, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, ) elif blocksize_parameter is not None: - _n_intermediates, output = pp._predict_with_blocking_impl( + output = pp.predict_with_blocking( sample, + ns=blocksize_parameter, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, - ns=blocksize_parameter, ) else: output = pp.predict_without_blocking( @@ -123,50 +123,10 @@ def predict( skip_postprocessing=skip_postprocessing, ) - if save_output_path: - output_ref: list[Sample | None] = [ - None - ] # to store final output when saving blockwise - if isinstance(output, Sample): - save_output = output - return_output = output - else: - if pp.has_samplewise_postprocessing: - final_output = None - for out in output: - final_output = out - - assert final_output is not None - save_output = final_output.sample - return_output = final_output.sample - else: - iterable_output = output - - def blockwise_output(): - for out in iterable_output: - output_ref[0] = out.sample - yield out.last_block - - save_output = blockwise_output() - return_output = None - - save_sample(save_output_path, save_output) - if return_output is None: - return_output = output_ref[0] - assert return_output is not None, ( - "saving output blockwise failed to produce a final output sample" - ) - else: - if isinstance(output, Sample): - return_output = output - else: - return_output = None - for out in output: - return_output = out.sample - - assert return_output is not None + if output_path: + save_sample(output_path, output) - return return_output + return output def predict_many( @@ -182,7 +142,7 @@ def predict_many( | None = None, skip_preprocessing: bool = False, skip_postprocessing: bool = False, - save_output_path: Path | str | None = None, + output_path: Path | str | None = None, ) -> Iterator[Sample]: """Run prediction for a multiple sets of inputs with a bioimage.io model @@ -197,14 +157,14 @@ def predict_many( blocksize according to any parametrized axis sizes defined in the model RDF. skip_preprocessing: Flag to skip the model's preprocessing. skip_postprocessing: Flag to skip the model's postprocessing. - save_output_path: A path to save the output to. + output_path: A path to save the output to. Must contain: - `{sample_id}` to differentiate predicted samples - `{output_id}` (or `{member_id}`) if the model has multiple outputs """ - if save_output_path is not None and "{sample_id}" not in str(save_output_path): + if output_path is not None and "{sample_id}" not in str(output_path): raise ValueError( - f"Missing `{{sample_id}}` in save_output_path={save_output_path}" + f"Missing `{{sample_id}}` in output_path={output_path}" + " to differentiate predicted samples." ) @@ -243,5 +203,5 @@ def predict_many( blocksize_parameter=blocksize_parameter, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, - save_output_path=save_output_path, + output_path=output_path, ) diff --git a/src/bioimageio/core/tensor.py b/src/bioimageio/core/tensor.py index e394d6b6..4dd96eea 100644 --- a/src/bioimageio/core/tensor.py +++ b/src/bioimageio/core/tensor.py @@ -29,7 +29,14 @@ from ._utils.color import get_default_channel_colors from ._utils.type_guards import is_dict, is_mapping, is_sequence from ._utils.type_hints import cast_and_assert -from .axis import BATCH_AXIS_ID, AxisId, AxisInfo, AxisLike, PerAxis +from .axis import ( + BATCH_AXIS_ID, + AxisId, + AxisInfo, + AxisLike, + PerAxis, + axis_id_from_axis_like, +) from .common import ( CropWhere, DTypeStr, @@ -69,9 +76,7 @@ def __init__( dims: Sequence[AxisId | AxisLike], ) -> None: super().__init__() - axes = tuple( - a if isinstance(a, AxisId) else AxisInfo.create(a).id for a in dims - ) + axes = tuple(axis_id_from_axis_like(a) for a in dims) if isinstance(array, xr.DataArray): self._data: xr.DataArray = array.transpose(*axes) assert isinstance(self._data, xr.DataArray) @@ -912,7 +917,7 @@ def transpose_and_return_unstacked_dim_order( "raise", "expand", "unstack", "unstack_or_expand" ] = "unstack_or_expand", ) -> tuple[Self, list[AxisId] | None]: - axes = [a if isinstance(a, AxisId) else AxisId(a) for a in axes] + axes = [axis_id_from_axis_like(a) for a in axes] array = self._data unhandled_missing_dims = [a for a in axes if a not in self.dims] diff --git a/tests/test_multi_index.py b/tests/test_multi_index.py index 950a5709..5ce45a72 100644 --- a/tests/test_multi_index.py +++ b/tests/test_multi_index.py @@ -8,14 +8,11 @@ from numpy.typing import NDArray from bioimageio.core import AxisId -from bioimageio.core.tensor import MultiIndex +from bioimageio.core.tensor import MultiIndex, Tensor UNSTACKED_SHAPE = {AxisId("batch"): 2, AxisId("z"): 3, AxisId("x"): 4} TRANSPOSE_TO = (AxisId("batch"), AxisId("x")) -ARRAY_BACKENDS = pytest.mark.parametrize( - "backend", - ["numpy", "dask"], -) +ARRAY_BACKENDS = pytest.mark.parametrize("backend", ["numpy", "dask"]) ArrayBackend = Literal["numpy", "dask"] @@ -28,75 +25,84 @@ def _array(backend: ArrayBackend, data: NDArray[Any]) -> NDArray[Any] | da.Array @ARRAY_BACKENDS -def test_create_tensor_with_multiindex_from_unstacked_data(backend: ArrayBackend): - unstacked_data = _array( - backend, - np.arange(2 * 3 * 4).reshape(2, 3, 4), - ) +def test_stack_and_unstack_batch_multi_index(backend: ArrayBackend): + data = np.arange(24).reshape(2, 3, 4) + tensor = Tensor.from_array(_array(backend, data), dims=tuple(UNSTACKED_SHAPE)) - tensor = create_tensor_with_multiindex( - UNSTACKED_SHAPE, - TRANSPOSE_TO, - unstacked_data=unstacked_data, + stacked, dim_order = tensor.transpose_and_return_unstacked_dim_order( + TRANSPOSE_TO, extra_dims="stack" ) - assert tensor.dims == TRANSPOSE_TO - assert tensor.shape == (6, 4) - np.testing.assert_array_equal( - tensor.to_numpy(), np.asarray(unstacked_data).reshape(6, 4) - ) - batch_index = tensor.data.indexes[AxisId("batch")] # pyright: ignore[reportUnknownVariableType] - assert isinstance(batch_index, MultiIndex) - assert tuple(str(level) for level in batch_index.names) == ("original_batch", "z") + assert stacked.dims == TRANSPOSE_TO + assert stacked.shape == (6, 4) + assert dim_order == list(UNSTACKED_SHAPE) + assert isinstance(stacked.data.data, da.Array if backend == "dask" else np.ndarray) + np.testing.assert_array_equal(stacked.to_numpy(), data.reshape(6, 4)) + index = stacked.batch_multi_index + assert isinstance(index, MultiIndex) + assert tuple(index.names) == ("original_batch", "z") + assert list(index) == [(b, z) for b in range(2) for z in range(3)] + + for restored in ( + stacked.unstack_batch_multi_index(unstacked_dim_order=dim_order), + stacked.transpose(tuple(UNSTACKED_SHAPE), missing_dims="unstack"), + ): + assert restored.dims == tensor.dims + assert restored.batch_multi_index is None + np.testing.assert_array_equal(restored.to_numpy(), data) -def test_create_tensor_with_multiindex_fills_unstacked_tensor(): - tensor = create_tensor_with_multiindex( - UNSTACKED_SHAPE, - TRANSPOSE_TO, - fill_value=7, - dtype=np.dtype(np.int16), - ) +@ARRAY_BACKENDS +def test_assign_batch_multi_index_to_stacked_data(backend: ArrayBackend): + data = np.arange(24, dtype=np.float32).reshape(6, 4) + tensor = Tensor.from_array(_array(backend, data), dims=TRANSPOSE_TO) + index = MultiIndex.from_product([range(2), range(3)], names=["original_batch", "z"]) + + indexed = tensor.assign_batch_multi_index(index) - assert tensor.dtype == "int16" - np.testing.assert_array_equal(tensor.to_numpy(), np.full((6, 4), 7, dtype=np.int16)) + assert tensor.batch_multi_index is None + assert indexed.batch_multi_index is not None + assert indexed.batch_multi_index.equals(index) + assert indexed.dtype == "float32" + np.testing.assert_array_equal(indexed.to_numpy(), data) + restored = indexed.unstack_batch_multi_index() + assert restored.dims == tuple(UNSTACKED_SHAPE) + np.testing.assert_array_equal(restored.to_numpy(), data.reshape(2, 3, 4)) @ARRAY_BACKENDS -def test_create_tensor_with_multiindex_from_stacked_data(backend: ArrayBackend): - stacked_data = _array( - backend, - np.arange(6 * 4, dtype=np.float32).reshape(6, 4), - ) +def test_stack_without_original_batch(backend: ArrayBackend): + data = np.arange(12).reshape(3, 4) + tensor = Tensor.from_array(_array(backend, data), dims=("z", "x")) + + stacked = tensor.transpose(TRANSPOSE_TO, extra_dims="stack") + + assert stacked.shape == (3, 4) + index = stacked.batch_multi_index + assert isinstance(index, MultiIndex) + assert tuple(index.names) == ("original_batch", "z") + assert list(index) == [(0, z) for z in range(3)] + unstacked = stacked.unstack_batch_multi_index() + assert unstacked.dims == tuple(UNSTACKED_SHAPE) + assert unstacked.shape == (1, 3, 4) + np.testing.assert_array_equal(unstacked.to_numpy(), data[np.newaxis]) + restored = stacked.transpose(tensor.dims, missing_dims="unstack") + assert restored.dims == tensor.dims + np.testing.assert_array_equal(restored.to_numpy(), data) - tensor = create_tensor_with_multiindex( - UNSTACKED_SHAPE, - TRANSPOSE_TO, - stacked_data=stacked_data, - ) - assert tensor.dtype == "float32" - np.testing.assert_array_equal(tensor.to_numpy(), stacked_data) +@ARRAY_BACKENDS +def test_multi_index_rejects_incompatible_axes(backend: ArrayBackend): + tensor = Tensor.from_array(_array(backend, np.zeros((3, 4))), dims=("z", "x")) + index = MultiIndex.from_product([range(3)], names=["z"]) + with pytest.raises(ValueError, match="without a 'batch' axis"): + _ = tensor.assign_batch_multi_index(index) -@ARRAY_BACKENDS -def test_create_tensor_with_multiindex_rejects_incompatible_inputs( - backend: ArrayBackend, -): - with pytest.raises(ValueError, match="may not be provided together"): - _ = create_tensor_with_multiindex( - UNSTACKED_SHAPE, - TRANSPOSE_TO, - unstacked_data=_array(backend, np.zeros((2, 3, 4))), - stacked_data=_array(backend, np.zeros((6, 4))), - ) - - with pytest.raises(ValueError, match="dtype must be specified"): - _ = create_tensor_with_multiindex(UNSTACKED_SHAPE, TRANSPOSE_TO) - - with pytest.raises(ValueError, match="does not match"): - _ = create_tensor_with_multiindex( - UNSTACKED_SHAPE, - TRANSPOSE_TO, - stacked_data=_array(backend, np.zeros((2, 3, 4))), - ) + with pytest.raises(ValueError, match="Cannot stack extra dimensions to batch"): + _ = tensor.transpose(("x",), extra_dims="stack") + + with pytest.raises(ValueError, match="does not have a MultiIndex"): + _ = Tensor.from_array( + _array(backend, np.zeros((6, 4))), dims=TRANSPOSE_TO + ).transpose(tuple(UNSTACKED_SHAPE), missing_dims="unstack") diff --git a/tests/test_prediction.py b/tests/test_prediction.py index a9bed59b..24e68647 100644 --- a/tests/test_prediction.py +++ b/tests/test_prediction.py @@ -127,7 +127,7 @@ def test_predict_save_output(prep: Prep, tmp_path: Path): out = predict( model=prep.prediction_pipeline, inputs=prep.input_sample, - save_output_path=save_path, + output_path=save_path, ) _assert_equal_samples(out, prep.output_sample) assert save_path.parent.exists() diff --git a/tests/test_prediction_pipeline.py b/tests/test_prediction_pipeline.py index 85edb581..4a872708 100644 --- a/tests/test_prediction_pipeline.py +++ b/tests/test_prediction_pipeline.py @@ -120,9 +120,7 @@ def test_blockwise_prediction_with_stacked_dims(unet2d_nuclei_broad_model: Path) ) ], ) - outputs = pp.predict_with_blocking(inputs).unstack_batch_multi_index()[ - {AxisId("extra_dim"): 0} - ] + outputs = pp.predict_with_blocking(inputs)[{AxisId("extra_dim"): 0}] expected_outputs = get_test_output_sample(bio_model) assert len(outputs.shape) == len(expected_outputs.shape) From a558b3ef3414feb52263ffd836c003fa1d29bf59 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 13 Oct 2026 10:39:22 +0200 Subject: [PATCH 32/85] WIP improve blockwise processing --- pyproject.toml | 4 +- src/bioimageio/core/__init__.py | 1 - src/bioimageio/core/_prediction_pipeline.py | 39 +- src/bioimageio/core/_resource_tests.py | 1 - src/bioimageio/core/_settings.py | 3 + src/bioimageio/core/_utils/chunking.py | 159 ------ src/bioimageio/core/_utils/multi_index.py | 113 ---- src/bioimageio/core/block.py | 318 ++++------- src/bioimageio/core/block_meta.py | 541 ------------------- src/bioimageio/core/cli.py | 7 +- src/bioimageio/core/common.py | 4 - src/bioimageio/core/io.py | 10 +- src/bioimageio/core/sample.py | 566 -------------------- src/bioimageio/core/sample_meta.py | 287 ---------- tests/test_multi_index.py | 10 +- tests/test_prediction.py | 3 +- tests/test_stat_calculators.py | 7 +- tests/test_stat_measures.py | 4 +- 18 files changed, 157 insertions(+), 1920 deletions(-) delete mode 100644 src/bioimageio/core/_utils/chunking.py delete mode 100644 src/bioimageio/core/_utils/multi_index.py delete mode 100644 src/bioimageio/core/block_meta.py delete mode 100644 src/bioimageio/core/sample_meta.py diff --git a/pyproject.toml b/pyproject.toml index 606cedcc..e638cb81 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -62,7 +62,7 @@ bioio = [ "bioio-ome-tiff", "bioio-ome-zarr", ] -# clearscale = ["clearscale", "dask[array]", "zarr"] # TODO: add clearscale extra deps once clearscale is available on pypi +# clearscale = ["clearscale", "zarr"] # TODO: add clearscale extra deps once clearscale is available on pypi dev = [ "bioio", "bioio-bioformats", @@ -83,7 +83,7 @@ dev = [ "onnxscript", "packaging>=17.0", "pre-commit", - "pyright==1.1.411", + "pyright==1.1.414", "pytest", "pytest-cov", "python-dotenv", diff --git a/src/bioimageio/core/__init__.py b/src/bioimageio/core/__init__.py index d70788d5..0ee2491c 100644 --- a/src/bioimageio/core/__init__.py +++ b/src/bioimageio/core/__init__.py @@ -39,7 +39,6 @@ from . import axis as axis from . import backends as backends -from . import block_meta as block_meta from . import cli as cli from . import commands as commands from . import common as common diff --git a/src/bioimageio/core/_prediction_pipeline.py b/src/bioimageio/core/_prediction_pipeline.py index 223ddf44..91bd6613 100644 --- a/src/bioimageio/core/_prediction_pipeline.py +++ b/src/bioimageio/core/_prediction_pipeline.py @@ -16,11 +16,11 @@ from ._model_adapter import ModelAdapter from ._op_base import BlockwiseOperator, SamplewiseOperator -from .axis import AxisId, PerAxis +from ._settings import settings +from .axis import AxisId, AxisSize, PerAxis from .backends import create_model_adapter from .block import apply_blockwise from .common import ( - PROCESSING_CHUNK_MIN_BYTES, BlocksizeParameter, Halo, MemberId, @@ -227,16 +227,23 @@ def _predict_with_blocking_impl( for a in ipt.axes if isinstance(a.size, v0_5.ParameterizedSize) } - block_shape = self._model_descr.get_tensor_sizes( + max_block_shape = self._model_descr.get_tensor_sizes( ns, batch_size or self._default_batch_size, sample.shape ).inputs - block_shape_factors = self._model_descr.get_tensor_sizes( - 0, 1, sample.shape - ).inputs + block_shape = { + ipt.id: { + a.id: AxisSize( + min=a.size.min, max=max_block_shape[ipt.id][a.id], step=a.size.step + ) + if isinstance(a.size, v0_5.ParameterizedSize) + else max_block_shape[ipt.id][a.id] + for a in ipt.axes + } + for ipt in self._model_descr.inputs + } return self._predict_with_fixed_blocking_impl( sample, block_shape=block_shape, - block_shape_factors=block_shape_factors, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, skip_input_padding=skip_input_padding, @@ -246,8 +253,7 @@ def _predict_with_blocking_impl( def predict_with_fixed_blocking( self, sample: Sample, - input_block_shape: PerMember[PerAxis[int]], - input_block_shape_factors: PerMember[PerAxis[int]] | None = None, + input_block_shape: PerMember[PerAxis[int | AxisSize]], skip_preprocessing: bool = False, skip_postprocessing: bool = False, skip_input_padding: bool = False, @@ -271,7 +277,6 @@ def predict_with_fixed_blocking( sample = pp._predict_with_fixed_blocking_impl( sample, block_shape=input_block_shape, - block_shape_factors=input_block_shape_factors or input_block_shape, skip_preprocessing=False, skip_postprocessing=False, skip_input_padding=False, @@ -281,7 +286,6 @@ def predict_with_fixed_blocking( return self._predict_with_fixed_blocking_impl( sample, block_shape=input_block_shape, - block_shape_factors=input_block_shape_factors or input_block_shape, skip_preprocessing=skip_preprocessing, skip_postprocessing=skip_postprocessing, skip_input_padding=skip_input_padding, @@ -292,9 +296,8 @@ def predict_with_fixed_blocking( def _predict_with_fixed_blocking_impl( self, sample: Sample, - block_shape: PerMember[PerAxis[int]], + block_shape: PerMember[PerAxis[int | AxisSize]], *, - block_shape_factors: PerMember[PerAxis[int]], skip_preprocessing: bool, skip_postprocessing: bool, skip_input_padding: bool, @@ -524,9 +527,8 @@ def _predict_without_blocking_impl( def _predict_with_fixed_blocking_impl( self, sample: Sample, - block_shape: PerMember[PerAxis[int]], + block_shape: PerMember[PerAxis[int | AxisSize]], *, - block_shape_factors: PerMember[PerAxis[int]], skip_preprocessing: bool, skip_postprocessing: bool, skip_input_padding: bool, @@ -558,9 +560,8 @@ def _predict_with_fixed_blocking_impl( output_members = self._forward_with_fixed_blocking( sample.members, - chunk_shape=PROCESSING_CHUNK_MIN_BYTES, + chunk_shape=settings.processing_chunk_min_bytes, block_shape=block_shape, - block_shape_factors=block_shape_factors, ) sample = Sample(id=sample.id, members=output_members, stat=sample.stat) @@ -585,8 +586,7 @@ def _forward_with_fixed_blocking( self, sample_members: PerMember[Tensor], chunk_shape: PerMember[PerAxis[int]] | int, - block_shape: PerMember[PerAxis[int]], - block_shape_factors: PerMember[PerAxis[int]], + block_shape: PerMember[PerAxis[int | AxisSize]], ) -> dict[MemberId, Tensor]: def get_output_shape( input_shape: PerMember[PerAxis[int]], @@ -610,7 +610,6 @@ def get_output_shape( input_halo=self._default_input_halo, output_halo=self._default_output_halo, get_output_shape=get_output_shape, - block_shape_factors=block_shape_factors, ) def load(self): diff --git a/src/bioimageio/core/_resource_tests.py b/src/bioimageio/core/_resource_tests.py index 162e1e32..a1e36065 100644 --- a/src/bioimageio/core/_resource_tests.py +++ b/src/bioimageio/core/_resource_tests.py @@ -1208,7 +1208,6 @@ def get_ns(n: int): }, stat=test_input.stat, id=test_input.id, - unstacked_dim_order=None, ) expected_output_shapes = { t.id: { diff --git a/src/bioimageio/core/_settings.py b/src/bioimageio/core/_settings.py index 4f6917f9..65129da0 100644 --- a/src/bioimageio/core/_settings.py +++ b/src/bioimageio/core/_settings.py @@ -46,6 +46,9 @@ def _set_default_mps_fallback(cls, value: bool | None): ) """URL to the bioimageio collection config""" + processing_chunk_min_bytes: int = 104_857_600 # 100 MiB + """Minimum number of bytes to process in a single (dask) chunk when processing large tensors blockwise.""" + gradio_server: str | None = None """URL or Hugging Face space name to connect to with the remote gradio model adapter or remote gradio prediction pipeline. diff --git a/src/bioimageio/core/_utils/chunking.py b/src/bioimageio/core/_utils/chunking.py deleted file mode 100644 index e4d0d976..00000000 --- a/src/bioimageio/core/_utils/chunking.py +++ /dev/null @@ -1,159 +0,0 @@ -from __future__ import annotations - -from collections.abc import Callable, Mapping -from typing import Literal, cast - -import dask.array as da - -from bioimageio.core._utils.type_hints import NestedList - -from ..axis import PerAxis -from ..common import HaloLike, MemberId, PadMode, PerMember -from ..tensor import Tensor - - -def chunk_tensors( - tensors: PerMember[Tensor], - chunk_shapes: PerMember[PerAxis[int]], - *, - halo: PerMember[PerAxis[HaloLike]] | None = None, - pad_mode: PadMode | PerMember[PadMode] | Literal["none"] = "none", -) -> dict[MemberId, Tensor]: - if unknown_chunks := [m for m in chunk_shapes if m not in tensors]: - raise ValueError( - f"chunk sizes specified for unknown tensors: {unknown_chunks}." - ) - return { - m: t.chunk( - chunk_shapes[m], - halo=halo.get(m) if halo is not None else None, - pad_mode=pad_mode - if isinstance(pad_mode, str) - else pad_mode.get(m, "none") - if isinstance(pad_mode, Mapping) - else pad_mode, - ) - for m, t in tensors.items() - } - - -def chunkwise_to_nested_list_prechunked( - tensors: PerMember[Tensor], func: Callable[[PerMember[Tensor]], PerMember[Tensor]] -) -> NestedList[PerMember[Tensor]]: - """Apply a function to each chunk of the chunked tensors and return a nested list of processed chunks. - - See [chunk_tensors][] for chunking the tensors beforehand. - - Args: - func: The function to apply to each sample chunk. - """ - - if not all(t.data.data.numblocks for t in tensors.values()): - raise ValueError( - "Sample is not chunked and no `chunk_shapes` provided. Please provide `chunk_shapes` or chunk the sample first." - ) - - chunk_ndims = cast( - PerMember[tuple[int, ...]], - {m: t.data.data.numblocks for m, t in tensors.items()}, - ) - if len(set(chunk_ndims.values())) != 1: - raise ValueError(f"Input chunk grids do not match: {chunk_ndims}") - - chunk_grid_shape = next(iter(chunk_ndims.values())) - - def func_chunks( - axis: int = 0, loc: tuple[int, ...] = () - ) -> NestedList[PerMember[Tensor]]: - if axis < len(chunk_grid_shape): - grid_size = chunk_grid_shape[axis] - return [func_chunks(axis + 1, (*loc, i)) for i in range(grid_size)] - - return func( - { - m: Tensor(t.data.data.blocks[loc], dims=t.dims) - for m, t in tensors.items() - } - ) - - return func_chunks() - - -# def blockwise_to_nested_list( -# tensors: PerMember[Tensor], func: Callable[[PerMember[Tensor]], PerMember[Tensor]], block_shapes: PerMember[PerAxis[int]], halo: PerMember[PerAxis[HaloLike]], pad_mode: PadMode | PerMember[PadMode] -# ) -> NestedList[PerMember[Tensor]]: -# """Apply a function to each block and return a nested list of processed blocks. -# Each block will have the same shape, the last two blocks along each dimension may overlap by more than the halo. - -# See [chunk_tensors][] for chunking the tensors beforehand. - -# Args: -# func: The function to apply to each sample chunk. -# """ - -# if not all(t.data.data.numblocks for t in tensors.values()): -# raise ValueError( -# "Sample is not chunked and no `chunk_shapes` provided. Please provide `chunk_shapes` or chunk the sample first." -# ) - -# chunk_ndims = cast( -# PerMember[tuple[int, ...]], -# {m: t.data.data.numblocks for m, t in tensors.items()}, -# ) -# if len(set(chunk_ndims.values())) != 1: -# raise ValueError(f"Input chunk grids do not match: {chunk_ndims}") - -# chunk_grid_shape = next(iter(chunk_ndims.values())) - -# def func_chunks( -# axis: int = 0, loc: tuple[int, ...] = () -# ) -> NestedList[PerMember[Tensor]]: -# if axis < len(chunk_grid_shape): -# grid_size = chunk_grid_shape[axis] -# return [func_chunks(axis + 1, (*loc, i)) for i in range(grid_size)] - -# return func( -# { -# m: Tensor(t.data.data.blocks[loc], dims=t.dims) -# for m, t in tensors.items() -# } -# ) - -# return func_chunks() - - -def chunkwise( - tensors: PerMember[Tensor], func: Callable[[PerMember[Tensor]], PerMember[Tensor]] -) -> dict[MemberId, Tensor]: - """Apply a function to each chunk of the chunked tensors. - - See [chunk_tensors][] for chunking the tensors beforehand. - - Args: - func: The function to apply to each sample chunk. - """ - nested_outputs = chunkwise_to_nested_list_prechunked(tensors, func) - - def select_output( - nested: NestedList[PerMember[Tensor]], output_id: MemberId - ) -> NestedList[da.Array]: - if isinstance(nested, list): - return [select_output(item, output_id) for item in nested] - else: - a = nested[output_id].data.data - assert isinstance(a, da.Array) - return a - - first_block = nested_outputs - while isinstance(first_block, list): - first_block = first_block[0] - - return { - output_id: Tensor( - da.block( - select_output(nested_outputs, output_id), - ), - dims=first_block[output_id].dims, - ) - for output_id in first_block - } diff --git a/src/bioimageio/core/_utils/multi_index.py b/src/bioimageio/core/_utils/multi_index.py deleted file mode 100644 index a0fea01b..00000000 --- a/src/bioimageio/core/_utils/multi_index.py +++ /dev/null @@ -1,113 +0,0 @@ -from __future__ import annotations - - -# def create_tensor_with_multiindex( -# unstacked_shape: PerAxis[int], -# transpose_to: Sequence[AxisId], -# *, -# fill_value: float | None = None, -# dtype: np.dtype | None = None, -# unstacked_data: NDArray[Any] | da.Array | None = None, -# stacked_data: NDArray[Any] | da.Array | None = None, -# ) -> Tensor: -# """Create a tensor potentially with a MultiIndex in the batch dimension. - -# The tensor is created with the given unstacked shape and then transposed to `transpose_to`. -# If `transpose_to` is missing some of the unstacked_shape dimensions a MultiIndex will be created. -# Otherwise the tensor will be created without a batch MultiIndex. - -# Args: -# unstacked_shape: The shape of the tensor before stacking. -# transpose_to: The order of the axes after transposing. -# fill_value: The value to fill the tensor with. If None, the tensor will be empty (ignored if unstacked_data or stacked_data are not None). -# dtype: The data type of the tensor. If None, the data type will be inferred from unstacked_data or stacked_data (must be specified if neither unstacked_data nor stacked_data is provided). -# unstacked_data: The data to use for the unstacked tensor. If provided, fill_value will be ignored. May not be provided together with stacked_data. -# stacked_data: The data to use for the stacked tensor. If provided, fill_value will be ignored. May not be provided together with unstacked_data. - -# Returns: -# A tensor (with a MultiIndex in the batch dimension). -# """ -# from ..tensor import Tensor - -# if unstacked_data is not None and stacked_data is not None: -# raise ValueError("unstacked_data and stacked_data may not be provided together") - -# if unstacked_data is None: -# if stacked_data is not None: -# dtype = stacked_data.dtype # pyright: ignore[reportUnknownVariableType] -# elif dtype is None: -# raise ValueError( -# "dtype must be specified if neither unstacked_data nor stacked_data is provided" -# ) - -# if fill_value is None or stacked_data is not None: -# unstacked_data = da.zeros( # pyright: ignore[reportUnknownVariableType] -# shape=tuple(unstacked_shape.values()), dtype=dtype -# ) -# else: -# unstacked_data = da.full( # pyright: ignore[reportUnknownVariableType] -# shape=tuple(unstacked_shape.values()), -# fill_value=fill_value, -# dtype=dtype, -# ) - -# assert isinstance(unstacked_data, da.Array) -# elif dtype is not None: -# unstacked_data = unstacked_data.astype(dtype) # pyright: ignore[reportUnknownVariableType] -# assert isinstance(unstacked_data, (np.ndarray, da.Array)) -# else: -# dtype = unstacked_data.dtype # pyright: ignore[reportUnknownVariableType] -# assert isinstance(dtype, np.dtype) - -# if isinstance(unstacked_data, da.Array): -# tensor = Tensor.from_array(unstacked_data, dims=tuple(unstacked_shape.keys())) -# elif isinstance(unstacked_data, np.ndarray): -# assert is_ndarray(unstacked_data) -# tensor = Tensor.from_numpy(unstacked_data, dims=tuple(unstacked_shape.keys())) -# else: -# assert_never(unstacked_data) - -# stacked_tensor = tensor.transpose( -# transpose_to, extra_dims="stack", missing_dims="expand" -# ) -# if stacked_data is not None: -# if stacked_data.shape != stacked_tensor.data.shape: -# raise ValueError( -# f"stacked_data shape {stacked_data.shape} does not match the stacked tensor shape {stacked_tensor.data.shape}" -# ) -# stacked_tensor.data.data = stacked_data - -# return stacked_tensor - - -# def get_unstacked_shape( -# stacked_shape: PerAxis[int], -# *, -# unstacked_dim_order: Sequence[AxisId] | None, -# unstacked_batch_shape: PerAxis[int] | None, -# ) -> PerAxis[int]: -# """Combine a shape with an optional unstacked batch shape and apply an optional unstacked dim order to get the final unstacked shape. - -# Returns `stacked_shape` if both `unstacked_dim_order` and `unstacked_batch_shape` are None. -# """ -# unstacked_shape_unordered = {**stacked_shape, **(unstacked_batch_shape or {})} - -# if unstacked_dim_order is not None: -# assert all(a in unstacked_dim_order for a in unstacked_shape_unordered), ( -# f"unstacked_dim_order={unstacked_dim_order} does not contain all axes of the sample shape {stacked_shape} and unstacked sample batch shape {unstacked_batch_shape}" -# ) -# order = unstacked_dim_order -# else: -# # if no explicit unstacked dim order is provided we insert the unstacked batch axes at the position of the batch axis in the sample shape -# order = list(stacked_shape) -# if unstacked_batch_shape: -# assert BATCH_AXIS_ID in stacked_shape, ( -# f"unstacked sample batch shape {unstacked_batch_shape} is provided but the sample shape {stacked_shape} does not contain a batch axis" -# ) -# b_idx = order.index(BATCH_AXIS_ID) -# order[b_idx : b_idx + 1] = list(unstacked_batch_shape) - -# assert all(a in order for a in unstacked_shape_unordered), ( -# f"unstacked_dim_order={unstacked_dim_order} does not contain all axes of the sample shape {stacked_shape} and unstacked sample batch shape {unstacked_batch_shape}" -# ) -# return {a: unstacked_shape_unordered[a] for a in order} diff --git a/src/bioimageio/core/block.py b/src/bioimageio/core/block.py index 9ff43487..d2a56ff4 100644 --- a/src/bioimageio/core/block.py +++ b/src/bioimageio/core/block.py @@ -25,9 +25,9 @@ from bioimageio.spec._internal.utils import SLOTS from ._axis_annotations import PerAxisAnno -from .axis import TIME_AXIS_ID, AxisId, PerAxis +from ._settings import settings +from .axis import TIME_AXIS_ID, AxisId, AxisSize, PerAxis from .common import ( - PROCESSING_CHUNK_MIN_BYTES, BlockLoc, Halo, HaloLike, @@ -390,10 +390,9 @@ def outer_tensor(self) -> Tensor: def split_tensor_into_blocks( tensor: Tensor, *, - block_shape: PerAxis[int], + block_shape: PerAxis[int | AxisSize], halo: PerAxis[HaloLike], pad_mode: PadMode | Literal["none"], - block_shape_factors: PerAxis[int] | None, ) -> BlockArray: """ Args: @@ -411,72 +410,81 @@ def split_tensor_into_blocks( f"unknown dims in halo: {unknown_halo_dims} for tensor {tensor.dims}" ) - if isinstance(pad_mode, Mapping) and ( - unknown_pad_dims := [a for a in pad_mode if a not in tensor.dims] - ): - raise ValueError( - f"unknown dims in pad_mode: {unknown_pad_dims} for tensor {tensor.dims}" - ) - - if large_block_dims := [ - (a, b, shape_size_limit) - for a in block_shape - if (shape_size_limit := tensor.tagged_shape.get(a, 1)) < (b := block_shape[a]) - ]: - logger.warning( - "Block shape dimensions {} larger than shape dimensions {} are limiting the shape size", - {a: b for a, b, _s in large_block_dims}, - {a: s for a, _b, s in large_block_dims}, - ) + # fill in default halo (0) + halo = {d: Halo.create(halo.get(d, 0)) for d in tensor.dims} - block_shape = {d: block_shape.get(d, s) for d, s in tensor.tagged_shape.items()} - if any(block_shape[d] > tensor.tagged_shape[d] for d in tensor.dims): - raise ValueError( - f"block_shape {block_shape} larger than tensor shape {tensor.tagged_shape}." + main_block_shape = { + d: ( + bs_limit := min( + s + (s_halo := 0 if pad_mode == "none" else sum(halo[d])), + bs.max or s + s_halo, + ) ) + - ((bs_limit - bs.min) % (bs.step or 1)) + if isinstance((bs := block_shape.get(d, s)), AxisSize) + else bs + for d, s in tensor.tagged_shape.items() + } - if block_shape_factors is None and any( - tensor.tagged_shape[d] % block_shape[d] != 0 for d in tensor.dims - ): + if large_block_dims := { + a + for a, bs in block_shape.items() + if tensor.tagged_shape.get(a, 1) + < (bs.max or bs.min if isinstance(bs, AxisSize) else bs) + }: raise ValueError( - f"tensor shape {tensor.tagged_shape} not divisible by block_shape {block_shape} and `block_shape_factors` is None." + "Block shape {} larger than tensor shape {} along {}", + block_shape, + tensor.tagged_shape, + large_block_dims, ) - # fill in default halo (0) - halo = {d: Halo.create(halo.get(d, 0)) for d in tensor.dims} - inner_1d_slices: dict[AxisId, list[SliceInfo | SliceInfoWithOverlap]] = {} - inner_sizes = {d: block_shape[d] - sum(halo[d]) for d in tensor.dims} + inner_sizes = {d: main_block_shape[d] - sum(halo[d]) for d in tensor.dims} for d in tensor.dims: - valid_edge_left = halo[d].left if pad_mode == "none" else 0 - valid_edge_right = halo[d].right if pad_mode == "none" else 0 - shape_size_limit = tensor.tagged_shape[d] - valid_edge_right - inner_size = inner_sizes[d] - block_shape_factor = ( - block_shape_factors.get(d, block_shape[d]) - if block_shape_factors is not None - else None + inner_first_start = halo[d].left if pad_mode == "none" else 0 + inner_last_stop = tensor.tagged_shape[d] - ( + halo[d].right if pad_mode == "none" else 0 ) - inner_1d_slices[d] = [ - SliceInfo(p, p + inner_size) - if p + inner_size <= shape_size_limit - or block_shape_factor - is None # redundant for typing as we check for divisiblity above for `block_shape_factors is None` - else SliceInfo(p, shape_size_limit) - if (shape_size_limit - p) % block_shape_factor == 0 - else SliceInfoWithOverlap( - shape_size_limit - - ( - block_shape_factor - * ceil((shape_size_limit - p) / block_shape_factor) - ) - + sum(halo[d]), - shape_size_limit, - p, - shape_size_limit, - ) - for p in range(valid_edge_left, shape_size_limit, inner_size) - ] + inner_size = inner_sizes[d] + outer_size = block_shape.get(d, tensor.tagged_shape[d] + sum(halo[d])) + if isinstance(outer_size, int): + outer_size = AxisSize(min=outer_size) + + outer_min = outer_size.min + step = outer_size.step or 1 + assert inner_size % step == 0 + + inner_1d_slices[d] = [] + for start in range(inner_first_start, inner_last_stop, inner_size): + if start + inner_size <= inner_last_stop: + inner_1d_slices[d].append(SliceInfo(start, start + inner_size)) + else: + inner_remainder = inner_last_stop - start + outer_remainder = inner_remainder + sum(halo[d]) + if ( + outer_remainder >= outer_min + and (outer_remainder - outer_min) % step == 0 + ): + # last block has valid shape + inner_1d_slices[d].append(SliceInfo(start, inner_last_stop)) + else: + # last block needs to overlap to have a valid shape + minimum_outer_remainder = outer_min + ( + step + * max( + 0, + ceil((outer_remainder - outer_min) / step), + ) + ) + valid_inner_start = inner_last_stop - ( + minimum_outer_remainder - sum(halo[d]) + ) + inner_1d_slices[d].append( + SliceInfoWithOverlap( + valid_inner_start, inner_last_stop, start, inner_last_stop + ) + ) out_shape = tuple(len(slices) for slices in inner_1d_slices.values()) out: BlockArray = np.empty(out_shape, dtype=object) @@ -484,12 +492,7 @@ def split_tensor_into_blocks( block_loc: BlockLoc = idx inner_slice = {d: inner_1d_slices[d][i] for d, i in zip(tensor.dims, block_loc)} out[idx] = Block( - parent=tensor, - inner_slice=inner_slice, - halo=halo, - # block_loc=block_loc, - # block_loc_shape=out_shape, - pad_mode=pad_mode, + parent=tensor, inner_slice=inner_slice, halo=halo, pad_mode=pad_mode ) return out @@ -497,27 +500,25 @@ def split_tensor_into_blocks( def split_multiple_tensors_into_blocks( tensors: PerMember[Tensor], - block_shape: PerMember[PerAxis[int]], + block_shape: PerMember[PerAxis[int | AxisSize]], *, halo: PerMember[PerAxis[HaloLike]], broadcast: bool = False, pad_mode: PadMode | PerMember[PadMode] | Literal["none"], - block_shape_factors: PerMember[PerAxis[int] | None] | None, ) -> dict[MemberId, BlockArray]: """Split multiple tensors (e.g. sample members) into aligned blocks. Args: tensors: the tensors to split into blocks - block_shape: the block shape per sample member (depending on `broaadcast` the block shapes must result in the same number of blocks for all tensors) - halo: the halo around each block per sample member + block_shape: the block shape per sample member (depending on `broaadcast` the block shapes must result in the same number of blocks for all tensors). + Omitted axes are treated as having a block size equal to the tensor axis size. + To allow for variable block sizes along a given axis, the block size can be specified as an `AxisSize` with a `min` `max` and `step` value. + Any such parameterized axis may result in blocks overlapping along that axis, if the tensor shape does not divide by the maximum block size with a valid remainder. + halo: the halo around each block per sample member, omitted axes are treated as having a halo of 0. broadcast: whether to allow broadcasting the resulting tensor blocks to each other, e.g. if one tensor does not have a dimension along which another is blocked up. - pad_mode: the padding mode (optionally per sample member) to use when the halo extends beyond the tensor boundary - block_shape_factors: The factors by which the block shape has to be divisible to allow smaller blocks. - Any axis not included in a given mapping is assumed to be set to the block shape. - If the tensor shape does not divide evenly by a block shape factor, the last two blocks will overlap along that axis. - If the tensor shape is smaller than a block shape factor, an exception is raised. - `None` means no smaller blocks are allowed, nor is overlap allowed; an exception is raised if the shape does not divide evenly. - To allow arbitrary small blocks, set the block shape factor to 1 for all axes, e.g. `defaultdict(lambda: defaultdict(lambda: 1))`. + pad_mode: the padding mode (optionally per sample member) to use when the halo extends beyond the tensor boundary. + Raises: + ValueError: If the tensor shape cannot be divided into blocks according to `block_shape`. """ if unknown_blocks := [m for m in block_shape if m not in tensors]: @@ -551,9 +552,6 @@ def split_multiple_tensors_into_blocks( pad_mode=pad_mode.get(m, "symmetric") if isinstance(pad_mode, Mapping) else pad_mode, - block_shape_factors=block_shape_factors.get(m, {}) - if block_shape_factors is not None - else None, ) assert len(blocks) > 0, blocks @@ -582,13 +580,12 @@ def apply_blockwise( ], inputs: PerMember[Tensor], *, - block_shape: PerMember[PerAxis[int]], + block_shape: PerMember[PerAxis[int | AxisSize]], pad_mode: PadMode | PerMember[PadMode] | Literal["none"], input_halo: PerMember[PerAxis[Halo]], output_halo: PerMember[PerAxis[Halo]], get_output_shape: Callable[[PerMember[PerAxis[int]]], PerMember[PerAxis[int]]], - block_shape_factors: PerMember[PerAxis[int]] | None = MappingProxyType({}), - chunk_shape: PerMember[PerAxis[int]] | int = PROCESSING_CHUNK_MIN_BYTES, + chunk_shape: PerMember[PerAxis[int]] | int = settings.processing_chunk_min_bytes, blockwise_processing_mode: Literal["parallel", "sequential", "auto"] = "auto", ) -> dict[MemberId, Tensor]: """Apply `func` to `inputs` blockwise. @@ -597,7 +594,7 @@ def apply_blockwise( Args: func: the function to apply to each block inputs: the input tensors to process - block_shape: the shape of the blocks to be processed by func + block_shape: the shape of the blocks to be processed by func. May be expressed using `AxisSize` objects to allow for variable block shapes. pad_mode: the padding mode (optionally per input member) to use when the halo extends beyond the tensor boundary if "none" and `output_halo` is not empty, the output will be smaller by `output_halo` input_halo: the halo around each input block (e.g. to compenstate for `output_halo` such that cropped output blocks can be stitched together) @@ -606,12 +603,6 @@ def apply_blockwise( get_output_shape: a function to compute the output shape based on the input shape. May return values <1 for output axes without halo, which means the output will be concatenated along those axes. (Only used if the input shape does not evenly divide by the block shape to correct overlapping outputs.) - block_shape_factors: The factors by which the block shape has to be divisible to allow smaller blocks. - Any axis not included in a given mapping is assumed to be set to the block shape. - If the tensor shape does not divide evenly by a block shape factor, the last two blocks will overlap along that axis. - If the tensor shape is smaller than a block shape factor, an exception is raised. - `None` means no smaller blocks are allowed, nor is overlap allowed; an exception is raised if the shape does not divide evenly. - To allow arbitrary small blocks, set the block shape factor to 1 for all axes, e.g. `defaultdict(lambda: defaultdict(lambda: 1))`. chunk_shape: the shape of the chunks to process. Chunks are larger than blocks and help to reduce the number of tasks in the dask graph for 'parallel' processing. Must be larger than `block_shape`. @@ -630,18 +621,42 @@ def apply_blockwise( "blockwise_processing_mode='sequential' is not yet implemented" ) + max_block_shape: dict[MemberId, dict[AxisId, int]] = {} + for m, b in block_shape.items(): + max_block_shape[m] = {} + for d, bs in b.items(): + if isinstance(bs, AxisSize): + max_tensor_size = inputs[m].tagged_shape.get(d, 1) + sum( + input_halo.get(m, {}).get(d, Halo(0, 0)) + ) + effective_bs = max( + bs.min, + max_tensor_size if bs.max is None else min(bs.max, max_tensor_size), + ) + if ( + not isinstance(chunk_shape, int) + and m in chunk_shape + and d in chunk_shape[m] + ): + effective_bs = min(effective_bs, chunk_shape[m][d]) + else: + effective_bs = bs + + max_block_shape[m][d] = effective_bs + if isinstance(chunk_shape, int): chunk_shape = get_chunk_shape( - inputs, block_shape=block_shape, chunk_min_bytes=chunk_shape + inputs, block_shape=max_block_shape, chunk_min_bytes=chunk_shape ) + if any( - chunk_shape[m][d] < block_shape[m][d] + max_block_shape[m][d] > chunk_shape[m][d] for m in chunk_shape for d in chunk_shape[m] - if m in block_shape and d in block_shape[m] + if m in max_block_shape and d in max_block_shape[m] ): raise ValueError( - f"chunk_shape {chunk_shape} must not be smaller than block_shape {block_shape}" + f"block_shape {block_shape} must not be larger than chunk_shape {chunk_shape}" ) process_chunk = partial( @@ -653,7 +668,6 @@ def apply_blockwise( output_halo=output_halo, processing_mode=blockwise_processing_mode, get_output_shape=get_output_shape, - block_shape_factors=block_shape_factors, ) return _apply_blockwise_impl( @@ -664,7 +678,6 @@ def apply_blockwise( input_halo=input_halo, output_halo=output_halo, processing_mode="parallel", - block_shape_factors=block_shape_factors, get_output_shape=get_output_shape, ) @@ -673,8 +686,7 @@ def _apply_blockwise_impl( func: Callable[[PerMember[Tensor]], PerMember[Tensor | None]], inputs: PerMember[Tensor], *, - block_shape: PerMember[PerAxis[int]], - block_shape_factors: PerMember[PerAxis[int]] | None = MappingProxyType({}), + block_shape: PerMember[PerAxis[int | AxisSize]], pad_mode: PadMode | PerMember[PadMode] | Literal["none"], input_halo: PerMember[PerAxis[HaloLike]], output_halo: PerMember[PerAxis[HaloLike]], @@ -687,13 +699,7 @@ def _apply_blockwise_impl( Args: func: the function to apply to each block inputs: the input tensors to process - block_shape: the shape of the blocks to be processed by func - block_shape_factors: The factors by which the block shape has to be divisible to allow smaller blocks. - Any axis not included in a given mapping is assumed to be set to the block shape. - If the tensor shape does not divide evenly by a block shape factor, the last two blocks will overlap along that axis. - If the tensor shape is smaller than a block shape factor, an exception is raised. - `None` means no smaller blocks are allowed, nor is overlap allowed; an exception is raised if the shape does not divide evenly. - To allow arbitrary small blocks, set the block shape factor to 1 for all axes, e.g. `defaultdict(lambda: defaultdict(lambda: 1))`. + block_shape: the shape of the blocks to be processed by func. May be expressed using `AxisSize` objects to allow for variable block shapes. pad_mode: the padding mode (optionally per input member) to use when the halo extends beyond the tensor boundary if "none" and `output_halo` is not empty, the output will be smaller by the `output_halo` input_halo: the halo around each input block (e.g. to compenstate for `output_halo` such that cropped output blocks can be stitched together) @@ -709,7 +715,6 @@ def _apply_blockwise_impl( halo=input_halo, broadcast=True, pad_mode=pad_mode, - block_shape_factors=block_shape_factors, ) return apply_to_block_array( @@ -885,7 +890,7 @@ def func_blocks( def get_chunk_shape( tensors: PerMember[Tensor], block_shape: PerMember[PerAxis[int]], - chunk_min_bytes: int = PROCESSING_CHUNK_MIN_BYTES, + chunk_min_bytes: int = settings.processing_chunk_min_bytes, ) -> PerMember[PerAxis[int]]: if unknown_members := [m for m in block_shape if m not in tensors]: raise ValueError( @@ -935,6 +940,7 @@ def increment_chunk_dims(incr_dims: Sequence[AxisId]): if a in chunk_shape[m] ): incr_dims.remove(a) + break # increment chunk dimensions in order of non-singleton spatial, time, other, then any singleton dim non_singleton_spatial_dims = [ @@ -959,99 +965,3 @@ def increment_chunk_dims(incr_dims: Sequence[AxisId]): {m: dict(t.tagged_shape) for m, t in tensors.items()}, ) return chunk_shape - - -# def get_transformed_block_arrays( -# input_blocks: PerMember[BlockArray], output_axes: PerMember[PerAxis[LinearAxisTransform | int]] -# ) -> PerMember[BlockArray]: -# first_block = {m: next(iter(cast(Iterator[Block], b.flat))) for m, b in input_blocks.items()} - -# all_input_blocks_ndims = {m: b.shape for m, b in input_blocks.items()} -# input_blocks_ndims = next(iter(all_input_blocks_ndims.values())) -# assert all(s == input_blocks_ndims for s in all_input_blocks_ndims.values()), ( -# f"Input block grids do not match: {all_input_blocks_ndims}" -# ) - -# def transform_shape(input_shape: PerMember[PerAxis[int]]): -# return { -# m: { -# a: ( -# trf -# if isinstance(trf, int) -# else trf.compute(input_shape[trf.member][trf.axis]) -# ) -# for a, trf in output_axes[m].items() -# } -# for m in output_axes -# } -# output_shape = transform_shape({m: first_block[m].full_shape for m in input_blocks}) - -# # output shape in number of blocks -# output_blocks_ndims = transform_shape({m: dict(zip(first_block[m].dims, b.shape)) for m, b in input_blocks.items()}) - -# output_blocks: PerMember[BlockArray] = {m: np.empty(tuple(v.values()), dtype=object) for m, v in output_blocks_ndims.items()} -# assert prod(output_blocks_ndims.values()) -# def get_member_halo(block_idx: tuple[int, ...], m: MemberId, round: Callable[[float], int]): -# return { -# a: ( -# Halo(0, 0) -# if isinstance(trf, int) -# or trf.axis not in input_blocks[trf.member][block_idx].halo -# else Halo( -# round(input_blocks[trf.member][block_idx].halo[trf.axis].left * trf.scale), -# round(input_blocks[trf.member][block_idx].halo[trf.axis].right * trf.scale), -# ) -# ) -# for a, trf in output_axes[m].items() -# } - -# example_input_key = next(iter(first_block)) -# example_input_ -# for idx in itertools.product(*(range(s) for s in output_blocks_ndims)): -# pass - - -# halo: dict[MemberId, dict[AxisId, Halo]] = {} -# for m in output_axes: -# halo[m] = get_member_halo(input_blocks[m], m, floor) -# if halo[m] != get_member_halo(input_blocks[m], m, ceil): -# raise ValueError( -# f"failed to unambiguously scale halo {halo[m]} with {output_axes[m]}" -# + f" for {m}." -# ) - -# inner_slice = { -# m: { -# a: ( -# SliceInfo(0, trf) -# if isinstance(trf, int) -# else SliceInfo( -# trf.compute( -# self.blocks[trf.member].inner_slice[trf.axis].start -# ), -# trf.compute(self.blocks[trf.member].inner_slice[trf.axis].stop), -# ) -# ) -# for a, trf in output_axes[m].items() -# } -# for m in output_axes -# } -# unstacked_batch_slice = None -# for b in self.blocks.values(): -# if b.unstacked_batch_slice is None: -# continue -# elif unstacked_batch_slice is None: -# unstacked_batch_slice = b.unstacked_batch_slice -# else: -# assert unstacked_batch_slice == b.unstacked_batch_slice, ( -# "Incompatible unstacked batch slices across sample block members" -# ) - -# return { -# m: Block( -# parent=output_shape[m], -# inner_slice=inner_slice[m], -# halo=halo[m], -# ) -# for m in output_axes -# } diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py deleted file mode 100644 index 914afa1e..00000000 --- a/src/bioimageio/core/block_meta.py +++ /dev/null @@ -1,541 +0,0 @@ -# from __future__ import annotations - -# import itertools -# from collections.abc import Collection, Iterator -# from dataclasses import dataclass -# from functools import cached_property -# from math import floor -# from types import MappingProxyType -# from typing import ( -# Annotated, -# Callable, -# Iterable, -# Iterator, -# ) - -# import numpy as np -# import pydantic -# from loguru import logger -# from numpy.typing import NDArray -# from typing_extensions import Self - -# from ._axis_annotations import PerAxisAnno -# from .axis import BATCH_AXIS_ID, AxisId, PerAxis -# from .common import ( -# BlockLoc, -# BlockLocShape, -# Halo, -# HaloLike, -# PadWidth, -# PerMember, -# SliceInfo, -# SliceInfoWithOverlap, -# ) - - -# @dataclass -# class LinearAxisTransform: -# axis: AxisId -# scale: float -# offset: int - -# def compute(self, s: int, round: Callable[[float], int] = floor) -> int: -# return round(s * self.scale) + self.offset - - -# @pydantic.dataclasses.dataclass(frozen=True) -# class BlockMeta: -# """Block meta data of a sample member (a tensor in a sample) - -# Figure for illustration: -# The first 2d block (dashed) of a sample member (**bold**). -# The inner slice (thin) is expanded by a halo in both dimensions on both sides. -# The outer slice reaches from the sample member origin (0, 0) to the right halo point. - -# ``` -# first block (at the sample origin) -# ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┐ -# ╷ halo(left) ╷ -# ╷ padding outside the sample ╷ -# ╷ (0, 0)┏━━━━━━━━━━━━━━━━━┯━━━━━━━━━┯━━━➔ -# ╷ ┃ │ ╷ sample member -# ╷ ┃ inner │ outer ╷ -# ╷ ┃ region │ region ╷ -# ╷ ┃ /slice │ /slice ╷ -# ╷ ┃ │ ╷ -# ╷ ┣─────────────────┘ ╷ -# ╷ ┃ outer region/slice ╷ -# ╷ ┃ halo(right) ╷ -# └ ─ ─ ─ ─┃─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┘ -# ⬇ -# ``` - -# Note: -# - Inner and outer slices are specified in sample member coordinates. -# - The outer_slice of a block at the sample edge may overlap by more than the -# halo with the neighboring block (the inner slices will not overlap though). - -# """ - -# sample_shape: PerAxisAnno[int] -# """the axis sizes of the whole (unblocked) sample""" - -# inner_slice: PerAxisAnno[SliceInfo | SliceInfoWithOverlap] -# """inner region (without halo) wrt the sample""" - -# halo: PerAxisAnno[Halo] -# """halo enlarging the inner region to the block's sizes""" - -# block_loc: BlockLoc -# """the block index within the sample""" - -# block_loc_shape: BlockLocShape -# """The sample shape in numbers of blocks""" - -# @cached_property -# def shape(self) -> PerAxis[int]: -# """axis lengths of the block""" -# return MappingProxyType( -# { -# a: s.stop - s.start + (sum(self.halo[a]) if a in self.halo else 0) -# for a, s in self.inner_slice.items() -# } -# ) - -# # @property -# # def unstacked_sample_shape(self) -> PerAxis[int]: -# # """combined (stacked) sample shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" -# # return get_unstacked_shape( -# # self.sample_shape, -# # unstacked_dim_order=self.unstacked_dim_order, -# # unstacked_batch_shape=self.unstacked_sample_batch_shape, -# # ) - -# @cached_property -# def padding(self) -> PerAxis[PadWidth]: -# """padding to realize the halo at the sample edge -# where we cannot simply enlarge the inner slice""" -# return MappingProxyType( -# { -# a: p -# for a in self.inner_slice -# if ( -# p := PadWidth( -# ( -# self.halo[a].left -# - (self.inner_slice[a].start - self.outer_slice[a].start) -# if a in self.halo -# else 0 -# ), -# ( -# self.halo[a].right -# - (self.outer_slice[a].stop - self.inner_slice[a].stop) -# if a in self.halo -# else 0 -# ), -# ) -# ) -# != PadWidth(0, 0) -# } -# ) - -# @cached_property -# def outer_slice(self) -> PerAxis[SliceInfo]: -# """slice of the outer block (without padding) wrt the sample""" -# return MappingProxyType( -# { -# a: SliceInfo( -# max( -# 0, -# min( -# self.inner_slice[a].start -# - (self.halo[a].left if a in self.halo else 0), -# self.sample_shape[a] -# - self.inner_shape[a] -# - (self.halo[a].left if a in self.halo else 0), -# ), -# ), -# min( -# self.sample_shape[a], -# self.inner_slice[a].stop -# + (self.halo[a].right if a in self.halo else 0), -# ), -# ) -# for a in self.inner_slice -# } -# ) - -# @cached_property -# def inner_shape(self) -> PerAxis[int]: -# """axis lengths of the inner region (without halo)""" -# return MappingProxyType( -# {a: s.stop - s.start for a, s in self.inner_slice.items()} -# ) - -# @cached_property -# def local_slice(self) -> PerAxis[SliceInfo]: -# """inner slice wrt the block, **not** the sample""" -# return MappingProxyType( -# { -# a: ( -# SliceInfo( -# h.left, -# h.left + self.inner_shape[a], -# ) -# if (h := self.halo.get(a)) is not None -# else SliceInfo(0, self.inner_shape[a]) -# ) -# for a in self.inner_slice -# } -# ) - -# @property -# def dims(self) -> Collection[AxisId]: -# return set(self.inner_shape) - -# @property -# def tagged_shape(self) -> PerAxis[int]: -# """alias for shape""" -# return self.shape - -# @property -# def inner_slice_wo_overlap(self) -> PerAxis[SliceInfo]: -# """subslice of the inner slice, such that all `inner_slice_wo_overlap` can be -# stiched together trivially to form the original sample. - -# This can also be used to calculate statistics -# without overrepresenting block edge regions.""" -# return { -# a: SliceInfo(s.start_wo_overlap, s.stop_wo_overlap) -# if isinstance(s, SliceInfoWithOverlap) -# else SliceInfo(s.start, s.stop) -# for a, s in self.inner_slice.items() -# } - -# def __post_init__(self): -# assert all(a in self.sample_shape for a in self.inner_slice), ( -# "block has axes not present in sample" -# ) - -# assert all(a in self.inner_slice for a in self.halo), ( -# "halo has axes not present in block" -# ) - -# if any(s > self.sample_shape[a] for a, s in self.shape.items()): -# logger.warning( -# "block {} larger than sample {}", self.shape, self.sample_shape -# ) - -# def get_transformed(self, new_axes: PerAxis[LinearAxisTransform | int]) -> Self: -# assert BATCH_AXIS_ID not in new_axes, "cannot transform the batch axis" - -# sample_shape = { -# a: ( -# trf -# if isinstance(trf, int) -# else trf.compute(self.sample_shape[trf.axis]) -# ) -# for a, trf in new_axes.items() -# } -# assert sample_shape.get(BATCH_AXIS_ID) == self.sample_shape.get( -# BATCH_AXIS_ID -# ), "block transform must not change the batch axis size" - -# inner_slice = { -# a: ( -# SliceInfo(0, trf) -# if isinstance(trf, int) -# else SliceInfo( -# trf.compute(self.inner_slice[trf.axis].start), -# trf.compute(self.inner_slice[trf.axis].stop), -# ) -# ) -# for a, trf in new_axes.items() -# } -# assert inner_slice.get(BATCH_AXIS_ID) == self.inner_slice.get(BATCH_AXIS_ID), ( -# "block transform must not change the batch axis slice" -# ) - -# return self.__class__( -# sample_shape=sample_shape, -# # sample_id=self.sample_id, -# inner_slice=inner_slice, -# halo={ -# a: ( -# Halo(0, 0) -# if isinstance(trf, int) -# else Halo(self.halo[trf.axis].left, self.halo[trf.axis].right) -# ) -# for a, trf in new_axes.items() -# }, -# block_loc=self.block_loc, -# block_loc_shape=self.block_loc_shape, -# ) - - -# BlockArray = Annotated[NDArray[np.object_], "NDArray[Block]"] - - -# def split_shape_into_blocks( -# shape: PerAxis[int], -# *, -# block_shape: PerAxis[int], -# halo: PerAxis[HaloLike], -# # sample_id: SampleId, -# ) -> tuple[BlockLocShape, BlockArray]: -# """ -# Args: -# item_size: size of a single element in bytes, used to find shard and chunk sizes -# """ -# if unknown_axes := [ -# a for a in block_shape if a not in shape and block_shape[a] != 1 -# ]: -# raise ValueError( -# f"unknown axes in block_shape: {unknown_axes} for shape {shape}" -# ) - -# if large_block_dims := [ -# (a, b, shape_size) -# for a in block_shape -# if (shape_size := shape.get(a, 1)) < (b := block_shape[a]) -# ]: -# logger.warning( -# "Block shape dimensions {} larger than shape dimensions {} are limiting the shape size", -# {a: b for a, b, _s in large_block_dims}, -# {a: s for a, _b, s in large_block_dims}, -# ) - -# block_shape = {a: min(s, block_shape.get(a, s)) for a, s in shape.items()} -# if unknown_halo_dims := [a for a in halo if a not in shape]: -# raise ValueError(f"unknown dims in halo: {unknown_halo_dims} for shape {shape}") - -# # fill in default halo (0) and block axis length (from tensor shape) -# halo = {a: Halo.create(halo.get(a, 0)) for a in shape} - -# inner_1d_slices: dict[AxisId, list[SliceInfo | SliceInfoWithOverlap]] = {} -# inner_sizes = {a: block_shape[a] - sum(halo[a]) for a in shape} -# for a, shape_size in shape.items(): -# inner_size = inner_sizes[a] -# inner_1d_slices[a] = [ -# SliceInfo(p, min(p + inner_size, shape_size)) -# if p <= shape_size - inner_size -# else SliceInfoWithOverlap( -# shape_size - inner_size, -# (stop := min(p + inner_size, shape_size)), -# shape_size - inner_size, -# stop, -# ) -# for p in range(0, shape_size, inner_size) -# ] - -# # n_blocks = prod(map(len, inner_1d_slices.values())) - -# # return n_blocks, _block_meta_generator( -# # shape, -# # unstacked_batch_shape, -# # blocks_in_sample=n_blocks, -# # unstacked_dim_order=unstacked_dim_order, -# # inner_1d_slices=inner_1d_slices, -# # halo=halo, -# # ) - -# # def _block_meta_generator( -# # sample_shape: PerAxis[int], -# # *, -# # unstacked_dim_order: Sequence[AxisId] | None, -# # blocks_in_sample: int, -# # inner_1d_slices: dict[AxisId, list[SliceInfo | SliceInfoWithOverlap]], -# # halo: PerAxis[HaloLike], -# # sample_id: SampleId, -# # ) -> BlockMetaArray: -# assert all(a in shape for a in halo) - -# halo = {a: Halo.create(halo.get(a, 0)) for a in inner_1d_slices} -# # if unstacked_batch_shape is None: -# # idx: MultiIndex | None = None -# # else: -# # idx = MultiIndex.from_product( # pyright: ignore[reportUnknownVariableType] -# # [range(unstacked_batch_shape[a]) for a in unstacked_batch_shape], -# # names=list(unstacked_batch_shape), -# # ) -# # assert isinstance(idx, MultiIndex) - -# out: list[BlockMeta] = [] -# out_shape = tuple(len(slices) for slices in inner_1d_slices.values()) -# for i, nd_tile in enumerate(itertools.product(*inner_1d_slices.values())): -# inner_1d_slices[BATCH_AXIS_ID] -# inner_slice = dict(zip(inner_1d_slices, nd_tile)) -# # if idx is None: -# # unstacked_batch_slice = None -# # else: -# # batch_slice = inner_slice[BATCH_AXIS_ID] -# # unstacked_batch_slice: dict[AxisId, SliceInfo] | None = {} -# # for a, c in zip(idx.names, idx.codes): -# # a = BATCH_AXIS_ID if a == "original_batch" else AxisId(a) -# # a_start = int(c[batch_slice.start]) -# # a_stop = int(c[batch_slice.stop - 1]) + 1 -# # unstacked_batch_slice[a] = SliceInfo(a_start, a_stop) -# out.append( -# BlockMeta( -# sample_shape=shape, -# sample_id=sample_id, -# # unstacked_sample_batch_shape=unstacked_sample_batch_shape, -# # unstacked_dim_order=unstacked_dim_order, -# inner_slice=inner_slice, -# # unstacked_batch_slice=unstacked_batch_slice, -# halo=halo, -# block_loc=i, # TODO: update -# block_loc_shape=blocks_in_sample, -# ) -# ) -# return np.asarray(out, dtype=object).reshape(out_shape) - - -# def split_multiple_shapes_into_blocks( -# shapes: PerMember[PerAxis[int]], -# block_shapes: PerMember[PerAxis[int]], -# *, -# halo: PerMember[PerAxis[HaloLike]], -# broadcast: bool = False, -# # sample_id: SampleId, -# ) -> tuple[BlockLocShape, PerMember[BlockArray]]: -# """Split multiple shapes (e.g. sample member shapes) into aligned blocks. - -# Args: -# shapes: the shapes of the sample members to split into blocks -# block_shapes: the block shape per sample member (depending on `broaadcast` the block shapes must result in the same number of blocks for all sample members) -# unstacked_batch_shape: the shape of the unstacked batch -# unstacked_dim_order: the order of the dimensions of the unstacked tensor dimensions (unstacked batch dimensions plus the other tensor dimensions) -# halo: the halo around each block per sample member -# broadcast: whether to broadcast block shapes of members resulting in fewer blocks to match other sample members. -# """ - -# if unknown_blocks := [t for t in block_shapes if t not in shapes]: -# raise ValueError( -# f"block shape specified for unknown tensors: {unknown_blocks}." -# ) - -# if not block_shapes: -# block_shapes = shapes - -# if not broadcast and ( -# missing_blocks := [t for t in shapes if t not in block_shapes] -# ): -# raise ValueError( -# f"no block shape specified for {missing_blocks}." -# + " Set `broadcast` to True if these tensors should be repeated" -# + " as a whole for each block." -# ) - -# if extra_halo := [t for t in halo if t not in block_shapes]: -# raise ValueError( -# f"`halo` specified for tensors without block shape: {extra_halo}." -# ) - -# blocks: PerMember[BlockArray] = {} -# block_locs: PerMember[BlockLocShape] = {} -# for t in block_shapes: -# block_locs[t], blocks[t] = split_shape_into_blocks( -# shape=shapes[t], -# # sample_id=sample_id, -# # unstacked_batch_shape=unstacked_batch_shape, -# # unstacked_dim_order=unstacked_dim_order[t] -# # if unstacked_dim_order is not None -# # else None, -# block_shape=block_shapes[t], -# halo=halo.get(t, {}), -# ) -# assert all(b > 0 for b in block_locs[t]), block_locs - -# assert len(blocks) > 0, blocks -# assert len(block_locs) > 0, block_locs -# unique_block_locs = set(block_locs.values()) - -# if len(unique_block_locs) != 1: -# if not broadcast: -# raise ValueError( -# f"Mismatch for number of blocks per dimension: { {t: b.shape for t, b in blocks.items()} }. Set `broadcast` to True if this is expected." -# ) - -# blocks = dict(zip(blocks.keys(), np.broadcast_arrays(*blocks.values()))) -# block_locs = {m: b.shape for m, b in blocks.items()} -# unique_block_locs = set(block_locs.values()) -# assert len(unique_block_locs) == 1, ( -# f"Mismatch for total number of blocks (after braodcasting?!): {block_locs}" -# ) - -# return next(iter(unique_block_locs)), blocks -# # def foo(*bs: BlockMeta) -> SampleBlockMeta: -# # return SampleBlockMeta.from_blocks(dict(zip(blocks.keys(), bs))) - -# # foo_op = np.frompyfunc(foo, len(blocks), 1) -# # out = foo_op(*blocks.values()) -# # assert out.shape == next(iter(blocks.values())).shape -# # return n, out - - -# # def _consolidate_blocks(blocks: PerMember[BlockMetaArray]) -> PerMember[BlockMetaArray]: -# # sample_ids = {b.sample_id for b in blocks.values() if b.sample_id is not None} -# # if len(sample_ids) > 1: -# # raise ValueError(f"Mismatch for sample_id across members: {sample_ids}") -# # block_loc_shapes = {b.block_loc_shape for b in blocks.values()} -# # if len(block_loc_shapes) != 1: -# # raise ValueError( -# # f"Mismatch for block_loc_shape (sample shape in blocks) across members: {block_loc_shapes}" -# # ) -# # block_locs = {b.block_loc for b in blocks.values()} -# # if len(block_locs) != 1: -# # raise ValueError( -# # f"Mismatch for block_loc (block location within sample) across members: {block_locs}" -# # ) -# # return blocks - - -# @dataclass -# class Chunk: -# blocks: Iterable[PerMember[BlockMeta]] -# n_blocks: int - -# def __iter__(self) -> Iterator[PerMember[BlockMeta]]: -# yield from self.blocks - - -# @dataclass -# class Shard: -# chunks: Iterable[Chunk] -# n_chunks: int - -# def __iter__(self) -> Iterator[Chunk]: -# yield from self.chunks - - -# def split_multiple_shapes_into_shards( -# shapes: PerMember[PerAxis[int]], -# *, -# block_shapes: PerMember[PerAxis[int]], -# halo: PerMember[PerAxis[HaloLike]], -# broadcast: bool = False, -# item_size: int | None, -# chunk_shapes: PerMember[PerAxis[int]] | None = None, -# shard_shapes: PerMember[PerAxis[int]] | None = None, -# ) -> tuple[BlockLocShape, Iterable[Shard]]: -# """Split sample member shapes into aligned blocks. - -# Shards/chunks will be aligned across all sample members, which might lead to sup-optimal chunking/sharding for some sample members, but simplifies keeping the blocks aligned across all sample members. - -# Args: -# shapes: the shapes of the sample members to split into blocks -# block_shapes: the block shape per sample member (depending on `broaadcast` the block shapes must result in the same number of blocks for all sample members) -# halo: the halo around each block per sample member -# broadcast: whether to broadcast block shapes of members resulting in fewer blocks to match other sample members. -# item_size: the size in bytes of the largest data type of all members. This is used to determine shard and chunk sizes. May be None if `chunk_shapes` and `shard_shapes` are given. -# chunk_shapes: the chunk shape for at least one sample member; must be a multiple of the respective block shape. May be None if `item_size` is given. -# shard_shapes: the shard shape for at leasst one sample member; must be a multiple of the respective chunk shape. May be None if `item_size` is given. - - -# Returns: -# A tuple of the total number of blocks and an iterable of shards. - -# """ - -# raise NotImplementedError("sharding is not yet implemented") diff --git a/src/bioimageio/core/cli.py b/src/bioimageio/core/cli.py index e13351eb..19427503 100644 --- a/src/bioimageio/core/cli.py +++ b/src/bioimageio/core/cli.py @@ -84,7 +84,7 @@ from ._utils import compare from .commands import WeightFormatArgAll, WeightFormatArgAny, package, test from .common import MemberId, SampleId, SupportedWeightsFormat -from .digest_spec import get_member_ids, load_sample_for_model +from .digest_spec import create_sample_for_model, get_member_ids from .io import load_stat, save_sample, save_stat from .proc_setup import get_required_dataset_measures from .remote_backends import create_remote_model_adapter @@ -801,9 +801,9 @@ def expand_outputs(): def input_dataset(stat: Stat): for s, sp_in in zip(sample_ids, sample_paths_in): - yield load_sample_for_model( + yield create_sample_for_model( model=model_descr, - paths=sp_in, + inputs=sp_in, stat=stat, sample_id=s, ) @@ -914,7 +914,6 @@ def _subtract_samples(a: Sample, b: Sample) -> Sample: members={t: a.members[t] - b.members[t] for t in a.members}, id=a.id, stat=a.stat, - unstacked_dim_order=a.unstacked_dim_order, ) diff --git a/src/bioimageio/core/common.py b/src/bioimageio/core/common.py index 56de3a6c..fc00808c 100644 --- a/src/bioimageio/core/common.py +++ b/src/bioimageio/core/common.py @@ -14,10 +14,6 @@ from bioimageio.spec._internal.utils import SLOTS from bioimageio.spec.model import v0_5 -# SHARDED_INNER_CHUNK_MAX_BYTES: int = 1048576 # 1 MiB -# NON_SHARDED_CHUNK_MAX_BYTES: int = 67_108_864 # 64MiB -PROCESSING_CHUNK_MIN_BYTES: int = 104_857_600 # 100 MiB - SupportedWeightsFormat: TypeAlias = Literal[ "keras_hdf5", "keras_v3", diff --git a/src/bioimageio/core/io.py b/src/bioimageio/core/io.py index 8105edd5..31a36ac8 100644 --- a/src/bioimageio/core/io.py +++ b/src/bioimageio/core/io.py @@ -272,7 +272,7 @@ def _load_tensor_bioio( f"Internal path {internal_path} not found with bioio in {src}" ) - dataarray_bioio = img.xarray_dask_data.squeeze() # pyright: ignore[reportUnknownVariableType] + dataarray_bioio = img.xarray_dask_data.squeeze() assert isinstance(dataarray_bioio, xr.DataArray) # map bioio TCZYX/YXS axes to AxisId objects @@ -280,7 +280,7 @@ def _load_tensor_bioio( axes_map = { d: AxisId("channel") if d == "S" else AxisId(d) for d in dataarray_bioio.dims } - dataarray = dataarray_bioio.rename(axes_map) # pyright: ignore[reportUnknownVariableType] + dataarray = dataarray_bioio.rename(axes_map) assert isinstance(dataarray, xr.DataArray) t = Tensor.from_xarray(dataarray) @@ -341,7 +341,7 @@ def _load_tensor_clearscale(source: FileDescr | PermissiveFileSource) -> Tensor: source.as_posix() if isinstance(source, Path) else str(source) ) image = da.from_zarr(array) - axes = tuple(str(a) for a in ms.axes()) + axes = tuple(str(a) for a in ms.axes) return Tensor.from_xarray(xr.DataArray(image, dims=axes)) @@ -374,7 +374,7 @@ def _load_tensor_tifffile(source: FileDescr | PermissiveFileSource) -> Tensor: with ctxt as f: assert not isinstance(f, TextIOWrapper) - with tifffile.TiffFile(f, name=name) as tif: # pyright: ignore[reportUnknownVariableType] + with tifffile.TiffFile(f, name=name) as tif: assert isinstance(tif, tifffile.TiffFile) xr_data = tif.asxarray() page = tif.pages[0] @@ -697,7 +697,7 @@ def _save_tensor_bioio( d: bioio_axis_map.get(str(d).lower(), str(d)[:1].upper()) for d in tensor.dims } - bioio_data = bioio_data.rename(dim_map) # pyright: ignore[reportUnknownVariableType] + bioio_data = bioio_data.rename(dim_map) assert isinstance(bioio_data, xr.DataArray) if not all(str(d) in bioio_axis_letters for d in bioio_data.dims): diff --git a/src/bioimageio/core/sample.py b/src/bioimageio/core/sample.py index 336963c5..defa5e7c 100644 --- a/src/bioimageio/core/sample.py +++ b/src/bioimageio/core/sample.py @@ -85,380 +85,14 @@ def __postinit__(self): if t.name is None: t.name = str(m) - # def chunk( - # self, - # chunk_shapes: PerMember[PerAxis[int]], - # *, - # halo: PerMember[PerAxis[HaloLike]] | None = None, - # pad_mode: PadMode | PerMember[PadMode] = "symmetric", - # ) -> Self: - # """Return a new sample with chunked sample members. - - # Args: - # chunk_shapes: The chunk shapes per member and axis. - # """ - # return self.__class__( - # members=chunk_tensors( - # self.members, chunk_shapes=chunk_shapes, halo=halo, pad_mode=pad_mode - # ), - # stat=self.stat, - # id=self.id, - # ) - - # def chunkwise( - # self, - # func: Callable[[Sample], Sample], - # ) -> Self: - # """Apply a function to each chunk of the chunked sample. - - # See [chunk][] for chunking a sample. - - # Args: - # func: The function to apply to each sample chunk. - # """ - # return self.__class__( - # id=self.id, - # members=chunkwise(self.members, func), - # stat=self.stat, - # ) - - # def split_into_blocks( - # self, - # block_shapes: PerMember[PerAxis[int]] | PerAxis[int], - # *, - # halo: PerMember[PerAxis[HaloLike]], - # broadcast: bool = False, - # ) -> tuple[BlockLocShape, PerMember[BlockArray]]: - # """Split multiple shapes (e.g. sample member shapes) into aligned blocks. - - # Args: - # block_shapes: the block shape per sample member (depending on `broaadcast` the block shapes must result in the same number of blocks for all sample members) - # halo: the halo around each block per sample member - # broadcast: whether to broadcast block shapes of members resulting in fewer blocks to match other sample members. - # """ - - # if unknown_blocks := [t for t in block_shapes if t not in self.shape]: - # raise ValueError( - # f"block shape specified for unknown tensors: {unknown_blocks}." - # ) - - # if not block_shapes: - # b_shapes = self.shape - # elif is_mapping(block_shapes, v_type=int): - # b_shapes = {m: block_shapes for m in self.shape} - # else: - # assert not any(isinstance(v, int) for v in block_shapes.values()) - # b_shapes: PerMember[PerAxis[int]] = block_shapes # pyright: ignore[reportAssignmentType] - - # del block_shapes - - # if not broadcast and ( - # missing_blocks := [t for t in self.shape if t not in b_shapes] - # ): - # raise ValueError( - # f"No block shape specified for {missing_blocks}." - # + " Set `broadcast` to True if these tensors should be repeated" - # + " as a whole for each block." - # ) - - # if extra_halo := [t for t in halo if t not in b_shapes]: - # raise ValueError( - # f"`halo` specified for tensors without block shape: {extra_halo}." - # ) - - # blocks: PerMember[BlockArray] = {} - # block_locs: PerMember[BlockLocShape] = {} - # for m in b_shapes: - # block_locs[m], blocks[m] = self._split_member_into_blocks( - # member=m, - # block_shape=b_shapes[m], - # halo=halo.get(m, {}), - # ) - # assert all(b > 0 for b in block_locs[m]), block_locs - - # assert len(blocks) > 0, blocks - # assert len(block_locs) > 0, block_locs - # unique_block_locs = set(block_locs.values()) - - # if len(unique_block_locs) != 1: - # if not broadcast: - # raise ValueError( - # f"Mismatch for number of blocks per dimension: { {t: b.shape for t, b in blocks.items()} }. Set `broadcast` to True if this is expected." - # ) - - # blocks = dict(zip(blocks.keys(), np.broadcast_arrays(*blocks.values()))) - # block_locs = {m: b.shape for m, b in blocks.items()} - # unique_block_locs = set(block_locs.values()) - # assert len(unique_block_locs) == 1, ( - # f"Mismatch for total number of blocks (after braodcasting?!): {block_locs}" - # ) - - # return next(iter(unique_block_locs)), blocks - - # def foo(*bs: BlockMeta) -> SampleBlockMeta: - # return SampleBlockMeta.from_blocks(dict(zip(blocks.keys(), bs))) - - # foo_op = np.frompyfunc(foo, len(blocks), 1) - # out = foo_op(*blocks.values()) - # assert out.shape == next(iter(blocks.values())).shape - # return n, out - - # @property - # def batch_multi_index(self) -> MultiIndex | None: - # """Return the batch multi-index of the sample, if it has one. - - # Returns: - # The batch multi-index of the sample, or `None` if the sample does not have a batch dimension. - # """ - # if not self.members: - # return None - - # for tensor in self.members.values(): - # idx = tensor.batch_multi_index - # if isinstance(idx, MultiIndex): - # return idx - - # return None - - # def set_block(self, block: SampleBlock) -> None: - # """Set values of `block`. - - # Note: - # - Updates only existing sample members (extra block members are ignored) - # - Ignores missing block members (i.e. members in the sample but not in the block are not modified) - - # Raises: - # ValueError if block and sample members do not overlap at all. - # """ - # no_overlap = True - # for m in self.members: - # if m not in block.blocks: - # continue - # b = block.blocks[m] - # self.members[m][b.inner_slice] = b.inner_data - # no_overlap = False - - # if no_overlap: - # raise ValueError( - # f"block with members {list(block.blocks)} does not overlap with sample members {list(self.members)}" - # ) - @property def shape(self) -> PerMember[PerAxis[int]]: return {tid: t.sizes for tid, t in self.members.items()} - # @property - # def unstacked_batch_shape(self) -> PerAxis[int] | None: - # """The shape of the unstacked batch axis - # For example `{"t": 10, "z": 5}` for a sample with stacked "t" and "z" dimensions resulting in a batch length of 50). - # Note: As the batch axis has to match across members this is the same for all members.""" - # if (idx := self.batch_multi_index) is None: - # return None - - # return { - # BATCH_AXIS_ID if a == "original_batch" else AxisId(a): s - # for a, s in zip(idx.names, idx.levshape) - # } - - # @property - # def unstacked_shape(self) -> PerMember[PerAxis[int]]: - # """combined (stacked) shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" - # return { - # m: get_unstacked_shape( - # sh, - # unstacked_dim_order=self.unstacked_dim_order[m] - # if self.unstacked_dim_order is not None - # else None, - # unstacked_batch_shape=self.unstacked_batch_shape, - # ) - # for m, sh in self.shape.items() - # } - def as_arrays(self) -> dict[MemberId, NDArray[Any]]: """Return sample as dictionary of arrays.""" return {m: t.to_numpy() for m, t in self.members.items()} - # def split_into_blocks_meta( - # self, - # block_shapes: PerMember[PerAxis[int]], - # halo: PerMember[PerAxis[HaloLike]], - # broadcast: bool = False, - # ) -> tuple[BlockLocShape, NDArray[Any]]: # really NDArray[SampleBlockMeta] - # assert not (missing := [m for m in block_shapes if m not in self.members]), ( - # f"`block_shapes` specified for unknown members: {missing}" - # ) - # assert not (missing := [m for m in halo if m not in block_shapes]), ( - # f"`halo` specified for members without `block_shape`: {missing}" - # ) - - # n, blocks = split_multiple_shapes_into_blocks( - # shapes=self.shape, - # unstacked_batch_shape=self.unstacked_batch_shape, - # unstacked_dim_order=self.unstacked_dim_order, - # block_shapes=block_shapes, - # halo=halo, - # broadcast=broadcast, - # ) - # return n, sample_block_meta_generator(blocks, sample_id=self.id) - - # def split_into_blocks( - # self, - # block_shapes: PerMember[PerAxis[int]], - # halo: PerMember[PerAxis[HaloLike]], - # pad_mode: PadMode | PerMember[PadMode], - # broadcast: bool = False, - # ) -> tuple[BlockLocShape, Iterable[SampleBlockWithOrigin]]: - # n_blocks, blocks = self.split_into_blocks_meta(block_shapes, halo, broadcast) - # return n_blocks, sample_block_generator( - # blocks.flatten(), origin=self, pad_mode=pad_mode - # ) - - # def get_blocks( - # self, - # block_shapes: PerMember[PerAxis[int]], - # *, - # halo: PerMember[PerAxis[HaloLike]], - # pad_mode: PadMode | PerMember[PadMode], - # broadcast: bool = False, - # ) -> tuple[BlockLocShape, PerMember[BlockMetaArray]]: - # n_blocks, blocks = self.split_into_blocks_meta(block_shapes, halo, broadcast) - # return n_blocks, sample_block_generator( - # blocks.flatten(), origin=self, pad_mode=pad_mode - # ) - - # def as_single_block(self, halo: PerMember[PerAxis[Halo]] | None = None): - # if halo is None: - # halo = {} - - # return SampleBlockWithOrigin( - # sample_shape=self.shape, - # sample_id=self.id, - # blocks={ - # m: Block( - # sample_shape=self.shape[m], - # unstacked_sample_batch_shape=self.unstacked_batch_shape, - # unstacked_dim_order=self.unstacked_dim_order[m] - # if self.unstacked_dim_order is not None - # else None, - # data=data, - # inner_slice={a: SliceInfo(0, s) for a, s in self.shape[m].items()}, - # unstacked_batch_slice=None - # if self.unstacked_batch_shape is None - # else { - # a: SliceInfo(0, s) - # for a, s in self.unstacked_batch_shape.items() - # }, - # halo=halo.get(m, {}), - # block_loc=0, - # block_loc_shape=1, - # ) - # for m, data in self.members.items() - # }, - # stat=self.stat, - # origin=self, - # block_loc=0, - # blocks_loc_shape=1, - # ) - - # @classmethod - # def from_blocks( - # cls, - # sample_blocks: Iterable[SampleBlock], - # *, - # fill_value: float = float("nan"), - # ) -> Self: - # """Create a `Sample` from an iterable of `SampleBlock`s. - - # Note: - # All sample blocks must have the same `sample_id`. - - # Args: - # sample_blocks: The blocks to create the sample from. - # fill_value: The value to fill missing values with (default: `nan`). - # """ - # output = None - # for output in cls.from_blocks_yield_intermediates( - # sample_blocks, fill_value=fill_value - # ): - # pass - - # if output is None: - # raise ValueError("no sample blocks provided") - - # return output - - # @classmethod - # def from_blocks_yield_intermediates( - # cls, - # sample_blocks: Iterable[SampleBlock], - # *, - # fill_value: float = float("nan"), - # ): - # """Create a `Sample` from an iterable of `SampleBlock`s, yielding the intermediate sample after each block. - - # Args: - # sample_blocks: The blocks to create the sample from. - # fill_value: The value to fill missing values with (default: `nan`). - # """ - # output = None - # for sample_block in sample_blocks: - # if output is None: - # output = cls( - # members={}, - # stat={}, - # id=None, - # unstacked_dim_order=sample_block.unstacked_dim_order, - # ) - # if output.id is None: - # output.id = sample_block.sample_id - # else: - # assert output.id == sample_block.sample_id, ( - # "sample id changed between sample blocks" - # ) - - # output.stat = sample_block.stat - - # for m, block in sample_block.blocks.items(): - # if m not in output.members: - # if -1 in block.sample_shape.values(): - # raise NotImplementedError( - # "merging blocks with data dependent axis not yet implemented" - # ) - - # if ( - # block.unstacked_sample_batch_shape is not None - # and block.unstacked_dim_order is not None - # ): - # sizes = { - # **block.sample_shape, - # **block.unstacked_sample_batch_shape, - # } - # full_unstacked_shape = { - # a: sizes[a] for a in block.unstacked_dim_order - # } - # t = create_tensor_with_multiindex( - # full_unstacked_shape, - # fill_value=fill_value, - # dtype=np.dtype(block.data.dtype), - # transpose_to=tuple(block.sample_shape.keys()), - # ) - # else: - # t = Tensor( - # np.full( - # shape=tuple(block.sample_shape.values()), - # fill_value=fill_value, - # dtype=block.data.dtype, - # ), - # dims=tuple(block.sample_shape.keys()), - # ) - - # output.members[m] = t - - # output.members[m][block.inner_slice] = block.inner_data - # yield output - - # yield output - def pad( self, pad_width: PerMember[PerAxis[int | PadWidthLike]], @@ -484,89 +118,6 @@ def pad( id=self.id, ) - # def transpose( - # self, - # axes: PerMember[Sequence[AxisId]], - # *, - # extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "raise", - # missing_dims: Literal[ - # "raise", "expand", "unstack", "unstack_or_expand" - # ] = "raise", - # ) -> Self: - # """Return a new sample with transposed sample members. - - # Raises: - # ValueError: If not all batch dimensions have the same length after transposition (and possibly stacking/unstacking extra dimensions). - - # """ - # if any(unknown := [m not in self.members for m in axes]): - # raise ValueError(f"Axes specified for unknown members: {unknown}") - - # members: dict[MemberId, Tensor] = {} - # individual_unstacked_dim_order: dict[MemberId, list[AxisId] | None] = {} - # for m, t in self.members.items(): - # if m in axes: - # members[m], individual_unstacked_dim_order[m] = ( - # t.transpose_and_return_unstacked_dim_order( - # axes=axes[m], - # extra_dims=extra_dims, - # missing_dims=missing_dims, - # ) - # ) - # else: - # members[m] = t - # individual_unstacked_dim_order[m] = None - - # # if any(order is None for order in individual_unstacked_dim_order.values()): - # # unstacked_dim_order = None - # # else: - # # unstacked_dim_order = { - # # m: order - # # for m, order in individual_unstacked_dim_order.items() - # # if order is not None - # # } - - # if ( - # len( - # batch_lengths := { - # t.sizes[AxisId("batch")] - # for t in members.values() - # if AxisId("batch") in t.dims - # } - # ) - # > 1 - # ): - # raise ValueError( - # f"Transposed sample members have incompatible batch lengths: {batch_lengths}." - # ) - - # return self.__class__(members=members, stat=dict(self.stat), id=self.id) - - # def assign_batch_multi_index(self, multi_index: MultiIndex) -> Self: - # """Return a new sample with the batch multi-index assigned to all sample members. - - # Raises: - # ValueError: If not all sample members have a batch dimension. - # """ - # if len( - # no_batch := [ - # m for m, t in self.members.items() if AxisId("batch") not in t.dims - # ] - # ) == len(self.members): - # raise ValueError(f"No member has a batch dimension: {no_batch}") - - # return self.__class__( - # members={ - # m: t - # if AxisId("batch") not in t.dims - # else t.assign_batch_multi_index(multi_index) - # for m, t in self.members.items() - # }, - # stat=dict(self.stat), - # id=self.id, - # unstacked_dim_order=self.unstacked_dim_order, - # ) - def unstack_batch_multi_index( self, *, @@ -634,120 +185,3 @@ def unstack_batch_multi_index( stat=stat, id=self.id, ) - - -# @dataclass(frozen=True) -# class SampleBlock(SampleBlockBase[Block]): -# """A block of a dataset sample""" - -# blocks: dict[MemberId, Block] -# """Individual tensor blocks comprising this sample block""" - -# stat: Stat -# """computed statistics""" - -# @property -# def members(self) -> PerMember[Tensor]: -# """the sample block's tensors""" -# return {m: b.data for m, b in self.blocks.items()} - -# @classmethod -# def from_meta( -# cls, meta: SampleBlockMeta, data: PerMember[Tensor], *, stat: Stat -# ) -> SampleBlock: -# return cls( -# sample_shape={ -# m: { -# a: data[m].tagged_shape[a] if s == -1 else s -# for a, s in member_shape.items() -# } -# for m, member_shape in meta.sample_shape.items() -# }, -# sample_id=meta.sample_id, -# blocks={ -# m: Block.from_meta(b, data=data[m]) for m, b in meta.blocks.items() -# }, -# stat=stat, -# block_loc=meta.block_loc, -# blocks_loc_shape=meta.blocks_loc_shape, -# ) - -# def get_meta(self) -> SampleBlockMeta: -# return SampleBlockMeta( -# sample_id=self.sample_id, -# blocks={m: b.get_meta() for m, b in self.blocks.items()}, -# sample_shape=self.sample_shape, -# block_loc=self.block_loc, -# blocks_loc_shape=self.blocks_loc_shape, -# ) - -# def as_sample(self) -> Sample: -# """Convert this sample block to a `Sample` with the shape of this block. - -# Note: -# If you want to convert one or more sample block to a sample with the shape of the original, whole sample, -# use `Sample.from_blocks()` instead. -# """ -# return Sample( -# members=dict(self.members), -# stat=dict(self.stat), -# id=self.sample_id, -# unstacked_dim_order=self.unstacked_dim_order, -# ) - -# @property -# def unstacked_dim_order(self) -> PerMember[Sequence[AxisId] | None]: -# return {m: b.unstacked_dim_order for m, b in self.blocks.items()} - -# @property -# def batch_multi_index(self) -> MultiIndex | None: -# """Return the batch multi-index of the sample block, if it has one. - -# Returns: -# The batch multi-index of the sample block, or `None` if the sample block does not have a multi-index batch dimension. -# """ -# for tensor in self.members.values(): -# idx = tensor.batch_multi_index -# if idx is not None: -# return idx - -# return None - - -# @dataclass(frozen=True) -# class SampleBlockWithOrigin(SampleBlock): -# """A `SampleBlock` with a reference (`origin`) to the whole `Sample`""" - -# origin: Sample -# """the sample this sample block was taken from""" - - -# def sample_block_generator( -# blocks: Iterable[PerMember[BlockMeta] | SampleBlockMeta], -# *, -# origin: Sample, -# pad_mode: PadMode | PerMember[PadMode], -# ) -> Iterable[SampleBlockWithOrigin]: -# for member_blocks in blocks: -# if isinstance(member_blocks, SampleBlockMeta): -# member_blocks = member_blocks.blocks - -# cons = _ConsolidatedMemberBlocks(member_blocks) -# yield SampleBlockWithOrigin( -# blocks={ -# m: Block.from_sample_member( -# origin.members[m], -# block=member_blocks[m], -# pad_mode=pad_mode.get(m, "symmetric") -# if isinstance(pad_mode, collections.abc.Mapping) -# else pad_mode, -# ) -# for m in origin.members -# }, -# sample_shape=origin.shape, -# origin=origin, -# stat=origin.stat, -# sample_id=origin.id, -# block_loc=cons.block_index, -# blocks_loc_shape=cons.blocks_in_sample, -# ) diff --git a/src/bioimageio/core/sample_meta.py b/src/bioimageio/core/sample_meta.py deleted file mode 100644 index 3e434be9..00000000 --- a/src/bioimageio/core/sample_meta.py +++ /dev/null @@ -1,287 +0,0 @@ -# from __future__ import annotations - -# from collections.abc import Sequence -# from dataclasses import dataclass -# from math import ceil, floor -# from types import MappingProxyType -# from typing import ( -# Any, -# Callable, -# Generic, -# TypeVar, -# ) - -# import numpy as np -# import pydantic -# from numpy.typing import NDArray -# from typing_extensions import Self, assert_never - -# from bioimageio.core._utils.multi_index import get_unstacked_dim_order -# from bioimageio.core._utils.type_guards import is_mapping - -# from ._common_annotations import PerMemberAnno -# from .axis import AxisId, PerAxis -# from .block_meta import ( -# BlockMeta, -# LinearAxisTransform, -# ) -# from .common import ( -# BlockLoc, -# BlockLocShape, -# Halo, -# MemberId, -# PerMember, -# SampleId, -# SliceInfo, -# ) - -# BlockT = TypeVar("BlockT", bound=BlockMeta) - - -# @pydantic.dataclasses.dataclass(frozen=True) -# class SampleBlockBase(Generic[BlockT]): -# """base class for `SampleBlockMeta` and `SampleBlock`""" - -# sample_shape: PerMemberAnno[PerAxis[int]] -# """the sample shape this block represents a part of""" - -# sample_id: SampleId -# """identifier for the sample within its dataset""" - -# blocks: PerMemberAnno[BlockT] -# """Individual tensor blocks comprising this sample block""" - -# block_loc: BlockLoc -# """the location of the block within the sample""" - -# blocks_loc_shape: BlockLocShape -# """the sample shape in number of blocks""" - -# @property -# def shape(self) -> PerMember[PerAxis[int]]: -# return MappingProxyType({mid: b.shape for mid, b in self.blocks.items()}) - -# @property -# def inner_shape(self) -> PerMember[PerAxis[int]]: -# return MappingProxyType({mid: b.inner_shape for mid, b in self.blocks.items()}) - -# @property -# def unstacked_sample_batch_shape(self) -> PerAxis[int] | None: -# """The shape of the unstacked batch axis, if the sample has a multi-index batch axis""" -# for b in self.blocks.values(): -# if b.unstacked_sample_batch_shape is not None: -# return b.unstacked_sample_batch_shape - -# return None - -# @property -# def unstacked_dim_order(self) -> PerMemberAnno[Sequence[AxisId] | None] | None: -# """The dim order of the unstacked tensors, if the sample has a multi-index batch axis""" -# for b in self.blocks.values(): -# if b.unstacked_dim_order is not None: -# return MappingProxyType( -# {mid: bb.unstacked_dim_order for mid, bb in self.blocks.items()} -# ) - -# return None - -# @property -# def unstacked_sample_shape(self) -> PerMemberAnno[PerAxis[int]]: -# """combined (stacked) sample shape with the unstacked sample batch shape in order of unstacked_dim_order (if available)""" -# return MappingProxyType( -# {mid: b.unstacked_sample_shape for mid, b in self.blocks.items()} -# ) - - -# @dataclass -# class LinearSampleAxisTransform(LinearAxisTransform): -# member: MemberId - - -# @pydantic.dataclasses.dataclass(frozen=True) -# class SampleBlockMeta(SampleBlockBase[BlockMeta]): -# """Meta data of a dataset sample block""" - -# # @classmethod -# # def from_blocks(cls, meta: PerMember[BlockMeta]) -> Self: -# # sample_ids = { -# # b.sample_id for b in meta.values() if b.sample_id is not None -# # } or {"unknown"} -# # block_loc_shapes = {b.block_loc_shape for b in meta.values()} -# # if len(block_loc_shapes) != 1: -# # raise ValueError( -# # f"Mismatch for block_loc_shape (sample shape in blocks) across members: {block_loc_shapes}" -# # ) -# # block_locs = {b.block_loc for b in meta.values()} -# # if len(block_locs) != 1: -# # raise ValueError( -# # f"Mismatch for block_loc (block location within sample) across members: {block_locs}" -# # ) -# # return cls( -# # sample_shape={m: b.sample_shape for m, b in meta.items()}, -# # sample_id=next(iter(sample_ids)), -# # blocks=meta, -# # block_loc=next(iter(block_locs)), -# # blocks_loc_shape=next(iter(block_loc_shapes)), -# # ) - -# def get_transformed( -# self, new_axes: PerMember[PerAxis[LinearSampleAxisTransform | int]] -# ) -> Self: -# sample_shape = { -# m: { -# a: ( -# trf -# if isinstance(trf, int) -# else trf.compute(self.sample_shape[trf.member][trf.axis]) -# ) -# for a, trf in new_axes[m].items() -# } -# for m in new_axes -# } - -# def get_member_halo(m: MemberId, round: Callable[[float], int]): -# return { -# a: ( -# Halo(0, 0) -# if isinstance(trf, int) -# or trf.axis not in self.blocks[trf.member].halo -# else Halo( -# round(self.blocks[trf.member].halo[trf.axis].left * trf.scale), -# round(self.blocks[trf.member].halo[trf.axis].right * trf.scale), -# ) -# ) -# for a, trf in new_axes[m].items() -# } - -# halo: dict[MemberId, dict[AxisId, Halo]] = {} -# for m in new_axes: -# halo[m] = get_member_halo(m, floor) -# if halo[m] != get_member_halo(m, ceil): -# raise ValueError( -# f"failed to unambiguously scale halo {halo[m]} with {new_axes[m]}" -# + f" for {m}." -# ) - -# inner_slice = { -# m: { -# a: ( -# SliceInfo(0, trf) -# if isinstance(trf, int) -# else SliceInfo( -# trf.compute( -# self.blocks[trf.member].inner_slice[trf.axis].start -# ), -# trf.compute(self.blocks[trf.member].inner_slice[trf.axis].stop), -# ) -# ) -# for a, trf in new_axes[m].items() -# } -# for m in new_axes -# } -# unstacked_batch_slice = None -# for b in self.blocks.values(): -# if b.unstacked_batch_slice is None: -# continue -# elif unstacked_batch_slice is None: -# unstacked_batch_slice = b.unstacked_batch_slice -# else: -# assert unstacked_batch_slice == b.unstacked_batch_slice, ( -# "Incompatible unstacked batch slices across sample block members" -# ) - -# unstacked_dim_order: dict[MemberId, list[AxisId] | None] = {} -# for m, trfs in new_axes.items(): -# for trf in trfs.values(): -# if isinstance(trf, LinearSampleAxisTransform): -# unstacked_in_order = self.blocks[trf.member].unstacked_dim_order -# stacked_dims = list( -# self.blocks[trf.member].unstacked_sample_batch_shape or () -# ) -# unstacked_dim_order[m] = get_unstacked_dim_order( -# list(trfs), -# unstacked_reference_order=unstacked_in_order, -# stacked_dims=stacked_dims, -# ) -# break -# elif isinstance(trf, int): -# pass -# else: -# assert_never(trf) -# else: -# unstacked_dim_order[m] = None - -# return self.__class__( -# blocks={ -# m: BlockMeta( -# sample_shape=sample_shape[m], -# sample_id=self.sample_id, -# unstacked_sample_batch_shape=self.unstacked_sample_batch_shape, -# unstacked_dim_order=unstacked_dim_order[m], -# inner_slice=inner_slice[m], -# unstacked_batch_slice=unstacked_batch_slice, -# halo=halo[m], -# block_loc=self.block_loc, -# block_loc_shape=self.blocks_loc_shape, -# ) -# for m in new_axes -# }, -# sample_shape=sample_shape, -# sample_id=self.sample_id, -# block_loc=self.block_loc, -# blocks_loc_shape=self.blocks_loc_shape, -# ) - - -# class _ConsolidatedMemberBlocks: -# def __init__(self, blocks: PerMember[BlockMeta]): -# super().__init__() -# block_indices = {b.block_loc for b in blocks.values()} -# assert len(block_indices) == 1 -# self.block_index = block_indices.pop() -# blocks_in_samples = {b.block_loc_shape for b in blocks.values()} -# assert len(blocks_in_samples) == 1 -# self.blocks_in_sample = blocks_in_samples.pop() - - -# def sample_block_meta_generator( -# blocks: NDArray[Any], # NDArray[PerMember[BlockMeta]], -# *, -# sample_id: SampleId, -# ): -# sample_shape: dict[MemberId, PerAxis[int]] = {} -# # out = np.empty(blocks.shape, dtype=object).flatten() -# out: list[SampleBlockMeta] = [] - -# # member_blocks: PerMember[BlockMeta] -# for member_blocks in blocks.flat: # pyright: ignore[reportUnknownVariableType] -# assert is_mapping(member_blocks, MemberId, BlockMeta) -# cons = _ConsolidatedMemberBlocks(member_blocks) -# unstacked_batch_shape = None -# for m, b in member_blocks.items(): -# if b.unstacked_sample_batch_shape is not None: -# if unstacked_batch_shape is None: -# unstacked_batch_shape = b.unstacked_sample_batch_shape -# else: -# if unstacked_batch_shape != b.unstacked_sample_batch_shape: -# raise ValueError( -# f"Unstacked batch shape mismatch for member {m}: " -# + f"{unstacked_batch_shape} != {b.unstacked_sample_batch_shape}" -# ) - -# if m not in sample_shape: -# sample_shape[m] = b.sample_shape -# else: -# assert b.sample_shape == sample_shape[m] - -# out.append( -# SampleBlockMeta( -# blocks=dict(member_blocks), -# sample_shape=sample_shape, -# sample_id=sample_id, -# block_loc=cons.block_index, -# blocks_loc_shape=cons.blocks_in_sample, -# ) -# ) - -# return np.asarray(out, dtype=object).reshape(blocks.shape) diff --git a/tests/test_multi_index.py b/tests/test_multi_index.py index 5ce45a72..8688304f 100644 --- a/tests/test_multi_index.py +++ b/tests/test_multi_index.py @@ -8,6 +8,7 @@ from numpy.typing import NDArray from bioimageio.core import AxisId +from bioimageio.core._utils.type_hints import cast_and_assert from bioimageio.core.tensor import MultiIndex, Tensor UNSTACKED_SHAPE = {AxisId("batch"): 2, AxisId("z"): 3, AxisId("x"): 4} @@ -56,7 +57,10 @@ def test_stack_and_unstack_batch_multi_index(backend: ArrayBackend): def test_assign_batch_multi_index_to_stacked_data(backend: ArrayBackend): data = np.arange(24, dtype=np.float32).reshape(6, 4) tensor = Tensor.from_array(_array(backend, data), dims=TRANSPOSE_TO) - index = MultiIndex.from_product([range(2), range(3)], names=["original_batch", "z"]) + index = cast_and_assert( + MultiIndex, + MultiIndex.from_product([range(2), range(3)], names=["original_batch", "z"]), + ) indexed = tensor.assign_batch_multi_index(index) @@ -94,7 +98,9 @@ def test_stack_without_original_batch(backend: ArrayBackend): @ARRAY_BACKENDS def test_multi_index_rejects_incompatible_axes(backend: ArrayBackend): tensor = Tensor.from_array(_array(backend, np.zeros((3, 4))), dims=("z", "x")) - index = MultiIndex.from_product([range(3)], names=["z"]) + index = cast_and_assert( + MultiIndex, MultiIndex.from_product([range(3)], names=["z"]) + ) with pytest.raises(ValueError, match="without a 'batch' axis"): _ = tensor.assign_batch_multi_index(index) diff --git a/tests/test_prediction.py b/tests/test_prediction.py index 24e68647..4f4390fd 100644 --- a/tests/test_prediction.py +++ b/tests/test_prediction.py @@ -173,7 +173,7 @@ def test_predict_z_as_batch(unet2d_nuclei_broad_model: str): def test_transpose_sample_for_model_stacks_extra_z_to_batch( unet2d_nuclei_broad_model: str, ): - from bioimageio.core._utils.multi_index import MultiIndex + from bioimageio.core.tensor import MultiIndex model = load_model( unet2d_nuclei_broad_model, perform_io_checks=False, format_version="latest" @@ -188,7 +188,6 @@ def test_transpose_sample_for_model_stacks_extra_z_to_batch( MemberId("raw"): Tensor(data, dims=["batch", "channel", "z", "y", "x"]) }, stat=input_sample.stat, - unstacked_dim_order=input_sample.unstacked_dim_order, ) transposed = transpose_sample_for_model(sample_with_z, model) diff --git a/tests/test_stat_calculators.py b/tests/test_stat_calculators.py index e0941dd3..e2494f28 100644 --- a/tests/test_stat_calculators.py +++ b/tests/test_stat_calculators.py @@ -24,12 +24,7 @@ def create_random_dataset(tid: MemberId, axes: tuple[AxisId, ...]): sizes = list(range(n, len(axes) + n)) data = np.asarray(np.random.rand(*sizes)) ds = [ - Sample( - members={tid: Tensor(data[i : i + 1], dims=axes)}, - stat={}, - id=None, - unstacked_dim_order=None, - ) + Sample(members={tid: Tensor(data[i : i + 1], dims=axes)}, stat={}, id=None) for i in range(n) ] return Tensor(data, dims=axes), ds diff --git a/tests/test_stat_measures.py b/tests/test_stat_measures.py index c108aec1..86001b00 100644 --- a/tests/test_stat_measures.py +++ b/tests/test_stat_measures.py @@ -65,9 +65,7 @@ def test_individual_percentile_measure( data = Tensor( np.random.random((5, 6, 3)), dims=(AxisId("x"), AxisId("y"), AxisId("c")) ) - actual = calc.compute( - Sample(members={tid: data}, stat={}, id=None, unstacked_dim_order=None) - ) + actual = calc.compute(Sample(members={tid: data}, stat={}, id=None)) for m in measures: expected = data.quantile(q=m.q, dim=m.axes, method=m.method) actual_data = actual[m] From b42469aae5c2cd0164dacd0b016ef0bfebb6f74e Mon Sep 17 00:00:00 2001 From: fynnbe Date: Tue, 13 Oct 2026 16:45:10 +0200 Subject: [PATCH 33/85] WIP improve laziness --- README.md | 9 +- aidecl.yaml | 38 ++ pyproject.toml | 3 +- src/bioimageio/core/_model_adapter.py | 51 ++- src/bioimageio/core/_prediction_pipeline.py | 36 +- src/bioimageio/core/_sample_serializer.py | 118 ++++++- src/bioimageio/core/_settings.py | 6 +- src/bioimageio/core/_utils/statistics.py | 29 ++ src/bioimageio/core/block.py | 12 +- src/bioimageio/core/cli.py | 2 +- src/bioimageio/core/io.py | 139 +++++--- src/bioimageio/core/prediction.py | 136 +++++++- .../core/remote_backends/gradio/serializer.py | 41 ++- src/bioimageio/core/sample.py | 26 +- src/bioimageio/core/stat_calculators.py | 197 ++++++----- src/bioimageio/core/stat_measures.py | 4 +- src/bioimageio/core/tensor.py | 18 +- tests/test_io.py | 49 ++- tests/test_prediction.py | 7 +- .../test_gradio_serializer.py | 112 ++++++ tests/test_sample_serializer.py | 151 ++++++++ tests/test_stat_calculators.py | 327 +++++++++++++++++- tests/test_tensor.py | 36 ++ 23 files changed, 1323 insertions(+), 224 deletions(-) create mode 100644 aidecl.yaml create mode 100644 src/bioimageio/core/_utils/statistics.py create mode 100644 tests/test_remote_backends/test_gradio_serializer.py create mode 100644 tests/test_sample_serializer.py diff --git a/README.md b/README.md index 60e26018..e97b0571 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ and compute selected dataset statistics used for preprocessing. [Here you find the bioimageio.core documentation.](https://bioimage-io.github.io/core-bioimage-io-python) -#### Examples +### Examples Notebooks that save and load resource descriptions and validate their format (using bioimageio.spec, a dependency of bioimageio.core)