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 = 2.0 law.b = 1.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
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
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_time_domain_integration(): po = Powerlaw() default_powerlaw = Powerlaw() src = PointSource("test", ra=0.0, dec=0.0, spectral_shape=po) m = Model(src) # type: model.Model # Add time independent variable time = IndependentVariable("time", 0.0, u.s) m.add_independent_variable(time) # Now link one of the parameters with a simple line law line = Line() line.a = 0.0 line.b = 0.0 m.link(po.index, time, line) # Test the display just to make sure it doesn't crash m.display() # Now test the average with the integral energies = np.linspace(1, 10, 10) results = m.get_point_source_fluxes(0, energies, tag=(time, 0, 10)) # type: np.ndarray assert np.all(results == 1.0) # Now test the linking of the normalization, first with a constant then with a line with a certain # angular coefficient m.unlink(po.index) po.index.value = default_powerlaw.index.value line2 = Line() line2.a = 1.0 line2.b = 0.0 m.link(po.K, time, line2) time.value = 1.0 results = m.get_point_source_fluxes(0, energies, tag=(time, 0, 10)) assert np.allclose(results, default_powerlaw(energies)) # Now make something actually happen line2.a = 1.0 line2.b = 1.0 results = m.get_point_source_fluxes(0, energies, tag=(time, 0, 10)) # type: np.ndarray # Compare with analytical result def F(x): return line2.b.value / 2.0 * x**2 + line2.a.value * x effective_norm = old_div((F(10) - F(0)), 10.0) expected_results = default_powerlaw( energies) * effective_norm # type: np.ndarray assert np.allclose(expected_results, results)
def test_function_composition(): Test_function = get_a_function_class() line = Test_function() powerlaw = Powerlaw() composite = powerlaw + line composite.set_units(u.keV, 1.0 / (u.keV * u.cm**2 * u.s)) for x in ([1, 2, 3, 4], [1, 2, 3, 4] * u.keV, 1.0, np.array([1.0, 2.0, 3.0, 4.0])): assert np.all(composite(x) == line(x) + powerlaw(x)) # Test - po = Powerlaw() li = Line() composite = po - li assert composite(1.0) == (po(1.0) - li(1.0)) # test * composite = po * li assert composite(2.25) == po(2.25) * li(2.25) # test / composite = po / li assert composite(2.25) == po(2.25) / li(2.25) # test .of composite = po.of(li) assert composite(2.25) == po(li(2.25)) # test power composite = po**li assert composite(2.25) == po(2.25)**li(2.25) # test negation neg_po = -po assert neg_po(2.25) == -po(2.25) # test abs new_li = Line() new_li.b = -10.0 abs_new_li = abs(new_li) assert new_li(1.0) < 0 assert abs_new_li(1.0) == abs(new_li(1.0)) # test rpower composite = 2.0**new_li assert composite(2.25) == 2.0**(new_li(2.25)) # test multiplication by a number composite = 2.0 * po assert composite(2.25) == 2.0 * po(2.25) # Number divided by composite = 1.0 / li assert composite(2.25) == 1.0 / li(2.25) # Composite of composite composite = po * li + po - li + 2 * po / li assert composite(2.25) == po(2.25) * li(2.25) + \ po(2.25) - li(2.25) + 2*po(2.25) / li(2.25) print(composite)