source: sasview/guiframe/local_perspectives/plotting/boxSum.py @ 2a3a890

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

working box sum interface—→need to display values

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