예제 #1
0
def test_spectrum_addition():
    ebounds = ChannelSet.from_list_of_edges(np.array([0, 1, 2, 3, 4, 5]))
    ebounds_different = ChannelSet.from_list_of_edges(
        np.array([0, 1, 2, 3, 4, 5]))

    obs_spectrum_1 = BinnedSpectrum(counts=np.ones(len(ebounds)),
                                    count_errors=np.ones(len(ebounds)),
                                    exposure=1,
                                    ebounds=ebounds,
                                    is_poisson=False)
    obs_spectrum_2 = BinnedSpectrum(counts=np.ones(len(ebounds)),
                                    count_errors=np.ones(len(ebounds)),
                                    exposure=2,
                                    ebounds=ebounds,
                                    is_poisson=False)
    obs_spectrum_incompatible = BinnedSpectrum(counts=np.ones(len(ebounds)),
                                               count_errors=np.ones(
                                                   len(ebounds)),
                                               exposure=2,
                                               ebounds=ebounds_different,
                                               is_poisson=False)

    spectrum_addition(obs_spectrum_1, obs_spectrum_2,
                      obs_spectrum_incompatible, lambda x, y: x + y,
                      addition_proof_simple)
    spectrum_addition(obs_spectrum_1, obs_spectrum_2,
                      obs_spectrum_incompatible,
                      lambda x, y: x.add_inverse_variance_weighted(y),
                      addition_proof_weighted)
예제 #2
0
def test_spectrum_addition_poisson():
    ebounds = ChannelSet.from_list_of_edges(np.array([0, 1, 2, 3, 4, 5]))
    ebounds_different = ChannelSet.from_list_of_edges(
        np.array([0, 1, 2, 3, 4, 5]))

    obs_spectrum_1 = BinnedSpectrum(counts=np.ones(len(ebounds)),
                                    exposure=1,
                                    ebounds=ebounds,
                                    is_poisson=True)
    obs_spectrum_2 = BinnedSpectrum(counts=np.ones(len(ebounds)),
                                    exposure=2,
                                    ebounds=ebounds,
                                    is_poisson=True)
    obs_spectrum_incompatible = BinnedSpectrum(
        counts=np.ones(len(ebounds_different)),
        exposure=2,
        ebounds=ebounds,
        is_poisson=True,
    )

    spectrum_addition(
        obs_spectrum_1,
        obs_spectrum_2,
        obs_spectrum_incompatible,
        lambda x, y: x + y,
        addition_proof_simple,
    )
예제 #3
0
def test_spectrum_addition_poisson():
    ebounds = ChannelSet.from_list_of_edges(np.array([0,1,2,3,4,5]))
    ebounds_different = ChannelSet.from_list_of_edges(np.array([0,1,2,3,4,5]))

    obs_spectrum_1 = BinnedSpectrum(counts=np.ones(len(ebounds)),exposure=1,ebounds=ebounds, is_poisson=True)
    obs_spectrum_2 = BinnedSpectrum(counts=np.ones(len(ebounds)),exposure=2,ebounds=ebounds, is_poisson=True)
    obs_spectrum_incompatible = BinnedSpectrum(counts=np.ones(len(ebounds_different)),exposure=2,ebounds=ebounds, is_poisson=True)
    
    spectrum_addition(obs_spectrum_1,obs_spectrum_2,obs_spectrum_incompatible,lambda x,y:x+y,addition_proof_simple)
예제 #4
0
def test_spectrum_addition():
    ebounds = ChannelSet.from_list_of_edges(np.array([0,1,2,3,4,5]))
    ebounds_different = ChannelSet.from_list_of_edges(np.array([0,1,2,3,4,5]))

    obs_spectrum_1 = BinnedSpectrum(counts=np.ones(len(ebounds)),count_errors=np.ones(len(ebounds)),exposure=1,ebounds=ebounds, is_poisson=False)
    obs_spectrum_2 = BinnedSpectrum(counts=np.ones(len(ebounds)),count_errors=np.ones(len(ebounds)),exposure=2,ebounds=ebounds, is_poisson=False)
    obs_spectrum_incompatible = BinnedSpectrum(counts=np.ones(len(ebounds)),count_errors=np.ones(len(ebounds)),exposure=2,ebounds=ebounds_different, is_poisson=False)

    spectrum_addition(obs_spectrum_1,obs_spectrum_2,obs_spectrum_incompatible,lambda x,y:x+y,addition_proof_simple)
    spectrum_addition(obs_spectrum_1,obs_spectrum_2,obs_spectrum_incompatible,lambda x,y:x.add_inverse_variance_weighted(y),addition_proof_weighted)
예제 #5
0
def test_dispersion_spectrum_addition(loaded_response):

    rsp = loaded_response
    ebounds = ChannelSet.from_instrument_response(rsp)

    obs_spectrum_1 = BinnedSpectrumWithDispersion(
        counts=np.ones(len(ebounds)),
        count_errors=np.ones(len(ebounds)),
        exposure=1,
        response=rsp,
        is_poisson=False,
    )
    obs_spectrum_2 = BinnedSpectrumWithDispersion(
        counts=np.ones(len(ebounds)),
        count_errors=np.ones(len(ebounds)),
        exposure=2,
        response=rsp,
        is_poisson=False,
    )
    obs_spectrum_incompatible = None

    spectrum_addition(
        obs_spectrum_1,
        obs_spectrum_2,
        obs_spectrum_incompatible,
        lambda x, y: x + y,
        addition_proof_simple,
    )
    spectrum_addition(
        obs_spectrum_1,
        obs_spectrum_2,
        obs_spectrum_incompatible,
        lambda x, y: x.add_inverse_variance_weighted(y),
        addition_proof_weighted,
    )
예제 #6
0
    def from_function(cls, name, source_function, response, source_errors=None, source_sys_errors=None,
                      background_function=None, background_errors=None, background_sys_errors=None):
        """

        Construct a simulated spectrum from a given source function and (optional) background function. If source and/or background errors are not supplied, the likelihood is assumed to be Poisson.

        :param name: simulated data set name
        :param source_function: astromodels function
        :param response: 3ML Instrument response
        :param source_errors: (optional) gaussian source errors
        :param source_sys_errors: (optional) systematic source errors
        :param background_function: (optional) astromodels background function
        :param background_errors: (optional) gaussian background errors
        :param background_sys_errors: (optional) background systematic errors
        :return: simulated DispersionSpectrumLike plugin
        """

        channel_set = ChannelSet.from_instrument_response(response)

        energy_min, energy_max = channel_set.bin_stack.T

        # pass the variables to the super class

        return super(DispersionSpectrumLike, cls).from_function(name, source_function, energy_min, energy_max,
                                                                source_errors, source_sys_errors,
                                                                background_function, background_errors,
                                                                background_sys_errors, response=response)
예제 #7
0
def test_dispersion_spectrum_addition_poisson(loaded_response):

    rsp = loaded_response
    ebounds = ChannelSet.from_instrument_response(rsp)

    obs_spectrum_1 = BinnedSpectrumWithDispersion(counts=np.ones(len(ebounds)),exposure=1, response=rsp, is_poisson=True)
    obs_spectrum_2 = BinnedSpectrumWithDispersion(counts=np.ones(len(ebounds)),exposure=2, response=rsp, is_poisson=True)
    obs_spectrum_incompatible = None
    
    spectrum_addition(obs_spectrum_1,obs_spectrum_2,obs_spectrum_incompatible,lambda x,y:x+y,addition_proof_simple)
예제 #8
0
def test_spectrum_constructor_no_background():

    ebounds = ChannelSet.from_list_of_edges(np.array([0,1,2,3,4,5]))

    obs_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)),exposure=1,ebounds=ebounds, is_poisson=True)

    assert np.all(obs_spectrum.counts == obs_spectrum.rates)

    specLike = SpectrumLike('fake', observation=obs_spectrum, background=None)

    specLike.__repr__()
예제 #9
0
def test_spectrum_clone():
    ebounds = ChannelSet.from_list_of_edges(np.array([0, 1, 2, 3, 4, 5]))

    obs_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)),
                                  count_errors=np.ones(len(ebounds)),
                                  exposure=1,
                                  ebounds=ebounds,
                                  is_poisson=False)
    obs_spectrum.clone(new_counts=np.zeros_like(obs_spectrum.counts),
                       new_count_errors=np.zeros_like(obs_spectrum.counts))
    obs_spectrum.clone()
예제 #10
0
    def from_function(
        cls,
        name: str,
        source_function,
        response,
        source_errors=None,
        source_sys_errors=None,
        background_function=None,
        background_errors=None,
        background_sys_errors=None,
        exposure=1.0,
        scale_factor=1.0
    ):
        # type: () -> DispersionSpectrumLike
        """

        Construct a simulated spectrum from a given source function and (optional) background function. If source and/or background errors are not supplied, the likelihood is assumed to be Poisson.

        :param name: simulated data set name
        :param source_function: astromodels function
        :param response: 3ML Instrument response
        :param source_errors: (optional) gaussian source errors
        :param source_sys_errors: (optional) systematic source errors
        :param background_function: (optional) astromodels background function
        :param background_errors: (optional) gaussian background errors
        :param background_sys_errors: (optional) background systematic errors
        :param exposure: the exposure to assume
        :param scale_factor: the scale factor between source exposure / bkg exposure
        :return: simulated DispersionSpectrumLike plugin
        """

        channel_set = ChannelSet.from_instrument_response(response)

        energy_min, energy_max = channel_set.bin_stack.T

        # pass the variables to the super class

        return super(DispersionSpectrumLike, cls).from_function(
            name,
            source_function,
            energy_min,
            energy_max,
            source_errors,
            source_sys_errors,
            background_function,
            background_errors,
            background_sys_errors,
            response=response,
            exposure=exposure,
            scale_factor=scale_factor
        )
예제 #11
0
def test_spectrum_constructor_no_background():

    ebounds = ChannelSet.from_list_of_edges(np.array([0, 1, 2, 3, 4, 5]))

    obs_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)),
                                  exposure=1,
                                  ebounds=ebounds,
                                  is_poisson=True)

    assert np.all(obs_spectrum.counts == obs_spectrum.rates)

    specLike = SpectrumLike("fake", observation=obs_spectrum, background=None)

    specLike.__repr__()
예제 #12
0
def test_spectrum_constructor():

    ebounds = ChannelSet.from_list_of_edges(np.array([0,1,2,3,4,5]))

    pl = Powerlaw()

    ps = PointSource('fake',0,0,spectral_shape=pl)

    model = Model(ps)

    obs_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)),exposure=1,ebounds=ebounds, is_poisson=True)
    bkg_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)),exposure=1,ebounds=ebounds, is_poisson=True)

    assert np.all(obs_spectrum.counts == obs_spectrum.rates)
    assert np.all(bkg_spectrum.counts == bkg_spectrum.rates)

    specLike = SpectrumLike('fake', observation=obs_spectrum, background=bkg_spectrum)
    specLike.set_model(model)
    specLike.get_model()

    specLike.get_simulated_dataset()

    specLike.rebin_on_background(min_number_of_counts=1E-1)
    specLike.remove_rebinning()


    specLike.significance
    specLike.significance_per_channel

    obs_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)), count_errors=np.ones(len(ebounds)),exposure=1, ebounds=ebounds, is_poisson=False)
    bkg_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)), exposure=1, ebounds=ebounds, is_poisson=True)


    with pytest.raises(NotImplementedError):

        specLike = SpectrumLike('fake', observation=obs_spectrum, background=bkg_spectrum)

    # gaussian source only

    obs_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)), count_errors=np.ones(len(ebounds)), exposure=1,
                                  ebounds=ebounds)

    specLike = SpectrumLike('fake', observation=obs_spectrum, background=None)
    specLike.set_model(model)
    specLike.get_model()

    specLike.get_simulated_dataset()

    with pytest.raises(AssertionError):
        specLike.rebin_on_background(min_number_of_counts=1E-1)
예제 #13
0
def test_dispersion_spectrum_addition_poisson(loaded_response):

    rsp = loaded_response
    ebounds = ChannelSet.from_instrument_response(rsp)

    obs_spectrum_1 = BinnedSpectrumWithDispersion(counts=np.ones(len(ebounds)),
                                                  exposure=1,
                                                  response=rsp,
                                                  is_poisson=True)
    obs_spectrum_2 = BinnedSpectrumWithDispersion(counts=np.ones(len(ebounds)),
                                                  exposure=2,
                                                  response=rsp,
                                                  is_poisson=True)
    obs_spectrum_incompatible = None

    spectrum_addition(obs_spectrum_1, obs_spectrum_2,
                      obs_spectrum_incompatible, lambda x, y: x + y,
                      addition_proof_simple)
예제 #14
0
def test_spectrum_constructor():

    ebounds = ChannelSet.from_list_of_edges(np.array([1, 2, 3, 4, 5, 6]))

    pl = Powerlaw()

    ps = PointSource("fake", 0, 0, spectral_shape=pl)

    model = Model(ps)

    obs_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)),
                                  exposure=1,
                                  ebounds=ebounds,
                                  is_poisson=True)
    bkg_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)),
                                  exposure=1,
                                  ebounds=ebounds,
                                  is_poisson=True)

    assert np.all(obs_spectrum.counts == obs_spectrum.rates)
    assert np.all(bkg_spectrum.counts == bkg_spectrum.rates)

    specLike = SpectrumLike("fake",
                            observation=obs_spectrum,
                            background=bkg_spectrum)
    specLike.set_model(model)
    specLike.get_model()

    specLike.get_simulated_dataset()

    specLike.rebin_on_background(min_number_of_counts=1e-1)
    specLike.remove_rebinning()

    specLike.significance
    specLike.significance_per_channel

    obs_spectrum = BinnedSpectrum(
        counts=np.ones(len(ebounds)),
        count_errors=np.ones(len(ebounds)),
        exposure=1,
        ebounds=ebounds,
        is_poisson=False,
    )
    bkg_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)),
                                  exposure=1,
                                  ebounds=ebounds,
                                  is_poisson=True)

    with pytest.raises(NotImplementedError):

        specLike = SpectrumLike("fake",
                                observation=obs_spectrum,
                                background=bkg_spectrum)

    # gaussian source only

    obs_spectrum = BinnedSpectrum(
        counts=np.ones(len(ebounds)),
        count_errors=np.ones(len(ebounds)),
        exposure=1,
        ebounds=ebounds,
    )

    specLike = SpectrumLike("fake", observation=obs_spectrum, background=None)
    specLike.set_model(model)
    specLike.get_model()

    specLike.get_simulated_dataset()

    with pytest.raises(AssertionError):
        specLike.rebin_on_background(min_number_of_counts=1e-1)
예제 #15
0
def test_spectrum_clone():
    ebounds = ChannelSet.from_list_of_edges(np.array([0,1,2,3,4,5]))

    obs_spectrum = BinnedSpectrum(counts=np.ones(len(ebounds)),count_errors=np.ones(len(ebounds)),exposure=1,ebounds=ebounds, is_poisson=False)
    obs_spectrum.clone(new_counts=np.zeros_like(obs_spectrum.counts), new_count_errors=np.zeros_like(obs_spectrum.counts))
    obs_spectrum.clone()