Struct metatensor::TensorMap

source ·
pub struct TensorMap { /* private fields */ }
Expand description

TensorMap is the main user-facing struct of this library, and can store any kind of data used in atomistic machine learning.

A tensor map contains a list of TensorBlocks, each one associated with a key in the form of a single Labels entry.

It provides functions to merge blocks together by moving some of these keys to the samples or properties labels of the blocks, transforming the sparse representation of the data to a dense one.

Implementations§

source§

impl TensorMap

source

pub fn new(keys: Labels, blocks: Vec<TensorBlock>) -> Result<TensorMap, Error>

Create a new TensorMap with the given keys and blocks.

The number of keys must match the number of blocks, and all the blocks must contain the same kind of data (same labels names, same gradients defined on all blocks).

source

pub unsafe fn from_raw(ptr: *mut mts_tensormap_t) -> TensorMap

Create a new TensorMap from a raw pointer.

This function takes ownership of the pointer, and will call mts_tensormap_free on it when the TensorMap goes out of scope.

Safety

The pointer must be non-null and created by mts_tensormap or TensorMap::into_raw.

source

pub fn into_raw(map: TensorMap) -> *mut mts_tensormap_t

Extract the underlying raw pointer.

The pointer should be passed back to TensorMap::from_raw or mts_tensormap_free to release the memory corresponding to this TensorMap.

source

pub fn try_clone(&self) -> Result<TensorMap, Error>

Clone this TensorMap, cloning all the data and metadata contained inside.

This can fail if the external data held inside an mts_array_t can not be cloned.

source

pub fn keys(&self) -> &Labels

Get the keys defined in this TensorMap

source

pub fn block_by_id(&self, index: usize) -> TensorBlockRef<'_>

Get a reference to the block at the given index in this TensorMap

Panics

If the index is out of bounds

source

pub fn block_mut_by_id(&mut self, index: usize) -> TensorBlockRefMut<'_>

Get a mutable reference to the block at the given index in this TensorMap

Panics

If the index is out of bounds

source

pub fn blocks_matching(&self, selection: &Labels) -> Result<Vec<usize>, Error>

Get the index of blocks matching the given selection.

The selection must contains a single entry, defining the requested key or keys. If the selection contains only a subset of the dimensions of the keys, there can be multiple matching blocks.

source

pub fn block_matching(&self, selection: &Labels) -> Result<usize, Error>

Get the index of the single block matching the given selection.

This function is similar to TensorMap::blocks_matching, but also returns an error if more than one block matches the selection.

source

pub fn block(&self, selection: &Labels) -> Result<TensorBlockRef<'_>, Error>

Get a reference to the block matching the given selection.

This function uses TensorMap::blocks_matching under the hood to find the matching block.

source

pub fn blocks(&self) -> Vec<TensorBlockRef<'_>>

Get a reference to every blocks in this TensorMap

source

pub fn blocks_mut(&mut self) -> Vec<TensorBlockRefMut<'_>>

Get a mutable reference to every blocks in this TensorMap

source

pub fn keys_to_samples( &self, keys_to_move: &Labels, sort_samples: bool ) -> Result<TensorMap, Error>

Merge blocks with the same value for selected keys dimensions along the samples axis.

The dimensions (names) of keys_to_move will be moved from the keys to the sample labels, and blocks with the same remaining keys dimensions will be merged together along the sample axis.

keys_to_move must be empty (keys_to_move.count() == 0), and the new sample labels will contain entries corresponding to the merged blocks’ keys.

The new sample labels will contains all of the merged blocks sample labels. The order of the samples is controlled by sort_samples. If sort_samples is true, samples are re-ordered to keep them lexicographically sorted. Otherwise they are kept in the order in which they appear in the blocks.

This function is only implemented if all merged block have the same property labels.

source

pub fn keys_to_properties( &self, keys_to_move: &Labels, sort_samples: bool ) -> Result<TensorMap, Error>

Merge blocks with the same value for selected keys dimensions along the property axis.

The dimensions (names) of keys_to_move will be moved from the keys to the property labels, and blocks with the same remaining keys dimensions will be merged together along the property axis.

If keys_to_move does not contains any entries (keys_to_move.count() == 0), then the new property labels will contain entries corresponding to the merged blocks only. For example, merging a block with key a=0 and properties p=1, 2 with a block with key a=2 and properties p=1, 3 will produce a block with properties a, p = (0, 1), (0, 2), (2, 1), (2, 3).

If keys_to_move contains entries, then the property labels must be the same for all the merged blocks. In that case, the merged property labels will contains each of the entries of keys_to_move and then the current property labels. For example, using a=2, 3 in keys_to_move, and blocks with properties p=1, 2 will result in a, p = (2, 1), (2, 2), (3, 1), (3, 2).

The new sample labels will contains all of the merged blocks sample labels. The order of the samples is controlled by sort_samples. If sort_samples is true, samples are re-ordered to keep them lexicographically sorted. Otherwise they are kept in the order in which they appear in the blocks.

source

pub fn components_to_properties( &self, dimensions: &[&str] ) -> Result<TensorMap, Error>

Move the given dimensions from the component labels to the property labels for each block in this TensorMap.

source

pub fn iter(&self) -> TensorMapIter<'_>

Get an iterator over the keys and associated blocks

source

pub fn iter_mut(&mut self) -> TensorMapIterMut<'_>

Get an iterator over the keys and associated blocks, with read-write access to the blocks

Trait Implementations§

source§

impl Debug for TensorMap

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for TensorMap

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a> IntoIterator for &'a TensorMap

§

type Item = (&'a [LabelValue], TensorBlockRef<'a>)

The type of the elements being iterated over.
§

type IntoIter = TensorMapIter<'a>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a> IntoIterator for &'a mut TensorMap

§

type Item = (&'a [LabelValue], TensorBlockRefMut<'a>)

The type of the elements being iterated over.
§

type IntoIter = TensorMapIterMut<'a>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl Send for TensorMap

source§

impl Sync for TensorMap

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.