source: sasview/guiframe/local_perspectives/plotting/old_ boxSum.py @ ffd23b5

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 ffd23b5 was ffd23b5, checked in by Gervaise Alina <gervyh@…>, 15 years ago
  • Property mode set to 100644
File size: 17.0 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.0025, x_max=0.0025, 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.0
37        self.center_y= 0.0
38       
39        ## Number of points on the plot
40        self.nbins = 20
41        self.count=0
42        self.error=0
43       
44        self.left_line = VerticalLine(self, self.base.subplot,color='blue', zorder=zorder, 
45                                        ymin= self.ymin, ymax= self.ymax,
46                                        x= self.xmin,
47                                        center_x= self.center_x,
48                                        center_y= self.center_y)
49        self.left_line.qmax = self.qmax
50       
51        self.right_line= VerticalLine(self, self.base.subplot,color='black', zorder=zorder,
52                                     ymin= self.ymin, ymax= self.ymax,
53                                     x=self.xmax,
54                                     center_x= self.center_x,
55                                     center_y= self.center_y)
56        self.right_line.qmax = self.qmax
57       
58        self.top_line= HorizontalLine(self, self.base.subplot,color='green', zorder=zorder,
59                                    y= self.ymax,
60                                    xmin= self.xmin, xmax= self.xmax,
61                                    center_x= self.center_x,
62                                    center_y= self.center_y)
63        self.top_line.qmax = self.qmax
64       
65        self.bottom_line= HorizontalLine(self, self.base.subplot,color='red', zorder=zorder,
66                                    y =self.ymin,
67                                    xmin= self.xmin, xmax= self.xmax,
68                                    center_x= self.center_x,
69                                    center_y= self.center_y)
70        self.bottom_line.qmax = self.qmax
71        #self.outer_circle.set_cursor(self.base.qmax/1.8, 0)
72       
73        self.connect_markers([])
74                     
75        self.update()
76        self._post_data()
77       
78        # Bind to slice parameter events
79        self.base.parent.Bind(SlicerParameters.EVT_SLICER_PARS, self._onEVT_SLICER_PARS)
80
81
82    def _onEVT_SLICER_PARS(self, event):
83        #printEVT("AnnulusSlicer._onEVT_SLICER_PARS")
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.left_line.clear()
110        self.right_line.clear()
111        self.top_line.clear()
112        self.bottom_line.clear()
113       
114        #self.base.connect.disconnect()
115        self.base.parent.Unbind(SlicerParameters.EVT_SLICER_PARS)
116       
117    def update(self):
118        """
119        Respond to changes in the model by recalculating the profiles and
120        resetting the widgets.
121        """
122        if self.left_line.has_move:
123            print "left has moved"
124            self.left_line.update(mline=[self.center_x, self.center_y],translation=True)
125           
126            #self.right_line.update(mline= [self.center_x, self.center_y],translation=True)
127            self.top_line.update( xmin= self.left_line.x ,xmax= self.right_line.x,
128                                  mline= [self.center_x, self.center_y],translation=True)
129            self.bottom_line.update(xmin= self.left_line.x ,xmax= self.right_line.x,
130                                    mline= [self.center_x, self.center_y],translation=True)
131        if self.right_line.has_move:
132            print "right has moved"
133            self.right_line.update(mline= [self.center_x, self.center_y],translation=True)
134            #self.left_line.update(mline=[self.center_x, self.center_y],translation=True)
135            #self.left_line.update(xmin= self.right_line.x ,xmax=-1*self.right_line.x)
136            self.top_line.update( xmin= self.left_line.x ,xmax= self.right_line.x,
137                                  mline= [self.center_x, self.center_y],translation=True)
138            self.bottom_line.update(xmin= self.left_line.x ,xmax= self.right_line.x,
139                                    mline= [self.center_x, self.center_y],translation=True)
140           
141           
142        if self.bottom_line.has_move:
143            print "bottom has moved"
144            self.bottom_line.update(mline= [self.center_x, self.center_y],translation=True)
145            #self.top_line.update(y= -1*self.top_line.y,translation=True)
146            self.left_line.update( ymin= self.bottom_line.y ,ymax= self.top_line.y,
147                                   mline= [self.center_x, self.center_y],translation=True)
148            self.right_line.update(ymin= self.bottom_line.y ,ymax= self.top_line.y,
149                                   mline= [self.center_x, self.center_y],translation=True)
150           
151        if self.top_line.has_move:
152            print "top has moved"
153            self.top_line.update(mline= [self.center_x, self.center_y],translation=True)
154           
155            #self.bottom_line.update(y= -1*self.top_line.y,mline= [self.center_x, self.center_y],
156            #                        translation=True )
157            self.left_line.update(ymin= self.bottom_line.y ,ymax= self.top_line.y,
158                                  mline= [self.center_x, self.center_y],translation=True)
159            self.right_line.update(ymin= self.bottom_line.y ,ymax= self.top_line.y,
160                                   mline= [self.center_x, self.center_y],translation=True)
161           
162   
163    def save(self, ev):
164        """
165        Remember the roughness for this layer and the next so that we
166        can restore on Esc.
167        """
168        self.base.freeze_axes()
169        self.inner_circle.save(ev)
170        self.outer_circle.save(ev)
171
172    def _post_data(self):
173        # Compute data
174        data = self.base.data2D
175        from DataLoader.manipulations import  Boxavg
176        radius = math.sqrt(math.pow(self.qmax,2)+math.pow(self.qmax,2))
177        x_min= self.left_line.x
178        x_max= self.right_line.x
179        y_min= self.bottom_line.y
180        y_max= self.top_line.y
181        box =  Boxavg (x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max)
182        self.count, self.error = box(self.base.data2D)
183        print "count, error",self.count, self.error
184       
185                                       
186    def moveend(self, ev):
187        self.base.thaw_axes()
188       
189        # Post paramters
190        event = SlicerParameters.SlicerParameterEvent()
191        event.type = self.__class__.__name__
192        print "event type boxsum: ", event.type
193        event.params = self.get_params()
194       
195        wx.PostEvent(self.base.parent, event)
196
197        self._post_data()
198           
199    def restore(self):
200        """
201        Restore the roughness for this layer.
202        """
203        self.inner_circle.restore()
204        self.outer_circle.restore()
205
206    def move(self, x, y, ev):
207        """
208        Process move to a new position, making sure that the move is allowed.
209        """
210        print "in move"
211        if self.xmin <= x and x <= self.xmax:
212            print "has move whole", x
213       
214    def set_cursor(self, x, y):
215        pass
216       
217    def get_params(self):
218        params = {}
219        params["x_min"] = self.left_line.x
220        params["x_max"] = self.right_line.x
221        params["y_min"] = self.bottom_line.y
222        params["y_max"] = self.top_line.y
223        params["count"] = self.count
224        params["errors"]= self.error
225        return params
226   
227   
228    def get_result(self):
229        """
230            return the result of box summation
231        """
232        result={}
233        result["count"] = self.count
234        result["error"] = self.error
235        return result
236       
237       
238    def set_params(self, params):
239       
240        x_min = -math.fabs(params["x_min"] )
241        x_max = math.fabs(params["x_max"] )
242        y_min = -math.fabs(params["y_min"])
243        y_max = math.fabs(params["y_max"]) 
244       
245        self.left_line.update(ymin= y_min ,ymax= y_max , x= x_min)
246        self.right_line.update(ymin= y_min ,ymax= y_max, x= x_max)
247        self.top_line.update( xmin= x_min ,xmax= x_max, y= y_max)
248        self.bottom_line.update(xmin= x_min ,xmax= x_max, y=y_min)
249        """
250        self.left_line.update(mline= [center_x, center_y],ymin= y_min ,ymax= y_max)
251        self.right_line.update(mline= [center_x, center_y],ymin= y_min ,ymax= y_max)
252        self.top_line.update(mline= [center_x, center_y], xmin= x_min ,xmax= xmax)
253        self.bottom_line.update(mline= [center_x, center_y],xmin= xmin ,xmax= xmax)
254        """
255       
256        self._post_data()
257    def freeze_axes(self):
258        self.base.freeze_axes()
259       
260    def thaw_axes(self):
261        self.base.thaw_axes()
262
263    def draw(self):
264        self.base.draw()
265class HorizontalLine(_BaseInteractor):
266    """
267         Select an annulus through a 2D plot
268    """
269    def __init__(self,base,axes,color='black', zorder=5, y=0.5,
270                 xmin=0.0,xmax=0.5,
271                 center_x= 0.0,
272                 center_y= 0.0):
273       
274        _BaseInteractor.__init__(self, base, axes, color=color)
275        self.markers = []
276        self.axes = axes
277        # center
278        self.center_x = center_x
279        self.center_y = center_y
280       
281        self.y= y - self.center_y
282        self.save_y= y- self.center_y
283       
284        self.xmin = xmin - self.center_x
285        self.save_xmin = xmin - self.center_x
286        self.xmax = xmax - self.center_x
287        self.save_xmax = xmax - self.center_x
288       
289       
290        self.line = self.axes.plot([self.xmin,self.xmax],[self.y,self.y],
291                                      linestyle='-', marker='',
292                                      color=self.color,
293                                      visible=True)[0]
294 
295
296        self.npts = 20
297        self.has_move=False
298        self.connect_markers([self.line])
299        self.update()
300
301    def set_layer(self, n):
302        self.layernum = n
303        self.update()
304       
305    def clear(self):
306        self.clear_markers()
307        try:
308           
309            self.line.remove()
310        except:
311            # Old version of matplotlib
312            for item in range(len(self.axes.lines)):
313                del self.axes.lines[0]
314       
315    def get_radius(self):
316       
317        return 0
318   
319    def update(self,xmin=None, xmax=None,y=None, mline=None,translation=False):
320        """
321        Draw the new roughness on the graph.
322        """
323        #print "update main line", self.has_move
324        if xmin !=None:
325            self.xmin = xmin # - self.center_x
326        if xmax !=None:
327            self.xmax = xmax #- self.center_x
328        if y !=None:
329            self.y = y #- self.center_y
330       
331        self.line.set(xdata=[self.xmin,self.xmax], ydata=[self.y,self.y])
332     
333       
334       
335    def save(self, ev):
336        """
337        Remember the roughness for this layer and the next so that we
338        can restore on Esc.
339        """
340        self.save_xmin= self.xmin
341        self.save_xmax= self.xmax
342       
343        self.save_y= self.y
344        self.base.freeze_axes()
345
346    def moveend(self, ev):
347       
348        self.has_move=False
349        self.base.moveend(ev)
350           
351    def restore(self):
352        """
353        Restore the roughness for this layer.
354        """
355        self.y= self.save_y
356       
357    def move(self, x, y, ev):
358        """
359        Process move to a new position, making sure that the move is allowed.
360        """
361        self.y= y - self.center_y
362       
363        self.has_move=True
364        self.base.base.update()
365       
366    def set_cursor(self, x, y):
367        self.move(x, y, None)
368        self.update()
369       
370       
371    def get_params(self):
372        params = {}
373        params["radius"] = self.xmin
374        params["theta"] = self.xmax
375       
376        return params
377   
378    def set_params(self, params):
379
380        x = params["radius"] 
381        self.set_cursor(x, self._inner_mouse_y)
382       
383
384
385
386class VerticalLine(_BaseInteractor):
387    """
388         Select an annulus through a 2D plot
389    """
390    def __init__(self,base,axes,color='black', zorder=5, ymin=0.0, 
391                 ymax=0.5,x= 0.5,
392                   center_x= 0,
393                 center_y= 0):
394       
395        _BaseInteractor.__init__(self, base, axes, color=color)
396        self.markers = []
397        self.axes = axes
398        # x coordinate of the vertical line
399        self.center_x= center_x
400        self.center_y= center_y
401        self.x = x - center_x
402        self.save_x = x - center_x
403        # minimum value of y coordinate of the vertical line
404        self.ymin = ymin - center_y
405        self.save_ymin = ymin - center_y
406        # maximum value of y coordinate of the vertical line
407        self.ymax= ymax - center_y
408        self.save_ymax= ymax - center_y
409       
410        # Draw vertical line
411        self.line = self.axes.plot([self.x,self.x],[self.ymin,self.ymax],
412                                      linestyle='-', marker='',
413                                      color=self.color,
414                                      visible=True)[0]
415     
416        self.npts = 20
417        # Check vertical line motion
418        self.has_move=False
419        self.connect_markers([self.line])
420        self.update()
421
422    def set_layer(self, n):
423        self.layernum = n
424        self.update()
425       
426    def clear(self):
427        self.clear_markers()
428        try:
429           
430            self.line.remove()
431        except:
432            # Old version of matplotlib
433            for item in range(len(self.axes.lines)):
434                del self.axes.lines[0]
435       
436    def get_radius(self):
437        return 0
438   
439    def update(self,x=None,ymin=None, ymax=None, mline=None,translation=False):
440        """
441        Draw the new roughness on the graph.
442        """
443        if x!=None:
444            self.x = x #-self.center_x
445        if ymin !=None:
446            self.ymin = ymin #- self.center_y
447        if ymax !=None:
448            self.ymax = ymax #- self.center_y
449       
450        self.line.set(xdata=[self.x,self.x], ydata=[self.ymin,self.ymax])
451     
452       
453       
454    def save(self, ev):
455        """
456        Remember the roughness for this layer and the next so that we
457        can restore on Esc.
458        """
459        self.save_x= self.x
460        self.save_ymin= self.ymin
461        self.save_ymax= self.ymax
462       
463        self.base.freeze_axes()
464
465    def moveend(self, ev):
466       
467        self.has_move=False
468        self.base.moveend(ev)
469           
470    def restore(self):
471        """
472        Restore the roughness for this layer.
473        """
474        self.x = self.save_x
475       
476        self.ymin=self.save_ymin
477        self.ymax=self.save_ymax
478    def move(self, x, y, ev):
479        """
480        Process move to a new position, making sure that the move is allowed.
481        """
482        self.x = x - self.center_x
483       
484        self.has_move=True
485        self.base.base.update()
486       
487       
488    def set_cursor(self, x, y):
489        self.move(x, y, None)
490        self.update()
491       
492       
493    def get_params(self):
494        params = {}
495        params["x"] = self.xmin
496        params["ymin"] = self.ymin
497        params["ymax"] = self.ymax
498        return params
499   
500    def set_params(self, params):
501        """
502            Draw a vertical line given some value of params
503            @param params: a dictionary containing value for x, ymin , ymax to draw
504            a vertical line
505        """
506        x = params["x"] 
507        ymin = params["ymin"] 
508        ymax = params["ymax"] 
509        #self.set_cursor(x, self._inner_mouse_y)
510        self.update(self,x =x,ymin =ymin, ymax =ymax)
511       
512
513         
Note: See TracBrowser for help on using the repository browser.