Skip to content

Commit

Permalink
Merge pull request #7 from sot/refactor_test
Browse files Browse the repository at this point in the history
Refactor test
  • Loading branch information
taldcroft authored Jan 2, 2020
2 parents c95594b + dc0317a commit 063fdcc
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 59 deletions.
13 changes: 13 additions & 0 deletions Ska/quatutil/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

__version__ = '3.3.2'

from .quatutil import * # noqa


def test(*args, **kwargs):
'''
Run py.test unit tests.
'''
import testr
return testr.test(*args, **kwargs)
36 changes: 19 additions & 17 deletions Ska/quatutil.py → Ska/quatutil/quatutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from numpy import sin, cos, tan, arctan2, radians, degrees, sqrt
from Quaternion import Quat

__version__ = '3.3.2'


def radec2eci(ra, dec):
"""
Expand All @@ -20,6 +18,7 @@ def radec2eci(ra, dec):
d = radians(dec)
return np.array([cos(r) * cos(d), sin(r) * cos(d), sin(d)])


def eci2radec(eci):
"""
Convert from ECI vector(s) to RA, Dec. The input ``eci`` value
Expand All @@ -29,7 +28,7 @@ def eci2radec(eci):
:param eci: ECI as 3-vector or (3,N) array
:rtype: list ra, dec (degrees)
"""
ra = degrees(arctan2(eci[1], eci[0]))
ra = degrees(arctan2(eci[1], eci[0]))
dec = degrees(arctan2(eci[2], sqrt(eci[1]**2 + eci[0]**2)))
ok = ra < 0
if isinstance(ok, np.ndarray):
Expand All @@ -38,23 +37,25 @@ def eci2radec(eci):
ra += 360
return ra, dec


def radec2yagzag(ra, dec, q):
"""
Given RA, Dec, and pointing quaternion, determine ACA Y-ang, Z-ang. The
input ``ra`` and ``dec`` values can be 1-d arrays in which case the output
``yag`` and ``zag`` will be corresponding arrays of the same length.
:param ra: Right Ascension (degrees)
:param dec: Declination (degrees)
:param q: Quaternion
:param ra: Right Ascension (degrees)
:param dec: Declination (degrees)
:param q: Quaternion
:rtype: list yag, zag (degrees)
"""
eci = radec2eci(ra, dec);
eci = radec2eci(ra, dec)
d_aca = np.dot(q.transform.transpose(), eci)
yag = degrees(arctan2(d_aca[1], d_aca[0]))
zag = degrees(arctan2(d_aca[2], d_aca[0]))
return yag, zag


def yagzag2radec(yag, zag, q):
"""
Given ACA Y-ang, Z-ang and pointing quaternion determine RA, Dec. The
Expand All @@ -73,11 +74,13 @@ def yagzag2radec(yag, zag, q):
d_aca = np.array([one, tan(radians(yag)), tan(radians(zag))])
d_aca *= 1.0 / np.sum(d_aca**2)
eci = np.dot(q.transform, d_aca)
return eci2radec(eci);
return eci2radec(eci)


def _norm(vec):
def norm(vec):
return vec / np.sqrt(np.sum(vec**2))


def quat_x_to_vec(vec, method='radec'):
"""Generate quaternion that rotates X-axis into ``vec``.
Expand All @@ -94,21 +97,21 @@ def quat_x_to_vec(vec, method='radec'):
[T * Rx(theta) * Z]_y = 0
T[1,1] * sin(theta) + T[1,2]*cos(theta) = 0
theta = atan2(T[1,2], T[1,1])
theta = atan2(T[1,2], T[1,1])
:param vec: Input 3-vector
:param method: method for determining path (shortest|keep_z|radec)
:returns: Quaternion object
"""
x = np.array([1.,0,0])
vec = _norm(np.array(vec))
x = np.array([1., 0, 0])
vec = norm(np.array(vec))
if method in ("shortest", "keep_z"):
dot = np.dot(x, vec)
if abs(dot) > 1-1e-8:
x = _norm(np.array([1., 0., 1e-7]))
if abs(dot) > 1 - 1e-8:
x = norm(np.array([1., 0., 1e-7]))
dot = np.dot(vec, x)
angle = np.arccos(dot)
axis = _norm(np.cross(x, vec))
axis = norm(np.cross(x, vec))
sin_a = np.sin(angle / 2)
cos_a = np.cos(angle / 2)
q = Quat([axis[0] * sin_a,
Expand All @@ -118,7 +121,7 @@ def quat_x_to_vec(vec, method='radec'):

if method == "keep_z":
T = q.transform
theta = np.arctan2(T[1,2], T[1,1])
theta = np.arctan2(T[1, 2], T[1, 1])
qroll = Quat([0, 0, degrees(theta)])
q = q * qroll
else:
Expand All @@ -127,4 +130,3 @@ def quat_x_to_vec(vec, method='radec'):
q = Quat([ra, dec, 0])

return q

Empty file added Ska/quatutil/tests/__init__.py
Empty file.
75 changes: 40 additions & 35 deletions test_quatutils.py → Ska/quatutil/tests/test_quatutils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from nose.tools import *
import numpy as np

import Ska.quatutil
Expand All @@ -11,77 +10,83 @@
ra2 = ra1 + .5
dec2 = dec1 - .1
roll = 30.
q0 = Quat([ra1,dec1,roll])
q0 = Quat([ra1, dec1, roll])


def test_radec2eci():
eci = Ska.quatutil.radec2eci(ra1, dec1)
assert_almost_equal(eci[0], 0.92541658)
assert_almost_equal(eci[1], 0.16317591)
assert_almost_equal(eci[2], 0.34202014)
assert np.allclose(eci[0], 0.92541658)
assert np.allclose(eci[1], 0.16317591)
assert np.allclose(eci[2], 0.34202014)


def test_radec2yagzag():
yag, zag = Ska.quatutil.radec2yagzag(ra2, dec2, q0)
assert_almost_equal(yag, 0.35751029916939936)
assert_almost_equal(zag, -0.32107186086370215)
assert np.allclose(yag, 0.35751029916939936)
assert np.allclose(zag, -0.32107186086370215)


def test_eci2radec():
eci = np.array([ 0.92541658, 0.16317591, 0.34202014])
eci = np.array([0.92541658, 0.16317591, 0.34202014])
tra, tdec = Ska.quatutil.eci2radec(eci)
assert_almost_equal(tra, 9.9999999129952908 )
assert_almost_equal(tdec, 19.999999794004037)
assert np.allclose(tra, 9.9999999129952908)
assert np.allclose(tdec, 19.999999794004037)


def test_vectorized_eci2radec():
eci = np.array([[ 0.92541658, 0.9248273 ],
[ 0.16317591, -0.16307201],
[ 0.34202014, 0.34365969]])
eci = np.array([[0.92541658, 0.9248273],
[0.16317591, -0.16307201],
[0.34202014, 0.34365969]])
tra, tdec = Ska.quatutil.eci2radec(eci)
assert_almost_equal(tra[0], 9.9999999129952908)
assert_almost_equal(tra[1], 349.9999997287627)
assert_almost_equal(tdec[0], 19.999999794004037)
assert_almost_equal(tdec[1], 20.099999743270516)
assert np.allclose(tra[0], 9.9999999129952908)
assert np.allclose(tra[1], 349.9999997287627)
assert np.allclose(tdec[0], 19.999999794004037)
assert np.allclose(tdec[1], 20.099999743270516)


def test_yagzag2radec():
yag = 0.35751029916939936
zag = -0.32107186086370215
tra, tdec = Ska.quatutil.yagzag2radec( yag, zag, q0)
assert_almost_equal( tra, ra2)
assert_almost_equal( tdec, dec2)

tra, tdec = Ska.quatutil.yagzag2radec(yag, zag, q0)
assert np.allclose(tra, ra2)
assert np.allclose(tdec, dec2)


def test_vectorized_radec():
ras = np.arange(10)
decs = np.arange(10)
ecis = Ska.quatutil.radec2eci(ras, decs)
assert_equal(ecis.shape, (3, 10))
assert ecis.shape == (3, 10)
ra1s, dec1s = Ska.quatutil.eci2radec(ecis)
for ra, ra1, dec, dec1 in zip(ras, ra1s, decs, dec1s):
assert_almost_equal(ra, ra1)
assert_almost_equal(dec, dec1)
assert np.allclose(ra, ra1)
assert np.allclose(dec, dec1)


def test_vectorized_yagzag():
ras = np.arange(1,11)
decs = np.arange(1,11)
ras = np.arange(1, 11)
decs = np.arange(1, 11)
yags, zags = Ska.quatutil.radec2yagzag(ras, decs, q0)
assert_equal(yags.shape, (10,))
assert_equal(zags.shape, (10,))
assert yags.shape == (10,)
assert zags.shape == (10,)
ra1s, dec1s = Ska.quatutil.yagzag2radec(yags, zags, q0)
for ra, ra1, dec, dec1 in zip(ras, ra1s, decs, dec1s):
assert_almost_equal(ra, ra1)
assert_almost_equal(dec, dec1)

assert np.allclose(ra, ra1)
assert np.allclose(dec, dec1)


def test_quat_x_to_vec():
vecs = np.random.random((100, 3)) - 0.5
for vec in vecs:
vec = Ska.quatutil._norm(vec)
vec = Ska.quatutil.norm(vec)
for method in ('keep_z', 'shortest', 'radec'):
q = Ska.quatutil.quat_x_to_vec(vec, method)
vec1 = np.dot(q.transform, [1.0, 0, 0])
print(method, vec, vec1)
for i in range(3):
assert_almost_equal(vec[i], vec1[i])
assert np.allclose(vec[i], vec1[i])
if method == 'radec':
assert_almost_equal(q.roll, 0.0)
assert np.allclose(q.roll, 0.0)
elif method == 'keep_z':
vec1 = np.dot(q.transform, [0, 0, 1.0])
assert_almost_equal(vec1[1], 0.0)
assert np.allclose(vec1[1], 0.0)
18 changes: 11 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@

from Ska.quatutil import __version__

try:
from testr.setup_helper import cmdclass
except ImportError:
cmdclass = {}

setup(name='Ska.quatutil',
author = 'Tom Aldcroft',
author='Tom Aldcroft',
description='ACA quaternion utilities',
author_email = 'aldcroft@head.cfa.harvard.edu',
py_modules = ['Ska.quatutil'],
url = 'http://cxc.harvard.edu/mta/ASPECT/tool_doc/pydocs/Ska.quatutil.html',
author_email='taldcroft@cfa.harvard.edu',
url='http://cxc.harvard.edu/mta/ASPECT/tool_doc/pydocs/Ska.quatutil.html',
version=__version__,
zip_safe=False,
packages=['Ska'],
package_dir={'Ska' : 'Ska'},
package_data={}
packages=['Ska', 'Ska.quatutil', 'Ska.quatutil.tests'],
tests_require=['pytest'],
cmdclass=cmdclass,
)

0 comments on commit 063fdcc

Please sign in to comment.