예제 #1
0
def test_get_randomized_value():

    # Test randomization no boundaries (normal distribution)
    p1 = Parameter('test_parameter', 1.0)

    val2 = p1.get_randomized_value(0.1)

    assert isinstance(val2, float)

    # Test the randomized value with truncated normal, i.e., with boundaries

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

    val1 = p2.get_randomized_value(0.1)

    assert p2.min_value <= val1 <= p2.max_value

    # Test the same but with a large variance

    val1 = p2.get_randomized_value(10.0)

    assert p2.min_value <= val1 <= p2.max_value

    p2.min_value = None

    val1 = p2.get_randomized_value(10.0)
    assert val1 <= p2.max_value

    p2.min_value = -5.0
    p2.max_value = None

    val1 = p2.get_randomized_value(10.0)
    assert val1 >= p2.min_value
예제 #2
0
def test_prior():

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

    my_prior = Uniform_prior()

    p1.prior = my_prior

    assert my_prior == p1.prior

    custom_prior = lambda x: x**2

    with pytest.raises(NotCallableOrErrorInCall):

        p1.prior = custom_prior

    invalid_prior = lambda x, y: x * y

    with pytest.raises(NotCallableOrErrorInCall):

        p1.prior = invalid_prior

    # Test the set_uninformative_prior method
    p1.min_value = None
    p1.max_value = 100.0

    with pytest.raises(ParameterMustHaveBounds):

        p1.set_uninformative_prior(Uniform_prior)

    p1.min_value = 0.0
    p1.max_value = None

    with pytest.raises(ParameterMustHaveBounds):

        p1.set_uninformative_prior(Uniform_prior)

    p1.min_value = 0.0
    p1.max_value = 100.0

    p1.set_uninformative_prior(Uniform_prior)

    # Log-uniform cannot be used if minimum is 0.0

    with pytest.raises(SettingOutOfBounds):

        p1.set_uninformative_prior(Log_uniform_prior)

    p1.min_value = 1.0
    p1.set_uninformative_prior(Log_uniform_prior)
예제 #3
0
def test_internal_delta():

    p = Parameter('test_parameter',1.0)
    p.min_value = None
    p.max_value = None

    p._get_internal_delta()


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

    p._get_internal_delta()

    with pytest.raises(AssertionError):
        p = Parameter('test_parameter', 1.0, min_value=-1., max_value=5.0, delta=0.2, desc='test',
                  free=False, unit=u.MeV, prior=Uniform_prior(), is_normalization=True, transformation=LogarithmicTransformation())


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



    p._get_internal_delta()
예제 #4
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