UnytArrayBuffer#

class fields.buffers.core.UnytArrayBuffer(array: unyt_array)[source]#

A buffer that wraps a unyt_array, providing unit-aware numerical storage.

This buffer is designed for use cases that require physical units. Internally, it stores data as a unyt_array and ensures consistent behavior when interacting with values that carry or lack units.

The class supports coercion from raw array-like types (NumPy arrays, lists, tuples) and automatically wraps them in a unyt_array.

Examples

Create an UnytArrayBuffer from a list of values.

>>> UnytArrayBuffer.from_array([1, 2, 3], units="m")
UnytArrayBuffer(shape=(3,), dtype=int64)

Generate a buffer using the ones() method.

>>> UnytArrayBuffer.ones((2, 2), units="kg").as_core()
unyt_array([[1., 1.],
       [1., 1.]], 'kg')

Wrap a generic unyt_array.

>>> UnytArrayBuffer.from_array(unyt_array([1.,1.],units='keV'))
UnytArrayBuffer(shape=(2,), dtype=float64)

Methods

__init__(array)

Initialize the buffer with a NumPy array.

as_array()

Return the buffer as a NumPy array.

as_core()

Return the raw backend array object stored in this buffer.

as_repr()

Return this buffer as an unyt array.

as_unyt_array()

Convert the buffer contents into a unyt_array with attached units.

astype(dtype, *args, **kwargs)

Return a copy of this buffer with a different data type.

broadcast_to(shape, *args, **kwargs)

Broadcast an array to a new shape.

can_handle(obj)

Return True if obj can be wrapped by this buffer class.

can_handle_list()

Return a list of type names that this buffer can wrap.

convert_to_base([unit_system, equivalence])

Convert this buffer in-place to base units for the given unit system.

convert_to_units(units[, equivalence])

Convert the buffer data to the specified physical units in-place.

copy(*args, **kwargs)

Return a deep copy of this buffer.

empty(shape, *args[, units])

Return a new array of given shape and type, without initializing entries.

empty_like(other, *args, **kwargs)

Create a new buffer allocation matching the shape of another buffer.

expand_dims(axis, *args, **kwargs)

Expand the shape of an array.

flatten(*args[, order])

Return a flattened 1D view of this buffer.

from_array(obj[, dtype, units, registry, ...])

Construct a new unit-aware buffer from any array-like input.

full(shape, *args[, fill_value])

Create a buffer filled with a constant value and attached units.

full_like(other[, fill_value])

Create a new buffer filled with a constant value and matching the shape of another buffer.

in_units(units, *args[, equivalence, ...])

Return a new copy of this buffer cast to the specified physical units.

ones(shape, *args[, units])

Create a buffer filled with ones and optional units.

ones_like(other, *args, **kwargs)

Create a new buffer filled with ones and matching the shape of another buffer.

reshape(shape, *args, **kwargs)

Return a reshaped copy of this buffer.

resolve(array_like, *args[, buffer_registry])

Resolve and instantiate a buffer subclass for an arbitrary array-like input.

squeeze(*args[, axis])

Remove axes of length one from self.

to(units, *args[, equivalence, ...])

Return a new buffer (or array) with values cast to the specified units.

to_value(units[, equivalence])

Return a NumPy array of values converted to the specified physical units.

transpose([axes])

Return this buffer with axes transposed.

zeros(shape, *args[, units])

Create a buffer filled with zeros and optional units.

zeros_like(other, *args, **kwargs)

Create a new buffer filled with zeros and matching the shape of another buffer.

Attributes

c

Shorthand for as_core().

d

Shorthand for as_array().

dtype

Data type of the array.

has_units

Whether the buffer carries unit metadata.

ndim

Number of dimensions.

shape

Shape of the underlying array.

size

Total number of elements.

units

Physical units attached to the buffer data.

v

Shorthand for as_unyt_array().