-
-
Notifications
You must be signed in to change notification settings - Fork 124
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
Template redshift enhancements #551
Changes from 11 commits
a31c0cb
2cf9bb8
f39854f
76f5ebe
ff48040
ff68e70
94b8bba
2adc0f3
19e2ec0
8bb7890
0e643a3
be05679
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -114,7 +114,7 @@ def _chi_square_for_templates(observed_spectrum, template_spectrum, resample_met | |||||||||||
|
||||||||||||
def template_match(observed_spectrum, spectral_templates, | ||||||||||||
resample_method="flux_conserving", known_redshift=None, | ||||||||||||
min_redshift=None, max_redshift=None, delta_redshift=None): | ||||||||||||
redshift=None): | ||||||||||||
""" | ||||||||||||
Find which spectral templates is the best fit to an observed spectrum by | ||||||||||||
computing the chi-squared. If two template_spectra have the same chi2, the | ||||||||||||
|
@@ -135,12 +135,10 @@ def template_match(observed_spectrum, spectral_templates, | |||||||||||
known_redshift: `float` | ||||||||||||
If the user knows the redshift they want to apply to the spectrum/spectra within spectral_templates, | ||||||||||||
then this redshift can be applied to each template before attempting the match. | ||||||||||||
min_redshift : `float` | ||||||||||||
The minimum redshift allowed. | ||||||||||||
max_redshift : `float` | ||||||||||||
The maximum redshift allowed. | ||||||||||||
delta_redshift : `float` | ||||||||||||
The amount the redshift will change between loops. | ||||||||||||
redshift : `list`, `tuple`, 'numpy.array` | ||||||||||||
An iterable with redshift values to be applied to each template, before computation of the | ||||||||||||
corresponding chi2 value. For each template, the redshift value that results in the smallest chi2 | ||||||||||||
is used. | ||||||||||||
|
||||||||||||
Returns | ||||||||||||
------- | ||||||||||||
|
@@ -151,17 +149,19 @@ def template_match(observed_spectrum, spectral_templates, | |||||||||||
normalized template spectrum. | ||||||||||||
smallest_chi_index : `int` | ||||||||||||
The index of the spectrum with the smallest chi2 in spectral templates. | ||||||||||||
chi2_list : `list` | ||||||||||||
A list with the best-fit chi2 values found for each template spectrum. | ||||||||||||
""" | ||||||||||||
if hasattr(spectral_templates, 'flux') and len(spectral_templates.flux.shape) == 1: | ||||||||||||
|
||||||||||||
# Account for redshift if provided | ||||||||||||
if all(x is not None for x in (min_redshift, max_redshift, delta_redshift)): | ||||||||||||
redshift, redshifted_spectrum = template_redshift(observed_spectrum, spectral_templates, | ||||||||||||
min_redshift, max_redshift, delta_redshift) | ||||||||||||
if redshift is not None and all(x is not None for x in redshift): | ||||||||||||
_, redshifted_spectrum, _ = template_redshift(observed_spectrum, spectral_templates, | ||||||||||||
redshift=redshift) | ||||||||||||
spectral_templates = redshifted_spectrum | ||||||||||||
elif known_redshift: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (This |
||||||||||||
redshift, redshifted_spectrum = template_redshift(observed_spectrum, spectral_templates, | ||||||||||||
known_redshift, known_redshift, 1.0) | ||||||||||||
_, redshifted_spectrum, _ = template_redshift(observed_spectrum, spectral_templates, | ||||||||||||
redshift=(known_redshift,)) | ||||||||||||
spectral_templates = redshifted_spectrum | ||||||||||||
|
||||||||||||
normalized_spectral_template, chi2 = _chi_square_for_templates( | ||||||||||||
|
@@ -175,32 +175,35 @@ def template_match(observed_spectrum, spectral_templates, | |||||||||||
# chi square. | ||||||||||||
chi2_min = None | ||||||||||||
smallest_chi_spec = None | ||||||||||||
chi2_list = [] | ||||||||||||
|
||||||||||||
for index, spectrum in enumerate(spectral_templates): | ||||||||||||
|
||||||||||||
# Account for redshift if provided | ||||||||||||
if all(x is not None for x in (min_redshift, max_redshift, delta_redshift)): | ||||||||||||
redshift, redshifted_spectrum = template_redshift(observed_spectrum, spectrum, | ||||||||||||
min_redshift, max_redshift, delta_redshift) | ||||||||||||
if redshift is not None and all(x is not None for x in redshift): | ||||||||||||
_, redshifted_spectrum, _ = template_redshift(observed_spectrum, spectrum, | ||||||||||||
redshift=redshift) | ||||||||||||
spectrum = redshifted_spectrum | ||||||||||||
|
||||||||||||
elif known_redshift: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Again, can be removed.) |
||||||||||||
redshift, redshifted_spectrum = template_redshift(observed_spectrum, spectrum, | ||||||||||||
known_redshift, known_redshift, 1.0) | ||||||||||||
_, redshifted_spectrum, _ = template_redshift(observed_spectrum, spectrum, | ||||||||||||
redshift=(known_redshift,)) | ||||||||||||
spectrum = redshifted_spectrum | ||||||||||||
|
||||||||||||
normalized_spectral_template, chi2 = _chi_square_for_templates( | ||||||||||||
observed_spectrum, spectrum, resample_method) | ||||||||||||
|
||||||||||||
chi2_list.append(chi2) | ||||||||||||
|
||||||||||||
if chi2_min is None or chi2 < chi2_min: | ||||||||||||
chi2_min = chi2 | ||||||||||||
smallest_chi_spec = normalized_spectral_template | ||||||||||||
smallest_chi_index = index | ||||||||||||
|
||||||||||||
return smallest_chi_spec, chi2_min, smallest_chi_index | ||||||||||||
return smallest_chi_spec, chi2_min, smallest_chi_index, chi2_list | ||||||||||||
|
||||||||||||
|
||||||||||||
def template_redshift(observed_spectrum, template_spectrum, min_redshift, max_redshift, delta_redshift): | ||||||||||||
def template_redshift(observed_spectrum, template_spectrum, redshift): | ||||||||||||
""" | ||||||||||||
Find the best-fit redshift for template_spectrum to match observed_spectrum using chi2. | ||||||||||||
|
||||||||||||
|
@@ -210,12 +213,8 @@ def template_redshift(observed_spectrum, template_spectrum, min_redshift, max_re | |||||||||||
The observed spectrum. | ||||||||||||
template_spectrum : :class:`~specutils.Spectrum1D` | ||||||||||||
The template spectrum, which will have it's redshift calculated. | ||||||||||||
min_redshift : `float` | ||||||||||||
The minimum redshift allowed. | ||||||||||||
max_redshift : `float` | ||||||||||||
The maximum redshift allowed. | ||||||||||||
delta_redshift : `float` | ||||||||||||
The amount the redshift will change between loops. | ||||||||||||
redshift : `list`, `tuple`, 'numpy.array` | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
An iterable with the redshift values to test. | ||||||||||||
|
||||||||||||
Returns | ||||||||||||
------- | ||||||||||||
|
@@ -224,29 +223,28 @@ def template_redshift(observed_spectrum, template_spectrum, min_redshift, max_re | |||||||||||
redshifted_spectrum: :class:`~specutils.Spectrum1D` | ||||||||||||
A new Spectrum1D object which incorporates the template_spectrum with a spectral_axis | ||||||||||||
that has been redshifted using the final_redshift. | ||||||||||||
chi2_list : `list` | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be an Relatedly, I'm having a bit of trouble following the logic: is the resulting array is of dimension equal to the spectral template set, or is it a 2D array that's n_template x n_redshift ? I think the latter is the eventual goal, but if we're not quite there it could be a follow-on. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In some out-of-band discussion, @ibusko said he'd write a test for the My preference is that it returns a 2D array as indicated above (or the transpose) |
||||||||||||
A list with the chi2 values corresponding to each input redshift value. | ||||||||||||
""" | ||||||||||||
if min_redshift > max_redshift: | ||||||||||||
raise ValueError("The `max_redshift` value must be greater than `min_redshift`.") | ||||||||||||
return | ||||||||||||
|
||||||||||||
redshift = min_redshift | ||||||||||||
chi2_min = None | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
final_redshift = None | ||||||||||||
chi2_list = [] | ||||||||||||
|
||||||||||||
# Loop which goes through available redshift options and finds the smallest chi2 | ||||||||||||
while redshift <= max_redshift: | ||||||||||||
# Loop which goes through available redshift values and finds the smallest chi2 | ||||||||||||
for rs in redshift: | ||||||||||||
|
||||||||||||
# Create new redshifted spectrum and run it through the chi2 method | ||||||||||||
redshifted_spectrum = Spectrum1D(spectral_axis=template_spectrum.spectral_axis*(1+redshift), | ||||||||||||
redshifted_spectrum = Spectrum1D(spectral_axis=template_spectrum.spectral_axis*(1+rs), | ||||||||||||
flux=template_spectrum.flux, uncertainty=template_spectrum.uncertainty, | ||||||||||||
meta=template_spectrum.meta) | ||||||||||||
normalized_spectral_template, chi2 = _chi_square_for_templates( | ||||||||||||
observed_spectrum, redshifted_spectrum, "flux_conserving") | ||||||||||||
|
||||||||||||
chi2_list.append(chi2) | ||||||||||||
|
||||||||||||
# Set new chi2_min if suitable replacement is found | ||||||||||||
if not np.isnan(chi2) and (chi2_min is None or chi2 < chi2_min): | ||||||||||||
chi2_min = chi2 | ||||||||||||
final_redshift = redshift | ||||||||||||
redshift += delta_redshift | ||||||||||||
final_redshift = rs | ||||||||||||
|
||||||||||||
return final_redshift, redshifted_spectrum | ||||||||||||
return final_redshift, redshifted_spectrum, chi2_list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should merge
redshift
andknown_redshift
into a single parameter. We can then check if the value is a float or array-like and deal with it appropriately. Using this function, I wouldn't expect that a single value or a list of values would require two different arguments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this, but the one question is: is there some specific science case for
known_redshift
being used in addition to the redshift grid? I can't think of one offhand but just want to be sure... maybe @camipacifici has some insight?