def traptransit_fit_magseries(times, mags, errs,<EOL>transitparams,<EOL>sigclip=<NUM_LIT>,<EOL>plotfit=False,<EOL>magsarefluxes=False,<EOL>verbose=True):
stimes, smags, serrs = sigclip_magseries(times, mags, errs,<EOL>sigclip=sigclip,<EOL>magsarefluxes=magsarefluxes)<EOL>nzind = np.nonzero(serrs)<EOL>stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind]<EOL>transitperiod, transitepoch, transitdepth = transitparams[<NUM_LIT:0>:<NUM_LIT:3>]<EOL>if transitepoch is None:<EOL><INDENT>if verbose:<EOL><INDENT>LOGWARNING('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>spfit = spline_fit_magseries(times, mags, errs, transitperiod,<EOL>sigclip=sigclip,<EOL>magsarefluxes=magsarefluxes,<EOL>verbose=verbose)<EOL>transitepoch = spfit['<STR_LIT>']['<STR_LIT>']<EOL><DEDENT>except Exception as e:<EOL><INDENT>sgfit = savgol_fit_magseries(times, mags, errs, transitperiod,<EOL>sigclip=sigclip,<EOL>magsarefluxes=magsarefluxes,<EOL>verbose=verbose)<EOL>transitepoch = sgfit['<STR_LIT>']['<STR_LIT>']<EOL><DEDENT>finally:<EOL><INDENT>if transitepoch is None:<EOL><INDENT>LOGERROR("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>returndict = {<EOL>'<STR_LIT>':'<STR_LIT>',<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':transitparams,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>},<EOL>'<STR_LIT>':np.nan,<EOL>'<STR_LIT>':np.nan,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':magsarefluxes,<EOL>},<EOL>}<EOL>return returndict<EOL><DEDENT>else:<EOL><INDENT>if transitepoch.size > <NUM_LIT:1>:<EOL><INDENT>if verbose:<EOL><INDENT>LOGWARNING(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>transitparams[<NUM_LIT:1>] = transitepoch[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>if verbose:<EOL><INDENT>LOGWARNING(<EOL>'<STR_LIT>'<EOL>% transitepoch<EOL>)<EOL><DEDENT>transitparams[<NUM_LIT:1>] = transitepoch.item()<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if magsarefluxes:<EOL><INDENT>if transitdepth < <NUM_LIT:0.0>:<EOL><INDENT>transitparams[<NUM_LIT:2>] = -transitdepth<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if transitdepth > <NUM_LIT:0.0>:<EOL><INDENT>transitparams[<NUM_LIT:2>] = -transitdepth<EOL><DEDENT><DEDENT>try:<EOL><INDENT>leastsqfit = spleastsq(transits.trapezoid_transit_residual,<EOL>transitparams,<EOL>args=(stimes, smags, serrs),<EOL>full_output=True)<EOL><DEDENT>except Exception as e:<EOL><INDENT>leastsqfit = None<EOL><DEDENT>if leastsqfit and leastsqfit[-<NUM_LIT:1>] in (<NUM_LIT:1>,<NUM_LIT:2>,<NUM_LIT:3>,<NUM_LIT:4>):<EOL><INDENT>finalparams = leastsqfit[<NUM_LIT:0>]<EOL>covxmatrix = leastsqfit[<NUM_LIT:1>]<EOL>fitmags, phase, ptimes, pmags, perrs, n_transitpoints = (<EOL>transits.trapezoid_transit_func(<EOL>finalparams,<EOL>stimes, smags, serrs,<EOL>get_ntransitpoints=True<EOL>)<EOL>)<EOL>fitchisq = np.sum(<EOL>((fitmags - pmags)*(fitmags - pmags)) / (perrs*perrs)<EOL>)<EOL>fitredchisq = fitchisq/(len(pmags) - len(finalparams) - <NUM_LIT:1>)<EOL>residuals = leastsqfit[<NUM_LIT:2>]['<STR_LIT>']<EOL>residualvariance = (<EOL>np.sum(residuals*residuals)/(pmags.size - finalparams.size)<EOL>)<EOL>if covxmatrix is not None:<EOL><INDENT>covmatrix = residualvariance*covxmatrix<EOL>stderrs = np.sqrt(np.diag(covmatrix))<EOL><DEDENT>else:<EOL><INDENT>LOGERROR('<STR_LIT>')<EOL>stderrs = None<EOL><DEDENT>if verbose:<EOL><INDENT>LOGINFO(<EOL>'<STR_LIT>' %<EOL>(fitchisq, fitredchisq)<EOL>)<EOL><DEDENT>fperiod, fepoch = finalparams[:<NUM_LIT:2>]<EOL>returndict = {<EOL>'<STR_LIT>':'<STR_LIT>',<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':transitparams,<EOL>'<STR_LIT>':finalparams,<EOL>'<STR_LIT>':stderrs,<EOL>'<STR_LIT>':leastsqfit,<EOL>'<STR_LIT>':fitmags,<EOL>'<STR_LIT>':fepoch,<EOL>'<STR_LIT>':n_transitpoints<EOL>},<EOL>'<STR_LIT>':fitchisq,<EOL>'<STR_LIT>':fitredchisq,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':phase,<EOL>'<STR_LIT>':ptimes,<EOL>'<STR_LIT>':pmags,<EOL>'<STR_LIT>':perrs,<EOL>'<STR_LIT>':magsarefluxes,<EOL>},<EOL>}<EOL>if plotfit and isinstance(plotfit, str):<EOL><INDENT>make_fit_plot(phase, pmags, perrs, fitmags,<EOL>fperiod, ptimes.min(), fepoch,<EOL>plotfit,<EOL>magsarefluxes=magsarefluxes)<EOL>returndict['<STR_LIT>'] = plotfit<EOL><DEDENT>return returndict<EOL><DEDENT>else:<EOL><INDENT>LOGERROR('<STR_LIT>')<EOL>returndict = {<EOL>'<STR_LIT>':'<STR_LIT>',<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':transitparams,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':leastsqfit,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':<NUM_LIT:0><EOL>},<EOL>'<STR_LIT>':np.nan,<EOL>'<STR_LIT>':np.nan,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':magsarefluxes,<EOL>},<EOL>}<EOL>return returndict<EOL><DEDENT>
This fits a trapezoid transit model to a magnitude time series.

    Parameters
    ----------

    times,mags,errs : np.array
        The input mag/flux time-series to fit a trapezoid planet-transit model
        to.

    period : float
        The period to use for the model fit.

    transitparams : list of floats
        These are initial parameters for the transit model fit. A list of the
        following form is required::

            transitparams = [transitperiod (time),
                             transitepoch (time),
                             transitdepth (flux or mags),
                             transitduration (phase),
                             ingressduration (phase)]

        - for magnitudes -> `transitdepth` should be < 0
        - for fluxes     -> `transitdepth` should be > 0

        If `transitepoch` is None, this function will do an initial spline fit
        to find an approximate minimum of the phased light curve using the given
        period.

        The `transitdepth` provided is checked against the value of
        `magsarefluxes`. if `magsarefluxes = True`, the `transitdepth` is forced
        to be > 0; if `magsarefluxes` = False, the `transitdepth` is forced to
        be < 0.

    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
    -------

    dict
        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':'traptransit',
                'fitinfo':{
                    'initialparams':the initial transit params provided,
                    'finalparams':the final model fit transit params ,
                    'finalparamerrs':formal errors in the params,
                    'leastsqfit':the full tuple returned by scipy.leastsq,
                    'fitmags': the model fit mags,
                    'fitepoch': the epoch of minimum light for the fit,
                    'ntransitpoints': the number of LC points in transit phase
                },
                '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
                }
            }