示例#1
0
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
示例#2
0
    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')