Skip to content
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

Merged
merged 5 commits into from Nov 18, 2016

Conversation

adonath
Copy link
Member

@adonath adonath commented Nov 16, 2016

This PR introduces a CrabSpectrum class, with the different reference models represented as SpectrumModel. This has the advantage, that all the methods of SpectrumModel, such as .integral()
and energy_flux() can be reused. It has now full Quantity support as well.

@adonath adonath added this to the 0.5 milestone Nov 16, 2016
@adonath adonath self-assigned this Nov 16, 2016
@adonath adonath force-pushed the rewrite_crab_spectrum_Class branch 2 times, most recently from 6ed41bf to 87af99e Compare November 16, 2016 17:28
Copy link
Contributor

@cdeil cdeil left a 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approximisation -> approximation ?

Copy link
Member Author

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
Copy link
Contributor

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.

Copy link
Member Author

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)
Copy link
Contributor

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.

Copy link
Member Author

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

@cdeil
Copy link
Contributor

cdeil commented Nov 17, 2016

After thinking about this some more, I'm thinking maybe keeping it a class it a good idea?
If we want to add extra methods like "convert given flux to Crab units" we could add it there and the method docstring would show up in the docs.

@adonath
Copy link
Member Author

adonath commented Nov 17, 2016

@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...

@cdeil
Copy link
Contributor

cdeil commented Nov 17, 2016

I think what you have now, explaining how to do it via the docs, is a good solution.
Merge when travis-ci passes, right?

@adonath adonath merged commit d171ecf into gammapy:master Nov 18, 2016
@adonath adonath deleted the rewrite_crab_spectrum_Class branch November 18, 2016 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants