source: sasview/guiframe/local_perspectives/plotting/SectorSlicer.py @ 216efab

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

print stament removed

  • Property mode set to 100644
File size: 16.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
10import math
11import wx
12from copy import deepcopy
13
14from BaseInteractor import _BaseInteractor
15from sans.guicomm.events import NewPlotEvent, StatusEvent,SlicerParameterEvent,EVT_SLICER_PARS
16
17
18
19
20
21
22class SectorInteractor(_BaseInteractor):
23    """
24         Select an annulus through a 2D plot
25    """
26    def __init__(self,base,axes,color='black', zorder=3):
27       
28        _BaseInteractor.__init__(self, base, axes, color=color)
29        self.markers = []
30        self.axes = axes       
31        self.qmax = math.sqrt(math.pow(max(self.base.data2D.xmax,abs(self.base.data2D.xmin)),2)+math.pow(max(self.base.data2D.xmax,abs(self.base.data2D.xmin)),2))
32        #print "sector qmax", self.qmax
33        self.connect = self.base.connect
34       
35        ## Number of points on the plot
36        self.nbins = 20
37        self.theta1= math.pi/4
38        self.theta2= math.pi/3
39        self.phi=math.pi/12
40        #self.theta3= 2*self.theta2 -self.theta1
41        # Inner circle
42        self.main_line = LineInteractor(self, self.base.subplot,color='green', zorder=zorder, r=self.qmax,
43                                           theta= self.theta2)
44        self.main_line.qmax = self.qmax
45        #self.left_line = SectionInteractor(self, self.base.subplot, zorder=zorder+1, r=self.qmax,
46        #                                   theta1= self.theta1, theta2= self.theta2)
47        #self.left_line.qmax = self.base.qmax
48        self.right_line= SideInteractor(self, self.base.subplot,color='black', zorder=zorder,
49                                     r=self.qmax,
50                                           phi= -1*self.phi,
51                                           theta2=self.theta2)
52        self.right_line.qmax = self.qmax
53        self.left_line= SideInteractor(self, self.base.subplot,color='blue', zorder=zorder,
54                                     r=self.qmax,
55                                           phi= self.phi,
56                                           theta2=self.theta2)
57        self.left_line.qmax = self.qmax
58        #self.outer_circle.set_cursor(self.base.qmax/1.8, 0)
59       
60                     
61        self.update()
62        self._post_data()
63       
64        # Bind to slice parameter events
65        self.base.parent.Bind(EVT_SLICER_PARS, self._onEVT_SLICER_PARS)
66
67
68    def _onEVT_SLICER_PARS(self, event):
69        wx.PostEvent(self.base.parent, StatusEvent(status="SectorSlicer._onEVT_SLICER_PARS"))
70        event.Skip()
71        if event.type == self.__class__.__name__:
72            self.set_params(event.params)
73            self.base.update()
74
75    def update_and_post(self):
76        self.update()
77        #self._post_data()
78
79    def save_data(self, path, image, x, y):
80        output = open(path, 'w')
81       
82        data_x, data_y = self.get_data(image, x, y)
83       
84        output.write("<phi>  <average>\n")
85        for i in range(len(data_x)):
86            output.write("%g  %g\n" % (data_x[i], data_y[i]))
87        output.close()
88
89    def set_layer(self, n):
90        self.layernum = n
91        self.update()
92       
93    def clear(self):
94        self.clear_markers()
95        self.main_line.clear()
96        self.left_line.clear()
97        self.right_line.clear()
98        #self.base.connect.disconnect()
99        #self.base.parent.Unbind(EVT_SLICER_PARS)
100        self.base.Unbind(EVT_SLICER_PARS)
101       
102    def update(self):
103        """
104        Respond to changes in the model by recalculating the profiles and
105        resetting the widgets.
106        """
107        # Update locations       
108       
109        #if self.main_line.has_move:
110        #self.main_line.update()   
111        #self.right_line.update()   
112        #self.left_line.update()     
113        if self.main_line.has_move:
114            self.main_line.update()
115            self.right_line.update( delta = self.main_line.get_radius(),mline= self.main_line)
116            self.left_line.update( delta = self.main_line.get_radius() ,mline= self.main_line)
117            print "Main line has moved ---> phi right",math.degrees(self.main_line.get_radius()+self.right_line.theta)
118            print "Main line has moved ---> phi left",math.degrees(self.left_line.theta+self.main_line.get_radius())
119        if self.left_line.has_move:
120            print "left line has moved --->"
121            self.main_line.update()
122            self.left_line.update(phi=None,delta=None, mline=self.main_line,side=True, left=True)
123            #self.right_line.update(-1*delta,linem=self.main_line,linel=self.left_line)
124            self.right_line.update(phi=-1*self.left_line.phi,delta=None, mline=self.main_line,side=True, left=True)
125        if self.right_line.has_move:
126            print "right line has moved --->"
127           
128            self.main_line.update()
129            self.right_line.update(phi=None,delta=None, mline=self.main_line,side=True, right=True)
130            #self.right_line.update(-1*delta,linem=self.main_line,linel=self.left_line)
131            self.left_line.update(phi=-1*self.right_line.phi,delta=None, mline=self.main_line,side=True, left=True)
132   
133   
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.inner_circle.save(ev)
143        self.outer_circle.save(ev)
144
145    def _post_data(self, nbins=None):
146        # Compute data
147        data = self.base.data2D
148        # If we have no data, just return
149        if data == None:
150            return
151
152        name = "Sector "
153        from DataLoader.manipulations import SectorQ
154        radius = self.qmax #radius=math.sqrt(math.pow(self.qmax,2)+math.pow(self.qmax,2))
155        phimin = self.right_line.theta+math.pi
156        phimax = self.left_line.theta+math.pi
157        print "sector Q", radius
158        sect = SectorQ(r_min=0.0, r_max= radius , phi_min=phimin, phi_max=phimax)
159        #sect = SectorQ(r_min=-1*radius , r_max= radius , phi_min=phimin, phi_max=phimax)
160        if nbins!=None:
161            sect.nbins = nbins
162       
163        sector = sect(self.base.data2D)
164       
165        from sans.guiframe.dataFitting import Data1D
166        if hasattr(sector,"dxl"):
167            dxl= sector.dxl
168        else:
169            dxl= None
170        if hasattr(sector,"dxw"):
171            dxw= sector.dxw
172        else:
173            dxw= None
174       
175        new_plot = Data1D(x=sector.x,y=sector.y,dy=sector.dy,dxl=dxl,dxw=dxw)
176        new_plot.name = "SectorQ" +"("+ self.base.data2D.name+")"
177       
178       
179
180        new_plot.source=self.base.data2D.source
181        #new_plot.info=self.base.data2D.info
182        new_plot.interactive = True
183        #print "loader output.detector",self.base.data2D.info,output.source,
184        new_plot.detector =self.base.data2D.detector
185        #print "loader output.detector",new_plot.detector
186        # If the data file does not tell us what the axes are, just assume...
187        new_plot.xaxis("\\rm{Q}", 'A^{-1}')
188        new_plot.yaxis("\\rm{Intensity} ","cm^{-1}")
189        new_plot.group_id = "SectorQ"+self.base.data2D.name
190        wx.PostEvent(self.base.parent, NewPlotEvent(plot=new_plot,
191                                                 title="SectorQ"+self.base.data2D.name ))
192       
193         
194       
195    def moveend(self, ev):
196        self.base.thaw_axes()
197       
198        # Post paramters
199        event = SlicerParameterEvent()
200        event.type = self.__class__.__name__
201        event.params = self.get_params()
202        #wx.PostEvent(self.base.parent, event)
203        wx.PostEvent(self.base, event)
204        self._post_data()
205           
206    def restore(self):
207        """
208        Restore the roughness for this layer.
209        """
210        self.main_line.restore()
211        self.left_line.restore()
212        self.right_line.restore()
213
214    def move(self, x, y, ev):
215        """
216        Process move to a new position, making sure that the move is allowed.
217        """
218        pass
219       
220    def set_cursor(self, x, y):
221        pass
222       
223    def get_params(self):
224        params = {}
225        params["main_phi"] = self.main_line.theta
226        if math.fabs(self.left_line.phi) != math.fabs(self.right_line.phi):
227            raise ValueError,"Phi left and phi right are different %f, %f"%(self.left_line.phi, self.right_line.phi)
228        params["left_phi"] = math.fabs(self.left_line.phi)
229        params["nbins"] = self.nbins
230        return params
231   
232    def set_params(self, params):
233       
234        main = params["main_phi"] 
235        phi = params["left_phi"] 
236        self.nbins = int(params["nbins"])
237        self.main_line.theta= main
238       
239        self.main_line.update()
240        self.right_line.update(phi=-1*phi,delta=None, mline=self.main_line,side=True)
241        self.left_line.update(phi=phi,delta=None, mline=self.main_line,side=True)
242       
243        self._post_data(nbins=self.nbins)
244       
245    def freeze_axes(self):
246        self.base.freeze_axes()
247       
248    def thaw_axes(self):
249        self.base.thaw_axes()
250
251    def draw(self):
252        self.base.draw()
253
254       
255class SideInteractor(_BaseInteractor):
256    """
257         Select an annulus through a 2D plot
258    """
259    def __init__(self,base,axes,color='black', zorder=5, r=1.0,phi=math.pi/4, theta2= math.pi/3):
260       
261        _BaseInteractor.__init__(self, base, axes, color=color)
262        self.markers = []
263        self.axes = axes
264       
265        self.save_theta = theta2 + phi
266        self.theta=  theta2 + phi
267        self.theta2 = theta2
268        self.radius = r
269        self.phi = phi
270        self.scale = 10.0
271        #print "init for line side theta2, phi, theta",math.degrees(theta2),math.degrees(phi),math.degrees(self.theta)
272        #raise "Version error", message
273         
274        # Inner circle
275        x1= self.radius*math.cos(self.theta)
276        y1= self.radius*math.sin(self.theta)
277        x2= -1*self.radius*math.cos(self.theta)
278        y2= -1*self.radius*math.sin(self.theta)
279       
280        self.line = self.axes.plot([x1,x2],[y1,y2],
281                                      linestyle='-', marker='',
282                                      color=self.color,
283                                      visible=True)[0]
284       
285        self.npts = 20
286        self.has_move=False
287        self.connect_markers([self.line])
288        #self.update()
289
290    def set_layer(self, n):
291        self.layernum = n
292        self.update()
293       
294    def clear(self):
295        self.clear_markers()
296        try:
297           
298            self.line.remove()
299        except:
300            # Old version of matplotlib
301            for item in range(len(self.axes.lines)):
302                del self.axes.lines[0]
303       
304       
305       
306    def get_radius(self):
307       
308        return self.theta - self.save_theta
309       
310    def update(self,phi=None,delta=None, mline=None,side=False, left= False, right=False):
311        """
312        Draw the new roughness on the graph.
313        """
314        #print "update left or right ", self.has_move
315       
316        if phi !=None:
317            self.phi = phi
318        if delta==None:
319            delta = 0
320        if side:
321            self.theta=  mline.theta + self.phi
322           
323           
324        if mline!=None:
325            self.theta2 = mline.theta
326            #self.phi= math.fabs(self.theta2 - (self.theta+delta))
327        #print "U:for line side theta2, phi, theta",math.degrees(self.theta2),math.degrees(self.phi),math.degrees(self.theta)
328        #if self.theta2 >= self.theta and self.theta>=0:
329        #    print "between 0-pi",math.degrees(self.theta)
330       
331        x1= self.radius*math.cos(self.theta + delta)
332        y1= self.radius*math.sin(self.theta + delta)
333        x2= -1*self.radius*math.cos(self.theta + delta)
334        y2= -1*self.radius*math.sin(self.theta + delta)
335       
336       
337        self.line.set(xdata=[x1,x2], ydata=[y1,y2]) 
338       
339       
340       
341       
342    def save(self, ev):
343        """
344        Remember the roughness for this layer and the next so that we
345        can restore on Esc.
346        """
347        self.save_theta= self.theta
348        self.base.freeze_axes()
349
350    def moveend(self, ev):
351       
352        self.has_move=False
353        self.base.moveend(ev)
354           
355    def restore(self):
356        """
357        Restore the roughness for this layer.
358        """
359        self.theta = self.save_theta
360
361    def move(self, x, y, ev):
362        """
363        Process move to a new position, making sure that the move is allowed.
364        """
365       
366        self.theta= math.atan2(y,x)
367       
368        self.phi= math.fabs(self.theta2 - self.theta)
369       
370        print "move left or right phi ---theta--thetaM", self.phi, self.theta, self.theta2
371        self.has_move=True
372        self.base.base.update()
373       
374    def set_cursor(self, x, y):
375        self.move(x, y, None)
376        self.update()
377       
378       
379    def get_params(self):
380        params = {}
381        params["radius"] = self.radius
382        params["theta"] = self.theta
383        return params
384   
385    def set_params(self, params):
386
387        x = params["radius"] 
388        self.set_cursor(x, self._inner_mouse_y)
389       
390
391
392       
393       
394class LineInteractor(_BaseInteractor):
395    """
396         Select an annulus through a 2D plot
397    """
398    def __init__(self,base,axes,color='black', zorder=5, r=1.0,theta=math.pi/4):
399       
400        _BaseInteractor.__init__(self, base, axes, color=color)
401        self.markers = []
402        self.axes = axes
403       
404        self.save_theta = theta
405        self.theta= theta
406       
407        self.radius = r
408     
409        self.scale = 10.0
410     
411        #raise "Version error", message
412           
413        # Inner circle
414
415           
416        x1= self.radius*math.cos(self.theta)
417        y1= self.radius*math.sin(self.theta)
418        x2= -1*self.radius*math.cos(self.theta)
419        y2= -1*self.radius*math.sin(self.theta)
420       
421        self.line = self.axes.plot([x1,x2],[y1,y2],
422                                      linestyle='-', marker='',
423                                      color=self.color,
424                                      visible=True)[0]
425     
426        self.npts = 20
427        self.has_move=False
428        self.connect_markers([self.line])
429        self.update()
430
431    def set_layer(self, n):
432        self.layernum = n
433        self.update()
434       
435    def clear(self):
436        self.clear_markers()
437        try:
438           
439            self.line.remove()
440        except:
441            # Old version of matplotlib
442            for item in range(len(self.axes.lines)):
443                del self.axes.lines[0]
444       
445       
446       
447    def get_radius(self):
448       
449        return self.theta - self.save_theta
450       
451    def update(self, theta=None):
452        """
453        Draw the new roughness on the graph.
454        """
455        print "update main line", self.theta
456        if theta !=None:
457            self.theta= theta
458        x1= self.radius*math.cos(self.theta)
459        y1= self.radius*math.sin(self.theta)
460        x2= -1*self.radius*math.cos(self.theta)
461        y2= -1*self.radius*math.sin(self.theta)
462     
463        self.line.set(xdata=[x1,x2], ydata=[y1,y2]) 
464     
465       
466       
467    def save(self, ev):
468        """
469        Remember the roughness for this layer and the next so that we
470        can restore on Esc.
471        """
472        self.save_theta= self.theta
473        self.base.freeze_axes()
474
475    def moveend(self, ev):
476       
477        self.has_move=False
478        self.base.moveend(ev)
479           
480    def restore(self):
481        """
482        Restore the roughness for this layer.
483        """
484        self.theta = self.save_theta
485
486    def move(self, x, y, ev):
487        """
488        Process move to a new position, making sure that the move is allowed.
489        """
490       
491        self.theta= math.atan2(y,x)
492        print "main_line previous theta --- next theta ",math.degrees(self.save_theta),math.degrees(self.theta)
493       
494        self.has_move=True
495        self.base.base.update()
496       
497    def set_cursor(self, x, y):
498        self.move(x, y, None)
499        self.update()
500       
501       
502    def get_params(self):
503        params = {}
504        params["radius"] = self.radius
505        params["theta"] = self.theta
506        return params
507   
508    def set_params(self, params):
509
510        x = params["radius"] 
511        self.set_cursor(x, self._inner_mouse_y)
512       
513
514
515       
Note: See TracBrowser for help on using the repository browser.