1 | import math |
---|
2 | import time |
---|
3 | import copy |
---|
4 | import numpy as np |
---|
5 | import wx |
---|
6 | import wx.aui |
---|
7 | import matplotlib as mpl |
---|
8 | import GSASIIpath |
---|
9 | import GSASIIgrid as G2gd |
---|
10 | import GSASIIimage as G2img |
---|
11 | import GSASIIIO as G2IO |
---|
12 | import GSASIIpwdGUI as G2pdG |
---|
13 | import GSASIIimgGUI as G2imG |
---|
14 | from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas |
---|
15 | from matplotlib.backends.backend_wxagg import NavigationToolbar2Wx as Toolbar |
---|
16 | |
---|
17 | # useful degree trig functions |
---|
18 | sind = lambda x: math.sin(x*math.pi/180.) |
---|
19 | cosd = lambda x: math.cos(x*math.pi/180.) |
---|
20 | tand = lambda x: math.tan(x*math.pi/180.) |
---|
21 | asind = lambda x: 180.*math.asin(x)/math.pi |
---|
22 | acosd = lambda x: 180.*math.acos(x)/math.pi |
---|
23 | atan2d = lambda x,y: 180.*math.atan2(y,x)/math.pi |
---|
24 | atand = lambda x: 180.*math.atan(x)/math.pi |
---|
25 | |
---|
26 | class G2Plot(wx.Panel): |
---|
27 | |
---|
28 | def __init__(self,parent,id=-1,dpi=None,**kwargs): |
---|
29 | wx.Panel.__init__(self,parent,id=id,**kwargs) |
---|
30 | self.figure = mpl.figure.Figure(dpi=dpi,figsize=(5,7)) |
---|
31 | self.canvas = Canvas(self,-1,self.figure) |
---|
32 | self.toolbar = Toolbar(self.canvas) |
---|
33 | |
---|
34 | self.toolbar.Realize() |
---|
35 | |
---|
36 | sizer=wx.BoxSizer(wx.VERTICAL) |
---|
37 | sizer.Add(self.canvas,1,wx.EXPAND) |
---|
38 | sizer.Add(self.toolbar,0,wx.LEFT|wx.EXPAND) |
---|
39 | self.SetSizer(sizer) |
---|
40 | |
---|
41 | class G2PlotNoteBook(wx.Panel): |
---|
42 | def __init__(self,parent,id=-1): |
---|
43 | wx.Panel.__init__(self,parent,id=id) |
---|
44 | #so one can't delete a plot page!! |
---|
45 | self.nb = wx.aui.AuiNotebook(self, \ |
---|
46 | style=wx.aui.AUI_NB_DEFAULT_STYLE ^ wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB) |
---|
47 | sizer = wx.BoxSizer() |
---|
48 | sizer.Add(self.nb,1,wx.EXPAND) |
---|
49 | self.SetSizer(sizer) |
---|
50 | self.status = parent.CreateStatusBar() |
---|
51 | self.status.SetFieldsCount(2) |
---|
52 | self.status.SetStatusWidths([125,-1]) |
---|
53 | self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.OnPageChanged) |
---|
54 | |
---|
55 | self.plotList = [] |
---|
56 | |
---|
57 | def add(self,name=""): |
---|
58 | page = G2Plot(self.nb) |
---|
59 | self.nb.AddPage(page,name) |
---|
60 | |
---|
61 | self.plotList.append(name) |
---|
62 | |
---|
63 | return page.figure |
---|
64 | |
---|
65 | def clear(self): |
---|
66 | while self.nb.GetPageCount(): |
---|
67 | self.nb.DeletePage(0) |
---|
68 | self.plotList = [] |
---|
69 | self.status.DestroyChildren() |
---|
70 | |
---|
71 | def OnPageChanged(self,event): |
---|
72 | self.status.DestroyChildren() #get rid of special stuff on status bar |
---|
73 | |
---|
74 | def PlotSngl(self,newPlot=False): |
---|
75 | from matplotlib.patches import Circle |
---|
76 | global HKL,HKLF |
---|
77 | |
---|
78 | def OnSCMotion(event): |
---|
79 | xpos = event.xdata |
---|
80 | if xpos: |
---|
81 | xpos = round(xpos) #avoid out of frame mouse position |
---|
82 | ypos = round(event.ydata) |
---|
83 | zpos = Data['Layer'] |
---|
84 | if '100' in Data['Zone']: |
---|
85 | HKLtxt = '(%3d,%3d,%3d)'%(zpos,xpos,ypos) |
---|
86 | elif '010' in Data['Zone']: |
---|
87 | HKLtxt = '(%3d,%3d,%3d)'%(xpos,zpos,ypos) |
---|
88 | elif '001' in Data['Zone']: |
---|
89 | HKLtxt = '(%3d,%3d,%3d)'%(xpos,ypos,zpos) |
---|
90 | Page.canvas.SetToolTipString(HKLtxt) |
---|
91 | self.G2plotNB.status.SetFields(['HKL = '+HKLtxt,'']) |
---|
92 | |
---|
93 | def OnSCPick(event): |
---|
94 | zpos = Data['Layer'] |
---|
95 | pos = event.artist.center |
---|
96 | if '100' in Data['Zone']: |
---|
97 | Page.canvas.SetToolTipString('(picked:(%3d,%3d,%3d))'%(zpos,pos[0],pos[1])) |
---|
98 | hkl = np.array([zpos,pos[0],pos[1]]) |
---|
99 | elif '010' in Data['Zone']: |
---|
100 | Page.canvas.SetToolTipString('(picked:(%3d,%3d,%3d))'%(pos[0],zpos,pos[1])) |
---|
101 | hkl = np.array([pos[0],zpos,pos[1]]) |
---|
102 | elif '001' in Data['Zone']: |
---|
103 | Page.canvas.SetToolTipString('(picked:(%3d,%3d,%3d))'%(pos[0],pos[1],zpos)) |
---|
104 | hkl = np.array([pos[0],pos[1],zpos]) |
---|
105 | h,k,l = hkl |
---|
106 | hklf = HKLF[np.where(np.all(HKL-hkl == [0,0,0],axis=1))] |
---|
107 | if len(hklf): |
---|
108 | Fosq,sig,Fcsq = hklf[0] |
---|
109 | HKLtxt = '(%3d,%3d,%3d %.2f %.3f %.2f %.2f)'%(h,k,l,Fosq,sig,Fcsq,(Fosq-Fcsq)/(scale*sig)) |
---|
110 | self.G2plotNB.status.SetFields(['','HKL, Fosq, sig, Fcsq, delFsq/sig = '+HKLtxt]) |
---|
111 | |
---|
112 | def OnSCKeyPress(event): |
---|
113 | print event.key |
---|
114 | |
---|
115 | try: |
---|
116 | plotNum = self.G2plotNB.plotList.index('Structure Factors') |
---|
117 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
118 | if not newPlot: |
---|
119 | Plot = Page.figure.gca() #get previous powder plot & get limits |
---|
120 | xylim = Plot.get_xlim(),Plot.get_ylim() |
---|
121 | Page.figure.clf() |
---|
122 | Plot = Page.figure.gca() #get a fresh plot after clf() |
---|
123 | except ValueError,error: |
---|
124 | Plot = self.G2plotNB.add('Structure Factors').gca() |
---|
125 | plotNum = self.G2plotNB.plotList.index('Structure Factors') |
---|
126 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
127 | # Page.canvas.mpl_connect('key_press_event', OnSCKeyPress) |
---|
128 | Page.canvas.mpl_connect('pick_event', OnSCPick) |
---|
129 | Page.canvas.mpl_connect('motion_notify_event', OnSCMotion) |
---|
130 | Page.SetFocus() |
---|
131 | |
---|
132 | Plot.set_aspect(aspect='equal') |
---|
133 | HKLref = self.PatternTree.GetItemPyData(self.Sngl) |
---|
134 | Data = self.PatternTree.GetItemPyData( \ |
---|
135 | G2gd.GetPatternTreeItemId(self,self.Sngl, 'HKL Plot Controls')) |
---|
136 | Type = Data['Type'] |
---|
137 | scale = Data['Scale'] |
---|
138 | HKLmax = Data['HKLmax'] |
---|
139 | HKLmin = Data['HKLmin'] |
---|
140 | FosqMax = Data['FoMax'] |
---|
141 | FoMax = math.sqrt(FosqMax) |
---|
142 | ifFc = Data['ifFc'] |
---|
143 | xlabel = ['k, h=','h, k=','h, l='] |
---|
144 | ylabel = ['l','l','k'] |
---|
145 | zones = ['100','010','001'] |
---|
146 | pzone = [[1,2],[0,2],[0,1]] |
---|
147 | izone = zones.index(Data['Zone']) |
---|
148 | Plot.set_title(self.PatternTree.GetItemText(self.Sngl)[5:]) |
---|
149 | HKL = [] |
---|
150 | HKLF = [] |
---|
151 | for H,Fosq,sig,Fcsq,x,x,x in HKLref: |
---|
152 | HKL.append(H) |
---|
153 | HKLF.append([Fosq,sig,Fcsq]) |
---|
154 | if H[izone] == Data['Layer']: |
---|
155 | B = 0 |
---|
156 | if Type == 'Fosq': |
---|
157 | A = scale*Fosq/FosqMax |
---|
158 | B = scale*Fcsq/FosqMax |
---|
159 | C = abs(A-B) |
---|
160 | elif Type == 'Fo': |
---|
161 | A = scale*math.sqrt(max(0,Fosq))/FoMax |
---|
162 | B = scale*math.sqrt(max(0,Fcsq))/FoMax |
---|
163 | C = abs(A-B) |
---|
164 | elif Type == '|DFsq|/sig': |
---|
165 | A = abs(Fosq-Fcsq)/(scale*sig) |
---|
166 | elif Type == '|DFsq|>sig': |
---|
167 | A = abs(Fosq-Fcsq)/(scale*sig) |
---|
168 | if A < 1.0: A = 0 |
---|
169 | elif Type == '|DFsq|>3sig': |
---|
170 | A = abs(Fosq-Fcsq)/(scale*sig) |
---|
171 | if A < 3.0: A = 0 |
---|
172 | xy = (H[pzone[izone][0]],H[pzone[izone][1]]) |
---|
173 | if A > 0.0: |
---|
174 | Plot.add_artist(Circle(xy,radius=A,ec='g',fc='w',picker=3)) |
---|
175 | if B: |
---|
176 | Plot.add_artist(Circle(xy,radius=B,ec='b',fc='w')) |
---|
177 | radius = C |
---|
178 | if radius > 0: |
---|
179 | if A > B: |
---|
180 | Plot.add_artist(Circle(xy,radius=radius,ec='g',fc='g')) |
---|
181 | else: |
---|
182 | Plot.add_artist(Circle(xy,radius=radius,ec='r',fc='r')) |
---|
183 | HKL = np.array(HKL,dtype=np.int) |
---|
184 | HKLF = np.array(HKLF) |
---|
185 | Plot.set_xlabel(xlabel[izone]+str(Data['Layer']),fontsize=12) |
---|
186 | Plot.set_ylabel(ylabel[izone],fontsize=12) |
---|
187 | Plot.set_xlim((HKLmin[pzone[izone][0]],HKLmax[pzone[izone][0]])) |
---|
188 | Plot.set_ylim((HKLmin[pzone[izone][1]],HKLmax[pzone[izone][1]])) |
---|
189 | if not newPlot: |
---|
190 | Page.toolbar.push_current() |
---|
191 | Plot.set_xlim(xylim[0]) |
---|
192 | Plot.set_ylim(xylim[1]) |
---|
193 | xylim = [] |
---|
194 | Page.toolbar.push_current() |
---|
195 | Page.toolbar.draw() |
---|
196 | else: |
---|
197 | Page.canvas.draw() |
---|
198 | |
---|
199 | def PlotPatterns(self,newPlot=False): |
---|
200 | global HKL |
---|
201 | |
---|
202 | def OnPick(event): |
---|
203 | if self.itemPicked is not None: return |
---|
204 | PatternId = self.PatternId |
---|
205 | PickId = self.PickId |
---|
206 | pick = event.artist |
---|
207 | mouse = event.mouseevent |
---|
208 | xpos = pick.get_xdata() |
---|
209 | ypos = pick.get_ydata() |
---|
210 | ind = event.ind |
---|
211 | view = Page.toolbar._views.forward() |
---|
212 | if view and 'line2' in str(pick): #apply offset only for picked powder pattern points |
---|
213 | ind += np.searchsorted(xye[0],view[0][0]) |
---|
214 | xy = zip(xpos[ind],ypos[ind])[0] |
---|
215 | if self.PatternTree.GetItemText(PickId) == 'Peak List': |
---|
216 | if ind.all() != [0]: #picked a data point |
---|
217 | inst = self.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(self,PatternId, 'Instrument Parameters')) |
---|
218 | if len(inst[1]) == 11: |
---|
219 | ins = inst[1][4:10] |
---|
220 | else: |
---|
221 | ins = inst[1][6:12] |
---|
222 | sig = ins[0]*tand(xy[0]/2.0)**2+ins[1]*tand(xy[0]/2.0)+ins[2] |
---|
223 | gam = ins[3]/cosd(xy[0]/2.0)+ins[4]*tand(xy[0]/2.0) |
---|
224 | data = self.PatternTree.GetItemPyData(self.PickId) |
---|
225 | XY = [xy[0],0, xy[1],1, sig,0, gam,0] #default refine intensity 1st |
---|
226 | data.append(XY) |
---|
227 | G2pdG.UpdatePeakGrid(self,data) |
---|
228 | PlotPatterns(self) |
---|
229 | else: #picked a peak list line |
---|
230 | self.itemPicked = pick |
---|
231 | elif self.PatternTree.GetItemText(PickId) == 'Limits': |
---|
232 | if ind.all() != [0]: #picked a data point |
---|
233 | LimitId = G2gd.GetPatternTreeItemId(self,PatternId, 'Limits') |
---|
234 | data = self.PatternTree.GetItemPyData(LimitId) |
---|
235 | if mouse.button==1: |
---|
236 | data[1][0] = min(xy[0],data[1][1]) |
---|
237 | if mouse.button==3: |
---|
238 | data[1][1] = max(xy[0],data[1][0]) |
---|
239 | self.PatternTree.SetItemPyData(LimitId,data) |
---|
240 | G2pdG.UpdateLimitsGrid(self,data) |
---|
241 | PlotPatterns(self) |
---|
242 | else: #picked a limit line |
---|
243 | self.itemPicked = pick |
---|
244 | |
---|
245 | def OnPlotKeyPress(event): |
---|
246 | if event.key == 'w': |
---|
247 | if self.Weight: |
---|
248 | self.Weight = False |
---|
249 | else: |
---|
250 | self.Weight = True |
---|
251 | print 'plot weighting:',self.Weight |
---|
252 | elif event.key == 'u' and self.Offset < 100.: |
---|
253 | self.Offset += 1. |
---|
254 | elif event.key == 'd' and self.Offset > 0.: |
---|
255 | self.Offset -= 1. |
---|
256 | elif event.key == 'c': |
---|
257 | if self.Contour: |
---|
258 | self.Contour = False |
---|
259 | else: |
---|
260 | self.Contour = True |
---|
261 | elif event.key == 's': |
---|
262 | if self.SinglePlot: |
---|
263 | self.SinglePlot = False |
---|
264 | else: |
---|
265 | self.SinglePlot = True |
---|
266 | |
---|
267 | PlotPatterns(self) |
---|
268 | |
---|
269 | def OnKeyBox(event): |
---|
270 | if self.G2plotNB.nb.GetSelection() == self.G2plotNB.plotList.index('Powder Patterns'): |
---|
271 | event.key = cb.GetValue()[0] |
---|
272 | cb.SetValue(' key press') |
---|
273 | OnPlotKeyPress(event) |
---|
274 | |
---|
275 | def OnMotion(event): |
---|
276 | xpos = event.xdata |
---|
277 | if xpos: #avoid out of frame mouse position |
---|
278 | ypos = event.ydata |
---|
279 | Page.canvas.SetCursor(wx.CROSS_CURSOR) |
---|
280 | try: |
---|
281 | wave = self.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(self, \ |
---|
282 | self.PatternId, 'Instrument Parameters'))[0][1] |
---|
283 | dsp = 0.0 |
---|
284 | if abs(xpos) > 0.: #avoid possible singularity at beam center |
---|
285 | dsp = wave/(2.*sind(abs(xpos)/2.0)) |
---|
286 | self.G2plotNB.status.SetStatusText('2-theta =%9.3f d =%9.5f Intensity =%9.1f'%(xpos,dsp,ypos),1) |
---|
287 | if self.itemPicked: |
---|
288 | Page.canvas.SetToolTipString('%9.3f'%(xpos)) |
---|
289 | if self.PickId and self.PatternTree.GetItemText(self.PickId) in ['Index Peak List','Unit Cells List']: |
---|
290 | found = [] |
---|
291 | if len(HKL): |
---|
292 | found = HKL[np.where(np.fabs(HKL.T[5]-xpos) < 0.05)] |
---|
293 | if len(found): |
---|
294 | h,k,l = found[0][:3] |
---|
295 | Page.canvas.SetToolTipString('%d,%d,%d'%(int(h),int(k),int(l))) |
---|
296 | else: |
---|
297 | Page.canvas.SetToolTipString('') |
---|
298 | |
---|
299 | except TypeError: |
---|
300 | self.G2plotNB.status.SetStatusText('Select PWDR powder pattern first',1) |
---|
301 | |
---|
302 | def OnRelease(event): |
---|
303 | if self.itemPicked is None: return |
---|
304 | xpos = event.xdata |
---|
305 | if xpos: #avoid out of frame mouse position |
---|
306 | lines = [] |
---|
307 | for line in self.Lines: lines.append(line.get_xdata()[0]) |
---|
308 | lineNo = lines.index(self.itemPicked.get_xdata()[0]) |
---|
309 | if lineNo in [0,1]: |
---|
310 | LimitId = G2gd.GetPatternTreeItemId(self,self.PatternId, 'Limits') |
---|
311 | data = self.PatternTree.GetItemPyData(LimitId) |
---|
312 | # print 'limits',xpos |
---|
313 | data[1][lineNo] = xpos |
---|
314 | self.PatternTree.SetItemPyData(LimitId,data) |
---|
315 | if self.PatternTree.GetItemText(self.PickId) == 'Limits': |
---|
316 | G2pdG.UpdateLimitsGrid(self,data) |
---|
317 | else: |
---|
318 | PeakId = G2gd.GetPatternTreeItemId(self,self.PatternId, 'Peak List') |
---|
319 | data = self.PatternTree.GetItemPyData(PeakId) |
---|
320 | # print 'peaks',xpos |
---|
321 | data[lineNo-2][0] = xpos |
---|
322 | self.PatternTree.SetItemPyData(PeakId,data) |
---|
323 | G2pdG.UpdatePeakGrid(self,data) |
---|
324 | PlotPatterns(self) |
---|
325 | self.itemPicked = None |
---|
326 | |
---|
327 | xylim = [] |
---|
328 | try: |
---|
329 | plotNum = self.G2plotNB.plotList.index('Powder Patterns') |
---|
330 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
331 | if not newPlot: |
---|
332 | Plot = Page.figure.gca() #get previous powder plot & get limits |
---|
333 | xylim = Plot.get_xlim(),Plot.get_ylim() |
---|
334 | Page.figure.clf() |
---|
335 | Plot = Page.figure.gca() #get a fresh plot after clf() |
---|
336 | except ValueError,error: |
---|
337 | newPlot = True |
---|
338 | Plot = self.G2plotNB.add('Powder Patterns').gca() |
---|
339 | plotNum = self.G2plotNB.plotList.index('Powder Patterns') |
---|
340 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
341 | Page.canvas.mpl_connect('key_press_event', OnPlotKeyPress) |
---|
342 | Page.canvas.mpl_connect('motion_notify_event', OnMotion) |
---|
343 | Page.canvas.mpl_connect('pick_event', OnPick) |
---|
344 | Page.canvas.mpl_connect('button_release_event', OnRelease) |
---|
345 | Page.SetFocus() |
---|
346 | cb = wx.ComboBox(self.G2plotNB.status,style=wx.CB_DROPDOWN|wx.CB_READONLY, |
---|
347 | choices=(' key press','d: offset down','u: offset up','c: toggle contour','s: toggle single plot')) |
---|
348 | cb.Bind(wx.EVT_COMBOBOX, OnKeyBox) |
---|
349 | cb.SetValue(' key press') |
---|
350 | |
---|
351 | PickId = self.PickId |
---|
352 | PatternId = self.PatternId |
---|
353 | colors=['b','g','r','c','m','k'] |
---|
354 | Lines = [] |
---|
355 | if self.SinglePlot: |
---|
356 | Pattern = self.PatternTree.GetItemPyData(self.PatternId) |
---|
357 | Pattern.append(self.PatternTree.GetItemText(self.PatternId)) |
---|
358 | PlotList = [Pattern,] |
---|
359 | else: |
---|
360 | PlotList = [] |
---|
361 | item, cookie = self.PatternTree.GetFirstChild(self.root) |
---|
362 | while item: |
---|
363 | if 'PWDR' in self.PatternTree.GetItemText(item): |
---|
364 | Pattern = self.PatternTree.GetItemPyData(item) |
---|
365 | Pattern.append(self.PatternTree.GetItemText(item)) |
---|
366 | PlotList.append(Pattern) |
---|
367 | item, cookie = self.PatternTree.GetNextChild(self.root, cookie) |
---|
368 | Ymax = 1.0 |
---|
369 | HKL = np.array(self.HKL) |
---|
370 | for Pattern in PlotList: |
---|
371 | xye = Pattern[1] |
---|
372 | Ymax = max(Ymax,max(xye[1])) |
---|
373 | offset = self.Offset*Ymax/100.0 |
---|
374 | Plot.set_title('Powder Patterns') |
---|
375 | Plot.set_xlabel(r'$\mathsf{2\theta}$',fontsize=14) |
---|
376 | Plot.set_ylabel('Intensity',fontsize=12) |
---|
377 | if self.Contour: |
---|
378 | ContourZ = [] |
---|
379 | ContourY = [] |
---|
380 | Nseq = 0 |
---|
381 | for N,Pattern in enumerate(PlotList): |
---|
382 | ifpicked = False |
---|
383 | LimitId = 0 |
---|
384 | xye = Pattern[1] |
---|
385 | if PickId: |
---|
386 | ifpicked = Pattern[2] == self.PatternTree.GetItemText(PatternId) |
---|
387 | LimitId = G2gd.GetPatternTreeItemId(self,PatternId, 'Limits') |
---|
388 | X = xye[0] |
---|
389 | Y = xye[1]+offset*N |
---|
390 | if LimitId: |
---|
391 | limits = self.PatternTree.GetItemPyData(LimitId) |
---|
392 | Lines.append(Plot.axvline(limits[1][0],color='g',dashes=(5,5),picker=3.)) |
---|
393 | Lines.append(Plot.axvline(limits[1][1],color='r',dashes=(5,5),picker=3.)) |
---|
394 | if self.Contour: |
---|
395 | ContourY.append(N) |
---|
396 | ContourZ.append(Y) |
---|
397 | ContourX = X |
---|
398 | Nseq += 1 |
---|
399 | Plot.set_ylabel('Data sequence',fontsize=12) |
---|
400 | else: |
---|
401 | if ifpicked: |
---|
402 | Z = xye[3]+offset*N |
---|
403 | W = xye[4]+offset*N |
---|
404 | D = xye[5]+offset*N-Ymax*.02 |
---|
405 | if self.Weight: |
---|
406 | W2 = np.sqrt(xye[2]) |
---|
407 | D *= W2-Ymax*.02 |
---|
408 | Plot.plot(X,Y,colors[N%6]+'+',picker=3.,clip_on=False) |
---|
409 | Plot.plot(X,Z,colors[(N+1)%6],picker=False) |
---|
410 | Plot.plot(X,W,colors[(N+2)%6],picker=False) |
---|
411 | Plot.plot(X,D,colors[(N+3)%6],picker=False) |
---|
412 | Plot.axhline(0.,color=wx.BLACK) |
---|
413 | Page.canvas.SetToolTipString('') |
---|
414 | if self.PatternTree.GetItemText(PickId) == 'Peak List': |
---|
415 | tip = 'On data point: Pick peak - L or R MB.On line: MB down to move' |
---|
416 | Page.canvas.SetToolTipString(tip) |
---|
417 | data = self.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(self,PatternId, 'Peak List')) |
---|
418 | for item in data: |
---|
419 | Lines.append(Plot.axvline(item[0],color=colors[N%6],picker=2.)) |
---|
420 | if self.PatternTree.GetItemText(PickId) == 'Limits': |
---|
421 | tip = 'On data point: Lower limit - L MB; Upper limit - R MB. On limit: MB down to move' |
---|
422 | Page.canvas.SetToolTipString(tip) |
---|
423 | data = self.LimitsTable.GetData() |
---|
424 | else: |
---|
425 | Plot.plot(X,Y,colors[N%6],picker=False) |
---|
426 | if PickId and self.PatternTree.GetItemText(PickId) in ['Index Peak List','Unit Cells List']: |
---|
427 | peaks = np.array((self.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(self,PatternId, 'Index Peak List')))) |
---|
428 | for peak in peaks: |
---|
429 | Plot.axvline(peak[0],color='b') |
---|
430 | for hkl in self.HKL: |
---|
431 | Plot.axvline(hkl[5],color='r',dashes=(5,5)) |
---|
432 | if self.Contour: |
---|
433 | acolor = mpl.cm.get_cmap('Paired') |
---|
434 | Plot.imshow(ContourZ,cmap=acolor,vmin=0,vmax=Ymax,interpolation='nearest', |
---|
435 | extent=[ContourX[0],ContourX[-1],ContourY[0],ContourY[-1]],aspect='auto') |
---|
436 | newPlot = True |
---|
437 | else: |
---|
438 | self.Lines = Lines |
---|
439 | if not newPlot: |
---|
440 | Page.toolbar.push_current() |
---|
441 | Plot.set_xlim(xylim[0]) |
---|
442 | Plot.set_ylim(xylim[1]) |
---|
443 | xylim = [] |
---|
444 | Page.toolbar.push_current() |
---|
445 | Page.toolbar.draw() |
---|
446 | else: |
---|
447 | Page.canvas.draw() |
---|
448 | self.Pwdr = True |
---|
449 | |
---|
450 | def PlotPowderLines(self): |
---|
451 | global HKL |
---|
452 | |
---|
453 | def OnMotion(event): |
---|
454 | xpos = event.xdata |
---|
455 | if xpos: #avoid out of frame mouse position |
---|
456 | Page.canvas.SetCursor(wx.CROSS_CURSOR) |
---|
457 | self.G2plotNB.status.SetFields(['','2-theta =%9.3f '%(xpos,)]) |
---|
458 | if self.PickId and self.PatternTree.GetItemText(self.PickId) in ['Index Peak List','Unit Cells List']: |
---|
459 | found = [] |
---|
460 | if len(HKL): |
---|
461 | found = HKL[np.where(np.fabs(HKL.T[5]-xpos) < 0.05)] |
---|
462 | if len(found): |
---|
463 | h,k,l = found[0][:3] |
---|
464 | Page.canvas.SetToolTipString('%d,%d,%d'%(int(h),int(k),int(l))) |
---|
465 | else: |
---|
466 | Page.canvas.SetToolTipString('') |
---|
467 | |
---|
468 | try: |
---|
469 | plotNum = self.G2plotNB.plotList.index('Powder Lines') |
---|
470 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
471 | Page.figure.clf() |
---|
472 | Plot = Page.figure.gca() |
---|
473 | except ValueError,error: |
---|
474 | Plot = self.G2plotNB.add('Powder Lines').gca() |
---|
475 | plotNum = self.G2plotNB.plotList.index('Powder Lines') |
---|
476 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
477 | Page.canvas.mpl_connect('motion_notify_event', OnMotion) |
---|
478 | |
---|
479 | Page.SetFocus() |
---|
480 | Plot.set_title('Powder Pattern Lines') |
---|
481 | Plot.set_xlabel(r'$\mathsf{2\theta}$',fontsize=14) |
---|
482 | PickId = self.PickId |
---|
483 | PatternId = self.PatternId |
---|
484 | peaks = self.PatternTree.GetItemPyData(G2gd.GetPatternTreeItemId(self,PatternId, 'Index Peak List')) |
---|
485 | for peak in peaks: |
---|
486 | Plot.axvline(peak[0],color='b') |
---|
487 | HKL = np.array(self.HKL) |
---|
488 | for hkl in self.HKL: |
---|
489 | Plot.axvline(hkl[5],color='r',dashes=(5,5)) |
---|
490 | xmin = peaks[0][0] |
---|
491 | xmax = peaks[-1][0] |
---|
492 | delt = xmax-xmin |
---|
493 | xlim = [max(0,xmin-delt/20.),min(180.,xmax+delt/20.)] |
---|
494 | Plot.set_xlim(xlim) |
---|
495 | Page.canvas.draw() |
---|
496 | |
---|
497 | def PlotPeakWidths(self): |
---|
498 | PatternId = self.PatternId |
---|
499 | limitID = G2gd.GetPatternTreeItemId(self,PatternId, 'Limits') |
---|
500 | if limitID: |
---|
501 | limits = self.PatternTree.GetItemPyData(limitID) |
---|
502 | else: |
---|
503 | return |
---|
504 | instParms = self.PatternTree.GetItemPyData( \ |
---|
505 | G2gd.GetPatternTreeItemId(self,PatternId, 'Instrument Parameters')) |
---|
506 | if instParms[0][0] == 'PXC': |
---|
507 | lam = instParms[1][1] |
---|
508 | if len(instParms[1]) == 13: |
---|
509 | GU,GV,GW,LX,LY = instParms[0][6:11] |
---|
510 | else: |
---|
511 | GU,GV,GW,LX,LY = instParms[0][4:9] |
---|
512 | peakID = G2gd.GetPatternTreeItemId(self,PatternId, 'Peak List') |
---|
513 | if peakID: |
---|
514 | peaks = self.PatternTree.GetItemPyData(peakID) |
---|
515 | else: |
---|
516 | peaks = [] |
---|
517 | |
---|
518 | try: |
---|
519 | plotNum = self.G2plotNB.plotList.index('Peak Widths') |
---|
520 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
521 | Page.figure.clf() |
---|
522 | Plot = Page.figure.gca() |
---|
523 | except ValueError,error: |
---|
524 | Plot = self.G2plotNB.add('Peak Widths').gca() |
---|
525 | plotNum = self.G2plotNB.plotList.index('Peak Widths') |
---|
526 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
527 | Page.SetFocus() |
---|
528 | |
---|
529 | Page.canvas.SetToolTipString('') |
---|
530 | colors=['b','g','r','c','m','k'] |
---|
531 | Xmin,Xmax = limits[1] |
---|
532 | Xmin = min(0.5,max(Xmin,1)) |
---|
533 | Xmin /= 2 |
---|
534 | Xmax /= 2 |
---|
535 | nPts = 100 |
---|
536 | delt = (Xmax-Xmin)/nPts |
---|
537 | thetas = [] |
---|
538 | for i in range(nPts): |
---|
539 | thetas.append(Xmin+i*delt) |
---|
540 | X = [] |
---|
541 | Y = [] |
---|
542 | Z = [] |
---|
543 | W = [] |
---|
544 | V = [] |
---|
545 | sig = lambda Th,U,V,W: 1.17741*math.sqrt(U*tand(Th)**2+V*tand(Th)+W)*math.pi/18000. |
---|
546 | gam = lambda Th,X,Y: (X/cosd(Th)+Y*tand(Th))*math.pi/18000. |
---|
547 | gamFW = lambda s,g: math.exp(math.log(s**5+2.69269*s**4*g+2.42843*s**3*g**2+4.47163*s**2*g**3+0.07842*s*g**4+g**5)/5.) |
---|
548 | gamFW2 = lambda s,g: math.sqrt(s**2+(0.4654996*g)**2)+.5345004*g #Ubaldo Bafile - private communication |
---|
549 | for theta in thetas: |
---|
550 | X.append(4.0*math.pi*sind(theta)/lam) #q |
---|
551 | s = sig(theta,GU,GV,GW) |
---|
552 | g = gam(theta,LX,LY) |
---|
553 | G = gamFW(g,s) |
---|
554 | H = gamFW2(g,s) |
---|
555 | Y.append(s/tand(theta)) |
---|
556 | Z.append(g/tand(theta)) |
---|
557 | W.append(G/tand(theta)) |
---|
558 | V.append(H/tand(theta)) |
---|
559 | Plot.set_title('Instrument and sample peak widths') |
---|
560 | Plot.set_ylabel(r'$\Delta q/q, \Delta d/d$',fontsize=14) |
---|
561 | Plot.set_xlabel(r'$q, \AA^{-1}$',fontsize=14) |
---|
562 | Plot.plot(X,Y,color='r',label='Gaussian') |
---|
563 | Plot.plot(X,Z,color='g',label='Lorentzian') |
---|
564 | Plot.plot(X,W,color='b',label='G+L') |
---|
565 | Plot.plot(X,V,color='k',label='G+L2') |
---|
566 | X = [] |
---|
567 | Y = [] |
---|
568 | Z = [] |
---|
569 | W = [] |
---|
570 | V = [] |
---|
571 | for peak in peaks: |
---|
572 | X.append(4.0*math.pi*sind(peak[0]/2.0)/lam) |
---|
573 | s = 1.17741*math.sqrt(peak[4])*math.pi/18000. |
---|
574 | g = peak[6]*math.pi/18000. |
---|
575 | G = gamFW(g,s) |
---|
576 | H = gamFW2(g,s) |
---|
577 | Y.append(s/tand(peak[0]/2.)) |
---|
578 | Z.append(g/tand(peak[0]/2.)) |
---|
579 | W.append(G/tand(peak[0]/2.)) |
---|
580 | V.append(H/tand(peak[0]/2.)) |
---|
581 | Plot.plot(X,Y,'+',color='r',label='G peak') |
---|
582 | Plot.plot(X,Z,'+',color='g',label='L peak') |
---|
583 | Plot.plot(X,W,'+',color='b',label='G+L peak') |
---|
584 | Plot.plot(X,V,'+',color='k',label='G+L2 peak') |
---|
585 | Plot.legend(loc='best') |
---|
586 | Page.canvas.draw() |
---|
587 | |
---|
588 | def PlotExposedImage(self,newPlot=False,event=None): |
---|
589 | plotNo = self.G2plotNB.nb.GetSelection() |
---|
590 | if self.G2plotNB.nb.GetPageText(plotNo) == '2D Powder Image': |
---|
591 | PlotImage(self,newPlot,event) |
---|
592 | elif self.G2plotNB.nb.GetPageText(plotNo) == '2D Integration': |
---|
593 | PlotIntegration(self,newPlot,event) |
---|
594 | |
---|
595 | def PlotImage(self,newPlot=False,event=None): |
---|
596 | from matplotlib.patches import Ellipse,Arc,Circle,Polygon |
---|
597 | import numpy.ma as ma |
---|
598 | Dsp = lambda tth,wave: wave/(2.*sind(tth/2.)) |
---|
599 | |
---|
600 | def OnImMotion(event): |
---|
601 | Data = self.PatternTree.GetItemPyData( |
---|
602 | G2gd.GetPatternTreeItemId(self,self.Image, 'Image Controls')) |
---|
603 | Page.canvas.SetToolTipString('') |
---|
604 | size = len(self.ImageZ) |
---|
605 | if event.xdata and event.ydata: #avoid out of frame errors |
---|
606 | Page.canvas.SetCursor(wx.CROSS_CURSOR) |
---|
607 | item = self.itemPicked |
---|
608 | pixelSize = Data['pixelSize'] |
---|
609 | scalex = 1000./pixelSize[0] |
---|
610 | scaley = 1000./pixelSize[1] |
---|
611 | if item and self.PatternTree.GetItemText(self.PickId) == 'Image Controls': |
---|
612 | if 'Text' in str(item): |
---|
613 | Page.canvas.SetToolTipString('%8.3f %8.3fmm'%(event.xdata,event.ydata)) |
---|
614 | else: |
---|
615 | xcent,ycent = Data['center'] |
---|
616 | xpos = event.xdata-xcent |
---|
617 | ypos = event.ydata-ycent |
---|
618 | if 'line3' in str(item) or 'line4' in str(item) and not Data['fullIntegrate']: |
---|
619 | ang = int(atan2d(xpos,ypos)) |
---|
620 | Page.canvas.SetToolTipString('%6d deg'%(ang)) |
---|
621 | elif 'line1' in str(item) or 'line2' in str(item): |
---|
622 | tth = G2img.GetTth(event.xdata,event.ydata,Data) |
---|
623 | Page.canvas.SetToolTipString('%8.3fdeg'%(tth)) |
---|
624 | else: |
---|
625 | xpos = event.xdata |
---|
626 | ypos = event.ydata |
---|
627 | xpix = xpos*scalex |
---|
628 | ypix = ypos*scaley |
---|
629 | Int = 0 |
---|
630 | if (0 <= xpix <= size) and (0 <= ypix <= size): |
---|
631 | Int = self.ImageZ[ypix][xpix] |
---|
632 | tth,azm,dsp = G2img.GetTthAzmDsp(xpos,ypos,Data) |
---|
633 | Q = 2.*math.pi/dsp |
---|
634 | self.G2plotNB.status.SetFields(\ |
---|
635 | ['','Detector 2-th =%9.2fdeg, dsp =%9.3fA, Q = %6.3fA-1, azm = %7.2fdeg, I = %6d'%(tth,dsp,Q,azm,Int)]) |
---|
636 | |
---|
637 | def OnImPlotKeyPress(event): |
---|
638 | if self.PatternTree.GetItemText(self.PickId) == 'Masks': |
---|
639 | Data = self.PatternTree.GetItemPyData( |
---|
640 | G2gd.GetPatternTreeItemId(self,self.Image, 'Image Controls')) |
---|
641 | Masks = self.PatternTree.GetItemPyData( |
---|
642 | G2gd.GetPatternTreeItemId(self,self.Image, 'Masks')) |
---|
643 | Xpos = event.xdata |
---|
644 | if not Xpos: #got point out of frame |
---|
645 | return |
---|
646 | Ypos = event.ydata |
---|
647 | if event.key == 's': |
---|
648 | print 'spot mask @ ',Xpos,Ypos |
---|
649 | Masks['Points'].append([Xpos,Ypos,1.]) |
---|
650 | elif event.key == 'r': |
---|
651 | tth = G2img.GetTth(Xpos,Ypos,Data) |
---|
652 | print 'ring mask @ ',Xpos,Ypos,tth |
---|
653 | Masks['Rings'].append([tth,0.1]) |
---|
654 | elif event.key == 'a': |
---|
655 | tth,azm = G2img.GetTthAzm(Xpos,Ypos,Data) |
---|
656 | azm = int(azm) |
---|
657 | print 'arc mask @ ', Xpos,Ypos |
---|
658 | Masks['Arcs'].append([tth,[azm-5,azm+5],0.1]) |
---|
659 | elif event.key == 'p': |
---|
660 | self.setPoly = True |
---|
661 | Masks['Polygons'].append([]) |
---|
662 | print 'Polygon mask active - pick points with mouse LB' |
---|
663 | print ' use RB to close when > 2 points chosen' |
---|
664 | print 'Vertices can be dragged with LB down after polygon closed' |
---|
665 | G2imG.UpdateMasks(self,Masks) |
---|
666 | PlotImage(self) |
---|
667 | |
---|
668 | def OnImPick(event): |
---|
669 | if self.PatternTree.GetItemText(self.PickId) not in ['Image Controls','Masks']: |
---|
670 | return |
---|
671 | if self.setPoly: |
---|
672 | Masks = self.PatternTree.GetItemPyData( |
---|
673 | G2gd.GetPatternTreeItemId(self,self.Image, 'Masks')) |
---|
674 | polygon = Masks['Polygons'][-1] |
---|
675 | xpos,ypos = event.mouseevent.xdata,event.mouseevent.ydata |
---|
676 | if xpos and ypos: #point inside image |
---|
677 | if len(polygon) > 2 and event.mouseevent.button == 3: |
---|
678 | x0,y0 = polygon[0] |
---|
679 | polygon.append([x0,y0]) |
---|
680 | self.setPoly = False |
---|
681 | else: |
---|
682 | polygon.append([xpos,ypos]) |
---|
683 | G2imG.UpdateMasks(self,Masks) |
---|
684 | else: |
---|
685 | if self.itemPicked is not None: return |
---|
686 | self.itemPicked = event.artist |
---|
687 | self.mousePicked = event.mouseevent |
---|
688 | |
---|
689 | def OnImRelease(event): |
---|
690 | PickName = self.PatternTree.GetItemText(self.PickId) |
---|
691 | if PickName not in ['Image Controls','Masks']: |
---|
692 | return |
---|
693 | Data = self.PatternTree.GetItemPyData( |
---|
694 | G2gd.GetPatternTreeItemId(self,self.Image, 'Image Controls')) |
---|
695 | Masks = self.PatternTree.GetItemPyData( |
---|
696 | G2gd.GetPatternTreeItemId(self,self.Image, 'Masks')) |
---|
697 | pixelSize = Data['pixelSize'] |
---|
698 | scalex = 1000./pixelSize[0] |
---|
699 | scaley = 1000./pixelSize[1] |
---|
700 | if self.itemPicked is None and PickName == 'Image Controls': |
---|
701 | size = len(self.ImageZ) |
---|
702 | Xpos = event.xdata |
---|
703 | if not (Xpos and self.ifGetRing): #got point out of frame |
---|
704 | return |
---|
705 | Ypos = event.ydata |
---|
706 | if Ypos and not Page.toolbar._active: #make sure zoom/pan not selected |
---|
707 | if event.button == 1: |
---|
708 | Xpix = Xpos*scalex |
---|
709 | Ypix = Ypos*scaley |
---|
710 | xpos,ypos,I,J = G2img.ImageLocalMax(self.ImageZ,20,Xpix,Ypix) |
---|
711 | if I and J: |
---|
712 | xpos += .5 #shift to pixel center |
---|
713 | ypos += .5 |
---|
714 | xpos /= scalex #convert to mm |
---|
715 | ypos /= scaley |
---|
716 | Data['ring'].append([xpos,ypos]) |
---|
717 | PlotImage(self) |
---|
718 | return |
---|
719 | else: |
---|
720 | xpos = event.xdata |
---|
721 | if xpos: #avoid out of frame mouse position |
---|
722 | ypos = event.ydata |
---|
723 | if self.ifGetRing: |
---|
724 | xypos = [xpos,ypos] |
---|
725 | rings = Data['ring'] |
---|
726 | for ring in rings: |
---|
727 | if np.allclose(ring,xypos,.01,0): |
---|
728 | rings.remove(ring) |
---|
729 | else: |
---|
730 | tth,azm,dsp = G2img.GetTthAzmDsp(xpos,ypos,Data) |
---|
731 | itemPicked = str(self.itemPicked) |
---|
732 | if 'Line2D' in itemPicked and PickName == 'Image Controls': |
---|
733 | print int(itemPicked.split('_line')[1].strip(')')) |
---|
734 | if 'line1' in itemPicked: |
---|
735 | Data['IOtth'][0] = tth |
---|
736 | elif 'line2' in itemPicked: |
---|
737 | Data['IOtth'][1] = tth |
---|
738 | elif 'line3' in itemPicked and not Data['fullIntegrate']: |
---|
739 | Data['LRazimuth'][0] = int(azm) |
---|
740 | elif 'line4' in itemPicked and not Data['fullIntegrate']: |
---|
741 | Data['LRazimuth'][1] = int(azm) |
---|
742 | |
---|
743 | if Data['LRazimuth'][1] < Data['LRazimuth'][0]: |
---|
744 | Data['LRazimuth'][1] += 360 |
---|
745 | if Data['IOtth'][0] > Data['IOtth'][1]: |
---|
746 | Data['IOtth'][0],Data['IOtth'][1] = Data['IOtth'][1],Data['IOtth'][0] |
---|
747 | |
---|
748 | self.InnerTth.SetValue("%8.2f" % (Data['IOtth'][0])) |
---|
749 | self.OuterTth.SetValue("%8.2f" % (Data['IOtth'][1])) |
---|
750 | self.Lazim.SetValue("%6d" % (Data['LRazimuth'][0])) |
---|
751 | self.Razim.SetValue("%6d" % (Data['LRazimuth'][1])) |
---|
752 | elif 'Circle' in itemPicked and PickName == 'Masks': |
---|
753 | spots = Masks['Points'] |
---|
754 | newPos = itemPicked.split(')')[0].split('(')[2].split(',') |
---|
755 | newPos = np.array([float(newPos[0]),float(newPos[1])]) |
---|
756 | for spot in spots: |
---|
757 | if np.allclose(np.array([spot[:2]]),newPos): |
---|
758 | spot[:2] = xpos,ypos |
---|
759 | G2imG.UpdateMasks(self,Masks) |
---|
760 | elif 'Line2D' in itemPicked and PickName == 'Masks': |
---|
761 | Obj = self.itemPicked.findobj() |
---|
762 | rings = Masks['Rings'] |
---|
763 | arcs = Masks['Arcs'] |
---|
764 | polygons = Masks['Polygons'] |
---|
765 | for ring in self.ringList: |
---|
766 | if Obj == ring[0]: |
---|
767 | rN = ring[1] |
---|
768 | if ring[2] == 'o': |
---|
769 | rings[rN][0] = G2img.GetTth(xpos,ypos,Data)-rings[rN][1]/2. |
---|
770 | else: |
---|
771 | rings[rN][0] = G2img.GetTth(xpos,ypos,Data)+rings[rN][1]/2. |
---|
772 | for arc in self.arcList: |
---|
773 | if Obj == arc[0]: |
---|
774 | aN = arc[1] |
---|
775 | if arc[2] == 'o': |
---|
776 | arcs[aN][0] = G2img.GetTth(xpos,ypos,Data)-arcs[aN][2]/2 |
---|
777 | elif arc[2] == 'i': |
---|
778 | arcs[aN][0] = G2img.GetTth(xpos,ypos,Data)+arcs[aN][2]/2 |
---|
779 | elif arc[2] == 'l': |
---|
780 | arcs[aN][1][0] = int(G2img.GetAzm(xpos,ypos,Data)) |
---|
781 | else: |
---|
782 | arcs[aN][1][1] = int(G2img.GetAzm(xpos,ypos,Data)) |
---|
783 | for poly in self.polyList: |
---|
784 | if Obj == poly[0]: |
---|
785 | ind = self.itemPicked.contains(self.mousePicked)[1]['ind'][0] |
---|
786 | oldPos = np.array([self.mousePicked.xdata,self.mousePicked.ydata]) |
---|
787 | pN = poly[1] |
---|
788 | for i,xy in enumerate(polygons[pN]): |
---|
789 | if np.allclose(np.array([xy]),oldPos,atol=1.0): |
---|
790 | polygons[pN][i] = xpos,ypos |
---|
791 | G2imG.UpdateMasks(self,Masks) |
---|
792 | # else: #keep for future debugging |
---|
793 | # print str(self.itemPicked),event.xdata,event.ydata,event.button |
---|
794 | PlotImage(self) |
---|
795 | self.itemPicked = None |
---|
796 | |
---|
797 | try: |
---|
798 | plotNum = self.G2plotNB.plotList.index('2D Powder Image') |
---|
799 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
800 | if not newPlot: |
---|
801 | Plot = Page.figure.gca() #get previous powder plot & get limits |
---|
802 | xylim = Plot.get_xlim(),Plot.get_ylim() |
---|
803 | Page.figure.clf() |
---|
804 | Plot = Page.figure.gca() #get a fresh plot after clf() |
---|
805 | |
---|
806 | except ValueError,error: |
---|
807 | Plot = self.G2plotNB.add('2D Powder Image').gca() |
---|
808 | plotNum = self.G2plotNB.plotList.index('2D Powder Image') |
---|
809 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
810 | Page.canvas.mpl_connect('key_press_event', OnImPlotKeyPress) |
---|
811 | Page.canvas.mpl_connect('motion_notify_event', OnImMotion) |
---|
812 | Page.canvas.mpl_connect('pick_event', OnImPick) |
---|
813 | Page.canvas.mpl_connect('button_release_event', OnImRelease) |
---|
814 | if not event: #event from GUI TextCtrl - don't want focus to change to plot!!! |
---|
815 | Page.SetFocus() |
---|
816 | Plot.set_title(self.PatternTree.GetItemText(self.Image)[4:]) |
---|
817 | size,imagefile = self.PatternTree.GetItemPyData(self.Image) |
---|
818 | if imagefile != self.oldImagefile: |
---|
819 | self.ImageZ = G2IO.GetImageData(imagefile,imageOnly=True) |
---|
820 | self.oldImagefile = imagefile |
---|
821 | Data = self.PatternTree.GetItemPyData( |
---|
822 | G2gd.GetPatternTreeItemId(self,self.Image, 'Image Controls')) |
---|
823 | Masks = self.PatternTree.GetItemPyData( |
---|
824 | G2gd.GetPatternTreeItemId(self,self.Image, 'Masks')) |
---|
825 | |
---|
826 | imScale = 1 |
---|
827 | if len(self.ImageZ) > 1024: |
---|
828 | imScale = len(self.ImageZ)/1024 |
---|
829 | pixelSize = Data['pixelSize'] |
---|
830 | scalex = 1000./pixelSize[0] |
---|
831 | scaley = 1000./pixelSize[1] |
---|
832 | xmax = len(self.ImageZ) |
---|
833 | Xmax = len(self.ImageZ)*pixelSize[0]/1000. |
---|
834 | xlim = (-0.5,Xmax-.5) |
---|
835 | ylim = (Xmax-.5,-0.5,) |
---|
836 | Imin,Imax = Data['range'][1] |
---|
837 | acolor = mpl.cm.get_cmap(Data['color']) |
---|
838 | xcent,ycent = Data['center'] |
---|
839 | Plot.set_xlabel('Image x-axis, mm',fontsize=12) |
---|
840 | Plot.set_ylabel('Image y-axis, mm',fontsize=12) |
---|
841 | #do threshold mask - "real" mask - others are just bondaries |
---|
842 | Zlim = Masks['Thresholds'][1] |
---|
843 | wx.BeginBusyCursor() |
---|
844 | try: |
---|
845 | MA = ma.masked_greater(ma.masked_less(self.ImageZ,Zlim[0]),Zlim[1]) |
---|
846 | MaskA = ma.getmaskarray(MA) |
---|
847 | A = G2img.ImageCompress(MA,imScale) |
---|
848 | AM = G2img.ImageCompress(MaskA,imScale) |
---|
849 | |
---|
850 | ImgM = Plot.imshow(AM,aspect='equal',cmap='Reds', |
---|
851 | interpolation='nearest',vmin=0,vmax=2,extent=[0,Xmax,Xmax,0]) |
---|
852 | Img = Plot.imshow(A,aspect='equal',cmap=acolor, |
---|
853 | interpolation='nearest',vmin=Imin,vmax=Imax,extent=[0,Xmax,Xmax,0]) |
---|
854 | if self.setPoly: |
---|
855 | Img.set_picker(True) |
---|
856 | |
---|
857 | Plot.plot(xcent,ycent,'x') |
---|
858 | if Data['showLines']: |
---|
859 | LRAzim = Data['LRazimuth'] #NB: integers |
---|
860 | IOtth = Data['IOtth'] |
---|
861 | wave = Data['wavelength'] |
---|
862 | dspI = wave/(2.0*sind(IOtth[0]/2.0)) |
---|
863 | ellI = G2img.GetEllipse(dspI,Data) #=False if dsp didn't yield an ellipse (ugh! a parabola or a hyperbola) |
---|
864 | dspO = wave/(2.0*sind(IOtth[1]/2.0)) |
---|
865 | ellO = G2img.GetEllipse(dspO,Data) #Ditto & more likely for outer ellipse |
---|
866 | if Data['fullIntegrate']: |
---|
867 | Azm = np.array(range(0,361)) |
---|
868 | else: |
---|
869 | Azm = np.array(range(LRAzim[0],LRAzim[1]+1)) |
---|
870 | if ellI: |
---|
871 | xyI = [] |
---|
872 | for azm in Azm: |
---|
873 | xyI.append(G2img.GetDetectorXY(dspI,azm,Data)) |
---|
874 | xyI = np.array(xyI) |
---|
875 | arcxI,arcyI = xyI.T |
---|
876 | Plot.plot(arcxI,arcyI,picker=3) |
---|
877 | if ellO: |
---|
878 | xyO = [] |
---|
879 | for azm in Azm: |
---|
880 | xyO.append(G2img.GetDetectorXY(dspO,azm,Data)) |
---|
881 | xyO = np.array(xyO) |
---|
882 | arcxO,arcyO = xyO.T |
---|
883 | Plot.plot(arcxO,arcyO,picker=3) |
---|
884 | if ellO and ellI and not Data['fullIntegrate']: |
---|
885 | Plot.plot([arcxI[0],arcxO[0]],[arcyI[0],arcyO[0]],picker=3) |
---|
886 | Plot.plot([arcxI[-1],arcxO[-1]],[arcyI[-1],arcyO[-1]],picker=3) |
---|
887 | for xring,yring in Data['ring']: |
---|
888 | Plot.plot(xring,yring,'r+',picker=3) |
---|
889 | if Data['setRings']: |
---|
890 | rings = np.concatenate((Data['rings']),axis=0) |
---|
891 | for xring,yring,dsp in rings: |
---|
892 | Plot.plot(xring,yring,'r+') |
---|
893 | for ellipse in Data['ellipses']: |
---|
894 | cent,phi,[width,height],col = ellipse |
---|
895 | Plot.add_artist(Ellipse([cent[0],cent[1]],2*width,2*height,phi,ec=col,fc='none')) |
---|
896 | Plot.text(cent[0],cent[1],'+',color=col,ha='center',va='center') |
---|
897 | #masks - mask lines numbered after integration limit lines |
---|
898 | spots = Masks['Points'] |
---|
899 | rings = Masks['Rings'] |
---|
900 | arcs = Masks['Arcs'] |
---|
901 | polygons = Masks['Polygons'] |
---|
902 | for x,y,d in spots: |
---|
903 | Plot.add_artist(Circle((x,y),radius=d/2,fc='none',ec='r',picker=3)) |
---|
904 | self.ringList = [] |
---|
905 | for iring,(tth,thick) in enumerate(rings): |
---|
906 | wave = Data['wavelength'] |
---|
907 | x1,y1 = np.hsplit(np.array(G2img.makeIdealRing(G2img.GetEllipse(Dsp(tth+thick/2.,wave),Data))),2) |
---|
908 | x2,y2 = np.hsplit(np.array(G2img.makeIdealRing(G2img.GetEllipse(Dsp(tth-thick/2.,wave),Data))),2) |
---|
909 | self.ringList.append([Plot.plot(x1,y1,'r',picker=3),iring,'o']) |
---|
910 | self.ringList.append([Plot.plot(x2,y2,'r',picker=3),iring,'i']) |
---|
911 | self.arcList = [] |
---|
912 | for iarc,(tth,azm,thick) in enumerate(arcs): |
---|
913 | wave = Data['wavelength'] |
---|
914 | x1,y1 = np.hsplit(np.array(G2img.makeIdealRing(G2img.GetEllipse(Dsp(tth+thick/2.,wave),Data),azm)),2) |
---|
915 | x2,y2 = np.hsplit(np.array(G2img.makeIdealRing(G2img.GetEllipse(Dsp(max(0.01,tth-thick/2.),wave),Data),azm)),2) |
---|
916 | self.arcList.append([Plot.plot(x1,y1,'r',picker=3),iarc,'o']) |
---|
917 | self.arcList.append([Plot.plot(x2,y2,'r',picker=3),iarc,'i']) |
---|
918 | self.arcList.append([Plot.plot([x1[0],x2[0]],[y1[0],y2[0]],'r',picker=3),iarc,'l']) |
---|
919 | self.arcList.append([Plot.plot([x1[-1],x2[-1]],[y1[-1],y2[-1]],'r',picker=3),iarc,'u']) |
---|
920 | self.polyList = [] |
---|
921 | for ipoly,polygon in enumerate(polygons): |
---|
922 | x,y = np.hsplit(np.array(polygon),2) |
---|
923 | self.polyList.append([Plot.plot(x,y,'r',picker=3),ipoly]) |
---|
924 | colorBar = Page.figure.colorbar(Img) |
---|
925 | Plot.set_xlim(xlim) |
---|
926 | Plot.set_ylim(ylim) |
---|
927 | if not newPlot: |
---|
928 | Page.toolbar.push_current() |
---|
929 | Plot.set_xlim(xylim[0]) |
---|
930 | Plot.set_ylim(xylim[1]) |
---|
931 | xylim = [] |
---|
932 | Page.toolbar.push_current() |
---|
933 | Page.toolbar.draw() |
---|
934 | else: |
---|
935 | Page.canvas.draw() |
---|
936 | finally: |
---|
937 | wx.EndBusyCursor() |
---|
938 | |
---|
939 | def PlotIntegration(self,newPlot=False,event=None): |
---|
940 | |
---|
941 | def OnMotion(event): |
---|
942 | Page.canvas.SetToolTipString('') |
---|
943 | Page.canvas.SetCursor(wx.CROSS_CURSOR) |
---|
944 | azm = event.ydata |
---|
945 | tth = event.xdata |
---|
946 | if azm and tth: |
---|
947 | self.G2plotNB.status.SetFields(\ |
---|
948 | ['','Detector 2-th =%9.3fdeg, azm = %7.2fdeg'%(tth,azm)]) |
---|
949 | |
---|
950 | try: |
---|
951 | plotNum = self.G2plotNB.plotList.index('2D Integration') |
---|
952 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
953 | if not newPlot: |
---|
954 | Plot = Page.figure.gca() #get previous plot & get limits |
---|
955 | xylim = Plot.get_xlim(),Plot.get_ylim() |
---|
956 | Page.figure.clf() |
---|
957 | Plot = Page.figure.gca() #get a fresh plot after clf() |
---|
958 | |
---|
959 | except ValueError,error: |
---|
960 | Plot = self.G2plotNB.add('2D Integration').gca() |
---|
961 | plotNum = self.G2plotNB.plotList.index('2D Integration') |
---|
962 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
963 | Page.canvas.mpl_connect('motion_notify_event', OnMotion) |
---|
964 | Page.views = False |
---|
965 | view = False |
---|
966 | if not event: |
---|
967 | Page.SetFocus() |
---|
968 | |
---|
969 | Data = self.PatternTree.GetItemPyData( |
---|
970 | G2gd.GetPatternTreeItemId(self,self.Image, 'Image Controls')) |
---|
971 | image = self.Integrate[0] |
---|
972 | xsc = self.Integrate[1] |
---|
973 | ysc = self.Integrate[2] |
---|
974 | Imin,Imax = Data['range'][1] |
---|
975 | acolor = mpl.cm.get_cmap(Data['color']) |
---|
976 | Plot.set_title(self.PatternTree.GetItemText(self.Image)[4:]) |
---|
977 | Plot.set_ylabel('azimuth',fontsize=12) |
---|
978 | Plot.set_xlabel('2-theta',fontsize=12) |
---|
979 | Img = Plot.imshow(image,cmap=acolor,vmin=Imin,vmax=Imax,interpolation='nearest', \ |
---|
980 | extent=[ysc[0],ysc[-1],xsc[-1],xsc[0]],aspect='auto') |
---|
981 | colorBar = Page.figure.colorbar(Img) |
---|
982 | if Data['setRings']: |
---|
983 | rings = np.concatenate((Data['rings']),axis=0) |
---|
984 | for xring,yring,dsp in rings: |
---|
985 | x,y = G2img.GetTthAzm(xring,yring,Data) |
---|
986 | Plot.plot(x,y,'r+') |
---|
987 | if Data['ellipses']: |
---|
988 | for ellipse in Data['ellipses']: |
---|
989 | ring = np.array(G2img.makeIdealRing(ellipse[:3])) #skip color |
---|
990 | x,y = np.hsplit(ring,2) |
---|
991 | tth,azm = G2img.GetTthAzm(x,y,Data) |
---|
992 | Plot.plot(tth,azm,'b,') |
---|
993 | if not newPlot: |
---|
994 | Page.toolbar.push_current() |
---|
995 | Plot.set_xlim(xylim[0]) |
---|
996 | Plot.set_ylim(xylim[1]) |
---|
997 | xylim = [] |
---|
998 | Page.toolbar.push_current() |
---|
999 | Page.toolbar.draw() |
---|
1000 | else: |
---|
1001 | Page.canvas.draw() |
---|
1002 | |
---|
1003 | |
---|
1004 | def PlotTRImage(self,tax,tay,taz,newPlot=False): |
---|
1005 | #a test plot routine - not normally used |
---|
1006 | |
---|
1007 | def OnMotion(event): |
---|
1008 | Page.canvas.SetToolTipString('') |
---|
1009 | Page.canvas.SetCursor(wx.CROSS_CURSOR) |
---|
1010 | azm = event.xdata |
---|
1011 | tth = event.ydata |
---|
1012 | if azm and tth: |
---|
1013 | self.G2plotNB.status.SetFields(\ |
---|
1014 | ['','Detector 2-th =%9.3fdeg, azm = %7.2fdeg'%(tth,azm)]) |
---|
1015 | |
---|
1016 | try: |
---|
1017 | plotNum = self.G2plotNB.plotList.index('2D Transformed Powder Image') |
---|
1018 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
1019 | if not newPlot: |
---|
1020 | Plot = Page.figure.gca() #get previous plot & get limits |
---|
1021 | xylim = Plot.get_xlim(),Plot.get_ylim() |
---|
1022 | Page.figure.clf() |
---|
1023 | Plot = Page.figure.gca() #get a fresh plot after clf() |
---|
1024 | |
---|
1025 | except ValueError,error: |
---|
1026 | Plot = self.G2plotNB.add('2D Transformed Powder Image').gca() |
---|
1027 | plotNum = self.G2plotNB.plotList.index('2D Transformed Powder Image') |
---|
1028 | Page = self.G2plotNB.nb.GetPage(plotNum) |
---|
1029 | Page.canvas.mpl_connect('motion_notify_event', OnMotion) |
---|
1030 | Page.views = False |
---|
1031 | view = False |
---|
1032 | Page.SetFocus() |
---|
1033 | |
---|
1034 | Data = self.PatternTree.GetItemPyData( |
---|
1035 | G2gd.GetPatternTreeItemId(self,self.Image, 'Image Controls')) |
---|
1036 | Imin,Imax = Data['range'][1] |
---|
1037 | step = (Imax-Imin)/5. |
---|
1038 | V = np.arange(Imin,Imax,step) |
---|
1039 | acolor = mpl.cm.get_cmap(Data['color']) |
---|
1040 | Plot.set_title(self.PatternTree.GetItemText(self.Image)[4:]) |
---|
1041 | Plot.set_xlabel('azimuth',fontsize=12) |
---|
1042 | Plot.set_ylabel('2-theta',fontsize=12) |
---|
1043 | Plot.contour(tax,tay,taz,V,cmap=acolor) |
---|
1044 | if Data['showLines']: |
---|
1045 | IOtth = Data['IOtth'] |
---|
1046 | if Data['fullIntegrate']: |
---|
1047 | LRAzim = [-180,180] |
---|
1048 | else: |
---|
1049 | LRAzim = Data['LRazimuth'] #NB: integers |
---|
1050 | Plot.plot([LRAzim[0],LRAzim[1]],[IOtth[0],IOtth[0]],picker=True) |
---|
1051 | Plot.plot([LRAzim[0],LRAzim[1]],[IOtth[1],IOtth[1]],picker=True) |
---|
1052 | if not Data['fullIntegrate']: |
---|
1053 | Plot.plot([LRAzim[0],LRAzim[0]],[IOtth[0],IOtth[1]],picker=True) |
---|
1054 | Plot.plot([LRAzim[1],LRAzim[1]],[IOtth[0],IOtth[1]],picker=True) |
---|
1055 | if Data['setRings']: |
---|
1056 | rings = np.concatenate((Data['rings']),axis=0) |
---|
1057 | for xring,yring,dsp in rings: |
---|
1058 | x,y = G2img.GetTthAzm(xring,yring,Data) |
---|
1059 | Plot.plot(y,x,'r+') |
---|
1060 | if Data['ellipses']: |
---|
1061 | for ellipse in Data['ellipses']: |
---|
1062 | ring = np.array(G2img.makeIdealRing(ellipse[:3])) #skip color |
---|
1063 | x,y = np.hsplit(ring,2) |
---|
1064 | tth,azm = G2img.GetTthAzm(x,y,Data) |
---|
1065 | Plot.plot(azm,tth,'b,') |
---|
1066 | if not newPlot: |
---|
1067 | Page.toolbar.push_current() |
---|
1068 | Plot.set_xlim(xylim[0]) |
---|
1069 | Plot.set_ylim(xylim[1]) |
---|
1070 | xylim = [] |
---|
1071 | Page.toolbar.push_current() |
---|
1072 | Page.toolbar.draw() |
---|
1073 | else: |
---|
1074 | Page.canvas.draw() |
---|
1075 | |
---|
1076 | |
---|