Model Fitting¶
- class pyspeckit.spectrum.fitters.Specfit(Spectrum, Registry=None)¶
Bases: pyspeckit.spectrum.interactive.Interactive
- EQW(plot=False, plotcolor='g', annotate=False, alpha=0.5, loc='lower left')¶
Returns the equivalent width (integral of “baseline” or “continuum” minus the spectrum) over the selected range
- annotate(loc='upper right', labelspacing=0.25, markerscale=0.01, borderpad=0.10000000000000001, handlelength=0.10000000000000001, handletextpad=0.10000000000000001, frameon=False, **kwargs)¶
Add a legend to the plot showing the fitted parameters
clearlegend() will remove the legend
kwargs passed to legend
Disconnect the interactiveness Perform the fit (or die trying) Hide the guesses
- clear(legend=True, components=True)¶
Remove the fitted model from the plot
Also removes the legend by default
- clear_all_connections(debug=False)¶
Prevent overlapping interactive sessions
- clear_highlights()¶
- copy(parent=None)¶
Create a copy of the spectral fit - includes copies of the _full_model, the registry, the fitter, parinfo, modelpars, modelerrs, model, npeaks
- [ parent ]
- A spectroscopic axis instance that is the parent of the specfit instance. This needs to be specified at some point, but defaults to None to prevent overwriting a previous plot.
- crop(x1pix, x2pix)¶
When spectrum.crop is called, this must be too
- downsample(factor)¶
Downsample the model spectrum (and the spectofit spectra) This should only be done when Spectrum.smooth is called
- event_manager(event, debug=False)¶
Decide what to do given input (click, keypress, etc.)
- firstclick_guess()¶
Initialize self.guesses
- firstclick_selection(include_all=False)¶
Initialize the include/exclude mask
- fullsizemodel()¶
If the gaussian was fit to a sub-region of the spectrum, expand it (with zeros) to fill the spectrum.
- guesspeakwidth(event, debug=False)¶
Interactively guess the peak height and width from user input
Width is assumed to be half-width-half-max
- highlight_fitregion(drawstyle='steps-mid', color='g', clear_highlights=True, **kwargs)¶
Re-highlight the fitted region
kwargs are passed to matplotlib.plot
- integral(direct=False, threshold='auto', integration_limits=[], return_error=False, **kwargs)¶
Return the integral of the fitted spectrum
if direct=True, return the integral of the spectrum over a range defined by the threshold or integration limits if defined
note that integration_limits will operate directly on the DATA, which means that if you’ve baselined without subtract=True, the baseline will be included in the integral
if return_error is set, the error computed by sigma = sqrt(sum(sigma_i^2)) * dx will be returned as well
- moments(**kwargs)¶
Return the moments
see moments
- multifit(fittype=None, renormalize='auto', annotate=None, show_components=None, verbose=True, **kwargs)¶
Fit multiple gaussians (or other profiles)
- fittype - What function will be fit? fittype must have been Registryed in the
- singlefitters dict. Uses default (‘gaussian’) if not specified
- renormalize - if ‘auto’ or True, will attempt to rescale small data (<1e-9) to be
- closer to 1 (scales by the median) so that the fit converges better
- peakbgfit(usemoments=True, annotate=None, vheight=True, height=0, negamp=None, fittype=None, renormalize='auto', show_components=None, debug=False, nsigcut_moments=None, **kwargs)¶
Fit a single peak (plus a background)
- usemoments - The initial guess will be set by the fitter’s ‘moments’ function
- (this overrides ‘guesses’)
annotate - Make a legend? vheight - Fit a (constant) background as well as a peak? height - initial guess for background negamp - If True, assumes amplitude is negative. If False, assumes positive. If
None, can be either.- fittype - What function will be fit? fittype must have been Registryed in the
- singlefitters dict
- renormalize - if ‘auto’ or True, will attempt to rescale small data (<1e-9) to be
- closer to 1 (scales by the median) so that the fit converges better
nsigcut_moments - pass to moment guesser; can do a sigma cut for moment guessing
- plot_fit(annotate=None, show_components=None, composite_fit_color='red', component_fit_color='blue', lw=0.5, composite_lw=0.75, component_lw=0.75, component_kwargs={}, **kwargs)¶
Plot the fit. Must have fitted something before calling this!
It will be automatically called whenever a spectrum is fit (assuming an axis for plotting exists)
kwargs are passed to the fitter’s components attribute
- plotresiduals(fig=2, axis=None, clear=True, **kwargs)¶
Plot residuals of the fit. Specify a figure or axis; defaults to figure(2).
kwargs are passed to matplotlib plot
- print_fit(print_baseline=True, **kwargs)¶
Print the best-fit parameters to the command line
- register_fitter(*args, **kwargs)¶
Register a model fitter
Register a fitter function.
Required Arguments:
- name: [ string ]
- The fit function name.
- function: [ function ]
- The fitter function. Single-fitters should take npars + 1 input parameters, where the +1 is for a 0th order baseline fit. They should accept an X-axis and data and standard fitting-function inputs (see, e.g., gaussfitter). Multi-fitters should take N * npars, but should also operate on X-axis and data arguments.
- npars: [ int ]
- How many parameters does the function being fit accept?
Optional Keyword Arguments:
- multisingle: [ ‘multi’ | ‘single’ ]
- Is the function a single-function fitter (with a background), or does it allow N copies of the fitting function?
- override: [ True | False ]
- Whether to override any existing type if already present.
- key: [ char ]
- Key to select the fitter in interactive mode
- savefit()¶
Save the fit parameters from a Gaussian fit to the FITS header *THESE SHOULD BE WRITTEN FOR EACH TYPE OF MODEL TO BE FIT*
- selectregion(xmin=None, xmax=None, xtype='wcs', highlight=False, fit_plotted_area=True, reset=False, verbose=False, debug=False, **kwargs)¶
Pick a fitting region in either WCS units or pixel units
- selectregion_interactive(event, mark_include=True, debug=False, **kwargs)¶
select regions for baseline fitting
- seterrspec(usestd=None, useresiduals=True)¶
Simple wrapper function to set the error spectrum; will either use the input spectrum or determine the error using the RMS of the residuals, depending on whether the residuals exist.
- setfitspec()¶
Set the spectrum that will be fit. This is primarily to remove NANs from consideration: if you simply remove the data from both the X-axis and the Y-axis, it will not be considered for the fit, and a linear X-axis is not needed for fitting.
However, it may be possible to do this using masked arrays instead of setting errors to be 1e10....
- shift_pars(frame=None)¶
Shift the velocity / wavelength / frequency of the fitted parameters into a different frame
Right now this only takes care of redshift and only if redshift is defined. It should be extended to do other things later
- start_interactive(debug=False, LoudDebug=False, print_message=True, clear_all_connections=True, **kwargs)¶