source: sasview/guiframe/local_perspectives/plotting/SectorSlicer.py @ 3426077

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

working on slicer

  • Property mode set to 100644
File size: 15.9 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 = 20
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.theta-self.right_line.theta)
114            print "Main line has moved ---> phi left",math.degrees(self.main_line.theta-self.left_line.theta)
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
152        phimax = self.left_line.theta
153        sect = SectorQ(r_min=-1*radius , r_max= radius , phi_min=phimin, phi_max=phimax)
154        if nbins!=None:
155            sect.nbins = nbins
156       
157        sector = sect(self.base.data2D)
158       
159        from sans.guiframe.dataFitting import Data1D
160        if hasattr(sector,"dxl"):
161            dxl= sector.dxl
162        else:
163            dxl= None
164        if hasattr(sector,"dxw"):
165            dxw= sector.dxw
166        else:
167            dxw= None
168       
169        new_plot = Data1D(x=sector.x,y=sector.y,dy=sector.dy,dxl=dxl,dxw=dxw)
170        new_plot.name = "SectorQ" +"("+ self.base.data2D.name+")"
171       
172       
173
174        new_plot.source=self.base.data2D.source
175        new_plot.info=self.base.data2D.info
176        new_plot.interactive = True
177        #print "loader output.detector",output.source
178        new_plot.detector =self.base.data2D.detector
179        # If the data file does not tell us what the axes are, just assume...
180        new_plot.xaxis("\\rm{Q}", 'rad')
181        new_plot.yaxis("\\rm{Intensity} ","cm^{-1}")
182        new_plot.group_id = "SectorQ"+self.base.data2D.name
183        wx.PostEvent(self.base.parent, NewPlotEvent(plot=new_plot,
184                                                 title="SectorQ" ))
185       
186         
187       
188    def moveend(self, ev):
189        self.base.thaw_axes()
190       
191        # Post paramters
192        event = SlicerParameters.SlicerParameterEvent()
193        event.type = self.__class__.__name__
194        event.params = self.get_params()
195        wx.PostEvent(self.base.parent, event)
196        self._post_data()
197           
198    def restore(self):
199        """
200        Restore the roughness for this layer.
201        """
202        self.main_line.restore()
203        self.left_line.restore()
204        self.right_line.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        pass
211       
212    def set_cursor(self, x, y):
213        pass
214       
215    def get_params(self):
216        params = {}
217        params["main_phi"] = self.main_line.theta
218        if math.fabs(self.left_line.phi) != math.fabs(self.right_line.phi):
219            raise ValueError,"Phi left and phi right are different %f, %f"%(self.left_line.phi, self.right_line.phi)
220        params["left_phi"] = self.left_line.phi
221        params["nbins"] = self.nbins
222        return params
223   
224    def set_params(self, params):
225       
226        main = params["main_phi"] 
227        phi = params["left_phi"] 
228        self.nbins = int(params["nbins"])
229        self.main_line.theta= main
230       
231        self.main_line.update()
232        self.right_line.update(phi=-1*phi,delta=None, mline=self.main_line,side=True)
233        self.left_line.update(phi=phi,delta=None, mline=self.main_line,side=True)
234       
235        self._post_data(nbins=self.nbins)
236       
237    def freeze_axes(self):
238        self.base.freeze_axes()
239       
240    def thaw_axes(self):
241        self.base.thaw_axes()
242
243    def draw(self):
244        self.base.draw()
245
246       
247class SideInteractor(_BaseInteractor):
248    """
249         Select an annulus through a 2D plot
250    """
251    def __init__(self,base,axes,color='black', zorder=5, r=1.0,phi=math.pi/4, theta2= math.pi/3):
252       
253        _BaseInteractor.__init__(self, base, axes, color=color)
254        self.markers = []
255        self.axes = axes
256       
257        self.save_theta = theta2 + phi
258        self.theta=  theta2 + phi
259        self.theta2 = theta2
260        self.radius = r
261        self.phi = phi
262        self.scale = 10.0
263        #print "init for line side theta2, phi, theta",math.degrees(theta2),math.degrees(phi),math.degrees(self.theta)
264        #raise "Version error", message
265         
266        # Inner circle
267        x1= self.radius*math.cos(self.theta)
268        y1= self.radius*math.sin(self.theta)
269        x2= -1*self.radius*math.cos(self.theta)
270        y2= -1*self.radius*math.sin(self.theta)
271       
272        self.line = self.axes.plot([x1,x2],[y1,y2],
273                                      linestyle='-', marker='',
274                                      color=self.color,
275                                      visible=True)[0]
276       
277        self.npts = 20
278        self.has_move=False
279        self.connect_markers([self.line])
280        #self.update()
281
282    def set_layer(self, n):
283        self.layernum = n
284        self.update()
285       
286    def clear(self):
287        self.clear_markers()
288        try:
289           
290            self.line.remove()
291        except:
292            # Old version of matplotlib
293            for item in range(len(self.axes.lines)):
294                del self.axes.lines[0]
295       
296       
297       
298    def get_radius(self):
299       
300        return self.theta - self.save_theta
301       
302    def update(self,phi=None,delta=None, mline=None,side=False):
303        """
304        Draw the new roughness on the graph.
305        """
306        #print "update left or right ", self.has_move
307       
308        if phi !=None:
309            self.phi = phi
310        if delta==None:
311            delta = 0
312        if side== True:
313            self.theta=  mline.theta + self.phi
314        if mline!=None:
315            self.theta2 = mline.theta
316        #print "U:for line side theta2, phi, theta",math.degrees(self.theta2),math.degrees(self.phi),math.degrees(self.theta)
317        #if self.theta2 >= self.theta and self.theta>=0:
318        #    print "between 0-pi",math.degrees(self.theta)
319       
320        x1= self.radius*math.cos(self.theta + delta)
321        y1= self.radius*math.sin(self.theta + delta)
322        x2= -1*self.radius*math.cos(self.theta + delta)
323        y2= -1*self.radius*math.sin(self.theta + delta)
324       
325       
326        self.line.set(xdata=[x1,x2], ydata=[y1,y2]) 
327       
328       
329       
330       
331    def save(self, ev):
332        """
333        Remember the roughness for this layer and the next so that we
334        can restore on Esc.
335        """
336        self.save_theta= self.theta
337        self.base.freeze_axes()
338
339    def moveend(self, ev):
340       
341        self.has_move=False
342        self.base.moveend(ev)
343           
344    def restore(self):
345        """
346        Restore the roughness for this layer.
347        """
348        self.theta = self.save_theta
349
350    def move(self, x, y, ev):
351        """
352        Process move to a new position, making sure that the move is allowed.
353        """
354       
355        self.theta= math.atan2(y,x)
356        self.phi= self.theta2 - self.theta
357       
358        print "move left or right phi ---theta--thetaM", self.phi, self.theta, self.theta2
359        self.has_move=True
360        self.base.base.update()
361       
362    def set_cursor(self, x, y):
363        self.move(x, y, None)
364        self.update()
365       
366       
367    def get_params(self):
368        params = {}
369        params["radius"] = self.radius
370        params["theta"] = self.theta
371        return params
372   
373    def set_params(self, params):
374
375        x = params["radius"] 
376        self.set_cursor(x, self._inner_mouse_y)
377       
378
379
380       
381       
382class LineInteractor(_BaseInteractor):
383    """
384         Select an annulus through a 2D plot
385    """
386    def __init__(self,base,axes,color='black', zorder=5, r=1.0,theta=math.pi/4):
387       
388        _BaseInteractor.__init__(self, base, axes, color=color)
389        self.markers = []
390        self.axes = axes
391       
392        self.save_theta = theta
393        self.theta= theta
394       
395        self.radius = r
396     
397        self.scale = 10.0
398     
399        #raise "Version error", message
400           
401        # Inner circle
402
403           
404        x1= self.radius*math.cos(self.theta)
405        y1= self.radius*math.sin(self.theta)
406        x2= -1*self.radius*math.cos(self.theta)
407        y2= -1*self.radius*math.sin(self.theta)
408       
409        self.line = self.axes.plot([x1,x2],[y1,y2],
410                                      linestyle='-', marker='',
411                                      color=self.color,
412                                      visible=True)[0]
413     
414        self.npts = 20
415        self.has_move=False
416        self.connect_markers([self.line])
417        self.update()
418
419    def set_layer(self, n):
420        self.layernum = n
421        self.update()
422       
423    def clear(self):
424        self.clear_markers()
425        try:
426           
427            self.line.remove()
428        except:
429            # Old version of matplotlib
430            for item in range(len(self.axes.lines)):
431                del self.axes.lines[0]
432       
433       
434       
435    def get_radius(self):
436       
437        return self.theta - self.save_theta
438       
439    def update(self, theta=None):
440        """
441        Draw the new roughness on the graph.
442        """
443        print "update main line", self.theta
444        if theta !=None:
445            self.theta= theta
446        x1= self.radius*math.cos(self.theta)
447        y1= self.radius*math.sin(self.theta)
448        x2= -1*self.radius*math.cos(self.theta)
449        y2= -1*self.radius*math.sin(self.theta)
450     
451        self.line.set(xdata=[x1,x2], ydata=[y1,y2]) 
452     
453       
454       
455    def save(self, ev):
456        """
457        Remember the roughness for this layer and the next so that we
458        can restore on Esc.
459        """
460        self.save_theta= self.theta
461        self.base.freeze_axes()
462
463    def moveend(self, ev):
464       
465        self.has_move=False
466        self.base.moveend(ev)
467           
468    def restore(self):
469        """
470        Restore the roughness for this layer.
471        """
472        self.theta = self.save_theta
473
474    def move(self, x, y, ev):
475        """
476        Process move to a new position, making sure that the move is allowed.
477        """
478       
479        self.theta= math.atan2(y,x)
480        print "main_line previous theta --- next theta ",math.degrees(self.save_theta),math.degrees(self.theta)
481       
482        self.has_move=True
483        self.base.base.update()
484       
485    def set_cursor(self, x, y):
486        self.move(x, y, None)
487        self.update()
488       
489       
490    def get_params(self):
491        params = {}
492        params["radius"] = self.radius
493        params["theta"] = self.theta
494        return params
495   
496    def set_params(self, params):
497
498        x = params["radius"] 
499        self.set_cursor(x, self._inner_mouse_y)
500       
501
502
503       
Note: See TracBrowser for help on using the repository browser.