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
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
"""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)
print(fit.result[0]) # ### Spectral points # # Finally, let's compute spectral points. The method used is to first choose an energy binning, and then to do a 1-dim likelihood fit / profile to compute the flux and flux error. # In[27]: # Flux points are computed on stacked observation stacked_obs = extract.observations.stack() print(stacked_obs) ebounds = EnergyBounds.equal_log_spacing(1, 40, 4, unit=u.TeV) seg = SpectrumEnergyGroupMaker(obs=stacked_obs) seg.compute_range_safe() seg.compute_groups_fixed(ebounds=ebounds) fpe = FluxPointEstimator( obs=stacked_obs, groups=seg.groups, model=fit.result[0].model, ) fpe.compute_points() fpe.flux_points.table # ### Plot # # Let's plot the spectral model and points. You could do it directly, but there is a helper class. # Note that a spectral uncertainty band, a "butterfly" is drawn, but it is very thin, i.e. barely visible.