Labels¶
-
class Labels¶
A set of labels used to carry metadata associated with a tensor map.
This is similar to an array of named tuples, but stored as a 2D array of shape
(count, size), with a set of names associated with the columns of this array (often called dimensions). Each row/entry in this array is unique, and they are often (but not always) sorted in lexicographic order.Public Functions
-
inline Labels(const std::vector<std::string> &dimensions, MtsArray values)¶
Create Labels from the given dimension names and values.
The Labels take ownership of the values.
-
inline Labels(const std::vector<std::string> &dimensions, std::initializer_list<std::initializer_list<int32_t>> values)¶
Create a new set of Labels from the given
namesandvalues.Each entry in the values must contain
names.size()elements.auto labels = Labels({"first", "second"}, { {0, 1}, {1, 4}, {2, 1}, {2, 3}, });
-
inline Labels(const std::vector<std::string> &dimensions, MtsArray array, assume_unique)¶
Create Labels from the given dimension names and a backing mts_array_t, assuming uniqueness of entries (no uniqueness check is performed).
The Labels take ownership of the array.
-
inline explicit Labels(const std::vector<std::string> &dimensions)¶
Create an empty set of Labels with the given dimension names.
-
inline Labels(const std::vector<std::string> &dimensions, const int32_t *values, size_t count)¶
Create labels with the given
dimensionsandvalues.valuesmust be an array withcount x dimensions.size()elements.
-
inline Labels(const std::vector<std::string> &dimensions, const int32_t *values, size_t count, assume_unique)¶
Unchecked variant, caller promises the labels are unique. Calling with non-unique entries is invalid and can lead to crashes or infinite loops.
-
inline std::vector<const char*> names() const¶
Get the names of the dimensions used in these
Labels.
-
inline size_t size() const¶
Get the number of dimensions in this set of Labels.
This is the same as
shape()[1]for the corresponding values array
-
inline const mts_labels_t *as_mts_labels_t() const¶
Get the underlying
mts_labels_tpointer.
-
inline std::optional<size_t> position(std::initializer_list<int32_t> entry) const¶
Get the position of the
entryin this set of Labels, orstd::nulloptif the entry is not part of these Labels.
-
template<size_t N>
inline std::optional<size_t> position(const std::array<int32_t, N> &entry) const¶ Variant of
Labels::positiontaking a fixed-size array as input.
-
inline std::optional<size_t> position(const std::vector<int32_t> &entry) const¶
Variant of
Labels::positiontaking a vector as input.
-
inline std::optional<size_t> position(const int32_t *entry, size_t length) const¶
Variant of
Labels::positiontaking a pointer and length as input.
-
inline MtsArray mts_array() const¶
Get the array of values for these Labels as an
mts_array_t.
-
inline DLPackArray<int32_t> values(DLDevice device = {kDLCPU, 0}, const int64_t *stream = nullptr) const¶
Get the array of values for these Labels as a DLPack array on the requested
device.- Parameters:
device – the DLPack device to request data on (default: CPU)
stream – pointer to a device stream, or nullptr for default
-
inline NDArray<int32_t> values_cpu() const¶
Get the array of values for these Labels on CPU.
This can trigger a copy if the values are not already on CPU, but following calls to this function will then return a view without copying.
-
inline Labels set_union(const Labels &other, int64_t *first_mapping = nullptr, size_t first_mapping_count = 0, int64_t *second_mapping = nullptr, size_t second_mapping_count = 0) const¶
Take the union of these
Labelswithother.If requested, this function can also give the positions in the union where each entry of the input
Labelsended up.The output data will be on CPU, regardless of the device of the inputs.
- Parameters:
other – the
Labelswe want to take the union withfirst_mapping – if you want the mapping from the positions of entries in
thisto the positions in the union, this should be a pointer to an array containingthis->count()elements, to be filled by this function. Otherwise it should be anullptr.first_mapping_count – number of elements in
first_mappingsecond_mapping – if you want the mapping from the positions of entries in
otherto the positions in the union, this should be a pointer to an array containingother.count()elements, to be filled by this function. Otherwise it should be anullptr.second_mapping_count – number of elements in
second_mapping
-
inline Labels set_union(const Labels &other, std::vector<size_t> &first_mapping, std::vector<size_t> &second_mapping) const¶
Take the union of these
Labelswithother.If requested, this function can also give the positions in the union where each entry of the input
Labelsended up.The output data will be on CPU, regardless of the device of the inputs.
- Parameters:
other – the
Labelswe want to take the union withfirst_mapping – if you want the mapping from the positions of entries in
thisto the positions in the union, this should be a vector containingthis->count()elements, to be filled by this function. Otherwise it should be an empty vector.second_mapping – if you want the mapping from the positions of entries in
otherto the positions in the union, this should be a vector containingother.count()elements, to be filled by this function. Otherwise it should be an empty vector.
-
inline Labels set_intersection(const Labels &other, int64_t *first_mapping = nullptr, size_t first_mapping_count = 0, int64_t *second_mapping = nullptr, size_t second_mapping_count = 0) const¶
Take the intersection of these
Labelswithother.If requested, this function can also give the positions in the intersection where each entry of the input
Labelsended up.The output data will be on CPU, regardless of the device of the inputs.
- Parameters:
other – the
Labelswe want to take the intersection withfirst_mapping – if you want the mapping from the positions of entries in
thisto the positions in the intersection, this should be a pointer to an array containingthis->count()elements, to be filled by this function. Otherwise it should be anullptr. If an entry inthisis not used in the intersection, the mapping will be set to -1.first_mapping_count – number of elements in
first_mappingsecond_mapping – if you want the mapping from the positions of entries in
otherto the positions in the intersection, this should be a pointer to an array containingother.count()elements, to be filled by this function. Otherwise it should be anullptr. If an entry inotheris not used in the intersection, the mapping will be set to -1.second_mapping_count – number of elements in
second_mapping
-
inline Labels set_intersection(const Labels &other, std::vector<std::optional<size_t>> &first_mapping, std::vector<std::optional<size_t>> &second_mapping) const¶
Take the intersection of this
Labelswithother.If requested, this function can also give the positions in the intersection where each entry of the input
Labelsended up.The output data will be on CPU, regardless of the device of the inputs.
- Parameters:
other – the
Labelswe want to take the intersection withfirst_mapping – if you want the mapping from the positions of entries in
thisto the positions in the intersection, this should be a vector containingthis->count()elements, to be filled by this function. Otherwise it should be an empty vector. If an entry inthisis not used in the intersection, the mapping will be set tostd::nullopt.second_mapping – if you want the mapping from the positions of entries in
otherto the positions in the intersection, this should be a vector containingother.count()elements, to be filled by this function. Otherwise it should be an empty vector. If an entry inotheris not used in the intersection, the mapping will be set tostd::nullopt.
-
inline Labels set_difference(const Labels &other, int64_t *first_mapping = nullptr, size_t first_mapping_count = 0) const¶
Take the difference of these
Labelswithother.If requested, this function can also give the positions in the difference where each entry of the input
Labelsended up.The output data will be on CPU, regardless of the device of the inputs.
- Parameters:
other – the
Labelswe want to take the difference withfirst_mapping – if you want the mapping from the positions of entries in
thisto the positions in the difference, this should be a pointer to an array containingthis->count()elements, to be filled by this function. Otherwise it should be anullptr. If an entry inthisis not used in the difference, the mapping will be set to -1.first_mapping_count – number of elements in
first_mapping
-
inline Labels set_difference(const Labels &other, std::vector<std::optional<size_t>> &first_mapping) const¶
Take the difference of this
Labelswithother.If requested, this function can also give the positions in the difference where each entry of the input
Labelsended up.The output data will be on CPU, regardless of the device of the inputs.
- Parameters:
other – the
Labelswe want to take the difference withfirst_mapping – if you want the mapping from the positions of entries in
thisto the positions in the difference, this should be a vector containingthis->count()elements, to be filled by this function. Otherwise it should be an empty vector. If an entry inthisis not used in the difference, the mapping will be set tostd::nullopt.
-
inline void select(const Labels &selection, int64_t *selected, size_t *selected_count) 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.- Parameters:
selection – definition of the selection criteria. Multiple entries are interpreted as a logical
oroperation.selected – on input, a pointer to an array with space for
*selected_countentries. On output, the first*selected_countvalues will contain the index inlabelsof selected entries.selected_count – on input, size of the
selectedarray. On output, this will contain the number of selected entries.
-
inline std::vector<size_t> select(const Labels &selection) const¶
Select entries in these
Labelsthat match theselection.This function does the same thing as the one above, but allocates and return the list of selected indexes in a
std::vector
-
inline void save(const std::string &path) const¶
Save
Labelsto the given path.This is identical to
metatensor::io::save(), and provided as a convenience API.
-
inline std::vector<uint8_t> save_buffer() const¶
Save
Labelsto an in-memory buffer.This is identical to
metatensor::io::save_buffer(), and provided as a convenience API.
-
template<typename Buffer>
inline Buffer save_buffer() const¶ Save
Labelsto an in-memory buffer.This is identical to
metatensor::io::save_buffer(), and provided as a convenience API.
Public Static Functions
-
static inline Labels load(const std::string &path)¶
Load previously saved
Labelsfrom the given path.This is identical to
metatensor::io::load_labels(), and provided as a convenience API.
-
static inline Labels load_buffer(const uint8_t *buffer, size_t buffer_count)¶
Load previously saved
Labelsfrom a in-memory buffer.This is identical to
metatensor::io::load_labels_buffer(), and provided as a convenience API.
-
template<typename Buffer>
static inline Labels load_buffer(const Buffer &buffer)¶ Load previously saved
Labelsfrom a in-memory buffer.This is identical to
metatensor::io::load_labels_buffer(), and provided as a convenience API.
-
inline Labels(const std::vector<std::string> &dimensions, MtsArray values)¶