source: sasview/guiframe/local_perspectives/plotting/AnnulusSlicer.py @ 8b30e62

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 8b30e62 was 8b30e62, checked in by Jae Cho <jhjcho@…>, 15 years ago

add id for averaging plot

  • Property mode set to 100644
File size: 10.7 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
8import math
9import wx
10from copy import deepcopy
11# Debug printout
12from sans.guicomm.events import NewPlotEvent, StatusEvent,SlicerParameterEvent,EVT_SLICER_PARS
13from BaseInteractor import _BaseInteractor
14
15
16class AnnulusInteractor(_BaseInteractor):
17    """
18         Select an annulus through a 2D plot
19    """
20    def __init__(self,base,axes,color='black', zorder=3):
21       
22        _BaseInteractor.__init__(self, base, axes, color=color)
23        self.markers = []
24        self.axes = axes
25        self.base= base
26        self.qmax = min(math.fabs(self.base.data2D.xmax),math.fabs(self.base.data2D.xmin))  #must be positive
27        self.connect = self.base.connect
28       
29
30        ## Number of points on the plot
31        self.nbins = 20
32       
33        #Cursor position of Rings (Left(-1) or Right(1))
34        self.xmaxd=self.base.data2D.xmax
35        self.xmind=self.base.data2D.xmin
36
37        #self.sign=1
38       
39        if (self.xmaxd+self.xmind)>0:
40            self.sign=1
41        else:
42            self.sign=-1
43                 
44        # Inner circle
45        self.inner_circle = RingInteractor(self, self.base.subplot, zorder=zorder, r=self.qmax/2.0,sign=self.sign)
46        self.inner_circle.qmax = self.qmax
47        self.outer_circle = RingInteractor(self, self.base.subplot, zorder=zorder+1, r=self.qmax/1.8,sign=self.sign)
48        self.outer_circle.qmax = self.qmax*1.2
49       
50        self.update()
51        self._post_data()
52       
53        # Bind to slice parameter events
54        self.base.parent.Bind(EVT_SLICER_PARS, self._onEVT_SLICER_PARS)
55       
56       
57
58    def _onEVT_SLICER_PARS(self, event):
59        wx.PostEvent(self.base.parent, StatusEvent(status="AnnulusSlicer._onEVT_SLICER_PARS"))
60        event.Skip()
61        if event.type == self.__class__.__name__:
62            self.set_params(event.params)
63            self.base.update()
64
65    def update_and_post(self):
66        self.update()
67        self._post_data()
68
69    def save_data(self, path, image, x, y):
70        output = open(path, 'w')
71       
72        data_x, data_y = self.get_data(image, x, y)
73       
74        output.write("<phi>  <average>\n")
75        for i in range(len(data_x)):
76            output.write("%g  %g\n" % (data_x[i], data_y[i]))
77        output.close()
78
79    def set_layer(self, n):
80        self.layernum = n
81        self.update()
82       
83    def clear(self):
84        self.clear_markers()
85        self.outer_circle.clear()
86        self.inner_circle.clear()
87        #self.base.connect.disconnect()
88        #self.base.parent.Unbind(EVT_SLICER_PARS)
89        self.base.Unbind(EVT_SLICER_PARS)
90       
91    def update(self):
92        """
93        Respond to changes in the model by recalculating the profiles and
94        resetting the widgets.
95        """
96        # Update locations       
97        self.inner_circle.update()
98        self.outer_circle.update()
99       
100 
101
102    def save(self, ev):
103        """
104        Remember the roughness for this layer and the next so that we
105        can restore on Esc.
106        """
107        self.base.freeze_axes()
108        self.inner_circle.save(ev)
109        self.outer_circle.save(ev)
110
111    def _post_data(self,nbins=None):
112         # Compute data
113        data = self.base.data2D
114        # If we have no data, just return
115        if data == None:
116            return
117       
118        from DataLoader.manipulations import SectorPhi
119        radius = self.qmax#math.sqrt(math.pow(self.qmax,2)+math.pow(self.qmax,2))
120        rmin= math.fabs(self.inner_circle.get_radius())
121        rmax = math.fabs(self.outer_circle.get_radius())
122        phi_min=-math.pi
123        phi_max=math.pi
124        phimi=phi_min+math.pi
125        phima=phi_max+math.pi
126        sect = SectorPhi(r_min=rmin , r_max= rmax, phi_min=phimi, phi_max=phima)
127        if nbins!=None:
128            sect.nbins = nbins
129       
130        sector = sect(self.base.data2D)
131       
132        from sans.guiframe.dataFitting import Data1D
133        if hasattr(sector,"dxl"):
134            dxl= sector.dxl
135        else:
136            dxl= None
137        if hasattr(sector,"dxw"):
138            dxw= sector.dxw
139        else:
140            dxw= None
141       
142        new_plot = Data1D(x=sector.x,y=sector.y,dy=sector.dy,dxl=dxl,dxw=dxw)
143        new_plot.name = "SectorPhi" +"("+ self.base.data2D.name+")"
144       
145       
146       
147        new_plot.source=self.base.data2D.source
148        #new_plot.info=self.base.data2D.info
149        new_plot.interactive = True
150        #print "loader output.detector",output.source
151        new_plot.detector =self.base.data2D.detector
152        # If the data file does not tell us what the axes are, just assume...
153        new_plot.xaxis("\\rm{\phi}", 'rad')
154        new_plot.yaxis("\\rm{Intensity} ","cm^{-1}")
155        new_plot.group_id = "SectorPhi"+self.base.data2D.name
156        new_plot.id= "SectorPhi"+self.base.data2D.name
157        new_plot.xtransform="x"
158        new_plot.ytransform="y"
159        wx.PostEvent(self.base.parent, NewPlotEvent(plot=new_plot,
160                                                 title="SectorPhi" ))
161       
162         
163       
164       
165    def moveend(self, ev):
166        self.base.thaw_axes()
167       
168        # Post paramters
169        event = SlicerParameterEvent()
170        event.type = self.__class__.__name__
171        event.params = self.get_params()
172        #wx.PostEvent(self.base.parent, event)
173        wx.PostEvent(self.base, event)
174        self._post_data()
175           
176    def restore(self):
177        """
178        Restore the roughness for this layer.
179        """
180        self.inner_circle.restore()
181        self.outer_circle.restore()
182
183    def move(self, x, y, ev):
184        """
185        Process move to a new position, making sure that the move is allowed.
186        """
187        pass
188       
189    def set_cursor(self, x, y):
190        pass
191       
192    def get_params(self):
193        params = {}
194        params["inner_radius"] = self.inner_circle._inner_mouse_x
195        params["outer_radius"] = self.outer_circle._inner_mouse_x
196        params["nbins"] = self.nbins
197        return params
198   
199    def set_params(self, params):
200       
201        inner = params["inner_radius"] 
202        outer = params["outer_radius"] 
203        self.nbins = int(params["nbins"])
204        self.inner_circle.set_cursor(inner, self.inner_circle._inner_mouse_y)
205        self.outer_circle.set_cursor(outer, self.outer_circle._inner_mouse_y)
206        self._post_data(self.nbins)
207       
208    def freeze_axes(self):
209        self.base.freeze_axes()
210       
211    def thaw_axes(self):
212        self.base.thaw_axes()
213
214    def draw(self):
215        self.base.draw()
216
217       
218class RingInteractor(_BaseInteractor):
219    """
220         Select an annulus through a 2D plot
221    """
222    def __init__(self,base,axes,color='black', zorder=5, r=1.0,sign=1):
223       
224        _BaseInteractor.__init__(self, base, axes, color=color)
225        self.markers = []
226        self.axes = axes
227        self._inner_mouse_x = r
228        self._inner_mouse_y = 0
229        self._inner_save_x  = r
230        self._inner_save_y  = 0
231        self.scale = 10.0
232        self.base= base
233        self.sign=sign
234         
235       
236        #print "sign",self.sign,self.sign*math.fabs(self._inner_mouse_x)
237                                                   
238        try:
239            # Inner circle marker
240            self.inner_marker = self.axes.plot([self.sign*math.fabs(self._inner_mouse_x)],[0], linestyle='',
241                                          marker='s', markersize=10,
242                                          color=self.color, alpha=0.6,
243                                          pickradius=5, label="pick", 
244                                          zorder=zorder, # Prefer this to other lines
245                                          visible=True)[0]
246        except:
247            self.inner_marker = self.axes.plot([self.sign*math.fabs(self._inner_mouse_x)],[0], linestyle='',
248                                          marker='s', markersize=10,
249                                          color=self.color, alpha=0.6,
250                                          label="pick", 
251                                          visible=True)[0]
252            message  = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n"
253            message += "Get the SVN version that is at least as recent as June 1, 2007"
254           
255            #raise "Version error", message
256           
257        # Inner circle
258
259           
260        [self.inner_circle] = self.axes.plot([],[],
261                                      linestyle='-', marker='',
262                                      color=self.color)
263        self.npts = 40
264           
265        self.connect_markers([self.inner_marker])
266        self.update()
267
268    def set_layer(self, n):
269        self.layernum = n
270        self.update()
271       
272    def clear(self):
273        self.clear_markers()
274        try:
275            self.inner_marker.remove()
276            self.inner_circle.remove()
277        except:
278            # Old version of matplotlib
279            for item in range(len(self.axes.lines)):
280                del self.axes.lines[0]
281       
282       
283       
284    def get_radius(self):
285        return self._inner_mouse_x
286       
287    def update(self):
288        """
289        Draw the new roughness on the graph.
290        """
291        # Plot inner circle
292        x = []
293        y = []
294        for i in range(self.npts):
295            phi = 2.0*math.pi/(self.npts-1)*i
296           
297            xval = 1.0*self._inner_mouse_x*math.cos(phi) 
298            yval = 1.0*self._inner_mouse_x*math.sin(phi) 
299           
300            x.append(xval)
301            y.append(yval)
302           
303        self.inner_marker.set(xdata=[self.sign*math.fabs(self._inner_mouse_x)],ydata=[0])
304        self.inner_circle.set_data(x, y)       
305
306    def save(self, ev):
307        """
308        Remember the roughness for this layer and the next so that we
309        can restore on Esc.
310        """
311        self._inner_save_x = self._inner_mouse_x
312        self._inner_save_y = self._inner_mouse_y
313        self.base.freeze_axes()
314
315    def moveend(self, ev):
316        self.base.moveend(ev)
317           
318    def restore(self):
319        """
320        Restore the roughness for this layer.
321        """
322        self._inner_mouse_x = self._inner_save_x
323        self._inner_mouse_y = self._inner_save_y
324
325    def move(self, x, y, ev):
326        """
327        Process move to a new position, making sure that the move is allowed.
328        """
329        self._inner_mouse_x = x
330        self._inner_mouse_y = y
331        self.base.base.update()
332       
333    def set_cursor(self, x, y):
334        self.move(x, y, None)
335        self.update()
336       
337       
338    def get_params(self):
339        params = {}
340        params["radius"] = math.fabs(self._inner_mouse_x)
341        return params
342   
343    def set_params(self, params):
344
345        x = params["radius"] 
346        self.set_cursor(x, self._inner_mouse_y)
347       
348       
Note: See TracBrowser for help on using the repository browser.