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 Background 2D class #1312
Add Background 2D class #1312
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,15 @@ | |
from __future__ import absolute_import, division, print_function, unicode_literals | ||
import pytest | ||
import astropy.units as u | ||
import numpy as np | ||
from numpy.testing import assert_allclose, assert_equal | ||
from astropy.tests.helper import assert_quantity_allclose | ||
from astropy.coordinates import Angle | ||
from astropy.io import fits | ||
from ...utils.testing import requires_dependency, requires_data | ||
from ..background import Background3D | ||
from ..background import Background3D, Background2D | ||
from ...utils.fits import table_to_fits_table | ||
from ...utils.energy import EnergyBounds | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
|
@@ -49,3 +54,58 @@ def test_background_3d_write(bkg_3d): | |
hdu = table_to_fits_table(bkg_3d.to_table()) | ||
assert_equal(hdu.data['DETX_LO'][0], bkg_3d.data.axis('detx').lo.value) | ||
assert hdu.header['TUNIT1'] == bkg_3d.data.axis('detx').lo.unit | ||
|
||
|
||
def make_test_array(): | ||
# Create a dummy `Background2D` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to have an even simpler test object. (even less code, easier to think about in the test methods below) How about this?
This is small, i.e. fast. And it's less code. And it's simpler to see what the expected value is in evaluate calls, if you choose a few points carefully (e.g. at a node and in the middle between two nodes, and throw in a third case that leads to extrapolation (e.g. offset=0) to establish behaviour). I think we should do all our IRF tests like this: have super-simple test cases and fill them with data values that is easy and generic to write when making the test case, and also to know what to expect in evaluate. |
||
energy = [1, 10, 100] * u.TeV | ||
offset = [0, 1, 2, 3] * u.deg | ||
data = np.zeros((len(energy)-1,len(offset)-1)) * u.Unit('s-1 MeV-1 sr-1') | ||
data.value[1,0]=2 | ||
data.value[1,1]=4 | ||
return Background2D( | ||
energy_lo=energy[:-1], energy_hi=energy[1:], | ||
offset_lo=offset[:-1], offset_hi=offset[1:], | ||
data=data, | ||
) | ||
|
||
|
||
def test_background2d_read_write(tmpdir): | ||
bkg_2d_1 = make_test_array() | ||
filename = str(tmpdir / "bkg2d.fits") | ||
prim_hdu = fits.PrimaryHDU() | ||
hdu_bkg = bkg_2d_1.to_fits() | ||
hdulist = fits.HDUList([prim_hdu, hdu_bkg]) | ||
hdulist.writeto(filename) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cdeil |
||
bkg_2d_2 = Background2D.read(filename) | ||
|
||
axis = bkg_2d_2.data.axis('energy') | ||
assert axis.nbins == 2 | ||
assert axis.unit == 'TeV' | ||
|
||
axis = bkg_2d_2.data.axis('offset') | ||
assert axis.nbins == 3 | ||
assert axis.unit == 'deg' | ||
|
||
data = bkg_2d_2.data.data | ||
assert data.shape == (2, 3) | ||
assert data.unit == u.Unit('s-1 MeV-1 sr-1') | ||
|
||
|
||
@requires_dependency('scipy') | ||
def test_background2d_evaluate(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs a |
||
bkg_2d = make_test_array() | ||
data_unit = u.Unit('s-1 MeV-1 sr-1') | ||
|
||
# Interoplate at the energy and offset bin of the bgk_2d axes | ||
off = bkg_2d.data.axis('offset').nodes | ||
e_reco = bkg_2d.data.axis('energy').nodes | ||
res = bkg_2d.evaluate(fov_offset=off, energy_reco=e_reco) | ||
assert_quantity_allclose(res, bkg_2d.data.data) | ||
|
||
#Test linear interpolation for offset | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cdeil There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I would manually compute |
||
off_tab=Angle(np.array([1]),"deg") | ||
e_reco_tab=bkg_2d.data.axis('energy').nodes[1] | ||
res = bkg_2d.evaluate(fov_offset=off_tab, energy_reco=e_reco_tab) | ||
assert_quantity_allclose(res, 3 * data_unit) | ||
|
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.
3d -> 2d