diff --git a/testsuite/MDAnalysisTests/coordinates/base.py b/testsuite/MDAnalysisTests/coordinates/base.py index 3856b37ff68..8205bb5938b 100644 --- a/testsuite/MDAnalysisTests/coordinates/base.py +++ b/testsuite/MDAnalysisTests/coordinates/base.py @@ -24,6 +24,7 @@ import numpy as np import pytest from six.moves import zip, range +from six.moves import cPickle as pickle from unittest import TestCase from numpy.testing import (assert_equal, assert_almost_equal, assert_array_almost_equal, assert_allclose) @@ -100,8 +101,8 @@ def test_coordinates(self): err_msg="wrong coordinates for A10:CA") def test_distances(self): - NTERM = self.universe.atoms.N[0] - CTERM = self.universe.atoms.C[-1] + NTERM = self.universe.select_atoms("name N")[0] + CTERM = self.universe.select_atoms("name C")[-1] d = mda.lib.mdamath.norm(NTERM.position - CTERM.position) assert_almost_equal(d, self.ref_distances['endtoend'], @@ -424,6 +425,16 @@ def test_add_another_transformations_raises_ValueError(self, transformed): with pytest.raises(ValueError): transformed.add_transformations(translate([2,2,2])) + @pytest.mark.parametrize("protocol", range(1, pickle.HIGHEST_PROTOCOL+1)) + def test_pickle(self, reader, protocol): + try: + s = pickle.dumps(reader, protocol=protocol) + except TypeError as err: + pytest.fail("Reader cannot be pickled with protocol={}\n{}".format( + protocol, err)) + assert len(s) > 1000 + + class MultiframeReaderTest(BaseReaderTest): def test_last_frame(self, ref, reader): ts = reader[-1] diff --git a/testsuite/MDAnalysisTests/coordinates/test_chainreader.py b/testsuite/MDAnalysisTests/coordinates/test_chainreader.py index d7661e1928e..4b82e0cdf8f 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_chainreader.py +++ b/testsuite/MDAnalysisTests/coordinates/test_chainreader.py @@ -21,6 +21,7 @@ # from __future__ import division, absolute_import from six.moves import zip +from six.moves import cPickle as pickle import numpy as np import os @@ -119,8 +120,8 @@ def test_write_dcd(self, universe, tmpdir): ts_new._pos, self.prec, err_msg="Coordinates disagree at frame {0:d}".format( - ts_orig.frame)) - + ts_orig.frame)) + def test_transform_iteration(self, universe, transformed): vector = np.float32([10,10,10]) # # Are the transformations applied and @@ -135,7 +136,7 @@ def test_transform_iteration(self, universe, transformed): frame = ts.frame ref = universe.trajectory[frame].positions + vector assert_almost_equal(ts.positions, ref, decimal = 6) - + def test_transform_slice(self, universe, transformed): vector = np.float32([10,10,10]) # what happens when we slice the trajectory? @@ -143,7 +144,7 @@ def test_transform_slice(self, universe, transformed): frame = ts.frame ref = universe.trajectory[frame].positions + vector assert_almost_equal(ts.positions, ref, decimal = 6) - + def test_transform_switch(self, universe, transformed): vector = np.float32([10,10,10]) # grab a frame: @@ -154,13 +155,22 @@ def test_transform_switch(self, universe, transformed): assert_almost_equal(transformed.trajectory[10].positions, newref, decimal = 6) # what happens when we comeback to the previous frame? assert_almost_equal(transformed.trajectory[2].positions, ref, decimal = 6) - + def test_transfrom_rewind(self, universe, transformed): vector = np.float32([10,10,10]) ref = universe.trajectory[0].positions + vector transformed.trajectory.rewind() assert_almost_equal(transformed.trajectory.ts.positions, ref, decimal = 6) + @pytest.mark.parametrize("protocol", range(1, pickle.HIGHEST_PROTOCOL+1)) + def test_pickle(self, universe, protocol): + try: + s = pickle.dumps(universe.trajectory, protocol=protocol) + except TypeError as err: + pytest.fail("ChainReader cannot be pickled with protocol={}\n{}".format( + protocol, err)) + assert len(s) > 1000 + class TestChainReaderCommonDt(object): common_dt = 100.0 prec = 3 diff --git a/testsuite/MDAnalysisTests/coordinates/test_crd.py b/testsuite/MDAnalysisTests/coordinates/test_crd.py index 305ea688967..ebfb96b5f09 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_crd.py +++ b/testsuite/MDAnalysisTests/coordinates/test_crd.py @@ -29,10 +29,61 @@ assert_equal, ) +import numpy as np + import MDAnalysis as mda +from MDAnalysis.coordinates.CRD import CRDReader, CRDWriter +from MDAnalysis.coordinates.base import Timestep -from MDAnalysisTests.datafiles import CRD +from MDAnalysisTests.datafiles import PSF, CRD, COORDINATES_ADK from MDAnalysisTests import make_Universe +from MDAnalysisTests.coordinates.base import ( + BaseReference, BaseReaderTest, BaseWriterTest, +) + + +class CRDReference(BaseReference): + def __init__(self): + super(CRDReference, self).__init__() + self.trajectory = CRD + self.topology = PSF + self.reader = CRDReader + self.writer = CRDWriter + self.ext = 'crd' + self.n_frames = 1 + self.totaltime = 0 + self.n_atoms = 3341 + self.container_format = True + self.dimensions = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) + self.volume = 0.0 + + self.first_frame = Timestep(self.n_atoms) + self.first_frame.positions = np.loadtxt(COORDINATES_ADK, delimiter=',', + dtype=np.float32) + self.first_frame.frame = 0 + self.first_frame.aux.lowf = self.aux_lowf_data[0] + self.first_frame.aux.highf = self.aux_highf_data[0] + + self.last_frame = self.first_frame.copy() + + +class TestCRDReader(BaseReaderTest): + @staticmethod + @pytest.fixture(scope='class') + def ref(): + return CRDReference() + + def test_time(self, ref, reader): + u = mda.Universe(ref.topology, ref.trajectory) + assert_equal(u.trajectory.time, 0.0, + "wrong time of the frame") + + def test_full_slice(self, ref, reader): + u = mda.Universe(ref.topology, ref.trajectory) + trj_iter = u.trajectory[:] + frames = [ts.frame for ts in trj_iter] + assert_equal(frames, np.arange(u.trajectory.n_frames)) + class TestCRDWriter(object): diff --git a/testsuite/MDAnalysisTests/data/adk_positions.csv.gz b/testsuite/MDAnalysisTests/data/adk_positions.csv.gz new file mode 100644 index 00000000000..38957a3ff2c Binary files /dev/null and b/testsuite/MDAnalysisTests/data/adk_positions.csv.gz differ diff --git a/testsuite/MDAnalysisTests/data/coordinates/test_topology.pdb b/testsuite/MDAnalysisTests/data/coordinates/test_topology.pdb index 197d7f27800..eaa1f862488 100644 --- a/testsuite/MDAnalysisTests/data/coordinates/test_topology.pdb +++ b/testsuite/MDAnalysisTests/data/coordinates/test_topology.pdb @@ -1,8 +1,8 @@ MODEL CRYST1 80.000 80.000 80.000 60.00 60.00 90.00 -ATOM 1 CA MET 1 00.000 00.000 00.000 1.00 84.71 -ATOM 2 CA ARG 2 01.000 01.000 01.000 1.00 68.59 -ATOM 3 CA ILE 3 02.000 02.000 02.000 1.00 48.30 -ATOM 4 CA LYS 4 03.000 03.000 03.000 1.00 49.38 -ATOM 5 CA LEU 5 04.000 04.000 04.000 1.00 42.10 +ATOM 1 CA MET 1 0.000 0.000 0.000 1.00 84.71 +ATOM 2 CA ARG 2 1.000 1.000 1.000 1.00 68.59 +ATOM 3 CA ILE 3 2.000 2.000 2.000 1.00 48.30 +ATOM 4 CA LYS 4 3.000 3.000 3.000 1.00 49.38 +ATOM 5 CA LEU 5 4.000 4.000 4.000 1.00 42.10 END diff --git a/testsuite/MDAnalysisTests/datafiles.py b/testsuite/MDAnalysisTests/datafiles.py index b616f8e4870..68c9f90442b 100644 --- a/testsuite/MDAnalysisTests/datafiles.py +++ b/testsuite/MDAnalysisTests/datafiles.py @@ -37,6 +37,8 @@ from __future__ import absolute_import __all__ = [ + "COORDINATES_ADK", # numpy/csv file with 3341 coordinates from CRD + # np.savetxt("data/adk_positions.csv.gz", u.atoms.positions, delimiter=",") "PSF", "DCD", "CRD", # CHARMM (AdK example, DIMS trajectory from JMB 2009 paper) "DCD2", # CHARMM (AdK example, DIMS trajectory from PLOS Comput Biol paper) "PSF_notop", "PSF_BAD", # Same as PSF but no bonds etc, malformed version of previous @@ -67,7 +69,6 @@ "PDB_chainidrepeat", # Issue #1107 "PDB", "GRO", "XTC", "TRR", "TPR", "GRO_velocity", # Gromacs (AdK) "GRO_incomplete_vels", - "COORDINATES_GRO_BZ2", "GRO_large", #atom number truncation at > 100,000 particles, Issue 550 "GRO_residwrap", # resids wrapping because of 5 digit field (Issue #728) "GRO_residwrap_0base", # corner case of #728 with resid=0 for first atom @@ -136,6 +137,7 @@ "COORDINATES_XYZ_BZ2", "COORDINATES_GRO", "COORDINATES_GRO_INCOMPLETE_VELOCITY", + "COORDINATES_GRO_BZ2", "Martini_membrane_gro", # for testing the leaflet finder "COORDINATES_XTC", "COORDINATES_TRR", @@ -185,6 +187,8 @@ COORDINATES_DCD = resource_filename(__name__, 'data/coordinates/test.dcd') COORDINATES_TOPOLOGY = resource_filename(__name__, 'data/coordinates/test_topology.pdb') +COORDINATES_ADK = resource_filename(__name__, 'data/adk_positions.csv.gz') + PSF = resource_filename(__name__, 'data/adk.psf') PSF_notop = resource_filename(__name__, 'data/adk_notop.psf') PSF_BAD = resource_filename(__name__, 'data/adk_notop_BAD.psf')