Tensor API

class xtensors.XTensor(data: Array, dims: Sequence[str | None] | None = None, coords: Sequence[Sequence[Any] | NDArray[Any] | None] | None = None)

A wrapper class of np.ndarray that attaches names to each axis. “axis” and “dimension” are synonyms in this context.

__init__(data: Array, dims: Sequence[str | None] | None = None, coords: Sequence[Sequence[Any] | NDArray[Any] | None] | None = None) None
Parameters:
  • data – the underlying array object, accepts any object that implements the __array__ protocol

  • dims – a sequence of strings specifying the names of each axis

  • coords – a sequence of 1D arrays specifying the coordinates of each axis

By default, each dimension has None as both its name and coordinates.

viewcopy() XTensor
Returns:

a new XTensor object with the same underlying data. Useful when one wishes to attach different metadata to the same array.

item() float
Returns:

data.item()

Raises:

ValueError if data is not scalar

property dims: Tuple[str | None, ...]

A tuple of strings or None corresponding to each axis’s name.

property coords: Tuple[NDArray[Any] | None, ...]

A tuple of np.ndarray or None corresponding to each axis’s coordinates.

property shape: Tuple[int, ...]

A tuple of int, same as data.shape

property rank: int

Number of axes, same as data.ndim

get_axis(dim: str | int | Tuple[str, int] | HasDimName) int

Performs an axis lookup with a DimLike object

Returns:

an integer representing the resulting axis

Raises:

TypeError if dim is not a valid DimLike object

get_axes(dims: DimLike | DimsLike | None) List[int]

Performs multiple axis lookups

Returns:

A list of axes (integers)

set_dims(dims: Sequence[str | None] | None) None

Set axis names.

set_dim(dim: DimLike, newdim: str | None) None

Set the name of a single axis.

get_coord(dim: DimLike) NDArray | None

Return the coordinates of the given axis.

Returns:

If the axis has coordinates, an np.ndarray is returned, else None is returned.

set_coords(coords: Sequence[Sequence[Any] | NDArray[Any] | None] | None) None

Set coordinates.

set_coord(dim: DimLike, coord: Sequence[Any] | NDArray[Any] | None) None

Set the coordinates of a single axis.

slc(dim: str | int | Tuple[str, int] | HasDimName, slc: slice) XTensor
Returns:

A new XTensor object with dim sliced by slice.

get(dim: str | int | Tuple[str, int] | HasDimName, index: int) XTensor
Returns:

A new XTensor object with dim indexed by index.