slice¶
- metatensor.slice(tensor: TensorMap, axis: str, selection: Labels) TensorMap[source]¶
- Slice a - TensorMapalong either the- "samples"or- "properties"` axis. The ``selectionspecifies which samples/properties (respectively) should be kept in the output- TensorMap.- This function will return a - TensorMapwhose blocks are of equal or smaller dimensions (due to slicing) than those of the input. However, the returned- TensorMapwill 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- selectionalong 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- TensorBlockis sliced.- Parameters:
- axis (str) – a - strindicating the axis along which slicing should occur. Should be either “samples” or “properties”.
- selection (Labels) – a - Labelsobject containing a selection for the- "samples"or- "properties"to keep in the sliced- TensorMap, or an array or- List[int]indicating the raw indices that should be kept. When using- Labelsselection, only a subset of the corresponding dimension names can be specified, and any entry with matching values will be selected.
 
- Returns:
- a - TensorMapthat corresponds to the sliced input tensor.
- Return type:
 
- metatensor.slice_block(block: TensorBlock, axis: str, selection: List[int] | ndarray | Labels) TensorBlock[source]¶
- Slices a - TensorBlockalong either the- "samples"or- "properties"axis. The- selectionspecifies which samples/properties (respectively) should be kept in the output- TensorMap.- If none of the entries in - selectioncan be found in the- block, the dimension corresponding to- axiswill 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 - TensorBlockto be sliced.
- axis (str) – a - strindicating the axis along which slicing should occur. Should be either “samples” or “properties”.
- selection (List[int] | ndarray | Labels) – a - Labelsobject containing a selection for the- "samples"or- "properties"to keep in the sliced- TensorBlock, or an array or- List[int]indicating the raw indices that should be kept. When using- Labelsselection, only a subset of the corresponding dimension names can be specified, and any entry with matching values will be selected.
 
- Return new_block:
- a - TensorBlockthat corresponds to the sliced input.
- Return type: