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 |
---|
10 | import math |
---|
11 | import wx |
---|
12 | from copy import deepcopy |
---|
13 | |
---|
14 | from BaseInteractor import _BaseInteractor |
---|
15 | from sans.guicomm.events import NewPlotEvent, StatusEvent,SlicerParameterEvent,EVT_SLICER_PARS |
---|
16 | |
---|
17 | |
---|
18 | |
---|
19 | class SectorInteractor(_BaseInteractor): |
---|
20 | """ |
---|
21 | Select an annulus through a 2D plot |
---|
22 | """ |
---|
23 | def __init__(self,base,axes,color='black', zorder=3): |
---|
24 | """ |
---|
25 | """ |
---|
26 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
27 | self.markers = [] |
---|
28 | self.axes = axes |
---|
29 | self.qmax = self.base.data2D.xmax |
---|
30 | self.connect = self.base.connect |
---|
31 | |
---|
32 | ## Number of points on the plot |
---|
33 | self.nbins = 20 |
---|
34 | theta1=math.pi/8 |
---|
35 | theta2=math.pi/2 |
---|
36 | theta1=2*math.pi/3 |
---|
37 | theta2=-2*math.pi/3 |
---|
38 | r1=self.qmax/2.0 |
---|
39 | r2=self.qmax/1.8 |
---|
40 | |
---|
41 | # Inner circle |
---|
42 | from Arc import ArcInteractor |
---|
43 | self.inner_circle = ArcInteractor(self, self.base.subplot, zorder=zorder, r=self.qmax/2.0,theta1= theta1, |
---|
44 | theta2=theta2) |
---|
45 | self.inner_circle.qmax = self.qmax |
---|
46 | self.outer_circle = ArcInteractor(self, self.base.subplot, zorder=zorder+1, r=self.qmax/1.8,theta1= theta1, |
---|
47 | theta2=theta2) |
---|
48 | self.outer_circle.qmax = self.qmax*1.2 |
---|
49 | #self.outer_circle.set_cursor(self.base.qmax/1.8, 0) |
---|
50 | from Edge import RadiusInteractor |
---|
51 | self.right_edge= RadiusInteractor(self, self.base.subplot, zorder=zorder+1, |
---|
52 | arc1=self.inner_circle, |
---|
53 | arc2=self.outer_circle, |
---|
54 | theta=theta1) |
---|
55 | self.left_edge= RadiusInteractor(self, self.base.subplot, zorder=zorder+1, |
---|
56 | arc1=self.inner_circle, |
---|
57 | arc2=self.outer_circle, |
---|
58 | theta=theta2) |
---|
59 | self.update() |
---|
60 | self._post_data() |
---|
61 | # Bind to slice parameter events |
---|
62 | self.base.parent.Bind(EVT_SLICER_PARS, self._onEVT_SLICER_PARS) |
---|
63 | |
---|
64 | def _onEVT_SLICER_PARS(self, event): |
---|
65 | """ |
---|
66 | """ |
---|
67 | #printEVT("AnnulusSlicer._onEVT_SLICER_PARS") |
---|
68 | event.Skip() |
---|
69 | if event.type == self.__class__.__name__: |
---|
70 | self.set_params(event.params) |
---|
71 | self.base.update() |
---|
72 | |
---|
73 | def save_data(self, path, image, x, y): |
---|
74 | """ |
---|
75 | """ |
---|
76 | output = open(path, 'w') |
---|
77 | |
---|
78 | data_x, data_y = self.get_data(image, x, y) |
---|
79 | |
---|
80 | output.write("<phi> <average>\n") |
---|
81 | for i in range(len(data_x)): |
---|
82 | output.write("%g %g\n" % (data_x[i], data_y[i])) |
---|
83 | output.close() |
---|
84 | |
---|
85 | def set_layer(self, n): |
---|
86 | """ |
---|
87 | """ |
---|
88 | self.layernum = n |
---|
89 | self.update() |
---|
90 | |
---|
91 | def clear(self): |
---|
92 | """ |
---|
93 | """ |
---|
94 | self.clear_markers() |
---|
95 | self.outer_circle.clear() |
---|
96 | self.inner_circle.clear() |
---|
97 | self.right_edge.clear() |
---|
98 | self.left_edge.clear() |
---|
99 | #self.base.connect.disconnect() |
---|
100 | self.base.parent.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 | if self.inner_circle.has_move: |
---|
109 | #print "inner circle has moved" |
---|
110 | self.inner_circle.update() |
---|
111 | r1=self.inner_circle.get_radius() |
---|
112 | r2=self.outer_circle.get_radius() |
---|
113 | self.right_edge.update(r1,r2) |
---|
114 | self.left_edge.update(r1,r2) |
---|
115 | if self.outer_circle.has_move: |
---|
116 | #print "outer circle has moved" |
---|
117 | self.outer_circle.update() |
---|
118 | r1=self.inner_circle.get_radius() |
---|
119 | r2=self.outer_circle.get_radius() |
---|
120 | self.left_edge.update(r1,r2) |
---|
121 | self.right_edge.update(r1,r2) |
---|
122 | if self.right_edge.has_move: |
---|
123 | #print "right edge has moved" |
---|
124 | self.right_edge.update() |
---|
125 | self.inner_circle.update(theta1=self.right_edge.get_angle(), theta2=None) |
---|
126 | self.outer_circle.update(theta1=self.right_edge.get_angle(), theta2=None) |
---|
127 | if self.left_edge.has_move: |
---|
128 | #print "left Edge has moved" |
---|
129 | self.left_edge.update() |
---|
130 | self.inner_circle.update(theta1=None, theta2=self.left_edge.get_angle()) |
---|
131 | self.outer_circle.update(theta1=None, theta2=self.left_edge.get_angle()) |
---|
132 | |
---|
133 | |
---|
134 | def save(self, ev): |
---|
135 | """ |
---|
136 | Remember the roughness for this layer and the next so that we |
---|
137 | can restore on Esc. |
---|
138 | """ |
---|
139 | self.base.freeze_axes() |
---|
140 | self.inner_circle.save(ev) |
---|
141 | self.outer_circle.save(ev) |
---|
142 | self.right_edge.save(ev) |
---|
143 | self.left_edge.save(ev) |
---|
144 | |
---|
145 | def _post_data(self): |
---|
146 | pass |
---|
147 | |
---|
148 | def post_data(self,new_sector ): |
---|
149 | """ post data averaging in Q""" |
---|
150 | if self.inner_circle.get_radius() < self.outer_circle.get_radius(): |
---|
151 | rmin=self.inner_circle.get_radius() |
---|
152 | rmax=self.outer_circle.get_radius() |
---|
153 | else: |
---|
154 | rmin=self.outer_circle.get_radius() |
---|
155 | rmax=self.inner_circle.get_radius() |
---|
156 | if self.right_edge.get_angle() < self.left_edge.get_angle(): |
---|
157 | phimin=self.right_edge.get_angle() |
---|
158 | phimax=self.left_edge.get_angle() |
---|
159 | else: |
---|
160 | phimin=self.left_edge.get_angle() |
---|
161 | phimax=self.right_edge.get_angle() |
---|
162 | |
---|
163 | #print "phimin, phimax, rmin ,rmax",math.degrees(phimin), math.degrees(phimax), rmin ,rmax |
---|
164 | #from DataLoader.manipulations import SectorQ |
---|
165 | |
---|
166 | sect = new_sector(r_min=rmin, r_max=rmax, phi_min=phimin, phi_max=phimax) |
---|
167 | sector = sect(self.base.data2D) |
---|
168 | |
---|
169 | from sans.guiframe.dataFitting import Data1D |
---|
170 | if hasattr(sector,"dxl"): |
---|
171 | dxl= sector.dxl |
---|
172 | else: |
---|
173 | dxl= None |
---|
174 | if hasattr(sector,"dxw"): |
---|
175 | dxw= sector.dxw |
---|
176 | else: |
---|
177 | dxw= None |
---|
178 | |
---|
179 | new_plot = Data1D(x=sector.x,y=sector.y,dy=sector.dy,dxl=dxl,dxw=dxw) |
---|
180 | new_plot.name = str(new_sector.__name__) +"("+ self.base.data2D.name+")" |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | new_plot.source=self.base.data2D.source |
---|
185 | new_plot.interactive = True |
---|
186 | #print "loader output.detector",output.source |
---|
187 | new_plot.detector =self.base.data2D.detector |
---|
188 | # If the data file does not tell us what the axes are, just assume... |
---|
189 | new_plot.xaxis("\\rm{Q}", 'rad') |
---|
190 | new_plot.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
191 | new_plot.group_id = str(new_sector.__name__)+self.base.data2D.name |
---|
192 | wx.PostEvent(self.base.parent, NewPlotEvent(plot=new_plot, |
---|
193 | title=str(new_sector.__name__) )) |
---|
194 | |
---|
195 | |
---|
196 | def moveend(self, ev): |
---|
197 | #self.base.thaw_axes() |
---|
198 | |
---|
199 | # Post paramters |
---|
200 | #event = SlicerParameterEvent() |
---|
201 | #event.type = self.__class__.__name__ |
---|
202 | #event.params = self.get_params() |
---|
203 | #print "main moveend ", event.params |
---|
204 | #wx.PostEvent(self.base.parent, event) |
---|
205 | #self._post_data() |
---|
206 | pass |
---|
207 | |
---|
208 | def restore(self): |
---|
209 | """ |
---|
210 | Restore the roughness for this layer. |
---|
211 | """ |
---|
212 | self.inner_circle.restore() |
---|
213 | self.outer_circle.restore() |
---|
214 | self.right_edge.restore() |
---|
215 | self.left_edge.restore() |
---|
216 | |
---|
217 | def move(self, x, y, ev): |
---|
218 | """ |
---|
219 | Process move to a new position, making sure that the move is allowed. |
---|
220 | """ |
---|
221 | pass |
---|
222 | |
---|
223 | def set_cursor(self, x, y): |
---|
224 | """ |
---|
225 | """ |
---|
226 | pass |
---|
227 | |
---|
228 | def get_params(self): |
---|
229 | """ |
---|
230 | """ |
---|
231 | params = {} |
---|
232 | params["r_min"] = self.inner_circle.get_radius() |
---|
233 | params["r_max"] = self.outer_circle.get_radius() |
---|
234 | params["phi_min"] = self.right_edge.get_angle() |
---|
235 | params["phi_max"] = self.left_edge.get_angle() |
---|
236 | params["nbins"] = self.nbins |
---|
237 | return params |
---|
238 | |
---|
239 | def set_params(self, params): |
---|
240 | """ |
---|
241 | """ |
---|
242 | #print "setparams on main slicer ",params |
---|
243 | inner = params["r_min"] |
---|
244 | outer = params["r_max"] |
---|
245 | phi_min= params["phi_min"] |
---|
246 | phi_max=params["phi_max"] |
---|
247 | self.nbins = int(params["nbins"]) |
---|
248 | |
---|
249 | self.inner_circle.set_cursor(inner, phi_min, phi_max,self.nbins) |
---|
250 | self.outer_circle.set_cursor(outer, phi_min, phi_max, self.nbins) |
---|
251 | self.right_edge.set_cursor(inner, outer, phi_min) |
---|
252 | self.left_edge.set_cursor(inner, outer, phi_max) |
---|
253 | self._post_data() |
---|
254 | |
---|
255 | def freeze_axes(self): |
---|
256 | """ |
---|
257 | """ |
---|
258 | self.base.freeze_axes() |
---|
259 | |
---|
260 | def thaw_axes(self): |
---|
261 | """ |
---|
262 | """ |
---|
263 | self.base.thaw_axes() |
---|
264 | |
---|
265 | def draw(self): |
---|
266 | """ |
---|
267 | """ |
---|
268 | self.base.draw() |
---|
269 | |
---|
270 | class SectorInteractorQ(SectorInteractor): |
---|
271 | """ |
---|
272 | """ |
---|
273 | def __init__(self,base,axes,color='black', zorder=3): |
---|
274 | """ |
---|
275 | """ |
---|
276 | SectorInteractor.__init__(self, base, axes, color=color) |
---|
277 | self.base=base |
---|
278 | self._post_data() |
---|
279 | |
---|
280 | def _post_data(self): |
---|
281 | """ |
---|
282 | """ |
---|
283 | from DataLoader.manipulations import SectorQ |
---|
284 | self.post_data(SectorQ ) |
---|
285 | |
---|
286 | |
---|
287 | class SectorInteractorPhi(SectorInteractor): |
---|
288 | """ |
---|
289 | """ |
---|
290 | def __init__(self,base,axes,color='black', zorder=3): |
---|
291 | """ |
---|
292 | """ |
---|
293 | SectorInteractor.__init__(self, base, axes, color=color) |
---|
294 | self.base=base |
---|
295 | self._post_data() |
---|
296 | |
---|
297 | def _post_data(self): |
---|
298 | """ |
---|
299 | """ |
---|
300 | from DataLoader.manipulations import SectorPhi |
---|
301 | self.post_data(SectorPhi ) |
---|
302 | |
---|
303 | |
---|