specutils
specutils copied to clipboard
Specutils template matching needs to handle resampling without returning nan
I am working on a tutorial for fitting an SDSS quasar spectrum with a template and solving for redshift: https://drive.google.com/file/d/1cOPKSH3ED_0fokcf07yPj2t2mqYwtnrP/view?usp=sharing
(Astropy Tutorials Issue #441)
Due to the nature of the template, the model has to be resampled onto the observed spectral axis in order to calculate chi^2 (see the first three lines of the _chi_square_for_templates
). If the template axis doesn't span the observed axis over the full range of redshift values provided, then the resampling function returns 'nan', which breaks everything.
The function needs a mask to ignore regions that can't be resampled, or at least return a value other 'nan' for regions that are out-of-bounds of the template.
@eblur This case should already be supported with the extrapolation_treatment
argument in the resamplers. By default it is nan_fill
, but you can use zero_fill
instead. There's an example in the docs as well.
The template_comparison
library doesn't provide any arguments or keywords for passing that information to the resampler. Thus, in order to apply this library to a very simple example (a spectrum downloaded directly to the SDSS archive), the user would have to re-write much of this library.
So, I see two possible ways forward:
(1) Make zero_fill extrapolation the default for the samplers returned in the _resampler
function, or
(2) Provide a resample_fill
(or similarly named) keyword in the template_match
and template_redshift
functions that allow the user to refine the template sampling method.
I suggest (2) because I can think of some examples where (1) could cause problems.
Also template_redshift
should have resample_method
keyword, like the template_match
function.
Speaking of which, is there an imperative reason resample
does not also offer some basic extrapolation option like const_fill
(i.e. instead of setting the off_edges
to zero, to the last valid flux value off the region)? Of course the simple solution would be quite sensitive to noise, so a better implementation should average over some region close to the edge, and at that point the options (and possibly also the temptation to abuse by extrapolating too far) may quickly get out of control.
Still it seems for points say within one original step width this could often be a better approximation than zero.
Proposed solution: do not fill with zeros/nans, do not extrapolate. Use only the wavelength range that both the observation and the template(s) cover. Issue a warning that the template(s) does(do) not cover a large enough wavelength rage and the spectra are truncated.
I implemented @camipacifici's proposal in #1121, closing this.