Skip to content

Commit

Permalink
Merge aead98a into 7eaeb57
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Jones committed Sep 9, 2019
2 parents 7eaeb57 + aead98a commit ff4a2c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
12 changes: 12 additions & 0 deletions specutils/spectra/spectrum1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def __init__(self, flux=None, spectral_axis=None, wcs=None,
raise LookupError("No WCS object or spectral axis information has "
"been given. Please provide one.")

# Check to make sure the wavelength length is the same in both
if flux is not None and spectral_axis is not None:
if not spectral_axis.shape[0] == flux.shape[-1]:
raise ValueError('Spectral axis ({}) and flux axis ({}) lengths must be the same'.format(
spectral_axis.shape[0], flux.shape[-1]))

self._velocity_convention = velocity_convention

if rest_value is None:
Expand All @@ -112,6 +118,12 @@ def __init__(self, flux=None, spectral_axis=None, wcs=None,
data=flux.value if isinstance(flux, u.Quantity) else flux,
wcs=wcs, **kwargs)

if hasattr(self, 'uncertainty') and self.uncertainty is not None:
if not flux.shape == self.uncertainty.array.shape:
raise ValueError('Flux axis ({}) and uncertainty ({}) shapes must be the same.'.format(
flux.shape, self.uncertainty.array.shape))


@property
def frequency(self):
"""
Expand Down
22 changes: 21 additions & 1 deletion specutils/tests/test_spectrum1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def test_create_from_multidimensional_arrays():
assert (spec.frequency == freqs).all()
assert (spec.flux == flux).all()

# Mis-matched lengths should raise an exception
freqs = np.arange(50) * u.GHz
flux = np.random.random((5, len(freqs)-1)) * u.Jy
with pytest.raises(ValueError) as e_info:
spec = Spectrum1D(spectral_axis=freqs, flux=flux)

def test_create_from_quantities():
spec = Spectrum1D(spectral_axis=np.arange(1, 50) * u.nm,
Expand All @@ -54,6 +59,11 @@ def test_create_from_quantities():
assert spec.spectral_axis.unit == u.nm
assert spec.spectral_axis.size == 49

# Mis-matched lengths should raise an exception
with pytest.raises(ValueError) as e_info:
spec = Spectrum1D(spectral_axis=np.arange(1, 50) * u.nm,
flux=np.random.randn(48) * u.Jy)


def test_create_implicit_wcs():
spec = Spectrum1D(spectral_axis=np.arange(50) * u.AA,
Expand Down Expand Up @@ -205,6 +215,16 @@ def test_create_with_uncertainty():

assert spec.flux.unit == spec.uncertainty.unit

# If flux and uncertainty are different sizes then raise exception
wavelengths = np.arange(0, 10)
flux=100*np.abs(np.random.randn(3, 4, 10))*u.Jy
uncertainty = StdDevUncertainty(np.abs(np.random.randn(3, 2, 10))*u.Jy)

with pytest.raises(ValueError) as e_info:
s1d = Spectrum1D(spectral_axis=wavelengths*u.um,
flux=flux,
uncertainty=uncertainty)


@remote_access([{'id': '1481190', 'filename': 'L5g_0355+11_Cruz09.fits'}])
def test_read_linear_solution(remote_data_path):
Expand Down Expand Up @@ -237,7 +257,7 @@ def test_repr():
spec_with_unc = Spectrum1D(spectral_axis=np.linspace(100, 1000, 10) * u.nm,
flux=np.random.random(10) * u.Jy,
uncertainty=StdDevUncertainty(
np.random.sample(50), unit='Jy'))
np.random.sample(10), unit='Jy'))
result = repr(spec_with_unc)
assert result.startswith('<Spectrum1D(flux=<Quantity [')
assert 'spectral_axis=<Quantity [' in result
Expand Down

0 comments on commit ff4a2c5

Please sign in to comment.