Ejemplo n.º 1
0
    def test_set_enabled_aliases(self):
        for i, (aliases, bad, unit) in enumerate(trials):
            u.set_enabled_aliases(aliases)

            assert u.get_current_unit_registry().aliases == aliases

            assert u.Unit(bad) == unit

            for _, bad2, unit2 in trials:
                if bad2 == bad or bad2 in aliases:
                    assert u.Unit(bad2) == unit2
                else:
                    with pytest.raises(ValueError):
                        u.Unit(bad2)
Ejemplo n.º 2
0
    def test_with_custom_units_qtable(self, tmpdir):
        # Test only for QTable - for Table's Column, new units are dropped
        # (as is checked in test_write_drop_nonstandard_units).
        filename = str(tmpdir.join('test_with_units.fits'))
        unit = u.def_unit('bandpass_sol_lum')
        t = QTable()
        t['l'] = np.ones(5) * unit
        with pytest.warns(AstropyUserWarning) as w:
            t.write(filename, overwrite=True)
        assert len(w) == 1
        assert 'bandpass_sol_lum' in str(w[0].message)
        # Just reading back, the data is fine but the unit is not recognized.
        with pytest.warns(u.UnitsWarning, match="'bandpass_sol_lum' did not parse") as w:
            t2 = QTable.read(filename)
        assert len(w) == 1
        assert isinstance(t2['l'].unit, u.UnrecognizedUnit)
        assert str(t2['l'].unit) == 'bandpass_sol_lum'
        assert np.all(t2['l'].value == t['l'].value)

        # But if we enable the unit, it should be recognized.
        with u.add_enabled_units(unit):
            t3 = QTable.read(filename)
            assert t3['l'].unit is unit
            assert equal_data(t3, t)

            # Regression check for #8897; write used to fail when a custom
            # unit was enabled.
            with pytest.warns(AstropyUserWarning):
                t3.write(filename, overwrite=True)

        # It should also be possible to read the file in using a unit alias,
        # even to a unit that may not be the same.
        with u.set_enabled_aliases({'bandpass_sol_lum': u.Lsun}):
            t3 = QTable.read(filename)
            assert t3['l'].unit is u.Lsun
Ejemplo n.º 3
0
 def test_read_with_unit_aliases(self, table_type):
     hdu = BinTableHDU(self.data)
     hdu.columns[0].unit = 'Angstroms'
     hdu.columns[2].unit = 'ergs/(cm.s.Angstroms)'
     with u.set_enabled_aliases(dict(Angstroms=u.AA, ergs=u.erg)):
         t = table_type.read(hdu)
     assert t['a'].unit == u.AA
     assert t['c'].unit == u.erg / (u.cm * u.s * u.AA)
Ejemplo n.º 4
0
    def test_set_enabled_aliases_context_manager(self, aliases, bad, unit, format_):
        if format_ == 'cds':
            bad = bad.replace(' ', '.').replace('**', '')

        with u.set_enabled_aliases(aliases):
            assert u.get_current_unit_registry().aliases == aliases
            assert u.Unit(bad) == unit

        assert u.get_current_unit_registry().aliases == {}
        with pytest.raises(ValueError):
            u.Unit(bad)
Ejemplo n.º 5
0
 def test_cannot_alias_existing_alias_to_another_unit(self):
     u.set_enabled_aliases({'counts': u.count})
     with pytest.raises(ValueError, match='already is an alias'):
         u.add_enabled_aliases({'counts': u.adu})
Ejemplo n.º 6
0
 def test_cannot_alias_existing_unit(self):
     with pytest.raises(ValueError, match='already means'):
         u.set_enabled_aliases({'pct': u.Unit(1e-12*u.count)})
Ejemplo n.º 7
0
 def teardown_method(self):
     u.set_enabled_aliases({})