def setonepar(self,pname,datlist,**kwargs): #print self[pname] inactivecount=0 for j,i in enumerate(self[pname]): if self[pname][j]['fitactive'] == 1: if pname == 'dw': self[pname][j]['par']=[datlist[j-inactivecount]]#(self['dwsign'][j]['par']*np.random.random(numb))*(bdscoll[1]-bdscoll[0])+bdscoll[0]*self['dwsign'][j]['par'] self['dwsign'][j]['par']=0 else: self[pname][j]['par']=[datlist[j-inactivecount]] if len(np.shape(self[pname][j]['bounds'])) == 1 or len(self[pname][j]['bounds']) != len(self[pname][j]['par']): bndsx=self[pname][j]['bounds'] if len(np.shape(self[pname][j]['bounds'])) == 1 else self[pname][j]['bounds'][0] self[pname][j]['bounds']=[bndsx for i in np.arange(len(self[pname][j]['par']))] if 'bounds' in kwargs.keys(): self[pname][j]['bounds']=kwargs['bounds'] if 'bndtol' in kwargs.keys(): self[pname][j]['bndtol']=kwargs['bndtol'] if 'resetbounds' in kwargs.keys(): self[pname][j]['bounds']=[] # print self[pname][j]['bounds'], 'ye now1',self[pname][j]['parbds'] self.generatemissing(pname) # print self[pname][j]['bounds'], 'ye now2',self[pname][j]['parbds'] if 'resetparbounds' in kwargs.keys(): #resetparbounds self[pname][j]['parbds']=self[pname][j]['bounds'] else: inactivecount+=1
def randomizepar(self,pname,numb): for j,i in enumerate(self[pname]): bdscoll=i['parbds'] #print bdscoll, j, pname if pname == 'dwsign': self[pname][j]['par']=np.random.choice(bdscoll,numb)#np.array([np.random.choice(bdscoll) for i in np.arange(numb)]) elif pname == 'dw': #print np.random.random(numb)*(bdscoll[1]-bdscoll[0])+bdscoll[0], '!!!!', bdscoll[1]-bdscoll[0], bdscoll[0] # print #rnddat=np.random.random(numb)* #print rnddat, self['dwsign'][j]['par'],bdscoll[0] self[pname][j]['par']=(self['dwsign'][j]['par']*np.random.random(numb))*(bdscoll[1]-bdscoll[0])+bdscoll[0]*self['dwsign'][j]['par'] else: self[pname][j]['par']=(np.random.random(numb)*(bdscoll[1]-bdscoll[0])+bdscoll[0]) if len(np.shape(self[pname][j]['bounds'])) == 1 or len(self[pname][j]['bounds']) != len(self[pname][j]['par']): bndsx=self[pname][j]['bounds'] if len(np.shape(self[pname][j]['bounds'])) == 1 else self[pname][j]['bounds'][0] self[pname][j]['bounds']=[bndsx for i in np.arange(len(self[pname][j]['par']))] try: self.generatemissing(pname) except: pass
def approxdefs(**kwargs): """general function to calculate a number of expressions used to get approximations and exact solutions. This function frequently calls itself. Pre-calculated symbolic solutions are also generated from expressions contained herein. However, the function most likely directly used by the user will be nEVapprox. The parameter "calctype" determines which type of calculation is performed. """ verb = 'quiet' #Parse arguments (parameters). Strings in the symbols list (sparlist) #will either take a value or become a symbol within the prm dictionary. #Strings in the other list (oparlist) will either take the value (from #kwargs or be set to 0: sparlist=['pa','pb','pc','pd','t','tt','k12','k13','k14','k23','k24','k34'\ ,'dwa','dwb','dwc','dwd','w1','deltao'] oparlist = [ 'calctype', 'exporder', 'mode', 'pade', 'sc', 'nDV', 'nhmatnewsc' ] prm = {} for x in sparlist + oparlist: if x in kwargs: prm[x] = kwargs[x] elif x in sparlist: prm[x] = smp.Symbol(x) else: prm[x] = 0 #We prefer variables (corresponding to values or symbols) #over bulky dictionary entries, which is why all dictionary entries are #assigned to corresponding values: pa = prm['pa'] pb = prm['pb'] pc = prm['pc'] pd = prm['pd'] t = prm['t'] tt=prm\ ['tt'] k12 = prm['k12'] k13 = prm['k13'] k14 = prm['k14'] k23 = prm['k23'] \ k24=prm['k24'] k34 = prm['k34'] dwb = prm['dwb'] dwc = prm['dwc'] dwd=prm\ ['dwd'] calctype = prm['calctype'] exporder = prm['exporder'] mode=prm\ ['mode'] pade = prm['pade'] sc = prm['sc'] nDV = prm['nDV'] dwa = prm['dwa'] \ w1=prm['w1'] deltao = prm['deltao'] #If verb is set to 'ose', then the calculation type is printed. if verb == 'ose': print 'calc ' + calctype + '... ', #calculation of the chemical shift matrix - including the exponential: #calculation of the chemical shift matrix - not including the #exponential. Similar to nAmatD. Only works in numerical mode. elif calctype == 'nAmatDnonex': if mode == 2: return np.matrix([[0,0,0,0],[0,-1j*dwb,0,0],[0,0,-1j*dwc,0],\ [0,0,0,-1j*dwd]]) if anyisnotsymbol([k14,k24,k34],0) else \ (np.matrix([[0,0,0],[0,-1j*dwb,0],[0,0,-1j*dwc]]) if \ anyisnotsymbol([k23,k13],0) else np.matrix([[0,0],\ [0,-1j*dwb]])) elif calctype == 'nAmatP': if mode == 1: [k13,k23,k14,k24,k34]=[k13,0,0,0,k34] if sc == 0 else \ ([k13,0,k14,0,0] if sc == 1 else ([k13,0,k14,0,k34] if sc == 2 \ else ([k13,0,0,k24,k34] if sc == 3 else ([0,k23,0,0,0] if sc == 4 \ else ([k13,0,0,0,0] if sc == 5 else ([k13,k23,0,0,0] if sc == 6 \ else [0,0,0,0,0])))))) pl = [k12, k13, k23, k14, k24, k34, pb, pc, pd] if ((anyisnotsymbol([k14, k24, k34], 3) or anyissymbol([k14, k24, k34]))): kineticmat=[[-kv('12',pl)-kv('13',pl)-kv('14',pl),kv('21',pl),\ kv('31',pl),kv('41',pl)],[kv('12',pl),-kv('21',pl)-kv('23',pl)\ -kv('24',pl),kv('32',pl),kv('42',pl)],[kv('13',pl),kv('23',pl),\ -kv('31',pl)-kv('32',pl)-kv('34',pl),kv('43',pl)],[kv('14',pl),\ kv('24',pl),kv('34',pl),-kv('41',pl)-kv('42',pl)-kv('43',pl)]] elif ((anyisnotsymbol([k13, k23], 3) or anyissymbol([k13, k23]))): kineticmat=[[-kv('12',pl)-kv('13',pl),kv('21',pl),kv('31',pl)],\ [kv('12',pl),-kv('21',pl)-kv('23',pl),kv('32',pl)],[kv('13',pl),\ kv('23',pl),-kv('31',pl)-kv('32',pl)]] else: kineticmat = [[-kv('12', pl), kv('21', pl)], [kv('12', pl), -kv('21', pl)]] if mode == 2: return np.matrix(kineticmat) else: return smp.Matrix(kineticmat) ##approxdefs(calctype='r1rLKmat',mode=mode,k12=k12x,k13=k13x,k14=k14x,k24=k24x,k23=k23x,k34=k34x,pb=pbx,pc=pcx,pd=pdx,deltao=deltaox,w1=w1x,dwb=dwbx,dwc=dwcx,dwd=dwdx) #elif calctype == 'cpmg1': # return np.dot(np.dot(scplin.expm(approxdefs(t=t,dwb=dwb,calctype='nAmat',mode=2)*t),scplin.expm(approxdefs(t=t,dwb=dwb,calctype='nAstar')*t*2)),scplin.expm(approxdefs(t=t,dwb=dwb,calctype='nAmat')*t)) #calculates the exact solution for the exponential matrix term elif calctype == 'cpmg2': part1=approxdefs(calctype='nAmatP',mode=2,k12=k12,k13=k13,k14=k14,k24=\ k24,k23=k23,k34=k34,pb=pb,pc=pc,pd=pd) part2=approxdefs(calctype='nAmatDnonex',mode=2,k12=k12,k13=k13,k14=k14\ ,k24=k24,k23=k23,k34=k34,dwb=dwb,dwc=dwc,dwd=dwd) return np.dot(np.dot(scplin.expm((part1+part2)*t),\ scplin.expm((part1-part2)*2*t)),\ scplin.expm((part1+part2)*t)) #elif calctype == 'cpmg2old': # return np.dot(scplin.expm((approxdefs(calctype='nAmatP',mode=2)+approxdefs(calctype='nAmatDnonex',mode=2))*2*t),scplin.expm((approxdefs(calctype='nAmatP',mode=2)-approxdefs(calctype='nAmatDnonex',mode=2))*2*t)) #For calculations of Hmat elif calctype == 'nHmatpro': #calculations which are based on calculations of the #exact exponential matrix. (all ExpExact) if mode == 2 and (exporder == 0 or exporder == 10): cpmg2mat=approxdefs(mode=2,calctype='cpmg2',t=t,\ k12=k12,k13=k13,k14=k14,k24=k24,k23=k23,k34=k34,dwb=dwb\ ,dwc=dwc,dwd=dwd,pb=pb,pc=pc,pd=pd) if pade == 0: #exact logarithm return scplin.logm(cpmg2mat) / (t * 4) elif pade == 1: #Pade 1,0 return (-np.identity(np.shape(cpmg2mat)[0]) + cpmg2mat) / (t * 4) if pade == 2 or pade == 3: xmt=-approxdefs(mode=2,pade=1,calctype='nHmatpro',t=t,\ exporder=exporder,k12=k12,k13=k13,k14=k14,k24=k24,\ k23=k23,k34=k34,dwb=dwb,dwc=dwc,dwd=dwd,pb=pb,pc=pc,\ pd=pd)*(4*t) if pade == 2: #Pade 2,2 return -(np.dot(np.linalg.inv(6*np.identity(np.shape(xmt)\ [0])-6*xmt+np.dot(xmt,xmt)),(6*xmt-np.dot(3*xmt,xmt\ ))))/(4*t) elif pade == 3: #Pade 3,3 return -(np.dot(np.linalg.inv(60*np.identity(np.shape(xmt)[0])\ -90*xmt+np.dot(36*xmt,xmt)-np.dot(3*xmt,np.dot(xmt,\ xmt))),(60*xmt-np.dot(60*xmt,xmt)+np.dot(11*xmt,\ np.dot(xmt,xmt)))))/(4*t) elif calctype == 'r1rBigL2': # print w1, '!!!!' Lmat = [[[0, -x, 0], [x, 0, -w1], [0, w1, 0]] for x in [dwa, dwb, dwc, dwd]] idm = np.matrix(np.identity(3)) if mode == 2: dimen = 4 if anyisnotsymbol([k14, k24, k34],0) else (3 if \ anyisnotsymbol([k13,k23],0) else 2) else: dimen = 4 if sc < 4 else (3 if sc < 7 else 2) if dimen == 4: BigL=np.reshape(np.array([np.transpose(np.reshape(np.array(\ [Lmat[0],0*idm,0*idm,0*idm]),(12,3))),np.transpose(np.reshape(\ np.array([0*idm,Lmat[1],0*idm,0*idm]),(12,3))),np.transpose(\ np.reshape(np.array([0*idm,0*idm,Lmat[2],0*idm]),(12,3))),np.\ transpose(np.reshape(np.array([0*idm,0*idm,0*idm,Lmat[3]]),\ (12,3)))]),(12,12),order='A') elif dimen == 3: BigL=np.reshape(np.array([np.transpose(np.reshape(np.array(\ [Lmat[0],0*idm,0*idm]),(9,3))),np.transpose(np.reshape(\ np.array([0*idm,Lmat[1],0*idm]),(9,3))),np.transpose(\ np.reshape(np.array([0*idm,0*idm,Lmat[2]]),(9,3)))]),\ (9,9),order='A') else: BigL=np.reshape(np.array([np.transpose(np.reshape(np.array(\ [Lmat[0],0*idm]),(6,3))),np.transpose(np.reshape(\ np.array([0*idm,Lmat[1]]),(6,3)))]),\ (6,6),order='A') if mode == 2: return np.matrix(BigL) else: return smp.Matrix(BigL) elif calctype == 'nAmatP2': if mode == 1: [k13,k23,k14,k24,k34]=[k13,0,0,0,k34] if sc == 0 else \ ([k13,0,k14,0,0] if sc == 1 else ([k13,0,k14,0,k34] if sc == 2 \ else ([k13,0,0,k24,k34] if sc == 3 else ([0,k23,0,0,0] if sc == 4 \ else ([k13,0,0,0,0] if sc == 5 else ([k13,k23,0,0,0] if sc == 6 \ else [0,0,0,0,0])))))) pl = [k12, k13, k23, k14, k24, k34, pb, pc, pd] if ((anyisnotsymbol([k14, k24, k34], 3) or anyissymbol([k14, k24, k34]))): kineticmat=[[-kv('12',pl)-kv('13',pl)-kv('14',pl),kv('21',pl),\ kv('31',pl),kv('41',pl)],[kv('12',pl),-kv('21',pl)-kv('23',pl)\ -kv('24',pl),kv('32',pl),kv('42',pl)],[kv('13',pl),kv('23',pl),\ -kv('31',pl)-kv('32',pl)-kv('34',pl),kv('43',pl)],[kv('14',pl),\ kv('24',pl),kv('34',pl),-kv('41',pl)-kv('42',pl)-kv('43',pl)]] elif ((anyisnotsymbol([k13, k23], 3) or anyissymbol([k13, k23]))): kineticmat=[[-kv('12',pl)-kv('13',pl),kv('21',pl),kv('31',pl)],\ [kv('12',pl),-kv('21',pl)-kv('23',pl),kv('32',pl)],[kv('13',pl),\ kv('23',pl),-kv('31',pl)-kv('32',pl)]] else: kineticmat = [[-kv('12', pl), kv('21', pl)], [kv('12', pl), -kv('21', pl)]] if mode == 2: return np.matrix(kineticmat) else: return smp.Matrix(kineticmat) elif calctype == 'r1rBigK2': ktest=np.array(approxdefs(calctype='nAmatP2',mode=mode,k12=k12,k13=k13,k14=k14,k24=\ k24,k23=k23,k34=k34,pb=pb,pc=pc,pd=pd,sc=sc)) if mode == 1: ident = smp.eye(3) z = smp.zeros(np.shape(ktest)[0] * 3) else: ident = np.identity(3) z = np.zeros((np.shape(ktest)[0] * 3, np.shape(ktest)[0] * 3)) i = 0 for x in ktest: j = 0 for y in x: z[i:i + 3, j:j + 3] = y * ident j += 3 i += 3 if mode == 2: return np.matrix(z) else: return smp.Matrix(z) elif calctype == 'r1rLKmat2': return approxdefs(calctype='r1rBigK2',mode=mode,k12=k12,k13=k13,k14=k14\ ,k24=k24,k23=k23,k34=k34,pb=pb,pc=pc,pd=pd,sc=sc)+approxdefs(\ calctype='r1rBigL2',mode=mode,sc=sc,k12=k12,k13=k13,k14=k14,k24=\ k24,k23=k23,k34=k34,pb=pb,pc=pc,pd=pd,dwa=-(deltao-pb*dwb-pc*dwc-\ pd*dwd),dwb=dwb-(deltao-pb*dwb-pc*dwc-pd*dwd),dwc=dwc-(deltao-\ pb*dwb-pc*dwc-pd*dwd),dwd=dwd-(deltao-pb*dwb-pc*dwc-pd*dwd),w1=w1) elif calctype == 'sinsqth2': if sc < 4: return (w1**2 / (w1**2 + ( (1 - pb - pc - pd) * dwa + pb * dwb + pc * dwc + pd * dwd)**2)) elif sc < 7: return (w1**2 / (w1**2 + ((1 - pb - pc) * dwa + pb * dwb + pc * dwc)**2)) else: #print w1, pb, dwa, dwb return (w1**2 / (w1**2 + ((1 - pb) * dwa + pb * dwb)**2)) elif calctype == 'r1rex': LKM=approxdefs(calctype='r1rLKmat2',mode=mode,k12=k12,k13=k13,\ k14=k14,k24=k24,k23=k23,k34=k34,pb=pb,pc=pc,\ pd=pd,deltao=deltao,w1=w1,dwa=-(deltao-pb*dwb-pc*dwc-\ pd*dwd),dwb=dwb-(deltao-pb*dwb-pc*dwc-pd*dwd),dwc=dwc-(deltao-\ pb*dwb-pc*dwc-pd*dwd),dwd=dwd-(deltao-pb*dwb-pc*dwc-pd*dwd),sc=sc) sinsqt=approxdefs(calctype='sinsqth2',mode=mode,k12=k12,k13=k13,\ k14=k14,k24=k24,k23=k23,k34=k34,pb=pb,pc=pc,\ pd=pd,deltao=deltao,w1=w1,dwa=-(deltao-pb*dwb-pc*dwc-\ pd*dwd),dwb=dwb-(deltao-pb*dwb-pc*dwc-pd*dwd),dwc=dwc-(deltao-\ pb*dwb-pc*dwc-pd*dwd),dwd=dwd-(deltao-pb*dwb-pc*dwc-pd*dwd),sc=sc) if mode == 2: return np.real( 1 / np.max(-1 / np.linalg.eigvals(LKM))).item() / sinsqt
def nEVapprox(*args, **kwargs): """Main function to generate exact solutions and approximations, except Carver-Richards. These arguments have to be supplied: t,dwb,pb,k12,mode,exporder,pade,evap,dwc,pc,k13,k23,dwd,pd,k14,k24,k34. t - tau; dwb, dwc, dwd - shifts of minor state peaks with respect to main peak, in s-1; pb, pc, pd: minor site populations; k12, k13, k23, k14, k24, k34 - exchange rate constants. For example, "k12" is actually corresponding to k12+k21. in s-1. mode=1: returns symbolic solution if available. mode=2: return numerical solution (sometimes based on equations which have been precalculated symbolically in part). exporder: 0: exact solution for matrix exponential. 1-4: higher order approximations for slow exchange (only order 1 turns out to be useful). log: 0: exact solution for logarithm. 1: Pade(1,0); 2: Pade(2,2); 3: Pade(3,3) evap: eigenvalue approximations: 0: exact solution. 1/1001/2001: First order solution. 2,3,... Newton-Raphson 1002,1003,... Laguerre approximation (not implemented in this version) 2002,2003,.... Halley's method (only numerical) """ [t,dwb,pb,k12,mode,exporder,pade,evap,dwc,pc,k13,k23,dwd,pd,k14,k24,k34]=\ args #The following section selects the right H matrix. This it, at this time, #a little convoluted because parts of this decision tree are located in #the function approxdefs. #switches: exporder=0 and exporder=10 (right now) identical, ExpExact #exporder=N corresponding to ExpN approx for slow exchange #mode=1 - symbolic; mode=2 - numerical; pade=0: LogExact; pade=1: Log10 #pade=2: Log22; pade=3: Log33. if exporder == 10 or exporder == 0 or pade == 0: mtx=approxdefs(mode=2,calctype='nHmatpro',exporder=exporder,pade=pade,\ pb=pb,pc=pc,pd=pd,k12=k12,k13=k13,k14=k14,k23=\ k23,k24=k24,k34=k34,dwb=dwb,dwc=dwc,dwd=dwd,t=t) #This selects and calculates the eigenvalue approximations #0: exact solution. #1/1001/2001: First order solution. #2,3,... Newton-Raphson #1002,1003,... Laguerre approximation (not implemented in this version) #2002,2003,.... Halley's method (only numerical) if evap == 1 or evap == 1001 or evap == 2001: if mode == 2: return -np.real(1 / np.trace(np.linalg.inv(mtx))).item() else: mtg = smp.simplify(mtx) return -1 / trace(mtg.inv()) elif evap > 1001 and evap < 2000: #Laguerre higher order, not implemented if mode == 2: substi=-nEVapprox(t,dwb,pb,k12,2,exporder,pade,evap-1,dwc,pc,k13,\ k23,dwd,pd,k14,k24,k34) zlaguerre = np.linalg.inv(mtx - substi * np.identity(np.shape(mtx)[0])) return 0 ##### NOT IMPLEMENTED HERE, AND NOT USED IN PLOTS else: mtg = smp.simplify(mtx) substi = -smp.simplify(nEVapprox(t,dwb,pb,k12,1,exporder,pade,evap\ -1,dwc,pc,k13,k23,dwd,pd,k14,k24,k34)) zlaguerre=smp.simplify((mtg-substi*smp.eye(np.shape(mtx)[0])).inv\ ()) return 0 ##### NOT IMPLEMENTED HERE, AND NOT USED IN PLOTS elif evap > 2001: ##### Halley's method higher order if mode == 2: #numerical substi=-nEVapprox(t,dwb,pb,k12,2,exporder,pade,evap-1,dwc,pc,k13,\ k23,dwd,pd,k14,k24,k34) zlaguerre = np.linalg.inv(mtx - substi * np.identity(np.shape(mtx)[0])) return -np.real(substi+((2*np.trace(zlaguerre))/((np.trace(\ zlaguerre**2))+(np.trace(zlaguerre)**2)))).item() else: #symbolic (not implemented) mtg = smp.simplify(mtx) substi = -smp.simplify(nEVapprox(t,dwb,pb,k12,1,exporder,\ pade,evap-1,dwc,pc,k13,k23,dwd,pd,k14,k24,k34)) zlaguerre=smp.simplify((mtg-substi*smp.eye(np.shape(mtx)[0])).inv\ ()) return 0 #### NOT IMPLEMENTED HERE elif evap > 1: #Newton-Raphson higher order approximations if mode == 2: #numerical substi=-nEVapprox(t,dwb,pb,k12,2,exporder,pade,evap-1,dwc,pc,k13,\ k23,dwd,pd,k14,k24,k34) return -np.real((substi)*(1-1/np.trace(np.linalg.inv(np.identity(\ np.shape(mtx)[0])-mtx/substi)))).item() else: #symbolic mtg = smp.simplify(mtx) substi = -smp.simplify(nEVapprox(t,dwb,pb,k12,1,exporder,pade,evap\ -1,dwc,pc,k13,k23,dwd,pd,k14,k24,k34)) #OTHER VERSION return -(substi)*(1-1/trace((smp.eye(np.shape(mtx)[0])-mtg/substi)\ .inv())) elif evap == 0: #exact solution (for eigenvalue) return np.real(1 / np.max(-1 / np.linalg.eigvals(mtx))).item()
def addproplist(self,pr1,propsets): for i,j in propsets: if len(np.shape(j)) == 1: m=range(len(j)) for k,l in enumerate(j): self.addprop(pr1,[[m[k],[[i,l]]]])
def fitcpmg4(praxs1,timedat,rawdata,field,err,mode,equationtype,conditions,savstatdir,filenamsav,resnam,poscoll,moreconditions,paramsx,fl): """ Fitting multiple types of relaxation dispersion data (function title somewhat misleading). input arguments: praxs1: property axis collection; timedat: x axis data; rawdata: \ y axis data; field: B0 field relative to 500 MHz, err: y error, mode: not used here, \ was used in the parent script as precalc; equationtype: type of relaxation dispersion, \ see multifunctg2 function for details; conditions: list of conditions: pos 0 - reshuffle, \ this is not used anymore, but not entirely deleted yet (check). pos 1 - details \ about many steps and attempts should be made for fitting. pos 1/0: number of initial \ attempts of the "precalculation" round to get to a small chi square at the beginning \ of the fitting procedure. High number can be useful when starting in a Monte Carlo \ manner at the beginning of the project (large boundaries, little idea about the system); pos 1/1 precalcatt: Number of documented major calculation steps during the precalculation; \ pos 1/2 precalclen: Number of undocumented steps within each major precalculation step; \ pos 1/3 maincalcatt: Number of documented major calculation steps during the main calculation;\ pos 1/4 maincalclen: Number of undocumented steps within each major main calculation step. filenamsav: Name under which progess of fit is saved to disk resnam: list of residue names included in fit (not used here) poscoll: (not used anymore, has to do with being able to undo some sorting action) moreconditions: package of conditions used by parent script, this is supposed to be saved with\ the status and therefore an input argument. paramsx: collection of parameter objects, can have different bounds etc fl: list of pointer lists. Each list member point to certain parameter list positions, \ there is one pointer list for each data point. """ allconditions=[timedat,rawdata,field,err,mode,equationtype,conditions,\ filenamsav,resnam,poscoll,moreconditions] reshuffle=conditions[0] #0 or 1 numbattempts,precalcatt,precalclen,maincalcatt,maincalclen=conditions[1][0:5]#1, 5, 5, 5, 10 numdat=np.shape(rawdata)[0] dwbsetp=[] setdwb=0 for i in np.arange(len(timedat)): for j in np.arange(len(flatten(timedat[i]))): dwbsetp.append(setdwb) setdwb+=1 par6=np.array(field) gpar=np.array(equationtype) par7=np.array(np.array(dwbsetp).astype('int')) par2=np.array(timedat) par3=np.array(rawdata) errvalpar=np.array(err) # print len(boundsl) if reshuffle == 1: par7x=par7;par6x=par6;par2x=par2;par3x=par3;errvalparx=errvalpar;gparx=gpar par7=[];par6=[];par2=[];par3=[];errvalpar=[];gpar=[] for i,j in enumerate(par2x): if gparx[i] == 6: par2.append(par2x[i]) par3.append(par3x[i]) par6.append(par6x[i]) par7.append(par7x[i]) errvalpar.append(errvalparx[i]) gpar.append(gparx[i]) else: foundrnd=0 np.random.seed() while foundrnd == 0: np.random.seed() n=np.random.randint(len(par2x)) if gparx[n] == 3: par2.append(par2x[n]) par3.append(par3x[n]) par6.append(par6x[n]) par7.append(par7x[n]) errvalpar.append(errvalparx[n]) gpar.append(gparx[n]) foundrnd=1 allrescoll=[] par1coll=[] costcoll=[] for u in np.arange(numbattempts): np.random.seed() evalmode=1 a,b1,b2,c,e=paramsx.getallparandbnds(praxs1,['p','k','dw','R20500','R2mult'],inclfilt=[]) par1=a[u];boundsl=b1[u];boundsh=b2[u] if evalmode == 1: for i in zip(par1,boundsl,boundsh): if i[0] <= i[1] or i[0] >= i[2]: print 'problem!', i[0], i[1], i[2] if precalcatt > 0: try: for k in np.arange(precalcatt): if evalmode ==1: for i in zip(par1,boundsl,boundsh): print i[0],i[1],i[2] # print par1,par2, par3, par6, par7, errvalpar, gpar, fl # print 'here' # print gpar, 'gparold' gparn=[] for ggg in gpar: gparn.append(list([gggg+1000 for gggg in ggg])) #gpar=np.array([ggg+1000 for ggg in gpar]) # print gparn, 'gparnew' res=optimize.least_squares(errfunctg3,par1,max_nfev=precalclen,\ bounds=(boundsl,boundsh),args=(par2,par3,par6,par7,\ errvalpar,gparn,fl),method='trf',jac='3-point',x_scale='jac') #, par1=res.x allrescoll.append(res) print 'attempt ', u, ' precalculation step ', u, k, par1,res.cost,filenamsav, allrescoll[-1].cost hkio.savstatus2b(savstatdir,filenamsav,resnam,poscoll,allrescoll,allconditions) except: print 'well this one didnt work' par1coll.append(res.x) costcoll.append(res.cost) if precalcatt > 0: try: par1=par1coll[np.argmin(costcoll)] except: print "unfeasable result" # try: for k in np.arange(maincalcatt): print len(par1), len(par2), len(flatten(par2)), 'well' res=optimize.least_squares(errfunctg3,par1,max_nfev=maincalclen,\ bounds=(boundsl,boundsh),args=(par2,par3,par6,par7,errvalpar,gpar,\ fl),method='trf',jac='3-point',x_scale='jac') allrescoll.append(res) hkio.savstatus2b(savstatdir,filenamsav,resnam,poscoll,allrescoll,allconditions) par1=res.x print 'mainalculation step ', u, k, par1, res.cost,filenamsav, allrescoll[-1].cost for i in res.x: print i print 'final cost', res.cost # except: # res=[0] # print 'late fitting error' return res, allrescoll #xtol=1e-9