source: sasview/guiframe/local_perspectives/plotting/SectorSlicer.py @ 116da060

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 116da060 was 116da060, checked in by Jae Cho <jhjcho@…>, 16 years ago

updated the phimin and phimax shift by pi to make them positive values.

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