equal_metadata#
- metatensor.equal_metadata(tensor_1: TensorMap, tensor_2: TensorMap, check: List[str] | str = 'all') bool [source]#
Checks if two
TensorMap
objects have the same metadata, returning a bool.The equivalence of the keys of the two
TensorMap
objects is always checked.If
check
is'all'
(the default), all metadata (i.e. the samples, components, and properties of each block) is checked to contain the same values in the same order. Passingcheck
as a list of strings will only check the specified metadata. Allowed values to pass are'samples'
,'components'
, and'properties'
.- Parameters:
- Returns:
True if the metadata of the two
TensorMap
objects are equal, False otherwise.- Return type:
Examples#
>>> import numpy as np >>> import metatensor >>> from metatensor import Labels, TensorBlock, TensorMap >>> tensor_1 = TensorMap( ... keys=Labels( ... names=["key_1", "key_2"], ... values=np.array([[1, 0], [2, 2]]), ... ), ... blocks=[ ... TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_1", "p_2"], np.array([[0, 1]])), ... ), ... TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_1", "p_2"], np.array([[0, 1]])), ... ), ... ], ... ) >>> tensor_2 = TensorMap( ... keys=Labels( ... names=["key_1", "key_2"], ... values=np.array([[1, 0], [2, 2]]), ... ), ... blocks=[ ... TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_3", "p_4"], np.array([[0, 1]])), ... ), ... TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_3", "p_4"], np.array([[0, 1]])), ... ), ... ], ... ) >>> metatensor.equal_metadata(tensor_1, tensor_2) False >>> metatensor.equal_metadata( ... tensor_1, ... tensor_2, ... check=("samples", "components"), ... ) True
- metatensor.equal_metadata_block(block_1: TensorBlock, block_2: TensorBlock, check: List[str] | str = 'all') bool [source]#
Checks if two
TensorBlock
objects have the same metadata, returning a bool.If
check
is'all'
(the default), all metadata (i.e. the samples, components, and properties of each block) is checked to contain the same values in the same order. Passingcheck
as a list of strings will only check the specified metadata. Allowed values to pass are'samples'
,'components'
, and'properties'
.- Parameters:
block_1 (TensorBlock) – The first
TensorBlock
.block_2 (TensorBlock) – The second
TensorBlock
to compare to the first.check (List[str] | str) – Which parts of the metadata to check. This can be a list containing any of
'samples'
,'components'
, and'properties'
; or the string'all'
to check everything. Defaults to'all'
.
- Returns:
True if the metadata of the two
TensorBlock
objects are equal, False otherwise.- Return type:
Examples#
>>> import numpy as np >>> import metatensor >>> from metatensor import Labels, TensorBlock >>> block_1 = TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_1", "p_2"], np.array([[0, 1]])), ... ) >>> block_2 = TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_3", "p_4"], np.array([[0, 1]])), ... ) >>> equal_metadata_block(block_1, block_2) False >>> equal_metadata_block( ... block_1, ... block_2, ... check=("samples", "components"), ... ) True
- metatensor.equal_metadata_raise(tensor_1: TensorMap, tensor_2: TensorMap, check: List[str] | str = 'all')[source]#
Raise a
NotEqualError
if twoTensorMap
have unequal metadata.The equivalence of the keys of the two
TensorMap
objects is always checked.If
check
is'all'
(the default), all metadata (i.e. the samples, components, and properties of each block) is checked to contain the same values in the same order. Passingcheck
as a list of strings will only check the specified metadata. Allowed values to pass are'samples'
,'components'
, and'properties'
.- Parameters:
- Raises:
NotEqualError – If the metadata is not the same.
Examples#
>>> import numpy as np >>> import metatensor >>> from metatensor import Labels, TensorBlock, TensorMap >>> tensor_1 = TensorMap( ... keys=Labels( ... names=["key_1", "key_2"], ... values=np.array([[1, 0], [2, 2]]), ... ), ... blocks=[ ... TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_1", "p_2"], np.array([[0, 1]])), ... ), ... TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_1", "p_2"], np.array([[0, 1]])), ... ), ... ], ... ) >>> tensor_2 = TensorMap( ... keys=Labels( ... names=["key_1", "key_2"], ... values=np.array([[1, 0], [2, 2]]), ... ), ... blocks=[ ... TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_3", "p_4"], np.array([[0, 1]])), ... ), ... TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_3", "p_4"], np.array([[0, 1]])), ... ), ... ], ... ) >>> metatensor.equal_metadata_raise(tensor_1, tensor_2) Traceback (most recent call last): ... metatensor.operations._utils.NotEqualError: inputs to 'equal_metadata_block_raise' should have the same properties, but they are not the same or not in the same order >>> metatensor.equal_metadata_raise( ... tensor_1, ... tensor_2, ... check=("samples", "components"), ... )
- metatensor.equal_metadata_block_raise(block_1: TensorBlock, block_2: TensorBlock, check: List[str] | str = 'all')[source]#
Raise a
NotEqualError
if twoTensorBlock
have unequal metadata.If
check
is'all'
(the default), all metadata (i.e. the samples, components, and properties of each block) is checked to contain the same values in the same order. Passingcheck
as a list of strings will only check the specified metadata. Allowed values to pass are'samples'
,'components'
, and'properties'
.- Parameters:
block_1 (TensorBlock) – The first
TensorBlock
.block_2 (TensorBlock) – The second
TensorBlock
to compare to the first.check (List[str] | str) – A sequence of strings specifying which metadata of each block to check. If none, all metadata is checked. Allowed values are “samples”, “components”, and “properties”.
- Raises:
NotEqualError – If the metadata is not the same.
Examples#
>>> import numpy as np >>> import metatensor >>> from metatensor import Labels, TensorBlock >>> block_1 = TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_1", "p_2"], np.array([[0, 1]])), ... ) >>> block_2 = TensorBlock( ... values=np.full((4, 3, 1), 4.0), ... samples=Labels(["samples"], np.array([[0], [1], [4], [5]])), ... components=[Labels.range("components", 3)], ... properties=Labels(["p_3", "p_4"], np.array([[0, 1]])), ... ) >>> metatensor.equal_metadata_block_raise(block_1, block_2) Traceback (most recent call last): ... metatensor.operations._utils.NotEqualError: inputs to 'equal_metadata_block_raise' should have the same properties, but they are not the same or not in the same order >>> metatensor.equal_metadata_block_raise( ... block_1, block_2, check=("samples", "components") ... )