join¶
- metatensor.join(tensors: List[TensorMap], axis: str, different_keys: str = 'error', add_dimension: str | None = None) TensorMap[source]¶
Join a sequence of
TensorMapwith similar keys along an axis.The
axisparameter specifies the type of joining: withaxis='properties'thetensorswill be joined along thepropertiesand foraxis='samples'they will be joined along thesamples.- Parameters:
axis (str) – Along which axis the
TensorMap`s should be joined. This can be either `’properties’`` or'samples'.different_keys (str) – Method to handle different keys between the tensors. For
"error"keys in all tensors have to be the same. For"intersection"only blocks present in all tensors will be taken into account. For"union"missing keys will be treated like if they where associated with an empty block.add_dimension (str | None) – Add an the extra dimension to the joined labels with the given name. See examples for the case where this is applicable. The dimension forms the last dimension of the joined labels.
- Returns:
The joined
TensorMapwith more properties or samples than the inputs.- Return type:
Examples¶
The first use case for this function is when joining
TensorMapwith the same labels names (either alongsamplesorproperties):>>> import numpy as np >>> import metatensor as mts >>> from metatensor import Labels, TensorBlock, TensorMap
>>> values = np.array([[1.1, 2.1, 3.1]]) >>> samples = Labels("sample", np.array([[0]]))
Define two disjoint set of
Labels.>>> properties_1 = Labels("n", np.array([[0], [2], [3]])) >>> properties_2 = Labels("n", np.array([[1], [4], [5]]))
>>> block_1 = TensorBlock( ... values=values, ... samples=Labels.single(), ... components=[], ... properties=properties_1, ... ) >>> block_2 = TensorBlock( ... values=values, ... samples=Labels.single(), ... components=[], ... properties=properties_2, ... )
>>> tensor_1 = TensorMap(keys=Labels.single(), blocks=[block_1]) >>> tensor_2 = TensorMap(keys=Labels.single(), blocks=[block_2])
joining along the properties leads to
>>> joined_tensor = mts.join([tensor_1, tensor_2], axis="properties") >>> joined_tensor[0].properties Labels( n 0 2 3 1 4 5 )
Second, if the labels names are the same but the values are not unique, you can ask to add an extra dimension to the labels when joining with
add_dimension, thus creating unique values>>> properties_3 = Labels("n", np.array([[0], [2], [3]]))
properties_3has the same name and also shares values withproperties_1as defined above.>>> block_3 = TensorBlock( ... values=values, ... samples=Labels.single(), ... components=[], ... properties=properties_3, ... ) >>> tensor_3 = TensorMap(keys=Labels.single(), blocks=[block_3])
joining along properties leads to
>>> joined_tensor = mts.join( ... [tensor_1, tensor_3], axis="properties", add_dimension="tensor" ... ) >>> joined_tensor[0].properties Labels( n tensor 0 0 2 0 3 0 0 1 2 1 3 1 )
Finally, when joining along properties, if different
TensorMaphave different property names, we’ll re-create new properties labels containing the original tensor index and the corresponding property index. This does not apply when joining along samples.>>> properties_4 = Labels(["a", "b"], np.array([[0, 0], [1, 2], [1, 3]]))
properties_4has the different names compared toproperties_1defined above.>>> block_4 = TensorBlock( ... values=values, ... samples=Labels.single(), ... components=[], ... properties=properties_4, ... ) >>> tensor_4 = TensorMap(keys=Labels.single(), blocks=[block_4])
joining along properties leads to
>>> joined_tensor = mts.join([tensor_1, tensor_4], axis="properties") >>> joined_tensor[0].properties Labels( joined_index property 0 0 0 1 0 2 1 0 1 1 1 2 )
- metatensor.join_blocks(blocks: List[TensorBlock], axis: str, add_dimension: str | None = None) TensorBlock[source]¶
Join a sequence of
TensorBlockalong an axis.The
axisparameter specifies the type of joining: withaxis='properties'theblockswill be joined along thepropertiesand foraxis='samples'they will be joined along thesamples.- Parameters:
tensors – sequence of
TensorMapto joinaxis (str) – Along which axis the blocks should be joined. This can be either
'properties'or'samples'.add_dimension (str | None) – Add an the extra dimension to the joined labels with the given name. The dimension forms the last dimension of the joined labels.
blocks (List[TensorBlock])
- Returns:
The joined
TensorBlockwith more properties or samples than the inputs.- Return type:
See also
The examples for
join().