Skip to content

block ¤

Classes:

Name Description
Block

A block/tile of a (larger) tensor

Functions:

Name Description
split_tensor_into_blocks

divide a sample tensor into tensor blocks.

Block dataclass ¤

Block(sample_shape: PerAxis[int], inner_slice: PerAxis[SliceInfo], halo: PerAxis[Halo], block_index: BlockIndex, blocks_in_sample: TotalNumberOfBlocks, data: Tensor)

Bases: BlockMeta


              flowchart TD
              bioimageio.core.block.Block[Block]
              bioimageio.core.block_meta.BlockMeta[BlockMeta]

                              bioimageio.core.block_meta.BlockMeta --> bioimageio.core.block.Block
                


              click bioimageio.core.block.Block href "" "bioimageio.core.block.Block"
              click bioimageio.core.block_meta.BlockMeta href "" "bioimageio.core.block_meta.BlockMeta"
            

A block/tile of a (larger) tensor

Methods:

Name Description
__post_init__
from_meta
from_sample_member
get_transformed

Attributes:

Name Type Description
block_index BlockIndex

the i-th block of the sample

blocks_in_sample TotalNumberOfBlocks

total number of blocks in the sample

data Tensor

the block's tensor, e.g. a (padded) slice of some larger, original tensor

dims Collection[AxisId]
halo PerAxis[Halo]

halo enlarging the inner region to the block's sizes

inner_data
inner_shape PerAxis[int]

axis lengths of the inner region (without halo)

inner_slice PerAxis[SliceInfo]

inner region (without halo) wrt the sample

inner_slice_wo_overlap PerAxis[SliceInfo]

subslice of the inner slice, such that all inner_slice_wo_overlap can be

local_slice PerAxis[SliceInfo]

inner slice wrt the block, not the sample

outer_slice PerAxis[SliceInfo]

slice of the outer block (without padding) wrt the sample

padding PerAxis[PadWidth]

padding to realize the halo at the sample edge

sample_shape PerAxis[int]

the axis sizes of the whole (unblocked) sample

shape PerAxis[int]

axis lengths of the block

tagged_shape PerAxis[int]

alias for shape

block_index instance-attribute ¤

block_index: BlockIndex

the i-th block of the sample

blocks_in_sample instance-attribute ¤

blocks_in_sample: TotalNumberOfBlocks

total number of blocks in the sample

data instance-attribute ¤

data: Tensor

the block's tensor, e.g. a (padded) slice of some larger, original tensor

dims property ¤

dims: Collection[AxisId]

halo instance-attribute ¤

halo: PerAxis[Halo]

halo enlarging the inner region to the block's sizes

inner_data property ¤

inner_data

inner_shape cached property ¤

inner_shape: PerAxis[int]

axis lengths of the inner region (without halo)

inner_slice instance-attribute ¤

inner_slice: PerAxis[SliceInfo]

inner region (without halo) wrt the sample

inner_slice_wo_overlap property ¤

inner_slice_wo_overlap: PerAxis[SliceInfo]

subslice of the inner slice, such that all inner_slice_wo_overlap can be stiched together trivially to form the original sample.

This can also be used to calculate statistics without overrepresenting block edge regions.

local_slice cached property ¤

local_slice: PerAxis[SliceInfo]

inner slice wrt the block, not the sample

outer_slice cached property ¤

outer_slice: PerAxis[SliceInfo]

slice of the outer block (without padding) wrt the sample

padding cached property ¤

padding: PerAxis[PadWidth]

padding to realize the halo at the sample edge where we cannot simply enlarge the inner slice

sample_shape instance-attribute ¤

sample_shape: PerAxis[int]

the axis sizes of the whole (unblocked) sample

shape cached property ¤

shape: PerAxis[int]

axis lengths of the block

tagged_shape property ¤

tagged_shape: PerAxis[int]

alias for shape

__post_init__ ¤

__post_init__()
Source code in src/bioimageio/core/block.py
36
37
38
39
40
41
42
43
44
45
46
def __post_init__(self):
    super().__post_init__()
    assert not any(v == -1 for v in self.sample_shape.values()), self.sample_shape
    for a, s in self.data.sizes.items():
        slice_ = self.inner_slice[a]
        halo = self.halo.get(a, Halo(0, 0))
        assert s == halo.left + (slice_.stop - slice_.start) + halo.right, (
            s,
            slice_,
            halo,
        )

from_meta classmethod ¤

from_meta(meta: BlockMeta, data: Tensor) -> Self
Source code in src/bioimageio/core/block.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@classmethod
def from_meta(cls, meta: BlockMeta, data: Tensor) -> Self:
    return cls(
        sample_shape={
            k: data.tagged_shape[k] if v == -1 else v
            for k, v in meta.sample_shape.items()
        },
        inner_slice={
            k: (
                SliceInfo(start=v.start, stop=data.tagged_shape[k])
                if v.stop == -1
                else v
            )
            for k, v in meta.inner_slice.items()
        },
        halo=meta.halo,
        block_index=meta.block_index,
        blocks_in_sample=meta.blocks_in_sample,
        data=data,
    )

from_sample_member classmethod ¤

from_sample_member(sample_member: Tensor, block: BlockMeta, *, pad_mode: PadMode) -> Self
Source code in src/bioimageio/core/block.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@classmethod
def from_sample_member(
    cls,
    sample_member: Tensor,
    block: BlockMeta,
    *,
    pad_mode: PadMode,
) -> Self:
    return cls(
        data=sample_member[block.outer_slice].pad(block.padding, pad_mode),
        sample_shape=sample_member.tagged_shape,
        inner_slice=block.inner_slice,
        halo=block.halo,
        block_index=block.block_index,
        blocks_in_sample=block.blocks_in_sample,
    )

get_transformed ¤

get_transformed(new_axes: PerAxis[Union[LinearAxisTransform, int]]) -> Self
Source code in src/bioimageio/core/block.py
65
66
67
68
def get_transformed(
    self, new_axes: PerAxis[Union[LinearAxisTransform, int]]
) -> Self:
    raise NotImplementedError

split_tensor_into_blocks ¤

split_tensor_into_blocks(tensor: Tensor, block_shape: PerAxis[int], *, halo: PerAxis[HaloLike], stride: Optional[PerAxis[int]] = None, pad_mode: PadMode) -> Tuple[TotalNumberOfBlocks, Generator[Block, Any, None]]

divide a sample tensor into tensor blocks.

Source code in src/bioimageio/core/block.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def split_tensor_into_blocks(
    tensor: Tensor,
    block_shape: PerAxis[int],
    *,
    halo: PerAxis[HaloLike],
    stride: Optional[PerAxis[int]] = None,
    pad_mode: PadMode,
) -> Tuple[TotalNumberOfBlocks, Generator[Block, Any, None]]:
    """divide a sample tensor into tensor blocks."""
    n_blocks, block_gen = split_shape_into_blocks(
        tensor.tagged_shape, block_shape=block_shape, halo=halo, stride=stride
    )
    return n_blocks, _block_generator(tensor, block_gen, pad_mode=pad_mode)