Beispiel #1
0
def test_constructor():

    # RA, Dec and L,B of the same point in the sky

    ra, dec = (125.6, -75.3)
    l, b = (288.44190139183564, -20.717313145391525)

    # This should throw an error as we are using Powerlaw instead of Powerlaw()
    with pytest.raises(RuntimeError):

        _ = ExtendedSource("my_source", Gaussian_on_sphere, Powerlaw)

    # This should throw an error because we should use a 2D function for the spatial shape
    with pytest.raises(RuntimeError):

        _ = ExtendedSource("my_source", Powerlaw(), Powerlaw())

    # Init with RA, Dec

    shape = Gaussian_on_sphere()
    source1 = ExtendedSource('my_source', shape, Powerlaw())
    shape.lon0 = ra * u.degree
    shape.lat0 = dec * u.degree

    assert source1.spatial_shape.lon0.value == ra
    assert source1.spatial_shape.lat0.value == dec

    # Verify that the position is free by default
    assert source1.spatial_shape.lon0.free
    assert source1.spatial_shape.lon0.free
Beispiel #2
0
    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)
Beispiel #3
0
def test_free_param():

    spectrum = Log_parabola()
    source = ExtendedSource("test_source",
                            spatial_shape=Gaussian_on_sphere(),
                            spectral_shape=spectrum)

    parameters = [
        spectrum.alpha, spectrum.beta, spectrum.piv, spectrum.K,
        source.spatial_shape.lat0, source.spatial_shape.lon0,
        source.spatial_shape.sigma
    ]

    for param in parameters:
        param.free = False

    assert len(source.free_parameters) == 0

    for i, param in enumerate(parameters):
        param.free = True
        assert len(source.free_parameters) == i + 1
Beispiel #4
0
def _get_extended_source(name="test_ext"):

    ext = ExtendedSource(name, Gaussian_on_sphere(), Powerlaw())

    return ext