forked from gammapy/gammapy
/
test_fov_cube.py
125 lines (100 loc) · 4.55 KB
/
test_fov_cube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
from numpy.testing import assert_allclose
from astropy.tests.helper import assert_quantity_allclose, pytest
from astropy.units import Quantity
from astropy.coordinates import Angle
from ...utils.testing import requires_dependency, requires_data
from ...datasets import make_test_bg_cube_model
from ...data import DataStore
from ..fov_cube import FOVCube
def read_cube():
"""Read example cube"""
filename = '$GAMMAPY_EXTRA/test_datasets/background/bg_cube_model_test1.fits'
scheme = 'bg_cube'
cube = FOVCube.read(filename, format='table', scheme=scheme, hdu='BACKGROUND')
return cube
# TODO: broken code ... rewrite with cube class.
@pytest.mark.xfail
class TestCube:
@requires_data('gammapy-extra')
def test_read_fits_table(self):
cube = read_cube()
# test shape and scheme of cube when reading a file
assert len(cube.data.shape) == 3
assert cube.data.shape == (len(cube.energy_edges) - 1,
len(cube.coordy_edges) - 1,
len(cube.coordx_edges) - 1)
@requires_dependency('matplotlib')
def test_image_plot(self):
cube = make_test_bg_cube_model().background_cube
# test bg rate values plotted for image plot of energy bin
# conaining E = 2 TeV
energy = Quantity(2., 'TeV')
ax_im = cube.plot_image(energy)
# get plot data (stored in the image)
image_im = ax_im.get_images()[0]
plot_data = image_im.get_array()
# get data from bg model object to compare
energy_bin = cube.energy_edges.find_energy_bin(energy)
model_data = cube.data[energy_bin]
# test if both arrays are equal
assert_allclose(plot_data, model_data.value)
@requires_dependency('matplotlib')
def test_spectrum_plot(self):
cube = make_test_bg_cube_model().background_cube
# test bg rate values plotted for spectrum plot of coordinate bin
# conaining coord (0, 0) deg (center)
coord = Angle([0., 0.], 'deg')
ax_spec = cube.plot_spectrum(coord)
# get plot data (stored in the line)
plot_data = ax_spec.get_lines()[0].get_xydata()
# get data from bg model object to compare
coord_bin = cube.find_coord_bin(coord)
model_data = cube.data[:, coord_bin[1], coord_bin[0]]
# test if both arrays are equal
assert_allclose(plot_data[:, 1], model_data.value)
@requires_data('gammapy-extra')
def test_write_fits_table(self, tmpdir):
cube1 = read_cube()
outfile = str(tmpdir / 'cube_table_test.fits')
cube1.write(outfile, format='table')
# test if values are correct in the saved file: compare both files
cube2 = FOVCube.read(outfile, format='table', scheme='bg_cube')
assert_quantity_allclose(cube2.data,
cube1.data)
assert_quantity_allclose(cube2.coordx_edges,
cube1.coordx_edges)
assert_quantity_allclose(cube2.coordy_edges,
cube1.coordy_edges)
assert_quantity_allclose(cube2.energy_edges,
cube1.energy_edges)
@requires_data('gammapy-extra')
def test_read_write_fits_image(self, tmpdir):
cube1 = read_cube()
outfile = str(tmpdir / 'cube_image_test.fits')
cube1.write(outfile, format='image')
# test if values are correct in the saved file: compare both files
cube2 = FOVCube.read(outfile, format='image', scheme='bg_cube')
assert_quantity_allclose(cube2.data,
cube1.data)
assert_quantity_allclose(cube2.coordx_edges,
cube1.coordx_edges)
assert_quantity_allclose(cube2.coordy_edges,
cube1.coordy_edges)
assert_quantity_allclose(cube2.energy_edges,
cube1.energy_edges)
@pytest.fixture
def event_lists():
dir = '$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2'
data_store = DataStore.from_dir(dir)
event_lists = data_store.load_all('events')
return event_lists
@requires_data('gammapy-extra')
def test_fill_cube(event_lists):
array = read_cube()
array.data = Quantity(np.zeros_like(array.data.value), 'u')
array.fill_events(event_lists)
# TODO: implement test that correct bin is hit
assert array.data.value.sum() == 5967