diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml
index e0be2de4c..28c32f579 100644
--- a/.github/workflows/build.yaml
+++ b/.github/workflows/build.yaml
@@ -69,23 +69,22 @@ jobs:
fail-fast: false
matrix:
include:
- - python-version: '3.9'
- numpy-version: 1
- - python-version: '3.9'
- numpy-version: 2
- python-version: '3.10'
- run-expensive-tests: true
- report-coverage: true
- save-cache: true
numpy-version: 1
- python-version: '3.11'
numpy-version: 2
- python-version: '3.12'
numpy-version: 1
- - python-version: '3.12'
- numpy-version: 2
spec-from-main: true
- # - python-version: '3.13'
+ - python-version: '3.13'
+ numpy-version: 2
+ run-expensive-tests: true
+ report-coverage: true
+ save-cache: true
+ # - python-version: '3.14'
+ # numpy-version: 2
+ # spec-from-main: true
+ # - python-version: '3.15'
# numpy-version: 2
steps:
@@ -99,6 +98,7 @@ jobs:
run: |
pip install --upgrade pip
pip install -e .[dev,partners] numpy==${{matrix.numpy-version}}.*
+ pip install git+https://github.com/ilastik/clearscale.git # TODO: remove once clearscale is in dev deps
- name: Pyright
if: matrix.run-expensive-tests # pyright is not expensive, but we only want to run it once due to otherwise inconsistent typing
run: |
@@ -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 }}
@@ -252,7 +252,7 @@ jobs:
path: dist
- uses: actions/setup-python@v6
with:
- python-version: '3.12'
+ python-version: '3.13'
cache: 'pip'
- run: pip install -e .[dev,docs,partners]
- name: Check doc scripts
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 196c94bfb..b201e40cc 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -1,15 +1,23 @@
repos:
- - repo: https://github.com/astral-sh/ruff-pre-commit
- rev: v0.12.8
- hooks:
- - id: ruff-check
- args: [--fix, --show-fixes]
- repo: local
hooks:
+ - id: ruff-check
+ name: ruff check
+ entry: ruff check --fix --show-fixes
+ language: system
+ pass_filenames: true
+ files: ^.*\.py$
+ always_run: false
+ - id: ruff-format
+ name: ruff format
+ entry: ruff format
+ language: system
+ pass_filenames: true
+ files: ^.*\.py$
+ always_run: false
- id: pyright
name: pyright
entry: pyright
language: system
- always_run: true
+ always_run: false
pass_filenames: true
- files: ^.*\.py$
diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 000000000..7bcab916c
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,22 @@
+{
+ // 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 / Tests",
+ "type": "debugpy",
+ "request": "launch",
+ "program": "${file}",
+ "purpose": [
+ "debug-test"
+ ],
+ "justMyCode": false,
+ "env": {
+ // The monitoring backend skips project files named io.py.
+ "PYDEVD_USE_SYS_MONITORING": "0"
+ }
+ }
+ ]
+}
diff --git a/.vscode/settings.json b/.vscode/settings.json
index a61bc3d9b..d79e78683 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -1,25 +1,34 @@
-{
- "window.title": "bioimageio.core",
- "python.analysis.languageServerMode": "full",
- "python.testing.unittestArgs": [
- "-v",
- "-s",
- "./tests",
- "-p",
- "test_*.py"
- ],
- "python.testing.pytestEnabled": true,
- "python.testing.unittestEnabled": false,
- "python-envs.pythonProjects": [
- {
- "path": "",
- "envManager": "ms-python.python:conda",
- "packageManager": "ms-python.python:conda"
- }
- ],
- "editor.formatOnSave": true,
- "editor.codeActionsOnSave": {
- "source.unusedImports": "explicit",
- "source.organizeImports": "explicit"
- },
-}
+{
+ "window.title": "bioimageio.core",
+ "python.languageServer": "Pylance",
+ "python.analysis.diagnosticMode": "workspace",
+ "python.testing.unittestArgs": [
+ "-v",
+ "-s",
+ "./tests",
+ "-p",
+ "test_*.py"
+ ],
+ "python.testing.pytestEnabled": true,
+ "python.testing.unittestEnabled": false,
+ "editor.formatOnSave": true,
+ "[python]": {
+ "editor.defaultFormatter": "charliermarsh.ruff",
+ "editor.codeActionsOnSave": {
+ "source.unusedImports": "never",
+ "source.organizeImports": "never",
+ "source.fixAll.pylance": "never",
+ "source.fixAll.ruff": "explicit",
+ "source.organizeImports.ruff": "explicit"
+ }
+ },
+ "python-envs.defaultEnvManager": "ms-python.python:conda",
+ "python-envs.defaultPackageManager": "ms-python.python:conda",
+ "python-envs.pythonProjects": [
+ {
+ "path": ".",
+ "envManager": "ms-python.python:conda",
+ "packageManager": "ms-python.python:conda"
+ }
+ ],
+}
diff --git a/README.md b/README.md
index 60e26018a..e97b0571d 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)
@@ -38,10 +38,15 @@ Use the described resources in Python with ="3.10"', '').replace(';python_version<"3.10"', '').lower().replace('_', '-') }}
{% endif %}
{% endfor %}
commands:
diff --git a/conftest.py b/conftest.py
deleted file mode 100644
index d58a89b2f..000000000
--- a/conftest.py
+++ /dev/null
@@ -1,13 +0,0 @@
-from __future__ import annotations
-
-import sys
-from pathlib import Path
-from typing import Any
-
-
-def pytest_ignore_collect(collection_path: Path, config: Any) -> bool:
- if sys.version_info >= (3, 10):
- return False
-
- path = str(collection_path).replace("\\", "/")
- return "/src/bioimageio/core/remote_backends/gradio/" in path
diff --git a/example/dataset_statistics_demo.ipynb b/example/dataset_statistics_demo.ipynb
index 7e9aff6b8..6cba4205f 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/export_cellpose_model/bioimageio_export.py b/example/export_cellpose_model/bioimageio_export.py
index 2f8edf295..eef6b6903 100644
--- a/example/export_cellpose_model/bioimageio_export.py
+++ b/example/export_cellpose_model/bioimageio_export.py
@@ -72,6 +72,7 @@ def sha256sum(path: Path) -> str:
h.update(chunk)
return h.hexdigest()
+
if __name__ == "__main__":
logger.enable("bioimageio")
@@ -192,5 +193,10 @@ def sha256sum(path: Path) -> str:
descr, output_path=Path("output/cpsam_bioimageio.zip")
)
- summary = test_model(descr, working_dir=Path("output/export_test"), absolute_tolerance=1e-3, relative_tolerance=1e-3)
+ summary = test_model(
+ descr,
+ working_dir=Path("output/export_test"),
+ absolute_tolerance=1e-3,
+ relative_tolerance=1e-3,
+ )
summary.display()
diff --git a/example/export_sam3_encoder/bioimageio_export.py b/example/export_sam3_encoder/bioimageio_export.py
index 761b4d20e..de43b35da 100644
--- a/example/export_sam3_encoder/bioimageio_export.py
+++ b/example/export_sam3_encoder/bioimageio_export.py
@@ -27,6 +27,8 @@ def _open(self, name, mode="r", pwd=None, *, force_zip64=False):
return _orig_open(self, name, mode, pwd, force_zip64=force_zip64)
zipfile.ZipFile.open = _open
+
+
from bioimageio.spec.model.v0_5 import (
ArchitectureFromFileDescr,
AxisId,
diff --git a/example/export_sam3_encoder/sam3_encoder_bioimageio.py b/example/export_sam3_encoder/sam3_encoder_bioimageio.py
index 2277f59df..08bab7bf7 100644
--- a/example/export_sam3_encoder/sam3_encoder_bioimageio.py
+++ b/example/export_sam3_encoder/sam3_encoder_bioimageio.py
@@ -28,8 +28,8 @@
from __future__ import annotations
import torch
-import torch.nn as nn
from sam3.model_builder import build_sam3_image_model
+from torch import nn
class Sam3EncoderBioImageIO(nn.Module):
diff --git a/example/export_sam3_encoder/sam3_original.py b/example/export_sam3_encoder/sam3_original.py
index 57f671e73..81b5aa3db 100644
--- a/example/export_sam3_encoder/sam3_original.py
+++ b/example/export_sam3_encoder/sam3_original.py
@@ -23,7 +23,6 @@
import imageio.v3 as imageio
import numpy as np
import torch
-
from sam3_encoder_bioimageio import Sam3EncoderBioImageIO
# SAM 3 was trained at 1008x1008 (patch size 14 -> 72x72 feature grid).
@@ -53,8 +52,12 @@ def resize_to_square(img: np.ndarray, size: int) -> np.ndarray:
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
- parser.add_argument("--checkpoint", default=DEFAULT_CKPT, help="SAM 3 .pt checkpoint")
- parser.add_argument("--image", default="sample_input.png", help="input RGB image")
+ _ = parser.add_argument(
+ "--checkpoint", default=DEFAULT_CKPT, help="SAM 3 .pt checkpoint"
+ )
+ _ = parser.add_argument(
+ "--image", default="sample_input.png", help="input RGB image"
+ )
args = parser.parse_args()
os.chdir(Path(__file__).parent)
diff --git a/example/model_usage.ipynb b/example/model_usage.ipynb
index 2e3db5d1b..9dddc388b 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",
@@ -87,22 +89,21 @@
}
],
"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\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",
- " 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",
@@ -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",
@@ -450,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/pyproject.toml b/pyproject.toml
index 45d8fb39b..eb8374610 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -2,11 +2,12 @@
name = "bioimageio.core"
description = "Python specific core utilities for bioimage.io resources (in particular DL models)."
authors = [{ name = "Fynn Beuttenmüller", email = "thefynnbe@gmail.com" }]
-requires-python = ">=3.9"
+requires-python = ">=3.10"
readme = "README.md"
dynamic = ["version"]
dependencies = [
- "bioimageio.spec ==0.5.12.0",
+ "bioimageio.spec ==0.5.14.4",
+ "dask[array]",
"imagecodecs",
"imageio>=2.10",
"loguru",
@@ -23,7 +24,6 @@ classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
- "Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
@@ -40,11 +40,12 @@ Source = "https://github.com/bioimage-io/core-bioimage-io-python"
[project.optional-dependencies]
onnx = [
+ "onnx",
"onnxruntime",
"onnxscript",
'onnx_ir!=0.1.14;python_version<"3.10"', # uses typing.Concatentate which requires py>=3.10
]
-pytorch = ["torch>=1.6,<3", "torchvision>=0.21", "keras>=3.0,<4"]
+pytorch = ["torch>=1.6,<3", "torchvision>=0.21", "keras>=3.0,<4", "safetensors"]
tensorflow = ["tensorflow", "keras>=2.15,<4", "h5py"]
partners = [
# "biapy", # pins core exactly
@@ -54,34 +55,51 @@ partners = [
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",
+ "bioio-ome-zarr",
+]
+# clearscale = ["clearscale", "zarr"] # TODO: add clearscale extra deps once clearscale is available on pypi
dev = [
- "cellpose<4.2", # for model testing
+ "bioio",
+ "bioio-bioformats",
+ "bioio-imageio",
+ "bioio-ome-tiff",
+ 'bioio-ome-zarr;python_version>="3.11" and python_version<"3.14"', # TODO: wait for bioio-ome-zarr to support py3.14
+ "cellpose<4.2", # for model testing
"crick",
- '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
+ 'onnx;python_version<"3.15"', # TODO: wait for onnx to support py3.15
+ 'onnxruntime;python_version<"3.15"', # TODO: wait for onnxruntime to support py3.15
+ 'onnxscript;python_version<"3.15"', # TODO: wait for onnxscript to support py3.15
"packaging>=17.0",
"pre-commit",
- "pyright==1.1.410",
- "pytest-cov",
+ "pyright==1.1.414",
"pytest",
+ "pytest-cov",
+ "pytest-timeout",
"python-dotenv",
- "segment-anything", # for model testing
+ 'tifffile;python_version>="3.12"', # we're using the new `tifffile.asxarray` which is only available in tifffile>=2026.5.2, which requires py>=3.12
+ "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
+ "zarr",
]
docs = [
"griffe-pydantic",
@@ -124,7 +142,7 @@ exclude = [
]
include = ["src", "scripts", "tests"]
pythonPlatform = "All"
-pythonVersion = "3.9"
+pythonVersion = "3.10"
reportDuplicateImport = "error"
reportImplicitStringConcatenation = "error"
reportIncompatibleMethodOverride = true
@@ -149,7 +167,7 @@ useLibraryCodeForTypes = true
extraPaths = ["../spec-bioimage-io/src"] # co-develop with bioimageio.spec
[tool.pytest.ini_options]
-addopts = "--doctest-modules --failed-first --ignore dogfood --ignore src/bioimageio/core/backends --ignore src/bioimageio/core/weight_converters --ignore scripts/generate_api_doc_pages.py"
+addopts = "--doctest-modules --failed-first --ignore dogfood --ignore src/bioimageio/core/backends --ignore src/bioimageio/core/weight_converters --ignore scripts/generate_api_doc_pages.py --timeout=3600 --timeout-method=thread"
testpaths = ["src", "tests"]
[tool.ruff]
@@ -163,6 +181,7 @@ exclude = [
[tool.ruff.lint]
extend-select = ["NPY201"]
+ignore = ["BLE001", "PLC0414"]
[tool.ruff.lint.isort]
known-first-party = ["bioimageio"]
@@ -171,4 +190,5 @@ known-first-party = ["bioimageio"]
exclude_also = ["if TYPE_CHECKING:", "assert_never\\("]
[tool.coverage.run]
+branch = true
patch = ["subprocess"]
diff --git a/scripts/generate_api_doc_pages.py b/scripts/generate_api_doc_pages.py
index becf0f37e..443691323 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/scripts/setup_dev_env.py b/scripts/setup_dev_env.py
index b1df230fd..c3dbe1d92 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/scripts/show_diff.py b/scripts/show_diff.py
index d1f4affee..1f536d04c 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/__init__.py b/src/bioimageio/core/__init__.py
index 85b84a23b..3d1cf47d8 100644
--- a/src/bioimageio/core/__init__.py
+++ b/src/bioimageio/core/__init__.py
@@ -14,9 +14,8 @@
- 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"
+__version__ = "0.12.0"
from loguru import logger
logger.disable("bioimageio.core")
@@ -40,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
@@ -54,7 +52,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 (
@@ -75,13 +72,11 @@
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 .block import apply_blockwise as apply_blockwise
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/__main__.py b/src/bioimageio/core/__main__.py
index edcdea06a..123b6a9c9 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/_axis_annotations.py b/src/bioimageio/core/_axis_annotations.py
index 8bd1a7e32..160629005 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/_collection.py b/src/bioimageio/core/_collection.py
index 5751e01b7..b06de7f13 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 bb255e35e..6c9b54d04 100644
--- a/src/bioimageio/core/_common_annotations.py
+++ b/src/bioimageio/core/_common_annotations.py
@@ -1,12 +1,12 @@
from __future__ import annotations
+from collections.abc import Hashable, Mapping
from types import MappingProxyType
from typing import (
Annotated,
Any,
- Hashable,
- Mapping,
TypeVar,
+ get_args,
)
import pydantic
@@ -18,7 +18,6 @@
no_info_plain_validator_function,
plain_serializer_function_ser_schema,
)
-from typing_extensions import get_args
from .common import PerMember
diff --git a/src/bioimageio/core/_description_serializer.py b/src/bioimageio/core/_description_serializer.py
index 516cda2c4..6899f562a 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 (
@@ -16,7 +15,8 @@
class DescriptionSerializer:
"""Description serializer intended for client/server communication, NOT for sharing resource descriptions.
- This serializer only includes local files to keep the serialized package small.
+ This serializer only includes local files and required v0.4 Python architectures
+ to keep the serialized package small.
"""
STRING_ENCODING = "ascii"
@@ -33,12 +33,17 @@ def serialize_to_string(cls, rd: ResourceDescr) -> str:
safe_bytes = cls._get_safe_bytes(package_bytes)
serialized_str = safe_bytes.decode(cls.STRING_ENCODING)
- if len(serialized_str) <= 2083:
- raise RuntimeError(
- "Serialized model description should be longer than 2083 characters to not be treated as a URL on the server side."
- )
+ serialized_str = cls._pad_to_min_length(serialized_str)
return serialized_str
+ MIN_LENGTH = 2084
+ """Serialized strings of at most 2083 characters would be treated as a URL on the server side."""
+
+ @classmethod
+ def _pad_to_min_length(cls, serialized_str: str) -> str:
+ # `base64.b64decode` discards non-alphabet characters, so newlines are safe padding
+ return serialized_str + "\n" * (cls.MIN_LENGTH - len(serialized_str))
+
@staticmethod
def _get_safe_bytes(raw_bytes: bytes) -> bytes:
return base64.b64encode(raw_bytes)
@@ -52,18 +57,15 @@ 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
@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)
- if len(serialized_str) <= 2083:
- raise RuntimeError(
- "Serialized model description should be longer than 2083 characters to not be treated as a URL on the server side."
- )
+ serialized_str = cls._pad_to_min_length(serialized_str)
sha256 = Sha256(hashlib.sha256(package_bytes).hexdigest())
return serialized_str, sha256
diff --git a/src/bioimageio/core/_magic_tensor_ops.py b/src/bioimageio/core/_magic_tensor_ops.py
index e60b7a736..bcf5f4f53 100644
--- a/src/bioimageio/core/_magic_tensor_ops.py
+++ b/src/bioimageio/core/_magic_tensor_ops.py
@@ -3,17 +3,21 @@
# TODO: should we generate this ourselves?
# TODO: test these magic methods
import operator
-from typing import Any, Callable
+from collections.abc import Callable
+from typing import TYPE_CHECKING, Any
from typing_extensions import Self
from xarray.core import nputils
-try:
- # xarray >= 2025.03
+if TYPE_CHECKING:
from xarray.computation import ops
-except ImportError:
- # xarray < 2025.03
- from xarray.core import ops # type: ignore
+else:
+ try:
+ # xarray >= 2025.03
+ from xarray.computation import ops
+ except ImportError:
+ # xarray < 2025.03
+ from xarray.core import ops
class MagicTensorOpsMixin:
diff --git a/src/bioimageio/core/_model_adapter.py b/src/bioimageio/core/_model_adapter.py
index 72f7a59d1..6675fcc59 100644
--- a/src/bioimageio/core/_model_adapter.py
+++ b/src/bioimageio/core/_model_adapter.py
@@ -1,33 +1,40 @@
+from __future__ import annotations
+
import gc
+import time
import warnings
from abc import ABC, abstractmethod
+from collections.abc import Callable, Sequence
+from dataclasses import dataclass, field
from queue import LifoQueue
from typing import (
Any,
- Dict,
Generic,
- Iterable,
- List,
- Optional,
- Sequence,
- Tuple,
- Union,
+ Literal,
+ cast,
)
+import dask.array as da
+from dask.delayed import Delayed, delayed # pyright: ignore[reportUnknownVariableType]
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.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 ._utils.type_hints import NestedList, cast_and_assert
+from .axis import AxisId, PerAxis
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 .tensor import Array, MultiIndex, Tensor, restore_batch_multi_index
class ModelAdapter(ABC):
@@ -51,7 +58,10 @@ class ModelAdapter(ABC):
"""
def __init__(
- self, model_description: AnyModelDescr, devices: Optional[Sequence[str]]
+ self,
+ model_description: AnyModelDescr,
+ devices: Sequence[str] | None,
+ computation_mode: Literal["eager", "lazy"] = "lazy",
):
super().__init__()
self._model_descr = model_description
@@ -64,32 +74,67 @@ def __init__(
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()
+ self._loaded = False
+
+ 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
+ @property
+ def is_loaded(self) -> bool:
+ """`True` if the model adapter is loaded and ready for inference."""
+ return self._loaded
+
@abstractmethod
def load(self) -> None:
+ """Load the model adapter and prepare it for inference."""
self._loaded = True
@abstractmethod
def forward(
- self, inputs: PerMember[Optional[Tensor]]
- ) -> PerMember[Optional[Tensor]]: ...
+ 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
@@ -106,15 +151,51 @@ def close(self):
ModelType = TypeVar("ModelType")
+@dataclass
+class _LocalModelAdapterStats:
+ forward_calls: dict[str, int] = field(
+ default_factory=cast(Callable[[], dict[str, int]], dict)
+ )
+ """forward calls per device"""
+ forward_time: dict[str, float] = field(
+ default_factory=cast(Callable[[], dict[str, float]], dict)
+ )
+ """total forward time per device"""
+
+ def __str__(self) -> str:
+ forward_time = ", ".join(
+ f"{device}: {time:.2g}" for device, time in self.forward_time.items()
+ )
+ forward_time_per_call = ", ".join(
+ f"{device}: {time / self.forward_calls[device]:.2g}"
+ for device, time in self.forward_time.items()
+ if device in self.forward_calls
+ )
+ return (
+ f"forward calls: {self.forward_calls}, "
+ f"forward time: {forward_time}, "
+ f"forward time per call: {forward_time_per_call}"
+ )
+
+
class LocalModelAdapter(ModelAdapter, ABC, Generic[DeviceType, ModelType]):
+ def __init__(
+ self,
+ model_description: AnyModelDescr,
+ devices: Sequence[str] | None,
+ computation_mode: Literal["eager", "lazy"] = "lazy",
+ ):
+ super().__init__(model_description, devices, computation_mode)
+ self._stats = _LocalModelAdapterStats()
+
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)
@@ -143,7 +224,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:
@@ -156,77 +237,178 @@ 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 | 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()")
+ raise RuntimeError("Model must be loaded before calling forward()")
unexpected = [mid for mid in inputs if mid not in self._input_ids]
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 = a.transpose(
+ in_order,
+ extra_dims="stack" if a.batch_multi_index is None else "raise",
+ )
+ if a.batch_multi_index is not None:
+ multi_batch_indices[in_id] = a.batch_multi_index
+
+ a = a.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":
+ deferred_inputs = [
+ cast_and_assert(
+ Delayed,
+ delayed(a),
+ )
+ if isinstance(a, da.Array)
+ else a
+ for a in input_arrays
+ ]
+
+ output_arrays = cast(
+ Sequence[Delayed | None],
+ delayed(
+ self._forward_arrays,
+ pure=False,
+ nout=len(self._output_ids),
+ )(*deferred_inputs),
)
- 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: 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()
+ self._stats.forward_calls[str(device)] = (
+ self._stats.forward_calls.get(str(device), 0) + 1
+ )
+ start = time.perf_counter()
+ 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._stats.forward_time[str(device)] = self._stats.forward_time.get(
+ str(device), 0.0
+ ) + (time.perf_counter() - start)
+ self._model_queue.put((device, model))
+
@abstractmethod
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):
+ if not self.is_loaded:
+ return
+
+ logger.info("model adapter stats: {}", self._stats)
for _ in range(len(self._initialized_devices)):
device, model = self._model_queue.get()
try:
@@ -274,25 +456,29 @@ def server(self) -> str:
return self._server
def forward(
- self, inputs: PerMember[Optional[Tensor]]
- ) -> PerMember[Optional[Tensor]]:
+ self, inputs: PerMember[Tensor | Array | None]
+ ) -> PerMember[Tensor | None]:
+ input_dims = dict(zip(self._input_ids, self._input_axes))
serialized_input = self._serializer.serialize_sample(
Sample(
- members={k: v for k, v in inputs.items() if v is not None},
+ members={
+ k: v if isinstance(v, Tensor) else Tensor(v, dims=input_dims[k])
+ for k, v in inputs.items()
+ if v is not None
+ },
stat={},
id=None,
)
)
serialized_output = self._forward_impl(serialized_input)
output = self._serializer.deserialize_sample(serialized_output).members
- output = restore_batch_multi_index(inputs, output)
return output
@abstractmethod
def _forward_impl(
- self, serialized_input_sample: Iterable[SerializedSampleBlockType]
- ) -> Iterable[SerializedSampleBlockType]: ...
+ self, serialized_input_sample: NestedList[SerializedSampleBlockType]
+ ) -> NestedList[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 9aa3a558c..2e5dc83b7 100644
--- a/src/bioimageio/core/_op_base.py
+++ b/src/bioimageio/core/_op_base.py
@@ -1,84 +1,100 @@
+from __future__ import annotations
+
from abc import ABC, abstractmethod
+from collections.abc import Collection, Sequence
from dataclasses import dataclass
-from typing import Collection, Generic, Union
+from typing import Any
-from typing_extensions import TypeVar, assert_never
+from torch import TYPE_CHECKING
-from ._restore_batch_multi_index import restore_batch_multi_index
from .axis import PerAxis
-from .block import Block
from .common import MemberId
-from .sample import Sample, SampleBlock, SampleBlockWithOrigin
-from .stat_measures import (
- Measure,
- Stat,
-)
from .tensor import Tensor
-SampleT = TypeVar("SampleT", bound=Union[Sample, SampleBlock, SampleBlockWithOrigin])
+if TYPE_CHECKING:
+ from .sample import Sample
+ from .stat_measures import Measure, Stat
+
+@dataclass(frozen=True, kw_only=True)
+class Operator(ABC):
+ """Base class for all operators.
-@dataclass
-class Operator(Generic[SampleT], ABC):
- """Base class for all operators."""
+ Implementations must record themselves in ``sample.applied_ops`` after success.
+ """
@abstractmethod
- def __call__(self, sample: SampleT) -> None: ...
+ def __call__(self, sample: Sample) -> None: ...
@property
- @abstractmethod
- def required_measures(self) -> Collection[Measure]: ...
+ def output_member_ids(self) -> Collection[MemberId]:
+ return ()
+
+ @property
+ def affects_statistics(self) -> bool:
+ """Whether this operator may change statistics of its output tensors."""
+ return True
+
+ def get_required_measures(self) -> Collection[Measure]:
+ return ()
+
+ def get_descr(self) -> Any:
+ raise NotImplementedError(
+ f"{type(self).__name__} has no processing description"
+ )
+
+def get_relevant_preceding_ops(
+ member_id: MemberId, applied_ops: Sequence[Operator]
+) -> tuple[Operator, ...]:
+ return tuple(
+ op
+ for op in applied_ops
+ if op.affects_statistics and member_id in op.output_member_ids
+ )
-@dataclass
-class SamplewiseOperator(Operator[Sample]):
+
+@dataclass(frozen=True, kw_only=True)
+class SamplewiseOperator(Operator):
"""Base class for operators that can only be applied to whole samples."""
-@dataclass
-class BlockwiseOperator(Operator[Union[Sample, SampleBlock]]):
+@dataclass(frozen=True, kw_only=True)
+class BlockwiseOperator(Operator):
"""Base class for operators that can be applied to whole sample or blockwise."""
-@dataclass
+@dataclass(frozen=True)
class SimpleOperator(BlockwiseOperator):
"""Convenience base class for blockwise operators with a single input and single output."""
input: MemberId
output: MemberId
+ @property
+ def output_member_ids(self) -> Collection[MemberId]:
+ return (self.output,)
+
@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) -> 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]
- sample.blocks[self.output] = Block(
- sample_shape=self.get_output_shape(sample.sample_shape[self.input]),
- data=output_tensor,
- inner_slice=b.inner_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
+ sample.applied_ops += (self,)
@abstractmethod
def _apply(self, x: Tensor, stat: Stat) -> Tensor: ...
diff --git a/src/bioimageio/core/_ops_cellpose.py b/src/bioimageio/core/_ops_cellpose.py
index 424ac76d3..8947d9a93 100644
--- a/src/bioimageio/core/_ops_cellpose.py
+++ b/src/bioimageio/core/_ops_cellpose.py
@@ -1,21 +1,24 @@
from dataclasses import dataclass
-from typing import Any, Collection
+from typing import Any, Literal, cast
import numpy as np
from numpy.typing import NDArray
-from typing_extensions import Literal, cast
-from bioimageio.spec.model.v0_5 import CellposeFlowDynamicsDescr
+from bioimageio.spec.model.v0_5 import (
+ CellposeFlowDynamicsDescr,
+ CellposeFlowDynamicsKwargs,
+)
from ._op_base import SamplewiseOperator
from .axis import AxisId, PerAxis
from .common import MemberId
from .sample import Sample
-from .stat_measures import Measure
from .tensor import Tensor
+DEFAULT_LABELS_ID = MemberId("labels")
-@dataclass
+
+@dataclass(frozen=True)
class CellposeFlowDynamics(SamplewiseOperator):
"""Cellpose flow-dynamics postprocessing operator.
@@ -29,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
@@ -47,8 +50,19 @@ def from_proc_descr(
)
@property
- def required_measures(self) -> Collection[Measure]:
- return set()
+ def output_member_ids(self) -> tuple[MemberId, ...]:
+ return (self.labels_id,)
+
+ def get_descr(self):
+ return CellposeFlowDynamicsDescr(
+ kwargs=CellposeFlowDynamicsKwargs(
+ cellprob_threshold=self.cellprob_threshold,
+ flow_threshold=self.flow_threshold,
+ do_3D=self.do_3D,
+ min_size=self.min_size,
+ output_dtype=self.output_dtype,
+ )
+ )
def get_output_shape(self, input_shape: PerAxis[int]) -> PerAxis[int]:
output_shape = dict(input_shape)
@@ -59,6 +73,7 @@ def __call__(self, sample: Sample) -> None:
input_tensor = sample.members[self.labels_id]
output_tensor = self._apply(input_tensor)
sample.members[self.labels_id] = output_tensor
+ sample.applied_ops += (self,)
def _apply(self, x: Tensor) -> Tensor:
if x.dims[0] != AxisId("batch"):
@@ -76,7 +91,10 @@ def _apply(self, x: Tensor) -> Tensor:
)
masks = [self._apply_impl(xx) for xx in x]
- return Tensor.from_numpy(np.stack(masks, axis=0), dims=x.dims)
+ output = Tensor.from_numpy(np.stack(masks, axis=0), dims=x.dims)
+ output.set_physical_scale(x.get_physical_scale())
+ output.set_physical_scale_unit(x.get_physical_scale_unit())
+ return output
def _apply_impl(self, x: Tensor) -> NDArray[Any]:
"""apply on a tensor without batch dimension"""
diff --git a/src/bioimageio/core/_ops_stardist.py b/src/bioimageio/core/_ops_stardist.py
index 27b863bda..7086135c8 100644
--- a/src/bioimageio/core/_ops_stardist.py
+++ b/src/bioimageio/core/_ops_stardist.py
@@ -1,18 +1,16 @@
+from __future__ import annotations
+
from abc import ABC, abstractmethod
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
@@ -20,21 +18,18 @@
from .axis import AxisId
from .common import MemberId
from .sample import Sample
-from .stat_measures import (
- Measure,
-)
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):
+@dataclass(frozen=True)
+class _StardistPostprocessingBase(SamplewiseOperator, ABC, Generic[NdTuple, NdBorder]):
prob_dist_input_id: MemberId
instance_labels_output_id: MemberId
@@ -47,15 +42,15 @@ 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
"""Number of radial lines (rays) cast from the center of an object to its boundary."""
@property
- def required_measures(self) -> Collection[Measure]:
- return set()
+ def output_member_ids(self) -> tuple[MemberId, ...]:
+ return (self.instance_labels_output_id,)
def __call__(self, sample: Sample) -> None:
prob_dist = sample.members[self.prob_dist_input_id]
@@ -86,7 +81,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}
@@ -105,7 +100,13 @@ def __call__(self, sample: Sample) -> None:
np.stack(labels)[..., None],
dims=(AxisId("batch"), *allowed_spatial, AxisId("channel")),
)
+ grid = dict(zip(allowed_spatial, self.grid))
+ instance_labels.set_physical_scale(
+ {a: s / grid.get(a, 1) for a, s in prob_dist.get_physical_scale().items()}
+ )
+ instance_labels.set_physical_scale_unit(prob_dist.get_physical_scale_unit())
sample.members[self.instance_labels_output_id] = instance_labels
+ sample.applied_ops += (self,)
@abstractmethod
def _impl(
@@ -114,21 +115,32 @@ def _impl(
raise NotImplementedError
-@dataclass
+@dataclass(frozen=True)
class StardistPostprocessing2D(
_StardistPostprocessingBase[
- Tuple[int, int], Tuple[Tuple[int, int], Tuple[int, int]]
+ tuple[int, int], tuple[tuple[int, int], tuple[int, int]]
]
):
+ def get_descr(self):
+ return v0_5.StardistPostprocessingDescr(
+ kwargs=v0_5.StardistPostprocessingKwargs2D(
+ grid=self.grid,
+ prob_threshold=self.prob_threshold,
+ nms_threshold=self.nms_threshold,
+ b=self.b,
+ n_rays=self.n_rays,
+ )
+ )
+
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 (
+ from stardist import ( # pyright: ignore[reportMissingTypeStubs]
non_maximum_suppression, # pyright: ignore[reportUnknownVariableType]
polygons_to_label, # pyright: ignore[reportUnknownVariableType]
)
- points, probi, disti = non_maximum_suppression( # pyright: ignore[reportUnknownVariableType]
+ points, probi, disti = non_maximum_suppression(
dist,
prob,
grid=self.grid,
@@ -160,30 +172,43 @@ def from_proc_descr(
)
-@dataclass
+@dataclass(frozen=True)
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 get_descr(self):
+ return v0_5.StardistPostprocessingDescr(
+ kwargs=v0_5.StardistPostprocessingKwargs3D(
+ grid=self.grid,
+ prob_threshold=self.prob_threshold,
+ nms_threshold=self.nms_threshold,
+ b=self.b,
+ n_rays=self.n_rays,
+ anisotropy=self.anisotropy,
+ overlap_label=self.overlap_label,
+ )
+ )
+
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 (
+ from stardist import ( # pyright: ignore[reportMissingTypeStubs]
Rays_GoldenSpiral,
non_maximum_suppression_3d, # pyright: ignore[reportUnknownVariableType]
polyhedron_to_label, # pyright: ignore[reportUnknownVariableType]
)
- from stardist.matching import (
+ from stardist.matching import ( # pyright: ignore[reportMissingTypeStubs]
relabel_sequential, # pyright: ignore[reportUnknownVariableType]
)
diff --git a/src/bioimageio/core/_prediction_pipeline.py b/src/bioimageio/core/_prediction_pipeline.py
index 87e0fd1ad..d19f2ceed 100644
--- a/src/bioimageio/core/_prediction_pipeline.py
+++ b/src/bioimageio/core/_prediction_pipeline.py
@@ -1,24 +1,14 @@
+from __future__ import annotations
+
import warnings
from abc import ABC, abstractmethod
-from itertools import chain
+from collections.abc import Iterable, Mapping, Sequence
from types import MappingProxyType
from typing import (
- Any,
- Iterable,
- List,
Literal,
- Mapping,
- NamedTuple,
- Optional,
- Sequence,
- Set,
- Tuple,
- TypeVar,
- Union,
)
from loguru import logger
-from tqdm import tqdm
from typing_extensions import assert_never
from bioimageio.spec import load_model_description
@@ -26,42 +16,31 @@
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 (
BlocksizeParameter,
Halo,
MemberId,
PerMember,
- SampleId,
SupportedWeightsFormat,
)
from .digest_spec import (
- get_block_transform,
+ TensorSource,
get_input_halo,
get_member_ids,
+ get_output_tensor_metadata,
+ get_tensor,
+ 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
-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):
def __init__(
@@ -70,9 +49,10 @@ 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
@@ -83,7 +63,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: {
@@ -96,20 +76,33 @@ 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
}
)
self._input_ids = tuple(get_member_ids(model_descr.inputs))
self._output_ids = tuple(get_member_ids(model_descr.outputs))
+ def __enter__(self):
+ self.load()
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb): # type: ignore
+ self.unload()
+ return False
+
+ @abstractmethod
+ def load(self): ...
+
+ @abstractmethod
+ def unload(self): ...
+
@property
def input_ids(self) -> Sequence[MemberId]:
return self._input_ids
@@ -126,35 +119,12 @@ 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_input_padding: bool = False,
skip_preprocessing: bool = False,
skip_postprocessing: bool = False,
- skip_input_padding: bool = False,
skip_output_cropping: bool = False,
) -> Sample:
"""Predict a whole sample at once.
@@ -165,34 +135,38 @@ def predict_sample_without_blocking(
Args:
sample: input sample
+ skip_input_padding: if `True`, skip padding the input sample according to the model's (optional) output halos.
skip_preprocessing: if `True`, skip all preprocessing steps (except for any preceding prediction pipeline).
skip_postprocessing: if `True`, skip all postprocessing steps (except for any preceding prediction pipeline).
- skip_input_padding: if `True`, skip padding the input sample according to the model's (optional) output halos.
skip_output_cropping: if `True`, skip cropping any output halos from the model output.
"""
+ sample = sample.copy()
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_input_padding=skip_input_padding,
+ skip_preprocessing=False,
+ skip_postprocessing=False,
skip_output_cropping=skip_output_cropping,
)
- return self._predict_sample_without_blocking_impl(
+ return self._predict_without_blocking_impl(
sample,
+ skip_input_padding=skip_input_padding,
skip_preprocessing=skip_preprocessing,
skip_postprocessing=skip_postprocessing,
- skip_input_padding=skip_input_padding,
skip_output_cropping=skip_output_cropping,
)
@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_input_padding: bool,
+ skip_preprocessing: bool,
+ skip_postprocessing: bool,
+ skip_output_cropping: bool,
) -> Sample:
"""Predict a whole sample at once.
@@ -202,136 +176,70 @@ def _predict_sample_without_blocking_impl(
Args:
sample: input sample
+ batch_size: if provided, use this batch size for the model prediction. Otherwise, use the default batch size of the pipeline.
+ skip_input_padding: if `True`, skip padding the input sample according to the model's (optional) output halos.
skip_preprocessing: if `True`, skip all preprocessing steps.
skip_postprocessing: if `True`, skip all postprocessing steps.
- skip_input_padding: if `True`, skip padding the input sample according to the model's (optional) output halos.
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,
+ ns: v0_5.ParameterizedSize_N
+ | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N]
+ | None = None,
+ *,
+ batch_size: int | None = None,
+ skip_input_padding: bool = False,
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,
+ 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.
- """
+ sample = sample.copy()
+ 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,
+ skip_input_padding=skip_input_padding,
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_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: Optional[
- Union[
- v0_5.ParameterizedSize_N,
- Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N],
- ]
- ] = None,
- batch_size: Optional[int] = 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)
-
+ ns: v0_5.ParameterizedSize_N
+ | Mapping[tuple[MemberId, AxisId], v0_5.ParameterizedSize_N]
+ | None,
+ *,
+ batch_size: int | None,
+ skip_input_padding: bool,
+ skip_preprocessing: bool,
+ skip_postprocessing: 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"
+ 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
+ ns = ns if ns is not None else self._default_blocksize_parameter
if isinstance(ns, int):
ns = {
(ipt.id, a.id): ns
@@ -339,96 +247,111 @@ def predict_sample_with_blocking_yield_intermediates(
for a in ipt.axes
if isinstance(a.size, v0_5.ParameterizedSize)
}
- input_block_shape = self._model_descr.get_tensor_sizes(
- ns, batch_size or self._default_batch_size
+ max_block_shape = self._model_descr.get_tensor_sizes(
+ ns, batch_size or self._default_batch_size, sample.shape
).inputs
+ block_shape: dict[MemberId, dict[AxisId, int | AxisSize]] = {}
+ for ipt in self._model_descr.inputs:
+ block_shape[ipt.id] = {}
+ for a in ipt.axes:
+ if isinstance(a.size, v0_5.ParameterizedSize):
+ axis_size = AxisSize(
+ min=a.size.min,
+ max=max(a.size.min, max_block_shape[ipt.id][a.id]),
+ step=a.size.step,
+ )
+ else:
+ axis_size = max_block_shape[ipt.id][a.id]
- 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,
- )
+ block_shape[ipt.id][a.id] = axis_size
+
+ return self._predict_with_fixed_blocking_impl(
+ sample,
+ block_shape=block_shape,
+ skip_input_padding=skip_input_padding,
+ skip_preprocessing=skip_preprocessing,
+ skip_postprocessing=skip_postprocessing,
+ 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]],
+ input_block_shape: PerMember[PerAxis[int | AxisSize]],
*,
+ skip_input_padding: bool = False,
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_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_input_padding: If `True`, skip input padding that compensates for the model's output halo.
+ skip_preprocessing: If `True`, skip all preprocessing steps.
+ skip_postprocessing: If `True`, skip all postprocessing steps.
+ skip_output_cropping: If `True`, skip output cropping of the model's halo.
"""
- total_prediction_steps = 0
- iterable_intermediates = ()
+ sample = sample.copy()
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(
+ 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,
+ block_shape=input_block_shape,
+ skip_input_padding=False,
+ skip_preprocessing=False,
+ skip_postprocessing=False,
+ skip_output_cropping=False,
)
+
+ return self._predict_with_fixed_blocking_impl(
+ sample,
+ block_shape=input_block_shape,
+ skip_input_padding=skip_input_padding,
+ skip_preprocessing=skip_preprocessing,
+ skip_postprocessing=skip_postprocessing,
+ 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]],
+ block_shape: PerMember[PerAxis[int | AxisSize]],
*,
- skip_preprocessing: bool = False,
- skip_postprocessing: bool = False,
- fill_value: float = float("nan"),
- ) -> Tuple[int, Iterable[IntermediatePrediction]]: ...
+ skip_input_padding: bool,
+ skip_preprocessing: bool,
+ skip_postprocessing: 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 ()
+
+ 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)
- Note that this does not apply samplewise preprocessing or postprocessing steps, but only blockwise ones.
+ if not required_inputs:
+ return preceding_pipelines
- 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.
- """
+ raise KeyError(
+ f"Sample is missing required inputs {required_inputs} for the prediction pipeline or its preceding pipelines."
+ )
class PredictionPipeline(_PredictionPipelineBase):
@@ -443,14 +366,15 @@ 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__(
@@ -468,19 +392,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 (
@@ -493,39 +417,77 @@ def __init__(
self._adapter = model_adapter
- def __enter__(self):
- self.load()
- return self
-
- def __exit__(self, exc_type, exc_val, exc_tb): # type: ignore
- self.unload()
- 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
+ def apply_input_padding(self, sample: Sample) -> None:
+ padded_sample = sample.pad(
+ pad_width=self._default_input_halo, mode=self._pad_mode
)
- 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."
- )
+ sample.members = padded_sample.members
+
+ 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 apply_output_cropping(self, sample: Sample) -> None:
+ sample.members = {
+ m: t
+ if m not in self._default_output_halo
+ else t[
+ {
+ a: slice(h.left, t.tagged_shape[a] + -h.right)
+ for a, h in self._default_output_halo[m].items()
+ }
+ ]
+ for m, t in sample.members.items()
+ }
def raise_for_non_blockwise_preprocessing(self):
"""
@@ -541,121 +503,64 @@ 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,
- skip_postprocessing: bool = False,
- skip_input_padding: bool = False,
- skip_output_cropping: bool = False,
+ *,
+ skip_input_padding: bool,
+ skip_preprocessing: bool,
+ skip_postprocessing: bool,
+ skip_output_cropping: bool,
) -> 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)
+ self.apply_input_padding(sample)
if not skip_preprocessing:
self.apply_preprocessing(sample)
- output = Sample(
- members={
- k: v
- for k, v in self._adapter.forward(sample.members).items()
- if v is not None
- },
- stat=sample.stat,
- id=sample.id,
- )
+ output_members = {
+ k: v for k, v in self._forward(sample.members).items() if v is not None
+ }
+
+ output = Sample(members=output_members, stat=sample.stat, id=sample.id)
if not skip_postprocessing:
self.apply_postprocessing(output)
if not skip_output_cropping:
- output.members = {
- m: t
- if m not in self._default_output_halo
- else t[
- {
- a: slice(h.left, None if h.right == 0 else -h.right)
- for a, h in self._default_output_halo[m].items()
- }
- ]
- for m, t in output.members.items()
- }
+ self.apply_output_cropping(output)
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: Optional[
- Union[
- v0_5.ParameterizedSize_N,
- Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N],
- ]
- ] = None,
- batch_size: Optional[int] = 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
-
- 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]],
+ block_shape: PerMember[PerAxis[int | AxisSize]],
*,
- 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_input_padding: bool,
+ skip_preprocessing: bool,
+ skip_postprocessing: 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.
@@ -667,122 +572,110 @@ def _predict_sample_with_fixed_blocking_yield_intermediates_impl(
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_input_padding: If `True`, skip input padding that compensates for the model's output halo.
skip_preprocessing: If `True`, skip all preprocessing steps.
skip_postprocessing: If `True`, skip all postprocessing steps.
+ skip_output_cropping: If `True`, skip output cropping of the model's 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:
+ self.apply_input_padding(sample)
+
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,
+ output_members = self._forward_with_fixed_blocking(
+ sample.members,
+ chunk_shape=settings.processing_chunk_min_bytes,
+ block_shape=block_shape,
)
- logger.info(
- "split sample shape {} into {} blocks of {}.",
- {k: dict(v) for k, v in sample.shape.items()},
- n_blocks,
- {k: dict(v) for k, v in input_block_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)
+ sample = Sample(id=sample.id, members=output_members, stat=sample.stat)
- return n_blocks, _predict_blocks()
+ if not skip_postprocessing:
+ self.apply_postprocessing(sample)
- def _apply_samplewise_preprocessing(self, sample: Sample, /) -> None:
- """Apply preprocessing operators up to and including the last samplewise operator in-place.
+ if not skip_output_cropping:
+ self.apply_output_cropping(sample)
- Note: This skips all blockwise preprocessing steps after the last samplewise operator.
- """
- if isinstance(sample, SampleBlock):
- self.raise_for_non_blockwise_preprocessing()
+ return sample
- for op in self._samplewise_preprocessing:
- op(sample)
-
- def _apply_blockwise_preprocessing(
- self, sample_block: Union[Sample, SampleBlock], /
- ) -> None:
- """Apply blockwise preprocessing operators in-place.
+ def _forward(self, inputs: PerMember[Tensor]) -> PerMember[Tensor | None]:
+ outputs = self._adapter.forward(inputs)
+ for m, metadata in get_output_tensor_metadata(
+ self._model_descr, inputs
+ ).items():
+ if (tensor := outputs.get(m)) is not None:
+ tensor.set_metadata(metadata)
+ return outputs
- 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: Union[Sample, SampleBlock]) -> None:
- """Apply preprocessing in-place, also may updates sample stats"""
+ def _forward_with_fixed_blocking(
+ self,
+ sample_members: PerMember[Tensor],
+ chunk_shape: PerMember[PerAxis[int]] | int,
+ block_shape: PerMember[PerAxis[int | AxisSize]],
+ ) -> dict[MemberId, Tensor]:
- if isinstance(sample, Sample):
- self._apply_samplewise_preprocessing(sample)
+ # fill block_shape to set treat extra dims unknown to the model to 1
+ if isinstance(self._model_descr, v0_4.ModelDescr):
+ model_axes = {
+ AxisId(ax) for ipt in self._model_descr.inputs for ax in ipt.axes
+ }
else:
- self.raise_for_non_blockwise_preprocessing()
-
- self._apply_blockwise_preprocessing(sample)
-
- def _apply_blockwise_postprocessing(
- self, sample_block: Union[Sample, SampleBlock], /
- ) -> None:
- """Apply in-place blockwise postprocessing operators
-
- Note: This does not apply all postprocessing operators from the first samplewise one onwards.
- """
- for op in self._blockwise_postprocessing:
- op(sample_block)
-
- def _apply_samplewise_postprocessing(self, sample: Sample, /) -> None:
- """Apply in-place postprocessing operators starting from and including the first samplewise operator.
+ model_axes = {ax.id for ipt in self._model_descr.inputs for ax in ipt.axes}
+ block_shape = {
+ m: {
+ a: block_shape[m][a]
+ if m in block_shape and a in block_shape[m]
+ else s
+ if a in model_axes
+ else 1
+ for a, s in t.tagged_shape.items()
+ }
+ for m, t in sample_members.items()
+ }
- Note: This skips all blockwise postprocessing steps before the first samplewise one.
- """
- if isinstance(sample, SampleBlock):
- self.raise_for_non_blockwise_postprocessing()
+ def get_output_shape(
+ input_shape: PerMember[PerAxis[int]],
+ ) -> PerMember[PerAxis[int]]:
+ if isinstance(self._model_descr, v0_4.ModelDescr):
+ return {
+ MemberId(m): {
+ AxisId(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(
+ {
+ v0_4.TensorName(m): {
+ str(a)[0]: s for a, s in s_per_a.items()
+ }
+ for m, s_per_a in input_shape.items()
+ }
+ ).items()
+ }
+ else:
+ 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()
+ }
- for op in self._samplewise_postprocessing:
- op(sample)
+ return apply_blockwise(
+ self._forward,
+ sample_members,
+ chunk_shape=chunk_shape,
+ block_shape=block_shape,
+ pad_mode=self._pad_mode,
+ pad_boundary="allow",
+ input_halo=self._default_input_halo,
+ output_halo=self._default_output_halo,
+ get_output_shape=get_output_shape,
+ )
- def apply_postprocessing(self, sample: Union[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()
+ @property
+ def is_loaded(self) -> bool:
+ """`True` if the model adapter is loaded and ready for inference."""
+ return self._adapter.is_loaded
def load(self):
"""Prepare prediction pipeline for use.
@@ -824,9 +717,10 @@ 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,
@@ -844,19 +738,25 @@ 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
+ | TensorSource
+ | Sequence[TensorSource]
+ | Mapping[MemberId, Tensor | TensorSource]
+ ] = (),
+ dataset_fits_in_memory: bool = False,
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,
- **deprecated_kwargs: Any,
+ preceding_prediction_pipelines: Sequence[
+ PredictionPipeline | RemotePredictionPipeline
+ ]
+ | None = None,
) -> PredictionPipeline:
"""
Creates prediction pipeline which includes:
@@ -876,9 +776,13 @@ def create_prediction_pipeline(
created to run inference with the **bioimageio_model**.
dataset_for_initial_statistics: (optional) If preprocessing steps require input
dataset statistics, **dataset_for_initial_statistics** allows you to
- specifcy a dataset from which these statistics are computed.
+ specify a dataset from which these statistics are computed. Required when
+ dataset statistics are not supplied by **fixed_dataset_statistics** unless
+ **keep_updating_initial_dataset_statistics** is explicitly enabled.
+ dataset_fits_in_memory: (optional) Set to `True` if the dataset for initial statistics fits in memory.
keep_updating_initial_dataset_statistics: (optional) Set to `True` if you want
- to update dataset statistics with each processed sample.
+ to update dataset statistics with each processed sample. This also
+ explicitly allows starting without initial dataset statistics.
fixed_dataset_statistics: (optional) Precomputed dataset (and optionally sample) statistics.
Any included sample statistics will not be calculated on the fly and it is the callers
responsibility to use samples with the corresponding statistics availble in `sample.stat`.
@@ -898,10 +802,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,
@@ -910,6 +810,7 @@ def create_prediction_pipeline(
)
input_ids = get_member_ids(bioimageio_model.inputs)
+ input_descrs = dict(zip(input_ids, bioimageio_model.inputs))
def dataset():
common_stat: Stat = {}
@@ -917,16 +818,32 @@ def dataset():
if isinstance(x, Sample):
yield x
else:
- yield Sample(members=dict(zip(input_ids, x)), stat=common_stat, id=i)
+ if not isinstance(x, Mapping):
+ if isinstance(x, Tensor) or not isinstance(x, Sequence):
+ x = [x]
+
+ x = dict(zip(input_ids, x))
+
+ yield Sample(
+ members={
+ k: v
+ if isinstance(v, Tensor)
+ else get_tensor(v, input_descrs[k])
+ for k, v in x.items()
+ },
+ stat=common_stat,
+ id=i,
+ )
preprocessing, postprocessing = setup_pre_and_postprocessing(
bioimageio_model,
dataset(),
keep_updating_initial_dataset_stats=keep_updating_initial_dataset_statistics,
fixed_dataset_stats=fixed_dataset_statistics,
+ dataset_fits_in_memory=dataset_fits_in_memory,
)
- 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
@@ -960,7 +877,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,
@@ -969,13 +886,19 @@ 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(
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 51ae745d9..00f59ba7a 100644
--- a/src/bioimageio/core/_resource_tests.py
+++ b/src/bioimageio/core/_resource_tests.py
@@ -1,9 +1,12 @@
+from __future__ import annotations
+
import hashlib
import os
import platform
import subprocess
import sys
import warnings
+from collections.abc import Callable, Hashable, Sequence
from contextlib import nullcontext
from copy import deepcopy
from io import StringIO
@@ -12,23 +15,15 @@
from tempfile import TemporaryDirectory
from typing import (
Any,
- Callable,
- Dict,
- Hashable,
- List,
Literal,
- Optional,
- Sequence,
- Set,
- Tuple,
- Union,
+ get_args,
overload,
)
import numpy as np
from loguru import logger
from numpy.typing import NDArray
-from typing_extensions import NotRequired, TypedDict, Unpack, assert_never, get_args
+from typing_extensions import NotRequired, TypedDict, Unpack, assert_never
from bioimageio.spec import (
AnyDatasetDescr,
@@ -74,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
@@ -85,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!
@@ -174,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"""
@@ -204,20 +199,20 @@ 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,
@@ -274,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"
@@ -283,8 +278,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:
@@ -356,14 +351,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],
):
@@ -571,113 +566,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.
@@ -748,7 +743,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:
@@ -783,14 +778,16 @@ 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]:
+ 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(
@@ -806,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"
@@ -826,18 +860,33 @@ 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
-) -> Tuple[float, str, Optional[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)
dims = expected.dims
@@ -847,7 +896,7 @@ def evaluate_mismatched_elements(
rtol_value = rtol * abs(expected_np)
abs_diff = abs(actual_np - expected_np)
- mismatched = abs_diff > atol + rtol_value
+ mismatched = ~np.isclose(actual_np, expected_np, rtol=rtol, atol=atol)
mismatched_elements = mismatched.sum().item()
mismatched_ppm = mismatched_elements / expected_np.size * 1e6
@@ -860,7 +909,7 @@ def evaluate_mismatched_elements(
r_expected = expected_np[r_max_idx].item()
# Calculate the max absolute difference with the relative tolerance subtracted
- abs_diff_wo_rtol: NDArray[np.float32] = abs_diff - rtol_value
+ abs_diff_wo_rtol = abs_diff - rtol_value
a_max_idx = np.unravel_index(abs_diff_wo_rtol.argmax(), abs_diff_wo_rtol.shape)
a_max = abs_diff[a_max_idx].item()
@@ -877,42 +926,52 @@ 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
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(
@@ -934,8 +993,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}"
@@ -956,40 +1015,45 @@ 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
+ bioimageio_model=model,
+ devices=devices,
+ weight_format=weight_format,
+ dataset_for_initial_statistics=[test_input],
) 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_not_postprocessed.members = {
+ m: tensor.as_numpy()
+ for m, tensor in results_not_postprocessed.members.items()
+ }
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():
+ intermediate_paths: list[Path] = []
+ 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")
@@ -1033,7 +1097,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)
@@ -1054,7 +1118,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)
@@ -1074,7 +1138,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:
@@ -1084,7 +1148,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}
@@ -1103,7 +1167,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(
@@ -1114,7 +1178,7 @@ def _test_parametrized_inference(
)
def generate_test_cases():
- tested: Set[Hashable] = set()
+ tested: set[Hashable] = set()
def get_ns(n: int):
return {
@@ -1166,12 +1230,15 @@ def get_ns(n: int):
test_input = get_test_input_sample(model)
with create_prediction_pipeline(
- bioimageio_model=model, devices=devices, weight_format=weight_format
+ bioimageio_model=model,
+ devices=devices,
+ weight_format=weight_format,
+ dataset_for_initial_statistics=[test_input],
) 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(
+ result = prediction_pipeline.predict_without_blocking(
inputs, skip_input_padding=True, skip_output_cropping=True
)
except Exception as e:
@@ -1190,7 +1257,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:
@@ -1231,7 +1298,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,9 +1317,7 @@ def get_ns(n: int):
)
-def _test_expected_resource_type(
- rd: Union[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/_restore_batch_multi_index.py b/src/bioimageio/core/_restore_batch_multi_index.py
deleted file mode 100644
index 8d05b1fe9..000000000
--- a/src/bioimageio/core/_restore_batch_multi_index.py
+++ /dev/null
@@ -1,28 +0,0 @@
-from typing import Optional
-
-import pandas as pd
-
-from .axis import AxisId
-from .common import PerMember
-from .tensor import Tensor
-
-
-def restore_batch_multi_index(
- inputs: PerMember[Optional[Tensor]], outputs: PerMember[Optional[Tensor]]
-) -> PerMember[Optional[Tensor]]:
- """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, pd.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/_sample_serializer.py b/src/bioimageio/core/_sample_serializer.py
index 2d3f461c6..2e23cd65d 100644
--- a/src/bioimageio/core/_sample_serializer.py
+++ b/src/bioimageio/core/_sample_serializer.py
@@ -1,18 +1,24 @@
+from __future__ import annotations
+
from abc import ABC, abstractmethod
+from collections.abc import Callable
from typing import (
Generic,
- Iterable,
- Tuple,
TypeVar,
- Union,
+ cast,
)
+import dask.array as da
+import numpy as np
+
from bioimageio.spec.model import v0_5
-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 ._utils.type_hints import NestedList
+from .axis import AxisSize, PerAxis
+from .block import Block, split_multiple_tensors_into_blocks
+from .common import HaloLike, MemberId, PadMode, PerMember
+from .sample import Sample
+from .tensor import Array, Tensor
SerializedSampleBlockType = TypeVar("SerializedSampleBlockType")
@@ -22,64 +28,151 @@ 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 as a single block."""
+
+ return cls.serialize_sample_block(sample)
@classmethod
def deserialize_sample(
cls,
- serialized: Iterable[SerializedSampleBlockType],
- fill_value: float = float("nan"),
+ serialized: NestedList[SerializedSampleBlockType],
) -> Sample:
- return Sample.from_blocks(
- (cls.deserialize_sample_block(s) for s in serialized), fill_value=fill_value
+ """Assemble a nested block grid; blocks must already be cropped to avoid overlap."""
+
+ if not isinstance(serialized, list):
+ return cls.deserialize_sample_block(serialized)
+
+ leaves: list[Sample] = []
+
+ def deserialize(
+ grid: NestedList[SerializedSampleBlockType],
+ ) -> NestedList[Sample]:
+ if isinstance(grid, list):
+ if not grid:
+ raise ValueError("Serialized sample contains an empty block grid")
+ return [
+ deserialize(item)
+ for item in cast(list[NestedList[SerializedSampleBlockType]], grid)
+ ]
+
+ sample = cls.deserialize_sample_block(grid)
+ leaves.append(sample)
+ return sample
+
+ samples = deserialize(cast(NestedList[SerializedSampleBlockType], serialized))
+ first = leaves[0]
+ for sample in leaves[1:]:
+ if sample.id != first.id:
+ raise ValueError("Sample blocks have different sample IDs")
+ if sample.members.keys() != first.members.keys():
+ raise ValueError("Sample blocks have different members")
+ if sample.applied_ops != first.applied_ops:
+ raise ValueError("Sample blocks have different applied operators")
+ if any(sample.members[m].dims != t.dims for m, t in first.members.items()):
+ raise ValueError("Sample blocks have different member dimensions")
+
+ def select_arrays(
+ grid: NestedList[Sample], member: MemberId
+ ) -> NestedList[Array]:
+ if isinstance(grid, list):
+ return [select_arrays(item, member) for item in grid]
+ return grid.members[member].data.data
+
+ members: dict[MemberId, Tensor] = {}
+ for member, tensor in first.members.items():
+ block_impl = cast(
+ Callable[[NestedList[Array]], Array],
+ da.block
+ if any(
+ isinstance(s.members[member].data.data, da.Array) for s in leaves
+ )
+ else np.block,
+ )
+ members[member] = Tensor(
+ block_impl(select_arrays(samples, member)), tensor.dims
+ )
+ members[member].set_metadata(tensor.get_metadata())
+
+ return Sample(
+ id=first.id,
+ members=members,
+ stat=first.stat,
+ applied_ops=first.applied_ops,
)
+ @classmethod
def serialize_sample_blockwise(
- self,
+ cls,
sample: Sample,
*,
model: v0_5.ModelDescr,
blocksize_parameter: int,
batch_size: int = 1,
- ) -> Iterable[SerializedSampleBlockType]:
- """Split a sample into blocks according to the model's input specifications and `blocksize_parameter` and serialize each block."""
+ ) -> NestedList[SerializedSampleBlockType]:
+ """Serialize a non-overlapping grid with model-derived maximum block sizes.
+
+ Edge blocks may be smaller so deserialization reconstructs the original sample.
+ """
- _n_blocks, blocks = split_sample_into_blocks_for_model(
+ block_shape = model.get_tensor_sizes(
+ blocksize_parameter, batch_size, sample.shape
+ ).inputs
+ return cls.serialize_sample_with_fixed_blocking(
sample,
- model=model,
- blocksize_parameter=blocksize_parameter,
- batch_size=batch_size,
+ block_shapes={
+ m: {a: AxisSize(min=1, max=size) for a, size in shape.items()}
+ for m, shape in block_shape.items()
+ },
+ halo={},
)
- for block in blocks:
- yield self.serialize_sample_block(block)
@classmethod
def serialize_sample_with_fixed_blocking(
cls,
sample: Sample,
*,
- block_shapes: PerMember[PerAxis[int]],
+ block_shapes: PerMember[PerAxis[int | AxisSize]],
halo: PerMember[PerAxis[HaloLike]],
- pad_mode: Union[PadMode, PerMember[PadMode]] = "symmetric",
- ) -> Iterable[SerializedSampleBlockType]:
+ pad_mode: PadMode | PerMember[PadMode] = "symmetric",
+ ) -> NestedList[SerializedSampleBlockType]:
+ """Serialize an aligned block grid, retaining each block's halo and overlap."""
- _n_blocks, input_blocks = sample.split_into_blocks(
- block_shapes=block_shapes,
+ blocks = split_multiple_tensors_into_blocks(
+ sample.members,
+ block_shape=block_shapes,
halo=halo,
pad_mode=pad_mode,
)
- for block in input_blocks:
- yield cls.serialize_sample_block(block)
+ grid_shape = next(iter(blocks.values())).shape
+ if any(size == 0 for size in grid_shape):
+ raise ValueError("Sample has no blocks")
+
+ def serialize_grid(
+ loc: tuple[int, ...] = (),
+ ) -> NestedList[SerializedSampleBlockType]:
+ if len(loc) < len(grid_shape):
+ return [serialize_grid((*loc, i)) for i in range(grid_shape[len(loc)])]
+ return cls.serialize_sample_block(
+ Sample(
+ id=sample.id,
+ stat=sample.stat,
+ applied_ops=sample.applied_ops,
+ members={
+ m: cast(Block, b[loc]).outer_tensor for m, b in blocks.items()
+ },
+ )
+ )
+
+ return serialize_grid()
@staticmethod
@abstractmethod
def serialize_sample_block(
- sample_block: SampleBlock,
+ sample_block: Sample,
) -> SerializedSampleBlockType: ...
@staticmethod
@abstractmethod
- def deserialize_sample_block(serialized: SerializedSampleBlockType) -> SampleBlock:
- """Deserialize a sample block into a new sample or merge it into `output_sample` if provided."""
+ def deserialize_sample_block(serialized: SerializedSampleBlockType) -> Sample:
+ """Deserialize one block into a sample."""
diff --git a/src/bioimageio/core/_settings.py b/src/bioimageio/core/_settings.py
index 16b460683..5344d940d 100644
--- a/src/bioimageio/core/_settings.py
+++ b/src/bioimageio/core/_settings.py
@@ -1,10 +1,11 @@
+from __future__ import annotations
+
import os
import platform
-from typing import Literal, Optional
+import warnings
+from typing import Annotated, Literal
-from loguru import logger
from pydantic import Field, field_validator
-from typing_extensions import Annotated
from bioimageio.spec._internal._settings import Settings as SpecSettings
@@ -17,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
@@ -31,7 +32,9 @@ def _set_default_mps_fallback(cls, value: Optional[bool]):
and platform.system().lower() == "darwin"
and platform.machine().lower() == "arm64"
):
- logger.info("Set environment variable 'PYTORCH_ENABLE_MPS_FALLBACK=1'.")
+ warnings.warn(
+ "environment variable 'PYTORCH_ENABLE_MPS_FALLBACK' not set, setting 'PYTORCH_ENABLE_MPS_FALLBACK=1'."
+ )
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
return True
@@ -45,7 +48,10 @@ def _set_default_mps_fallback(cls, value: Optional[bool]):
)
"""URL to the bioimageio collection config"""
- gradio_server: Optional[str] = None
+ 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.
Example: "bioimage-io/bioimage-io-gradio-server"
diff --git a/src/bioimageio/core/_utils/__init__.py b/src/bioimageio/core/_utils/__init__.py
new file mode 100644
index 000000000..615849003
--- /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/color.py b/src/bioimageio/core/_utils/color.py
new file mode 100644
index 000000000..df4e90a92
--- /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/_compare.py b/src/bioimageio/core/_utils/compare.py
similarity index 93%
rename from src/bioimageio/core/utils/_compare.py
rename to src/bioimageio/core/_utils/compare.py
index d57336dd7..ae1db32c0 100644
--- a/src/bioimageio/core/utils/_compare.py
+++ b/src/bioimageio/core/_utils/compare.py
@@ -1,9 +1,10 @@
+from collections.abc import Sequence
from difflib import HtmlDiff, unified_diff
-from typing import Sequence
+from typing import Literal
from loguru import logger
from ruyaml import Optional
-from typing_extensions import Literal, assert_never
+from typing_extensions import assert_never
from bioimageio.spec._internal.version_type import Version
diff --git a/src/bioimageio/core/_utils/io_zarr.py b/src/bioimageio/core/_utils/io_zarr.py
new file mode 100644
index 000000000..a182e3799
--- /dev/null
+++ b/src/bioimageio/core/_utils/io_zarr.py
@@ -0,0 +1,130 @@
+from __future__ import annotations
+
+from collections.abc import Mapping, Sequence
+from typing import Any
+
+import zarr
+from loguru import logger
+
+
+def open_zarr_multiscale_array(
+ uri: str,
+) -> tuple[zarr.Array[Any], Any]:
+ """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[sub_uri]
+
+
+def _recurse_url_to_find_group(uri: str) -> tuple[Any, 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) -> Any:
+ 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[Any] = []
+ 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/resolve_per_member_key.py b/src/bioimageio/core/_utils/resolve_per_member_key.py
new file mode 100644
index 000000000..2a69c6f76
--- /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/statistics.py b/src/bioimageio/core/_utils/statistics.py
new file mode 100644
index 000000000..40132c87b
--- /dev/null
+++ b/src/bioimageio/core/_utils/statistics.py
@@ -0,0 +1,29 @@
+from __future__ import annotations
+
+from collections.abc import Sequence
+
+import xarray as xr
+
+from ..axis import AxisId
+from ..common import QuantileMethod
+from ..tensor import Tensor
+from .type_hints import cast_and_assert
+
+
+def compute_quantiles(
+ tensor: Tensor,
+ q: float | Sequence[float],
+ axes: Sequence[AxisId] | None,
+ method: QuantileMethod,
+) -> Tensor:
+ tensor = tensor.as_dask()
+ # Exact quantiles need all values along each reduced axis in the same chunk.
+ tensor = Tensor.from_xarray(
+ cast_and_assert(
+ xr.DataArray,
+ tensor.data.chunk(
+ {d: -1 for d in tensor.dims if axes is None or d in axes}
+ ),
+ )
+ )
+ return tensor.quantile(q, dim=axes, method=method)
diff --git a/src/bioimageio/core/_utils/tiff_metadata.py b/src/bioimageio/core/_utils/tiff_metadata.py
new file mode 100644
index 000000000..9c34fe1b3
--- /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
new file mode 100644
index 000000000..82218a9fc
--- /dev/null
+++ b/src/bioimageio/core/_utils/type_guards.py
@@ -0,0 +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, TypeGuard
+
+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]]:
+ """to avoid List[Unknown]"""
+ return is_list(v) and all(isinstance(x, str) for x in v)
diff --git a/src/bioimageio/core/_utils/type_hints.py b/src/bioimageio/core/_utils/type_hints.py
new file mode 100644
index 000000000..c06ec2b8b
--- /dev/null
+++ b/src/bioimageio/core/_utils/type_hints.py
@@ -0,0 +1,11 @@
+from typing import Any, TypeVar
+
+from typing_extensions import TypeAliasType
+
+_T = TypeVar("_T")
+NestedList = TypeAliasType("NestedList", list["NestedList[_T]"] | _T, type_params=(_T,))
+
+
+def cast_and_assert(typ: type[_T] | tuple[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
new file mode 100644
index 000000000..13ed7d725
--- /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/src/bioimageio/core/axis.py b/src/bioimageio/core/axis.py
index 6c85b8b3a..0c2f4d7f6 100644
--- a/src/bioimageio/core/axis.py
+++ b/src/bioimageio/core/axis.py
@@ -1,39 +1,42 @@
from __future__ import annotations
+from collections.abc import Mapping, Sequence
from dataclasses import dataclass
from typing import (
Literal,
- Mapping,
- Optional,
+ TypeAlias,
TypeVar,
Union,
)
-from typing_extensions import Protocol, TypeAlias, assert_never, runtime_checkable
+from loguru import logger
+from typing_extensions import Protocol, assert_never, runtime_checkable
from bioimageio.spec.model import v0_5
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: '"
- + "', '".join(
- ["b", "batch", "t", "time", "i", "index", "c", "channel", "x", "y", "z"]
- )
- + "'. Or creating an `Axis` object instead."
+ 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 with an explicity type instead.",
+ a,
)
+ return "unknown"
S = TypeVar("S", bound=str)
@@ -42,6 +45,10 @@ 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
+CHANNEL_AXIS_ID = v0_5.CHANNEL_AXIS_ID
+TIME_AXIS_ID = v0_5.DEFAULT_TIME_AXIS_ID
+
_T = TypeVar("_T")
PerAxis = Mapping[AxisId, _T]
@@ -49,7 +56,7 @@ def _guess_axis_type(a: str):
BatchSize = int
AxisLetter = Literal["b", "i", "t", "c", "z", "y", "x"]
-_AxisLikePlain = Union[str, AxisId, AxisLetter]
+_AxisLikePlain = str | AxisId | AxisLetter
@runtime_checkable
@@ -61,16 +68,37 @@ 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
- 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:
@@ -97,8 +125,18 @@ 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
+
+ def __post_init__(self):
+ if self.min < 0:
+ raise ValueError(f"AxisSize min must be non-negative, but got {self.min}")
+ if self.max is not None and self.max < self.min:
+ raise ValueError(
+ f"AxisSize max must be greater than or equal to min, but got min={self.min}, max={self.max}"
+ )
+ if self.step is not None and self.step <= 0:
+ raise ValueError(f"AxisSize step must be positive, but got {self.step}")
@dataclass
@@ -106,9 +144,7 @@ class AxisInfo(Axis):
size: AxisSize
@classmethod
- def create(
- cls, axis: AxisLike, size: Optional[Union[int, AxisSize]] = None
- ) -> AxisInfo:
+ def create(cls, axis: AxisLike, size: int | AxisSize | None = None) -> AxisInfo:
if isinstance(axis, AxisInfo):
return axis
@@ -142,3 +178,32 @@ 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: 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] = []
+
+ for d in dims:
+ d = str(d).lower()
+ if d in ("batch", "b"):
+ single_letter_dims.append("b")
+ elif d in ("time", "t"):
+ single_letter_dims.append("t")
+ elif d in ("index", "i"):
+ single_letter_dims.append("i")
+ elif d in ("channel", "c"):
+ single_letter_dims.append("c")
+ elif d in "zyx":
+ single_letter_dims.append(d)
+ else:
+ # Return original dims if any dim cannot be converted to a single letter
+ return tuple(dims)
+
+ 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/backends/__init__.py b/src/bioimageio/core/backends/__init__.py
index 7fc53d589..d431df00c 100644
--- a/src/bioimageio/core/backends/__init__.py
+++ b/src/bioimageio/core/backends/__init__.py
@@ -1,10 +1,6 @@
-from typing import (
- List,
- Optional,
- Sequence,
- Tuple,
- Union,
-)
+from __future__ import annotations
+
+from collections.abc import Sequence
from exceptiongroup import ExceptionGroup
from typing_extensions import assert_never
@@ -15,7 +11,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 +22,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 +34,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 fbdfe498d..50de52d8e 100644
--- a/src/bioimageio/core/backends/keras_backend.py
+++ b/src/bioimageio/core/backends/keras_backend.py
@@ -1,8 +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, Sequence, Tuple
+from typing import Any
from keras.src.legacy.saving import ( # pyright: ignore[reportMissingTypeStubs]
legacy_h5_format,
@@ -15,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
@@ -25,7 +28,7 @@
# TODO: check if we should prefer keras
try:
import tensorflow as tf
- from tensorflow import keras
+ from tensorflow import keras # pyright: ignore[reportMissingModuleSource]
tf_version = Version(tf.__version__) # pyright: ignore[reportUnknownArgumentType]
except Exception:
@@ -35,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(
@@ -99,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 0dab80c12..7290d4b7c 100644
--- a/src/bioimageio/core/backends/onnx_backend.py
+++ b/src/bioimageio/core/backends/onnx_backend.py
@@ -1,9 +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, Sequence, Union, cast
+from typing import Any, cast
import onnxruntime as rt # pyright: ignore[reportMissingTypeStubs]
from loguru import logger
@@ -12,26 +15,24 @@
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]):
+class ONNXModelAdapter(LocalModelAdapter[str | None, 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]]:
+ 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())
@@ -69,7 +70,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)
@@ -135,10 +136,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):
@@ -149,9 +150,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 2d4634522..5472e4ba2 100644
--- a/src/bioimageio/core/backends/pytorch_backend.py
+++ b/src/bioimageio/core/backends/pytorch_backend.py
@@ -1,9 +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, Mapping, Optional, Sequence, Tuple, Union
+from typing import Any, Literal
import torch
from loguru import logger
@@ -17,8 +20,9 @@
from bioimageio.spec.utils import download
from .._model_adapter import LocalModelAdapter
+from .._utils.type_guards import is_list, is_ndarray, is_tuple
+from .._utils.type_hints import cast_and_assert
from ..digest_spec import import_callable
-from ..utils._type_guards import is_list, is_ndarray, is_tuple
@runtime_checkable
@@ -32,15 +36,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"""
@@ -52,7 +56,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:
@@ -62,9 +66,7 @@ def __init__(
self._mode: Literal["eval", "train"] = mode
super().__init__(model_description=model_description, devices=devices)
- def _parse_devices(
- self, devices: Optional[Sequence[str]]
- ) -> 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:
@@ -83,8 +85,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
]
@@ -104,14 +106,12 @@ 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)
elif isinstance(r, torch.Tensor):
- r_np: NDArray[Any] = ( # pyright: ignore[reportUnknownVariableType]
- r.detach().cpu().numpy()
- )
+ r_np: NDArray[Any] = r.detach().cpu().numpy()
result.append(r_np)
elif is_ndarray(r):
result.append(r)
@@ -132,12 +132,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,
@@ -163,7 +161,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)
@@ -182,39 +180,59 @@ 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:
model = model.to(devices[0])
- if isinstance(path, (Path, ZipPath)):
- ctxt = path.open("rb")
- else:
- ctxt = nullcontext(BytesIO(path.read()))
+ if path.suffix == ".safetensors":
+ if isinstance(path, Path):
+ from safetensors.torch import load_file # pyright: ignore[reportUnknownVariableType]
+
+ state = load_file(path, device=str(devices[0]))
+ elif isinstance(path, (ZipPath, BytesReader)):
+ from safetensors.torch import load
- with ctxt as f:
- assert not isinstance(f, TextIOWrapper)
- if Version(str(torch.__version__)) < Version("1.13"):
- state = torch.load(f, map_location=devices[0])
+ if isinstance(path, ZipPath):
+ data = cast_and_assert(bytes, path.read_bytes())
+ else:
+ data = path.read()
+
+ state = {k: v.to(devices[0]) for k, v in load(data).items()}
else:
- try:
- state = torch.load(f, map_location=devices[0], weights_only=True)
- except Exception as e:
- msg = (
- f"Failed to load weights with `weights_only=True`: {e}\n\n"
- + "This usually means the weights file contains non-tensor objects"
- + " (e.g. numpy arrays, custom classes, or nested dicts with"
- + " metadata). The BioImage.IO spec requires a pure state dict —"
- + " an OrderedDict mapping parameter names to tensors only.\n\n"
- + "To fix this, extract only the state dict from your checkpoint:\n\n"
- + " import torch\n"
- + " checkpoint = torch.load('original.pth', weights_only=False)\n"
- + " # Inspect keys, e.g.: checkpoint.keys()"
- + " -> dict_keys(['model', 'optimizer', ...])\n"
- + " torch.save(checkpoint['model'], 'weights.pt')\n\n"
- + "Then reference 'weights.pt' in your bioimageio.yaml."
- )
- raise ValueError(msg) from e
+ assert_never(path)
+
+ else:
+ if isinstance(path, (Path, ZipPath)):
+ ctxt = path.open("rb")
+ elif isinstance(path, BytesReader):
+ ctxt = nullcontext(BytesIO(path.read()))
+ else:
+ assert_never(path)
+
+ with ctxt as f:
+ assert not isinstance(f, TextIOWrapper)
+ if Version(str(torch.__version__)) < Version("1.13"):
+ state = torch.load(f, map_location=devices[0])
+ else:
+ try:
+ state = torch.load(f, map_location=devices[0], weights_only=True)
+ except Exception as e:
+ msg = (
+ f"Failed to load weights with `weights_only=True`: {e}\n\n"
+ + "This usually means the weights file contains non-tensor objects"
+ + " (e.g. numpy arrays, custom classes, or nested dicts with"
+ + " metadata). The BioImage.IO spec requires a pure state dict —"
+ + " an OrderedDict mapping parameter names to tensors only.\n\n"
+ + "To fix this, extract only the state dict from your checkpoint:\n\n"
+ + " import torch\n"
+ + " checkpoint = torch.load('original.pth', weights_only=False)\n"
+ + " # Inspect keys, e.g.: checkpoint.keys()"
+ + " -> dict_keys(['model', 'optimizer', ...])\n"
+ + " torch.save(checkpoint['model'], 'weights.pt')\n\n"
+ + "Then reference 'weights.pt' in your bioimageio.yaml."
+ )
+ raise ValueError(msg) from e
incompatible = model.load_state_dict(state, strict=strict)
if (
@@ -240,8 +258,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 263dc8783..374b99e1c 100644
--- a/src/bioimageio/core/backends/tensorflow_backend.py
+++ b/src/bioimageio/core/backends/tensorflow_backend.py
@@ -1,5 +1,8 @@
+from __future__ import annotations
+
+from collections.abc import Sequence
from pathlib import Path
-from typing import Any, List, Optional, Sequence, Tuple, Union
+from typing import Any
import numpy as np
import tensorflow as tf
@@ -19,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:
@@ -34,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(
@@ -82,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
@@ -101,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")
@@ -126,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}"
@@ -158,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 = [
@@ -177,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 f1f81ed35..fdcdeb308 100644
--- a/src/bioimageio/core/backends/torchscript_backend.py
+++ b/src/bioimageio/core/backends/torchscript_backend.py
@@ -1,6 +1,9 @@
# pyright: reportUnknownVariableType=false
+from __future__ import annotations
+
import gc
-from typing import Any, List, Optional, Sequence, Union
+from collections.abc import Sequence
+from typing import Any
import torch
from loguru import logger
@@ -9,15 +12,15 @@
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
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(
@@ -27,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: Optional[Sequence[str]]
- ) -> 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:
@@ -46,8 +47,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 69af9118a..fab0e338f 100644
--- a/src/bioimageio/core/block.py
+++ b/src/bioimageio/core/block.py
@@ -1,117 +1,1111 @@
+from __future__ import annotations
+
+import itertools
+from collections.abc import Callable, 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 (
- Any,
- Generator,
- Iterable,
- Optional,
- Tuple,
- Union,
+ Annotated,
+ Generic,
+ Literal,
+ TypeVar,
+ cast,
)
-from typing_extensions import Self
+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 .axis import PerAxis
-from .block_meta import BlockMeta, LinearAxisTransform, split_shape_into_blocks
+from ._axis_annotations import PerAxisAnno
+from ._settings import settings
+from ._utils.type_hints import NestedList, cast_and_assert
+from .axis import TIME_AXIS_ID, AxisId, AxisSize, PerAxis
from .common import (
+ BlockLoc,
Halo,
HaloLike,
+ MemberId,
PadMode,
+ PadWidth,
+ PerMember,
SliceInfo,
- TotalNumberOfBlocks,
+ SliceInfoWithOverlap,
)
-from .tensor import Tensor
+from .tensor import Array, Tensor
-@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=True)
+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 axis {a}: 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)
+ }
+ )
- @classmethod
- def from_sample_member(
- cls,
- sample_member: Tensor,
- block: BlockMeta,
- *,
- pad_mode: PadMode,
- ) -> Self:
- return cls(
- data=sample_member[block.outer_slice].pad(block.padding, pad_mode),
- sample_shape=sample_member.tagged_shape,
- inner_slice=block.inner_slice,
- halo=block.halo,
- block_index=block.block_index,
- blocks_in_sample=block.blocks_in_sample,
- )
-
- def get_transformed(
- self, new_axes: PerAxis[Union[LinearAxisTransform, int]]
- ) -> Self:
- raise NotImplementedError
-
- @classmethod
- def from_meta(cls, meta: BlockMeta, data: Tensor) -> Self:
- return cls(
- sample_shape={
- k: data.tagged_shape[k] if v == -1 else v
- for k, v in meta.sample_shape.items()
- },
- inner_slice={
- k: (
- SliceInfo(start=v.start, stop=data.tagged_shape[k])
- if v.stop == -1
- else v
+ @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 k, v in meta.inner_slice.items()
- },
- halo=meta.halo,
- block_index=meta.block_index,
- blocks_in_sample=meta.blocks_in_sample,
- data=data,
+ for a in self.inner_slice
+ }
)
- def get_meta(self) -> BlockMeta:
- return BlockMeta(
- sample_shape=self.sample_shape,
- inner_slice=self.inner_slice,
- halo=self.halo,
- block_index=self.block_index,
- blocks_in_sample=self.blocks_in_sample,
+ @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) -> tuple[AxisId, ...]:
+ return tuple(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()
+ }
+
+ @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"
+ )
+
+ 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,
+ )
+
+ # 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=True)
+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
+ """The padding mode to use when the halo extends beyond the tensor boundary."""
+
+ @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:
+ outer_tensor = outer_tensor.pad(self.padding, mode=self.pad_mode)
+
+ return outer_tensor
+
+
+_GridItem = TypeVar("_GridItem")
+
+
+@dataclass(frozen=True, slots=True)
+class NestedListWithNamedDims(Generic[_GridItem]):
+ """A nested list of values with names for each nesting dimension.
+
+ `dims` maps each nested-list level in `values` to an axis.
+ """
+
+ dims: tuple[AxisId, ...]
+ """Axis corresponding to each nested-list level in `values`."""
+
+ values: NestedList[_GridItem]
+ """Values arranged in a rectangular nested list whose dimensions are named by `dims`."""
+
+
+BlockArray = Annotated[NDArray[np.object_], "NDArray[Block]"]
+"""An n-dimensional object array of tensor `Block`s.
+
+Unlike `NestedListWithNamedDims`, its Block elements have the same dimensionality as the overall array.
+
+I.e. all array dimensions are explicitly present in the `Block` objects, even if they have length 1.
+This is in contrast to a `NestedListWithNamedDims[Blocks]`, where each block has the same dimensions,
+but the nested list structure may skip singleton dimensions and add additional dimensions.
+"""
+
def split_tensor_into_blocks(
tensor: Tensor,
- block_shape: PerAxis[int],
*,
+ block_shape: PerAxis[int | AxisSize],
halo: PerAxis[HaloLike],
- stride: Optional[PerAxis[int]] = None,
pad_mode: PadMode,
-) -> 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
+ pad_boundary: Literal["allow", True, False] = True,
+) -> BlockArray:
+ """Split a tensor, optionally padding single blocks to a valid axis size.
+
+ Args:
+ tensor: the tensor to split into blocks
+ block_shape: the block shape, 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.
+ halo: the halo around each block, omitted axes are treated as having a halo of 0.
+ pad_mode: the padding mode to use when the halo extends beyond the tensor boundary.
+ pad_boundary:
+ True: pad boundary halos, possibly more to make a single block valid;
+ False: forbid boundary padding
+ "allow": pad only to make a single block valid, no to account for boundary halos.
+ """
+ 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}"
+ )
+
+ # fill in default halo (0)
+ halo = {d: Halo.create(halo.get(d, 0)) for d in tensor.dims}
+
+ main_block_shape: dict[AxisId, int] = {}
+ single_block_dims: set[AxisId] = set()
+ for d, s in tensor.tagged_shape.items():
+ outer_size = s + (sum(halo[d]) if pad_boundary is True else 0)
+ bs = block_shape.get(d, outer_size)
+ if isinstance(bs, AxisSize):
+ step = bs.step or 1
+ padded_size = bs.min + max(0, ceil((outer_size - bs.min) / step)) * step
+ limit = min(outer_size, bs.max or outer_size)
+ main_block_shape[d] = limit - (limit - bs.min) % step
+ fits_single_block = bs.max is None or padded_size <= bs.max
+ else:
+ padded_size = bs
+ main_block_shape[d] = min(bs, outer_size) if pad_boundary is False else bs
+ fits_single_block = outer_size <= bs
+
+ if pad_boundary is not False and fits_single_block:
+ main_block_shape[d] = padded_size
+ single_block_dims.add(d)
+
+ if large_block_dims := {
+ a
+ for a, bs in block_shape.items()
+ if main_block_shape.get(a, 1) < (bs.min if isinstance(bs, AxisSize) else bs)
+ }:
+ raise ValueError(
+ f"Block shape {block_shape} larger than tensor shape + halo {main_block_shape} along {large_block_dims}.",
+ )
+
+ inner_1d_slices: dict[AxisId, list[SliceInfo | SliceInfoWithOverlap]] = {}
+ inner_sizes = {d: main_block_shape[d] - sum(halo[d]) for d in tensor.dims}
+ for d in tensor.dims:
+ inner_first_start = halo[d].left if pad_boundary is not True else 0
+ inner_last_stop = tensor.tagged_shape[d] - (
+ halo[d].right if pad_boundary is not True else 0
+ )
+ inner_size = inner_sizes[d]
+ if inner_size <= 0:
+ raise ValueError(f"Block size must be larger than halo along {d}")
+ if d in single_block_dims:
+ padded_stop = inner_first_start + inner_size
+ inner_1d_slices[d] = [
+ SliceInfo(inner_first_start, inner_last_stop)
+ if padded_stop == inner_last_stop
+ else SliceInfoWithOverlap(
+ inner_first_start, padded_stop, inner_first_start, inner_last_stop
+ )
+ ]
+ continue
+
+ 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
+
+ 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)
+ 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, pad_mode=pad_mode
+ )
+
+ return out
+
+
+def split_multiple_tensors_into_blocks(
+ tensors: PerMember[Tensor],
+ block_shape: PerMember[PerAxis[int | AxisSize]],
+ *,
+ halo: PerMember[PerAxis[HaloLike]],
+ broadcast: bool = False,
+ pad_mode: PadMode | PerMember[PadMode],
+ pad_boundary: Literal["allow", True, False] = True,
+) -> 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).
+ 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.
+ pad_boundary:
+ True: pad boundary halos, possibly more to make a single block valid;
+ False: forbid boundary padding
+ "allow": pad only to make a single block valid, no to account for boundary halos.
+ 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]:
+ 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,
+ pad_boundary=pad_boundary,
+ )
+
+ 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 | AxisSize]],
+ pad_mode: PadMode | PerMember[PadMode],
+ pad_boundary: Literal["allow", True, False] = True,
+ input_halo: PerMember[PerAxis[Halo]],
+ output_halo: PerMember[PerAxis[Halo]],
+ get_output_shape: Callable[[PerMember[PerAxis[int]]], PerMember[PerAxis[int]]],
+ 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.
+
+
+ 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.
+ May be expressed using `AxisSize` objects to allow for variable block shapes.
+ Missing axes are treated as having a block size equal to the tensor axis size.
+ pad_mode:
+ The padding mode (optionally per input member) to use when the halo extends beyond the tensor boundary
+ pad_boundary:
+ True: pad boundary halos, possibly more to make a single block valid;
+ False: forbid boundary padding
+ "allow": pad only to make a single block valid, no to account for boundary halos.
+ 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.)
+ 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.
+ 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 chunks to avoid large task graphs.
+ """
+ # add missing block dims explicitly
+ block_shape = {
+ m: {d: block_shape.get(m, {}).get(d, t.tagged_shape[d]) for d in t.dims}
+ for m, t in inputs.items()
+ }
+
+ 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)
+ if pad_boundary is True:
+ max_tensor_size += 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=max_block_shape,
+ chunk_min_bytes=chunk_shape,
+ )
+
+ if any(
+ max_block_shape[m][d] > chunk_shape[m][d]
+ for m in chunk_shape
+ for d in chunk_shape[m]
+ if m in max_block_shape and d in max_block_shape[m]
+ ):
+ raise ValueError(
+ f"block_shape {block_shape} must not be larger than chunk_shape {chunk_shape}"
+ )
+
+ if blockwise_processing_mode == "auto":
+ n_input_chunks = max(
+ prod(
+ ceil(
+ inputs[m].tagged_shape[d]
+ / (
+ bs
+ if isinstance((bs := block_shape[m][d]), int)
+ else bs.max or bs.min
+ )
+ )
+ for d in block_shape[m]
+ )
+ for m in block_shape
+ )
+ if n_input_chunks > 100:
+ blockwise_processing_mode = "sequential"
+ else:
+ blockwise_processing_mode = "parallel"
+
+ logger.info(
+ "using blockwise_processing_mode='{}' within each of the {} input chunks",
+ blockwise_processing_mode,
+ n_input_chunks,
+ )
+
+ process_chunk = partial(
+ _apply_blockwise_impl,
+ func,
+ block_shape=block_shape,
+ pad_mode=pad_mode,
+ pad_boundary="allow" if pad_boundary is not False else False,
+ input_halo=input_halo,
+ output_halo=output_halo,
+ processing_mode=blockwise_processing_mode,
+ get_output_shape=get_output_shape,
+ )
+
+ return _apply_blockwise_impl(
+ process_chunk,
+ inputs,
+ block_shape=chunk_shape,
+ pad_mode=pad_mode,
+ pad_boundary=pad_boundary,
+ input_halo=input_halo,
+ output_halo=output_halo,
+ processing_mode="parallel",
+ 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 | AxisSize]],
+ pad_mode: PadMode | PerMember[PadMode],
+ pad_boundary: Literal["allow", True, False] = True,
+ 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. 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
+ pad_boundary:
+ True: pad boundary halos, possibly more to make a single block valid;
+ False: forbid boundary padding
+ "allow": pad only to make a single block valid, no to account for boundary halos.
+ 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=input_halo,
+ broadcast=True,
+ pad_mode=pad_mode,
+ pad_boundary=pad_boundary,
+ )
+
+ return apply_to_block_array(
+ func,
+ input_blocks,
+ output_halo=output_halo,
+ keep_output_halo_at_sample_edge=pad_boundary is not True,
+ processing_mode=processing_mode,
+ get_output_shape=get_output_shape,
+ )
+
+
+def apply_to_block_array(
+ func: Callable[[PerMember[Tensor]], PerMember[Tensor | None]],
+ 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]:
+
+ output_grid = apply_to_blocks(
+ func,
+ input_blocks,
+ output_halo=output_halo,
+ get_output_shape=get_output_shape,
+ keep_output_halo_at_sample_edge=keep_output_halo_at_sample_edge,
)
- return n_blocks, _block_generator(tensor, block_gen, pad_mode=pad_mode)
+ nested_outputs = output_grid.values
+
+ def select_arrays(
+ nested: NestedList[dict[MemberId, Tensor]], member: MemberId
+ ) -> NestedList[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]
+
+ if processing_mode == "parallel":
+ concatenate = cast(Callable[[Sequence[Array], int], Array], da.concatenate)
+ elif processing_mode == "sequential":
+ concatenate = cast(Callable[[Sequence[Array], int], Array], np.concatenate)
+ else:
+ assert_never(processing_mode)
+
+ def concatenate_blocks(
+ nested: NestedList[Array], output_dims: Sequence[AxisId], level: int = 0
+ ) -> Array:
+ if not isinstance(nested, list):
+ return nested
+
+ arrays = [concatenate_blocks(item, output_dims, level + 1) for item in nested]
+ if len(arrays) == 1:
+ return arrays[0]
+
+ grid_dim = output_grid.dims[level]
+ if grid_dim not in output_dims:
+ raise ValueError(
+ f"Cannot concatenate blocks along input axis {grid_dim!r}, which is "
+ + f"missing from output axes {tuple(output_dims)}"
+ )
+
+ return concatenate(arrays, output_dims.index(grid_dim))
+
+ outputs = {
+ output_id: Tensor(
+ concatenate_blocks(
+ select_arrays(nested_outputs, output_id),
+ first_block[output_id].dims,
+ ),
+ dims=first_block[output_id].dims,
+ )
+ for output_id in first_block
+ }
+ for output_id, tensor in outputs.items():
+ tensor.set_metadata(first_block[output_id].get_metadata())
+ return outputs
+
+
+def apply_to_blocks(
+ func: Callable[[PerMember[Tensor]], PerMember[Tensor | None]],
+ blocks: PerMember[BlockArray],
+ *,
+ output_halo: PerMember[PerAxis[HaloLike]],
+ get_output_shape: Callable[[PerMember[PerAxis[int]]], PerMember[PerAxis[int]]],
+ keep_output_halo_at_sample_edge: bool,
+) -> NestedListWithNamedDims[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()))
+ 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()
+ }
+
+ 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
+ }
+ 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(input_members).items():
+ if t is None:
+ continue
+
+ # crop halo to keep inner output
+ 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(0, overlap) if a in right_input_overlap else 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 | right_input_overlap, (
+ f"need to crop overlap {overlap_halo} from {t.tagged_shape} to "
+ + f"{valid_output_shape[m]}, but expected overlap in "
+ + f"{left_input_overlap | right_input_overlap}"
+ )
+ t = t.crop_halo(overlap_halo)
+
+ ret[m] = t
+
+ return ret
+
+ return NestedListWithNamedDims(dims=block_grid_dims, values=func_blocks())
+
+
+def get_chunk_shape(
+ tensors: PerMember[Tensor],
+ block_shape: PerMember[PerAxis[int]],
+ 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(
+ f"Block shape specified for unknown tensors: {unknown_members}."
+ )
+
+ # default to tensor shape for missing block dimensions or block dimensions set to -1
+ 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} }"
+ )
+
+ if inconsistent := {
+ d: f"{s} != {bs[d]} (from {m})"
+ for m, bs in block_shape.items()
+ for d, s in dim_sizes.items()
+ if d in bs and bs[d] != s
+ }:
+ raise ValueError(
+ f"Block shape has inconsistent sizes across members: {inconsistent}"
+ )
+ chunk_shape = deepcopy(block_shape)
+ item_sizes = {m: np.dtype(t.dtype).itemsize for m, t in tensors.items()}
-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 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)
+ for _ in range(1_000_000):
+ for a in list(incr_dims):
+ if get_max_chunk_size() >= chunk_min_bytes:
+ return
+ max_chunk_sizes = {
+ m: max(block_shape[m][a], tensors[m].tagged_shape[a])
+ for m in tensors
+ if a in tensors[m].tagged_shape
+ }
+ for m in tensors:
+ if a not in tensors[m].dims:
+ continue
+
+ chunk_shape[m][a] = min(
+ max_chunk_sizes[m],
+ chunk_shape[m][a] + block_shape[m][a],
+ )
+
+ if all(
+ chunk_shape[m][a] == max_chunk_sizes[m]
+ for m in chunk_shape
+ if a in chunk_shape[m]
+ ):
+ incr_dims.remove(a)
+
+ if not incr_dims:
+ break
+
+ # increment chunk dimensions in order of non-singleton spatial, time, other, then any singleton dim
+ if dim_sizes.get(AxisId("z"), 0) <= 1:
+ non_singleton_xy = [
+ d for d in (AxisId("x"), AxisId("y")) if dim_sizes.get(d, 0) > 1
+ ]
+ increment_chunk_dims(non_singleton_xy)
+
+ if dim_sizes.get((d := AxisId("z")), 0) > 1:
+ increment_chunk_dims((d,))
+ else:
+ non_singleton_space_dims = [
+ d
+ for d in (AxisId("x"), AxisId("y"), AxisId("z"))
+ if dim_sizes.get(d, 0) > 1
+ ]
+ increment_chunk_dims(non_singleton_space_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 {} ({} {}) for block shape {} and sample shape {}",
+ chunk_shape,
+ c
+ if (c := get_max_chunk_size()) < 1024
+ else c // 1024
+ if c < 1024**2
+ else c // 1024**2
+ if c < 1024**3
+ else c // 1024**3,
+ "B" if c < 1024 else "KiB" if c < 1024**2 else "MiB" if c < 1024**3 else "GiB",
+ block_shape,
+ {m: dict(t.tagged_shape) for m, t in tensors.items()},
+ )
+ return chunk_shape
diff --git a/src/bioimageio/core/block_meta.py b/src/bioimageio/core/block_meta.py
deleted file mode 100644
index d4ab3c873..000000000
--- a/src/bioimageio/core/block_meta.py
+++ /dev/null
@@ -1,399 +0,0 @@
-import itertools
-from dataclasses import dataclass
-from functools import cached_property
-from math import floor, prod
-from types import MappingProxyType
-from typing import (
- Any,
- Callable,
- Collection,
- Dict,
- Generator,
- Iterable,
- List,
- Optional,
- Tuple,
- Union,
-)
-
-import pydantic
-from loguru import logger
-from typing_extensions import Self
-
-from ._axis_annotations import PerAxisAnno
-from .axis import AxisId, PerAxis
-from .common import (
- BlockIndex,
- Halo,
- HaloLike,
- MemberId,
- PadWidth,
- PerMember,
- SliceInfo,
- TotalNumberOfBlocks,
-)
-
-
-@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]
- """inner region (without halo) wrt the 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()
- }
- )
-
- @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[Union[LinearAxisTransform, int]]
- ) -> Self:
- 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),
- )
- )
- for a, trf in new_axes.items()
- },
- 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],
- block_shape: PerAxis[int],
- halo: PerAxis[HaloLike],
- stride: Optional[PerAxis[int]] = 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(
- f"unknown axes in block_shape: {unknown_axes} for shape {shape}"
- )
-
- if any(shape[a] < block_shape[a] for a in block_shape):
- # TODO: allow larger blockshape
- raise ValueError(f"shape {shape} is smaller than block shape {block_shape}")
-
- 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 = {}
-
- 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)
- ]
-
- n_blocks = prod(map(len, inner_1d_slices.values()))
-
- return n_blocks, _block_meta_generator(
- shape,
- blocks_in_sample=n_blocks,
- inner_1d_slices=inner_1d_slices,
- halo=halo,
- )
-
-
-def _block_meta_generator(
- sample_shape: PerAxis[int],
- *,
- 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}
- for i, nd_tile in enumerate(itertools.product(*inner_1d_slices.values())):
- inner_slice: PerAxis[SliceInfo] = dict(zip(inner_1d_slices, nd_tile))
-
- yield BlockMeta(
- sample_shape=sample_shape,
- inner_slice=inner_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]],
- *,
- halo: PerMember[PerAxis[HaloLike]],
- strides: Optional[PerMember[PerAxis[int]]] = None,
- 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}."
- )
-
- 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:
- n_blocks[t], blocks[t] = split_shape_into_blocks(
- shape=shapes[t],
- block_shape=block_shapes[t],
- halo=halo.get(t, {}),
- stride=strides.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
diff --git a/src/bioimageio/core/cli.py b/src/bioimageio/core/cli.py
index 0a8dd8a0f..67c1d1450 100644
--- a/src/bioimageio/core/cli.py
+++ b/src/bioimageio/core/cli.py
@@ -4,12 +4,15 @@
to place the added '(default: ...)' on a new line.
"""
+from __future__ import annotations
+
import json
import shutil
import subprocess
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
@@ -18,17 +21,7 @@
from typing import (
Annotated,
Any,
- Dict,
- Iterable,
- List,
Literal,
- Mapping,
- Optional,
- Sequence,
- Set,
- Tuple,
- Type,
- Union,
)
import numpy as np
@@ -73,6 +66,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
@@ -87,16 +81,16 @@
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
+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
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(
@@ -116,7 +110,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"),
@@ -130,7 +124,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)
@@ -149,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", getattr(self.descr, "name")))
+ return str(
+ (self.descr.model_extra or {}).get("id")
+ or (self.descr.model_extra or {}).get("name", "unknown")
+ )
nickname = None
if (
@@ -197,12 +194,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
@@ -213,7 +210,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"
@@ -224,7 +221,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.
@@ -266,7 +263,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(
@@ -282,6 +279,7 @@ def _get_stat(
dataset: Iterable[Sample],
dataset_length: int,
stats_path: Path,
+ dataset_fits_in_memory: bool,
) -> Stat:
req_dataset_meas, _ = get_required_dataset_measures(model_descr)
if not req_dataset_meas:
@@ -298,20 +296,22 @@ def _get_stat(
return stat
- stats_calc = StatsCalculator(req_dataset_meas)
+ stats_calc = StatsCalculator(
+ req_dataset_meas, dataset_fits_in_memory=dataset_fits_in_memory
+ )
for sample in tqdm(
dataset, total=dataset_length, desc="precomputing dataset stats", unit="sample"
):
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.
@@ -319,7 +319,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).
@@ -332,7 +332,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):
@@ -431,7 +431,11 @@ def updated(self):
class PredictCmd(CmdBase, WithSource):
"""Run inference on your data with a bioimage.io model."""
- inputs: NotEmpty[List[Union[str, NotEmpty[List[str]]]]] = Field(
+ @cached_property
+ def descr(self):
+ return load_description(self.source, perform_io_checks=False)
+
+ inputs: NotEmpty[list[str | NotEmpty[list[str]]]] = Field(
default_factory=lambda: ["{input_id}/001.tif"],
validation_alias=AliasChoices("inputs", "input"),
)
@@ -467,7 +471,7 @@ class PredictCmd(CmdBase, WithSource):
"""
- 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"),
)
@@ -483,7 +487,7 @@ class PredictCmd(CmdBase, WithSource):
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.
@@ -515,12 +519,12 @@ class PredictCmd(CmdBase, WithSource):
)
"""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(
@@ -534,6 +538,12 @@ class PredictCmd(CmdBase, WithSource):
"""
+ dataset_fits_in_memory: bool = False
+ """Allow dataset statistics to be computed in memory.
+
+ Note: Currently this only makes a difference for quantiles that are estimated from sample quantiles for OOM computation.
+ """
+
example: bool = False
"""generate and run an example
@@ -561,7 +571,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)
@@ -579,12 +589,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,
@@ -633,7 +643,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$ "
@@ -644,16 +654,16 @@ def get_example_command(preview: bool, escape: bool = False):
def cli_cmd(self):
try:
for out_sample, out_path in self._yield_predictions(self.blockwise):
- save_sample(out_path, out_sample)
+ _ = save_sample(out_path, out_sample)
except Exception as e:
if not self.blockwise:
raise RuntimeError(
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()
@@ -672,7 +682,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
@@ -708,7 +718,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 +773,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 +791,16 @@ def expand_outputs():
print("🛈 bioimageio prediction preview structure:")
pprint(
{
- "{sample_id}": dict(
- inputs={"{input_id}": ""},
- outputs={"{output_id}": "