Skip to content

Commit 76282dd

Browse files
committed
Finish Calibrate option - now fits peak shape parms to peak fit shapes
1 parent f9ee5c0 commit 76282dd

File tree

1 file changed

+101
-44
lines changed

1 file changed

+101
-44
lines changed

GSASII/GSASIIpwd.py

Lines changed: 101 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,35 +2237,40 @@ def SetPosData():
22372237
return np.array(peakPos),np.array(peakDsp),np.array(peakWt)
22382238

22392239
def SetSigData():
2240+
sigDsp = []
22402241
peakSig = []
22412242
peakSigWt = []
22422243
ig = 4
22432244
if dataType[2] in ['A','B','T']:
22442245
ig = 8
22452246
for ip,peak in enumerate(fitPeaks['peaks']):
22462247
if peak[ig+1] and IndexPeaks[0][ip][2] and IndexPeaks[0][ip][3]:
2248+
sigDsp.append(IndexPeaks[0][ip][-1])
22472249
peakSig.append(peak[ig])
22482250
peakSigWt.append(1./fitPeaks['sigDict']['sig%d'%ip])
2249-
return np.array(peakSig),np.array(peakSigWt)
2251+
return np.array(sigDsp),np.array(peakSig),np.array(peakSigWt)
22502252

22512253
def SetAlpData():
2254+
alpDsp = []
22522255
peakAlp = []
22532256
peakAlpWt = []
22542257
for ip,peak in enumerate(fitPeaks['peaks']):
22552258
if peak[5] and IndexPeaks[0][ip][2] and IndexPeaks[0][ip][3]:
2259+
alpDsp.append(IndexPeaks[0][ip][-1])
22562260
peakAlp.append(peak[4])
22572261
peakAlpWt.append(1./fitPeaks['sigDict']['alp%d'%ip])
2258-
return np.array(peakAlp),np.array(peakAlpWt)
2262+
return np.array(alpDsp),np.array(peakAlp),np.array(peakAlpWt)
22592263

22602264
def SetBetData():
2265+
betDsp = []
22612266
peakBet = []
22622267
peakBetWt = []
22632268
for ip,peak in enumerate(fitPeaks['peaks']):
22642269
if peak[7] and IndexPeaks[0][ip][2] and IndexPeaks[0][ip][3]:
2270+
betDsp.append(IndexPeaks[0][ip][-1])
22652271
peakBet.append(peak[6])
22662272
peakBetWt.append(1./fitPeaks['sigDict']['bet%d'%ip])
2267-
return np.array(peakBet),np.array(peakBetWt)
2268-
2273+
return np.array(betDsp),np.array(peakBet),np.array(peakBetWt)
22692274

22702275
def SetPosParms():
22712276
if 'T' in dataType:
@@ -2304,7 +2309,7 @@ def SetPosParms():
23042309
if Sample['Shift'][1]:
23052310
insVary.append('Shift')
23062311

2307-
instDict = dict(zip(insNames,insVals))
2312+
instDict = zip(insNames,insVals)
23082313
return instDict,insVary
23092314

23102315
def SetSigParms():
@@ -2317,7 +2322,7 @@ def SetSigParms():
23172322
if parm in ['sig-0','sig-1','sig-2','sig-q','U','V','W']:
23182323
if Inst[parm][2]:
23192324
sigVary.append(parm)
2320-
sigDict = dict(zip(sigNames,sigVals))
2325+
sigDict = zip(sigNames,sigVals)
23212326
return sigDict,sigVary
23222327

23232328
def SetAlpParms():
@@ -2330,7 +2335,7 @@ def SetAlpParms():
23302335
if parm in ['alpha','alpha-0','alpha-1']:
23312336
if Inst[parm][2]:
23322337
alpVary.append(parm)
2333-
alpDict = dict(zip(alpNames,alpVals))
2338+
alpDict = zip(alpNames,alpVals)
23342339
return alpDict,alpVary
23352340

23362341
def SetBetParms():
@@ -2343,13 +2348,13 @@ def SetBetParms():
23432348
if parm in ['beta-0','beta-1','beta-q']:
23442349
if Inst[parm][2]:
23452350
betVary.append(parm)
2346-
betDict = dict(zip(betNames,betVals))
2351+
betDict = zip(betNames,betVals)
23472352
return betDict,betVary
23482353

23492354
def GetInstParms(parmDict):
23502355
for name in Inst:
23512356
Inst[name][1] = parmDict[name]
2352-
if Inst['Type'][0][2] in ['A','B','C']:
2357+
if dataType[2] in ['A','B','C']:
23532358
for name in Sample:
23542359
if name in ['DisplaceX','DisplaceY','Shift']: # for CW only
23552360
try:
@@ -2359,24 +2364,36 @@ def GetInstParms(parmDict):
23592364

23602365
def InstPrint(sigDict):
23612366
print ('Instrument/Sample Parameters:')
2362-
if Inst['Type'][0][2] in ['A','B','C']:
2367+
if dataType[2] in ['A','B','C']:
23632368
ptfmt = "%12.6f"
23642369
else:
23652370
ptfmt = "%12.3f"
23662371
ptlbls = 'names :'
23672372
ptstr = 'values:'
23682373
sigstr = 'esds :'
2374+
ip = 0
23692375
for parm in Inst:
23702376
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',
2377+
'alpha','alpha-0','alpha-1','beta-0','beta-1','beta-2','beta-q',
23722378
'sig-0','sig-1','sig-2','sig-q','U','V','W']:
23732379
ptlbls += "%s" % (parm.center(12))
23742380
ptstr += ptfmt % (Inst[parm][1])
23752381
if parm in sigDict:
23762382
sigstr += ptfmt % (sigDict[parm])
23772383
else:
23782384
sigstr += 12*' '
2379-
if Inst['Type'][0][2] in ['A','B','C']:
2385+
ip += 1
2386+
if ip == 5:
2387+
print (ptlbls)
2388+
print (ptstr)
2389+
print (sigstr)
2390+
print (' ')
2391+
ptlbls = 'names :'
2392+
ptstr = 'values:'
2393+
sigstr = 'esds :'
2394+
ip = 0
2395+
2396+
if dataType[2] in ['A','B','C']:
23802397
parmList = ['Shift',]
23812398
if 'Debye' in Sample['Type']:
23822399
parmList = ['DisplaceX','DisplaceY']
@@ -2393,7 +2410,7 @@ def InstPrint(sigDict):
23932410
print (sigstr)
23942411

23952412
def errPeakPos(values,peakDsp,peakPos,peakWt,dataType,parmDict,varyList):
2396-
parmDict.update(zip(varyList,values))
2413+
parmDict.update(dict(zip(varyList,values)))
23972414
calcPos = G2lat.getPeakPos(dataType,parmDict,peakDsp)
23982415
if dataType[2] in ['A','B','C']:
23992416
const = 0.18/(np.pi*parmDict['radius'])
@@ -2405,39 +2422,38 @@ def errPeakPos(values,peakDsp,peakPos,peakWt,dataType,parmDict,varyList):
24052422
else:
24062423
return peakWt*(calcPos-peakPos)
24072424

2408-
def errPeakAlp(values,peakDsp,peakPos,peakAlp,peakWt,dataType,parmDict,varyList):
2409-
parmDict.update(zip(varyList,values))
2425+
def errPeakAlp(values,peakDsp,peakAlp,peakWt,dataType,parmDict,varyList):
2426+
parmDict.update(dict(zip(varyList,values)))
24102427
if dataType[2] in ['A','B']:
2411-
calcAlp = parmDict['alpha-0']+ parmDict['alpha-1']*npsind(peakPos/2.)
2428+
calcPos = G2lat.getPeakPos(dataType,parmDict,peakDsp)
2429+
calcAlp = parmDict['alpha-0']+ parmDict['alpha-1']*npsind(calcPos/2.)
24122430
else: #'T'
24132431
calcAlp = parmDict['alpha']/peakDsp
24142432
return peakWt*(calcAlp-peakAlp)
24152433

2416-
def errPeakBet(values,peakDsp,peakPos,peakBet,peakWt,dataType,parmDict,varyList):
2417-
parmDict.update(zip(varyList,values))
2434+
def errPeakBet(values,peakDsp,peakBet,peakWt,dataType,parmDict,varyList):
2435+
parmDict.update(dict(zip(varyList,values)))
24182436
if dataType[2] in ['A','B']:
2419-
calcBet = parmDict['beta-0']+ parmDict['beat-1']*npsind(peakPos/2.)
2437+
calcPos = G2lat.getPeakPos(dataType,parmDict,peakDsp)
2438+
calcBet = parmDict['beta-0']+ parmDict['beat-1']*npsind(calcPos/2.)
24202439
else: #'T'
24212440
calcBet = parmDict['beta-0']+parmDict['beta-1']/peakDsp**4+parmDict['beta-q']/peakDsp**2
24222441
return peakWt*(calcBet-peakBet)
24232442

2424-
def errPeakSig(values,peakDsp,peakPos,peakSig,peakWt,dataType,parmDict,varyList):
2425-
parmDict.update(zip(varyList,values))
2443+
def errPeakSig(values,peakDsp,peakSig,peakWt,dataType,parmDict,varyList):
2444+
parmDict.update(dict(zip(varyList,values)))
24262445
if dataType[2] in ['A','B','C']:
2427-
tp = tand(peakPos/2.0)
2446+
calcPos = G2lat.getPeakPos(dataType,parmDict,peakDsp)
2447+
tp = nptand(calcPos/2.0)
24282448
calcSig = parmDict['U']*tp**2+parmDict['V']*tp+parmDict['W']
24292449
else: #'T'
24302450
calcSig = parmDict['sig-0']+parmDict['sig-1']*peakDsp**2+parmDict['sig-2']*peakDsp**4+parmDict['sig-q']*peakDsp
24312451
return peakWt*(calcSig-peakSig)
24322452

2433-
def outResult(begin):
2434-
ncyc = int(result[2]['nfev']/2)
2435-
runtime = time.time()-begin
2453+
def outResult():
24362454
chisq = np.sum(result[2]['fvec']**2)
2437-
Values2Dict(parmDict, posVary, result[0])
24382455
GOF = chisq/(len(peakPos)-len(posVary)) #reduced chi^2
24392456
G2fil.G2Print ('Number of function calls: %d Number of observations: %d Number of parameters: %d'%(result[2]['nfev'],len(peakPos),len(posVary)))
2440-
G2fil.G2Print ('calib time = %8.3fs, %8.3fs/cycle'%(runtime,runtime/ncyc))
24412457
G2fil.G2Print ('chi**2 = %12.6g, reduced chi**2 = %6.2f'%(chisq,GOF))
24422458
try:
24432459
sig = np.sqrt(np.diag(result[1])*GOF)
@@ -2451,31 +2467,72 @@ def outResult(begin):
24512467
dataType = Inst['Type'][0]
24522468
peakPos,peakDsp,peakPosWt = SetPosData()
24532469
posDict,posVary = SetPosParms()
2454-
peakSig,peakSigWt = SetSigData()
2470+
sigDsp,peakSig,peakSigWt = SetSigData()
24552471
sigDict,sigVary = SetSigParms()
24562472
if dataType[2] in ['A','B','T']:
2457-
peakAlp,peakAlpWt = SetAlpData()
2473+
alpDsp,peakAlp,peakAlpWt = SetAlpData()
24582474
alpDict,alpVary = SetAlpParms()
2459-
peakBet,peakBetWt = SetBetData()
2475+
betDsp,peakBet,peakBetWt = SetBetData()
24602476
betDict,betVary = SetBetParms()
24612477

24622478
parmDict = {}
2479+
Sigmas = {}
24632480
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
2481+
if len(peakPos) > 5 and len(posVary):
2482+
values = np.array(Dict2Values(parmDict, posVary))
2483+
result = so.leastsq(errPeakPos,values,full_output=True,ftol=0.000001,
2484+
args=(peakDsp,peakPos,peakPosWt,dataType,parmDict,posVary))
2485+
G2fil.G2Print('Position calibration:')
2486+
Values2Dict(parmDict, posVary, result[0])
2487+
sig = outResult()
2488+
if len(sig):
2489+
Sigmas.update(zip(posVary,sig))
2490+
GetInstParms(parmDict)
2491+
parmDict = {}
2492+
parmDict.update(sigDict)
2493+
if len(peakSig) > 5 and len(sigVary):
2494+
values = np.array(Dict2Values(parmDict, sigVary))
2495+
result = so.leastsq(errPeakSig,values,full_output=True,ftol=0.000001,
2496+
args=(sigDsp,peakSig,peakSigWt,dataType,parmDict,sigVary))
2497+
G2fil.G2Print('Sigma calibration:')
2498+
Values2Dict(parmDict, sigVary, result[0])
2499+
sig = outResult()
2500+
if len(sig):
2501+
Sigmas.update(zip(sigVary,sig))
2502+
GetInstParms(parmDict)
24772503
else:
2478-
return False
2504+
G2fil.G2Print(' Sigma not calibrated: insufficient data or not selected')
2505+
if dataType[2] in ['A','B','T']:
2506+
parmDict = {}
2507+
parmDict.update(alpDict)
2508+
if len(peakAlp) > 5 and len(alpVary):
2509+
values = np.array(Dict2Values(parmDict, alpVary))
2510+
result = so.leastsq(errPeakAlp,values,full_output=True,ftol=0.000001,
2511+
args=(alpDsp,peakAlp,peakAlpWt,dataType,parmDict,alpVary))
2512+
G2fil.G2Print('Alpha calibration:')
2513+
Values2Dict(parmDict, alpVary, result[0])
2514+
sig = outResult()
2515+
if len(sig):
2516+
Sigmas.update(zip(alpVary,sig))
2517+
GetInstParms(parmDict)
2518+
else:
2519+
G2fil.G2Print(' Alpha not calibrated: insufficient data or not selected')
2520+
parmDict = {}
2521+
parmDict.update(betDict)
2522+
if len(peakBet) > 5 and len(betVary):
2523+
values = np.array(Dict2Values(parmDict, betVary))
2524+
result = so.leastsq(errPeakBet,values,full_output=True,ftol=0.000001,
2525+
args=(betDsp,peakBet,peakBetWt,dataType,parmDict,betVary))
2526+
G2fil.G2Print('Beta calibration:')
2527+
Values2Dict(parmDict, betVary, result[0])
2528+
sig = outResult()
2529+
if len(sig):
2530+
Sigmas.update(zip(betVary,sig))
2531+
GetInstParms(parmDict)
2532+
else:
2533+
G2fil.G2Print(' Beta not calibrated: insufficient data or not selected')
2534+
InstPrint(Sigmas)
2535+
return True
24792536

24802537
def getHeaderInfo(dataType):
24812538
'''Provide parameter name, label name and formatting information for the

0 commit comments

Comments
 (0)