Пример #1
0
    def _calculate(self):
        roi = self.roi
        name = self.name
        
        if self.verbosity: print 'Testing cutoff in pointlike'
        emin,emax=get_full_energy_range(roi)

        self.results = d = dict(
            energy = energy_dict(emin=emin, emax=emax, energy_units=self.energy_units)
        )

        saved_state = PointlikeState(roi)

        old_flux = roi.get_model(name).i_flux(emin,emax)

        if not isinstance(roi.get_model(name),PowerLaw):

            powerlaw_model=PowerLaw(norm=1e-11, index=2, e0=np.sqrt(emin*emax))
            powerlaw_model.set_mapper('Index', PowerLaw.default_limits['Index'])
            powerlaw_model.set_flux(old_flux,emin=emin,emax=emax)

            if self.verbosity: print "powerlaw_model is ",powerlaw_model

            roi.modify(which=name, model=powerlaw_model, keep_old_flux=False)

        fit = lambda: roi.fit(**self.fit_kwargs)
        def ts():
            old_quiet = roi.quiet; roi.quiet=True
            ts = roi.TS(name,quick=False)
            roi.quiet = old_quiet
            return ts

        spectrum = lambda: spectrum_to_dict(roi.get_model(name), errors=True)

        if self.verbosity: 
            print 'About to fit powerlaw_model'
            roi.print_summary()

        fit()
        
        if self.verbosity:
            print 'Done fitting powerlaw_model'
            roi.print_summary()

        d['hypothesis_0'] = source_dict(roi, name, emin=emin, emax=emax,
                                        flux_units=self.flux_units,
                                        energy_units=self.energy_units,
                                        verbosity=self.verbosity)

        if self.cutoff_model is not None:
            pass
        else:
            self.cutoff_model=PLSuperExpCutoff(norm=1e-9, index=1, cutoff=1000, e0=1000, b=1)
            # Note, don't limit the normalization parameter
            for p in ['Index', 'Cutoff', 'b']:
                self.cutoff_model.set_mapper(p, PLSuperExpCutoff.default_limits[p])
            self.cutoff_model.set_free('b', False)
            self.cutoff_model.set_flux(old_flux,emin=emin,emax=emax)

        if self.verbosity: print "cutoff_model is ",self.cutoff_model

        roi.modify(which=name, model=self.cutoff_model, keep_old_flux=False)

        if self.verbosity: 
            print 'About to fit cutoff_model'
            roi.print_summary()

        fit()

        ll = -roi.logLikelihood(roi.parameters())

        if ll < d['hypothesis_0']['logLikelihood']:
            # if fit is worse than PowerLaw fit, then
            # restart fit with parameters almost
            # equal to best fit powerlaw
            self.cutoff_plaw=PLSuperExpCutoff(b=1)
            self.cutoff_plaw.set_free('b', False)
            self.cutoff_plaw.setp('norm', d['hypothesis_0']['spectrum']['Norm'])
            self.cutoff_plaw.setp('index', d['hypothesis_0']['spectrum']['Index'])
            self.cutoff_plaw.setp('e0', d['hypothesis_0']['spectrum']['e0'])
            self.cutoff_plaw.setp('cutoff', 1e6)

            roi.modify(which=name, model=self.cutoff_plaw, keep_old_flux=False)
            fit()

            if self.verbosity: 
                print 'Redoing fit with cutoff same as plaw'
                print 'Before:'
                roi.print_summary()
                print fit()

        if self.verbosity:
            print 'Done fitting cutoff_model'
            roi.print_summary()

        d['hypothesis_1'] = source_dict(roi, name, emin=emin, emax=emax,
                                        flux_units=self.flux_units,
                                        energy_units=self.energy_units,
                                        verbosity=self.verbosity)


        d['TS_cutoff']=d['hypothesis_1']['TS']['noquick']-d['hypothesis_0']['TS']['noquick']

        saved_state.restore()
Пример #2
0
    def _calculate(self):
        roi = self.roi
        name = self.name

        if self.verbosity: print 'Testing cutoff in pointlike'
        emin, emax = get_full_energy_range(roi)

        self.results = d = dict(energy=energy_dict(
            emin=emin, emax=emax, energy_units=self.energy_units))

        saved_state = PointlikeState(roi)

        old_flux = roi.get_model(name).i_flux(emin, emax)

        if not isinstance(roi.get_model(name), PowerLaw):

            powerlaw_model = PowerLaw(norm=1e-11,
                                      index=2,
                                      e0=np.sqrt(emin * emax))
            powerlaw_model.set_mapper('Index',
                                      PowerLaw.default_limits['Index'])
            powerlaw_model.set_flux(old_flux, emin=emin, emax=emax)

            if self.verbosity: print "powerlaw_model is ", powerlaw_model

            roi.modify(which=name, model=powerlaw_model, keep_old_flux=False)

        fit = lambda: roi.fit(**self.fit_kwargs)

        def ts():
            old_quiet = roi.quiet
            roi.quiet = True
            ts = roi.TS(name, quick=False)
            roi.quiet = old_quiet
            return ts

        spectrum = lambda: spectrum_to_dict(roi.get_model(name), errors=True)

        if self.verbosity:
            print 'About to fit powerlaw_model'
            roi.print_summary()

        fit()

        if self.verbosity:
            print 'Done fitting powerlaw_model'
            roi.print_summary()

        d['hypothesis_0'] = source_dict(roi,
                                        name,
                                        emin=emin,
                                        emax=emax,
                                        flux_units=self.flux_units,
                                        energy_units=self.energy_units,
                                        verbosity=self.verbosity)

        if self.cutoff_model is not None:
            pass
        else:
            self.cutoff_model = PLSuperExpCutoff(norm=1e-9,
                                                 index=1,
                                                 cutoff=1000,
                                                 e0=1000,
                                                 b=1)
            # Note, don't limit the normalization parameter
            for p in ['Index', 'Cutoff', 'b']:
                self.cutoff_model.set_mapper(
                    p, PLSuperExpCutoff.default_limits[p])
            self.cutoff_model.set_free('b', False)
            self.cutoff_model.set_flux(old_flux, emin=emin, emax=emax)

        if self.verbosity: print "cutoff_model is ", self.cutoff_model

        roi.modify(which=name, model=self.cutoff_model, keep_old_flux=False)

        if self.verbosity:
            print 'About to fit cutoff_model'
            roi.print_summary()

        fit()

        ll = -roi.logLikelihood(roi.parameters())

        if ll < d['hypothesis_0']['logLikelihood']:
            # if fit is worse than PowerLaw fit, then
            # restart fit with parameters almost
            # equal to best fit powerlaw
            self.cutoff_plaw = PLSuperExpCutoff(b=1)
            self.cutoff_plaw.set_free('b', False)
            self.cutoff_plaw.setp('norm',
                                  d['hypothesis_0']['spectrum']['Norm'])
            self.cutoff_plaw.setp('index',
                                  d['hypothesis_0']['spectrum']['Index'])
            self.cutoff_plaw.setp('e0', d['hypothesis_0']['spectrum']['e0'])
            self.cutoff_plaw.setp('cutoff', 1e6)

            roi.modify(which=name, model=self.cutoff_plaw, keep_old_flux=False)
            fit()

            if self.verbosity:
                print 'Redoing fit with cutoff same as plaw'
                print 'Before:'
                roi.print_summary()
                print fit()

        if self.verbosity:
            print 'Done fitting cutoff_model'
            roi.print_summary()

        d['hypothesis_1'] = source_dict(roi,
                                        name,
                                        emin=emin,
                                        emax=emax,
                                        flux_units=self.flux_units,
                                        energy_units=self.energy_units,
                                        verbosity=self.verbosity)

        d['TS_cutoff'] = d['hypothesis_1']['TS']['noquick'] - d[
            'hypothesis_0']['TS']['noquick']

        saved_state.restore()