From 8bc088ce145f563f4cc48c33bd2f19dc6a40b434 Mon Sep 17 00:00:00 2001 From: Irfan Date: Wed, 4 Sep 2019 17:51:20 +0100 Subject: [PATCH 1/2] Changes cell_angle units to degree --- package/CHANGELOG | 4 +++- package/MDAnalysis/coordinates/TRJ.py | 9 ++++--- .../coordinates/test_netcdf.py | 24 +++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/package/CHANGELOG b/package/CHANGELOG index 99a96fc2a4e..146c8b29ca3 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -13,12 +13,14 @@ The rules for this file: * release numbers follow "Semantic Versioning" http://semver.org ------------------------------------------------------------------------------ -mm/dd/yy ??? This could be you +mm/dd/yy IAlibay * 0.20.1 Enhancements Fixes + * The NetCDF writer now writes `cell_angle` units as `degree` instead of + `degrees` in accordance with the AMBER NetCDF convention (Issue #2327). diff --git a/package/MDAnalysis/coordinates/TRJ.py b/package/MDAnalysis/coordinates/TRJ.py index a595a4513ca..7ff3f15d495 100644 --- a/package/MDAnalysis/coordinates/TRJ.py +++ b/package/MDAnalysis/coordinates/TRJ.py @@ -847,10 +847,13 @@ class NCDFWriter(base.WriterBase): .. versionchanged:: 0.17.0 Use fast :mod:`netCDF4` for writing but fall back to slow :mod:`scipy.io.netcdf` if :mod:`netCDF4` is not available. + .. versionchanged:: 0.20.1 + Changes the `cell_angles` unit to the AMBER NetCDF convention standard + of `degree` instead of the `degrees` written in previous version of + MDAnalysis (Issue #2327). .. TODO: - * Change the `cell_angles` units to `degree`, implement `scale_factor` - handling (Issue #2327). + * Implement `scale_factor` handling (Issue #2327). """ @@ -967,7 +970,7 @@ def _init_netcdf(self, periodic=True): cell_angles = ncfile.createVariable('cell_angles', 'f8', ('frame', 'cell_angular')) - setattr(cell_angles, 'units', 'degrees') + setattr(cell_angles, 'units', 'degree') cell_angular = ncfile.createVariable('cell_angular', 'c', ('cell_angular', 'label')) diff --git a/testsuite/MDAnalysisTests/coordinates/test_netcdf.py b/testsuite/MDAnalysisTests/coordinates/test_netcdf.py index c4265cb4899..1274cfbf96f 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_netcdf.py +++ b/testsuite/MDAnalysisTests/coordinates/test_netcdf.py @@ -795,6 +795,30 @@ def test_write_AtomGroup(self, universe, outfile, outtop): self.prec, err_msg="unitcells are not identical") + @pytest.mark.parametrize('var, expected', ( + ('coordinates', 'angstrom'), + ('time', 'picosecond'), + ('cell_lengths', 'angstrom'), + ('cell_angles', 'degree'), + ('velocities', 'angstrom/picosecond'), + )) + def test_writer_units(self, universe, outfile, var, expected): + """Tests that the writer adheres to AMBER convention units + TODO: switch to input trajectory that also has force information. + """ + trr = mda.Universe(GRO, TRR) + + with mda.Writer(outfile, trr.trajectory.n_atoms, velocities=True, + format='ncdf') as W: + for ts in trr.trajectory: + W.write_next_timestep(ts) + + with netcdf.netcdf_file(outfile, mode='r') as ncdf: + unit = ncdf.variables[var].units.decode('utf-8') + assert_equal(unit, expected) + + del trr + class TestNCDFWriter(_NCDFWriterTest, RefVGV): pass From 85e4508cf69b3c3d2e1eda3bd715c6e9e6a22ab3 Mon Sep 17 00:00:00 2001 From: Irfan Date: Wed, 4 Sep 2019 19:42:51 +0100 Subject: [PATCH 2/2] Switches units test to DLP_CONFIG and removes duplicate test --- .../coordinates/test_netcdf.py | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/testsuite/MDAnalysisTests/coordinates/test_netcdf.py b/testsuite/MDAnalysisTests/coordinates/test_netcdf.py index 1274cfbf96f..a45e0720d5c 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_netcdf.py +++ b/testsuite/MDAnalysisTests/coordinates/test_netcdf.py @@ -37,7 +37,7 @@ from MDAnalysisTests.datafiles import (PFncdf_Top, PFncdf_Trj, GRO, TRR, XYZ_mini, - PRM_NCBOX, TRJ_NCBOX) + PRM_NCBOX, TRJ_NCBOX, DLP_CONFIG) from MDAnalysisTests.coordinates.test_trj import _TRJReaderTest from MDAnalysisTests.coordinates.reference import (RefVGV, RefTZ2) from MDAnalysisTests import make_Universe @@ -795,30 +795,6 @@ def test_write_AtomGroup(self, universe, outfile, outtop): self.prec, err_msg="unitcells are not identical") - @pytest.mark.parametrize('var, expected', ( - ('coordinates', 'angstrom'), - ('time', 'picosecond'), - ('cell_lengths', 'angstrom'), - ('cell_angles', 'degree'), - ('velocities', 'angstrom/picosecond'), - )) - def test_writer_units(self, universe, outfile, var, expected): - """Tests that the writer adheres to AMBER convention units - TODO: switch to input trajectory that also has force information. - """ - trr = mda.Universe(GRO, TRR) - - with mda.Writer(outfile, trr.trajectory.n_atoms, velocities=True, - format='ncdf') as W: - for ts in trr.trajectory: - W.write_next_timestep(ts) - - with netcdf.netcdf_file(outfile, mode='r') as ncdf: - unit = ncdf.variables[var].units.decode('utf-8') - assert_equal(unit, expected) - - del trr - class TestNCDFWriter(_NCDFWriterTest, RefVGV): pass @@ -899,6 +875,33 @@ def test_write_ts(self, pos, vel, force, tmpdir, ts1, ts2): u.trajectory.close() +class TestNCDFWriterUnits(object): + """Tests that the writer adheres to AMBER convention units""" + @pytest.fixture() + def outfile(self, tmpdir): + return str(tmpdir) + 'ncdf-writer-1.ncdf' + + @pytest.mark.parametrize('var, expected', ( + ('coordinates', 'angstrom'), + ('time', 'picosecond'), + ('cell_lengths', 'angstrom'), + ('cell_angles', 'degree'), + ('velocities', 'angstrom/picosecond'), + ('forces', 'kilocalorie/mole/angstrom') + )) + def test_writer_units(self, outfile, var, expected): + trr = mda.Universe(DLP_CONFIG, format='CONFIG') + + with mda.Writer(outfile, trr.trajectory.n_atoms, velocities=True, + forces=True, format='ncdf') as W: + for ts in trr.trajectory: + W.write_next_timestep(ts) + + with netcdf.netcdf_file(outfile, mode='r') as ncdf: + unit = ncdf.variables[var].units.decode('utf-8') + assert_equal(unit, expected) + + class TestNCDFWriterErrors(object): @pytest.fixture() def outfile(self, tmpdir):