Serialization

metatensor.save(file: str | Path | BinaryIO, data: TensorMap | TensorBlock | Labels, use_numpy=False)[source]

Save the given data (one of TensorMap, TensorBlock, or Labels) to the given file.

TensorMap are serialized using numpy’s NPZ format, i.e. a ZIP file without compression (storage method is STORED), where each file is stored as a .npy array. See the C API documentation for more information on the format.

The recommended file extension when saving data is .mts, to prevent confusion with generic .npz files.

Parameters:
  • file (str | Path | BinaryIO) – where to save the data. This can be a string, pathlib.Path containing the path to the file to load, or a file-like object that should be opened in binary mode.

  • data (TensorMap | TensorBlock | Labels) – data to serialize and save

  • use_numpy – should we use numpy or the native serializer implementation? Numpy should be able to process more dtypes than the native implementation, which is limited to float64, but the native implementation is usually faster than going through numpy. This is ignored when saving Labels.

metatensor.io.save_buffer(data: TensorMap | TensorBlock | Labels, use_numpy=False) → memoryview[source]

Save the given data (one of TensorMap, TensorBlock, or Labels) to an in-memory buffer.

Parameters:
  • data (TensorMap | TensorBlock | Labels) – data to serialize and save

  • use_numpy – should we use numpy or the native serializer implementation?

Return type:

memoryview

Loading TensorMap

metatensor.load(file: str | Path | BinaryIO, use_numpy=False, *, array_backend: Literal['numpy', 'torch'] = 'numpy') → TensorMap[source]

Load a previously saved TensorMap from the given file.

TensorMap are serialized using numpy’s NPZ format, i.e. a ZIP file without compression (storage method is STORED), where each file is stored as a .npy array. See the C API documentation for more information on the format.

Parameters:
  • file (str | Path | BinaryIO) – file to load: this can be a string, a pathlib.Path containing the path to the file to load, or a file-like object that should be opened in binary mode.

  • use_numpy – should we use numpy or the native implementation? Numpy should be able to process more dtypes than the native implementation, which is limited to float64, but the native implementation is usually faster than going through numpy.

  • array_backend (Literal['numpy', 'torch']) – array backend to use for all data in the TensorMap. See Data allocated outside of Python for more information.

Return type:

TensorMap

metatensor.io.load_buffer(buffer: bytes | bytearray | memoryview, use_numpy=False, *, array_backend: Literal['numpy', 'torch'] = 'numpy') → TensorMap[source]

Load a previously saved TensorMap from an in-memory buffer.

Parameters:
  • buffer (bytes | bytearray | memoryview) – In-memory buffer containing a serialized TensorMap

  • use_numpy – should we use numpy or the native implementation?

  • array_backend (Literal['numpy', 'torch']) – array backend to use for all data in the TensorMap. See Data allocated outside of Python for more information.

Return type:

TensorMap

Loading TensorBlock

metatensor.load_block(file: str | Path | BinaryIO, use_numpy=False, *, array_backend: Literal['numpy', 'torch'] = 'numpy') → TensorBlock[source]

Load a previously saved TensorBlock from the given file.

Parameters:
  • file (str | Path | BinaryIO) – file to load: this can be a string, a pathlib.Path containing the path to the file to load, or a file-like object that should be opened in binary mode.

  • use_numpy – should we use numpy or the native implementation? Numpy should be able to process more dtypes than the native implementation, which is limited to float64, but the native implementation is usually faster than going through numpy.

  • array_backend (Literal['numpy', 'torch']) – array backend to use for all data in the TensorBlock. See Data allocated outside of Python for more information.

Return type:

TensorBlock

metatensor.io.load_block_buffer(buffer: bytes | bytearray | memoryview, use_numpy=False, *, array_backend: Literal['numpy', 'torch'] = 'numpy') → TensorBlock[source]

Load a previously saved TensorBlock from an in-memory buffer.

Parameters:
  • buffer (bytes | bytearray | memoryview) – In-memory buffer containing a serialized TensorBlock

  • use_numpy – should we use numpy or the native implementation?

  • array_backend (Literal['numpy', 'torch']) – array backend to use for all data in the TensorBlock. See Data allocated outside of Python for more information.

Return type:

TensorBlock

Loading Labels

metatensor.load_labels(file: str | Path | BinaryIO, *, array_backend: Literal['numpy', 'torch'] = 'numpy') → Labels[source]

Load previously saved Labels from the given file.

Parameters:
Return type:

Labels

metatensor.io.load_labels_buffer(buffer: bytes | bytearray | memoryview, *, array_backend: Literal['numpy', 'torch'] = 'numpy') → Labels[source]

Load previously saved Labels from an in-memory buffer.

Parameters:
Return type:

Labels

Loading with custom arrays

metatensor.io.CreateArrayCallback

Callback type for creating arrays when loading data. The arguments are a pointer to the shape, the number of elements in the shape, a DLDataType describing the data type of the array, and a pointer to the mts_array_t to be filled. The callback should return None. alias of Callable[[LP_c_ulong, c_ulong, DLDataType, LP_mts_array_t], None]

metatensor.io.load_custom_array(path: str | Path, create_array: CreateArrayCallback, *, array_backend: Literal['numpy', 'torch'] | None = None) → TensorMap[source]

Load a previously saved TensorMap from the given path using a custom array creation callback. This advanced functionality should not be needed by most users.

metatensor.io.create_numpy_array() and metatensor.io.create_torch_array() can be used as callbacks to load data into numpy and torch arrays respectively.

Parameters:
Return type:

TensorMap

metatensor.io.load_buffer_custom_array(buffer: bytes | bytearray | memoryview, create_array: CreateArrayCallback, *, array_backend: Literal['numpy', 'torch'] | None = None) → TensorMap[source]

Load a previously saved TensorMap from the given buffer using a custom array creation callback. This advanced functionality should not be needed by most users.

metatensor.io.create_numpy_array() and metatensor.io.create_torch_array() can be used as callbacks to load data into numpy and torch arrays respectively.

Parameters:
Return type:

TensorMap

metatensor.io.load_block_custom_array(path: str | Path, create_array: CreateArrayCallback, *, array_backend: Literal['numpy', 'torch'] | None = None) → TensorBlock[source]

Load a previously saved TensorBlock from the given path using a custom array creation callback. This advanced functionality should not be needed by most users.

metatensor.io.create_numpy_array() and metatensor.io.create_torch_array() can be used as callbacks to load data into numpy and torch arrays respectively.

Parameters:
Return type:

TensorBlock

metatensor.io.load_block_buffer_custom_array(buffer: bytes | bytearray | memoryview, create_array: CreateArrayCallback, *, array_backend: Literal['numpy', 'torch'] | None = None) → TensorBlock[source]

Load a previously saved TensorBlock from the given buffer using a custom array creation callback. This advanced functionality should not be needed by most users.

metatensor.io.create_numpy_array() and metatensor.io.create_torch_array() can be used as callbacks to load data into numpy and torch arrays respectively.

Parameters:
Return type:

TensorBlock

metatensor.io.load_labels_custom_array(path: str | Path, create_array: CreateArrayCallback, *, array_backend: Literal['numpy', 'torch'] | None = None) → Labels[source]

Load previously saved Labels from the given path using a custom array creation callback. This advanced functionality should not be needed by most users.

metatensor.io.create_numpy_array() and metatensor.io.create_torch_array() can be used as callbacks to load data into numpy and torch arrays respectively.

Parameters:
Return type:

Labels

metatensor.io.load_labels_buffer_custom_array(buffer: bytes | bytearray | memoryview, create_array: CreateArrayCallback, *, array_backend: Literal['numpy', 'torch'] | None = None) → Labels[source]

Load previously saved Labels from the given buffer using a custom array creation callback. This advanced functionality should not be needed by most users.

metatensor.io.create_numpy_array() and metatensor.io.create_torch_array() can be used as callbacks to load data into numpy and torch arrays respectively.

Parameters:
Return type:

Labels

metatensor.io.create_numpy_array()[source]

Callback function that can be used with metatensor.io.load_custom_array() to load data in numpy arrays.

metatensor.io.create_torch_array()[source]

Callback function that can be used with metatensor.io.load_custom_array() to load data in torch tensors. The resulting tensors are stored on CPU.