1 | import wx.lib.newevent |
---|
2 | import matplotlib |
---|
3 | matplotlib.interactive(False) |
---|
4 | #Use the WxAgg back end. The Wx one takes too long to render |
---|
5 | matplotlib.use('WXAgg') |
---|
6 | from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg |
---|
7 | from matplotlib.figure import Figure |
---|
8 | import os |
---|
9 | import fittings |
---|
10 | import transform |
---|
11 | from canvas import FigureCanvas |
---|
12 | from matplotlib.widgets import RectangleSelector |
---|
13 | from pylab import gca, gcf |
---|
14 | from plottables import Theory1D |
---|
15 | #from plottables import Data1D |
---|
16 | #TODO: make the plottables interactive |
---|
17 | |
---|
18 | DEBUG = False |
---|
19 | |
---|
20 | from plottables import Graph |
---|
21 | #(FuncFitEvent, EVT_FUNC_FIT) = wx.lib.newevent.NewEvent() |
---|
22 | import math,pylab,re |
---|
23 | |
---|
24 | def show_tree(obj,d=0): |
---|
25 | """Handy function for displaying a tree of graph objects""" |
---|
26 | print "%s%s" % ("-"*d,obj.__class__.__name__) |
---|
27 | if 'get_children' in dir(obj): |
---|
28 | for a in obj.get_children(): show_tree(a,d+1) |
---|
29 | |
---|
30 | def convertUnit(pow,unit): |
---|
31 | """ |
---|
32 | Displays the unit with the proper convertion |
---|
33 | @param pow: the user set the power of the unit |
---|
34 | @param unit: the unit of the data |
---|
35 | """ |
---|
36 | toks=re.match("^", unit) |
---|
37 | if not toks==None: |
---|
38 | unitValue= re.split("{",unit) |
---|
39 | unitPower= re.split("}",unitValue[1]) |
---|
40 | power= int(unitPower[0])*pow |
---|
41 | word= unitValue[0]+"{"+str(power)+"}" |
---|
42 | if power==1: |
---|
43 | tempUnit=re.split("\^",unitValue[0]) |
---|
44 | unit=tempUnit[0] |
---|
45 | else: |
---|
46 | unit = word |
---|
47 | #print"this is unit",unit |
---|
48 | return unit |
---|
49 | def _rescale(lo,hi,step,pt=None,bal=None,scale='linear'): |
---|
50 | """ |
---|
51 | Rescale (lo,hi) by step, returning the new (lo,hi) |
---|
52 | The scaling is centered on pt, with positive values of step |
---|
53 | driving lo/hi away from pt and negative values pulling them in. |
---|
54 | If bal is given instead of point, it is already in [0,1] coordinates. |
---|
55 | |
---|
56 | This is a helper function for step-based zooming. |
---|
57 | """ |
---|
58 | # Convert values into the correct scale for a linear transformation |
---|
59 | # TODO: use proper scale transformers |
---|
60 | loprev = lo |
---|
61 | hiprev = hi |
---|
62 | ptprev = pt |
---|
63 | if scale=='log': |
---|
64 | assert lo >0 |
---|
65 | if lo > 0 : |
---|
66 | lo = math.log10(lo) |
---|
67 | if hi > 0 : |
---|
68 | hi = math.log10(hi) |
---|
69 | if pt is not None: pt = math.log10(pt) |
---|
70 | |
---|
71 | # Compute delta from axis range * %, or 1-% if persent is negative |
---|
72 | if step > 0: |
---|
73 | delta = float(hi-lo)*step/100 |
---|
74 | else: |
---|
75 | delta = float(hi-lo)*step/(100-step) |
---|
76 | |
---|
77 | # Add scale factor proportionally to the lo and hi values, preserving the |
---|
78 | # point under the mouse |
---|
79 | if bal is None: |
---|
80 | bal = float(pt-lo)/(hi-lo) |
---|
81 | lo = lo - bal*delta |
---|
82 | hi = hi + (1-bal)*delta |
---|
83 | |
---|
84 | # Convert transformed values back to the original scale |
---|
85 | if scale=='log': |
---|
86 | if (lo <= -250) or (hi >= 250): |
---|
87 | lo=loprev |
---|
88 | hi=hiprev |
---|
89 | print "Not possible to scale" |
---|
90 | |
---|
91 | else: |
---|
92 | lo,hi = math.pow(10.,lo),math.pow(10.,hi) |
---|
93 | #assert lo >0,"lo = %g"%lo |
---|
94 | print "possible to scale" |
---|
95 | |
---|
96 | print "these are low and high",lo,hi |
---|
97 | |
---|
98 | return (lo,hi) |
---|
99 | |
---|
100 | |
---|
101 | class PlotPanel(wx.Panel): |
---|
102 | """ |
---|
103 | The PlotPanel has a Figure and a Canvas. OnSize events simply set a |
---|
104 | flag, and the actually redrawing of the |
---|
105 | figure is triggered by an Idle event. |
---|
106 | """ |
---|
107 | def __init__(self, parent, id = -1, color = None,\ |
---|
108 | dpi = None, **kwargs): |
---|
109 | wx.Panel.__init__(self, parent, id = id, **kwargs) |
---|
110 | self.parent = parent |
---|
111 | self.figure = Figure(None, dpi) |
---|
112 | #self.figure = pylab.Figure(None, dpi) |
---|
113 | #self.canvas = NoRepaintCanvas(self, -1, self.figure) |
---|
114 | self.canvas = FigureCanvas(self, -1, self.figure) |
---|
115 | self.SetColor(color) |
---|
116 | #self.Bind(wx.EVT_IDLE, self._onIdle) |
---|
117 | #self.Bind(wx.EVT_SIZE, self._onSize) |
---|
118 | self._resizeflag = True |
---|
119 | self._SetSize() |
---|
120 | self.subplot = self.figure.add_subplot(111) |
---|
121 | self.figure.subplots_adjust(left=.2, bottom=.2) |
---|
122 | self.yscale = 'linear' |
---|
123 | self.xscale = 'linear' |
---|
124 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
125 | sizer.Add(self.canvas,1,wx.EXPAND) |
---|
126 | self.SetSizer(sizer) |
---|
127 | |
---|
128 | # Graph object to manage the plottables |
---|
129 | self.graph = Graph() |
---|
130 | #self.Bind(EVT_FUNC_FIT, self.onFitRange) |
---|
131 | self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu) |
---|
132 | #self.Bind(EVT_PROPERTY, self._onEVT_FUNC_PROPERTY) |
---|
133 | # Define some constants |
---|
134 | self.colorlist = ['b','g','r','c','m','y'] |
---|
135 | self.symbollist = ['o','x','^','v','<','>','+','s','d','D','h','H','p'] |
---|
136 | #User scale |
---|
137 | self.xLabel ="x" |
---|
138 | self.yLabel ="log10(y)" |
---|
139 | self.viewModel ="--" |
---|
140 | # keep track if the previous transformation of x and y in Property dialog |
---|
141 | self.prevXtrans =" " |
---|
142 | self.prevYtrans =" " |
---|
143 | self.canvas.mpl_connect('scroll_event',self.onWheel) |
---|
144 | self.axes = [self.subplot] |
---|
145 | # new data for the fit |
---|
146 | self.fit_result = Theory1D(x=[], y=[], dy=None) |
---|
147 | #self.fit_result = Data1D(x=[], y=[],dx=None, dy=None) |
---|
148 | self.fit_result.name = "Fit" |
---|
149 | # For fit Dialog initial display |
---|
150 | self.xmin=0.0 |
---|
151 | self.xmax=0.0 |
---|
152 | self.xminView=0.0 |
---|
153 | self.xmaxView=0.0 |
---|
154 | self.Avalue=None |
---|
155 | self.Bvalue=None |
---|
156 | self.ErrAvalue=None |
---|
157 | self.ErrBvalue=None |
---|
158 | self.Chivalue=None |
---|
159 | def resetFitView(self): |
---|
160 | """ |
---|
161 | For fit Dialog initial display |
---|
162 | """ |
---|
163 | self.xmin=0.0 |
---|
164 | self.xmax=0.0 |
---|
165 | self.xminView=0.0 |
---|
166 | self.xmaxView=0.0 |
---|
167 | self.Avalue=None |
---|
168 | self.Bvalue=None |
---|
169 | self.ErrAvalue=None |
---|
170 | self.ErrBvalue=None |
---|
171 | self.Chivalue=None |
---|
172 | |
---|
173 | def onWheel(self, event): |
---|
174 | """ |
---|
175 | Process mouse wheel as zoom events |
---|
176 | @param event: Wheel event |
---|
177 | """ |
---|
178 | ax = event.inaxes |
---|
179 | step = event.step |
---|
180 | |
---|
181 | if ax != None: |
---|
182 | # Event occurred inside a plotting area |
---|
183 | lo,hi = ax.get_xlim() |
---|
184 | lo,hi = _rescale(lo,hi,step,pt=event.xdata,scale=ax.get_xscale()) |
---|
185 | if not self.xscale=='log' or lo>0: |
---|
186 | ax.set_xlim((lo,hi)) |
---|
187 | |
---|
188 | lo,hi = ax.get_ylim() |
---|
189 | lo,hi = _rescale(lo,hi,step,pt=event.ydata,scale=ax.get_yscale()) |
---|
190 | if not self.yscale=='log' or lo>0: |
---|
191 | ax.set_ylim((lo,hi)) |
---|
192 | else: |
---|
193 | # Check if zoom happens in the axes |
---|
194 | xdata,ydata = None,None |
---|
195 | x,y = event.x,event.y |
---|
196 | |
---|
197 | for ax in self.axes: |
---|
198 | insidex,_ = ax.xaxis.contains(event) |
---|
199 | if insidex: |
---|
200 | xdata,_ = ax.transAxes.inverse_xy_tup((x,y)) |
---|
201 | insidey,_ = ax.yaxis.contains(event) |
---|
202 | if insidey: |
---|
203 | _,ydata = ax.transAxes.inverse_xy_tup((x,y)) |
---|
204 | if xdata is not None: |
---|
205 | lo,hi = ax.get_xlim() |
---|
206 | lo,hi = _rescale(lo,hi,step,bal=xdata,scale=ax.get_xscale()) |
---|
207 | if not self.xscale=='log' or lo>0: |
---|
208 | ax.set_xlim((lo,hi)) |
---|
209 | if ydata is not None: |
---|
210 | lo,hi = ax.get_ylim() |
---|
211 | lo,hi = _rescale(lo,hi,step,bal=ydata,scale=ax.get_yscale()) |
---|
212 | if not self.yscale=='log' or lo>0: |
---|
213 | ax.set_ylim((lo,hi)) |
---|
214 | |
---|
215 | self.canvas.draw_idle() |
---|
216 | |
---|
217 | |
---|
218 | def returnTrans(self): |
---|
219 | """ |
---|
220 | Return values and labels used by Fit Dialog |
---|
221 | """ |
---|
222 | return self.xLabel,self.yLabel, self.Avalue, self.Bvalue,\ |
---|
223 | self.ErrAvalue,self.ErrBvalue,self.Chivalue |
---|
224 | |
---|
225 | def setTrans(self,xtrans,ytrans): |
---|
226 | """ |
---|
227 | @param xtrans: set x transformation on Property dialog |
---|
228 | @param ytrans: set y transformation on Property dialog |
---|
229 | """ |
---|
230 | self.prevXtrans =xtrans |
---|
231 | self.prevYtrans =ytrans |
---|
232 | |
---|
233 | def onFitting(self, event): |
---|
234 | """ |
---|
235 | when clicking on linear Fit on context menu , display Fitting Dialog |
---|
236 | """ |
---|
237 | list =[] |
---|
238 | list = self.graph.returnPlottable() |
---|
239 | from fitDialog import LinearFit |
---|
240 | |
---|
241 | if len(list.keys())>0: |
---|
242 | first_item = list.keys()[0] |
---|
243 | dlg = LinearFit( None, first_item, self.onFitDisplay,self.returnTrans, -1, 'Linear Fit') |
---|
244 | |
---|
245 | if (self.xmin !=0.0 )and ( self.xmax !=0.0)\ |
---|
246 | and(self.xminView !=0.0 )and ( self.xmaxView !=0.0): |
---|
247 | dlg.setFitRange(self.xminView,self.xmaxView,self.xmin,self.xmax) |
---|
248 | dlg.ShowModal() |
---|
249 | |
---|
250 | def _onProperties(self, event): |
---|
251 | """ |
---|
252 | when clicking on Properties on context menu ,The Property dialog is displayed |
---|
253 | The user selects a transformation for x or y value and a new plot is displayed |
---|
254 | """ |
---|
255 | list =[] |
---|
256 | list = self.graph.returnPlottable() |
---|
257 | if len(list.keys())>0: |
---|
258 | first_item = list.keys()[0] |
---|
259 | if first_item.x !=[]: |
---|
260 | from PropertyDialog import Properties |
---|
261 | dial = Properties(self, -1, 'Properties') |
---|
262 | dial.setValues( self.prevXtrans, self.prevYtrans,self.viewModel ) |
---|
263 | if dial.ShowModal() == wx.ID_OK: |
---|
264 | self.xLabel, self.yLabel,self.viewModel = dial.getValues() |
---|
265 | if self.viewModel =="Guinier lny vs x^(2)": |
---|
266 | self.xLabel="x^(2)" |
---|
267 | self.yLabel="ln(y)" |
---|
268 | self.viewModel = "--" |
---|
269 | dial.setValues( self.xLabel, self.yLabel,self.viewModel ) |
---|
270 | self._onEVT_FUNC_PROPERTY() |
---|
271 | dial.Destroy() |
---|
272 | |
---|
273 | |
---|
274 | def set_yscale(self, scale='linear'): |
---|
275 | """ |
---|
276 | Set the scale on Y-axis |
---|
277 | @param scale: the scale of y-axis |
---|
278 | """ |
---|
279 | self.subplot.set_yscale(scale) |
---|
280 | self.yscale = scale |
---|
281 | |
---|
282 | def get_yscale(self): |
---|
283 | """ |
---|
284 | @return: Y-axis scale |
---|
285 | """ |
---|
286 | return self.yscale |
---|
287 | |
---|
288 | def set_xscale(self, scale='linear'): |
---|
289 | """ |
---|
290 | Set the scale on x-axis |
---|
291 | @param scale: the scale of x-axis |
---|
292 | """ |
---|
293 | self.subplot.set_xscale(scale) |
---|
294 | self.xscale = scale |
---|
295 | |
---|
296 | def get_xscale(self): |
---|
297 | """ |
---|
298 | @return: x-axis scale |
---|
299 | """ |
---|
300 | return self.xscale |
---|
301 | |
---|
302 | def SetColor(self, rgbtuple): |
---|
303 | """Set figure and canvas colours to be the same""" |
---|
304 | if not rgbtuple: |
---|
305 | rgbtuple = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE).Get() |
---|
306 | col = [c/255.0 for c in rgbtuple] |
---|
307 | self.figure.set_facecolor(col) |
---|
308 | self.figure.set_edgecolor(col) |
---|
309 | self.canvas.SetBackgroundColour(wx.Colour(*rgbtuple)) |
---|
310 | |
---|
311 | def _onSize(self, event): |
---|
312 | self._resizeflag = True |
---|
313 | |
---|
314 | def _onIdle(self, evt): |
---|
315 | if self._resizeflag: |
---|
316 | self._resizeflag = False |
---|
317 | self._SetSize() |
---|
318 | self.draw() |
---|
319 | |
---|
320 | def _SetSize(self, pixels = None): |
---|
321 | """ |
---|
322 | This method can be called to force the Plot to be a desired size, which defaults to |
---|
323 | the ClientSize of the panel |
---|
324 | """ |
---|
325 | if not pixels: |
---|
326 | pixels = self.GetClientSize() |
---|
327 | self.canvas.SetSize(pixels) |
---|
328 | self.figure.set_size_inches(pixels[0]/self.figure.get_dpi(), |
---|
329 | pixels[1]/self.figure.get_dpi()) |
---|
330 | |
---|
331 | def draw(self): |
---|
332 | """Where the actual drawing happens""" |
---|
333 | self.figure.canvas.draw_idle() |
---|
334 | |
---|
335 | |
---|
336 | |
---|
337 | |
---|
338 | |
---|
339 | def onSaveImage(self, evt): |
---|
340 | #figure.savefig |
---|
341 | #print "Save image not implemented" |
---|
342 | path = None |
---|
343 | dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.png", wx.SAVE) |
---|
344 | if dlg.ShowModal() == wx.ID_OK: |
---|
345 | path = dlg.GetPath() |
---|
346 | mypath = os.path.basename(path) |
---|
347 | print path |
---|
348 | dlg.Destroy() |
---|
349 | if not path == None: |
---|
350 | self.subplot.figure.savefig(path,dpi=300, facecolor='w', edgecolor='w', |
---|
351 | orentation='portrait', papertype=None, format='png') |
---|
352 | |
---|
353 | def onContextMenu(self, event): |
---|
354 | """ |
---|
355 | Default context menu for a plot panel |
---|
356 | """ |
---|
357 | # Slicer plot popup menu |
---|
358 | slicerpop = wx.Menu() |
---|
359 | slicerpop.Append(313,'&Save image', 'Save image as PNG') |
---|
360 | wx.EVT_MENU(self, 313, self.onSaveImage) |
---|
361 | |
---|
362 | slicerpop.Append(316, '&Load 1D data file') |
---|
363 | wx.EVT_MENU(self, 316, self._onLoad1DData) |
---|
364 | |
---|
365 | slicerpop.AppendSeparator() |
---|
366 | slicerpop.Append(315, '&Properties') |
---|
367 | wx.EVT_MENU(self, 315, self._onProperties) |
---|
368 | |
---|
369 | slicerpop.AppendSeparator() |
---|
370 | slicerpop.Append(317, '&Linear Fit') |
---|
371 | wx.EVT_MENU(self, 317, self.onFitting) |
---|
372 | |
---|
373 | slicerpop.AppendSeparator() |
---|
374 | slicerpop.Append(318, '&Reset Graph') |
---|
375 | wx.EVT_MENU(self, 318, self.onResetGraph) |
---|
376 | |
---|
377 | pos = event.GetPosition() |
---|
378 | pos = self.ScreenToClient(pos) |
---|
379 | self.PopupMenu(slicerpop, pos) |
---|
380 | |
---|
381 | ## The following is plottable functionality |
---|
382 | |
---|
383 | |
---|
384 | def properties(self,prop): |
---|
385 | """Set some properties of the graph. |
---|
386 | |
---|
387 | The set of properties is not yet determined. |
---|
388 | """ |
---|
389 | # The particulars of how they are stored and manipulated (e.g., do |
---|
390 | # we want an inventory internally) is not settled. I've used a |
---|
391 | # property dictionary for now. |
---|
392 | # |
---|
393 | # How these properties interact with a user defined style file is |
---|
394 | # even less clear. |
---|
395 | |
---|
396 | # Properties defined by plot |
---|
397 | self.subplot.set_xlabel(r"$%s$" % prop["xlabel"]) |
---|
398 | self.subplot.set_ylabel(r"$%s$" % prop["ylabel"]) |
---|
399 | self.subplot.set_title(prop["title"]) |
---|
400 | |
---|
401 | # Properties defined by user |
---|
402 | #self.axes.grid(True) |
---|
403 | |
---|
404 | def clear(self): |
---|
405 | """Reset the plot""" |
---|
406 | |
---|
407 | # TODO: Redraw is brutal. Render to a backing store and swap in |
---|
408 | # TODO: rather than redrawing on the fly. |
---|
409 | self.subplot.clear() |
---|
410 | self.subplot.hold(True) |
---|
411 | |
---|
412 | def render(self): |
---|
413 | """Commit the plot after all objects are drawn""" |
---|
414 | # TODO: this is when the backing store should be swapped in. |
---|
415 | from matplotlib.font_manager import FontProperties |
---|
416 | self.subplot.legend(prop=FontProperties(size=10)) |
---|
417 | #self.subplot.legend() |
---|
418 | pass |
---|
419 | |
---|
420 | def xaxis(self,label,units): |
---|
421 | """xaxis label and units. |
---|
422 | |
---|
423 | Axis labels know about units. |
---|
424 | |
---|
425 | We need to do this so that we can detect when axes are not |
---|
426 | commesurate. Currently this is ignored other than for formatting |
---|
427 | purposes. |
---|
428 | """ |
---|
429 | if units != "": label = label + " (" + units + ")" |
---|
430 | self.subplot.set_xlabel(label) |
---|
431 | pass |
---|
432 | |
---|
433 | def yaxis(self,label,units): |
---|
434 | """yaxis label and units.""" |
---|
435 | if units != "": label = label + " (" + units + ")" |
---|
436 | self.subplot.set_ylabel(label) |
---|
437 | pass |
---|
438 | |
---|
439 | def _connect_to_xlim(self,callback): |
---|
440 | """Bind the xlim change notification to the callback""" |
---|
441 | def process_xlim(axes): |
---|
442 | lo,hi = subplot.get_xlim() |
---|
443 | callback(lo,hi) |
---|
444 | self.subplot.callbacks.connect('xlim_changed',process_xlim) |
---|
445 | |
---|
446 | #def connect(self,trigger,callback): |
---|
447 | # print "PlotPanel.connect???" |
---|
448 | # if trigger == 'xlim': self._connect_to_xlim(callback) |
---|
449 | |
---|
450 | def points(self,x,y,dx=None,dy=None,color=0,symbol=0,label=None): |
---|
451 | """Draw markers with error bars""" |
---|
452 | self.subplot.set_yscale('linear') |
---|
453 | self.subplot.set_xscale('linear') |
---|
454 | # Convert tuple (lo,hi) to array [(x-lo),(hi-x)] |
---|
455 | if dx != None and type(dx) == type(()): |
---|
456 | dx = nx.vstack((x-dx[0],dx[1]-x)).transpose() |
---|
457 | if dy != None and type(dy) == type(()): |
---|
458 | dy = nx.vstack((y-dy[0],dy[1]-y)).transpose() |
---|
459 | |
---|
460 | if dx==None and dy==None: |
---|
461 | h = self.subplot.plot(x,y,color=self._color(color), |
---|
462 | marker=self._symbol(symbol),linestyle='',label=label) |
---|
463 | else: |
---|
464 | self.subplot.errorbar(x, y, yerr=dy, xerr=None, |
---|
465 | ecolor=self._color(color), capsize=2,linestyle='', barsabove=False, |
---|
466 | marker=self._symbol(symbol), |
---|
467 | lolims=False, uplims=False, |
---|
468 | xlolims=False, xuplims=False,label=label) |
---|
469 | |
---|
470 | self.subplot.set_yscale(self.yscale) |
---|
471 | self.subplot.set_xscale(self.xscale) |
---|
472 | |
---|
473 | def curve(self,x,y,dy=None,color=0,symbol=0,label=None): |
---|
474 | """Draw a line on a graph, possibly with confidence intervals.""" |
---|
475 | c = self._color(color) |
---|
476 | self.subplot.set_yscale('linear') |
---|
477 | self.subplot.set_xscale('linear') |
---|
478 | |
---|
479 | hlist = self.subplot.plot(x,y,color=c,marker='',linestyle='-',label=label) |
---|
480 | |
---|
481 | self.subplot.set_yscale(self.yscale) |
---|
482 | self.subplot.set_xscale(self.xscale) |
---|
483 | |
---|
484 | def _color(self,c): |
---|
485 | """Return a particular colour""" |
---|
486 | return self.colorlist[c%len(self.colorlist)] |
---|
487 | |
---|
488 | def _symbol(self,s): |
---|
489 | """Return a particular symbol""" |
---|
490 | return self.symbollist[s%len(self.symbollist)] |
---|
491 | |
---|
492 | def _onEVT_FUNC_PROPERTY(self): |
---|
493 | """ |
---|
494 | Receive the x and y transformation from myDialog,Transforms x and y in View |
---|
495 | and set the scale |
---|
496 | """ |
---|
497 | list =[] |
---|
498 | list = self.graph.returnPlottable() |
---|
499 | self.fit_result.x =[] |
---|
500 | self.fit_result.y =[] |
---|
501 | self.fit_result.dx=None |
---|
502 | self.fit_result.dy=None |
---|
503 | |
---|
504 | for item in list: |
---|
505 | item.setLabel(self.xLabel,self.yLabel) |
---|
506 | if ( self.xLabel=="x" ): |
---|
507 | item.transformX(transform.toX,transform.errToX) |
---|
508 | self.set_xscale("linear") |
---|
509 | name, units = item.get_xaxis() |
---|
510 | self.graph.xaxis("%s" % name, "%s" % units) |
---|
511 | |
---|
512 | |
---|
513 | if ( self.xLabel=="x^(2)" ): |
---|
514 | item.transformX(transform.toX2,transform.errToX2) |
---|
515 | self.set_xscale('linear') |
---|
516 | name, units = item.get_xaxis() |
---|
517 | units=convertUnit(2,units) |
---|
518 | self.graph.xaxis("%s^{2}" % name, "%s" % units) |
---|
519 | |
---|
520 | |
---|
521 | if (self.xLabel=="log10(x)" ): |
---|
522 | item.transformX(transform.toX,transform.errToX) |
---|
523 | self.set_xscale("log") |
---|
524 | name, units = item.get_xaxis() |
---|
525 | self.graph.xaxis("\log_{10}\ \ (%s)" % name, "%s" % units) |
---|
526 | |
---|
527 | |
---|
528 | if ( self.yLabel=="ln(y)" ): |
---|
529 | item.transformY(transform.toLogX,transform.errToLogX) |
---|
530 | self.set_yscale("linear") |
---|
531 | name, units = item.get_yaxis() |
---|
532 | self.graph.yaxis("log\ \ %s" % name, "%s" % units) |
---|
533 | |
---|
534 | |
---|
535 | if ( self.yLabel=="y" ): |
---|
536 | item.transformY(transform.toX,transform.errToX) |
---|
537 | self.set_yscale("linear") |
---|
538 | name, units = item.get_yaxis() |
---|
539 | self.graph.yaxis("%s" % name, "%s" % units) |
---|
540 | |
---|
541 | |
---|
542 | if ( self.yLabel=="log10(y)" ): |
---|
543 | item.transformY(transform.toX,transform.errToX) |
---|
544 | self.set_yscale("log") |
---|
545 | name, units = item.get_yaxis() |
---|
546 | self.graph.yaxis("\log_{10}\ \ (%s)" % name, "%s" % units) |
---|
547 | |
---|
548 | |
---|
549 | if ( self.yLabel=="y^(2)" ): |
---|
550 | item.transformY( transform.toX2,transform.errToX2 ) |
---|
551 | self.set_yscale("linear") |
---|
552 | name, units = item.get_yaxis() |
---|
553 | units=convertUnit(2,units) |
---|
554 | self.graph.yaxis("%s^{2}" % name, "%s" % units) |
---|
555 | |
---|
556 | |
---|
557 | if ( self.yLabel =="1/y"): |
---|
558 | item.transformY(transform.toOneOverX,transform.errOneOverX ) |
---|
559 | self.set_yscale("linear") |
---|
560 | name, units = item.get_yaxis() |
---|
561 | units=convertUnit(-1,units) |
---|
562 | self.graph.yaxis("1/%s" % name, "%s" % units) |
---|
563 | |
---|
564 | if ( self.yLabel =="1/sqrt(y)" ): |
---|
565 | item.transformY(transform.toOneOverSqrtX,transform.errOneOverSqrtX ) |
---|
566 | self.set_yscale("linear") |
---|
567 | name, units = item.get_yaxis() |
---|
568 | units=convertUnit(-1,units) |
---|
569 | self.graph.yaxis("1/\sqrt{%s}" %name, "%s" % units) |
---|
570 | |
---|
571 | if ( self.yLabel =="ln(y*x)"): |
---|
572 | item.transformY( transform.toLogXY,transform.errToLogXY) |
---|
573 | self.set_yscale("linear") |
---|
574 | yname, yunits = item.get_yaxis() |
---|
575 | xname, xunits = item.get_xaxis() |
---|
576 | self.graph.yaxis("log\ (%s \ \ %s)" % (yname,xname), "%s%s" % (yunits,xunits)) |
---|
577 | |
---|
578 | |
---|
579 | if ( self.yLabel =="ln(y*x^(2))"): |
---|
580 | item.transformY( transform.toLogYX2,transform.errToLogYX2) |
---|
581 | self.set_yscale("linear") |
---|
582 | yname, yunits = item.get_yaxis() |
---|
583 | xname, xunits = item.get_xaxis() |
---|
584 | xunits = convertUnit(2,xunits) |
---|
585 | self.graph.yaxis("Log (%s \ \ %s)" % (yname,xname), "%s%s" % (yunits,xunits)) |
---|
586 | |
---|
587 | |
---|
588 | if ( self.yLabel =="ln(y*x^(4))"): |
---|
589 | item.transformY(transform.toLogYX4,transform.errToLogYX4) |
---|
590 | self.set_yscale("linear") |
---|
591 | yname, yunits = item.get_yaxis() |
---|
592 | xname, xunits = item.get_xaxis() |
---|
593 | xunits = convertUnit(4,xunits) |
---|
594 | self.graph.yaxis("Log (%s \ \ %s)" % (yname,xname), "%s%s" % (yunits,xunits)) |
---|
595 | |
---|
596 | if ( self.viewModel == "Guinier lny vs x^(2)"): |
---|
597 | |
---|
598 | item.transformX(transform.toX2,transform.errToX2) |
---|
599 | self.set_xscale('linear') |
---|
600 | name, units = item.get_xaxis() |
---|
601 | units = convertUnit(2,units) |
---|
602 | self.graph.xaxis("%s^{2}" % name, "%s" % units) |
---|
603 | |
---|
604 | |
---|
605 | item.transformY(transform.toLogX,transform.errToLogX ) |
---|
606 | self.set_yscale("linear") |
---|
607 | name, units = item.get_yaxis() |
---|
608 | self.graph.yaxis("$Log %s$" % name, "%s" % units) |
---|
609 | |
---|
610 | |
---|
611 | item.transformView() |
---|
612 | |
---|
613 | |
---|
614 | self.resetFitView() |
---|
615 | self.prevXtrans = self.xLabel |
---|
616 | self.prevYtrans = self.yLabel |
---|
617 | self.graph.render(self) |
---|
618 | self.subplot.figure.canvas.draw_idle() |
---|
619 | |
---|
620 | |
---|
621 | |
---|
622 | def onFitDisplay(self, tempx,tempy,xminView,xmaxView,xmin,xmax,func): |
---|
623 | """ |
---|
624 | Add a new plottable into the graph .In this case this plottable will be used |
---|
625 | to fit some data |
---|
626 | @param tempx: The x data of fit line |
---|
627 | @param tempy: The y data of fit line |
---|
628 | @param xminView: the lower bound of fitting range |
---|
629 | @param xminView: the upper bound of fitting range |
---|
630 | @param xmin: the lowest value of data to fit to the line |
---|
631 | @param xmax: the highest value of data to fit to the line |
---|
632 | """ |
---|
633 | # Saving value to redisplay in Fit Dialog when it is opened again |
---|
634 | self.Avalue,self.Bvalue,self.ErrAvalue,self.ErrBvalue,self.Chivalue=func |
---|
635 | self.xminView=xminView |
---|
636 | self.xmaxView=xmaxView |
---|
637 | self.xmin= xmin |
---|
638 | self.xmax= xmax |
---|
639 | #In case need to change the range of data plotted |
---|
640 | list =[] |
---|
641 | list = self.graph.returnPlottable() |
---|
642 | for item in list: |
---|
643 | #item.onFitRange(xminView,xmaxView) |
---|
644 | item.onFitRange(None,None) |
---|
645 | |
---|
646 | # Create new data plottable with result |
---|
647 | self.fit_result.x =[] |
---|
648 | self.fit_result.y =[] |
---|
649 | self.fit_result.x =tempx |
---|
650 | self.fit_result.y =tempy |
---|
651 | self.fit_result.dx=None |
---|
652 | self.fit_result.dy=None |
---|
653 | #Load the view with the new values |
---|
654 | self.fit_result.reset_view() |
---|
655 | # Add the new plottable to the graph |
---|
656 | self.graph.add(self.fit_result) |
---|
657 | self.graph.render(self) |
---|
658 | self.subplot.figure.canvas.draw_idle() |
---|
659 | |
---|
660 | |
---|
661 | def onResetGraph(self,event): |
---|
662 | """ |
---|
663 | Reset the graph by plotting the full range of data |
---|
664 | """ |
---|
665 | list =[] |
---|
666 | list = self.graph.returnPlottable() |
---|
667 | for item in list: |
---|
668 | item.onReset() |
---|
669 | self.graph.render(self) |
---|
670 | self.subplot.figure.canvas.draw_idle() |
---|
671 | |
---|
672 | class NoRepaintCanvas(FigureCanvasWxAgg): |
---|
673 | """We subclass FigureCanvasWxAgg, overriding the _onPaint method, so that |
---|
674 | the draw method is only called for the first two paint events. After that, |
---|
675 | the canvas will only be redrawn when it is resized. |
---|
676 | """ |
---|
677 | def __init__(self, *args, **kwargs): |
---|
678 | FigureCanvasWxAgg.__init__(self, *args, **kwargs) |
---|
679 | self._drawn = 0 |
---|
680 | |
---|
681 | def _onPaint(self, evt): |
---|
682 | """ |
---|
683 | Called when wxPaintEvt is generated |
---|
684 | """ |
---|
685 | if not self._isRealized: |
---|
686 | self.realize() |
---|
687 | if self._drawn < 2: |
---|
688 | self.draw(repaint = False) |
---|
689 | self._drawn += 1 |
---|
690 | self.gui_repaint(drawDC=wx.PaintDC(self)) |
---|
691 | |
---|