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 quantity support to map coordinates #1685

Merged
merged 7 commits into from Aug 9, 2018
Prev

Add quantity test for Map.interp_by_coord

  • Loading branch information...
adonath committed Aug 9, 2018
commit a02e0dc73528c303cdb9636bf77590a225a3ac8b
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))
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))
ProTip! Use n and p to navigate between commits in a pull request.