1 | # -*- coding: utf-8 -*- |
---|
2 | #GSASIIrestr - restraint GUI routines |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2017-02-14 18:17:11 +0000 (Tue, 14 Feb 2017) $ |
---|
5 | # $Author: vondreele $ |
---|
6 | # $Revision: 2704 $ |
---|
7 | # $URL: trunk/GSASIIrestrGUI.py $ |
---|
8 | # $Id: GSASIIrestrGUI.py 2704 2017-02-14 18:17:11Z vondreele $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | ''' |
---|
11 | *GSASIIrestrGUI: Restraint GUI routines* |
---|
12 | ---------------------------------------- |
---|
13 | |
---|
14 | Used to define restraints. |
---|
15 | |
---|
16 | ''' |
---|
17 | import wx |
---|
18 | import wx.grid as wg |
---|
19 | import numpy as np |
---|
20 | import numpy.ma as ma |
---|
21 | import os.path |
---|
22 | import GSASIIpath |
---|
23 | GSASIIpath.SetVersionNumber("$Revision: 2704 $") |
---|
24 | import GSASIImath as G2mth |
---|
25 | import GSASIIlattice as G2lat |
---|
26 | import GSASIIspc as G2spc |
---|
27 | import GSASIIgrid as G2gd |
---|
28 | import GSASIIplot as G2plt |
---|
29 | import GSASIIdata as G2data |
---|
30 | import GSASIIctrls as G2G |
---|
31 | |
---|
32 | VERY_LIGHT_GREY = wx.Colour(235,235,235) |
---|
33 | |
---|
34 | ################################################################################ |
---|
35 | ##### Restraints |
---|
36 | ################################################################################ |
---|
37 | def GetSelectedRows(widget): |
---|
38 | '''Returns a list of selected rows. Rows can be selected, blocks of cells |
---|
39 | or individual cells can be selected. The column for selected cells is ignored. |
---|
40 | ''' |
---|
41 | rows = widget.GetSelectedRows() |
---|
42 | if not rows: |
---|
43 | top = widget.GetSelectionBlockTopLeft() |
---|
44 | bot = widget.GetSelectionBlockBottomRight() |
---|
45 | if top and bot: |
---|
46 | rows = range(top[0][0],bot[0][0]+1) |
---|
47 | if not rows: |
---|
48 | rows = sorted(list(set([cell[0] for cell in widget.GetSelectedCells()]))) |
---|
49 | return rows |
---|
50 | |
---|
51 | def UpdateRestraints(G2frame,data,Phases,phaseName): |
---|
52 | '''Respond to selection of the Restraints item on the |
---|
53 | data tree |
---|
54 | ''' |
---|
55 | if not Phases: |
---|
56 | print 'There are no phases to form restraints' |
---|
57 | return |
---|
58 | if not len(Phases): |
---|
59 | print 'There are no phases to form restraints' |
---|
60 | return |
---|
61 | phasedata = Phases[phaseName] |
---|
62 | tabIndex = {} |
---|
63 | if phaseName not in data: |
---|
64 | data[phaseName] = {} |
---|
65 | restrData = data[phaseName] |
---|
66 | if 'Bond' not in restrData: |
---|
67 | restrData['Bond'] = {'wtFactor':1.0,'Range':1.1,'Bonds':[],'Use':True} |
---|
68 | if 'Angle' not in restrData: |
---|
69 | restrData['Angle'] = {'wtFactor':1.0,'Range':0.85,'Angles':[],'Use':True} |
---|
70 | if 'Plane' not in restrData: |
---|
71 | restrData['Plane'] = {'wtFactor':1.0,'Planes':[],'Use':True} |
---|
72 | if 'Chiral' not in restrData: |
---|
73 | restrData['Chiral'] = {'wtFactor':1.0,'Volumes':[],'Use':True} |
---|
74 | if 'Torsion' not in restrData: |
---|
75 | restrData['Torsion'] = {'wtFactor':1.0,'Coeff':{},'Torsions':[],'Use':True} |
---|
76 | if 'Rama' not in restrData: |
---|
77 | restrData['Rama'] = {'wtFactor':1.0,'Coeff':{},'Ramas':[],'Use':True} |
---|
78 | if 'Texture' not in restrData: |
---|
79 | restrData['Texture'] = {'wtFactor':1.0,'HKLs':[],'Use':True} |
---|
80 | if 'ChemComp' not in restrData: |
---|
81 | restrData['ChemComp'] = {'wtFactor':1.0,'Sites':[],'Use':True} |
---|
82 | General = phasedata['General'] |
---|
83 | Cell = General['Cell'][1:7] #skip flag & volume |
---|
84 | Amat,Bmat = G2lat.cell2AB(Cell) |
---|
85 | SGData = General['SGData'] |
---|
86 | cx,ct,cs,cia = General['AtomPtrs'] |
---|
87 | Atoms = phasedata['Atoms'] |
---|
88 | AtLookUp = G2mth.FillAtomLookUp(Atoms,cia+8) |
---|
89 | if 'macro' in General['Type']: |
---|
90 | Names = [atom[0]+':'+atom[1]+atom[2]+' '+atom[3] for atom in Atoms] |
---|
91 | Ids = [] |
---|
92 | Coords = [] |
---|
93 | Types = [] |
---|
94 | else: |
---|
95 | Names = ['all '+ name for name in General['AtomTypes']] |
---|
96 | iBeg = len(Names) |
---|
97 | Types = [name for name in General['AtomTypes']] |
---|
98 | Coords = [ [] for type in Types] |
---|
99 | Ids = [ 0 for type in Types] |
---|
100 | Names += [atom[ct-1] for atom in Atoms] |
---|
101 | Types += [atom[ct] for atom in Atoms] |
---|
102 | Coords += [atom[cx:cx+3] for atom in Atoms] |
---|
103 | Ids += [atom[cia+8] for atom in Atoms] |
---|
104 | rama = G2data.ramachandranDist['All'] |
---|
105 | ramaName = 'All' |
---|
106 | |
---|
107 | def OnSelectPhase(event): |
---|
108 | dlg = wx.SingleChoiceDialog(G2frame,'Select','Phase',Phases.keys()) |
---|
109 | try: |
---|
110 | if dlg.ShowModal() == wx.ID_OK: |
---|
111 | phaseName = Phases.keys()[dlg.GetSelection()] |
---|
112 | UpdateRestraints(G2frame,data,Phases,phaseName) |
---|
113 | finally: |
---|
114 | dlg.Destroy() |
---|
115 | |
---|
116 | def getMacroFile(macName): |
---|
117 | defDir = os.path.join(os.path.split(__file__)[0],'GSASIImacros') |
---|
118 | dlg = wx.FileDialog(G2frame,message='Choose '+macName+' restraint macro file', |
---|
119 | defaultDir=defDir,defaultFile="",wildcard="GSAS-II macro file (*.mac)|*.mac", |
---|
120 | style=wx.OPEN | wx.CHANGE_DIR) |
---|
121 | try: |
---|
122 | if dlg.ShowModal() == wx.ID_OK: |
---|
123 | macfile = dlg.GetPath() |
---|
124 | macro = open(macfile,'Ur') |
---|
125 | head = macro.readline() |
---|
126 | if macName not in head: |
---|
127 | print head |
---|
128 | print '**** ERROR - wrong restraint macro file selected, try again ****' |
---|
129 | macro = [] |
---|
130 | finally: |
---|
131 | dlg.Destroy() |
---|
132 | return macro #advanced past 1st line |
---|
133 | |
---|
134 | def OnPlotAARestraint(event): |
---|
135 | page = G2frame.dataDisplay.GetSelection() |
---|
136 | if 'Torsion' in G2frame.dataDisplay.GetPageText(page): |
---|
137 | torNames = [] |
---|
138 | torNames += restrData['Torsion']['Coeff'].keys() |
---|
139 | dlg = wx.SingleChoiceDialog(G2frame,'Select','Torsion data',torNames) |
---|
140 | try: |
---|
141 | if dlg.ShowModal() == wx.ID_OK: |
---|
142 | torName = torNames[dlg.GetSelection()] |
---|
143 | torsion = G2data.torsionDist[torName] |
---|
144 | torCoeff = restrData['Torsion']['Coeff'][torName] |
---|
145 | torList = restrData['Torsion']['Torsions'] |
---|
146 | Names = [] |
---|
147 | Angles = [] |
---|
148 | for i,[indx,ops,cofName,esd] in enumerate(torList): |
---|
149 | if cofName == torName: |
---|
150 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,0,4) |
---|
151 | name = '('+atoms[2][1]+atoms[2][0].strip()+atoms[2][2]+')' |
---|
152 | for atom in atoms: |
---|
153 | name += ' '+atom[3] |
---|
154 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
155 | angle = G2mth.getRestTorsion(XYZ,Amat) |
---|
156 | Angles.append(angle) |
---|
157 | Names.append(name) |
---|
158 | G2plt.PlotTorsion(G2frame,phaseName,torsion,torName,Names,np.array(Angles),torCoeff) |
---|
159 | finally: |
---|
160 | dlg.Destroy() |
---|
161 | |
---|
162 | elif 'Rama' in G2frame.dataDisplay.GetPageText(page): |
---|
163 | ramaNames = ['All',] |
---|
164 | ramaNames += restrData['Rama']['Coeff'].keys() |
---|
165 | dlg = wx.SingleChoiceDialog(G2frame,'Select','Ramachandran data',ramaNames) |
---|
166 | try: |
---|
167 | if dlg.ShowModal() == wx.ID_OK: |
---|
168 | ramaName = ramaNames[dlg.GetSelection()] |
---|
169 | rama = G2data.ramachandranDist[ramaName] |
---|
170 | ramaCoeff = [] |
---|
171 | if ramaName != 'All': |
---|
172 | ramaCoeff = restrData['Rama']['Coeff'][ramaName] |
---|
173 | ramaList = restrData['Rama']['Ramas'] |
---|
174 | Names = [] |
---|
175 | PhiPsi = [] |
---|
176 | for i,[indx,ops,cofName,esd] in enumerate(ramaList): |
---|
177 | if cofName == ramaName or (ramaName == 'All' and '-1' in cofName): |
---|
178 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,0,4) |
---|
179 | name = '('+atoms[3][1]+atoms[3][0].strip()+atoms[3][2]+')' |
---|
180 | for atom in atoms: |
---|
181 | name += ' '+atom[3] |
---|
182 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
183 | phi,psi = G2mth.getRestRama(XYZ,Amat) |
---|
184 | PhiPsi.append([phi,psi]) |
---|
185 | Names.append(name) |
---|
186 | G2plt.PlotRama(G2frame,phaseName,rama,ramaName,Names,np.array(PhiPsi),ramaCoeff) |
---|
187 | finally: |
---|
188 | dlg.Destroy() |
---|
189 | |
---|
190 | def OnAddRestraint(event): |
---|
191 | page = G2frame.dataDisplay.GetSelection() |
---|
192 | if 'Bond' in G2frame.dataDisplay.GetPageText(page): |
---|
193 | AddBondRestraint(restrData['Bond']) |
---|
194 | elif 'Angle' in G2frame.dataDisplay.GetPageText(page): |
---|
195 | AddAngleRestraint(restrData['Angle']) |
---|
196 | elif 'Plane' in G2frame.dataDisplay.GetPageText(page): |
---|
197 | AddPlaneRestraint(restrData['Plane']) |
---|
198 | elif 'Chem' in G2frame.dataDisplay.GetPageText(page): |
---|
199 | AddChemCompRestraint(restrData['ChemComp']) |
---|
200 | elif 'Texture' in G2frame.dataDisplay.GetPageText(page): |
---|
201 | AddTextureRestraint(restrData['Texture']) |
---|
202 | |
---|
203 | def OnAddAARestraint(event): |
---|
204 | page = G2frame.dataDisplay.GetSelection() |
---|
205 | if 'Bond' in G2frame.dataDisplay.GetPageText(page): |
---|
206 | AddAABondRestraint(restrData['Bond']) |
---|
207 | elif 'Angle' in G2frame.dataDisplay.GetPageText(page): |
---|
208 | AddAAAngleRestraint(restrData['Angle']) |
---|
209 | elif 'Plane' in G2frame.dataDisplay.GetPageText(page): |
---|
210 | AddAAPlaneRestraint(restrData['Plane']) |
---|
211 | elif 'Chiral' in G2frame.dataDisplay.GetPageText(page): |
---|
212 | AddAAChiralRestraint(restrData['Chiral']) |
---|
213 | elif 'Torsion' in G2frame.dataDisplay.GetPageText(page): |
---|
214 | AddAATorsionRestraint(restrData['Torsion']) |
---|
215 | elif 'Rama' in G2frame.dataDisplay.GetPageText(page): |
---|
216 | AddAARamaRestraint(restrData['Rama']) |
---|
217 | |
---|
218 | def AddBondRestraint(bondRestData): |
---|
219 | Lists = {'origin':[],'target':[]} |
---|
220 | for listName in ['origin','target']: |
---|
221 | dlg = wx.MultiChoiceDialog(G2frame,'Bond restraint '+listName+' for '+General['Name'], |
---|
222 | 'Select bond restraint '+listName+' atoms',Names) |
---|
223 | if dlg.ShowModal() == wx.ID_OK: |
---|
224 | sel = dlg.GetSelections() |
---|
225 | for x in sel: |
---|
226 | if 'all' in Names[x]: |
---|
227 | allType = Types[x] |
---|
228 | for name,Type,coords,id in zip(Names,Types,Coords,Ids): |
---|
229 | if Type == allType and 'all' not in name: |
---|
230 | Lists[listName].append([id,Type,coords]) |
---|
231 | else: |
---|
232 | Lists[listName].append([Ids[x],Types[x],Coords[x],]) |
---|
233 | else: |
---|
234 | break |
---|
235 | if len(Lists['origin']) and len(Lists['target']): |
---|
236 | bond = 1.54 |
---|
237 | dlg = G2G.SingleFloatDialog(G2frame,'Distance','Enter restraint distance for bond',bond,[0.01,4.],'%.4f') |
---|
238 | if dlg.ShowModal() == wx.ID_OK: |
---|
239 | bond = dlg.GetValue() |
---|
240 | dlg.Destroy() |
---|
241 | Factor = bondRestData['Range'] |
---|
242 | indices = (-2,-1,0,1,2) |
---|
243 | Units = np.array([[h,k,l] for h in indices for k in indices for l in indices]) |
---|
244 | origAtoms = Lists['origin'] |
---|
245 | targAtoms = Lists['target'] |
---|
246 | dlg = wx.ProgressDialog("Generating bond restraints","Processed origin atoms",len(origAtoms), |
---|
247 | style = wx.PD_ELAPSED_TIME|wx.PD_AUTO_HIDE|wx.PD_REMAINING_TIME) |
---|
248 | try: |
---|
249 | Norig = 0 |
---|
250 | for Oid,Otype,Ocoord in origAtoms: |
---|
251 | Norig += 1 |
---|
252 | dlg.Update(Norig) |
---|
253 | for Tid,Ttype,Tcoord in targAtoms: |
---|
254 | if 'macro' in General['Type']: |
---|
255 | result = [[Tcoord,1,[0,0,0]],] |
---|
256 | else: |
---|
257 | result = G2spc.GenAtom(Tcoord,SGData,False,Move=False) |
---|
258 | for Txyz,Top,Tunit in result: |
---|
259 | Dx = (Txyz-np.array(Ocoord))+Units |
---|
260 | dx = np.inner(Amat,Dx) |
---|
261 | dist = ma.masked_less(np.sqrt(np.sum(dx**2,axis=0)),bond/Factor) |
---|
262 | IndB = ma.nonzero(ma.masked_greater(dist,bond*Factor)) |
---|
263 | if np.any(IndB): |
---|
264 | for indb in IndB: |
---|
265 | for i in range(len(indb)): |
---|
266 | unit = Units[indb][i]+Tunit |
---|
267 | if np.any(unit): |
---|
268 | Topstr = '%d+%d,%d,%d'%(Top,unit[0],unit[1],unit[2]) |
---|
269 | else: |
---|
270 | Topstr = str(Top) |
---|
271 | newBond = [[Oid,Tid],['1',Topstr],bond,0.01] |
---|
272 | if newBond not in bondRestData['Bonds']: |
---|
273 | bondRestData['Bonds'].append(newBond) |
---|
274 | finally: |
---|
275 | dlg.Destroy() |
---|
276 | UpdateBondRestr(bondRestData) |
---|
277 | |
---|
278 | def AddAABondRestraint(bondRestData): |
---|
279 | macro = getMacroFile('bond') |
---|
280 | if not macro: |
---|
281 | return |
---|
282 | macStr = macro.readline() |
---|
283 | atoms = zip(Names,Coords,Ids) |
---|
284 | |
---|
285 | Factor = bondRestData['Range'] |
---|
286 | while macStr: |
---|
287 | items = macStr.split() |
---|
288 | if 'F' in items[0]: |
---|
289 | restrData['Bond']['wtFactor'] = float(items[1]) |
---|
290 | elif 'S' in items[0]: |
---|
291 | oIds = [] |
---|
292 | oCoords = [] |
---|
293 | tIds = [] |
---|
294 | tCoords = [] |
---|
295 | res = items[1] |
---|
296 | dist = float(items[2]) |
---|
297 | esd = float(items[3]) |
---|
298 | oAtm,tAtm = items[4:6] |
---|
299 | for Name,coords,Id in atoms: |
---|
300 | names = Name.split() |
---|
301 | if res == '*' or res in names[0]: |
---|
302 | if oAtm == names[2]: |
---|
303 | oIds.append(Id) |
---|
304 | oCoords.append(np.array(coords)) |
---|
305 | if tAtm == names[2]: |
---|
306 | tIds.append(Id) |
---|
307 | tCoords.append(np.array(coords)) |
---|
308 | for i,[oId,oCoord] in enumerate(zip(oIds,oCoords)): |
---|
309 | for tId,tCoord in zip(tIds,tCoords)[i:]: |
---|
310 | obsd = np.sqrt(np.sum(np.inner(Amat,tCoord-oCoord)**2)) |
---|
311 | if dist/Factor < obsd < dist*Factor: |
---|
312 | newBond = [[oId,tId],['1','1'],dist,esd] |
---|
313 | if newBond not in bondRestData['Bonds']: |
---|
314 | bondRestData['Bonds'].append(newBond) |
---|
315 | macStr = macro.readline() |
---|
316 | macro.close() |
---|
317 | UpdateBondRestr(bondRestData) |
---|
318 | |
---|
319 | def AddAngleRestraint(angleRestData): |
---|
320 | Radii = dict(zip(General['AtomTypes'],zip(General['BondRadii'],General['AngleRadii']))) |
---|
321 | Lists = {'A-atom':[],'B-atom':[],'C-atom':[]} |
---|
322 | for listName in ['A-atom','B-atom']: |
---|
323 | dlg = wx.MultiChoiceDialog(G2frame,'Select '+listName+' for angle A-B-C for '+General['Name'] , |
---|
324 | 'Select angle restraint '+listName,Names) |
---|
325 | if dlg.ShowModal() == wx.ID_OK: |
---|
326 | sel = dlg.GetSelections() |
---|
327 | for x in sel: |
---|
328 | if 'all' in Names[x]: |
---|
329 | allType = Types[x] |
---|
330 | for name,Type,coords,id in zip(Names,Types,Coords,Ids): |
---|
331 | if Type == allType and 'all' not in name: |
---|
332 | if 'A' in listName: |
---|
333 | Lists[listName].append(Type) |
---|
334 | else: |
---|
335 | Lists[listName].append([id,Type,coords]) |
---|
336 | else: |
---|
337 | if 'A' in listName: |
---|
338 | Lists[listName].append(Types[x]) |
---|
339 | else: |
---|
340 | Lists[listName].append([Ids[x],Types[x],Coords[x],]) |
---|
341 | else: |
---|
342 | break |
---|
343 | targAtoms = [[Ids[x+iBeg],Types[x+iBeg],Coords[x+iBeg]] for x in range(len(Names[iBeg:]))] |
---|
344 | if len(Lists['B-atom']): |
---|
345 | value = 109.54 |
---|
346 | dlg = G2G.SingleFloatDialog(G2frame,'Angle','Enter restraint angle ',value,[30.,180.],'%.2f') |
---|
347 | if dlg.ShowModal() == wx.ID_OK: |
---|
348 | value = dlg.GetValue() |
---|
349 | dlg.Destroy() |
---|
350 | |
---|
351 | Factor = angleRestData['Range'] |
---|
352 | indices = (-2,-1,0,1,2) |
---|
353 | Units = np.array([[h,k,l] for h in indices for k in indices for l in indices]) |
---|
354 | VectA = [] |
---|
355 | for Oid,Otype,Ocoord in Lists['B-atom']: |
---|
356 | IndBlist = [] |
---|
357 | VectB = [] |
---|
358 | for Tid,Ttype,Tcoord in targAtoms: |
---|
359 | result = G2spc.GenAtom(Tcoord,SGData,False,Move=False) |
---|
360 | BsumR = (Radii[Otype][0]+Radii[Ttype][0])*Factor |
---|
361 | AsumR = (Radii[Otype][1]+Radii[Ttype][1])*Factor |
---|
362 | for Txyz,Top,Tunit in result: |
---|
363 | Dx = (Txyz-Ocoord)+Units |
---|
364 | dx = np.inner(Amat,Dx) |
---|
365 | dist = ma.masked_less(np.sqrt(np.sum(dx**2,axis=0)),0.5) |
---|
366 | IndB = ma.nonzero(ma.masked_greater(dist,BsumR)) |
---|
367 | if np.any(IndB): |
---|
368 | for indb in IndB: |
---|
369 | for i in range(len(indb)): |
---|
370 | if str(dx.T[indb][i]) not in IndBlist: |
---|
371 | IndBlist.append(str(dx.T[indb][i])) |
---|
372 | unit = Units[indb][i]+Tunit |
---|
373 | if np.any(unit): |
---|
374 | Topstr = '%d+%d,%d,%d'%(Top,unit[0],unit[1],unit[2]) |
---|
375 | else: |
---|
376 | Topstr = str(Top) |
---|
377 | Dist = ma.getdata(dist[indb])[i] |
---|
378 | if (Dist-AsumR) <= 0.: |
---|
379 | VectB.append([Oid,'1',Ocoord,Ttype,Tid,Topstr,Tcoord,Dist]) |
---|
380 | VectA.append(VectB) |
---|
381 | for Vects in VectA: |
---|
382 | for i,vecta in enumerate(Vects): |
---|
383 | for vectb in Vects[:i]: |
---|
384 | if vecta[3] in Lists['A-atom']: |
---|
385 | ids = [vecta[4],vecta[0],vectb[4]] |
---|
386 | ops = [vecta[5],vecta[1],vectb[5]] |
---|
387 | angle = [ids,ops,value,1.0] |
---|
388 | if angle not in angleRestData['Angles']: |
---|
389 | angleRestData['Angles'].append(angle) |
---|
390 | UpdateAngleRestr(angleRestData) |
---|
391 | |
---|
392 | def AddAAAngleRestraint(angleRestData): |
---|
393 | macro = getMacroFile('angle') |
---|
394 | if not macro: |
---|
395 | return |
---|
396 | atoms = zip(Names,Ids) |
---|
397 | macStr = macro.readline() |
---|
398 | while macStr: |
---|
399 | items = macStr.split() |
---|
400 | if 'F' in items[0]: |
---|
401 | restrData['Angle']['wtFactor'] = float(items[1]) |
---|
402 | elif 'S' in items[0]: |
---|
403 | res = items[1] |
---|
404 | value = float(items[2]) |
---|
405 | esd = float(items[3]) |
---|
406 | Atms = items[4:7] |
---|
407 | pAtms = ['','',''] |
---|
408 | for i,atm in enumerate(Atms): |
---|
409 | if '+' in atm: |
---|
410 | pAtms[i] = atm.strip('+') |
---|
411 | ids = np.array([0,0,0]) |
---|
412 | rNum = -1 |
---|
413 | for name,id in atoms: |
---|
414 | names = name.split() |
---|
415 | tNum = int(names[0].split(':')[0]) |
---|
416 | if res in names[0]: |
---|
417 | try: |
---|
418 | ipos = Atms.index(names[2]) |
---|
419 | ids[ipos] = id |
---|
420 | except ValueError: |
---|
421 | continue |
---|
422 | elif res == '*': |
---|
423 | try: |
---|
424 | ipos = Atms.index(names[2]) |
---|
425 | if not np.all(ids): |
---|
426 | rNum = int(names[0].split(':')[0]) |
---|
427 | ids[ipos] = id |
---|
428 | except ValueError: |
---|
429 | try: |
---|
430 | if tNum == rNum+1: |
---|
431 | ipos = pAtms.index(names[2]) |
---|
432 | ids[ipos] = id |
---|
433 | except ValueError: |
---|
434 | continue |
---|
435 | if np.all(ids): |
---|
436 | angle = [list(ids),['1','1','1'],value,esd] |
---|
437 | if angle not in angleRestData['Angles']: |
---|
438 | angleRestData['Angles'].append(angle) |
---|
439 | ids = np.array([0,0,0]) |
---|
440 | macStr = macro.readline() |
---|
441 | macro.close() |
---|
442 | UpdateAngleRestr(angleRestData) |
---|
443 | |
---|
444 | def AddPlaneRestraint(restrData): |
---|
445 | ids = [] |
---|
446 | dlg = wx.MultiChoiceDialog(G2frame,'Select 4 or more atoms for plane in '+General['Name'], |
---|
447 | 'Select 4+ atoms',Names[iBeg:]) |
---|
448 | if dlg.ShowModal() == wx.ID_OK: |
---|
449 | sel = dlg.GetSelections() |
---|
450 | if len(sel) > 3: |
---|
451 | for x in sel: |
---|
452 | ids.append(Ids[x+iBeg]) |
---|
453 | ops = ['1' for i in range(len(sel))] |
---|
454 | plane = [ids,ops,0.0,0.01] |
---|
455 | if plane not in restrData['Planes']: |
---|
456 | restrData['Planes'].append(plane) |
---|
457 | else: |
---|
458 | print '**** ERROR - not enough atoms for a plane restraint - try again ****' |
---|
459 | UpdatePlaneRestr(restrData) |
---|
460 | |
---|
461 | def AddAAPlaneRestraint(planeRestData): |
---|
462 | macro = getMacroFile('plane') |
---|
463 | if not macro: |
---|
464 | return |
---|
465 | atoms = zip(Names,Ids) |
---|
466 | macStr = macro.readline() |
---|
467 | while macStr: |
---|
468 | items = macStr.split() |
---|
469 | if 'F' in items[0]: |
---|
470 | restrData['Plane']['wtFactor'] = float(items[1]) |
---|
471 | elif 'S' in items[0]: |
---|
472 | res = items[1] |
---|
473 | esd = float(items[2]) |
---|
474 | Atms = items[3:] |
---|
475 | pAtms = ['' for i in Atms] |
---|
476 | for i,atm in enumerate(Atms): |
---|
477 | if '+' in atm: |
---|
478 | pAtms[i] = atm.strip('+') |
---|
479 | rNum = -1 |
---|
480 | ids = np.zeros(len(Atms)) |
---|
481 | ops = ['1' for i in range(len(Atms))] |
---|
482 | for name,id in atoms: |
---|
483 | names = name.split() |
---|
484 | tNum = int(names[0].split(':')[0]) |
---|
485 | if res in names[0]: |
---|
486 | try: |
---|
487 | ipos = Atms.index(names[2]) |
---|
488 | ids[ipos] = id |
---|
489 | except ValueError: |
---|
490 | continue |
---|
491 | elif res == '*': |
---|
492 | try: |
---|
493 | ipos = Atms.index(names[2]) |
---|
494 | if not np.all(ids): |
---|
495 | rNum = int(names[0].split(':')[0]) |
---|
496 | ids[ipos] = id |
---|
497 | except ValueError: |
---|
498 | try: |
---|
499 | if tNum == rNum+1: |
---|
500 | ipos = pAtms.index(names[2]) |
---|
501 | ids[ipos] = id |
---|
502 | except ValueError: |
---|
503 | continue |
---|
504 | if np.all(ids): |
---|
505 | plane = [list(ids),ops,0.0,esd] |
---|
506 | if plane not in planeRestData['Planes']: |
---|
507 | planeRestData['Planes'].append(plane) |
---|
508 | ids = np.zeros(len(Atms)) |
---|
509 | macStr = macro.readline() |
---|
510 | macro.close() |
---|
511 | UpdatePlaneRestr(planeRestData) |
---|
512 | |
---|
513 | def AddAAChiralRestraint(chiralRestData): |
---|
514 | macro = getMacroFile('chiral') |
---|
515 | if not macro: |
---|
516 | return |
---|
517 | atoms = zip(Names,Ids) |
---|
518 | macStr = macro.readline() |
---|
519 | while macStr: |
---|
520 | items = macStr.split() |
---|
521 | if 'F' in items[0]: |
---|
522 | restrData['Chiral']['wtFactor'] = float(items[1]) |
---|
523 | elif 'S' in items[0]: |
---|
524 | res = items[1] |
---|
525 | value = float(items[2]) |
---|
526 | esd = float(items[3]) |
---|
527 | Atms = items[4:8] |
---|
528 | ids = np.array([0,0,0,0]) |
---|
529 | for name,id in atoms: |
---|
530 | names = name.split() |
---|
531 | if res in names[0]: |
---|
532 | try: |
---|
533 | ipos = Atms.index(names[2]) |
---|
534 | ids[ipos] = id |
---|
535 | except ValueError: |
---|
536 | pass |
---|
537 | if np.all(ids): |
---|
538 | chiral = [list(ids),['1','1','1','1'],value,esd] |
---|
539 | if chiral not in chiralRestData['Volumes']: |
---|
540 | chiralRestData['Volumes'].append(chiral) |
---|
541 | ids = np.array([0,0,0,0]) |
---|
542 | macStr = macro.readline() |
---|
543 | macro.close() |
---|
544 | UpdateChiralRestr(chiralRestData) |
---|
545 | |
---|
546 | def makeChains(Names,Ids): |
---|
547 | Chains = {} |
---|
548 | atoms = zip(Names,Ids) |
---|
549 | for name,id in atoms: |
---|
550 | items = name.split() |
---|
551 | rnum,res = items[0].split(':') |
---|
552 | if items[1] not in Chains: |
---|
553 | Residues = {} |
---|
554 | Chains[items[1]] = Residues |
---|
555 | if int(rnum) not in Residues: |
---|
556 | Residues[int(rnum)] = [] |
---|
557 | Residues[int(rnum)].append([res,items[2],id]) |
---|
558 | return Chains |
---|
559 | |
---|
560 | def AddAATorsionRestraint(torsionRestData): |
---|
561 | macro = getMacroFile('torsion') |
---|
562 | if not macro: |
---|
563 | return |
---|
564 | Chains = makeChains(Names,Ids) |
---|
565 | macStr = macro.readline()[:-1] |
---|
566 | while macStr: |
---|
567 | items = macStr.split() |
---|
568 | if 'F' in items[0]: |
---|
569 | restrData['Torsion']['wtFactor'] = float(items[1]) |
---|
570 | elif 'A' in items[0]: |
---|
571 | name = items[10] |
---|
572 | coeff = np.zeros(9) |
---|
573 | for i,item in enumerate(items[1:10]): |
---|
574 | coeff[i] = float(item) |
---|
575 | torsionRestData['Coeff'][name] = coeff |
---|
576 | elif 'S' in items[0]: |
---|
577 | Name = items[1] |
---|
578 | Res = items[2] |
---|
579 | Esd = float(items[3]) |
---|
580 | Atms = items[4:8] |
---|
581 | pAtms = ['','','',''] |
---|
582 | for i,atm in enumerate(Atms): |
---|
583 | if '+' in atm: |
---|
584 | pAtms[i] = atm.strip('+') |
---|
585 | ids = np.array([0,0,0,0]) |
---|
586 | chains = Chains.keys() |
---|
587 | chains.sort() |
---|
588 | for chain in chains: |
---|
589 | residues = Chains[chain].keys() |
---|
590 | residues.sort() |
---|
591 | for residue in residues: |
---|
592 | if residue == residues[-1] and Res == '*': |
---|
593 | continue |
---|
594 | if Res != '*': |
---|
595 | for res,name,id in Chains[chain][residue]: |
---|
596 | if Res == res: |
---|
597 | try: |
---|
598 | ipos = Atms.index(name) |
---|
599 | ids[ipos] = id |
---|
600 | except ValueError: |
---|
601 | continue |
---|
602 | else: |
---|
603 | try: |
---|
604 | for res,name,id in Chains[chain][residue]: |
---|
605 | try: |
---|
606 | ipos = Atms.index(name) |
---|
607 | ids[ipos] = id |
---|
608 | except ValueError: |
---|
609 | continue |
---|
610 | for res,name,id in Chains[chain][residue+1]: |
---|
611 | try: |
---|
612 | ipos = pAtms.index(name) |
---|
613 | ids[ipos] = id |
---|
614 | except ValueError: |
---|
615 | continue |
---|
616 | except KeyError: |
---|
617 | continue |
---|
618 | if np.all(ids): |
---|
619 | torsion = [list(ids),['1','1','1','1'],Name,Esd] |
---|
620 | if torsion not in torsionRestData['Torsions']: |
---|
621 | torsionRestData['Torsions'].append(torsion) |
---|
622 | ids = np.array([0,0,0,0]) |
---|
623 | macStr = macro.readline() |
---|
624 | macro.close() |
---|
625 | UpdateTorsionRestr(torsionRestData) |
---|
626 | |
---|
627 | def AddAARamaRestraint(ramaRestData): |
---|
628 | macro = getMacroFile('Ramachandran') |
---|
629 | if not macro: |
---|
630 | return |
---|
631 | Chains = makeChains(Names,Ids) |
---|
632 | macStr = macro.readline() |
---|
633 | while macStr: |
---|
634 | items = macStr.split() |
---|
635 | if 'F' in items[0]: |
---|
636 | restrData['Rama']['wtFactor'] = float(items[1]) |
---|
637 | elif 'A' in items[0]: |
---|
638 | nTerms = int(items[1]) |
---|
639 | name = items[2] |
---|
640 | coeff = np.zeros((nTerms,6)) |
---|
641 | for i in range(nTerms): |
---|
642 | macStr = macro.readline() |
---|
643 | items = macStr.split() |
---|
644 | for j,val in enumerate(items): |
---|
645 | coeff[i][j] = float(val) |
---|
646 | ramaRestData['Coeff'][name] = coeff |
---|
647 | elif 'S' in items[0]: |
---|
648 | Name = items[1] |
---|
649 | Res = items[2] |
---|
650 | Esd = float(items[3]) |
---|
651 | Atms = items[4:9] |
---|
652 | mAtms = ['','','','',''] |
---|
653 | pAtms = ['','','','',''] |
---|
654 | for i,atm in enumerate(Atms): |
---|
655 | if '+' in atm: |
---|
656 | pAtms[i] = atm.strip('+') |
---|
657 | elif '-' in atm: |
---|
658 | mAtms[i] = atm.strip('-') |
---|
659 | ids = np.array([0,0,0,0,0]) |
---|
660 | chains = Chains.keys() |
---|
661 | chains.sort() |
---|
662 | for chain in chains: |
---|
663 | residues = Chains[chain].keys() |
---|
664 | residues.sort() |
---|
665 | if not (any(mAtms) or any(pAtms)): |
---|
666 | for residue in residues: |
---|
667 | for res,name,id in Chains[chain][residue]: |
---|
668 | if Res == res: |
---|
669 | try: |
---|
670 | ipos = Atms.index(name) |
---|
671 | ids[ipos] = id |
---|
672 | except ValueError: |
---|
673 | continue |
---|
674 | if np.all(ids): |
---|
675 | rama = [list(ids),['1','1','1','1','1'],Name,Esd] |
---|
676 | if rama not in ramaRestData['Ramas']: |
---|
677 | ramaRestData['Ramas'].append(rama) |
---|
678 | ids = np.array([0,0,0,0,0]) |
---|
679 | else: |
---|
680 | for residue in residues[1:-1]: |
---|
681 | try: |
---|
682 | for res,name,id in Chains[chain][residue-1]: |
---|
683 | try: |
---|
684 | ipos = mAtms.index(name) |
---|
685 | ids[ipos] = id |
---|
686 | except ValueError: |
---|
687 | continue |
---|
688 | for res,name,id in Chains[chain][residue+1]: |
---|
689 | try: |
---|
690 | ipos = pAtms.index(name) |
---|
691 | ids[ipos] = id |
---|
692 | except ValueError: |
---|
693 | continue |
---|
694 | for res,name,id in Chains[chain][residue]: |
---|
695 | if Res == res: |
---|
696 | try: |
---|
697 | ipos = Atms.index(name) |
---|
698 | ids[ipos] = id |
---|
699 | except ValueError: |
---|
700 | continue |
---|
701 | if np.all(ids): |
---|
702 | rama = [list(ids),['1','1','1','1','1'],Name,Esd] |
---|
703 | if rama not in ramaRestData['Ramas']: |
---|
704 | ramaRestData['Ramas'].append(rama) |
---|
705 | ids = np.array([0,0,0,0,0]) |
---|
706 | except KeyError: |
---|
707 | continue |
---|
708 | macStr = macro.readline() |
---|
709 | macro.close() |
---|
710 | UpdateRamaRestr(ramaRestData) |
---|
711 | |
---|
712 | def AddChemCompRestraint(chemcompRestData): |
---|
713 | ids = [] |
---|
714 | factors = [] |
---|
715 | dlg = wx.MultiChoiceDialog(G2frame,'Select atoms for chemical restraint in '+General['Name'], |
---|
716 | 'Select atoms',Names) |
---|
717 | if dlg.ShowModal() == wx.ID_OK: |
---|
718 | sel = dlg.GetSelections() |
---|
719 | for x in sel: |
---|
720 | if 'all' in Names[x]: |
---|
721 | allType = Types[x] |
---|
722 | for name,Type,id in zip(Names,Types,Ids): |
---|
723 | if Type == allType and 'all' not in name: |
---|
724 | ids.append(id) |
---|
725 | factors.append(1.0) |
---|
726 | else: |
---|
727 | ids.append(Ids[x]) |
---|
728 | factors.append(1.0) |
---|
729 | dlg.Destroy() |
---|
730 | if len(ids) > 0: |
---|
731 | value = 1.0 |
---|
732 | dlg = G2G.SingleFloatDialog(G2frame,'Angle','Enter unit cell sum ',value,[-1.e6,1.e6],'%.2f') |
---|
733 | if dlg.ShowModal() == wx.ID_OK: |
---|
734 | value = dlg.GetValue() |
---|
735 | comp = [ids,factors,value,0.01] |
---|
736 | if comp not in chemcompRestData['Sites']: |
---|
737 | chemcompRestData['Sites'].append(comp) |
---|
738 | UpdateChemcompRestr(chemcompRestData) |
---|
739 | else: |
---|
740 | print '**** ERROR - not enough atoms for a composition restraint - try again ****' |
---|
741 | |
---|
742 | def AddTextureRestraint(textureRestData): |
---|
743 | dlg = wx.TextEntryDialog(G2frame,'Enter h k l for pole figure restraint','Enter HKL','') |
---|
744 | if dlg.ShowModal() == wx.ID_OK: |
---|
745 | text = dlg.GetValue() |
---|
746 | vals = text.split() |
---|
747 | try: |
---|
748 | hkl = [int(vals[i]) for i in range(3)] |
---|
749 | texture = [hkl,24,0.01,False,1.0] |
---|
750 | if texture not in textureRestData['HKLs']: |
---|
751 | textureRestData['HKLs'].append(texture) |
---|
752 | UpdateTextureRestr(textureRestData) |
---|
753 | except (ValueError,IndexError): |
---|
754 | print '**** ERROR - bad input of h k l - try again ****' |
---|
755 | dlg.Destroy() |
---|
756 | |
---|
757 | def WtBox(wind,restData): |
---|
758 | if 'Range' not in restData: restData['Range'] = 1.1 #patch |
---|
759 | |
---|
760 | def OnUseData(event): |
---|
761 | Obj = event.GetEventObject() |
---|
762 | restData['Use'] = Obj.GetValue() |
---|
763 | |
---|
764 | wtBox = wx.BoxSizer(wx.HORIZONTAL) |
---|
765 | wtBox.Add(wx.StaticText(wind,-1,'Restraint weight factor:'),0,wx.ALIGN_CENTER_VERTICAL) |
---|
766 | wtfactor = G2G.ValidatedTxtCtrl(wind,restData,'wtFactor',nDig=(10,2),typeHint=float) |
---|
767 | wtBox.Add(wtfactor,0,wx.ALIGN_CENTER_VERTICAL) |
---|
768 | useData = wx.CheckBox(wind,-1,label=' Use?') |
---|
769 | useData.Bind(wx.EVT_CHECKBOX, OnUseData) |
---|
770 | useData.SetValue(restData['Use']) |
---|
771 | wtBox.Add(useData,0,wx.ALIGN_CENTER_VERTICAL) |
---|
772 | if 'Bonds' in restData or 'Angles' in restData: |
---|
773 | wtBox.Add(wx.StaticText(wind,-1,' Search range:'),0,wx.ALIGN_CENTER_VERTICAL) |
---|
774 | sRange = G2G.ValidatedTxtCtrl(wind,restData,'Range',nDig=(10,2),typeHint=float) |
---|
775 | wtBox.Add(sRange,0,wx.ALIGN_CENTER_VERTICAL) |
---|
776 | wtBox.Add(wx.StaticText(wind,-1,'(x sum(atom radii)'),0,wx.ALIGN_CENTER_VERTICAL) |
---|
777 | return wtBox |
---|
778 | |
---|
779 | def OnRowSelect(event): |
---|
780 | r,c = event.GetRow(),event.GetCol() |
---|
781 | Obj = event.GetEventObject() |
---|
782 | if r < 0 and c < 0: |
---|
783 | if Obj.IsSelection(): |
---|
784 | Obj.ClearSelection() |
---|
785 | else: |
---|
786 | for row in range(Obj.GetNumberRows()): |
---|
787 | Obj.SelectRow(row,True) |
---|
788 | elif c < 0: #only row clicks |
---|
789 | if event.ControlDown(): |
---|
790 | if r in Obj.GetSelectedRows(): |
---|
791 | Obj.DeselectRow(r) |
---|
792 | else: |
---|
793 | Obj.SelectRow(r,True) |
---|
794 | elif event.ShiftDown(): |
---|
795 | indxs = Obj.GetSelectedRows() |
---|
796 | Obj.ClearSelection() |
---|
797 | ibeg = 0 |
---|
798 | if indxs: |
---|
799 | ibeg = indxs[-1] |
---|
800 | for row in range(ibeg,r+1): |
---|
801 | Obj.SelectRow(row,True) |
---|
802 | else: |
---|
803 | Obj.ClearSelection() |
---|
804 | Obj.SelectRow(r,True) |
---|
805 | |
---|
806 | |
---|
807 | def UpdateBondRestr(bondRestData): |
---|
808 | |
---|
809 | def OnCellChange(event): |
---|
810 | r,c = event.GetRow(),event.GetCol() |
---|
811 | try: |
---|
812 | new = float(bondTable.GetValue(r,c)) |
---|
813 | if new <= 0.: |
---|
814 | raise ValueError |
---|
815 | bondRestData['Bonds'][r][c] = new |
---|
816 | except ValueError: |
---|
817 | pass |
---|
818 | wx.CallAfter(UpdateBondRestr,bondRestData) |
---|
819 | |
---|
820 | def OnChangeValue(event): |
---|
821 | rows = GetSelectedRows(Bonds) |
---|
822 | if not rows: |
---|
823 | return |
---|
824 | Bonds.ClearSelection() |
---|
825 | val = bondList[rows[0]][2] |
---|
826 | dlg = G2G.SingleFloatDialog(G2frame,'New value','Enter new value for bond',val,[0.,5.],'%.4f') |
---|
827 | if dlg.ShowModal() == wx.ID_OK: |
---|
828 | parm = dlg.GetValue() |
---|
829 | for r in rows: |
---|
830 | bondRestData['Bonds'][r][2] = parm |
---|
831 | dlg.Destroy() |
---|
832 | UpdateBondRestr(bondRestData) |
---|
833 | |
---|
834 | def OnChangeEsd(event): |
---|
835 | rows = GetSelectedRows(Bonds) |
---|
836 | if not rows: |
---|
837 | return |
---|
838 | Bonds.ClearSelection() |
---|
839 | val = bondList[rows[0]][3] |
---|
840 | dlg = G2G.SingleFloatDialog(G2frame,'New value','Enter new esd for bond',val,[0.,1.],'%.4f') |
---|
841 | if dlg.ShowModal() == wx.ID_OK: |
---|
842 | parm = dlg.GetValue() |
---|
843 | for r in rows: |
---|
844 | bondRestData['Bonds'][r][3] = parm |
---|
845 | dlg.Destroy() |
---|
846 | UpdateBondRestr(bondRestData) |
---|
847 | |
---|
848 | def OnDeleteRestraint(event): |
---|
849 | rows = GetSelectedRows(Bonds) |
---|
850 | if not rows: |
---|
851 | return |
---|
852 | Bonds.ClearSelection() |
---|
853 | rows.sort() |
---|
854 | rows.reverse() |
---|
855 | for row in rows: |
---|
856 | bondList.remove(bondList[row]) |
---|
857 | UpdateBondRestr(bondRestData) |
---|
858 | |
---|
859 | #BondRestr.DestroyChildren() |
---|
860 | if BondRestr.GetSizer(): |
---|
861 | BondRestr.GetSizer().Clear(True) |
---|
862 | mainSizer = wx.BoxSizer(wx.VERTICAL) |
---|
863 | mainSizer.Add((5,5),0) |
---|
864 | mainSizer.Add(WtBox(BondRestr,bondRestData),0,wx.ALIGN_CENTER_VERTICAL) |
---|
865 | |
---|
866 | bondList = bondRestData['Bonds'] |
---|
867 | if len(bondList) and len(bondList[0]) == 6: #patch |
---|
868 | bondList = bondRestData['Bonds'] = [] |
---|
869 | if len(bondList): |
---|
870 | table = [] |
---|
871 | rowLabels = [] |
---|
872 | bad = [] |
---|
873 | chisq = 0. |
---|
874 | Types = [wg.GRID_VALUE_STRING,]+4*[wg.GRID_VALUE_FLOAT+':10,3',] |
---|
875 | if 'macro' in General['Type']: |
---|
876 | colLabels = ['(res) A - (res) B','calc','target','esd','delt/sig'] |
---|
877 | for i,[indx,ops,obs,esd] in enumerate(bondList): |
---|
878 | try: |
---|
879 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,0,4) |
---|
880 | name = '' |
---|
881 | for atom in atoms: |
---|
882 | name += '('+atom[1]+atom[0].strip()+atom[2]+') '+atom[3]+' - ' |
---|
883 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
884 | calc = G2mth.getRestDist(XYZ,Amat) |
---|
885 | chisq += bondRestData['wtFactor']*((obs-calc)/esd)**2 |
---|
886 | table.append([name[:-3],calc,obs,esd,(obs-calc)/esd]) |
---|
887 | rowLabels.append(str(i)) |
---|
888 | except KeyError: |
---|
889 | print '**** WARNING - missing atom - restraint deleted ****' |
---|
890 | bad.append(i) |
---|
891 | else: |
---|
892 | colLabels = ['A+SymOp - B+SymOp','calc','target','esd','delt/sig'] |
---|
893 | for i,[indx,ops,obs,esd] in enumerate(bondList): |
---|
894 | try: |
---|
895 | names = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,ct-1) |
---|
896 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
897 | XYZ = G2mth.getSyXYZ(XYZ,ops,SGData) |
---|
898 | calc = G2mth.getRestDist(XYZ,Amat) |
---|
899 | chisq += bondRestData['wtFactor']*((obs-calc)/esd)**2 |
---|
900 | table.append([names[0]+'+('+ops[0]+') - '+names[1]+'+('+ops[1]+')',calc,obs,esd,(obs-calc)/esd]) |
---|
901 | rowLabels.append(str(i)) |
---|
902 | except KeyError: |
---|
903 | print '**** WARNING - missing atom - restraint deleted ****' |
---|
904 | bad.append(i) |
---|
905 | if len(bad): |
---|
906 | bad.reverse() |
---|
907 | for ibad in bad: |
---|
908 | del bondList[ibad] |
---|
909 | bondTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types) |
---|
910 | Bonds = G2G.GSGrid(BondRestr) |
---|
911 | Bonds.SetTable(bondTable, True) |
---|
912 | Bonds.AutoSizeColumns(False) |
---|
913 | for r in range(len(bondList)): |
---|
914 | for c in [0,1,4]: |
---|
915 | Bonds.SetReadOnly(r,c,True) |
---|
916 | Bonds.SetCellStyle(r,c,VERY_LIGHT_GREY,True) |
---|
917 | Bonds.Bind(wg.EVT_GRID_LABEL_LEFT_CLICK,OnRowSelect) |
---|
918 | Bonds.Bind(wg.EVT_GRID_CELL_CHANGE, OnCellChange) |
---|
919 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnDeleteRestraint, id=G2gd.wxID_RESTDELETE) |
---|
920 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnChangeValue, id=G2gd.wxID_RESRCHANGEVAL) |
---|
921 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnChangeEsd, id=G2gd.wxID_RESTCHANGEESD) |
---|
922 | mainSizer.Add(wx.StaticText(BondRestr,-1, |
---|
923 | 'Bond restraints: sum(wt*(delt/sig)^2) = %.2f, mean(wt*(delt/sig)^2) = %.2f' \ |
---|
924 | %(chisq,chisq/len(bondList))),0,wx.ALIGN_CENTER_VERTICAL) |
---|
925 | mainSizer.Add(Bonds,0,) |
---|
926 | else: |
---|
927 | mainSizer.Add(wx.StaticText(BondRestr,-1,'No bond distance restraints for this phase'),0,) |
---|
928 | |
---|
929 | BondRestr.SetSizer(mainSizer) |
---|
930 | Size = mainSizer.Fit(G2frame.dataFrame) |
---|
931 | Size[0] = 600 |
---|
932 | Size[1] = min(Size[1]+50,500) #make room for tab, but not too big |
---|
933 | BondRestr.SetSize(Size) |
---|
934 | BondRestr.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1) |
---|
935 | G2frame.dataFrame.setSizePosLeft(Size) |
---|
936 | |
---|
937 | def UpdateAngleRestr(angleRestData): |
---|
938 | |
---|
939 | def OnCellChange(event): |
---|
940 | r,c = event.GetRow(),event.GetCol() |
---|
941 | try: |
---|
942 | new = float(angleTable.GetValue(r,c)) |
---|
943 | if new <= 0. or new > 180.: |
---|
944 | raise ValueError |
---|
945 | angleRestData['Angles'][r][c] = new |
---|
946 | except ValueError: |
---|
947 | pass |
---|
948 | wx.CallAfter(UpdateAngleRestr,angleRestData) |
---|
949 | |
---|
950 | def OnChangeValue(event): |
---|
951 | rows = GetSelectedRows(Angles) |
---|
952 | if not rows: |
---|
953 | return |
---|
954 | Angles.ClearSelection() |
---|
955 | val = angleList[rows[0]][2] |
---|
956 | dlg = G2G.SingleFloatDialog(G2frame,'New value','Enter new value for angle',val,[0.,360.],'%.2f') |
---|
957 | if dlg.ShowModal() == wx.ID_OK: |
---|
958 | parm = dlg.GetValue() |
---|
959 | for r in rows: |
---|
960 | angleRestData['Angles'][r][2] = parm |
---|
961 | dlg.Destroy() |
---|
962 | UpdateAngleRestr(angleRestData) |
---|
963 | |
---|
964 | def OnChangeEsd(event): |
---|
965 | rows = GetSelectedRows(Angles) |
---|
966 | if not rows: |
---|
967 | return |
---|
968 | Angles.ClearSelection() |
---|
969 | val = angleList[rows[0]][3] |
---|
970 | dlg = G2G.SingleFloatDialog(G2frame,'New value','Enter new esd for angle',val,[0.,5.],'%.2f') |
---|
971 | if dlg.ShowModal() == wx.ID_OK: |
---|
972 | parm = dlg.GetValue() |
---|
973 | for r in rows: |
---|
974 | angleRestData['Angles'][r][3] = parm |
---|
975 | dlg.Destroy() |
---|
976 | UpdateAngleRestr(angleRestData) |
---|
977 | |
---|
978 | def OnDeleteRestraint(event): |
---|
979 | rows = GetSelectedRows(Angles) |
---|
980 | if not rows: |
---|
981 | return |
---|
982 | rows.sort() |
---|
983 | rows.reverse() |
---|
984 | for row in rows: |
---|
985 | angleList.remove(angleList[row]) |
---|
986 | UpdateAngleRestr(angleRestData) |
---|
987 | |
---|
988 | #AngleRestr.DestroyChildren() |
---|
989 | if AngleRestr.GetSizer(): |
---|
990 | AngleRestr.GetSizer().Clear(True) |
---|
991 | mainSizer = wx.BoxSizer(wx.VERTICAL) |
---|
992 | mainSizer.Add((5,5),0) |
---|
993 | mainSizer.Add(WtBox(AngleRestr,angleRestData),0,wx.ALIGN_CENTER_VERTICAL) |
---|
994 | |
---|
995 | angleList = angleRestData['Angles'] |
---|
996 | if len(angleList): |
---|
997 | table = [] |
---|
998 | rowLabels = [] |
---|
999 | bad = [] |
---|
1000 | chisq = 0. |
---|
1001 | Types = [wg.GRID_VALUE_STRING,]+4*[wg.GRID_VALUE_FLOAT+':10,2',] |
---|
1002 | if 'macro' in General['Type']: |
---|
1003 | colLabels = ['(res) A - (res) B - (res) C','calc','target','esd','delt/sig'] |
---|
1004 | for i,[indx,ops,obs,esd] in enumerate(angleList): |
---|
1005 | try: |
---|
1006 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,0,4) |
---|
1007 | name = '' |
---|
1008 | for atom in atoms: |
---|
1009 | name += '('+atom[1]+atom[0].strip()+atom[2]+') '+atom[3]+' - ' |
---|
1010 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
1011 | calc = G2mth.getRestAngle(XYZ,Amat) |
---|
1012 | chisq += angleRestData['wtFactor']*((obs-calc)/esd)**2 |
---|
1013 | table.append([name[:-3],calc,obs,esd,(obs-calc)/esd]) |
---|
1014 | rowLabels.append(str(i)) |
---|
1015 | except KeyError: |
---|
1016 | print '**** WARNING - missing atom - restraint deleted ****' |
---|
1017 | bad.append(i) |
---|
1018 | else: |
---|
1019 | colLabels = ['A+SymOp - B+SymOp - C+SymOp','calc','target','esd','delt/sig'] |
---|
1020 | for i,[indx,ops,obs,esd] in enumerate(angleList): |
---|
1021 | try: |
---|
1022 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,ct-1) |
---|
1023 | name = atoms[0]+'+('+ops[0]+') - '+atoms[1]+'+('+ops[1]+') - '+atoms[2]+ \ |
---|
1024 | '+('+ops[2]+')' |
---|
1025 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
1026 | XYZ = G2mth.getSyXYZ(XYZ,ops,SGData) |
---|
1027 | calc = G2mth.getRestAngle(XYZ,Amat) |
---|
1028 | chisq += angleRestData['wtFactor']*((obs-calc)/esd)**2 |
---|
1029 | table.append([name,calc,obs,esd,(obs-calc)/esd]) |
---|
1030 | rowLabels.append(str(i)) |
---|
1031 | except KeyError: |
---|
1032 | print '**** WARNING - missing atom - restraint deleted ****' |
---|
1033 | bad.append(i) |
---|
1034 | if len(bad): |
---|
1035 | bad.reverse() |
---|
1036 | for ibad in bad: |
---|
1037 | del angleList[ibad] |
---|
1038 | angleTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types) |
---|
1039 | Angles = G2G.GSGrid(AngleRestr) |
---|
1040 | Angles.SetTable(angleTable, True) |
---|
1041 | Angles.AutoSizeColumns(False) |
---|
1042 | for r in range(len(angleList)): |
---|
1043 | for c in [0,1,4]: |
---|
1044 | Angles.SetReadOnly(r,c,True) |
---|
1045 | Angles.SetCellStyle(r,c,VERY_LIGHT_GREY,True) |
---|
1046 | Angles.Bind(wg.EVT_GRID_LABEL_LEFT_CLICK,OnRowSelect) |
---|
1047 | Angles.Bind(wg.EVT_GRID_CELL_CHANGE, OnCellChange) |
---|
1048 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnDeleteRestraint, id=G2gd.wxID_RESTDELETE) |
---|
1049 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnChangeValue, id=G2gd.wxID_RESRCHANGEVAL) |
---|
1050 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnChangeEsd, id=G2gd.wxID_RESTCHANGEESD) |
---|
1051 | mainSizer.Add(wx.StaticText(AngleRestr,-1, |
---|
1052 | 'Angle restraints: sum(wt*(delt/sig)^2) = %.2f, mean(wt*(delt/sig)^2) = %.2f' \ |
---|
1053 | %(chisq,chisq/len(angleList))),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1054 | mainSizer.Add(Angles,0,) |
---|
1055 | else: |
---|
1056 | mainSizer.Add(wx.StaticText(AngleRestr,-1,'No bond angle restraints for this phase'),0,) |
---|
1057 | |
---|
1058 | AngleRestr.SetSizer(mainSizer) |
---|
1059 | Size = mainSizer.Fit(G2frame.dataFrame) |
---|
1060 | Size[0] = 600 |
---|
1061 | Size[1] = min(Size[1]+50,500) #make room for tab, but not too big |
---|
1062 | AngleRestr.SetSize(Size) |
---|
1063 | AngleRestr.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1) |
---|
1064 | G2frame.dataFrame.setSizePosLeft(Size) |
---|
1065 | |
---|
1066 | def UpdatePlaneRestr(planeRestData): |
---|
1067 | |
---|
1068 | items = G2frame.dataFrame.RestraintEdit.GetMenuItems() |
---|
1069 | for item in items: |
---|
1070 | if item.GetLabel() in ['Change value']: |
---|
1071 | item.Enable(False) |
---|
1072 | |
---|
1073 | def OnCellChange(event): |
---|
1074 | r,c = event.GetRow(),event.GetCol() |
---|
1075 | try: |
---|
1076 | new = float(planeTable.GetValue(r,c)) |
---|
1077 | if new <= 0.: |
---|
1078 | raise ValueError |
---|
1079 | planeRestData['Planes'][r][c] = new |
---|
1080 | except ValueError: |
---|
1081 | pass |
---|
1082 | wx.CallAfter(UpdatePlaneRestr,planeRestData) |
---|
1083 | |
---|
1084 | def OnChangeEsd(event): |
---|
1085 | rows = GetSelectedRows(Planes) |
---|
1086 | if not rows: |
---|
1087 | return |
---|
1088 | Planes.ClearSelection() |
---|
1089 | val = planeList[rows[0]][3] |
---|
1090 | dlg = G2G.SingleFloatDialog(G2frame,'New value','Enter new esd for plane',val,[0.,5.],'%.2f') |
---|
1091 | if dlg.ShowModal() == wx.ID_OK: |
---|
1092 | parm = dlg.GetValue() |
---|
1093 | for r in rows: |
---|
1094 | planeRestData['Planes'][r][3] = parm |
---|
1095 | dlg.Destroy() |
---|
1096 | UpdatePlaneRestr(planeRestData) |
---|
1097 | |
---|
1098 | def OnDeleteRestraint(event): |
---|
1099 | rows = GetSelectedRows(Planes) |
---|
1100 | if not rows: |
---|
1101 | return |
---|
1102 | rows.sort() |
---|
1103 | rows.reverse() |
---|
1104 | for row in rows: |
---|
1105 | planeList.remove(planeList[row]) |
---|
1106 | UpdatePlaneRestr(planeRestData) |
---|
1107 | |
---|
1108 | #PlaneRestr.DestroyChildren() |
---|
1109 | if PlaneRestr.GetSizer(): |
---|
1110 | PlaneRestr.GetSizer().Clear(True) |
---|
1111 | mainSizer = wx.BoxSizer(wx.VERTICAL) |
---|
1112 | mainSizer.Add((5,5),0) |
---|
1113 | mainSizer.Add(WtBox(PlaneRestr,planeRestData),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1114 | |
---|
1115 | planeList = planeRestData['Planes'] |
---|
1116 | if len(planeList): |
---|
1117 | table = [] |
---|
1118 | rowLabels = [] |
---|
1119 | bad = [] |
---|
1120 | chisq = 0. |
---|
1121 | Types = [wg.GRID_VALUE_STRING,]+3*[wg.GRID_VALUE_FLOAT+':10,2',] |
---|
1122 | if 'macro' in General['Type']: |
---|
1123 | colLabels = ['(res) atom','calc','target','esd'] |
---|
1124 | for i,[indx,ops,obs,esd] in enumerate(planeList): |
---|
1125 | try: |
---|
1126 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,0,4) |
---|
1127 | name = '' |
---|
1128 | for a,atom in enumerate(atoms): |
---|
1129 | name += '('+atom[1]+atom[0].strip()+atom[2]+') '+atom[3]+' - ' |
---|
1130 | if (a+1)%3 == 0: |
---|
1131 | name += '\n' |
---|
1132 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
1133 | calc = G2mth.getRestPlane(XYZ,Amat) |
---|
1134 | chisq += planeRestData['wtFactor']*((calc)/esd)**2 |
---|
1135 | table.append([name[:-3],calc,obs,esd]) |
---|
1136 | rowLabels.append(str(i)) |
---|
1137 | except KeyError: |
---|
1138 | print '**** WARNING - missing atom - restraint deleted ****' |
---|
1139 | bad.append(i) |
---|
1140 | else: |
---|
1141 | colLabels = ['atom+SymOp','calc','target','esd'] |
---|
1142 | for i,[indx,ops,obs,esd] in enumerate(planeList): |
---|
1143 | try: |
---|
1144 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,ct-1) |
---|
1145 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
1146 | XYZ = G2mth.getSyXYZ(XYZ,ops,SGData) |
---|
1147 | calc = G2mth.getRestPlane(XYZ,Amat) |
---|
1148 | chisq += planeRestData['wtFactor']*((calc)/esd)**2 |
---|
1149 | name = '' |
---|
1150 | for a,atom in enumerate(atoms): |
---|
1151 | name += atom+'+ ('+ops[a]+'),' |
---|
1152 | if (a+1)%3 == 0: |
---|
1153 | name += '\n' |
---|
1154 | table.append([name[:-1],calc,obs,esd]) |
---|
1155 | rowLabels.append(str(i)) |
---|
1156 | except KeyError: |
---|
1157 | print '**** WARNING - missing atom - restraint deleted ****' |
---|
1158 | bad.append(i) |
---|
1159 | if len(bad): |
---|
1160 | bad.reverse() |
---|
1161 | for ibad in bad: |
---|
1162 | del planeList[ibad] |
---|
1163 | planeTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types) |
---|
1164 | Planes = G2G.GSGrid(PlaneRestr) |
---|
1165 | Planes.SetTable(planeTable, True) |
---|
1166 | Planes.AutoSizeColumns(False) |
---|
1167 | Planes.AutoSizeRows(False) |
---|
1168 | for r in range(len(planeList)): |
---|
1169 | for c in range(3): |
---|
1170 | Planes.SetReadOnly(r,c,True) |
---|
1171 | Planes.SetCellStyle(r,c,VERY_LIGHT_GREY,True) |
---|
1172 | Planes.Bind(wg.EVT_GRID_LABEL_LEFT_CLICK,OnRowSelect) |
---|
1173 | Planes.Bind(wg.EVT_GRID_CELL_CHANGE, OnCellChange) |
---|
1174 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnDeleteRestraint, id=G2gd.wxID_RESTDELETE) |
---|
1175 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnChangeEsd, id=G2gd.wxID_RESTCHANGEESD) |
---|
1176 | mainSizer.Add(wx.StaticText(PlaneRestr,-1, |
---|
1177 | 'Plane restraints: sum(wt*(delt/sig)^2) = %.2f, mean(wt*(delt/sig)^2) = %.2f' \ |
---|
1178 | %(chisq,chisq/len(planeList))),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1179 | mainSizer.Add(Planes,0,) |
---|
1180 | else: |
---|
1181 | mainSizer.Add(wx.StaticText(PlaneRestr,-1,'No plane restraints for this phase'),0,) |
---|
1182 | |
---|
1183 | PlaneRestr.SetSizer(mainSizer) |
---|
1184 | Size = mainSizer.Fit(G2frame.dataFrame) |
---|
1185 | Size[0] = 600 |
---|
1186 | Size[1] = min(Size[1]+50,500) #make room for tab, but not too big |
---|
1187 | PlaneRestr.SetSize(Size) |
---|
1188 | PlaneRestr.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1) |
---|
1189 | G2frame.dataFrame.setSizePosLeft(Size) |
---|
1190 | |
---|
1191 | def UpdateChiralRestr(chiralRestData): |
---|
1192 | |
---|
1193 | def OnCellChange(event): |
---|
1194 | r,c = event.GetRow(),event.GetCol() |
---|
1195 | try: |
---|
1196 | new = float(volumeTable.GetValue(r,c)) |
---|
1197 | if new <= 0.: |
---|
1198 | raise ValueError |
---|
1199 | chiralRestData['Volumes'][r][c] = new |
---|
1200 | except ValueError: |
---|
1201 | pass |
---|
1202 | wx.CallAfter(UpdateChiralRestr,chiralRestData) |
---|
1203 | |
---|
1204 | def OnDeleteRestraint(event): |
---|
1205 | rows = GetSelectedRows(Volumes) |
---|
1206 | if not rows: |
---|
1207 | return |
---|
1208 | rows.sort() |
---|
1209 | rows.reverse() |
---|
1210 | for row in rows: |
---|
1211 | volumeList.remove(volumeList[row]) |
---|
1212 | UpdateChiralRestr(chiralRestData) |
---|
1213 | |
---|
1214 | def OnChangeValue(event): |
---|
1215 | rows = GetSelectedRows(Volumes) |
---|
1216 | if not rows: |
---|
1217 | return |
---|
1218 | Volumes.ClearSelection() |
---|
1219 | val = volumeList[rows[0]][2] |
---|
1220 | dlg = G2G.SingleFloatDialog(G2frame,'New value','Enter new value for chiral volume',val,[0.,360.],'%.2f') |
---|
1221 | if dlg.ShowModal() == wx.ID_OK: |
---|
1222 | parm = dlg.GetValue() |
---|
1223 | for r in rows: |
---|
1224 | chiralRestData['Volumes'][r][2] = parm |
---|
1225 | dlg.Destroy() |
---|
1226 | UpdateChiralRestr(chiralRestData) |
---|
1227 | |
---|
1228 | def OnChangeEsd(event): |
---|
1229 | rows = GetSelectedRows(Volumes) |
---|
1230 | if not rows: |
---|
1231 | return |
---|
1232 | Volumes.ClearSelection() |
---|
1233 | val = volumeList[rows[0]][3] |
---|
1234 | dlg = G2G.SingleFloatDialog(G2frame,'New value','Enter new esd for chiral volume',val,[0.,5.],'%.2f') |
---|
1235 | if dlg.ShowModal() == wx.ID_OK: |
---|
1236 | parm = dlg.GetValue() |
---|
1237 | for r in rows: |
---|
1238 | chiralRestData['Volumes'][r][3] = parm |
---|
1239 | dlg.Destroy() |
---|
1240 | UpdateChiralRestr(chiralRestData) |
---|
1241 | |
---|
1242 | #ChiralRestr.DestroyChildren() |
---|
1243 | if ChiralRestr.GetSizer(): |
---|
1244 | ChiralRestr.GetSizer().Clear(True) |
---|
1245 | mainSizer = wx.BoxSizer(wx.VERTICAL) |
---|
1246 | mainSizer.Add((5,5),0) |
---|
1247 | mainSizer.Add(WtBox(ChiralRestr,chiralRestData),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1248 | |
---|
1249 | volumeList = chiralRestData['Volumes'] |
---|
1250 | if len(volumeList): |
---|
1251 | table = [] |
---|
1252 | rowLabels = [] |
---|
1253 | bad = [] |
---|
1254 | chisq = 0. |
---|
1255 | Types = [wg.GRID_VALUE_STRING,]+4*[wg.GRID_VALUE_FLOAT+':10,2',] |
---|
1256 | if 'macro' in General['Type']: |
---|
1257 | colLabels = ['(res) O (res) A (res) B (res) C','calc','target','esd','delt/sig'] |
---|
1258 | for i,[indx,ops,obs,esd] in enumerate(volumeList): |
---|
1259 | try: |
---|
1260 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,0,4) |
---|
1261 | name = '' |
---|
1262 | for atom in atoms: |
---|
1263 | name += '('+atom[1]+atom[0].strip()+atom[2]+') '+atom[3]+' ' |
---|
1264 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
1265 | calc = G2mth.getRestChiral(XYZ,Amat) |
---|
1266 | chisq += chiralRestData['wtFactor']*((obs-calc)/esd)**2 |
---|
1267 | table.append([name,calc,obs,esd,(obs-calc)/esd]) |
---|
1268 | rowLabels.append(str(i)) |
---|
1269 | except KeyError: |
---|
1270 | print '**** WARNING - missing atom - restraint deleted ****' |
---|
1271 | bad.append(i) |
---|
1272 | else: |
---|
1273 | colLabels = ['O+SymOp A+SymOp B+SymOp C+SymOp)','calc','target','esd','delt/sig'] |
---|
1274 | for i,[indx,ops,obs,esd] in enumerate(volumeList): |
---|
1275 | try: |
---|
1276 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,ct-1) |
---|
1277 | name = atoms[0]+'+('+ops[0]+') '+atoms[1]+'+('+ops[1]+') '+atoms[2]+ \ |
---|
1278 | '+('+ops[2]+') '+atoms[3]+'+('+ops[3]+')' |
---|
1279 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
1280 | XYZ = G2mth.getSyXYZ(XYZ,ops,SGData) |
---|
1281 | calc = G2mth.getRestChiral(XYZ,Amat) |
---|
1282 | chisq += chiralRestData['wtFactor']*((obs-calc)/esd)**2 |
---|
1283 | table.append([name,calc,obs,esd,(obs-calc)/esd]) |
---|
1284 | rowLabels.append(str(i)) |
---|
1285 | except KeyError: |
---|
1286 | print '**** WARNING - missing atom - restraint deleted ****' |
---|
1287 | bad.append(i) |
---|
1288 | if len(bad): |
---|
1289 | bad.reverse() |
---|
1290 | for ibad in bad: |
---|
1291 | del volumeList[ibad] |
---|
1292 | volumeTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types) |
---|
1293 | Volumes = G2G.GSGrid(ChiralRestr) |
---|
1294 | Volumes.SetTable(volumeTable, True) |
---|
1295 | Volumes.AutoSizeColumns(False) |
---|
1296 | for r in range(len(volumeList)): |
---|
1297 | for c in [0,1,4]: |
---|
1298 | Volumes.SetReadOnly(r,c,True) |
---|
1299 | Volumes.SetCellStyle(r,c,VERY_LIGHT_GREY,True) |
---|
1300 | Volumes.Bind(wg.EVT_GRID_LABEL_LEFT_CLICK,OnRowSelect) |
---|
1301 | Volumes.Bind(wg.EVT_GRID_CELL_CHANGE, OnCellChange) |
---|
1302 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnDeleteRestraint, id=G2gd.wxID_RESTDELETE) |
---|
1303 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnChangeValue, id=G2gd.wxID_RESRCHANGEVAL) |
---|
1304 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnChangeEsd, id=G2gd.wxID_RESTCHANGEESD) |
---|
1305 | mainSizer.Add(wx.StaticText(ChiralRestr,-1, |
---|
1306 | 'Chiral volume restraints: sum(wt*(delt/sig)^2) = %.2f, mean(wt*(delt/sig)^2) = %.2f' \ |
---|
1307 | %(chisq,chisq/len(volumeList))),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1308 | mainSizer.Add(Volumes,0,) |
---|
1309 | else: |
---|
1310 | mainSizer.Add(wx.StaticText(ChiralRestr,-1,'No chiral volume restraints for this phase'),0,) |
---|
1311 | |
---|
1312 | ChiralRestr.SetSizer(mainSizer) |
---|
1313 | Size = mainSizer.Fit(G2frame.dataFrame) |
---|
1314 | Size[0] = 600 |
---|
1315 | Size[1] = min(Size[1]+50,500) #make room for tab, but not too big |
---|
1316 | ChiralRestr.SetSize(Size) |
---|
1317 | ChiralRestr.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1) |
---|
1318 | G2frame.dataFrame.setSizePosLeft(Size) |
---|
1319 | |
---|
1320 | def UpdateTorsionRestr(torsionRestData): |
---|
1321 | |
---|
1322 | def OnCellChange(event): |
---|
1323 | r,c = event.GetRow(),event.GetCol() |
---|
1324 | try: |
---|
1325 | new = float(torsionTable.GetValue(r,c)) |
---|
1326 | if new <= 0. or new > 5.: |
---|
1327 | raise ValueError |
---|
1328 | torsionRestData['Torsions'][r][3] = new #only esd is editable |
---|
1329 | except ValueError: |
---|
1330 | pass |
---|
1331 | wx.CallAfter(UpdateTorsionRestr,torsionRestData) |
---|
1332 | |
---|
1333 | def OnDeleteRestraint(event): |
---|
1334 | rows = GetSelectedRows(TorsionRestr.Torsions) |
---|
1335 | if not rows: |
---|
1336 | return |
---|
1337 | rows.sort() |
---|
1338 | rows.reverse() |
---|
1339 | for row in rows: |
---|
1340 | torsionList.remove(torsionList[row]) |
---|
1341 | wx.CallAfter(UpdateTorsionRestr,torsionRestData) |
---|
1342 | |
---|
1343 | def OnChangeEsd(event): |
---|
1344 | rows = GetSelectedRows(TorsionRestr.Torsions) |
---|
1345 | if not rows: |
---|
1346 | return |
---|
1347 | TorsionRestr.Torsions.ClearSelection() |
---|
1348 | val = torsionList[rows[0]][4] |
---|
1349 | dlg = G2G.SingleFloatDialog(G2frame,'New value','Enter new esd for torsion restraints',val,[0.,5.],'%.2f') |
---|
1350 | if dlg.ShowModal() == wx.ID_OK: |
---|
1351 | parm = dlg.GetValue() |
---|
1352 | for r in rows: |
---|
1353 | torsionRestData['Torsions'][r][4] = parm |
---|
1354 | dlg.Destroy() |
---|
1355 | wx.CallAfter(UpdateTorsionRestr,torsionRestData) |
---|
1356 | |
---|
1357 | #TorsionRestr.DestroyChildren() |
---|
1358 | if TorsionRestr.GetSizer(): |
---|
1359 | TorsionRestr.GetSizer().Clear(True) |
---|
1360 | mainSizer = wx.BoxSizer(wx.VERTICAL) |
---|
1361 | mainSizer.Add((5,5),0) |
---|
1362 | mainSizer.Add(WtBox(TorsionRestr,torsionRestData),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1363 | |
---|
1364 | coeffDict = torsionRestData['Coeff'] |
---|
1365 | torsionList = torsionRestData['Torsions'] |
---|
1366 | if len(torsionList): |
---|
1367 | table = [] |
---|
1368 | rowLabels = [] |
---|
1369 | bad = [] |
---|
1370 | chisq = 0. |
---|
1371 | Types = 2*[wg.GRID_VALUE_STRING,]+4*[wg.GRID_VALUE_FLOAT+':10,2',] |
---|
1372 | if 'macro' in General['Type']: |
---|
1373 | colLabels = ['(res) A B C D','coef name','torsion','obs E','restr','esd'] |
---|
1374 | for i,[indx,ops,cofName,esd] in enumerate(torsionList): |
---|
1375 | try: |
---|
1376 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,0,4) |
---|
1377 | name = '('+atoms[2][1]+atoms[2][0].strip()+atoms[2][2]+')' |
---|
1378 | for atom in atoms: |
---|
1379 | name += ' '+atom[3] |
---|
1380 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
1381 | tor = G2mth.getRestTorsion(XYZ,Amat) |
---|
1382 | restr,calc = G2mth.calcTorsionEnergy(tor,coeffDict[cofName]) |
---|
1383 | chisq += torsionRestData['wtFactor']*(restr/esd)**2 |
---|
1384 | table.append([name,cofName,tor,calc,restr,esd]) |
---|
1385 | rowLabels.append(str(i)) |
---|
1386 | except KeyError: |
---|
1387 | print '**** WARNING - missing atom - restraint deleted ****' |
---|
1388 | bad.append(i) |
---|
1389 | if len(bad): |
---|
1390 | bad.reverse() |
---|
1391 | for ibad in bad: |
---|
1392 | del torsionList[ibad] |
---|
1393 | torsionTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types) |
---|
1394 | TorsionRestr.Torsions = G2G.GSGrid(TorsionRestr) |
---|
1395 | TorsionRestr.Torsions.SetTable(torsionTable, True) |
---|
1396 | TorsionRestr.Torsions.AutoSizeColumns(False) |
---|
1397 | for r in range(len(torsionList)): |
---|
1398 | for c in range(5): |
---|
1399 | TorsionRestr.Torsions.SetReadOnly(r,c,True) |
---|
1400 | TorsionRestr.Torsions.SetCellStyle(r,c,VERY_LIGHT_GREY,True) |
---|
1401 | TorsionRestr.Torsions.Bind(wg.EVT_GRID_LABEL_LEFT_CLICK,OnRowSelect) |
---|
1402 | TorsionRestr.Torsions.Bind(wg.EVT_GRID_CELL_CHANGE, OnCellChange) |
---|
1403 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnDeleteRestraint, id=G2gd.wxID_RESTDELETE) |
---|
1404 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnChangeEsd, id=G2gd.wxID_RESTCHANGEESD) |
---|
1405 | mainSizer.Add(wx.StaticText(TorsionRestr,-1, |
---|
1406 | 'Torsion restraints: sum(wt*(delt/sig)^2) = %.2f, mean(wt*(delt/sig)^2) = %.2f' \ |
---|
1407 | %(chisq,chisq/len(torsionList))),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1408 | mainSizer.Add(TorsionRestr.Torsions,0,) |
---|
1409 | |
---|
1410 | mainSizer.Add((5,5)) |
---|
1411 | mainSizer.Add(wx.StaticText(TorsionRestr,-1,'Torsion function coefficients:'),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1412 | table = [] |
---|
1413 | rowLabels = [] |
---|
1414 | Types = 9*[wg.GRID_VALUE_FLOAT+':10,4',] |
---|
1415 | colLabels = ['Mag A','Pos A','Width A','Mag B','Pos B','Width B','Mag C','Pos C','Width C'] |
---|
1416 | for item in coeffDict: |
---|
1417 | rowLabels.append(item) |
---|
1418 | table.append(coeffDict[item]) |
---|
1419 | coeffTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types) |
---|
1420 | Coeff = G2G.GSGrid(TorsionRestr) |
---|
1421 | Coeff.SetTable(coeffTable, True) |
---|
1422 | Coeff.AutoSizeColumns(False) |
---|
1423 | for r in range(len(coeffDict)): |
---|
1424 | for c in range(9): |
---|
1425 | Coeff.SetReadOnly(r,c,True) |
---|
1426 | Coeff.SetCellStyle(r,c,VERY_LIGHT_GREY,True) |
---|
1427 | mainSizer.Add(Coeff,0,) |
---|
1428 | else: |
---|
1429 | mainSizer.Add(wx.StaticText(TorsionRestr,-1,'No torsion restraints for this phase'),0,) |
---|
1430 | |
---|
1431 | TorsionRestr.SetSizer(mainSizer) |
---|
1432 | Size = mainSizer.Fit(G2frame.dataFrame) |
---|
1433 | Size[0] = 600 |
---|
1434 | Size[1] = min(Size[1]+50,500) #make room for tab, but not too big |
---|
1435 | TorsionRestr.SetSize(Size) |
---|
1436 | TorsionRestr.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1) |
---|
1437 | G2frame.dataFrame.setSizePosLeft(Size) |
---|
1438 | |
---|
1439 | def UpdateRamaRestr(ramaRestData): |
---|
1440 | |
---|
1441 | def OnCellChange(event): |
---|
1442 | r,c = event.GetRow(),event.GetCol() |
---|
1443 | try: |
---|
1444 | new = float(ramaTable.GetValue(r,c)) |
---|
1445 | if new <= 0. or new > 5.: |
---|
1446 | raise ValueError |
---|
1447 | ramaRestData['Ramas'][r][4] = new #only esd is editable |
---|
1448 | except ValueError: |
---|
1449 | pass |
---|
1450 | wx.CallAfter(UpdateRamaRestr,ramaRestData) |
---|
1451 | |
---|
1452 | def OnDeleteRestraint(event): |
---|
1453 | rows = GetSelectedRows(RamaRestr.Ramas) |
---|
1454 | if not rows: |
---|
1455 | return |
---|
1456 | rows.sort() |
---|
1457 | rows.reverse() |
---|
1458 | for row in rows: |
---|
1459 | ramaList.remove(ramaList[row]) |
---|
1460 | UpdateRamaRestr(ramaRestData) |
---|
1461 | |
---|
1462 | def OnChangeEsd(event): |
---|
1463 | rows = GetSelectedRows(RamaRestr.Ramas) |
---|
1464 | if not rows: |
---|
1465 | return |
---|
1466 | RamaRestr.Ramas.ClearSelection() |
---|
1467 | val = ramaList[rows[0]][4] |
---|
1468 | dlg = G2G.SingleFloatDialog(G2frame,'New value','Enter new esd for energy',val,[0.,5.],'%.2f') |
---|
1469 | if dlg.ShowModal() == wx.ID_OK: |
---|
1470 | parm = dlg.GetValue() |
---|
1471 | for r in rows: |
---|
1472 | ramaRestData['Ramas'][r][4] = parm |
---|
1473 | dlg.Destroy() |
---|
1474 | UpdateRamaRestr(ramaRestData) |
---|
1475 | |
---|
1476 | #RamaRestr.DestroyChildren() |
---|
1477 | if RamaRestr.GetSizer(): |
---|
1478 | RamaRestr.GetSizer().Clear(True) |
---|
1479 | mainSizer = wx.BoxSizer(wx.VERTICAL) |
---|
1480 | mainSizer.Add((5,5),0) |
---|
1481 | mainSizer.Add(WtBox(RamaRestr,ramaRestData),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1482 | |
---|
1483 | ramaList = ramaRestData['Ramas'] |
---|
1484 | coeffDict = ramaRestData['Coeff'] |
---|
1485 | if len(ramaList): |
---|
1486 | mainSizer.Add(wx.StaticText(RamaRestr,-1,'Ramachandran restraints:'),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1487 | table = [] |
---|
1488 | rowLabels = [] |
---|
1489 | bad = [] |
---|
1490 | chisq = 0. |
---|
1491 | Types = 2*[wg.GRID_VALUE_STRING,]+5*[wg.GRID_VALUE_FLOAT+':10,2',] |
---|
1492 | if 'macro' in General['Type']: |
---|
1493 | colLabels = ['(res) A B C D E','coef name','phi','psi','obs E','restr','esd'] |
---|
1494 | for i,[indx,ops,cofName,esd] in enumerate(ramaList): |
---|
1495 | try: |
---|
1496 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,0,4) |
---|
1497 | name = '('+atoms[3][1]+atoms[3][0].strip()+atoms[3][2]+')' |
---|
1498 | for atom in atoms: |
---|
1499 | name += ' '+atom[3] |
---|
1500 | XYZ = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cx,3)) |
---|
1501 | phi,psi = G2mth.getRestRama(XYZ,Amat) |
---|
1502 | restr,calc = G2mth.calcRamaEnergy(phi,psi,coeffDict[cofName]) |
---|
1503 | chisq += ramaRestData['wtFactor']*(restr/esd)**2 |
---|
1504 | table.append([name,cofName,phi,psi,calc,restr,esd]) |
---|
1505 | rowLabels.append(str(i)) |
---|
1506 | except KeyError: |
---|
1507 | print '**** WARNING - missing atom - restraint deleted ****' |
---|
1508 | bad.append(i) |
---|
1509 | if len(bad): |
---|
1510 | bad.reverse() |
---|
1511 | for ibad in bad: |
---|
1512 | del ramaList[ibad] |
---|
1513 | ramaTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types) |
---|
1514 | RamaRestr.Ramas = G2G.GSGrid(RamaRestr) |
---|
1515 | RamaRestr.Ramas.SetTable(ramaTable, True) |
---|
1516 | RamaRestr.Ramas.AutoSizeColumns(False) |
---|
1517 | for r in range(len(ramaList)): |
---|
1518 | for c in range(6): |
---|
1519 | RamaRestr.Ramas.SetReadOnly(r,c,True) |
---|
1520 | RamaRestr.Ramas.SetCellStyle(r,c,VERY_LIGHT_GREY,True) |
---|
1521 | RamaRestr.Ramas.Bind(wg.EVT_GRID_LABEL_LEFT_CLICK,OnRowSelect) |
---|
1522 | RamaRestr.Ramas.Bind(wg.EVT_GRID_CELL_CHANGE, OnCellChange) |
---|
1523 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnDeleteRestraint, id=G2gd.wxID_RESTDELETE) |
---|
1524 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnChangeEsd, id=G2gd.wxID_RESTCHANGEESD) |
---|
1525 | mainSizer.Add(wx.StaticText(RamaRestr,-1, |
---|
1526 | 'Ramachandran restraints: sum(wt*(delt/sig)^2) = %.2f, mean(wt*(delt/sig)^2) = %.2f' \ |
---|
1527 | %(chisq,chisq/len(ramaList))),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1528 | mainSizer.Add(RamaRestr.Ramas,0,) |
---|
1529 | else: |
---|
1530 | mainSizer.Add(wx.StaticText(RamaRestr,-1,'No Ramachandran restraints for this phase'),0,) |
---|
1531 | mainSizer.Add((5,5)) |
---|
1532 | mainSizer.Add(wx.StaticText(RamaRestr,-1,'Ramachandran function coefficients:'),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1533 | if len(coeffDict): |
---|
1534 | table = [] |
---|
1535 | rowLabels = [] |
---|
1536 | Types = 6*[wg.GRID_VALUE_FLOAT+':10,4',] |
---|
1537 | colLabels = ['Mag','Pos phi','Pos psi','sig(phi)','sig(psi)','sig(cov)'] |
---|
1538 | for item in coeffDict: |
---|
1539 | for i,term in enumerate(coeffDict[item]): |
---|
1540 | rowLabels.append(item+' term:'+str(i)) |
---|
1541 | table.append(term) |
---|
1542 | coeffTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types) |
---|
1543 | Coeff = G2G.GSGrid(RamaRestr) |
---|
1544 | Coeff.SetTable(coeffTable, True) |
---|
1545 | Coeff.AutoSizeColumns(False) |
---|
1546 | for r in range(Coeff.GetNumberRows()): |
---|
1547 | for c in range(6): |
---|
1548 | Coeff.SetReadOnly(r,c,True) |
---|
1549 | Coeff.SetCellStyle(r,c,VERY_LIGHT_GREY,True) |
---|
1550 | mainSizer.Add(Coeff,0,) |
---|
1551 | |
---|
1552 | RamaRestr.SetSizer(mainSizer) |
---|
1553 | Size = mainSizer.Fit(G2frame.dataFrame) |
---|
1554 | Size[0] = 600 |
---|
1555 | Size[1] = min(Size[1]+50,500) #make room for tab, but not too big |
---|
1556 | RamaRestr.SetSize(Size) |
---|
1557 | RamaRestr.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1) |
---|
1558 | G2frame.dataFrame.setSizePosLeft(Size) |
---|
1559 | |
---|
1560 | def UpdateChemcompRestr(chemcompRestData): |
---|
1561 | |
---|
1562 | def OnCellChange(event): |
---|
1563 | r,c = event.GetRow(),event.GetCol() |
---|
1564 | rowLabl = ChemComps.GetRowLabelValue(r) |
---|
1565 | row = int(rowLabl.split(':')[1]) |
---|
1566 | if 'Restr' in rowLabl: |
---|
1567 | try: |
---|
1568 | new = float(chemcompTable.GetValue(r,c)) |
---|
1569 | chemcompRestData['Sites'][row][c-2] = new #obsd or esd |
---|
1570 | except ValueError: |
---|
1571 | pass |
---|
1572 | else: |
---|
1573 | try: |
---|
1574 | new = float(chemcompTable.GetValue(r,c)) |
---|
1575 | id = int(rowLabl.split(':')[2]) |
---|
1576 | chemcompRestData['Sites'][row][1][id] = new #only factor |
---|
1577 | except ValueError: |
---|
1578 | pass |
---|
1579 | wx.CallAfter(UpdateChemcompRestr,chemcompRestData) |
---|
1580 | |
---|
1581 | def OnDeleteRestraint(event): |
---|
1582 | #rows = GetSelectedRows() |
---|
1583 | rows = ChemComps.GetSelectedRows() |
---|
1584 | if not rows: |
---|
1585 | return |
---|
1586 | rowLabl = ChemComps.GetRowLabelValue(r) |
---|
1587 | row = int(rowLabl.split(':')[1]) |
---|
1588 | if 'Restr' in rowLabl: |
---|
1589 | del chemcompList[row] |
---|
1590 | else: |
---|
1591 | term = int(rowLabl.split(':')[2]) |
---|
1592 | del chemcompList[row][0][term] |
---|
1593 | del chemcompList[row][1][term] |
---|
1594 | UpdateChemcompRestr(chemcompRestData) |
---|
1595 | |
---|
1596 | def OnChangeValue(event): |
---|
1597 | rows = GetSelectedRows(ChemComps) |
---|
1598 | if not rows: |
---|
1599 | return |
---|
1600 | ChemComps.ClearSelection() |
---|
1601 | dlg = G2G.SingleFloatDialog(G2frame,'New value', |
---|
1602 | 'Enter new value for restraint multiplier',1.0,[-1.e6,1.e6],'%.2f') |
---|
1603 | if dlg.ShowModal() == wx.ID_OK: |
---|
1604 | parm = dlg.GetValue() |
---|
1605 | for r in rows: |
---|
1606 | rowLabl = ChemComps.GetRowLabelValue(r) |
---|
1607 | if 'term' in rowLabl: |
---|
1608 | items = rowLabl.split(':') |
---|
1609 | chemcompRestData['Sites'][int(items[1])][1][int(items[2])] = parm |
---|
1610 | dlg.Destroy() |
---|
1611 | UpdateChemcompRestr(chemcompRestData) |
---|
1612 | |
---|
1613 | #ChemCompRestr.DestroyChildren() |
---|
1614 | if ChemCompRestr.GetSizer(): |
---|
1615 | ChemCompRestr.GetSizer().Clear(True) |
---|
1616 | mainSizer = wx.BoxSizer(wx.VERTICAL) |
---|
1617 | mainSizer.Add((5,5),0) |
---|
1618 | mainSizer.Add(WtBox(ChemCompRestr,chemcompRestData),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1619 | mainSizer.Add(wx.StaticText(ChemCompRestr,-1, |
---|
1620 | 'NB: The chemical restraint sum is over the unit cell contents'),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1621 | mainSizer.Add((5,5),0) |
---|
1622 | |
---|
1623 | chemcompList = chemcompRestData['Sites'] |
---|
1624 | if len(chemcompList): |
---|
1625 | table = [] |
---|
1626 | rowLabels = [] |
---|
1627 | bad = [] |
---|
1628 | chisq = 0. |
---|
1629 | Types = [wg.GRID_VALUE_STRING,]+5*[wg.GRID_VALUE_FLOAT+':10,2',] |
---|
1630 | colLabels = ['Atoms','mul*frac','factor','calc','target','esd'] |
---|
1631 | for i,[indx,factors,obs,esd] in enumerate(chemcompList): |
---|
1632 | try: |
---|
1633 | atoms = G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,ct-1) |
---|
1634 | mul = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cs+1)) |
---|
1635 | frac = np.array(G2mth.GetAtomItemsById(Atoms,AtLookUp,indx,cs-1)) |
---|
1636 | mulfrac = mul*frac |
---|
1637 | calcs = mul*frac*factors |
---|
1638 | chisq += chemcompRestData['wtFactor']*((obs-np.sum(calcs))/esd)**2 |
---|
1639 | for iatm,[atom,mf,fr,clc] in enumerate(zip(atoms,mulfrac,factors,calcs)): |
---|
1640 | table.append([atom,mf,fr,clc,'','']) |
---|
1641 | rowLabels.append('term:'+str(i)+':'+str(iatm)) |
---|
1642 | table.append(['Sum','','',np.sum(calcs),obs,esd]) |
---|
1643 | rowLabels.append('Restr:'+str(i)) |
---|
1644 | except KeyError: |
---|
1645 | print '**** WARNING - missing atom - restraint deleted ****' |
---|
1646 | bad.append(i) |
---|
1647 | if len(bad): |
---|
1648 | bad.reverse() |
---|
1649 | for ibad in bad: |
---|
1650 | del chemcompList[ibad] |
---|
1651 | chemcompTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types) |
---|
1652 | ChemComps = G2G.GSGrid(ChemCompRestr) |
---|
1653 | ChemComps.SetTable(chemcompTable, True) |
---|
1654 | ChemComps.AutoSizeColumns(False) |
---|
1655 | for r in range(chemcompTable.GetNumberRows()): |
---|
1656 | for c in range(2): |
---|
1657 | ChemComps.SetReadOnly(r,c,True) |
---|
1658 | ChemComps.SetCellStyle(r,c,VERY_LIGHT_GREY,True) |
---|
1659 | if 'Restr' in ChemComps.GetRowLabelValue(r): |
---|
1660 | for c in range(4): |
---|
1661 | ChemComps.SetReadOnly(r,c,True) |
---|
1662 | ChemComps.SetCellStyle(r,c,VERY_LIGHT_GREY,True) |
---|
1663 | for c in [1,2]: |
---|
1664 | ChemComps.SetCellTextColour(r,c,VERY_LIGHT_GREY) |
---|
1665 | else: |
---|
1666 | for c in [3,4,5]: |
---|
1667 | ChemComps.SetReadOnly(r,c,True) |
---|
1668 | ChemComps.SetCellStyle(r,c,VERY_LIGHT_GREY,True) |
---|
1669 | for c in [4,5]: |
---|
1670 | ChemComps.SetCellTextColour(r,c,VERY_LIGHT_GREY) |
---|
1671 | |
---|
1672 | ChemComps.Bind(wg.EVT_GRID_LABEL_LEFT_CLICK,OnRowSelect) |
---|
1673 | ChemComps.Bind(wg.EVT_GRID_CELL_CHANGE, OnCellChange) |
---|
1674 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnDeleteRestraint, id=G2gd.wxID_RESTDELETE) |
---|
1675 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnChangeValue, id=G2gd.wxID_RESRCHANGEVAL) |
---|
1676 | mainSizer.Add(wx.StaticText(ChemCompRestr,-1, |
---|
1677 | 'Chemical composition restraints: sum(wt*(delt/sig)^2) = %.2f, mean(wt*(delt/sig)^2) = %.2f' \ |
---|
1678 | %(chisq,chisq/len(chemcompList))),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1679 | mainSizer.Add(ChemComps,0,) |
---|
1680 | else: |
---|
1681 | mainSizer.Add(wx.StaticText(ChemCompRestr,-1,'No chemical composition restraints for this phase'),0,) |
---|
1682 | |
---|
1683 | ChemCompRestr.SetSizer(mainSizer) |
---|
1684 | Size = mainSizer.Fit(G2frame.dataFrame) |
---|
1685 | Size[0] = 600 |
---|
1686 | Size[1] = min(Size[1]+50,500) #make room for tab, but not too big |
---|
1687 | ChemCompRestr.SetSize(Size) |
---|
1688 | ChemCompRestr.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1) |
---|
1689 | G2frame.dataFrame.setSizePosLeft(Size) |
---|
1690 | |
---|
1691 | |
---|
1692 | def UpdateTextureRestr(textureRestData): |
---|
1693 | |
---|
1694 | def OnDeleteRestraint(event): |
---|
1695 | rows = GetSelectedRows(Textures) |
---|
1696 | if not rows: |
---|
1697 | return |
---|
1698 | rows.sort() |
---|
1699 | rows.reverse() |
---|
1700 | for row in rows: |
---|
1701 | textureList.remove(textureList[row]) |
---|
1702 | wx.CallAfter(UpdateTextureRestr,textureRestData) |
---|
1703 | |
---|
1704 | def OnCellChange(event): |
---|
1705 | r,c = event.GetRow(),event.GetCol() |
---|
1706 | try: |
---|
1707 | if c == 1: #grid size |
---|
1708 | new = int(textureTable.GetValue(r,c)) |
---|
1709 | if new < 6 or new > 24: |
---|
1710 | raise ValueError |
---|
1711 | elif c in [2,4]: #esds |
---|
1712 | new = float(textureTable.GetValue(r,c)) |
---|
1713 | if new < -1. or new > 2.: |
---|
1714 | raise ValueError |
---|
1715 | else: |
---|
1716 | new = textureTable.GetValue(r,c) |
---|
1717 | textureRestData['HKLs'][r][c] = new |
---|
1718 | except ValueError: |
---|
1719 | pass |
---|
1720 | wx.CallAfter(UpdateTextureRestr,textureRestData) |
---|
1721 | |
---|
1722 | #TextureRestr.DestroyChildren() |
---|
1723 | if TextureRestr.GetSizer(): |
---|
1724 | TextureRestr.GetSizer().Clear(True) |
---|
1725 | mainSizer = wx.BoxSizer(wx.VERTICAL) |
---|
1726 | mainSizer.Add((5,5),0) |
---|
1727 | mainSizer.Add(WtBox(TextureRestr,textureRestData),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1728 | mainSizer.Add(wx.StaticText(TextureRestr,-1, |
---|
1729 | 'NB: The texture restraints suppress negative pole figure values for the selected HKLs\n' |
---|
1730 | ' "unit esd" gives a bias toward a flatter polefigure'),0,wx.ALIGN_CENTER_VERTICAL) |
---|
1731 | mainSizer.Add((5,5),0) |
---|
1732 | |
---|
1733 | textureList = textureRestData['HKLs'] |
---|
1734 | if len(textureList): |
---|
1735 | table = [] |
---|
1736 | rowLabels = [] |
---|
1737 | Types = [wg.GRID_VALUE_STRING,wg.GRID_VALUE_LONG,wg.GRID_VALUE_FLOAT+':10,2', |
---|
1738 | wg.GRID_VALUE_BOOL,wg.GRID_VALUE_FLOAT+':10,2'] |
---|
1739 | colLabels = ['HKL','grid','neg esd','use unit?','unit esd'] |
---|
1740 | for i,[hkl,grid,esd1,ifesd2,esd2] in enumerate(textureList): |
---|
1741 | table.append(['%d %d %d'%(hkl[0],hkl[1],hkl[2]),grid,esd1,ifesd2,esd2]) |
---|
1742 | rowLabels.append(str(i)) |
---|
1743 | textureTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types) |
---|
1744 | Textures = G2G.GSGrid(TextureRestr) |
---|
1745 | Textures.SetTable(textureTable, True) |
---|
1746 | Textures.AutoSizeColumns(False) |
---|
1747 | for r in range(len(textureList)): |
---|
1748 | Textures.SetReadOnly(r,0,True) |
---|
1749 | Textures.SetCellStyle(r,0,VERY_LIGHT_GREY,True) |
---|
1750 | if not textureTable.GetValue(r,3): |
---|
1751 | Textures.SetReadOnly(r,4,True) |
---|
1752 | Textures.SetCellStyle(r,4,VERY_LIGHT_GREY,True) |
---|
1753 | Textures.SetCellTextColour(r,4,VERY_LIGHT_GREY) |
---|
1754 | Textures.Bind(wg.EVT_GRID_LABEL_LEFT_CLICK,OnRowSelect) |
---|
1755 | Textures.Bind(wg.EVT_GRID_CELL_CHANGE, OnCellChange) |
---|
1756 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnDeleteRestraint, id=G2gd.wxID_RESTDELETE) |
---|
1757 | mainSizer.Add(Textures,0,) |
---|
1758 | else: |
---|
1759 | mainSizer.Add(wx.StaticText(TextureRestr,-1,'No texture restraints for this phase'),0,) |
---|
1760 | TextureRestr.SetSizer(mainSizer) |
---|
1761 | Size = mainSizer.Fit(G2frame.dataFrame) |
---|
1762 | Size[0] = 600 |
---|
1763 | Size[1] = min(Size[1]+50,500) #make room for tab, but not too big |
---|
1764 | TextureRestr.SetSize(Size) |
---|
1765 | TextureRestr.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1) |
---|
1766 | G2frame.dataFrame.setSizePosLeft(Size) |
---|
1767 | |
---|
1768 | def OnPageChanged(event): |
---|
1769 | #print 'OnPageChanged' |
---|
1770 | page = event.GetSelection() |
---|
1771 | text = G2frame.dataDisplay.GetPageText(page) |
---|
1772 | G2frame.dataFrame.RestraintEdit.SetLabel(G2gd.wxID_RESRCHANGEVAL,'Change value') |
---|
1773 | if text == 'Bond': |
---|
1774 | G2gd.SetDataMenuBar(G2frame,G2frame.dataFrame.RestraintMenu) |
---|
1775 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESTRAINTADD,True) |
---|
1776 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESRCHANGEVAL,True) |
---|
1777 | bondRestData = restrData['Bond'] |
---|
1778 | UpdateBondRestr(bondRestData) |
---|
1779 | elif text == 'Angle': |
---|
1780 | G2gd.SetDataMenuBar(G2frame,G2frame.dataFrame.RestraintMenu) |
---|
1781 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESTRAINTADD,True) |
---|
1782 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESRCHANGEVAL,True) |
---|
1783 | angleRestData = restrData['Angle'] |
---|
1784 | UpdateAngleRestr(angleRestData) |
---|
1785 | elif text == 'Plane': |
---|
1786 | G2gd.SetDataMenuBar(G2frame,G2frame.dataFrame.RestraintMenu) |
---|
1787 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESTRAINTADD,True) |
---|
1788 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESRCHANGEVAL,False) |
---|
1789 | planeRestData = restrData['Plane'] |
---|
1790 | UpdatePlaneRestr(planeRestData) |
---|
1791 | elif text == 'Chiral': |
---|
1792 | G2gd.SetDataMenuBar(G2frame,G2frame.dataFrame.RestraintMenu) |
---|
1793 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESTRAINTADD,False) |
---|
1794 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESRCHANGEVAL,True) |
---|
1795 | chiralRestData = restrData['Chiral'] |
---|
1796 | UpdateChiralRestr(chiralRestData) |
---|
1797 | elif text == 'Torsion': |
---|
1798 | G2gd.SetDataMenuBar(G2frame,G2frame.dataFrame.RestraintMenu) |
---|
1799 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESTRAINTADD,False) |
---|
1800 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESRCHANGEVAL,False) |
---|
1801 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_AARESTRAINTPLOT,True) |
---|
1802 | torsionRestData = restrData['Torsion'] |
---|
1803 | UpdateTorsionRestr(torsionRestData) |
---|
1804 | elif text == 'Ramachandran': |
---|
1805 | G2gd.SetDataMenuBar(G2frame,G2frame.dataFrame.RestraintMenu) |
---|
1806 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESTRAINTADD,False) |
---|
1807 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESRCHANGEVAL,False) |
---|
1808 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_AARESTRAINTPLOT,True) |
---|
1809 | ramaRestData = restrData['Rama'] |
---|
1810 | UpdateRamaRestr(ramaRestData) |
---|
1811 | wx.CallAfter(G2plt.PlotRama,G2frame,phaseName,rama,ramaName) |
---|
1812 | elif text == 'Chem. comp.': |
---|
1813 | G2gd.SetDataMenuBar(G2frame,G2frame.dataFrame.RestraintMenu) |
---|
1814 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESTRAINTADD,True) |
---|
1815 | G2frame.dataFrame.RestraintEdit.SetLabel(G2gd.wxID_RESRCHANGEVAL,'Change factor') |
---|
1816 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESRCHANGEVAL,True) |
---|
1817 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESTCHANGEESD,False) |
---|
1818 | chemcompRestData = restrData['ChemComp'] |
---|
1819 | UpdateChemcompRestr(chemcompRestData) |
---|
1820 | elif text == 'Texture': |
---|
1821 | G2gd.SetDataMenuBar(G2frame,G2frame.dataFrame.RestraintMenu) |
---|
1822 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESTRAINTADD,True) |
---|
1823 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESRCHANGEVAL,True) |
---|
1824 | textureRestData = restrData['Texture'] |
---|
1825 | UpdateTextureRestr(textureRestData) |
---|
1826 | |
---|
1827 | event.Skip() |
---|
1828 | |
---|
1829 | def RaisePage(event): |
---|
1830 | 'Respond to a "select tab" menu button' |
---|
1831 | # class PseudoEvent(object): |
---|
1832 | # def __init__(self,page): self.page = page |
---|
1833 | # def Skip(self): pass |
---|
1834 | # def GetSelection(self): return self.page |
---|
1835 | try: |
---|
1836 | i = tabIndex.get(event.GetId()) |
---|
1837 | G2frame.dataDisplay.SetSelection(i) |
---|
1838 | #OnPageChanged(PseudoEvent(i)) |
---|
1839 | except ValueError: |
---|
1840 | print('Unexpected event in RaisePage') |
---|
1841 | |
---|
1842 | if G2frame.dataDisplay: |
---|
1843 | G2frame.dataDisplay.Destroy() |
---|
1844 | |
---|
1845 | G2gd.SetDataMenuBar(G2frame,G2frame.dataFrame.RestraintMenu) |
---|
1846 | G2frame.dataFrame.SetLabel('restraints for '+phaseName) |
---|
1847 | |
---|
1848 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESTSELPHASE,False) |
---|
1849 | if len(Phases) > 1: |
---|
1850 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_RESTSELPHASE,True) |
---|
1851 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnSelectPhase, id=G2gd.wxID_RESTSELPHASE) |
---|
1852 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnAddRestraint, id=G2gd.wxID_RESTRAINTADD) |
---|
1853 | if 'macro' in phasedata['General']['Type']: |
---|
1854 | G2frame.dataFrame.RestraintEdit.Enable(G2gd.wxID_AARESTRAINTADD,True) |
---|
1855 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnAddAARestraint, id=G2gd.wxID_AARESTRAINTADD) |
---|
1856 | G2frame.dataFrame.Bind(wx.EVT_MENU, OnPlotAARestraint, id=G2gd.wxID_AARESTRAINTPLOT) |
---|
1857 | G2frame.dataDisplay = G2G.GSNoteBook(parent=G2frame.dataFrame,size=G2frame.dataFrame.GetClientSize()) |
---|
1858 | |
---|
1859 | # clear menu and menu pointers |
---|
1860 | tabIndex.clear() |
---|
1861 | tabcount = -1 |
---|
1862 | for i in G2frame.dataFrame.RestraintTab.GetMenuItems(): |
---|
1863 | G2frame.dataFrame.RestraintTab.DestroyItem(i) |
---|
1864 | |
---|
1865 | txt = 'Bond' |
---|
1866 | BondRestr = wx.ScrolledWindow(G2frame.dataDisplay) |
---|
1867 | G2frame.dataDisplay.AddPage(BondRestr,txt) |
---|
1868 | item = G2frame.dataFrame.RestraintTab.Append( |
---|
1869 | id=wx.ID_ANY, kind=wx.ITEM_NORMAL,text=txt, |
---|
1870 | help='Select restraint editing tab') |
---|
1871 | G2frame.dataFrame.Bind(wx.EVT_MENU, RaisePage,id=item.GetId()) |
---|
1872 | tabcount += 1 |
---|
1873 | tabIndex[item.GetId()] = tabcount |
---|
1874 | |
---|
1875 | txt = 'Angle' |
---|
1876 | AngleRestr = wx.ScrolledWindow(G2frame.dataDisplay) |
---|
1877 | G2frame.dataDisplay.AddPage(AngleRestr,txt) |
---|
1878 | item = G2frame.dataFrame.RestraintTab.Append( |
---|
1879 | id=wx.ID_ANY, kind=wx.ITEM_NORMAL,text=txt, |
---|
1880 | help='Select restraint editing tab') |
---|
1881 | G2frame.dataFrame.Bind(wx.EVT_MENU, RaisePage,id=item.GetId()) |
---|
1882 | tabcount += 1 |
---|
1883 | tabIndex[item.GetId()] = tabcount |
---|
1884 | |
---|
1885 | txt = 'Plane' |
---|
1886 | PlaneRestr = wx.ScrolledWindow(G2frame.dataDisplay) |
---|
1887 | G2frame.dataDisplay.AddPage(PlaneRestr,txt) |
---|
1888 | item = G2frame.dataFrame.RestraintTab.Append( |
---|
1889 | id=wx.ID_ANY, kind=wx.ITEM_NORMAL,text=txt, |
---|
1890 | help='Select restraint editing tab') |
---|
1891 | G2frame.dataFrame.Bind(wx.EVT_MENU, RaisePage,id=item.GetId()) |
---|
1892 | tabcount += 1 |
---|
1893 | tabIndex[item.GetId()] = tabcount |
---|
1894 | |
---|
1895 | txt = 'Chiral' |
---|
1896 | ChiralRestr = wx.ScrolledWindow(G2frame.dataDisplay) |
---|
1897 | G2frame.dataDisplay.AddPage(ChiralRestr,txt) |
---|
1898 | item = G2frame.dataFrame.RestraintTab.Append( |
---|
1899 | id=wx.ID_ANY, kind=wx.ITEM_NORMAL,text=txt, |
---|
1900 | help='Select restraint editing tab') |
---|
1901 | G2frame.dataFrame.Bind(wx.EVT_MENU, RaisePage,id=item.GetId()) |
---|
1902 | tabcount += 1 |
---|
1903 | tabIndex[item.GetId()] = tabcount |
---|
1904 | |
---|
1905 | if 'macro' in General['Type']: |
---|
1906 | txt = 'Torsion' |
---|
1907 | TorsionRestr = wx.ScrolledWindow(G2frame.dataDisplay) |
---|
1908 | G2frame.dataDisplay.AddPage(TorsionRestr,txt) |
---|
1909 | item = G2frame.dataFrame.RestraintTab.Append( |
---|
1910 | id=wx.ID_ANY, kind=wx.ITEM_NORMAL,text=txt, |
---|
1911 | help='Select restraint editing tab') |
---|
1912 | G2frame.dataFrame.Bind(wx.EVT_MENU, RaisePage,id=item.GetId()) |
---|
1913 | tabcount += 1 |
---|
1914 | tabIndex[item.GetId()] = tabcount |
---|
1915 | |
---|
1916 | txt = 'Ramachandran' |
---|
1917 | RamaRestr = wx.ScrolledWindow(G2frame.dataDisplay) |
---|
1918 | G2frame.dataDisplay.AddPage(RamaRestr,txt) |
---|
1919 | item = G2frame.dataFrame.RestraintTab.Append( |
---|
1920 | id=wx.ID_ANY, kind=wx.ITEM_NORMAL,text=txt, |
---|
1921 | help='Select restraint editing tab') |
---|
1922 | G2frame.dataFrame.Bind(wx.EVT_MENU, RaisePage,id=item.GetId()) |
---|
1923 | tabcount += 1 |
---|
1924 | tabIndex[item.GetId()] = tabcount |
---|
1925 | |
---|
1926 | txt = 'Chem. comp.' |
---|
1927 | ChemCompRestr = wx.ScrolledWindow(G2frame.dataDisplay) |
---|
1928 | G2frame.dataDisplay.AddPage(ChemCompRestr,txt) |
---|
1929 | item = G2frame.dataFrame.RestraintTab.Append( |
---|
1930 | id=wx.ID_ANY, kind=wx.ITEM_NORMAL,text=txt, |
---|
1931 | help='Select restraint editing tab') |
---|
1932 | G2frame.dataFrame.Bind(wx.EVT_MENU, RaisePage,id=item.GetId()) |
---|
1933 | tabcount += 1 |
---|
1934 | tabIndex[item.GetId()] = tabcount |
---|
1935 | |
---|
1936 | if General['SH Texture']['Order']: |
---|
1937 | txt = 'Texture' |
---|
1938 | TextureRestr = wx.ScrolledWindow(G2frame.dataDisplay) |
---|
1939 | G2frame.dataDisplay.AddPage(TextureRestr,txt) |
---|
1940 | item = G2frame.dataFrame.RestraintTab.Append( |
---|
1941 | id=wx.ID_ANY, kind=wx.ITEM_NORMAL,text=txt, |
---|
1942 | help='Select restraint editing tab') |
---|
1943 | G2frame.dataFrame.Bind(wx.EVT_MENU, RaisePage,id=item.GetId()) |
---|
1944 | tabcount += 1 |
---|
1945 | tabIndex[item.GetId()] = tabcount |
---|
1946 | |
---|
1947 | UpdateBondRestr(restrData['Bond']) |
---|
1948 | |
---|
1949 | G2frame.dataDisplay.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, OnPageChanged) |
---|