- Timestamp:
- Jan 17, 2021 3:12:29 PM (2 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/GSASIIdataGUI.py
r4764 r4780 5216 5216 else: 5217 5217 msg = '' 5218 # if len(Rvals['psing']) == 1:5219 # h = 'This variable appears'5220 # # that = 'that variable'5221 # else:5222 # h = 'These variables appear'5223 # # that = 'those variables'5224 # msg += h + ' to cause a singular matrix:\n\t'5225 # for i,var in enumerate(Rvals['psing']):5226 # if i: msg += ', '5227 # msg += varyList[var]5228 # # msg += '\n\nRefine again with '+that+' frozen?'5229 5218 result = wx.ID_NO 5230 5219 try: 5231 # dlg = wx.MessageDialog(self, msg,'Refine again?',5232 # wx.YES_NO | wx.ICON_QUESTION)5233 5220 dlg = wx.MessageDialog(self, msg,'Note singularities', 5234 5221 wx.OK) … … 5238 5225 finally: 5239 5226 dlg.Destroy() 5240 # if result != wx.ID_YES: return5241 # Controls = self.GPXtree.GetItemPyData(GetGPXtreeItemId(self,self.root, 'Controls'))5242 # if 'parmFrozen' not in Controls:5243 # Controls['parmFrozen'] = {}5244 # if 'FrozenList' not in Controls['parmFrozen']:5245 # Controls['parmFrozen']['FrozenList'] = []5246 # Controls['parmFrozen']['FrozenList'] += [5247 # G2obj.G2VarObj(varyList[i]) for i in Rvals['plist']]5248 # wx.CallAfter(self.OnRefine,event)5249 5227 else: 5250 5228 self.ErrorDialog('Refinement error',Rvals['msg']) -
trunk/GSASIImath_new.py
r4759 r4780 2 2 #GSASIImath - major mathematics routines 3 3 ########### SVN repository information ################### 4 # $Date: 2021-01- 04 10:41:46 -0600 (Mon, 04 Jan 2021) $4 # $Date: 2021-01-14 14:10:10 -0600 (Thu, 14 Jan 2021) $ 5 5 # $Author: vondreele $ 6 # $Revision: 47 09$6 # $Revision: 4767 $ 7 7 # $URL: https://subversion.xray.aps.anl.gov/pyGSAS/trunk/GSASIImath.py $ 8 # $Id: GSASIImath.py 47 09 2021-01-04 16:41:46Z vondreele $8 # $Id: GSASIImath.py 4767 2021-01-14 20:10:10Z vondreele $ 9 9 ########### SVN repository information ################### 10 10 ''' … … 24 24 import copy 25 25 import GSASIIpath 26 GSASIIpath.SetVersionNumber("$Revision: 47 09$")26 GSASIIpath.SetVersionNumber("$Revision: 4767 $") 27 27 import GSASIIElem as G2el 28 28 import GSASIIlattice as G2lat … … 126 126 127 127 def setHcorr(info,Amat,xtol,problem=False): 128 '''Find & report high correlation terms in covariance matrix 129 ''' 128 130 Adiag = np.sqrt(np.diag(Amat)) 129 131 if np.any(np.abs(Adiag) < 1.e-14): raise G2NormException # test for any hard singularities … … 141 143 info['Hcorr'] = [(i,j,AcovUp[i,j]) for i,j in zip(*np.where(AcovUp > m))] 142 144 return Bmat,Nzeros 143 145 146 def setSVDwarn(info,Amat,Nzeros,indices): 147 '''Find & report terms causing SVN zeros 148 ''' 149 if Nzeros == 0: return 150 d = np.abs(np.diag(nl.qr(Amat)[1])) 151 svdsing = list(np.where(d < 1.e-14)[0]) 152 if len(svdsing) < Nzeros: # try to get the Nzeros worst terms 153 svdsing = list(np.where(d <= sorted(d)[Nzeros-1])[0]) 154 155 156 if not len(svdsing): # make sure at least the worst term is shown 157 svdsing = [np.argmin(d)] 158 info['SVDsing'] = [indices[i] for i in svdsing] 159 144 160 def HessianLSQ(func,x0,Hess,args=(),ftol=1.49012e-8,xtol=1.e-6, maxcyc=0,lamda=-3,Print=False,refPlotUpdate=None): 145 161 ''' … … 187 203 ifConverged = False 188 204 deltaChi2 = -10. 189 x0 = np.array(x0, ndmin=1) #might be redundant? 205 x0 = np.array(x0, ndmin=1, dtype=np.float64) # make sure that x0 float 1-D 206 # array (in case any parameters were set to int) 190 207 n = len(x0) 191 208 if type(args) != type(()): … … 193 210 194 211 icycle = 0 212 AmatAll = None # changed only if cycles > 0 195 213 lamMax = lam = 0 # start w/o Marquardt and add it in if needed 196 214 nfev = 0 … … 213 231 info = {'num cyc':0,'fvec':M2,'nfev':1,'lamMax':0,'psing':[],'SVD0':0} 214 232 info['msg'] = 'no variables: skipping refinement\n' 215 return [x0,None,info] 233 info.update({'Converged':True, 'DelChi2':0, 'Xvec':None, 'chisq0':chisq00}) 234 return [x0,np.array([]),info] 235 indices = range(n) 216 236 while icycle < maxcyc: 217 237 M = M2 … … 219 239 nfev += 1 220 240 chisq0 = np.sum(M**2) 221 Yvec,AmatAll = Hess(x0,*args) # compute hessian & vectors with all variables 241 YvecAll,AmatAll = Hess(x0,*args) # compute hessian & vectors with all variables 242 Yvec = copy.copy(YvecAll) 222 243 Amat = copy.copy(AmatAll) 223 244 Xvec = copy.copy(XvecAll) … … 255 276 info = {'num cyc':icycle,'fvec':M,'nfev':nfev,'lamMax':lamMax,'SVD0':Nzeros} 256 277 info['psing'] = [i for i in range(n) if i not in indices] 257 258 278 info['msg'] = 'SVD inversion failure\n' 259 279 return [x0,None,info] 260 if Nzeros: # remove bad vars from the matrix261 d = np.abs(np.diag(nl.qr(Amatlam)[1]))262 psing = list(np.where(d < 1.e-14)[0])263 if not len(psing): # make sure at least the worst term is removed264 psing = [np.argmin(d)]265 G2fil.G2Print('{} SVD Zeros: dropping terms for for variable(s) #{}'.266 format(Nzeros,psing), mode='warn')267 Amat, indices, Xvec, Yvec, Adiag = dropTerms(psing,Amat, indices, Xvec, Yvec, Adiag)268 Amatlam = Amat*(1.+np.eye(Amat.shape[0])*lam)269 Ainv,nz = pinv(Amatlam,xtol) #do Moore-Penrose inversion (via SVD)270 if nz > 0: G2fil.G2Print('Note: there are {} new SVD Zeros after drop'.format(nz),271 mode='warn')272 280 Xvec = np.inner(Ainv,Yvec)/Adiag #solve for LS terms 273 281 XvecAll[indices] = Xvec # expand … … 293 301 G2fil.G2Print(Msg.msg,mode='warn') 294 302 info['msg'] = Msg.msg + '\n\n' 303 setSVDwarn(info,Amatlam,Nzeros,indices) 295 304 return [x0,None,info] 296 305 nfev += 1 … … 313 322 Bmat,Nzeros = setHcorr(info,AmatAll,xtol,problem=True) 314 323 info['SVD0'] = Nzeros 324 setSVDwarn(info,Amatlam,Nzeros,indices) 315 325 return [x0,Bmat,info] 316 326 except Exception as Msg: … … 329 339 if Print and n-len(indices): 330 340 G2fil.G2Print( 331 'Cycle %d: %.2fs Chi2: %.5g; Obs: %d; Lam: %.3g Del: %.3g; drop=%d '%332 (icycle,time.time()-time0,chisq1,Nobs,lamMax,deltaChi2,n-len(indices) ))341 'Cycle %d: %.2fs Chi2: %.5g; Obs: %d; Lam: %.3g Del: %.3g; drop=%d, SVD=%d'% 342 (icycle,time.time()-time0,chisq1,Nobs,lamMax,deltaChi2,n-len(indices),Nzeros)) 333 343 elif Print: 334 344 G2fil.G2Print( 335 'Cycle %d: %.2fs, Chi**2: %.5g for %d obs., Lambda: %.3g, Delta: %.3g '%336 (icycle,time.time()-time0,chisq1,Nobs,lamMax,deltaChi2 ))345 'Cycle %d: %.2fs, Chi**2: %.5g for %d obs., Lambda: %.3g, Delta: %.3g, SVD=%d'% 346 (icycle,time.time()-time0,chisq1,Nobs,lamMax,deltaChi2,Nzeros)) 337 347 Histograms = args[0][0] 338 348 if refPlotUpdate is not None: refPlotUpdate(Histograms,icycle) # update plot … … 353 363 info = {'num cyc':icycle,'fvec':M2,'nfev':nfev,'lamMax':lamMax,'psing':psing,'SVD0':-2} 354 364 info['msg'] = Msg.msg + '\n' 365 setSVDwarn(info,Amatlam,Nzeros,indices) 355 366 return [x0,None,info] 356 367 chisqf = np.sum(M**2) # ending chi**2 357 Yvec,Amat = Hess(x0,*args) # we could save some time and use the last Hessian from the last refinement cycle358 368 psing_prev = [i for i in range(n) if i not in indices] # save dropped vars 369 if AmatAll is None: # Save some time and use Hessian from the last refinement cycle 370 Yvec,Amat = Hess(x0,*args) 371 else: 372 Yvec = copy.copy(YvecAll) 373 Amat = copy.copy(AmatAll) 359 374 indices = range(n) 360 375 info = {} … … 362 377 Bmat,Nzeros = setHcorr(info,Amat,xtol,problem=False) 363 378 info.update({'num cyc':icycle,'fvec':M,'nfev':nfev,'lamMax':lamMax,'SVD0':Nzeros,'psing':psing_prev, 364 'Converged':ifConverged, 'DelChi2':deltaChi2, 'Xvec':XvecAll, 'chisq0':chisq00}) 379 'Converged':ifConverged, 'DelChi2':deltaChi2, 'chisq0':chisq00}) 380 if icycle > 0: info.update({'Xvec':XvecAll}) 381 setSVDwarn(info,Amatlam,Nzeros,indices) 382 # expand Bmat by filling with zeros if columns have been dropped 383 if len(psing_prev): 384 ins = [j-i for i,j in enumerate(psing_prev)] 385 Bmat = np.insert(np.insert(Bmat,ins,0,1),ins,0,0) 365 386 return [x0,Bmat,info] 366 387 except nl.LinAlgError: … … 389 410 info['psing'] = [i for i in range(n) if i not in indices] 390 411 return [x0,None,info] 391 if Nzeros: # sometimes SVD zeros show up when lam=0392 d = np.abs(np.diag(nl.qr(Amat)[1]))393 psing = list(np.where(d < 1.e-14)[0])394 if len(psing) < Nzeros:395 psing = list(np.where(d <= sorted(d)[Nzeros-1])[0])396 if Print:397 G2fil.G2Print('Found %d SVD zeros w/o Lambda. '+398 'Likely problem with variable(s) #%s'%(Nzeros,psing), mode='warn')399 Amat, indices, Yvec = dropTerms(psing, Amat, indices, Yvec)400 412 # expand Bmat by filling with zeros if columns have been dropped 401 413 psing = [i for i in range(n) if i not in indices] … … 406 418 'Converged':ifConverged, 'DelChi2':deltaChi2, 'Xvec':XvecAll, 'chisq0':chisq00}) 407 419 info['psing'] = [i for i in range(n) if i not in indices] 420 setSVDwarn(info,Amat,Nzeros,indices) 408 421 return [x0,Bmat,info] 409 422 … … 554 567 ##### Atom manipulations 555 568 ################################################################################ 569 570 def getAtomPtrs(data,draw=False): 571 ''' get atom data pointers cx,ct,cs,cia in Atoms or Draw Atoms lists 572 NB:may not match column numbers in displayed table 573 574 param: dict: data phase data structure 575 draw: boolean True if Draw Atoms list pointers are required 576 return: cx,ct,cs,cia pointers to atom xyz, type, site sym, uiso/aniso flag 577 ''' 578 if draw: 579 return data['Drawing']['atomPtrs'] 580 else: 581 return data['General']['AtomPtrs'] 556 582 557 583 def FindMolecule(ind,generalData,atomData): #uses numpy & masks - very fast even for proteins! … … 749 775 generalData = data['General'] 750 776 SGData = generalData['SGData'] 751 cx,ct,cs,cia = ge neralData['AtomPtrs']777 cx,ct,cs,cia = getAtomPtrs(data) 752 778 drawingData = data['Drawing'] 753 dcx,dct,dcs,dci = drawingData['atomPtrs']779 dcx,dct,dcs,dci = getAtomPtrs(data,True) 754 780 atoms = data['Atoms'] 755 781 drawAtoms = drawingData['Atoms'] … … 782 808 def FindNeighbors(phase,FrstName,AtNames,notName=''): 783 809 General = phase['General'] 784 cx,ct,cs,cia = General['AtomPtrs']810 cx,ct,cs,cia = getAtomPtrs(phase) 785 811 Atoms = phase['Atoms'] 786 812 atNames = [atom[ct-1] for atom in Atoms] … … 870 896 def FindAllNeighbors(phase,FrstName,AtNames,notName='',Orig=None,Short=False): 871 897 General = phase['General'] 872 cx,ct,cs,cia = General['AtomPtrs']898 cx,ct,cs,cia = getAtomPtrs(phase) 873 899 Atoms = phase['Atoms'] 874 900 atNames = [atom[ct-1] for atom in Atoms] … … 1882 1908 SGData = generalData['SGData'] 1883 1909 SSGData = generalData['SSGData'] 1884 cx,ct,cs,cia = ge neralData['AtomPtrs']1910 cx,ct,cs,cia = getAtomPtrs(data) 1885 1911 drawingData = data['Drawing'] 1886 1912 modul = generalData['SuperVec'][0] 1887 dcx,dct,dcs,dci = drawingData['atomPtrs']1913 dcx,dct,dcs,dci = getAtomPtrs(data,True) 1888 1914 atoms = data['Atoms'] 1889 1915 drawAtoms = drawingData['Atoms'] … … 2940 2966 General = Phase['General'] 2941 2967 Amat,Bmat = G2lat.cell2AB(General['Cell'][1:7]) 2942 cx,ct,cs,cia = General['AtomPtrs']2968 cx,ct,cs,cia = getAtomPtrs(Phase) 2943 2969 Atoms = Phase['Atoms'] 2944 2970 cartAtoms = [] … … 4144 4170 Unique.append(icntr) 4145 4171 return Unique 4172 4173 def AtomsCollect(data,Ind,Sel): 4174 '''Finds the symmetry set of atoms for those selected. Selects 4175 the one closest to the selected part of the unit cell. 4176 Works on the contents of data['Map Peaks']. Called from OnPeaksUnique in 4177 GSASIIphsGUI.py, 4178 4179 :param data: the phase data structure 4180 :param list Ind: list of selected peak indices 4181 :param int Sel: selected part of unit to find atoms closest to 4182 4183 :returns: the list of symmetry unique peaks from among those given in Ind 4184 ''' 4185 cx,ct,cs,ci = getAtomPtrs(data) 4186 cent = np.ones(3)*.5 4187 generalData = data['General'] 4188 Amat,Bmat = G2lat.cell2AB(generalData['Cell'][1:7]) 4189 SGData = generalData['SGData'] 4190 Atoms = copy.deepcopy(data['Atoms']) 4191 Indx = [True for ind in Ind] 4192 # scan through peaks, finding all peaks equivalent to peak ind 4193 for ind in Ind: 4194 if Indx[ind]: 4195 xyz = Atoms[ind][cx:cx+3] 4196 uij = Atoms[ind][ci+2:ci+8] 4197 if Atoms[ind][ci] == 'A': 4198 Equiv = list(G2spc.GenAtom(xyz,SGData,Uij=uij)) 4199 Uijs = np.array([x[1] for x in Equiv]) 4200 else: 4201 Equiv = G2spc.GenAtom(xyz,SGData) 4202 xyzs = np.array([x[0] for x in Equiv]) 4203 dzeros = np.sqrt(np.sum(np.inner(Amat,xyzs)**2,axis=0)) 4204 dcent = np.sqrt(np.sum(np.inner(Amat,xyzs-cent)**2,axis=0)) 4205 xyzs = np.hstack((xyzs,dzeros[:,nxs],dcent[:,nxs])) 4206 cind = np.argmin(xyzs.T[Sel-1]) 4207 Atoms[ind][cx:cx+3] = xyzs[cind][:3] 4208 if Atoms[ind][ci] == 'A': 4209 Atoms[ind][ci+2:ci+8] = Uijs[cind] 4210 return Atoms 4146 4211 4147 4212 ################################################################################ … … 5058 5123 SGT = np.array([SGData['SGOps'][i][1] for i in range(len(SGData['SGOps']))]) 5059 5124 fixAtoms = data['Atoms'] #if any 5060 cx,ct,cs = ge neralData['AtomPtrs'][:3]5125 cx,ct,cs = getAtomPtrs(data)[:3] 5061 5126 aTypes = set([]) 5062 5127 parmDict = {'Bmat':Bmat,'Gmat':Gmat} -
trunk/GSASIIstrMain.py
r4761 r4780 55 55 #report on SVD 0's and highly correlated variables 56 56 msg = '' 57 SVD0 = result[2].get('SVD0') 58 if SVD0 > 0: 59 msg += 'Warning: There were {} singularities in the Hessian'.format(SVD0) 60 # process singular variables 57 # process singular variables; all vars go to console, first 10 to 58 # dialog window 61 59 psing = result[2].get('psing',[]) 62 60 if len(psing): 63 61 if msg: msg += '\n' 64 m = ' {} Parameters dropped due to singularities:'.format(len(psing))62 m = 'Error: {} Parameter(s) dropped:'.format(len(psing)) 65 63 msg += m 66 64 G2fil.G2Print(m, mode='warn') 67 65 m = '' 68 66 for i,val in enumerate(psing): 67 if i == 0: 68 msg += '\n{}'.format(varyList[val]) 69 m = ' {}'.format(varyList[val]) 70 else: 71 if len(m) > 70: 72 G2fil.G2Print(m, mode='warn') 73 m = ' ' 74 else: 75 m += ', ' 76 m += '{}'.format(varyList[val]) 77 if i == 10: 78 msg += ', {}... see console for full list'.format(varyList[val]) 79 elif i > 10: 80 pass 81 else: 82 msg += ', {}'.format(varyList[val]) 83 if m: G2fil.G2Print(m, mode='warn') 84 SVD0 = result[2].get('SVD0') 85 if SVD0 == 1: 86 msg += 'Warning: Soft (SVD) singularity in the Hessian' 87 elif SVD0 > 0: 88 msg += 'Warning: {} soft (SVD) Hessian singularities'.format(SVD0) 89 SVDsing = result[2].get('SVDsing',[]) 90 if len(SVDsing): 91 if msg: msg += '\n' 92 m = 'SVD problem(s) likely from:' 93 msg += m 94 G2fil.G2Print(m, mode='warn') 95 m = '' 96 for i,val in enumerate(SVDsing): 69 97 if i == 0: 70 98 msg += '\n{}'.format(varyList[val]) … … 209 237 if 'Hessian' in Controls['deriv type']: 210 238 num = len(varyList)-1 239 # BHT -- I am not sure if this works correctly: 211 240 for i,val in enumerate(np.flipud(result[2]['psing'])): 212 241 if val: … … 260 289 if dlg: break # refining interactively 261 290 # non-interactive refinement 262 for val in sorted(psing,reverse=True):263 G2fil.G2Print ('Removing parameter: '+varyList[val])264 del(varyList[val])265 if not psing: break # removed variable(s), try again291 #for val in sorted(psing,reverse=True): 292 # G2fil.G2Print ('Removing parameter: '+varyList[val]) 293 # del(varyList[val]) 294 #if not psing: break # removed variable(s), try again 266 295 else: 267 296 G2fil.G2Print ('**** Refinement failed - singular matrix ****',mode='error')
Note: See TracChangeset
for help on using the changeset viewer.