pydims.DimensionedArray#

class pydims.DimensionedArray(*, dims, values, unit)[source]#

Array with named dimensions and optional unit.

__init__(*, dims, values, unit)[source]#
Parameters:
  • dims (tuple[Hashable, ...]) – Dimension names.

  • values (ArrayImplementation) – Array of values.

  • unit (UnitImplementation | None) – Optional unit.

Methods

__init__(*, dims, values, unit)

type dims:

tuple[Hashable, ...]

astype(dtype[, copy])

rtype:

TypeVar(DimArr, bound= DimensionedArray)

expand_dims(sizes)

Expand an array by adding new dimensions of the given sizes at the beginning.

flatten(*[, dims, dim])

Flatten a set of dimensions into a single dimension.

fold(dim, *, sizes)

Fold a dimension of an array into a new set of dimensions.

permute_dims(dims)

Permute the dimensions of an array.

reshape(**kwargs)

Not supported since it relies on a particular axis order.

squeeze([dim])

Remove dimensions of size 1.

to(*[, dtype, unit, copy])

Convert to a new dtype and/or unit.

Attributes

array_namespace

dim

dims

dtype

ndim

shape

size

sizes

unit

units_namespace

values

__getitem__(key)[source]#

Get a sub-array identified by key.

Parameters:

key (int | slice | dict[Hashable, int | slice] | EllipsisType) – Index or slice or dictionary of dimension names and indices.

Returns:

TypeVar(DimArr, bound= DimensionedArray) – Sub-array.

__setitem__(key, array)[source]#

Set a sub-array identified by key to the values of array.

The array to set will be automatically transposed and/or broadcast to match the dimensions of the sub-array, using the named dimensions.

Parameters:
  • key (int | slice | dict[Hashable, int | slice] | EllipsisType) – Index or slice or dictionary of dimension names and indices.

  • array (TypeVar(DimArr, bound= DimensionedArray)) – Array to set.

expand_dims(sizes)#

Expand an array by adding new dimensions of the given sizes at the beginning.

Note that in contrast to the underlying expand_dims function, this function does not add dimensions of size 1, since this library implements broadcasting differently.

Use permute_dims() to control the order of the old and new dimensions.

Parameters:
  • array (TypeVar(DimArr, bound= DimensionedArray)) – Array to expand.

  • sizes (dict[Hashable, int]) – Ordered names and sizes of the new dimensions.

Returns:

TypeVar(DimArr, bound= DimensionedArray) – Expanded array.

flatten(*, dims=None, dim=None)#

Flatten a set of dimensions into a single dimension.

Parameters:
  • array (TypeVar(DimArr, bound= DimensionedArray)) – Array to flatten.

  • dims (tuple[Hashable, ...] | None, default: None) – Dimensions to flatten.

  • dim (Hashable | None, default: None) – Name of the new dimension.

Returns:

TypeVar(DimArr, bound= DimensionedArray) – Flattened array.

fold(dim, *, sizes)#

Fold a dimension of an array into a new set of dimensions.

Parameters:
  • array (TypeVar(DimArr, bound= DimensionedArray)) – Array to fold.

  • dim (Hashable) – Dimension to fold.

  • sizes (Mapping[Hashable, int]) – Sizes of the dimensions after folding.

Returns:

TypeVar(DimArr, bound= DimensionedArray) – Folded array.

permute_dims(dims)#

Permute the dimensions of an array.

Parameters:
  • array (TypeVar(DimArr, bound= DimensionedArray)) – Array to permute.

  • dims (tuple[Hashable, ...]) – New order of dimensions.

Returns:

TypeVar(DimArr, bound= DimensionedArray) – Permuted array.

reshape(**kwargs)#

Not supported since it relies on a particular axis order.

This function of the Python Array API is not supported by PyDims because it relies on a particular axis order. This library does not support this since it can lead to errors that are hard to debug.

Raises:

NotImplementedError – Always.

Return type:

NoReturn

squeeze(dim=None)#

Remove dimensions of size 1.

Parameters:
  • array (TypeVar(DimArr, bound= DimensionedArray)) – Array to squeeze.

  • dim (Hashable | tuple[Hashable, ...] | None, default: None) – Dimensions to remove. If None, remove all dimensions of size 1.

Returns:

TypeVar(DimArr, bound= DimensionedArray) – Squeezed array.

to(*, dtype=None, unit=None, copy=True)[source]#

Convert to a new dtype and/or unit.

Parameters:
  • dtype (Any | None, default: None) – New dtype, None if no conversion is needed.

  • unit (Any | None, default: None) – New unit, None if no conversion is needed.

  • copy (bool, default: True) – If True, a copy of the values is made, even if no conversion is needed.

Returns:

TypeVar(DimArr, bound= DimensionedArray) – New array with the requested dtype and/or unit.