Beispiel #1
0
def test_roundtrip_ogip(unit):
    a = core.Unit(unit.to_string('ogip'), format='ogip')
    assert_allclose(a.decompose().scale, unit.decompose().scale, rtol=1e-2)
    try:
        b = core.Unit(unit.decompose().to_string('ogip'), format='ogip')
    except ValueError:  # skip mag: decomposes into dex, unknown to OGIP
        return
    assert_allclose(b.decompose().scale, unit.decompose().scale, rtol=1e-2)
Beispiel #2
0
def test_roundtrip_vo_unit(unit):
    a = core.Unit(unit.to_string('vounit'), format='vounit')
    assert_allclose(a.decompose().scale, unit.decompose().scale, rtol=1e-2)
    if unit not in (u.mag, u.dB):
        ud = unit.decompose().to_string('vounit')
        assert '  ' not in ud
        b = core.Unit(ud, format='vounit')
        assert_allclose(b.decompose().scale, unit.decompose().scale, rtol=1e-2)
Beispiel #3
0
    def check_roundtrip(self, unit, output_format=None):
        if output_format is None:
            output_format = self.format_
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')  # Same warning shows up multiple times
            s = unit.to_string(output_format)

        if s in self.deprecated_units:
            with pytest.warns(UnitsWarning, match='deprecated') as w:
                a = core.Unit(s, format=self.format_)
            assert len(w) == 1
        else:
            a = core.Unit(s, format=self.format_)  # No warning

        assert_allclose(a.decompose().scale, unit.decompose().scale, rtol=1e-9)
Beispiel #4
0
    def test_roundtrip(self, unit):
        if str(unit) in ('d', '0.001 Crab'):
            # Special-case day, which gets auto-converted to hours, and mCrab,
            # which the default check does not recognize as a deprecated unit.
            with pytest.warns(UnitsWarning):
                s = unit.to_string(self.format_)
                a = core.Unit(s, format=self.format_)
            assert_allclose(a.decompose().scale, unit.decompose().scale, rtol=1e-9)
        else:
            self.check_roundtrip(unit)
        if str(unit) in ('mag', 'byte', 'Crab'):
            # Skip mag and byte, which decompose into dex and bit, resp.,
            # both of which are unknown to OGIP, as well as Crab, which does
            # not decompose, and thus gives a deprecated unit warning.
            return

        power_of_ten = np.log10(unit.decompose().scale)
        if abs(power_of_ten - round(power_of_ten)) > 1e-3:
            ctx = pytest.warns(UnitsWarning, match='power of 10')
        elif str(unit) == '0.001 Crab':
            ctx = pytest.warns(UnitsWarning, match='deprecated')
        else:
            ctx = nullcontext()
        with ctx:
            self.check_roundtrip_decompose(unit)
Beispiel #5
0
def decompose_to_known_units(unit, func):
    """
    Partially decomposes a unit so it is only composed of units that
    are "known" to a given format.

    Parameters
    ----------
    unit : `~astropy.units.UnitBase` instance

    func : callable
        This function will be called to determine if a given unit is
        "known".  If the unit is not known, this function should raise a
        `ValueError`.

    Returns
    -------
    unit : `~astropy.units.UnitBase` instance
        A flattened unit.
    """
    from astropy.units import core
    if isinstance(unit, core.CompositeUnit):
        new_unit = core.Unit(unit.scale)
        for base, power in zip(unit.bases, unit.powers):
            new_unit = new_unit * decompose_to_known_units(base, func)**power
        return new_unit
    elif isinstance(unit, core.NamedUnit):
        try:
            func(unit)
        except ValueError:
            if isinstance(unit, core.Unit):
                return decompose_to_known_units(unit._represents, func)
            raise
        return unit
Beispiel #6
0
    def check_roundtrip(self, unit):
        with catch_warnings() as w:
            s = unit.to_string(self.format_)
            a = core.Unit(s, format=self.format_)

        if s in self.deprecated_units:
            assert w
            assert 'deprecated' in str(w[0])
        else:
            assert not w

        assert_allclose(a.decompose().scale, unit.decompose().scale, rtol=1e-9)
Beispiel #7
0
    def check_roundtrip(self, unit):
        with pytest.warns(None) as w:
            s = unit.to_string(self.format_)
            a = core.Unit(s, format=self.format_)

        if s in self.deprecated_units:
            assert len(w) == 3  # Same warning shows up multiple times
            for ww in w:
                assert issubclass(ww.category, UnitsWarning)
                assert 'deprecated' in str(ww.message)
        else:
            assert len(w) == 0

        assert_allclose(a.decompose().scale, unit.decompose().scale, rtol=1e-9)
Beispiel #8
0
def test_roundtrip_fits(unit):
    s = unit.to_string('fits')
    a = core.Unit(s, format='fits')
    assert_allclose(a.decompose().scale, unit.decompose().scale, rtol=1e-2)
Beispiel #9
0
def test_roundtrip(unit):
    a = core.Unit(unit.to_string('generic'), format='generic')
    b = core.Unit(unit.decompose().to_string('generic'), format='generic')
    assert_allclose(a.decompose().scale, unit.decompose().scale, rtol=1e-2)
    assert_allclose(b.decompose().scale, unit.decompose().scale, rtol=1e-2)
Beispiel #10
0
 def check_roundtrip_decompose(self, unit):
     ud = unit.decompose()
     s = ud.to_string(self.format_)
     assert '  ' not in s
     a = core.Unit(s, format=self.format_)
     assert_allclose(a.decompose().scale, ud.scale, rtol=1e-5)