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

Some updates to specutils.analysis functions #937

Merged
merged 2 commits into from
Jan 30, 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
12 changes: 8 additions & 4 deletions specutils/analysis/template_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ def _normalize_for_template_matching(observed_spectrum, template_spectrum, stdde
"""
if stddev is None:
stddev = observed_spectrum.uncertainty.represent_as(StdDevUncertainty).quantity
num = np.sum((observed_spectrum.flux*template_spectrum.flux) / (stddev**2))
denom = np.sum((template_spectrum.flux / stddev)**2)
num = np.nansum((observed_spectrum.flux*template_spectrum.flux) / (stddev**2))
# We need to limit this sum to where observed_spectrum is not NaN as well.
template_filtered = ((template_spectrum.flux / stddev)**2)
template_filtered = template_filtered[np.where(~np.isnan(observed_spectrum.flux))]
denom = np.nansum(template_filtered)

return num/denom

Expand Down Expand Up @@ -107,7 +110,7 @@ def _chi_square_for_templates(observed_spectrum, template_spectrum, resample_met

# Get chi square
result = (num/denom)**2
chi2 = np.sum(result.value)
chi2 = np.nansum(result.value)

# Create normalized template spectrum, which will be returned with
# corresponding chi2
Expand Down Expand Up @@ -262,5 +265,6 @@ def template_redshift(observed_spectrum, template_spectrum, redshift):
if not np.isnan(chi2) and (chi2_min is None or chi2 < chi2_min):
chi2_min = chi2
final_redshift = rs
final_spectrum = redshifted_spectrum

return redshifted_spectrum, final_redshift, normalized_spectral_template, chi2_min, chi2_list
return final_spectrum, final_redshift, normalized_spectral_template, chi2_min, chi2_list
8 changes: 4 additions & 4 deletions specutils/tests/test_template_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_template_match_no_overlap():

# Get result from template_match
tm_result = template_comparison.template_match(spec, spec1)
assert np.isnan(tm_result[3])
assert tm_result[3] == 0.0

# Create new spectrum for comparison
spec_result = Spectrum1D(spectral_axis=spec_axis,
Expand Down Expand Up @@ -65,15 +65,15 @@ def test_template_match_minimal_overlap():

# Get result from template_match
tm_result = template_comparison.template_match(spec, spec1)
assert np.isnan(tm_result[3])
assert tm_result[3] == 0.0

# Create new spectrum for comparison
spec_result = Spectrum1D(spectral_axis=spec_axis,
flux=spec1.flux * template_comparison._normalize_for_template_matching(spec, spec1))
try:
assert quantity_allclose(tm_result[0].flux, spec_result.flux, atol=0.01*u.Jy)
except AssertionError:
pytest.xfail('TODO: investigate why the all elements in tm_result[1] are NaN even with overlap')
pytest.xfail('TODO: investigate why all elements in tm_result[0] are NaN even with overlap')


def test_template_match_spectrum():
Expand Down Expand Up @@ -332,7 +332,7 @@ def test_template_redshift_with_multiple_template_spectra_in_match():
# TODO: Determine cause of and fix failing assert
# np.testing.assert_almost_equal(tm_result[1], redshift)

np.testing.assert_almost_equal(tm_result[3], 6803.922741644725)
np.testing.assert_almost_equal(tm_result[3], 1172.135458895792)

# When a spectrum collection is matched with a redshift
# grid, a list-of-lists is returned with the trial chi2
Expand Down
Loading