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 | r1=self.qmax/2.0 |
---|
36 | r2=self.qmax/1.8 |
---|
37 | # Inner circle |
---|
38 | from Arc import ArcInteractor |
---|
39 | self.inner_circle = ArcInteractor(self, self.base.subplot, zorder=zorder, r=self.qmax/2.0,theta1= theta1, |
---|
40 | theta2=theta2) |
---|
41 | self.inner_circle.qmax = self.base.qmax |
---|
42 | self.outer_circle = ArcInteractor(self, self.base.subplot, zorder=zorder+1, r=self.qmax/1.8,theta1= theta1, |
---|
43 | theta2=theta2) |
---|
44 | self.outer_circle.qmax = self.base.qmax*1.2 |
---|
45 | #self.outer_circle.set_cursor(self.base.qmax/1.8, 0) |
---|
46 | from Edge import RadiusInteractor |
---|
47 | self.inner_radius= RadiusInteractor(self, self.base.subplot, zorder=zorder+1, |
---|
48 | arc1=self.inner_circle, |
---|
49 | arc2=self.outer_circle, |
---|
50 | theta=math.pi/8) |
---|
51 | self.outer_radius= RadiusInteractor(self, self.base.subplot, zorder=zorder+1, |
---|
52 | arc1=self.inner_circle, |
---|
53 | arc2=self.outer_circle, |
---|
54 | theta=math.pi/2) |
---|
55 | self.update() |
---|
56 | self._post_data() |
---|
57 | |
---|
58 | # Bind to slice parameter events |
---|
59 | #self.base.parent.Bind(SlicerParameters.EVT_SLICER_PARS, self._onEVT_SLICER_PARS) |
---|
60 | |
---|
61 | |
---|
62 | def _onEVT_SLICER_PARS(self, event): |
---|
63 | #printEVT("AnnulusSlicer._onEVT_SLICER_PARS") |
---|
64 | event.Skip() |
---|
65 | if event.type == self.__class__.__name__: |
---|
66 | self.set_params(event.params) |
---|
67 | self.base.update() |
---|
68 | |
---|
69 | |
---|
70 | def update_and_post(self): |
---|
71 | self.update() |
---|
72 | self._post_data() |
---|
73 | |
---|
74 | |
---|
75 | def save_data(self, path, image, x, y): |
---|
76 | output = open(path, 'w') |
---|
77 | |
---|
78 | data_x, data_y = self.get_data(image, x, y) |
---|
79 | |
---|
80 | output.write("<phi> <average>\n") |
---|
81 | for i in range(len(data_x)): |
---|
82 | output.write("%g %g\n" % (data_x[i], data_y[i])) |
---|
83 | output.close() |
---|
84 | |
---|
85 | def set_layer(self, n): |
---|
86 | self.layernum = n |
---|
87 | self.update() |
---|
88 | |
---|
89 | def clear(self): |
---|
90 | self.clear_markers() |
---|
91 | self.outer_circle.clear() |
---|
92 | self.inner_circle.clear() |
---|
93 | #self.base.connect.disconnect() |
---|
94 | #self.base.parent.Unbind(SlicerParameters.EVT_SLICER_PARS) |
---|
95 | |
---|
96 | def update(self): |
---|
97 | """ |
---|
98 | Respond to changes in the model by recalculating the profiles and |
---|
99 | resetting the widgets. |
---|
100 | """ |
---|
101 | # Update locations |
---|
102 | self.inner_circle.update() |
---|
103 | self.outer_circle.update() |
---|
104 | r1=self.inner_circle.get_radius() |
---|
105 | r2=self.outer_circle.get_radius() |
---|
106 | #print"annulus update",r1, r2 |
---|
107 | if self.inner_radius.update(r1,r2)==1: |
---|
108 | #print "went here" |
---|
109 | self.inner_circle.update(theta1=self.inner_radius.get_radius(), theta2=None) |
---|
110 | self.outer_circle.update(theta1=self.inner_radius.get_radius(),theta2=None) |
---|
111 | |
---|
112 | if self.outer_radius.update(r1,r2)==1:#self.outer_circle.update(self.outer_radius.get_radius()) |
---|
113 | self.inner_circle.update(theta1=None, theta2=self.outer_radius.get_radius()) |
---|
114 | self.outer_circle.update(theta1=None,theta2=self.outer_radius.get_radius()) |
---|
115 | |
---|
116 | |
---|
117 | def save(self, ev): |
---|
118 | """ |
---|
119 | Remember the roughness for this layer and the next so that we |
---|
120 | can restore on Esc. |
---|
121 | """ |
---|
122 | self.base.freeze_axes() |
---|
123 | self.inner_circle.save(ev) |
---|
124 | self.outer_circle.save(ev) |
---|
125 | |
---|
126 | def _post_data(self): |
---|
127 | """ post data""" |
---|
128 | rmin=self.inner_circle.get_radius() |
---|
129 | rmax=self.outer_circle.get_radius() |
---|
130 | phimin=self.inner_radius.get_radius() |
---|
131 | phimax=self.outer_radius.get_radius() |
---|
132 | from DataLoader.manipulations import SectorQ, SectorPhi |
---|
133 | sectQ = SectorQ(r_min=rmin, r_max=rmax, phi_min=phimin, phi_max=phimax) |
---|
134 | sectorQ = sectQ(self.base.data2D) |
---|
135 | |
---|
136 | sectPhi = SectorPhi(r_min=rmin, r_max=rmax, phi_min=phimin, phi_max=phimax) |
---|
137 | sectorPhi = sectPhi(self.base.data2D) |
---|
138 | from sans.guiframe.dataFitting import Data1D |
---|
139 | if hasattr(sectorQ,"dxl"): |
---|
140 | dxl= sectorQ.dxl |
---|
141 | else: |
---|
142 | dxl= None |
---|
143 | if hasattr(sectorQ,"dxw"): |
---|
144 | dxw= sectorQ.dxw |
---|
145 | else: |
---|
146 | dxw= None |
---|
147 | |
---|
148 | new_plot1 = Data1D(x=sectorQ.x,y=sectorQ.y,dy=sectorQ.dy,dxl=dxl,dxw=dxw) |
---|
149 | new_plot1.name = "SectorQ ("+ self.base.data2D.name+")" |
---|
150 | if hasattr(sectorPhi,"dxl"): |
---|
151 | dxl= sectorPhi.dxl |
---|
152 | else: |
---|
153 | dxl= None |
---|
154 | if hasattr(sectorPhi,"dxw"): |
---|
155 | dxw= sectorPhi.dxw |
---|
156 | else: |
---|
157 | dxw= None |
---|
158 | new_plot2 = Data1D(x=sectorPhi.x,y=sectorPhi.y,dy=sectorPhi.dy,dxl=dxl,dxw=dxw) |
---|
159 | new_plot2.name = "SectorPhi ("+ self.base.data2D.name+")" |
---|
160 | self.plot_data( new_plot1) |
---|
161 | self.plot_data( new_plot2) |
---|
162 | |
---|
163 | |
---|
164 | def plot_data(self, new_plot): |
---|
165 | """ |
---|
166 | plot a new data 1D corresponding to the phi and Q sector average |
---|
167 | """ |
---|
168 | new_plot.source=self.base.data2D.source |
---|
169 | new_plot.info=self.base.data2D.info |
---|
170 | new_plot.interactive = True |
---|
171 | #print "loader output.detector",output.source |
---|
172 | new_plot.detector =self.base.data2D.detector |
---|
173 | # If the data file does not tell us what the axes are, just assume... |
---|
174 | new_plot.xaxis(self.base.data2D._xaxis,self.base.data2D._xunit) |
---|
175 | new_plot.yaxis(self.base.data2D._yaxis,self.base.data2D._yunit) |
---|
176 | new_plot.group_id = "sector"+self.base.data2D.name |
---|
177 | wx.PostEvent(self.base.parent, NewPlotEvent(plot=new_plot, title=new_plot.name)) |
---|
178 | |
---|
179 | |
---|
180 | def moveend(self, ev): |
---|
181 | self.base.thaw_axes() |
---|
182 | |
---|
183 | # Post paramters |
---|
184 | event = SlicerParameters.SlicerParameterEvent() |
---|
185 | event.type = self.__class__.__name__ |
---|
186 | event.params = self.get_params() |
---|
187 | wx.PostEvent(self.base.parent, event) |
---|
188 | |
---|
189 | self._post_data() |
---|
190 | |
---|
191 | def restore(self): |
---|
192 | """ |
---|
193 | Restore the roughness for this layer. |
---|
194 | """ |
---|
195 | self.inner_circle.restore() |
---|
196 | #self.outer_circle.restore() |
---|
197 | |
---|
198 | def move(self, x, y, ev): |
---|
199 | """ |
---|
200 | Process move to a new position, making sure that the move is allowed. |
---|
201 | """ |
---|
202 | pass |
---|
203 | |
---|
204 | def set_cursor(self, x, y): |
---|
205 | pass |
---|
206 | |
---|
207 | def get_params(self): |
---|
208 | params = {} |
---|
209 | params["inner_radius"] = self.inner_circle._inner_mouse_x |
---|
210 | #params["outer_radius"] = self.outer_circle._inner_mouse_x |
---|
211 | params["nbins"] = self.nbins |
---|
212 | return params |
---|
213 | |
---|
214 | def set_params(self, params): |
---|
215 | |
---|
216 | inner = params["inner_radius"] |
---|
217 | outer = params["outer_radius"] |
---|
218 | self.nbins = int(params["nbins"]) |
---|
219 | self.inner_circle.set_cursor(inner, self.inner_circle._inner_mouse_y) |
---|
220 | self.outer_circle.set_cursor(outer, self.outer_circle._inner_mouse_y) |
---|
221 | #self._post_data() |
---|
222 | |
---|
223 | def freeze_axes(self): |
---|
224 | self.base.freeze_axes() |
---|
225 | |
---|
226 | def thaw_axes(self): |
---|
227 | self.base.thaw_axes() |
---|
228 | |
---|
229 | def draw(self): |
---|
230 | self.base.draw() |
---|
231 | |
---|
232 | |
---|