Esempio n. 1
1
    def fit(self):
        self.logger.info('Method %s' % self.config['method'])
        self.logger.info('Parameter  names %s' % self.config['params'])

        gmod = Model(self.callable, independent_vars=self.config['independent'].keys(), param_names=self.config['params'], method=self.config['method'])
        self._set_params(self.config['params'], self.config['initial_values'], self.config['limits'], gmod)
        self.config['independent']['x'] = self.param_dict['wave_range']
        helper_param = {key:val for key, val in self.config['independent'].items() if key != 'x'}
        self.logger.debug("Setting %s parameters fix" % helper_param)
        self.result = gmod.fit(self.param_dict['spectra_range'], weights=self.param_dict['weights'],  **self.config['independent'])
        self.param_dict['variables'] = dict()
        for key in self.result.params.keys():
            self.param_dict['variables'][key] = dict()
            self.param_dict['variables'][key]['stderr'] = self.result.params[key].stderr
            self.param_dict['variables'][key]['value'] = self.result.params[key].value
        return self.result, self.param_dict
Esempio n. 2
0
def get_resvar(x, y, mod='linear', zero_intercept=False):
    """
    If `mod == 'linear'` then uses linear regression; otherwise
    `mod == 'sigmoid'` and uses sigmoid regression.
    """
    assert len(x) == len(y)
    if len(x) < 3 or mod == 'linear':
        model = Model(linear)
        params = model.make_params()
        params['a'].value = (y.max() - y.min()) / (x.max() - x.min())
        params['b'].value = y.min()
        if zero_intercept:
            params['b'].value = 0
            params['b'].vary = False
        modfit = model.fit(y, x=x, params=params)
    else:
        model = Model(sigmoid)
        modfit = model.fit(y, x=x, a=y.max(), b=1 / (x.max() - x.min()),
                           c=x.max())
    finex = np.linspace(x.min(), x.max(), 50)
    result = {
        'mod': mod,
        'params': modfit.best_values,
        'resvar': modfit.residual.var(),
        'y': modfit.best_fit,
        'finex': finex,
        'finey': modfit.eval(x=finex),
        'r2': 1 - modfit.residual.var() / np.var(y),
        'modfit': modfit}
    return result
Esempio n. 3
0
    def test_extra_param_issues_warning(self):
        # The function accepts extra params, Model will warn but not raise.
        def flexible_func(x, amplitude, center, sigma, **kwargs):
            return gaussian(x, amplitude, center, sigma)

        flexible_model = Model(flexible_func)
        pars = flexible_model.make_params(**self.guess())
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            flexible_model.fit(self.data, pars, x=self.x, extra=5)
        self.assertTrue(len(w) == 1)
        self.assertTrue(issubclass(w[-1].category, UserWarning))
Esempio n. 4
0
    def test_extra_param_issues_warning(self):
        # The function accepts extra params, Model will warn but not raise.
        guess = self.guess()
        guess['extra'] = 5

        def flexible_func(x, height, center, sigma, **kwargs):
            return gaussian(x, height, center, sigma)
        
        flexible_model = Model(flexible_func, ['x'])
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            flexible_model.fit(self.data, x=self.x, **guess)
        self.assertTrue(len(w) == 1)
        self.assertTrue(issubclass(w[-1].category, UserWarning))
def FitBiExpo_ResTime(xdata,ydata,pixdim,lambda_p,isplot=False):
    # flatten the input
    xx = xdata.flatten()
    yy = ydata.flatten()

    # normalize the input
    ymax = np.amax(yy)
    nydata = yy/ymax

    fmod = Model(Func_BiExpo)

    initialp = {'a0':[0.0,1.0,0.0],'b0':[0.0,0.0,0.0],'a1':[1.0,-5.0,-10.],'b1':[1.0,1.0,0.0]}
    chisqrThresh = 0.15
    for ii in range(len(initialp['a0'])):
        result = fmod.fit(np.squeeze(nydata),x=np.squeeze(xdata),a0=initialp['a0'][ii],b0=initialp['b0'][ii],a1=initialp['a1'][ii],b1=initialp['b1'][ii])
        param = result.params.valuesdict()
        auc = param['a0']/(param['b0'] + lambda_p) + param['a1']/(param['b1'] + lambda_p)
        if result.chisqr > chisqrThresh or auc < 0.0:
            print 'fit with tnc: chisqr = {}, auc = {}'.format(result.chisqr,auc)
        else:
            print 'leastsq: good fit!'
            break
    if result.chisqr > chisqrThresh or auc < 0.0:
        for ii in range(len(initialp['a0'])):
            result = fmod.fit(np.squeeze(nydata),x=np.squeeze(xdata),a0=initialp['a0'][ii],b0=initialp['b0'][ii],a1=initialp['a1'][ii],b1=initialp['b1'][ii],method='tnc')
            if result.chisqr > chisqrThresh or auc < 0.0:
                print 'fit with tnc: chisqr = {}, auc = {}'.format(result.chisqr,auc)
            else:
                print 'tnc: good fit!'
                break

    # set up best-fit result
    param = result.params.valuesdict()
    p1 = [param['a0'],param['b0'],param['a1'],param['b1']]
    fitydata = ymax*result.eval(x=xdata)  # regularize the fitted result
    r2 = GetR2(ydata,fitydata)

    xfit = np.linspace(np.amin(xx),np.amax(xx),500)[:,np.newaxis]
    yfit = ymax*result.eval(x=xfit)

    if isplot:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot(xx,yy,'ro',label='Original data')
        ax.plot(xfit,yfit,label='Fitted data')
        plt.show()

    return p1,r2,ymax,xfit,yfit
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)
Esempio n. 7
0
def lmDDOPhaseFit(xdata, ydata, params, f0_range = 1.2, Q_range = 3):    
    f0= params[0]
    Q=1/(2*params[2])
    
    x = xdata
    y = ydata
#Define a linear model and a Damped Oscillator Model    
#    ddophase_mod = ExpressionModel('off + m*x- arctan(1/Q * 1/(f0/x - x/f0))-')
    ddophase_mod = Model(phase)
#Initial Pars for Linear Model
    pars =  ddophase_mod.make_params(off=0, m=0, f0=f0, Q=Q)

#Add fit parameters, Center, Amplitude, and Sigma
    pars['f0'].set(min=f0/f0_range, max=f0*f0_range)
    pars['Q'].set(min=Q/Q_range, max=Q*Q_range)
#Create full model. Add linear model and all peaks
#Initialize fit
    init = ddophase_mod.eval(pars, x=x)
#Do the fit. The weight exponential can weight the points porportional to the
#amplitude of y point. In this way, points on peak can be given more weight.     
    out=ddophase_mod.fit(y, pars,x=x)
#Get the fit parameters
    fittedf0= out.params['f0'].value
    fittedQ = out.params['Q'].value
#Returns the output fit as well as an array of the fit parameters
    """Returns output fit as will as list of important fitting parameters"""
    return out, [fittedf0, np.nan, np.nan, fittedQ]
def rescale_reframe(scisub, refsub, verbose=False):
    """Example function with types documented in the docstring.

        `PEP 484`_ type annotations are supported. If attribute, parameter, and
        return types are annotated according to `PEP 484`_, they do not need to be
        included in the docstring:

        Args:
            param1 (int): The first parameter.
            param2 (str): The second parameter.

        Returns:
            bool: The return value. True for success, False otherwise.

        .. _PEP 484:
            https://www.python.org/dev/peps/pep-0484/

    """
    
    params = Parameters()
    params.add('sigma', 1.0, True, 0.0, inf)
    
    image_sub         = Model(res_sigma_image_flat, independent_vars=['scisub', 'refsub'])
    image_sub_results = image_sub.fit(data=scisub.ravel(), params=params, scisub=scisub, refsub=refsub)
    
    if verbose: print('Sigma of Rescale: {}'.format(image_sub_results.params['sigma'].value))
    
    return partial_res_sigma_image(image_sub_results.params['sigma'].value)
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()
Esempio n. 11
0
    def fitGauss(self, xdata=None, ydata=None, withBG=False, inits=None):

        '''
        
        Usage: fitGauss(xdata,ydata,withBG=False,inits=None)

        xdata and ydata must have the same length, and a well defined peak

        withBG: fit gaussian with linear background

        inits: withBG == False -> [A,mu,sigma]
               withBG == True  -> [A,mu,sigma,m,b]

        '''
        def gauss(x, A, mu, sigma):
            return A*np.exp(-(x-mu)**2/(2.*sigma**2))
                    
        def gaussBG(x, A, mu, sigma, m, b):
            return b+m*x+A*np.exp(-(x-mu)**2/(2.*sigma**2))

        if withBG:
            gBGmod = Model(gaussBG)
        else:
            gmod = Model(gauss)

        if inits is None:
            mu_i = xdata[np.argmax(ydata)]
            A_i = np.max(ydata)
            sigma_i = 0.5

            if withBG:
                m_i = -0.1
                b_i = ydata[np.argmax(ydata)-50]
        else:
            A_i = inits[0]
            mu_i = inits[1]
            sigma_i = inits[2]
            if withBG:
                m_i = inits[3]
                b_i = inits[4]

        if withBG:
            results = gBGmod.fit(ydata,x=xdata,A=A_i,mu=mu_i,sigma=sigma_i,b=b_i,m=m_i)
        else:
            results = gmod.fit(ydata,x=xdata,A=A_i,mu=mu_i,sigma=sigma_i)

        return results
Esempio n. 12
0
def multi_disk_fit(arcmin_sma, intens, intens_err, bounds, psf):
    h_mat_0 = numpy.ones(len(bounds - 1))
    mod = Model(multi_disk_func)
    pars = mod.make_params(I0 = 1.0, h_mat = h_mat_0, bounds = bounds, PSF = PSF)
    pars['bounds'].vary = False
    pars['PSF'].vary = False
    results = mod.fit(intens, params = pars, x = arcmin_sma, weights = 1.0/intens_err)
    pdb.set_trace()
Esempio n. 13
0
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
Esempio n. 14
0
def fit_D_phi_powerlaw(phi_, D):
    """fit stuff"""
    Dmod = Model(Dhop_powerlaw)
    params = Dmod.make_params() 
    params['p'].set(0.3)
    result = Dmod.fit(D, params, phi=phi_)
    print result.fit_report(min_correl=0.5)
    p = result.params['p'].value
    return p
Esempio n. 15
0
 def fit(self):
     self.logger.info('Method %s' % self.config['method'])
     gmod = Model(self.callable, independent_vars=['x'], param_names=self.config['params'], method=self.config['method'])
     self._set_params(self.config['params'], self.config['initial_values'], self.config['limits'], gmod)
     self.result = gmod.fit(self.param_dict['spectra_range'], x=self.param_dict['wave_range'])
     for key in self.result.params.keys():
         self.param_dict[key] = dict()
         self.param_dict[key]['stderr'] = self.result.params[key].stderr
         self.param_dict[key]['value'] = self.result.params[key].value
     return self.result, self.param_dict
Esempio n. 16
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)
def linefit(fitam, fitcps, fitweights):
    try:
        model = Model(linefunc)
        params = Parameters()
        params.add('linem', value=5)
        params.add('linek', value=0.25, min=0, max=1)
        fit = model.fit(asarray(fitcps), params, fitweights, linex=asarray(fitam))
        fitvalues = fit.best_values
        fiterr = fit.covar
    except RuntimeError:
        return float('nan'), float('nan')
    return [[fitvalues['linem'], fitvalues['linek']], fiterr]
Esempio n. 18
0
def Gaussian_Fit(xData,yData, cen, ifPlot=False):

    gmod = Model(gaussian)
    result = gmod.fit(yData, x=xData, amp=5, cen=cen, wid=1)
    
    if ifPlot == True : 
        plt.plot(xData, yData,         'bo')
        plt.plot(xData, result.best_fit, 'r-')
        plt.ylim(min(min(result.best_fit),min(xData)),max(max(result.best_fit),max(yData)))
        plt.show()
    
    return result.best_values, result.best_fit
Esempio n. 19
0
def fit_D_phi(phi_, D, c1, c2):
    """fit stuff"""
    Dmod = Model(Dhop)
    params = Dmod.make_params()
    params['c1'].set(c1, vary=False)
    params['c2'].set(c2, vary=False)
    params['c3'].set(1, vary=False)
    params['beta'].set(0.70, min=0)
    result = Dmod.fit(D, params, phi=phi_)
    print result.fit_report(min_correl=0.5)
    beta = result.params['beta'].value
    c3 = result.params['c3'].value
    return c3, beta
Esempio n. 20
0
def fit_D_phi_alt(phi_, D, c1, c2, c1o, c2o):
    """fit stuff"""
    Dmod = Model(Dhopalt)
    params = Dmod.make_params()
    params['c1'].set(c1, vary=False)
    params['c2'].set(c2, vary=False)
    params['c1o'].set(c1o, vary=False)
    params['c2o'].set(c2o, vary=False)
    params['c3'].set(0.25)
    result = Dmod.fit(D, params, phi=phi_)
    print result.fit_report(min_correl=0.5)
    c3 = result.params['c3'].value
    return c3
Esempio n. 21
0
def fit_Dinf_model(Input, diffusion, startindex, endindex):
    time_ = diffusion[startindex:endindex,0]
    D = diffusion[startindex:endindex,1]
    Dmod = Model(Dinf_model)
    params = Dmod.make_params()
    params['var1'].set(0.1) 
    params['Dinf'].set(0.0001)#, min = 0, max=0.1)
    result = Dmod.fit(D, params, time=time_)
    print result.fit_report(min_correl=0.5)
    var1 = result.params['var1'].value
    Dinf = result.params['Dinf'].value
    Dinfstderr = result.params['Dinf'].stderr
    return var1, Dinf, Dinfstderr
Esempio n. 22
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
Esempio n. 23
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()
Esempio n. 24
0
def fit_data(x, y, peak_pos, peak_type='LO', width=None):
    """ 
    Builds a lmfit model of peaks in listed by index in `peak_pos` with lineshape
    given by `peak_type`. 

    Parameters
    ----------
    x : array
    y : array
    peak_pos : array of peak postion indices
    
    peak_type : string
        Peaks can be of the following types:
        - 'LO' : symmetric lorentzian (default)
        - 'GA' : symmetric gaussain
        - 'VO' : symmetric pseudo voigt

    width : float
    amp : float

    Returns
    -------
    out : Fitted lmfit model

    """
    
    # get appropriate line shape function

    if peak_type == 'LO':
        peak_function = lorentzian
    elif peak_type == 'GA':
        peak_function = gaussian
    elif peak_type == 'VO':
        peak_function = voigt

    # build model
    model = Model(peak_function, prefix='p0_')
    for i, p in enumerate(peak_pos[1:]):
        model += Model(peak_function, prefix='p%s_' % str(i+1))

    pars = model.make_params()

    # initialize params
    for i, p in enumerate(peak_pos):
        pars['p%s_x0' % i].set(x[p])
        pars['p%s_fwhm' % i].set(width, min=0)
        pars['p%s_amp' % i].set(y[p], min=0)

    out = model.fit(y, pars, x=x)
    return out
Esempio n. 25
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()
Esempio n. 26
0
def get_resvar(x, y, predict=False, fine_predict=False):

    def sigmoid(x, a, b, c):
        return a / (1 + np.exp(b * (c - x)))

    def linear(x, a, b):
        return a * x + b
    assert len(x) == len(y)
    if len(x) < 3:
        sigmoidmod = Model(linear)
        modfit = sigmoidmod.fit(y, x=x,
                                a=(y.max() - y.min()) / (x.max() - x.min()),
                                b=y.min())
    else:
        sigmoidmod = Model(sigmoid)
        modfit = sigmoidmod.fit(y, x=x, a=y.max(), b=1 / (x.max() - x.min()),
                                c=x.max())
    if not predict:
        return modfit.residual.var()
    elif not fine_predict:
        return (modfit.residual.var(), modfit.best_fit)
    else:
        finex = np.linspace(x.min(), x.max(), 50)
        return (modfit.residual.var(), modfit.eval(x=finex), finex)
Esempio n. 27
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
Esempio n. 28
0
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
Esempio n. 29
0
def prueba():
    y2 = [49.0, 49.0, 101.0, 67.0, 65.0, 52.0, 49.0, 49.0, 54.0, 49.0, 48.0, 49.0]
    y = [i-50 for i in y2]
    x = linspace(0.0, 11.0, num=12) #variable ind para evento
    x2 = linspace(0.0, 11.0, num=400)   #variable ind para fit
    gmod = Model(gaussian)  #Generar un Modelo Gaussiano
    
    peak = max(y)
    result = gmod.fit(y, x=x, amp=peak, cen=5, wid=1)
    final = gmod.eval(x=x2, amp=result.best_values['amp'], cen=result.best_values['cen'], wid=result.best_values['wid'])
    
    plt.plot(x, y,         'bo')
    plt.plot(x, result.init_fit, 'k--')
    #plt.plot(x, result.best_fit, 'r-')
    plt.plot(x2, final, 'r-')
    plt.show()
Esempio n. 30
0
    def test_params_usersyms(self):
        # test passing usersymes to Parameters()
        def myfun(x):
            return x**3

        params = Parameters(usersyms={"myfun": myfun})
        params.add("a", value=2.3)
        params.add("b", expr="myfun(a)")

        xx = np.linspace(0, 1, 10)
        yy = 3 * xx + np.random.normal(scale=0.002, size=len(xx))

        model = Model(lambda x, a: a * x)
        result = model.fit(yy, params=params, x=xx)
        assert_(np.isclose(result.params['a'].value, 3.0, rtol=0.025))
        assert_(result.nfev > 3)
        assert_(result.nfev < 300)
k = np.zeros((num_angle_out,1))
patches = []
fig = plt.figure(figsize=(6,4))
for i in range(0,num_angle_out):
    print(i)
#    plt.cla();
    patches.append(mpatches.Patch(color=cmap(i),label='',alpha = 0.3))
    name = savepath+savename+"_%04d"%i+".pdf"
    r = np.flip(countdata[:,i],0)
    gmodel = Model(vonmises)
    x = np.array(np.arange(0,360,360/num_angle_in))
    params = Parameters()
    params.add('amp',r.max(0))
    params.add('cen',i*angular_resolution)
    params.add('kappa',np.pi/4.0)    
    result = gmodel.fit(r,params,x=x)
    a[i] = result.params['amp'].value
    c[i] = result.params['cen'].value
    k[i] = result.params['kappa'].value
#    print(c[i])  
    plotdata = vonmises(np.arange(0,360,angular_resolution),a[i],c[i],k[i])
        
    plt.plot(np.arange(0,360,angular_resolution),plotdata,color = cmap(i),label='Fitted data',alpha = 0.5)
    plt.plot(np.arange(0,360,360/num_angle_in),r,'+',color=cmap(i),alpha=0.5,label='Observation')
    plt.legend(fontsize=7,loc = 1)
    plt.xlim((0,360))
    plt.ylim((0,14000))
    plt.legend(handles = patches,bbox_to_anchor=(1.08,1),labelspacing = 0.01,fontsize = 5,frameon=False)
#    s = r'$ %03d^{\circ}$'%(angular_resolution*i)
    plt.title("Imaging response curve of anglular slice")
    plt.xlabel("Imaging angle")
Esempio n. 32
0
def fit_peak(bincenters, y, params, weights, input):

    x = bincenters
    # sets region to fit. Determined by taking the peak location 'params[1]' and adding and subtracting the two times the width of the peak 'params[2]' and removing data outside that interval.
    cut1 = int(params[1] + 2 * params[2])
    cut2 = int(params[1] - 2 * params[2])
    cut = (x > cut2) & (x < cut1)

    #Models to fit peaks with. Each peak is approxiamtly gaussian, but as the amount of data decreases with increasing energy, a line is also used to account for the slope of the spectrum.

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

    def line(x, slope, intercept):
        """a line"""
        return slope * x + intercept

    mod = Model(gaussian) + Model(line)

    #takes parameters and uses them as initial guesses for the fit.
    pars = mod.make_params(amp=params[0],
                           cen=params[1],
                           wid=params[2],
                           slope=params[3],
                           intercept=params[4])

    #fits each peak in a region defined by lines 18-20.
    fit = mod.fit(y[cut], pars, weights=weights[cut], x=x[cut])

    #plots the fit for each peak.

    plt.figure(figsize=(9.0, 8.0))
    plt.plot(x, y, label="Peak")
    plt.plot(x[cut], fit.best_fit, 'r--', label='Best Fit')
    plt.yscale('log')

    #As this is used before and after the calibration, the plot label will depend on the input to this function
    if input == 'ADC':
        label = 'ADC Channel'
        lower = params[1] - 2 * params[2]
        upper = 2 * params[2] + params[1]
    else:
        label = 'Energy [keV]'
        lower = params[1] - 2 * params[2]
        upper = 2 * params[2] + params[1]

    plt.xlim(lower, upper)
    plt.xlabel(label)
    plt.ylabel("Count ")
    plt.grid(linestyle='-', linewidth=0.35)
    plt.legend()

    plt.show()

    parameters = []
    for key in fit.params:
        #print(key, "=", "{0:.2f}".format(fit.params[key].value), "+/-", "{0:.2f}".format(fit.params[key].stderr))
        parameters.append(fit.params[key].value)

    print(fit.fit_report())

    return parameters, fit
Esempio n. 33
0
n_points = len(x)
gaussian_true = gaussian(x, 2.33, 0.21, 1.51)
gmod = Model(gaussian)

print '--Single fit'
start = timer()
best_vals, covar = curve_fit(gaussian,
                             x,
                             gaussian_true + random.normal(0, 0.2, n_points),
                             p0=init_vals)
end = timer()
print 'curve fit', best_vals, ' time ', (end - start)
start = timer()
result = gmod.fit(gaussian_true + random.normal(0, 0.2, n_points),
                  x=x,
                  amp=1,
                  cen=0,
                  wid=1)
end = timer()
print 'lmfit', array(result.params.valuesdict().values()), (end - start)

print '--Bootstrap'
results_matrix_curvefit = empty([3, 1000])
results_matrix_lmfit = empty([3, 1000])
start = timer()
for i in range(1000):
    best_vals, covar = curve_fit(gaussian,
                                 x,
                                 gaussian_true +
                                 random.normal(0, 0.2, n_points),
                                 p0=init_vals)
Esempio n. 34
0
    def fit_pos(self, Rm, Xm, zf_real, zf_imag, name):
        # global fi_Rf, fi_Xf
        # Zmes = mirror_array(Xm, Rm)
        # Rm2 = mirror_array(Rm, Rm)
        # Xm2 = mirror_array(Xm, Xm)
        # zf_impd = mirror_array(zf_imag, zf_real)
        # freq = mirror_array(-1*self.freq_array, self.freq_array)
        # Chl = self.c_hl
        # C1 = self.c_ph
        # C2 = self.c_pl
        # Cx = self.c_x
        # Cg = self.c_g
        # w = freq*2*np.pi
        # step = np.heaviside(w,0.0)    
      
        # def fit(a, b, Chl, Cg, Rm, Xm, w, step, fi_Rf, fi_Xf):
        #     w= ((step-1)*w + step*w)
        #     I = 1j
        #     a = b*a
        #     if a < 0.5:
        #         a = 0.5
        #     Zm = Rm + I*Xm
        #     Zx = 1/(Cx*w*I + 1/self.total_r)
        #     ### considering calibration error
        #     Zm = Zm/(1-Zm*Chl*w*I)

        #     if fi_Rf == None and fi_Xf == None:
        #         Zf = 0.25*self.total_r**2/(Zm - self.total_r)
        #         fi_Rf = Zf.real
        #         fi_Xf = Zf.imag
        #     # ## use rf xf as initial value to find the root of nonlineal systems of equations

        #     Z1 = Zx*a
        #     Z2 = Zx*(b-a)
        #     Z3 = Zx*(1-b)
        #     Zf = (fi_Rf, fi_Xf)
        #     Zm = (Zm.real, Zm.imag)
        #     Zg = 1/(Cg*w*I)
        #     def func(Z_f, Zm, Z1, Z2, Z3, Zg):
        #         Rf, Xf = Z_f[:92], Z_f[92:]
        #         Rm, Xm = Zm[0], Zm[1]
        #         I = 1j
        #         Zf = Rf + Xf*I
        #         Zf2 = Zf*Z2/(Z2+2*Zf) 
        #         Zff = Zf*Zf/(Z2+2*Zf)
        #         Zh = Z1+Zf2
        #         Zl = Z3+Zf2
        #         Zs = Zff*Zg/(Zff+Zg)
        #         Zm = Zh + Zl + Zh*Zl/Zs
        #         return np.array([Zm.real -Rm, Zm.imag -Xm]).ravel()
                
        #     Zf, flag = leastsq(func, Zf, args = (Zm, Z1, Z2, Z3, Zg), xtol = 0.1)
        #     fi_Rf = Zf[:92]
        #     fi_Xf = Zf[92:]

        #     return (1-step)*fi_Xf + step*fi_Rf

        ########## New try
        I = 1j
        Z_fit = mirror_array(np.sqrt(Xm**2 + Rm**2),np.sqrt(Xm**2 + Rm**2))
        # Z_fit = mirror_array(Xm,Rm)
        zf_imag = mirror_array(zf_imag, zf_imag)
        zf_real = mirror_array(zf_real, zf_real)
        # zf_impd = zf_real + I*zf_imag 
        freq = mirror_array(-1*self.freq_array,self.freq_array)
        Chl = self.c_hl
        C1 = self.c_ph
        C2 = self.c_pl
        Cx = self.c_x
        Cg = self.c_g
        w = freq*2*np.pi 
        step = np.heaviside(w,0.0)  

        def fit(a, b, Chl, Cg, Cx, w, step, zf_imag, zf_real,Zf):
            w= ((step-1)*w + step*w)
            I = 1j
            a = a*b
            if a < 0.5:
                a = 0.5
            Zx = self.total_r/(1+self.total_r*Cx*w*I)
            ### considering calibration error
            
            Z1 = Zx*a
            Z2 = Zx*(b-a)
            Z3 = Zx*(1-b)
            Zg = 1/(Cg*w*I)
            Zf = zf_real + I*zf_imag
            Zf2 = Zf*Z2/(Z2+2*Zf) 
            Zff = Zf*Zf/(Z2+2*Zf)
            Zh = Z1+Zf2
            Zl = Z3+Zf2
            Zs = Zff*Zg/(Zff+Zg)
            Zm = Zh + Zl + Zh*Zl/Zs
            Zm = Zm/(Chl*w*I*Zm + 1)
            z = np.sqrt(Zm.imag**2 + Zm.real**2)
            # print((1-step)*Zm.imag + step*Zm.real - Zf)
            # return (1-step)*Zm.imag + step*Zm.real
            return z

        params = Parameters()
        params.add('Cx', value = Cx, vary = False)
        # params.add('Cx', value = Cx, min = 0.1*Cx, max = 10*Cx, brute_step = 0.5*Cx)
        params.add('Chl', value = Chl, vary = False)
        # params.add('Cg', value = Cg, vary = False)
        params.add('Cg', value = Cg, min = 0.5*Cg, max = 2*Cg, brute_step = 0.5*Cg)
        # params.add('Chl', value = Chl, min = 0.5*Chl, max = 2*Chl, brute_step = 0.5*Chl)
        params.add('a', value = 0.6, min = 0.6 , max = 0.85, brute_step = 0.01)
        # params.add('a', value = 0.7, vary = False)
        # params.add('b', value = 0.7, vary = False)
        params.add('b', value = 0.6, min = 0.6, max = 0.90, brute_step = 0.01)
        model = Model(fit, independent_vars = ['w', 'step', 'zf_imag', 'zf_real', 'Zf'])
        result = model.fit(Z_fit, params, method = 'differential_evolution',  w = w, step = step, zf_imag = zf_imag, zf_real = zf_real, Zf =Z_fit )
        # print("results from {}".format(name))
        if result.best_values['b']*result.best_values['a'] < 0.5:
            print("0.5")
        else:
            print(result.best_values['b']*result.best_values['a']) 
        print(result.best_values['b'])
        # print(result.fit_report())
        plt.plot(freq, Z_fit,'-')
        plt.plot(freq, result.best_fit,'-')
Esempio n. 35
0
def fit(data: str,
        data_skip: int,
        kappa_function: callable,
        m_min=0.1 * m_sun,
        m_max=30 * m_sun,
        m_init=7 * m_sun,
        vsc_min=0.01e9,
        vsc_max=2e9,
        vsc_init=1e9,
        mni_min=0.001 * m_sun,
        mni_max=1.5 * m_sun,
        mni_init=0.1 * m_sun):

    # ============== EDIT THESE ============== #
    # data = "data/2009jf_bolom.txt"
    # data_skip = 4
    # below should be an instance of a function that takes 1 arg: time, t.
    # kappa_function = kappa_const
    # ======================================== #

    # load data file
    phase, log_lum, uncert = loadtxt(data, unpack=True)

    time = array([(t + abs(phase[0])) * day2sec for t in phase])[data_skip:]
    log_lum = log_lum[data_skip:]

    lc_model = Model(_lc_maker, kappa_func=kappa_function)
    print('parameter names: {}'.format(lc_model.param_names))
    print('independent variables: {}'.format(lc_model.independent_vars))

    params = Parameters()
    # ============== EDIT THESE? ============= #
    params.add(name="m", value=m_init, min=m_min, max=m_max)
    params.add(name="v_sc", value=vsc_init, min=vsc_min, max=vsc_max)
    params.add(name="mni_minus_m", value=1, vary=True, min=1e-12)
    # params.add(name="m_ni", value=mni_init, min=mni_min, max=mni_max)
    # params.add(name="m_ni", expr='0.5*m + mni_minus_m')  # max value of m_ni is 0.5*m
    params.add(name="mni_factor", min=0.0001, max=0.5)
    params.add(name="m_ni", expr='m * mni_factor')
    params.add(name="shift", value=500, min=1, max=15 * day2sec)
    # ======================================== #

    # Do a fit:
    print(f"\033[94mFitting {data}....\033[0m")
    try:
        result = lc_model.fit(log_lum, params, times=time)
    except Exception as e:
        raise OverflowError(
            f"Something went wrong during fitting, perhaps parameters went mad, try different initial "
            f"values?\n\033[91m{str(e)}\033[0m")
        # print("Showing plot of the data and initial params")
        # print(type(time), type(log_lum))
        # plt.scatter(time/day2sec, log_lum, 'bo', label="data")
        # plt.plot(time/day2sec, _lc_maker(time, params["m"].value, params["v_sc"].value, params["m_ni"].value, 500),
        # 'k--', label="init params")
        # plt.legend()
        # plt.show()
    else:  # it didn't crash
        print("Done!")
        # print(result.fit_report())  # dumps a load of fitting data

        # generate a smoother plot than result.best_fit provides:
        # extract fit results
        m_result = result.params['m'].value
        v_sc_result = result.params['v_sc'].value
        m_ni_result = result.params['m_ni'].value
        shift_result = result.params["shift"].value
        chi2_result = result.chisqr
        red_chi2_result = result.redchi

        smooth_times = linspace(
            time[0], time[-1],
            len(time) * 3)  # 3 times as many time points, and evenly spaced
        init_result = _lc_maker(  # smooth curve of initial guess
            smooth_times,
            result.init_values["m"],
            result.init_values["v_sc"],
            result.init_values["m_ni"],
            result.init_values["shift"],
            kappa_func=kappa_function)
        best_result = _lc_maker(  # smooth curve of best guess
            smooth_times,
            m_result,
            v_sc_result,
            m_ni_result,
            shift_result,
            kappa_func=kappa_function)

        fig, ax = plt.subplots(1, 1)
        ax.plot(
            time / day2sec,
            log_lum,
            'bo',
            label=f"Data for {data.split('/')[1].split('.')[0].split('_')[0]}")
        # ax.plot(smooth_times / day2sec, init_result, 'k--', label="Initial fit")
        ax.plot(smooth_times / day2sec, best_result, 'r-', label="Best fit")
        ax.set_xlabel("time (days)")
        ax.set_ylabel(r"log$_{10}$ luminosity")
        ax.legend()
        formatter = ScalarFormatter()
        formatter.set_scientific(False)
        ax.xaxis.set_major_formatter(formatter)
        result_string = (
            f"kappa function: {kappa_function.__name__}\n"
            f"Data skip: {data_skip}\n"
            f"M = {m_result / m_sun:.5f} solar masses\n"
            f"v_sc = {v_sc_result / 1e9:.5f} *10^9 cm/s\n"
            f"M_Ni = {m_ni_result / m_sun:.5f} solar masses\n"
            f"shift = {shift_result/day2sec:.5f} days\n"
            f"chi squared = {chi2_result}\n"
            f"reduced chi = {red_chi2_result}\n"
            f"sqrt(2.53M_Ni/M) = {sqrt(2.53 * m_ni_result / m_result)}")
        print(result_string)

        print("Writing results to disk...")
        try:
            with open(
                    f"fits/{data.split('/')[1].split('.')[0]}_{kappa_function.__name__}.txt",
                    'w') as f:
                f.write(result_string)
            fig.savefig(
                f"fits\\{data.split('/')[1].split('.')[0]}_{kappa_function.__name__}.png"
            )
        except Exception as e:
            print("Something went wrong when saving!", str(e))
        else:
            print("Done!")  # Showing plot.")
Esempio n. 36
0
x = data[:, 7]
y = data[:, 8]
# tet_f, k_p, T10, b

result_sec_or = []
result_first_or = []
result_bi_exp = []
result_dl_fl_sing_exp = []

result_ph_sing_exp = []
result_ph_bi_exp = []

for i in range(7):
    second_or_model = Model(second_order_p_type)
    result_sec_or.append(
        second_or_model.fit(dl_fl_y_norm[i], t=x, a=10, T10=1, b=10))
    #  print(result_sec_or[i].fit_report())

    first_or_model = Model(first_order_p_type)
    result_first_or.append(
        first_or_model.fit(dl_fl_y_norm[i], t=x, a=10, T10=1, b=10))
    # print(result_first_or[i].fit_report())

    bi_exp_model = Model(exponential)
    result_bi_exp.append(
        bi_exp_model.fit(dl_fl_y_norm[i], t=x, a=10, b=10, tau1=1,
                         tau2=10))  # рассчёт трёх видов фиттинга

    sing_exp_model = Model(single_exponential)
    result_dl_fl_sing_exp.append(
        sing_exp_model.fit(dl_fl_y_norm[i], t=x, a=10, tau1=1))
Esempio n. 37
0
def fit_Ecal_dips_symmetric(xdata,
                            ydata,
                            guessed_sigma=.01,
                            wguess=66.,
                            D="Si",
                            theta_offset=-35.26):
    '''
        This fits for two symmetric peaks.

        Parameters
        ----------
        xdata : the xdata (usually theta)
        ydata : the counts measured
        guessed_sigma: float, optional
            the guessed sigma of the peaks (supplied to initial guess)
        wguess :
                the guessed wavelength
        D : str
            The str identifier for the reference sample (usually just "Si")
        theta_offset :
            The offset in theta of the theta motor
    '''
    # this is a cludge used to pass result around.
    # TODO : remove this when we finally agree on a method on how to scan...
    if isinstance(D, str):
        D = D_SPACINGS[D]

    guessed_wavelength = wguess

    # theta-tth factor
    factor = 1
    offset = theta_offset  # degrees

    # TODO : Make a Model
    #def dips(x, c0, wavelength, a1, a2, sigma):
    def dips(x, c0, wavelength, a1, sigma):
        sign = -1
        # x comes from hardware in [theta or two-theta] degrees
        x = np.deg2rad(x / factor - offset)  # radians
        assert np.all(wavelength < 2 * D), \
            "wavelength would result in illegal arg to arcsin"
        centers = np.arcsin(wavelength / (2 * D))
        # just look at one center for now
        c1 = centers[0]

        result = (
            voigt(x=x, amplitude=sign * a1, center=c0 - c1, sigma=sigma) +
            voigt(x=x, amplitude=sign * a1, center=c0 + c1, sigma=sigma))

        return result

    model = Model(dips) + LinearModel()

    guessed_average = np.max(ydata)
    guessed_amplitude = np.abs(np.min(ydata) - np.mean(ydata))
    # Fill out initial guess.
    init_guess = {
        'intercept':
        Parameter(
            'intercept',
            value=guessed_average,
        ),
        'slope':
        Parameter('slope', value=0, min=-100, max=100, vary=False),
        'sigma':
        Parameter('sigma', value=np.deg2rad(guessed_sigma)),
        'c0':
        Parameter('c0', value=np.deg2rad(0)),
        'wavelength':
        Parameter('wavelength',
                  guessed_wavelength,
                  min=0.8 * guessed_wavelength,
                  max=1.2 * guessed_wavelength),
        'a1':
        Parameter('a1', guessed_amplitude, min=0),
    }
    params = Parameters(init_guess)

    # fit_kwargs = dict(ftol=1)
    fit_kwargs = dict()
    result = model.fit(ydata, x=xdata, params=params, fit_kwargs=fit_kwargs)
    print('WAVELENGTH: {} [Angstroms]'.format(result.values['wavelength']))
    print('CENTER is : {} [deg]'.format(result.values['c0']))

    plt.figure(2)
    plt.clf()
    plt.plot(xdata, ydata, linewidth=0, marker='o', color='b', label="data")
    plt.plot(xdata, result.best_fit, color='r', label="best fit")
    plt.legend()
    return result
Esempio n. 38
0
donor_list = data.index.get_level_values(0).unique()

i = 1  # counter for the subplot id

for donor in donor_list:
    plt.subplot(math.ceil(len(donor_list) / 5), 5, i)  # 5 graphs on each row

    # each donor may have a different set of antibodies tested
    antibody_list = data.loc[donor].index.get_level_values(0).unique()

    for antibody in antibody_list:
        idx = (donor, antibody)

        # model fitting for each antibody
        results = gmodel.fit(pivot.loc[idx][('mean', 'percent_spe_release')],
                             x=pivot.loc[idx].index,
                             params=fit_params)

        # plotting
        ax = results.plot_fit(numpoints=50,
                              fit_kws={
                                  'lw': 2,
                                  'c': color_antibody[antibody]
                              },
                              datafmt='none')

        ax.plot(data.loc[idx].index,
                data.loc[idx]['percent_spe_release'],
                label=antibody,
                marker='+',
                mec=color_antibody[antibody],
Esempio n. 39
0
def simple_echo_fits():
    """
    Takes the highest point of each echo and fits the echo_as_T2 function to those points.
    """
    xrs, yrs, xr, yr, filename, dat1 = range_to_list()
    length = len(yrs)
    max_y = [np.max(yrs[i]) for i in range(length)]
    max_y_loc = [np.where(yrs[i] == max_y[i])[0][0] for i in range(length)]
    cents = [xrs[i][max_y_loc[i]] for i in range(length)]
    heights = max_y
    # TODO: Find a better value for the uncertainty on y-values.
    heights = np.array(heights)
    cents = np.array(cents)
    maxy = np.max(heights)
    miny = np.min(heights)
    decay_pos = np.where(heights == find_nearest(heights, maxy / e))[0][0]
    decay_pos_time = cents[decay_pos]
    avg_y_sep = abs(np.mean(np.diff(heights)))
    efit = Model(echo_as_T2)
    param = efit.make_params()
    param.add('M0', value=maxy, min=maxy * 0.8, max=maxy + (avg_y_sep * 3))
    param.add('T2',
              value=decay_pos_time,
              min=decay_pos_time * 0.1,
              max=decay_pos_time * 1.5)
    param.add('c', value=miny * 0.3, min=miny * 0.1, max=miny * 1.2)
    param.add('ph', value=cents[0] * 0.5, min=0, max=cents[0] * 1)
    result_2 = efit.fit(heights,
                        param,
                        t=cents,
                        method='leastsq',
                        weights=np.mean(np.diff(dat1['m'])) / heights)
    print(result_2.fit_report())
    print('\n', result_2.params.pretty_print())
    ax = plt.gca()
    ax.set_xlabel('Time (s)', fontsize=14)
    ax.set_ylabel('Magnetization (A/m)', fontsize=14)
    xes = np.linspace(np.min(cents), np.max(cents), 100)
    y = efit.eval(t=xes, params=result_2.params)
    plt.plot(xes, y, antialiased=True)
    plt.plot(cents, heights, 'x', ms=8, color='k')
    plt.plot(dat1['t'],
             dat1['m'],
             lw=2,
             antialiased=True,
             color='#4a4a4a',
             zorder=1)
    plt.title(filename)
    plt.xlim(left=0, right=np.max(cents) * 1.1)
    # plt.ylim(bottom=0, top=result_2.params['M0'].value * 1.1)
    plt.axhline(result_2.params['M0'].value,
                color='k',
                ls='--',
                alpha=0.7,
                lw=1,
                zorder=2)
    plt.axhline(result_2.params['M0'].value / e,
                color='k',
                ls='--',
                alpha=0.7,
                lw=1,
                zorder=2)
    plt.text(0.9,
             0.9,
             "T_1: {:.4f} s".format(result_2.params['T2'].value),
             horizontalalignment='center',
             verticalalignment="center",
             transform=ax.transAxes,
             bbox={
                 'pad': 8,
                 'fc': 'w'
             },
             fontsize=14)
    plt.tight_layout()
    plt.tick_params(axis='both', which='major', labelsize=13)
    fig_manager = plt.get_current_fig_manager()
    fig_manager.window.showMaximized()
    plt.show()
Esempio n. 40
0
def echo_fits():
    """
    Fits a Gaussian with a linear background to each of the echo peaks, finds the centroid and top of
    the Gaussian, then fits the echo_as_T2 function to the points given by x=centroid, y=top.
    """
    xrs, yrs, xr, yr, filename, dat1 = range_to_list()
    cents: List[float] = []
    cents_uncert: List[float] = []
    heights: List[float] = []
    heights_uncert: List[float] = []
    for i in range(0, len(xrs)):
        mdl = GaussianModel(prefix='G_')
        lne = LinearModel(prefix='L_')
        params = mdl.guess(yrs[i], x=xrs[i])
        params += lne.guess(yrs[i], x=xrs[i])
        max_y = np.max(yrs[i])
        min_y = np.min(yrs[i])
        max_x = np.max(yrs[i])
        min_x = np.min(yrs[i])
        predicted_slope = (max_y - min_y) / (max_x - min_x)
        params.add('L_slope',
                   value=predicted_slope,
                   min=predicted_slope * 1.1,
                   max=predicted_slope * 0.9)
        params.add('L_intercept',
                   value=min_y,
                   min=min_y * 0.9,
                   max=min_y * 1.1)
        params.add('G_height',
                   value=max_y - min_y,
                   min=(max_y - min_y) * 0.99,
                   max=(max_y - min_y) * 1.05)
        model = mdl + lne
        result = model.fit(yrs[i], params, x=xrs[i], method='leastsq')
        cent: float = result.params['G_center'].value
        amp: float = result.params['G_height'].value
        inter: float = result.params['L_intercept'].value
        grad: float = result.params['L_slope'].value
        height: float = amp + ((cent * grad) + inter)
        heights.append(height)
        cents.append(cent)
        cents_uncert.append(result.params['G_center'].stderr)
        partial_amp = 1
        partial_grad = cent
        partial_x = grad
        partial_inter = 1
        amp_term = partial_amp * result.params['G_height'].stderr
        grad_term = partial_grad * result.params['L_slope'].stderr
        x_term = partial_x * np.mean(np.diff(xrs[i]))
        inter_term = partial_inter * result.params['L_intercept'].stderr
        height_uncert = np.sqrt(amp_term**2 + grad_term**2 + x_term**2 +
                                inter_term**2)
        heights_uncert.append(height_uncert)
    heights = np.array(heights)
    cents = np.array(cents)
    maxy = np.max(heights)
    miny = np.min(heights)
    decay_pos = np.where(heights == find_nearest(heights, maxy / e))[0][0]
    decay_pos_time = cents[decay_pos]
    avg_y_sep = abs(np.mean(np.diff(heights)))
    efit = Model(echo_as_T2)
    param = efit.make_params()
    param.add('M0', value=maxy, min=maxy * 0.8, max=maxy + (avg_y_sep * 2))
    param.add('T2',
              value=decay_pos_time,
              min=decay_pos_time * 0.1,
              max=decay_pos_time * 1.5)
    param.add('c', value=miny * 0.3, min=miny * 0.1, max=miny * 1)
    param.add('ph', value=cents[0] * 0.1, min=0, max=cents[0] * 1)
    result_2 = efit.fit(
        heights,
        param,
        t=cents,
        method='leastsq',
        weights=np.sqrt(
            np.mean(np.diff(dat1['m']))**2 + np.array(heights_uncert)**2) /
        heights)
    print(result_2.fit_report())
    print('\n', result_2.params.pretty_print(fmt='e', precision=2))
    ax = plt.gca()
    ax.set_xlabel('Time (s)', fontsize=14)
    ax.set_ylabel('Magnetization (A/m)', fontsize=14)
    xes = np.linspace(np.min(cents), np.max(cents), 100)
    y = efit.eval(t=xes, params=result_2.params)
    plt.plot(xes, y, antialiased=True)
    plt.plot(cents, heights, 'x', ms=8, color='k')
    plt.plot(dat1['t'],
             dat1['m'],
             lw=2,
             antialiased=True,
             color='#4a4a4a',
             zorder=1)
    plt.title(filename)
    plt.xlim(left=0, right=np.max(cents) * 1.1)
    plt.ylim(bottom=0, top=result_2.params['M0'].value * 1.3)
    plt.axhline(result_2.params['M0'].value,
                color='k',
                ls='--',
                alpha=0.7,
                lw=1,
                zorder=2)
    plt.axhline(result_2.params['M0'].value / e,
                color='k',
                ls='--',
                alpha=0.7,
                lw=1,
                zorder=2)
    plt.text(0.9,
             0.9,
             "T_1: {:.4f} s".format(result_2.params['T2'].value),
             horizontalalignment='center',
             verticalalignment="center",
             transform=ax.transAxes,
             bbox={
                 'pad': 8,
                 'fc': 'w'
             },
             fontsize=14)
    plt.tight_layout()
    plt.tick_params(axis='both', which='major', labelsize=13)
    fig_manager = plt.get_current_fig_manager()
    fig_manager.window.showMaximized()
    plt.show()
Esempio n. 41
0
def plotfile():
    #------------------------------------------------------------------------------------------------------------------
    # This function plots magnetization and spin current data
    #------------------------------------------------------------------------------------------------------------------

    #------------------------------------------------------------------------------------------------------------------
    # parameters
    #------------------------------------------------------------------------------------------------------------------
    LZ = 256  #length of the system
    alpha = 0.05  # damping constant
    omega = 0.5  # excitation frequency
    output_number = 10  # number of timesteps where output was created
    timestep = 100000  # number of steps between outputs

    #------------------------------------------------------------------------------------------------------------------
    # reading the input files of the simulation: Spin_STM-File, NN-table, DMI-vector
    #------------------------------------------------------------------------------------------------------------------
    job = "mag_0.500000.dat"
    infile = open(job, "r")

    #------------------------------------------------------------------------------------------------------------------------------
    # defining output arrays
    #--------------------------------------------------------------------------------------------------------------------------
    magx_array = []
    magy_array = []
    magz_array = []
    pos_array = []

    #--------------------------------------------------------------------------------------------------------------------------------
    # reading and extracting the input file
    #-----------------------------------------------------------------------------------------------------------------------------------
    for index, line in enumerate(infile):
        if (index > LZ * (output_number - 1)):
            current_line = line.split()
            pos_array.append(int(current_line[0]))
            magx_array.append(float(current_line[1]))
            magy_array.append(float(current_line[2]))
            magz_array.append(float(current_line[3]))
    #--------------------------------------------------------------------------------------------------------------------------------
    # fit of the data
    #-----------------------------------------------------------------------------------------------------------------------------------
    def wave(x, a, b, c, d):
        return a * np.cos(b * x + c) * np.exp(-x / d)

    gmodel = Model(wave)
    result = gmodel.fit(magy_array, x=pos_array, a=0.1, b=0.5, c=3.0, d=10)
    params = gmodel.make_params()
    print(result.fit_report())

    result2 = gmodel.fit(magz_array, x=pos_array, a=0.1, b=0.5, c=3.0, d=10)
    params2 = gmodel.make_params()
    print(result2.fit_report())

    #---------------------------------------------------------------------------------------------------------------------------
    #creating the plots
    #--------------------------------------------------------------------------------------------------------------------------
    pp = PdfPages('mag_0.5.pdf')
    plt.plot(pos_array, magy_array, label='m_y')
    plt.plot(pos_array, magz_array, label='m_z')
    plt.plot(pos_array, result.best_fit, label='Fit: m_y')
    plt.plot(pos_array, result2.best_fit, label='Fit: m_z')
    plt.xlabel('position (a)')
    plt.ylabel('magnetization comp.')
    plt.savefig(pp, format='pdf')
    pp.close()
    plt.legend()
    plt.show()
    
    for row in reader:
        try:
            t.append(float(row[0]))
            f.append(float(row[1]))
        except ValueError:
            pass

t = np.asarray(t)
f = np.asarray(f)
#pdb.set_trace()
#AJUSTE LINEAL------------------------------------------------------------------------------------------------------------------------------------------------------

mod1 = Model(lineal)
pars1 = mod1.make_params(m=1, b=1)
result1 = mod1.fit(f, pars1, x=t)
#sig = 8
params1 = result1.best_values
m = params1["m"]
b = params1["b"]
#dely1 = result1.eval_uncertainty(sigma=sig)
ylim_fit = lineal(t, m, b)

lin_substract = np.zeros(len(f))
#Substaraccion de ajuste
for k in range(len(f)):
    lin_substract[k] = f[k] - ylim_fit[k]
    
#pdb.set_trace()   

#AJUSTE SENO-------------------------------------------------------------------------------------------------------------------------------------------------------
def single_cluster_fits(df, x, tight_fmaxes, fmin_val=0.0):
    """
    Fit single clusters for cluster in df
    Input:
        df = dataframe of clusters
        x = concentrations
    """
    clusterIDs = df.index.values.tolist()
    data = df.values
    results_dict = {}
    fit_model = Model(hill_equation)
    tight_fmax_2p5, med_fmax, tight_fmax_97p5 = np.nanpercentile(
        tight_fmaxes, q=[2.5, 50.0, 97.5])

    for i, clust in enumerate(clusterIDs):

        fluorescence = data[i, :]
        #non_binder = 0.0
        params = hill_equation_params(
            # Force fmax to be at least 2.5th percentile of previous fmax distribution
            fmax={
                "value": med_fmax,
                "vary": True,
                "min": tight_fmax_2p5
            },
            Kd={"value": max(x)},
            fmin={
                "value": fmin_val,
                "vary": False
            })
        """
        non_binder = 0.0
        if all(fluorescence[~np.isnan(fluorescence)] < tight_fmax_2p5*0.2):
            non_binder = 1.0
            params = hill_equation_params(
                    fmax={"value": med_fmax, "vary": False}, 
                    Kd={"value":max(x)},
                    fmin={"value":fmin_val, "vary":False})
        else:
            params = hill_equation_params(
                fmax={"value":med_fmax, "vary":True},
                Kd={"value":max(x)},
                fmin={"value":fmin_val, "vary":False})
        """

        try:
            fit = fit_model.fit(fluorescence, params, x=x)
        except Exception as e:
            print "Error while fitting {}".format(vID)
            print str(e)
            continue
        ## Things we want to report
        # quality of fit:
        ss_error = np.sum((fit.residual)**2)
        ss_total = np.sum((fluorescence - np.nanmean(fluorescence))**2)
        rsq = 1 - ss_error / ss_total
        rmse = np.sqrt(np.nanmean((fit.residual)**2))

        results_dict[clust] = {
            'Kd': fit.params['Kd'].value,
            'fmax': fit.params['fmax'].value,
            'fmin': fit.params['fmin'].value,
            'rsq': rsq,
            'rmse': rmse,
            'ier': fit.ier,
            'Kd_stderr': fit.params['Kd'].stderr,
            'fmax_stderr': fit.params['fmax'].stderr,
            'fmin_stderr': fit.params['fmin'].stderr
            #'non_binder': non_binder
        }

    return pd.DataFrame(results_dict).T
Esempio n. 44
0
def main():

    grass_path = "data/1pctCO2/grassFrac"
    gpp_path = "data/1pctCO2/gpp"
    npp_path = "data/1pctCO2/npp"
    models = ["BNU-ESM","GFDL-ESM2M","IPSL-CM5A-LR","IPSL-CM5B-LR",\
              "MPI-ESM-LR","MPI-ESM-P","GFDL-ESM2G","HadGEM2-ES",\
              "IPSL-CM5A-MR","MIROC-ESM","MPI-ESM-MR"]

    # Drop GFDL model as model has a weird response to CO2 and CO2 does
    # not increase throughout the timeseries, see comment in Swann paper
    models = ["BNU-ESM","IPSL-CM5A-LR","IPSL-CM5B-LR",\
              "MPI-ESM-LR","MPI-ESM-P","HadGEM2-ES",\
              "IPSL-CM5A-MR","MIROC-ESM","MPI-ESM-MR"]

    rate = 1.0
    start_co2 = 359.08095860482547
    en = 96
    st = 24
    ny = en - st
    co2_conc = []
    for t in range(1, ny + 1):
        co2_conc.append(start_co2 * ((1 + rate / 100)**t))

    fig = plt.figure(figsize=[9, 6])
    fig.subplots_adjust(hspace=0.1)
    fig.subplots_adjust(wspace=0.05)
    plt.rcParams['text.usetex'] = False
    plt.rcParams['font.family'] = "sans-serif"
    plt.rcParams['font.sans-serif'] = "Helvetica"
    plt.rcParams['axes.labelsize'] = 14
    plt.rcParams['font.size'] = 14
    plt.rcParams['legend.fontsize'] = 10
    plt.rcParams['xtick.labelsize'] = 14
    plt.rcParams['ytick.labelsize'] = 14

    #plt.axes([0.125,0.2,0.95-0.125,0.95-0.2])

    almost_black = '#262626'
    # change the tick colors also to the almost black
    plt.rcParams['ytick.color'] = almost_black
    plt.rcParams['xtick.color'] = almost_black

    # change the text colors also to the almost black
    plt.rcParams['text.color'] = almost_black

    # Change the default axis colors from black to a slightly lighter black,
    # and a little thinner (0.5 instead of 1)
    plt.rcParams['axes.edgecolor'] = almost_black
    plt.rcParams['axes.labelcolor'] = almost_black

    ax = fig.add_subplot(111)

    for model in models:
        print(model)
        fname = "%s/grassFrac_%s_1pctCO2_r1i1p1_1_140_1pctCO2_regrid_setgrid.nc"\
                    % (model, model)
        fname = os.path.join(grass_path, fname)
        ds = xr.open_dataset(fname)

        en = 96
        st = 24
        nmonths = 12
        real_st = st * nmonths
        real_en = real_st + ((en - st) * nmonths)

        # 359 ppm - 735 ppm
        grass_frac = ds.grassFrac[real_st:real_en, :, :]

        fname = "%s/npp_%s_1pctCO2_r1i1p1_gC_m2_month_1_140_1pctCO2_regrid_setgrid.nc"\
                    % (model, model)
        fname = os.path.join(npp_path, fname)
        ds = xr.open_dataset(fname)
        # 359 ppm - 735 ppm
        npp = ds.npp[real_st:real_en, :, :]

        mainly_grass = np.where(grass_frac > 0.7, npp, np.nan)

        mainly_grass = np.nanmean(mainly_grass,
                                  axis=(1, 2))  # avg over lat,lon
        #mainly_grass = np.nanmean(mainly_grass, axis=(1)) # avg over lat
        #mainly_grass = np.nanmean(mainly_grass, axis=(1)) # avg over lon

        mainly_grass = mainly_grass.reshape((en - st), 12)
        mainly_grass = np.nansum(mainly_grass, axis=1)  # annual sum

        ini_val = mainly_grass[0]
        response = ((mainly_grass / ini_val) - 1.) * 100.

        ax.plot(co2_conc, response, label=model)

    df_obs = pd.read_csv(
        "/Users/mdekauwe/research/FACE/analysis/GRASS/observations/GRASS_FACE_OBS.csv",
        skiprows=1)
    df_obs["ANPP_RR"] = np.log(1.0 + df_obs.ANPP / 100.)
    mean_response = (np.exp(df_obs.groupby("SITE").ANPP_RR.mean()) -
                     1.0) * 100.0
    sigma_response = (np.exp(df_obs.groupby("SITE").ANPP_RR.std()) -
                      1.0) * 100.0

    CO2_INC = df_obs.CO2_INC.unique()

    popt, pcov = optimize.curve_fit(
        func,
        CO2_INC,
        mean_response.values,
        sigma=sigma_response.values,
        #sigma=np.ones(len(sigma_response.values)),
        absolute_sigma=True)

    xrange = np.arange(np.min(CO2_INC), 360)
    y_fit2 = func(xrange, popt[0], popt[1])

    params = Parameters()
    params.add('a', value=np.random.rand())
    params.add('b', value=np.random.rand())

    mod = Model(func)
    #result = mod.fit(mean_response.values, weights=1.0/(sigma_response.values**2),
    #                 x=CO2_INC, a=np.random.rand(), b=np.random.rand())

    result = mod.fit(mean_response.values,
                     weights=1.0 / sigma_response.values,
                     x=CO2_INC,
                     a=np.random.rand(),
                     b=np.random.rand())

    a = result.params["a"].value
    b = result.params["b"].value

    xrange = np.arange(np.min(CO2_INC), 360)
    y_fit = func(xrange, a, b)

    xrange += 350

    ax.plot(xrange, y_fit, lw=3, ls="-", color="black", label="Observations")
    #ax1.plot(xrange, y_fit2, ls="-.", color="seagreen")

    ax.legend(numpoints=1, loc="best", ncol=2, frameon=False)
    ax.set_xlabel("CO$_2$ (\u03BCmol mol$^{-1}$)")
    ax.set_ylabel("Normalised NPP response to CO$_2$ (%)")

    odir = "/Users/mdekauwe/Desktop"
    fig.savefig(os.path.join(odir, "CMIP5_GPP.pdf"),
                bbox_inches='tight',
                pad_inches=0.1)
Esempio n. 45
0
def Ecal(wguess,
         detectors=[sc],
         motor=th_cal,
         coarse_step=.0012,
         coarse_nsteps=120,
         D='Si',
         detector_name='sc_chan1',
         theta_offset=-35.26,
         nsigma_fine=.1,
         nsigma_range=5,
         output_file="result.csv",
         motor_type='th'):
    '''
        This is the new Ecal scan for dips.
            We should treat peaks separately to simplify matters (leaves for
            easier tweaking)


        This algorithm will search for a peak within a certain theta range
            The theta range is determined from the wavelength guess

        Parameters
        ----------
        wguess : the guessed wavelength
        detectors : list, optional
            list of detectors. Defaults to [sc] detector
        motor : motor, optional
            the motor to scan on (th_cal). Defaults to th_cal
        coarse_step : float, optional
            the max_step to scan on. This is the step size we use for
            the coarse initial scan
        coarse_nsteps : int, optional
            the number of steps to take for coarse scan
        D : string, optional
            the reference sample to use for the calculation of the d spacings
            Defaults to "Si"
        detector_name : str, optional
            the name of the detector
        
        theta_offset : float, optional
            the offset of theta zero estimated from the sample
        nsigma_fine : float, optional
            the fraction of sigma per point for the fine scan
            (the sigma used is the fitted sigma)
        nsigma_range: int, optional
            the range to scan for fine scan:
            +/- nsigma_range*fitted_sigma
            where fitted_sigma is the sigma obtained from the fit from
            the coarse scan
        motor_type : str, optional
            the type of motor used, ether "th" (theta) or "tth"(two-theta)

        Example
        -------

    '''
    # an object for passing messages
    global myresult
    factors = dict(th=1, tth=2)
    factor = factors[motor_type]

    # first guess the theta positions
    # TODO : do for two theta
    cen_guesses = guess_theta_from_reference(wguess, D=D)
    # now find the symmetric peaks
    left_guesses, right_guesses = (theta_offset - cen_guesses,
                                   theta_offset + cen_guesses)

    # just take first peak + symmetric for now
    peak_guesses = right_guesses[0], left_guesses[0]

    print("Trying {} +/- {} = {} and {}".format(theta_offset, cen_guesses[0],
                                                peak_guesses[0],
                                                peak_guesses[1]))

    # set up the live Plotting
    fig = plt.figure(detector_name)
    fig.clf()
    ax = plt.gca()

    #detector_name = detectors[0].name

    # set up a live plotter
    lp = LivePlot(detector_name, x=motor.name, marker='o', ax=ax)

    xdata_total = list()
    ydata_total = list()
    results_list = list()
    cnt = 0
    # TODO : this should be a plan on its own
    for theta_guess in peak_guesses:
        cnt += 1
        # scan the first peak
        # the number of points for each side
        npoints = coarse_nsteps
        start, stop = theta_guess - coarse_step * npoints, theta_guess + coarse_step * npoints
        # reverse to go negative
        start, stop = stop, start
        print("Trying to a guess. Moving {} from {} to {} in {} steps".format(
            motor.name, start, stop, npoints))
        yield from bpp.subs_wrapper(
            bp.scan(detectors, motor, start, stop, npoints), lp)
        # TODO : check if a peak was found here
        # (can use ispeak(... , sdev=2)
        # find the position c1 in terms of theta

        xdata = lp.x_data
        ydata = lp.y_data

        peakmodel = Model(peakfunc, independent_vars=['x'])
        init_guess = guess(xdata, ydata, sigma=coarse_step / 10.)
        res = peakmodel.fit(data=ydata, x=xdata, params=init_guess)
        fitted_sigma = res.best_values['sigma']

        print("guess: {}".format(init_guess))

        plt.figure('fitting coarse {}'.format(cnt))
        plt.clf()
        plt.plot(lp.x_data,
                 lp.y_data,
                 linewidth=0,
                 marker='o',
                 color='b',
                 label="data")
        plt.plot(lp.x_data, res.best_fit, color='r', label="fit")
        plt.plot(init_guess['x0'].value,
                 init_guess['amplitude'].value + init_guess['intercept'].value,
                 'ro')

        # best guess of center position
        new_theta_guess = res.best_values['x0']
        print("Found center at {}, running finer scan".format(new_theta_guess))
        start, stop = new_theta_guess - fitted_sigma * nsigma_range, new_theta_guess + fitted_sigma * nsigma_range
        # force number of steps to yield a step size of approx sigma_fine
        npoints = int(np.abs(stop - start) / fitted_sigma)
        # reverse to go negative
        start, stop = stop, start
        print("Trying to a guess. Moving {} from {} to {} in {} steps".format(
            motor.name, start, stop, npoints))
        yield from bpp.subs_wrapper(
            bp.scan(detectors, motor, start, stop, npoints), lp)

        xdata = lp.x_data
        ydata = lp.y_data

        peakmodel = Model(peakfunc, independent_vars=['x'])
        init_guess = guess(xdata, ydata, sigma=coarse_step / 10.)
        res = peakmodel.fit(data=ydata, x=xdata, params=init_guess)

        plt.figure('fitting fine {}'.format(cnt))
        plt.clf()
        plt.plot(lp.x_data,
                 lp.y_data,
                 linewidth=0,
                 marker='o',
                 color='b',
                 label="data")
        plt.plot(lp.x_data, res.best_fit, color='r', label="fit")
        plt.plot(init_guess['x0'].value,
                 init_guess['amplitude'].value + init_guess['intercept'].value,
                 'ro')

        results_list.append(res)

    # now convert peak to cen
    # just use first peak for now
    # left right doesnt mean anything by the symmetric peak pairs
    peak_left_cen = results_list[0].best_values['x0']
    peak_right_cen = results_list[1].best_values['x0']

    new_theta_offset = (peak_left_cen + peak_right_cen) * .5
    average_peak_theta = (np.abs(peak_left_cen - new_theta_offset) +
                          np.abs(peak_right_cen - new_theta_offset)) * .5
    print("new theta offset : {} deg".format(new_theta_offset))
    print("average peak theta: {} deg".format(average_peak_theta))

    # use first d spacing
    # if th, factor =1 , if tth factor=2 since th = tth/2
    fitted_wavelength = wavelength_from_theta(average_peak_theta / factor,
                                              D_SPACINGS[D][0])
    print("Fitted wavelength is {} angs".format(fitted_wavelength))
    print("Are you happy with results? (y/n)")
    prompt_result = yield from bps.input_plan(">")
    if prompt_result.lower() == "y":
        print("ok, not finalizing. Please run this again")
    else:
        print("Great. Finalizing the Ecal...")
        #yield from finalize_ecal()

    # in case we want access to the results list
    myresult.results_list = results_list
    myresult.wavelength = fitted_wavelength
Esempio n. 46
0
    def boltzman_sigmoidal_model(self, param_dict):
        '''
        calculate boltzman sigmoidal parameters using lmfit
        '''
        def boltzman_sigmoidal(x, bottom, top, v50, slope):
            '''
            Boltzman Sigmoidal equation
            '''
            return bottom + (top - bottom) / (1 + np.exp((v50 - x) / slope))
        
        self.bottom_vary = param_dict['bottom_vary']
        try:
            self.bottom = float(param_dict['bottom'])
        except:
            self.bottom = np.min(self.data.y)
            
        self.top_vary = param_dict['top_vary']
        try:
            self.top = float(param_dict['top'])
        except:
            self.top = np.max(self.data.y)
            
        self.v50_vary = True
        self.v50 = np.mean([np.max(self.data.x), np.min(self.data.x)])
        
        self.slope_vary = param_dict['slope_vary']
        try:
            self.slope = float(param_dict['slope'])
        except:
            self.slope = 1
        
        model = Model(boltzman_sigmoidal)
        
        params = Parameters()
        params.add(name='bottom', value=self.bottom, vary=self.bottom_vary)
        params.add(name='top', value=self.top, vary=self.top_vary)
        params.add(name='v50', value=self.v50, vary=self.v50_vary)
        params.add(name='slope', value=self.slope, vary=self.slope_vary)
        try:
            result = model.fit(self.data.y, params, x=self.data.x)
            fit_error = np.sqrt(np.diag(result.covar))

            self.y_fit = result.best_fit
            self.residuals = self.data.y - self.y_fit
            self.bottom_fit = np.round(result.params['bottom'].value, 2)
            self.bottom_err = np.round(result.params['bottom'].stderr, 2)
            self.top_fit = np.round(result.params['top'].value, 2)
            self.top_err = np.round(result.params['top'].stderr, 2)
            self.v50_fit = np.round(result.params['v50'].value, 2)
            self.v50_err = np.round(result.params['v50'].stderr, 2)
            self.slope_fit = np.round(result.params['slope'].value, 2)
            self.slope_err = np.round(result.params['slope'].stderr, 2)
        except:
            self.y_fit = np.empty(len(self.data['y'].values))
            self.residuals = np.empty(len(self.data['y'].values))
            self.bottom_fit = np.nan
            self.bottom_err = np.nan
            self.top_fit = np.nan
            self.top_err = np.nan
            self.v50_fit = np.nan
            self.v50_err = np.nan
            self.slope_fit = np.nan
            self.slope_err = np.nan
            
        return self
Esempio n. 47
0
def fitspheresubvolume(vol,
                       voxsizes,
                       relth=0.25,
                       Rfix=None,
                       FWHMfix=None,
                       dfix=None,
                       Sfix=None,
                       Bfix=None,
                       wm='dist',
                       cl=False,
                       sphere_center=None):
    """Fit the radial sphere profile of a 3d volume containg 1 sphere

    Parameters
    ----------
    vol : 3d numpy array 
      containing the volume

    voxsizes : 3 component array 
      with the voxel sizes

    relth : float, optional
      the relative threshold (signal over background) for the first coarse 
      delination of the sphere

    dfix, Sfix, Bfix, Rfix : float, optional 
      fixed values for the wall thickness, signal, background and radius

    wm : string, optinal   
      the weighting method of the data (equal, dist, sqdist)

    cl : bool, optional
      bool whether to compute the confidence limits (this takes very long)
 
    sphere_center : 3 element np.array 
      containing the center of the spheres in mm
      this is the center of in voxel coordiantes multiplied by the voxel sizes

    Returns
    -------
    Dictionary
      with the fitresults (as returned by lmfit)
    """

    if sphere_center is None:
        sphere_center = get_sphere_center(vol, voxsizes, relth=relth)

    i0, i1, i2 = np.indices(vol.shape)
    i0 = i0 * voxsizes[0]
    i1 = i1 * voxsizes[1]
    i2 = i2 * voxsizes[2]

    rmax = np.min((i0.max(), i1.max(), i2.max())) / 2
    r = np.sqrt((i0 - sphere_center[0])**2 + (i1 - sphere_center[1])**2 +
                (i2 - sphere_center[2])**2)

    data = vol[r <= rmax].flatten()
    rfit = r[r <= rmax].flatten()

    if (Rfix == None): Rinit = 0.5 * rmax
    else: Rinit = Rfix

    if (FWHMfix == None): FWHMinit = 2 * voxsizes[0]
    else: FWHMinit = FWHMfix

    if (dfix == None): dinit = 0.15
    else: dinit = dfix

    if (Sfix == None): Sinit = data.max()
    else: Sinit = Sfix

    if (Bfix == None): Binit = data.min()
    else: Binit = Bfix

    # lets do the actual fit
    pmodel = Model(glasssphere_profile)
    params = pmodel.make_params(R=Rinit,
                                FWHM=FWHMinit,
                                d=dinit,
                                S=Sinit,
                                B=Binit)

    # fix the parameters that should be fixed
    if Rfix != None: params['R'].vary = False
    if FWHMfix != None: params['FWHM'].vary = False
    if dfix != None: params['d'].vary = False
    if Sfix != None: params['S'].vary = False
    if Bfix != None: params['B'].vary = False

    params['R'].min = 0
    params['FWHM'].min = 0
    params['d'].min = 0
    params['S'].min = 0
    params['B'].min = 0

    if wm == 'equal': weigths = np.ones_like(rfit)
    elif wm == 'sqdist': weights = 1.0 / (rfit**2)
    else: weights = 1.0 / rfit

    weights[weights == np.inf] = 0

    fitres = pmodel.fit(data, r=rfit, params=params, weights=weights)
    fitres.rdata = rfit
    if cl: fitres.cls = fitres.conf_interval()

    # calculate the a50 mean
    fitres.a50th = fitres.values['B'] + 0.5 * (vol.max() - fitres.values['B'])
    fitres.mean_a50 = np.mean(data[data >= fitres.a50th])

    # calculate the mean
    fitres.mean = np.mean(data[rfit <= fitres.values['R']])

    # calculate the max
    fitres.max = data.max()

    # add the sphere center to the fit results
    fitres.sphere_center = sphere_center

    return fitres
Esempio n. 48
0
def calib_g(df_fc_suews, g_max=33.1, lai_max=5.9, s1=5.56, method='cobyla', prms_init=None, debug=False):
    """Calibrate parameters for modelling surface conductance over vegetated surfaces using `LMFIT <https://lmfit.github.io/lmfit-py/model.html>`.

    Parameters
    ----------
    df_fc_suews : pandas.DataFrame
        DataFrame in `SuPy forcing <https://supy.readthedocs.io/en/latest/data-structure/df_forcing.html>`_ format
    g_max : numeric, optional
        Maximum surface conductance [mm s-1], by default 30
    lai_max : numeric, optional
        Maximum LAI [m2 m-2], by default 6
    s1 : numeric, optional
        Wilting point (WP=s1/g6, indicated as deficit [mm]) related parameter, by default 5.56
    method: str, optional
        Method used in minimisation by `lmfit.minimize`: details refer to its `method<lmfit:minimize>`.
    prms_init: lmfit.Parameters
        Initial parameters for calibration
    debug : bool, optional
        Option to output final calibrated `ModelResult <lmfit:ModelResult>`, by default False

    Returns
    -------
    dict, or `ModelResult <lmfit:ModelResult>` if `debug==True`
        1. dict: {parameter_name -> best_fit_value}
        2. `ModelResult`

        Note:
            Parameters for surface conductance:
            g1 (LAI related), g2 (solar radiation related),
            g3 (humidity related), g4 (humidity related),
            g5 (air temperature related),
            g6 (soil moisture related)

    Note
    ----
    For calibration validity, turbulent fluxes, QH and QE, in `df_fc_suews` should ONLY be observations, i.e., interpolated values should be avoided.
    To do so, please place `np.nan` as missing values for QH and QE.

    """
    list_var_sel = ['qh', 'qe', 'Tair', 'RH', 'pres', 'kdown', 'xsmd', 'lai']
    df_obs = df_fc_suews[list_var_sel].copy().dropna()
    df_obs.pres *= 100
    df_obs.Tair += 273.15

    gs_obs = cal_gs_obs(df_obs.qh, df_obs.qe, df_obs.Tair,
                        df_obs.RH, df_obs.pres)

    def func_fit_g(kd, ta, rh, pa, smd, lai, g1, g2, g3, g4, g5, g6):
        return cal_gs_mod(kd, ta, rh, pa, smd, lai,
                          [g1, g2, g3, g4, g5, g6],
                          g_max, lai_max, s1)

    gmodel = Model(func_fit_g,
                   independent_vars=['lai', 'kd', 'ta', 'rh', 'pa', 'smd'],
                   param_names=['g1', 'g2', 'g3', 'g4', 'g5', 'g6'])
    if prms_init is None:
        print('Preset parameters will be loaded!')
        print('Please use with caution.')
        prms = Parameters()
        prm_g_0 = [3.5, 200.0, 0.13, 0.7, 30.0, 0.05]
        list_g = (Parameter(f'g{i+1}', prm_g_0[i], True, 0, None, None,
                            None) for i in range(6))
        prms.add_many(*list_g)
        # set specific bounds:
        # g3, g4: specific humidity related
        prms['g3'].set(min=0, max=1)
        prms['g4'].set(min=0, max=1)
        # g5: within reasonable temperature ranges
        prms['g5'].set(min=-10, max=55)
        # g6: within sensitive ranges of SMD
        prms['g6'].set(min=.02, max=.1)
    else:
        print('User provided parameters are loaded!')
        prms = prms_init

    # pack into a DataFrame for filtering out nan
    df_fit = pd.concat([gs_obs.rename('gs_obs'), df_obs], axis=1).dropna()

    res_fit = gmodel.fit(
        df_fit.gs_obs,
        kd=df_fit.kdown,
        ta=df_fit.Tair,
        rh=df_fit.RH,
        pa=df_fit.pres,
        smd=df_fit.xsmd,
        lai=df_fit.lai,
        params=prms,
        # useful ones: ['nelder', 'powell', 'cg', 'cobyla', 'bfgs', 'trust-tnc']
        method=method,
        #     nan_policy='omit',
        verbose=True,
    )

    # provide full fitted model if debug == True otherwise only a dict with best fit parameters
    res = res_fit if debug else res_fit.best_values

    return res
Esempio n. 49
0
    def fit_pos(self, Rm, Xm, zf_real, zf_imag, name):
        Rm2 = mirror_array(Rm, Rm)
        Xm2 = mirror_array(Xm, Xm)
        zf_impd = mirror_array(zf_imag, zf_real)
        freq = mirror_array(-1*self.freq_array, self.freq_array)
        Cg = self.c_g
        Chl = self.c_hl
        C1 = self.c_ph
        C2 = self.c_pl
        Cx = self.c_x
        w = freq*2*np.pi
        step = np.heaviside(w,0.0)
        
        ### offset resistor in series of DUT
        def fit_rz(a, Cg, Chl, C1, C2, Rm, Xm, w, step):
            w= ((step-1)*w + step*w)
            I = 1j

            Zk = 1/(C1*w*I + 1/self.r1)
            Zt = 1/(C2*w*I + 1/self.r2)
            Za = Zt*a
            Zb = Zt*(1-a)
            Zg = 1/(Cg*w*I)
            Zm = Rm + Xm*I
            Zc = Zm/(1-Zm*Chl*w*I)
            Zh = Zk+Za + Zk*Za/Zg
            Zp = Za + Zg + Zg*Za/Zk
            Zpf = Zh*Zb/(Zc-Zh-Zb)
            Zf = Zp*Zpf/(Zp-Zpf)
            real = Zf.real
            imag = Zf.imag
        
            return (1-step)*imag + step*real     

        ### without offset resistor
        def fit(a, k, Cg, Chl, Cx, Rm, Xm, w, step):
            w= ((step-1)*w + step*w)
            I = 1j


            Zx = 1/(Cx*w*I + 1/self.total_r)
            Zk = Zx*k
            Za = Zx*(a-k)
            Zb = Zx*(1-a)
            Zg = 1/(Cg*w*I)
            Zm = Rm + Xm*I
            Zc = Zm/(1-Zm*Chl*w*I)
            Zh = Zk+Za + Zk*Za/Zg
            Zp = Za + Zg + Zg*Za/Zk
            Zpf = Zh*Zb/(Zc-Zh-Zb)
            Zf = Zp*Zpf/(Zp-Zpf)
            real = Zf.real
            imag = Zf.imag
        
            return (1-step)*imag + step*real  
        
        params = Parameters()
        if self.mode is False:
            # params.add('C1', value = C1, min = 0.5*C1, max = 10*C1, brute_step = 0.1*C1)
            # params.add('C2', value = C2, min = 0.5*C2, max = 10*C2, brute_step = 0.1*C2)
            params.add('C1', value = C1, vary = False)
            params.add('C2', value = C2, vary = False)
        else:
            # params.add('k', value = 0.21, vary = False)
            params.add('k', value = 0.21, min = 0.1 , max = 0.4, brute_step = 0.005)
            params.add('Cx', value = Cx, vary = False)
            # params.add('Cx', value = Cx, min = 0.1*Cx, max = 1000*Cx, brute_step = 0.1*Cx)
            

        params.add('Cg', value = Cg, min = 0.01*Cg, max = 100*Cg, brute_step = 0.1*Cg)
        # params.add('Chl', value = Chl, min = 0.1*Chl, max = 10*Chl, brute_step = 0.1*Chl)
        # params.add('Cg', value = Cg, vary = False)
        params.add('Chl', value = Chl, vary = False)
        params.add('a', value = 0.7, min = 0.5 , max = 1, brute_step = 0.005)
        # params.add('a', value = 0.71, vary = False)

        if self.mode is True:
            model = Model(fit, independent_vars = ['Rm', 'Xm', 'w', 'step'])
        else:
            model = Model(fit_rz, independent_vars = ['Rm', 'Xm', 'w', 'step'])
        result = model.fit(zf_impd, params, method = 'linear_square', Rm = Rm2 , Xm = Xm2 , w = w, step = step)
        # print("results from {}".format(name))
        print(result.best_values['a'])
        # print(result.fit_report())
        fit_result = lowess(result.best_fit, np.arange(92), frac=0.1)[:,1]
        # plt.plot(freq, fit_result)
        plt.plot(freq, result.best_fit)
Esempio n. 50
0
def CurveFit(functionName: str, 
             moduleName: str,
             paramList, 
             times,
             AIFConcs, 
             VIFConcs, 
             concROI, 
             inletType, 
             constantsString):

    """This function calls the fit function of the Model object 
    imported from the lmfit package.  It is used to fit the
    time/MR signal data calculated by a model in this module 
    to the actual Region of Interest (ROI) MR signal/time data using   
    non-linear least squares. 

    Input Parameters
    ----------------
        functionName - The name of the function corresponding to the model.

        paramArray - list of model input parameter values.

        time - NumPy Array of time values stored as floats. Created from a 
            Python list.

        AIFConcs - NumPy Array of MR signals values stored as floats. 
            Created from a Python list.  These MR signals are the Arterial
            Input Function input to the model.

        VIFConcs - NumPy Array of MR signals values stored as floats. 
            Created from a Python list.  These MR signals are the Venous
            Input Function input to the model.

        concROI - NumPy Array of MR signals values stored as floats. 
            Created from a Python list.  These MR signals belong to
            the Region of Interest (ROI).

        inletType - String variable indicating if the model is single or
            dual compartment. The value 'single' indicates single compartment.
            The value 'dual' indicates dual compartment.

        constantsString - String representation of a dictionary of constant
            name:value pairs used to convert concentrations predicted by the
            models to MR signal values.
        
        Returns
        ------
        result.best_values - An array of optimum values of the model input parameters
                that achieve the best curve fit.
        result.covar - The estimated covariance of the values in optimumParams.
            Used to calculate 95% confidence limits.
    """
    try:
        logger.info(
            'Function ModelFunctionsHelper.CurveFit called with function name={} & parameters = {}'
            .format(functionName, paramList) )
        
        if inletType == 'dual':
            timeInputConcs2DArray = np.column_stack((times, AIFConcs, VIFConcs))
        elif inletType == 'single':
            timeInputConcs2DArray = np.column_stack((times, AIFConcs))

        modelFunctions = importlib.import_module(moduleName, package=None)
        modelFunction=getattr(modelFunctions, functionName)

        params = Parameters()
        params.add_many(*paramList)
        #Uncomment the statement below to check parameters 
        #loaded ok into the Parameter object
        #print(params.pretty_print())

        objModel = Model(modelFunction, \
            independent_vars=['xData2DArray', 'constantsString'])
        #print(objModel.param_names, objModel.independent_vars)

        result = objModel.fit(data=concROI, 
                              params=params, 
                              xData2DArray=timeInputConcs2DArray, 
                              constantsString=constantsString)
       
        return result.best_values, result.covar
            
    except ValueError as ve:
        print ('ModelFunctionsHelper.CurveFit Value Error: ' + str(ve))
    except RuntimeError as re:
        print('ModelFunctionsHelper.CurveFit runtime error: ' + str(re))
    except Exception as e:
        print('Error in ModelFunctionsHelper.CurveFit: ' + str(e))   
Esempio n. 51
0
    return R2 / (np.sqrt((R + R2)**2 + (x * L - 1 / (x * C))**2))


# esto era para chequear que sea parecido el modelo a los datos antes de fittear
#w = np.linspace(50075,50110)
#y = transfer(w, R, R2, L, C)
#
#plt.plot(w,y)
#plt.plot(w_1,t_1)
#plt.show
#------------

# Ajuste

gmodel = Model(transfer)
result = gmodel.fit(t_1, x=w_1, R=R, L=L, C=C)
print(result.fit_report())

#plt.scatter(w_1,t_1, marker = 'o', c = 'k', s= 5)
#plt.errorbar(w_1, t_1,yerr=error, fmt = ' ', c = 'dimgray', alpha=0.5)
##plt.plot(w_1, result.init_fit, 'k--', label='initial fit')
#plt.plot(w_1, result.best_fit, 'r-', label='best fit')
#plt.legend(loc='best')
##plt.show()

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

# Para configurar el grid (start, stop, step)
x_major_ticks = np.arange(50070, 50115, 5)
x_minor_ticks = np.arange(50070, 50115, 2.5)
Esempio n. 52
0
	vel_50 = 60*lab_wvl/(c*1e-3)
	print '200 km/s limit:', vel_limit


#setting gauss parameters
	gmod.set_param_hint('amp',value=max(y), vary=False) #careful???!!!
	gmod.set_param_hint('cen',value=63.18)
	gmod.set_param_hint('level',value=0.0, vary=False) 
	gmod.set_param_hint('fwhm', value = fwhm_start, vary=False) #fixed
	gmod.make_params()
#parameters with fixed fwhm and very constrained amplitude
#basically we let the fitting to play only with center


#fitting
	result = gmod.fit(y,x=x)
#optimal parameters
	amp = result.best_values['amp']
	cen = result.best_values['cen']
	fwhm = result.best_values['fwhm']
	level = result.best_values['level']
	sigma = fwhm/2.35482


#calculate field under the gauss function
	gaussfield = amp*math.sqrt(2*math.pi*sigma**2)*1e-4*c/(cen**2) 


#residual of data-gauss
	residual=y-result.best_fit
Esempio n. 53
0
Gain = GainHOT
Tsys = Power_watt/(k*Delta_nu*Gain)
GaindBm = 10*np.log10(GainHOT)
print(GaindBm, 'Gain in dBm')
GaindBW = GaindBm - 30
print(GaindBW, 'Gain in DbW')
print(Gain)

#### Start fitting https://lmfit.github.io/lmfit-py/model.html ####
def background(x, Tcmb, Tau0,Tatm,Trcvr):
    return Tcmb*np.exp(-Tau0 / (np.cos(x))) + Tatm*(1-np.exp(-Tau0 / (np.cos(x)))) + Trcvr

gmod = Model(background)
print(gmod.param_names, gmod.independent_vars) #show parameters and independant variables
#gmod.guess()
result = gmod.fit(Z_Angle[2:-20], x=Z_Angle[2:-20], Tcmb=1, Tau0=1, Tatm=Tatm, Trcvr = Trcvr )

print(result.fit_report())
##########################

############# Plot things ################################
fig = plt.figure()
frame1 = fig.add_subplot(1,1,1)

#lables and titles
frame1.set_title("System temperature as a function of Zenith temperature")    
frame1.set_xlabel('# Angle (deg from Zenith)')
frame1.set_ylabel('Temperature (Kelvin)')
plt.ylim((0,200))

Esempio n. 54
0
def plot_catalogue_result(catalogue_csv):

    with open(catalogue_csv, 'r', newline='') as file:
        reader = csv.reader(file)
        for row in reader:
            #read in header string describing how the search was preformed
            search_params = ast.literal_eval(*row)
            break

    data = np.loadtxt(catalogue_csv, skiprows=2, delimiter=",")

    stacked_data = np.stack((data[:, 3], data[:, 4]), axis=1)
    stacked_data = stacked_data[stacked_data[:, 0].argsort()]

    magnitudes = stacked_data[:, 0]
    magnitudes_e = stacked_data[:, 1]
    # mag_limits = np.array([magnitudes[0]])
    N = np.array([1])
    for m in magnitudes[1:]:
        # mag_limits = np.append(mag_limits, m)
        N = np.append(N, N[-1] + 1)
    N_e = np.sqrt(N)
    log_N = np.log10(N)
    log_N_e = N_e / (np.log(10) * N)

    weights = 1 / np.sqrt((log_N_e / log_N)**2 +
                          (magnitudes_e / magnitudes)**2)
    for i in range(len(magnitudes)):
        if magnitudes[i] > 16.3:
            weights[i] = 0

    fig, axis = plt.subplots(figsize=(7, 5))

    # for offsets in np.arange(0, np.max(magnitudes)):
    #     y = (mag_limits - offsets) * 0.6
    #     plt.plot(mag_limits, y, "salmon", alpha = 0.5)
    # for offsets in np.arange(0, np.max(magnitudes)):
    #     y = (mag_limits - offsets) * plot_grad
    #     plt.plot(mag_limits, y, "skyblue", alpha = 0.5)

    # custom_lines=[Line2D([0],[0], color="salmon", lw=2),
    #             Line2D([0],[0], color="skyblue", lw=2)]
    # custom_labels = ["Gradient = 0.6", "Gradient = {}".format(plot_grad)]
    # axis.legend(custom_lines, custom_labels,loc = 'lower right')

    # axis.errorbar(mag_limits, log_N, xerr= magnitudes_e,
    #     fmt = "none", color = "salmon", alpha = 0.5)
    axis.fill_between(magnitudes,
                      y1=log_N + log_N_e,
                      y2=log_N - log_N_e,
                      color="salmon",
                      alpha=0.5,
                      label="log$_{10}$(N) Error")
    # axis.errorbar(mag_limits, log_N, yerr= log_N_e,
    #     fmt = "none", color = "skyblue", alpha = 0.5)
    axis.fill_betweenx(log_N,
                       x1=magnitudes - magnitudes_e,
                       x2=magnitudes + magnitudes_e,
                       color="skyblue",
                       alpha=0.7,
                       label="Magnitude Error")
    axis.plot(magnitudes, log_N, "k")

    def linear(x, gradient, intercept):
        return x * gradient + intercept

    lmodel = Model(linear)
    result = lmodel.fit(log_N,
                        x=magnitudes,
                        gradient=0.6,
                        intercept=-2.5,
                        weights=weights)
    print(result.fit_report())
    axis.plot(magnitudes, result.best_fit, 'k--', label='Best Fit')

    axis.set_xlabel("Calibrated Galaxy Magnitude")
    axis.set_ylabel("log$_{10}$[N(<m)]")
    axis.set_xlim(np.min(magnitudes), np.max(magnitudes))
    axis.set_ylim(np.min(log_N), np.max(log_N) + 0.03)
    axis.legend()
    plt.show()
Esempio n. 55
0
def ps(uid='-1',det='default',suffix='default',shift=.5, logplot='off', der  = False, plot = True ):
    '''
    YG Copied from CHX beamline@March 18, 2018
    function to determine statistic on line profile (assumes either peak or erf-profile)
    calling sequence: uid='-1',det='default',suffix='default',shift=.5)
    det='default' -> get detector from metadata, otherwise: specify, e.g. det='eiger4m_single'
    suffix='default' -> _stats1_total / _sum_all, otherwise: specify, e.g. suffix='_stats2_total'
    shift: scale for peak presence (0.5 -> peak has to be taller factor 2 above background)
    '''
    #import datetime
    #import time
    #import numpy as np
    #from PIL import Image
    #from databroker import db, get_fields, get_images, get_table
    #from matplotlib import pyplot as pltfrom
    #from lmfit import  Model
    #from lmfit import minimize, Parameters, Parameter, report_fit
    #from scipy.special import erf

    # get the scan information:
    if uid == '-1':
        uid=-1
    if det == 'default':
        if db[uid].start.detectors[0] == 'elm' and suffix=='default':
            intensity_field='elm_sum_all'
        elif db[uid].start.detectors[0] == 'elm':
            intensity_field='elm'+suffix
        elif suffix == 'default':
            intensity_field= db[uid].start.detectors[0]+'_stats1_total'
        else:
            intensity_field= db[uid].start.detectors[0]+suffix
    else:
        if det=='elm' and suffix == 'default':
            intensity_field='elm_sum_all'
        elif det=='elm':
            intensity_field = 'elm'+suffix
        elif suffix == 'default':
            intensity_field=det+'_stats1_total'
        else:
            intensity_field=det+suffix

    field = db[uid].start.motors[0]

    #field='dcm_b';intensity_field='elm_sum_all'
    [x,y,t]=get_data(uid,field=field, intensity_field=intensity_field, det=None, debug=False)  #need to re-write way to get data
    x=np.array(x)
    y=np.array(y)
    #print(t)
    if der:
        y = np.diff( y )
        x = x[1:]
        
    PEAK=x[np.argmax(y)]
    PEAK_y=np.max(y)
    COM=np.sum(x * y) / np.sum(y)

    ### from Maksim: assume this is a peak profile:
    def is_positive(num):
        return True if num > 0 else False

    # Normalize values first:
    ym = (y - np.min(y)) / (np.max(y) - np.min(y)) - shift  # roots are at Y=0

    positive = is_positive(ym[0])
    list_of_roots = []
    for i in range(len(y)):
        current_positive = is_positive(ym[i])
        if current_positive != positive:
            list_of_roots.append(x[i - 1] + (x[i] - x[i - 1]) / (abs(ym[i]) + abs(ym[i - 1])) * abs(ym[i - 1]))
            positive = not positive
    if len(list_of_roots) >= 2:
        FWHM=abs(list_of_roots[-1] - list_of_roots[0])
        CEN=list_of_roots[0]+0.5*(list_of_roots[1]-list_of_roots[0])
        ps.fwhm=FWHM
        ps.cen=CEN
        #return {
        #    'fwhm': abs(list_of_roots[-1] - list_of_roots[0]),
        #    'x_range': list_of_roots,
       #}
    else:    # ok, maybe it's a step function..
        print('no peak...trying step function...')
        ym = ym + shift
        def err_func(x, x0, k=2, A=1,  base=0 ):     #### erf fit from Yugang
            return base - A * erf(k*(x-x0))
        mod = Model(  err_func )
        ### estimate starting values:
        x0=np.mean(x)
        #k=0.1*(np.max(x)-np.min(x))
        pars  = mod.make_params( x0=x0, k=200,  A = 1., base = 0. )
        result = mod.fit(ym, pars, x = x )
        CEN=result.best_values['x0']
        FWHM = result.best_values['k']
        ps.cen = CEN
        ps.fwhm = FWHM

    ### re-plot results:
    if plot:
        if logplot=='on':
            plt.close(999)
            plt.figure(999)
            plt.semilogy([PEAK,PEAK],[np.min(y),np.max(y)],'k--',label='PEAK')
            #plt.hold(True)
            plt.semilogy([CEN,CEN],[np.min(y),np.max(y)],'r-.',label='CEN')
            plt.semilogy([COM,COM],[np.min(y),np.max(y)],'g.-.',label='COM')
            plt.semilogy(x,y,'bo-')
            plt.xlabel(field);plt.ylabel(intensity_field)
            plt.legend()
            plt.title('uid: '+str(uid)+' @ '+str(t)+'\nPEAK: '+str(PEAK_y)[:8]+' @ '+str(PEAK)[:8]+'   COM @ '+str(COM)[:8]+ '\n FWHM: '+str(FWHM)[:8]+' @ CEN: '+str(CEN)[:8],size=9)
            plt.show()
        else:
            plt.close(999)
            plt.figure(999)
            plt.plot([PEAK,PEAK],[np.min(y),np.max(y)],'k--',label='PEAK')
            #plt.hold(True)
            plt.plot([CEN,CEN],[np.min(y),np.max(y)],'r-.',label='CEN')
            plt.plot([COM,COM],[np.min(y),np.max(y)],'g.-.',label='COM')
            plt.plot(x,y,'bo-')
            plt.xlabel(field);plt.ylabel(intensity_field)
            plt.legend()
            plt.title('uid: '+str(uid)+' @ '+str(t)+'\nPEAK: '+str(PEAK_y)[:8]+' @ '+str(PEAK)[:8]+'   COM @ '+str(COM)[:8]+ '\n FWHM: '+str(FWHM)[:8]+' @ CEN: '+str(CEN)[:8],size=9)
            plt.show()

    ### assign values of interest as function attributes:
    ps.peak=PEAK
    ps.com=COM
Esempio n. 56
0
    def fit_parasitics(self, bg_real, bg_imag, color1, color2):
        i = self.num
        fit_impd = np.array([0.0]*2*i)
        fit_freq = np.array([0.0]*2*i)
        for j in range(i):
            fit_impd[i+j] = bg_real[j]
            fit_impd[i-j-1] = bg_imag[j]
            fit_freq[i+j] = 10000+j*2000
            fit_freq[i-j-1] = -10000 - j*2000
        
        ### fitting function
        ### offset resistor in series of DUT
        def fit_rz_p(C1, C2, R1, R2, Chl, Cg, w):
            step = np.heaviside(w,0.0)
            w= ((step-1)*w + step*w)
            I = 1j
            Z1 = 1/(C1*w*I + 1/R1)
            Z2 = 1/(C2*w*I + 1/R2)
            Zg = 1/(Cg*w*I)
            Zt = Z1 + Z2 + Z1*Z2/Zg
            Zm = Zt/(1+ Zt*Chl*w*I)
            
            real = Zm.real
            imag = Zm.imag
            return (1-step)*(imag) + step*(real)

        ### without offset resistor
        def fit_p(Cx, Rt, Chl, Cg, w, k):
            step = np.heaviside(w,0.0)
            w= ((step-1)*w + step*w)
            I = 1j
            Zx = 1/(Cx*w*I + 1/Rt)
            Zg = 1/(Cg*w*I)
            Zt = Zx + Zx*Zx*k*(1-k)/Zg

            Zm = Zt/(1+ Zt*Chl*w*I)
            
            real = Zm.real
            imag = Zm.imag
            return (1-step)*(imag) + step*(real)
    
        params = Parameters()

        ### offset resistor in series of DUT
        if self.mode is False:
            # params.add('C1', value = 8e-14, min = 1e-16, max=  1e-10)
            # params.add('C2', value = 8e-14, min = 1e-16, max=  1e-10)
            params.add('C1', value = 0, vary = False)
            params.add('C2', value = 0, vary = False)
            # params.add('R1', value = self.r1, min = self.r1-1e3, max = self.r1+1e3)
            # params.add('R2', value = self.r2, min = self.r2-1e3, max = self.r2+1e3)
            params.add('R1', value = self.r1, vary = False)
            params.add('R2', value = self.r2, vary = False)

        ### without offset resistor
        else:
            params.add('k', value = 0.21, min = 0.1, max = 0.4)
            # params.add('k', value = 0.21, vary = False)
            params.add('Cx', value = 1e-12, min = 1e-14, max=  1e-11, brute_step = 1e-13)
            params.add('Rt', value = self.total_r, min = self.total_r-1e4, max = self.total_r+1e4, brute_step = 100)
            # params.add('Rt', value = self.total_r, vary = False)

        # params.add('Cg', value = 2e-13, vary = False)
        params.add('Cg', value = 2e-13, min = 1e-13, max = 1e-11, brute_step = 1e-13)
        params.add('Chl', value = 2.5609e-13, min = 1e-15, max= 5e-13, brute_step = 1e-13)
        # params.add('Chl', value = 2.5e-13, vary = False) 

        if self.mode is True:
            model = Model(fit_p, independent_vars = ['w'])
            result = model.fit(fit_impd, params, method='linear_square', w = 2*np.pi*fit_freq)
            print(result.fit_report())
            ### update parasitics value
            self.c_hl = result.best_values['Chl']
            self.c_g = result.best_values['Cg']
            self.c_x = result.best_values['Cx']
        else:
            model = Model(fit_rz_p, independent_vars = ['w'])
            result = model.fit(fit_impd, params, method='linear_square', w = 2*np.pi*fit_freq)
            print(result.fit_report())
            ### update parasitics value
            self.c_hl = result.best_values['Chl']
            self.c_ph = result.best_values['C1']
            self.c_pl = result.best_values['C2']
            self.c_g = result.best_values['Cg']
Esempio n. 57
0
# clear; python eg2.py; rm *~
from numpy import sqrt, pi, exp, linspace, loadtxt
from lmfit import Model

import matplotlib.pyplot as plt

data = loadtxt('model1d_gauss.dat')
x = data[:, 0]
y = data[:, 1] + 0.25*x - 1.0

def gaussian(x, amp, cen, wid):
    "1-d gaussian: gaussian(x, amp, cen, wid)"
    return (amp/(sqrt(2*pi)*wid)) * exp(-(x-cen)**2 /(2*wid**2))

def line(x, slope, intercept):
    "line"
    return slope * x + intercept

mod = Model(gaussian) + Model(line)
pars  = mod.make_params( amp=5, cen=5, wid=1, slope=0, intercept=1)

result = mod.fit(y, pars, x=x)

print(result.fit_report())

plt.plot(x, y,         'bo')
plt.plot(x, result.init_fit, 'k--')
plt.plot(x, result.best_fit, 'r-')
plt.show()
#<end examples/model_doc2.py>
Esempio n. 58
0
sma = isolist.sma[1:]
sma *= ps * distance

#=========================
#Fitting Hernquist profile
#=========================


def Hernquist(r, M, a):
    return (M * a) / (2 * np.pi * r * (r + a)**3)


model = Model(Hernquist)
params = Parameters()
params.add('M', value=1e+10)
params.add('a', value=1e+3)
result = model.fit(mass, params, r=sma)

print(result.fit_report())

plt.plot(sma, mass, 'X', color='yellow')
plt.plot(sma,
         Hernquist(sma, result.params['M'].value, result.params['a'].value),
         '--',
         color='blue')
plt.xlabel(r'$r\ [pc]$')
plt.ylabel(r'$\rho(r)\ [\frac{M_\odot}{pc^3}]$')

plt.show()
                                           x[j].std_dev,
                                           size=MC_iterations)
        Y_matrix[j, :] = random.normal(y[j].nominal_value,
                                       y[j].std_dev,
                                       size=MC_iterations)

    #Run the fits
    for k in range(MC_iterations):
        x_i = metal_matrix[:, k]
        y_i = Y_matrix[:, k]

        m, n, r_value, p_value, std_err = stats.linregress(x_i, y_i)
        m_vector[k], n_vector[k] = m, n

        #Lmfit
        result_lmfit = lmod.fit(y_i, x=x_i, m=0.005, n=0.24709)
        lmfit_matrix[:, k] = array(result_lmfit.params.valuesdict().values())
        lmfit_error[:, k] = array(
            [result_lmfit.params['m'].stderr, result_lmfit.params['n'].stderr])

        #Curvefit
        best_vals, covar = curve_fit(linear_model, x_i, y_i, p0=p0)
        curvefit_matrix[:, k] = best_vals

        #kapteyn
        fitobj = kmpfit.Fitter(residuals=residuals_lin, data=(x_i, y_i))
        fitobj.fit(params0=p0)
        kapteyn_matrix[:, k] = fitobj.params

    #Get fit mean values
    n_Median, n_16th, n_84th = median(n_vector), percentile(n_vector,
Esempio n. 60
0
    def ontype(self, event):
        zabs = np.double(0.)
        self.ax.draw_artist(self.ax)
        # when the user hits 'r': clear the axes and plot the original spectrum
        if event.key == 'r':
            self.ax.cla()
            self.ax.step(self.wave, self.flux, 'k-', linewidth=1)
            self.ax.set_xlabel('Wavelength')
            self.ax.set_ylabel('Flux')
            xr = [np.min(self.wave), np.max(self.wave)]
            yr = [np.min(self.flux), np.max(self.flux)]
            self.ax.set_ylim([yr[0], yr[1]])
            self.ax.set_xlim([xr[0], xr[1]])

        # Set top y max
        elif event.key == 't':
            xlim = self.ax.get_xlim()
            ylim = self.ax.get_ylim()
            self.ax.set_ylim([ylim[0], event.ydata])
            self.ax.set_xlim(xlim)
            plt.draw()
        elif event.key == 'Z':
            print('Testing to see if we can get a pop up window to work')
            self.set_redshift()
            self.DrawLineList(self.label)

        elif event.key == 'j':
            lambda_rest, LineList = self.identify_line_GUI()
            print(lambda_rest, LineList)
            self.zabs = (event.xdata - lambda_rest) / lambda_rest
            self.label = LineList
            self.DrawLineList(self.label)
            print('Target Redshfit set at : ' + np.str(self.zabs))

        # Manage and plot multiple absorbers
        #Clunky GUI to plot lines
        elif event.key == 'K':
            self.manage_identified_absorbers()

        #Load a saved linelist
        elif event.key == '0':
            self.load_linelist_GUI()
            self.manage_identified_absorbers()
        #Load pre identified individual absorbers
        elif event.key == '+':
            #filename=self.read_identified_linelist_GUI()
            #print(filename)
            #self.plot_identified_linelist(filename)
            self.load_linelist_GUI()
            self.manage_identified_absorbers()
            self.fig.canvas.draw()
            plt.draw()

        # Set top y min
        elif event.key == 'b':
            xlim = self.ax.get_xlim()
            ylim = self.ax.get_ylim()
            self.ax.set_ylim([event.ydata, ylim[1]])
            self.ax.set_xlim(xlim)
            plt.draw()

        # Smooth spectrum
        elif event.key == 'S':
            self.vel[0] += 2
            Filter_size = np.int(self.vel[0])
            self.smoothed_spectrum = convolve(
                self.flux,
                Box1DKernel(Filter_size))  #medfilt(flux,np.int(Filter_size))
            self.specplot()
            plt.draw()

        #Unsmooth Spectrum
        elif event.key == 'U':
            self.vel[0] -= 2
            if self.vel[0] <= 0:
                self.vel[0] = 1
            Filter_size = np.int(self.vel[0])
            self.smoothed_spectrum = convolve(
                self.flux,
                Box1DKernel(Filter_size))  #medfilt(flux,np.int(Filter_size))
            self.specplot()
            plt.draw()

            # Set X max
        elif event.key == 'X':
            xlim = self.ax.get_xlim()
            ylim = self.ax.get_ylim()
            self.ax.set_xlim([xlim[0], event.xdata])
            self.ax.set_ylim(ylim)
            plt.draw()
        # Set x min
        elif event.key == 'x':
            xlim = self.ax.get_xlim()
            ylim = self.ax.get_ylim()
            self.ax.set_xlim([event.xdata, xlim[1]])
            self.ax.set_ylim(ylim)
            plt.draw()

        # Set pan spectrum
        elif event.key == ']':
            xlim = self.ax.get_xlim()
            ylim = self.ax.get_ylim()
            delx = (xlim[1] - xlim[0])
            self.ax.set_xlim([xlim[1], xlim[1] + delx])
            self.ax.set_ylim(ylim)
            plt.draw()

        # Set pan spectrum
        elif event.key == '[':
            xlim = self.ax.get_xlim()
            ylim = self.ax.get_ylim()
            delx = (xlim[1] - xlim[0])
            self.ax.set_xlim([xlim[0] - delx, xlim[0]])
            self.ax.set_ylim(ylim)
            plt.draw()

        # Compute Equivalent Width between two points
        elif event.key == 'E':
            #ekeycounts +=1
            self.lam_lim = np.append(self.lam_lim, event.xdata)
            self.lam_ylim = np.append(self.lam_ylim, event.ydata)

            # Keep running tab of all E clicks
            eclick = len(self.lam_lim)

            #self.specplot()

            self.ax.plot(event.xdata,
                         event.ydata,
                         'rs',
                         ms=5,
                         picker=5,
                         label='EW_pt',
                         markeredgecolor='k')
            #self.fig.canvas.draw()

            if eclick == 2:
                # Check if the wave entries are monotonously increasing
                tab = self.lam_lim.argsort()

                EW, sig_EW, cont, wave_slice = self.compute_EW(
                    self.wave / (1. + zabs), self.flux, self.lam_lim[tab],
                    self.lam_ylim[tab], self.error)
                EW = np.array(EW) * 1000.
                sig_EW = np.array(sig_EW) * 1000.
                self.ax.plot(wave_slice, cont, 'r--')
                #ipdb.set_trace()
                print(
                    '---------------------- Equivalent Width -------------------------------------'
                )
                Wval = 'EW [mAA]: ' + '%.1f' % EW + ' +/- ' + '%.1f' % sig_EW
                self.ax.text(np.mean([self.lam_lim]),
                             np.max(self.lam_ylim) + 0.2,
                             Wval,
                             rotation=90,
                             verticalalignment='bottom')
                print(Wval)
                print(
                    '---------------------------------------------------------------------------'
                )
                self.lam_lim = []
                self.lam_ylim = []

            plt.draw()
            self.fig.canvas.draw()

        # Fit a Gaussian
        elif event.key == 'F':
            self.FXval = np.append(self.FXval, event.xdata)
            self.FYval = np.append(self.FYval, event.ydata)

            fclick = len(self.FXval)
            self.ax.plot(event.xdata,
                         event.ydata,
                         'rs',
                         ms=5,
                         picker=5,
                         label='EW_pt',
                         markeredgecolor='k')
            self.fig.canvas.draw()
            plt.show()

            #Start Fitting
            if fclick == 3:
                # First fit a quick continuum
                qtq = np.where(((self.wave / (1. + zabs)) >= self.FXval[0])
                               & ((self.wave / (1. + zabs)) <= self.FXval[2]))
                ww = self.wave[qtq] / (1. + zabs)
                flux1 = self.flux[qtq]
                spline = splrep(np.append(self.FXval[0], self.FXval[2]),
                                np.append(self.FYval[0], self.FYval[2]),
                                k=1)
                continuum = splev(ww, spline)

                # Check if it is an absorption or emission line
                if ((self.FYval[1] < self.FYval[0]) &
                    (self.FYval[1] < self.FYval[2])):
                    ydata = 1. - (flux1 / continuum)
                    gmodel = Model(gaussian)
                    result = gmodel.fit(ydata,
                                        x=ww,
                                        amp=self.FYval[1] - self.FYval[1],
                                        cen=self.FXval[2],
                                        wid=0.5 *
                                        (self.FXval[2] - self.FXval[0]))
                    Final_fit = (1. - result.best_fit) * continuum
                else:
                    ydata = (flux1 / continuum)
                    gmodel = Model(gaussian)
                    result = gmodel.fit(ydata,
                                        x=ww,
                                        amp=self.FYval[1] - self.FYval[1],
                                        cen=self.FXval[2],
                                        wid=0.5 *
                                        (self.FXval[2] - self.FXval[0]))
                    Final_fit = result.best_fit * continuum

                print(result.fit_report())
                model_fit = self.ax.plot(ww, Final_fit, 'r-')
                plt.draw()
                print("Gaussian Fit")

                FXval = []
                FYval = []

            #If the user presses 'h': The help is printed on the screen
        elif event.key == 'h':
            print('''    
            ---------------------------------------------------------------------------
            This is an interactive 1D spectrum viewer.
            The help scene activates by pressing h on the plot.
    
    
            The program only works properly if none of the toolbar buttons in the figure is activated. 
            It also needs pysimpleGUI code to be installed. 
            https://pysimplegui.readthedocs.io/en/latest/
    
    
    
    
    
            Useful Keystrokes:
    
                Keystrokes:
                  
                  r        :    Reset Spectrum and replot to default settings.
                  h        :    Prints this help window.
                  x or X   :    Set xmin, xmax
                  b or t   :    Set ymin, ymax
                  [ or ]   :    Pan left or right 
                  s or S.  :    Smooth or Unsmooth spectra
                  E        :    Two E keystrokes will compute rest frame equivalent width at a defined region
                  F        :    Three keystrokes to fit a Gaussian profile. [Currently not drawing on the spectrum]
    
                  #GUI ELEMENTS [WARNING UNSTABLE]
                  Works with TkAGG backend and pysimplegui
    
                  Z  :   pop up window to select absorber redshift and linelist
                  j  :   pop up window to select a corresponding rest frame transition and linelist
                  K  :   pop up window to select multiple absorber lines and plot them
                  0  :   pop up window to select identified absorber list to show with 1d spectrum
                  +  :   pop up window to select filename of pre-identified list of individual absorbers and plot them
    
                  q     :    Quit Program.
             ---------------------------------------------------------------------------
            Written By:  Rongmon Bordoloi                                   August 2020.
    
            HEALTH WARNING: The GUI implementation is still in alpha version and is quite unstable.
            User must be careful to make sure that they exit individual GUIs first by pressing the correct button
            before closing the plot window. 
            ---------------------------------------------------------------------------
            import matplotlib
            matplotlib.use('TkAgg')
            from linetools.spectra.xspectrum1d import XSpectrum1D  
            from GUIs import rb_plot_spec as r

            sp=XSpectrum1D.from_file('PG0832+251_nbin3_coadd.fits') 
            r.rb_plot_spec(sp.wavelength.value,sp.flux.value,sp.sig.value) 
    
            ''')

        # Making sure any drawn line list remains drawn
        self.DrawLineList(self.label)
        q = np.where(self.zabs_list['List'] != 'None')
        if len(q[0] > 0):
            self.draw_any_linelist()
        plt.draw()
        self.fig.canvas.draw()