-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite crab spectrum as class #777
Conversation
6ed41bf
to
87af99e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I've left two inline comments.
Concerning API: I probably would have made this a factory function, and just attached whatever other info you want to the SpectralModel objects after construction. But if you prefer the CrabSpectrum class where one accesses the model all the time, OK.
In any case: add one or two examples how to call it to the docstring?
def spectral_index(self, energy): | ||
""" | ||
Compute spectral index at given energy using a local powerlaw | ||
approximisation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approximisation -> approximation ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
|
||
See the ``gammapy.spectrum.crab`` module docstring for a description | ||
of the available reference spectra. | ||
* 'meyer', 2010arXiv1008.4524M |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to indent lists, putting them at the same level as the surrounding text gives nice HTML output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
index : float | ||
Estimated spectral index. | ||
""" | ||
eps = np.sqrt(np.finfo(float).eps) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does such a small eps
yield numerically stable results?
I think I would have put something like 1e-3
.
But I don't know, if you think or know the spectral index with that small eps gives stable output, OK to keep as-is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made it an option to the method. Default is now 1E-5
After thinking about this some more, I'm thinking maybe keeping it a class it a good idea? |
87af99e
to
2e3d1ed
Compare
@cdeil The use case with the unit is what I had in mind as well. Right now the following works, which isn't too bad: from astropy import units as u
from gammapy.spectrum import CrabSpectrum
from gammapy.spectrum.models import PowerLaw
# define power law model
amplitude = 1E-12 * u.Unit('1 / (cm2 s TeV)')
index = 2.3
reference = 1 * u.TeV
pwl = PowerLaw(index, amplitude, reference)
# define crab reference
crab = CrabSpectrum('hess_pl').model
# compute flux at 10 TeV in crab units
energy = 10 * u.TeV
flux_cu = (pwl(energy) / crab(energy)).to('%')
print(flux_cu)
# compute integral flux in crab units
emin, emax = [1, 10] * u.TeV
flux_int_cu = (pwl.integral(emin, emax) / crab.integral(emin, emax)).to('%')
print(flux_int_cu) I think it's hard to implement a better API, because the flux unit is parametric and depends on what you do with the spectrum you want to measure the flux in crab units for...so something along the lines of: pwl(energy).to('crab')
pwl.integral(emin, emax).to('crab') Is very hard to implement and probably not worth the effort. Possibly one could have something like: pwl_cu = CrabSpectrum('meyer').use_as_unit_for(pwl)
pwl_cu(energy) But it's not a super nice API either... |
I think what you have now, explaining how to do it via the docs, is a good solution. |
This PR introduces a
CrabSpectrum
class, with the different reference models represented asSpectrumModel
. This has the advantage, that all the methods ofSpectrumModel
, such as.integral()
and
energy_flux()
can be reused. It has now fullQuantity
support as well.