Permalink
Browse files

Add quantity test for Map.interp_by_coord

  • Loading branch information...
adonath committed Aug 9, 2018
1 parent 4ec4362 commit a02e0dc73528c303cdb9636bf77590a225a3ac8b
Showing with 26 additions and 15 deletions.
  1. +6 −6 gammapy/maps/geom.py
  2. +2 −7 gammapy/maps/tests/test_geom.py
  3. +18 −2 gammapy/maps/tests/test_wcsnd.py
View
@@ -637,6 +637,9 @@ def __init__(self, data, coordsys=None, match_by_name=True):
if 'lon' not in data or 'lat' not in data:
raise ValueError("data dictionary must contain axes named 'lon' and 'lat'.")
if issubclass(data['lon'].__class__, u.Quantity) or issubclass(data['lat'].__class__, u.Quantity):
raise ValueError('No quantities supported.')
data = OrderedDict([
(k, np.atleast_1d(np.asanyarray(v)))
for k, v in data.items()
@@ -764,7 +767,7 @@ def _from_tuple(cls, coords, coordsys=None):
raise TypeError('Type not supported: {}'.format(type(coords)))
@classmethod
def _from_dict(cls, coords, coordsys=None, copy=False):
def _from_dict(cls, coords, coordsys=None):
"""Create from a dictionary of coordinate vectors."""
if 'lon' in coords and 'lat' in coords:
return cls(coords, coordsys=coordsys)
@@ -784,7 +787,7 @@ def _from_dict(cls, coords, coordsys=None, copy=False):
"or 'skycoord'.")
@classmethod
def create(cls, data, coordsys=None, copy=False):
def create(cls, data, coordsys=None):
"""Create a new `~MapCoord` object.
This method can be used to create either unnamed (with tuple input)
@@ -795,12 +798,9 @@ def create(cls, data, coordsys=None, copy=False):
data : `tuple`, `dict`, `~MapCoord` or `~astropy.coordinates.SkyCoord`
Object containing coordinate arrays.
coordsys : {'CEL', 'GAL', None}, optional
Set the coordinate system for longitude and latitude. If
Set the coordinate system for longitude and latitude. If
None longitude and latitude will be assumed to be in
the coordinate system native to a given map geometry.
copy : bool
Make copies of the input coordinate arrays. If False this
object will store views.
Examples
--------
@@ -176,8 +176,7 @@ def test_mapcoords_create():
assert coords.ndim == 3
# 3D OrderedDict w/ vectors
coords = MapCoord.create(OrderedDict([('energy', energy),
('lat', lat), ('lon', lon)]))
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)
@@ -187,12 +186,8 @@ def test_mapcoords_create():
assert coords.ndim == 3
# Quantities
coords = MapCoord.create(OrderedDict([('energy', energy * u.TeV),
('lat', lat * u.deg), ('lon', lon * u.deg)]))
coords = MapCoord.create(dict(energy=energy * u.TeV, lat=lat, lon=lon))

This comment has been minimized.

Show comment
Hide comment
@cdeil

cdeil Aug 9, 2018

Member

I'll change back to OrderedDict here in this one case: if we assert on coord order, it has to be passed in by OrderedDict for now.

See https://ci.appveyor.com/project/cdeil/gammapy/build/1.0.4188/job/x78c82dy8mhgvtc5

@cdeil

cdeil Aug 9, 2018

Member

I'll change back to OrderedDict here in this one case: if we assert on coord order, it has to be passed in by OrderedDict for now.

See https://ci.appveyor.com/project/cdeil/gammapy/build/1.0.4188/job/x78c82dy8mhgvtc5

This comment has been minimized.

Show comment
Hide comment
@cdeil

cdeil Aug 9, 2018

Member

Should be fixed by 19bb755

@cdeil

cdeil Aug 9, 2018

Member

Should be fixed by 19bb755

assert coords['energy'].unit == 'TeV'
assert coords['lon'].unit == 'deg'
assert coords['lat'].unit == 'deg'
def test_mapcoords_to_coordsys():
@@ -197,8 +197,8 @@ def test_set_get_by_coord_quantities():
geom = WcsGeom.create(binsz=0.1, npix=(3, 4), axes=[ax])
m = WcsNDMap(geom)
coords_dict = {
'lon': 0 * u.deg,
'lat': 0 * u.deg,
'lon': 0,
'lat': 0,
'energy': 1000 * u.GeV
}
@@ -263,6 +263,22 @@ def test_wcsndmap_interp_by_coord(npix, binsz, coordsys, proj, skydir, axes):
assert_allclose(coords[1], m.interp_by_coord(coords, interp='cubic'))
def test_interp_by_coord_quantities():
ax = MapAxis(np.logspace(0., 3., 3), interp='log', name='energy', unit='TeV')
geom = WcsGeom.create(binsz=0.1, npix=(3, 3), axes=[ax])
m = WcsNDMap(geom)
coords_dict = {
'lon': 0,
'lat': 0,
'energy': 1000 * u.GeV
}
m.set_by_coord(coords_dict, 42)
coords_dict['energy'] = 1 * u.TeV
assert_allclose(42, m.interp_by_coord(coords_dict, interp='nearest'))
def test_wcsndmap_interp_by_coord_fill_value():
# Introduced in https://github.com/gammapy/gammapy/pull/1559/files
m = Map.create(npix=(20, 10))

0 comments on commit a02e0dc

Please sign in to comment.