Array

class Array(dtype: str, shape: list[int], stype: str, data: bytes)[source]
class Array(ndarray: ndarray[Any, dtype[Any]])
class Array(torch_tensor: torch.Tensor)

Bases : InflatableObject

Array type.

A dataclass containing serialized data from an array-like or tensor-like object along with metadata about it. The class can be initialized in one of three ways:

  1. By specifying explicit values for dtype, shape, stype, and data.

  2. By providing a NumPy ndarray (via the ndarray argument).

  3. By providing a PyTorch tensor (via the torch_tensor argument).

In scenarios (2)-(3), the dtype, shape, stype, and data are automatically derived from the input. In scenario (1), these fields must be specified manually.

Paramètres:
  • dtype (Optional[str] (default: None)) – A string representing the data type of the serialized object (e.g. « float32 »). Only required if you are not passing in a ndarray or a tensor.

  • shape (Optional[list[int]] (default: None)) – A list representing the shape of the unserialized array-like object. Only required if you are not passing in a ndarray or a tensor.

  • stype (Optional[str] (default: None)) – A string indicating the serialization mechanism used to generate the bytes in data from an array-like or tensor-like object. Only required if you are not passing in a ndarray or a tensor.

  • data (Optional[bytes] (default: None)) – A buffer of bytes containing the data. Only required if you are not passing in a ndarray or a tensor.

  • ndarray (Optional[NDArray] (default: None)) – A NumPy ndarray. If provided, the dtype, shape, stype, and data fields are derived automatically from it.

  • torch_tensor (Optional[torch.Tensor] (default: None)) – A PyTorch tensor. If provided, it will be detached and moved to CPU before conversion, and the dtype, shape, stype, and data fields will be derived automatically from it.

Exemples

Initializing by specifying all fields directly:

arr1 = Array(
    dtype="float32",
    shape=[3, 3],
    stype="numpy.ndarray",
    data=b"serialized_data...",
)

Initializing with a NumPy ndarray:

import numpy as np
arr2 = Array(np.random.randn(3, 3))

Initializing with a PyTorch tensor:

import torch
arr3 = Array(torch.randn(3, 3))

Methods

deflate()

Deflate the Array.

from_numpy_ndarray(ndarray)

Create Array from NumPy ndarray.

from_torch_tensor(tensor)

Create Array from PyTorch tensor.

inflate(object_content[, children])

Inflate an Array from bytes.

numpy()

Return the array as a NumPy array.

Attributes

children

Get all child objects as a dictionary or None if there are no children.

is_dirty

Check if the object is dirty after the last deflation.

object_id

Get object ID.

shape

Get the shape of the array.

dtype

stype

data

property children: dict[str, InflatableObject] | None

Get all child objects as a dictionary or None if there are no children.

deflate() bytes[source]

Deflate the Array.

classmethod from_numpy_ndarray(ndarray: ndarray[Any, dtype[Any]]) Array[source]

Create Array from NumPy ndarray.

classmethod from_torch_tensor(tensor: torch.Tensor) Array[source]

Create Array from PyTorch tensor.

classmethod inflate(object_content: bytes, children: dict[str, InflatableObject] | None = None) Array[source]

Inflate an Array from bytes.

Paramètres:
  • object_content (bytes) – The deflated object content of the Array.

  • children (Optional[dict[str, InflatableObject]] (default: None)) – Must be None. Array does not support child objects. Providing any children will raise a ValueError.

Renvoie:

The inflated Array.

Type renvoyé:

Array

property is_dirty: bool

Check if the object is dirty after the last deflation.

numpy() ndarray[Any, dtype[Any]][source]

Return the array as a NumPy array.

property object_id: str

Get object ID.

property shape: list[int]

Get the shape of the array.