Skip to content

Commit

Permalink
Allow template correlation for spectrum with no uncertainty
Browse files Browse the repository at this point in the history
  • Loading branch information
rosteen committed Jan 8, 2024
1 parent fb5bd08 commit c3a772b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 7 additions & 3 deletions specutils/analysis/correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,12 @@ def _normalize(observed_spectrum, template_spectrum):
A float which will normalize the template spectrum's flux so that it
can be compared to the observed spectrum.
"""
unc = observed_spectrum.uncertainty.represent_as(StdDevUncertainty)
num = np.nansum((observed_spectrum.flux*template_spectrum.flux)/(unc.array**2))
denom = np.nansum((template_spectrum.flux/unc.array)**2)
if hasattr(observed_spectrum, "uncertainty") and observed_spectrum.uncertainty is not None:
unc = observed_spectrum.uncertainty.represent_as(StdDevUncertainty)
num = np.nansum((observed_spectrum.flux*template_spectrum.flux)/(unc.array**2))
denom = np.nansum((template_spectrum.flux/unc.array)**2)
else:
num = np.nansum(observed_spectrum.flux*template_spectrum.flux)
denom = np.nansum(template_spectrum.flux**2)

return num/denom
11 changes: 11 additions & 0 deletions specutils/tests/test_correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ def test_autocorrelation():
maximum = np.argmax(corr)
assert maximum == midpoint

# Test that this works without uncertainty
spec1 = Spectrum1D(spectral_axis=spec_axis,
flux=flux1,
velocity_convention='optical',
rest_value=5020.*u.AA)

corr, lag = correlation.template_correlate(spec1, spec2)

assert corr.unit == u.dimensionless_unscaled
assert lag.unit == u.km / u.s


def test_correlation():
"""
Expand Down

0 comments on commit c3a772b

Please sign in to comment.