Example #1
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()
Example #2
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()
Example #3
0
def test_input_output_with_external_parameters():

    mg = ModelGetter()
    m = mg.model

    # Create an external parameter
    fake_parameter = Parameter("external_parameter",
                               1.0,
                               min_value=-1.0,
                               max_value=1.0,
                               free=True)

    # Link as equal (default)
    m.add_external_parameter(fake_parameter)

    # Save model

    temp_file = "__test.yml"

    m.save(temp_file, overwrite=True)

    # Now reload it again
    m_reloaded = load_model(temp_file)

    os.remove(temp_file)

    # Check that all sources have been recovered
    assert list(m_reloaded.sources.keys()) == list(m.sources.keys())

    # Check that the external parameter have been recovered
    assert "external_parameter" in m_reloaded

    # Remove external parameter
    m.remove_external_parameter("external_parameter")

    # Add a prior to the external parameter
    fake_parameter.prior = Uniform_prior()

    fake_parameter.value = -0.1

    m.add_external_parameter(fake_parameter)

    # Save model

    temp_file = "__test.yml"

    m.save(temp_file, overwrite=True)

    # Now reload it again
    m_reloaded = load_model(temp_file)

    os.remove(temp_file)

    # Check that all sources have been recovered
    assert list(m_reloaded.sources.keys()) == list(m.sources.keys())

    # Check that the external parameter have been recovered
    assert "external_parameter" in m_reloaded

    assert m.external_parameter.value == m_reloaded.external_parameter.value
Example #4
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)
Example #5
0
def test_input_output_basic():

    mg = ModelGetter()
    m = mg.model

    temp_file = "__test.yml"

    m.save(temp_file, overwrite=True)

    # Now reload it again
    m_reloaded = load_model(temp_file)

    # Check that all sources have been recovered
    assert list(m_reloaded.sources.keys()) == list(m.sources.keys())

    os.remove(temp_file)

    # Now check that saving twice with the same name issues an exception if overwrite is not True
    m.save(temp_file, overwrite=True)

    with pytest.raises(ModelFileExists):

        m.save(temp_file, overwrite=False)

    # Try to write in an impossible location and verify that we throw an exception
    with pytest.raises(CannotWriteModel):

        m.save("/dev/null/ciaps", overwrite=True)

    # Add a prior to one of the parameters
    list(m.free_parameters.values())[0].prior = Uniform_prior()

    m.save(temp_file, overwrite=True)

    new_m = load_model(temp_file)

    assert (list(m.free_parameters.values())[0].prior.to_dict() == list(
        new_m.free_parameters.values())[0].prior.to_dict())

    os.remove(temp_file)

    # Free and fix parameters
    m.one.position.ra.free = True
    m.two.spectrum.main.shape.K.fix = True

    m.save(temp_file, overwrite=True)

    new_m = load_model(temp_file)

    assert new_m.one.position.ra.free == True
    assert new_m.two.spectrum.main.shape.K.fix == True

    os.remove(temp_file)
Example #6
0
def test_links_and_pickle():

    import pickle

    p_orig = 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())

    # Test the linkinking and pickle

    # Add a link
    x = Parameter('aux_variable', 1.0)

    # ax + b

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

    p_orig.add_auxiliary_variable(x, law)

    # Now pickle and unpickle

    d = pickle.dumps(p_orig)

    p = pickle.loads(d)

    assert p.has_auxiliary_variable() == True

    assert p.value == 3.0

    assert p.free == False

    p.auxiliary_variable[0].value = 4.0

    assert p.value == 6.0

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

    assert p.value == 6.0
Example #7
0
def test_pickle():

    from astromodels.core.cpickle_compatibility_layer import cPickle

    p_orig = 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())

    # Add a callback

    working_callback = Callback()

    p_orig.add_callback(working_callback)

    # Now pickle and unpickle

    d = cPickle.dumps(p_orig)

    p = cPickle.loads(d)

    # Check that everything is fine

    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

    # Test the callback
    p.value = 2.0

    callback = p.get_callbacks()[0]

    assert callback._control_value == p.value
Example #8
0
def test_remove_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 p1.has_prior() == True

    # Now remove it
    p1.prior = None

    assert p1.has_prior() == False