Coverage for local_installation/dynasor/trajectory/trajectory.py: 94%
158 statements
« prev ^ index » next coverage.py v7.3.2, created at 2024-12-21 12:02 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2024-12-21 12:02 +0000
1__all__ = ['Trajectory', 'WindowIterator']
3import numpy as np
5from collections import deque
6from itertools import islice, chain
7from os.path import isfile
8from typing import Callable, Dict, Union, List
10from dynasor.trajectory.atomic_indices import parse_gromacs_index_file
11from dynasor.trajectory.ase_trajectory_reader import ASETrajectoryReader
12from dynasor.trajectory.extxyz_trajectory_reader import ExtxyzTrajectoryReader
13from dynasor.trajectory.lammps_trajectory_reader import LammpsTrajectoryReader
14from dynasor.trajectory.mdanalysis_trajectory_reader import MDAnalysisTrajectoryReader
15from dynasor.trajectory.trajectory_frame import TrajectoryFrame
16from dynasor.logging_tools import logger
19class Trajectory:
20 """Instances of this class hold trajectories in a format suitable for
21 the computation of correlation functions. They behave as
22 iterators, where each step returns the next frame as a
23 :class:`TrajectoryFrame` object. The latter hold information
24 regarding atomic positions, types, and velocities.
26 Parameters
27 ----------
28 filename
29 Name of input file
30 trajectory_format
31 Type of trajectory. Possible values are:
32 ``'lammps_internal'``, ``'extxyz'``, ``'ase'`` or one of the formats supported by
33 `MDAnalysis <https://www.mdanalysis.org/>`_ (except for ``'lammpsdump'``,
34 which can be called by specifying ``'lammps_mdanalysis'`` to avoid ambiguity)
35 atomic_indices
36 Specify which indices belong to which atom type. Can be
37 (1) a dictionary where the keys specicy species and the values are list of atomic indices,
38 (2) ``'read_from_trajectory'``, in which case the species are read from the trajectory or
39 (3) the path to an index file.
40 length_unit
41 Length unit of trajectory (``'Angstrom'``, ``'nm'``, ``'pm'``, ``'fm'``). Necessary for
42 correct conversion to internal dynasor units if the trajectory file does not contain unit
43 information.
44 time_unit
45 Time unit of trajectory (``'fs'``, ``'ps'``, ``'ns'``). Necessary for correct conversion to
46 internal dynasor units if the trajectory file does not contain unit information.
47 frame_start
48 First frame to read; must be larger or equal ``0``.
49 frame_stop
50 Last frame to read. By default (``None``) the entire trajectory is read.
51 frame_step
52 Read every :attr:`frame_step`-th step of the input trajectory.
53 By default (``1``) every frame is read. Must be larger than ``0``.
55 """
56 def __init__(
57 self,
58 filename: str,
59 trajectory_format: str,
60 atomic_indices: Union[str, Dict[str, List[int]]] = None,
61 length_unit: str = 'Angstrom',
62 time_unit: str = 'fs',
63 frame_start: int = 0,
64 frame_stop: int = None,
65 frame_step: int = 1
66 ):
68 if frame_start < 0:
69 raise ValueError('frame_start should be positive')
70 if frame_step < 0:
71 raise ValueError('frame_step should be positive')
73 self._frame_start = frame_start
74 self._frame_step = frame_step
75 self._frame_stop = frame_stop
77 # setup trajectory reader
78 if not isfile(filename):
79 raise IOError(f'File {filename} does not exist')
80 self._filename = filename
82 if trajectory_format == 'lammps_internal':
83 reader = LammpsTrajectoryReader
84 elif trajectory_format == 'extxyz':
85 reader = ExtxyzTrajectoryReader
86 elif trajectory_format == 'lammps_mdanalysis':
87 reader = MDAnalysisTrajectoryReader
88 trajectory_format = 'lammpsdump'
89 elif trajectory_format == 'ase': 89 ↛ 90line 89 didn't jump to line 90, because the condition on line 89 was never true
90 reader = ASETrajectoryReader
91 elif trajectory_format == 'lammps':
92 raise IOError('Ambiguous trajectory format, '
93 'did you mean lammps_internal or lammps_mdanalysis?')
94 else:
95 reader = MDAnalysisTrajectoryReader
97 logger.debug(f'Using trajectory reader: {reader.__name__}')
98 if reader == MDAnalysisTrajectoryReader:
99 self._reader_obj = reader(self._filename, trajectory_format,
100 length_unit=length_unit, time_unit=time_unit)
101 else:
102 self._reader_obj = reader(self._filename, length_unit=length_unit, time_unit=time_unit)
104 # Get two frames to set cell etc.
105 frame0 = next(self._reader_obj)
106 frame1 = next(self._reader_obj)
107 self._cell = frame0.cell
108 self._n_atoms = frame0.n_atoms
110 # Make sure cell is not changed during consecutive frames
111 if not np.allclose(frame0.cell, frame1.cell):
112 raise ValueError('The cell changes between the first and second frame. '
113 'The concept of q-points becomes muddy if the simulation cell is '
114 'changing, such as during NPT MD simulations, so trajectories where '
115 'the cell changes are not supported by dynasor.')
117 # setup iterator slice (reuse frame0 and frame1 via chain)
118 self.number_of_frames_read = 0
119 self.current_frame_index = 0
120 self._reader_iter = islice(chain([frame0, frame1], self._reader_obj),
121 self._frame_start, self._frame_stop, self._frame_step)
123 # setup atomic indices
124 if atomic_indices is None: # Default behaviour
125 atomic_indices = {'X': np.arange(0, self.n_atoms)}
126 elif isinstance(atomic_indices, str): # Str input
127 if atomic_indices == 'read_from_trajectory':
128 if frame0.atom_types is None:
129 raise ValueError('Could not read atomic indices from the trajectory.')
130 else:
131 uniques = np.unique(frame0.atom_types)
132 atomic_indices = {uniques[i]: (frame0.atom_types == uniques[i]).nonzero()[0]
133 for i in range(len(uniques))}
134 else:
135 atomic_indices = parse_gromacs_index_file(atomic_indices)
136 elif isinstance(atomic_indices, dict): # Dict input
137 pass
138 else:
139 raise ValueError('Could not understand atomic_indices.')
140 self._atomic_indices = atomic_indices
142 # sanity checks for atomic_indices
143 for key, indices in self._atomic_indices.items():
144 if np.max(indices) > self.n_atoms:
145 raise ValueError('maximum index in atomic_indices exceeds number of atoms')
146 if np.min(indices) < 0:
147 raise ValueError('minimum index in atomic_indices is negative')
148 if '_' in key:
149 # Since '_' is what we use to distinguish atom types in the results, e.g. Sqw_Cs_Pb
150 raise ValueError('The char "_" is not allowed in atomic_indices.')
152 # log info on trajectory and atom types etc
153 logger.info(f'Trajectory file: {self.filename}')
154 logger.info(f'Total number of particles: {self.n_atoms}')
155 logger.info(f'Number of atom types: {len(self.atom_types)}')
156 for atom_type, indices in self._atomic_indices.items():
157 logger.info(f'Number of atoms of type {atom_type}: {len(indices)}')
158 logger.info(f'Simulation cell (in Angstrom):\n{str(self._cell)}')
160 def __iter__(self):
161 return self
163 def __next__(self):
164 frame = next(self._reader_iter)
165 new_frame = TrajectoryFrame(self.atomic_indices, frame.frame_index, frame.positions,
166 frame.velocities)
167 self.number_of_frames_read += 1
168 self.current_frame_index = frame.frame_index
169 return new_frame
171 def __str__(self) -> str:
172 s = ['Trajectory']
173 s += ['{:12} : {}'.format('filename', self.filename)]
174 s += ['{:12} : {}'.format('natoms', self.n_atoms)]
175 s += ['{:12} : {}'.format('frame_start', self._frame_start)]
176 s += ['{:12} : {}'.format('frame_stop', self._frame_stop)]
177 s += ['{:12} : {}'.format('frame_step', self.frame_step)]
178 s += ['{:12} : {}'.format('frame_index', self.current_frame_index)]
179 s += ['{:12} : [{}\n {}\n {}]'
180 .format('cell', self.cell[0], self.cell[1], self.cell[2])]
181 return '\n'.join(s)
183 def __repr__(self) -> str:
184 return str(self)
186 def _repr_html_(self) -> str:
187 s = [f'<h3>{self.__class__.__name__}</h3>']
188 s += ['<table border="1" class="dataframe">']
189 s += ['<thead><tr><th style="text-align: left;">Field</th><th>Value</th></tr></thead>']
190 s += ['<tbody>']
191 s += [f'<tr"><td style="text-align: left;">File name</td><td>{self.filename}</td></tr>']
192 s += [f'<tr><td style="text-align: left;">Number of atoms</td><td>{self.n_atoms}</td></tr>']
193 s += [f'<tr><td style="text-align: left;">Cell metric</td><td>{self.cell}</td></tr>']
194 s += [f'<tr><td style="text-align: left;">Frame step</td><td>{self.frame_step}</td></tr>']
195 s += [f'<tr><td style="text-align: left;">Atom types</td><td>{self.atom_types}</td></tr>']
196 s += ['</tbody>']
197 s += ['</table>']
198 return '\n'.join(s)
200 @property
201 def cell(self):
202 """ Simulation cell """
203 return self._cell
205 @property
206 def n_atoms(self):
207 """ Number of atoms """
208 return self._n_atoms
210 @property
211 def filename(self):
212 """ The trajectory filename """
213 return self._filename
215 @property
216 def atomic_indices(self):
217 """ Return copy of index arrays """
218 atomic_indices = dict()
219 for name, inds in self._atomic_indices.items():
220 atomic_indices[name] = inds.copy()
221 return atomic_indices
223 @property
224 def atom_types(self) -> List[str]:
225 return sorted(self._atomic_indices.keys())
227 @property
228 def frame_step(self):
229 """ Frame to access, trajectory will return every :attr:`frame_step`-th snapshot """
230 return self._frame_step
233def consume(iterator, n):
234 """ Advance the iterator by :attr:`n` steps. If :attr:`n` is ``None``, consume entirely. """
235 # From the python.org
236 if n is None: 236 ↛ 237line 236 didn't jump to line 237, because the condition on line 236 was never true
237 deque(iterator, maxlen=0)
238 else:
239 next(islice(iterator, n, n), None)
242class WindowIterator:
243 """Sliding window iterator.
245 Returns consecutive windows (a window is represented as a list
246 of objects), created from an input iterator.
248 Parameters
249 ----------
250 itraj
251 Trajectory object
252 width
253 Length of window (``window_size`` + 1)
254 window_step
255 Distance between the start of two consecutive window frames
256 element_processor
257 Enables processing each non-discarded object; useful if ``window_step >
258 width`` and ``map_item`` is expensive (as compared to directly passing
259 ``map(fun, itraj)`` as ``itraj``); if ``window_step < width``, you could as
260 well directly pass ``map(fun, itraj)``.
261 """
262 def __init__(self,
263 itraj: Trajectory,
264 width: int,
265 window_step: int = 1,
266 element_processor: Callable = None):
268 self._raw_it = itraj
269 if element_processor:
270 self._it = map(element_processor, self._raw_it)
271 else:
272 self._it = self._raw_it
273 assert window_step >= 1
274 assert width >= 1
275 self.width = width
276 self.window_step = window_step
277 self._window = None
279 def __iter__(self):
280 return self
282 def __next__(self):
283 """ Returns next element in sequence. """
284 if self._window is None:
285 self._window = deque(islice(self._it, self.width), self.width)
286 else:
287 if self.window_step >= self.width:
288 self._window.clear()
289 consume(self._raw_it, self.window_step - self.width)
290 else:
291 for _ in range(min((self.window_step, len(self._window)))):
292 self._window.popleft()
293 for f in islice(self._it, min((self.window_step, self.width))):
294 self._window.append(f)
296 if len(self._window) == 0:
297 raise StopIteration
299 return list(self._window)