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