def get_fit_result(
    data,
    init_params,
    model_function,
):
    '''
    find best fit parameters.

    :param data: DataFrame
    raw data. index values are sampling events.

    :param init_params: dict

    :param model_function
    function to fit data with.

    :return: result: ModelResult

    '''

    x = data.index.values
    y = data.values

    model = Model(model_function)
    for param, init_value in init_params.items():
        model.set_param_hint(param, value=init_value)
    params = model.make_params()

    result = model.fit(y, params, t=x)

    return result
예제 #2
0
    def double_gaussian(self, mod):
        """

            Function to fit a double gaussian model to the data

            :param mod: model to fit
            :return:
                - model : results of the fit
                - Report of the fit function
        """
        min = np.asarray(self.x).min()
        max = np.asarray(self.x).max()
        moy1 = (min + max) / 2 + 5
        moy2 = (min + max) / 2 - 5
        gmodel = Model(mod)  # create model
        # set constraint on parameters value
        gmodel.set_param_hint('sigma1_dg', min=0)
        gmodel.set_param_hint('sigma2_dg', min=0)
        gmodel.set_param_hint('mu1_dg', min=min, max=max)
        gmodel.set_param_hint('mu2_dg', min=min, max=max)
        self.solution["double_gaussian"] = gmodel.fit(self.y, x=self.x, sigma1_dg=1, sigma2_dg=1, mu1_dg=moy1,
                                                      mu2_dg=moy2, add_dg=1, method='powell')
        # get results
        self.master.info(self.solution["double_gaussian"].fit_report())
        print(self.solution["double_gaussian"].fit_report())
        self.solution["double_gaussian_fit"] = [self.solution["double_gaussian"].best_values['sigma1_dg'],
                                                self.solution["double_gaussian"].best_values['mu1_dg'],
                                                self.solution["double_gaussian"].best_values['sigma2_dg'],
                                                self.solution["double_gaussian"].best_values['mu2_dg'],
                                                self.solution["double_gaussian"].best_values['add_dg']]
        return self.solution["double_gaussian_fit"], self.solution["double_gaussian"].fit_report()
예제 #3
0
    def lorentz(self, mod):
        """

            Function to fit a simple lorentz model to the data

            :param mod: model to fit
            :return:
                - model : results of the fit
                - Report of the fit function
        """
        min = np.asarray(self.x).min()
        max = np.asarray(self.x).max()
        moy = (min + max) / 2
        gmodel = Model(mod)  # create model
        # set constraint on parameters value
        gmodel.set_param_hint('gamma_l', min=0)
        gmodel.set_param_hint('mu_l', min=min, max=max)
        self.solution["lorentz"] = gmodel.fit(self.y, x=self.x, mu_l=moy, gamma_l=1, add_l=1, method='powell')
        # get results
        print(self.solution["lorentz"].fit_report())
        self.master.info(self.solution["lorentz"].fit_report())
        self.solution["lorentz_fit"] = [self.solution["lorentz"].best_values['mu_l'],
                                        self.solution["lorentz"].best_values['gamma_l'],
                                        self.solution["lorentz"].best_values['add_l']]
        return self.solution["lorentz_fit"], self.solution["lorentz"].fit_report()
예제 #4
0
def gauss_step_const(signal, guess):
    """
    Fits high contrast data very well
    """
    if guess == False:
        return [0, 0]
    else:
        amp, centre, stdev, offset = guess
        
        data = np.array([range(len(signal)), signal]).T
        X = data[:,0]
        Y = data[:,1]

#         gauss_mod = Model(gaussian)
        gauss_mod = Model(gaussian)
        const_mod = ConstantModel()
        step_mod = StepModel(prefix='step')
        
        pars = gauss_mod.make_params(height=amp, center=centre, width=stdev / 3., offset=offset)
#         pars = gauss_mod.make_params(amplitude=amp, center=centre, sigma=stdev / 3.)
        gauss_mod.set_param_hint('sigma', value = stdev / 3., min=stdev / 2., max=stdev)
        pars += step_mod.guess(Y, x=X, center=centre)

        pars += const_mod.guess(Y, x=X)
    
        
        mod = const_mod + gauss_mod + step_mod
        result = mod.fit(Y, pars, x=X)
        # write error report
        #print result.fit_report()
        print "contrast fit", result.redchi
    
    return X, result.best_fit, result.redchi
예제 #5
0
def fit_act(led_volt, led):
    model = Model(act, independent_vars=['x'])
    model.set_param_hint('xo', value=-1, vary=True)
    model.set_param_hint('a', value=1, vary=True)
    result = model.fit(led_volt[led][:, 1],
                       x=led_volt[led][:, 0],
                       nan_policy='propagate')
    return result
예제 #6
0
def fit_gaussian_peak(w,
                      f,
                      amp_hint=-0.5,
                      cen_hint=None,
                      wid_hint=0.01,
                      shift_hint=0.8,
                      minmax='min'):
    """Fit a single Gaussian to a spectrum line.

    Gaussian G(x)=shift+amp*e^(-(x-cen)^2/(2*wid^2))

    Uses lmfit package.

    Parameters
    ----------
    w, f : 1d arrays
        Spectrum range where the minimum is located.
    amp_hint : float
        Amplitude value to be used as 1st guess when performing the fit.
    cen_hint : float
        Wavelength of the minimum location, to be used as 1st guess when fitting. If None, the mean value of `w` is used.
    wid_hint : float
    shift_hint : float
    minmax = str
        Specifies if the peak to be fitted is a minimum or a maximum, to know if the gaussian amplitude parameter `amp` must be negative or positive, respectively. Default: 'min'.

    Returns
    -------
    lmfit results object

    """
    def gaussian(x, amp=1, cen=0, wid=1, shift=0):
        return shift + amp * np.exp(-(x - cen)**2 / (2 * wid**2))

    # Fit model and parameters
    mod = Model(gaussian)

    # Amplitude `amp`
    if minmax == 'min': mod.set_param_hint('amp', value=amp_hint, max=0.)
    elif minmax == 'max': mod.set_param_hint('amp', value=amp_hint, min=0.)
    # Center `cen`
    if cen_hint is None: cen_hint = np.mean(w)
    mod.set_param_hint('cen', value=cen_hint)
    # Width `wid`
    mod.set_param_hint('wid', value=wid_hint, min=0.)
    # Shift in the y-axis `shift`
    mod.set_param_hint('shift', value=shift_hint)
    # mod.set_param_hint('fwhm', expr='2.35482004503*wid')
    # mod.set_param_hint('height', expr='shift+amp')

    gfitparams = mod.make_params()

    # Fit
    lmfitresult = Model(gaussian).fit(f, x=w, params=gfitparams)

    return lmfitresult
예제 #7
0
def fit_model(ms_dx, guess_s, hours_per_frame=5/60):
    fitfunc = lambda T, s, p: (s**2 * p**2) * (T/p - 1 + exp(-T/p))
    n = len(ms_dx)//2
    model = Model(fitfunc)
    model.set_param_hint('s', value=guess_s, min=0, max=250)
    # Constrain persistence time to ≥ 1 minute
    model.set_param_hint('p', value=0.5, min=1/60.)
    T = (arange(len(ms_dx)) + 1) * hours_per_frame
    result = model.fit(ms_dx[:n], T=T[:n])
    return result, (T, result.best_fit)
예제 #8
0
def gaussfit2d_multi(data, x, y, p0, **kwargs):

    plot = kwargs.get('plot', True)

    import numpy as np
    from lmfit import Parameters, Parameter, minimize, Model
    import funk
    import matplotlib.pyplot as plt

    def gauss2d_flat(x, y, amp, xcenter, ycenter, xwidth,
                     ywidth):  # This ends up as f(y,x)
        my, mx = np.meshgrid(x, y)
        xwidth, ywidth = xwidth / 2., ywidth / 2.
        return np.ravel(amp * np.exp(-2 * (np.square(mx - ycenter) /
                                           (np.square(ywidth)))) *
                        np.exp(-2 * (np.square(my - xcenter)) /
                               (np.square(xwidth))))

    def zero(x, y, a):
        return np.ravel(a * np.zeros((np.shape(x)[0], np.shape(y)[0])))

    n = len(p0[0])
    fullmod = Model(zero, independent_vars=['x', 'y'], param_names='a')
    fullmod.set_param_hint('a', value=0, max=.001, min=-.001)
    for i in range(n):
        # Define x,y grid and p_0 names
        p_0s = ['amp', 'xcenter', 'ycenter', 'xwidth', 'ywidth']
        func_model = Model(gauss2d_flat,
                           independent_vars=['x', 'y'],
                           param_names=p_0s,
                           prefix='g' + str(i + 1) + '_')
        fullmod += func_model
        fullmod.set_param_hint('g' + str(i + 1) + '_amp',
                               value=p0[0][i],
                               min=0)
        fullmod.set_param_hint('g' + str(i + 1) + '_xcenter', value=p0[1][i])
        fullmod.set_param_hint('g' + str(i + 1) + '_ycenter', value=p0[2][i])
        fullmod.set_param_hint('g' + str(i + 1) + '_xwidth',
                               value=p0[3][i],
                               min=0)
        fullmod.set_param_hint('g' + str(i + 1) + '_ywidth',
                               value=p0[4][i],
                               min=0)

    result = fullmod.fit(np.ravel(data), x=x, y=y)
    if plot == True:
        plt.figure('best_fit')
        plt.clf()
        plt.imshow(np.flipud(np.reshape(result.eval(), np.shape(data))),
                   interpolation='nearest',
                   extent=[x[0], x[-1], y[0], y[-1]])
        plt.colorbar(orientation='vertical')

    return result.best_values, np.flipud(
        np.reshape(result.eval(), np.shape(data)))
예제 #9
0
def fit_gauss_ramsey(x_data, y_data, weight_power=None, maxiter=None, maxfun=5000, verbose=1, initial_params=None):
    """ Fit a gauss_ramsey. The function gauss_ramsey gives a model for the measurement result of a pulse Ramsey
    sequence while varying the free evolution time, the phase of the second pulse is made dependent on the free
    evolution time.
    This results in a gaussian decay multiplied by a sinus. Function as used by T.F. Watson et all.,
    see function 'gauss_ramsey' and example in qtt/docs/notebooks/example_fit_ramsey.ipynb

    Args:
        x_data (array): the data for the independent variable
        y_data (array): the data for the measured variable
        weight_power (float or None): If a float then weight all the residual errors with a scale factor
        maxiter (int): maximum number of iterations to perform
        maxfun (int): maximum number of function evaluations to make
        verbose (int): set to >0 to print convergence messages
        initial_params (None or array): optional, initial guess for the fit parameters: [A,C,ramseyfreq,angle,B]

    Returns:
        par_fit (array): array with the fit parameters: [A,t2s,ramseyfreq,angle,B]
        result_dict (dict): dictionary containing a description, the par_fit and initial_params

    """

    def gauss_ramsey_model(x, amplitude, decay_time, frequency, phase, offset):
        """  """
        y = gauss_ramsey(x, [amplitude, decay_time, frequency, phase, offset])
        return y

    if weight_power is None:
        weights = None
    else:
        diff_x = np.diff(x_data)
        weights = np.hstack((diff_x[0], diff_x)) ** weight_power

    if initial_params is None:
        initial_parameters = estimate_parameters_damped_sine_wave(x_data, y_data, exponent=2)
    else:
        initial_parameters = initial_params
    lmfit_model = Model(gauss_ramsey_model)
    lmfit_model.set_param_hint('amplitude', min=0)
    lmfit_model.set_param_hint('decay_time', min=0)
    lmfit_result = lmfit_model.fit(y_data, x=x_data, **dict(zip(lmfit_model.param_names, initial_parameters)),
                                   verbose=verbose >= 2, weights=weights)

    import qtt.algorithms.fitting
    result_dict = qtt.algorithms.fitting.extract_lmfit_parameters(lmfit_model, lmfit_result)

    result_dict['description'] = 'Function to analyse the results of a Ramsey experiment, ' + \
        'fitted function: gauss_ramsey = A * exp(-(x_data/t2s)**2) * sin(2*pi*ramseyfreq * x_data - angle) + B'

    # backwards compatibility
    result_dict['parameters fit'] = result_dict['fitted_parameters']
    result_dict['parameters initial guess'] = initial_parameters

    return result_dict['fitted_parameters'], result_dict
예제 #10
0
파일: core.py 프로젝트: yangluom/impact
class CurveFitObject(object):
    """
    Wrapper for curve fitting objects

    Parameters:
        paramList: List of parameters, with initial guess, max and min values
            Each parameter is a dict with the following form
                {'name': str PARAMETER NAME,
                'guess': float or lambda function
                'max': float or lambda function
                'min': float or lambda function
                'vary' True or False}
        growthEquation: A function to fit with the following form:
            def growthEquation(t, param1, param2, ..): return f(param1,param2,..)

        method: lmfit method (slsqp, leastsq)
    """

    def __init__(self, paramList, growthEquation, method='slsqp'):
        self.paramList = paramList
        self.growthEquation = growthEquation
        self.gmod = Model(growthEquation)
        self.method = method

    def calcFit(self, t, data, **kwargs):
        if 'method' not in kwargs:
            method = self.method
        else:
            method = kwargs['method']

        for param in self.paramList:
            # Check if the parameter is a lambda function
            temp = dict()
            for hint in ['guess', 'min', 'max']:
                # print('hint: ',hint,'param[hint]: ',param[hint])
                if type(param[hint]) == type(lambda x: 0):
                    temp[hint] = param[hint](data)
                else:
                    temp[hint] = param[hint]
            self.gmod.set_param_hint(param['name'],
                                     value=temp['guess'],
                                     min=temp['min'],
                                     max=temp['max'],
                                     vary=param['vary'])
        try:
            params = self.gmod.make_params()
        except Exception as e:
            print(data)
            print(e)

        result = self.gmod.fit(data, params, t=t, method=method, **kwargs)
        result.fit_report()
        return result
예제 #11
0
def PlotSPE(h,b,c,ext):
    
    #b1=numpy.array(b1)
    #c1=numpy.array(c1)
    #cond = (b1>=600) & (b1<=800)
    #b = b1[ cond ]
    #c = c1[ cond ]
    
    ofilename=OUTDIR+"/"+bsfile[0]+".png"
    sh_marker=["+",".","^","*","p","s","x","D","h","o","+",".","^","*","p","s","x","D","h","o","+",".","^","*","p","s"]
    sh_color=['b','g','r','c','m','y','k','orange','darkblue','darkgreen','darkred','darkcyan','darkmagenta','deeppink','firebrick','gold','darkviolet','lavenderblush','dodgerblue','indigo','limegreen']
    #gmodel = Model(gaussian) + Model(line)
    #gmodel = Model(gaussian) + Model(expo)
    gmodel = Model(fit_function)
    gmodel.set_param_hint('A', value=10000)
    gmodel.set_param_hint('beta', value=0.001)
    gmodel.set_param_hint('B', value=500)
    gmodel.set_param_hint('mu', value=660)
    gmodel.set_param_hint('sigma', value=30)
    
    pars = gmodel.make_params()
    result = gmodel.fit(c, pars,x=b)
    print(result.fit_report())
    #print result.params
    print "%6.2f %6.4f"%(result.params["mu"].value,result.params["sigma"].value)

    fig = plt.gcf()
    plt.title(h[0],fontsize=14,fontweight='bold')
    if "spe" in ext:
        plt.xlabel('Energy ',fontsize=14,fontweight='bold')
    else:
        plt.xlabel('Bins ',fontsize=14,fontweight='bold')
    plt.ylabel('Counts ',fontsize=14,fontweight='bold')
    plt.tick_params(labelsize=18)
    #plt.text(energyBins[binid][0]+10, 0.8*maxv2,strv,fontsize=14,fontweight='bold')

    plt.plot(b,c,marker=sh_marker[0],linestyle='None',color=sh_color[0],label=bsfile)
    plt.plot(b, result.best_fit, 'r-', label="fit %6.2f %6.4f"%(result.params["mu"].value,result.params["sigma"].value))

    legend = plt.legend(loc='upper right', shadow=True)
    frame = legend.get_frame()
    frame.set_facecolor('0.90')
    for label in legend.get_texts():
        label.set_fontsize('large')
    for label in legend.get_lines():
        label.set_linewidth(1.5)  # the legend line width

    plt.grid(True)
    #plt.yscale('log')
    plt.autoscale(enable=True, axis='y')
    fig.set_size_inches(16,12)
    plt.savefig(ofilename,dpi=100)
    plt.show()
예제 #12
0
def all_fits(count,pers,plot=False,subax=None,do_two_expo=False):
	tmp={}

	# single
	try:
		popt, pcov = curve_fit(single_exp,pers[2::],count[2::])
		model=Model(single_exp)
		model.set_param_hint('a1', value=popt[0],vary=False)
		model.set_param_hint('b1', value=popt[1],vary=False)
		result = model.fit(count[2::], x=pers[2::])
		tmp['single_exp']={'bic':result.bic,'params':result.best_values,'best_fit':result.best_fit}
	except Exception,e:
		tmp['single_exp']={'bic':None,'params':{'a1':np.nan,'b1':np.nan},'best_fit':None}
예제 #13
0
def test_main():
    from get_ssa import get_ssa
    zenith = 53.1836240528
    AMass = 1.66450160404
    rel_h = 0.665
    pressure = 950
    AM = 5
    ssa = get_ssa(rel_h, AM)
    x = np.linspace(200, 800, 100)  # config
    variables = ['alpha', 'beta', 'g_dsa', 'g_dsr']  # config
    expected_values = [2.5, 0.06, 0.6, 0.5]
    print('Expected: %s' % expected_values)
    guess = [1.0, 0.01, 0.5, 0.8]  # config
    bounds = [(-0.2, 4), (0., 3), (0., 2.), (0., 2.)]  # config

    # Theano
    irr_symbol = IrradianceModel_sym(x, zenith, AMass, pressure, ssa, variables)
    getIrrRatio = irr_symbol.getcompiledModel('ratio')
    y_theano = getIrrRatio(*expected_values)
    res = Residuum(irr_symbol, 'ratio')
    residuum = FitWrapper(res.getResiduum())
    residuals = FitWrapper(res.getResiduals())
    derivative = FitWrapper(res.getDerivative())
    Fit = FitModel()
    result = Fit._minimize(residuum, guess, y_theano, bounds, jacobian=derivative)
    print("Got %s" % result.x)
    resultls = Fit._least_squares(residuals, guess, y_theano, bounds)
    print("Got %s" % resultls.x)


    # Python
    IrradianceObject = IrradianceModel_python(AMass, rel_h, ssa, zenith, pressure)
    y_python = IrradianceObject.irradiance_ratio(x, 2.5, 0.06,0.0, 0.6, 0.5)

    gmod = Model(IrradianceObject.irradiance_ratio, independent_vars=['x'], param_names=variables)
    gmod.set_param_hint('alpha', value=guess[0], min=bounds[0][0], max=bounds[0][1])
    gmod.set_param_hint('beta',  value=guess[1], min=bounds[1][0], max=bounds[1][1])
    gmod.set_param_hint('g_dsa', value=guess[2], min=bounds[2][0], max=bounds[2][1])
    gmod.set_param_hint('g_dsr', value=guess[3], min=bounds[3][0], max=bounds[3][1])

    result_lmfit = gmod.fit(y_python, x=x)
    print(result_lmfit.fit_report())

    plt.plot(x, y_theano)
    x_new = np.linspace(300, 900,150)
    irr_symbol.set_wavelengthAOI(x_new)
    getIrrRatio = irr_symbol.getcompiledModel('ratio')
    y_new = getIrrRatio(*expected_values)
    plt.plot(x_new, y_new, '+', label='different wavelengths')
    plt.legend()
    plt.show()
def center_on_cos(raw_quadratures, phi0=None, omega=None, snap_omega=False):
    mean = scipy.average(raw_quadratures, axis=1)
    no_angles, no_pulses = raw_quadratures.shape
    model = Model(cos_model)
    offset, amplitude, phi0, omega = guess_initial_parameters(
        mean, phi0, omega)
    model.set_param_hint("offset", value=offset)
    model.set_param_hint("amplitude", min=0., value=amplitude)
    model.set_param_hint("phi0", value=phi0)
    model.set_param_hint("omega", min=0., value=omega)
    model.make_params(verbose=False)
    steps = scipy.arange(no_angles)
    res = model.fit(mean, x=steps, verbose=False)
    omega_param = res.params["omega"]
    if snap_omega:
        appx_omega = float(omega_param)
        no_pi_intervals = int(round(pi / appx_omega))
        omega = pi / no_pi_intervals
        omega_param.set(omega, vary=False)
        res.fit(mean, x=steps, verbose=False)
    d_value, p_value_ks = kstest(res.residual, 'norm')
    mean_fit = res.eval(x=steps)
    offset = mean - mean_fit
    aligned_quadratures = raw_quadratures - offset[:, None]
    centered_quadratures = aligned_quadratures - float(res.params["offset"])
    return (centered_quadratures, float(omega_param),
            float(res.params["phi0"]), p_value_ks)
def center_on_cos(raw_quadratures, phi0=None, omega=None, snap_omega=False):
    mean = scipy.average(raw_quadratures, axis=1)
    no_angles, no_pulses = raw_quadratures.shape
    model = Model(cos_model)
    offset, amplitude, phi0, omega = guess_initial_parameters(mean, phi0, omega)
    model.set_param_hint("offset", value=offset)
    model.set_param_hint("amplitude", min=0., value=amplitude)
    model.set_param_hint("phi0", value=phi0)
    model.set_param_hint("omega", min=0., value=omega)
    model.make_params(verbose=False)
    steps = scipy.arange(no_angles)
    res = model.fit(mean, x=steps, verbose=False)
    omega_param = res.params["omega"]
    if snap_omega:
        appx_omega = float(omega_param)
        no_pi_intervals = int(round(pi/appx_omega))
        omega = pi/no_pi_intervals
        omega_param.set(omega, vary=False)
        res.fit(mean, x=steps, verbose=False)
    d_value, p_value_ks = kstest(res.residual, 'norm')
    mean_fit = res.eval(x=steps)
    offset = mean-mean_fit
    aligned_quadratures = raw_quadratures - offset[:,None]
    centered_quadratures = aligned_quadratures - float(res.params["offset"])
    return (centered_quadratures,
            float(omega_param), float(res.params["phi0"]), p_value_ks)
예제 #16
0
def create_model(func, backend="lmfit"):
    """
    Returns a model-class of the selected package for fitting.

    Parameters
    ----------
    func    : 
        sth

    backend : str
        - lmfit: uses `lmfit´ package for fitting
        - iminuit: uses `iminuit´ package for fitting

    Return
    ------
    model   : lmfit.Model or iminuit.Minuit object
    """
    if backend.upper() == "LMFIT":
        model = Model(func)
        model.set_param_hint("A", min=0.0)
        model.set_param_hint("omega", value=2 * pi / 16, vary=False)
        model.set_param_hint("phi", min=0.0, max=2 * pi)
        model.set_param_hint("y0", min=0.0)
        return model

    elif backend.upper() == "IMINUIT":
        raise NotImplementedError

    else:
        raise KeyError("The backend '{}' is not recognized.".format(backend))
예제 #17
0
def fit_model_error(t, y, wt=1):
    gmodel = Model(correlation)
    gmodel.set_param_hint('g0', value=0.1)
    gmodel.set_param_hint('tauD', value=1e-4, min=1e-6, max=100)
    gmodel.set_param_hint('sp', value=0.01, min=0.001, max=0.1)
    gmodel.set_param_hint('bl', value=1e-6)
    pars = gmodel.make_params()
    return gmodel.fit(y, pars, t=t, weights=wt)
예제 #18
0
    def test_weird_param_hints(self):
        # tests Github Issue 312, a very weird way to access param_hints
        def func(x, amp):
            return amp*x

        m = Model(func)
        models = {}
        for i in range(2):
            m.set_param_hint('amp', value=1)
            m.set_param_hint('amp', value=25)

            models[i] = Model(func, prefix='mod%i_' % i)
            models[i].param_hints['amp'] = m.param_hints['amp']

        self.assertEqual(models[0].param_hints['amp'],
                         models[1].param_hints['amp'])
예제 #19
0
    def test_hints_in_composite_models(self):
        # test propagation of hints from base models to composite model
        def func(x, amplitude):
            pass

        m1 = Model(func, prefix='p1_')
        m2 = Model(func, prefix='p2_')

        m1.set_param_hint('amplitude', value=1)
        m2.set_param_hint('amplitude', value=2)

        mx = (m1 + m2)
        params = mx.make_params()
        param_values = {name: p.value for name, p in params.items()}
        self.assertEqual(param_values['p1_amplitude'], 1)
        self.assertEqual(param_values['p2_amplitude'], 2)
예제 #20
0
def _iterate(simulation, expected, guess, bounds, variables, independent, callable_, global_, local_):
    biased_parameters = np.zeros((len(local_['input']), len(expected)))
    success_ar = np.zeros(len(local_['input']))
    expected_dict = {key: value for key, value in zip(variables, expected)}
    gmod = Model(callable_, independent_vars=independent.keys(), param_names=variables, method='lbfgsb')
    for param, ini, limits in zip(variables, guess, bounds):
        gmod.set_param_hint(param, value=ini, min=limits[0], max=limits[1])

    independent[global_['param']] = global_['input']
    for i, input_ in enumerate(local_['input']):
        independent[local_['param']] = input_
        result = gmod.fit(simulation, verbose=False, **independent)
        print(result.fit_report())
        success_ar[i] = result.success
        biased_parameters[i] = np.array([result.params[key].value - expected_dict[key] for key in variables])
    return biased_parameters, success_ar
def test_constraints_function_call():
    """Test a constraint with simple function call in Model class."""
    x = [1723, 1773, 1823, 1523, 1773, 1033.03078, 1042.98077, 1047.90937,
         1053.95899, 1057.94906, 1063.13788, 1075.74218, 1086.03102]
    y = [0.79934, -0.31876, -0.46852, 0.05, -0.21, 11.1708, 10.31844, 9.73069,
         9.21319, 9.12457, 9.05243, 8.66407, 8.29664]

    def VFT(T, ninf=-3, A=5e3, T0=800):
        return ninf + A/(T-T0)

    vftModel = Model(VFT)
    vftModel.set_param_hint('D', vary=False, expr=r'A*log(10)/T0')
    result = vftModel.fit(y, T=x)

    assert 2600.0 < result.params['A'].value < 2650.0
    assert 7.0 < result.params['D'].value < 7.5
예제 #22
0
    def getModel(self, guess_T12, guess_fragility, guess_log_eta_inf):
        '''
        Creates a model for regression.

        Parameters
        ----------
        guess_T12 : float
            Guess for the temperature were the viscosity is 10^12 Pa.s.

        guess_fragility : float
            Guess for the fragility index.

        guess_log_eta_inf : array_like, optional
            Guess for the base-10 logarithm of the infinite viscosity.

        Notes
        -----
        The parameters 'T0' and 'A' are also added in the model paremeters.

        Returns
        -------
        model : instance of lmfit's Model class.

        '''
        model = Model(eq.Dienes, name=self.__str__())

        T12 = guess_T12
        m = guess_fragility
        n = guess_log_eta_inf

        guess_T0 = T12 * (1 - (12 - n) / m)
        guess_A = -log(10) * (12 - n - m) / (guess_T0 / (T12 - guess_T0)**2)
        guess_B = T12 * log(10) / guess_T0 * (T12 *
                                              (12 - n - m) + guess_T0 * m)

        model.set_param_hint('log_eta_inf',
                             vary=True,
                             max=11.99,
                             value=guess_log_eta_inf)
        model.set_param_hint(
            'T0',
            vary=True,
            min=0,
            value=guess_T0,
        )
        model.set_param_hint(
            'A',
            vary=True,
            value=guess_A,
        )
        model.set_param_hint(
            'B',
            vary=True,
            value=guess_B,
        )

        return model
예제 #23
0
def fit_fold(pspec, init):
    '''
    Fit the Fold power spectrum model to pspec and compute AIC score.
    Uses the package LMFIT for optimisation.
    
    Args
    --------------
    pspec: pd.Series
        Power spectrum data as a Series indexed by frequency.
    init: list of floats
        Initial parameter guesses of the form [sigma_init, lambda_init].
        
    Returns
    ----------------
    list:
        Form [aic, result] where aic is the AIC score for the model fit,
        and result is a handle that contains further information on the fit.

    '''

    # Put frequency values and power values as a list to use LMFIT
    freq_vals = pspec.index.tolist()
    power_vals = pspec.tolist()

    sigma_init, lambda_init = init
    # Assign model object
    model = Model(psd_fold)
    # Set up constraint S(wMax) < psi_fold*S(0)
    psi_fold = 0.5
    wMax = max(freq_vals)
    # Parameter constraints for sigma
    model.set_param_hint('sigma', value=sigma_init, min=0, max=10 * sigma_init)
    # Parameter constraints for lambda
    model.set_param_hint('lam',
                         min=-np.sqrt(psi_fold / (1 - psi_fold)) * wMax,
                         max=0,
                         value=lambda_init)

    # Assign initial parameter values and constraints
    params = model.make_params()
    # Fit model to the empircal spectrum
    result = model.fit(power_vals, params, w=freq_vals)
    # Compute AIC score
    aic = result.aic

    # Export AIC score and model fit
    return [aic, result]
예제 #24
0
def fit_model(t, y, jj, zjj, meanz, stdd):
    gmodel = Model(gaus1)
    gmodel.set_param_hint('a', value=zjj, min=0.8*zjj, max=1.2*zjj)
    gmodel.set_param_hint('b', value=jj, min=jj-2, max=jj+2)
    gmodel.set_param_hint('w', value=1.2, min=0.1, max=20)
    gmodel.set_param_hint('y0', value=meanz, min=meanz -
                          stdd/2, max=meanz + stdd/2)
    pars = gmodel.make_params()
    return gmodel.fit(y, pars, t=t)
예제 #25
0
def PixSED_Xstk(nus, maps, FuncModel, pix, pix_red, istk, covMat, nus_edge,
		   maxfev = 10000, verbose = False, initP0 = None, chi2 = None,
		  nsamples = 5000, new = False):
	
	if new:
		gmodel = Model(FuncModel)

		#Set initial params...
		#print(gmodel.fit(maps[:, pix, istk], pars, x = nus, nan_policy = "omit"))
		if initP0 != None:
			for j, ipar in enumerate(gmodel.param_names):
				gmodel.set_param_hint(ipar, value = initP0[j])
		pars = gmodel.make_params()
		fit_par = gmodel.fit(maps[:, pix, istk], pars, x = nus, nan_policy = "omit")

		popt = list(fit_par.best_values.values())
	else:
		popt, pcov = curve_fit(FuncModel, nus, maps[:, pix, istk], 
								sigma = covMat[:, :, istk, pix_red], absolute_sigma=True,
								maxfev = maxfev, p0 = initP0)
		#popt = initP0
	if verbose:
		print("Calling LogLikelihood", popt, pcov)
		print("xvals, yvals", nus, maps[:, pix, istk])
	myfit = mcmc.LogLikelihood(xvals = nus, yvals = maps[:, pix, istk], 
							   errors = covMat[:, :, istk, pix_red], chi2 = chi2,
							   model = FuncModel, p0 = popt)
	#print("myfit info: " )
	fit_prep = myfit.run(nsamples)
	#print("Doing chain")
	flat_samples = fit_prep.get_chain(discard = nsamples//2, thin=32, flat=True)
	#print("Samples ", flat_samples, np.shape(flat_samples))
	nspls = flat_samples.shape[0]
	#Generating realizations for parameters of the model (fake X(nu))
	
	x = np.linspace(nus_edge[0], nus_edge[-1], nsamples//2)
	vals = np.zeros((len(x), nspls))
	for i in range(len(x)):
		for j in range(nspls):
			vals[i, j] = FuncModel(x[i], *flat_samples[j, :])
	
	mvals = np.mean(vals, axis=1)
	svals = np.std(vals, axis=1)
	
	return mvals, svals, x, flat_samples
예제 #26
0
def fit_null(pspec, init):
    '''
    Fit the Null power spectrum model to pspec and compute AIC score.
    Uses the package LMFIT for optimisation.
    
    Args
    --------------
    pspec: pd.Series
        Power spectrum data as a Series indexed by frequency
    init: list of floats
        Initial parameter guesses of the form [sigma_init]
        
    Returns
    ----------------
    list:
        Form [aic, result] where aic is the AIC score for the model fit,
        and result is a handle that contains further information on the fit.

    '''

    # Put frequency values and power values as a list to use LMFIT
    freq_vals = pspec.index.tolist()
    power_vals = pspec.tolist()

    sigma_init = init[0]

    # Assign model object
    model = Model(psd_null)

    # Initial parameter value for Null fit
    model.set_param_hint('sigma',
                         value=sigma_init,
                         vary=True,
                         min=0,
                         max=10 * sigma_init)

    # Assign initial parameter values and constraints
    params = model.make_params()
    # Fit model to the empircal spectrum
    result = model.fit(power_vals, params, w=freq_vals)
    # Compute AIC score
    aic = result.aic

    # Export AIC score and model fit
    return [aic, result]
예제 #27
0
    def getModel(self, guess_T12, guess_fragility, guess_log_eta_inf):
        '''
        Creates a model for regression.

        Parameters
        ----------
        guess_T12 : float
            Guess for the temperature were the viscosity is 10^12 Pa.s.

        guess_fragility : float
            Guess for the fragility index.

        guess_log_eta_inf : array_like, optional
            Guess for the base-10 logarithm of the infinite viscosity.

        Notes
        -----
        The parameters 'T0' and 'A' are also added in the model paremeters.

        Returns
        -------
        model : instance of lmfit's Model class.

        '''
        model = Model(eq.AM_alt, name=self.__str__())

        model.set_param_hint('T12', vary=True, min=0, value=guess_T12)
        model.set_param_hint('m', vary=True, min=0, value=guess_fragility)
        model.set_param_hint('log_eta_inf',
                             vary=True,
                             max=11.99,
                             value=guess_log_eta_inf)
        model.set_param_hint(
            'alpha',
            vary=False,
            expr=r'm / (12 - log_eta_inf)',
        )
        model.set_param_hint(
            'beta',
            vary=False,
            expr=
            r'T12 * (log(10) * (12 - log_eta_inf))**((12 - log_eta_inf)/m)',
        )

        return model
예제 #28
0
def fit_gaussian(seq, *params, report=False):
    x = np.arange(len(seq))
    y = seq
    amp, cen, wid = params
    gmodel = Model(gaussian)
    # limit the amplitude to be in 0-256
    gmodel.set_param_hint('amp', min=0)
    gmodel.set_param_hint('amp', max=256)
    result = gmodel.fit(y, x=x, amp=amp, cen=cen, wid=wid)
    if result.redchi >= 1e3:
        #    if report:
        plt.figure()
        plt.plot(x, y, 'bo')
        #        plt.plot(x, result.init_fit, 'k--')
        plt.plot(x, np.ceil(result.best_fit), 'r-')
        plt.title('chi2: {0:.2e} - red_chi2: {0:.2e}'.format(
            result.chisqr, result.redchi))
        plt.show()
    return result
예제 #29
0
    def getModel(self, guess_T12, guess_fragility, guess_log_eta_inf):
        '''
        Creates a model for regression.

        Parameters
        ----------
        guess_T12 : float
            Guess for the temperature were the viscosity is 10^12 Pa.s.

        guess_fragility : float
            Guess for the fragility index.

        guess_log_eta_inf : array_like, optional
            Guess for the base-10 logarithm of the infinite viscosity.

        Notes
        -----
        The parameters 'K' and 'C' from Eq. (6) from Ref. [1] are also added in
        the model paremeters.

        Returns
        -------
        model : instance of lmfit's Model class.

        '''
        model = Model(eq.MYEGA_alt, name=self.__str__())

        model.set_param_hint('T12', vary=True, min=0, value=guess_T12)
        model.set_param_hint('m', vary=True, min=0, value=guess_fragility)
        model.set_param_hint('log_eta_inf',
                             vary=True,
                             max=11.99,
                             value=guess_log_eta_inf)
        model.set_param_hint(
            'K',
            vary=False,
            expr=r'(12-log_eta_inf)*T12*exp(1-m/(12-log_eta_inf))')
        model.set_param_hint('C',
                             vary=False,
                             expr=r'T12*(m/(12-log_eta_inf)-1)')

        return model
    def FitSpectrumInit(self, label):
        """
        Fit the spectrum with the fit params of another spectrum (given by label) as initial values. Useful when you fit big number of similar spectra.
        """

        borders = np.genfromtxt(label + '/spectrumborders_' + label + '.txt',
                                unpack=True)
        np.savetxt(self.label + '/spectrumborders_' + self.label + '.txt',
                   borders)
        self.y = self.y[(self.x > borders[0]) & (self.x < borders[-1])]
        self.x = self.x[(self.x > borders[0]) & (self.x < borders[-1])]
        FitData = np.load(label + '/fitparams_' + label + '.npz')
        baseline = FitData['c'] / self.maxyvalue
        ctr = FitData['x0']
        sigma = FitData['sigma']
        gamma = FitData['gamma']
        ramanmodel = ConstantModel()
        ramanmodel.set_param_hint('c', value=baseline[0], min=0)

        for i in range(len(sigma)):
            prefix = 'p' + str(i + 1)
            tempvoigt = Model(func=voigt, prefix=prefix)
            tempvoigt.set_param_hint(prefix + 'x0', value=ctr[i], min=0)
            tempvoigt.set_param_hint(prefix + 'sigma', value=sigma[i], min=0)
            tempvoigt.set_param_hint(prefix + 'gamma', value=gamma[i], min=0)
            tempvoigt.set_param_hint(prefix + 'height',
                                     expr='wofz(((0) + 1j*' + prefix +
                                     'gamma) / ' + prefix +
                                     'sigma / sqrt(2)).real')
            tempvoigt.set_param_hint(prefix + 'fwhm',
                                     expr='0.5346 * 2 *' + prefix +
                                     'gamma + sqrt(0.2166 * (2*' + prefix +
                                     'gamma)**2 + (2 * ' + prefix +
                                     'sigma * sqrt(2 * log(2) ) )**2  )')
            ramanmodel += tempvoigt

        pars = ramanmodel.make_params()
        fitresult = ramanmodel.fit(self.y, pars, x=self.x, scale_covar=True)

        plt.clf()
        comps = fitresult.eval_components()
        xplot = np.linspace(self.x[0], self.x[-1], 1000)
        plt.plot(self.x, self.y * self.maxyvalue, 'r-')
        plt.plot(self.x, fitresult.best_fit * self.maxyvalue)
        for i in range(0, len(sigma)):
            plt.plot(
                self.x, comps['p' + str(i + 1)] * self.maxyvalue +
                comps['constant'] * self.maxyvalue, 'k-')
        plt.savefig(self.label + '/rawplot_' + self.label + '.pdf')
        save_modelresult(fitresult,
                         self.label + '/modelresult_' + self.label + '.sav')
        plt.clf()
    def FitSpectrum(self):
        """
        Fit Spectrum with initial values provided by SelectBaseline() and SelectPeaks()
        """

        polyparams = self.Fitbaseline(self)
        base = polyparams[0].n
        ramanmodel = ConstantModel()
        ramanmodel.set_param_hint('c', value=base, min=0)
        globwidth = 1

        xpeak, ypeak = np.genfromtxt(self.peakfile, unpack=True)
        if type(xpeak) == np.float64:
            xpeak = [xpeak]
            ypeak = [ypeak]

        for i in range(0, len(xpeak)):
            prefix = 'p' + str(i + 1)

            tempvoigt = Model(func=voigt, prefix=prefix)
            tempvoigt.set_param_hint(prefix + 'x0', value=xpeak[i], min=0)
            tempvoigt.set_param_hint(prefix + 'sigma', value=globwidth, min=0)
            tempvoigt.set_param_hint(prefix + 'gamma', value=globwidth, min=0)
            tempvoigt.set_param_hint(prefix + 'height',
                                     value=ypeak[i],
                                     expr='wofz(((0) + 1j*' + prefix +
                                     'gamma) / ' + prefix +
                                     'sigma / sqrt(2)).real')
            tempvoigt.set_param_hint(prefix + 'fwhm',
                                     expr='0.5346 * 2 *' + prefix +
                                     'gamma + sqrt(0.2166 * (2*' + prefix +
                                     'gamma)**2 + (2 * ' + prefix +
                                     'sigma * sqrt(2 * log(2) ) )**2  )')
            ramanmodel += tempvoigt

        pars = ramanmodel.make_params()
        fitresult = ramanmodel.fit(self.y, pars, x=self.x, scale_covar=True)

        print(fitresult.fit_report(min_correl=0.5))
        comps = fitresult.eval_components()
        xplot = np.linspace(self.x[0], self.x[-1], 1000)
        plt.plot(self.x, self.y * self.maxyvalue, 'rx')
        plt.plot(self.x, fitresult.best_fit * self.maxyvalue)
        for i in range(0, len(xpeak)):
            plt.plot(
                self.x, comps['p' + str(i + 1)] * self.maxyvalue +
                comps['constant'] * self.maxyvalue, 'k-')
        plt.show()
        plt.savefig(self.label + '/rawplot_' + self.label + '.pdf')
        save_modelresult(fitresult,
                         self.label + '/modelresult_' + self.label + '.sav')
예제 #32
0
def fit_beam_center(Ixy_data):
    """
    Calculates the center of a 2D dataset with I(x, y) as
    input. Fitting a 1D gaussian function to each dimension summing
    over all remaining ones.

    Parameters
    ----------
    Ixy_data: np.ndarray
        neutron intensity data as given by a 2D detector (CASCADE, 'neutron camera')

    Return
    ------
    ('center axis0', 'center axis1', 'center axis2', ...)

    Notes
    -----
    works also for n-dimensional data set I(x1, x2, ..., xn)
    """

    def sum_axis(index, length):
        templist = list(range(length))
        templist.remove(index)
        return templist

    center_vals = []

    gaussian_model = Model(gaussian_function)
    gaussian_model.set_param_hint('amp', min=0)
    gaussian_model.set_param_hint('x0', min=0, max=128)
    gaussian_model.set_param_hint('sig', min=0)
    gaussian_model.set_param_hint('bckg', min=0)

    I_shape = Ixy_data.shape
    for i, l in enumerate(I_shape):
        fit_data = Ixy_data.sum(axis = tuple(sum_axis(i, len(I_shape))))
        x0_estimate = np.argmax(fit_data)
        temp_amp = np.max(fit_data)
        sig_estimate = len(np.where(fit_data > temp_amp/2)[0]) / 2.35
        amp_estimate = temp_amp * sqrt(2*pi) * sig_estimate
        fit_res = gaussian_model.fit(
                x=range(l),
                data=fit_data,
                amp=amp_estimate,
                x0=x0_estimate,
                sig=sig_estimate,
                bckg=0
                )
        center_vals.append(fit_res.params['x0'].value)

    return tuple(center_vals)
def fit_gaussian_lorentzian(ET,
                            I,
                            cen=0,
                            offset=10,
                            fwhm=.27,
                            gamma=.3,
                            Gampl=10,
                            Lampl=10,
                            fixcen=False,
                            fixline=False,
                            plot=False):

    mod = Model(gaussian_lorentzian)
    mod.set_param_hint('fwhm', value=fwhm, vary=False)
    mod.set_param_hint('cen', value=cen, vary=not fixcen)
    mod.set_param_hint('offset', value=offset, vary=not fixline)

    out = mod.fit(I,
                  x=ET,
                  offset=offset,
                  gamma=gamma,
                  Gampl=Gampl,
                  Lampl=Lampl)

    if plot:
        fig = plt.figure()
        out.plot_fit()

    return out
예제 #34
0
def Rturn_Vmax(r_array, v_array, negative=False):
    r_array, v_array = abs(r_array), abs(v_array)
    r_array = np.append(r_array, 0.1)
    v_array = np.append(v_array, 5)
    v0 = np.nanmedian(v_array)

    try:
        #x0=[250,5,0,1]
        #vmax ,r_turn,beta,gamma = optimization.curve_fit(vrot, r_array, v_array, p0=x0, bounds = ([0,0,0,0],[500,60,1,50]))[0]

        mod = Model(vrot)
        mod.set_param_hint('v_max', value=v0, vmin=0, vmax=360)
        mod.set_param_hint('beta', value=0, vary=False, min=0, max=1.0)
        mod.set_param_hint('R_turn', value=5, min=0, max=35)
        mod.set_param_hint('gamma', value=1, min=-2, max=12)
        pars = mod.make_params()

        result = mod.fit(v_array, r_sky=r_array)  #, fit_method = "Powell")
        best = result.best_values
        vmax, r_turn, gamma, beta = best["v_max"], best["R_turn"], best[
            "gamma"], best["beta"]

    except (RuntimeError, TypeError, ValueError):
        vmax, r_turn, gamma, beta = 500, 0, 1, 1

    return vmax, r_turn, beta, gamma
예제 #35
0
파일: bazin.py 프로젝트: djbrout/deepsnid
def lmfit(mjd,flux,fluxerr):
    t0_guess = mjd[np.argmax(flux)]
    tau_fall_guess = 40.
    tau_rise_guess = -5.
    A = 150.
    B = 20.
    # nflux = np.zeros(2+len(np.array(flux)))
    # nfluxerr = np.ones(2+len(np.array(flux)))/10.
    # nmjd = np.zeros(2+len(np.array(flux)))
    #
    # nflux[1:-1] = flux
    # nfluxerr[1:-1] = fluxerr
    # nmjd[1:-1] = mjd
    # nmjd[1] = mjd[0]-100.
    # nmjd[-1] = mjd[-1]+150
    #
    # flux = nflux
    # fluxerr = nfluxerr
    # mjd = nmjd

    bmod = Model(bazinfunc)
    bmod.set_param_hint('t0', value=t0_guess, min=t0_guess-20, max=t0_guess+20)
    bmod.set_param_hint('tau_fall', value=tau_fall_guess)
    bmod.set_param_hint('tau_rise', value=tau_rise_guess)
    bmod.set_param_hint('A',value=A)
    bmod.set_param_hint('B',value=B)

    pars = bmod.make_params()
    #print(bmod.param_names)
    #print(bmod.independent_vars)
    # print(np.array(flux))
    # print(np.array(1./np.array(fluxerr)))
    # print(np.array(mjd))
    result = bmod.fit(np.array(flux),method='leastsq',weights=1./np.array(fluxerr), t=np.array(mjd))

    #print(result.fit_report())
    # plt.clf()
    # plt.errorbar(np.array(mjd), np.array(flux), yerr=fluxerr,fmt='o')
    # plt.plot(np.array(mjd), result.init_fit, 'k--')
    # plt.plot(np.array(mjd), result.best_fit, 'r-')
    # #plt.xlim(mjd[1],mjd[-2])
    # plt.savefig('bazinfit.png')



    chisq = result.redchi
    ps = result.best_values
    popt = [ps['t0'],ps['tau_fall'],ps['tau_rise'],ps['A'],ps['B']]
    #print('popt',popt)
    #sys.exit()
    # if chisq < 2.:
    #     input('good chisq!')

    # popt, pcov, infodict, errmsg, ier = curve_fit(bazinfunc, mjd, flux,
    #                                               sigma=fluxerr, p0=p0, maxfev=2000000, full_output=True)
    #
    # chisq = (infodict['fvec'] ** 2).sum() / (len(infodict['fvec']) - len(popt))
    return chisq,popt
def GaussStepConst(signal, guess):
    """
    Fits high contrast data very well
    """
    if guess == False:
        return [0, 0, 0]
    else:
        amp, centre, stdev, offset = guess
        
        data = np.array([range(len(signal)), signal]).T
        X = data[:,0]
        Y = data[:,1]

#         gauss_mod = Model(gaussian)
        gauss_mod = Model(gaussian)
        const_mod = ConstantModel()
        step_mod = StepModel(prefix='step')
        
        gauss_mod.set_param_hint('width', value = stdev / 2., min=stdev / 3., max=stdev)
        gauss_mod.set_param_hint('fwhm', expr='2.3548*width')
        pars = gauss_mod.make_params(height=amp, center=centre, width=stdev / 2., offset=offset)
        
        pars += step_mod.guess(Y, x=X, center=centre)

        pars += const_mod.guess(Y, x=X)
        
        pars['width'].vary = False
        
        mod = const_mod + gauss_mod + step_mod
        result = mod.fit(Y, pars, x=X)
        # write error report
        #print result.fit_report()
        
        fwhm = result.best_values['width'] * 2.3548
        
    return X, result.best_fit, result.redchi, fwhm
예제 #37
0
def tau_fitter(data,nbins):
    
    profile_peak = np.max(data)
    binpeak = np.argmax(data)    
    modelname = GxETrain
    model = Model(modelname)
            
    model.set_param_hint('nbins', value=nbins, vary=False)       
    model.set_param_hint('sigma', value=15, vary=True, min =0, max = nbins)
    model.set_param_hint('mu', value=binpeak, vary=True, min=0, max = nbins)
    model.set_param_hint('A',value=profile_peak, vary=True)
    model.set_param_hint('tau',value=200, vary=True, min=0)
    model.set_param_hint('dc',value = 0, vary = True)
    pars = model.make_params()
    
    #"""Fit data"""
    result = model.fit(data,pars,x=np.linspace(1,nbins,nbins))
#    print(result.fit_report(show_correl = False))
    
    noiselessmodel = result.best_fit
    besttau = result.best_values['tau']
    taustd = result.params['tau'].stderr  ##estimated 1 sigma error

    return noiselessmodel, besttau, taustd
예제 #38
0
def example():
    # possible values
    from get_ssa import get_ssa
    from Model import IrradianceModel

    zenith = 53.1836240528
    AMass = 1.66450160404
    rel_h = 0.665
    pressure = 950
    AM = 5
    ssa = get_ssa(rel_h, AM)
    iteration = 20
    alphas = np.zeros(len(range(1, iteration)) + 1)

    x = np.linspace(200, 800, 100)
    irr = IrradianceModel_python(AMass, rel_h, ssa, zenith, pressure)
    irr_symbol = IrradianceModel(x, zenith, AMass, pressure, ssa)

    func = irr_symbol._irradiance_ratio()

    y = irr.irradiance_ratio(x, 2.5, 0.06, 0.0, 1.0, 1.0)
    for i in range(0, iteration):
        ssa = get_ssa(rel_h, AM)
        print(ssa)
        irr = IrradianceModel_python(AMass, rel_h, ssa, zenith, pressure)
        yerror = np.random.normal(0, 0.009, len(x))
        y = irr.irradiance_ratio(x, 1.5, 0.06, 0.0, 0.6, 0.9) + yerror
        weights = 1 / yerror

        gmod = Model(irr.irradiance_ratio, independent_vars=["x"], param_names=["alpha", "beta", "g_dsa", "g_dsr"])

        gmod.set_param_hint("alpha", value=1.0, min=-0.2, max=2.5)
        gmod.set_param_hint("beta", value=0.01, min=0.0, max=2.0)
        gmod.set_param_hint("g_dsa", value=0.6, min=0.0, max=1.0)
        gmod.set_param_hint("g_dsr", value=0.9, min=0.0, max=1.0)
        print(gmod.param_hints)
        print(gmod.param_names)
        print(gmod.independent_vars)

        result = gmod.fit(y, x=x)
        print(result.fit_report())
        alphas[i] = result.params["alpha"].value

        # plt.plot(x, y, label='%s' % AM)
        # plt.plot(x, result.best_fit, 'r-', label='fit')
    y = irr.irradiance_ratio(x, 1.5, 0.06, 0.0, 0.6, 0.9)
    y2 = irr.irradiance_ratio(x, 1.5, 0.08, 0.0, 0.6, 0.9)

    plt.legend()
    plt.show()
예제 #39
0
"""
A two-parameter model that does not fit a dataset with two observations
"""

import numpy as np
from lmfit import  Model
import matplotlib.pyplot as plt

x = np.linspace(0.1, 0.5, 2)
y = 2.1*x + 3

def line(x, m, b):
    return m*x + b

gmod = Model(line)

# this makes the model impossible
gmod.set_param_hint('m', min=-10.0, max=0.)

result = gmod.fit(y, x=x, m=1, b=0)

print(result.fit_report())

plt.plot(x, y, 'bo')
plt.plot(x, result.init_fit, 'k--')
plt.plot(x, result.best_fit, 'r-')
plt.xlim([0, 0.6])
plt.show()
def minimizefunction(vars,low,high,data):
    mymod=Model(myfitfunction)
    newdata=[]
    x=np.array(range(low,high),int)
    for i in xrange(low,high):
        newdata.append(data[i])
    #params=getparams(vars)
    mymod.set_param_hint('a',value=vars[0])
    mymod.set_param_hint('b',value=vars[1],min=0,max=0.5)
    mymod.set_param_hint('c',value=vars[2])
    mymod.set_param_hint('d',value=vars[3],min=-1,max=1.1)
    mymod.set_param_hint('e',value=vars[4])
    mymod.set_param_hint('f',value=vars[5],min=-1,max=1.1)
    mymod.set_param_hint('g',value=vars[6])
    #print params
    out=mymod.fit(newdata,x=x)
    #result=getresult(out.params)
    print(out.fit_report())
    #x1=np.linspace(1,length,10000)
    plt.plot(x,newdata,'blue',linestyle='dashed',marker='.')
    plt.plot(x,out.init_fit,'y',linewidth=2)
    plt.plot(x,out.best_fit,'r',linewidth=2)
    plt.xlabel("circle(Time)")
    plt.ylabel("fluorescence")
    plt.legend()
    plt.show()
    file_result=open('./result/result_select_itera.txt','r+')
    file_result.read()
    file_result.write(out.fit_report())
    file_result.write('\n\n')
    file_result.close()
def generalfit(vars,Xmin,Xmax,method,data):
    mymod=Model(myfitfunction)
    x=np.array(range(Xmin+1,Xmax+1),int)
    #params=getparams(vars)
    mymod.set_param_hint('a',value=vars[0])
    mymod.set_param_hint('b',value=vars[1],min=0,max=0.5)
    mymod.set_param_hint('c',value=vars[2])
    mymod.set_param_hint('d',value=vars[3],min=-1,max=1.1)
    mymod.set_param_hint('e',value=vars[4])
    mymod.set_param_hint('f',value=vars[5],min=-1,max=1.1)
    mymod.set_param_hint('g',value=vars[6])
    #print params
    '''newdata=[]
    for i in xrange(Xmin,Xmax):
        newdata.append(data[i])'''
    out=mymod.fit(data,x=x,method=method,jac=True)
    #result=getresult(out.params)
    print(out.fit_report())
    #x1=np.linspace(1,length,10000)
    plt.plot(x,data,'blue',linestyle='dashed',marker='.')
    plt.plot(x,out.init_fit,'y',linewidth=2)
    plt.plot(x,out.best_fit,'r',linewidth=2)
    plt.xlabel("circle(Time)")
    plt.ylabel("fluorescence")
    plt.legend()
    plt.show()
    file_result=open('./result/result_general_itera.txt','r+')
    file_result.read()
    file_result.write(out.fit_report())
    file_result.write('\n\n')
    file_result.close()
예제 #42
0
  #print(len(retVal))
  return retVal

def SSE(data, fit_dv):
  r = data - fit_dv
  return (r*r).sum()

# loop through each file in the input
for f in pArgs.input:
  df = pd.read_table(f,sep=',',skiprows=2)
  vv = df.voltage.as_matrix()
  ii = df.current.as_matrix()
  ii = ii*-1
  
  cellModel = Model(cellEqn,nan_policy='omit')
  cellModel.set_param_hint('n',value=1)
  cellModel.set_param_hint('Rs',value=6)
  cellModel.set_param_hint('Rsh',value=1e5)
  cellModel.set_param_hint('Iph',value=20e-3)
  cellModel.set_param_hint('I0',value=1e-9)
  
  #cellModel.set_param_hint('n',min=0)
  #cellModel.set_param_hint('Rs',min=0)
  #cellModel.set_param_hint('Rsh',min=0)
  #cellModel.set_param_hint('Iph',min=0)
  #cellModel.set_param_hint('I0',min=0)  
  
  cellModelV = Model(cellEqnV,nan_policy='omit')
  cellModelV.set_param_hint('n',value=1)
  cellModelV.set_param_hint('Rs',value=6)
  cellModelV.set_param_hint('Rsh',value=1e5)
def fitfunction(vars,length,data,weight=None,method='leastsq'):
    mymod=Model(myfitfunction)
    x=np.array(range(1,length+1),int)
    #params=getparams(vars)
    mymod.set_param_hint('a',value=vars[0])
    mymod.set_param_hint('b',value=vars[1],min=0,max=0.5)
    mymod.set_param_hint('c',value=vars[2])
    mymod.set_param_hint('d',value=vars[3],min=-1,max=1.1)
    mymod.set_param_hint('e',value=vars[4])
    mymod.set_param_hint('f',value=vars[5],min=-1,max=1.1)
    mymod.set_param_hint('g',value=vars[6])
    #print params
    out=mymod.fit(data,x=x,weight=weight)
    #result=getresult(out.params)
    print(out.fit_report())
    #x1=np.linspace(1,length,10000)
    initialRMSE=printresult(data,out.init_fit)
    bestRMSE=printresult(data,out.best_fit)
    plt.plot(x,data,'blue',linestyle='dashed',marker='.')
    plt.plot(x,out.init_fit,'y',linewidth=2)
    print("RMSE of pso is{}".format(initialRMSE))
    if initialRMSE<bestRMSE:
        plt.plot(x,out.init_fit,'r',linewidth=2)
        print("RMSE of iteration is {}".format(initialRMSE))
    else:
        plt.plot(x,out.best_fit,'r',linewidth=2)
        print("RMSE of iteration is {}".format(bestRMSE))
    plt.xlabel("circle(Time)")
    plt.ylabel("fluorescence")
    plt.legend()
    plt.show()
    file_result=open('./result/result_all_itera.txt','r+')
    file_result.read()
    file_result.write(out.fit_report())
    file_result.write('\n\n')
    file_result.close()
예제 #44
0
def tau_fitter(data,nbins, verbose=True):
    profile_peak = np.max(data)
    binpeak = np.argmax(data)  
    modelname = GxETrain
    model = Model(modelname)
                 
    model.set_param_hint('nbins', value=nbins, vary=False)            
    model.set_param_hint('sigma', value=15, vary=True, min =0, max = nbins)
    model.set_param_hint('mu', value=binpeak, vary=True, min=0, max = nbins)
    model.set_param_hint('A',value=profile_peak, vary=True, min=0)
    model.set_param_hint('tau',value=200, vary=True, min=0)
    model.set_param_hint('dc',value = 0, vary = True)
    pars = model.make_params()
    xax=np.linspace(1,nbins,nbins)

    #"""Fit data"""
    result = model.fit(data,pars,x=xax)
    if verbose == True:
        print(result.fit_report(show_correl = True))
    else:
        print "To see fit report, use verbose=True"
    
    noiselessmodel = result.best_fit
    besttau = result.best_values['tau']
    taustd = result.params['tau'].stderr  ##estimated 1 sigma error
    if taustd == None:
       taustd = 0

    bestsig = result.best_values['sigma']
    bestmu = result.best_values['mu']
    bestA = result.best_values['A']
    bestdc = result.best_values['dc']
    
    bestsig_std = result.params['sigma'].stderr
    bestmu_std = result.params['mu'].stderr
    bestA_std = result.params['A'].stderr
    bestdc_std = result.params['dc'].stderr    
    
    bestparams = np.array([bestsig,bestmu,bestA,bestdc])
    bestparams_std = np.array([bestsig_std,bestmu_std,bestA_std,bestdc_std])
    
    """correlations with sigma"""    
    corsig = result.params['sigma'].correl
    #corA = result.params['A'].correl
    #corlist = [corsig,corA]
    
    
    rchi = result.redchi
    #return best values and std errors on the other parameters as well    
    
    return result, noiselessmodel, besttau, taustd, bestparams, bestparams_std, rchi, corsig
##
###Create limits on s, via w50
###The distribution of pulsar duty cylces is heavily skewed with a median at 2.5% and an overall minimum at 0.3% and overall maximum at 63% (Ref:Jayanth)
###This max is clearly huge - and therefore the process is pretty much unconstrained. Should consider inserting the actual distribution

w50min = float((0.3/100)*P)  
w50max =  float((3.0/100)*P)  

smin = w50min/(2*np.sqrt(2*np.log(2)))
smax = w50max/(2*np.sqrt(2*np.log(2)))


modelname = GxETrain
model = Model(modelname)

model.set_param_hint('sigma', value=s, vary=True, min=smin, max=smax)
model.set_param_hint('mu', value=m, vary=True)
model.set_param_hint('A',value=1.5, vary=True, min=0)
model.set_param_hint('tau',value=200, vary=True, min=0)
pars = model.make_params()
#print model.param_hints

#modelname2 = GxESingleFold
#model2 = Model(modelname2)
#
#model2.set_param_hint('sigma', value=s, vary=True, min=smin, max=smax)
#model2.set_param_hint('mu', value=m, vary=True)
#model2.set_param_hint('A',value=1.5, vary=True, min=0)
#model2.set_param_hint('tau',value=200, vary=True, min=0)
#pars2 = model2.make_params()
##print model2.param_hints
예제 #46
0
def tau_1D_fitter(data,nbins):

    profile_peak = np.max(data)
    binpeak = np.argmax(data)
    modelname = GxETrain1D
    model = Model(modelname)

    model.set_param_hint('nbins', value=nbins, vary=False)
    model.set_param_hint('sigma', value=15, vary=True, min =0, max = nbins)
    model.set_param_hint('mu', value=binpeak, vary=True, min=0, max = nbins)
    model.set_param_hint('A',value=profile_peak, vary=True,min=0)
    model.set_param_hint('tau1',value=200, vary=True, min=0)
#    model.set_param_hint('tau1',value=166.792877, vary=False)
    model.set_param_hint('dc',value = 0, vary = True)
    pars = model.make_params()

    result = model.fit(data,pars,x=np.linspace(1,nbins,nbins))
#    print(result.fit_report(show_correl = False))

    noiselessmodel = result.best_fit
    besttau = result.best_values['tau1']
    taustd = result.params['tau1'].stderr  ##estimated 1 sigma error
    if taustd == None:
       taustd = 0
    bestsig = result.best_values['sigma']
    bestmu = result.best_values['mu']
    bestA = result.best_values['A']
    bestdc = result.best_values['dc']

    bestsig_std = result.params['sigma'].stderr
    bestmu_std = result.params['mu'].stderr
    bestA_std = result.params['A'].stderr
    bestdc_std = result.params['dc'].stderr

    bestparams = np.array([bestsig,bestmu,bestA,bestdc])
    bestparams_std = np.array([bestsig_std,bestmu_std,bestA_std,bestdc_std])

    """correlations with sigma"""
    corsig = result.params['sigma'].correl
    #corA = result.params['A'].correl
    #corlist = [corsig,corA]

    rchi = result.redchi

    return result, noiselessmodel, besttau, taustd, bestparams, bestparams_std, rchi, corsig
예제 #47
0
def gauss_fit(x, y, a0=None, x0=None, sig0=None, emission=True):
    """ Return ``curve_fit``, i.e., ``popt, pcov``.

    def gauss_fit(x, y, a0=None, x0=None, sig0=None, emission=True, ssize=0.05):
    
    # def gauss(x, a, x0, sigma):
    #     y0=1.
    #     return a*_np.exp(-(x-x0)**2/(2*sigma**2))+y0
    if ssize < 0 or ssize > .5:
        _warn.warn('Invalid ssize value...', stacklevel=2)
        ssize = 0
    ssize = int(ssize * len(y))
    if ssize == 0:
        ssize = 1

    q = 95
    func = _np.max
    if not emission:
        func = _np.min
        q = 5
    if a0 is None:
        a0 = _np.abs(_np.percentile(y, q)) - _np.median(y)
    if x0 is None:
        x0 = x[_np.where(y == func(y))]
    if sig0 is None:
        sig0 = (_np.max(x)-_np.min(x))/10.
    # if y0 is None:
    #     y0 = np.median(y)
    # gmodel = _Model(gauss)
    # gmodel.set_param_hint('a', min=0.2, max=20)
    # if not emission:
    #     gmodel.set_param_hint('a', min=-0.2, max=-4)
    # gmodel.set_param_hint('sigma', min=50, max=1000)
    # result = gmodel.fit(y, x=x, a=a0, x0=x0, sigma=sig0)
    # print(result.params['a'], result.params['sigma'], result.params['x0'])
    # return result.params['a']*_np.sqrt(_np.pi*2)*result.params['sigma']
    medx0, medx1 = _np.average(x[:ssize]), _np.average(x[-ssize:])
    if ssize > 9:
        medy0, medy1 = _np.median(y[:ssize]), _np.median(y[-ssize:])
    else:
        medy0, medy1 = _np.average(y[:ssize]), _np.average(y[-ssize:])
    new_y = medy0 + (medy1 - medy0) * (x - medx0) / (medx1 - medx0)
    g_init = _models.Gaussian1D(amplitude=a0, mean=x0, stddev=sig0)
    fit_g = _fitting.LevMarLSQFitter()
    g = fit_g(g_init, x, y-new_y)
    """
    q = 95
    func = np.max
    if not emission:
        func = np.min
        q = 5
    if a0 is None:
        a0 = np.abs(np.percentile(y, q)) - np.median(y)
    if x0 is None:
        x0 = x[np.where(y == func(y))]
    if sig0 is None:
        sig0 = (np.max(x)-np.min(x))/10.
    # if y0 is None:
    #     y0 = np.median(y)
    gmodel = Model(gauss)
    gmodel.set_param_hint('a', min=0.2, max=4)
    if not emission:
        gmodel.set_param_hint('a', min=-0.2, max=-4)
    gmodel.set_param_hint('sigma', min=50, max=1000)
    result = gmodel.fit(y, x=x, a=a0, x0=x0, sigma=sig0)

    fig, (ax0, ax1) = plt.subplots(2, 1)
    ax0.plot(x, y, 'bo')
    ax0.plot(x, result.init_fit, 'k--')
    ax0.plot(x, result.best_fit, 'r-')
    ax0.set_title(fitsfile)
    print(lbc, np.min(x))
    idx = np.where((wl > lbc*0.98) & (wl < lbc*1.03))
    ax1.plot(wl[idx], flux[idx], 'o')
    plt.show(block=False)
    cmd = phc.user_input('# Problem (y/other)? ')
    if cmd.lower().startswith('y'):
        raise ValueError
    # phc.savefig(fig, figname=fitsfile)
    return result.params['x0']
예제 #48
0
	# Find Rupture Force
	ruptureI = np.argmin(retractD)
	ruptureF = k_L*(retractD[ruptureI] - y_shift)/contactS
	ruptureL = (retractZ[ruptureI] - (retractD[ruptureI] - y_shift) - x_shift)
	
	for x in range(len(retractZ)):
		if (retractZ[x] - x_shift) < 0:
			originPt = x
			break

	# Fit WLC model to rupture
	separation = (retractZ - (retractD - y_shift) - x_shift)

	skipPLT5 = True
	gmod = Model(WLCmodel)
	gmod.set_param_hint('L_C', value = -60.0)
	gmod.set_param_hint('L_P', value = -0.38, min=-0.42, max=-0.34)
	gmod.set_param_hint('a', value = 0.0, min=-10.0, max=10.0)
	gmod.set_param_hint('b', value = 0.0, min=-10.0, max=10.0)
	params = gmod.make_params()
	try:
		result = gmod.fit(smooth25[originPt:ruptureI], x=separation[originPt:ruptureI]) # method='cobyla'
	except Exception:
		skipPLT5 = False
		sys.exc_clear()
	if skipPLT5:
		x_off = result.params['a'].value
		y_off = result.params['b'].value
		WLC_P = result.params['L_P'].value
		WLC_L0 = result.params['L_C'].value
	else:
예제 #49
0
 def _fit_aengstrom(self, Aods, lambdas):
     gmod = Model(self._aengstrom_formula, independent_vars=['x'], param_names=['beta'])
     gmod.set_param_hint('beta', value=0.1)
     result = gmod.fit(Aods, x=lambdas, verbose=False)
     return result
예제 #50
0
start_bimod_params = {'bimod_centerx': {'value':92,'min':40, 'max':200},
                      'bimod_centery':{'value':71,'min':40, 'max':200},
                      'bimod_peakg':{'value':.02,'min' : .009,'max':.05},
                      'bimod_peaktf':{'value':.15,'min' : 0,'max':.5},
                      'bimod_Rx':{'value':13 ,'min' : 9,'max':14},
                      'bimod_Ry':{'value':13,'min' : 9,'max':14},
                      'bimod_sigx':{'value':17,'min' :14,'max':24},
                      'bimod_sigy':{'value':17,'min' : 14,'max':24},
                      'bimod_off':{'value':0 ,'min' : -1,'max': 1},
                      'bimod_theta':{'value':48.5, 'min' : 48, 'max': 50}
                      }


#set parameter hings in model                      
for key, value in start_bimod_params.items():
    bimod_2d_mod.set_param_hint(key, **value)       
    
##################
#Fitting
##################
def fit_image(args, data_in, filename, filepath):
    """
    function to fit image.  For a sequential fit, proceed as follows
    1. Do full bimodal fit to determine approximate TF radius
    2. Mask TF and fit to flat Gaussian
    3. Fix flat Gaussian and re-fit TF
    
    :param args: arguments passed from command line
    :param data_in: image data
    :param filename: filename of thing being fit
    :param filepath: path to results folder