コード例 #1
0
    def test_repr(self):
        model = UnitsMapping(((u.m, None), ), name='foo')
        assert repr(
            model) == f"<UnitsMapping((({repr(u.m)}, None),), name='foo')>"

        model = UnitsMapping(((u.m, None), ))
        assert repr(model) == f"<UnitsMapping((({repr(u.m)}, None),))>"
コード例 #2
0
    def test___init__(self):
        # Set values
        model = UnitsMapping(((u.m, None), ),
                             input_units_equivalencies='test_eqiv',
                             input_units_allow_dimensionless=True,
                             name='test')
        assert model._mapping == ((u.m, None), )
        assert model._input_units_strict == {'x': True}
        assert model.input_units_equivalencies == 'test_eqiv'
        assert model.input_units_allow_dimensionless == {'x': True}
        assert model.name == 'test'
        assert model._input_units == {'x': u.m}

        # Default values
        model = UnitsMapping(((u.K, None), ))
        assert model._mapping == ((u.K, None), )
        assert model._input_units_strict == {'x': True}
        assert model.input_units_equivalencies is None
        assert model.input_units_allow_dimensionless == {'x': False}
        assert model.name is None
        assert model._input_units == {'x': u.K}

        # Error
        with pytest.raises(ValueError) as err:
            UnitsMapping(((u.m, None), (u.m, u.K)))
        assert str(err.value) == \
            "If one return unit is None, then all must be None"
コード例 #3
0
def test_repr():
    model = UnitsMapping(((u.m, None), ))
    assert repr(model) == """<UnitsMapping(((Unit("m"), None),))>"""

    model = UnitsMapping(((u.m, None), ), name="foo")
    assert repr(
        model) == """<UnitsMapping(((Unit("m"), None),), name='foo')>"""
コード例 #4
0
def test_custom_inputs_and_outputs():
    model = UnitsMapping(((u.m, u.dimensionless_unscaled), ))

    model.inputs = ("foo", )
    model.outputs = ("bar", )

    assert model.inputs == ("foo", )
    assert model.input_units == {"foo": u.m}
    assert model.outputs == ("bar", )
コード例 #5
0
def test_with_allow_dimensionless(tmpdir):
    m = UnitsMapping(((u.m, u.dimensionless_unscaled), (u.s, u.Hz)),
                     input_units_allow_dimensionless=True)
    assert_model_roundtrip(m, tmpdir)

    m = UnitsMapping(((u.m, u.dimensionless_unscaled), (u.s, u.Hz)),
                     input_units_allow_dimensionless={
                         "x0": True,
                         "x1": False
                     })
    assert_model_roundtrip(m, tmpdir)
コード例 #6
0
    def test_evaluate(self):
        model = UnitsMapping(((u.m, None), ))
        assert model(10 * u.m) == 10

        model = UnitsMapping(((u.m, u.K), ))
        assert model(10 * u.m) == 10 * u.K

        model = UnitsMapping(((u.m, None), (u.K, None)), )
        assert model(10 * u.m, 20 * u.K) == (10, 20)

        model = UnitsMapping(((u.m, u.K), (u.K, u.m)), )
        assert model(10 * u.m, 20 * u.K) == (10 * u.K, 20 * u.m)
コード例 #7
0
def test_allow_dimensionless():
    model = UnitsMapping(((u.m, u.dimensionless_unscaled), ))

    with pytest.raises(UnitsError):
        model(10)

    model = UnitsMapping(((u.m, u.dimensionless_unscaled), ),
                         input_units_allow_dimensionless=True)
    result = model(10)
    assert isinstance(result, Quantity)
    assert result.value == 10
    assert result.unit == u.dimensionless_unscaled
コード例 #8
0
def test_equivalencies():
    model = UnitsMapping(((u.m, u.dimensionless_unscaled), ))

    with pytest.raises(UnitsError):
        model(Quantity(100, u.Hz))

    model = UnitsMapping(
        ((u.m, u.dimensionless_unscaled), ),
        input_units_equivalencies={"x": equivalencies.spectral()})

    result = model(Quantity(100, u.Hz))
    assert result.unit == u.dimensionless_unscaled
コード例 #9
0
def test_add_units():
    model = UnitsMapping(((u.dimensionless_unscaled, u.m), ))

    for value in [10, Quantity(10), np.arange(10), Quantity(np.arange(10))]:
        result = model(value)
        assert isinstance(result, Quantity)
        assert np.all(result.value == value)
        assert result.unit == u.m

    with pytest.raises(UnitsError):
        model(Quantity(10, u.s))
コード例 #10
0
def test_remove_quantity():
    model = UnitsMapping(((u.m, None), ))

    result = model(Quantity(10, u.m))
    assert result == 10

    result = model(Quantity(1000, u.cm))
    assert result == 10

    with pytest.raises(UnitsError):
        model(10)

    with pytest.raises(UnitsError):
        model(Quantity(10))

    # The model shouldn't allow a mixture of None and non-None
    # output units.
    with pytest.raises(
            ValueError,
            match=r"If one return unit is None, then all must be None"):
        UnitsMapping(((u.m, None), (u.s, u.dimensionless_unscaled)))
コード例 #11
0
def test_properties():
    model = UnitsMapping(
        ((u.dimensionless_unscaled, u.m), (u.dimensionless_unscaled, u.s)))
    assert model.n_inputs == 2
    assert model.n_outputs == 2
    assert model.inputs == ("x0", "x1")
    assert model.outputs == ("x0", "x1")
    assert model.input_units == {
        "x0": u.dimensionless_unscaled,
        "x1": u.dimensionless_unscaled
    }
    assert model.mapping == ((u.dimensionless_unscaled, u.m),
                             (u.dimensionless_unscaled, u.s))
コード例 #12
0
def test_remove_units():
    model = UnitsMapping(((u.m, u.dimensionless_unscaled), ))

    result = model(Quantity(10, u.m))
    assert isinstance(result, Quantity)
    assert result.value == 10
    assert result.unit == u.dimensionless_unscaled

    result = model(Quantity(1000, u.cm))
    assert isinstance(result, Quantity)
    assert result.value == 10
    assert result.unit == u.dimensionless_unscaled

    with pytest.raises(UnitsError):
        model(10)

    with pytest.raises(UnitsError):
        model(Quantity(10))
コード例 #13
0
def test_with_equivalencies(tmpdir):
    m = UnitsMapping(
        ((u.m, u.dimensionless_unscaled), ),
        input_units_equivalencies={"x": u.equivalencies.spectral()})
    assert_model_roundtrip(m, tmpdir)
コード例 #14
0
def test_accept_any_units(tmpdir):
    m = UnitsMapping(((None, u.m), ))
    assert_model_roundtrip(m, tmpdir)
コード例 #15
0
def test_remove_units(tmpdir):
    m = UnitsMapping(((u.m, None), ))
    assert_model_roundtrip(m, tmpdir)
コード例 #16
0
def test_basic(tmpdir):
    m = UnitsMapping(((u.m, u.dimensionless_unscaled), ))
    assert_model_roundtrip(m, tmpdir)