forked from gammapy/gammapy
/
test_geom.py
235 lines (196 loc) · 8.4 KB
/
test_geom.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import absolute_import, division, print_function, unicode_literals
import pytest
from collections import OrderedDict
import numpy as np
from numpy.testing import assert_allclose
from astropy import units as u
from astropy.coordinates import SkyCoord
from ..geom import MapAxis, MapCoord
pytest.importorskip('scipy')
mapaxis_geoms = [
(np.array([0.25, 0.75, 1.0, 2.0]), 'lin'),
(np.array([0.25, 0.75, 1.0, 2.0]), 'log'),
(np.array([0.25, 0.75, 1.0, 2.0]), 'sqrt'),
]
mapaxis_geoms_node_type = [
(np.array([0.25, 0.75, 1.0, 2.0]), 'lin', 'edge'),
(np.array([0.25, 0.75, 1.0, 2.0]), 'log', 'edge'),
(np.array([0.25, 0.75, 1.0, 2.0]), 'sqrt', 'edge'),
(np.array([0.25, 0.75, 1.0, 2.0]), 'lin', 'center'),
(np.array([0.25, 0.75, 1.0, 2.0]), 'log', 'center'),
(np.array([0.25, 0.75, 1.0, 2.0]), 'sqrt', 'center'),
]
@pytest.mark.parametrize(('edges', 'interp'),
mapaxis_geoms)
def test_mapaxis_init_from_edges(edges, interp):
axis = MapAxis(edges, interp=interp)
assert_allclose(axis.edges, edges)
assert_allclose(axis.nbin, len(edges) - 1)
@pytest.mark.parametrize(('nodes', 'interp'),
mapaxis_geoms)
def test_mapaxis_from_nodes(nodes, interp):
axis = MapAxis.from_nodes(nodes, interp=interp)
assert_allclose(axis.center, nodes)
assert_allclose(axis.nbin, len(nodes))
@pytest.mark.parametrize(('nodes', 'interp'),
mapaxis_geoms)
def test_mapaxis_from_bounds(nodes, interp):
axis = MapAxis.from_bounds(nodes[0], nodes[-1], 3,
interp=interp)
assert_allclose(axis.edges[0], nodes[0])
assert_allclose(axis.edges[-1], nodes[-1])
assert_allclose(axis.nbin, 3)
@pytest.mark.parametrize(('nodes', 'interp', 'node_type'),
mapaxis_geoms_node_type)
def test_mapaxis_pix_to_coord(nodes, interp, node_type):
axis = MapAxis(nodes, interp=interp, node_type=node_type)
assert_allclose(axis.center,
axis.pix_to_coord(np.arange(axis.nbin, dtype=float)))
assert_allclose(np.arange(axis.nbin + 1, dtype=float) - 0.5,
axis.coord_to_pix(axis.edges))
@pytest.mark.parametrize(('nodes', 'interp', 'node_type'),
mapaxis_geoms_node_type)
def test_mapaxis_coord_to_idx(nodes, interp, node_type):
axis = MapAxis(nodes, interp=interp, node_type=node_type)
assert_allclose(np.arange(axis.nbin, dtype=int),
axis.coord_to_idx(axis.center))
@pytest.mark.parametrize(('nodes', 'interp', 'node_type'),
mapaxis_geoms_node_type)
def test_mapaxis_slice(nodes, interp, node_type):
axis = MapAxis(nodes, interp=interp, node_type=node_type)
saxis = axis.slice(slice(1, 3))
assert_allclose(saxis.nbin, 2)
assert_allclose(saxis.center, axis.center[slice(1, 3)])
axis = MapAxis(nodes, interp=interp, node_type=node_type)
saxis = axis.slice(slice(1, None))
assert_allclose(saxis.nbin, axis.nbin - 1)
assert_allclose(saxis.center, axis.center[slice(1, None)])
axis = MapAxis(nodes, interp=interp, node_type=node_type)
saxis = axis.slice(slice(None, 2))
assert_allclose(saxis.nbin, 2)
assert_allclose(saxis.center, axis.center[slice(None, 2)])
axis = MapAxis(nodes, interp=interp, node_type=node_type)
saxis = axis.slice(slice(None, -1))
assert_allclose(saxis.nbin, axis.nbin - 1)
assert_allclose(saxis.center, axis.center[slice(None, -1)])
def test_mapcoords_create():
# From existing MapCoord
coords_cel = MapCoord.create((0.0, 1.0), coordsys='CEL')
coords_gal = MapCoord.create(coords_cel, coordsys='GAL')
assert_allclose(coords_gal.lon, coords_cel.skycoord.galactic.l.deg)
assert_allclose(coords_gal.lat, coords_cel.skycoord.galactic.b.deg)
# 2D Tuple of scalars
coords = MapCoord.create((0.0, 1.0))
assert_allclose(coords.lon, 0.0)
assert_allclose(coords.lat, 1.0)
assert_allclose(coords[0], 0.0)
assert_allclose(coords[1], 1.0)
assert coords.coordsys is None
assert coords.ndim == 2
# 3D Tuple of scalars
coords = MapCoord.create((0.0, 1.0, 2.0))
assert_allclose(coords[0], 0.0)
assert_allclose(coords[1], 1.0)
assert_allclose(coords[2], 2.0)
assert coords.coordsys is None
assert coords.ndim == 3
# 2D Tuple w/ NaN coordinates
coords = MapCoord.create((np.nan, np.nan))
# 2D Tuple w/ NaN coordinates
lon, lat = np.array([np.nan, 1.0]), np.array([np.nan, 3.0])
coords = MapCoord.create((lon, lat))
assert_allclose(coords.lon, lon)
assert_allclose(coords.lat, lat)
# 2D Tuple w/ SkyCoord
lon, lat = np.array([0.0, 1.0]), np.array([2.0, 3.0])
energy = np.array([100., 1000.])
skycoord_cel = SkyCoord(lon, lat, unit='deg', frame='icrs')
skycoord_gal = SkyCoord(lon, lat, unit='deg', frame='galactic')
coords = MapCoord.create((skycoord_cel,))
assert_allclose(coords.lon, lon)
assert_allclose(coords.lat, lat)
assert coords.coordsys == 'CEL'
assert coords.ndim == 2
coords = MapCoord.create((skycoord_gal,))
assert_allclose(coords.lon, lon)
assert_allclose(coords.lat, lat)
assert coords.coordsys == 'GAL'
assert coords.ndim == 2
# SkyCoord
coords = MapCoord.create(skycoord_cel)
assert_allclose(coords.lon, lon)
assert_allclose(coords.lat, lat)
assert coords.coordsys == 'CEL'
assert coords.ndim == 2
coords = MapCoord.create(skycoord_gal)
assert_allclose(coords.lon, lon)
assert_allclose(coords.lat, lat)
assert coords.coordsys == 'GAL'
assert coords.ndim == 2
# 2D Dict w/ vectors
coords = MapCoord.create(dict(lon=lon, lat=lat))
assert_allclose(coords.lon, lon)
assert_allclose(coords.lat, lat)
assert coords.ndim == 2
# 3D Dict w/ vectors
coords = MapCoord.create(dict(lon=lon, lat=lat, energy=energy))
assert_allclose(coords.lon, lon)
assert_allclose(coords.lat, lat)
assert_allclose(coords['energy'], energy)
assert coords.ndim == 3
# 3D Dict w/ SkyCoord
coords = MapCoord.create(dict(skycoord=skycoord_cel, energy=energy))
assert_allclose(coords.lon, lon)
assert_allclose(coords.lat, lat)
assert_allclose(coords['energy'], energy)
assert coords.ndim == 3
# 3D OrderedDict w/ vectors
coords = MapCoord.create(dict(energy=energy, lat=lat, lon=lon))
assert_allclose(coords.lon, lon)
assert_allclose(coords.lat, lat)
assert_allclose(coords['energy'], energy)
assert_allclose(coords[0], energy)
assert_allclose(coords[1], lat)
assert_allclose(coords[2], lon)
assert coords.ndim == 3
# Quantities
coords = MapCoord.create(dict(energy=energy * u.TeV, lat=lat, lon=lon))
assert coords['energy'].unit == 'TeV'
def test_mapcoords_to_coordsys():
lon, lat = np.array([0.0, 1.0]), np.array([2.0, 3.0])
energy = np.array([100., 1000.])
skycoord_cel = SkyCoord(lon, lat, unit='deg', frame='icrs')
skycoord_gal = SkyCoord(lon, lat, unit='deg', frame='galactic')
coords = MapCoord.create(
dict(lon=lon, lat=lat, energy=energy), coordsys='CEL')
assert coords.coordsys == 'CEL'
assert_allclose(coords.skycoord.transform_to(
'icrs').ra.deg, skycoord_cel.ra.deg)
assert_allclose(coords.skycoord.transform_to(
'icrs').dec.deg, skycoord_cel.dec.deg)
coords = coords.to_coordsys('GAL')
assert coords.coordsys == 'GAL'
assert_allclose(coords.skycoord.transform_to(
'galactic').l.deg, skycoord_cel.galactic.l.deg)
assert_allclose(coords.skycoord.transform_to(
'galactic').b.deg, skycoord_cel.galactic.b.deg)
coords = MapCoord.create(
dict(lon=lon, lat=lat, energy=energy), coordsys='GAL')
assert coords.coordsys == 'GAL'
assert_allclose(coords.skycoord.transform_to(
'galactic').l.deg, skycoord_gal.l.deg)
assert_allclose(coords.skycoord.transform_to(
'galactic').b.deg, skycoord_gal.b.deg)
coords = coords.to_coordsys('CEL')
assert coords.coordsys == 'CEL'
assert_allclose(coords.skycoord.transform_to(
'icrs').ra.deg, skycoord_gal.icrs.ra.deg)
assert_allclose(coords.skycoord.transform_to(
'icrs').dec.deg, skycoord_gal.icrs.dec.deg)
def test_mapaxis_repr():
axis = MapAxis([1, 2, 3], name='test')
assert 'MapAxis' in repr(axis)
def test_mapcoord_repr():
coord = MapCoord({'lon': 0, 'lat': 0, 'energy': 5})
assert 'MapCoord' in repr(coord)