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

Add LightCurve class #564

Merged
merged 9 commits into from Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/example_lightcurve.py
@@ -0,0 +1,7 @@
### Using the Lightcurve class
from gammapy.time import LightCurve, make_example_lightcurve

lc = make_example_lightcurve()
print('the mean flux is {}'.format(lc['FLUX'].mean()))
print('the std dev of the flux is {}'.format(lc['FLUX'].std()))
lc.lc_plot()
1 change: 1 addition & 0 deletions gammapy/time/__init__.py
Expand Up @@ -5,3 +5,4 @@
from .plot import *
from .simulate import *
from .utils import *
from .lightcurve import *
59 changes: 59 additions & 0 deletions gammapy/time/lightcurve.py
@@ -0,0 +1,59 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
Lightcurve and elementary temporal functions
"""
from astropy.table import Table
from astropy.table import QTable
from astropy.units import Quantity
import astropy.units as u


__all__ = [
'LightCurve',
'make_example_lightcurve',
]


class LightCurve(QTable):
"""LightCurve class.

Contains all data in the tabular form with columns
tstart, tstop, flux, flux error, time bin (opt).
Possesses functions allowing plotting data, saving as txt
and elementary stats like mean & std dev.

to do: specification of format is work in progress ; will add link
"""

# def __init__(self, table):
# super(LightCurve,self).__init__(table)

def lc_plot(self):
"""Plots the Lightcurve.

The flux as a function of time.
Here, time for each bin is equal to the center of the bin,
i.e. the average of tstart and tstop
"""
import matplotlib.pyplot as plt
tstart = self['TIME_MIN'].to('s')
tstop = self['TIME_MAX'].to('s')
time = (tstart + tstop) / 2.0
flux = self['FLUX'].to('cm-2 s-1')
errors = self['FLUX_ERR'].to('cm-2 s-1')
plt.errorbar(time.value, flux.value,
yerr=errors.value, linestyle="None")
plt.scatter(time, flux)
plt.xlabel("Time (secs)")
plt.ylabel("Flux ($cm^{-2} sec^{-1}$)")
plt.title("Lightcurve")

def make_example_lightcurve():
""" Make an example lightcurve.
"""
lc = LightCurve()
lc['TIME_MIN'] = [1, 4, 7, 9] * u.s
lc['TIME_MAX'] = [1, 4, 7, 9] * u.s
lc['FLUX'] = Quantity([1, 4, 7, 9], 'cm^-2 s^-1')
lc['FLUX_ERR'] = Quantity([0.1, 0.4, 0.7, 0.9], 'cm^-2 s^-1')
return lc
16 changes: 16 additions & 0 deletions gammapy/time/tests/test_lightcurve.py
@@ -0,0 +1,16 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import absolute_import, division, print_function, unicode_literals
from numpy.testing import assert_almost_equal
from astropy.units import Quantity
from astropy.tests.helper import assert_quantity_allclose
from ..lightcurve import LightCurve, make_example_lightcurve


def test_lightcurve():
lc = make_example_lightcurve()
flux_mean = lc['FLUX'].mean()
assert_quantity_allclose(flux_mean, Quantity(5.25,'cm^-2 s^-1'))

def test_lightcurve_plot():
lc = make_example_lightcurve()
lc.lc_plot()