Beispiel #1
0
def assert_follows_unicode_guidelines(
        x, roundtrip=None):
    """
    Test that an object follows our Unicode policy.  See
    "Unicode guidelines" in the coding guidelines.

    Parameters
    ----------
    x : object
        The instance to test

    roundtrip : module, optional
        When provided, this namespace will be used to evaluate
        ``repr(x)`` and ensure that it roundtrips.  It will also
        ensure that ``__bytes__(x)`` roundtrip.
        If not provided, no roundtrip testing will be performed.
    """
    from astropy import conf

    with conf.set_temp('unicode_output', False):
        bytes_x = bytes(x)
        unicode_x = str(x)
        repr_x = repr(x)

        assert isinstance(bytes_x, bytes)
        bytes_x.decode('ascii')
        assert isinstance(unicode_x, str)
        unicode_x.encode('ascii')
        assert isinstance(repr_x, str)
        if isinstance(repr_x, bytes):
            repr_x.decode('ascii')
        else:
            repr_x.encode('ascii')

        if roundtrip is not None:
            assert x.__class__(bytes_x) == x
            assert x.__class__(unicode_x) == x
            assert eval(repr_x, roundtrip) == x

    with conf.set_temp('unicode_output', True):
        bytes_x = bytes(x)
        unicode_x = str(x)
        repr_x = repr(x)

        assert isinstance(bytes_x, bytes)
        bytes_x.decode('ascii')
        assert isinstance(unicode_x, str)
        assert isinstance(repr_x, str)
        if isinstance(repr_x, bytes):
            repr_x.decode('ascii')
        else:
            repr_x.encode('ascii')

        if roundtrip is not None:
            assert x.__class__(bytes_x) == x
            assert x.__class__(unicode_x) == x
            assert eval(repr_x, roundtrip) == x
Beispiel #2
0
def assert_follows_unicode_guidelines(
        x, roundtrip=None):
    """
    Test that an object follows our Unicode policy.  See
    "Unicode guidelines" in the coding guidelines.

    Parameters
    ----------
    x : object
        The instance to test

    roundtrip : module, optional
        When provided, this namespace will be used to evaluate
        ``repr(x)`` and ensure that it roundtrips.  It will also
        ensure that ``__bytes__(x)`` roundtrip.
        If not provided, no roundtrip testing will be performed.
    """
    from astropy import conf

    with conf.set_temp('unicode_output', False):
        bytes_x = bytes(x)
        unicode_x = str(x)
        repr_x = repr(x)

        assert isinstance(bytes_x, bytes)
        bytes_x.decode('ascii')
        assert isinstance(unicode_x, str)
        unicode_x.encode('ascii')
        assert isinstance(repr_x, str)
        if isinstance(repr_x, bytes):
            repr_x.decode('ascii')
        else:
            repr_x.encode('ascii')

        if roundtrip is not None:
            assert x.__class__(bytes_x) == x
            assert x.__class__(unicode_x) == x
            assert eval(repr_x, roundtrip) == x

    with conf.set_temp('unicode_output', True):
        bytes_x = bytes(x)
        unicode_x = str(x)
        repr_x = repr(x)

        assert isinstance(bytes_x, bytes)
        bytes_x.decode('ascii')
        assert isinstance(unicode_x, str)
        assert isinstance(repr_x, str)
        if isinstance(repr_x, bytes):
            repr_x.decode('ascii')
        else:
            repr_x.encode('ascii')

        if roundtrip is not None:
            assert x.__class__(bytes_x) == x
            assert x.__class__(unicode_x) == x
            assert eval(repr_x, roundtrip) == x
Beispiel #3
0
 def test_format(self, Column):
     """Show that the formatted output from str() works"""
     from astropy import conf
     with conf.set_temp('max_lines', 8):
         c1 = Column(np.arange(2000), name='a', dtype=float, format='%6.2f')
         assert str(c1).splitlines() == [
             '   a   ', '-------', '   0.00', '   1.00', '    ...',
             '1998.00', '1999.00', 'Length = 2000 rows'
         ]
Beispiel #4
0
 def test_column_format_with_threshold(self, table_type):
     from astropy import conf
     with conf.set_temp('max_lines', 8):
         t = table_type([np.arange(20)], names=['a'])
         t['a'].format = '%{0:}'
         assert str(t['a']).splitlines() == [
             ' a ', '---', ' %0', ' %1', '...', '%18', '%19',
             'Length = 20 rows'
         ]
         t['a'].format = '{ %4.2f }'
         assert str(t['a']).splitlines() == [
             '    a    ', '---------', ' { 0.00 }', ' { 1.00 }',
             '      ...', '{ 18.00 }', '{ 19.00 }', 'Length = 20 rows'
         ]
Beispiel #5
0
 def test_format(self, Column):
     """Show that the formatted output from str() works"""
     from astropy import conf
     with conf.set_temp('max_lines', 8):
         c1 = Column(np.arange(2000), name='a', dtype=float,
                     format='%6.2f')
         assert str(c1).splitlines() == ['   a   ',
                                         '-------',
                                         '   0.00',
                                         '   1.00',
                                         '    ...',
                                         '1998.00',
                                         '1999.00',
                                         'Length = 2000 rows']
Beispiel #6
0
 def test_column_format_with_threshold_masked_table(self):
     from astropy import conf
     with conf.set_temp('max_lines', 8):
         t = Table([np.arange(20)], names=['a'], masked=True)
         t['a'].format = '%{0:}'
         t['a'].mask[0] = True
         t['a'].mask[-1] = True
         assert str(t['a']).splitlines() == [
             ' a ', '---', ' --', ' %1', '...', '%18', ' --',
             'Length = 20 rows'
         ]
         t['a'].format = '{ %4.2f }'
         assert str(t['a']).splitlines() == [
             '    a    ', '---------', '       --', ' { 1.00 }',
             '      ...', '{ 18.00 }', '       --', 'Length = 20 rows'
         ]
Beispiel #7
0
 def test_column_format_with_threshold(self, table_type):
     from astropy import conf
     with conf.set_temp('max_lines', 8):
         t = table_type([np.arange(20)], names=['a'])
         t['a'].format = '%{0:}'
         assert str(t['a']).splitlines() == [' a ',
                                             '---',
                                             ' %0',
                                             ' %1',
                                             '...',
                                             '%18',
                                             '%19',
                                             'Length = 20 rows']
         t['a'].format = '{ %4.2f }'
         assert str(t['a']).splitlines() == ['    a    ',
                                             '---------',
                                             ' { 0.00 }',
                                             ' { 1.00 }',
                                             '      ...',
                                             '{ 18.00 }',
                                             '{ 19.00 }',
                                             'Length = 20 rows']
Beispiel #8
0
def test_sip_hst():
    """Test SIP against astropy.wcs"""

    test_file = get_pkg_data_filename(os.path.join('data', 'hst_sip.hdr'))
    hdr = fits.Header.fromtextfile(test_file)
    crpix1 = hdr['CRPIX1']
    crpix2 = hdr['CRPIX2']
    wobj = wcs.WCS(hdr)
    a_pars = dict(**hdr['A_*'])
    b_pars = dict(**hdr['B_*'])
    a_order = a_pars.pop('A_ORDER')
    b_order = b_pars.pop('B_ORDER')
    sip = SIP([crpix1, crpix2], a_order, b_order, a_pars, b_pars)
    coords = [1, 1]
    rel_coords = [1 - crpix1, 1 - crpix2]
    astwcs_result = wobj.sip_pix2foc([coords], 1)[0] - rel_coords
    assert_allclose(sip(1, 1), astwcs_result)

    # Test changing of inputs and calling it with keyword argumenrts.
    sip.inputs = ("r", "t")
    assert_allclose(sip(r=1, t=1), astwcs_result)
    assert_allclose(sip(1, t=1), astwcs_result)

    # Test representations
    assert repr(sip) == (
        "<SIP([<Shift(offset=-2048.)>, <Shift(offset=-1024.)>, "
        "<_SIP1D(4, 'A', A_2_0=0.00000855, A_3_0=-0., A_4_0=0., A_0_2=0.00000217, "
        "A_0_3=0., A_0_4=0., A_1_1=-0.0000052, A_1_2=-0., A_1_3=-0., "
        "A_2_1=-0., A_2_2=0., A_3_1=0.)>, "
        "<_SIP1D(4, 'B', B_2_0=-0.00000175, B_3_0=0., B_4_0=-0., B_0_2=-0.00000722, "
        "B_0_3=-0., B_0_4=-0., B_1_1=0.00000618, B_1_2=-0., B_1_3=0., "
        "B_2_1=-0., B_2_2=-0., B_3_1=-0.)>])>")
    with conf.set_temp('max_width', 80):
        assert str(sip) == (
            "Model: SIP\n"
            "    Model: Shift\n"
            "    Inputs: ('x',)\n"
            "    Outputs: ('y',)\n"
            "    Model set size: 1\n"
            "    Parameters:\n"
            "         offset\n"
            "        -------\n"
            "        -2048.0\n"
            "\n"
            "    Model: Shift\n"
            "    Inputs: ('x',)\n"
            "    Outputs: ('y',)\n"
            "    Model set size: 1\n"
            "    Parameters:\n"
            "         offset\n"
            "        -------\n"
            "        -1024.0\n"
            "\n"
            "    Model: _SIP1D\n"
            "    Inputs: ('x', 'y')\n"
            "    Outputs: ('z',)\n"
            "    Model set size: 1\n"
            "    Order: 4\n"
            "    Coeff. Prefix: A\n"
            "    Parameters:\n"
            "                A_2_0                 A_3_0          ...         A_3_1        \n"
            "        --------------------- ---------------------- ... ---------------------\n"
            "        8.551277582556502e-06 -4.730444829222791e-10 ... 1.971022971660309e-15\n"
            "\n"
            "    Model: _SIP1D\n"
            "    Inputs: ('x', 'y')\n"
            "    Outputs: ('z',)\n"
            "    Model set size: 1\n"
            "    Order: 4\n"
            "    Coeff. Prefix: B\n"
            "    Parameters:\n"
            "                B_2_0                  B_3_0         ...         B_3_1         \n"
            "        ---------------------- --------------------- ... ----------------------\n"
            "        -1.746491877058669e-06 8.567635427816317e-11 ... -3.779506805487476e-15\n"
        )

    # Test get num of coeffs
    assert sip.sip1d_a.get_num_coeff(1) == 6
    # Test error
    message = "Degree of polynomial must be 2< deg < 9"
    sip.sip1d_a.order = 1
    with pytest.raises(ValueError) as err:
        sip.sip1d_a.get_num_coeff(1)
    assert str(err.value) == message
    sip.sip1d_a.order = 10
    with pytest.raises(ValueError) as err:
        sip.sip1d_a.get_num_coeff(1)
    assert str(err.value) == message