Skip to content

Commit

Permalink
Asserted everywhere, fixed error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdetle committed Jul 10, 2016
1 parent 40eb01c commit 91ba82a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
15 changes: 7 additions & 8 deletions package/MDAnalysis/analysis/rms.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,12 @@ def __init__(self, traj, reference=None, select='all',
self.ref_atoms = self.reference.select_atoms(*self.select['reference'])
self.traj_atoms = self.universe.select_atoms(*self.select['mobile'])
if len(self.ref_atoms) != len(self.traj_atoms):
errmsg = "Reference and trajectory atom selections do "
"not contain the same number of atoms: "
"N_ref={0:d}, N_traj={1:d}".format(
self.ref_atoms.n_atoms,
self.traj_atoms.n_atoms)
errmsg = ("Reference and trajectory atom selections do "+
"not contain the same number of atoms: "+
"N_ref={0:d}, N_traj={1:d}".format(self.ref_atoms.n_atoms,
self.traj_atoms.n_atoms))
logger.exception(errmsg)
raise SelectionError(errormsg)
raise SelectionError(errmsg)
logger.info("RMS calculation for {0:d} atoms.".format(len(self.ref_atoms)))
mass_mismatches = (np.absolute(self.ref_atoms.masses - self.traj_atoms.masses) > self.tol_mass)
if np.any(mass_mismatches):
Expand All @@ -367,9 +366,9 @@ def __init__(self, traj, reference=None, select='all',
if ar.name != at.name:
logger.error("{0!s:>4} {1:3d} {2!s:>3} {3!s:>3} {4:6.3f} | {5!s:>4} {6:3d} {7!s:>3} {8!s:>3} {9:6.3f}".format(ar.segid, ar.resid, ar.resname, ar.name, ar.mass,
at.segid, at.resid, at.resname, at.name, at.mass))
errmsg = "Inconsistent selections, masses differ by more than"
errmsg = ("Inconsistent selections, masses differ by more than"
+ "{0:f}; mis-matching atoms are shown above.".format(
self.tol_mass)
self.tol_mass))
logger.error(errmsg)
raise SelectionError(errmsg)
del mass_mismatches
Expand Down
36 changes: 25 additions & 11 deletions testsuite/MDAnalysisTests/analysis/test_rms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import MDAnalysis as mda
from MDAnalysis.analysis import rms, align

from numpy.testing import TestCase, assert_almost_equal, raises, assert_
from numpy.testing import (TestCase, assert_almost_equal, raises, assert_,
assert_array_almost_equal)

import numpy as np

import os
Expand Down Expand Up @@ -133,7 +135,11 @@ class TestRMSD(object):
def setUp(self):
self.universe = MDAnalysis.Universe(PSF, DCD)
self.tempdir = tempdir.TempDir()
self.outfile = os.path.join(self.tempdir.name, 'rmsd.npy')
self.outfile = os.path.join(self.tempdir.name, 'rmsd.txt')
self.correct_values = [[0, 0, 0], [49, 48.9999, 4.68953]]
self.correct_values_group = [[0 , 0, 0, 0, 0],
[49, 48.9999, 4.7857, 4.7002,
4.68981]]

def tearDown(self):
del self.universe
Expand All @@ -142,29 +148,37 @@ def tearDown(self):
def test_rmsd(self):
RMSD = MDAnalysis.analysis.rms.RMSD(self.universe, select='name CA')
RMSD.run(step=49)
correct_values = [[ 0, 0, 0],
[ 49, 48.9999, 4.68953]]
assert_almost_equal(RMSD.rmsd, correct_values, 4,
err_msg="error: rmsd profile should match test " +
"values")

assert_array_almost_equal(RMSD.rmsd, self.correct_values, 4,
err_msg="error: rmsd profile should match" +
"test values")

def test_rmsd_single_frame(self):
RMSD = MDAnalysis.analysis.rms.RMSD(self.universe, select='name CA')
RMSD.run(start=5, stop=6)

single_frame = [[ 5, 5, 0.91544906]]
assert_array_almost_equal(RMSD.rmsd, single_frame, 4,
err_msg="error: rmsd profile should match" +
"test values")

def test_mass_weighted_and_save(self):
RMSD = MDAnalysis.analysis.rms.RMSD(self.universe, select='name CA')
RMSD.run(step=49, mass_weighted= True)
RMSD.save()

RMSD.save(self.outfile)
saved = np.loadtxt(self.outfile)
assert_array_almost_equal(RMSD.rmsd, saved, 4,
err_msg="error: rmsd profile should match " +
"test values")

def test_rmsd_group_selections(self):
RMSD = MDAnalysis.analysis.rms.RMSD(self.universe,
groupselections=
['backbone','name CA'])
RMSD.run(step=49)

assert_array_almost_equal(RMSD.rmsd, self.correct_values_group, 4,
err_msg="error: rmsd profile should match" +
"test values")


@raises(SelectionError)
def test_ref_length_unequal_len(self):
Expand Down

0 comments on commit 91ba82a

Please sign in to comment.