1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
|
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
#
# MDAnalysis --- https://www.mdanalysis.org
# Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors
# (see the file AUTHORS for the full list of names)
#
# Released under the Lesser GNU Public Licence, v2.1 or any higher version
#
# Please cite your use of MDAnalysis in published work:
#
# R. J. Gowers, M. Linke, J. Barnoud, T. J. E. Reddy, M. N. Melo, S. L. Seyler,
# D. L. Dotson, J. Domanski, S. Buchoux, I. M. Kenney, and O. Beckstein.
# MDAnalysis: A Python package for the rapid analysis of molecular dynamics
# simulations. In S. Benthall and S. Rostrup editors, Proceedings of the 15th
# Python in Science Conference, pages 102-109, Austin, TX, 2016. SciPy.
# doi: 10.25080/majora-629e541a-00e
#
# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein.
# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations.
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
"""\
=========================================================================
Reading trajectories from memory --- :mod:`MDAnalysis.coordinates.memory`
=========================================================================
:Author: Wouter Boomsma
:Year: 2016
:Copyright: Lesser GNU Public License v2.1+
:Maintainer: Wouter Boomsma <wb@di.ku.dk>, wouterboomsma on github
.. versionadded:: 0.16.0
The module contains a trajectory reader that operates on an array in
memory, rather than reading from file. This makes it possible to
operate on raw coordinates using existing MDAnalysis tools. In
addition, it allows the user to make changes to the coordinates in a
trajectory (e.g. through
:attr:`MDAnalysis.core.groups.AtomGroup.positions`) without having
to write the entire state to file.
How to use the :class:`MemoryReader`
====================================
The :class:`MemoryReader` can be used to either directly generate a
trajectory as a numpy array or by transferring an existing trajectory
to memory.
In-memory representation of arbitrary trajectories
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If sufficient memory is available to hold a whole trajectory in memory
then analysis can be sped up substantially by transferring the
trajectory to memory.
The most straightforward use of the :class:`MemoryReader` is to simply
use the ``in_memory=True`` flag for the
:class:`~MDAnalysis.core.universe.Universe` class, which
automatically transfers a trajectory to memory::
import MDAnalysis as mda
from MDAnalysisTests.datafiles import TPR, XTC
universe = mda.Universe(TPR, XTC, in_memory=True)
Of course, sufficient memory has to be available to hold the whole
trajectory.
Switching a trajectory to an in-memory representation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The decision to transfer the trajectory to memory can be made at any
time with the
:meth:`~MDAnalysis.core.universe.Universe.transfer_to_memory` method
of a :class:`~MDAnalysis.core.universe.Universe`::
universe = mda.Universe(TPR, XTC)
universe.transfer_to_memory()
This operation may take a while (with `verbose=True` a progress bar is
displayed) but then subsequent operations on the trajectory directly
operate on the in-memory array and will be very fast.
Constructing a Reader from a numpy array
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The :class:`MemoryReader` provides great flexibility because it
becomes possible to create a :class:`~MDAnalysis.core.universe.Universe` directly
from a numpy array.
A simple example consists of a new universe created from the array
extracted from a DCD
:meth:`~MDAnalysis.coordinates.DCD.DCDReader.timeseries`::
import MDAnalysis as mda
from MDAnalysisTests.datafiles import DCD, PSF
from MDAnalysis.coordinates.memory import MemoryReader
universe = mda.Universe(PSF, DCD)
coordinates = universe.trajectory.timeseries(universe.atoms)
universe2 = mda.Universe(PSF, coordinates, format=MemoryReader, order='afc')
.. _create-in-memory-trajectory-with-AnalysisFromFunction:
.. rubric:: Creating an in-memory trajectory with
:func:`~MDAnalysis.analysis.base.AnalysisFromFunction`
The :meth:`~MDAnalysis.coordinates.DCD.DCDReader.timeseries` is
currently only implemented for the
:class:`~MDAnalysis.coordinates.DCD.DCDReader`. However, the
:func:`MDAnalysis.analysis.base.AnalysisFromFunction` can provide the
same functionality for any supported trajectory format::
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import PDB, XTC
from MDAnalysis.coordinates.memory import MemoryReader
from MDAnalysis.analysis.base import AnalysisFromFunction
u = mda.Universe(PDB, XTC)
coordinates = AnalysisFromFunction(lambda ag: ag.positions.copy(),
u.atoms).run().results['timeseries']
u2 = mda.Universe(PDB, coordinates, format=MemoryReader)
.. _creating-in-memory-trajectory-label:
Creating an in-memory trajectory of a sub-system
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Creating a trajectory for just a selection of an existing trajectory
requires the transfer of the appropriate coordinates as well as
creation of a topology of the sub-system. For the latter one can use
the :func:`~MDAnalysis.core.universe.Merge` function, for the former
the :meth:`~MDAnalysis.core.universe.Universe.load_new` method of a
:class:`~MDAnalysis.core.universe.Universe` together with the
:class:`MemoryReader`. In the following, an in-memory trajectory of
only the protein is created::
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import PDB, XTC
from MDAnalysis.coordinates.memory import MemoryReader
from MDAnalysis.analysis.base import AnalysisFromFunction
u = mda.Universe(PDB, XTC)
protein = u.select_atoms("protein")
coordinates = AnalysisFromFunction(lambda ag: ag.positions.copy(),
protein).run().results['timeseries']
u2 = mda.Merge(protein) # create the protein-only Universe
u2.load_new(coordinates, format=MemoryReader)
The protein coordinates are extracted into ``coordinates`` and then
the in-memory trajectory is loaded from these coordinates. In
principle, this could have all be done in one line::
u2 = mda.Merge(protein).load_new(
AnalysisFromFunction(lambda ag: ag.positions.copy(),
protein).run().results['timeseries'],
format=MemoryReader)
The new :class:`~MDAnalysis.core.universe.Universe` ``u2`` can be used
to, for instance, write out a new trajectory or perform fast analysis
on the sub-system.
Classes
=======
.. autoclass:: MemoryReader
:members:
:inherited-members:
"""
import logging
import errno
import numpy as np
import warnings
import copy
from . import base
from .timestep import Timestep
# These methods all pass in an existing *view* onto a larger array
def _replace_positions_array(ts, new):
"""Replace the array of positions
Replaces the array of positions by another array.
Note
----
The behavior of :meth:`_replace_positions_array` is different from the
behavior of the :attr:`position` property that replaces the **content**
of the array. The :meth:`_replace_positions_array` method should only be
used to set the positions to a different frame in
:meth:`MemoryReader._read_next_timestep`; there, the memory reader sets
the positions to a view of the correct frame. Modifying the positions
for a given frame should be done with the :attr:`positions` attribute
that does not break the link between the array of positions in the time
step and the :attr:`MemoryReader.coordinate_array`.
.. versionadded:: 0.19.0
.. versionchanged:: 2.0.0
This function, and the _repalace helper functions for velocities,
forces, and dimensions, have been moved out of the now removed
custom timestep object for :class:`MemoryReader`.
"""
ts.has_positions = True
ts._pos = new
def _replace_velocities_array(ts, new):
ts.has_velocities = True
ts._velocities = new
def _replace_forces_array(ts, new):
ts.has_forces = True
ts._forces = new
def _replace_dimensions(ts, new):
ts._unitcell = new
class MemoryReader(base.ProtoReader):
"""
MemoryReader works with trajectories represented as numpy arrays.
A trajectory reader interface to a numpy array of the coordinates.
For compatibility with the timeseries interface, support is provided for
specifying the order of columns through the `order` keyword.
.. versionadded:: 0.16.0
.. versionchanged:: 1.0.0
Support for the deprecated `format` keyword for
:meth:`MemoryReader.timeseries` has now been removed.
"""
format = "MEMORY"
def __init__(
self,
coordinate_array,
order="fac",
dimensions=None,
dt=1,
filename=None,
velocities=None,
forces=None,
**kwargs,
):
"""
Parameters
----------
coordinate_array : numpy.ndarray
The underlying array of coordinates. The MemoryReader now
necessarily requires a np.ndarray
order : {"afc", "acf", "caf", "fac", "fca", "cfa"} (optional)
the order/shape of the return data array, corresponding
to (a)tom, (f)rame, (c)oordinates all six combinations
of 'a', 'f', 'c' are allowed ie "fac" - return array
where the shape is (frame, number of atoms,
coordinates).
dimensions: [A, B, C, alpha, beta, gamma] (optional)
unitcell dimensions (*A*, *B*, *C*, *alpha*, *beta*, *gamma*)
lengths *A*, *B*, *C* are in the MDAnalysis length unit (Å), and
angles are in degrees. An array of dimensions can be given,
which must then be shape (nframes, 6)
dt: float (optional)
The time difference between frames (ps). If :attr:`time`
is set, then `dt` will be ignored.
filename: string (optional)
The name of the file from which this instance is created. Set to ``None``
when created from an array
velocities : numpy.ndarray (optional)
Atom velocities. Must match shape of coordinate_array. Will share order
with coordinates.
forces : numpy.ndarray (optional)
Atom forces. Must match shape of coordinate_array Will share order
with coordinates
Raises
------
TypeError if the coordinate array passed is not a np.ndarray
Note
----
At the moment, only a fixed `dimension` is supported, i.e., the same
unit cell for all frames in `coordinate_array`. See issue `#1041`_.
.. _`#1041`: https://github.com/MDAnalysis/mdanalysis/issues/1041
.. versionchanged:: 0.19.0
The input to the MemoryReader now must be a np.ndarray
Added optional velocities and forces
.. versionchanged:: 2.2.0
Input kwargs are now stored under the :attr:`_kwargs` attribute,
and are passed on class creation in :meth:`copy`.
"""
super(MemoryReader, self).__init__()
self.filename = filename
self.stored_order = order
self._kwargs = kwargs
# See Issue #1685. The block below checks if the coordinate array
# passed is of shape (N, 3) and if it is, the coordiante array is
# reshaped to (1, N, 3)
try:
if coordinate_array.ndim == 2 and coordinate_array.shape[1] == 3:
coordinate_array = coordinate_array[np.newaxis, :, :]
except AttributeError:
errmsg = (
"The input has to be a numpy.ndarray that corresponds "
"to the layout specified by the 'order' keyword."
)
raise TypeError(errmsg) from None
self.set_array(coordinate_array, order)
self.n_frames = self.coordinate_array.shape[
self.stored_order.find("f")
]
self.n_atoms = self.coordinate_array.shape[self.stored_order.find("a")]
if velocities is not None:
try:
velocities = np.asarray(velocities, dtype=np.float32)
except ValueError:
errmsg = (
f"'velocities' must be array-like got "
f"{type(velocities)}"
)
raise TypeError(errmsg) from None
# if single frame, make into array of 1 frame
if velocities.ndim == 2:
velocities = velocities[np.newaxis, :, :]
if not velocities.shape == self.coordinate_array.shape:
raise ValueError(
"Velocities has wrong shape {} "
"to match coordinates {}"
"".format(velocities.shape, self.coordinate_array.shape)
)
self.velocity_array = velocities.astype(np.float32, copy=False)
else:
self.velocity_array = None
if forces is not None:
try:
forces = np.asarray(forces, dtype=np.float32)
except ValueError:
errmsg = f"'forces' must be array like got {type(forces)}"
raise TypeError(errmsg) from None
if forces.ndim == 2:
forces = forces[np.newaxis, :, :]
if not forces.shape == self.coordinate_array.shape:
raise ValueError(
"Forces has wrong shape {} "
"to match coordinates {}"
"".format(forces.shape, self.coordinate_array.shape)
)
self.force_array = forces.astype(np.float32, copy=False)
else:
self.force_array = None
provided_n_atoms = kwargs.pop("n_atoms", None)
if provided_n_atoms is not None and provided_n_atoms != self.n_atoms:
raise ValueError(
"The provided value for n_atoms ({}) "
"does not match the shape of the coordinate "
"array ({})".format(provided_n_atoms, self.n_atoms)
)
self.ts = self._Timestep(self.n_atoms, **kwargs)
self.ts.dt = dt
if dimensions is None:
self.dimensions_array = np.zeros(
(self.n_frames, 6), dtype=np.float32
)
else:
try:
dimensions = np.asarray(dimensions, dtype=np.float32)
except ValueError:
errmsg = (
f"'dimensions' must be array-like got "
f"{type(dimensions)}"
)
raise TypeError(errmsg) from None
if dimensions.shape == (6,):
# single box, tile this to trajectory length
# allows modifying the box of some frames
dimensions = np.tile(dimensions, (self.n_frames, 1))
elif dimensions.shape != (self.n_frames, 6):
raise ValueError(
"Provided dimensions array has shape {}. "
"This must be a array of shape (6,) or "
"(n_frames, 6)".format(dimensions.shape)
)
self.dimensions_array = dimensions
self.ts.frame = -1
self.ts.time = -1
self._read_next_timestep()
@staticmethod
def _format_hint(thing):
"""For internal use: Check if MemoryReader can operate on *thing*
.. versionadded:: 1.0.0
"""
return isinstance(thing, np.ndarray)
@staticmethod
def parse_n_atoms(filename, order="fac", **kwargs):
"""Deduce number of atoms in a given array of coordinates
Parameters
----------
filename : numpy.ndarray
data which will be used later in MemoryReader
order : {"afc", "acf", "caf", "fac", "fca", "cfa"} (optional)
the order/shape of the return data array, corresponding
to (a)tom, (f)rame, (c)oordinates all six combinations
of 'a', 'f', 'c' are allowed ie "fac" - return array
where the shape is (frame, number of atoms,
coordinates).
Returns
-------
n_atoms : int
number of atoms in system
"""
# assume filename is a numpy array
return filename.shape[order.find("a")]
def copy(self):
"""Return a copy of this Memory Reader"""
vels = (
self.velocity_array.copy()
if self.velocity_array is not None
else None
)
fors = (
self.force_array.copy() if self.force_array is not None else None
)
dims = self.dimensions_array.copy()
new = self.__class__(
self.coordinate_array.copy(),
order=self.stored_order,
dimensions=dims,
velocities=vels,
forces=fors,
dt=self.ts.dt,
filename=self.filename,
**self._kwargs,
)
new[self.ts.frame]
for auxname, auxread in self._auxs.items():
new.add_auxiliary(auxname, auxread.copy())
# since transformations are already applied to the whole trajectory
# simply copy the property
new.transformations = self.transformations
return new
def set_array(self, coordinate_array, order="fac"):
"""
Set underlying array in desired column order.
Parameters
----------
coordinate_array : :class:`~numpy.ndarray` object
The underlying array of coordinates
order : {"afc", "acf", "caf", "fac", "fca", "cfa"} (optional)
the order/shape of the return data array, corresponding
to (a)tom, (f)rame, (c)oordinates all six combinations
of 'a', 'f', 'c' are allowed ie "fac" - return array
where the shape is (frame, number of atoms,
coordinates).
"""
# Only make copy if not already in float32 format
self.coordinate_array = coordinate_array.astype("float32", copy=False)
self.stored_format = order
def get_array(self):
"""
Return underlying array.
"""
return self.coordinate_array
def _reopen(self):
"""Reset iteration to first frame"""
self.ts.frame = -1
self.ts.time = -1
def timeseries(
self, asel=None, atomgroup=None, start=0, stop=-1, step=1, order="afc"
):
"""Return a subset of coordinate data for an AtomGroup in desired
column order. If no selection is given, it will return a view of the
underlying array, while a copy is returned otherwise.
Parameters
---------
asel : AtomGroup (optional)
Atom selection. Defaults to ``None``, in which case the full set of
coordinate data is returned. Note that in this case, a view
of the underlying numpy array is returned, while a copy of the
data is returned whenever `asel` is different from ``None``.
.. deprecated:: 2.7.0
asel argument will be renamed to atomgroup in 3.0.0
atomgroup: AtomGroup (optional)
Same as `asel`, will replace `asel` in 3.0.0
start : int (optional)
the start trajectory frame
stop : int (optional)
the end trajectory frame
.. deprecated:: 2.4.0
Note that `stop` is currently *inclusive* but will be
changed in favour of being *exclusive* in version 3.0.
step : int (optional)
the number of trajectory frames to skip
order : {"afc", "acf", "caf", "fac", "fca", "cfa"} (optional)
the order/shape of the return data array, corresponding
to (a)tom, (f)rame, (c)oordinates all six combinations
of 'a', 'f', 'c' are allowed ie "fac" - return array
where the shape is (frame, number of atoms,
coordinates).
.. versionchanged:: 1.0.0
Deprecated `format` keyword has been removed. Use `order` instead.
.. versionchanged:: 2.4.0
ValueError now raised instead of NoDataError for empty input
AtomGroup
"""
if asel is not None:
warnings.warn(
"asel argument to timeseries will be renamed to"
"'atomgroup' in 3.0, see #3911",
category=DeprecationWarning,
)
if atomgroup:
raise ValueError(
"Cannot provide both asel and atomgroup kwargs"
)
atomgroup = asel
if stop != -1:
warnings.warn(
"MemoryReader.timeseries inclusive `stop` "
"indexing will be removed in 3.0 in favour of exclusive "
"indexing",
category=DeprecationWarning,
)
array = self.get_array()
if order == self.stored_order:
pass
elif order[0] == self.stored_order[0]:
array = np.swapaxes(array, 1, 2)
elif order[1] == self.stored_order[1]:
array = np.swapaxes(array, 0, 2)
elif order[2] == self.stored_order[2]:
array = np.swapaxes(array, 0, 1)
elif order[0] == self.stored_order[1]:
array = np.swapaxes(array, 1, 0)
array = np.swapaxes(array, 1, 2)
elif order[0] == self.stored_order[2]:
array = np.swapaxes(array, 2, 0)
array = np.swapaxes(array, 1, 2)
a_index = order.find("a")
f_index = order.find("f")
stop_index = stop + 1
if stop_index == 0:
stop_index = None
basic_slice = (
[slice(None)] * f_index
+ [slice(start, stop_index, step)]
+ [slice(None)] * (2 - f_index)
)
# Return a view if either:
# 1) atomgroup is None
# 2) atomgroup corresponds to the selection of all atoms.
array = array[tuple(basic_slice)]
if atomgroup is None or atomgroup is atomgroup.universe.atoms:
return array
else:
if len(atomgroup) == 0:
raise ValueError(
"Timeseries requires at least one atom " "to analyze"
)
# If selection is specified, return a copy
return array.take(asel.indices, a_index)
def _read_next_timestep(self, ts=None):
"""copy next frame into timestep"""
if self.ts.frame >= self.n_frames - 1:
raise IOError(errno.EIO, "trying to go over trajectory limit")
if ts is None:
ts = self.ts
ts.frame += 1
f_index = self.stored_order.find("f")
basic_slice = (
[slice(None)] * (f_index)
+ [self.ts.frame]
+ [slice(None)] * (2 - f_index)
)
_replace_positions_array(ts, self.coordinate_array[tuple(basic_slice)])
_replace_dimensions(ts, self.dimensions_array[self.ts.frame])
if self.velocity_array is not None:
_replace_velocities_array(
ts, self.velocity_array[tuple(basic_slice)]
)
if self.force_array is not None:
_replace_forces_array(ts, self.force_array[tuple(basic_slice)])
ts.time = self.ts.frame * self.dt
return ts
def _read_frame(self, i):
"""read frame i"""
# Frame number is incremented to zero by _read_next_timestep()
self.ts.frame = i - 1
return self._read_next_timestep()
def __repr__(self):
"""String representation"""
return "<{cls} with {nframes} frames of {natoms} atoms>" "".format(
cls=self.__class__.__name__,
nframes=self.n_frames,
natoms=self.n_atoms,
)
def add_transformations(self, *transformations):
"""Add all transformations to be applied to the trajectory.
This function take as list of transformations as an argument. These
transformations are functions that will be called by the Reader and given
a :class:`Timestep` object as argument, which will be transformed and returned
to the Reader.
The transformations can be part of the :mod:`~MDAnalysis.transformations`
module, or created by the user, and are stored as a list `transformations`.
This list can only be modified once, and further calls of this function will
raise an exception.
.. code-block:: python
u = MDAnalysis.Universe(topology, coordinates)
workflow = [some_transform, another_transform, this_transform]
u.trajectory.add_transformations(*workflow)
Parameters
----------
transform_list : list
list of all the transformations that will be applied to the coordinates
See Also
--------
:mod:`MDAnalysis.transformations`
"""
# Overrides :meth:`~MDAnalysis.coordinates.base.ProtoReader.add_transformations`
# to avoid unintended behaviour where the coordinates of each frame are transformed
# multiple times when iterating over the trajectory.
# In this method, the trajectory is modified all at once and once only.
super(MemoryReader, self).add_transformations(*transformations)
for i, ts in enumerate(self):
for transform in self.transformations:
ts = transform(ts)
def _apply_transformations(self, ts):
"""Applies the transformations to the timestep."""
# Overrides :meth:`~MDAnalysis.coordinates.base.ProtoReader.add_transformations`
# to avoid applying the same transformations multiple times on each frame
return ts
|