From 1453fb58bb0c49413c8226aeeb394f7835e0b178 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhuang Date: Wed, 24 Jun 2020 21:20:06 +0200 Subject: [PATCH 01/11] enable travis temporarily --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d526eb12d93..10ac800ebef 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,8 @@ branches: only: - master - develop - + - serialize_io + os: linux From 8a923fe75561c8b831dcbc3b61f835b026db99f5 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhuang Date: Wed, 24 Jun 2020 21:24:22 +0200 Subject: [PATCH 02/11] restore travis --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e4fa60eaef3..e14785752eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,6 @@ branches: only: - master - develop - - serialize_io os: linux From 953507faeee11a8ae01a384e5e64d203cfbcc045 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhuang Date: Fri, 3 Jul 2020 10:36:59 +0200 Subject: [PATCH 03/11] check for n_atoms for trz --- package/MDAnalysis/coordinates/TRZ.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/package/MDAnalysis/coordinates/TRZ.py b/package/MDAnalysis/coordinates/TRZ.py index 1fa9ac1c78a..4557777d436 100644 --- a/package/MDAnalysis/coordinates/TRZ.py +++ b/package/MDAnalysis/coordinates/TRZ.py @@ -136,12 +136,14 @@ class TRZReader(base.ReaderBase): *little-endian* byte order and are read as such. + .. versionchanged:: 0.11.0 Frames now 0-based instead of 1-based. Extra data (Temperature, Energies, Pressures, etc) now read into ts.data dictionary. Now passes a weakref of self to ts (ts._reader). - + .. versionchanged:: 2.0.0 + Now checks for the right `n_atoms` during initilization. """ format = "TRZ" @@ -159,6 +161,11 @@ def __init__(self, trzfilename, n_atoms=None, **kwargs): number of atoms in trajectory, must be taken from topology file! convert_units : bool (optional) converts units to MDAnalysis defaults + + Raises + ------ + AttributeError + If wrong `n_atoms`/topology file is provided. """ super(TRZReader, self).__init__(trzfilename, **kwargs) @@ -221,6 +228,14 @@ def __init__(self, trzfilename, n_atoms=None, **kwargs): self._read_next_timestep() + try: + self._get_dt() + except OSError: + raise AttributeError("`n_atoms` is incompatible " + "with provided trajectory file. " + "(Maybe `topology` is wrong?)") + + def _read_trz_header(self): """Reads the header of the trz trajectory""" self._headerdtype = np.dtype([ From a75d27e8795040b3c29e598e852482d0afd61350 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhuang Date: Fri, 3 Jul 2020 10:40:10 +0200 Subject: [PATCH 04/11] changelog --- package/CHANGELOG | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index ded76760b0d..58d4c878840 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -14,7 +14,7 @@ The rules for this file: ------------------------------------------------------------------------------ ??/??/?? richardjgowers, IAlibay, hmacdope, orbeckst, cbouy, lilyminium, - daveminh + daveminh, yuxuanzhuang * 2.0.0 @@ -24,6 +24,7 @@ Fixes * TOPParser no longer guesses elements when missing atomic number records (Issues #2449, #2651) * Testsuite does not any more matplotlib.use('agg') (#2191) + * TRZReader now checks `n_atoms` provided right during initilization. Enhancements * Added the RDKitParser which creates a `core.topology.Topology` object from From 84c0c33346471a5ea812556ca842bb65e8ebf9d2 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhuang Date: Fri, 3 Jul 2020 10:54:08 +0200 Subject: [PATCH 05/11] test --- package/MDAnalysis/coordinates/TRZ.py | 8 ++++---- testsuite/MDAnalysisTests/coordinates/test_trz.py | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/package/MDAnalysis/coordinates/TRZ.py b/package/MDAnalysis/coordinates/TRZ.py index 4557777d436..1c4e64499e4 100644 --- a/package/MDAnalysis/coordinates/TRZ.py +++ b/package/MDAnalysis/coordinates/TRZ.py @@ -164,7 +164,7 @@ def __init__(self, trzfilename, n_atoms=None, **kwargs): Raises ------ - AttributeError + ValueError If wrong `n_atoms`/topology file is provided. """ super(TRZReader, self).__init__(trzfilename, **kwargs) @@ -231,9 +231,9 @@ def __init__(self, trzfilename, n_atoms=None, **kwargs): try: self._get_dt() except OSError: - raise AttributeError("`n_atoms` is incompatible " - "with provided trajectory file. " - "(Maybe `topology` is wrong?)") + raise ValueError("Supplied n_atoms {} is incompatible " + "with provided trajectory file. " + "Maybe `topology` is wrong?".format(self.n_atoms)) def _read_trz_header(self): diff --git a/testsuite/MDAnalysisTests/coordinates/test_trz.py b/testsuite/MDAnalysisTests/coordinates/test_trz.py index 3de3445e523..4725fee0940 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_trz.py +++ b/testsuite/MDAnalysisTests/coordinates/test_trz.py @@ -129,6 +129,10 @@ def test_get_writer_2(self, universe, tmpdir): assert_equal(isinstance(W, mda.coordinates.TRZ.TRZWriter), True) assert_equal(W.n_atoms, 100) + def test_get_wrong_n_atoms(self): + with pytest.raises(ValueError, match=r"Supplied n_atoms *"): + mda.Universe(TRZ, n_atoms = 8080) + class TestTRZWriter(RefTRZ): prec = 3 From f2bf33139d79409df8508ac1a404cd489fed0ded Mon Sep 17 00:00:00 2001 From: Yuxuan Zhuang Date: Fri, 3 Jul 2020 10:57:40 +0200 Subject: [PATCH 06/11] blank --- .travis.yml | 2 +- package/MDAnalysis/coordinates/TRZ.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 07f8874fffe..623408a2836 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ branches: only: - master - develop - + os: linux diff --git a/package/MDAnalysis/coordinates/TRZ.py b/package/MDAnalysis/coordinates/TRZ.py index 1c4e64499e4..e8a195efcca 100644 --- a/package/MDAnalysis/coordinates/TRZ.py +++ b/package/MDAnalysis/coordinates/TRZ.py @@ -84,7 +84,6 @@ :members: """ -import sys import warnings import numpy as np import os @@ -136,7 +135,6 @@ class TRZReader(base.ReaderBase): *little-endian* byte order and are read as such. - .. versionchanged:: 0.11.0 Frames now 0-based instead of 1-based. Extra data (Temperature, Energies, Pressures, etc) now read From 5977404b78faefb6dabbad2e20c32685d01d9931 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhuang Date: Mon, 6 Jul 2020 13:35:47 +0200 Subject: [PATCH 07/11] check data.natoms --- package/MDAnalysis/coordinates/TRZ.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/package/MDAnalysis/coordinates/TRZ.py b/package/MDAnalysis/coordinates/TRZ.py index e8a195efcca..a1c9ac6eceb 100644 --- a/package/MDAnalysis/coordinates/TRZ.py +++ b/package/MDAnalysis/coordinates/TRZ.py @@ -226,14 +226,6 @@ def __init__(self, trzfilename, n_atoms=None, **kwargs): self._read_next_timestep() - try: - self._get_dt() - except OSError: - raise ValueError("Supplied n_atoms {} is incompatible " - "with provided trajectory file. " - "Maybe `topology` is wrong?".format(self.n_atoms)) - - def _read_trz_header(self): """Reads the header of the trz trajectory""" self._headerdtype = np.dtype([ @@ -257,6 +249,11 @@ def _read_next_timestep(self, ts=None): try: data = np.fromfile(self.trzfile, dtype=self._dtype, count=1) + if data['natoms'][0] != self.n_atoms: + raise ValueError("Supplied n_atoms {} is incompatible " + "with provided trajectory file. " + "Maybe `topology` is wrong?".format( + self.n_atoms)) ts.frame = data['nframe'][0] - 1 # 0 based for MDA ts._frame = data['ntrj'][0] ts.time = data['treal'][0] From 7178736c2c8c0253dfe6860558de36fba9576373 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhuang Date: Mon, 6 Jul 2020 13:40:44 +0200 Subject: [PATCH 08/11] add n_atoms to writer --- package/MDAnalysis/coordinates/TRZ.py | 1 + 1 file changed, 1 insertion(+) diff --git a/package/MDAnalysis/coordinates/TRZ.py b/package/MDAnalysis/coordinates/TRZ.py index a1c9ac6eceb..4ecbfdd9a18 100644 --- a/package/MDAnalysis/coordinates/TRZ.py +++ b/package/MDAnalysis/coordinates/TRZ.py @@ -598,6 +598,7 @@ def _write_next_frame(self, obj): out['p1a'], out['p1b'] = 20, 20 out['nframe'] = ts.frame + 1 # TRZ wants 1 based out['ntrj'] = data['step'] + out['natoms'] = self.n_atoms out['treal'] = data['time'] out['p2a'], out['p2b'] = 72, 72 out['box'] = self.convert_pos_to_native(unitcell, inplace=False) From 1f086ea4f0a5514f5861c8966f069479d0ead275 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhuang Date: Mon, 6 Jul 2020 13:48:27 +0200 Subject: [PATCH 09/11] pep8 --- package/MDAnalysis/coordinates/TRZ.py | 2 +- testsuite/MDAnalysisTests/coordinates/test_trz.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package/MDAnalysis/coordinates/TRZ.py b/package/MDAnalysis/coordinates/TRZ.py index 4ecbfdd9a18..57b2c002192 100644 --- a/package/MDAnalysis/coordinates/TRZ.py +++ b/package/MDAnalysis/coordinates/TRZ.py @@ -253,7 +253,7 @@ def _read_next_timestep(self, ts=None): raise ValueError("Supplied n_atoms {} is incompatible " "with provided trajectory file. " "Maybe `topology` is wrong?".format( - self.n_atoms)) + self.n_atoms)) ts.frame = data['nframe'][0] - 1 # 0 based for MDA ts._frame = data['ntrj'][0] ts.time = data['treal'][0] diff --git a/testsuite/MDAnalysisTests/coordinates/test_trz.py b/testsuite/MDAnalysisTests/coordinates/test_trz.py index 4725fee0940..a16b77b8a3a 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_trz.py +++ b/testsuite/MDAnalysisTests/coordinates/test_trz.py @@ -131,7 +131,7 @@ def test_get_writer_2(self, universe, tmpdir): def test_get_wrong_n_atoms(self): with pytest.raises(ValueError, match=r"Supplied n_atoms *"): - mda.Universe(TRZ, n_atoms = 8080) + mda.Universe(TRZ, n_atoms=8080) class TestTRZWriter(RefTRZ): From a8a0180f805388fff813749aff020ed10696096d Mon Sep 17 00:00:00 2001 From: Yuxuan Zhuang Date: Tue, 7 Jul 2020 09:54:04 +0200 Subject: [PATCH 10/11] doc --- package/CHANGELOG | 3 ++- package/MDAnalysis/coordinates/TRZ.py | 6 ++++-- testsuite/MDAnalysisTests/coordinates/test_trz.py | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index 69bff4ca708..1a54c4f6980 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -26,7 +26,8 @@ Fixes * Testsuite does not any more matplotlib.use('agg') (#2191) * In ChainReader, read_frame does not trigger change of iterating position. (Issue #2723, PR #2815) - * TRZReader now checks `n_atoms` provided right during initilization. (#2820) + * TRZReader now checks `n_atoms` during initilization. (PR #2820) + * TRZWriter now writes `n_atoms` to the file. (PR #2820) Enhancements diff --git a/package/MDAnalysis/coordinates/TRZ.py b/package/MDAnalysis/coordinates/TRZ.py index 57b2c002192..2893a99b04b 100644 --- a/package/MDAnalysis/coordinates/TRZ.py +++ b/package/MDAnalysis/coordinates/TRZ.py @@ -141,7 +141,8 @@ class TRZReader(base.ReaderBase): into ts.data dictionary. Now passes a weakref of self to ts (ts._reader). .. versionchanged:: 2.0.0 - Now checks for the right `n_atoms` during initilization. + Now checks for the correct `n_atoms` during initilization + and can raise :exc:`ValueError`. """ format = "TRZ" @@ -163,7 +164,8 @@ def __init__(self, trzfilename, n_atoms=None, **kwargs): Raises ------ ValueError - If wrong `n_atoms`/topology file is provided. + If `n_atoms` or the number of atoms in the topology file do not + match the number of atoms in the trajectory. """ super(TRZReader, self).__init__(trzfilename, **kwargs) diff --git a/testsuite/MDAnalysisTests/coordinates/test_trz.py b/testsuite/MDAnalysisTests/coordinates/test_trz.py index a16b77b8a3a..b3efafc170f 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_trz.py +++ b/testsuite/MDAnalysisTests/coordinates/test_trz.py @@ -130,7 +130,7 @@ def test_get_writer_2(self, universe, tmpdir): assert_equal(W.n_atoms, 100) def test_get_wrong_n_atoms(self): - with pytest.raises(ValueError, match=r"Supplied n_atoms *"): + with pytest.raises(ValueError, match=r"Supplied n_atoms"): mda.Universe(TRZ, n_atoms=8080) From a8275aecace2aaaf6912d5957f930b1677a1e5e4 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhuang Date: Tue, 7 Jul 2020 16:09:55 +0200 Subject: [PATCH 11/11] prec doc --- package/CHANGELOG | 4 ++-- package/MDAnalysis/coordinates/TRZ.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index 1a54c4f6980..06c0be60c2a 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -26,8 +26,8 @@ Fixes * Testsuite does not any more matplotlib.use('agg') (#2191) * In ChainReader, read_frame does not trigger change of iterating position. (Issue #2723, PR #2815) - * TRZReader now checks `n_atoms` during initilization. (PR #2820) - * TRZWriter now writes `n_atoms` to the file. (PR #2820) + * TRZReader now checks `n_atoms` on reading. (Issue #2817, PR #2820) + * TRZWriter now writes `n_atoms` to the file. (Issue #2817, PR #2820) Enhancements diff --git a/package/MDAnalysis/coordinates/TRZ.py b/package/MDAnalysis/coordinates/TRZ.py index 2893a99b04b..e0f6de6a3a1 100644 --- a/package/MDAnalysis/coordinates/TRZ.py +++ b/package/MDAnalysis/coordinates/TRZ.py @@ -141,7 +141,7 @@ class TRZReader(base.ReaderBase): into ts.data dictionary. Now passes a weakref of self to ts (ts._reader). .. versionchanged:: 2.0.0 - Now checks for the correct `n_atoms` during initilization + Now checks for the correct `n_atoms` on reading and can raise :exc:`ValueError`. """