TensorMap#
- class metatensor.torch.TensorMap(keys: Labels, blocks: List[TensorBlock])[source]#
- A TensorMap is the main user-facing class of this library, and can store any kind of data used in atomistic machine learning. - A tensor map contains a list of - TensorBlock, each one associated with a key. It also provides functions to access blocks associated with a key or part of a key, functions to merge blocks together and in general to manipulate this collection of blocks.- See also - The pure Python version of this class - metatensor.TensorMap, and the differences between TorchScript and Python API for metatensor.- Parameters:
- keys (Labels) – keys associated with each block 
- blocks (List[TensorBlock]) – set of blocks containing the actual data 
 
 - __getitem__(selection: int | Labels | LabelsEntry | Dict[str, int]) TensorBlock[source]#
- Get a single block with indexing syntax. This calls - TensorMap.block()directly.- Parameters:
- Return type:
 
 - copy() TensorMap[source]#
- get a deep copy of this - TensorMap, including all the data and metadata- Return type:
 
 - static load(path: str) TensorMap[source]#
- Load a serialized - TensorMapfrom the file at- path, this is equivalent to- metatensor.torch.load().- Warning - PyTorch can execute - staticfunctions (like this one) coming from a TorchScript extension, but fails when trying to save code calling this function with- torch.jit.save(), giving the following error:- Failed to downcast a Function to a GraphFunction - This issue is reported as PyTorch#115639. In the mean time, you should use - metatensor.torch.load()instead of this function to save your code to TorchScript.
 - static load_buffer(buffer: Tensor) TensorMap[source]#
- Load a serialized - TensorMapfrom an in-memory- buffer, this is equivalent to- metatensor.torch.load_buffer().- Warning - PyTorch can execute - staticfunctions (like this one) coming from a TorchScript extension, but fails when trying to save code calling this function with- torch.jit.save(), giving the following error:- Failed to downcast a Function to a GraphFunction - This issue is reported as PyTorch#115639. In the mean time, you should use - metatensor.torch.load_buffer()instead of this function to save your code to TorchScript.
 - save(path: str)[source]#
- Save this - TensorMapto a file, this is equivalent to- metatensor.torch.save().- Parameters:
- path (str) – Path of the file. If the file already exists, it will be overwritten 
 
 - save_buffer() Tensor[source]#
- Save this - TensorMapto an in-memory buffer, this is equivalent to- metatensor.torch.save_buffer().- Return type:
 
 - items() List[Tuple[LabelsEntry, TensorBlock]][source]#
- get an iterator over (key, block) pairs in this - TensorMap- Return type:
 
 - keys_to_samples(keys_to_move: str | List[str] | Tuple[str, ...] | Labels, sort_samples: bool = True) TensorMap[source]#
- Merge blocks along the samples axis, adding - keys_to_moveto the end of the samples labels dimensions.- This function will remove - keys_to_movefrom the keys, and find all blocks with the same remaining keys values. It will then merge these blocks along the samples direction (i.e. do a vertical concatenation), adding- keys_to_moveto the end of the samples labels dimensions. The values taken by- keys_to_movein the new samples labels will be the values of these dimensions in the merged blocks’ keys.- If - keys_to_moveis a set of- Labels, it must be empty (- len(keys_to_move) == 0), and only the- Labels.nameswill be used.- The order of the samples in the merged blocks is controlled by - sort_samples. If- sort_samplesis- True, samples are re-ordered to keep them lexicographically sorted. Otherwise they are kept in the order in which they appear in the blocks.- This function is only implemented when the blocks to merge have the same properties values. 
 - keys_to_properties(keys_to_move: str | List[str] | Tuple[str, ...] | Labels, sort_samples: bool = True) TensorMap[source]#
- Merge blocks along the properties direction, adding - keys_to_moveat the beginning of the properties labels dimensions.- This function will remove - keys_to_movefrom the keys, and find all blocks with the same remaining keys values. Then it will merge these blocks along the properties direction (i.e. do an horizontal concatenation).- If - keys_to_moveis given as strings, then the new property labels will only contain entries from the existing blocks. For example, merging a block with key- a=0and properties- p=1, 2with a block with key- a=2and properties- p=1, 3will produce a block with properties- a, p = (0, 1), (0, 2), (2, 1), (2, 3).- If - keys_to_moveis a set of- Labelsand it is empty (- len(keys_to_move) == 0), the- Labels.nameswill be used as if they where passed directly.- Finally, if - keys_to_moveis a non empty set of- Labels, the new properties labels will contains all of the entries of- keys_to_move(regardless of the values taken by- keys_to_move.namesin the merged blocks’ keys) followed by the existing properties labels. For example, using- a=2, 3in- keys_to_move, blocks with properties- p=1, 2will result in- a, p = (2, 1), (2, 2), (3, 1), (3, 2). If there is no values (no block/missing sample) for a given property in the merged block, then the value will be set to zero.- When using a non empty - Labelsfor- keys_to_move, the properties labels of all the merged blocks must take the same values.- The order of the samples in the merged blocks is controlled by - sort_samples. If- sort_samplesis- True, samples are re-ordered to keep them lexicographically sorted. Otherwise they are kept in the order in which they appear in the blocks.
 - components_to_properties(dimensions: str | List[str] | Tuple[str, ...]) TensorMap[source]#
- Move the given - dimensionsfrom the component labels to the property labels for each block.
 - blocks_matching(selection: Labels) List[int][source]#
- Get a (possibly empty) list of block indexes matching the - selection.- This function finds all keys in this - TensorMapwith the same values as- selectionfor the dimensions/names contained in the- selection; and return the corresponding indexes.- The - selectionshould contain a single entry.
 - block_by_id(index: int) TensorBlock[source]#
- Get the block at - indexin this- TensorMap.- Parameters:
- index (int) – index of the block to retrieve 
- Return type:
 
 - blocks_by_id(indices: List[int]) List[TensorBlock][source]#
- Get the blocks with the given - indicesin this- TensorMap.- Parameters:
- Return type:
 
 - block(selection: None | int | Labels | LabelsEntry | Dict[str, int] = None) TensorBlock[source]#
- Get the single block in this - TensorMapmatching the- selection.- When - selectionis an- int, this is equivalent to- TensorMap.block_by_id().- When - selectionis an- Labels, it should only contain a single entry, which will be used for the selection.- When - selectionis a- Dict[str, int], it is converted into a single single- LabelsEntry(the dict keys becoming the names and the dict values being joined together to form the- LabelsEntryvalues), which is then used for the selection.- When - selectionis a- LabelsEntry, this function finds the key in this- TensorMapwith the same values as- selectionfor the dimensions/names contained in the- selection; and return the corresponding indexes.
 - blocks(selection: None | List[int] | int | Labels | LabelsEntry | Dict[str, int] = None) List[TensorBlock][source]#
- Get the blocks in this - TensorMapmatching the- selection.- When - selectionis- None(the default), all blocks are returned.- When - selectionis an- int, this is equivalent to- TensorMap.block_by_id(); and for a- List[int]this is equivalent to- TensorMap.blocks_by_id().- When - selectionis an- Labels, it should only contain a single entry, which will be used for the selection.- When - selectionis a- Dict[str, int], it is converted into a single single- LabelsEntry(the dict keys becoming the names and the dict values being joined together to form the- LabelsEntryvalues), which is then used for the selection.- When - selectionis a- LabelsEntry, this function finds all keys in this- TensorMapwith the same values as- selectionfor the dimensions/names contained in the- selection; and return the corresponding blocks.
 - print(max_keys: int) str[source]#
- Print this - TensorMapto a string, including at most- max_keysin the output.
 - property dtype: dtype#
- get the dtype of all the arrays stored inside this - TensorMap- Warning - Due to limitations in TorchScript C++ extensions, the dtype is returned as an integer, which can not be compared with - torch.dtypeinstances. See- TensorBlock.dtype()for more information.
 - to(dtype: dtype | None = None, device: device | None = None, arrays: str | None = None) TensorMap[source]#
- Move all the data (keys and blocks) in this - TensorMapto the given- dtype,- deviceand- arraysbackend.- Parameters:
- dtype (dtype | None) – new dtype to use for all arrays. The dtype stays the same if this is set to - None.
- device (device | None) – new device to use for all arrays. The device stays the same if this is set to - None.
- arrays (str | None) – new backend to use for the arrays. This parameter is here for compatibility with the pure Python API, can only be set to - "torch"or- Noneand does nothing.
 
- Return type: