Skip to content

Commit

Permalink
TST: Add a test case that fails
Browse files Browse the repository at this point in the history
currently but should pass when code is implemented.

Just simple test cases for add and sub for now. Will add more in later commits.

Also refactored some tests for arithmetic.
  • Loading branch information
pllim committed Dec 1, 2022
1 parent bad7d82 commit 79bd22f
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions specutils/tests/test_arithmetic.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
from copy import deepcopy

import astropy.units as u
from astropy.tests.helper import assert_quantity_allclose
import numpy as np
import pytest
from astropy.tests.helper import assert_quantity_allclose

from ..spectra.spectrum1d import Spectrum1D
from specutils.spectra.spectrum1d import Spectrum1D


def test_spectral_axes():
flux1 = (np.random.sample(49) * 100).astype(int)
flux2 = (np.random.sample(49) * 100).astype(int)
class TestMathWithAllOnes:
def setup_class(self):
flux = np.ones(10) * u.nJy
wave = (np.arange(flux.size) + 1) * u.um
self.spec = Spectrum1D(spectral_axis=wave, flux=flux)

flux3 = flux1 + flux2
def test_add_sub_spectral_axes_same(self):
spec_added = self.spec + self.spec
assert_quantity_allclose(spec_added.flux, 2 * u.nJy)

spec1 = Spectrum1D(spectral_axis=np.arange(1, 50) * u.nm,
flux=flux1 * u.Jy)
spec2 = Spectrum1D(spectral_axis=np.arange(1, 50) * u.nm,
flux=flux2 * u.Jy)
spec_subbed = self.spec - self.spec
assert_quantity_allclose(spec_subbed.flux, 0 * u.nJy)

spec3 = spec1 + spec2
def test_add_sub_spectral_axes_different(self):
new_wave = self.spec.spectral_axis + (1 * u.um)
new_spec = Spectrum1D(spectral_axis=new_wave, flux=self.spec.flux)

assert np.allclose(spec3.flux.value, flux3)
with pytest.raises(ValueError):
self.spec + new_spec

with pytest.raises(ValueError):
self.spec - new_spec

def test_mask_nans(self):
new_flux = deepcopy(self.spec.flux)
nan_idx = [1, 3, 5]
new_flux[nan_idx] = np.nan
new_spec = Spectrum1D(spectral_axis=self.spec.spectral_axis, flux=new_flux)
spec_added = self.spec + new_spec
assert spec_added.mask[nan_idx].all()


def test_add_basic_spectra(simulated_spectra):
Expand Down Expand Up @@ -105,19 +124,6 @@ def test_masks(simulated_spectra):
assert np.all(masked_diff.mask == masked_sum.mask | masked_spec.mask)


def test_mask_nans():
flux1 = np.random.random(10)
flux2 = np.random.random(10)
nan_idx = [1, 3, 5]
flux2[nan_idx] = np.nan
spec1 = Spectrum1D(spectral_axis=np.arange(10) * u.nm, flux=flux1 * u.Jy)
spec2 = Spectrum1D(spectral_axis=np.arange(10) * u.nm, flux=flux2 * u.Jy)

spec3 = spec1 + spec2

assert spec3.mask[nan_idx].all() == True # noqa


def test_with_constants(simulated_spectra):
spec = simulated_spectra.s1_um_mJy_e1

Expand Down

0 comments on commit 79bd22f

Please sign in to comment.