Skip to content

sample ¤

Classes:

Name Description
LinearSampleAxisTransform
Sample

A dataset sample.

SampleBlock

A block of a dataset sample

SampleBlockBase

base class for SampleBlockMeta and SampleBlock

SampleBlockMeta

Meta data of a dataset sample block

SampleBlockWithOrigin

A SampleBlock with a reference (origin) to the whole Sample

Functions:

Name Description
sample_block_generator
sample_block_meta_generator

Attributes:

Name Type Description
BlockT

BlockT module-attribute ¤

BlockT = TypeVar('BlockT', Block, BlockMeta)

LinearSampleAxisTransform dataclass ¤

LinearSampleAxisTransform(axis: AxisId, scale: float, offset: int, member: MemberId)

Bases: LinearAxisTransform


              flowchart TD
              bioimageio.core.sample.LinearSampleAxisTransform[LinearSampleAxisTransform]
              bioimageio.core.block_meta.LinearAxisTransform[LinearAxisTransform]

                              bioimageio.core.block_meta.LinearAxisTransform --> bioimageio.core.sample.LinearSampleAxisTransform
                


              click bioimageio.core.sample.LinearSampleAxisTransform href "" "bioimageio.core.sample.LinearSampleAxisTransform"
              click bioimageio.core.block_meta.LinearAxisTransform href "" "bioimageio.core.block_meta.LinearAxisTransform"
            

Methods:

Name Description
compute

Attributes:

Name Type Description
axis AxisId
member MemberId
offset int
scale float

axis instance-attribute ¤

axis: AxisId

member instance-attribute ¤

member: MemberId

offset instance-attribute ¤

offset: int

scale instance-attribute ¤

scale: float

compute ¤

compute(s: int, round: Callable[[float], int] = floor) -> int
Source code in src/bioimageio/core/block_meta.py
41
42
def compute(self, s: int, round: Callable[[float], int] = floor) -> int:
    return round(s * self.scale) + self.offset

Sample dataclass ¤

Sample(members: Dict[MemberId, Tensor], stat: Stat, id: SampleId)

A dataset sample.

A Sample has members, which allows to combine multiple tensors into a single sample. For example a Sample from a dataset with masked images may contain a MemberId("raw") and MemberId("mask") image.

Methods:

Name Description
as_arrays

Return sample as dictionary of arrays.

as_single_block
from_blocks
split_into_blocks

Attributes:

Name Type Description
id SampleId

Identifies the Sample within the dataset -- typically a number or a string.

members Dict[MemberId, Tensor]

The sample's tensors

shape PerMember[PerAxis[int]]
stat Stat

Sample and dataset statistics

id instance-attribute ¤

Identifies the Sample within the dataset -- typically a number or a string.

members instance-attribute ¤

members: Dict[MemberId, Tensor]

The sample's tensors

shape property ¤

shape: PerMember[PerAxis[int]]

stat instance-attribute ¤

stat: Stat

Sample and dataset statistics

as_arrays ¤

as_arrays() -> Dict[str, NDArray[Any]]

Return sample as dictionary of arrays.

Source code in src/bioimageio/core/sample.py
68
69
70
def as_arrays(self) -> Dict[str, NDArray[Any]]:
    """Return sample as dictionary of arrays."""
    return {str(m): t.data.to_numpy() for m, t in self.members.items()}

as_single_block ¤

as_single_block(halo: Optional[PerMember[PerAxis[Halo]]] = None)
Source code in src/bioimageio/core/sample.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def as_single_block(self, halo: Optional[PerMember[PerAxis[Halo]]] = None):
    if halo is None:
        halo = {}
    return SampleBlockWithOrigin(
        sample_shape=self.shape,
        sample_id=self.id,
        blocks={
            m: Block(
                sample_shape=self.shape[m],
                data=data,
                inner_slice={
                    a: SliceInfo(0, s) for a, s in data.tagged_shape.items()
                },
                halo=halo.get(m, {}),
                block_index=0,
                blocks_in_sample=1,
            )
            for m, data in self.members.items()
        },
        stat=self.stat,
        origin=self,
        block_index=0,
        blocks_in_sample=1,
    )

from_blocks classmethod ¤

from_blocks(sample_blocks: Iterable[SampleBlock], *, fill_value: float = float('nan')) -> Self
Source code in src/bioimageio/core/sample.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
@classmethod
def from_blocks(
    cls,
    sample_blocks: Iterable[SampleBlock],
    *,
    fill_value: float = float("nan"),
) -> Self:
    members: PerMember[Tensor] = {}
    stat: Stat = {}
    sample_id = None
    for sample_block in sample_blocks:
        assert sample_id is None or sample_id == sample_block.sample_id
        sample_id = sample_block.sample_id
        stat = sample_block.stat
        for m, block in sample_block.blocks.items():
            if m not in members:
                if -1 in block.sample_shape.values():
                    raise NotImplementedError(
                        "merging blocks with data dependent axis not yet implemented"
                    )

                members[m] = Tensor(
                    np.full(
                        tuple(block.sample_shape[a] for a in block.data.dims),
                        fill_value,
                        dtype=block.data.dtype,
                    ),
                    dims=block.data.dims,
                )

            members[m][block.inner_slice] = block.inner_data

    return cls(members=members, stat=stat, id=sample_id)

split_into_blocks ¤

split_into_blocks(block_shapes: PerMember[PerAxis[int]], halo: PerMember[PerAxis[HaloLike]], pad_mode: PadMode, broadcast: bool = False) -> Tuple[TotalNumberOfBlocks, Iterable[SampleBlockWithOrigin]]
Source code in src/bioimageio/core/sample.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def split_into_blocks(
    self,
    block_shapes: PerMember[PerAxis[int]],
    halo: PerMember[PerAxis[HaloLike]],
    pad_mode: PadMode,
    broadcast: bool = False,
) -> Tuple[TotalNumberOfBlocks, Iterable[SampleBlockWithOrigin]]:
    assert not (missing := [m for m in block_shapes if m not in self.members]), (
        f"`block_shapes` specified for unknown members: {missing}"
    )
    assert not (missing := [m for m in halo if m not in block_shapes]), (
        f"`halo` specified for members without `block_shape`: {missing}"
    )

    n_blocks, blocks = split_multiple_shapes_into_blocks(
        shapes=self.shape,
        block_shapes=block_shapes,
        halo=halo,
        broadcast=broadcast,
    )
    return n_blocks, sample_block_generator(blocks, origin=self, pad_mode=pad_mode)

SampleBlock dataclass ¤

SampleBlock(sample_shape: PerMember[PerAxis[int]], sample_id: SampleId, blocks: Dict[MemberId, BlockT], block_index: BlockIndex, blocks_in_sample: TotalNumberOfBlocks, stat: Stat)

Bases: SampleBlockBase[Block]


              flowchart TD
              bioimageio.core.sample.SampleBlock[SampleBlock]
              bioimageio.core.sample.SampleBlockBase[SampleBlockBase]

                              bioimageio.core.sample.SampleBlockBase --> bioimageio.core.sample.SampleBlock
                


              click bioimageio.core.sample.SampleBlock href "" "bioimageio.core.sample.SampleBlock"
              click bioimageio.core.sample.SampleBlockBase href "" "bioimageio.core.sample.SampleBlockBase"
            

A block of a dataset sample

Methods:

Name Description
get_transformed_meta

Attributes:

Name Type Description
block_index BlockIndex

the n-th block of the sample

blocks Dict[MemberId, BlockT]

Individual tensor blocks comprising this sample block

blocks_in_sample TotalNumberOfBlocks

total number of blocks in the sample

inner_shape PerMember[PerAxis[int]]
members PerMember[Tensor]

the sample block's tensors

sample_id SampleId

identifier for the sample within its dataset

sample_shape PerMember[PerAxis[int]]

the sample shape this block represents a part of

shape PerMember[PerAxis[int]]
stat Stat

computed statistics

block_index instance-attribute ¤

block_index: BlockIndex

the n-th block of the sample

blocks instance-attribute ¤

blocks: Dict[MemberId, BlockT]

Individual tensor blocks comprising this sample block

blocks_in_sample instance-attribute ¤

blocks_in_sample: TotalNumberOfBlocks

total number of blocks in the sample

inner_shape property ¤

inner_shape: PerMember[PerAxis[int]]

members property ¤

members: PerMember[Tensor]

the sample block's tensors

sample_id instance-attribute ¤

sample_id: SampleId

identifier for the sample within its dataset

sample_shape instance-attribute ¤

sample_shape: PerMember[PerAxis[int]]

the sample shape this block represents a part of

shape property ¤

shape: PerMember[PerAxis[int]]

stat instance-attribute ¤

stat: Stat

computed statistics

get_transformed_meta ¤

get_transformed_meta(new_axes: PerMember[PerAxis[Union[LinearSampleAxisTransform, int]]]) -> SampleBlockMeta
Source code in src/bioimageio/core/sample.py
296
297
298
299
300
301
302
303
304
305
def get_transformed_meta(
    self, new_axes: PerMember[PerAxis[Union[LinearSampleAxisTransform, int]]]
) -> SampleBlockMeta:
    return SampleBlockMeta(
        sample_id=self.sample_id,
        blocks=dict(self.blocks),
        sample_shape=self.sample_shape,
        block_index=self.block_index,
        blocks_in_sample=self.blocks_in_sample,
    ).get_transformed(new_axes)

SampleBlockBase dataclass ¤

SampleBlockBase(sample_shape: PerMember[PerAxis[int]], sample_id: SampleId, blocks: Dict[MemberId, BlockT], block_index: BlockIndex, blocks_in_sample: TotalNumberOfBlocks)

Bases: Generic[BlockT]


              flowchart TD
              bioimageio.core.sample.SampleBlockBase[SampleBlockBase]

              

              click bioimageio.core.sample.SampleBlockBase href "" "bioimageio.core.sample.SampleBlockBase"
            

base class for SampleBlockMeta and SampleBlock

Attributes:

Name Type Description
block_index BlockIndex

the n-th block of the sample

blocks Dict[MemberId, BlockT]

Individual tensor blocks comprising this sample block

blocks_in_sample TotalNumberOfBlocks

total number of blocks in the sample

inner_shape PerMember[PerAxis[int]]
sample_id SampleId

identifier for the sample within its dataset

sample_shape PerMember[PerAxis[int]]

the sample shape this block represents a part of

shape PerMember[PerAxis[int]]

block_index instance-attribute ¤

block_index: BlockIndex

the n-th block of the sample

blocks instance-attribute ¤

blocks: Dict[MemberId, BlockT]

Individual tensor blocks comprising this sample block

blocks_in_sample instance-attribute ¤

blocks_in_sample: TotalNumberOfBlocks

total number of blocks in the sample

inner_shape property ¤

inner_shape: PerMember[PerAxis[int]]

sample_id instance-attribute ¤

sample_id: SampleId

identifier for the sample within its dataset

sample_shape instance-attribute ¤

sample_shape: PerMember[PerAxis[int]]

the sample shape this block represents a part of

shape property ¤

shape: PerMember[PerAxis[int]]

SampleBlockMeta dataclass ¤

SampleBlockMeta(sample_shape: PerMember[PerAxis[int]], sample_id: SampleId, blocks: Dict[MemberId, BlockT], block_index: BlockIndex, blocks_in_sample: TotalNumberOfBlocks)

Bases: SampleBlockBase[BlockMeta]


              flowchart TD
              bioimageio.core.sample.SampleBlockMeta[SampleBlockMeta]
              bioimageio.core.sample.SampleBlockBase[SampleBlockBase]

                              bioimageio.core.sample.SampleBlockBase --> bioimageio.core.sample.SampleBlockMeta
                


              click bioimageio.core.sample.SampleBlockMeta href "" "bioimageio.core.sample.SampleBlockMeta"
              click bioimageio.core.sample.SampleBlockBase href "" "bioimageio.core.sample.SampleBlockBase"
            

Meta data of a dataset sample block

Methods:

Name Description
get_transformed
with_data

Attributes:

Name Type Description
block_index BlockIndex

the n-th block of the sample

blocks Dict[MemberId, BlockT]

Individual tensor blocks comprising this sample block

blocks_in_sample TotalNumberOfBlocks

total number of blocks in the sample

inner_shape PerMember[PerAxis[int]]
sample_id SampleId

identifier for the sample within its dataset

sample_shape PerMember[PerAxis[int]]

the sample shape this block represents a part of

shape PerMember[PerAxis[int]]

block_index instance-attribute ¤

block_index: BlockIndex

the n-th block of the sample

blocks instance-attribute ¤

blocks: Dict[MemberId, BlockT]

Individual tensor blocks comprising this sample block

blocks_in_sample instance-attribute ¤

blocks_in_sample: TotalNumberOfBlocks

total number of blocks in the sample

inner_shape property ¤

inner_shape: PerMember[PerAxis[int]]

sample_id instance-attribute ¤

sample_id: SampleId

identifier for the sample within its dataset

sample_shape instance-attribute ¤

sample_shape: PerMember[PerAxis[int]]

the sample shape this block represents a part of

shape property ¤

shape: PerMember[PerAxis[int]]

get_transformed ¤

get_transformed(new_axes: PerMember[PerAxis[Union[LinearSampleAxisTransform, int]]]) -> Self
Source code in src/bioimageio/core/sample.py
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
def get_transformed(
    self, new_axes: PerMember[PerAxis[Union[LinearSampleAxisTransform, int]]]
) -> Self:
    sample_shape = {
        m: {
            a: (
                trf
                if isinstance(trf, int)
                else trf.compute(self.sample_shape[trf.member][trf.axis])
            )
            for a, trf in new_axes[m].items()
        }
        for m in new_axes
    }

    def get_member_halo(m: MemberId, round: Callable[[float], int]):
        return {
            a: (
                Halo(0, 0)
                if isinstance(trf, int)
                or trf.axis not in self.blocks[trf.member].halo
                else Halo(
                    round(self.blocks[trf.member].halo[trf.axis].left * trf.scale),
                    round(self.blocks[trf.member].halo[trf.axis].right * trf.scale),
                )
            )
            for a, trf in new_axes[m].items()
        }

    halo: Dict[MemberId, Dict[AxisId, Halo]] = {}
    for m in new_axes:
        halo[m] = get_member_halo(m, floor)
        if halo[m] != get_member_halo(m, ceil):
            raise ValueError(
                f"failed to unambiguously scale halo {halo[m]} with {new_axes[m]}"
                + f" for {m}."
            )

    inner_slice = {
        m: {
            a: (
                SliceInfo(0, trf)
                if isinstance(trf, int)
                else SliceInfo(
                    trf.compute(
                        self.blocks[trf.member].inner_slice[trf.axis].start
                    ),
                    trf.compute(self.blocks[trf.member].inner_slice[trf.axis].stop),
                )
            )
            for a, trf in new_axes[m].items()
        }
        for m in new_axes
    }
    return self.__class__(
        blocks={
            m: BlockMeta(
                sample_shape=sample_shape[m],
                inner_slice=inner_slice[m],
                halo=halo[m],
                block_index=self.block_index,
                blocks_in_sample=self.blocks_in_sample,
            )
            for m in new_axes
        },
        sample_shape=sample_shape,
        sample_id=self.sample_id,
        block_index=self.block_index,
        blocks_in_sample=self.blocks_in_sample,
    )

with_data ¤

with_data(data: PerMember[Tensor], *, stat: Stat) -> SampleBlock
Source code in src/bioimageio/core/sample.py
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
def with_data(self, data: PerMember[Tensor], *, stat: Stat) -> SampleBlock:
    return SampleBlock(
        sample_shape={
            m: {
                a: data[m].tagged_shape[a] if s == -1 else s
                for a, s in member_shape.items()
            }
            for m, member_shape in self.sample_shape.items()
        },
        sample_id=self.sample_id,
        blocks={
            m: Block.from_meta(b, data=data[m]) for m, b in self.blocks.items()
        },
        stat=stat,
        block_index=self.block_index,
        blocks_in_sample=self.blocks_in_sample,
    )

SampleBlockWithOrigin dataclass ¤

SampleBlockWithOrigin(sample_shape: PerMember[PerAxis[int]], sample_id: SampleId, blocks: Dict[MemberId, BlockT], block_index: BlockIndex, blocks_in_sample: TotalNumberOfBlocks, stat: Stat, origin: Sample)

Bases: SampleBlock


              flowchart TD
              bioimageio.core.sample.SampleBlockWithOrigin[SampleBlockWithOrigin]
              bioimageio.core.sample.SampleBlock[SampleBlock]
              bioimageio.core.sample.SampleBlockBase[SampleBlockBase]

                              bioimageio.core.sample.SampleBlock --> bioimageio.core.sample.SampleBlockWithOrigin
                                bioimageio.core.sample.SampleBlockBase --> bioimageio.core.sample.SampleBlock
                



              click bioimageio.core.sample.SampleBlockWithOrigin href "" "bioimageio.core.sample.SampleBlockWithOrigin"
              click bioimageio.core.sample.SampleBlock href "" "bioimageio.core.sample.SampleBlock"
              click bioimageio.core.sample.SampleBlockBase href "" "bioimageio.core.sample.SampleBlockBase"
            

A SampleBlock with a reference (origin) to the whole Sample

Methods:

Name Description
get_transformed_meta

Attributes:

Name Type Description
block_index BlockIndex

the n-th block of the sample

blocks Dict[MemberId, BlockT]

Individual tensor blocks comprising this sample block

blocks_in_sample TotalNumberOfBlocks

total number of blocks in the sample

inner_shape PerMember[PerAxis[int]]
members PerMember[Tensor]

the sample block's tensors

origin Sample

the sample this sample block was taken from

sample_id SampleId

identifier for the sample within its dataset

sample_shape PerMember[PerAxis[int]]

the sample shape this block represents a part of

shape PerMember[PerAxis[int]]
stat Stat

computed statistics

block_index instance-attribute ¤

block_index: BlockIndex

the n-th block of the sample

blocks instance-attribute ¤

blocks: Dict[MemberId, BlockT]

Individual tensor blocks comprising this sample block

blocks_in_sample instance-attribute ¤

blocks_in_sample: TotalNumberOfBlocks

total number of blocks in the sample

inner_shape property ¤

inner_shape: PerMember[PerAxis[int]]

members property ¤

members: PerMember[Tensor]

the sample block's tensors

origin instance-attribute ¤

origin: Sample

the sample this sample block was taken from

sample_id instance-attribute ¤

sample_id: SampleId

identifier for the sample within its dataset

sample_shape instance-attribute ¤

sample_shape: PerMember[PerAxis[int]]

the sample shape this block represents a part of

shape property ¤

shape: PerMember[PerAxis[int]]

stat instance-attribute ¤

stat: Stat

computed statistics

get_transformed_meta ¤

get_transformed_meta(new_axes: PerMember[PerAxis[Union[LinearSampleAxisTransform, int]]]) -> SampleBlockMeta
Source code in src/bioimageio/core/sample.py
296
297
298
299
300
301
302
303
304
305
def get_transformed_meta(
    self, new_axes: PerMember[PerAxis[Union[LinearSampleAxisTransform, int]]]
) -> SampleBlockMeta:
    return SampleBlockMeta(
        sample_id=self.sample_id,
        blocks=dict(self.blocks),
        sample_shape=self.sample_shape,
        block_index=self.block_index,
        blocks_in_sample=self.blocks_in_sample,
    ).get_transformed(new_axes)

sample_block_generator ¤

sample_block_generator(blocks: Iterable[PerMember[BlockMeta]], *, origin: Sample, pad_mode: PadMode) -> Iterable[SampleBlockWithOrigin]
Source code in src/bioimageio/core/sample.py
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
def sample_block_generator(
    blocks: Iterable[PerMember[BlockMeta]],
    *,
    origin: Sample,
    pad_mode: PadMode,
) -> Iterable[SampleBlockWithOrigin]:
    for member_blocks in blocks:
        cons = _ConsolidatedMemberBlocks(member_blocks)
        yield SampleBlockWithOrigin(
            blocks={
                m: Block.from_sample_member(
                    origin.members[m], block=member_blocks[m], pad_mode=pad_mode
                )
                for m in origin.members
            },
            sample_shape=origin.shape,
            origin=origin,
            stat=origin.stat,
            sample_id=origin.id,
            block_index=cons.block_index,
            blocks_in_sample=cons.blocks_in_sample,
        )

sample_block_meta_generator ¤

sample_block_meta_generator(blocks: Iterable[PerMember[BlockMeta]], *, sample_shape: PerMember[PerAxis[int]], sample_id: SampleId)
Source code in src/bioimageio/core/sample.py
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
def sample_block_meta_generator(
    blocks: Iterable[PerMember[BlockMeta]],
    *,
    sample_shape: PerMember[PerAxis[int]],
    sample_id: SampleId,
):
    for member_blocks in blocks:
        cons = _ConsolidatedMemberBlocks(member_blocks)
        yield SampleBlockMeta(
            blocks=dict(member_blocks),
            sample_shape=sample_shape,
            sample_id=sample_id,
            block_index=cons.block_index,
            blocks_in_sample=cons.blocks_in_sample,
        )