Skip to content
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

Fixed return type and shape for pixel <-> world conversions in SpectralCoordinates #82

Merged
merged 3 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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