Auxiliary readers¶
Auxiliary reader for extended XYZ files.
Background¶
Extended XYZ (extxyz) files, as written by ASE and i-PI, store arbitrary data next to the coordinates:
per-atom properties as extra columns, declared by the
Propertieskey of the comment line, e.g.Properties=species:S:1:pos:R:3:forces:R:3,per-frame properties as
key=valuepairs on the comment line, e.g.energy=-1.2 polarization="0.1 0.2 0.3".
The coordinate reader MDAnalysis.coordinates.XYZ.XYZReader only reads the
positions. The XYZReader of this module reads everything else: every
per-atom property except species and pos and every per-frame key. It makes them
available as MDAnalysis auxiliary data, for example Born effective charges, forces,
energies, dipoles or polarizabilities.
The comment line is parsed with the rules of ASE’s
ase.io.extxyz.key_val_str_to_dict(): values become integers, floats or booleans
(or arrays of them) where possible and stay strings otherwise. virial and stress
are reshaped to 3x3 matrices in Fortran order, and Lattice to a 3x3 matrix whose
rows are the cell vectors (like atoms.cell in ASE). Per-atom properties keep the
type declared in Properties (R: float, I: int, L: bool, S: str). A
property with one column has shape (n_atoms,), one with n columns
(n_atoms, n).
A file without a Properties key is read as plain XYZ: like for the coordinate
reader, its comment line is a free-text title and ignored, and all columns after the
positions are read as one per-atom term data.
Standalone usage¶
All data is read when the reader is created and stored in XYZReader.data_dict,
a dictionary that maps each term to an array with the frames along the first axis:
from spectrumkit.auxiliary import XYZReader
aux = XYZReader("ipi.becx_0.extxyz", dt=0.0005)
aux.terms # ['becx', 'Lattice', 'ipi_comment', 'pbc']
aux.data_dict["becx"].shape # (n_frames, n_atoms, 3)
Adding data to trajectories¶
Like other auxiliary readers, XYZReader attaches its data to the trajectory
frames with matching time. The aux_spec dictionary maps the name used in ts.aux
to a term of the file:
import MDAnalysis as mda
import spectrumkit # registers the EXTXYZ and XYZ auxiliary formats
u = mda.Universe("ipi.positions_0.extxyz", format="XYZ", dt=0.002)
u.trajectory.add_auxiliary({"becx": "becx"}, "ipi.becx_0.extxyz", dt=0.0005)
u.trajectory.ts.aux.becx # (n_atoms, 3)
Without aux_spec all terms are attached under their own names:
u.trajectory.add_auxiliary(auxdata=XYZReader("forces.extxyz", dt=0.0005))
Note
Extended XYZ has no standard time key. Unless a time_selector names a per-frame
key holding the time in ps, the time of step i is initial_time + i * dt,
and dt defaults to 1 ps. Pass the actual dt of the file, here 0.5 fs for
i-PI output written every step. The coordinate reader needs its own dt as well.
Note
Atoms are matched by index: the file MUST list the atoms in the same order as the topology.
Note
Units are not converted. represent_ts_as="average" works only for numeric
terms.
- class spectrumkit.auxiliary.xyz.XYZReader(filename: str | Path, **kwargs)[source]¶
Bases:
AuxReaderAuxiliary reader for extended XYZ files.
All data of the file is read and stored on initialisation. Readers of the same unchanged file share one parse. See
spectrumkit.auxiliary.xyzfor the conventions.- Parameters:
filename (str or pathlib.Path) – Location of the extended XYZ file, optionally compressed (
.gz,.bz2).**kwargs – Other AuxReader options, e.g.
dt,initial_timeortime_selector.
- data_dict¶
Maps each term to a NumPy array with the frames along the first axis: per-atom terms have shape
(n_steps, n_atoms[, ncols]), per-frame terms(n_steps, ...).- Type:
- terms¶
Names of all terms in
data_dict: the per-atom terms in column order, followed by the per-frame terms in the order of the comment line.
- Raises:
ValueError – If the frames differ in the number of atoms or in
Properties, if a frame is truncated, if the atom columns don’t matchPropertiesor if a per-frame key changes its shape or is missing from frames while not being numeric.
See also
MDAnalysis.auxiliary.base.AuxReader,MDAnalysis.coordinates.base.ReaderBase.add_auxiliary()Note
The file is assumed to be of a size such that reading and storing the full contents is practical. The arrays of
data_dictare read-only, since readers of the same file share them.
- class spectrumkit.auxiliary.xyz.XYZStep(dt=1, initial_time=0, time_selector=None, data_selector=None, constant_dt=True, memory_limit=None)[source]¶
Bases:
AuxStepAuxStepfor extended XYZ files.The data of each step is a dictionary of all terms of the file; time and data of interest are selected by term name.
- Parameters:
See also
- spectrumkit.auxiliary.xyz.parse_comment_line(line: str) dict[source]¶
Parse the
key=valuepairs of an extended XYZ comment line.Follows
ase.io.extxyz.key_val_str_to_dict(): values may be delimited by quotes, curly or square brackets. Values are converted to int, float or bool, or to arrays of them, and stay strings otherwise. A key without value isTrue.Lattice,virialandstressare reshaped to 3x3 matrices in Fortran order. Escaped characters, malformed quoting and JSON values are not supported.- Parameters:
line (str) – Comment (second) line of an extended XYZ frame.
- Returns:
Parsed values by key, in the order of the line.
- Return type:
- Raises:
ValueError – If
Lattice,virialorstressis not a vector of 9 numbers.
- spectrumkit.auxiliary.xyz.parse_properties(spec: str) list[tuple[str, str, int]][source]¶
Parse the
Propertiesvalue of an extended XYZ comment line.- Parameters:
spec (str) – Colon-separated
name:type:ncolstriplets, e.g."species:S:1:pos:R:3".typeis one ofR(real),I(integer),S(string) andL(logical).- Returns:
(name, type, ncols)of every property, in column order.- Return type:
- Raises:
ValueError – If
specis not a sequence of valid triplets.