source: sasview/guiframe/local_perspectives/plotting/boxSum.py @ 7608cb5

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

working on boxsum

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