source: sasview/guiframe/local_perspectives/plotting/boxSlicer.py @ aa1b747

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 aa1b747 was aa1b747, checked in by Gervaise Alina <gervyh@…>, 16 years ago

worling on slicer panel

  • Property mode set to 100644
File size: 15.4 KB
RevLine 
[2d107b8]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
[38224f10]10#from config import printEVT
[2d107b8]11from BaseInteractor import _BaseInteractor
12from copy import deepcopy
13import math
14
[38224f10]15#from Plotter1D import AddPlotEvent
[2d107b8]16import SlicerParameters
17import wx
18
[0f6d05f8]19
[38224f10]20class BoxInteractor(_BaseInteractor):
[2d107b8]21    """
22         Select an annulus through a 2D plot
23    """
[c73d871]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):
[2d107b8]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
[0f6d05f8]32        self.x= x_max
33        self.y= y_max
34               
[3554b99a]35        self.theta2= math.pi/3
[2d107b8]36        ## Number of points on the plot
37        self.nbins = 20
[38224f10]38        self.count=0
39        self.error=0
[0f6d05f8]40       
[c73d871]41        self.left_line = VerticalLine(self, self.base.subplot,color='blue', 
42                                      zorder=zorder,
[0f6d05f8]43                                        ymin= -self.y , 
44                                        ymax= self.y ,
45                                        xmin= -self.x,
46                                        xmax= -self.x)
[2d107b8]47        self.left_line.qmax = self.base.qmax
[dd40217]48       
49        self.right_line= VerticalLine(self, self.base.subplot,color='black', 
50                                      zorder=zorder,
[0f6d05f8]51                                     ymin= -self.y , 
52                                     ymax= self.y,
53                                     xmin= self.x,
54                                     xmax= self.x)
[dd40217]55        self.right_line.qmax = self.base.qmax
56       
57        self.top_line= HorizontalLine(self, self.base.subplot,color='green', 
58                                      zorder=zorder,
[0f6d05f8]59                                      xmin= -self.x,
60                                      xmax= self.x,
61                                      ymin= self.y,
62                                      ymax= self.y)
[dd40217]63        self.top_line.qmax= self.base.qmax
64       
65        self.bottom_line= HorizontalLine(self, self.base.subplot,color='gray', 
66                                      zorder=zorder,
[0f6d05f8]67                                      xmin= -self.x,
68                                      xmax= self.x,
69                                      ymin= -self.y,
70                                      ymax= -self.y)
[dd40217]71        self.bottom_line.qmax= self.base.qmax
72       
[38224f10]73        self.update()
[0f6d05f8]74        self._post_data()
[2d107b8]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):
[38224f10]81        #printEVT("AnnulusSlicer._onEVT_SLICER_PARS")
[2d107b8]82        event.Skip()
83        if event.type == self.__class__.__name__:
[0f6d05f8]84            self.set_params(event.params)
[2d107b8]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()
[0f6d05f8]107        self.left_line.clear()
108        self.right_line.clear()
109        self.top_line.clear()
110        self.bottom_line.clear()
[2d107b8]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        """
[8ff3ec1]119       
[dd40217]120        if self.top_line.has_move:
[0f6d05f8]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           
[dd40217]130        if self.bottom_line.has_move:
[0f6d05f8]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           
[dd40217]140        if self.left_line.has_move:
[0f6d05f8]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           
[dd40217]150        if self.right_line.has_move:
[0f6d05f8]151           
152            self.right_line.update()
153            self.left_line.update(xmin = -self.right_line.x1,
154                                   xmax = -self.right_line.x1)
[dd40217]155           
[0f6d05f8]156            self.bottom_line.update(xmin= self.left_line.x1,
157                                 xmax= self.right_line.x1)
[dd40217]158           
[0f6d05f8]159            self.top_line.update(xmin= self.left_line.x1,
160                                    xmax= self.right_line.x1)
[dd40217]161               
[0f6d05f8]162           
[2d107b8]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()
[0f6d05f8]169        self.left_line.save(ev)
170        self.right_line.save(ev)
171        self.top_line.save(ev)
172        self.bottom_line.save(ev)
[2d107b8]173
174    def _post_data(self):
175        # Compute data
[0f6d05f8]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))
[aa1b747]179        self.x= math.fabs(self.right_line.x1)
180        self.y= math.fabs(self.top_line.y1 )
181       
182        box =  Boxavg (x_min=-self.x, x_max=self.x, y_min=-self.y, y_max=self.y)
[38224f10]183       
[0f6d05f8]184        self.count, self.error= box(self.base.data2D)
[b319def8]185       
[0f6d05f8]186        #print "post data"
[b319def8]187             
[38224f10]188                                       
[2d107b8]189    def moveend(self, ev):
190        self.base.thaw_axes()
191       
192        # Post paramters
193        event = SlicerParameters.SlicerParameterEvent()
194        event.type = self.__class__.__name__
[0f6d05f8]195        event.params = self.get_params()
[2d107b8]196        wx.PostEvent(self.base.parent, event)
197
198        self._post_data()
199           
200    def restore(self):
201        """
202        Restore the roughness for this layer.
203        """
[0f6d05f8]204        self.left_line.restore()
205        self.right_line.restore()
206        self.top_line.restore()
207        self.bottom_line.restore()
[2d107b8]208
209    def move(self, x, y, ev):
210        """
211        Process move to a new position, making sure that the move is allowed.
212        """
213        pass
214       
215    def set_cursor(self, x, y):
216        pass
217       
218    def get_params(self):
219        params = {}
[0f6d05f8]220        params["x_max"]= math.fabs(self.right_line.x1)
[aa1b747]221        params["y_max"]= math.fabs(self.top_line.y1)
[0f6d05f8]222       
223        params["errors"] = self.error
224        params["count"]= self.count
[2d107b8]225        return params
226   
227    def set_params(self, params):
[aa1b747]228       
[0f6d05f8]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)
[aa1b747]250       
[38224f10]251        self._post_data()
[2d107b8]252    def freeze_axes(self):
253        self.base.freeze_axes()
254       
255    def thaw_axes(self):
256        self.base.thaw_axes()
257
258    def draw(self):
259        self.base.draw()
260
261class HorizontalLine(_BaseInteractor):
262    """
263         Select an annulus through a 2D plot
264    """
[030873e]265    def __init__(self,base,axes,color='black', zorder=5,mline=None,ymin=None, ymax=None, y=0.5,
[0f6d05f8]266                 xmin=0.0,xmax=0.5):
[2d107b8]267       
268        _BaseInteractor.__init__(self, base, axes, color=color)
269        self.markers = []
270        self.axes = axes
[dd40217]271        self.x1= xmax
272        self.save_x1= xmax
[2d107b8]273       
[dd40217]274        self.x2= xmin
275        self.save_x2= xmin
[3554b99a]276       
[dd40217]277        self.y1= ymax
278        self.save_y1= ymax
[3554b99a]279       
[dd40217]280        self.y2= ymin
281        self.save_y2= ymin
[3554b99a]282        self.mline= mline
[0f6d05f8]283        self.line = self.axes.plot([self.x1,-self.x1],
[3554b99a]284                                   [self.y1,self.y2],
[2d107b8]285                                      linestyle='-', marker='',
286                                      color=self.color,
287                                      visible=True)[0]
[c73d871]288       
[2d107b8]289        self.npts = 20
290        self.has_move=False
291        self.connect_markers([self.line])
[3554b99a]292        self.update()
[2d107b8]293
294    def set_layer(self, n):
295        self.layernum = n
296        self.update()
297       
298    def clear(self):
299        self.clear_markers()
300        try:
301           
302            self.line.remove()
303        except:
304            # Old version of matplotlib
305            for item in range(len(self.axes.lines)):
306                del self.axes.lines[0]
[030873e]307   
[2d107b8]308    def get_radius(self):
309       
310        return 0
[8ff3ec1]311   
[b6b1669]312    def update(self,xmin=None, xmax=None,ymin=None,ymax=None, mline=None,translation=False):
[2d107b8]313        """
314        Draw the new roughness on the graph.
315        """
[3554b99a]316        if xmin !=None:
[0f6d05f8]317            self.x1 = xmin
[3554b99a]318        if ymin !=None:
[0f6d05f8]319            self.y1 = ymin
320        self.line.set(xdata=[self.x1,-self.x1],
321                       ydata=[self.y1,self.y1])
[030873e]322   
[2d107b8]323       
324       
325    def save(self, ev):
326        """
327        Remember the roughness for this layer and the next so that we
328        can restore on Esc.
329        """
[3554b99a]330        self.save_x1= self.x1
331        self.save_x2= self.x2
[2d107b8]332       
[3554b99a]333        self.save_y1= self.y1
334        self.save_y2= self.y2
[0f6d05f8]335   
[3554b99a]336       
[2d107b8]337        self.base.freeze_axes()
338
339    def moveend(self, ev):
[030873e]340       
[2d107b8]341        self.has_move=False
342        self.base.moveend(ev)
343           
344    def restore(self):
345        """
346        Restore the roughness for this layer.
347        """
[3554b99a]348        self.x1 = self.save_x1
349        self.x2 = self.save_x2
350        self.y1 = self.save_y1
351        self.y2 = self.save_y2
[0f6d05f8]352       
[2d107b8]353
354    def move(self, x, y, ev):
355        """
356        Process move to a new position, making sure that the move is allowed.
357        """
[0f6d05f8]358        print "horizontal move x y "
359        self.y1= y
[2d107b8]360        self.has_move=True
361        self.base.base.update()
362       
363    def set_cursor(self, x, y):
364        self.move(x, y, None)
365        self.update()
366       
367       
368    def get_params(self):
369        params = {}
[3554b99a]370        params["radius"] = self.x1
371        #params["theta"] = self.xmax
[2d107b8]372        return params
373   
374    def set_params(self, params):
375
376        x = params["radius"] 
377        self.set_cursor(x, self._inner_mouse_y)
378       
379
380
381
382class VerticalLine(_BaseInteractor):
383    """
384         Select an annulus through a 2D plot
385    """
[c73d871]386    def __init__(self,base,axes,color='black', zorder=5, mline=None, ymin=0.0, 
[0f6d05f8]387                 ymax=0.5,xmin=-0.5,xmax=0.5
388                 ):
[2d107b8]389       
390        _BaseInteractor.__init__(self, base, axes, color=color)
391        self.markers = []
392        self.axes = axes
[030873e]393       
[0f6d05f8]394        self.x1= xmax
395        self.x2= xmin
396        self.y1= ymax
397        self.y2= ymin
[c73d871]398        self.line = self.axes.plot([self.x1,self.x2],[self.y1,self.y2],
[2d107b8]399                                      linestyle='-', marker='',
400                                      color=self.color,
401                                      visible=True)[0]
402     
403        self.has_move=False
404        self.connect_markers([self.line])
405        self.update()
406
407    def set_layer(self, n):
408        self.layernum = n
409        self.update()
410       
411    def clear(self):
412        self.clear_markers()
413        try:
414           
415            self.line.remove()
416        except:
417            # Old version of matplotlib
418            for item in range(len(self.axes.lines)):
419                del self.axes.lines[0]
[c73d871]420   
421   
[2d107b8]422    def get_radius(self):
423        return 0
[8ff3ec1]424   
[d5a792a]425    def update(self,xmin=None,xmax=None,ymin=None, ymax=None, opline=None,translation=False):
[2d107b8]426        """
427        Draw the new roughness on the graph.
428        """
[0f6d05f8]429
430   
431        if xmin!=None:
432            self.x1=xmin
433        if ymin!=None:
434            self.y1=ymin
435        self.line.set(xdata=[self.x1,self.x1],
436                       ydata=[self.y1,-self.y1]) 
[dd40217]437       
[0f6d05f8]438   
[3554b99a]439       
[2d107b8]440    def save(self, ev):
441        """
442        Remember the roughness for this layer and the next so that we
443        can restore on Esc.
444        """
[dd40217]445        self.save_x1= self.x1
446        self.save_x2= self.x2
447        self.save_y1= self.y1
448        self.save_y2= self.y2
[8ff3ec1]449       
[2d107b8]450        self.base.freeze_axes()
451
452    def moveend(self, ev):
[030873e]453       
[2d107b8]454        self.has_move=False
455        self.base.moveend(ev)
456           
457    def restore(self):
458        """
459        Restore the roughness for this layer.
460        """
[3554b99a]461        self.x1 = self.save_x1
462        self.x2 = self.save_x2
463        self.y1 = self.save_y1
464        self.y2= self.save_y2
465     
[78ed1ad]466       
[2d107b8]467    def move(self, x, y, ev):
468        """
469        Process move to a new position, making sure that the move is allowed.
470        """
[8ff3ec1]471        self.has_move=True
[3554b99a]472       
473        # compute the b intercept of the vertical line
[0f6d05f8]474        self.x1= x
[3554b99a]475       
476       
[8ff3ec1]477        self.base.base.update()
478       
479       
480    def set_cursor(self, x, y):
481        self.move(x, y, None)
482        self.update()
483       
484       
485    def get_params(self):
486        params = {}
487        params["x"] = self.xmin
488        params["ymin"] = self.ymin
489        params["ymax"] = self.ymax
490        return params
491   
492    def set_params(self, params):
493        """
494            Draw a vertical line given some value of params
495            @param params: a dictionary containing value for x, ymin , ymax to draw
496            a vertical line
497        """
498        x = params["x"] 
499        ymin = params["ymin"] 
500        ymax = params["ymax"] 
501        #self.set_cursor(x, self._inner_mouse_y)
502        self.update(self,x =x,ymin =ymin, ymax =ymax)
[2d107b8]503       
[8ff3ec1]504
[2d107b8]505       
Note: See TracBrowser for help on using the repository browser.