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