astrobase.lcfit.nonphysical module

Light curve fitting routines for ‘non-physical’ models:

astrobase.lcfit.nonphysical.spline_fit_magseries(times, mags, errs, period, knotfraction=0.01, maxknots=30, sigclip=30.0, plotfit=False, ignoreinitfail=False, magsarefluxes=False, verbose=True)[source]

This fits a univariate cubic spline to the phased light curve.

This fit may be better than the Fourier fit for sharply variable objects, like EBs, so can be used to distinguish them from other types of variables.

Parameters:
  • times,mags,errs (np.array) – The input mag/flux time-series to fit a spline to.
  • period (float) – The period to use for the spline fit.
  • knotfraction (float) – The knot fraction is the number of internal knots to use for the spline. A value of 0.01 (or 1%) of the total number of non-nan observations appears to work quite well, without over-fitting. maxknots controls the maximum number of knots that will be allowed.
  • maxknots (int) – The maximum number of knots that will be used even if knotfraction gives a value to use larger than maxknots. This helps dealing with over-fitting to short time-scale variations.
  • sigclip (float or int or sequence of two floats/ints or None) –

    If a single float or int, a symmetric sigma-clip will be performed using the number provided as the sigma-multiplier to cut out from the input time-series.

    If a list of two ints/floats is provided, the function will perform an ‘asymmetric’ sigma-clip. The first element in this list is the sigma value to use for fainter flux/mag values; the second element in this list is the sigma value to use for brighter flux/mag values. For example, sigclip=[10., 3.], will sigclip out greater than 10-sigma dimmings and greater than 3-sigma brightenings. Here the meaning of “dimming” and “brightening” is set by physics (not the magnitude system), which is why the magsarefluxes kwarg must be correctly set.

    If sigclip is None, no sigma-clipping will be performed, and the time-series (with non-finite elems removed) will be passed through to the output.

  • magsarefluxes (bool) – If True, will treat the input values of mags as fluxes for purposes of plotting the fit and sig-clipping.
  • plotfit (str or False) – If this is a string, this function will make a plot for the fit to the mag/flux time-series and writes the plot to the path specified here.
  • ignoreinitfail (bool) – If this is True, ignores the initial failure to find a set of optimized Fourier parameters using the global optimization function and proceeds to do a least-squares fit anyway.
  • verbose (bool) – If True, will indicate progress and warn of any problems.
Returns:

This function returns a dict containing the model fit parameters, the minimized chi-sq value and the reduced chi-sq value. The form of this dict is mostly standardized across all functions in this module:

{
    'fittype':'spline',
    'fitinfo':{
        'nknots': the number of knots used for the fit
        'fitmags': the model fit mags,
        'fitepoch': the epoch of minimum light for the fit,
    },
    'fitchisq': the minimized value of the fit's chi-sq,
    'fitredchisq':the reduced chi-sq value,
    'fitplotfile': the output fit plot if fitplot is not None,
    'magseries':{
        'times':input times in phase order of the model,
        'phase':the phases of the model mags,
        'mags':input mags/fluxes in the phase order of the model,
        'errs':errs in the phase order of the model,
        'magsarefluxes':input value of magsarefluxes kwarg
    }
}

Return type:

dict

astrobase.lcfit.nonphysical.savgol_fit_magseries(times, mags, errs, period, windowlength=None, polydeg=2, sigclip=30.0, plotfit=False, magsarefluxes=False, verbose=True)[source]

Fit a Savitzky-Golay filter to the magnitude/flux time series.

SG fits successive sub-sets (windows) of adjacent data points with a low-order polynomial via least squares. At each point (magnitude), it returns the value of the polynomial at that magnitude’s time. This is made significantly cheaper than actually performing least squares for each window through linear algebra tricks that are possible when specifying the window size and polynomial order beforehand. Numerical Recipes Ch 14.8 gives an overview, Eq. 14.8.6 is what Scipy has implemented.

The idea behind Savitzky-Golay is to preserve higher moments (>=2) of the input data series than would be done by a simple moving window average.

Note that the filter assumes evenly spaced data, which magnitude time series are not. By pretending the data points are evenly spaced, we introduce an additional noise source in the function values. This is a relatively small noise source provided that the changes in the magnitude values across the full width of the N=windowlength point window is < sqrt(N/2) times the measurement noise on a single point.

TODO: - Find correct dof for reduced chi squared in savgol_fit_magseries

Parameters:
  • times,mags,errs (np.array) – The input mag/flux time-series to fit the Savitsky-Golay model to.
  • period (float) – The period to use for the model fit.
  • windowlength (None or int) – The length of the filter window (the number of coefficients). Must be either positive and odd, or None. (The window is the number of points to the left, and to the right, of whatever point is having a polynomial fit to it locally). Bigger windows at fixed polynomial order risk lowering the amplitude of sharp features. If None, this routine (arbitrarily) sets the windowlength for phased LCs to be either the number of finite data points divided by 300, or polydeg+3, whichever is bigger.
  • polydeg (int) – This is the order of the polynomial used to fit the samples. Must be less than windowlength. “Higher-order filters do better at preserving feature heights and widths, but do less smoothing on broader features.” (Numerical Recipes).
  • sigclip (float or int or sequence of two floats/ints or None) –

    If a single float or int, a symmetric sigma-clip will be performed using the number provided as the sigma-multiplier to cut out from the input time-series.

    If a list of two ints/floats is provided, the function will perform an ‘asymmetric’ sigma-clip. The first element in this list is the sigma value to use for fainter flux/mag values; the second element in this list is the sigma value to use for brighter flux/mag values. For example, sigclip=[10., 3.], will sigclip out greater than 10-sigma dimmings and greater than 3-sigma brightenings. Here the meaning of “dimming” and “brightening” is set by physics (not the magnitude system), which is why the magsarefluxes kwarg must be correctly set.

    If sigclip is None, no sigma-clipping will be performed, and the time-series (with non-finite elems removed) will be passed through to the output.

  • magsarefluxes (bool) – If True, will treat the input values of mags as fluxes for purposes of plotting the fit and sig-clipping.
  • plotfit (str or False) – If this is a string, this function will make a plot for the fit to the mag/flux time-series and writes the plot to the path specified here.
  • ignoreinitfail (bool) – If this is True, ignores the initial failure to find a set of optimized Fourier parameters using the global optimization function and proceeds to do a least-squares fit anyway.
  • verbose (bool) – If True, will indicate progress and warn of any problems.
Returns:

This function returns a dict containing the model fit parameters, the minimized chi-sq value and the reduced chi-sq value. The form of this dict is mostly standardized across all functions in this module:

{
    'fittype':'savgol',
    'fitinfo':{
        'windowlength': the window length used for the fit,
        'polydeg':the polynomial degree used for the fit,
        'fitmags': the model fit mags,
        'fitepoch': the epoch of minimum light for the fit,
    },
    'fitchisq': the minimized value of the fit's chi-sq,
    'fitredchisq':the reduced chi-sq value,
    'fitplotfile': the output fit plot if fitplot is not None,
    'magseries':{
        'times':input times in phase order of the model,
        'phase':the phases of the model mags,
        'mags':input mags/fluxes in the phase order of the model,
        'errs':errs in the phase order of the model,
        'magsarefluxes':input value of magsarefluxes kwarg
    }
}

Return type:

dict

astrobase.lcfit.nonphysical.legendre_fit_magseries(times, mags, errs, period, legendredeg=10, sigclip=30.0, plotfit=False, magsarefluxes=False, verbose=True)[source]

Fit an arbitrary-order Legendre series, via least squares, to the magnitude/flux time series.

This is a series of the form:

p(x) = c_0*L_0(x) + c_1*L_1(x) + c_2*L_2(x) + ... + c_n*L_n(x)

where L_i’s are Legendre polynomials (also called “Legendre functions of the first kind”) and c_i’s are the coefficients being fit.

This function is mainly just a wrapper to numpy.polynomial.legendre.Legendre.fit.

Parameters:
  • times,mags,errs (np.array) – The input mag/flux time-series to fit a Legendre series polynomial to.
  • period (float) – The period to use for the Legendre fit.
  • legendredeg (int) – This is n in the equation above, e.g. if you give n=5, you will get 6 coefficients. This number should be much less than the number of data points you are fitting.
  • sigclip (float or int or sequence of two floats/ints or None) –

    If a single float or int, a symmetric sigma-clip will be performed using the number provided as the sigma-multiplier to cut out from the input time-series.

    If a list of two ints/floats is provided, the function will perform an ‘asymmetric’ sigma-clip. The first element in this list is the sigma value to use for fainter flux/mag values; the second element in this list is the sigma value to use for brighter flux/mag values. For example, sigclip=[10., 3.], will sigclip out greater than 10-sigma dimmings and greater than 3-sigma brightenings. Here the meaning of “dimming” and “brightening” is set by physics (not the magnitude system), which is why the magsarefluxes kwarg must be correctly set.

    If sigclip is None, no sigma-clipping will be performed, and the time-series (with non-finite elems removed) will be passed through to the output.

  • magsarefluxes (bool) – If True, will treat the input values of mags as fluxes for purposes of plotting the fit and sig-clipping.
  • plotfit (str or False) – If this is a string, this function will make a plot for the fit to the mag/flux time-series and writes the plot to the path specified here.
  • ignoreinitfail (bool) – If this is True, ignores the initial failure to find a set of optimized Fourier parameters using the global optimization function and proceeds to do a least-squares fit anyway.
  • verbose (bool) – If True, will indicate progress and warn of any problems.
Returns:

This function returns a dict containing the model fit parameters, the minimized chi-sq value and the reduced chi-sq value. The form of this dict is mostly standardized across all functions in this module:

{
    'fittype':'legendre',
    'fitinfo':{
        'legendredeg': the Legendre polynomial degree used,
        'fitmags': the model fit mags,
        'fitepoch': the epoch of minimum light for the fit,
    },
    'fitchisq': the minimized value of the fit's chi-sq,
    'fitredchisq':the reduced chi-sq value,
    'fitplotfile': the output fit plot if fitplot is not None,
    'magseries':{
        'times':input times in phase order of the model,
        'phase':the phases of the model mags,
        'mags':input mags/fluxes in the phase order of the model,
        'errs':errs in the phase order of the model,
        'magsarefluxes':input value of magsarefluxes kwarg
    }
}

Return type:

dict