Coverage for src/bioimageio/core/_op_base.py: 96%

44 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 09:46 +0000

1from __future__ import annotations 

2 

3from abc import ABC, abstractmethod 

4from collections.abc import Collection, Sequence 

5from dataclasses import dataclass 

6from typing import Any 

7 

8from torch import TYPE_CHECKING 

9 

10from .axis import PerAxis 

11from .common import MemberId 

12from .tensor import Tensor 

13 

14if TYPE_CHECKING: 

15 from .sample import Sample 

16 from .stat_measures import Measure, Stat 

17 

18 

19@dataclass(frozen=True, kw_only=True) 

20class Operator(ABC): 

21 """Base class for all operators. 

22 

23 Implementations must record themselves in ``sample.applied_ops`` after success. 

24 """ 

25 

26 @abstractmethod 

27 def __call__(self, sample: Sample) -> None: ... 

28 

29 @property 

30 def output_member_ids(self) -> Collection[MemberId]: 

31 return () 

32 

33 @property 

34 def affects_statistics(self) -> bool: 

35 """Whether this operator may change statistics of its output tensors.""" 

36 return True 

37 

38 def get_required_measures(self) -> Collection[Measure]: 

39 return () 

40 

41 def get_descr(self) -> Any: 

42 raise NotImplementedError( 

43 f"{type(self).__name__} has no processing description" 

44 ) 

45 

46 

47def get_relevant_preceding_ops( 

48 member_id: MemberId, applied_ops: Sequence[Operator] 

49) -> tuple[Operator, ...]: 

50 return tuple( 

51 op 

52 for op in applied_ops 

53 if op.affects_statistics and member_id in op.output_member_ids 

54 ) 

55 

56 

57@dataclass(frozen=True, kw_only=True) 

58class SamplewiseOperator(Operator): 

59 """Base class for operators that can only be applied to whole samples.""" 

60 

61 

62@dataclass(frozen=True, kw_only=True) 

63class BlockwiseOperator(Operator): 

64 """Base class for operators that can be applied to whole sample or blockwise.""" 

65 

66 

67@dataclass(frozen=True) 

68class SimpleOperator(BlockwiseOperator): 

69 """Convenience base class for blockwise operators with a single input and single output.""" 

70 

71 input: MemberId 

72 output: MemberId 

73 

74 @property 

75 def output_member_ids(self) -> Collection[MemberId]: 

76 return (self.output,) 

77 

78 @abstractmethod 

79 def get_output_shape(self, input_shape: PerAxis[int]) -> PerAxis[int]: ... 

80 

81 def __call__(self, sample: Sample) -> None: # | SampleBlock 

82 if self.input not in sample.members: 

83 raise ValueError( 

84 f"Input {self.input} not found in sample members: {list(sample.members)}" 

85 ) 

86 

87 input_tensor = sample.members[self.input] 

88 output_tensor = self._apply(input_tensor, sample.stat) 

89 if (multi_index := input_tensor.batch_multi_index) is not None: 

90 output_tensor = output_tensor.assign_batch_multi_index(multi_index) 

91 

92 assert self.output not in sample.members or ( 

93 sample.members[self.output].tagged_shape == output_tensor.tagged_shape 

94 ), "overwriting existing output member with different shape" 

95 

96 sample.members[self.output] = output_tensor 

97 sample.applied_ops += (self,) 

98 

99 @abstractmethod 

100 def _apply(self, x: Tensor, stat: Stat) -> Tensor: ...