source: sasview/guiframe/local_perspectives/plotting/boxSlicer.py @ 212417e

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 212417e was 0f6d05f8, checked in by Gervaise Alina <gervyh@…>, 15 years ago

boxaveraging still bugging on set_params

  • Property mode set to 100644
File size: 15.4 KB
Line 
1#TODO: the line slicer should listen to all 2DREFRESH events, get the data and slice it
2#      before pushing a new 1D data update.
3
4#
5#TODO: NEED MAJOR REFACTOR
6#
7
8
9# Debug printout
10#from config import printEVT
11from BaseInteractor import _BaseInteractor
12from copy import deepcopy
13import math
14
15#from Plotter1D import AddPlotEvent
16import SlicerParameters
17import wx
18
19
20class BoxInteractor(_BaseInteractor):
21    """
22         Select an annulus through a 2D plot
23    """
24    def __init__(self,base,axes,color='black', zorder=3,
25                  x_min=0.0025, x_max=0.0025, y_min=0.0025, y_max=0.0025):
26       
27        _BaseInteractor.__init__(self, base, axes, color=color)
28        self.markers = []
29        self.axes = axes
30        self.qmax = self.base.qmax
31        self.connect = self.base.connect
32        self.x= x_max
33        self.y= y_max
34               
35        self.theta2= math.pi/3
36        ## Number of points on the plot
37        self.nbins = 20
38        self.count=0
39        self.error=0
40       
41        self.left_line = VerticalLine(self, self.base.subplot,color='blue', 
42                                      zorder=zorder,
43                                        ymin= -self.y , 
44                                        ymax= self.y ,
45                                        xmin= -self.x,
46                                        xmax= -self.x)
47        self.left_line.qmax = self.base.qmax
48       
49        self.right_line= VerticalLine(self, self.base.subplot,color='black', 
50                                      zorder=zorder,
51                                     ymin= -self.y , 
52                                     ymax= self.y,
53                                     xmin= self.x,
54                                     xmax= self.x)
55        self.right_line.qmax = self.base.qmax
56       
57        self.top_line= HorizontalLine(self, self.base.subplot,color='green', 
58                                      zorder=zorder,
59                                      xmin= -self.x,
60                                      xmax= self.x,
61                                      ymin= self.y,
62                                      ymax= self.y)
63        self.top_line.qmax= self.base.qmax
64       
65        self.bottom_line= HorizontalLine(self, self.base.subplot,color='gray', 
66                                      zorder=zorder,
67                                      xmin= -self.x,
68                                      xmax= self.x,
69                                      ymin= -self.y,
70                                      ymax= -self.y)
71        self.bottom_line.qmax= self.base.qmax
72       
73        self.update()
74        self._post_data()
75       
76        # Bind to slice parameter events
77        self.base.parent.Bind(SlicerParameters.EVT_SLICER_PARS, self._onEVT_SLICER_PARS)
78
79
80    def _onEVT_SLICER_PARS(self, event):
81        #printEVT("AnnulusSlicer._onEVT_SLICER_PARS")
82        event.Skip()
83        if event.type == self.__class__.__name__:
84            self.set_params(event.params)
85            self.base.update()
86
87    def update_and_post(self):
88        self.update()
89        self._post_data()
90
91    def save_data(self, path, image, x, y):
92        output = open(path, 'w')
93       
94        data_x, data_y = self.get_data(image, x, y)
95       
96        output.write("<phi>  <average>\n")
97        for i in range(len(data_x)):
98            output.write("%g  %g\n" % (data_x[i], data_y[i]))
99        output.close()
100
101    def set_layer(self, n):
102        self.layernum = n
103        self.update()
104       
105    def clear(self):
106        self.clear_markers()
107        self.left_line.clear()
108        self.right_line.clear()
109        self.top_line.clear()
110        self.bottom_line.clear()
111        #self.base.connect.disconnect()
112        self.base.parent.Unbind(SlicerParameters.EVT_SLICER_PARS)
113       
114    def update(self):
115        """
116        Respond to changes in the model by recalculating the profiles and
117        resetting the widgets.
118        """
119       
120        if self.top_line.has_move:
121            print"top has moved"
122            self.top_line.update()
123            self.bottom_line.update(ymin= -self.top_line.y1,
124                                    ymax= -self.top_line.y2)
125            self.left_line.update(ymin= -self.top_line.y1,
126                                    ymax= -self.top_line.y2)
127            self.right_line.update(ymin= -self.top_line.y1,
128                                    ymax= -self.top_line.y2)
129           
130        if self.bottom_line.has_move:
131            print "bottom has move"
132            self.bottom_line.update()
133            self.top_line.update(ymin= -self.bottom_line.y1,
134                                    ymax= -self.bottom_line.y2)
135            self.left_line.update(ymin= self.bottom_line.y1,
136                                    ymax= self.top_line.y1)
137            self.right_line.update(ymin= self.bottom_line.y1,
138                                    ymax=self.top_line.y1)
139           
140        if self.left_line.has_move:
141           
142            self.left_line.update()
143            self.right_line.update(xmin = - self.left_line.x1,
144                                   xmax = - self.left_line.x1)
145            self.bottom_line.update(xmin=  self.left_line.x1,
146                                     xmax= self.right_line.x1)
147            self.top_line.update(xmin= self.left_line.x1,
148                                    xmax= self.right_line.x1)
149           
150        if self.right_line.has_move:
151           
152            self.right_line.update()
153            self.left_line.update(xmin = -self.right_line.x1,
154                                   xmax = -self.right_line.x1)
155           
156            self.bottom_line.update(xmin= self.left_line.x1,
157                                 xmax= self.right_line.x1)
158           
159            self.top_line.update(xmin= self.left_line.x1,
160                                    xmax= self.right_line.x1)
161               
162           
163    def save(self, ev):
164        """
165        Remember the roughness for this layer and the next so that we
166        can restore on Esc.
167        """
168        self.base.freeze_axes()
169        self.left_line.save(ev)
170        self.right_line.save(ev)
171        self.top_line.save(ev)
172        self.bottom_line.save(ev)
173
174    def _post_data(self):
175        # Compute data
176        data = self.base.data2D
177        from DataLoader.manipulations import  Boxavg
178        radius = math.sqrt(math.pow(self.qmax,2)+math.pow(self.qmax,2))
179        x_min= self.left_line.x1
180        x_max= self.right_line.x1
181        y_min= self.bottom_line.y1
182        y_max= self.top_line.y1
183        box =  Boxavg (x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max)
184       
185        self.count, self.error= box(self.base.data2D)
186       
187        #print "post data"
188             
189                                       
190    def moveend(self, ev):
191        self.base.thaw_axes()
192       
193        # Post paramters
194        event = SlicerParameters.SlicerParameterEvent()
195        event.type = self.__class__.__name__
196        event.params = self.get_params()
197        wx.PostEvent(self.base.parent, event)
198
199        self._post_data()
200           
201    def restore(self):
202        """
203        Restore the roughness for this layer.
204        """
205        self.left_line.restore()
206        self.right_line.restore()
207        self.top_line.restore()
208        self.bottom_line.restore()
209
210    def move(self, x, y, ev):
211        """
212        Process move to a new position, making sure that the move is allowed.
213        """
214        pass
215       
216    def set_cursor(self, x, y):
217        pass
218       
219    def get_params(self):
220        params = {}
221        params["x_max"]= math.fabs(self.right_line.x1)
222        params["y_max"]= math.fabs(self.top_line.x1)
223       
224        params["errors"] = self.error
225        params["count"]= self.count
226        return params
227   
228    def set_params(self, params):
229        self.x = float(math.fabs(params["x_max"]))
230        self.y = float(math.fabs(params["y_max"] ))
231       
232        self.left_line.update(xmin= -1*self.x,
233                              xmax = -1*self.x,
234                              ymin= -self.y,
235                              ymax=  self.y, 
236                              )
237        self.right_line.update(xmin= self.x,
238                              xmax = self.x,
239                              ymin= -self.y,
240                              ymax=  self.y, 
241                              )
242        self.top_line.update(xmin= -1*self.x,
243                             xmax= self.x,
244                             ymin= self.y,
245                             ymax= self.y)
246        self.bottom_line.update(xmin= -1*self.x,
247                                 xmax= self.x,
248                                 ymin= -1*self.y,
249                                 ymax= -1*self.y)
250        self._post_data()
251    def freeze_axes(self):
252        self.base.freeze_axes()
253       
254    def thaw_axes(self):
255        self.base.thaw_axes()
256
257    def draw(self):
258        self.base.draw()
259
260class HorizontalLine(_BaseInteractor):
261    """
262         Select an annulus through a 2D plot
263    """
264    def __init__(self,base,axes,color='black', zorder=5,mline=None,ymin=None, ymax=None, y=0.5,
265                 xmin=0.0,xmax=0.5):
266       
267        _BaseInteractor.__init__(self, base, axes, color=color)
268        self.markers = []
269        self.axes = axes
270        self.x1= xmax
271        self.save_x1= xmax
272       
273        self.x2= xmin
274        self.save_x2= xmin
275       
276        self.y1= ymax
277        self.save_y1= ymax
278       
279        self.y2= ymin
280        self.save_y2= ymin
281        self.mline= mline
282        self.line = self.axes.plot([self.x1,-self.x1],
283                                   [self.y1,self.y2],
284                                      linestyle='-', marker='',
285                                      color=self.color,
286                                      visible=True)[0]
287       
288        self.npts = 20
289        self.has_move=False
290        self.connect_markers([self.line])
291        self.update()
292
293    def set_layer(self, n):
294        self.layernum = n
295        self.update()
296       
297    def clear(self):
298        self.clear_markers()
299        try:
300           
301            self.line.remove()
302        except:
303            # Old version of matplotlib
304            for item in range(len(self.axes.lines)):
305                del self.axes.lines[0]
306   
307    def get_radius(self):
308       
309        return 0
310   
311    def update(self,xmin=None, xmax=None,ymin=None,ymax=None, mline=None,translation=False):
312        """
313        Draw the new roughness on the graph.
314        """
315        if xmin !=None:
316            self.x1 = xmin
317        if ymin !=None:
318            self.y1 = ymin
319        self.line.set(xdata=[self.x1,-self.x1],
320                       ydata=[self.y1,self.y1])
321   
322       
323       
324    def save(self, ev):
325        """
326        Remember the roughness for this layer and the next so that we
327        can restore on Esc.
328        """
329        self.save_x1= self.x1
330        self.save_x2= self.x2
331       
332        self.save_y1= self.y1
333        self.save_y2= self.y2
334   
335       
336        self.base.freeze_axes()
337
338    def moveend(self, ev):
339       
340        self.has_move=False
341        self.base.moveend(ev)
342           
343    def restore(self):
344        """
345        Restore the roughness for this layer.
346        """
347        self.x1 = self.save_x1
348        self.x2 = self.save_x2
349        self.y1 = self.save_y1
350        self.y2 = self.save_y2
351       
352
353    def move(self, x, y, ev):
354        """
355        Process move to a new position, making sure that the move is allowed.
356        """
357        print "horizontal move x y "
358        self.y1= y
359        self.has_move=True
360        self.base.base.update()
361       
362    def set_cursor(self, x, y):
363        self.move(x, y, None)
364        self.update()
365       
366       
367    def get_params(self):
368        params = {}
369        params["radius"] = self.x1
370        #params["theta"] = self.xmax
371        return params
372   
373    def set_params(self, params):
374
375        x = params["radius"] 
376        self.set_cursor(x, self._inner_mouse_y)
377       
378
379
380
381class VerticalLine(_BaseInteractor):
382    """
383         Select an annulus through a 2D plot
384    """
385    def __init__(self,base,axes,color='black', zorder=5, mline=None, ymin=0.0, 
386                 ymax=0.5,xmin=-0.5,xmax=0.5
387                 ):
388       
389        _BaseInteractor.__init__(self, base, axes, color=color)
390        self.markers = []
391        self.axes = axes
392       
393        self.x1= xmax
394        self.x2= xmin
395        self.y1= ymax
396        self.y2= ymin
397        self.line = self.axes.plot([self.x1,self.x2],[self.y1,self.y2],
398                                      linestyle='-', marker='',
399                                      color=self.color,
400                                      visible=True)[0]
401     
402        self.has_move=False
403        self.connect_markers([self.line])
404        self.update()
405
406    def set_layer(self, n):
407        self.layernum = n
408        self.update()
409       
410    def clear(self):
411        self.clear_markers()
412        try:
413           
414            self.line.remove()
415        except:
416            # Old version of matplotlib
417            for item in range(len(self.axes.lines)):
418                del self.axes.lines[0]
419   
420   
421    def get_radius(self):
422        return 0
423   
424    def update(self,xmin=None,xmax=None,ymin=None, ymax=None, opline=None,translation=False):
425        """
426        Draw the new roughness on the graph.
427        """
428
429   
430        if xmin!=None:
431            self.x1=xmin
432        if ymin!=None:
433            self.y1=ymin
434        self.line.set(xdata=[self.x1,self.x1],
435                       ydata=[self.y1,-self.y1]) 
436       
437   
438       
439    def save(self, ev):
440        """
441        Remember the roughness for this layer and the next so that we
442        can restore on Esc.
443        """
444        self.save_x1= self.x1
445        self.save_x2= self.x2
446        self.save_y1= self.y1
447        self.save_y2= self.y2
448       
449        self.base.freeze_axes()
450
451    def moveend(self, ev):
452       
453        self.has_move=False
454        self.base.moveend(ev)
455           
456    def restore(self):
457        """
458        Restore the roughness for this layer.
459        """
460        self.x1 = self.save_x1
461        self.x2 = self.save_x2
462        self.y1 = self.save_y1
463        self.y2= self.save_y2
464     
465       
466    def move(self, x, y, ev):
467        """
468        Process move to a new position, making sure that the move is allowed.
469        """
470        self.has_move=True
471       
472        # compute the b intercept of the vertical line
473        self.x1= x
474       
475       
476        self.base.base.update()
477       
478       
479    def set_cursor(self, x, y):
480        self.move(x, y, None)
481        self.update()
482       
483       
484    def get_params(self):
485        params = {}
486        params["x"] = self.xmin
487        params["ymin"] = self.ymin
488        params["ymax"] = self.ymax
489        return params
490   
491    def set_params(self, params):
492        """
493            Draw a vertical line given some value of params
494            @param params: a dictionary containing value for x, ymin , ymax to draw
495            a vertical line
496        """
497        x = params["x"] 
498        ymin = params["ymin"] 
499        ymax = params["ymax"] 
500        #self.set_cursor(x, self._inner_mouse_y)
501        self.update(self,x =x,ymin =ymin, ymax =ymax)
502       
503
504       
Note: See TracBrowser for help on using the repository browser.