[docs]defcutout(self,*args,**kwargs):raiseNotImplementedError("Method not supported on a spectrum dataset")
[docs]defplot_residuals_spatial(self,*args,**kwargs):raiseNotImplementedError("Method not supported on a spectrum dataset")
[docs]@classmethoddefread(cls,filename,format="ogip",**kwargs):"""Read from file For OGIP formats, filename is assumed to the name of a PHA file where BKG file, ARF, and RMF names must be set in the PHA header and be present in the same folder. For details, see `OGIPDatasetReader.read` For the GADF format, a MapDataset serialisation is used Parameters ---------- filename : `~pathlib.Path` or str OGIP PHA file to read format : {"ogip", "ogip-sherpa", "gadf"} Format to use. kwargs : arguments passed to `MapDataset.read` """from.ioimportOGIPDatasetReaderifformat=="gadf":returnsuper().read(filename,format="gadf",**kwargs)reader=OGIPDatasetReader(filename=filename)returnreader.read()
[docs]defwrite(self,filename,overwrite=False,format="ogip"):"""Write spectrum dataset on off to file. Can be serialised either as a `MapDataset` with a `RegionGeom` following the GADF specifications, or as per the OGIP format. For OGIP formats specs see `OGIPDatasetWriter` Parameters ---------- filename : `~pathlib.Path` or str Filename to write to. overwrite : bool Overwrite existing file. format : {"ogip", "ogip-sherpa", "gadf"} Format to use. """from.ioimportOGIPDatasetWriterifformat=="gadf":super().write(filename=filename,overwrite=overwrite)elifformatin["ogip","ogip-sherpa"]:writer=OGIPDatasetWriter(filename=filename,format=format,overwrite=overwrite)writer.write(self)else:raiseValueError(f"{format} is not a valid serialisation format")
[docs]@classmethoddeffrom_dict(cls,data,**kwargs):"""Create spectrum dataset from dict. Reads file from the disk as specified in the dict Parameters ---------- data : dict Dict containing data to create dataset from. Returns ------- dataset : `SpectrumDatasetOnOff` Spectrum dataset on off. """filename=make_path(data["filename"])dataset=cls.read(filename=filename)dataset.mask_fit=Nonereturndataset
[docs]defto_dict(self):"""Convert to dict for YAML serialization."""filename=f"pha_obs{self.name}.fits"return{"name":self.name,"type":self.tag,"filename":filename}
[docs]@classmethoddeffrom_spectrum_dataset(cls,**kwargs):"""Create spectrum dataseton off from another dataset. Parameters ---------- dataset : `SpectrumDataset` Spectrum dataset defining counts, edisp, exposure etc. acceptance : `~numpy.array` or float Relative background efficiency in the on region. acceptance_off : `~numpy.array` or float Relative background efficiency in the off region. counts_off : `~gammapy.maps.RegionNDMap` Off counts spectrum . If the dataset provides a background model, and no off counts are defined. The off counts are deferred from counts_off / alpha. Returns ------- dataset : `SpectrumDatasetOnOff` Spectrum dataset on off. """returncls.from_map_dataset(**kwargs)
[docs]defto_spectrum_dataset(self,name=None):"""Convert a SpectrumDatasetOnOff to a SpectrumDataset The background model template is taken as alpha*counts_off Parameters ---------- name: str Name of the new dataset Returns ------- dataset: `SpectrumDataset` SpectrumDatset with cash statistics """returnself.to_map_dataset(name=name).to_spectrum_dataset(on_region=None)