spectrum¶
- class pyspeckit.spectrum.Spectrum(filename=None, filetype=None, xarr=None, data=None, error=None, header=None, doplot=False, maskdata=True, plotkwargs={}, xarrkwargs={}, **kwargs)¶
Bases: object
The core class for the spectroscopic toolkit. Contains the data and error arrays along with wavelength / frequency / velocity information in various formats.
- Contains functions / classes to:
- -read and write FITS-compliant spectroscopic data sets
- -read fits binaries? (not implemented)
-plot a spectrum -fit a spectrum both interactively and non-interactively
-with gaussian, voigt, lorentzian, and multiple (gvl) profiles- -fit a continuum “baseline” to a selected region with an n-order
- polynomial
-perform fourier transforms and operations in fourier space on a spectrum (not implemented)
Example usage:
# automatically load a “compliant” (linear X-axis) FITS file
spec = pyspeckit.Spectrum(‘test.fits’)
# plot the spectrum
spec.plotter()
__init__ Initialize the Spectrum. Accepts files in the following formats:
- .fits
- .txt
- .hdf5
Must either pass in a filename or ALL of xarr, data, and header, plus optionally error.
doplot - if specified, will generate a plot when created
kwargs are passed to the reader, not the plotter
- copy(deep=True)¶
Create a copy of the spectrum with its own plotter, fitter, etc. Useful for, e.g., comparing smoothed to unsmoothed data
- crop(x1, x2, units=None, **kwargs)¶
Replace the current spectrum with a subset from x1 to x2 in current units
Fixes CRPIX1 and baseline and model spectra to match cropped data spectrum
- getlines(linetype='radio', **kwargs)¶
Access a registered database of spectral lines. Will add an attribute with the name linetype, which then has properties defined by the speclines module (most likely, a table and a “show” function to display the lines)
- interpnans(spec)¶
Interpolate over NAN values, replacing them with their neighbors...
- measure(z=None, d=None, fluxnorm=None, miscline=None, misctol=None, ignore=None, derive=True, **kwargs)¶
Initialize the measurements class - only do this after you have run a fitter otherwise pyspeckit will be angry!
- moments(unit='km/s', **kwargs)¶
- Return the moments of the spectrum. In order to assure that the 1st
and 2nd moments are meaningful, a ‘default’ unit is set. If unit is not set, will use current unit.
Returns (height, amplitude, x, width_x)
the gaussian parameters of a 1D distribution by calculating its moments. Depending on the input parameters, will only output a subset of the above. “height” is the background level “amplitude” is the maximum (or minimum) of the data after background subtraction “x” is the first moment “width_x” is the second moment
If using masked arrays, pass estimator=np.ma.median ‘estimator’ is used to measure the background level (height)
negamp can be used to force the peak negative (True), positive (False), or it will be “autodetected” (negamp=None)
- “nsigcut” - try to estimate the noise and only use data above/below nsigma
- above the noise. Estimate the noise from the data unless passed as a keyword
Theory: From first principles (in the absence of noise): integral(gaussian) = sqrt(2*pi*sigma^2) * amp sigma = integral / amp / sqrt(2*pi)
in the presence of noise, this gets much more complicated
- parse_hdf5_header(hdr)¶
HDF5 reader will create a hdr dictionary from HDF5 dataset attributes if they exist. This routine will convert that dict to a pyfits header instance.
- parse_header(hdr, specname=None)¶
Parse parameters from a .fits header into required spectrum structure parameters
This should be moved to the FITSSpectrum subclass when that is available
- parse_text_header(Table)¶
Grab relevant parameters from a table header (xaxis type, etc)
This function should only exist for Spectrum objects created from .txt or other atpy table type objects
- shape()¶
Return the data shape
- slice(start=None, stop=None, units='pixel', copy=True, preserve_fits=False)¶
Slicing the spectrum
- start: numpy.float or int
- start of slice
- stop: numpy.float or int
- stop of slice
- units: str
- allowed values are any supported physical unit, ‘pixel’
- smooth(smooth, **kwargs)¶
- Smooth the spectrum by factor “smooth”.
- sm.smooth doc:
Smooth and downsample the data array
- smooth [ float ]
- Number of pixels to smooth by
- smoothtype [ ‘gaussian’,’hanning’, or ‘boxcar’ ]
- type of smoothing kernel to use
- downsample [ bool ]
- Downsample the data?
- downsample_factor [ int ]
- Downsample by the smoothing factor, or something else?
- convmode [ ‘full’,’valid’,’same’ ]
- see numpy.convolve. ‘same’ returns an array of the same length as ‘data’ (assuming data is larger than the kernel)
- stats(statrange=(), interactive=False)¶
Return some statistical measures in a dictionary (somewhat self-explanatory)
range - X-range over which to perform measures interactive - specify range interactively in plotter
- write(filename, type=None, **kwargs)¶
Write the spectrum to a file. The available file types are listed in spectrum.writers.writers
type - what type of file to write to? If not specified, will attempt to determine type from suffix
- class pyspeckit.spectrum.Spectra(speclist, xunits='GHz', **kwargs)¶
Bases: pyspeckit.spectrum.classes.Spectrum
A list of individual Spectrum objects. Intended to be used for concatenating different wavelength observations of the SAME OBJECT. Can be operated on just like any Spectrum object, incuding fitting. Useful for fitting multiple lines on non-continguous axes simultaneously. Be wary of plotting these though...
Can be indexed like python lists.
X array is forcibly sorted in increasing order
- fiteach(**kwargs)¶
Fit each spectrum within the Spectra object
- ploteach(xunits=None, inherit_fit=False, plot_fit=True, plotfitkwargs={}, **plotkwargs)¶
Plot each spectrum in its own window inherit_fit - if specified, will grab the fitter & fitter properties from Spectra
- smooth(smooth, **kwargs)¶
Smooth the spectrum by factor “smooth”. Options are defined in sm.smooth
because ‘Spectra’ does not have a header attribute, don’t do anything to it...
- class pyspeckit.spectrum.ObsBlock(speclist, xtype='frequency', xarr=None, force=False, **kwargs)¶
Bases: pyspeckit.spectrum.classes.Spectra
An Observation Block
Consists of multiple spectra with a shared X-axis. Intended to hold groups of observations of the same object in the same setup for later averaging.
ObsBlocks can be indexed like python lists.
- average(weight=None, inverse_weight=False, error='erravgrtn')¶
Average all scans in an ObsBlock. Returns a single Spectrum object
weight - a header keyword to weight by error - estimate the error spectrum. Can be:
‘scanrms’ - the standard deviation of each pixel across all scans ‘erravg’ - the average of all input error spectra ‘erravgrtn’ - the average of all input error spectra divided by sqrt(n_obs)
- smooth(smooth, **kwargs)¶
Smooth the spectrum by factor “smooth”. Options are defined in sm.smooth
- pyspeckit.spectrum.register_reader(filetype, function, suffix, default=False)¶
Register a reader function.
Required Arguments:
- filetype: [ string ]
- The file type name
- function: [ function ]
- The reader function. Should take a filename as input and return an X-axis object (see units.py), a spectrum, an error spectrum (initialize it to 0’s if empty), and a pyfits header instance
- suffix: [ int ]
- What suffix should the file have?
Optional Keyword Arguments:
- pyspeckit.spectrum.register_writer(filetype, function, suffix, default=False)¶
Register a writer function.
Required Arguments:
- filetype: [ string ]
- The file type name
- function: [ function ]
- The writer function. Will be an attribute of Spectrum object, and called as spectrum.Spectrum.write_hdf5(), for example.
- suffix: [ int ]
- What suffix should the file have?
Optional Keyword Arguments: