import matplotlib.pyplot as plt # In[ ]: import numpy as np import astropy.units as u from gammapy.spectrum import models from gammapy.utils.fitting import Parameter, Parameters # ## Create a model # # To create a spectral model, instantiate an object of the spectral model class you're interested in. # In[ ]: pwl = models.PowerLaw() print(pwl) # This will use default values for the model parameters, which is rarely what you want. # # Usually you will want to specify the parameters on object creation. # One way to do this is to pass `astropy.utils.Quantity` objects like this: # In[ ]: pwl = models.PowerLaw(index=2.3, amplitude=1e-12 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV) print(pwl) # As you see, some of the parameters have default ``min`` and ``values`` as well as a ``frozen`` flag. This is only relevant in the context of spectral fitting and thus covered in [spectrum_analysis.ipynb](https://github.com/gammapy/gammapy/blob/master/tutorials/spectrum_analysis.ipynb). Also, the parameter errors are not set. This will be covered later in this tutorial.
from gammapy.irf import EnergyDispersion, EffectiveAreaTable from gammapy.spectrum import models, SpectrumEvaluator import numpy as np import astropy.units as u import matplotlib.pyplot as plt e_true = np.logspace(-2, 2.5, 109) * u.TeV e_reco = np.logspace(-2, 2, 73) * u.TeV aeff = EffectiveAreaTable.from_parametrization(energy=e_true) edisp = EnergyDispersion.from_gauss(e_true=e_true, e_reco=e_reco, sigma=0.3, bias=0) model = models.PowerLaw(index=2.3, amplitude="2.5e-12 cm-2 s-1 TeV-1", reference="1 TeV") livetime = 1 * u.h predictor = SpectrumEvaluator(model=model, aeff=aeff, edisp=edisp, livetime=livetime) predictor.compute_npred().plot_hist() plt.show()
from gammapy.irf import EnergyDispersion, EffectiveAreaTable from gammapy.spectrum import models, CountsPredictor import numpy as np import astropy.units as u import matplotlib.pyplot as plt e_true = np.logspace(-2, 2.5, 109) * u.TeV e_reco = np.logspace(-2, 2, 73) * u.TeV aeff = EffectiveAreaTable.from_parametrization(energy=e_true) edisp = EnergyDispersion.from_gauss(e_true=e_true, e_reco=e_reco, sigma=0.3, bias=0) model = models.PowerLaw(index=2.3, amplitude=2.5 * 1e-12 * u.Unit('cm-2 s-1 TeV-1'), reference=1 * u.TeV) livetime = 1 * u.h predictor = CountsPredictor(model=model, aeff=aeff, edisp=edisp, livetime=livetime) predictor.run() predictor.npred.plot_hist() plt.show()
extract = SpectrumExtraction( obs_list=obs_list, bkg_estimate=bkg_estimate, ) extract.run() # ### Model fit # # The next step is to fit a spectral model, using all data (i.e. a "global" fit, using all energies). # In[26]: model = models.PowerLaw( index=2 * u.Unit(''), amplitude=1e-11 * u.Unit('cm-2 s-1 TeV-1'), reference=1 * u.TeV, ) fit = SpectrumFit(extract.observations, model) fit.fit() fit.est_errors() 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
extract.run() # Now we will look at the files we just created. We will use the SpectrumObservation object that are still in memory from the extraction step. Note, however, that you could also read them from disk if you have written them in the step above . The ANALYSIS_DIR folder contains 4 FITS files for each observation. These files are described in detail at https://gamma-astro-data-formats.readthedocs.io/en/latest/ogip/index.html. In short they correspond to the on vector, the off vector, the effectie area, and the energy dispersion. # In[44]: extract.observations[0].peek() # Now we’ll fit a global model to the spectrum. First we do a joint likelihood fit to all observations. # In[45]: model = models.PowerLaw( index=2 * u.Unit(''), amplitude=1e-11 * u.Unit('cm-2 s-1 TeV-1'), reference=1 * u.TeV, ) fit = SpectrumFit(extract.observations, model, fit_range=(1 * u.TeV, 10 * u.TeV)) #probably not working fit.fit() fit.est_errors() print(fit.result[0]) # We will also produce a debug plot in order to show how the global fit matches one of the individual observations. # In[46]:
# coding: utf-8 import matplotlib.pyplot as plt import numpy as np import astropy.units as u from gammapy.spectrum import models from gammapy.utils.modeling import Parameter, ParameterList pwl=models.PowerLaw() pwl print pwl pwl.parameters.names pwl.parameters.names[index]=2.3 pwl.parameters.names["index"]=2.3 pwl.parameters["index"]=2.3 pwl["index"]=2.3 pwl("index")=2.3 pwl("index") pwl["index"] pwl.parameters print pwl.parameters pwl.parameters["index"].value=2.3 pwl.parameters["index"].min=1.0 print pwl pwl.parameters["index"].value=2.6 print pwl pwl.parameters["index"] print pwl.parameters["index"] pwl.parameters["index"].min=1.0 pwl.parameters["index"].min pwl.parameters["index"].max pwl.parameters["index"].max=3.0 pwl.parameters["index"].max