Skip to content

Commit

Permalink
Issue 363 align pa (#1051)
Browse files Browse the repository at this point in the history
* fix bug and docs in rotaxis

If a == b this used to return [nan, nan, nan] due to the division by 0.

* pep8 changes test_transformations.py

* add rotaxis test

* add align_principal_axis test

* update changelog
  • Loading branch information
kain88-de authored and richardjgowers committed Oct 25, 2016
1 parent c0ddc89 commit a3e601f
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 58 deletions.
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Fixes
* Progress meters are now displayed as expected on jupyter notebooks
(Issue #927)
* Reset trajectory to 0 after sliced iteration (Issue #1031)
* Fixed rotaxis returning NaN if a=b (Issue #1045)
* Fixed align_principal_axis onto a principal axes (Issue #1045)

Changes
* Started unifying the API of analysis classes (named internally
Expand Down
21 changes: 20 additions & 1 deletion package/MDAnalysis/lib/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,26 @@ def _import_module(module_name, warn=True, prefix='_py_', ignore='_'):
# orbeckst --- some simple geometry

def rotaxis(a, b):
"""Return the rotation axis to rotate vector a into b."""
"""Return the rotation axis to rotate vector a into b.
Parameters
----------
a, b : array_like
two vectors
Returns
-------
c : np.ndarray
vector to rotate a into b
Note
----
If a == b this will always return [1, 0, 0]
"""
if np.allclose(a, b):
return np.array([1, 0, 0])
c = np.cross(a, b)
return c / np.linalg.norm(c)

Expand Down
37 changes: 35 additions & 2 deletions testsuite/MDAnalysisTests/core/test_topologyattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
"""Tests for MDAnalysis.core.topologyattrs objects.
"""
import numpy as np

from numpy.testing import (
assert_,
assert_array_equal,
assert_array_almost_equal,
)
import numpy as np
from nose.tools import assert_raises

from MDAnalysisTests.plugins.knownfailure import knownfailure
from MDAnalysisTests.datafiles import PSF, DCD

import MDAnalysis as mda
import MDAnalysis.core.topologyattrs as tpattrs
from MDAnalysis.core.topology import Topology
from MDAnalysis.exceptions import NoDataError
Expand Down Expand Up @@ -233,3 +236,33 @@ def test_set_segments(self):
np.array([23, -0.0002]))
assert_array_equal(self.attr.get_segments(DummyGroup([1, 0, 1])),
np.array([-0.0002, 23, -0.0002]))


class TestAttr(object):
def setUp(self):
self.universe = mda.Universe(PSF, DCD)
self.ag = self.universe.atoms # prototypical AtomGroup

def tearDown(self):
del self.universe
del self.ag

def test_principal_axes(self):
assert_array_almost_equal(
self.ag.principal_axes(),
np.array([[-9.99925632e-01, 1.21546132e-02, 9.98264877e-04],
[1.20986911e-02, 9.98951474e-01, -4.41539838e-02],
[1.53389276e-03, 4.41386224e-02, 9.99024239e-01]]))

def test_align_principal_axes_with_self(self):
pa = self.ag.principal_axes()
self.ag.align_principal_axis(0, pa[0])
assert_array_almost_equal(self.ag.principal_axes(), pa)

def test_align_principal_axes_with_x(self):
self.ag.align_principal_axis(0, [1, 0, 0])
# This is a very loose check that the difference is not more then 0.5.
# This is OK here because the rounding error in the calculation really
# is that big.
assert_(np.allclose(np.abs(self.ag.principal_axes()), np.eye(3),
rtol=0, atol=0.5))
Loading

0 comments on commit a3e601f

Please sign in to comment.