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()
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()
def test_callback(): p1 = Parameter('test_parameter', 1.0, min_value=-5.0, max_value=5.0, delta=0.2, desc='test', free=False, unit='MeV') class Callback(object): def __init__(self): self._control_value = None def __call__(self, p): assert p == p1 self._control_value = p1.value working_callback = Callback() p1.add_callback(working_callback) # Test the callback p1.value = 2.0 assert working_callback._control_value == p1.value def not_working_callback(): # Wrong calling sequence pass p1.add_callback(not_working_callback) # This should work because we do not change the parameter, so the callback # does not get called p1.value = 2.0 # This should instead raise because we do change the value with pytest.raises(NotCallableOrErrorInCall): p1.value = 3.0 p1.empty_callbacks() assert len(p1._callbacks) == 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 m_reloaded.sources.keys() == 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 m_reloaded.sources.keys() == 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
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()
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()
def test_set_no_units(): p = Parameter('test_parameter', 1.0) p.value = 25.4 assert p.value == 25.4 p.display()
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()
def test_set_remove_minimum(): p1 = Parameter('test_parameter', 1.0, min_value=-5.0, max_value=5.0, delta=0.2, desc='test', free=False, unit='MeV') p1.remove_minimum() assert p1.min_value == None p1.value = -1000.0 assert p1.value == -1000.0
def test_remove_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 = 1.0 law.b = 2.0 p1.add_auxiliary_variable(x, law) assert p1.value == 3.0 x.value = 4.0 assert p1.value == 6.0 p1.remove_auxiliary_variable() assert p1.has_auxiliary_variable() == False p1.value = -1.0 assert p1.value == -1.0 with pytest.warns(RuntimeWarning): p1.remove_auxiliary_variable()
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