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

Add EnergyDispersion and CountsSpectrum #330

Merged
merged 10 commits into from Aug 31, 2015

Conversation

Projects
None yet
2 participants
@joleroi
Contributor

joleroi commented Aug 18, 2015

Energy Dispersion 2D TODO

  • Fix https://travis-ci.org/gammapy/gammapy/jobs/77662317#L1384
  • Adress Christoph's inline comments
  • The fits file is 40 kB. I'd prefer you gzip it or put it in gammapy-extra to keep the code repo small.
  • Add more tests, especially for I/O (see here for an example).
  • Check EnergyDispersion class (Energy handling, RMF export) -> leave to later
  • More Documentation? -> Later.

@cdeil cdeil added the feature label Aug 18, 2015

@cdeil cdeil added this to the 0.4 milestone Aug 18, 2015

@cdeil cdeil self-assigned this Aug 18, 2015

@joleroi joleroi force-pushed the joleroi:spectralfit branch 2 times, most recently from 9900495 to 5e91cda Aug 19, 2015

@joleroi joleroi force-pushed the joleroi:spectralfit branch 2 times, most recently from e1e19e9 to 286b0a2 Aug 28, 2015

@joleroi

This comment has been minimized.

Contributor

joleroi commented Aug 28, 2015

Maybe we should focus on the Energy Disperson 2D class in this PR and do the rest of the spectral fitting in a separate one

import numpy as np
from astropy import log
from ..spectrum.energy import *

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

Please no import *.
If you use < 5 things, just import them explicitly, i.e.

from ..spectrum.energy import this, that, the_other_thing
.. code-block:: python
from gammapy.spectrum import *

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

Same here ... also for end-users it's better to never user import *.
If you show them where things are in the examples, they'll learn.
Plus import * can lead to name conflicts and errors that are hard to understand for novices.

Counts
energy: `~gammapy.spectrum.Energy`, `~gammapy.spectrum.EnergyBounds`.
Energy axis

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

Document livetime parameter.

return cls(val, energy, livetime)
def to_pha(self, **kwargs):

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

Is kwargs needed here?
If yes, mention in the docstring what the user can pass in?

class EnergyDispersion2D(object):
"""Offset-dependent energy dispersion matrix.

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

Document Parameters here?
Move plotting examples to the plot method docstrings?

return cls.from_fits(hdulist['ENERGY DISPERSION'])
def evaluate(self, offset=None, e_true=None, migra=None):
"""Probability for a given offset, true energy, and migration

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

Here you could be more specific and explain that the value is dP/dmigra (if that is the case).
I don't like the name migra but don't have a better suggestion at the moment.

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

Worth adding a Parameters section to this docstring?

This comment has been minimized.

@joleroi

joleroi Aug 28, 2015

Contributor

I also don't like the name migra
I guess there should be an explaation of what an RM is and so on in the high level docs. I can do this before the workshop

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

There's the option to use more explicit names like dp_dy, but it's still cryptic, so I don't have a proposal for something better at the moment.

Adding a formula to the docs to better explain what the functions return (i.e. which probability density) is easy.
See here as an example.

if ax[i].shape == ():
ax[i] = ax[i].reshape(1)
# There is a bug here that could be investigated

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

Add "TODO: " here or a new Github issue.
Otherwise we'll never find / remember this.

Parameters
----------
e_true : `~gammapy.spectrum.energy.EnergyBounds`, None

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

~gammapy.spectrum.EnergyBounds

True energy axis
e_reco : `~gammapy.spectrum.energy.EnergyBounds`
Reconstructed energy axis
offset : `~astropy.coordonates.Angle`

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

Typo: coordonates -> coordinates

Returns
-------
edisp : `~gammapy.irf.energy_dispersion.EnergyDispersion`

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

This should be ~gammapy.irf.EnergyDispersion

I don't think ~gammapy.irf.energy_dispersion.EnergyDispersion will resolve to a link in the HTML docs.

Read this and then check your HTML docs locally via

python setup.py build_sphinx
open docs/_build/html/index.html
Energy disperion matrix
"""
if e_reco is None:

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

So e_reco is a required argument.
I'd say make it a positional argument without default, i.e.

to_energy_dispersion(self, e_reco, e_true=None, offset=None):

and then remove this check for None and ValueError.

return ax
def plot_bias(self, ax=None, offset=None, energy=None,
migra=None, **kwargs):

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

Please add a one line docstring.

assert_allclose(actual, desired, rtol=1e-06)
# Check that values between node make sense

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

I'd say let's leave extensive testing to later.
We can use a simple analytical model (e.g. Gauss with some dependence of center and width) and use that for testing the interpolation and density transformation.
But that's at least a day of work, and I'd say let's just run it and see if we get spectral results first.

Parameters
----------
lower : `~astropy.units.Quantity`, float

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

If you have such very similar arguments, you can combine them to save some typing for you and reading for the user:

lower, upper : `~astropy.units.Quantity`, float
    Lowest and highest energy bin
return self[mask]
def select_sky_ring(self, center, inner_radius = None, outer_radius = None,

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

My suggestion would be to do a simpler API / implementation here:

def select_sky_ring(self, center, inner_radius, outer_radius)

If the user has a thickness, it's still short and readable:

select_sky_ring(center, inner_radius, inner_radius + thickness)

But if you like the flexibility here, OK to keep (the implementation with the many if statements looks correct to me).

@cdeil

This comment has been minimized.

Member

cdeil commented Aug 28, 2015

I've left a few inline comments and updated the task list at the top.
This is looking good already!

@joleroi

This comment has been minimized.

Contributor

joleroi commented Aug 28, 2015

Thank you very much, I will focus on getting the fit to work and then clean up after me (maybe after a more thorough discussion about the IRF classes)

@cdeil

This comment has been minimized.

Member

cdeil commented Aug 28, 2015

I'd say let's try to get this in master quickly (so that anyone can start using it) and then if there are changes needed to get the fit to work, do it in follow-up PRs next week.
(this is already a big PR and it gets harder and harder if you want to do more and more stuff in one PR.)

Parameters
----------
counts: `~numpy.array`, list

This comment has been minimized.

@cdeil

cdeil Aug 28, 2015

Member

This won't render correctly in the Sphinx HTML docs.
There needs to be a space before the colon:

counts : ...

There's a few other places where this needs to be fixed.
I always check the HTML version of the docstrings I write ... need to find a way to make such formatting errors make the Sphinx build fail on travis-ci.

@cdeil cdeil changed the title from WIP: Add missing functionality to perform a spectral fit with gammapy.hspec to Add EnergyDispersion and CountsSpectrum Aug 28, 2015

@pytest.mark.xfail
def test_EnergyDispersion():
edisp = EnergyDispersion()
pdf = edisp(3, 4)
assert_allclose(pdf, 42)
@cdeil

This comment has been minimized.

Member

cdeil commented Aug 28, 2015

The np_to_rmf function no longer exists ... this needs to be adapted:
https://travis-ci.org/gammapy/gammapy/jobs/77671805#L1409

@joleroi joleroi force-pushed the joleroi:spectralfit branch from d888ec7 to f47ac43 Aug 31, 2015

joleroi pushed a commit that referenced this pull request Aug 31, 2015

Merge pull request #330 from kingj90/spectralfit
Add EnergyDispersion and CountsSpectrum

@joleroi joleroi merged commit b828e03 into gammapy:master Aug 31, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

joleroi added a commit that referenced this pull request Aug 31, 2015

dlennarz pushed a commit to dlennarz/gammapy that referenced this pull request Oct 11, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment