1 | |
---|
2 | import math |
---|
3 | import wx |
---|
4 | #from copy import deepcopy |
---|
5 | from BaseInteractor import _BaseInteractor |
---|
6 | from SectorSlicer import SideInteractor |
---|
7 | from SectorSlicer import LineInteractor |
---|
8 | from sans.guiframe.events import SlicerParameterEvent |
---|
9 | |
---|
10 | class SectorMask(_BaseInteractor): |
---|
11 | """ |
---|
12 | Draw a sector slicer.Allow to find the data 2D inside of the sector lines |
---|
13 | """ |
---|
14 | def __init__(self, base, axes, color='gray', zorder=3, side=False): |
---|
15 | """ |
---|
16 | """ |
---|
17 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
18 | ## Class initialization |
---|
19 | self.markers = [] |
---|
20 | self.axes = axes |
---|
21 | self.is_inside = side |
---|
22 | ## connect the plot to event |
---|
23 | self.connect = self.base.connect |
---|
24 | |
---|
25 | ## compute qmax limit to reset the graph |
---|
26 | x = math.pow(max(self.base.data.xmax, |
---|
27 | math.fabs(self.base.data.xmin)), 2) |
---|
28 | y = math.pow(max(self.base.data.ymax, |
---|
29 | math.fabs(self.base.data.ymin)), 2) |
---|
30 | self.qmax = math.sqrt(x + y) |
---|
31 | ## Number of points on the plot |
---|
32 | self.nbins = 20 |
---|
33 | ## Angle of the middle line |
---|
34 | self.theta2 = math.pi/3 |
---|
35 | ## Absolute value of the Angle between the middle line and any side line |
---|
36 | self.phi = math.pi/12 |
---|
37 | |
---|
38 | ## Middle line |
---|
39 | self.main_line = LineInteractor(self, self.base.subplot, color='blue', |
---|
40 | zorder=zorder, r=self.qmax, theta=self.theta2) |
---|
41 | self.main_line.qmax = self.qmax |
---|
42 | ## Right Side line |
---|
43 | self.right_line = SideInteractor(self, self.base.subplot, color='gray', |
---|
44 | zorder=zorder, r=self.qmax, phi= -1*self.phi, |
---|
45 | theta2=self.theta2) |
---|
46 | self.right_line.qmax = self.qmax |
---|
47 | ## Left Side line |
---|
48 | self.left_line = SideInteractor(self, self.base.subplot, color='gray', |
---|
49 | zorder=zorder, r=self.qmax, phi= self.phi, |
---|
50 | theta2=self.theta2) |
---|
51 | self.left_line.qmax = self.qmax |
---|
52 | ## draw the sector |
---|
53 | self.update() |
---|
54 | self._post_data() |
---|
55 | |
---|
56 | def clear(self): |
---|
57 | """ |
---|
58 | Clear the slicer and all connected events related to this slicer |
---|
59 | """ |
---|
60 | self.clear_markers() |
---|
61 | self.main_line.clear() |
---|
62 | self.left_line.clear() |
---|
63 | self.right_line.clear() |
---|
64 | self.base.connect.clearall() |
---|
65 | #self.base.Unbind(EVT_SLICER_PARS) |
---|
66 | |
---|
67 | def update(self): |
---|
68 | """ |
---|
69 | Respond to changes in the model by recalculating the profiles and |
---|
70 | resetting the widgets. |
---|
71 | """ |
---|
72 | # Update locations |
---|
73 | ## Check if the middle line was dragged and |
---|
74 | #update the picture accordingly |
---|
75 | if self.main_line.has_move: |
---|
76 | self.main_line.update() |
---|
77 | self.right_line.update(delta=-self.left_line.phi/2, |
---|
78 | mline=self.main_line.theta) |
---|
79 | self.left_line.update(delta=self.left_line.phi/2, |
---|
80 | mline=self.main_line.theta) |
---|
81 | ## Check if the left side has moved and update the slicer accordingly |
---|
82 | if self.left_line.has_move: |
---|
83 | self.main_line.update() |
---|
84 | self.left_line.update(phi=None, delta=None, mline=self.main_line , |
---|
85 | side=True, left=True ) |
---|
86 | self.right_line.update(phi=self.left_line.phi, delta=None, |
---|
87 | mline=self.main_line, side=True, |
---|
88 | left=False, right=True) |
---|
89 | ## Check if the right side line has moved and |
---|
90 | #update the slicer accordingly |
---|
91 | if self.right_line.has_move: |
---|
92 | self.main_line.update() |
---|
93 | self.right_line.update(phi=None, delta=None, mline=self.main_line, |
---|
94 | side=True, left=False, right=True) |
---|
95 | self.left_line.update(phi=self.right_line.phi, delta=None, |
---|
96 | mline=self.main_line, side=True, left=False) |
---|
97 | #if self.is_inside != None: |
---|
98 | out = self._post_data() |
---|
99 | return out |
---|
100 | |
---|
101 | def save(self, ev): |
---|
102 | """ |
---|
103 | Remember the roughness for this layer and the next so that we |
---|
104 | can restore on Esc. |
---|
105 | """ |
---|
106 | self.base.freeze_axes() |
---|
107 | self.main_line.save(ev) |
---|
108 | self.right_line.save(ev) |
---|
109 | self.left_line.save(ev) |
---|
110 | |
---|
111 | def _post_data(self): |
---|
112 | """ |
---|
113 | compute sector averaging of data into data1D |
---|
114 | """ |
---|
115 | ## get the data to average |
---|
116 | data = self.base.data |
---|
117 | # If we have no data, just return |
---|
118 | if data == None: |
---|
119 | return |
---|
120 | |
---|
121 | mask = data.mask |
---|
122 | ## Averaging |
---|
123 | from DataLoader.manipulations import Sectorcut |
---|
124 | radius = self.qmax |
---|
125 | phimin = -self.left_line.phi + self.main_line.theta |
---|
126 | phimax = self.left_line.phi + self.main_line.theta |
---|
127 | |
---|
128 | mask = Sectorcut(phi_min=phimin, phi_max=phimax) |
---|
129 | if self.is_inside: |
---|
130 | out = (mask(data) == False) |
---|
131 | else: |
---|
132 | out = (mask(data)) |
---|
133 | #self.base.data.mask=out |
---|
134 | return out |
---|
135 | |
---|
136 | def moveend(self, ev): |
---|
137 | """ |
---|
138 | Called a dragging motion ends.Get slicer event |
---|
139 | """ |
---|
140 | self.base.thaw_axes() |
---|
141 | ## Post parameters |
---|
142 | event = SlicerParameterEvent() |
---|
143 | event.type = self.__class__.__name__ |
---|
144 | event.params = self.get_params() |
---|
145 | ## Send slicer paramers to plotter2D |
---|
146 | wx.PostEvent(self.base, event) |
---|
147 | self._post_data() |
---|
148 | |
---|
149 | def restore(self): |
---|
150 | """ |
---|
151 | Restore the roughness for this layer. |
---|
152 | """ |
---|
153 | self.main_line.restore() |
---|
154 | self.left_line.restore() |
---|
155 | self.right_line.restore() |
---|
156 | |
---|
157 | def move(self, x, y, ev): |
---|
158 | """ |
---|
159 | Process move to a new position, making sure that the move is allowed. |
---|
160 | """ |
---|
161 | pass |
---|
162 | |
---|
163 | def set_cursor(self, x, y): |
---|
164 | pass |
---|
165 | |
---|
166 | def get_params(self): |
---|
167 | """ |
---|
168 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
169 | |
---|
170 | :return params: the dictionary created |
---|
171 | |
---|
172 | """ |
---|
173 | params = {} |
---|
174 | ## Always make sure that the left and the right line are at phi |
---|
175 | ## angle of the middle line |
---|
176 | if math.fabs(self.left_line.phi) != math.fabs(self.right_line.phi): |
---|
177 | msg = "Phi left and phi right are " |
---|
178 | msg += "different %f, %f" % (self.left_line.phi, |
---|
179 | self.right_line.phi) |
---|
180 | raise ValueError, msg |
---|
181 | params["Phi"] = self.main_line.theta |
---|
182 | params["Delta_Phi"] = math.fabs(self.left_line.phi) |
---|
183 | return params |
---|
184 | |
---|
185 | def set_params(self, params): |
---|
186 | """ |
---|
187 | Receive a dictionary and reset the slicer with values contained |
---|
188 | in the values of the dictionary. |
---|
189 | |
---|
190 | :param params: a dictionary containing name of slicer parameters and |
---|
191 | values the user assigned to the slicer. |
---|
192 | """ |
---|
193 | main = params["Phi"] |
---|
194 | phi = math.fabs(params["Delta_Phi"]) |
---|
195 | |
---|
196 | self.main_line.theta = main |
---|
197 | ## Reset the slicer parameters |
---|
198 | self.main_line.update() |
---|
199 | self.right_line.update(phi=phi, delta=None, mline=self.main_line, |
---|
200 | side=True, right=True) |
---|
201 | self.left_line.update(phi=phi, delta=None, |
---|
202 | mline=self.main_line, side=True) |
---|
203 | ## post the new corresponding data |
---|
204 | self._post_data() |
---|
205 | |
---|
206 | def freeze_axes(self): |
---|
207 | """ |
---|
208 | """ |
---|
209 | self.base.freeze_axes() |
---|
210 | |
---|
211 | def thaw_axes(self): |
---|
212 | """ |
---|
213 | """ |
---|
214 | self.base.thaw_axes() |
---|
215 | |
---|
216 | def draw(self): |
---|
217 | """ |
---|
218 | """ |
---|
219 | self.base.update() |
---|
220 | |
---|