allclose#
- metatensor.allclose(tensor_1: TensorMap, tensor_2: TensorMap, rtol: float = 1e-13, atol: float = 1e-12, equal_nan: bool = False) bool[source]#
- Compare two - TensorMap.- This function returns - Trueif the two tensors have the same keys (potentially in different order) and all the- TensorBlockhave the same (and in the same order) samples, components, properties, and their values matrices pass the numpy-like- allclosetest with the provided- rtol, and- atol.- The - TensorMapcontains gradient data, then this function only returns- Trueif all the gradients also have the same samples, components, properties and their data matrices pass the numpy-like- allclosetest with the provided- rtol, and- atol.- In practice this function calls - allclose_raise(), returning- Trueif no exception is raised,- Falseotherwise.- Parameters:
- Return type:
 - Examples#- >>> import numpy as np >>> from metatensor import Labels, TensorBlock - Create simple block - >>> block_1 = TensorBlock( ... values=np.array( ... [ ... [1, 2, 4], ... [3, 5, 6], ... ] ... ), ... samples=Labels( ... ["structure", "center"], ... np.array( ... [ ... [0, 0], ... [0, 1], ... ] ... ), ... ), ... components=[], ... properties=Labels(["properties"], np.array([[0], [1], [2]])), ... ) - Create a second block that is equivalent to - block_1.- >>> block_2 = TensorBlock( ... values=np.array( ... [ ... [1, 2, 4], ... [3, 5, 6], ... ] ... ), ... samples=Labels( ... ["structure", "center"], ... np.array( ... [ ... [0, 0], ... [0, 1], ... ] ... ), ... ), ... components=[], ... properties=Labels(["properties"], np.array([[0], [1], [2]])), ... ) - Create tensors from blocks, using keys with different names - >>> keys1 = Labels(names=["key1"], values=np.array([[0]])) >>> keys2 = Labels(names=["key2"], values=np.array([[0]])) >>> tensor_1 = TensorMap(keys1, [block_1]) >>> tensor_2 = TensorMap(keys2, [block_2]) - Call - metatensor.allclose(), which should fail as the blocks have different keys associated with them.- >>> allclose(tensor_1, tensor_2) False - Create a third tensor, which differs from - tensor_1only by- 1e-5in a single block value.- >>> block3 = TensorBlock( ... values=np.array( ... [ ... [1 + 1e-5, 2, 4], ... [3, 5, 6], ... ] ... ), ... samples=Labels( ... ["structure", "center"], ... np.array( ... [ ... [0, 0], ... [0, 1], ... ] ... ), ... ), ... components=[], ... properties=Labels(["properties"], np.array([[0], [1], [2]])), ... ) - Create tensors from blocks, using a key with same name as - block_1.- >>> keys3 = Labels(names=["key1"], values=np.array([[0]])) >>> tensor3 = TensorMap(keys3, [block3]) - Call - metatensor.allclose(), which should return False because the default- rtolis- 1e-13, and the difference in the first value between the blocks of the two tensors is- 1e-5.- >>> allclose(tensor_1, tensor3) False - Calling allclose again with the optional argument - rtol=1e-5should return- True, as the difference in the first value between the blocks of the two tensors is within the tolerance limit- >>> allclose(tensor_1, tensor3, rtol=1e-5) True 
- metatensor.allclose_raise(tensor_1: TensorMap, tensor_2: TensorMap, rtol: float = 1e-13, atol: float = 1e-12, equal_nan: bool = False)[source]#
- Compare two - TensorMap, raising- NotEqualErrorif they are not the same.- The message associated with the exception will contain more information on where the two - TensorMapdiffer. See- allclose()for more information on which- TensorMapare considered equal.- Raises:
- NotEqualErrorif the blocks are different
- Parameters:
 - Examples#- >>> import numpy as np >>> import metatensor >>> from metatensor import Labels, TensorBlock - Create simple block, with one py:obj:np.nan value. - >>> block_1 = TensorBlock( ... values=np.array( ... [ ... [1, 2, 4], ... [3, 5, np.nan], ... ] ... ), ... samples=Labels( ... ["structure", "center"], ... np.array( ... [ ... [0, 0], ... [0, 1], ... ] ... ), ... ), ... components=[], ... properties=Labels(["properties"], np.array([[0], [1], [2]])), ... ) - Create a second block that differs from - block_1by- 1e-5in its first value.- >>> block_2 = TensorBlock( ... values=np.array( ... [ ... [1 + 1e-5, 2, 4], ... [3, 5, np.nan], ... ] ... ), ... samples=Labels( ... ["structure", "center"], ... np.array( ... [ ... [0, 0], ... [0, 1], ... ] ... ), ... ), ... components=[], ... properties=Labels(["properties"], np.array([[0], [1], [2]])), ... ) - Create tensors from blocks, using same keys - >>> keys = Labels(names=["key"], values=np.array([[0]])) >>> tensor_1 = TensorMap(keys, [block_1]) >>> tensor_2 = TensorMap(keys, [block_2]) - Call - metatensor.allclose_raise(), which should raise- metatensor.NotEqualErrorbecause:- The two - NaNare not considered equal.
- The difference between the first value in the blocks is greater than the default - rtolof- 1e-13.
 - If this is executed yourself, you will see a nested exception explaining that the - valuesof the two blocks are not allclose.- >>> allclose_raise(tensor_1, tensor_2) Traceback (most recent call last): ... metatensor.operations._utils.NotEqualError: blocks for key (key=0) are different: values are not allclose - call - metatensor.allclose_raise()again, but use- equal_nan=Trueand- rtol=1e-5This passes, as the two- NaNare now considered equal, and the difference between the first value of the blocks of the two tensors is within the- rtollimit of- 1e-5.- >>> allclose_raise(tensor_1, tensor_2, equal_nan=True, rtol=1e-5) 
- metatensor.allclose_block(block_1: TensorBlock, block_2: TensorBlock, rtol: float = 1e-13, atol: float = 1e-12, equal_nan: bool = False) bool[source]#
- Compare two - TensorBlock.- This function returns - Trueif the two- TensorBlockhave the same samples, components, properties and their values matrices must pass the numpy-like- allclosetest with the provided- rtol, and- atol.- If the - TensorBlockcontains gradients, then the gradient must also have same (and in the same order) samples, components, properties and their data matrices must pass the numpy-like- allclosetest with the provided- rtol, and- atol.- In practice this function calls - allclose_block_raise(), returning- Trueif no exception is raised,- Falseotherwise.- Parameters:
- block_1 (TensorBlock) – first - TensorBlock
- block_2 (TensorBlock) – second - TensorBlock
- rtol (float) – relative tolerance for - allclose
- atol (float) – absolute tolerance for - allclose
- equal_nan (bool) – should two - NaNbe considered equal?
 
- Return type:
 - Examples#- >>> import numpy as np >>> from metatensor import Labels, TensorBlock - Create simple block - >>> block_1 = TensorBlock( ... values=np.array( ... [ ... [1, 2, 4], ... [3, 5, 6], ... ] ... ), ... samples=Labels( ... ["structure", "center"], ... np.array( ... [ ... [0, 0], ... [0, 1], ... ] ... ), ... ), ... components=[], ... properties=Labels(["property_1"], np.array([[0], [1], [2]])), ... ) - Recreate - block_1, but change first value in the block from- 1to- 1.00001.- >>> block_2 = TensorBlock( ... values=np.array( ... [ ... [1 + 1e-5, 2, 4], ... [3, 5, 6], ... ] ... ), ... samples=Labels( ... ["structure", "center"], ... np.array( ... [ ... [0, 0], ... [0, 1], ... ] ... ), ... ), ... components=[], ... properties=Labels(["property_1"], np.array([[0], [1], [2]])), ... ) - Call - metatensor.allclose_block(), which should return- Falsebecause the default- rtolis- 1e-13, and the difference in the first value between the two blocks is- 1e-5.- >>> allclose_block(block_1, block_2) False - Calling - metatensor.allclose_block()with the optional argument- rtol=1e-5should return- True, as the difference in the first value between the two blocks is within the tolerance limit.- >>> allclose_block(block_1, block_2, rtol=1e-5) True 
- metatensor.allclose_block_raise(block_1: TensorBlock, block_2: TensorBlock, rtol: float = 1e-13, atol: float = 1e-12, equal_nan: bool = False)[source]#
- Compare two - TensorBlock, raising- NotEqualErrorif they are not the same.- The message associated with the exception will contain more information on where the two - TensorBlockdiffer. See- allclose_block()for more information on which- TensorBlockare considered equal.- Raises:
- NotEqualErrorif the blocks are different
- Parameters:
- block_1 (TensorBlock) – first - TensorBlock
- block_2 (TensorBlock) – second - TensorBlock
- rtol (float) – relative tolerance for - allclose
- atol (float) – absolute tolerance for - allclose
- equal_nan (bool) – should two - NaNbe considered equal?
 
 - Examples#- >>> import numpy as np >>> import metatensor >>> from metatensor import Labels, TensorBlock - Create simple block - >>> block_1 = TensorBlock( ... values=np.array( ... [ ... [1, 2, 4], ... [3, 5, 6], ... ] ... ), ... samples=Labels( ... ["structure", "center"], ... np.array( ... [ ... [0, 0], ... [0, 1], ... ] ... ), ... ), ... components=[], ... properties=Labels(["property_1"], np.array([[0], [1], [2]])), ... ) - Recreate - block_1, but rename properties label- 'property_1'to- 'property_2'.- >>> block_2 = TensorBlock( ... values=np.array( ... [ ... [1, 2, 4], ... [3, 5, 6], ... ] ... ), ... samples=Labels( ... ["structure", "center"], ... np.array( ... [ ... [0, 0], ... [0, 1], ... ] ... ), ... ), ... components=[], ... properties=Labels(["property_2"], np.array([[0], [1], [2]])), ... ) - Call - metatensor.allclose_block_raise(), which should raise- metatensor.NotEqualError()because the properties of the two blocks are not equal.- >>> allclose_block_raise(block_1, block_2) Traceback (most recent call last): ... metatensor.operations._utils.NotEqualError: inputs to 'allclose' should have the same properties, but they are not the same or not in the same order