Manipulating TensorMap dimensions#
Functions for manipulating dimensions of an metatensor.TensorMap (i.e.
changing the columns of the metatensor.Labels within).
- metatensor.append_dimension(tensor: TensorMap, axis: str, name: str, values) TensorMap[source]#
- Append a - metatensor.Labelsdimension along the given axis.- For - axis=="samples"the new dimension is not appended to gradients.- Parameters:
- Raises:
- ValueError – if - axisis a not valid value
- Returns:
- a new - metatensor.TensorMapwith appended labels dimension.
- Return type:
 - Examples#- >>> import numpy as np >>> import metatensor >>> values = np.array([[1, 2], [3, 4]]) >>> block = metatensor.block_from_array(values) >>> keys = metatensor.Labels(["foo"], np.array([[0]])) >>> tensor = metatensor.TensorMap(keys=keys, blocks=[block]) >>> tensor TensorMap with 1 blocks keys: foo 0 >>> metatensor.append_dimension( ... tensor, ... axis="keys", ... name="bar", ... values=np.array([1]), ... ) TensorMap with 1 blocks keys: foo bar 0 1 
- metatensor.insert_dimension(tensor: TensorMap, axis: str, index: int, name: str, values) TensorMap[source]#
- Insert a - metatensor.Labelsdimension along the given axis before the given index.- For - axis=="samples"a new dimension is not appended to gradients.- Parameters:
- axis (str) – axis for which the - nameshould be inserted. Allowed are- "keys",- "properties"or- "samples".
- index (int) – index before the new dimension is inserted. 
- name (str) – the name to be inserted 
- values – values to be inserted ( - np.arrayor- torch.Tensoraccording to whether- metatensoror- metatensor.torchis being used)
 
- Raises:
- ValueError – if - axisis a not valid value
- Returns:
- a new - metatensor.TensorMapwith inserted labels dimension.
- Return type:
 - Examples#- >>> import numpy as np >>> import metatensor >>> values = np.array([[1, 2], [3, 4]]) >>> block = metatensor.block_from_array(values) >>> keys = metatensor.Labels(["foo"], np.array([[0]])) >>> tensor = metatensor.TensorMap(keys=keys, blocks=[block]) >>> tensor TensorMap with 1 blocks keys: foo 0 >>> metatensor.insert_dimension( ... tensor, ... axis="keys", ... index=0, ... name="bar", ... values=np.array([1]), ... ) TensorMap with 1 blocks keys: bar foo 1 0 
- metatensor.permute_dimensions(tensor: TensorMap, axis: str, dimensions_indexes: List[int]) TensorMap[source]#
- Permute dimensions of a - Labelsof the given axis according to a- listof indexes.- Values of - dimensions_indexeshave to be same as the indexes of- Labelsbut can be in a different order.- For - axis=="samples"gradients samples dimensions are not permuted.- Parameters:
- Raises:
- ValueError – if - axisis a not valid value
- Returns:
- a new - metatensor.TensorMapwith the labels dimension permuted
- Return type:
 - Examples#- >>> import numpy as np >>> import metatensor >>> values = np.array([[1, 2], [3, 4]]) >>> block = metatensor.block_from_array(values) >>> keys = metatensor.Labels(["foo", "bar", "baz"], np.array([[42, 10, 3]])) >>> tensor = metatensor.TensorMap(keys=keys, blocks=[block]) >>> tensor TensorMap with 1 blocks keys: foo bar baz 42 10 3 - Move the last (second) dimension to the first position. - >>> metatensor.permute_dimensions(tensor, axis="keys", dimensions_indexes=[2, 0, 1]) TensorMap with 1 blocks keys: baz foo bar 3 42 10 
- metatensor.remove_dimension(tensor: TensorMap, axis: str, name: str) TensorMap[source]#
- Remove a - metatensor.Labelsdimension along the given axis.- Removal can only be performed if the resulting - metatensor.Labelsinstance will be unique.- For - axis=="samples"the dimension is not removed from gradients.- Parameters:
- axis (str) – axis for which - nameshould be removed. Allowed are- "keys",- "properties"or- "samples".
- name (str) – the - metatensor.Labelsname to be removed
 
- Raises:
- ValueError – if - axisis a not valid value
- ValueError – if the only dimension should be removed 
- ValueError – if name is not part of the axis 
 
- Returns:
- a new - metatensor.TensorMapwith removed labels dimension.
- Return type:
 - Examples#- >>> import numpy as np >>> import metatensor >>> values = np.array([[1, 2], [3, 4]]) >>> block = metatensor.block_from_array(values) >>> keys = metatensor.Labels(["key", "extra"], np.array([[0, 0]])) >>> tensor = metatensor.TensorMap(keys=keys, blocks=[block]) >>> tensor TensorMap with 1 blocks keys: key extra 0 0 >>> metatensor.remove_dimension(tensor, axis="keys", name="extra") TensorMap with 1 blocks keys: key 0 - Removing a dimension can only be performed if the resulting - Labelswill contain unique entries.- >>> from metatensor import MetatensorError >>> block = metatensor.block_from_array(values) >>> keys = metatensor.Labels(["key", "extra"], np.array([[0, 0], [0, 1]])) >>> tensor = metatensor.TensorMap(keys=keys, blocks=[block.copy(), block.copy()]) >>> tensor TensorMap with 2 blocks keys: key extra 0 0 0 1 >>> try: ... metatensor.remove_dimension(tensor, axis="keys", name="extra") ... except MetatensorError as e: ... print(e) ... invalid parameter: can not have the same label value multiple time: [0] is already present at position 0 
- metatensor.rename_dimension(tensor: TensorMap, axis: str, old: str, new: str) TensorMap[source]#
- Rename a - metatensor.Labelsdimension name for a given axis.- Parameters:
- Raises:
- ValueError – if - axisis a not valid value
- Returns:
- a new - metatensor.TensorMapwith renamed labels dimension.
- Return type:
 - Examples#- >>> import numpy as np >>> import metatensor >>> values = np.array([[1, 2], [3, 4]]) >>> block = metatensor.block_from_array(values) >>> keys = metatensor.Labels(["foo"], np.array([[0]])) >>> tensor = metatensor.TensorMap(keys=keys, blocks=[block]) >>> tensor TensorMap with 1 blocks keys: foo 0 >>> metatensor.rename_dimension(tensor, axis="keys", old="foo", new="bar") TensorMap with 1 blocks keys: bar 0