Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DCD XDR pickle tests #2911

Merged
merged 7 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package/MDAnalysis/lib/formats/libdcd.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ cdef class DCDFile:
return

current_frame = state[1]
self.seek(current_frame - 1)
self.current_frame = current_frame
self.seek(current_frame)

def tell(self):
"""
Expand Down Expand Up @@ -402,7 +401,7 @@ cdef class DCDFile:
seek the file to given frame (0-based)

"""
if frame >= self.n_frames:
if frame >= self.n_frames + 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to allow to seek to the end of the file? If so, add a comment, because under normal circumstances I expect a 0-based index for a sequence to fail when it hits len(sequence).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is possible to seek to the end of the file (after the last coordinate frame) with

dcd.seek(len(dcd))

then state it in the doc string. This is a fairly odd thing to allow so it should be documented.

raise EOFError('Trying to seek over max number of frames')
self.reached_eof = False

Expand Down
10 changes: 8 additions & 2 deletions package/MDAnalysis/lib/formats/libmdaxdr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,14 @@ cdef class _XDRFile:

# where was I
current_frame = state[1]
self.seek(current_frame - 1)
self.current_frame = current_frame
if current_frame == self.offsets.size:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch the decision in the if statement to make the more common situation come first

if current_frame < self.offsets.size:
     ...
elif current_frame == self.offsets.size:
    ...
else:
    #pragma: no cov
    raise RuntimeError("Invalid frame number {} > {} -- this should not happen.".format(current_frame, self.offsets.size)

and add defensive code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment under which circumstances we have to do funny things – namely when we need to serialize at the end of the trajectory.

# cannot seek to self.offsets.size (like in DCD file)
# because here seeking depends on the offsets list size.
# Instead, we seek to one frame ahead and read the next frame.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean "previous frame" instead of "frame ahead"?

self.seek(current_frame - 1)
_ = self.read()
else:
self.seek(current_frame)

def seek(self, frame):
"""Seek to Frame.
Expand Down
44 changes: 40 additions & 4 deletions testsuite/MDAnalysisTests/formats/test_libdcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,61 @@ def dcd():
yield dcd


def _assert_compare_readers(old_reader, new_reader):
# same as next(old_reader)
frame = old_reader.read()
# same as next(new_reader)
new_frame = new_reader.read()

assert old_reader.fname == new_reader.fname
assert old_reader.tell() == new_reader.tell()
assert_almost_equal(frame.xyz, new_frame.xyz)
assert_almost_equal(frame.unitcell, new_frame.unitcell)


def test_pickle(dcd):
mid = len(dcd) // 2
dcd.seek(mid)
new_dcd = pickle.loads(pickle.dumps(dcd))
_assert_compare_readers(dcd, new_dcd)


def test_pickle_last(dcd):
# This is the file state when DCDReader is in its last frame.
# (Issue #2878)

dcd.seek(len(dcd) - 1)
dump = pickle.dumps(dcd)
new_dcd = pickle.loads(dump)
_ = dcd.read()
new_dcd = pickle.loads(pickle.dumps(dcd))

assert dcd.fname == new_dcd.fname
assert dcd.tell() == new_dcd.tell()
with pytest.raises(StopIteration):
new_dcd.read()


def test_pickle_closed(dcd):
dcd.seek(len(dcd) - 1)
dcd.close()
dump = pickle.dumps(dcd)
new_dcd = pickle.loads(dump)
new_dcd = pickle.loads(pickle.dumps(dcd))

assert dcd.fname == new_dcd.fname
assert dcd.tell() != new_dcd.tell()


def test_pickle_after_read(dcd):
_ = dcd.read()
new_dcd = pickle.loads(pickle.dumps(dcd))
_assert_compare_readers(dcd, new_dcd)


def test_pickle_immediately(dcd):
new_dcd = pickle.loads(pickle.dumps(dcd))

assert dcd.fname == new_dcd.fname
assert dcd.tell() == new_dcd.tell()


@pytest.mark.parametrize("new_frame", (10, 42, 21))
def test_seek_normal(new_frame, dcd):
# frame seek within range is tested
Expand Down
41 changes: 35 additions & 6 deletions testsuite/MDAnalysisTests/formats/test_libmdaxdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import numpy as np

from numpy.testing import (assert_almost_equal, assert_array_almost_equal,
assert_array_equal)
assert_array_equal, assert_equal)

from MDAnalysis.lib.formats.libmdaxdr import TRRFile, XTCFile

Expand Down Expand Up @@ -127,24 +127,53 @@ def test_read_write_mode_file(self, xdr, tmpdir, fname):
with pytest.raises(IOError):
f.read()

@staticmethod
def _assert_compare_readers(old_reader, new_reader):
frame = old_reader.read()
new_frame = new_reader.read()

assert old_reader.fname == new_reader.fname
assert old_reader.tell() == new_reader.tell()

assert_equal(old_reader.offsets, new_reader.offsets)
assert_almost_equal(frame.x, new_frame.x)
assert_almost_equal(frame.box, new_frame.box)
assert frame.step == new_frame.step
assert_almost_equal(frame.time, new_frame.time)

def test_pickle(self, reader):
mid = len(reader) // 2
reader.seek(mid)
new_reader = pickle.loads(pickle.dumps(reader))
self._assert_compare_readers(reader, new_reader)

def test_pickle_last_frame(self, reader):
# This is the file state when XDRReader is in its last frame.
# (Issue #2878)
reader.seek(len(reader) - 1)
dump = pickle.dumps(reader)
new_reader = pickle.loads(dump)
_ = reader.read()
new_reader = pickle.loads(pickle.dumps(reader))

assert reader.fname == new_reader.fname
assert reader.tell() == new_reader.tell()
assert_almost_equal(reader.offsets, new_reader.offsets)
with pytest.raises(StopIteration):
new_reader.read()

def test_pickle_closed(self, reader):
reader.seek(len(reader) - 1)
reader.close()
dump = pickle.dumps(reader)
new_reader = pickle.loads(dump)
new_reader = pickle.loads(pickle.dumps(reader))

assert reader.fname == new_reader.fname
assert reader.tell() != new_reader.tell()

def test_pickle_immediately(self, reader):
new_reader = pickle.loads(pickle.dumps(reader))

assert reader.fname == new_reader.fname
assert reader.tell() == new_reader.tell()



@pytest.mark.parametrize("xdrfile, fname, offsets",
((XTCFile, XTC_multi_frame, XTC_OFFSETS),
Expand Down