pyretis.inout.writers package

This sub-package handle writers for PyRETIS data.

Writers are basically formatting the data created from PyRETIS. The writers also have some additional functionality and can be used to load data written by PyRETIS as well. This is used when analysing the output from a PyRETIS simulation.

Package structure

Modules

cp2k.py (pyretis.inout.writers.cp2kio)
Module for handling input/output from CP2K.
fileio.py (pyretis.inout.writers.fileio)
Module defining a class for handling writing of files.
gromacsio.py (pyretis.inout.writers.gromacsio)
Module defining some io methods for use with GROMACS.
__init__.py
This file.
pathfile.py (pyretis.inout.writers.pathfile)
Module for handling path data and path-ensemble data.
tablewriter.py (pyretis.inout.writers.tablewriter)
Module defining generic methods for creating text tables.
txtinout.py (pyretis.inout.writers.txtinout)
Module defining some text based output.
writers.py (pyretis.inout.writers.writers)
Module for defining the base writer and some simple derived writers (for crossing data, energy and order parameter data).
xyzio.py (pyretis.inout.writers.xyzio)
Module for handling writing of trajectory data in XYZ format.

Important methods defined in this package

get_writer (get_writer())
Opens a file for reading given a file type and file name.
prepare_load (prepare_load())
Open up a file for reading, given file type and file name, and create the generator for it. This method can be set to fail if the file is not found.

Important classes defined in this package

CrossWriter (CrossWriter)
A writer of crossing data.
EnergyWriter (EnergyWriter)
A writer of energy data
EnergyPathWriter (EnergyPathWriter)
A class for writing out energy data for paths.
OrderWriter (OrderWriter)
A writer of order parameter data.
OrderPathWriter (OrderPathWriter)
A class for writing out order parameter data for paths.
TrajWriter (TrajWriter)
Generic class for writing trajectory output.
PathExtWriter (PathExtWriter)
A class for writing external paths to file.
PathIntWriter (PathIntWriter)
A class for writing internal paths to file.
PathEnsembleWriter (PathEnsembleWriter)
A writer of path ensemble data.
PathEnsembleFile (PathEnsembleFile)
A class which represent path ensembles in files. This class is intended for use in an analysis.
TxtTable (TxtTable)
A generic table writer.
ThermoTable (ThermoTable)
A specific table writer for energy output.
PathTable (PathTable)
A specific table writer for path results.
pyretis.inout.writers.get_writer(file_type, settings=None)[source]

Return a file object which can be used for loading files.

This is a convenience function to return an instance of a Writer or derived classes so that we are ready to read data from that file. Usage is intended to be in cases when we just want to open a file easily. The returned object can then be used to read the file using load(filename).

Parameters:
  • file_type (string) – The desired file type
  • settings (dict) – A dict of settings we might need to pass for to the writer. This can for instance be the units for a trajectory writer.
Returns:

out – An object which implements the load(filename) method.

Return type:

object like Writer

Examples

>>> from pyretis.inout.writers import get_writer
>>> crossfile = get_writer('cross')
>>> print(crossfile)
>>> for block in crossfile.load('cross.dat'):
>>>     print(len(block['data']))
pyretis.inout.writers.prepare_load(file_type, filename, required=True)[source]

Prepare to load a file of a given file type

Parameters:
  • file_type (string) – This selects the file type.
  • filename (string) – The path to the file we are to open.
  • required (boolen) – If True, we will fail if we can’t find the file.
Returns:

out – A generator which can be used to read the file.

Return type:

generator or None

pyretis.inout.writers.fileio module

Module defining the base classes for file writers.

This module defines a class that is useful for writing data to the disk. The typical usage in PyRETIS is to write the output from a Writer to a file using a FileIO object.

Important classes defined here

FileIO (FileIO)
A generic class for handling output to files.
class pyretis.inout.writers.fileio.FileIO(filename, oldfile='backup', header=None)[source]

Bases: object

A generic file writer class.

This class defines a simple object for writing to files. Formatting etc. is handled by objects like Writer from pyretis.inout.writers.writers. This class handle the creation/opening of the file with backup/overwriting etc.

filename

string – Name (path) of the file to open.

oldfile

string – Determines if we should backup, overwrite or append if a file exist with the given filename.

fileh

object like io.IOBase – The file handle we are using.

OLDFILE = ('append', 'overwrite', 'backup')
__del__()[source]

Close a file in case the object is deleted.

This method will just close the file in case the program crashes or exits in some other way.

__str__()[source]

Return basic info.

close()[source]

Close the file, in case that is explicitly needed.

force_flush()[source]

Attempt to force flushing of data to the file.

open_file(oldfile)[source]

Open the file for writing.

Parameters:oldfile (string) – This determines how we deal with already existing files. If they should be backed up, overwritten or appended to.
write(towrite, end='\n')[source]

Write a string to the file.

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

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

Return type:

boolean

pyretis.inout.writers.gromacsio module

Module for handling GROMACS input/output.

Important methods defined here

read_gromacs_file (read_gromacs_file())
A method for reading snapshots from a GROMACS GRO file.
read_gromacs_gro_file (read_gromacs_gro_file())
Read a single snapshot from a GROMACS GRO file.
write_gromacs_gro_file (write_gromacs_gro_file())
Write configuration in GROMACS GRO format.
read_gromos96_file (read_gromos96_file())
Read a single configuration GROMACS .g96 file.
write_gromos96_file (write_gromos96_file())
Write configuration in GROMACS g96 format.
read_xvg_file (read_xvg_file())
For reading .xvg files from GROMACS.
read_trr_header (read_trr_header())
Read a header from an open .trr
read_trr_data (read_trr_data())
Read data from an open .trr file.
skip_trr_data (skip_trr_data())
Skip reading data from an open .trr file and move on to the next header.
read_trr_file (read_trr_file())
Yield frames from a .trr file.
trr_frame_to_g96 (trr_frame_to_g96())
Dump a specific frame from a .trr file to a .g96 file.
pyretis.inout.writers.gromacsio.read_gromacs_file(filename)[source]

A method for reading GROMACS GRO files.

This method will read a GROMACS file and yield the different snapshots found in the file.

Parameters:filename (string) – The file to open.
Yields:out (dict) – This dict contains the snapshot.

Examples

>>> from pyretis.inout.writers.gromacsio import read_gromacs_file
>>> for snapshot in read_gromacs_file('traj.gro'):
...     print(snapshot['x'][0])
pyretis.inout.writers.gromacsio.read_gromacs_gro_file(filename)[source]

Read a single configuration GROMACS .gro file.

Parameters:filename (string) – The file to read.
Returns:
  • frame (dict) – This dict contains all the data read from the file.
  • xyz (numpy.array) – The positions.
  • vel (numpy.array) – The velocities.
pyretis.inout.writers.gromacsio.write_gromacs_gro_file(outfile, txt, xyz, vel)[source]

Write configuration in GROMACS GRO format.

Parameters:
  • filename (string) – The name of the file to create.
  • raw (dict of lists of strings) – This contains the raw data read from a .gro file.
  • xyz (numpy.array) – The positions to write.
  • vel (numpy.array) – The velocities to write.
pyretis.inout.writers.gromacsio.read_gromos96_file(filename)[source]

Read a single configuration GROMACS .g96 file.

Parameters:filename (string) – The file to read.
Returns:
  • rawdata (dict of list of strings) – This is the raw data read from the file grouped into sections. Note that this does not include the actual positions and velocities as these are returned separately.
  • xyz (numpy.array) – The positions.
  • vel (numpy.array) – The velocities.
  • box (numpy.array) – The simulation box.
pyretis.inout.writers.gromacsio.write_gromos96_file(filename, raw, xyz, vel)[source]

Write configuration in GROMACS g96 format.

Parameters:
  • filename (string) – The name of the file to create.
  • raw (dict of lists of strings) – This contains the raw data read from a .g96 file.
  • xyz (numpy.array) – The positions to write.
  • vel (numpy.array) – The velocities to write.
pyretis.inout.writers.gromacsio.read_xvg_file(filename)[source]

Return data in xvg file as numpy array.

pyretis.inout.writers.gromacsio.read_trr_header(fileh)[source]

Read a header from a .trr file.

Parameters:fileh (file object) – The file handle for the file we are reading.
Returns:header – The header read from the file.
Return type:dict
pyretis.inout.writers.gromacsio.read_trr_data(fileh, header)[source]

Read box, cooridnates etc. from a .trr file.

Parameters:
  • fileh (file object) – The file handle for the file we are reading.
  • header (dict) – The header read from the file.
Returns:

data – The data we read from the file. It may contain the following keys if the data was found in the frame:

  • box : the box matrix,
  • vir : the virial matrix,
  • pres : the pressure matrix,
  • x : the coordinates,
  • v : the velocities, and
  • f : the forces

Return type:

dict

pyretis.inout.writers.gromacsio.skip_trr_data(fileh, header)[source]

Skip coordinates/box data etc.

This method is used when we want to skip a data section in the .trr file. Rather than reading the data it will use the sized read in the header to skip ahead to the next frame.

Parameters:
  • fileh (file object) – The file handle for the file we are reading.
  • header (dict) – The header read from the .trr file.
pyretis.inout.writers.gromacsio.read_trr_file(filename, read_data=True)[source]

Yields frames in a trr file.

pyretis.inout.writers.gromacsio.trr_frame_to_g96(trr_file, index, outfile)[source]

Dump a .trr frame to a .g96 file.

Parameters:
  • trr_file (string) – The .trr file to read from.
  • index (integer) – The index for the frame to dump from trr_file.
  • outfile (string) – The g96 file to write.

pyretis.inout.writers.pathfile module

Methods and classes for input/output of path data.

This module defines classes for writing path ensemble data.

Important classes defined here

PathEnsembleWriter (PathEnsembleWriter)
Writing/reading of path ensemble data.
PathEnsembleFile (PathEnsembleFile)
Reading of path ensemble data. Mainly used for analysis.
class pyretis.inout.writers.pathfile.PathEnsembleWriter[source]

Bases: pyretis.inout.writers.writers.Writer

A class for writing path ensemble data.

This class handles writing/reading of path ensemble data to a file.

In the future, this should be made smarter, for instance could path data be read in portions, or the full path file could be read if it’s small enough to fit in the memory. A line-by-line analysis as it is right now might not be the most efficient way.

ensemble

str – This is a string representation of the path ensemble. Typically something like ‘0-‘, ‘0+’, ‘1’, ‘2’, …, ‘001’ and so on.

interfaces

list of ints – These are the interfaces specified with the values for the order parameters: [left, middle, right]

PATH_FMT = '{0:>10d} {1:>10d} {2:>10d} {3:1s} {4:1s} {5:1s} {6:>7d} {7:3s} {8:2s} {9:>16.9e} {10:>16.9e} {11:>7d} {12:>7d} {13:>16.9e} {14:>7d} {15:7d}'
generate_output(cycle, path_ensemble)[source]

Generate the output for the path ensemble writer

The latest path from the path ensemble will be written.

Parameters:
  • cycle (integer) – This is the current cycle number.
  • path_ensemble (object like PathEnsemble) – We will write the path defined by PathEnsemble.paths[-1]
Yields:

out (string) – The line(s) to be written

static line_parser(line)[source]

Convert a text line to simplified representation of a path.

This is used to parse a file with path data. It will not create a real pyretis.core.path.Path objects but only a dict with information about this path. This dict can be used to build up a path ensemble.

Parameters:line (string) – The line of text to convert.
Returns:out – This dict contains the path information.
Return type:dict
load(filename)[source]

Yield the different paths stored in the file.

The lines are read on-the-fly, converted and yielded one-by-one. Note that the file will be opened and closed here.

Parameters:filename (string) – The path/filename to open.
Yields:out (object like Path) – The current path in the file.
class pyretis.inout.writers.pathfile.PathEnsembleFile(filename, ensemble, interfaces, detect=None)[source]

Bases: pyretis.core.pathensemble.PathEnsemble, pyretis.inout.writers.pathfile.PathEnsembleWriter

A class for writing path ensemble data to files.

This class is intended to mimic the PathEnsemble class but using files. It overloads the get_paths() from the PathEnsemble so that the analysis can be run on this object in the same way that it is run on a PathEnsemble object. This object is included as a convenient way of interacting with a path ensemble file without having to load the entire file into memory. The PathensembleWriter does not include a reference to a file name but we do that in this class. We can then use the load() function to iterate over paths in the file.

filename

string – The file we are going to read.

get_paths()[source]

Load paths from the file.

pyretis.inout.writers.tablewriter module

A module defining a generic writer for tables.

The tables are defined with a set of variables (keys) and some rules for formatting. The table writer is similar to the other writer but can be made more generic and is more suited for creating output that will be displayed on screen.

Important classes defined here

TxtTable (TxtTable)
A class for generating table output.
PathTable (PathTable)
A class for table-like output from path simulations.
ThermoTable (ThermoTable)
A class for thermodynamic output, useful for output from MD-simulations.
class pyretis.inout.writers.tablewriter.TxtTable(variables, title, **kwargs)[source]

Bases: pyretis.inout.writers.writers.Writer

A class for generating table output.

This class handles writing/reading of output data to a table-like format.

variables

list of strings – These are the variables we will use in the table.

fmt

string – The format to apply to the columns.

row_fmt

list of strings – A list of strings used for formatting, used to construct fmt.

title

string – A title for the table.

__str__()[source]

Return a string with some info about the TxtTable.

generate_output(step, row)[source]

Generate output from a dictionary using the requested variables.

Parameters:
  • step (int) – This is the current step number or a cycle number in a simulation.
  • row (dict) – A dictionary that hopefully contains the variables we want to output.
Yields:

out (string) – A line with the formatted output.

class pyretis.inout.writers.tablewriter.PathTable[source]

Bases: pyretis.inout.writers.tablewriter.TxtTable

A special table output class for path ensembles.

This object will return a table of text with a header and with formatted rows for a path ensemble. The table rows will contain data from the PathEnsemble.nstats variable. This table is just meant as output to the screen during a path ensemble simulation.

Identical to the `TxtTable` object.
generate_output(step, path_ensemble)[source]

Generate the output for the path table.

Here we overload the TxtTable.generate_output() method in order to write path ensemble statistics to (presumably) the screen.

Parameters:
  • step (int) – This is the current step number or a cycle number in a simulation.
  • path_ensemble (object like PathEnsemble) – This is the path ensemble to output for.
Yields:

out (string) – This string is the formatted row.

class pyretis.inout.writers.tablewriter.ThermoTable[source]

Bases: pyretis.inout.writers.tablewriter.TxtTable

A special text table for energy output.

This object will return a table of text with a header and with formatted rows for energy output. Typical use is in MD simulation where we want to display energies at different steps in the simulations.

Identical to the `TxtTable` object.

pyretis.inout.writers.txtinout module

Methods and classes for text based output and input.

This file contains some methods and classes that handle output and input of ‘table-based’ output. Typically the data created here will be written to the screen during the simulation or as a simple column output.

Important methods defined here

txt_save_columns (txt_save_columns)
For writing a simple column-based output using numpy.
pyretis.inout.writers.txtinout.txt_save_columns(outputfile, header, variables, backup=False)[source]

Save variables to a text file using numpy.savetxt.

Note that the variables are assumed to be numpy.arrays of equal shape and that the output file may also be a compressed file in gzip format (this is selected by letting the output file name end with ‘.gz’).

Parameters:
  • outputfile (string) – This is the name of the output file to create.
  • header (string) – String that will be written at the beginning of the file.
  • variables (tuple of numpy.arrays) – These are the variables that will be save to the text file.
  • backup (boolean) – Determines if we should backup old files or not.

pyretis.inout.writers.writers module

Module for handling input and output of data.

The input and output of data are handled by writers who are responsible for turning raw data from PyRETIS into an output (in some form). Note that the writers are not responsible for actually writing the output to the screen or to files - this is done by an output task.

Important classes defined here

Writer (Writer)
A generic class for the writers.
CrossWriter (CrossWriter)
A class for writing crossing data from flux simulations.
EnergyWriter (EnergyWriter)
A class for writing energy data.
EnergyPathWriter (EnergyPathWriter)
A class for writing out energy data for paths.
OrderWriter (OrderWriter)
A class for writing out order parameter data.
OrderPathWriter (OrderPathWriter)
A class for writing out order parameter data for paths.
TrajWriter (TrajWriter)
Generic class for writing trajectory output.
PathExtWriter (PathExtWriter)
A class for writing external paths to file.
PathIntWriter (PathIntWriter)
A class for writing internal paths to file.

Important methods defined here

adjust_coordinates (adjust_coordinates())
Helper method to add dimensions when writing data in 1D or 2D to an output format that requires 3D data.
read_some_lines (read_some_lines())
Open a file and try to read as many lines as possible. This method is useful when we are reading possibly unfinished results.
class pyretis.inout.writers.writers.CrossWriter[source]

Bases: pyretis.inout.writers.writers.Writer

A class for writing crossing data from flux simulations.

This class handles writing/reading of crossing data. The format for the crossing file is three columns:

  1. First column is the step number (an integer).
  2. Second column is the interface number (an integer). These are numbered from 1 (_NOT_ from 0).
  3. The direction we are moving in - + for the positive direction or - for the negative direction. Internally this is converted to an integer (+1 or -1).
CROSS_FMT = '{:>10d} {:>4d} {:>3s}'
generate_output(step, *data)[source]

Generate output data to be written to a file or screen.

It will just write a space separated file without fancy formatting.

Parameters:
  • step (int) – This is the current step number. It is only used here for debugging and can possibly be removed. However, it’s useful to have here since this gives a common write interface for all writers.
  • data (list of tuples) – The tuples are crossing with interfaces (if any) on the form (timestep, interface, direction) where the direction is ‘-‘ or ‘+’.
Yields:

out (string) – The line(s) to be written.

See also

check_crossing in pyretis.core.path calculates the tuple cross which is used in this routine.

Note

We add 1 to the interface number here. This is for compatibility with the old Fortran code where the interfaces are numbered 1, 2, … rather than 0, 1, … .

static line_parser(line)[source]

Define a simple parser for reading the file.

It is used in the self.load() to parse the input file.

Parameters:line (string) – A line to parse.
Returns:out – out is (step number, interface number and direction).
Return type:tuple of ints

Note

The interface will be subtracted ‘1’ in the analysis. This is just for backwards compatibility with the old Fortran code.

class pyretis.inout.writers.writers.EnergyWriter[source]

Bases: pyretis.inout.writers.writers.Writer

A class for writing energy data from PyRETIS.

This class handles writing/reading of energy data. The data is written in 7 columns:

  1. Time, i.e. the step number.
  2. Potential energy.
  3. Kinetic energy.
  4. Total energy, should equal the sum of the two previous columns.
  5. Temperature.
ENERGY_FMT = ['{:>10d}', '{:>14.6f}', '{:>14.6f}', '{:>14.6f}', '{:>14.6f}', '{:>14.6f}']
ENERGY_TERMS = ('vpot', 'ekin', 'etot', 'temp')
HEADER = {'width': [10, 14], 'labels': ['Time', 'Potential', 'Kinetic', 'Total', 'Temperature']}
format_data(step, energy)[source]

Format a line of data.

Parameters:
  • step (int) – This is the current step number.
  • energy (dict) – This is the energy data stored as a dictionary.
Returns:

out – A formatted line of data.

Return type:

string

generate_output(step, *data)[source]

Yield formatted energy data.

load(filename)[source]

Load entire energy blocks into memory.

In the future, a more intelligent way of handling files like this may be in order, but for now the entire file is read as it’s very convenient for the subsequent analysis.

Parameters:filename (string) – The path/file name of the file we want to open.
Yields:data_dict (dict) – This is the energy data read from the file, stored in a dict. This is for convenience, so that each energy term can be accessed by data_dict[‘data’][key].

See also

read_some_lines.

class pyretis.inout.writers.writers.EnergyPathWriter[source]

Bases: pyretis.inout.writers.writers.EnergyWriter

A class for writing out energy data for paths.

ENERGY_TERMS = ('vpot', 'ekin')
HEADER = {'width': [10, 14], 'labels': ['Time', 'Potential', 'Kinetic']}
generate_output(step, *data)[source]

Format the order parameter data from a path.

Parameters:
  • step (int) – The cycle number we are creating output for.
  • path (object like PathBase) – The path we are creating output for.
  • status (string) – Status for the path (accepted/rejected).
Yields:

out (string) – The strings to be written.

class pyretis.inout.writers.writers.OrderWriter[source]

Bases: pyretis.inout.writers.writers.Writer

A class for writing out order parameter data.

This class handles writing/reading of order parameter data. The format for the order file is column-based and the columns are:

  1. Time.
  2. Main order parameter.
  3. Collective variable 1
  4. Collective variable 2
ORDER_FMT = ['{:>10d}', '{:>12.6f}']
format_data(step, orderdata)[source]

Format order parameter data.

Parameters:
  • step (int) – This is the current step number.
  • orderdata (list of floats) – This is the raw order parameter data.
Yields:

out (string) – The strings to be written.

generate_output(step, *data)[source]

Yield formatted order parameter data.

load(filename)[source]

Load entire order parameter blocks into memory.

In the future, a more intelligent way of handling files like this may be in order, but for now the entire file is read as it’s very convenient for the subsequent analysis. In case blocks are found in the file, they will be yielded, this is just to reduce the memory usage. The format is: time orderp0 orderp1` orderp2 … where the actual meaning of orderp1 orderp2 and the following order parameters are left to be defined by the user.

Parameters:filename (string) – The path/file name of the file we want to open.
Yields:data_dict (dict) – The data read from the order parameter file.

See also

read_some_lines.

class pyretis.inout.writers.writers.OrderPathWriter[source]

Bases: pyretis.inout.writers.writers.OrderWriter

A class for writing out order parameter data for paths.

generate_output(step, *data)[source]

Format the order parameter data from a path.

Parameters:
  • step (int) – The cycle number we are creating output for.
  • path (object like PathBase) – The path we are creating output for.
Yields:

out (string) – The strings to be written.

class pyretis.inout.writers.writers.TrajWriter(write_vel=True, fmt=None)[source]

Bases: pyretis.inout.writers.writers.Writer

Generic class for writing system shapshots.

write_vel

boolean – If True, we will also write velocities

fmt

string – Format to use for position output.

fmt_vel

string – Format to use for position and velocity output.

data_keys = ('atomname', 'x', 'y', 'z', 'vx', 'vy', 'vz')
format_snapshot(step, system)[source]

Format the given snapshot.

Parameters:
  • step (int) – The current simulation step.
  • system (object like System) – The system object with the positions to write
Returns:

out – The formatted snapshot

Return type:

list of strings

generate_output(step, *data)[source]

Generate the snapshot output.

load(filename)[source]

Read snapshots from the trajectory file.

Parameters:filename (string) – The path/filename to open.
Yields:out (dict) – This dict contains the snapshot.
class pyretis.inout.writers.writers.PathExtWriter[source]

Bases: pyretis.inout.writers.writers.Writer

A class for writing external trajectories.

print_header

boolean – Determines if we should print the header on the first step.

FMT = '{:>10} {:>20s} {:>10} {:>5}'
format_output(time, filename, index, vel)[source]

Just format the output.

generate_output(step, *data)[source]

Output path data.

static line_parser(line)[source]

A simple parser for reading path data.

Parameters:line (string) – The line to parse.
Returns:out – The columns of data.
Return type:list
pyretis.inout.writers.writers.read_some_lines(filename, line_parser=<function _simple_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) – This string is used to identify blocks.
Yields:

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

pyretis.inout.writers.writers.adjust_coordinate(coord)[source]

Method to adjust the dimensionality of coordinates.

This is a helper method for trajectory writers.

A lot of the different formats expects us to have 3 dimensional data. This method just adds dummy dimensions equal to zero.

Parameters:coord (numpy.array) – The real coordinates.
Returns:out – The “zero-padded” coordinates.
Return type:numpy.array

pyretis.inout.writers.xyzio module

Module for handling the output/input of trajectory data.

This module defines some classes for writing out and reading snapshots and trajectories in a XYZ-like format. By XYZ-like we here mean that we also include velocities as part of the file.

Important methods defined here

format_xyz_data (format_xyz_data())
A method for formatting position/velocity data in to a XYZ-like format. This can be used by external engines to convert to a standard format.
read_xyz_file (read_xyz_file())
A method for reading snapshots from a XYZ file.
write_xyz_file (write_xyz_file())
Just a convenience method for writing to a new file.
write_xyz_trajectory (write_xyz_trajectory())
A helper method to write xyz trajectories. This will also attempt to write box information to the xyz-header.
xyz_merge (xyz_merge())
A method to merge a forward and a backward xyz trajectory. This method is mainly used when creating a whole trajectory for visualization.
reverse_xyz_file (reverse_xyz_file())
Method to read an xyz-file in reverse, helps xyz_merge() merge trajectories.
txt_to_xyz (txt_to_xyz())
Method for converting/extracting paths from the internal trajectory format and writing them as a .xyz file.
pyretis.inout.writers.xyzio.format_xyz_data(pos, vel=None, names=None, header=None, fmt=None)[source]

Format XYZ data for outputting.

Parameters:
  • pos (numpy.array) – The positions to write.
  • vel (numpy.array, optional) – The velocities to write.
  • names (list, optional) – The atom names.
  • header (string, optional) – Header to use for writing the XYZ-file.
  • fmt (string) – A format to use for the writing
Yields:

out (string) – The formatted lines.

pyretis.inout.writers.xyzio.read_xyz_file(filename)[source]

A method for reading files in XYZ format.

This method will read a XYZ file and yield the different snapshots found in the file.

Parameters:filename (string) – The file to open.
Yields:out (dict) – This dict contains the snapshot.

Examples

>>> from pyretis.inout.writers.xyzio import read_xyz_file
>>> for snapshot in read_xyz_file('traj.xyz'):
...     print(snapshot['x'][0])

Note

The positions will NOT be converted to a specified set of units.

pyretis.inout.writers.xyzio.reverse_xyz_file(filename, outputfile)[source]

Reverse the order for frames in a given xyz-file.

Parameters:
  • filename (string) – The input .xyz file to open.
  • outputfile (string) – The .xyz file to write.

Note

This method will NOT reverse velocities.

pyretis.inout.writers.xyzio.write_xyz_file(filename, pos, vel=None, names=None, header=None)[source]

Create a new XYZ file with the given file name.

Parameters:
  • filename (string) – The file to create.
  • pos (numpy.array) – The positions to write.
  • vel (numpy.array, optional) – The velocities to write.
  • names (list, optional) – The atom names.
  • header (string, optional) – Header to use for writing the XYZ-file.

Note

We will here just overwrite if the file already exist.

Examples

>>> import numpy as np
>>> from pyretis.inout.writers.xyzio import write_xyz_file
>>> xyz = 10 * np.random.rand(10, 3)
>>> write_xyz_file('conf.xyz', xyz)
>>> vel = 10 * np.random.rand(10, 3)
>>> write_xyz_file('confv.xyz', xyz, vel)
pyretis.inout.writers.xyzio.write_xyz_trajectory(filename, pos, vel, names, box, step=None, append=True)[source]

Write XYZ snapshot to a trajectory.

This is indended as a lightweight alternative for just dumping snapshots to a trajectory file.

Parameters:
  • filename (string) – The file name to dump to.
  • pos (numpy.array) – The positions we are to write.
  • vel (numpy.array) – The velocities we are to write.
  • names (list of strings) – Atom names to write.
  • box (numpy.array) – The box dimensions/vectors
  • step (integer, optional) – If the step is given, then the step number is written to the header.
  • append (boolean) – Determines if we append or overwrite an existing file.

Note

We will here append to the file.

pyretis.inout.writers.xyzio.xyz_merge(backward, forward, merged)[source]

Merge forward and backward trajectories into one.

Parameters:
  • backward (string) – File path to the backward trajectory.
  • forward (string) – File path to the forward trajectory.
  • merged (string) – File path to the merged (output) trajectory. This will overwrite existing files with the same file path!

Note

The velocities in the backward trajectory will not be reversed, the purpose of this method is mainly to make whole trajectories for visualization purposes.