pyretis.inout package

The sub-package handles input and output for PyRETIS.

This package is intended for creating various forms of output from the PyRETIS program. It includes writers for simple text-based output and plotters for creating figures. Figures and the text results can be combined into reports, which are handled by the report module.

Package structure

Modules

__init__.py
Imports from the other modules.
common.py (pyretis.inout.common)
Common functions and variables for the input/output. These functions are mainly intended for internal use and are not imported here.
fileio.py (pyretis.inout.fileio)
A module which defines a generic file class for PyRETIS output files.
settings.py (pyretis.inout.settings)
A module which handles the reading/writing of settings.
restart.py (pyretis.inout.restart)
A module which handles restart reading/writing.

Sub-packages

analysisio (pyretis.inout.analysisio)
Handles the input and output needed for analysis.
formats (pyretis.inout.formats)
Handles the input and output of different data formats. This includes the configurations and the internal data formats.
plotting (pyretis.inout.plotting)
Handles the plotting needed by the analysis by defining plotting tools, methods and styles.
report (pyretis.inout.report)
Generate reports with results from simulations.
setup (pyretis.inout.setup)
Handles set-up of simulations etc. from user settings.

Important classes defined in this package

Important methods defined in this package

create_engine (create_engine())
Create an engine from input settings.
create_force_field (create_force_field())
Create a force field from input settings.
create_orderparameter (create_orderparameter())
Create an order parameter from input settings.
create_simulation (create_simulation())
Create a simulation from input settings.
create_system (create_system())
Create a system from input settings.
generate_report (generate_report())
A function to generate reports from analysis output(s).
parse_settings_file (parse_settings_file())
Method for parsing settings from a given input file.
write_settings_file (write_settings_file())
Method for writing settings from a simulation to a given file.
write_restart_file (write_restart_file())
Method for writing restart information.

Subpackages

pyretis.inout.archive module

Module defining the base classes for the PyRETIS output.

Important classes defined here

PathStorage (PathStorage)
A class for handling storage of external trajectories to an archive. In this case, it is simply a folder based structure.
PathStorageTar (PathStorageTar)
A class for handling storage of external trajectories to an archive format (tar in this case).
class pyretis.inout.archive.PathStorage[source]

Bases: pyretis.inout.common.OutputBase

A class for handling storage of external trajectories.

target

Determines the target for this output class. Here it will be a file archive (i.e. a directory based collection of files).

Type:string
archive_acc

Basename for the archive with accepted trajectories.

Type:string
archive_rej

Basename for the archive with rejected trajectories.

Type:string
archive_traj

Basename for a sub-folder containing the actual files for a trajectory.

Type:string
formatters

This dict contains the formatters for writing path data, with default filenames used for them.

Type:dict
out_dir_fmt

A format to use for creating directories within the archive. This one is applied to the step number for the output.

Type:string
__init__()[source]

Set up the storage.

Note that we here do not pass any formatters to the parent class. This is because this class is less flexible and only intended to do one thing - write path data for external trajectories.

__str__()[source]

Return basic info.

archive_acc = 'traj-acc'
archive_name_from_status(status)[source]

Return the name of the archive to use.

archive_rej = 'traj-rej'
archive_traj = 'traj'
formatter_info()[source]

Return info about the formatters.

formatters = {'energy': {'file': 'energy.txt', 'fmt': <pyretis.inout.formats.energy.EnergyPathFormatter object>}, 'order': {'file': 'order.txt', 'fmt': <pyretis.inout.formats.order.OrderPathFormatter object>}, 'traj': {'file': 'traj.txt', 'fmt': <pyretis.inout.formats.path.PathExtFormatter object>}}
out_dir_fmt = '{}'
output(step, data)[source]

Format the path data and store the path.

Parameters:
  • step (integer) – The current simulation step.
  • data (list) – Here, data[0] is assumed to be an object like Path, data[1] a string containing the status of this path and data[2] the path ensemble for which the path was generated.
Returns:

files (list of tuples of strings) – The files added to the archive.

output_path_files(step, data, target_dir)[source]

Write the output files for energy, path and order parameter.

Parameters:
  • step (integer) – The current simulation step.
  • data (list) – Here, data[0] is assumed to be an object like Path and data[1]` a string containing the status of this path.
  • target_dir (string) – The path to where we archive the files.
Returns:

files (list of tuple of strings) – These are the files created. The tuple contains the files as a full path and a relative path (to the given target directory). The form is files[i] = (full_path[i], relative_path[i]). The relative path is useful for organizing internally in archives.

target = 'file-archive'
write(towrite, end='\n')[source]

We do not need the write method for this object.

class pyretis.inout.archive.PathStorageTar[source]

Bases: pyretis.inout.archive.PathStorage

A class for handling storage of external trajectories.

Here, we collect the external trajectories into tar archives.

__str__()[source]

Return basic info.

static _add_to_archive(archive_file, files)[source]

Add some files to an archive file.

Parameters:
  • archive_file (string) – The path to the archive file to create.
  • files (list of tuples) – The path(s) to the file(s) to add with names to create internally in the archive. That is, the form is: file[i] = (source[i], target[i]) where target[i] will be used as the archive name for file source[i].
Returns:

add (boolean) – True if we added files to the archive.

archive_acc = 'traj-acc.tar'
archive_rej = 'traj-rej.tar'
output(step, data)[source]

Format the path data and store the path.

Parameters:
  • step (integer) – The current simulation step.
  • data (list) – Here, data[0] is assumed to be an object like Path, data[1] a string containing the status of this path and data[2] the path ensemble for which the path was generated.
Returns:

files (list of tuples of strings) – The files added to the archive.

pyretis.inout.archive.add_to_tar_file(tar_file, files, file_mode='a')[source]

Open a tar file and add some files.

Parameters:
  • tar_file (string) – The path to the tar file to create.
  • files (list of tuples) – The path(s) to the file(s) to add with names to create internaly in the tar file. That is: file[i] = (source[i], target[i]) where target[i] will be used as the archname for source[i].
  • file_mode (string) – Determines how we try to open the tar file.
Returns:

out (boolean) – True if we managed to write to the file.

pyretis.inout.archive.generate_traj_names(path, target_dir)[source]

Generate new file names for moving copying paths.

Parameters:
  • path (object like PathBase) – This is the path object we are going to store.
  • target_dir (string) – The location where we are moving the path to.
Returns:

files (list of tuples of strings) – The files to add and the file names to use internally for storing. That is: file[i] = (source[i], target[i]) where target[i] is the internal storage name.

pyretis.inout.common module

This file contains common functions for the input/output.

Important classes defined here

OutputBase (OutputBase)
A base class for handling the output.

Important methods defined here

check_python_version (check_python_version())
A method that will give warnings when we use older and possibly unsupported Python versions.
create_backup (create_backup())
A function to handle the creation of backups of old files.
make_dirs (make_dirs())
Create directories (for path simulation).
simplify_ensemble_name (simplify_ensemble_name())
Simplify the name of ensembles for creating directories.
generate_file_name (generate_file_name())
Generate file name for an output task, from settings.
pyretis.inout.common.check_python_version()[source]

Give a warning about old python version(s).

pyretis.inout.common.create_backup(outputfile)[source]

Check if a file exist and create backup if requested.

This function will check if the given file name exists and if it does, it will move that file to a new file name such that the given one can be used without overwriting.

Parameters:outputfile (string) – This is the name of the file we wish to create.
Returns:out (string) – This string is None if no backup is made, otherwise, it will just say what file was moved (and to where).

Note

No warning is issued here. This is just in case the msg returned here will be part of some more elaborate message.

pyretis.inout.common.make_dirs(dirname)[source]

Create directories for path simulations.

This function will create a folder using a specified path. If the path already exists and if it’s a directory, we will do nothing. If the path exists and is a file we will raise an OSError exception here.

Parameters:dirname (string) – This is the directory to create.
Returns:out (string) – A string with some info on what this function did. Intended for output.
pyretis.inout.common.simplify_ensemble_name(ensemble, fmt='{:03d}')[source]

Simplify path names for file/directory names.

Here, we are basically translating ensemble names to more friendly names for directories and files that is:

  • [0^-] returns 000,
  • [0^+] returns 001,
  • [1^+] returns 002, etc.
Parameters:
  • ensemble (string) – This is the string to simplify.
  • fmt (string. optional) – This is a format to use for the directories.

pyretis.inout.fileio module

Module defining the base classes for the PyRETIS output.

Important classes defined here

FileIO (FileIO)
A generic class for handling input & output with files.

Important methods defined here

read_some_lines (read_some_lines())
Method to read lines from PyRETIS data files.
class pyretis.inout.fileio.FileIO(filename, file_mode, formatter, backup=True)[source]

Bases: pyretis.inout.common.OutputBase

A generic class for handling IO with files.

This class defines how PyRETIS stores and reads data. Formatting is handled by an object like OutputFormatter

filename

Name (e.g. path) to the file to read or write.

Type:string
file_mode

Specifies the mode in which the file is opened.

Type:string
backup

Determines the behavior if we want to write to a file that is already existing.

Type:boolean
fileh

The file handle we are interacting with.

Type:object like io.IOBase
last_flush

The previous time for flushing to the file.

Type:object like datetime.datetime
FILE_FLUSH

The interval for flushing to the file. That is, we will flush if the time since the last flush is larger than this value. Note that this is only checked in relation to writing.

Type:integer
FILE_FLUSH = 20
__del__()[source]

Close the file in case the object is deleted.

__enter__()[source]

Context manager for opening the file.

__exit__(*args)[source]

Context manager for closing the file.

__init__(filename, file_mode, formatter, backup=True)[source]

Set up the file object.

Parameters:
  • filename (string) – The path to the file to open or read.
  • file_mode (string) – Specifies the mode for opening the file.
  • formatter (object like py:class:.OutputFormatter) – The object responsible for formatting output.
  • backup (boolean, optional) – Defines how we handle cases where we write to a file which is already existing.
__iter__()[source]

Make it possible to iterate over lines in the file.

__next__()[source]

Let the file object handle the iteration.

__str__()[source]

Return basic info.

close()[source]

Close the file.

flush()[source]

Flush file buffers to file.

load()[source]

Read blocks or lines from the file.

open()[source]

Open a file for reading or writing.

open_file_read()[source]

Open a file for reading.

open_file_write()[source]

Open a file for writing.

In this method, we also handle the possible backup settings.

output(step, data)[source]

Open file before first write.

target = 'file'
write(towrite, end='\n')[source]

Write a string to the file.

Parameters:
  • towrite (string) – The string to output to the file.
  • end (string, optional) – Appended to towrite when writing, can be used to print a new line after the input towrite.
Returns:

status (boolean) – True if we managed to write, False otherwise.

pyretis.inout.fileio.read_some_lines(filename, line_parser, block_label='#')[source]

Open a file and try to read as many lines as possible.

This method will read a file using the given line_parser. If the given line_parser fails at a line in the file, read_some_lines will stop here. Further, this method will read data in blocks and yield a block when a new block is found. A special string (block_label) is assumed to identify the start of blocks.

Parameters:
  • filename (string) – This is the name/path of the file to open and read.
  • line_parser (function, optional) – This is a function which knows how to translate a given line to a desired internal format. If not given, a simple float will be used.
  • block_label (string, optional) – This string is used to identify blocks.
Yields:

data (list) – The data read from the file, arranged in dicts.

pyretis.inout.restart module

This module defines how we write and read restart files.

Important methods defined here

read_restart_file (read_restart_file())
A method for reading restart information from a file.
write_restart_file (write_restart_file())
A method for writing the restart file.
write_path_ensemble_restart (write_path_ensemble_restart())
A method for writing restart files for path ensembles.
pyretis.inout.restart.read_restart_file(filename)[source]

Read restart info for a simulation.

Parameters:filename (string) – The file we are going to read from.
pyretis.inout.restart.write_restart_file(filename, simulation)[source]

Write restart info for a simulation.

Parameters:
  • filename (string) – The file we are going to write to.
  • simulation (object like Simulation) – A simulation object we will get information from.
pyretis.inout.restart.write_path_ensemble_restart(path_ensemble)[source]

Write a restart file for a path ensemble.

Parameters:path_ensemble (object like PathEnsemble) – The path ensemble we are writing restart info for.

pyretis.inout.screen module

Module defining the base classes for the PyRETIS output.

Important classes defined here

ScreenOutput (FileIO)
A generic class for handling output to the screen.

Important methods defined here

print_to_screen (print_to_screen())
A method used for printing messages to screen.
class pyretis.inout.screen.ScreenOutput(formatter)[source]

Bases: pyretis.inout.common.OutputBase

A class for handling output to the screen.

target = 'screen'
write(towrite, end=None)[source]

Write a string to the file.

Parameters:
  • towrite (string) – The string to output to the file.
  • end (string, optional) – Override how the print statements ends.
Returns:

status (boolean) – True if we managed to write, False otherwise.

pyretis.inout.screen.print_to_screen(txt=None, level=None)[source]

Print output to standard out.

This method is included to ensure that output from PyRETIS to the screen is written out in a uniform way across the library and application(s).

Parameters:
  • txt (string, optional) – The text to write to the screen.
  • level (string, optional) – The level can be used to color the output.

pyretis.inout.settings module

This module handles parsing of input settings.

This module defines the file format for PyRETIS input files.

Important methods defined here

parse_settings_file (parse_settings_file())
Method for parsing settings from a given input file.
write_settings_file (write_settings_file())
Method for writing settings from a simulation to a given file.
pyretis.inout.settings.parse_settings_file(filename, add_default=True)[source]

Parse settings from a file name.

Here, we read the file line-by-line and check if the current line contains a keyword, if so, we parse that keyword.

Parameters:
  • filename (string) – The file to parse.
  • add_default (boolean) – If True, we will add default settings as well for keywords not found in the input.
Returns:

settings (dict) – A dictionary with settings for PyRETIS.

pyretis.inout.settings.write_settings_file(settings, outfile, backup=True)[source]

Write simulation settings to an output file.

This will write a dictionary to an output file in the PyRETIS input file format.

Parameters:
  • settings (dict) – The dictionary to write.
  • outfile (string) – The file to create.
  • backup (boolean, optional) – If True, we will backup existing files with the same file name as the provided file name.

Note

This will currently fail if objects have made it into the supplied settings.

pyretis.inout.simulationio module

Definition of a class for handling output related to simulations.

Important classes defined here

Task (Task)
Base class for tasks. This is used by SimulationTask and OutputTask.
OutputTask (OutputTask)
A class representing a simulation output task.

Important methods defined here

get_task_type (get_task_type())
Do additional handling for a path task.
get_file_mode (get_file_mode())
Determine if we should append or backup existing files.
task_from_settings (task_from_settings())
Create output task from simulation settings.

Important variables defined here

OUTPUT_TASKS (OUTPUT_TASKS)
A dictionary defining the different output tasks known to PyRETIS.
pyretis.inout.simulationio.OUTPUT_TASKS = {'cross': {'filename': 'cross.txt', 'formatter': <class 'pyretis.inout.formats.cross.CrossFormatter'>, 'result': ('cross',), 'target': 'file', 'when': 'cross-file'}, 'energy': {'filename': 'energy.txt', 'formatter': <class 'pyretis.inout.formats.energy.EnergyFormatter'>, 'result': ('thermo',), 'target': 'file', 'when': 'energy-file'}, 'order': {'filename': 'order.txt', 'formatter': <class 'pyretis.inout.formats.order.OrderFormatter'>, 'result': ('order',), 'target': 'file', 'when': 'order-file'}, 'path-energy': {'filename': 'energy.txt', 'formatter': <class 'pyretis.inout.formats.energy.EnergyPathFormatter'>, 'result': ('path', 'status'), 'target': 'file', 'when': 'energy-file'}, 'path-order': {'filename': 'order.txt', 'formatter': <class 'pyretis.inout.formats.order.OrderPathFormatter'>, 'result': ('path', 'status'), 'target': 'file', 'when': 'order-file'}, 'path-traj-ext': {'filename': 'traj.txt', 'result': ('path', 'status', 'pathensemble'), 'target': 'file-archive', 'when': 'trajectory-file', 'writer': <class 'pyretis.inout.archive.PathStorage'>}, 'path-traj-int': {'filename': 'traj.txt', 'formatter': <class 'pyretis.inout.formats.path.PathIntFormatter'>, 'result': ('path', 'status'), 'target': 'file', 'when': 'trajectory-file'}, 'pathensemble': {'filename': 'pathensemble.txt', 'formatter': <class 'pyretis.inout.formats.pathensemble.PathEnsembleFormatter'>, 'result': ('pathensemble',), 'target': 'file', 'when': 'pathensemble-file'}, 'pathensemble-retis-screen': {'formatter': <class 'pyretis.inout.formats.txt_table.RETISResultFormatter'>, 'result': ('pathensemble',), 'target': 'screen', 'when': 'screen'}, 'pathensemble-screen': {'formatter': <class 'pyretis.inout.formats.txt_table.PathTableFormatter'>, 'result': ('pathensemble',), 'target': 'screen', 'when': 'screen'}, 'thermo-file': {'filename': 'thermo.txt', 'formatter': <class 'pyretis.inout.formats.txt_table.ThermoTableFormatter'>, 'result': ('thermo',), 'target': 'file', 'when': 'energy-file'}, 'thermo-screen': {'formatter': <class 'pyretis.inout.formats.txt_table.ThermoTableFormatter'>, 'result': ('thermo',), 'target': 'screen', 'when': 'screen'}, 'traj-txt': {'filename': 'traj.txt', 'formatter': <class 'pyretis.inout.formats.snapshot.SnapshotFormatter'>, 'result': ('system',), 'target': 'file', 'when': 'trajectory-file'}, 'traj-xyz': {'filename': 'traj.xyz', 'formatter': <class 'pyretis.inout.formats.snapshot.SnapshotFormatter'>, 'result': ('system',), 'target': 'file', 'when': 'trajectory-file'}}

Define a set of known output tasks.

The output tasks are defined as dictionaries with the following keys:

  • targetstring

    “file” or “screen”, defines where the task writes to.

  • filenamestring

    A default file name for an output file if writing to a file.

  • resulttuple of strings

    Determines what item from the result dictionary we are outputting.

  • whenstring

    Determines what input setting from the “output” section is used to define the output frequency. Default values are defined by the output section, see: py:mod:pyretis.inout.settings.settings.

  • formatterobject like OutputFormatter

    Selects the formatter for the output.

  • writerobject like OutputBase

    Selects the writer for the output.

  • settingstuple of strings, optional

    A dict with additional settings which can be passed to the formatter if needed. These settings can, for instance, be defined in the output section of the input file.

The writer can be defined explicitly, or via the formatter. If a formatter is given, then the generic FileIO will be used. If no formatter is given, then the writer is assumed to be given.

class pyretis.inout.simulationio.OutputTask(name, result, writer, when)[source]

Bases: pyretis.inout.simulationio.Task

A base class for simulation output.

This class will handle an output task for a simulation. The output task consists of one object which is responsible for formatting the output data and one object which is responsible for writing that data, for instance to the screen or to a file.

target

This string identifies what kind of output we are dealing with. This will typically be either “screen” or “file”.

Type:string
name

This string identifies the task, it can, for instance, be used to reference the dictionary used to create the writer.

Type:string
result

This string defines the result we are going to output.

Type:tuple of strings
writer

This object will handle the actual outputting of the result.

Type:object like OutputBase
when

Determines if the task should be executed.

Type:dict
__init__(name, result, writer, when)[source]

Initialise the generic output task.

Parameters:
  • name (string) – This string identifies the task, it can, for instance, be used to reference the dictionary used to create the writer.
  • result (list of strings) – These strings define the results we are going to output.
  • writer (object like IOBase) – This object will handle formatting of the actual result and output to screen or to a file.
  • when (dict) – Determines when the output should be written. Example: {‘every’: 10} will be executed at every 10th step.
__str__()[source]

Print information about the output task.

output(simulation_result)[source]

Output given results from simulation steps.

This will output the task using the result found in the simulation_result which should be the dictionary returned from a simulation object (e.g. object like Simulation) after a step.

Parameters:simulation_result (dict) – This is the result from a simulation step.
Returns:out (boolean) – True if the writer wrote something, False otherwise.
task_dict()[source]

Return a dict with info about the task.

class pyretis.inout.simulationio.Task(when)[source]

Bases: object

Base representation of a “task”.

A task is just something that is supposed to be executed at a certain point. This class will just set up functionality that is common for output tasks and for simulation tasks.

when

Determines when the task should be executed.

Type:dict
__init__(when)[source]

Initialise the task.

Parameters:when (dict, optional) – Determines if the task should be executed.
execute_now(step)[source]

Determine if a task should be executed.

Parameters:step (dict of ints) – Keys are ‘step’ (current cycle number), ‘start’ cycle number at start ‘stepno’ the number of cycles we have performed so far.
Returns:out (boolean) – True of the task should be executed.
task_dict()[source]

Return basic info about the task.

when

Return the “when” property.

pyretis.inout.simulationio.get_file_mode(settings)[source]

Determine if we should append or backup existing files.

This method translates the backup settings into a file mode string. We assume here that the file is opened for writing.

Parameters:settings (dict) – The simulation settings.
Returns:file_mode (string) – A string representing the file mode to use.
pyretis.inout.simulationio.get_task_type(task, engine)[source]

Do additional handling for a path task.

The path task is special since we do very different things for external paths. The set-up required to do this is handled here.

Parameters:
  • task (dict) – Settings related to the specific task.
  • engine (object like EngineBase) – This object is used to determine if we need to do something special for external engines. If no engine is given, we do not do anything special.
Returns:

out (string) – The task type we are going to be creating for.

pyretis.inout.simulationio.task_from_settings(task, settings, directory, engine, progress=False)[source]

Create output task from simulation settings.

Parameters:
  • task (dict) – Settings for creating a task. This dict contains the type and name of the task to create. It can also contain overrides to the default settings in OUTPUT_TASKS.
  • settings (dict) – Settings for the simulation.
  • directory (string) – The directory to write output files to.
  • engine (object like EngineBase) – This object is used to determine if we need to do something special for external engines. If no engine is given, we do not do anything special.
  • progress (boolean, optional) – For some simulations, the user may select to display a progress bar. We will then just disable the other screen output.
Returns:

out (object like OutputTask) – An output task we can use in the simulation.