Baseline Fitting

There are a number of cool features in baselining that aren’t well-described below, partly due to Sphinx errors as of 12/22/2011.

exclude and include allow you to specify which parts of the spectrum to use for baseline fitting. Enter values as pairs of coordinates.

Excludefit makes use of an existing fit and excludes all points with signal above a (very low) threshold when fitting the baseline. Going back and forth between baseline(excludefit=True) and specfit() is a nice way to iteratively measure the baseline & emission/absorption line components.


class pyspeckit.spectrum.baseline.Baseline(Spectrum)[source] [github] [bitbucket]

Class to measure and subtract baselines from spectra.

While the term ‘baseline’ is generally used in the radio to refer to broad-band features in a spectrum not necessarily associated with a source, in this package it refers to general continuum fitting. In principle, there’s no reason to separate ‘continuum’ and ‘spectral feature’ fitting into different categories (both require some model, data, and optional weights when fitting). In practice, however, ‘continuum’ is frequently something to be removed and ignored, while spectral features are the desired measurable quantity. In order to accurately measure spectral features, it is necessary to allow baselines of varying complexity.

The Baseline class has both interactive and command-based data selection features. It can be used to fit both polynomial and power-law continua. Blackbody fitting is not yet implemented [12/21/2011]. Baseline fitting is a necessary prerequisite for Equivalent Width measurement.

As you may observe in the comments on this code, this has been one of the buggiest and least adequately tested components of pyspeckit. Bug reports are welcome. (as of 1/15/2012, a major change has probably fixed most of the bugs, and the code base is much simpler)

__call__(*args, **kwargs) [github] [bitbucket]

Fit and remove a polynomial from the spectrum. It will be saved in the variable “self.basespec” and the fit parameters will be saved in “self.order”


order: int :

Order of the polynomial to fit

excludefit: bool :

If there is a spectroscopic line fit, you can automatically exclude the region with signal above some tolerance set by exclusionlevel (it works for absorption lines by using the absolute value of the signal)

exclusionlevel: float :

The minimum value of the spectroscopic fit to exclude when fitting the baseline

save: bool :

Write the baseline fit coefficients into the spectrum’s header in the keywords BLCOEFnn

interactive: bool :

Specify the include/exclude regions through the interactive plot window

fit_original: bool :

Fit the original spectrum instead of the baseline-subtracted spectrum. If disabled, will overwrite the original data with the baseline-subtracted version.


If this is set False, behavior of unsubtract may be unexpected

fit_plotted_area: bool :

Will respect user-specified zoom (using the pan/zoom buttons) unless xmin/xmax have been set manually

reset_selection: bool :

Reset the selected region to those specified by this command only (will override previous xmin/xmax settings)

select_region: bool :

Run the region selection procedure? If false, will leave ‘includemask’ untouched

baseline_fit_color: color name (string) :

[plot parameter] Color to plot the baseline

clear_all_connections: bool :

[plot parameter] Disable any previous interactive sessions

highlight_fitregion: bool :

[plot parameter] Highlight the selected region for baseline fitting (default green)

__init__(Spectrum)[source] [github] [bitbucket]

Declare interactive variables.

Must have a parent Spectrum class

Must declare button2action and button3action

__module__ = 'pyspeckit.spectrum.baseline'
annotate(loc='upper left', fontsize=10)[source] [github] [bitbucket]
button2action(event=None, debug=False, subtract=True, powerlaw=None, fit_original=False, spline=False, spline_sampling=None, spline_downsampler=<function median>, baseline_fit_color='orange', **kwargs)[source] [github] [bitbucket]

Do the baseline fitting and save and plot the results.

Note that powerlaw fitting will only consider positive data.

button3action(*args, **kwargs)[source] [github] [bitbucket]

Wrapper - same as button2action, but with subtract=False

clearlegend()[source] [github] [bitbucket]
copy(parent=None)[source] [github] [bitbucket]

Create a copy of the baseline fit

[ 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)[source] [github] [bitbucket]

When spectrum.crop is called, this must be too

downsample(factor)[source] [github] [bitbucket]
fit(powerlaw=None, order=None, includemask=None, spline=False, spline_sampling=10, spline_downsampler=<function median>, xarr_fit_unit='pixels', **kwargs)[source] [github] [bitbucket]

Run the fit and set self.basespec

get_model(xarr=None, baselinepars=None)[source] [github] [bitbucket]
plot_baseline(annotate=True, baseline_fit_color='orange', use_window_limits=None, linewidth=1, alpha=0.75, plotkwargs={}, **kwargs)[source] [github] [bitbucket]

Overplot the baseline fit


annotate : bool

Display the fit parameters for the best-fit baseline on the top-left of the plot

baseline_fit_color : matplotlib color

What color to use for overplotting the line (default is slightly transparent orange)

use_window_limits : None or bool

Keep the current window or expand the plot limits? If left as None, will use self.use_window_limits

savefit()[source] [github] [bitbucket]
set_basespec_frompars(baselinepars=None, xarr_fit_unit=None)[source] [github] [bitbucket]

Set the baseline spectrum based on the fitted parameters


baselinepars : list

Optional list of fit parameters, e.g. a list of polynomial coefficients

xarr_fit_unit : None or ‘pixels’ or ‘native’ or unit

The units that were used in the baseline fit

set_spectofit(fit_original=True, fit_residuals=False)[source] [github] [bitbucket]

Reset the spectrum-to-fit from the data

unsubtract(replot=True, preserve_limits=True)[source] [github] [bitbucket]

Restore the spectrum to “pristine” state (un-subtract the baseline)

replot [ True ]
Re-plot the spectrum? (only happens if unsubtraction proceeds, i.e. if there was a baseline to unsubtract)
preserve_limits [ True ]
Preserve the current x,y limits