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

test doppler shift #26

Merged
merged 1 commit into from
Aug 18, 2024
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
68 changes: 68 additions & 0 deletions src/scope/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,71 @@ def test_perform_pca_variance_removed_compared_input_diffcompoents(self):
assert np.std(scaled_cube10) < np.std(
scaled_cube5
) # the variance should be contained in the main components


class TestDopplerShift(unittest.TestCase):
def test_same_after_shift(self):
"""
shouldn't globally change the properties. should really just shift things.
"""
eval_wave = np.linspace(1.1, 1.8, 10000) * 1e-6 # meters.
template_wave = np.linspace(1, 2, 10000) * 1e-6

template_flux = np.random.random(10000)
interped_flux = np.interp(eval_wave, template_wave, template_flux)
v = 1e-6 # m/s. should not change much

shifted_flux = calc_doppler_shift(eval_wave, template_wave, template_flux, v)
# test that shifted flux and interpred flux are very similar
assert (
np.testing.assert_allclose(shifted_flux, interped_flux, rtol=1e-2) == None
)

def test_diff_after_shift(self):
"""
shouldn't globally change the properties. should really just shift things.
"""
eval_wave = np.linspace(1.1, 1.8, 10000) * 1e-6 # meters.
template_wave = np.linspace(1, 2, 10000) * 1e-6

template_flux = np.random.random(10000)
interped_flux = np.interp(eval_wave, template_wave, template_flux)
v = 2e3 # m/s. should not change much

shifted_flux = calc_doppler_shift(eval_wave, template_wave, template_flux, v)
# test that shifted flux and interpred flux are very similar
np.testing.assert_raises(
AssertionError, np.testing.assert_array_equal, shifted_flux, interped_flux
)

def test_gaussian_shifted_red(self):
"""
shouldn't globally change the properties. should really just shift things.
"""
eval_wave = np.linspace(1.1, 1.8, 10000) * 1e-6
template_wave = np.linspace(1, 2, 10000) * 1e-6
# make a gaussian centered at 1.5
template_flux = np.exp(-0.5 * ((template_wave - 1.5e-6) / 0.01) ** 2)
interped_flux = np.interp(eval_wave, template_wave, template_flux)

v = -5e4 # m/s. should not change much
shifted_flux = calc_doppler_shift(eval_wave, template_wave, template_flux, v)
wav_max_shifted = eval_wave[np.argmax(shifted_flux)]
wav_max = eval_wave[np.argmax(interped_flux)]
assert wav_max_shifted > wav_max

def test_gaussian_shifted_blue(self):
"""
shouldn't globally change the properties. should really just shift things.
"""
eval_wave = np.linspace(1.1, 1.8, 10000) * 1e-6
template_wave = np.linspace(1, 2, 10000) * 1e-6
# make a gaussian centered at 1.5
template_flux = np.exp(-0.5 * ((template_wave - 1.5e-6) / 0.01) ** 2)

v = 5e4 # m/s. should not change much
shifted_flux = calc_doppler_shift(eval_wave, template_wave, template_flux, v)
interped_flux = np.interp(eval_wave, template_wave, template_flux)
wav_max_shifted = eval_wave[np.argmax(shifted_flux)]
wav_max = eval_wave[np.argmax(interped_flux)]
assert wav_max_shifted < wav_max
1 change: 1 addition & 0 deletions src/scope/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def perform_pca(input_matrix, n_princ_comp, return_noplanet=False):
def calc_doppler_shift(eval_wave, template_wave, template_flux, v):
"""
Doppler shifts a spectrum. Evaluates the flux at a different grid.
convention: negative v is redshift.
Inputs
------
Expand Down
Loading