예제 #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 set_active_time_interval(self, *intervals, **kwargs):
        """
        Set the time interval to be used during the analysis.
        For now, only one interval can be selected. This may be
        updated in the future to allow for self consistent time
        resolved analysis.
        Specified as 'tmin-tmax'. Intervals are in seconds. Example:

        set_active_time_interval("0.0-10.0")

        which will set the energy range 0-10. seconds.
        :param options:
        :param intervals:
        :return:
        """

        self._time_series.set_active_time_intervals(*intervals)

        # extract a spectrum

        if self._response is None:

            self._observed_spectrum = BinnedSpectrum.from_time_series(
                self._time_series, use_poly=False)

        else:

            if self._rsp_is_weighted:
                self._response = self._weighted_rsp.weight_by_counts(
                    *self._time_series.time_intervals.to_string().split(','))

            self._observed_spectrum = BinnedSpectrumWithDispersion.from_time_series(
                self._time_series, self._response, use_poly=False)

        self._active_interval = intervals

        if self._time_series.poly_fit_exists:

            if self._response is None:

                self._background_spectrum = BinnedSpectrum.from_time_series(
                    self._time_series, use_poly=True)

            else:

                self._background_spectrum = BinnedSpectrumWithDispersion.from_time_series(
                    self._time_series, self._response, use_poly=True)

        self._tstart = self._time_series.time_intervals.absolute_start_time
        self._tstop = self._time_series.time_intervals.absolute_stop_time
예제 #4
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__()
예제 #5
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()
예제 #6
0
    def set_background_interval(self, *intervals, **options):
        """
        Set the time interval to fit the background.
        Multiple intervals can be input as separate arguments
        Specified as 'tmin-tmax'. Intervals are in seconds. Example:

        setBackgroundInterval("-10.0-0.0","10.-15.")


        :param *intervals:
        :param **options:

        :return: none

        """
        if 'unbinned' in options:

            unbinned = options.pop('unbinned')
        else:

            unbinned = self._default_unbinned

        self._time_series.set_polynomial_fit_interval(*intervals,
                                                      unbinned=unbinned)

        # In theory this will automatically get the poly counts if a
        # time interval already exists

        if self._active_interval is not None:

            if self._response is None:

                self._background_spectrum = BinnedSpectrum.from_time_series(
                    self._time_series, use_poly=True)

            else:

                # we do not need to worry about the interval of the response if it is a set. only the ebounds are extracted here

                self._background_spectrum = BinnedSpectrumWithDispersion.from_time_series(
                    self._time_series, self._response, use_poly=True)
예제 #7
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)
예제 #8
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()