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.guicomm.events import NewPlotEvent |
---|
15 | from sans.guicomm.events import StatusEvent |
---|
16 | from sans.guicomm.events import SlicerParameterEvent |
---|
17 | from sans.guicomm.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 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 | wx.PostEvent(self.base.parent, |
---|
203 | NewPlotEvent(plot=new_plot, title=str(new_sector.__name__))) |
---|
204 | |
---|
205 | def moveend(self, ev): |
---|
206 | #self.base.thaw_axes() |
---|
207 | |
---|
208 | # Post paramters |
---|
209 | #event = SlicerParameterEvent() |
---|
210 | #event.type = self.__class__.__name__ |
---|
211 | #event.params = self.get_params() |
---|
212 | #print "main moveend ", event.params |
---|
213 | #wx.PostEvent(self.base.parent, event) |
---|
214 | #self._post_data() |
---|
215 | pass |
---|
216 | |
---|
217 | def restore(self): |
---|
218 | """ |
---|
219 | Restore the roughness for this layer. |
---|
220 | """ |
---|
221 | self.inner_circle.restore() |
---|
222 | self.outer_circle.restore() |
---|
223 | self.right_edge.restore() |
---|
224 | self.left_edge.restore() |
---|
225 | |
---|
226 | def move(self, x, y, ev): |
---|
227 | """ |
---|
228 | Process move to a new position, making sure that the move is allowed. |
---|
229 | """ |
---|
230 | pass |
---|
231 | |
---|
232 | def set_cursor(self, x, y): |
---|
233 | """ |
---|
234 | """ |
---|
235 | pass |
---|
236 | |
---|
237 | def get_params(self): |
---|
238 | """ |
---|
239 | """ |
---|
240 | params = {} |
---|
241 | params["r_min"] = self.inner_circle.get_radius() |
---|
242 | params["r_max"] = self.outer_circle.get_radius() |
---|
243 | params["phi_min"] = self.right_edge.get_angle() |
---|
244 | params["phi_max"] = self.left_edge.get_angle() |
---|
245 | params["nbins"] = self.nbins |
---|
246 | return params |
---|
247 | |
---|
248 | def set_params(self, params): |
---|
249 | """ |
---|
250 | """ |
---|
251 | #print "setparams on main slicer ",params |
---|
252 | inner = params["r_min"] |
---|
253 | outer = params["r_max"] |
---|
254 | phi_min= params["phi_min"] |
---|
255 | phi_max=params["phi_max"] |
---|
256 | self.nbins = int(params["nbins"]) |
---|
257 | |
---|
258 | self.inner_circle.set_cursor(inner, phi_min, phi_max,self.nbins) |
---|
259 | self.outer_circle.set_cursor(outer, phi_min, phi_max, self.nbins) |
---|
260 | self.right_edge.set_cursor(inner, outer, phi_min) |
---|
261 | self.left_edge.set_cursor(inner, outer, phi_max) |
---|
262 | self._post_data() |
---|
263 | |
---|
264 | def freeze_axes(self): |
---|
265 | """ |
---|
266 | """ |
---|
267 | self.base.freeze_axes() |
---|
268 | |
---|
269 | def thaw_axes(self): |
---|
270 | """ |
---|
271 | """ |
---|
272 | self.base.thaw_axes() |
---|
273 | |
---|
274 | def draw(self): |
---|
275 | """ |
---|
276 | """ |
---|
277 | self.base.draw() |
---|
278 | |
---|
279 | class SectorInteractorQ(SectorInteractor): |
---|
280 | """ |
---|
281 | """ |
---|
282 | def __init__(self, base, axes, color='black', zorder=3): |
---|
283 | """ |
---|
284 | """ |
---|
285 | SectorInteractor.__init__(self, base, axes, color=color) |
---|
286 | self.base=base |
---|
287 | self._post_data() |
---|
288 | |
---|
289 | def _post_data(self): |
---|
290 | """ |
---|
291 | """ |
---|
292 | from DataLoader.manipulations import SectorQ |
---|
293 | self.post_data(SectorQ) |
---|
294 | |
---|
295 | |
---|
296 | class SectorInteractorPhi(SectorInteractor): |
---|
297 | """ |
---|
298 | """ |
---|
299 | def __init__(self, base, axes, color='black', zorder=3): |
---|
300 | """ |
---|
301 | """ |
---|
302 | SectorInteractor.__init__(self, base, axes, color=color) |
---|
303 | self.base=base |
---|
304 | self._post_data() |
---|
305 | |
---|
306 | def _post_data(self): |
---|
307 | """ |
---|
308 | """ |
---|
309 | from DataLoader.manipulations import SectorPhi |
---|
310 | self.post_data(SectorPhi ) |
---|
311 | |
---|
312 | |
---|