Coverage for src/bioimageio/core/digest_spec.py: 84%
325 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
1from __future__ import annotations
3import collections.abc
4import importlib.abc
5import importlib.util
6import sys
7from collections.abc import Callable, Iterable, Mapping, Sequence
8from itertools import chain
9from pathlib import Path
10from tempfile import TemporaryDirectory
11from typing import (
12 Any,
13 Final,
14 Literal,
15 TypeAlias,
16)
17from zipfile import ZipFile, is_zipfile
19import dask.array as da
20import numpy as np
21import xarray as xr
22from loguru import logger
23from numpy.typing import NDArray
24from typing_extensions import Unpack, assert_never
26from bioimageio.spec._internal.io import HashKwargs, PermissiveFileSource
27from bioimageio.spec.common import FileDescr, FileSource
28from bioimageio.spec.model import AnyModelDescr, v0_4, v0_5
29from bioimageio.spec.model.v0_4 import CallableFromDepencency, CallableFromFile
30from bioimageio.spec.model.v0_5 import (
31 ArchitectureFromFileDescr,
32 ArchitectureFromLibraryDescr,
33)
34from bioimageio.spec.utils import load_array
36from ._utils.type_hints import cast_and_assert
37from .axis import CHANNEL_AXIS_ID, Axis, AxisId, AxisInfo, PerAxis
38from .block import LinearAxisTransform
39from .common import Halo, MemberId, PerMember, SampleId
40from .io import load_tensor
41from .sample import (
42 Sample,
43)
44from .stat_measures import Stat
45from .tensor import Tensor, TensorMetaData, get_unstacked_dim_order
47TensorSource: TypeAlias = (
48 Tensor | xr.DataArray | NDArray[Any] | da.Array | PermissiveFileSource
49)
52def import_callable(
53 node: ArchitectureFromFileDescr
54 | ArchitectureFromLibraryDescr
55 | CallableFromDepencency
56 | CallableFromFile
57 | v0_5.CustomProcessingDescr,
58 /,
59 **kwargs: Unpack[HashKwargs],
60) -> Callable[..., Any]:
61 """import a callable (e.g. a torch.nn.Module) from a spec node describing it"""
62 if isinstance(node, CallableFromDepencency): 62 ↛ 63line 62 didn't jump to line 63 because the condition on line 62 was never true
63 module = importlib.import_module(node.module_name)
64 c = getattr(module, str(node.callable_name))
65 elif isinstance(node, ArchitectureFromLibraryDescr):
66 module = importlib.import_module(node.import_from)
67 c = getattr(module, str(node.callable))
68 elif isinstance(node, CallableFromFile):
69 c = _import_from_file_impl(node.source_file, str(node.callable_name), **kwargs)
70 elif isinstance(node, (ArchitectureFromFileDescr, v0_5.CustomProcessingDescr)):
71 c = _import_from_file_impl(node.source, str(node.callable), sha256=node.sha256)
72 else:
73 assert_never(node)
75 if not callable(c): 75 ↛ 76line 75 didn't jump to line 76 because the condition on line 75 was never true
76 raise TypeError(f"{node} (imported: {c}) is not callable")
78 return c
81tmp_dirs_in_use: list[TemporaryDirectory[str]] = []
82"""keep global reference to temporary directories created during import to delay cleanup"""
85def _import_from_file_impl(
86 source: FileSource, callable_name: str, **kwargs: Unpack[HashKwargs]
87):
88 src_descr = FileDescr(source=source, **kwargs)
89 # ensure sha is valid even if perform_io_checks=False
90 # or the source has changed since last sha computation
91 src_descr.validate_sha256(force_recompute=True)
92 assert src_descr.sha256 is not None
93 source_sha = src_descr.sha256
95 reader = src_descr.get_reader()
96 # make sure we have unique module name
97 module_name = f"{reader.original_file_name.split('.')[0]}_{source_sha}"
99 # make sure we have a unique and valid module name
100 if not module_name.isidentifier(): 100 ↛ 101line 100 didn't jump to line 101 because the condition on line 100 was never true
101 module_name = f"custom_module_{source_sha}"
102 assert module_name.isidentifier(), module_name
104 source_bytes = reader.read()
106 module = sys.modules.get(module_name)
107 if module is None:
108 try:
109 td_kwargs: dict[str, Any] = (
110 {"ignore_cleanup_errors": True} if sys.version_info >= (3, 10) else {}
111 )
112 if sys.version_info >= (3, 12):
113 td_kwargs["delete"] = False
115 tmp_dir = TemporaryDirectory(**td_kwargs)
116 # keep global ref to tmp_dir to delay cleanup until program exit
117 # TODO: remove for py >= 3.12, when delete=False works
118 tmp_dirs_in_use.append(tmp_dir)
120 module_path = Path(tmp_dir.name) / module_name
121 if reader.original_file_name.endswith(".zip") or is_zipfile(reader): 121 ↛ 122line 121 didn't jump to line 122 because the condition on line 121 was never true
122 module_path.mkdir()
123 ZipFile(reader).extractall(path=module_path)
124 else:
125 module_path = module_path.with_suffix(".py")
126 _ = module_path.write_bytes(source_bytes)
128 importlib_spec = importlib.util.spec_from_file_location(
129 module_name, str(module_path)
130 )
132 if importlib_spec is None: 132 ↛ 133line 132 didn't jump to line 133 because the condition on line 132 was never true
133 raise ImportError(f"Failed to import {source}")
135 module = importlib.util.module_from_spec(importlib_spec)
137 sys.modules[module_name] = module # cache this module
139 assert isinstance(importlib_spec.loader, importlib.abc.ExecutionLoader)
140 importlib_spec.loader.exec_module(module)
142 except Exception as e:
143 if module_name in sys.modules:
144 del sys.modules[module_name]
146 raise ImportError(f"Failed to import {source}") from e
148 try:
149 callable_attr = getattr(module, callable_name)
150 except AttributeError as e:
151 raise AttributeError(
152 f"Imported custom module from {source} has no `{callable_name}` attribute."
153 ) from e
154 except Exception as e:
155 raise AttributeError(
156 f"Failed to access `{callable_name}` attribute from custom module imported from {source} ."
157 ) from e
159 else:
160 return callable_attr
163def get_output_tensor_metadata(
164 model: AnyModelDescr, inputs: PerMember[Tensor]
165) -> dict[MemberId, TensorMetaData]:
166 """Map input metadata like physical scales and units to newly created network outputs."""
167 metadata: dict[MemberId, TensorMetaData] = {}
168 if isinstance(model, v0_4.ModelDescr):
169 return metadata
171 input_axes = {(ipt.id, a.id): a for ipt in model.inputs for a in ipt.axes}
172 for out in model.outputs:
173 scales: dict[AxisId, float] = {}
174 units: dict[AxisId, str] = {}
175 channel_names = None
176 for axis in out.axes:
177 if axis.type == "channel":
178 channel_names = axis.channel_names
179 continue
180 if axis.type not in ("space", "time"):
181 continue
182 if isinstance(axis.size, v0_5.SizeReference):
183 ref = (axis.size.tensor_id, axis.size.axis_id)
184 else:
185 candidates = [key for key in input_axes if key[1] == axis.id]
186 if len(candidates) != 1:
187 continue
188 ref = candidates[0]
190 if ref not in input_axes or ref[0] not in inputs: 190 ↛ 191line 190 didn't jump to line 191 because the condition on line 190 was never true
191 continue
192 tensor = inputs[ref[0]]
193 scale = tensor.get_physical_scale().get(ref[1])
194 if scale is None: 194 ↛ 195line 194 didn't jump to line 195 because the condition on line 194 was never true
195 continue
196 scales[axis.id] = scale * axis.scale / input_axes[ref].scale
197 units[axis.id] = tensor.get_physical_scale_unit()[ref[1]]
199 if scales: 199 ↛ 172line 199 didn't jump to line 172 because the condition on line 199 was always true
200 metadata[out.id] = TensorMetaData(
201 name=str(out.id),
202 channel_names=channel_names,
203 physical_scale=scales,
204 physical_scale_unit=units,
205 )
206 return metadata
209def get_axes_infos(
210 io_descr: v0_4.InputTensorDescr
211 | v0_4.OutputTensorDescr
212 | v0_5.InputTensorDescr
213 | v0_5.OutputTensorDescr,
214) -> list[AxisInfo]:
215 """get a unified, simplified axis representation from spec axes"""
216 return [AxisInfo.create(a) for a in io_descr.axes]
219def get_member_id(
220 tensor_description: v0_4.InputTensorDescr
221 | v0_4.OutputTensorDescr
222 | v0_5.InputTensorDescr
223 | v0_5.OutputTensorDescr,
224) -> MemberId:
225 """get the normalized tensor ID, usable as a sample member ID"""
227 if isinstance(tensor_description, (v0_4.InputTensorDescr, v0_4.OutputTensorDescr)):
228 return MemberId(tensor_description.name)
229 elif isinstance(
230 tensor_description, (v0_5.InputTensorDescr, v0_5.OutputTensorDescr)
231 ):
232 return tensor_description.id
233 else:
234 assert_never(tensor_description)
237def get_member_ids(
238 tensor_descriptions: Iterable[
239 v0_4.InputTensorDescr
240 | v0_4.OutputTensorDescr
241 | v0_5.InputTensorDescr
242 | v0_5.OutputTensorDescr
243 ],
244) -> list[MemberId]:
245 """get normalized tensor IDs to be used as sample member IDs"""
246 return [get_member_id(descr) for descr in tensor_descriptions]
249def get_test_input_sample(
250 model: AnyModelDescr,
251 *,
252 repetitions: Sequence[tuple[int, Axis | AxisInfo, Sequence[float]]] = (),
253) -> Sample:
254 """returns a model's test input sample
256 Args:
257 model: The model for which to create a test input sample.
258 repetitions: A mapping of additional axes (keyed by their index and ID) to sequences of value factors. This adds new dimensions with scaled repetitions of the original test sample that will be stacked into a/the batch dimension.
260 Returns:
261 A test input sample for the given model.
262 """
263 if isinstance(model, v0_4.ModelDescr):
264 info = {
265 MemberId(d.name): (d, t) for d, t in zip(model.inputs, model.test_inputs)
266 }
267 else:
268 info = {d.id: d for d in model.inputs}
270 return _get_test_sample(info, repetitions=repetitions)
273get_test_inputs = get_test_input_sample
274"""DEPRECATED: use `get_test_input_sample` instead"""
277def get_test_output_sample(
278 model: AnyModelDescr,
279 *,
280 repetitions: Sequence[tuple[int, Axis | AxisInfo, Sequence[float]]] = (),
281) -> Sample:
282 """returns a model's test output sample"""
283 if isinstance(model, v0_4.ModelDescr):
284 info = {
285 MemberId(d.name): (d, t) for d, t in zip(model.outputs, model.test_outputs)
286 }
287 else:
288 info = {d.id: d for d in model.outputs}
290 return _get_test_sample(info, repetitions=repetitions)
293get_test_outputs = get_test_output_sample
294"""DEPRECATED: use `get_test_output_sample` instead"""
297def _get_test_sample(
298 info: Mapping[MemberId, v0_5.InputTensorDescr | v0_5.OutputTensorDescr]
299 | Mapping[MemberId, tuple[v0_4.InputTensorDescr, FileSource]]
300 | Mapping[MemberId, tuple[v0_4.OutputTensorDescr, FileSource]],
301 *,
302 repetitions: Sequence[tuple[int, Axis | AxisInfo, Sequence[float]]] = (),
303) -> Sample:
304 arrays: dict[MemberId, NDArray[Any] | da.Array] = {}
305 for m, src in info.items():
306 if isinstance(src, tuple):
307 arrays[m] = load_array(src[1])
308 elif isinstance(src, (v0_5.InputTensorDescr, v0_5.OutputTensorDescr)):
309 if src.test_tensor is None: 309 ↛ 310line 309 didn't jump to line 310 because the condition on line 309 was never true
310 raise ValueError(
311 f"Model input '{m}' has no test tensor defined, cannot create test sample."
312 )
313 arrays[m] = load_array(src.test_tensor)
314 else:
315 assert_never(src)
317 axes: dict[MemberId, list[Axis | AxisInfo]] = {
318 m: list(get_axes_infos(t[0] if isinstance(t, tuple) else t))
319 for m, t in info.items()
320 }
321 tensors: dict[MemberId, Tensor] = {}
322 for m in info:
323 a = arrays[m]
324 ax = axes[m]
325 if repetitions:
326 for rep_idx, rep_ax, factors in repetitions:
327 if rep_idx < -1 or rep_idx >= len(ax): 327 ↛ 328line 327 didn't jump to line 328 because the condition on line 327 was never true
328 raise ValueError(
329 f"Invalid repetition index {rep_idx} for tensor '{m}' with axes {ax}"
330 )
331 # Repeat the array along the specified axis according to the factors
332 repitition_arrays = {
333 factor: cast_and_assert(da.Array, da.from_array(a) * factor)
334 for factor in set(factors)
335 }
336 a = cast_and_assert(
337 da.Array,
338 da.stack(
339 [repitition_arrays[factor] for factor in factors], axis=rep_idx
340 ),
341 )
343 if len(factors) > 100: 343 ↛ 345line 343 didn't jump to line 345 because the condition on line 343 was never true
344 # max 100 chunks along repition axis
345 chunk_size = max(1, len(factors) // 100)
346 chunk_shape = list(a.shape)
347 chunk_shape.insert(rep_idx, chunk_size)
348 a = cast_and_assert(da.Array, a.rechunk(chunk_shape)) # pyright: ignore[reportArgumentType]
350 if isinstance(rep_ax, AxisInfo): 350 ↛ 360line 350 didn't jump to line 360 because the condition on line 350 was always true
351 if len(factors) < rep_ax.size.min: 351 ↛ 352line 351 didn't jump to line 352 because the condition on line 351 was never true
352 raise ValueError(
353 f"Invalid number of repetitions {len(factors)} for axis '{rep_ax.id}' with min size {rep_ax.size.min}"
354 )
355 if rep_ax.size.max is not None and len(factors) > rep_ax.size.max: 355 ↛ 356line 355 didn't jump to line 356 because the condition on line 355 was never true
356 raise ValueError(
357 f"Invalid number of repetitions {len(factors)} for axis '{rep_ax.id}' with max size {rep_ax.size.max}"
358 )
360 ax.insert(rep_idx, rep_ax)
362 tensors[m] = Tensor.from_array(a, dims=ax)
364 return Sample(members=tensors, stat={}, id="test-sample")
367def get_unstacked_output_dim_orders_for_model(
368 model: v0_4.ModelDescr | v0_5.ModelDescr,
369 unstacked_input_order: PerMember[Sequence[AxisId]] | None,
370 stacked_output_dims: PerMember[Sequence[AxisId]] | None,
371) -> PerMember[Sequence[AxisId]]:
372 """Returns the unstacked output dim order for each output tensor of a model,
373 given optional unstacked input dim order and optional output stacked dims."""
374 output_orders: dict[MemberId, Sequence[AxisId]] = {}
375 for out in model.outputs:
376 in_order = None
377 if isinstance(out, v0_5.OutputTensorDescr): 377 ↛ 385line 377 didn't jump to line 385 because the condition on line 377 was always true
378 out_id = out.id
379 stacked_out_order = [ax.id for ax in out.axes]
380 if unstacked_input_order is not None: 380 ↛ 398line 380 didn't jump to line 398 because the condition on line 380 was always true
381 for ax in out.axes:
382 if isinstance(ax.size, v0_5.SizeReference):
383 in_order = unstacked_input_order.get(ax.size.tensor_id)
384 break
385 elif isinstance(out, v0_4.OutputTensorDescr):
386 out_id = MemberId(out.name)
387 stacked_out_order = [AxisId(ax) for ax in out.axes]
388 if unstacked_input_order is not None:
389 for s in out.shape:
390 if isinstance(s, v0_4.ImplicitOutputShape):
391 in_order = unstacked_input_order.get(
392 MemberId(s.reference_tensor)
393 )
394 break
395 else:
396 assert_never(out)
398 output_orders[out_id] = get_unstacked_dim_order(
399 stacked_out_order,
400 unstacked_reference_order=in_order,
401 stacked_dims=stacked_output_dims.get(out_id)
402 if stacked_output_dims is not None
403 else None,
404 )
406 return output_orders
409def get_input_halo(
410 model: v0_5.ModelDescr, output_halo: PerMember[PerAxis[Halo]] | None = None
411):
412 """returns which halo input tensors need to be divided into blocks with, such that
413 `output_halo` can be cropped from their outputs without introducing gaps."""
414 input_halo: dict[MemberId, dict[AxisId, Halo]] = {}
415 outputs = {t.id: t for t in model.outputs}
416 all_tensors = {**{t.id: t for t in model.inputs}, **outputs}
417 if output_halo is None:
418 output_halo = {
419 t.id: {
420 a.id: Halo(a.halo, a.halo)
421 for a in t.axes
422 if isinstance(a, v0_5.WithHalo)
423 }
424 for t in model.outputs
425 }
427 for t, th in output_halo.items():
428 axes = {a.id: a for a in outputs[t].axes}
430 for a, ah in th.items():
431 s = axes[a].size
432 if not isinstance(s, v0_5.SizeReference): 432 ↛ 433line 432 didn't jump to line 433 because the condition on line 432 was never true
433 raise TypeError(
434 f"Unable to map output halo for {t}.{a} to an input axis"
435 )
437 axis = axes[a]
438 ref_axis = {a.id: a for a in all_tensors[s.tensor_id].axes}[s.axis_id]
440 input_halo_left = ah.left * axis.scale / ref_axis.scale
441 input_halo_right = ah.right * axis.scale / ref_axis.scale
442 assert input_halo_left == int(input_halo_left), f"{input_halo_left} not int"
443 assert input_halo_right == int(input_halo_right), (
444 f"{input_halo_right} not int"
445 )
447 input_halo.setdefault(s.tensor_id, {})[a] = Halo(
448 int(input_halo_left), int(input_halo_right)
449 )
451 return input_halo
454def get_block_transform(
455 model: v0_5.ModelDescr,
456) -> PerMember[PerAxis[LinearAxisTransform | int]]:
457 """returns how a model's output tensor shapes relates to given input shapes"""
458 ret: dict[MemberId, dict[AxisId, LinearAxisTransform | int]] = {}
459 batch_axis_trf = None
460 for ipt in model.inputs:
461 for a in ipt.axes:
462 if a.type == "batch":
463 batch_axis_trf = LinearAxisTransform(
464 member=ipt.id, axis=a.id, scale=1, offset=0
465 )
466 break
467 if batch_axis_trf is not None:
468 break
469 axis_scales = {
470 t.id: {a.id: a.scale for a in t.axes}
471 for t in chain(model.inputs, model.outputs)
472 }
473 # a common inaccuracy in bioimage.io model specs is to define a fixed output size equal to a correspoding fixed input size, while the output size should actually be a size reference to the fixed input size to encode the relationship between input and output sizes. The following dict is used to patch such cases, by replacing the fixed output size with a size reference to the corresponding fixed input size.
474 patched_size_ref_for_fixed_output_size_matching_fixed_input_size: dict[
475 MemberId, dict[AxisId, v0_5.SizeReference]
476 ] = {}
477 inconsistet_outputs: set[MemberId] = set()
478 for out in model.outputs:
479 for a in out.axes:
480 if not isinstance(a.size, int):
481 continue
483 for ipt in model.inputs:
484 for ia in ipt.axes:
485 if ( 485 ↛ 495line 485 didn't jump to line 495 because the condition on line 485 was always true
486 a.type not in ("space", "time")
487 or a.type != ia.type
488 or a.id != ia.id
489 or a.size != ia.size
490 or a.scale != ia.scale
491 or a.unit != ia.unit
492 ):
493 continue
495 previous_size_refs = list(
496 patched_size_ref_for_fixed_output_size_matching_fixed_input_size.get(
497 out.id, {}
498 ).values()
499 )
500 if previous_size_refs and ipt.id != previous_size_refs[0].tensor_id:
501 inconsistet_outputs.add(out.id)
502 else:
503 patched_size_ref_for_fixed_output_size_matching_fixed_input_size.setdefault(
504 out.id, {}
505 )[a.id] = v0_5.SizeReference(tensor_id=ipt.id, axis_id=ia.id)
507 # remove inconsistent outputs from the patch dict
508 patched_size_ref_for_fixed_output_size_matching_fixed_input_size = {
509 m: {a: sr for a, sr in srs.items()}
510 for m, srs in patched_size_ref_for_fixed_output_size_matching_fixed_input_size.items()
511 if m not in inconsistet_outputs
512 }
513 if patched_size_ref_for_fixed_output_size_matching_fixed_input_size: 513 ↛ 514line 513 didn't jump to line 514 because the condition on line 513 was never true
514 logger.warning(
515 "Patched output size references: {}\nThe fixed output sizes were likely intended to be defined as size references to the corresponding fixed input sizes.",
516 {
517 (k, kk): (sr.tensor_id, sr.axis_id)
518 for k, v in patched_size_ref_for_fixed_output_size_matching_fixed_input_size.items()
519 for kk, sr in v.items()
520 },
521 )
523 for out in model.outputs:
524 new_axes: dict[AxisId, LinearAxisTransform | int] = {}
525 for a in out.axes:
526 a_size = (
527 patched_size_ref_for_fixed_output_size_matching_fixed_input_size.get(
528 out.id, {}
529 ).get(a.id, a.size)
530 )
531 if a_size is None:
532 assert a.type == "batch"
533 if batch_axis_trf is None: 533 ↛ 534line 533 didn't jump to line 534 because the condition on line 533 was never true
534 raise ValueError(
535 "no batch axis found in any input tensor, but output tensor"
536 + f" '{out.id}' has one."
537 )
538 s = batch_axis_trf
539 elif isinstance(a_size, int):
540 if a.type == "batch":
541 if batch_axis_trf is None: 541 ↛ 542line 541 didn't jump to line 542 because the condition on line 541 was never true
542 raise ValueError(
543 "no batch axis found in any input tensor, but output tensor"
544 + f" '{out.id}' has one."
545 )
546 s = batch_axis_trf
547 else:
548 s = a_size
549 elif isinstance(a_size, v0_5.DataDependentSize): 549 ↛ 550line 549 didn't jump to line 550 because the condition on line 549 was never true
550 assert a.type != "batch", (
551 "data dependent size cannot be used for batch axis"
552 )
553 s = -1
554 elif isinstance(a_size, v0_5.SizeReference):
555 assert a.type != "batch", "size reference cannot be used for batch axis"
556 s = LinearAxisTransform(
557 axis=a_size.axis_id,
558 scale=axis_scales[a_size.tensor_id][a_size.axis_id] / a.scale,
559 offset=a_size.offset,
560 member=a_size.tensor_id,
561 )
562 else:
563 assert_never(a_size)
565 new_axes[a.id] = s
567 # account for postprocessing that changes the nubmer of output channels by
568 # overwriting described output shape by the intermediate output shape
569 if CHANNEL_AXIS_ID in new_axes:
570 for post in out.postprocessing:
571 if post.id == "cellpose_flow_dynamics":
572 new_axes[CHANNEL_AXIS_ID] = 3
573 break
574 elif post.id == "stardist_postprocessing": 574 ↛ 575line 574 didn't jump to line 575 because the condition on line 574 was never true
575 new_axes[CHANNEL_AXIS_ID] = post.kwargs.n_rays + 1
576 break
578 ret[out.id] = new_axes
580 return ret
583def get_tensor(
584 src: TensorSource,
585 descr: v0_4.InputTensorDescr
586 | v0_5.InputTensorDescr
587 | v0_4.OutputTensorDescr
588 | v0_5.OutputTensorDescr
589 | Sequence[AxisInfo],
590):
591 """helper to cast/load various tensor sources
593 Args:
594 src: the tensor source to load/cast
595 descr: the tensor description or a sequence of axis infos
597 """
599 if isinstance( 599 ↛ 610line 599 didn't jump to line 610 because the condition on line 599 was always true
600 descr,
601 (
602 v0_4.InputTensorDescr,
603 v0_5.InputTensorDescr,
604 v0_4.OutputTensorDescr,
605 v0_5.OutputTensorDescr,
606 ),
607 ):
608 axes = get_axes_infos(descr)
609 else:
610 axes = descr
612 if isinstance(src, Tensor):
613 output = src
614 elif isinstance(src, xr.DataArray):
615 output = Tensor.from_xarray(src)
617 elif isinstance(src, (np.ndarray, da.Array)):
618 output = Tensor.from_array(src, dims=axes)
619 else:
620 output = load_tensor(src, axes=axes)
622 for a in axes:
623 if a.id not in output.dims:
624 raise ValueError(
625 f"Missing axis '{a.id}' in tensor with dims {output.dims}. Expected axes: {axes}"
626 )
628 return output
631def create_sample_for_model(
632 model: AnyModelDescr,
633 inputs: PerMember[TensorSource] | TensorSource,
634 *,
635 stat: Stat | None = None,
636 sample_id: SampleId = None,
637) -> Sample:
638 """Create a sample from a single set of input(s) for a specific bioimage.io model
640 Args:
641 model: a bioimage.io model description
642 inputs: the input(s) constituting a single sample, e.g. {'raw': ..., 'mask': ...}.
643 stat: dictionary with sample and dataset statistics (may be updated in-place!)
644 sample_id: sample ID
645 """
647 model_inputs = {get_member_id(d): d for d in model.inputs}
648 if isinstance(inputs, collections.abc.Mapping):
649 inputs = {MemberId(k): v for k, v in inputs.items()}
650 elif len(model_inputs) == 1:
651 inputs = {next(iter(model_inputs)): inputs}
652 else:
653 raise TypeError(
654 f"Expected `inputs` to be a mapping with keys {tuple(model_inputs)}"
655 )
657 if unknown := {k for k in inputs if k not in model_inputs}:
658 raise ValueError(f"Got unexpected inputs: {unknown}")
660 if missing := {
661 k
662 for k, v in model_inputs.items()
663 if k not in inputs and not (isinstance(v, v0_5.InputTensorDescr) and v.optional)
664 }:
665 raise ValueError(f"Missing non-optional model inputs: {missing}")
667 members: dict[MemberId, Tensor] = {}
668 for m, ipt in model_inputs.items():
669 if m in inputs:
670 tensor = get_tensor(inputs[m], ipt)
671 members[m] = tensor
673 return Sample(members=members, stat={} if stat is None else stat, id=sample_id)
676load_sample_for_model: Final = create_sample_for_model
679def transpose_sample_for_model(
680 sample: Sample,
681 model: AnyModelDescr,
682 *,
683 extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "stack",
684 missing_dims: Literal[
685 "raise", "expand", "unstack", "unstack_or_expand"
686 ] = "unstack",
687) -> Sample:
688 return Sample(
689 members=transpose_inputs_for_model(
690 sample.members, model, extra_dims=extra_dims, missing_dims=missing_dims
691 ),
692 stat=sample.stat,
693 id=sample.id,
694 applied_ops=sample.applied_ops,
695 )
698def transpose_inputs_for_model(
699 inputs: PerMember[Tensor],
700 model: AnyModelDescr,
701 *,
702 extra_dims: Literal["raise", "squeeze", "stack", "squeeze_or_stack"] = "stack",
703 missing_dims: Literal[
704 "raise", "expand", "unstack", "unstack_or_expand"
705 ] = "unstack",
706) -> dict[MemberId, Tensor]:
707 """Transpose sample members to the order expected as inputs by the model."""
709 axes = {get_member_id(d): [a.id for a in get_axes_infos(d)] for d in model.inputs}
710 for m in axes:
711 if m not in inputs and not (
712 isinstance(model, v0_5.ModelDescr)
713 and next(d for d in model.inputs if get_member_id(d) == m).optional
714 ):
715 raise ValueError(
716 f"Sample is missing non-optional member {m} required by model"
717 )
719 transposed = {
720 m: t.transpose(axes[m], extra_dims=extra_dims, missing_dims=missing_dims)
721 for m, t in inputs.items()
722 if m in axes
723 }
724 if (
725 len(
726 batch_lengths := {
727 t.sizes[AxisId("batch")]
728 for t in transposed.values()
729 if AxisId("batch") in t.dims
730 }
731 )
732 > 1
733 ):
734 raise ValueError(
735 f"Transposed tensors have incompatible batch lengths: {batch_lengths}."
736 )
738 return transposed