unique_metadata¶
- metatensor.unique_metadata(tensor: TensorMap, axis: str, names: List[str] | Tuple[str] | str, gradient: str | None = None) Labels[source]¶
Returns a
Labelsobject containing the unique metadata across all blocks of the inputTensorMaptensor. Unique Labels are returned for the specifiedaxis(either"samples"or"properties") and metadatanames.Passing
gradientas astrcorresponding to a gradient parameter (for instance"strain"or"positions") returns the unique indices only for the gradient blocks. Note that gradient blocks by definition have the same properties metadata as their parentTensorBlock.An empty
Labelsobject is returned if there are no indices in the (gradient) blocks oftensorcorresponding to the specifiedaxisandnames. This will have length zero but the names will be the same as passed innames.For example, to find the unique
"system"indices in the"samples"metadata present in a givenTensorMap:>>> import numpy as np >>> from metatensor import Labels, TensorBlock, TensorMap >>> import metatensor >>> block = TensorBlock( ... values=np.random.rand(5, 3), ... samples=Labels( ... names=["system", "atom"], ... values=np.array([[0, 0], [0, 1], [1, 0], [1, 1], [2, 3]]), ... ), ... components=[], ... properties=Labels.range("properties", 3), ... ) >>> keys = Labels(names=["key"], values=np.array([[0]])) >>> tensor = TensorMap(keys, [block.copy()]) >>> unique_systems = metatensor.unique_metadata( ... tensor, ... axis="samples", ... names=["system"], ... ) >>> unique_systems Labels( system 0 1 2 )
Or, to find the unique
(system, atom)pairs of indices in the"samples"metadata present in the"positions"gradient blocks of a givenTensorMap:>>> gradient = TensorBlock( ... values=np.random.rand(4, 3, 3), ... samples=Labels( ... names=["sample", "system", "atom"], ... values=np.array([[0, 0, 0], [1, 0, 0], [2, 1, 4], [3, 2, 5]]), ... ), ... components=[Labels.range("xyz", 3)], ... properties=Labels.range("properties", 3), ... ) >>> block.add_gradient("positions", gradient) >>> tensor = TensorMap(keys, [block]) >>> metatensor.unique_metadata( ... tensor, ... axis="samples", ... names=["system", "atom"], ... gradient="positions", ... ) Labels( system atom 0 0 1 4 2 5 )
- Parameters:
tensor (TensorMap) – the
TensorMapto find unique indices for.axis (str) – a
str, either"samples"or"properties", corresponding to theaxisalong which the named unique indices should be found.names (List[str] | Tuple[str] | str) – a
str,listofstr, ortupleofstrcorresponding to the name(s) of the indices along the specifiedaxisfor which the unique values should be found.gradient (str | None) – a
strcorresponding to the gradient parameter name for the gradient blocks to find the unique indices for. IfNone(default), the unique indices of theTensorBlockcontaining the values will be calculated.
- Returns:
a sorted
Labelsobject containing the unique metadata for the blocks of the inputtensoror its gradient blocks for the specified parameter. Each element in the returnedLabelsobject has len(names) entries.- Return type:
- metatensor.unique_metadata_block(block: TensorBlock, axis: str, names: List[str] | Tuple[str] | str, gradient: str | None = None) Labels[source]¶
Returns a
Labelsobject containing the unique metadata in the inputTensorBlockblock, for the specifiedaxis(either"samples"or"properties") and metadatanames.Passing
gradientas astrcorresponding to a gradient parameter (for instance"strain"or"positions") returns the unique indices only for the gradient block associated withblock. Note that gradient blocks by definition have the same properties metadata as their parentTensorBlock.An empty
Labelsobject is returned if there are no indices in the (gradient) blocks oftensorcorresponding to the specifiedaxisandnames. This will have length zero but the names will be the same as passed innames.- Parameters:
block (TensorBlock) – the
TensorBlockto find unique indices for.axis (str) – a str, either
"samples"or"properties", corresponding to theaxisalong which the named unique metadata should be found.names (List[str] | Tuple[str] | str) – a
str,listofstr, ortupleofstrcorresponding to the name(s) of the metadata along the specifiedaxisfor which the unique indices should be found.gradient (str | None) – a
strcorresponding to the gradient parameter name for the gradient blocks to find the unique metadata for. IfNone(default), the unique metadata of the regularTensorBlockobjects will be calculated.
- Returns:
a sorted
Labelsobject containing the unique metadata for the inputblockor its gradient for the specified parameter. Each element in the returnedLabelsobject has len(names) entries.- Return type: