Labels¶
-
using metatensor_torch::Labels = torch::intrusive_ptr<LabelsHolder>¶
TorchScript will always manipulate
LabelsHolderthrough atorch::intrusive_ptr
-
class LabelsHolder : public CustomClassHolder¶
Wrapper around
metatensor::Labelsfor integration with TorchScriptPython/TorchScript code will typically manipulate
torch::intrusive_ptr<LabelsHolder>(i.e.Labels) instead of instances ofLabelsHolder.The main difference with
metatensor::Labelsis that the values of the labels entries are stored twice: once inside the Rust side labels, and once in atorch::Tensor. The data inside the tensor can be moved to different devices if needed.Public Functions
-
LabelsHolder(torch::IValue names, torch::Tensor values)¶
Construct
LabelsHolderfrom a set of names and the corresponding valuesThe names should be either a single string or a list/tuple of strings; and the values should be a 2D tensor of integers.
-
explicit LabelsHolder(metatensor::Labels labels)¶
Create a
LabelsHolderfrom a pre-existingmetatensor::Labels
-
inline std::vector<std::string> names() const¶
Get the names of the dimensions/columns of these Labels.
-
inline torch::Tensor values() const¶
Get the values of these labels as a torch Tensor.
-
Labels append(std::string name, torch::Tensor values) const¶
Create new
Labelswith a new dimension with the givennameandvaluesadded to the end of the dimensions list.
-
Labels insert(int64_t index, std::string name, torch::Tensor values) const¶
Create new
Labelswith a new dimension with the givennameandvaluesbeforeindex.
-
Labels permute(std::vector<int64_t> dimensions_indexes) const¶
Create new
Labelswith permuted dimensions.
-
Labels remove(std::string name) const¶
Create new
Labelswithnameremoved from the dimensions list.
-
Labels rename(std::string old_name, std::string new_name) const¶
Create new
Labelswithold_namerenamed tonew_namein the dimensions list
-
inline torch::Device device() const¶
Get the current device for these
Labels
-
torch::Tensor column(std::string dimension)¶
Get the values associated with a single dimension (i.e. a single column of
values()) in these labels.
-
inline int64_t count() const¶
Get the number of entries in this set of Labels.
This is the same as
values().size(0)
-
inline int64_t size() const¶
Get the number of dimensions in this set of Labels.
This is the same as
values().size(1)
-
torch::optional<int64_t> position(torch::IValue entry) const¶
Get the position of the given
entryin this set of Labels, or None if the entry is not part of these labels.- Parameters:
entry – one of:
a
LabelsEntrya 1-D torch::Tensor containing integers;
a list of integers;
a tuple of integers;
-
std::string print(int64_t max_entries, int64_t indent) const¶
Print the names and values of these Labels to a string, including at most
max_entriesentries (set this to -1 to print all entries), and indenting all lines after the first withindentspaces.
-
std::string str() const¶
Implementation of
__str__for Python.
-
std::string repr() const¶
Implementation of
__repr__for Python.
-
const metatensor::Labels &as_metatensor() const¶
Get the underlying metatensor::Labels.
-
inline bool is_view() const¶
Is this a view inside existing Labels or an owned Labels?
-
Labels to_owned() const¶
Transform a view of Labels into owned Labels, which can be further given to metatensor functions. This does nothing if the Labels are already owned.
-
std::tuple<Labels, torch::Tensor, torch::Tensor> union_and_mapping(const Labels &other) const¶
Get the union of
thisandother, as well as the mapping from positions of entries in the input to the position of entries in the output.
-
std::tuple<Labels, torch::Tensor, torch::Tensor> intersection_and_mapping(const Labels &other) const¶
Get the intersection of
thisandother, as well as the mapping from positions of entries in the input to the position of entries in the output.
-
torch::Tensor select(const Labels &selection) const¶
Select entries in these
Labelsthat match theselection.The selection’s names must be a subset of the names of these labels.
All entries in these
Labelsthat match one of the entry in theselectionfor all the selection’s dimension will be picked. Any entry in theselectionbut not in theseLabelswill be ignored.
-
void save(const std::string &path) const¶
Serialize and save Labels to the given path.
-
torch::Tensor save_buffer() const¶
Serialize and save Labels to an in-memory buffer (represented as a
torch::Tensorof bytes)
Public Static Functions
-
static Labels create(std::vector<std::string> names, const std::vector<std::initializer_list<int32_t>> &values)¶
Convenience constructor for building
LabelsHolderin C++, similar tometatensor::Labels.
-
static Labels view(const Labels &labels, std::vector<std::string> names)¶
Get a view of
labelscorresponding to only the given columns names.
-
static Labels empty(torch::IValue names)¶
Create Labels with the given dimension names and zero entries.
-
LabelsHolder(torch::IValue names, torch::Tensor values)¶
-
using metatensor_torch::LabelsEntry = torch::intrusive_ptr<LabelsEntryHolder>¶
TorchScript will always manipulate
LabelsEntryHolderthrough atorch::intrusive_ptr
-
class LabelsEntryHolder : public CustomClassHolder¶
A single entry inside a
LabelsPublic Functions
-
LabelsEntryHolder(Labels labels, int64_t index)¶
Create a new
LabelsEntryHoldercorresponding to the entry at the givenindexin the givenlabels
-
inline std::vector<std::string> names() const¶
Get the names of the dimensions/columns of these Labels.
-
inline torch::Tensor values() const¶
Get the values of these labels as a torch Tensor.
-
inline torch::Device device() const¶
Get the current device for this
LabelsEntry
-
inline int64_t size() const¶
Get the number of dimensions in this
LabelsEntry.This is the same as
values().size(0)
-
inline int32_t operator[](int64_t index) const¶
Get the value at
indexin thisLabelsEntry
-
int32_t operator[](const std::string &name) const¶
Get the value for the
namedimension in thisLabelsEntry
-
int64_t getitem(torch::IValue index) const¶
implementation of getitem, forwarding to one of the operator[]
-
std::string print() const¶
Print this entry as a named tuple (i.e.
(key=value, key=value)).
-
std::string repr() const¶
Implementation of repr for Python.
-
LabelsEntryHolder(Labels labels, int64_t index)¶