Data arrays#
- class metatensor.data.Array#
An
Array
contains the actual data stored in ametatensor.TensorBlock
.This data is manipulated by
metatensor
in a completely opaque way: this library does not know what’s inside the arrays appart from a small set of constrains:the array contains numeric data (loading and saving
metatensor.TensorMap
additionally assumes arrays of 64-bit IEEE-754 floating points numbers);they are stored as row-major, n-dimensional arrays with at least 2 dimensions;
it is possible to create new arrays and move data from one array to another.
The actual type of an
Array
depends on how themetatensor.TensorBlock
was created. Currently,numpy.ndarray
andtorch.Tensor
are supported.
- metatensor.data.register_external_data_wrapper(origin, klass)[source]#
Register a non-Python data origin and the corresponding class wrapper.
The wrapper class constructor must take two arguments (raw
mts_array
and pythonparent
object) and return a subclass of eithernumpy.ndarray
ortorch.Tensor
, which keepsparent
alive. Themetatensor.data.ExternalCpuArray
class should provide the right behavior for data living in CPU memory, and can serve as an example for more advanced custom arrays.- Parameters:
origin – new origin to register as a string
klass – wrapper class to use for this origin
- class metatensor.data.ExternalCpuArray(mts_array: mts_array_t, parent: Any)[source]#
Small wrapper class around
np.ndarray
, adding a reference to a parent Python object that actually owns the memory used inside the array. This prevents the parent from being garbage collected while the ndarray is still alive, thus preventing use-after-free.This is intended to be used to wrap Rust-owned memory inside a numpy’s ndarray, while making sure the memory owner is kept around for long enough.
- Parameters:
mts_array (mts_array_t) – raw array to wrap in a Python-compatible class
parent (Any) – owner of the raw array, we will keep a reference to this python object