source: sasview/guiframe/local_perspectives/plotting/boxSum.py @ 6d920cd

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

print statement removed

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