Exemplo n.º 1
0
def test_constructor_with_delta():

    p = Parameter('test_parameter', 1.0, delta=0.3)

    assert p.delta == 0.3

    p.display()
Exemplo n.º 2
0
def test_set_units():

    p = Parameter('test_parameter', 1.0, unit=u.keV)

    p.value = 3.0 * u.MeV

    assert p.value == 3000.0

    with pytest.raises(u.UnitConversionError):

        p.value = 3.0 * u.cm

    with pytest.raises(CannotConvertValueToNewUnits):

        p.unit = u.cm

    with pytest.raises(CannotConvertValueToNewUnits):

        p.unit = u.dimensionless_unscaled

    p.unit = u.MeV

    assert p.unit == u.MeV

    p.display()
Exemplo n.º 3
0
def test_constructor_with_units():

    p = Parameter('test_parameter', 1.0, unit=u.keV)

    assert p.unit == u.keV

    p.display()
Exemplo n.º 4
0
def test_constructor_complete():

    p = Parameter(
        'test_parameter',
        1.0,
        min_value=-5.0,
        max_value=5.0,
        delta=0.2,
        desc='test',
        free=False,
        unit=u.MeV,
        prior=Uniform_prior(),
        is_normalization=True,
    )

    assert p.min_value == -5.0
    assert p.max_value == 5.0
    assert p.value == 1.0
    assert p.delta == 0.2
    assert p.name == 'test_parameter'
    assert p.description == 'test'
    assert p.fix == True
    assert p.free == False
    assert p.has_prior() == True

    assert p.unit == u.MeV

    assert p.is_normalization

    p.display()
Exemplo n.º 5
0
def test_constructor_with_boundaries():

    p = Parameter('test_parameter', 1.0, min_value=-5, max_value=5)

    assert p.min_value == -5
    assert p.max_value == 5

    p.display()
Exemplo n.º 6
0
def test_set_within_bounds_units():

    p = Parameter('test_parameter',1.0 * u.keV, min_value = -2.0 * u.MeV, max_value = 2.0 * u.MeV, unit=u.keV)

    p.value = 1.2 * u.MeV

    assert p.value == 1200.0

    p.display()
Exemplo n.º 7
0
def test_set_no_units():

    p = Parameter('test_parameter', 1.0)

    p.value = 25.4

    assert p.value == 25.4

    p.display()
Exemplo n.º 8
0
def test_set_delta_nounits():

    p = Parameter('test_parameter', 1.0)

    p.delta = 0.5

    assert p.delta == 0.5

    p.display()
Exemplo n.º 9
0
def test_set_delta_units():

    p = Parameter('test_parameter', 1.0, unit='GeV')

    p.delta = 500 * u.MeV

    assert p.delta == 0.5

    p.display()
Exemplo n.º 10
0
def test_set_within_bounds_no_units():

    p = Parameter('test_parameter', 1.0, min_value=-2.0, max_value=2.0)

    p.value = 1.5

    assert p.value == 1.5

    p.display()
Exemplo n.º 11
0
def test_set_bounds_units():

    p = Parameter('test_parameter', 1.0 * u.keV)

    p.bounds = (-2.0 * u.MeV, 2.0 * u.eV)

    assert p.min_value == -2000
    assert p.max_value == 2e-3

    p.display()
Exemplo n.º 12
0
def test_duplicate():

    p1 = Parameter('test_parameter', 1.0, min_value=-5.0, max_value=5.0, delta=0.2, desc='test', free=False, unit='MeV')

    p2 = p1.duplicate()

    assert p1.to_dict() == p2.to_dict()

    p1.display()
    p2.display()
Exemplo n.º 13
0
def test_set_bounds_nounits():

    p = Parameter('test_parameter', 1.0)

    p.bounds = (-2.0, 2.0)

    assert p.min_value == -2.0
    assert p.max_value == 2.0

    p.display()
Exemplo n.º 14
0
def test_internal_setting():

    p = Parameter('test_parameter', 1.0)

    p._set_internal_value(5.)

    p._get_internal_value()

    p.display()

    p._get_internal_min_value()
    p._get_internal_max_value()
Exemplo n.º 15
0
def test_set_outside_bounds_units():

    p = Parameter('test_parameter', 1.0 * u.keV, min_value = -2.0 * u.MeV, max_value = 2.0 * u.MeV, unit=u.keV)

    with pytest.raises(SettingOutOfBounds):

        p.value = -10.0 * u.MeV

    with pytest.raises(SettingOutOfBounds):

        p.value = 10.0 * u.MeV

    p.display()
Exemplo n.º 16
0
def test_set_outside_bounds_no_units():

    p = Parameter('test_parameter', 1.0, min_value=-2.0, max_value=2.0)

    with pytest.raises(SettingOutOfBounds):

        p.value = -10.0

    with pytest.raises(SettingOutOfBounds):

        p.value = 10.0

    p.display()
Exemplo n.º 17
0
def test_set_bounds_nounits():

    p = Parameter('test_parameter', 1.0)

    p.bounds = (-2.0 ,2.0)

    assert p.min_value == -2.0
    assert p.max_value == 2.0

    p.display()

    with pytest.warns(RuntimeWarning):

        p.value = 1.0
        p.min_value = 2.0
Exemplo n.º 18
0
def test_set_auxiliary_variable():

    p1 = Parameter('test_parameter',
                   1.0,
                   min_value=-5.0,
                   max_value=5.0,
                   delta=0.2,
                   desc='test',
                   free=False,
                   unit='MeV')

    x = Parameter('aux_variable', 1.0)

    # ax + b

    law = Line()
    law.a = 2.0
    law.b = 1.0

    p1.add_auxiliary_variable(x, law)

    assert p1.has_auxiliary_variable() == True

    assert p1.value == 3.0

    assert p1.free == False

    x.value = 4.0

    assert p1.value == 6.0

    # Check that assigning to the parameter doesn't produce any effect
    p1.value = -1.0

    assert p1.value == 6.0

    # Now check errors reporting
    with pytest.raises(AttributeError):

        p1.add_auxiliary_variable(1.0, law)

    # Now add it twice to verify that it overwrites it
    p1.add_auxiliary_variable(x, law)
    p1.add_auxiliary_variable(x, law)

    p1.display()
Exemplo n.º 19
0
def test_conflicting_units_in_initial_value_and_unit_keyword():

    p = Parameter('test_parameter', 1.0 * u.keV, desc='Description', unit=u.MeV)

    assert p.min_value is None
    assert p.max_value is None
    assert p.value == 1.0e-3
    assert isinstance(p.delta, float)
    assert p.name == 'test_parameter'
    assert p.description == 'Description'
    assert p.fix == False
    assert p.free == True
    assert p.has_prior() == False
    assert p.prior is None

    assert p.unit == u.MeV

    p.display()
Exemplo n.º 20
0
def test_default_constructor_units():

    p = Parameter('test_parameter', 1.0 * u.keV, desc='Description')

    assert p.min_value is None
    assert p.max_value is None
    assert p.value == 1.0
    assert isinstance(p.delta, float)
    assert p.name == 'test_parameter'
    assert p.description == 'Description'
    assert p.fix == False
    assert p.free == True
    assert p.has_prior() == False
    assert p.prior is None

    assert p.unit == u.keV

    p.display()
Exemplo n.º 21
0
def test_default_constructor():

    p = Parameter('test_parameter', 1.0, desc='Description')

    assert p.min_value is None
    assert p.max_value is None
    assert p.value == 1.0
    assert isinstance(p.delta, float)
    assert p.name == 'test_parameter'
    assert p.description == 'Description'
    assert p.fix == False
    assert p.free == True
    assert p.has_prior() == False
    assert p.prior is None

    assert p.unit == u.dimensionless_unscaled

    # Test that we cannot call a parameter with a name with spaces in it
    with pytest.raises(AssertionError):

        _ = Parameter('test parameter 2', 1.0)

    # Test some failures cases
    with pytest.raises(ValueError):

        _ = Parameter('test', 'pippo')

    with pytest.raises(ValueError):
        _ = Parameter('test', 1.0, min_value='a')

    with pytest.raises(ValueError):

        _ = Parameter('test', 1.0, max_value='b')

    with pytest.raises(TypeError):

        _ = Parameter('test', 1.0, delta='b')

    p.display()