コード例 #1
0
    def setup(self):
        self.model = PowerLaw(
            index=Quantity(2, ''),
            amplitude=Quantity(1e-11, 'm-2 s-1 TeV-1'),
            reference=Quantity(1, 'TeV'),
        )

        # TODO: simulate known spectrum instead of using this example:
        filename = '$GAMMAPY_EXTRA/datasets/hess-crab4_pha/pha_obs23523.fits'
        self.obs = SpectrumObservation.read(filename)
        self.seg = SpectrumEnergyGroupMaker(obs=self.obs)
        ebounds = [0.3, 1.001, 3, 10.001, 30] * u.TeV
        self.seg.compute_range_safe()
        self.seg.compute_groups_fixed(ebounds=ebounds)

        self.groups = self.seg.groups
コード例 #2
0
class TestFluxEstimator:
    def setup(self):
        self.model = PowerLaw(
            index=Quantity(2, ''),
            amplitude=Quantity(1e-11, 'm-2 s-1 TeV-1'),
            reference=Quantity(1, 'TeV'),
        )

        # TODO: simulate known spectrum instead of using this example:
        filename = '$GAMMAPY_EXTRA/datasets/hess-crab4_pha/pha_obs23523.fits'
        self.obs = SpectrumObservation.read(filename)
        self.seg = SpectrumEnergyGroupMaker(obs=self.obs)
        ebounds = [0.3, 1.001, 3, 10.001, 30] * u.TeV
        self.seg.compute_range_safe()
        self.seg.compute_groups_fixed(ebounds=ebounds)

        self.groups = self.seg.groups

    def test_with_power_law(self):
        # import logging
        # logging.basicConfig(level=logging.DEBUG)

        fpe = FluxPointEstimator(
            obs=self.obs,
            groups=self.groups,
            model=self.model,
        )

        assert 'FluxPointEstimator' in str(fpe)

        fpe.compute_points()
        flux_points = fpe.flux_points
        flux_points.pprint()
        flux_points.info()

        actual = flux_points['diff_flux'][2]
        desired = Quantity(5.737510858664804e-09, 'm-2 s-1 TeV-1')
        assert_quantity_allclose(actual, desired, rtol=1e-3)

        actual = flux_points['diff_flux_err_hi'][2]
        desired = Quantity(9.904468386098078e-10, 'm-2 s-1 TeV-1')
        assert_quantity_allclose(actual, desired, rtol=1e-3)

    def test_with_ecpl(self):
        # TODO: implement
        assert True
コード例 #3
0
ファイル: test_flux_point.py プロジェクト: dlennarz/gammapy
class TestFluxEstimator:
    def setup(self):
        self.model = PowerLaw(
            index=Quantity(2, ""), amplitude=Quantity(1e-11, "m-2 s-1 TeV-1"), reference=Quantity(1, "TeV")
        )

        # TODO: simulate known spectrum instead of using this example:
        filename = "$GAMMAPY_EXTRA/datasets/hess-crab4_pha/pha_obs23523.fits"
        self.obs = SpectrumObservation.read(filename)
        self.seg = SpectrumEnergyGroupMaker(obs=self.obs)
        ebounds = [0.3, 1.001, 3, 10.001, 30] * u.TeV
        self.seg.compute_range_safe()
        self.seg.compute_groups_fixed(ebounds=ebounds)

        self.groups = self.seg.groups

    def test_with_power_law(self):
        # import logging
        # logging.basicConfig(level=logging.DEBUG)

        fpe = FluxPointEstimator(obs=self.obs, groups=self.groups, model=self.model)

        assert "FluxPointEstimator" in str(fpe)

        fpe.compute_points()
        flux_points = fpe.flux_points
        flux_points.pprint()
        flux_points.info()

        actual = flux_points["diff_flux"][2]
        desired = Quantity(5.737510858664804e-09, "m-2 s-1 TeV-1")
        assert_quantity_allclose(actual, desired, rtol=1e-3)

        actual = flux_points["diff_flux_err_hi"][2]
        desired = Quantity(9.904468386098078e-10, "m-2 s-1 TeV-1")
        assert_quantity_allclose(actual, desired, rtol=1e-3)

    def test_with_ecpl(self):
        # TODO: implement
        assert True
コード例 #4
0
ファイル: test_flux_point.py プロジェクト: dlennarz/gammapy
    def setup(self):
        self.model = PowerLaw(
            index=Quantity(2, ""), amplitude=Quantity(1e-11, "m-2 s-1 TeV-1"), reference=Quantity(1, "TeV")
        )

        # TODO: simulate known spectrum instead of using this example:
        filename = "$GAMMAPY_EXTRA/datasets/hess-crab4_pha/pha_obs23523.fits"
        self.obs = SpectrumObservation.read(filename)
        self.seg = SpectrumEnergyGroupMaker(obs=self.obs)
        ebounds = [0.3, 1.001, 3, 10.001, 30] * u.TeV
        self.seg.compute_range_safe()
        self.seg.compute_groups_fixed(ebounds=ebounds)

        self.groups = self.seg.groups
コード例 #5
0
joint_fit = SpectrumFit(obs_list=extraction.spectrum_observations,
                        model=model,
                        fit_range=fit_range)
joint_fit.run()
joint_result = joint_fit.result

print(joint_result[0])

# Now you might want to do the stacking here even if in our case there is only one observation which makes it superfluous.
# We can compute flux points by fitting the norm of the global model in energy bands.

# In[ ]:

stacked_obs = extraction.spectrum_observations.stack()
seg = SpectrumEnergyGroupMaker(obs=stacked_obs)

seg.compute_groups_fixed(ebounds=ebounds)
fpe = FluxPointEstimator(obs=stacked_obs,
                         groups=seg.groups,
                         model=joint_result[0].model)
flux_points = fpe.run()

amplitude_ref = 0.57 * 19.4e-14 * u.Unit("1 / (cm2 s MeV)")
spec_model_true = PowerLaw(index=4.5,
                           amplitude=amplitude_ref,
                           reference="20 GeV")

spectrum_result = SpectrumResult(points=flux_points,
                                 model=joint_result[0].model)
コード例 #6
0
"""Test spectrum energy grouping.
"""
import warnings
import astropy.units as u
from gammapy.spectrum import SpectrumObservation, SpectrumEnergyGroupMaker

warnings.filterwarnings('ignore')

filename = '$GAMMAPY_EXTRA/datasets/hess-crab4_pha/pha_obs23523.fits'
obs = SpectrumObservation.read(filename)
table = obs.stats_table()

seg = SpectrumEnergyGroupMaker(obs=obs)
ebounds = [0.03, 1, 3, 10, 30] * u.TeV
seg.compute_range_safe()
seg.compute_groups_fixed(ebounds=ebounds)

print('\nTable of original energy bins:')
seg.table[['energy_group_idx', 'bin_idx', 'energy_min', 'energy_max']].pprint(max_lines=-1)

print('\nTable of grouped energy bins:')
seg.groups.to_group_table().pprint(max_lines=-1)

print('\nTable of grouped energy bins:')
seg.groups.to_total_table().pprint(max_lines=-1)

print(seg)
コード例 #7
0
"""Test spectrum energy grouping.
"""
import warnings
import astropy.units as u
from gammapy.spectrum import SpectrumObservation, SpectrumEnergyGroupMaker

warnings.filterwarnings('ignore')

filename = '$GAMMAPY_EXTRA/datasets/hess-crab4_pha/pha_obs23523.fits'
obs = SpectrumObservation.read(filename)
table = obs.stats_table()

seg = SpectrumEnergyGroupMaker(obs=obs)
ebounds = [0.03, 1, 3, 10, 30] * u.TeV
seg.compute_range_safe()
seg.compute_groups_fixed(ebounds=ebounds)

print('\nTable of original energy bins:')
seg.table[['energy_group_idx', 'bin_idx', 'energy_min',
           'energy_max']].pprint(max_lines=-1)

print('\nTable of grouped energy bins:')
seg.groups.to_group_table().pprint(max_lines=-1)

print('\nTable of grouped energy bins:')
seg.groups.to_total_table().pprint(max_lines=-1)

print(seg)