source: sasview/guiframe/local_perspectives/plotting/boxSum.py @ 0bd125d

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

closing onf slicerpnale fixed

  • Property mode set to 100644
File size: 25.2 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#(SlicerParamUpdateEvent, EVT_SLICER_PARS_UPDATE)   = wx.lib.newevent.NewEvent()
19from sans.guicomm.events import SlicerParamUpdateEvent
20class BoxSum(_BaseInteractor):
21    """
22         Select an annulus through a 2D plot
23    """
24    def __init__(self,base,axes,color='black', zorder=3, x_min=0.008, x_max=0.008, y_min=0.0025, y_max=0.0025):
25       
26        _BaseInteractor.__init__(self, base, axes, color=color)
27        self.markers = []
28        self.axes = axes
29        self.qmax = min(self.base.data2D.xmax, self.base.data2D.xmin)
30        self.connect = self.base.connect
31        self.xmin= -1* 0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin))
32        self.ymin= -1* 0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin))
33        #self.xmax= x_max
34        #self.ymax=  y_max
35        self.xmax= 0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin))
36        self.ymax=  0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin))
37        # center of the figure
38        self.center_x= 0.0002
39        self.center_y= 0.0003
40       
41        ## Number of points on the plot
42        self.nbins = 20
43        self.count=0
44        self.error=0
45        self.has_move= False
46   
47        self.horizontal_lines= Horizontal_DoubleLine(self, self.base.subplot,color='blue',
48                                                      zorder=zorder,
49                                    y= self.ymax,
50                                    x= self.xmax,
51                                    center_x= self.center_x,
52                                    center_y= self.center_y)
53        self.horizontal_lines.qmax = self.qmax
54       
55        self.vertical_lines= Vertical_DoubleLine(self, self.base.subplot,color='black',
56                                                      zorder=zorder,
57                                    y= self.ymax,
58                                    x= self.xmax,
59                                    center_x= self.center_x,
60                                    center_y= self.center_y)
61        self.vertical_lines.qmax = self.qmax
62        self.center= PointInteractor(self, self.base.subplot,color='grey',
63                                                      zorder=zorder,
64                                    center_x= self.center_x,
65                                    center_y= self.center_y)
66     
67           
68        #self.connect_markers([])
69        self.panel_name=""     
70        self.update()
71        self._post_data()
72       
73        # Bind to slice parameter events
74        #print "box sum  self.base.parent",self.base.parent
75        self.base.parent.Bind(SlicerParameters.EVT_SLICER_PARS, self._onEVT_SLICER_PARS)
76    def set_panel_name(self, name):
77        self.panel_name= name
78    def _onEVT_SLICER_PARS(self, event):
79        #printEVT("AnnulusSlicer._onEVT_SLICER_PARS")
80        event.Skip()
81        if event.type == self.__class__.__name__:
82            self.set_params(event.params)
83            self.base.update()
84
85    def update_and_post(self):
86        self.update()
87        self._post_data()
88
89    def save_data(self, path, image, x, y):
90        output = open(path, 'w')
91       
92        data_x, data_y = self.get_data(image, x, y)
93       
94        output.write("<phi>  <average>\n")
95        for i in range(len(data_x)):
96            output.write("%g  %g\n" % (data_x[i], data_y[i]))
97        output.close()
98
99    def set_layer(self, n):
100        self.layernum = n
101        self.update()
102       
103    def clear(self):
104        self.clear_markers()
105        self.horizontal_lines.clear()
106        self.vertical_lines.clear()
107        self.center.clear()
108       
109        #self.base.connect.disconnect()
110        self.base.parent.Unbind(SlicerParameters.EVT_SLICER_PARS)
111       
112    def update(self):
113        """
114        Respond to changes in the model by recalculating the profiles and
115        resetting the widgets.
116        """
117        if self.center.has_move:
118            #print "center has move"
119            self.center.update()
120            self.horizontal_lines.update( center= self.center)
121            self.vertical_lines.update( center= self.center)
122           
123        if self.horizontal_lines.has_move:
124            #print "top has moved"
125            self.horizontal_lines.update()
126            self.vertical_lines.update(y1=self.horizontal_lines.y1,
127                                       y2=self.horizontal_lines.y2,
128                                       height= self.horizontal_lines.half_height )
129        if self.vertical_lines.has_move:
130            #print "right has moved"
131            self.vertical_lines.update()
132            self.horizontal_lines.update(x1=self.vertical_lines.x1,
133                                         x2=self.vertical_lines.x2,
134                                         width=self.vertical_lines.half_width)
135           
136
137    def save(self, ev):
138        """
139        Remember the roughness for this layer and the next so that we
140        can restore on Esc.
141        """
142        self.base.freeze_axes()
143        self.horizontal_lines.save(ev)
144        self.vertical_lines.save(ev)
145        self.center.save(ev)
146       
147    def _post_data(self):
148        # Compute data
149        print 
150       
151        data = self.base.data2D
152        from DataLoader.manipulations import  Boxavg
153        #radius = math.sqrt(math.pow(self.qmax,2)+math.pow(self.qmax,2))
154        x_min= self.horizontal_lines.x2
155        x_max= self.horizontal_lines.x1
156        y_min= self.vertical_lines.y2
157        y_max= self.vertical_lines.y1
158        box =  Boxavg (x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max)
159        self.count, self.error = box(self.base.data2D)
160        #print "count, error",self.count, self.error
161                         
162    def moveend(self, ev):
163        self.base.thaw_axes()
164        # Post paramters
165        self._post_data()
166       
167        self.type= self.__class__.__name__
168        params= self.get_params()
169        event = SlicerParamUpdateEvent(type=self.type, params=params,
170                                       panel_name= self.panel_name)
171        wx.PostEvent(self.base.parent, event)
172       
173           
174    def restore(self):
175        """
176        Restore the roughness for this layer.
177        """
178        self.horizontal_lines.restore()
179        self.vertical_lines.restore()
180        self.center.restore()
181    def move(self, x, y, ev):
182        """
183        Process move to a new position, making sure that the move is allowed.
184        """
185        pass
186   
187    def set_cursor(self, x, y):
188        pass
189       
190    def get_params(self):
191        params = {}
192       
193        params["Width"] = math.fabs(self.vertical_lines.half_width)*2
194        params["Height"] = math.fabs(self.horizontal_lines.half_height)*2 
195       
196        params["center_x"] = self.center.x
197        params["center_y"] =self.center.y
198        params["count"] = self.count
199        params["errors"]= self.error
200       
201        return params
202   
203   
204    def get_result(self):
205        """
206            return the result of box summation
207        """
208        result={}
209        result["count"] = self.count
210        result["error"] = self.error
211        return result
212       
213       
214    def set_params(self, params):
215       
216        x_max = math.fabs(params["Width"] )/2
217        y_max = math.fabs(params["Height"] )/2
218       
219        self.center_x=params["center_x"] 
220        self.center_y=params["center_y"]
221        """
222        self.center.update(center_x=self.center_x,center_y=self.center_y)
223       
224        self.horizontal_lines.update(center= self.center,
225                                     width=x_max,
226                                     height=y_max)
227        self.vertical_lines.update(center= self.center,
228                                    width=x_max,
229                                    height=y_max)
230       
231        self._post_data()
232       
233        """
234       
235    def freeze_axes(self):
236        self.base.freeze_axes()
237       
238    def thaw_axes(self):
239        self.base.thaw_axes()
240
241    def draw(self):
242        self.base.draw()
243class PointInteractor(_BaseInteractor):
244    """
245         Select an annulus through a 2D plot
246    """
247    def __init__(self,base,axes,color='black', zorder=5,
248                 center_x= 0.0,
249                 center_y= 0.0):
250       
251        _BaseInteractor.__init__(self, base, axes, color=color)
252        self.markers = []
253        self.axes = axes
254        # center
255        self.x = center_x
256        self.y = center_y
257       
258        self.save_x = center_x
259        self.save_y = center_y
260         
261       
262        try:
263            self.center_marker = self.axes.plot([self.x],[self.y], linestyle='',
264                                          marker='s', markersize=10,
265                                          color=self.color, alpha=0.6,
266                                          pickradius=5, label="pick", 
267                                          zorder=zorder, # Prefer this to other lines
268                                          visible=True)[0]
269        except:
270            self.center_marker = self.axes.plot([self.x],[self.y], linestyle='',
271                                          marker='s', markersize=10,
272                                          color=self.color, alpha=0.6,
273                                          label="pick", 
274                                          visible=True)[0]
275            message  = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n"
276            message += "Get the SVN version that is at least as recent as June 1, 2007"
277           
278            #raise "Version error", message
279           
280        # line
281        self.center = self.axes.plot([self.x],[self.y],
282                                      linestyle='-', marker='',
283                                      color=self.color,
284                                      visible=True)[0]
285   
286        self.npts = 30
287        self.has_move=False   
288        self.connect_markers([self.center_marker])
289        self.update()
290   
291    def set_layer(self, n):
292        self.layernum = n
293        self.update()
294       
295    def clear(self):
296        self.clear_markers()
297        try:
298            self.center.remove()
299            self.center_marker.remove()
300        except:
301            # Old version of matplotlib
302            for item in range(len(self.axes.lines)):
303                del self.axes.lines[0]
304       
305    def get_radius(self):
306       
307        return 0
308   
309    def update(self, center_x=None,center_y=None):
310        """
311        Draw the new roughness on the graph.
312        """
313        if center_x !=None: self.x= center_x
314        if center_y !=None: self.y= center_y
315   
316        self.center_marker.set(xdata=[self.x], ydata=[self.y])
317        self.center.set(xdata=[self.x], ydata=[self.y])
318       
319       
320       
321       
322    def save(self, ev):
323        """
324        Remember the roughness for this layer and the next so that we
325        can restore on Esc.
326        """
327        self.save_x= self.x
328        self.save_y= self.y
329       
330        self.base.freeze_axes()
331
332    def moveend(self, ev):
333       
334        self.has_move=False
335        self.base.moveend(ev)
336           
337    def restore(self):
338        """
339        Restore the roughness for this layer.
340        """
341        self.y= self.save_y
342        self.x= self.save_x
343       
344    def move(self, x, y, ev):
345        """
346        Process move to a new position, making sure that the move is allowed.
347        """
348        self.x= x
349        self.y= y
350       
351        self.has_move=True
352        self.base.base.update()
353       
354    def set_cursor(self, x, y):
355        self.move(x, y, None)
356        self.update()
357       
358       
359    def get_params(self):
360        params = {}
361        params["x"] = self.x
362        params["y"] = self.y
363       
364        return params
365   
366    def set_params(self, params):
367        center_x = params["x"] 
368        center_y = params["y"] 
369        self.update(center_x=center_x,center_y=center_y)
370       
371       
372class Vertical_DoubleLine(_BaseInteractor):
373    """
374         Select an annulus through a 2D plot
375    """
376    def __init__(self,base,axes,color='black', zorder=5, x=0.5,y=0.5,
377                 center_x= 0.0,
378                 center_y= 0.0):
379       
380        _BaseInteractor.__init__(self, base, axes, color=color)
381        self.markers = []
382        self.axes = axes
383        # center
384        self.center_x = center_x
385        self.center_y = center_y
386       
387       
388       
389        self.y1     = y + self.center_y
390        self.save_y1= self.y1
391       
392        delta= self.y1- self.center_y
393        self.y2= self.center_y - delta
394        self.save_y2= self.y2
395       
396        self.x1      = x + self.center_x
397        self.save_x1 = self.x1
398         
399        delta= self.x1- self.center_x
400        self.x2= self.center_x - delta
401        self.save_x2 = self.x2
402       
403        self.color=color
404       
405        self.half_height= math.fabs(y)
406        self.save_half_height= math.fabs(y)
407       
408        self.half_width= math.fabs(self.x1- self.x2)/2
409        self.save_half_width=math.fabs(self.x1- self.x2)/2
410       
411        try:
412            self.right_marker = self.axes.plot([self.x1],[0], linestyle='',
413                                          marker='s', markersize=10,
414                                          color=self.color, alpha=0.6,
415                                          pickradius=5, label="pick", 
416                                          zorder=zorder, # Prefer this to other lines
417                                          visible=True)[0]
418        except:
419            self.right_marker = self.axes.plot([self.x1],[0], linestyle='',
420                                          marker='s', markersize=10,
421                                          color=self.color, alpha=0.6,
422                                          label="pick", 
423                                          visible=True)[0]
424            message  = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n"
425            message += "Get the SVN version that is at least as recent as June 1, 2007"
426           
427            #raise "Version error", message
428           
429        # line
430        self.right_line = self.axes.plot([self.x1,self.x1],[self.y1,self.y2],
431                                      linestyle='-', marker='',
432                                      color=self.color,
433                                      visible=True)[0]
434        self.left_line = self.axes.plot([self.x2,self.x2],[self.y1,self.y2],
435                                      linestyle='-', marker='',
436                                      color=self.color,
437                                      visible=True)[0]
438   
439        self.npts = 30
440        self.has_move=False   
441        self.connect_markers([self.right_marker])
442        self.update()
443
444
445    def set_layer(self, n):
446        self.layernum = n
447        self.update()
448       
449    def clear(self):
450        self.clear_markers()
451        try:
452            self.right_marker.remove()
453            self.right_line.remove()
454            self.left_line.remove()
455        except:
456            # Old version of matplotlib
457            for item in range(len(self.axes.lines)):
458                del self.axes.lines[0]
459       
460    def get_radius(self):
461       
462        return 0
463   
464    def update(self,x1=None,x2=None, y1=None,y2=None,width=None, height=None, center=None):
465        """
466        Draw the new roughness on the graph.
467        """
468        #print "self.half_height",self.half_height,self.half_width
469        if width!=None:
470            self.half_width= width
471        if height!=None:
472            self.half_height= height
473        if center!=None:
474            self.center_x= center.x
475            self.center_y= center.y
476            #print "vertical width",self.half_width ,self.center_x
477            self.x1 = self.half_width + self.center_x
478            self.x2= -self.half_width + self.center_x
479           
480            self.y1 = self.half_height + self.center_y
481            self.y2= -self.half_height + self.center_y
482         
483            self.right_marker.set(xdata=[self.x1],ydata=[self.center_y])
484            self.right_line.set(xdata=[self.x1,self.x1], ydata=[self.y1,self.y2])
485            self.left_line.set(xdata=[self.x2,self.x2], ydata=[self.y1,self.y2])
486            return 
487        if x1 !=None: 
488            self.x1= x1
489        if x2 !=None: 
490            self.x2= x2
491        if y1 !=None: 
492            self.y1= y1
493        if y2 !=None: 
494            self.y2= y2
495       
496       
497       
498        self.right_marker.set(xdata=[self.x1],ydata=[self.center_y])
499        self.right_line.set(xdata=[self.x1,self.x1], ydata=[self.y1,self.y2])
500        self.left_line.set(xdata=[self.x2,self.x2], ydata=[self.y1,self.y2])
501       
502       
503    def save(self, ev):
504        """
505        Remember the roughness for this layer and the next so that we
506        can restore on Esc.
507        """
508        self.save_x2= self.x2
509        self.save_y2= self.y2
510       
511        self.save_x1= self.x1
512        self.save_y1= self.y1
513       
514        self.save_half_height= self.half_height
515        #self.save_half_width = math.fabs(self.x1-self.x2)/2
516        self.save_half_width = self.half_width
517        self.base.freeze_axes()
518
519    def moveend(self, ev):
520       
521        self.has_move=False
522        self.base.moveend(ev)
523           
524    def restore(self):
525        """
526        Restore the roughness for this layer.
527        """
528        self.y2= self.save_y2
529        self.x2= self.save_x2
530       
531        self.y1= self.save_y1
532        self.x1= self.save_x1
533       
534        self.half_height= self.save_half_height
535        #self.half_width= math.fabs(self.x1-self.x2)/2
536        self.half_width= self.save_half_width
537       
538    def move(self, x, y, ev):
539        """
540        Process move to a new position, making sure that the move is allowed.
541        """
542        self.x1= x
543        delta= self.x1- self.center_x
544        self.x2= self.center_x - delta
545       
546        self.half_width= math.fabs(self.x1-self.x2)/2
547        #print "Move vert: vertical width",self.half_width ,self.center_x
548        self.has_move=True
549        self.base.base.update()
550       
551    def set_cursor(self, x, y):
552        self.move(x, y, None)
553        self.update()
554       
555       
556    def get_params(self):
557        params = {}
558        params["x"] = self.x1
559        params["y"] = self.y1
560       
561        return params
562   
563    def set_params(self, params):
564        x = params["x"] 
565        y = params["y"] 
566        self.update(x=x, y=y, center_x=None,center_y=None)
567
568class Horizontal_DoubleLine(_BaseInteractor):
569    """
570         Select an annulus through a 2D plot
571    """
572    def __init__(self,base,axes,color='black', zorder=5, x=0.5,y=0.5,
573                 center_x= 0.0,
574                 center_y= 0.0):
575       
576        _BaseInteractor.__init__(self, base, axes, color=color)
577        self.markers = []
578        self.axes = axes
579        # center
580        self.center_x = center_x
581        self.center_y = center_y
582       
583        self.y1     = y + self.center_y
584        self.save_y1= self.y1
585       
586        delta= self.y1- self.center_y
587        self.y2=  self.center_y - delta
588        self.save_y2= self.y2
589       
590        self.x1      = x + self.center_x
591        self.save_x1 = self.x1
592       
593        delta= self.x1- self.center_x
594        self.x2=  self.center_x - delta
595        self.save_x2 = self.x2
596       
597        self.color=color
598       
599        self.half_height= math.fabs(y)
600        self.save_half_height= math.fabs(y)
601       
602        self.half_width= math.fabs(x)
603        self.save_half_width=math.fabs(x)
604   
605        try:
606            self.top_marker = self.axes.plot([0],[self.y1], linestyle='',
607                                          marker='s', markersize=10,
608                                          color=self.color, alpha=0.6,
609                                          pickradius=5, label="pick", 
610                                          zorder=zorder, # Prefer this to other lines
611                                          visible=True)[0]
612        except:
613            self.top_marker = self.axes.plot([0],[self.y1], linestyle='',
614                                          marker='s', markersize=10,
615                                          color=self.color, alpha=0.6,
616                                          label="pick", 
617                                          visible=True)[0]
618            message  = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n"
619            message += "Get the SVN version that is at least as recent as June 1, 2007"
620           
621            #raise "Version error", message
622           
623        # line
624        self.top_line = self.axes.plot([self.x1,-self.x1],[self.y1,self.y1],
625                                      linestyle='-', marker='',
626                                      color=self.color,
627                                      visible=True)[0]
628        self.bottom_line = self.axes.plot([self.x1,-self.x1],[self.y2,self.y2],
629                                      linestyle='-', marker='',
630                                      color=self.color,
631                                      visible=True)[0]
632   
633        self.npts = 30
634        self.has_move=False   
635        self.connect_markers([self.top_marker])
636        self.update()
637
638
639    def set_layer(self, n):
640        self.layernum = n
641        self.update()
642       
643    def clear(self):
644        self.clear_markers()
645        try:
646            self.top_marker.remove()
647            self.bottom_line.remove()
648            self.top_line.remove()
649        except:
650            # Old version of matplotlib
651            for item in range(len(self.axes.lines)):
652                del self.axes.lines[0]
653       
654    def get_radius(self):
655       
656        return 0
657   
658    def update(self,x1=None,x2=None, y1=None,y2=None,width=None,height=None, center=None):
659        """
660        Draw the new roughness on the graph.
661        """
662        #print "self.half_width",self.half_width
663        if width!=None:
664            self.half_width= width
665        if height!=None:
666            self.half_height= height
667        if center!=None:
668            self.center_x= center.x
669            self.center_y= center.y
670           
671            self.x1 = self.half_width + self.center_x
672            self.x2= -self.half_width + self.center_x
673           
674            self.y1 = self.half_height + self.center_y
675            self.y2= -self.half_height + self.center_y
676           
677            self.top_marker.set(xdata=[self.center_x],ydata=[self.y1])
678            self.top_line.set(xdata=[self.x1,self.x2], ydata=[self.y1,self.y1])
679            self.bottom_line.set(xdata=[self.x1,self.x2], ydata=[self.y2,self.y2])
680            return 
681        if x1 !=None: 
682            self.x1= x1
683        if x2 !=None: 
684            self.x2= x2
685        if y1 !=None: 
686            self.y1= y1
687        if y2 !=None: 
688            self.y2= y2
689       
690             
691        self.top_marker.set(xdata=[self.center_x],ydata=[self.y1])
692        self.top_line.set(xdata=[self.x1,self.x2], ydata=[self.y1,self.y1])
693        self.bottom_line.set(xdata=[self.x1,self.x2], ydata=[self.y2,self.y2])
694       
695       
696       
697    def save(self, ev):
698        """
699        Remember the roughness for this layer and the next so that we
700        can restore on Esc.
701        """
702        self.save_x2= self.x2
703        self.save_y2= self.y2
704       
705        self.save_x1= self.x1
706        self.save_y1= self.y1
707       
708        self.save_half_height= self.half_height
709        self.save_half_width =  self.half_width
710        self.base.freeze_axes()
711
712
713    def moveend(self, ev):
714       
715        self.has_move=False
716        self.base.moveend(ev)
717           
718    def restore(self):
719        """
720        Restore the roughness for this layer.
721        """
722        self.y2= self.save_y2
723        self.x2= self.save_x2
724       
725        self.y1= self.save_y1
726        self.x1= self.save_x1
727        self.half_height= self.save_half_height
728        self.half_width= self.save_half_width
729       
730    def move(self, x, y, ev):
731        """
732        Process move to a new position, making sure that the move is allowed.
733        """
734        self.y1= y
735        delta= self.y1- self.center_y
736        self.y2=  self.center_y - delta
737        self.half_height=  math.fabs(self.y1)-self.center_y
738        self.has_move=True
739        self.base.base.update()
740       
741    def set_cursor(self, x, y):
742        self.move(x, y, None)
743        self.update()
744       
745       
746    def get_params(self):
747        params = {}
748        params["x"] = self.x
749        params["y"] = self.y
750       
751        return params
752   
753    def set_params(self, params):
754        x = params["x"] 
755        y = params["y"] 
756        self.update(x=x, y=y, center_x=None,center_y=None)
757         
Note: See TracBrowser for help on using the repository browser.