Esempio n. 1
0
def test_angle():
    """Test basic construction and use of Angle and AngleUnit classes
    """
    # First Angle:
    theta1 = numpy.pi / 4. * galsim.radians
    theta2 = 45 * galsim.degrees
    theta3 = 3 * galsim.hours
    theta4 = 45 * 60 * galsim.arcmin
    theta5 = galsim.Angle(45 * 3600,
                          galsim.arcsec)  # Check explicit installation too.

    assert theta1.rad() == numpy.pi / 4.
    numpy.testing.assert_almost_equal(theta2.rad(), numpy.pi / 4., decimal=12)
    numpy.testing.assert_almost_equal(theta3.rad(), numpy.pi / 4., decimal=12)
    numpy.testing.assert_almost_equal(theta4.rad(), numpy.pi / 4., decimal=12)
    numpy.testing.assert_almost_equal(theta5.rad(), numpy.pi / 4., decimal=12)

    # Check wrapping
    theta6 = (45 + 360) * galsim.degrees
    assert abs(theta6.rad() - theta1.rad()) > 6.
    numpy.testing.assert_almost_equal(theta6.wrap().rad(),
                                      theta1.rad(),
                                      decimal=12)

    theta7 = (45 - 360) * galsim.degrees
    assert abs(theta7.rad() - theta1.rad()) > 6.
    numpy.testing.assert_almost_equal(theta7.wrap().rad(),
                                      theta1.rad(),
                                      decimal=12)

    # Check wrapping with non-default center
    pi_rad = pi * galsim.radians
    numpy.testing.assert_almost_equal(theta6.wrap(pi_rad).rad(),
                                      theta1.rad(),
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta6.rad(),
                                      theta1.wrap(2 * pi_rad).rad(),
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta6.rad(),
                                      theta1.wrap(3 * pi_rad).rad(),
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta7.rad(),
                                      theta1.wrap(-pi_rad).rad(),
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta7.rad(),
                                      theta1.wrap(-2 * pi_rad).rad(),
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta6.wrap(27 * galsim.radians).rad(),
                                      theta1.wrap(27 * galsim.radians).rad(),
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta7.wrap(-127 * galsim.radians).rad(),
                                      theta1.wrap(-127 * galsim.radians).rad(),
                                      decimal=12)

    # Make a new AngleUnit as described in the AngleUnit docs
    gradians = galsim.AngleUnit(2. * numpy.pi / 400.)
    theta8 = 50 * gradians
    numpy.testing.assert_almost_equal(theta8.rad(), numpy.pi / 4., decimal=12)

    # Check simple math
    numpy.testing.assert_almost_equal((theta1 + theta2).rad(),
                                      numpy.pi / 2.,
                                      decimal=12)
    numpy.testing.assert_almost_equal((4 * theta3).rad(), numpy.pi, decimal=12)
    numpy.testing.assert_almost_equal((4 * theta4 - theta2).rad(),
                                      0.75 * numpy.pi,
                                      decimal=12)
    numpy.testing.assert_almost_equal((theta5 / 2.).rad(),
                                      numpy.pi / 8.,
                                      decimal=12)

    numpy.testing.assert_almost_equal(theta3 / galsim.radians,
                                      numpy.pi / 4.,
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta1 / galsim.hours, 3., decimal=12)
    numpy.testing.assert_almost_equal(galsim.hours / galsim.arcmin,
                                      15 * 60,
                                      decimal=12)

    # Check picklability
    do_pickle(galsim.radians)
    do_pickle(galsim.degrees)
    do_pickle(galsim.hours)
    do_pickle(galsim.arcmin)
    do_pickle(galsim.arcsec)
    do_pickle(gradians)
    do_pickle(theta1)
    do_pickle(theta2)
    do_pickle(theta3)
    do_pickle(theta4)
    do_pickle(theta5)
    do_pickle(theta6)
    do_pickle(theta7)
    do_pickle(theta8)
Esempio n. 2
0
def test_angle():
    """Test basic construction and use of Angle and AngleUnit classes
    """
    # First Angle:
    theta1 = pi / 4. * galsim.radians
    theta2 = 45 * galsim.degrees
    theta3 = 3 * galsim.hours
    theta4 = 45 * 60 * galsim.arcmin
    theta5 = galsim.Angle(45 * 3600,
                          galsim.arcsec)  # Check explicit installation too.
    theta6 = galsim._Angle(
        pi / 4.)  # Underscore constructor implicitly uses radians

    assert theta1.rad == pi / 4.
    numpy.testing.assert_almost_equal(theta2.rad, pi / 4., decimal=12)
    numpy.testing.assert_almost_equal(theta3.rad, pi / 4., decimal=12)
    numpy.testing.assert_almost_equal(theta4.rad, pi / 4., decimal=12)
    numpy.testing.assert_almost_equal(theta5.rad, pi / 4., decimal=12)
    numpy.testing.assert_almost_equal(theta6.rad, pi / 4., decimal=12)

    # Check wrapping
    theta6 = (45 + 360) * galsim.degrees
    assert abs(theta6.rad - theta1.rad) > 6.
    numpy.testing.assert_almost_equal(theta6.wrap().rad,
                                      theta1.rad,
                                      decimal=12)

    # Check trig calls
    numpy.testing.assert_almost_equal(theta6.sin(), theta1.sin(), decimal=12)
    numpy.testing.assert_almost_equal(theta6.cos(), theta1.cos(), decimal=12)
    numpy.testing.assert_almost_equal(theta6.tan(), theta1.tan(), decimal=12)
    numpy.testing.assert_almost_equal(theta6.sin(), math.sqrt(0.5), decimal=12)
    numpy.testing.assert_almost_equal(theta6.cos(), math.sqrt(0.5), decimal=12)
    numpy.testing.assert_almost_equal(theta6.tan(), 1., decimal=12)
    numpy.testing.assert_array_almost_equal(theta6.sincos(),
                                            math.sqrt(0.5),
                                            decimal=12)

    theta7 = (45 - 360) * galsim.degrees
    assert abs(theta7.rad - theta1.rad) > 6.
    numpy.testing.assert_almost_equal(theta7.wrap().rad,
                                      theta1.rad,
                                      decimal=12)

    # Check wrapping with non-default center
    pi_rad = pi * galsim.radians
    numpy.testing.assert_almost_equal(theta6.wrap(pi_rad).rad,
                                      theta1.rad,
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta6.rad,
                                      theta1.wrap(2 * pi_rad).rad,
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta6.rad,
                                      theta1.wrap(3 * pi_rad).rad,
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta7.rad,
                                      theta1.wrap(-pi_rad).rad,
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta7.rad,
                                      theta1.wrap(-2 * pi_rad).rad,
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta6.wrap(27 * galsim.radians).rad,
                                      theta1.wrap(27 * galsim.radians).rad,
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta7.wrap(-127 * galsim.radians).rad,
                                      theta1.wrap(-127 * galsim.radians).rad,
                                      decimal=12)

    # Make a new AngleUnit as described in the AngleUnit docs
    gradians = galsim.AngleUnit(2. * pi / 400.)
    theta8 = 50 * gradians
    numpy.testing.assert_almost_equal(theta8.rad, pi / 4., decimal=12)
    numpy.testing.assert_almost_equal(theta8 / gradians, 50., decimal=12)
    numpy.testing.assert_almost_equal(gradians.value,
                                      2. * pi / 400.,
                                      decimal=12)
    numpy.testing.assert_almost_equal(gradians / galsim.radians,
                                      2. * pi / 400.,
                                      decimal=12)

    # Check simple math
    numpy.testing.assert_almost_equal((theta1 + theta2).rad,
                                      pi / 2.,
                                      decimal=12)
    numpy.testing.assert_almost_equal((4 * theta3).rad, pi, decimal=12)
    numpy.testing.assert_almost_equal((4 * theta4 - theta2).rad,
                                      0.75 * pi,
                                      decimal=12)
    numpy.testing.assert_almost_equal((theta5 / 2.).rad, pi / 8., decimal=12)

    numpy.testing.assert_almost_equal(theta3 / galsim.radians,
                                      pi / 4.,
                                      decimal=12)
    numpy.testing.assert_almost_equal(theta1 / galsim.hours, 3., decimal=12)
    numpy.testing.assert_almost_equal(galsim.hours / galsim.arcmin,
                                      15 * 60,
                                      decimal=12)

    # Check copy constructor
    theta9 = galsim.Angle(theta1)
    numpy.testing.assert_equal(theta9.rad, theta1.rad)

    # Check picklability
    do_pickle(galsim.radians)
    do_pickle(galsim.degrees)
    do_pickle(galsim.hours)
    do_pickle(galsim.arcmin)
    do_pickle(galsim.arcsec)
    do_pickle(gradians)
    do_pickle(theta1)
    do_pickle(theta2)
    do_pickle(theta3)
    do_pickle(theta4)
    do_pickle(theta5)
    do_pickle(theta6)
    do_pickle(theta7)
    do_pickle(theta8)

    # Check invalid constructors
    assert_raises(TypeError, galsim.AngleUnit, galsim.degrees)
    assert_raises(ValueError, galsim.AngleUnit, 'spam')
    assert_raises(TypeError, galsim.AngleUnit, 1, 3)
    assert_raises(TypeError, galsim.Angle, 3.4)
    assert_raises(TypeError, galsim.Angle, theta1, galsim.degrees)
    assert_raises(ValueError, galsim.Angle, 'spam', galsim.degrees)
    assert_raises(TypeError, galsim.Angle, 1, 3)