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

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 8f584c9 was d468daa, checked in by Gervaise Alina <gervyh@…>, 15 years ago

print stament removed

  • 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.xtransform="x"
157        new_plot.ytransform="y"
158        wx.PostEvent(self.base.parent, NewPlotEvent(plot=new_plot,
159                                                 title="SectorPhi" ))
160       
161         
162       
163       
164    def moveend(self, ev):
165        self.base.thaw_axes()
166       
167        # Post paramters
168        event = SlicerParameterEvent()
169        event.type = self.__class__.__name__
170        event.params = self.get_params()
171        #wx.PostEvent(self.base.parent, event)
172        wx.PostEvent(self.base, event)
173        self._post_data()
174           
175    def restore(self):
176        """
177        Restore the roughness for this layer.
178        """
179        self.inner_circle.restore()
180        self.outer_circle.restore()
181
182    def move(self, x, y, ev):
183        """
184        Process move to a new position, making sure that the move is allowed.
185        """
186        pass
187       
188    def set_cursor(self, x, y):
189        pass
190       
191    def get_params(self):
192        params = {}
193        params["inner_radius"] = self.inner_circle._inner_mouse_x
194        params["outer_radius"] = self.outer_circle._inner_mouse_x
195        params["nbins"] = self.nbins
196        return params
197   
198    def set_params(self, params):
199       
200        inner = params["inner_radius"] 
201        outer = params["outer_radius"] 
202        self.nbins = int(params["nbins"])
203        self.inner_circle.set_cursor(inner, self.inner_circle._inner_mouse_y)
204        self.outer_circle.set_cursor(outer, self.outer_circle._inner_mouse_y)
205        self._post_data(self.nbins)
206       
207    def freeze_axes(self):
208        self.base.freeze_axes()
209       
210    def thaw_axes(self):
211        self.base.thaw_axes()
212
213    def draw(self):
214        self.base.draw()
215
216       
217class RingInteractor(_BaseInteractor):
218    """
219         Select an annulus through a 2D plot
220    """
221    def __init__(self,base,axes,color='black', zorder=5, r=1.0,sign=1):
222       
223        _BaseInteractor.__init__(self, base, axes, color=color)
224        self.markers = []
225        self.axes = axes
226        self._inner_mouse_x = r
227        self._inner_mouse_y = 0
228        self._inner_save_x  = r
229        self._inner_save_y  = 0
230        self.scale = 10.0
231        self.base= base
232        self.sign=sign
233         
234       
235        #print "sign",self.sign,self.sign*math.fabs(self._inner_mouse_x)
236                                                   
237        try:
238            # Inner circle marker
239            self.inner_marker = self.axes.plot([self.sign*math.fabs(self._inner_mouse_x)],[0], linestyle='',
240                                          marker='s', markersize=10,
241                                          color=self.color, alpha=0.6,
242                                          pickradius=5, label="pick", 
243                                          zorder=zorder, # Prefer this to other lines
244                                          visible=True)[0]
245        except:
246            self.inner_marker = self.axes.plot([self.sign*math.fabs(self._inner_mouse_x)],[0], linestyle='',
247                                          marker='s', markersize=10,
248                                          color=self.color, alpha=0.6,
249                                          label="pick", 
250                                          visible=True)[0]
251            message  = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n"
252            message += "Get the SVN version that is at least as recent as June 1, 2007"
253           
254            #raise "Version error", message
255           
256        # Inner circle
257
258           
259        [self.inner_circle] = self.axes.plot([],[],
260                                      linestyle='-', marker='',
261                                      color=self.color)
262        self.npts = 40
263           
264        self.connect_markers([self.inner_marker])
265        self.update()
266
267    def set_layer(self, n):
268        self.layernum = n
269        self.update()
270       
271    def clear(self):
272        self.clear_markers()
273        try:
274            self.inner_marker.remove()
275            self.inner_circle.remove()
276        except:
277            # Old version of matplotlib
278            for item in range(len(self.axes.lines)):
279                del self.axes.lines[0]
280       
281       
282       
283    def get_radius(self):
284        return self._inner_mouse_x
285       
286    def update(self):
287        """
288        Draw the new roughness on the graph.
289        """
290        # Plot inner circle
291        x = []
292        y = []
293        for i in range(self.npts):
294            phi = 2.0*math.pi/(self.npts-1)*i
295           
296            xval = 1.0*self._inner_mouse_x*math.cos(phi) 
297            yval = 1.0*self._inner_mouse_x*math.sin(phi) 
298           
299            x.append(xval)
300            y.append(yval)
301           
302        self.inner_marker.set(xdata=[self.sign*math.fabs(self._inner_mouse_x)],ydata=[0])
303        self.inner_circle.set_data(x, y)       
304
305    def save(self, ev):
306        """
307        Remember the roughness for this layer and the next so that we
308        can restore on Esc.
309        """
310        self._inner_save_x = self._inner_mouse_x
311        self._inner_save_y = self._inner_mouse_y
312        self.base.freeze_axes()
313
314    def moveend(self, ev):
315        self.base.moveend(ev)
316           
317    def restore(self):
318        """
319        Restore the roughness for this layer.
320        """
321        self._inner_mouse_x = self._inner_save_x
322        self._inner_mouse_y = self._inner_save_y
323
324    def move(self, x, y, ev):
325        """
326        Process move to a new position, making sure that the move is allowed.
327        """
328        self._inner_mouse_x = x
329        self._inner_mouse_y = y
330        self.base.base.update()
331       
332    def set_cursor(self, x, y):
333        self.move(x, y, None)
334        self.update()
335       
336       
337    def get_params(self):
338        params = {}
339        params["radius"] = math.fabs(self._inner_mouse_x)
340        return params
341   
342    def set_params(self, params):
343
344        x = params["radius"] 
345        self.set_cursor(x, self._inner_mouse_y)
346       
347       
Note: See TracBrowser for help on using the repository browser.