Example #1
0
def fitting(x,y,f,p0,c0,yerr='none',qval='none'):
   if yerr=='none':
      yerr=y*0+y/y
   global func
   func=f
   npar=len(p0)
   func=def_func(npar,c0)
   print 'fitting with funcion: ', func
   print 'no of parameters: ', len(p0)
#   plsq = leastsq(residuals, p0, args=(y,x,yerr), col_deriv=1, maxfev=20000)
   if 'duri' in func:
      plsq= leastsq(residuals_duri_global, p0, args=(y,x,yerr,qval), col_deriv=0, ftol=1e-4, maxfev=2000000)
   else:
      plsq = leastsq(residuals, p0, args=(y,x,yerr), col_deriv=0, maxfev=20000)
   if npar==1:
      final_par = array([plsq[0]])
   else:
      final_par = plsq[0]
   if 'duri' in func:
      yfit=0*y
      for i,q in enumerate(qval):
         q=float(q)
         yfit[i,:]=pyl.array(peval_duri_global(x,final_par,q),typecode='d')
   else:
      yfit=pyl.array(peval(x,final_par),typecode='d')
   return yfit, final_par, func
Example #2
0
def main():
    """Set up a simple recipe in a few lines."""

    # The SimpleRecipe class is a type of FitRecipe. It provides attribute-like
    # access to variables and a residual function that can be minimized.
    recipe = SimpleRecipe()

    # Load text from file.
    recipe.loadtxt("data/gaussian.dat")

    # Set the equation. The variable "x" is taken from the data that was just
    # loaded. The other variables, "A", "x0" and "sigma" are turned into
    # attributes with an initial value of 0.
    recipe.setEquation("A * exp(-0.5*(x-x0)**2/sigma**2)")

    # We can give them other values here.
    recipe.A = 1
    recipe.x0 = 5
    recipe.sigma = 1

    # We explicitly optimize the residual method of the SimpleRecipe
    from scipy.optimize import leastsq
    leastsq(recipe.residual, recipe.values)

    # Print the results
    recipe.printResults()

    return
Example #3
0
    def fitBrokenToStrips(self):

        """Fits broken-line to strip data"""

        # convenience views to avoid typos
        bStrip = (self.stripCounts > 10)
        if np.sum(bStrip) < 1:
            return

        x = self.stripFeHs[bStrip]
        y = self.stripMedns[bStrip]  # could come back to this later

        # guess set of parameters
        #guess = [-0.4, -40., -240., 40.]
        guess = [-0.3, 0.0, -250., 0.0]

        # Yes this could all be looped through...

        meritStraight = lambda pars, x, y: oneStraight(x, pars) - y
        meritBroken = lambda pars, x, y: twoStraight(x, pars) - y 

        # do the fitting 
        self.parsStraight, self.successStraight = \
            leastsq(meritStraight,guess[0:2] ,args=(x,y))

        self.parsBroken, self.successBroken = \
            leastsq(meritBroken,guess[:] ,args=(x,y))

        # compute the sum of residuals, over the strips (not the
        # stars)
        self.chisqStraight = np.sum(meritStraight(self.parsStraight, x, y)**2)
        self.chisqBroken = np.sum(meritBroken(self.parsBroken, x, y)**2)
Example #4
0
def fit_OrnsteinZernike(g6, envelope, p0=[1,1]):
    fitfunc = OrnsteinZernike3D
    errfunc = lambda p, x, y: fitfunc(p, x) - y
    logerrfunc = lambda p, x, y: np.log(fitfunc(p, x)) - np.log(y)
    p1, success = optimize.leastsq(errfunc, p0[:], args=(g6[:,0][envelope], g6[:,1][envelope]))
    p2, success = optimize.leastsq(logerrfunc, p0[:], args=(g6[:,0][envelope], g6[:,1][envelope]))
    return p1, p2
def GetBestFitSc(Method, Data, DataErrs=None):
    """
    Compute the best fit Sc value to the data using the scipy optimize.leassq
    package. Also returns the reduced chi squared as a measure of the goodness
    of fit.
    """
    ScInit = (
        0.8
    )  # Need to have an initial for the optimizer, any valid Sc value can be used - will not impact the final value
    Fit_Sc = []  # Need to initialize this in case Method is incorrectly defined. Need some error handling!

    if Method == "raw":
        Fit_Sc, _, infodict, _, _ = optimize.leastsq(
            Residuals, ScInit, args=(Data[4], Data[2], Data[3]), full_output=True
        )
        chi = reduced_chi_square(infodict["fvec"], Fit_Sc[0])
    elif Method == "patches":
        Fit_Sc, _, infodict, _, _ = optimize.leastsq(
            Residuals, ScInit, args=(Data[10], Data[2], Data[6]), full_output=True
        )
        chi = reduced_chi_square(infodict["fvec"], Fit_Sc[0], DataErrs)
    elif Method == "basins":
        Fit_Sc, _, infodict, _, _ = optimize.leastsq(
            Residuals, ScInit, args=(Data[7], Data[5], Data[6]), full_output=True
        )
        chi = reduced_chi_square(infodict["fvec"], Fit_Sc[0], DataErrs)

    return Fit_Sc[0], chi
Example #6
0
    def track_all(self, problem_name, result_type):
        problem = self.problems[problem_name]

        if problem.lb is not None or problem.ub is not None:
            raise NotImplementedError

        ftol = 1e-5

        if result_type == 'average time':
            n_runs = 10
            t0 = time.time()
            for _ in range(n_runs):
                leastsq(problem.fun, problem.x0, Dfun=problem.jac, ftol=ftol,
                        full_output=True)
            return (time.time() - t0) / n_runs

        x, cov_x, info, message, ier = leastsq(
            problem.fun, problem.x0, Dfun=problem.jac,
            ftol=ftol, full_output=True
        )
        if result_type == 'nfev':
            return info['nfev']
        elif result_type == 'success':
            return int(problem.check_answer(x, ftol))
        else:
            raise NotImplementedError
Example #7
0
def fit_pk_parms_1d(p0,x,f,pktype):
    """
    Performs least squares fit to find parameters for 1d analytic functions fit
    to diffraction data

    Required Arguments:
    p0 -- (m) ndarray containing initial guesses for parameters for the input peaktype
    x -- (n) ndarray of coordinate positions
    f -- (n) ndarray of intensity measurements at coordinate positions x
    pktype -- string, type of analytic function that will be used to fit the data,
    current options are "gaussian","lorentzian","pvoigt" (psuedo voigt), and
    "split_pvoigt" (split psuedo voigt)
    

    Outputs:
    p -- (m) ndarray containing fit parameters for the input peaktype (see peak function
    help for what each parameters corresponds to)
    
    
    Notes:
    1. Currently no checks are in place to make sure that the guess of parameters
    has a consistent number of parameters with the requested peak type
    """    
    
    
    fitArgs=(x,f,pktype)
    
    ftol=1e-6
    xtol=1e-6    
    
    weight=np.max(f)*10.#hard coded should be changed
    
    if pktype == 'gaussian':
        p, outflag = optimize.leastsq(fit_pk_obj_1d, p0, args=fitArgs,Dfun=eval_pk_deriv_1d,ftol=ftol,xtol=xtol) 
    elif pktype == 'lorentzian':
        p, outflag = optimize.leastsq(fit_pk_obj_1d, p0, args=fitArgs,Dfun=eval_pk_deriv_1d,ftol=ftol,xtol=xtol) 
    elif pktype == 'pvoigt':
        lb=[p0[0]*0.5,np.min(x),0.,      0., 0.,None]
        ub=[p0[0]*2.0,np.max(x),4.*p0[2],1., 2.*p0[4],None]         
                
        fitArgs=(x,f,pktype,weight,lb,ub)
        p, outflag = optimize.leastsq(fit_pk_obj_1d_bnded, p0, args=fitArgs,ftol=ftol,xtol=xtol) 
    elif pktype == 'split_pvoigt':
        lb=[p0[0]*0.5,np.min(x),0.,      0.,      0., 0., 0.,None]
        ub=[p0[0]*2.0,np.max(x),4.*p0[2],4.*p0[2],1., 1., 2.*p0[4],None]         
        fitArgs=(x,f,pktype,weight,lb,ub)
        p, outflag = optimize.leastsq(fit_pk_obj_1d_bnded, p0, args=fitArgs,ftol=ftol,xtol=xtol) 
        
    elif pktype == 'tanh_stepdown':
        p, outflag = optimize.leastsq(fit_pk_obj_1d, p0, args=fitArgs,ftol=ftol,xtol=xtol)
    else:
        p=p0        
        print('non-valid option, returning guess')
           
    
    if np.any(np.isnan(p)):
        p=p0
        print('failed fitting, returning guess')
    
    return p
Example #8
0
    def Ajustar_Curvas_Caracteristicas(self):
        """Define la curva característica de la bomba a partir de los datos, todos ellos en forma de array de igual dimensión
        Q: caudal, m3/s
        h: carga, m
        mu: rendimiento
        NPSHr: carga neta de aspiración requerida por la bomba para no entrar en cavitación
        """
        Q=r_[self.curvaActual[2]]
        h=r_[self.curvaActual[3]]
        Pot=r_[self.curvaActual[4]]
        NPSH=r_[self.curvaActual[5]]
        
        funcion_h = lambda p, x: p[0]*x**2+p[1]*x+p[2] # Función a ajustar
        residuo_h = lambda p, x, y: funcion_h(p, x) - y # Residuo
        inicio=r_[0, 0, 0]
        ajuste_h, exito_h=optimize.leastsq(residuo_h,inicio,args=(Q, h))
        self.CurvaHQ=ajuste_h
        
        funcion_Pot = lambda p, x: p[0]*x**2+p[1]*x+p[2] # Función a ajustar
        residuo_Pot = lambda p, x, y: funcion_Pot(p, x) - y # Residuo
        inicio=r_[0, 0, 0]
        ajuste_Pot, exito_Pot=optimize.leastsq(residuo_Pot,inicio,args=(Q, Pot))
        self.CurvaPotQ=ajuste_Pot

        funcion_NPSH = lambda p, x: p[0]+p[1]*exp(p[2]*x) # Función a ajustar
        residuo_NPSH = lambda p, x, y: funcion_NPSH(p, x) - y # Residuo
        inicio=r_[0, 0, 0]
        ajuste_NPSH, exito_NPSH=optimize.leastsq(residuo_NPSH,inicio,args=(Q, NPSH))
        self.CurvaNPSHQ=ajuste_NPSH
Example #9
0
def sinefit(frames, dt, sin=np.sin):
    """fit_parameters = sinefit(frames)
    Takes the output of parse_csv and runs a sine-fitting function against it.
    For frames with n dots, returns an n-element list of tuples of SineFit objects (x,y).
    e.g., fit_parameters[0] = (SineFit(for dot0 in x), SineFit(for dot0 in y))
    """

    # p = [amplitude, period, phase offset, y offset]
    fitfunc = lambda p, x: p[0] * sin(2*pi/p[1]*x + p[2]) + p[3]
    errfunc = lambda p, x, y: fitfunc(p, x) - y
    p0 = [1., 1., 0., 0.]
    t = np.arange(len(frames)) * dt
    fit_parameters = []

    for idot in xrange(len(frames[0])):
        print 'Sine fitting: dot %d' % idot
        dx, dy = zip(*[(frame[idot].xpos, frame[idot].ypos) for frame in frames])
        p0[0] = (max(dx)-min(dx))/2.0
        p0[3] = np.mean(dx)
        # FIXME: "success" here is not a valid success measure
        px, success = optimize.leastsq(errfunc, p0, args=(t, dx))
        if not success:
            raise "Problem with optimize for dot %d in x" % idot
        xfit = SineFit(*px, sin=sin)
        xfit.r2 = stats.mstats.pearsonr(dx, xfit.eval(t))[0] ** 2
        p0[0] = (max(dy)-min(dy))/2.0
        p0[3] = np.mean(dy)
        py, success = optimize.leastsq(errfunc, p0, args=(t, dy))
        if not success:
            raise "Problem with optimize for dot %d in y" % idot
        yfit = SineFit(*py, sin=sin)
        yfit.r2 = stats.mstats.pearsonr(dy, yfit.eval(t))[0] ** 2
        fit_parameters.append((xfit, yfit))

    return fit_parameters
Example #10
0
def update(val):
    min_val = x_min.val
    max_val = x_max.val

    filter = logical_and(r>min_val,r<max_val)

    new_r = r.compress(filter)

    slopes = []
    for column in m_range:
        cms = matrix[:, column]
        cms = cms.compress(filter)

        error_min=lambda p, y, x: y - (p[0] * x + p[1])
        fit=array([1, 0])
        fit, success = leastsq(error_min, fit.copy(), args=(cms, new_r))

        slopes.append(fit)

    l_fit.set_ydata(slopes[MAIN][0] * new_r + slopes[MAIN][1])
    l_fit.set_xdata(new_r)
    
    regressions = [x[0] for x in slopes]
    slopes_plot.set_ydata(regressions)

    m=m_range
    dm=array(regressions)
    error_min=lambda p, dm, m: dm-p[0]*(1-exp(-p[1]*m))
    p=array([6.0,1.0])
    p_result, success=leastsq(error_min, p.copy(), args=(dm, m))
    hline.set_ydata(p_result[0])
    draw()
Example #11
0
def LSFit(parlen,xdata,yerr,fitfun,ydata):
    iGuess = None
    MI = MaxIters
    derfun = None
    data = CreateArgs(xdata,ydata,yerr)
    LSfitfun,LSDerfitfun,iGuess = GetLSFuns(fitfun,derfun,iGuess,parlen)
    # if isinstance(ydata[0],complex):iGuess = map(complex,iGuess)
    if Debug:
        print LSDerfitfun.__name__
        print LSfitfun.__name__
        print np.array(iGuess)
        print np.array(data)
    if ForceNoDer:
        x,covar, infodict, mesg, ier=leastsq(LSfitfun,np.array(iGuess),args=np.array(data), maxfev=MI, full_output=1)
    else:
        x,covar, infodict, mesg, ier=leastsq(LSfitfun,np.array(iGuess),args=np.array(data), Dfun=LSDerfitfun, maxfev=MI, full_output=1)
    if float(len(ydata)-len(x)) == 0:
        chisqpdf = float('NaN')
    else:
        chisqpdf=sum(infodict["fvec"]*infodict["fvec"])/float(len(ydata)-len(x))
    # if ier != 1:
    #    print x
    #    print "WARNING: Optimal parameters not found: " + mesg
    #      raise ValueError, "Optimal parameters not found: " + mesg
    #   print x,covar
    return x,covar,chisqpdf
Example #12
0
    def quick_exponential(self, x, y):
#        kw = dict()
        fitfunc = lambda p, x:p[0] * exp(p[1] * x)
        errfunc = lambda p, x, y: fitfunc(p, x) - y

        # do a linear fit first to estimate the initial guess
        # p0=[intercept, slope]

#        r = self.linear(x, y)

        slope, intercept = polyfit(x, y, 1)
#        slope, intercept = r['coefficients']
        p0 = [intercept, slope]

        from scipy import optimize
        coeffs, cov_params, _infodict, _msg, ier = optimize.leastsq(errfunc, p0[:], args=(x, y),
                                    full_output=1,
                                    # maxfev = 1000
                                     )
        while ier != 1:
            p0 = [pi / 2.0 for pi in p0]
            coeffs, cov_params, _infodict, _msg, ier = optimize.leastsq(errfunc, p0[:], args=(x, y),
                                full_output=1,
                            #    maxfev = 1000
                            )
        coeff_errors = sqrt(diagonal(cov_params))

        return coeffs, coeff_errors
Example #13
0
def plotcurving(M, N, text, reg = 0):
	#choose N point randomly as x
	x = np.linspace(0, 1, N)
	#plot with the point
	x_show = np.linspace(0, 1, 1000)

	y0 = real_func(x)
	#G-distribution y with noise	
	y1 = [np.random.normal(0, 0.1) + y for y in y0]

	p0 = np.random.randn(M)

	if reg == 0:
		plsq = leastsq(regresiduals, p0, args=(y1, x))
	else:
		plsq = leastsq(residuals, p0, args=(y1, x))

	print ('Fitting Parameters: ', plsq[0])

	font = {'family' : 'serif',
        'color'  : 'darkred',
        'weight' : 'normal',
        'size'   : 16,
        }

	plt.title(text, fontdict=font)
	plt.plot(x_show, real_func(x_show), label='real')
	plt.plot(x_show, fake_func(plsq[0], x_show), label='fitted curve')
	plt.plot(x, y1, 'bo', label='with noise')
	plt.legend()
	plt.show()
def fitting_Vthre_then_Fout(Fout, muV, sV, TvN, muGn, Gl, Cm, El,\
        maxiter=1e5, ftol=1e-15,\
        correction=default_correction, print_things=True,\
        return_chi2=False):

    P = linear_fitting_of_threshold_with_firing_weight(\
            Fout, muV, sV, TvN, muGn, Gl, Cm, El,\
            maxiter=maxiter, xtol=ftol,\
            correction=correction,\
            print_things=print_things)

    def Res(p, muV, sV, TvN, muGn, Fout):
        return (Fout-erfc_func(muV, sV, TvN,\
           final_threshold_func(p, muV, sV, TvN, muGn, El,\
                                correction=correction), Gl, Cm))
                                
    if return_chi2:
        P,cov,infodict,mesg,ier = leastsq(
            Res,P, args=(muV, sV, TvN, muGn, Fout),\
            full_output=True)
        ss_err=(infodict['fvec']**2).sum()
        ss_tot=((Fout-Fout.mean())**2).sum()
        rsquared=1-(ss_err/ss_tot)
        return P, rsquared
    else:
        P = leastsq(Res, P, args=(muV, sV, TvN, muGn, Fout))[0]
        return P
Example #15
0
def getKandVariance(inputRawData,
					inputRelations,
					maxIterations = 0,
					kSeed = 1.0,
					varianceSeed = 0.001,
					medianSide = 100,
					verbose = False,
					showSumSQ = False,
					alphaSeed = 1.0,
					useCooperativity = False):

	if useCooperativity:
		seed = [kSeed, varianceSeed, alphaSeed]
		bestSol, success = leastsq(residuals, seed, args = (inputRawData, inputRelations, medianSide, verbose, showSumSQ, useCooperativity), maxfev = maxIterations)
	else:
		seed = [kSeed, varianceSeed]
		bestSol, success = leastsq(residuals, seed, args = (inputRawData, inputRelations, medianSide, verbose, showSumSQ, useCooperativity), maxfev = maxIterations)
	
	kOut = bestSol[0]
	varianceOut = bestSol[1]
	if useCooperativity: alphaOut = bestSol[2]
	# kOut = k
	# varianceOut = variance
	
	if verbose:
		print()
		print("K = " + str(kOut))
		print("Variance = " + str(varianceOut))
		if useCooperativity: print("Alpha = " + str(alphaOut))
		
	if useCooperativity: return [kOut, varianceOut, alphaOut]
	else: return [kOut, varianceOut]
Example #16
0
def fit_wcs(res,x0,y0):
    ra,de,x,y=res[:,0],res[:,1],res[:,2],res[:,3]
    ra0,de0=np.mean(ra),np.mean(de)
    dx,dy=x-x0,y-y0

    for i in range(5):
        w=wcs.WCS(naxis=2)
        w.wcs.crpix=np.array([0.0,0.0])
        w.wcs.crval=np.array([ra0,de0])
        w.wcs.cd=np.array([[1.0,0.0],[0.0,1.0]])
        w.wcs.ctype=["RA---TAN","DEC--TAN"]
        w.wcs.set_pv([(2,1,45.0)])

        world=np.stack((ra,de),axis=-1)
        pix=w.wcs_world2pix(world,1)
        rx,ry=pix[:,0],pix[:,1]

        ax,cov_q,infodict,mesg,ierr=optimize.leastsq(residual,[0.0,0.0,0.0],args=(dx,dy,rx),full_output=1)
        ay,cov_q,infodict,mesg,ierr=optimize.leastsq(residual,[0.0,0.0,0.0],args=(dx,dy,ry),full_output=1)

        ra0,de0=w.wcs_pix2world([[ax[0],ay[0]]],1)[0]

    w=wcs.WCS(naxis=2)
    w.wcs.crpix=np.array([x0,y0])
    w.wcs.crval=np.array([ra0,de0])
    w.wcs.cd=np.array([[ax[1],ax[2]],[ay[1],ay[2]]])
    w.wcs.ctype=["RA---TAN","DEC--TAN"]
    w.wcs.set_pv([(2,1,45.0)])

    whdr={"CRPIX1":x0,"CRPIX2":y0,"CRVAL1":ra0,"CRVAL2":de0,"CD1_1":ax[1],"CD1_2":ax[2],"CD2_1":ay[1],"CD2_2":ay[2],"CTYPE1":"RA---TAN","CTYPE2":"DEC--TAN","CUNIT1":"DEG","CUNIT2":"DEG"}

    return whdr
def loess_robust_parameter_estimates(data):
    """ Calculate Loess robust parameter estimates.

    :param data: A data set.
     :type data: list(float)

    :return: The parameter estimates.
     :rtype: list(float)
    """
    def f(p, x, y, weights):
        return weights * (y - (p[0] + p[1] * x))

    n = len(data)
    x = np.array(range(1, n + 1))
    y = np.array(data)
    weights = np.array(tricube_weights(n))
    estimates, _ = leastsq(f, [1., 1.], args=(x, y, weights))

    p = estimates.tolist()
    residuals = (y - (p[0] + p[1] * x))

    weights2 = np.array(tricube_bisquare_weights(residuals.tolist()))
    estimates2, _ = leastsq(f, [1., 1.], args=(x, y, weights2))

    return estimates2.tolist()
Example #18
0
def fit_data(data, nmodes):

    npars = 1+2*nmodes
    p0 = numpy.zeros(npars, dtype=numpy.float64)
    p1=None ; vl1=None
    p2=None ; vl2=None
    L_S = numpy.arange(360)
    fit_results=dict(L_S=L_S)
    if "vl1" in data:
        print data["L_S"].shape, data["vl1"].shape
        p1, success =  optimize.leastsq(errfunc, 
                        p0[:], 
                        args=(data["L_S"][1:-1].astype(numpy.float64), 
                              data["vl1"][1:-1].astype(numpy.float64)))
        vl1 = fitfunc(p1, L_S)
        fit_results["p1"]=p1
        fit_results["vl1"]=vl1
    if "vl2" in data:
        p2, success =  optimize.leastsq(errfunc, 
                        p0[:], 
                        args=(data["L_S"][1:-1].astype(numpy.float64), 
                              data["vl2"][1:-1].astype(numpy.float64)))
        vl2 = fitfunc(p2, L_S)
        fit_results["p2"]=p2
        fit_results["vl2"]=vl2

    return     fit_results
def two_gaussian2d_fit(sx, sy, guess=[0.5,1]):
    """2D-Gaussian fit of samples S using a fit to the empirical CDF."""
    ## UNFINISHED (I have 2 alphas unp.sign the xy projections)
    assert sx.size == sy.size

    ## Empirical CDF
    ecdfx = [np.sort(sx), np.arange(0.5,sx.size+0.5)*1./sx.size]
    ecdfy = [np.sort(sy), np.arange(0.5,sy.size+0.5)*1./sy.size]

    ## Analytical gaussian CDF
    gauss_cdf = lambda x, mu, sigma: 0.5*(1+erf((x-mu)/(np.sqrt(2)*sigma)))

    gauss2d_cdf = lambda X,Y,mx,sx,my,sy: gauss_cdf(X,mx,sx)*gauss_cdf(Y,my,sy)

    two_cdf = lambda x, m1, s1, m2, s2, a:\
        a*gauss_cdf(x,m1,s1)+(1-a)*gauss_cdf(x,m2,s2)

    two2d_cdf = lambda X,Y, mx1, sx1, mx2, sx2, my1, sy1, my2, sy2, a:\
        a*gauss2d_cdf(X,Y,mx1,sx1,my1,sy1)+(1-a)*gauss_cdf(X,Y,mx2,sx2,my2,sy2)

    ## Fitting the empirical CDF
    fitfunc = lambda p, x: two_cdf(x, *p)
    errfunc = lambda p, x, y: fitfunc(p, x) - y
    fitfunc2d = lambda p, X,Y: two2d_cdf(X,Y, *p)
    errfunc2d = lambda p, X,Y,Z: fitfunc2d(p, X,Y) - Z

    px,v = leastsq(errfunc, x0=guess, args=(ecdfx[0],ecdfx[1]))
    py,v = leastsq(errfunc, x0=guess, args=(ecdfy[0],ecdfy[1]))
    print("2D Two-Gaussians CDF fit", px, py)

    mux1, sigmax1, mux2, sigmax2, alphax = px
    muy1, sigmay1, muy2, sigmay2, alphay = py
    return mu1, sigma1, mu2, sigma2, alpha
Example #20
0
def pulseIntFit(t, pulseInt, shape='gaussian'):
	'''
	Fit the pulse with a specific pulse shape
		* t: time vector
		* pulseInt: Pulse intensity
		* shape: Pulse shape ('gaussian', 'sech', 'parabolic')
	'''

	# Distance to the target function
	errfunc_g = lambda p, t, y: pulse.gaussianPulseCmpt(t, p) - y
	errfunc_s = lambda p, t, y: pulse.sechPulseCmpt(t, p) - y
	errfunc_p = lambda p, t, y: pulse.parabolicPulseFitCmpt(t, p) - y

	# Initial guess for the parameters
	p0_g = [5.0, 0.0, 1.0, 0.0]
	p0_s = [5.0, 0.0, 1.0, 0.0]
	p0_p = [5.0, 0.0, pulseInt.max()]

	# Fit with rge least square method
	[p, success]  = {
	  'gaussian': lambda: leastsq(errfunc_g, p0_g[:], args=(t, pulseInt)),
	  'sech': lambda: leastsq(errfunc_s, p0_s[:], args=(t, pulseInt)),
	  'parabolic': lambda: leastsq(errfunc_p, p0_p[:], args=(t, pulseInt)),
	}[shape]()

	fit  = {
	  'gaussian': lambda: pulse.gaussianPulseCmpt(t, p),
	  'sech': lambda: pulse.sechPulseCmpt(t, p),
	  'parabolic': lambda: pulse.parabolicPulseFitCmpt(t, p),
	}[shape]()

	return fit
def gaussian_fit_curve(x, y, mu0=0, sigma0=1, a0=None, return_all=False,
        **kwargs):
    """Gaussian fit of curve (x,y).
    If a0 is None then only (mu,sigma) are fitted (to a gaussian density).
    `kwargs` are passed to the leastsq() function.

    If return_all=False then return only the fitted (mu,sigma) values
    If return_all=True (or full_output=True is passed to leastsq)
    then the full output of leastsq is returned.
    """
    if a0 is None:
        gauss_pdf = lambda x, m, s: np.exp(-((x-m)**2)/(2*s**2))/\
                                    (np.sqrt(2*np.pi)*s)
        err_fun = lambda p, x, y: gauss_pdf(x, *p) - y
        res = leastsq(err_fun, x0=[mu0, sigma0], args=(x, y), **kwargs)
    else:
        gauss_fun = lambda x, m, s, a: a*np.sign(s)*np.exp(-((x-m)**2)/(2*s**2))
        err_fun = lambda p, x, y: gauss_fun(x, *p) - y
        res = leastsq(err_fun, x0=[mu0, sigma0, a0], args=(x, y), **kwargs)

    if 'full_output' in kwargs:
        return_all = kwargs['full_output']
    mu, sigma = res[0][0], res[0][1]
    if return_all: return res
    return mu, sigma
Example #22
0
 def __solver__(self, p):
     
     p.xk = p.x0.copy()
     p.fk = asfarray((p.f(p.x0)) ** 2).sum().flatten()
         
     p.iterfcn()
     if p.istop:
         p.xf, p.ff = p.xk, p.fk
         return 
     
     if p.userProvided.df:
         xf, cov_x, infodict, mesg, ier = leastsq(p.f, p.x0, Dfun=p.df, xtol = p.xtol, ftol = p.ftol, maxfev = p.maxFunEvals, full_output = 1)
     else:
         xf, cov_x, infodict, mesg, ier = leastsq(p.f, p.x0, xtol = p.xtol, maxfev = p.maxFunEvals, epsfcn = p.diffInt, ftol = p.ftol, full_output = 1)
     
     if ier == 1: p.istop = 1000
     else: p.istop = -1000
     p.msg = mesg
         
     ff = asfarray((p.f(xf)) ** 2).sum().flatten()
     p.xk = xf
     p.fk = ff
     
     p.xf = xf
     p.ff = ff        
     p.iterfcn()
Example #23
0
def fitDSTFMRscan(dataframe,f,i,theta_wi):
	positive=dataframe[dataframe.field>0.01]
	posmin= positive.l1x.argmin()
	posupper = posmin+30
	poslower = posmin-30
	positive = dataframe[poslower:posupper]
	#print(positive)
	#print('positive')
	negative=dataframe[dataframe.field<-0.01]
	negmin= negative.l1x.argmin()
	negative = dataframe[negmin-30:negmin+30]
	#print(negative)

	lsqpos = leastsq(deriv_stfmr_with_i_residual, theta_wi, 
					args=(positive.field, f,i, positive.l1x*10**6), maxfev=10000,
					full_output = 1, )
	lsqneg = leastsq(deriv_stfmr_with_i_residual, theta_wi, 
					args=(abs(negative.field), f,i, negative.l1x*10**6), maxfev=10000,
					full_output = 1, )
	poptpos, coptpos, _,_,_ =lsqpos
	poptneg,coptneg,_,_,_ = lsqneg
	alpha_pos =poptpos[0]
	alpha_neg =poptneg[0]
	Meff_neg = poptneg[1]
	Meff_pos = poptpos[1]
	hdcpos = poptpos[4]
	hdcneg = poptneg[4]
	kpos = poptpos[5]
	kneg = poptneg[5]

	cxeven = (poptpos[2]-poptneg[2])/2
	cxodd = (poptpos[2]+poptneg[2])/2
	czeven = (poptpos[3]-poptneg[3])/2
	czodd =  (poptpos[3]+poptneg[3])/2
	return (np.array([alpha_pos,alpha_neg,Meff_pos,Meff_neg,hdcpos,hdcneg,kpos,kneg,cxeven,cxodd,czeven,czodd]),lsqpos,lsqneg)   
Example #24
0
    def sendCalibration(self):
        n = len(self.pointList)
        data = numpy.empty((3, n))
        for i in range(n):
            data[:,i] = self.pointList[i][1]
        print "Data:", data
        ## find coefficients for function T = f(V)
        #     R = R1 * ((1023 / V) - 1)
        #     1/T = A + B log(R) + C log(R)^3   ## the parameter R1 gets absorbed by A, so there are only 3 variables.

        def fn(v, x):
            R = (1023. / x) - 1.
            lnR = numpy.log(R)
            return 1.0 / (v[0] + v[1] * lnR + v[2] * lnR * lnR * lnR)
            
        def erf(v, x, y):
            return fn(v, x)-y
            
        guess = numpy.array([0.04, 0.03, -0.01]) 
        fit1 = leastsq(erf, guess, args=(data[1], data[0]))
        fit2 = leastsq(erf, guess, args=(data[2], data[0]))
        
        print "Fit:"
        print fit1
        print fit2
        cmd = 'c' + fit1[0].astype(numpy.float32).tostring() + fit2[0].astype(numpy.float32).tostring()
        self.ArduinoWrite(cmd)
Example #25
0
def bmfit_1D(x, y):
    p = [0, 0, 0, 0]
    p = sec_order_fit(x, y)
    return leastsq(residuals, p, args=(y, x), full_output=1)
def runclient(args, pars):

    # load single pulse data
    singlePulse = np.load('singlePulse.npz')['avePulse']
    # make it match the length of the Acqiris trace (this will change)
    singlePulse = np.pad(singlePulse, (7500, 7500),
                         'constant',
                         constant_values=(0, 0))
    # normalize trace to peak so that the amplitude we fit is proportional to peak values on MCP trace
    singlePulse = singlePulse / np.max(singlePulse)

    # location of peak in reference trace
    peak1 = np.argmax(singlePulse)

    # optimization function
    def optFunc(p, xdata, ydata, pulse):
        a1, a2, x1, x2 = p
        err = np.abs(ydata - (a1 * np.interp(xdata - x1, xdata, pulse) +
                              a2 * np.interp(xdata - x2, xdata, pulse)))
        return err

    # function that returns curve
    def evalFunc(p, xdata, pulse):
        a1, a2, x1, x2 = p
        curve = a1 * np.interp(xdata - x1, xdata, pulse) + a2 * np.interp(
            xdata - x2, xdata, pulse)
        return curve

    # expected delay based on what we asked from the accelerator
    delay = args.delay

    # initialize fitting parameters (actually I don't think these are being used...)
    x1_0 = 4478.0  # based on visual inspection of data
    x2_0 = x1_0 + delay / .125  # based on expected delay time
    a1_0 = 1.0  # arbitrary
    a2_0 = 1.0  # arbitrary
    p0 = [a1_0, a2_0, x1_0, x2_0]  # put into array

    # array for interpolation
    xdata = np.arange(0, 20000)

    # where to find the data
    sh_mem = pars['live']
    expName = pars['expName']
    runNum = args.run
    runString = 'exp=' + expName + ':run=' + runNum + ':smd'

    # needed for shared memory access
    calibDir = '/reg/d/psdm/sxr/' + expName + '/calib'

    # check whether we're reading from shared memory or old run
    ds = []
    if sh_mem:
        runString += ':dir=/reg/d/ffb/sxr/' + expName + '/xtc:live'
        ds = DataSource(''.join(['shmem=psana.', str(rank % 8), ':stop=no']))
        setOption('psana.calib-dir', calibDir)
    else:
        ds = DataSource(runString)

    # acqiris detector
    acq = Detector('Acq02')

    # number of events between updates (set in MCP_driver.py)
    updateEvents = pars['updateEvents']

    # initialize instance of the mpidata class for communication with the master process
    md = mpidata()

    # counter (local per core)
    n1 = -1

    # initialize arrays
    x1Array = np.empty(0)
    x2Array = np.empty(0)
    a1Array = np.empty(0)
    a2Array = np.empty(0)

    # initialize acqiris trace
    wf = np.zeros(20000)

    # event loop
    for nevent, evt in enumerate(ds.events()):

        # check if we've reached the event limit
        if nevent == args.noe: break
        if nevent % (size - 1) != rank - 1:
            continue  # different ranks look at different events
        # skip damaged events
        if acq.waveform(evt) is None: continue  # damage

        # update counter
        n1 += 1

        # get MCP trace
        wf = acq.waveform(evt)[0].flatten()
        # subtract background (assume at least 10000 data points before pulse)
        wf = wf - np.mean(wf[0:10000])

        # guesses for fit parameters
        # peak of first pulse
        p0a1 = np.max(wf)
        # time position of first pulse (relative to reference)
        p0x1 = np.argmax(wf) - peak1
        # subtract first pulse guess from MCP trace
        wf2 = wf - evalFunc([p0a1, 0, p0x1, 0], xdata, singlePulse)

        # peak of second pulse
        p0a2 = np.max(wf2)
        # time position of second pulse (relative to reference)
        p0x2 = np.argmax(wf2) - peak1
        # put guesses into a list
        p0 = [p0a1, p0a2, p0x1, p0x2]

        # fit the curve (put small limit on number of function evaluations for speed)
        p = optimization.leastsq(optFunc,
                                 p0,
                                 args=(xdata, wf, singlePulse),
                                 maxfev=10)

        # update arrays based on current event
        a1Array = np.append(a1Array, p[0][0])
        a2Array = np.append(a2Array, p[0][1])
        x1Array = np.append(x1Array, p[0][2])
        x2Array = np.append(x2Array, p[0][3])

        if ((n1 + 1) % updateEvents == 0
            ):  # send mpi data object to master when desired

            # include fit to current trace to make sure fit looks good
            fit1 = evalFunc([p0a1, p0a2, p0x1, p0x2], xdata, singlePulse)
            md.addarray('wf', wf)
            md.addarray('fit', fit1)
            md.addarray('a1', a1Array)
            md.addarray('a2', a2Array)
            md.addarray('delay', np.abs(x1Array - x2Array) * .125)
            md.small.event = nevent
            md.send()

            # reset counter
            n1 = -1
            # reinitialize arrays
            a1Array = np.empty(0)
            a2Array = np.empty(0)
            x1Array = np.empty(0)
            x2Array = np.empty(0)

    md.endrun()
Example #27
0
def alignY(datalists,xmin,xmax,zeropoint=0,p0=[1,0,1.5,-2.5],func='power'):

    """

    *** WILL BE REWRITTEN ***

    Align two datasets by shifting Y coordinate.

    Works on multiple data lists at a time. Each dataset is shifted to match
    with the previous dataset AFTER the previous one has already been shifted.

    e.g. for the third dataset y_new = zeropoint + shifts[1]*shifts[2]*y

    The shifts between datasets are multiplicative, the first dataset is
    shifted additively by the keyword zeropoint.

    At least 3 points of overlap are required!

    @param datalists: two or more 2d lists of data giving (x,y)
    @type datalists: list[list[(.,.)]]
    @param xmin: the lower boundar(y)(ies) of the overlapping region, used for
                 alignment. Must be 1 value for 2 datasets, n-1 values for more
    @type xmin: float or list
    @param xmax: the upper boundar(y)(ies) of the overlapping region, used for
                 alignment. Must be 1 value for 2 datasets, n-1 values for more
    @type xmax: float or list

    @keyword zeropoint: The first dataset is shifted additively with this value

                        (default: 0)
    @type zeropoint: float
    @keyword p0: initial parameters for fitting function definition

                 (default: [1,0,1.5,-2.5])
    @type p0: list

    @return: The datasets are returned as given, but with shifted Y-values, and
             the shifts used (first value additively, rest multiplicatively)
    @rtype: (list[list[(.,.)]], list)

    """

    #- zeropoint correction
    shifts = [zeropoint]
    current_data = array(datalists[0])
    corrected = [[coord + array([0,zeropoint]) for coord in current_data]]

    #- Power law is fitted to overlapping xrange for both datasets with leastsq
    #- x of second list is evaluated with both functions
    #- second list's y values are corrected by the mean of the ratios of the
    #- two function evaluations

    for i in xrange(len(datalists)-1):
        p_lsqlist1 = leastsq(Interpol.getResiduals,p0,\
                             args=([x
                                    for x in array(corrected[i])[:,0]
                                    if x >= xmin[i] and x <= xmax[i]],\
                                   [coord[1]
                                    for coord in array(corrected[i])
                                    if coord[0] >= xmin[i] and coord[0] <= xmax[i]],\
                                   func),\
                             maxfev=2000)[0]
        p_lsqlist2 = leastsq(Interpol.getResiduals,p0,\
                             args=([x
                                    for x in array(datalists[i+1])[:,0]
                                    if x >= xmin[i] and x <= xmax[i]],\
                                   [coord[1]
                                    for coord in array(datalists[i+1])
                                    if coord[0] >= xmin[i] and coord[0] <= xmax[i]],\
                                   func),\
                             maxfev=2000)[0]
        f1x2 = Interpol.pEval([x
                               for x in array(datalists[i+1])[:,0]
                               if x >= xmin[i] and x <= xmax[i]],
                              p_lsqlist1,func)
        f2x2 = Interpol.pEval([x
                               for x in array(datalists[i+1])[:,0]
                               if x >= xmin[i] and x <= xmax[i]],\
                              p_lsqlist2,func)
        shifts.append(mean(f1x2/f2x2))
        corrected.append([coord*array([1,shifts[i+1]])
                          for coord in array(datalists[i+1])])
    return corrected,shifts
Example #28
0
def fit_SaturationFunction(xdata,ydata,p0):
    plsq = leastsq(_residuals_SaturationFunction, p0, args=(xdata,ydata))
    return plsq
Example #29
0
def fit_AOMfunction(xdata,ydata,p0):
    plsq = leastsq(_residuals_AOMfunction, p0, args=(xdata,ydata))
    return plsq
Example #30
0
def FitLorrentz1(x, y, xmin=445, xmax=447, DEBUGFILE=False):

    ii = np.nonzero(np.logical_and(x >= xmin, x <= xmax))[0]
    xm = x
    ym = y
    x = x[ii]
    y = y[ii]
    a0 = np.max(y)
    imx = np.argmax(y)
    x0 = x[imx]
    ilo = imx - 1
    ihi = imx + 1
    #Estimate the FWHM
    while ilo > 1 and y[ilo] > 0.5 * a0:
        ilo = ilo - 1
    while ihi < ii.shape[0] - 1 and y[ihi] > 0.5 * a0:
        ihi = ihi + 1
    gam0 = np.abs(x[ihi] - x[ilo])
    #Estimate the initial background (form <y = m*x + b>)
    m = 0
    b = 0

    #Define the function to minimize via the least-squares method
    def ObjFxn(xf):
        A = np.abs(xf[0])
        x0 = xf[1]
        gam = xf[2]
        m = xf[3]
        b = xf[4]
        return y - A * np.power(gam, 2) / ((np.power(
            (x - x0), 2) + np.power(gam, 2))) - b
        #ne pas mettre A au mumerateur, ca compliaue les calculs

    #Do the fit
    x0 = np.array([a0, x0, gam0, m, b])
    xfit = spop.leastsq(ObjFxn, x0)[0]

    #For debugging purposes, plot each fit individually
    if DEBUGFILE:
        #Define the fit function
        def FitFxn(xf):
            A = np.abs(xf[0])
            x0 = xf[1]
            gam = xf[2]
            m = xf[3]
            b = xf[4]
            return lambda x: A * np.power(gam, 2) / ((np.power(
                (x - x0), 2) + np.power(gam, 2))) + b

        xf = np.linspace(xmin, xmax, 1000)
        fit = FitFxn(xfit)
        plt.plot(xm, ym, 'ko', ms=4, label='Data')
        plt.plot(xm, fit(xm), 'r-', lw=1.5, label='Fit')
        plt.legend(loc='upper left')
        plt.xlabel('wavelength [nm]')
        plt.ylabel('Count rate [1/s]')
        plt.savefig('DEBUGFILE/%s' % (DEBUGFILE))
        plt.clf()

    #Return the fit parameters
    return [xfit[0], xfit[1], fit(xm)]

#Se creó la función R para ajustar los datos obtenidos
def R(x):
    r = np.zeros(N)
    for i in range(0, N):
        r[i] = ydata[i] - (x[0] * np.sin(tdata[i] * x[1] + x[2]) +
                           x[3] * tdata[i])
    return np.transpose(r)


#Se creó el vector de datos iniciales
x0 = np.array([8.98, 0.95, 0.60, 0.30])

#Se aplicó la funcion leastsq para R y datos iniciales x0
xoptimo = leastsq(R, x0)

print(xobjetivo)
print(xoptimo[0])

#Se utilizó solo la primera componente de la salida de la funcion leastsq
xbacan = np.array(xoptimo[0])

#Se creó una función que permitiera el ajuste de datos con lo obtenido en la salida de leastsq
newdata = xbacan[0] * np.sin(xbacan[1] * tdata + xbacan[2]) + tdata * xbacan[3]

#Graficar datos originales vs leastsq
plt.plot(tdata, ydata, color='blue', marker='.', linestyle='', markersize=1)
plt.plot(tdata, newdata, color='red', marker='.', linestyle='', markersize=1)
plt.show()
Example #32
0
    def fit(self, order=(0, 0), start_params=None, method="ls", **optkwds):
        '''
        Estimate lag coefficients of an ARIMA process.

        Parameters
        ----------
        order : sequence
            p,d,q where p is the number of AR lags, d is the number of
            differences to induce stationarity, and q is the number of
            MA lags to estimate.
        method : str {"ls", "ssm"}
            Method of estimation.  LS is conditional least squares.
            SSM is state-space model and the Kalman filter is used to
            maximize the exact likelihood.
        rhoy0, rhoe0 : array_like (optional)
            starting values for estimation

        Returns
        -------
        (rh, cov_x, infodict, mesg, ier) : output of scipy.optimize.leastsq

        rh :
            estimate of lag parameters, concatenated [rhoy, rhoe]
        cov_x :
            unscaled (!) covariance matrix of coefficient estimates
        '''
        if not hasattr(order, '__iter__'):
            raise ValueError("order must be an iterable sequence.  Got type \
%s instead" % type(order))

        p, q = order
        self.nar = p  # needed for geterrors, needs cleanup
        self.nma = q

        ##        if d > 0:
        ##            raise ValueError("Differencing not implemented yet")
        ##            # assume no constant, ie mu = 0
        ##            # unless overwritten then use w_bar for mu
        ##            Y = np.diff(endog, d, axis=0) #TODO: handle lags?

        x = self.endog.squeeze()  # remove the squeeze might be needed later
        #        def errfn( rho):
        #            #rhoy, rhoe = rho
        #            rhoy = np.concatenate(([1], rho[:p]))
        #            rhoe = np.concatenate(([1], rho[p:]))
        #            etahatr = signal.lfilter(rhoy, rhoe, x)
        #            #print rho,np.sum(etahatr*etahatr)
        #            return etahatr

        #replace with start_params
        if start_params is None:
            arcoefs0 = 0.5 * np.ones(p)
            macoefs0 = 0.5 * np.ones(q)
            start_params = np.r_[arcoefs0, macoefs0]

        method = method.lower()

        if method == "ls":
            #update
            optim_kwds = dict(ftol=1e-10, full_output=True)
            optim_kwds.update(optkwds)
            #changes: use self.geterrors  (nobs,):
            #            rh, cov_x, infodict, mesg, ier = \
            #               optimize.leastsq(errfn, np.r_[rhoy0, rhoe0],ftol=1e-10,full_output=True)
            rh, cov_x, infodict, mesg, ier = \
               optimize.leastsq(self.geterrors, start_params, **optim_kwds)
            #TODO: need missing parameter estimates for LS, scale, residual-sdt
            #TODO: integrate this into the MLE.fit framework?
        elif method == "ssm":
            pass
        else:  #this is also conditional least squares
            # fmin_bfgs is slow or does not work yet
            errfnsum = lambda rho: np.sum(self.geterrors(rho)**2)
            #xopt, {fopt, gopt, Hopt, func_calls, grad_calls
            optim_kwds = dict(maxiter=2, full_output=True)
            optim_kwds.update(optkwds)

            rh, fopt, gopt, cov_x, _,_, ier = \
                optimize.fmin_bfgs(errfnsum, start_params, **optim_kwds)
            infodict, mesg = None, None
        self.params = rh
        self.ar_est = np.concatenate(([1], -rh[:p]))
        self.ma_est = np.concatenate(([1], rh[p:p + q]))
        #rh[-q:])) doesnt work for q=0, added p+q as endpoint for safety if var is included
        self.error_estimate = self.geterrors(rh)
        return rh, cov_x, infodict, mesg, ier
def fit(p0,x,y,res):
    p1=leastsq(res, c, args=(x, y))[0]
    return p1
Example #34
0
    given a vector of parameters and volumes, return a vector of energies.
    equation From PRB 28,5480 (1983)
    '''
    E0 = parameters[0]
    B0 = parameters[1]
    BP = parameters[2]
    V0 = parameters[3]
    E = E0 + B0*vol/BP*(((V0/vol)**BP)/(BP-1)+1) - V0*B0/(BP-1.)
    return E
# and we define an objective function that will be minimized
def objective(pars,y,x):
    #we will minimize this function
    err =  y - Murnaghan(pars,x)
    return err
x0 = [e0, b0, bP, v0] #initial guesses in the same order used in the Murnaghan function
murnpars, ier = leastsq(objective, x0, args=(e,v)) #this is from scipy
#now we make a figure summarizing the results
plt.plot(v,e,'ro')
plt.plot(vfit, a*vfit**2 + b*vfit + c,'--',label='parabolic fit')
plt.plot(vfit, Murnaghan(murnpars,vfit), label='Murnaghan fit')
plt.xlabel('Volume ($\AA^3$)')
plt.ylabel('Energy (eV)')
plt.legend(loc='best')
#add some text to the figure in figure coordinates
ax = plt.gca()
plt.text(0.4, 0.5, 'Min volume = {0:1.2f} $\AA^3$'.format(murnpars[3]),
     transform = ax.transAxes)
plt.text(0.4, 0.4, 'Bulk modulus = {0:1.2f} eV/$\AA^3$ = {1:1.2f} GPa'.format(murnpars[1],
                                                                          murnpars[1]*160.21773),
     transform = ax.transAxes)
plt.savefig('images/a-eos.png')
Example #35
0
    def doselfconsistent(self):

        iteration = 0
        u_diff = 1.0
        convergence_threshold = 1e-5
        print("RHF energy =", self.ints.fullEhf)

        while (u_diff > convergence_threshold):

            iteration += 1
            print("DMET iteration", iteration)
            umat_old = np.array(self.umat, copy=True)
            rdm_old = self.transform_ed_1rdm(
            )  # At the very first iteration, this matrix will be zero

            # Find the chemical potential for the correlated impurity problem
            start_ed = time.time()
            if ((self.method == 'CC') and (self.CC_E_TYPE == 'CASCI')):
                self.mu_imp = 0.0
                self.doexact(self.mu_imp)
            elif ((self.method == 'CASSCF') and (self.CC_E_TYPE == 'CASCI')):
                self.mu_imp = 0.0
                self.doexact(self.mu_imp)
            elif ((self.method == 'RHF') and (self.CC_E_TYPE == 'CASCI')):
                self.mu_imp = 0.0
                self.doexact(self.mu_imp)
            else:
                self.mu_imp = optimize.newton(self.numeleccostfunction,
                                              self.mu_imp)
                print("   Chemical potential =", self.mu_imp)
            stop_ed = time.time()
            self.time_ed += (stop_ed - start_ed)
            print("   Energy =", self.energy)
            # self.verify_gradient( self.square2flat( self.umat ) ) # Only works for self.doSCF == False!!
            if (self.SCmethod != 'NONE' and not (self.altcostfunc)):
                self.hessian_eigenvalues(self.square2flat(self.umat))

            # Solve for the u-matrix
            start_cf = time.time()
            if (self.altcostfunc and self.SCmethod == 'BFGS'):
                result = optimize.minimize(
                    self.alt_costfunction,
                    self.square2flat(self.umat),
                    jac=self.alt_costfunction_derivative,
                    options={'disp': False})
                self.umat = self.flat2square(result.x)
            elif (self.SCmethod == 'LSTSQ'):
                result = optimize.leastsq(self.rdm_differences,
                                          self.square2flat(self.umat),
                                          Dfun=self.rdm_differences_derivative,
                                          factor=0.1)
                self.umat = self.flat2square(result[0])
            elif (self.SCmethod == 'BFGS'):
                result = optimize.minimize(self.costfunction,
                                           self.square2flat(self.umat),
                                           jac=self.costfunction_derivative,
                                           options={'disp': False})
                self.umat = self.flat2square(result.x)
            self.umat = self.umat - np.eye(self.umat.shape[0]) * np.average(
                np.diag(
                    self.umat))  # Remove arbitrary chemical potential shifts
            if (self.altcostfunc):
                print("   Cost function after convergence =",
                      self.alt_costfunction(self.square2flat(self.umat)))
            else:
                print("   Cost function after convergence =",
                      self.costfunction(self.square2flat(self.umat)))
            stop_cf = time.time()
            self.time_cf += (stop_cf - start_cf)

            # Possibly print the u-matrix / 1-RDM
            if self.print_u:
                self.print_umat()
            if self.print_rdm:
                self.print_1rdm()

            # Get the error measure
            u_diff = np.linalg.norm(umat_old - self.umat)
            rdm_diff = np.linalg.norm(rdm_old - self.transform_ed_1rdm())
            self.umat = self.relaxation * umat_old + (
                1.0 - self.relaxation) * self.umat
            print("   2-norm of difference old and new u-mat =", u_diff)
            print("   2-norm of difference old and new 1-RDM =", rdm_diff)
            print("******************************************************")

            if (self.SCmethod == 'NONE'):
                u_diff = 0.1 * convergence_threshold  # Do only 1 iteration

        print("Time cf func =", self.time_func)
        print("Time cf grad =", self.time_grad)
        print("Time dmet ed =", self.time_ed)
        print("Time dmet cf =", self.time_cf)

        return self.energy
Example #36
0
# Motore 1
kt = 60.3e-6
rp = 0.75 / 26.17585
N = 1

x = np.loadtxt('MOT')
t = x[200:264, 0]
y = x[200:264, 1]
t = t - t[0]
y = y - y[0]
t = np.arange(0, ts * (len(t)), ts)

U0 = 1500
p0 = [1., 4.]
y1 = y / U0
plsq = leastsq(residuals, p0, args=(y1, t))

M = kt / plsq[0][0]  # Motor Inertia
d = plsq[0][1] * M  # Motor friction

g = tf([kt / M], [1, d / M, 0])  # Transfer function

Y, T = step(g, t)
plot(T, Y)
plot(t, y1)
#show()

# Plant
AP = [[0, 1], [0, -d / M]]
BP = [[0], [kt / M]]
CP = [1, 0]
Example #37
0
def fit_ellipse_amorphous_ring(data,center,fitradii,p0=None,mask=None):
    """
    Fit the amorphous halo of a diffraction pattern, including any elliptical distortion.

    The fit function is::

        f(x,y; I0,I1,sigma0,sigma1,sigma2,c_bkgd,x0,y0,A,B,C) =
            Norm(r; I0,sigma0,0) +
            Norm(r; I1,sigma1,R)*Theta(r-R)
            Norm(r; I1,sigma2,R)*Theta(R-r) + c_bkgd

    where

        * (x,y) are cartesian coordinates,
        * r is the radial coordinate,
        * (I0,I1,sigma0,sigma1,sigma2,c_bkgd,x0,y0,R,B,C) are parameters,
        * Norm(x;I,s,u) is a gaussian in the variable x with maximum amplitude I,
          standard deviation s, and mean u
        * Theta(x) is a Heavyside step function
        * R is the radial center of the double sided gaussian, derived from (A,B,C)
          and set to the mean of the semiaxis lengths

    The function thus contains a pair of gaussian-shaped peaks along the radial
    direction of a polar-elliptical parametrization of a 2D plane. The first gaussian is
    centered at the origin. The second gaussian is centered about some finite R, and is
    'two-faced': it's comprised of two half-gaussians of different standard deviations,
    stitched together at their mean value of R. This Janus (two-faced ;p) gaussian thus
    comprises an elliptical ring with different inner and outer widths.

    The parameters of the fit function are

        * I0: the intensity of the first gaussian function
        * I1: the intensity of the Janus gaussian
        * sigma0: std of first gaussian
        * sigma1: inner std of Janus gaussian
        * sigma2: outer std of Janus gaussian
        * c_bkgd: a constant offset
        * x0,y0: the origin
        * A,B,C: The ellipse parameters, in the form Ax^2 + Bxy + Cy^2 = 1

    Args:
        data (2d array): the data
        center (2-tuple of numbers): the center (x0,y0)
        fitradii (2-tuple of numbers): the inner and outer radii of the fitting annulus
        p0 (11-tuple): initial guess parameters. If p0 is None, the function will compute
            a guess at all parameters. If p0 is a 11-tuple it must be populated by some
            mix of numbers and None; any parameters which are set to None will be guessed
            by the function.  The parameters are the 11 parameters of the fit function
            described above, p0 = (I0,I1,sigma0,sigma1,sigma2,c_bkgd,x0,y0,A,B,C).
            Note that x0,y0 are redundant; their guess values are the x0,y0 values passed
            to the main function, but if they are passed as elements of p0 these will
            take precendence.
        mask (2d array of bools): only fit to datapoints where mask is True

    Returns:
        (2-tuple comprised of a 5-tuple and an 11-tuple): Returns a 2-tuple.

        The first element is the ellipse parameters need to elliptically parametrize
        diffraction space, and is itself a 5-tuple:

            * **x0**: x center
            * **y0**: y center,
            * **a**: the semimajor axis length
            * **b**: the semiminor axis length
            * **theta**: tilt of a-axis w.r.t x-axis, in radians

        The second element is the full set of fit parameters to the double sided gaussian
        function, described above, and is an 11-tuple
    """
    if mask is None:
        mask = np.ones_like(data).astype(bool)
    assert data.shape == mask.shape, "data and mask must have same shapes."
    x0,y0 = center
    ri,ro = fitradii

    # Get data mask
    Nx,Ny = data.shape
    yy,xx = np.meshgrid(np.arange(Ny),np.arange(Nx))
    rr = np.hypot(xx-x0,yy-y0)
    _mask = ((rr>ri)*(rr<ro)).astype(bool)
    _mask *= mask

    # Make coordinates, get data values
    x_inds, y_inds = np.nonzero(_mask)
    vals = data[_mask]

    # Get initial parameter guesses
    I0 = np.max(data)
    I1 = np.max(data*mask)
    sigma0 = ri/2.
    sigma1 = (ro-ri)/4.
    sigma2 = (ro-ri)/4.
    c_bkgd = np.min(data)
    # To guess R, we take a radial integral
    q,radial_profile = radial_integral(data,x0,y0,1)
    R = q[(q>ri)*(q<ro)][np.argmax(radial_profile[(q>ri)*(q<ro)])]
    # Initial guess at A,B,C
    A,B,C = convert_ellipse_params_r(R,R,0)

    # Populate initial parameters
    p0_guess = tuple([I0,I1,sigma0,sigma1,sigma2,c_bkgd,x0,y0,A,B,C])
    if p0 is None:
        _p0 = p0_guess
    else:
        assert len(p0)==11
        _p0 = tuple([p0_guess[i] if p0[i] is None else p0[i] for i in range(len(p0))])

    # Perform fit
    p = leastsq(double_sided_gaussian_fiterr, _p0, args=(x_inds, y_inds, vals))[0]

    # Return
    _x0,_y0 = p[6],p[7]
    _A,_B,_C = p[8],p[9],p[10]
    _a,_b,_theta = convert_ellipse_params(_A,_B,_C)
    return (_x0,_y0,_a,_b,_theta),p
Example #38
0
def fit_test(x, y):
    params = np.zeros(len(name_label_dict))
    wd = 1e-5
    error = lambda p: np.concatenate((Count_soft(x, p) - y, wd * p), axis=None)
    p, success = opt.leastsq(error, params)
    return p
Example #39
0
# initial variables(M, P & Q)
vars = [sum(sales), 0.002, 0.002]


# residual (error) function
def residual(vars, t, sales):
    M = vars[0]
    P = vars[1]
    Q = vars[2]
    Bass = M * (((P + Q)**2 / P) *
                np.exp(-(P + Q) * t)) / (1 + (Q / P) * np.exp(-(P + Q) * t))**2
    return (Bass - (sales))


# non linear least square fitting
varfinal, success = leastsq(residual, vars, args=(t, sales))

# estimated coefficients
m = varfinal[0]
p = varfinal[1]
q = varfinal[2]

print(varfinal)
#sales plot (pdf)
#time interpolation
tp = (np.linspace(1.0, float(len(sales)) * 10, num=len(sales) * 10)) / 10
cofactor = np.exp(-(p + q) * tp)
sales_pdf = m * (((p + q)**2 / p) * cofactor) / (1 + (q / p) * cofactor)**2
plt.plot(tp, sales_pdf, t, sales)
plt.title('Keywords pdf')
plt.legend(['Fit', 'True'])
Example #40
0
xtol = 1e-6

fitArgs = (x, f, pktype, num_pks)

tth = matl.planeData.getTTh() * 180. / np.pi

p0 = np.zeros([num_tth, 4])

for ii in np.arange(num_tth):
    pt = np.argmin(np.abs(x - tth[ii]))

    p0[ii, :] = [f[pt], tth[ii], 0.1, 0.5]

p, outflag = optimize.leastsq(fit_pk_obj_1d_mpeak,
                              p0,
                              args=fitArgs,
                              ftol=ftol,
                              xtol=xtol)

p = np.reshape(p, [num_pks, p.shape[0] / num_pks])
f_fit = np.zeros(len(x))

for ii in np.arange(num_pks):
    f_fit = f_fit + pkfuncs._pvoigt1d_no_bg(p[ii], x)

#plt.plot(x,f,'x')
#plt.hold('true')
#plt.plot(x,f_fit)
ax[1].plot(x, f_fit, 'm+', ms=1)

#%%
    print("Iteration:" , step[0])
    return func(W, x) - y


# In[6]:


W0 = [100, 2]
step = [0]
# 把error 函数中其他参数打包到args中
'''
func:误差函数
x0:表示函数的参数
args()表示数据点
'''
lst = leastsq(error, W0, args=(X, Y, step))
k, b = lst[0]
print("K = ", k, "b = ", b)


# 拟合结果

# In[7]:


X_points = np.linspace(0, 10, 100)
Y_points = k * X_points + b
plt.plot(X, Y, 'bo', label = 'Data with noise')
plt.plot(X_points, Y_points, 'r', label = 'Fitting data')
plt.legend()
def doAll(file_location, filename, function_name="IdealFunc"):
    '''
    Fits, plots, and saves Ster-Gerlac data
    Input: file path, fit function
    Output: graph, final fit parameters, initial guess, covariance matrix, chi2. 
    '''

    function_in_use = FUNCTION_DICT[function_name]
    residual_in_use = RESIDUAL_DICT[function_name]

    print "\n\n -------- Processing file %s" % filename
    x, y = fSG.load_data(file_location, filename)

    #do fit
    pname = ['A', 'Center', 'q', 'D']
    param0 = [10, -0.0005, 4.2, 0.05]  #initial params
    plsq = leastsq(residual_in_use,
                   param0,
                   args=(y, x),
                   maxfev=10000,
                   full_output=True)
    paramFinal, err = plsq[0], plsq[1]

    fit = function_in_use(paramFinal, x)
    initialGuess = function_in_use(param0, x)

    #show results and plot
    fig = pl.figure()
    pl.plot(x, y, 'bo', label=' data')
    pl.plot(x, fit, 'r', label='fit')
    pl.plot(x, initialGuess, 'g', label='initial guess')
    pl.legend()
    pl.xlabel('Distance [mm]')
    pl.ylabel('Voltage [V]')
    pl.title('%s at %s' % (function_name, filename))
    txt_final = 'Final parameters'
    print txt_final, paramFinal
    txt_initial = 'Initial guess'
    print txt_initial, param0
    txt_cov = 'Covariance matrix'
    print txt_cov, err

    chi2 = sum(residual_in_use(paramFinal, y, x)**2)
    txt_chi2 = 'chi2 '
    print 'chi2 ', chi2

    #save graph
    graph_name = "%s-%s.png" % (function_name, filename)
    output_name = os.path.join(file_location, graph_name)
    print "Saving figure to ", output_name
    pl.savefig(output_name)

    #save fit info
    text_output_name = os.path.join(file_location, graph_name[:-3] + "txt")
    print "Saving fit info to ", text_output_name
    out_f = open(text_output_name, "wb")
    out_f.write(graph_name[:-4] + "\n")
    out_f.write(txt_initial + "\n")
    out_f.write(str(param0) + "\n")
    out_f.write(txt_final + "\n")
    out_f.write(str(paramFinal) + "\n")
    out_f.write(txt_cov + "\n")
    out_f.write(str(err) + "\n")
    out_f.write(txt_chi2 + "\n")
    out_f.write(str(chi2) + "\n")
    out_f.write("output of least square fit" + "\n")
    for piece in plsq:
        out_f.write(str(piece) + "\n")
    out_f.close()
Example #43
0
def fitgaussian(y, x=None, guess=None, lsargs={"maxfev": 2000}, minout=None):
    '''
      Fits an N-dimensional Gaussian to (value, coordinate) data.

      Parameters
      ----------
      y : array_like
          Array giving the values of the function.
      x : ndarray, same shape as np.indices(y)
          (optional) An array giving the abcissas of y in the format of
          np.indices().  Must be sorted ascending (which is not checked)
          if guess is not given.  Default: np.indices(y)
      guess : tuple, (width, center, height)
          Tuple giving an initial guess of the Gaussian parameters for
          the optimizer.  If supplied, x and y can be any shape and need
          not be sorted.  See gaussian() for meaning and format of this
          tuple.
      lsargs : dictionary
          Optional arguments to scipy.optimize.leastsq().  Interesting
          ones include maxfev, ftol, xtol, and gtol.  Do not redefine
          full_output, as the code depends on the value it sets for this
          parameter.  By default, maxfev is set to 2000 since the
          default in the minimizer is too low.
      minout : Boolean
          If true, append the minimizer's output tuple to the other outputs.

      Returns
      -------
      width : ndarray, one element per dimension
          The fitted Gaussian widths in each dimension.
      center : ndarray, one element per dimension
          The fitted Gaussian center coordinate in each dimension.
      height : ndarray scalar
          The fitted height.
      err : ndarray, 2 * number-of-dims + 1 elements
          An array containing the concatenated uncertainties for width,
          center, and height, ordered as above.  For example, 2D input gives
          np.array([widthyerr, widthxerr, centeryerr, centerxerr, heighterr]).
      lsout : tuple
          (Optional) Minimizer output, all in one tuple.  Included only
          if minout=True.  See scipy.optimize.leastsq() for
          documentation of its contents.

      Notes
      -----
      If the input does not look anything like a Gaussian, the result
      might not even be the best fit to that.

      Method: First guess the parameters (if no guess is provided), then
      call a Levenberg-Marquardt optimizer to finish the job.

      Examples
      --------

      >>> import matplotlib.pyplot as plt
      >>> import gaussian as g

      >>> # parameters for X
      >>> lx = -3.  # low end of range
      >>> hx = 5.   # high end of range
      >>> dx = 0.05 # step

      >>> # parameters of the noise
      >>> nc = 0.0  # noice center
      >>> ns = 1.0  # noise width
      >>> na = 0.2  # noise amplitude

      >>> # 1D Example

      >>> # parameters of the underlying Gaussian
      >>> wd = 1.1  # width
      >>> ct = 1.2  # center
      >>> ht = 2.2  # height

      >>> # x and y data to fit
      >>> x  = np.arange(lx, hx + dx / 2., dx)
      >>> x +=                             na * np.random.normal(nc, ns, x.size)
      >>> y  = g.gaussian(x, wd, ct, ht) + na * np.random.normal(nc, ns, x.size)
      >>> s  = x.argsort()    # sort, in case noise violated order
      >>> xs = x[s]
      >>> ys = y[s]

      >>> # calculate guess and fit
      >>> (width, center, height)     = g.gaussianguess(ys, xs)
      >>> (fw,    fc,     fh,    err) = g.fitgaussian(ys, xs)

      >>> # plot results
      >>> plt.clf()
      >>> plt.plot(xs, ys)
      >>> plt.plot(xs,      g.gaussian(xs, wd,    ct,     ht))
      >>> plt.plot(xs,      g.gaussian(xs, width, center, height))
      >>> plt.plot(xs,      g.gaussian(xs, fw,    fc,     fh))
      >>> plt.title('Gaussian Data, Guess, and Fit')
      >>> plt.xlabel('Abcissa')
      >>> plt.ylabel('Ordinate')
      >>> # plot residuals
      >>> plt.clf()
      >>> plt.plot(xs, ys - g.gaussian(xs, fw,    fc,     fh))
      >>> plt.title('Gaussian Fit Residuals')
      >>> plt.xlabel('Abcissa')
      >>> plt.ylabel('Ordinate')

      >>> # 2D Example

      >>> # parameters of the underlying Gaussian
      >>> wd = (1.1, 3.2)  # width
      >>> ct = (1.2, 3.1)  # center
      >>> ht = 2.2         # height

      >>> # x and y data to fit
      >>> nx = (hx - lx) / dx + 1
      >>> x  = np.indices((nx, nx)) * dx + lx
      >>> y  = g.gaussian(x, wd, ct, ht) + na * np.random.normal(nc, ns, x.shape[1:])

      >>> # calculate guess and fit
      >>> (width, center, height)     = g.gaussianguess(y, x)
      >>> (fw,    fc,     fh,    err) = g.fitgaussian(y, x)

      >>> # plot results
      >>> plt.clf()
      >>> plt.title('2D Gaussian Given')
      >>> plt.xlabel('X')
      >>> plt.ylabel('Y')
      >>> plt.imshow(    g.gaussian(x, wd,    ct,     ht))
      >>> plt.clf()
      >>> plt.title('2D Gaussian With Noise')
      >>> plt.xlabel('X')
      >>> plt.ylabel('Y')
      >>> plt.imshow(y)
      >>> plt.clf()
      >>> plt.title('2D Gaussian Guessed')
      >>> plt.xlabel('X')
      >>> plt.ylabel('Y')
      >>> plt.imshow(    g.gaussian(x, width, center, height))
      >>> plt.clf()
      >>> plt.title('2D Gaussian Fit')
      >>> plt.xlabel('X')
      >>> plt.ylabel('Y')
      >>> plt.imshow(    g.gaussian(x, fw,    fc,     fh))
      >>> plt.clf()
      >>> plt.title('2D Gaussian Fit Residuals')
      >>> plt.xlabel('X')
      >>> plt.ylabel('Y')
      >>> plt.imshow(y - g.gaussian(x, fw,    fc,     fh))

      >>> # All cases benefit from...

      >>> # show difference between fit and underlying Gaussian
      >>> # Random data, your answers WILL VARY.
      >>> np.array(fw) - np.array(wd)
      array([-0.00015463,  0.01373218])
      >>> np.array(fc) - np.array(ct)
      array([ 0.00061797, -0.00113882])
      >>> np.array(fh) - np.array(ht)
      -0.0026334217737993271

      Revisions
      ---------
      2007-09-17 0.1 [email protected]	Initial version 0.01,
                     portions adapted from
                     http://www.scipy.org/Cookbook/FittingData.
      2007-11-13 0.2 [email protected]    Made N-dimensional.
      2008-12-02 0.3 [email protected] Included error
                     calculation, and return Fixed a bug in which if the
                     initial guess was None, and incorrect shape array
                     was generated. This caused gaussian guess to fail.
      2009-10-25 0.4 [email protected]    Converted to standard doc
                     header, fixed examples to return 4 parameters.
      2009-11-14 0.5 [email protected]    Fixed examples for
                     N-dimensional gaussianguess(), fixed code spacing.
      2010-12-15 0.6 [email protected]    Tested for success in leastsq.
      2011-10-11 0.7 [email protected]    Fixed test for success,
                     added argument passing and full return to/from minimizer.
      2014-10-07 0.8 [email protected]    Convert to Python 3, update examples.
      2014-11-24 0.9 [email protected]    Change 2x np.append into 1x np.hstack.
      2016-10-25 0.11 Changed == to "is" and != to "is not" in None comparisons.
      2017-10-10 0.12 Integer division of pdim to use as index in Python3.
    '''
    if x is None:
        x = np.indices(y.shape)
    else:
        if (((x.ndim == 1) and (x.shape != y.shape))
                or ((x.ndim > 1) and (x.shape[1:] != y.shape))):
            raise ValueError("x must give coordinates of points in y.")

    if guess is None:
        guess = gaussianguess(y, x)

    # "ravel" the guess
    gss = np.hstack((guess[0], guess[1], guess[2]))

    # fit
    residuals = lambda p: np.ravel(gaussian(x, param=p) - y)
    lsout = so.leastsq(residuals, gss, full_output=True, **lsargs)
    p, cov, info, mesg, success = lsout
    if success < 1 \
            or success > 4:
        raise ValueError(mesg + " No convergence.  Guess = " + str(guess) \
                         + "; try a different guess, maxfev, or tolerances.\n See scipy.optimize.leastsq() docs.")
    err = np.sqrt(np.diagonal(cov))
    # unravel the result
    pdim = len(p)
    pdim = (pdim - 1) // 2  # integer division to use as an index
    width = p[:pdim]
    center = p[pdim:2 * pdim]
    height = p[-1]

    ret = (width, center, height, err)

    if minout:
        ret = (width, center, height, err, lsout)

    return ret
Example #44
0
import scipy.optimize as opt


def gauss(x, p):  # p[0]==mean, p[1]==stdev
    return 1.0 / (p[1] * np.sqrt(2 * np.pi)) * np.exp(-(x - p[0])**2 /
                                                      (2 * p[1]**2))


# Create some sample data
known_param = np.array([2.0, .7])
xmin, xmax = -1.0, 5.0
N = 1000
X = np.linspace(xmin, xmax, N)
Y = gauss(X, known_param)

# Add some noise
Y += .10 * np.random.random(N)

# Renormalize to a proper PDF
Y /= ((xmax - xmin) / N) * Y.sum()

# Fit a guassian
p0 = [0, 1]  # Inital guess is a normal distribution
errfunc = lambda p, x, y: gauss(x, p) - y  # Distance to the target function
p1, success = opt.leastsq(errfunc, p0[:], args=(X, Y))

fit_mu, fit_stdev = p1

FWHM = 2 * np.sqrt(2 * np.log(2)) * fit_stdev  # Full width Half Max
print("FWHM = ", FWHM)
Example #45
0
def plotfunction(str):
    #val = '%.2f' % i[j]
    #print '%s' % val
    filename = "ave_%s.plot.err_trim" % i[j]
    x, y, err = np.loadtxt(fname='../%s.xvg' % filename,
                           delimiter=' ',
                           skiprows=4,
                           usecols=(0, 1, 2),
                           unpack=True)
    ## load data in for fitting
    xdata, ydata, yerr = np.loadtxt(fname='../%s.xvg' % filename,
                                    delimiter=' ',
                                    skiprows=4000,
                                    usecols=(0, 1, 2),
                                    unpack=True)
    ##########
    # Fitting the data -- Least Squares Method
    ##########
    # Power-law fitting is best done by first converting
    # to a linear equation and then fitting to a straight line.
    #
    #  y = a * x^b
    #  log(y) = log(a) + b*log(x)
    #
    # adapted from http://scipy-cookbook.readthedocs.io/items/FittingData.html
    powerlaw = lambda x, amp, index: amp * (x**index)
    #yerr = 0
    #yerr = 0.01 * ydata
    logx = np.log10(xdata)
    logy = np.log10(ydata)
    logyerr = yerr / ydata
    #define our (line) fitting function
    #fitfunc = lambda p, x: p[0] + p[1] * x
    #errfunc = lambda p, x, y: (y - fitfunc(p, x))
    #pinit = [1.0, -1.0]
    #out = optimize.leastsq(fitfunc, pinit,
    #                       args=(logx, logy), full_output=1)
    # define our (line) fitting function
    fitfunc = lambda p, x: p[0] + p[1] * x
    errfunc = lambda p, x, y, err: (y - fitfunc(p, x)) / err
    pinit = [1.0, -1.0]
    out = optimize.leastsq(errfunc,
                           pinit,
                           args=(logx, logy, logyerr),
                           full_output=1)
    pfinal = out[0]
    covar = out[1]
    #print pfinal
    #print covar
    index = pfinal[1]
    amp = 10.0**pfinal[0]
    #print index
    #print amp
    indexErr = np.sqrt(covar[0][0])
    #ampErr = np.sqrt( covar[1][1] ) * amp
    ##########
    # Plotting data
    ##########
    #plt.title('%s' % filename)
    plt.plot(x, y, color='%s' % plotStyle[j], marker='.', markersize=2)
    #plt.plot(x, y, color='%s' % plotStyle[j], marker='.', markersize=2)
    #plt.text(5, 6.5, 'a = %5.2f' % index)
    #plt.text(5, 4.5, 'k = %5.2f' % amp)
    #plt.errorbar(x, y, yerr=err, fmt='k.')
    plt.loglog(xdata, powerlaw(xdata, amp, index), 'r-', alpha=0)
    plt.xlabel('Time (ps)')
    plt.ylabel('MSD (nm)')
    plt.xlim(0, 50)
    plt.savefig('All.png')
Example #46
0
    #
    simplex, esow = Monitor(), Monitor()
    solver = fmin(len(point))
    solver.SetInitialPoints(point)
    solver.SetEvaluationMonitor(esow)
    solver.SetGenerationMonitor(simplex)
    solver.Solve(cost_function, CRT())
    sol = solver.Solution()

    print("\nsimplex solution: %s" % sol)
    #
    solcg = fmin_cg(cost_function, point)
    print("\nConjugate-Gradient (Polak Rubiere) : %s" % solcg)
    #
    if leastsq:
        sollsq = leastsq(vec_cost_function, point)
        sollsq = sollsq[0]
        print("\nLeast Squares (Levenberg Marquardt) : %s" % sollsq)
    #
    legend = ['Noisy data', 'Differential Evolution', 'Nelder Mead', 'Polak Ribiere']
    plot_noisy_data()
    plot_sol(desol,'r-')
    plot_sol(sol,'k--')
    plot_sol(solcg,'b-.')
    if leastsq:
        plot_sol(sollsq,'g-.')
        legend += ['Levenberg Marquardt']
    plt.legend(legend) 
    plt.show()

# end of file
Example #47
0
def nls_fit_fwdki(design_matrix,
                  design_matrix_dki,
                  data,
                  S0,
                  params=None,
                  Diso=3e-3,
                  f_transform=True,
                  mdreg=2.7e-3):
    """
    Fit the water elimination DKI model using the non-linear least-squares.

    Parameters
    ----------
    design_matrix : array (g, 22)
        Design matrix holding the covariants used to solve for the regression
        coefficients.
    data : ndarray ([X, Y, Z, ...], g)
        Data or response variables holding the data. Note that the last
        dimension should contain the data. It makes no copies of data.
    S0 : ndarray ([X, Y, Z])
        A first guess of the non-diffusion signal S0. 
    params : ndarray ([X, Y, Z, ...], 28), optional
        A first model parameters guess (3 eigenvalues, 3 coordinates
        of 3 eigenvalues, 15 elements of the kurtosis tensor and the volume
        fraction of the free water compartment). If the initial params are
        not given, for the diffusion and kurtosis tensor parameters, its 
        initial guess is obtain from the standard DKI model, while for the
        free water fraction its value is estimated using the fwDTI model.
        Default: None
    Diso : float, optional
        Value of the free water isotropic diffusion. Default is set to 3e-3
        $mm^{2}.s^{-1}$. Please ajust this value if you are assuming different
        units of diffusion.
    f_transform : bool, optional
        If true, the water volume fractions is converted during the convergence
        procedure to ft = arcsin(2*f - 1) + pi/2, insuring f estimates between
        0 and 1.
        Default: True
    mdreg : float, optimal
        DTI's mean diffusivity regularization threshold. If standard DTI
        diffusion tensor's mean diffusivity is almost near the free water
        diffusion value, the diffusion signal is assumed to be only free water
        diffusion (i.e. volume fraction will be set to 1 and tissue's diffusion
        parameters are set to zero). Default md_reg is 2.7e-3 $mm^{2}.s^{-1}$
        (corresponding to 90% of the free water diffusion value).

    Returns
    -------
    fw_params : ndarray (x, y, z, 28)
        Matrix containing in the dimention the free water model parameters in
        the following order:
            1) Three diffusion tensor's eigenvalues
            2) Three lines of the eigenvector matrix each containing the
               first, second and third coordinates of the eigenvector
            3) Fifteen elements of the kurtosis tensor
            4) The volume fraction of the free water compartment
    S0 : ndarray (x, y, z)
        The models estimate of the non diffusion-weighted signal S0.
    """
    # preparing data and initializing parameters
    data = np.asarray(data)
    data_flat = np.reshape(data, (-1, data.shape[-1]))
    S0out = S0.copy()
    S0out = S0out.ravel()

    # Computing WLS DTI solution for MD regularization
    dtiparams = dti.wls_fit_tensor(design_matrix, data_flat)
    md = dti.mean_diffusivity(dtiparams[..., :3])
    cond = md > mdreg  # removal condition
    data_cond = data_flat[~cond, :]

    # Initializing fw_params according to selected initial guess
    if np.any(params) is None:
        params_out = np.zeros((len(data_flat), 28))
        dkiparams = dki.wls_fit_dki(design_matrix_dki, data_flat)
        fweparams, sd = fwdti.wls_fit_tensor(design_matrix,
                                             data_flat,
                                             S0=S0,
                                             Diso=Diso,
                                             mdreg=2.7e-3)
        params_out[:, 0:27] = dkiparams
        params_out[:, 27] = fweparams[:, 12]
    else:
        params_out = params.copy()
        params_out = np.reshape(params_out, (-1, params_out.shape[-1]))

    params_cond = params_out[~cond, :]
    S0_cond = S0out[~cond]

    for vox in range(data_cond.shape[0]):
        if np.all(data_cond[vox] == 0):
            raise ValueError("The data in this voxel contains only zeros")

        params = params_cond[vox]

        # converting evals and evecs to diffusion tensor elements
        evals = params[:3]
        evecs = params[3:12].reshape((3, 3))
        dt = lower_triangular(vec_val_vect(evecs, evals))
        kt = params[..., 12:27]
        s0 = S0_cond[vox]
        MD = evals.mean()

        # f transformation if requested
        if f_transform:
            f = np.arcsin(2 * params[27] - 1) + np.pi / 2
        else:
            f = params[27]

        # Use the Levenberg-Marquardt algorithm wrapped in opt.leastsq
        start_params = np.concatenate((dt, kt * MD * MD, [np.log(s0), f]),
                                      axis=0)
        this_tensor, status = opt.leastsq(_nls_err_func,
                                          start_params,
                                          args=(design_matrix_dki,
                                                data_cond[vox], Diso,
                                                f_transform))

        # Invert f transformation if this was requested
        if f_transform:
            this_tensor[22] = 0.5 * (1 + np.sin(this_tensor[22] - np.pi / 2))

        # The parameters are the evals and the evecs:
        evals, evecs = decompose_tensor(from_lower_triangular(this_tensor[:6]))
        MD = evals.mean()
        params_cond[vox, :3] = evals
        params_cond[vox, 3:12] = evecs.ravel()
        params_cond[vox, 12:27] = this_tensor[6:21] / (MD**2)
        params_cond[vox, 27] = this_tensor[22]
        S0_cond[vox] = np.exp(-this_tensor[21])

    params_out[~cond, :] = params_cond
    params_out[cond, 27] = 1  # Only free water
    params_out = np.reshape(params_out, (data.shape[:-1]) + (28, ))
    S0out[~cond] = S0_cond
    S0out[cond] = \
        np.mean(data_flat[cond, :] / \
                np.exp(np.dot(design_matrix[..., :6],
                              np.array([Diso, 0, Diso, 0, 0, Diso]))),
                -1)  # Only free water
    S0out = S0out.reshape(data.shape[:-1])
    return params_out, S0out
Example #48
0
    def _clean(self, ar):
        #        plot = self.configs.plot
        #        if plot:
        #            import matplotlib.pyplot as plt
        patient = ar.clone()
        patient.pscrunch()
        patient.remove_baseline()

        # Remove profile from dedispersed data
        patient.dedisperse()
        utils.print_info('Loading template', 2)
        data = patient.get_data().squeeze()
        if self.configs.template is None:
            # Sum over all axes except last, which is phase bins
            template = np.apply_over_axes(np.sum, data,
                                          tuple(range(data.ndim -
                                                      1))).squeeze()
            # smooth data
            template = savgol_filter(template, 5, 1)
        else:
            template_ar = psrchive.Archive_load(self.configs.template)
            template_ar.pscrunch()
            template_ar.remove_baseline()
            template_ar.dedisperse()
            if len(template_ar.get_frequencies()) > 1 and len(
                    template_ar.get_frequencies()) < len(
                        patient.get_frequencies()):
                utils.print_info(
                    "Template channel number doesn't match data... f-scrunching!",
                    2)
                template_ar.fscrunch()
            template_data = template_ar.get_data().squeeze()
            template = np.apply_over_axes(np.sum, template_data,
                                          tuple(range(template_data.ndim -
                                                      1))).squeeze()
            # make sure template is 1D
            if len(np.shape(template)) > 1:  # sum over frequencies too
                template_ar.fscrunch()
                utils.print_info(
                    "2D template found. Assuming it has same frequency coverage and channels as data!",
                    2)
                template_phs = np.apply_over_axes(
                    np.sum, template_data,
                    tuple(range(template_data.ndim - 1))).squeeze()
            else:
                template_phs = template

        utils.print_info('Estimating template and profile phase offset', 2)
        if self.configs.template is None:
            phase_offset = 0
        else:
            # Calculate phase offset of template in number of bins, using full obs
            # Get profile data of full obs
            profile = np.apply_over_axes(np.sum, data,
                                         tuple(range(data.ndim -
                                                     1))).squeeze()
            if np.shape(template_phs) != np.shape(profile):
                utils.print_info(
                    'Template and profile have different numbers of phase bins',
                    2)
            #err = (lambda (amp, phs, base): amp*clean_utils.fft_rotate(template_phs, phs) + base - profile)
            err = (lambda amp_phs: amp_phs[0] * clean_utils.fft_rotate(
                template_phs, amp_phs[1]) - profile)
            amp_guess = np.median(profile) / np.median(template_phs)
            phase_guess = -(np.argmax(profile) - np.argmax(template_phs))
            amp_phs_guess = [amp_guess, phase_guess]
            #params, status = leastsq(err, [amp_guess, phase_guess, np.min(profile) - np.min(template_phs)])
            params, status = leastsq(err, amp_phs_guess)
            phase_offset = params[1]
            utils.print_info(
                'Template phase offset = {0}'.format(round(phase_offset, 3)),
                2)

        utils.print_info('Removing profile from patient', 2)
        #        if plot:
        #            preop_patient = patient.clone()
        #            preop_weights = preop_patient.get_weights()
        clean_utils.remove_profile_inplace(patient, template, phase_offset)

        utils.print_info('Accessing weights and applying to patient', 2)
        # re-set DM to 0
        # patient.dededisperse()

        # Get weights
        weights = patient.get_weights()
        # Get data (select first polarization - recall we already P-scrunched)
        data = patient.get_data()[:, 0, :, :]
        data = clean_utils.apply_weights(data, weights)
        #        if plot:
        #            preop_data = preop_patient.get_data()[:,0,:,:]
        #            preop_patient = []  # clear for the sake of memory
        #            preop_data = clean_utils.apply_weights(preop_data, weights)

        # Mask profiles where weight is 0
        mask_2d = np.bitwise_not(np.expand_dims(weights, 2).astype(bool))
        mask_3d = mask_2d.repeat(ar.get_nbin(), axis=2)
        data = np.ma.masked_array(data, mask=mask_3d)
        #        if plot:
        #            preop_data = np.ma.masked_array(preop_data, mask=mask_3d)

        utils.print_info('Masking on-pulse region as determined from template',
                         2)
        # consider residual only in off-pulse region
        if len(np.shape(template)) > 1:  # sum over frequencies
            utils.print_info(
                'Estimating on-pulse region by f-scrunching 2D template', 2)
            template_ar.fscrunch()
            template_1D = np.apply_over_axes(np.sum, template_ar.get_data(),
                                             (0, 1)).squeeze()
        else:
            template_1D = template
        # Rotate template by apropriate amount
        template_rot = clean_utils.fft_rotate(template_1D,
                                              phase_offset).squeeze()
        # masked_template = np.ma.masked_greater(template_rot, np.min(template_rot) + 0.01*np.ptp(template_rot))
        masked_template = np.ma.masked_greater(template_rot,
                                               np.median(template_rot))
        masked_std = np.ma.std(masked_template)
        # use this std of masked data as cutoff
        masked_template = np.ma.masked_greater(
            template_rot,
            np.median(template_rot) + masked_std)
        #        if plot:
        #            plt.figure(figsize=(10, 5))
        #            plt.subplot(1, 2, 1)
        #            plt.plot(np.apply_over_axes(np.sum, preop_data, tuple(range(data.ndim - 1))).squeeze(), alpha=1)
        #            # Do fit again to scale template
        #            subchan, err, params = clean_utils.remove_profile1d(np.apply_over_axes(np.sum, preop_data, (0, 1)).squeeze(), 0, 0, template_rot, 0, return_params=True)
        #            # plt.plot(params[0]*template_rot + params[1], alpha=0.5)
        #            # plt.plot(params[0]*masked_template + params[1], 'k')
        #            plt.plot(params[0]*template_rot, alpha=0.5)
        #            plt.plot(params[0]*masked_template, 'k')
        #            plt.legend(('Pre-op data', 'Scaled and rotated template', 'Masked template'))
        # Loop through chans and subints to mask on-pulse phase bins
        for ii in range(0, np.shape(data)[0]):
            for jj in range(0, np.shape(data)[1]):
                data.mask[ii, jj, :] = masked_template.mask
        data = np.ma.masked_array(data, mask=data.mask)

        #        if plot:
        #            plt.subplot(1, 2, 2)
        #            plt.plot(np.apply_over_axes(np.ma.sum, data, tuple(range(data.ndim - 1))).squeeze())
        #            plt.title("Residual data")
        #            plt.savefig('data_and_template.png')

        utils.print_info(
            'Calculating robust statistics to determine where RFI removal is required',
            2)
        # RFI-ectomy must be recommended by average of tests
        # BWM: Ok, so this is where the magical stuff actually happens - need to know actually WHAT are the comprehensive stats
        # DJR: At this stage the stats are; (found to work well experimentally)
        #          geometric mean, peak-to-peak, standard deviation, normaltest.
        #      In original coast_guard they were;
        #          mean, peak-to-peak, standard deviation, and max value of FFT
        avg_test_results = clean_utils.comprehensive_stats(data, axis=2, \
                                    chanthresh=self.configs.chanthresh, \
                                    subintthresh=self.configs.subintthresh, \
                                    chan_order=self.configs.chan_order, \
                                    chan_breakpoints=self.configs.chan_breakpoints, \
                                    chan_numpieces=self.configs.chan_numpieces, \
                                    subint_order=self.configs.subint_order, \
                                    subint_breakpoints=self.configs.subint_breakpoints, \
                                    subint_numpieces=self.configs.subint_numpieces, \
                                    )

        utils.print_info('Applying RFI masking weights to archive', 2)
        for (isub, ichan) in np.argwhere(avg_test_results >= 1):
            # Be sure to set weights on the original archive, and
            # not the clone we've been working with.
            integ = ar.get_Integration(int(isub))
            integ.set_weight(int(ichan), 0.0)

        freq_fraczap = clean_utils.freq_fraczap(ar)
Example #49
0
    def residuals(self, data):
        """Determine residuals of data to model.

        For each point the shortest distance to the ellipse is returned.

        Parameters
        ----------
        data : (N, 2) array
            N points with ``(x, y)`` coordinates, respectively.

        Returns
        -------
        residuals : (N, ) array
            Residual for each data point.

        """

        _check_data_dim(data, dim=2)

        xc, yc, a, b, theta = self.params

        ctheta = math.cos(theta)
        stheta = math.sin(theta)

        x = data[:, 0]
        y = data[:, 1]

        N = data.shape[0]

        def fun(t, xi, yi):
            ct = math.cos(t)
            st = math.sin(t)
            xt = xc + a * ctheta * ct - b * stheta * st
            yt = yc + a * stheta * ct + b * ctheta * st
            return (xi - xt)**2 + (yi - yt)**2

        # def Dfun(t, xi, yi):
        #     ct = math.cos(t)
        #     st = math.sin(t)
        #     xt = xc + a * ctheta * ct - b * stheta * st
        #     yt = yc + a * stheta * ct + b * ctheta * st
        #     dfx_t = - 2 * (xi - xt) * (- a * ctheta * st
        #                                - b * stheta * ct)
        #     dfy_t = - 2 * (yi - yt) * (- a * stheta * st
        #                                + b * ctheta * ct)
        #     return [dfx_t + dfy_t]

        residuals = np.empty((N, ), dtype=np.double)

        # initial guess for parameter t of closest point on ellipse
        t0 = np.arctan2(y - yc, x - xc) - theta

        # determine shortest distance to ellipse for each point
        for i in range(N):
            xi = x[i]
            yi = y[i]
            # faster without Dfun, because of the python overhead
            t, _ = optimize.leastsq(fun, t0[i], args=(xi, yi))
            residuals[i] = np.sqrt(fun(t, xi, yi))

        return residuals
Example #50
0
def residuals(p, y, x):
    A,k,theta = p
    err = y-func(x,A,k,theta)
    return err

def peval(x, p):
    A,k,theta = p
    return func(x,A,k,theta)

p0 = [np.max(y_meas), np.pi/11, np.pi/3]
print 'Guess:\t',np.array(p0)
# [  8.      43.4783   1.0472]


plsq = leastsq(residuals, p0, args=(y_meas, x))
print 'Found:\t',plsq[0]
# [ 10.9437  33.3605   0.5834]

print 'Theor:\t',np.array([A, k, theta])
# [ 10.      33.3333   0.5236]

print 'RMSD :\t',rmsd(plsq[0], np.array([A, k, theta]))

plt.plot(x,peval(x,plsq[0]), label='Fit' )
plt.plot(x,y_meas,'x', label='Measured')
plt.plot(x,y_true , label='Theoretical')

plt.title('Least-squares fit to noisy data')
plt.legend()
plt.show()
Example #51
0
def fit_cos_fixed_phase_offset(xdata,ydata,p0):
    plsq,cov,info,mesg,success = leastsq(_residuals_cos_fixed_phase_offset, p0, args=(xdata,ydata), full_output = 1)
    plsq=[plsq]
    dof = len(xdata) - 1
    errors = estimate_errors(plsq, info, cov, dof)
    return plsq, errors
Example #52
0
def bestfit_circle_numpy(points):
    """Fit a circle through a set of points.

    Parameters
    ----------
    points : list
        XYZ coordinates of the points.

    Returns
    -------
    tuple
        XYZ coordinates of the center of the circle, the normal vector of the
        local frame, and the radius of the circle.

    Notes
    -----
    The point of this function is to find the bestfit frame through the given points
    and transform the points to make the problem 2D.

    Once in 2D, the problem simplifies to finding the center point that minimises
    the difference between the resulting circles for all given points, i.e.
    minimise in the least squares sense the deviation between the individual
    radii and the average radius.

    For more information see [1]_.

    References
    ----------
    .. [1] Scipy. *Least squares circle*.
           Available at: http://scipy-cookbook.readthedocs.io/items/Least_Squares_Circle.html.

    Examples
    --------
    >>>

    """
    o, uvw, _ = pca_numpy(points)
    frame = [o, uvw[1], uvw[2]]

    rst = world_to_local_coordinates_numpy(frame, points)

    x = rst[:, 0]
    y = rst[:, 1]

    def dist(xc, yc):
        # compute the radius of the circle through each of the given points
        # for the current centre point
        return sqrt((x - xc) ** 2 + (y - yc) ** 2)

    def f(c):
        # compute the deviation of the radius of each sample point
        # from the average radius
        # => minimize this deviation
        Ri = dist(*c)
        return Ri - Ri.mean()

    xm = mean(x)
    ym = mean(y)
    c0 = xm, ym
    c, error = leastsq(f, c0)

    # compute the radius of the circle through each sample point for the
    # computed center point.
    Ri = dist(*c)

    # compute the radius of the bestfit circle as the average of the individual
    # radii.
    R = Ri.mean()

    # residu = sum((Ri - R) ** 2)
    # print(residu)

    # convert the location of the center point back to global coordinates.
    xyz = local_to_world_coordinates_numpy(frame, [[c[0], c[1], 0.0]])[0]
    return xyz, uvw[2], R
Example #53
0
def fit_integer_phase(ndata, ydata, p0):
    '''
    p = [phase, A, y0]
    '''
    plsq,cov,info,mesg,success = leastsq(_residuals_integer_pahse, p0, args=(ndata,ydata), full_output = 1)
Example #54
0
    def estimate(self, data):
        """Estimate circle model from data using total least squares.

        Parameters
        ----------
        data : (N, 2) array
            N points with ``(x, y)`` coordinates, respectively.

        Returns
        -------
        success : bool
            True, if model estimation succeeds.

        """

        _check_data_dim(data, dim=2)

        x = data[:, 0]
        y = data[:, 1]

        N = data.shape[0]

        # pre-allocate jacobian for all iterations
        A = np.zeros((N + 5, 2 * N), dtype=np.double)
        # same for all iterations: xc, yc
        A[0, :N] = -1
        A[1, N:] = -1

        diag_idxs = np.diag_indices(N)

        def fun(params):
            xyt = self.predict_xy(params[5:], params[:5])
            fx = x - xyt[:, 0]
            fy = y - xyt[:, 1]
            return np.append(fx, fy)

        def Dfun(params):
            xc, yc, a, b, theta = params[:5]
            t = params[5:]

            ct = np.cos(t)
            st = np.sin(t)
            ctheta = math.cos(theta)
            stheta = math.sin(theta)

            # derivatives for fx, fy in the following order:
            #       xc, yc, a, b, theta, t_i

            # fx
            A[2, :N] = -ctheta * ct
            A[3, :N] = stheta * st
            A[4, :N] = a * stheta * ct + b * ctheta * st
            A[5:, :N][diag_idxs] = a * ctheta * st + b * stheta * ct
            # fy
            A[2, N:] = -stheta * ct
            A[3, N:] = -ctheta * st
            A[4, N:] = -a * ctheta * ct + b * stheta * st
            A[5:, N:][diag_idxs] = a * stheta * st - b * ctheta * ct

            return A

        # initial guess of parameters using a circle model
        params0 = np.empty((N + 5, ), dtype=np.double)
        xc0 = x.mean()
        yc0 = y.mean()
        r0 = np.sqrt((x - xc0)**2 + (y - yc0)**2).mean()
        params0[:5] = (xc0, yc0, r0, 0, 0)
        params0[5:] = np.arctan2(y - yc0, x - xc0)

        params, _ = optimize.leastsq(fun, params0, Dfun=Dfun, col_deriv=True)

        self.params = params[:5]

        return True