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')
Exemple #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)