Skip to content

remove singledispatch for better static typing #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# core-bioimage-io-python

Python specific core utilities for running models in the [BioImage Model Zoo](https://bioimage.io).
Python specific core utilities for [bioimage.io]("https://bioimage.io") resources (in particular models).

## Installation

@@ -67,7 +67,7 @@ mamba activate core
pip install -e . --no-deps
```

There are different environment files that only install tensorflow or pytorch as dependencies available.
There are different environment files available that only install tensorflow or pytorch as dependencies.

## 💻 Command Line

@@ -106,19 +106,31 @@ bioimagei predict-images -m <MODEL> -i <INPUT_PATTERN> - o <OUTPUT_FOLDER>

## From python

`bioimageio.core` is a python library that implements loading models, running prediction with them and more.
To get an overview of this functionality, check out the example notebooks:
`bioimageio.core` is a python package that implements prediction with bioimageio models
including standardized pre- and postprocessing operations.
These models are described by---and can be loaded with---the bioimageio.spec package.

* [example/model_usage](https://github.com/bioimage-io/core-bioimage-io-python/blob/main/example/model_usage.ipynb) for how to load models and run prediction with them
* [example/model_creation](https://github.com/bioimage-io/core-bioimage-io-python/blob/main/example/model_creation.ipynb) for how to create bioimage.io compatible model packages
* [example/dataset_statistics_demo](https://github.com/bioimage-io/core-bioimage-io-python/blob/main/example/dataset_statistics_demo.ipynb) for how to use the dataset statistics for advanced pre-and-postprocessing
In addition bioimageio.core provides functionality to convert model weight formats.

To get an overview of this functionality, check out these example notebooks:

* [model creation/loading with bioimageio.spec](https://github.com/bioimage-io/spec-bioimage-io/blob/main/example_use/load_model_and_create_your_own.ipynb)

## Model Specification

The model specification and its validation tools can be found at <https://github.com/bioimage-io/spec-bioimage-io>.

## Changelog

### 0.6.1

* Fix [#378](https://github.com/bioimage-io/core-bioimage-io-python/pull/378) (with [#379](https://github.com/bioimage-io/core-bioimage-io-python/pull/379))*

### 0.6.0

* add compatibility with new bioimageio.spec 0.5 (0.5.2post1)
* improve interfaces

### 0.5.10

* [Fix critical bug in predict with tiling](https://github.com/bioimage-io/core-bioimage-io-python/pull/359)
2 changes: 1 addition & 1 deletion bioimageio/core/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.6.0"
"version": "0.6.1"
}
49 changes: 18 additions & 31 deletions bioimageio/core/digest_spec.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import importlib.util
from functools import singledispatch
from itertools import chain
from typing import (
Any,
@@ -20,7 +19,7 @@
from numpy.typing import NDArray
from typing_extensions import Unpack, assert_never

from bioimageio.spec._internal.io import HashKwargs, download
from bioimageio.spec._internal.io_utils import HashKwargs, download
from bioimageio.spec.common import FileSource
from bioimageio.spec.model import AnyModelDescr, v0_4, v0_5
from bioimageio.spec.model.v0_4 import CallableFromDepencency, CallableFromFile
@@ -44,44 +43,32 @@
from .tensor import Tensor


@singledispatch
def import_callable(node: type, /) -> Callable[..., Any]:
def import_callable(
node: Union[CallableFromDepencency, ArchitectureFromLibraryDescr],
/,
**kwargs: Unpack[HashKwargs],
) -> Callable[..., Any]:
"""import a callable (e.g. a torch.nn.Module) from a spec node describing it"""
raise TypeError(type(node))


@import_callable.register
def _(node: CallableFromDepencency, **kwargs: Unpack[HashKwargs]) -> Callable[..., Any]:
module = importlib.import_module(node.module_name)
c = getattr(module, str(node.callable_name))
if not callable(c):
raise ValueError(f"{node} (imported: {c}) is not callable")

return c
if isinstance(node, CallableFromDepencency):
module = importlib.import_module(node.module_name)
c = getattr(module, str(node.callable_name))
elif isinstance(node, ArchitectureFromLibraryDescr):
module = importlib.import_module(node.import_from)
c = getattr(module, str(node.callable))
elif isinstance(node, CallableFromFile):
c = _import_from_file_impl(node.source_file, str(node.callable_name), **kwargs)
elif isinstance(node, ArchitectureFromFileDescr):
c = _import_from_file_impl(node.source, str(node.callable), sha256=node.sha256)

else:
assert_never(node)

@import_callable.register
def _(
node: ArchitectureFromLibraryDescr, **kwargs: Unpack[HashKwargs]
) -> Callable[..., Any]:
module = importlib.import_module(node.import_from)
c = getattr(module, str(node.callable))
if not callable(c):
raise ValueError(f"{node} (imported: {c}) is not callable")

return c


@import_callable.register
def _(node: CallableFromFile, **kwargs: Unpack[HashKwargs]):
return _import_from_file_impl(node.source_file, str(node.callable_name), **kwargs)


@import_callable.register
def _(node: ArchitectureFromFileDescr, **kwargs: Unpack[HashKwargs]):
return _import_from_file_impl(node.source, str(node.callable), sha256=node.sha256)


def _import_from_file_impl(
source: FileSource, callable_name: str, **kwargs: Unpack[HashKwargs]
):