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 | def moveend(self, ev): |
---|
185 | #TODO: why is this empty? |
---|
186 | pass |
---|
187 | |
---|
188 | def restore(self): |
---|
189 | """ |
---|
190 | Restore the roughness for this layer. |
---|
191 | """ |
---|
192 | self.inner_circle.restore() |
---|
193 | self.outer_circle.restore() |
---|
194 | self.right_edge.restore() |
---|
195 | self.left_edge.restore() |
---|
196 | |
---|
197 | def move(self, x, y, ev): |
---|
198 | """ |
---|
199 | Process move to a new position, making sure that the move is allowed. |
---|
200 | """ |
---|
201 | pass |
---|
202 | |
---|
203 | def set_cursor(self, x, y): |
---|
204 | """ |
---|
205 | """ |
---|
206 | pass |
---|
207 | |
---|
208 | def get_params(self): |
---|
209 | """ |
---|
210 | """ |
---|
211 | params = {} |
---|
212 | params["r_min"] = self.inner_circle.get_radius() |
---|
213 | params["r_max"] = self.outer_circle.get_radius() |
---|
214 | params["phi_min"] = self.right_edge.get_angle() |
---|
215 | params["phi_max"] = self.left_edge.get_angle() |
---|
216 | params["nbins"] = self.nbins |
---|
217 | return params |
---|
218 | |
---|
219 | def set_params(self, params): |
---|
220 | """ |
---|
221 | """ |
---|
222 | # print "setparams on main slicer ",params |
---|
223 | inner = params["r_min"] |
---|
224 | outer = params["r_max"] |
---|
225 | phi_min = params["phi_min"] |
---|
226 | phi_max = params["phi_max"] |
---|
227 | self.nbins = int(params["nbins"]) |
---|
228 | |
---|
229 | self.inner_circle.set_cursor(inner, phi_min, phi_max, self.nbins) |
---|
230 | self.outer_circle.set_cursor(outer, phi_min, phi_max, self.nbins) |
---|
231 | self.right_edge.set_cursor(inner, outer, phi_min) |
---|
232 | self.left_edge.set_cursor(inner, outer, phi_max) |
---|
233 | self._post_data() |
---|
234 | |
---|
235 | def freeze_axes(self): |
---|
236 | """ |
---|
237 | """ |
---|
238 | self.base.freeze_axes() |
---|
239 | |
---|
240 | def thaw_axes(self): |
---|
241 | """ |
---|
242 | """ |
---|
243 | self.base.thaw_axes() |
---|
244 | |
---|
245 | def draw(self): |
---|
246 | """ |
---|
247 | """ |
---|
248 | self.base.draw() |
---|
249 | |
---|
250 | class SectorInteractorQ(SectorInteractor): |
---|
251 | """ |
---|
252 | """ |
---|
253 | def __init__(self, base, axes, color='black', zorder=3): |
---|
254 | """ |
---|
255 | """ |
---|
256 | SectorInteractor.__init__(self, base, axes, color=color) |
---|
257 | self.base = base |
---|
258 | self._post_data() |
---|
259 | |
---|
260 | def _post_data(self): |
---|
261 | """ |
---|
262 | """ |
---|
263 | from sas.sascalc.dataloader.manipulations import SectorQ |
---|
264 | self.post_data(SectorQ) |
---|
265 | |
---|
266 | |
---|
267 | class SectorInteractorPhi(SectorInteractor): |
---|
268 | """ |
---|
269 | """ |
---|
270 | def __init__(self, base, axes, color='black', zorder=3): |
---|
271 | """ |
---|
272 | """ |
---|
273 | SectorInteractor.__init__(self, base, axes, color=color) |
---|
274 | self.base = base |
---|
275 | self._post_data() |
---|
276 | |
---|
277 | def _post_data(self): |
---|
278 | """ |
---|
279 | """ |
---|
280 | from sas.sascalc.dataloader.manipulations import SectorPhi |
---|
281 | self.post_data(SectorPhi) |
---|
282 | |
---|