Skip to content

Commit

Permalink
Merge pull request #82 from astrofrog/fix-spectral-coordinate-type
Browse files Browse the repository at this point in the history
Fixed return type for pixel <-> world conversions in SpectralCoordinates
  • Loading branch information
dhomeier authored Jan 19, 2023
2 parents 8d2a3e4 + 7be8779 commit 1c535cf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
17 changes: 13 additions & 4 deletions glue_astronomy/spectral_coordinates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np

from astropy.units import Quantity
from glue.core.coordinates import Coordinates

__all__ = ['SpectralCoordinates']
Expand All @@ -12,6 +13,8 @@ class SpectralCoordinates(Coordinates):
"""

def __init__(self, values):
if not isinstance(values, Quantity):
raise TypeError('values should be a Quantity instance')
self._index = np.arange(len(values))
self._values = values
super().__init__(n_dim=1)
Expand All @@ -32,8 +35,11 @@ def world_to_pixel_values(self, *world):
Returns
-------
"""
return tuple(np.interp(world, self._values.value, self._index,
left=np.nan, right=np.nan))
if len(world) > 1:
raise ValueError('SpectralCoordinates is a 1-d coordinate class '
'and only accepts a single scalar or array to convert')
return np.interp(world[0], self._values.value, self._index,
left=np.nan, right=np.nan)

def pixel_to_world_values(self, *pixel):
"""
Expand All @@ -43,5 +49,8 @@ def pixel_to_world_values(self, *pixel):
Returns
-------
"""
return tuple(np.interp(pixel, self._index, self._values.value,
left=np.nan, right=np.nan))
if len(pixel) > 1:
raise ValueError('SpectralCoordinates is a 1-d coordinate class '
'and only accepts a single scalar or array to convert')
return np.interp(pixel[0], self._index, self._values.value,
left=np.nan, right=np.nan)
Empty file.
35 changes: 35 additions & 0 deletions glue_astronomy/tests/test_spectral_coordinates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest
import numpy as np
from astropy import units as u
from numpy.testing import assert_allclose
from glue_astronomy.spectral_coordinates import SpectralCoordinates


def test_basic():

sc = SpectralCoordinates([10, 20, 30] * u.Hz)

assert_allclose(sc.pixel_to_world_values(0), 10)
assert_allclose(sc.pixel_to_world_values([0, 1]), [10, 20])
assert_allclose(sc.pixel_to_world_values([-0.5, 0, 0.5, 1, 10]),
[np.nan, 10, 15, 20, np.nan])

assert_allclose(sc.world_to_pixel_values(10), 0)
assert_allclose(sc.world_to_pixel_values([10, 15, 20]), [0, 0.5, 1.0])


def test_invalid_init():

with pytest.raises(TypeError, match='values should be a Quantity instance'):
SpectralCoordinates([10, 20, 30])


def test_invalid_conversion():

sc = SpectralCoordinates([10, 20, 30] * u.Hz)

with pytest.raises(ValueError, match='SpectralCoordinates is a 1-d coordinate class'):
sc.pixel_to_world_values(1, 2)

with pytest.raises(ValueError, match='SpectralCoordinates is a 1-d coordinate class'):
sc.world_to_pixel_values(1, 2)
2 changes: 1 addition & 1 deletion glue_astronomy/translators/tests/test_spectrum1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_to_spectrum1d_with_spectral_coordinates():
data.add_component(Component(np.array([3, 4, 5]), units='Jy'), 'x')

assert_allclose(data.coords.pixel_to_world_values([0, 0.5, 1, 1.5, 2]),
[[1, 2.5, 4, 7, 10]])
[1, 2.5, 4, 7, 10])

spec = data.get_object(Spectrum1D, attribute=data.id['x'])
assert_quantity_allclose(spec.spectral_axis, [1, 4, 10] * u.micron)
Expand Down

0 comments on commit 1c535cf

Please sign in to comment.