def test_constructor_with_many_point_sources(): # Test with 200 point sources many_p_sources = [_get_point_source("pts_source%i" %x) for x in range(200)] m = Model(*many_p_sources) assert m.get_number_of_point_sources()==200
def test_constructor_with_many_point_sources(): # Test with 200 point sources many_p_sources = map(lambda x: _get_point_source("pts_source%i" % x), range(200)) m = Model(*many_p_sources) assert m.get_number_of_point_sources() == 200
def test_constructor_with_many_extended_sources(): # Test with one extended source ext = _get_extended_source() _ = Model(ext) # Test with 200 extended sources many_e_sources = [_get_extended_source("ext_source%i" %x) for x in range(200)] m = Model(*many_e_sources) assert m.get_number_of_extended_sources() == 200
def test_default_constructor(): # Test that we cannot build a model with no sources with pytest.raises(AssertionError): _ = Model()
def test_constructor_duplicated_sources(): # Test with one point source pts = _get_point_source() with pytest.raises(DuplicatedNode): m = Model(pts, pts)
def test_one(class_type, name): print("testing %s ..." % name) shape = class_type() source = ExtendedSource('test_source_%s' % name, spatial_shape=shape, components=[c1, c2]) if name != "SpatialTemplate_2D": shape.lon0 = ra * u.degree shape.lat0 = dec * u.degree else: make_test_template(ra, dec, "__test.fits") shape.load_file("__test.fits") shape.K = 1.0 assert np.all( source.spectrum.component1([1, 2, 3] * u.keV) == po1([1, 2, 3] * u.keV)) assert np.all( source.spectrum.component2([1, 2, 3] * u.keV) == po2([1, 2, 3] * u.keV)) one = source.spectrum.component1([1, 2, 3] * u.keV) two = source.spectrum.component2([1, 2, 3] * u.keV) #check spectral components assert np.all( np.abs(one + two - source.get_spatially_integrated_flux([1, 2, 3] * u.keV)) == 0) #check spectral and spatial components #spatial = source.spatial_shape( ra*u.deg,dec*u.deg ) spatial = source.spatial_shape([ra, ra, ra] * u.deg, [dec, dec, dec] * u.deg) total = source([ra, ra, ra] * u.deg, [dec, dec, dec] * u.deg, [1, 2, 3] * u.keV) spectrum = one + two assert np.all(np.abs(total - spectrum * spatial) == 0) total = source([ra * 1.01] * 3 * u.deg, [dec * 1.01] * 3 * u.deg, [1, 2, 3] * u.keV) spectrum = one + two spatial = source.spatial_shape([ra * 1.01] * 3 * u.deg, [dec * 1.01] * 3 * u.deg) assert np.all(np.abs(total - spectrum * spatial) == 0) model = Model(source) new_model = clone_model(model) new_total = new_model['test_source_%s' % name]( [ra * 1.01] * 3 * u.deg, [dec * 1.01] * 3 * u.deg, [1, 2, 3] * u.keV) assert np.all(np.abs(total - new_total) == 0)
def test_default_constructor(): # Test that we can build a model with no sources m = Model() assert len(m.sources) == 0 assert len(m.point_sources) == 0 assert len(m.extended_sources) == 0
def test_constructor_with_mix(): many_p_sources = [_get_point_source("pts_source%i" % x) for x in range(200)] many_e_sources = [_get_extended_source("ext_source%i" % x) for x in range(200)] many_part_sources = [_get_particle_source("part_source%i" % x) for x in range(200)] all_sources = [] all_sources.extend(many_p_sources) all_sources.extend(many_e_sources) all_sources.extend(many_part_sources) m = Model(*all_sources) assert m.get_number_of_point_sources() == 200 assert m.get_number_of_extended_sources() == 200 assert m.get_number_of_particle_sources() == 200
def test_input_output_with_complex_functions(): my_particle_distribution = Powerlaw() my_particle_distribution.index = -1.52 electrons = ParticleSource('electrons', distribution_shape=my_particle_distribution) # Now set up the synch. spectrum for our source and the source itself synch_spectrum = _ComplexTestFunction() # Use the particle distribution we created as source for the electrons # producing synch. emission synch_spectrum.particle_distribution = my_particle_distribution synch_source = PointSource('synch_source', ra=12.6, dec=-13.5, spectral_shape=synch_spectrum) my_model = Model(electrons, synch_source) my_model.display() my_model.save("__test.yml") new_model = load_model("__test.yml") assert len(new_model.sources) == len(my_model.sources) assert my_particle_distribution.index.value == new_model.electrons.spectrum.main.shape.index.value
def test_constructor_1source(): # Test with one point source pts = _get_point_source() m = Model(pts) # Test with a point source with an invalid name with pytest.raises(AssertionError): _ = PointSource("name", 0, 0, Powerlaw()) assert len(m.sources) == 1
def test_one(class_type): instance = class_type() if not instance.is_prior: # if we have fixed x_units then we will use those # in the test if instance.has_fixed_units(): x_unit_to_use = instance.fixed_units[0] else: x_unit_to_use = u.keV # Use the function as a spectrum ps = PointSource("test", 0, 0, instance) if instance.name in ["Synchrotron", "_ComplexTestFunction"]: particleSource = ParticleSource("particles", Powerlaw()) instance.set_particle_distribution( particleSource.spectrum.main.shape) result = ps(1.0) assert isinstance(result, float) result = ps(1.0 * x_unit_to_use) assert isinstance(result, u.Quantity) result = ps(np.array([1, 2, 3]) * x_unit_to_use) assert isinstance(result, u.Quantity) if instance.name in ["Synchrotron", "_ComplexTestFunction"]: model = Model(particleSource, ps) else: model = Model(ps) new_model = clone_model(model) new_result = new_model["test"](np.array([1, 2, 3]) * x_unit_to_use) assert np.all(new_result == result) model.save("__test.yml", overwrite=True) new_model = load_model("__test.yml") new_result = new_model["test"](np.array([1, 2, 3]) * x_unit_to_use) assert np.all(new_result == result) else: print('Skipping prior function')
def __init__(self): # 2 point sources and 3 ext sources pts1 = _get_point_source("one") # 5 params, 2 free pts2 = _get_point_source_gal("two") pts3 = _get_point_source_composite('three') ext1 = _get_extended_source("ext_one") # 6 params, 5 free ext2 = _get_extended_source("ext_two") ext3 = _get_extended_source("ext_three") # Two particle sources part1 = _get_particle_source("part_one") part2 = _get_particle_source("part_two") self._m = Model(pts1, pts2, pts3, ext1, ext2, ext3, part1, part2)
def test_display(): mg = ModelGetter() m = mg.model m.display() m.display(complete=True) # Now display a model without free parameters m = Model(PointSource("test", 0.0, 0.0, Powerlaw())) for parameter in m.parameters.values(): parameter.fix = True m.display() # Now display a model without fixed parameters (very unlikely) for parameter in m.parameters.values(): parameter.free = True m.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 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 = 0.0 line2.b = 1.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.a.value / 2.0 * x**2 + line2.b.value * x effective_norm = (F(10) - F(0)) / 10.0 expected_results = default_powerlaw( energies) * effective_norm # type: np.ndarray assert np.allclose(expected_results, results)