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

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

print stament removed

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