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