Exemple #1
0
 def setup(self):
     pha = gammapy_extra.filename("datasets/hess-crab4_pha/pha_obs23592.fits")
     self.obs = SpectrumObservation.read(pha)
     self.best_fit_model = models.PowerLaw(index=2 * u.Unit(''),
                                           amplitude=1e-11 * u.Unit('cm-2 s-1 TeV-1'),
                                           reference=1 * u.TeV)
     self.npred = self.obs.predicted_counts(self.best_fit_model).data.value
     self.covar_axis = ['index', 'amplitude']
     self.covar = np.diag([0.1 ** 2, 1e-12 ** 2])
     self.fit_range = [0.1, 50] * u.TeV
     self.fit_result = SpectrumFitResult(
         model=self.best_fit_model,
         covariance=self.covar,
         covar_axis=self.covar_axis,
         fit_range=self.fit_range,
         statname='wstat',
         statval=42,
         npred=self.npred,
         obs=self.obs,
     )
Exemple #2
0
class TestSpectrumFitResult:

    def setup(self):
        pha = gammapy_extra.filename("datasets/hess-crab4_pha/pha_obs23592.fits")
        self.obs = SpectrumObservation.read(pha)
        self.best_fit_model = models.PowerLaw(index=2 * u.Unit(''),
                                              amplitude=1e-11 * u.Unit('cm-2 s-1 TeV-1'),
                                              reference=1 * u.TeV)
        self.npred = self.obs.predicted_counts(self.best_fit_model).data.value
        self.covar_axis = ['index', 'amplitude']
        self.covar = np.diag([0.1 ** 2, 1e-12 ** 2])
        self.fit_range = [0.1, 50] * u.TeV
        self.fit_result = SpectrumFitResult(
            model=self.best_fit_model,
            covariance=self.covar,
            covar_axis=self.covar_axis,
            fit_range=self.fit_range,
            statname='wstat',
            statval=42,
            npred=self.npred,
            obs=self.obs,
        )

    def test_basic(self):
        assert 'PowerLaw' in str(self.fit_result)
        assert 'index' in self.fit_result.to_table().colnames

    @requires_dependency('matplotlib')
    def test_plot(self):
        self.fit_result.plot()

    def test_io(self, tmpdir):
        self.fit_result.to_yaml(tmpdir / 'test.yaml')
        read_result = SpectrumFitResult.from_yaml(tmpdir / 'test.yaml')
        test_e = 12.5 * u.TeV
        assert_quantity_allclose(self.fit_result.model(test_e),
                                 read_result.model(test_e))

    def test_model_with_uncertainties(self):
        actual = self.fit_result.model_with_uncertainties.parameters.index.s
        desired = np.sqrt(self.covar[0][0])
        assert actual == desired
Exemple #3
0
 def test_io(self, tmpdir):
     self.fit_result.to_yaml(tmpdir / 'test.yaml')
     read_result = SpectrumFitResult.from_yaml(tmpdir / 'test.yaml')
     test_e = 12.5 * u.TeV
     assert_quantity_allclose(self.fit_result.model(test_e),
                              read_result.model(test_e))
Exemple #4
0
plt.cla()

fit.run()
fit.result[0].plot_fit()
plt.savefig('debug_fit.png')

# TODO: implement properly
plt.cla()

fig = plt.figure()
ax = fig.add_subplot(111)
fit.result[0].fit.plot_butterfly(ax=ax, label='Fit result')
input_parameters = dict(index = 2.3 * u.Unit(''),
                        norm = 2.5 * 1e-12 * u.Unit('cm-2 s-1 TeV-1'),
                        reference = 1 * u.TeV)
input_parameter_errors = dict(index = 0 * u.TeV,
                              norm = 0 * u.Unit('cm-2 s-1 TeV-1'),
                              reference = 0 * u.TeV)

input_model = SpectrumFitResult(spectral_model = 'PowerLaw',
                                parameters = input_parameters,
                                parameter_errors = input_parameter_errors)


input_model.plot(ax=ax, label='Input model', energy_range = [0.1, 80] * u.TeV)
ax.legend(numpoints=1)
plt.savefig('model_fit.png')