biopb.image.utils¶
biopb.image.utils ¶
deserialize_to_numpy ¶
deserialize_to_numpy(
pixels: Pixels,
*,
singleton_t: bool = True,
np_index_order: str = "ZYXC"
) -> np.ndarray
Convert protobuf Pixels to a numpy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pixels
|
Pixels
|
protobuf data |
required |
Keyword Args: singleton_t: DEPRECATED. Use np_index_order to control output dimensions. np_index_order: Numpy index order string describing which axis corresponds to which dimension. First letter corresponds to numpy axis 0, second to axis 1, etc. Must be 2-5 characters (a permutation of subset of "ZYXCT"). Dimensions not in np_index_order are squeezed (must be singleton). Defaults to "ZYXC" (4D output, T squeezed for backward compatibility).
Returns:
| Type | Description |
|---|---|
ndarray
|
Numpy array (C-contiguous) with shape matching np_index_order. |
ndarray
|
The dtype and byteorder matches the input. |
Source code in src/main/python/biopb/image/utils.py
serialize_from_numpy ¶
serialize_from_numpy(
np_img: ndarray,
dimension_order: str = "CXYZT",
np_index_order: str = None,
**kwargs
) -> Pixels
Convert numpy array representation of image to protobuf representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
np_img
|
ndarray
|
image as numpy array (any memory order is accepted) |
required |
dimension_order
|
str
|
F-order string describing dimension order in the output protobuf. Must be exactly 5 characters (a permutation of "XYZCT"). First letter varies fastest in the serialized bytes. Default is "CXYZT". |
'CXYZT'
|
np_index_order
|
str
|
Numpy index order string describing which axis corresponds to which dimension. First letter corresponds to numpy axis 0, second to axis 1, etc. Must be 2-5 characters (a permutation of subset of "XYZCT"). If None (default), inferred from np_img.ndim: - 2D -> "YX" - 3D -> "YXC" - 4D -> "ZYXC" - 5D -> "TZYXC" |
None
|
**kwargs
|
additional metadata, e.g. physical_size_x etc (pixel size) |
{}
|
Returns:
| Type | Description |
|---|---|
Pixels
|
protobuf Pixels |
Source code in src/main/python/biopb/image/utils.py
roi_to_mask ¶
Convert a ROI protobuf to a binary mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
roi
|
ROI
|
ROI protobuf message containing shape (point, rectangle, polygon, or mask). |
required |
mask
|
ndarray
|
Template numpy array defining output shape and dtype. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Binary mask as numpy array with same shape/dtype as input mask. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If mask dimension is not 2 or 3. |
NotImplementedError
|
For unsupported ROI types or 3D polygons. |
Source code in src/main/python/biopb/image/utils.py
mask_to_roi ¶
Convert a binary mask to a ROI protobuf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
Binary mask as numpy array (2D or 3D). |
required |
bitorder
|
str
|
Bit order for packing ('big' or 'little'). Defaults to 'big'. |
'big'
|
Returns:
| Type | Description |
|---|---|
ROI
|
ROI protobuf message containing the mask data. |
Source code in src/main/python/biopb/image/utils.py
get_image_data_dim_labels ¶
get_image_data_dim_labels(
image_data: ImageData, *, implicit: bool = False
) -> Optional[Sequence[str]]
Get a copy of the dimension labels from ImageData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_data
|
ImageData
|
ImageData protobuf message |
required |
implicit
|
bool
|
If True and dim_labels field is unset, return heuristic labels based on shape. Heuristics: - 2D -> ['y', 'x'] - 3D and dim[0] <= 4 -> ['c', 'y', 'x'] - 3D and dim[-1] <= 4 -> ['y', 'x', 'c'] - All other cases -> None |
False
|
Returns:
| Type | Description |
|---|---|
Optional[Sequence[str]]
|
List of dimension labels, or None if unset and implicit=False or |
Optional[Sequence[str]]
|
heuristics don't apply. |
Source code in src/main/python/biopb/image/utils.py
deserialize_image_data ¶
deserialize_image_data(
image_data: ImageData, *, cache_bytes: int = 1000000000
) -> Union[np.ndarray, da.Array]
Convert protobuf ImageData to a numpy array or dask array.
Handles both eager (inline) and lazy (Flight server) data representations. Also handles legacy pixels field for backward compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_data
|
ImageData
|
protobuf ImageData message |
required |
Keyword Args: cache_bytes: Cache size for lazy_data chunk cache (default 1GB).
Returns:
| Type | Description |
|---|---|
Union[ndarray, Array]
|
numpy array for eager_data, or dask array for lazy_data. |
Source code in src/main/python/biopb/image/utils.py
normalize_array_dims ¶
normalize_array_dims(
arr: Union[ndarray, Array],
dim_labels: Sequence[str],
target_dim_labels: Sequence[str],
) -> Union[np.ndarray, da.Array]
Normalize array dimensions to match target dimension labels.
Transposes, squeezes, and adds singleton dimensions to convert an array with given dimension labels to match the target dimension order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
Union[ndarray, Array]
|
Input array (numpy or dask) |
required |
dim_labels
|
Sequence[str]
|
Current dimension labels for each axis of arr. If None, raises ValueError. |
required |
target_dim_labels
|
Sequence[str]
|
Target dimension labels for the output array. |
required |
Returns:
| Type | Description |
|---|---|
Union[ndarray, Array]
|
Array with dimensions reordered to match target_dim_labels. |
Union[ndarray, Array]
|
The output will have len(target_dim_labels) dimensions. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If dim_labels is None, if dim_labels length doesn't match array ndim, if dim_labels or target_dim_labels has duplicates, or if a dimension exists in dim_labels but not target_dim_labels with size > 1 (cannot squeeze non-singleton dimension). |
Source code in src/main/python/biopb/image/utils.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | |
serialize_from_numpy_to_image_data ¶
serialize_from_numpy_to_image_data(
np_img: ndarray,
*,
dim_labels: Optional[Sequence[str]] = None
) -> ImageData
Convert numpy array to protobuf ImageData with eager_data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
np_img
|
ndarray
|
image as numpy array (any memory order is accepted) |
required |
Keyword Args: dim_labels: Dimension labels for the tensor. Must be same length as np_img.ndim. Returns: protobuf ImageData with eager_data set.