def fitLorentz(self,x,y): if x[0] / 1e9 > 1: raise 'I hate the large number, please divided by 1e9, processing x in GHz' para = self.guessLorentz(x,y) # mybounds = MyBounds(xmin=[-np.inf,-np.inf,-np.inf,-np.inf,0,0],xmax=[np.inf,np.inf,np.inf,np.inf,1.5*w,2*np.pi]) res = bh(self.errLorentz,para,niter=20,minimizer_kwargs={"method":"Nelder-Mead","args":(x, y)}) # res = ls(self.errLorentz,para,args=(x,y)) a,b,c,d = res.x return res,np.sqrt(np.abs(1/c))*2e3
def fitCavity(self,x,y): f, s = self.deductPhase(x,y) s = s[0]/np.max(np.abs(s[0])) f0, Qi, Qe, phi, QL = self.guessParams(f,s) res = bh(self.err,(f0, Qi, Qe, phi),niter = 100,\ minimizer_kwargs={"method":"Nelder-Mead","args":(f, 1/s)}) f0, Qi, Qe, phi = res.x QL = 1 / (1 / Qi + 1 / Qe) return f0, Qi, Qe, QL, phi, f, s
def fitTwoexp(self,x,y): p0 = self.guess(x,y) a, b, c, d, e = p0 lower = [0.95*i if i > 0 else 1.05*i for i in p0] higher = [1.05*i if i > 0 else 0.95*i for i in p0] lower[2], lower[3] = -np.abs(a)*self.percent, -np.abs(b)*self.percent higher[2], higher[3] = self.percent*np.abs(a), self.percent*np.abs(b) print(p0) # res = ls(self.err,p0,args=(x,y),bounds=(lower,higher)) res = bh(self.err,p0,niter = 10,minimizer_kwargs={"method":"Nelder-Mead","args":(x, y)}) return res.x
def fitCos(self,volt,s): x, y = volt, s if x[0] / 1e9 > 1: raise 'I hate the large number, please divided by 1e9, processing x in GHz' Ag, Cg, Wg, phig = self.guessCos(x,y) p0 = Ag, Cg, Wg, phig # print(Ag, Cg, Wg, phig) # res = ls(self.errCos, [Ag,Cg,Wg,phig], args=(x, y)) mybounds = MyBounds(xmin=[-np.inf,-np.inf,0,-np.pi],xmax=[np.inf,np.inf,1.5*Wg,np.pi]) res = bh(self.errCos,p0,niter=80,minimizer_kwargs={"method":"Nelder-Mead","args":(x, y)},accept_test=mybounds) A, C, W, phi = res.x return res, self.func
def fitTwoexp(self,x,y,num=2): paras = np.zeros((2*num+1,)) xmin, xmax = np.zeros((2*num+1,)), np.zeros((2*num+1,)) p0 = self.guess(x,y,paras) xmin[0], xmax[0] = p0[0]*0.5, p0[0]*1.5 xmin[1::2], xmin[2::2] = p0[1::2]*0.5, -(np.max(x)-np.min(x))*2 xmax[1::2], xmax[2::2] = p0[1::2]*1.5, (np.max(x)-np.min(x))*2 mybounds = MyBounds(xmin=xmin,xmax=xmax) res = bh(self.err,p0,niter = 50,minimizer_kwargs={"method":"Nelder-Mead","args":(x, y)}) # res = bh(self.err,res.x,niter = 50,minimizer_kwargs={"method":"Nelder-Mead","args":(x, y)},accept_test=mybounds) p0 = res.x print(xmin) # res = ls(self.err, p0, args=(np.array(x), np.array(y))) return res.x
def fitRabi(self,x,y): if self.envelopemethod == 'hilbert': out = self.envelope_Hilbert(y) else: out = self.envelope(y) A,B,T1,w,phi = self.guessRabi(x,out,y) env = (A,B,T1,out) amp = (np.max(y)-np.min(y)) / 2 A = A if np.abs(A-amp) < 0.1*amp else amp B = B if np.abs(B-np.mean(y)) < 0.1*np.mean(y) else np.mean(y) p0 = A,B,T1,w,self.phi print(p0) # res = ls(self.errRabi, p0, args=(np.array(x), np.array(y))) res = bh(self.errRabi,p0,niter = 10,minimizer_kwargs={"method":"L-BFGS-B","args":(x, y)}) A,B,T1,w,phi = res.x return A,B,T1,w,phi,env
def fitCavitymodulation(self,v,f,s,classify=False): v,f = self.manipulation(v,f,s) paras, func = self.fitCos(v,f) A, C, W, phi = paras.x voffset, vperiod, ec, d= self.firstMax(v,f,num=0), 1/W, 0.1*np.min(f), 1 # g = np.min(f)-fc ejs = (np.max(f)+ec)**2/8/ec g, fc = ec, np.mean(f) p0 = [voffset, vperiod, ejs, ec, d, g, fc] print(p0) mybounds = MyBounds(xmin=[-0.25*vperiod,0,0,0,0,0,0],xmax=[0.25*vperiod,1.5*vperiod,2*ejs,2*ec,2,2*g,2*fc]) res = bh(self.err,p0,niter = 200,minimizer_kwargs={"method":"Nelder-Mead","args":(v, f)},accept_test=mybounds) # res = ls(self.err,res.x,args=(v, f)) # A, C, W, phi = res.x voffset, vperiod, ejs, ec, d, g, fc = res.x return f, v, res, self.func
def fitRabi(self,x,y): if self.envelopemethod == 'hilbert': out = self.envelope_Hilbert(y) else: out = self.envelope(y) A,B,T1,w,phi = self.guessRabi(x,out,y) env = (A,B,T1,out) amp = (np.max(y)-np.min(y)) / 2 A = A if np.abs(A-amp) < 0.1*amp else amp B = B if np.abs(B-np.mean(y)) < 0.1*np.mean(y) else np.mean(y) p0 = A,B,T1,w,self.phi print(p0) # res = ls(self.errRabi, p0, args=(np.array(x), np.array(y))) mybounds = MyBounds(xmin=[0,-np.inf,100,0,0],xmax=[np.inf,np.inf,100e3,1.5*w,2*np.pi]) res = bh(self.errRabi,p0,niter=30,minimizer_kwargs={"method":"Nelder-Mead","args":(x, y)},accept_test=mybounds) A,B,T1,w,phi = res.x return A,B,T1,w,phi,env
def fitT2(self,x,y): ''' 几个参数的限制范围还需要考究,A,T1,T2 ''' d = self.space(y) if self.envelopemethod == 'hilbert': out = self.envelope_Hilbert(y) else: out = self.envelope(y) A,B,T1,T2,w,phi = self.guessT2(x,out,y) env = A,B,T1,T2,out if T2 > 0.8*x[d-1] and d < 0.8*len(y): T2 = 0.37*x[d-1] amp = (np.max(y)-np.min(y)) / 2 A = A if np.abs(A-amp) < 0.1*amp else amp p0 = A,B,T1,T2,w,self.phi print(p0) # res = ls(self.errT2, p0, args=(x, y)) res = bh(self.errT2,p0,niter = 10,minimizer_kwargs={"method":"L-BFGS-B","args":(x, y)}) A,B,T1,T2,w,phi = res.x return A,B,T1,T2,w,phi,env
def fitT2(self,x,y): ''' 几个参数的限制范围还需要考究,A,T1,T2 ''' d = self.space(y) if self.envelopemethod == 'hilbert': out = self.envelope_Hilbert(y) else: out = self.envelope(y) A,B,T1,T2,w,phi = self.guessT2(x,out,y) env = A,B,T1,T2,out if T2 > 0.8*x[d-1] and d < 0.8*len(y): T2 = 0.37*x[d-1] amp = (np.max(y)-np.min(y)) / 2 A = A if np.abs(A-amp) < 0.1*amp else amp p0 = A,B,T1,T2,w,self.phi print(p0) # res = ls(self.errT2, p0, args=(x, y)) mybounds = MyBounds(xmin=[0,-np.inf,100,10,0,-np.pi],xmax=[np.inf,np.inf,100000,100000,1.5*w,np.pi]) res = bh(self.errT2,p0,niter = 80,minimizer_kwargs={"method":"Nelder-Mead","args":(x, y)},accept_test=mybounds) A,B,T1,T2,w,phi = res.x return res, self.func
def fitSpec2d(self,v,f,s=None,classify=False): if s is not None: v,f = self.profile(v,f,s,classify) paras, func = self.fitCos(v,f) A, C, W, phi = paras.x voffset, vperiod, ec, d = self.firstMax(v,f,num=0), 1/W, 0.2, 0 ejs = (np.max(f)+ec)**2/8/ec p0 = [voffset, vperiod,ejs,ec,d] while 1: # print(p0) mybounds = MyBounds(xmin=[0.5*voffset,0,0,0,0],xmax=[1.5*voffset,1.5*vperiod,2*ejs,2*ec,10]) res = bh(self.err,p0,niter = 200,minimizer_kwargs={"method":"Nelder-Mead","args":(v, f)},accept_test=mybounds) res = ls(self.err,res.x,args=(v, f)) voffset, vperiod, ejs, ec, d = res.x space = self.errspace(self.f01,res.x,{'x':v,'y':f}) if np.max(space) > 0.001: v = v[space<0.001] f = f[space<0.001] p0 = res.x # print(len(v),(space<0.001)) else: return f, v, voffset, vperiod, ejs, ec, d
def fitExp(self,x,y): p0 = self.guessExp(x,y) # res = ls(self.errExp, p0, args=(x, y)) res = bh(self.errExp,p0,niter = 10,minimizer_kwargs={"method":"L-BFGS-B","args":(x, y)}) return res.x
def fitVcrabi(self,x,y): p0 = self.guess(x,y) mybounds = MyBounds(xmin=[0,0,-1.1],xmax=[0.1,np.inf,1.1]) res = bh(self.err,p0,niter=50,minimizer_kwargs={"method":"Nelder-Mead","args":(x, y)},accept_test=mybounds) return res.x
def fitErf(self,x,y): paras = self.guess(x,y) # print(paras) res = bh(self.err,paras,niter = 50,minimizer_kwargs={"method":"Nelder-Mead","args":(x, y)}) return res, self.func
def fitExp(self,x,y): p0 = self.guessExp(x,y) # res = ls(self.errExp, p0, args=(x, y)) res = bh(self.errExp,p0,niter = 50,minimizer_kwargs={"method":"Nelder-Mead","args":(x, y)}) return res.x