def setup(self): path = "$GAMMAPY_DATA/joint-crab/spectra/hess/" obs1 = SpectrumDatasetOnOff.from_ogip_files(path + "pha_obs23523.fits") obs2 = SpectrumDatasetOnOff.from_ogip_files(path + "pha_obs23592.fits") self.obs_list = [obs1, obs2] self.pwl = PowerLaw(index=2, amplitude=1e-12 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV) self.ecpl = ExponentialCutoffPowerLaw( index=2, amplitude=1e-12 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV, lambda_=0.1 / u.TeV, )
def read(): model = PowerLawSpectralModel(index=2, amplitude=2e-11 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV) stacked = SpectrumDatasetOnOff.from_ogip_files( filename="pha_obsstacked.fits") stacked.model = model return stacked
def test_datasets_stack_reduce(): obs_ids = [23523, 23526, 23559, 23592] dataset_list = [] for obs in obs_ids: filename = "$GAMMAPY_DATA/joint-crab/spectra/hess/pha_obs{}.fits" ds = SpectrumDatasetOnOff.from_ogip_files(filename.format(obs)) dataset_list.append(ds) datasets = Datasets(dataset_list) stacked = datasets.stack_reduce() assert_allclose(stacked.livetime.to_value("s"), 6313.8116406202325)
def setup(self): path = "$GAMMAPY_DATA/joint-crab/spectra/hess/" obs1 = SpectrumDatasetOnOff.from_ogip_files(path + "pha_obs23523.fits") obs2 = SpectrumDatasetOnOff.from_ogip_files(path + "pha_obs23592.fits") self.obs_list = [obs1, obs2] self.pwl = PowerLawSpectralModel(index=2, amplitude=1e-12 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV) self.ecpl = ExpCutoffPowerLawSpectralModel( index=2, amplitude=1e-12 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV, lambda_=0.1 / u.TeV, ) # Example fit for one observation self.obs_list[0].model = self.pwl self.fit = Fit(self.obs_list[0])
def read(): datasets = [] model = PowerLawSpectralModel(index=2, amplitude=2e-11 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV) for ind in range(N_OBS): dataset = SpectrumDatasetOnOff.from_ogip_files( filename=f"pha_obs{ind}.fits") dataset.model = model datasets.append(dataset) return datasets
def setup(self): path = "$GAMMAPY_DATA/joint-crab/spectra/hess/" self.datasets = Datasets([ SpectrumDatasetOnOff.from_ogip_files(path + "pha_obs23523.fits"), SpectrumDatasetOnOff.from_ogip_files(path + "pha_obs23592.fits"), ]) self.pwl = SkyModel( spectral_model=PowerLawSpectralModel(index=2, amplitude=1e-12 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV)) self.ecpl = SkyModel(spectral_model=ExpCutoffPowerLawSpectralModel( index=2, amplitude=1e-12 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV, lambda_=0.1 / u.TeV, )) # Example fit for one observation self.datasets[0].models = self.pwl self.fit = Fit([self.datasets[0]])
def test_run(tmpdir, extraction): """Test the run method and check if files are written correctly""" extraction.run() extraction.write(outdir=str(tmpdir), overwrite=True) testobs = SpectrumDatasetOnOff.from_ogip_files( str(tmpdir / "ogip_data" / "pha_obs23523.fits")) assert_quantity_allclose( testobs.aeff.data.data, extraction.spectrum_observations[0].aeff.data.data) assert_quantity_allclose( testobs.counts.data, extraction.spectrum_observations[0].counts.data) assert_allclose( testobs.counts.energy.center, extraction.spectrum_observations[0].counts.energy.center, )
def test_datasets_stack_reduce(): obs_ids = [23523, 23526, 23559, 23592] dataset_list = [] for obs in obs_ids: filename = "$GAMMAPY_DATA/joint-crab/spectra/hess/pha_obs{}.fits" ds = SpectrumDatasetOnOff.from_ogip_files(filename.format(obs)) dataset_list.append(ds) datasets = Datasets(dataset_list) stacked = datasets.stack_reduce() assert_allclose(stacked.livetime.to_value("s"), 6313.8116406202325) info_table = datasets.info_table() assert_allclose(info_table["n_on"], [124, 126, 119, 90]) info_table_cum = datasets.info_table(cumulative=True) assert_allclose(info_table_cum["n_on"], [124, 250, 369, 459])
def test_to_from_ogip_files_no_edisp(self, tmpdir): dataset = SpectrumDatasetOnOff( counts=self.on_counts, aeff=self.aeff, livetime=self.livetime, mask_safe=np.ones(self.on_counts.energy.nbin, dtype=bool), acceptance=1, obs_id="test", ) dataset.to_ogip_files(outdir=tmpdir, overwrite=True) filename = tmpdir / "pha_obstest.fits" newdataset = SpectrumDatasetOnOff.from_ogip_files(filename) assert_allclose(self.on_counts.data, newdataset.counts.data) assert newdataset.counts_off is None assert newdataset.edisp is None
def test_to_from_ogip_files_no_edisp(self, tmp_path): dataset = SpectrumDatasetOnOff( counts=self.on_counts, aeff=self.aeff, livetime=self.livetime, mask_safe=np.ones(self.on_counts.energy.nbin, dtype=bool), acceptance=1, name="test", ) dataset.to_ogip_files(outdir=tmp_path) newdataset = SpectrumDatasetOnOff.from_ogip_files(tmp_path / "pha_obstest.fits") assert_allclose(self.on_counts.data, newdataset.counts.data) assert newdataset.counts_off is None assert newdataset.edisp is None assert newdataset.gti is None
def test_to_from_ogip_files(self, tmp_path): dataset = SpectrumDatasetOnOff( counts=self.on_counts, counts_off=self.off_counts, aeff=self.aeff, edisp=self.edisp, livetime=self.livetime, mask_safe=np.ones(self.on_counts.energy.nbin, dtype=bool), acceptance=1, acceptance_off=10, name="test", gti=self.gti, ) dataset.to_ogip_files(outdir=tmp_path) newdataset = SpectrumDatasetOnOff.from_ogip_files(tmp_path / "pha_obstest.fits") assert_allclose(self.on_counts.data, newdataset.counts.data) assert_allclose(self.off_counts.data, newdataset.counts_off.data) assert_allclose(self.edisp.pdf_matrix, newdataset.edisp.pdf_matrix) assert_time_allclose(newdataset.gti.time_start, dataset.gti.time_start)
def _read_hess_obs(): path = "$GAMMAPY_DATA/joint-crab/spectra/hess/" obs1 = SpectrumDatasetOnOff.from_ogip_files(path + "pha_obs23523.fits") obs2 = SpectrumDatasetOnOff.from_ogip_files(path + "pha_obs23592.fits") return [obs1, obs2]
crab_model = dataset_fermi.models["Crab Nebula"] crab_spec = crab_model.spectral_model print(crab_spec) # ### HESS-DL3: 1D ON/OFF dataset for spectral fitting # # The ON/OFF datasets can be read from PHA files following the [OGIP standards](https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/spectra/ogip_92_007/node5.html). # We read the PHA files from each observation, and compute a stacked dataset for simplicity. # Then the Crab spectral model previously defined is added to the dataset. # In[ ]: datasets = [] for obs_id in [23523, 23526]: dataset = SpectrumDatasetOnOff.from_ogip_files( f"$GAMMAPY_DATA/joint-crab/spectra/hess/pha_obs{obs_id}.fits") datasets.append(dataset) dataset_hess = Datasets(datasets).stack_reduce() dataset_hess.name = "HESS" dataset_hess.models = crab_model # ### HAWC: 1D dataset for flux point fitting # # The HAWC flux point are taken from https://arxiv.org/pdf/1905.12518.pdf. Then these flux points are read from a pre-made FITS file and passed to a `FluxPointsDataset` together with the source spectral model. # # In[ ]: # read flux points from https://arxiv.org/pdf/1905.12518.pdf filename = "$GAMMAPY_DATA/hawc_crab/HAWC19_flux_points.fits"
# In[ ]: for dataset in datasets: dataset.to_ogip_files(outdir=path, overwrite=True) # If you want to read back the datasets from disk you can use: # In[ ]: datasets = [] for obs_id in obs_ids: filename = path / f"pha_obs{obs_id}.fits" datasets.append(SpectrumDatasetOnOff.from_ogip_files(filename)) # ## Fit spectrum # # Now we'll fit a global model to the spectrum. First we do a joint likelihood fit to all observations. If you want to stack the observations see below. We will also produce a debug plot in order to show how the global fit matches one of the individual observations. # In[ ]: spectral_model = PowerLawSpectralModel( index=2, amplitude=2e-11 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV ) model = SkyModel(spectral_model=spectral_model) for dataset in datasets: