Skip to content

Commit

Permalink
increase mangle test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverba137 committed Jun 14, 2016
1 parent 99d841c commit f912cda
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/pydl/pydlutils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ API
.. automodapi:: pydl.pydlutils.image

.. automodapi:: pydl.pydlutils.mangle
:skip: PydlutilsException
:skip: PydlutilsException, PydlutilsUserWarning

.. automodapi:: pydl.pydlutils.math

Expand Down
11 changes: 6 additions & 5 deletions pydl/pydlutils/mangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from astropy.io import fits
from astropy.extern import six
# import astropy.utils as au
from warnings import warn
import warnings
from . import PydlutilsException, PydlutilsUserWarning


Expand Down Expand Up @@ -227,8 +227,9 @@ def _garea_helper(self):
"""
if self.gzeroar():
return 0.0
warn(("The ManglePolygon.garea() method is incomplete and is " +
"returning a dummy value."), PydlutilsUserWarning)
warnings.warn(("The ManglePolygon.garea() method is incomplete" +
"and is returning a dummy value."),
PydlutilsUserWarning)
return 1.0

def gzeroar(self):
Expand Down Expand Up @@ -369,7 +370,7 @@ def angles_to_x(points, latitude=False):
theta = np.radians(90.0 - points[:, 1])
else:
theta = np.radians(points[:, 1])
st = np.sin(np.radians(theta))
st = np.sin(theta)
x[:, 0] = np.cos(phi) * st
x[:, 1] = np.sin(phi) * st
x[:, 2] = np.cos(theta)
Expand Down Expand Up @@ -428,7 +429,7 @@ def circle_cap(radius, points):
:func:`tuple`
A tuple containing ``X`` and ``CM`` values for the cap.
"""
npoints, ncol == points.shape
npoints, ncol = points.shape
if ncol == 2:
x = angles_to_x(points, latitude=True)
elif ncol == 3:
Expand Down
91 changes: 85 additions & 6 deletions pydl/pydlutils/tests/test_mangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from .. import PydlutilsException
from ..mangle import (ManglePolygon, is_cap_used, read_fits_polygons,
read_mangle_polygons, set_use_caps, cap_distance,
is_in_cap, angles_to_x)
is_in_cap, angles_to_x, x_to_angles, circle_cap,
is_in_polygon)


class TestMangle(object):
Expand All @@ -29,7 +30,7 @@ def test_ManglePolygon(self):
poly = ManglePolygon()
x = np.array([[0.0, 0.0, 1.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 1.0]])
[0.0, 1.0, 0.0]])
cm = np.array([1.0, 1.0, 1.0])
poly = ManglePolygon(x=x, cm=cm, str=np.pi/2.0)
assert poly.ncaps == 3
Expand All @@ -45,9 +46,32 @@ def test_ManglePolygon(self):
assert poly2.use_caps == poly.use_caps
assert (poly2.cm == poly.cm).all()
assert np.allclose(poly2.str, np.pi/2.0)
x = np.array([[0.0, 0.0, 1.0],
[1.0, 0.0, 0.0]])
cm = np.array([1.0, 1.0])
poly = ManglePolygon(x=x, cm=cm)
poly2 = poly.add_caps(np.array([[0.0, 1.0, 0.0], ]), np.array([1.0, ]))
assert poly2.ncaps == 3
assert poly2.use_caps == poly.use_caps
assert poly2.str == 1.0 # dummy value!
poly3 = poly.polyn(poly2, 2)
assert poly3.ncaps == 3
assert poly3.use_caps == poly.use_caps
assert np.allclose(poly3.x[2, :], np.array([0.0, 1.0, 0.0]))
poly3 = poly.polyn(poly2, 2, complement=True)
assert poly3.ncaps == 3
assert poly3.use_caps == poly.use_caps
assert np.allclose(poly3.cm[2], -1.0)

def test_angles_to_x(self):
pass
x = angles_to_x(np.array([[0.0, 0.0], [90.0, 90.0],
[0.0, 90.0]]))
assert np.allclose(x, np.array([[0.0, 0.0, 1.0], [0.0, 1.0, 0.0],
[1.0, 0.0, 0.0]]))
a = angles_to_x(np.array([[0.0, 90.0], [90.0, 0.0],
[0.0, 0.0]]), latitude=True)
assert np.allclose(x, np.array([[0.0, 0.0, 1.0], [0.0, 1.0, 0.0],
[1.0, 0.0, 0.0]]))

def test_cap_distance(self):
x = np.array([0.0, 0.0, 1.0])
Expand All @@ -65,7 +89,24 @@ def test_cap_distance(self):
assert np.allclose(d, np.array([45.0]))

def test_circle_cap(self):
pass
with raises(ValueError):
x, cm = circle_cap(90.0, np.array([[1.0, 2.0, 3.0, 4.0], ]))
xin = np.array([[0.0, 0.0, 1.0], ])
x, cm = circle_cap(90.0, xin)
assert np.allclose(x, xin)
assert np.allclose(cm, 1.0)
radec = np.array([[0.0, 90.0], ])
x, cm = circle_cap(90.0, radec)
assert np.allclose(x, xin)
assert np.allclose(cm, 1.0)
x, cm = circle_cap(np.float32(90.0), radec)
assert np.allclose(x, xin)
assert np.allclose(cm, 1.0)
x, cm = circle_cap(np.array([90.0, ]), radec)
assert np.allclose(x, xin)
assert np.allclose(cm, np.array([1.0, ]))
with raises(ValueError):
x, cm = circle_cap(np.array([90.0, 90.0]), radec)

def test_is_cap_used(self):
assert is_cap_used(1 << 2, 2)
Expand All @@ -78,7 +119,15 @@ def test_is_in_cap(self):
assert (d == np.array([True, False])).all()

def test_is_in_polygon(self):
pass
x = np.array([[0.0, 0.0, 1.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0]])
cm = np.array([1.0, 1.0, 1.0])
p = ManglePolygon(x=x, cm=cm)
d = is_in_polygon(p, np.array([[45.0, 45.0], [135.0, -45.0]]))
assert (d == np.array([True, False])).all()
d = is_in_polygon(p, np.array([[45.0, 45.0], [-45.0, 45.0]]), ncaps=2)
assert (d == np.array([True, False])).all()

def test_read_fits_polygons(self):
poly = read_fits_polygons(self.poly_fits)
Expand Down Expand Up @@ -120,9 +169,39 @@ def test_set_use_caps(self):
assert use_caps == poly[0].use_caps
use_caps = set_use_caps(poly[0], index_list)
assert use_caps == poly[0].use_caps
x = np.array([[0.0, 0.0, 1.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[1.0-1.0e-8, 1.0e-8, 0.0]])
cm = np.array([1.0, 1.0, 1.0, 1.0-1.0e-8])
p = ManglePolygon(x=x, cm=cm)
index_list = list(range(p.ncaps))
assert p.use_caps == 2**4 - 1
use_caps = set_use_caps(p, index_list, tol=1.0e-7)
assert use_caps == 2**3 - 1
x = np.array([[0.0, 0.0, 1.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[1.0-1.0e-8, 1.0e-8, 0.0]])
cm = np.array([1.0, 1.0, 1.0, -1.0+1.0e-8])
p = ManglePolygon(x=x, cm=cm)
index_list = list(range(p.ncaps))
assert p.use_caps == 2**4 - 1
use_caps = set_use_caps(p, index_list, tol=1.0e-7)
assert use_caps == 2**3 - 1
use_caps = set_use_caps(p, index_list, tol=1.0e-7,
allow_neg_doubles=True)
assert use_caps == 2**4 - 1

def test_x_to_angles(self):
pass
a = x_to_angles(np.array([[0.0, 0.0, 1.0], [0.0, 1.0, 0.0],
[1.0, 0.0, 0.0]]))
assert np.allclose(a, np.array([[0.0, 0.0], [90.0, 90.0],
[0.0, 90.0]]))
a = x_to_angles(np.array([[0.0, 0.0, 1.0], [0.0, 1.0, 0.0],
[1.0, 0.0, 0.0]]), latitude=True)
assert np.allclose(a, np.array([[0.0, 90.0], [90.0, 0.0],
[0.0, 0.0]]))


def fits_polygon_file():
Expand Down

0 comments on commit f912cda

Please sign in to comment.