Skip to content

Arrays

Name What it does
Shape the dimensions of an array field, a record member or a stream item, given in the annotation
MaxShape the largest dimensions of an array in a stream item or a box field, which stores its own shape with each value
DType the element type of an array field; a numpy annotation that names its dtype needs none
SupportsDLPack an object a DLPack library can import; a field annotated with it reads back sharedbox's own array
register_array_type makes fields of another array type read back through that library's from_dlpack

Shape dataclass

Shape(dims: tuple[int, ...])

Dimensions of an array field, fixed when the class is defined.

It goes in the annotation, as in Annotated[numpy.ndarray, Shape((480, 640)), DType("uint8")], for a box field, a record member and a stream item alike. A default array of a box field must have that shape. When the annotation names a number of dimensions, as in numpy.ndarray[tuple[int, int], numpy.dtype[numpy.uint8]], the sizes must have as many.

Raises:

Type Description
ValueError

Unless dims is a tuple of 1 to 8 dimensions, each an int of at least 1.

MaxShape dataclass

MaxShape(dims: tuple[int, ...])

Largest dimensions of an array in a box field or a stream item, whose own shape each value stores.

It goes in the annotation, as in Annotated[numpy.ndarray, MaxShape((2048, 2048)), DType("uint16")], and an array takes a MaxShape or a Shape, not both. A box field or a stream item may hold the array as the field or item itself or as a member of a record or tuple, at any depth, and each value written has as many dimensions as the MaxShape, each from 0 to its maximum. A member of an optional, union or collection cannot hold one.

Raises:

Type Description
ValueError

Unless dims is a tuple of 1 to 8 dimensions, each an int of at least 1.

DType

Bases: StrEnum

Element type of an array field.

Calling it takes a name as numpy spells it ("float32", "bfloat16", "complex64", "bool"), a numpy.dtype or scalar type, or a torch.dtype, and returns the member of that name, so DType(numpy.uint8) is DType("uint8") is DType.uint8, which equals the string "uint8". A numpy annotation that names its dtype, as in numpy.ndarray[tuple[int], numpy.dtype[numpy.uint8]], needs no DType.

Raises:

Type Description
ValueError

For an element type DLPack cannot describe.

SupportsDLPack

Bases: Protocol

An object another library can import through DLPack, such as numpy.from_dlpack takes.

A field annotated with it reads back sharedbox's own array object, which implements it.

register_array_type

register_array_type(array_type: type, from_dlpack: Callable[[SupportsDLPack], Any]) -> None

Read fields annotated with array_type back through from_dlpack.

from_dlpack receives the array a read copied out of the box and returns one of array_type. Register before defining a class that names the type; numpy and torch need no registration.