@@ -2223,10 +2223,51 @@ def autoBkgCalc(bkgdict,ydata):
22232223 func = pybaselines .whittaker .iarpls
22242224 return func (ydata , lam = lamb , max_iter = 10 )[0 ]
22252225
2226- def DoCalibInst (IndexPeaks ,Inst ,Sample ):
2227-
2228- def SetInstParms ():
2229- dataType = Inst ['Type' ][0 ]
2226+ def DoCalibInst (IndexPeaks ,fitPeaks ,Inst ,Sample ):
2227+
2228+ def SetPosData ():
2229+ peakPos = []
2230+ peakDsp = []
2231+ peakWt = []
2232+ for peak ,sig in zip (IndexPeaks [0 ],IndexPeaks [1 ]):
2233+ if peak [2 ] and peak [3 ] and sig > 0. :
2234+ peakPos .append (peak [0 ])
2235+ peakDsp .append (peak [- 1 ]) #d-calc
2236+ peakWt .append (1. / (sig * peak [- 1 ])) #
2237+ return np .array (peakPos ),np .array (peakDsp ),np .array (peakWt )
2238+
2239+ def SetSigData ():
2240+ peakSig = []
2241+ peakSigWt = []
2242+ ig = 4
2243+ if dataType [2 ] in ['A' ,'B' ,'T' ]:
2244+ ig = 8
2245+ for ip ,peak in enumerate (fitPeaks ['peaks' ]):
2246+ if peak [ig + 1 ] and IndexPeaks [0 ][ip ][2 ] and IndexPeaks [0 ][ip ][3 ]:
2247+ peakSig .append (peak [ig ])
2248+ peakSigWt .append (1. / fitPeaks ['sigDict' ]['sig%d' % ip ])
2249+ return np .array (peakSig ),np .array (peakSigWt )
2250+
2251+ def SetAlpData ():
2252+ peakAlp = []
2253+ peakAlpWt = []
2254+ for ip ,peak in enumerate (fitPeaks ['peaks' ]):
2255+ if peak [5 ] and IndexPeaks [0 ][ip ][2 ] and IndexPeaks [0 ][ip ][3 ]:
2256+ peakAlp .append (peak [4 ])
2257+ peakAlpWt .append (1. / fitPeaks ['sigDict' ]['alp%d' % ip ])
2258+ return np .array (peakAlp ),np .array (peakAlpWt )
2259+
2260+ def SetBetData ():
2261+ peakBet = []
2262+ peakBetWt = []
2263+ for ip ,peak in enumerate (fitPeaks ['peaks' ]):
2264+ if peak [7 ] and IndexPeaks [0 ][ip ][2 ] and IndexPeaks [0 ][ip ][3 ]:
2265+ peakBet .append (peak [6 ])
2266+ peakBetWt .append (1. / fitPeaks ['sigDict' ]['bet%d' % ip ])
2267+ return np .array (peakBet ),np .array (peakBetWt )
2268+
2269+
2270+ def SetPosParms ():
22302271 if 'T' in dataType :
22312272 insNames = []
22322273 insVals = []
@@ -2264,9 +2305,48 @@ def SetInstParms():
22642305 insVary .append ('Shift' )
22652306
22662307 instDict = dict (zip (insNames ,insVals ))
2267- return dataType ,instDict ,insVary
2268-
2269- def GetInstParms (parmDict ,varyList ):
2308+ return instDict ,insVary
2309+
2310+ def SetSigParms ():
2311+ sigNames = []
2312+ sigVals = []
2313+ sigVary = []
2314+ for parm in Inst :
2315+ sigNames .append (parm )
2316+ sigVals .append (Inst [parm ][1 ])
2317+ if parm in ['sig-0' ,'sig-1' ,'sig-2' ,'sig-q' ,'U' ,'V' ,'W' ]:
2318+ if Inst [parm ][2 ]:
2319+ sigVary .append (parm )
2320+ sigDict = dict (zip (sigNames ,sigVals ))
2321+ return sigDict ,sigVary
2322+
2323+ def SetAlpParms ():
2324+ alpNames = []
2325+ alpVals = []
2326+ alpVary = []
2327+ for parm in Inst :
2328+ alpNames .append (parm )
2329+ alpVals .append (Inst [parm ][1 ])
2330+ if parm in ['alpha' ,'alpha-0' ,'alpha-1' ]:
2331+ if Inst [parm ][2 ]:
2332+ alpVary .append (parm )
2333+ alpDict = dict (zip (alpNames ,alpVals ))
2334+ return alpDict ,alpVary
2335+
2336+ def SetBetParms ():
2337+ betNames = []
2338+ betVals = []
2339+ betVary = []
2340+ for parm in Inst :
2341+ betNames .append (parm )
2342+ betVals .append (Inst [parm ][1 ])
2343+ if parm in ['beta-0' ,'beta-1' ,'beta-q' ]:
2344+ if Inst [parm ][2 ]:
2345+ betVary .append (parm )
2346+ betDict = dict (zip (betNames ,betVals ))
2347+ return betDict ,betVary
2348+
2349+ def GetInstParms (parmDict ):
22702350 for name in Inst :
22712351 Inst [name ][1 ] = parmDict [name ]
22722352 if Inst ['Type' ][0 ][2 ] in ['A' ,'B' ,'C' ]:
@@ -2287,7 +2367,9 @@ def InstPrint(sigDict):
22872367 ptstr = 'values:'
22882368 sigstr = 'esds :'
22892369 for parm in Inst :
2290- if parm in ['Lam' ,'difC' ,'difA' ,'difB' ,'Zero' ,'2-theta' ,'XE' ,'YE' ,'ZE' ]:
2370+ if parm in ['Lam' ,'difC' ,'difA' ,'difB' ,'Zero' ,'2-theta' ,'XE' ,'YE' ,'ZE' ,
2371+ 'alpha' ,'alp-0' ,'alp-1' ,'bet-0' ,'bet-1' ,'bet-2' ,'bet-q' ,
2372+ 'sig-0' ,'sig-1' ,'sig-2' ,'sig-q' ,'U' ,'V' ,'W' ]:
22912373 ptlbls += "%s" % (parm .center (12 ))
22922374 ptstr += ptfmt % (Inst [parm ][1 ])
22932375 if parm in sigDict :
@@ -2322,51 +2404,78 @@ def errPeakPos(values,peakDsp,peakPos,peakWt,dataType,parmDict,varyList):
23222404 return peakWt * (calcPos + shft - peakPos )
23232405 else :
23242406 return peakWt * (calcPos - peakPos )
2407+
2408+ def errPeakAlp (values ,peakDsp ,peakPos ,peakAlp ,peakWt ,dataType ,parmDict ,varyList ):
2409+ parmDict .update (zip (varyList ,values ))
2410+ if dataType [2 ] in ['A' ,'B' ]:
2411+ calcAlp = parmDict ['alpha-0' ]+ parmDict ['alpha-1' ]* npsind (peakPos / 2. )
2412+ else : #'T'
2413+ calcAlp = parmDict ['alpha' ]/ peakDsp
2414+ return peakWt * (calcAlp - peakAlp )
2415+
2416+ def errPeakBet (values ,peakDsp ,peakPos ,peakBet ,peakWt ,dataType ,parmDict ,varyList ):
2417+ parmDict .update (zip (varyList ,values ))
2418+ if dataType [2 ] in ['A' ,'B' ]:
2419+ calcBet = parmDict ['beta-0' ]+ parmDict ['beat-1' ]* npsind (peakPos / 2. )
2420+ else : #'T'
2421+ calcBet = parmDict ['beta-0' ]+ parmDict ['beta-1' ]/ peakDsp ** 4 + parmDict ['beta-q' ]/ peakDsp ** 2
2422+ return peakWt * (calcBet - peakBet )
23252423
2326- peakPos = []
2327- peakDsp = []
2328- peakWt = []
2329- for peak ,sig in zip (IndexPeaks [0 ],IndexPeaks [1 ]):
2330- if peak [2 ] and peak [3 ] and sig > 0. :
2331- peakPos .append (peak [0 ])
2332- peakDsp .append (peak [- 1 ]) #d-calc
2333- peakWt .append (1. / (sig * peak [- 1 ])) #
2334- peakPos = np .array (peakPos )
2335- peakDsp = np .array (peakDsp )
2336- peakWt = np .array (peakWt )
2337- dataType ,insDict ,insVary = SetInstParms ()
2338- parmDict = {}
2339- parmDict .update (insDict )
2340- varyList = insVary
2341- if not len (varyList ):
2342- G2fil .G2Print ('**** ERROR - nothing to refine! ****' )
2343- return False
2344- while True :
2345- begin = time .time ()
2346- values = np .array (Dict2Values (parmDict , varyList ))
2347- result = so .leastsq (errPeakPos ,values ,full_output = True ,ftol = 0.000001 ,
2348- args = (peakDsp ,peakPos ,peakWt ,dataType ,parmDict ,varyList ))
2424+ def errPeakSig (values ,peakDsp ,peakPos ,peakSig ,peakWt ,dataType ,parmDict ,varyList ):
2425+ parmDict .update (zip (varyList ,values ))
2426+ if dataType [2 ] in ['A' ,'B' ,'C' ]:
2427+ tp = tand (peakPos / 2.0 )
2428+ calcSig = parmDict ['U' ]* tp ** 2 + parmDict ['V' ]* tp + parmDict ['W' ]
2429+ else : #'T'
2430+ calcSig = parmDict ['sig-0' ]+ parmDict ['sig-1' ]* peakDsp ** 2 + parmDict ['sig-2' ]* peakDsp ** 4 + parmDict ['sig-q' ]* peakDsp
2431+ return peakWt * (calcSig - peakSig )
2432+
2433+ def outResult (begin ):
23492434 ncyc = int (result [2 ]['nfev' ]/ 2 )
23502435 runtime = time .time ()- begin
23512436 chisq = np .sum (result [2 ]['fvec' ]** 2 )
2352- Values2Dict (parmDict , varyList , result [0 ])
2353- GOF = chisq / (len (peakPos )- len (varyList )) #reduced chi^2
2354- G2fil .G2Print ('Number of function calls: %d Number of observations: %d Number of parameters: %d' % (result [2 ]['nfev' ],len (peakPos ),len (varyList )))
2437+ Values2Dict (parmDict , posVary , result [0 ])
2438+ GOF = chisq / (len (peakPos )- len (posVary )) #reduced chi^2
2439+ G2fil .G2Print ('Number of function calls: %d Number of observations: %d Number of parameters: %d' % (result [2 ]['nfev' ],len (peakPos ),len (posVary )))
23552440 G2fil .G2Print ('calib time = %8.3fs, %8.3fs/cycle' % (runtime ,runtime / ncyc ))
23562441 G2fil .G2Print ('chi**2 = %12.6g, reduced chi**2 = %6.2f' % (chisq ,GOF ))
23572442 try :
23582443 sig = np .sqrt (np .diag (result [1 ])* GOF )
23592444 if np .any (np .isnan (sig )):
23602445 G2fil .G2Print ('*** Least squares aborted - some invalid esds possible ***' )
2361- break #refinement succeeded - finish up!
23622446 except ValueError : #result[1] is None on singular matrix
23632447 G2fil .G2Print ('**** Refinement failed - singular matrix ****' )
2364- return False
2365-
2366- sigDict = dict (zip (varyList ,sig ))
2367- GetInstParms (parmDict ,varyList )
2368- InstPrint (sigDict )
2369- return True
2448+ return []
2449+ return sig
2450+
2451+ dataType = Inst ['Type' ][0 ]
2452+ peakPos ,peakDsp ,peakPosWt = SetPosData ()
2453+ posDict ,posVary = SetPosParms ()
2454+ peakSig ,peakSigWt = SetSigData ()
2455+ sigDict ,sigVary = SetSigParms ()
2456+ if dataType [2 ] in ['A' ,'B' ,'T' ]:
2457+ peakAlp ,peakAlpWt = SetAlpData ()
2458+ alpDict ,alpVary = SetAlpParms ()
2459+ peakBet ,peakBetWt = SetBetData ()
2460+ betDict ,betVary = SetBetParms ()
2461+
2462+ parmDict = {}
2463+ parmDict .update (posDict )
2464+ if not len (posVary ):
2465+ G2fil .G2Print ('**** ERROR - nothing to refine! ****' )
2466+ return False
2467+ begin = time .time ()
2468+ values = np .array (Dict2Values (parmDict , posVary ))
2469+ result = so .leastsq (errPeakPos ,values ,full_output = True ,ftol = 0.000001 ,
2470+ args = (peakDsp ,peakPos ,peakPosWt ,dataType ,parmDict ,posVary ))
2471+ sig = outResult (begin )
2472+ if len (sig ):
2473+ sigDict = dict (zip (posVary ,sig ))
2474+ GetInstParms (parmDict )
2475+ InstPrint (sigDict )
2476+ return True
2477+ else :
2478+ return False
23702479
23712480def getHeaderInfo (dataType ):
23722481 '''Provide parameter name, label name and formatting information for the
0 commit comments