Skip to content

Commit

Permalink
Merge pull request #394 from bioimage-io/update_usage
Browse files Browse the repository at this point in the history
Update model usage example
  • Loading branch information
FynnBe authored Jun 11, 2024
2 parents 2b645a7 + 6d6117a commit ebe126b
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 376 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ The model specification and its validation tools can be found at <https://github

## Changelog

### 0.6.7

* `predict()` argument `inputs` may be sample

### 0.6.6

* add aliases to match previous API more closely
Expand Down
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.6"
"version": "0.6.7"
}
3 changes: 2 additions & 1 deletion bioimageio/core/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ def save_sample(path: Union[Path, str], sample: Sample) -> None:
`path` must contain `{member_id}` and may contain `{sample_id}`,
which are resolved with the `sample` object.
"""
path = str(path).format(sample_id=sample.id)
if "{member_id}" not in path:
raise ValueError(f"missing `{{member_id}}` in path {path}")

path = str(path).format(sample_id=sample.id, member_id="{member_id}")

for m, t in sample.members.items():
save_tensor(Path(path.format(member_id=m)), t)
13 changes: 8 additions & 5 deletions bioimageio/core/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def predict(
model: Union[
PermissiveFileSource, v0_4.ModelDescr, v0_5.ModelDescr, PredictionPipeline
],
inputs: PerMember[Union[Tensor, xr.DataArray, NDArray[Any], Path]],
inputs: Union[Sample, PerMember[Union[Tensor, xr.DataArray, NDArray[Any], Path]]],
sample_id: Hashable = "sample",
blocksize_parameter: Optional[
Union[
Expand All @@ -56,7 +56,7 @@ def predict(
Args:
model: model to predict with.
May be given as RDF source, model description or prediction pipeline.
inputs: the named input(s) for this model as a dictionary
inputs: the input sample or the named input(s) for this model as a dictionary
sample_id: the sample id.
blocksize_parameter: (optional) tile the input into blocks parametrized by
blocksize according to any parametrized axis sizes defined in the model RDF
Expand All @@ -82,9 +82,12 @@ def predict(

pp = create_prediction_pipeline(model)

sample = create_sample_for_model(
pp.model_description, inputs=inputs, sample_id=sample_id
)
if isinstance(inputs, Sample):
sample = inputs
else:
sample = create_sample_for_model(
pp.model_description, inputs=inputs, sample_id=sample_id
)

if blocksize_parameter is None:
output = pp.predict_sample_without_blocking(
Expand Down
18 changes: 11 additions & 7 deletions bioimageio/core/proc_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@

from typing_extensions import assert_never

from bioimageio.core.common import MemberId
from bioimageio.core.digest_spec import get_member_ids
from bioimageio.spec.model import AnyModelDescr, v0_4, v0_5
from bioimageio.spec.model.v0_5 import TensorId

from .proc_ops import AddKnownDatasetStats, Processing, UpdateStats, get_proc_class
from .proc_ops import (
AddKnownDatasetStats,
EnsureDtype,
Processing,
UpdateStats,
get_proc_class,
)
from .sample import Sample
from .stat_calculators import StatsCalculator
from .stat_measures import DatasetMeasure, Measure, MeasureValue
Expand Down Expand Up @@ -87,12 +95,8 @@ def _prepare_setup_pre_and_postprocessing(model: AnyModelDescr) -> _SetupProcess
pre_measures: Set[Measure] = set()
post_measures: Set[Measure] = set()

if isinstance(model, v0_4.ModelDescr):
input_ids = {TensorId(str(d.name)) for d in model.inputs}
output_ids = {TensorId(str(d.name)) for d in model.outputs}
else:
input_ids = {d.id for d in model.inputs}
output_ids = {d.id for d in model.outputs}
input_ids = set(get_member_ids(model.inputs))
output_ids = set(get_member_ids(model.outputs))

def prepare_procs(tensor_descrs: Sequence[TensorDescr]):
procs: List[Processing] = []
Expand Down
125 changes: 0 additions & 125 deletions example/dataset_creation.ipynb

This file was deleted.

Loading

0 comments on commit ebe126b

Please sign in to comment.