slice

metatensor.slice(tensor: TensorMap, axis: str, labels: Labels) TensorMap[source]

Slice a TensorMap along either the "samples" or "properties"` ``axis. labels is a Labels objects that specifies the samples/properties (respectively) names and indices that should be sliced, i.e. kept in the output TensorMap.

This function will return a TensorMap whose blocks are of equal or smaller dimensions (due to slicing) than those of the input. However, the returned TensorMap will be returned with the same number of blocks and the corresponding keys as the input. If any block upon slicing is reduced to nothing, i.e. in the case that it has none of the specified labels along the "samples" or "properties" axis, an empty block (i.e. a block with one of the dimension set to 0) will used for this key, and a warning will be emitted.

See the documentation for the slice_block() function to see how an individual TensorBlock is sliced.

Parameters:
  • tensor (TensorMap) – the input TensorMap to be sliced.

  • axis (str) – a str indicating the axis along which slicing should occur. Should be either “samples” or “properties”.

  • labels (Labels) – a Labels object containing the names and indices of the “samples” or “properties” to keep in each of the sliced TensorBlock of the output TensorMap.

Returns:

a TensorMap that corresponds to the sliced input tensor.

Return type:

TensorMap

metatensor.slice_block(block: TensorBlock, axis: str, labels: Labels) TensorBlock[source]

Slices an input TensorBlock along either the "samples" or "properties" axis. labels is a Labels objects that specify the sample/property names and indices that should be sliced, i.e. kept in the output TensorBlock.

If none of the entries in labels can be found in the block, the dimension corresponding to axis will be sliced to 0, and the returned block with have a shape of either (0, n_components, n_properties) or (n_samples, n_components, 0).

Parameters:
  • block (TensorBlock) – the input TensorBlock to be sliced.

  • axis (str) – a str indicating the axis along which slicing should occur. Should be either “samples” or “properties”.

  • labels (Labels) – a Labels object containing the names and indices of the “samples” or “properties” to keep in the sliced output TensorBlock.

Return new_block:

a TensorBlock that corresponds to the sliced input.

Return type:

TensorBlock