block_from_array#
- metatensor.block_from_array(array) TensorBlock[source]#
- Creates a simple TensorBlock from an array. - The metadata in the resulting - TensorBlockis filled with ranges of integers. This function should be seen as a quick way of creating a- TensorBlockfrom arbitrary data. However, the metadata generated in this way has little meaning.- Parameters:
- array – An array with two or more dimensions. This can either be a - numpy.ndarrayor a- torch.Tensor.
- Returns:
- A - TensorBlockwhose values correspond to the provided- array. The metadata names are set to- "sample"for samples;- "component_1",- "component_2", … for components; and- propertyfor properties. The number of- componentlabels is adapted to the dimensionality of the input array. The metadata associated with each label is a range of integers going from 0 to the size of the corresponding axis. The returned- TensorBlockhas no gradients.
- Return type:
 - >>> import numpy as np >>> import metatensor >>> # Construct a simple 4D array: >>> array = np.linspace(0, 10, 42).reshape((7, 3, 1, 2)) >>> # Transform it into a TensorBlock: >>> tensor_block = metatensor.block_from_array(array) >>> print(tensor_block) TensorBlock samples (7): ['sample'] components (3, 1): ['component_1', 'component_2'] properties (2): ['property'] gradients: None >>> # The data inside the TensorBlock will correspond to the provided array: >>> print(np.all(array == tensor_block.values)) True