tkp.sourcefinder.fitting – Source fitting routines

Source fitting routines.

tkp.sourcefinder.fitting.fitgaussian(pixels, params, fixed=None, maxfev=0)[source]

Calculate source positional values by fitting a 2D Gaussian

  • pixels ( – Pixel values (with bad pixels masked)
  • params (dict) – initial fit parameters (possibly estimated using the moments() function, above)
fixed (dict): parameters & their values to be kept frozen (ie, not

maxfev (int): maximum number of calls to the error function

Returns:peak, total, x barycenter, y barycenter, semimajor, semiminor, theta (radians)
Return type:dict
Raises:exceptions.ValueError – In case of a bad fit.

Perform a least squares fit to an elliptical Gaussian.

If a dict called fixed is passed in, then parameters specified within the dict with the same names as fit_params (below) will be “locked” in the fitting process.

tkp.sourcefinder.fitting.goodness_of_fit(masked_residuals, noise, beam)[source]

Calculates the goodness-of-fit values, chisq and reduced_chisq.


We do not use the standard chi-squared formula for calculating these goodness-of-fit values, and should probably rename them in the next release. See below for details.

These goodness-of-fit values are related to, but not quite the same as reduced chi-squared. Strictly speaking the reduced chi-squared is statistically invalid for a Gaussian model from the outset (see arxiv:1012.3754). We attempt to provide a resolution-independent estimate of goodness-of-fit (‘reduced chi-squared’), by using the same ‘independent pixels’ correction as employed when estimating RMS levels, to normalize the chi-squared value. However, as applied to the standard formula this will sometimes imply that we are fitting a fractional number of datapoints less than 1! As a result, it doesn’t really make sense to try and apply the ‘degrees-of-freedom’ correction, as this would likely result in a negative reduced_chisq value. (And besides, the ‘degrees of freedom’ concept is invalid for non-linear models.) Finally, note that when called from source_profile_and_errors(), the noise-estimate at the peak-pixel is supplied, so will typically over-estimate the noise and hence under-estimate the chi-squared values.

  • masked_residuals ( – The pixel-residuals from the fit
  • noise (float) – An estimate of the noise level. Could also be set to a masked numpy array matching the data, for per-pixel noise estimates.
  • beam (tuple) – Beam parameters

chisq, reduced_chisq

Return type:


tkp.sourcefinder.fitting.moments(data, beam, threshold=0)[source]

Calculate source positional values using moments

  • data (numpy.ndarray) – Actual 2D image data
  • beam (3-tuple) – beam (psf) information, with semi-major and semi-minor axes

peak, total, x barycenter, y barycenter, semimajor axis, semiminor axis, theta

Return type:



exceptions.ValueError – in case of NaN in input.

Use the first moment of the distribution is the barycenter of an ellipse. The second moments are used to estimate the rotation angle and the length of the axes.