Serializers

This module contains the structures for serializing and deserializing trajectories.

class CSVSerializer

Handles trajectory files in JSON format.

static save(traj, file_path, overwrite=False, **kwargs)

Writes a trajectory to a file.

Parameters:
  • traj (Trajectory) – The trajectory to write to the file.

  • file_path (str | Path) – The path of the file to write.

  • overwrite (bool) – If True, overwrites the file if it already exists.

  • kwargs (Any) –

    Additional arguments to pass to the open function.

    Encoding is set to UTF-8 as default.

Return type:

None

static load(file_path, **kwargs)

Loads a trajectory from a file.

Parameters:
  • file_path (str | Path) – The path of the file to loaded.

  • kwargs (dict) –

    Additional keyword arguments.

    Encoding is set to UTF-8 as default.

Returns:

The trajectory loaded from the file.

Return type:

Trajectory

exception InvalidTrajectoryFileExtensionError(file_path, expected_extension=None)

Raised when the trajectory file extension is invalid.

Parameters:
  • file_path (str | Path)

  • expected_extension (str | None)

Return type:

None

class JSONSerializer

Handles trajectory files in JSON format.

static save(traj, file_path, overwrite=False, **kwargs)

Writes a trajectory to a file.

Parameters:
  • traj (Trajectory) – The trajectory to write to the file.

  • file_path (str | Path) – The path of the file to write.

  • overwrite (bool) – If True, overwrites the file if it already exists.

  • kwargs (Any) –

    Additional arguments to pass to the open function.

    Encoding is set to UTF-8 as default.

Return type:

None

static save_ensemble(trajs, file_path, overwrite=False, **kwargs)

Writes an ensemble to a file.

The main difference with the save method is that all the trajectories are saved in a single file.

Parameters:
  • trajs (list[Trajectory]) – The ensemble to write to the file.

  • file_path (str | Path) – The path of the file to write.

  • overwrite (bool) – If True, overwrites the file if it already exists.

  • kwargs (Any) –

    Additional arguments to pass to the open function.

    Encoding is set to UTF-8 as default.

Return type:

None

static load(file_path, **kwargs)

Loads a trajectory from a file.

Parameters:
  • file_path (str | Path) – The path of the file to loaded.

  • kwargs (dict) –

    Additional keyword arguments.

    Encoding is set to UTF-8 as default.

Returns:

The trajectory loaded from the file.

Return type:

Trajectory

static load_ensemble(file_path, **kwargs)

Loads an ensemble from a file.

The main difference with the load method is that all the trajectories are loaded from a single file.

Parameters:
  • file_path (str | Path) – The path of the file to loaded.

  • kwargs (dict) –

    Additional keyword arguments.

    Encoding is set to UTF-8 as default.

Returns:

The ensemble loaded from the file.

Return type:

list[Trajectory]

static to_json(traj)

Converts a trajectory to a JSON dictionary.

Parameters:

traj (Trajectory) – The trajectory to convert.

Returns:

The JSON dictionary.

Return type:

dict

static from_json(json_traj)

Converts a JSON dictionary to a trajectory.

Parameters:

json_traj (dict) – The JSON dictionary to convert.

Returns:

The trajectory.

Return type:

Trajectory

class Serializer

Abstract class for trajectory files.

abstract static save(traj, file_path, overwrite=False, **kwargs)

Saves a trajectory to a file.

Parameters:
  • traj (Trajectory) – The trajectory to be saved.

  • file_path (str | Path) – The path of the file to save.

  • overwrite (bool) – If True, overwrites the file if it already exists.

  • kwargs (Any) – Additional keyword arguments.

Return type:

None

abstract static load(file_path, **kwargs)

Loads a trajectory from a file.

Parameters:
  • file_path (str | Path) – The path of the file to loaded.

  • kwargs (dict) – Additional keyword arguments.

Returns:

The trajectory loaded from the file.

Return type:

Trajectory

static check_save_path(file_path, overwrite, extension)

Checks if the file can be saved.

Parameters:
  • file_path (str | Path) – The path of the file to save.

  • overwrite (bool) – If True, overwrites the file if it already exists.

  • extension (str | None) – If given, it checks that the file has the given extension.

Return type:

None

static check_load_path(file_path, extension)

Checks if the file can be loaded.

Parameters:
  • file_path (str | Path) – The path of the file to loaded.

  • extension (str | None) – If given, it checks that the file has the given extension.

Return type:

None