1 | |
---|
2 | import math |
---|
3 | import wx |
---|
4 | from copy import deepcopy |
---|
5 | |
---|
6 | from BaseInteractor import _BaseInteractor |
---|
7 | from SectorSlicer import SideInteractor, LineInteractor |
---|
8 | from sans.guicomm.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 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
17 | ## Class initialization |
---|
18 | self.markers = [] |
---|
19 | self.axes = axes |
---|
20 | self.is_inside = side |
---|
21 | ## connect the plot to event |
---|
22 | self.connect = self.base.connect |
---|
23 | |
---|
24 | ## compute qmax limit to reset the graph |
---|
25 | x = math.pow(max(self.base.data.xmax,math.fabs(self.base.data.xmin)),2) |
---|
26 | y = math.pow(max(self.base.data.ymax,math.fabs(self.base.data.ymin)),2) |
---|
27 | self.qmax= math.sqrt(x + y) |
---|
28 | ## Number of points on the plot |
---|
29 | self.nbins = 20 |
---|
30 | ## Angle of the middle line |
---|
31 | self.theta2= math.pi/3 |
---|
32 | ## Absolute value of the Angle between the middle line and any side line |
---|
33 | self.phi=math.pi/12 |
---|
34 | |
---|
35 | ## Middle line |
---|
36 | self.main_line = LineInteractor(self, self.base.subplot,color='blue', zorder=zorder, r=self.qmax, |
---|
37 | theta= self.theta2) |
---|
38 | self.main_line.qmax = self.qmax |
---|
39 | ## Right Side line |
---|
40 | self.right_line= SideInteractor(self, self.base.subplot,color='gray', zorder=zorder, |
---|
41 | r=self.qmax, |
---|
42 | phi= -1*self.phi, |
---|
43 | theta2=self.theta2) |
---|
44 | self.right_line.qmax = self.qmax |
---|
45 | ## Left Side line |
---|
46 | self.left_line= SideInteractor(self, self.base.subplot,color='gray', zorder=zorder, |
---|
47 | r=self.qmax, |
---|
48 | phi= self.phi, |
---|
49 | theta2=self.theta2) |
---|
50 | self.left_line.qmax = self.qmax |
---|
51 | ## draw the sector |
---|
52 | self.update() |
---|
53 | self._post_data() |
---|
54 | |
---|
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 | |
---|
68 | def update(self): |
---|
69 | """ |
---|
70 | Respond to changes in the model by recalculating the profiles and |
---|
71 | resetting the widgets. |
---|
72 | """ |
---|
73 | # Update locations |
---|
74 | ## Check if the middle line was dragged and 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 update the slicer accordingly |
---|
90 | if self.right_line.has_move: |
---|
91 | self.main_line.update() |
---|
92 | self.right_line.update( phi=None, delta=None, mline=self.main_line, |
---|
93 | side=True, left=False, right=True ) |
---|
94 | self.left_line.update( phi=self.right_line.phi, delta=None, |
---|
95 | mline=self.main_line, side=True, left=False ) |
---|
96 | #if self.is_inside != None: |
---|
97 | out = self._post_data() |
---|
98 | return out |
---|
99 | |
---|
100 | def save(self, ev): |
---|
101 | """ |
---|
102 | Remember the roughness for this layer and the next so that we |
---|
103 | can restore on Esc. |
---|
104 | """ |
---|
105 | self.base.freeze_axes() |
---|
106 | self.main_line.save(ev) |
---|
107 | self.right_line.save(ev) |
---|
108 | self.left_line.save(ev) |
---|
109 | |
---|
110 | def _post_data(self): |
---|
111 | """ |
---|
112 | compute sector averaging of data into data1D |
---|
113 | """ |
---|
114 | ## get the data to average |
---|
115 | data = self.base.data |
---|
116 | # If we have no data, just return |
---|
117 | if data == None: |
---|
118 | return |
---|
119 | |
---|
120 | mask = data.mask |
---|
121 | ## Averaging |
---|
122 | from DataLoader.manipulations import Sectorcut |
---|
123 | radius = self.qmax |
---|
124 | phimin = -self.left_line.phi + self.main_line.theta |
---|
125 | phimax = self.left_line.phi + self.main_line.theta |
---|
126 | |
---|
127 | mask = Sectorcut(phi_min= phimin, phi_max= phimax) |
---|
128 | |
---|
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 | |
---|
150 | def restore(self): |
---|
151 | """ |
---|
152 | Restore the roughness for this layer. |
---|
153 | """ |
---|
154 | self.main_line.restore() |
---|
155 | self.left_line.restore() |
---|
156 | self.right_line.restore() |
---|
157 | |
---|
158 | def move(self, x, y, ev): |
---|
159 | """ |
---|
160 | Process move to a new position, making sure that the move is allowed. |
---|
161 | """ |
---|
162 | pass |
---|
163 | |
---|
164 | |
---|
165 | def set_cursor(self, x, y): |
---|
166 | pass |
---|
167 | |
---|
168 | |
---|
169 | def get_params(self): |
---|
170 | """ |
---|
171 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
172 | @return params: the dictionary created |
---|
173 | """ |
---|
174 | params = {} |
---|
175 | ## Always make sure that the left and the right line are at phi |
---|
176 | ## angle of the middle line |
---|
177 | if math.fabs(self.left_line.phi) != math.fabs(self.right_line.phi): |
---|
178 | raise ValueError,"Phi left and phi right are different %f, %f"%(self.left_line.phi, self.right_line.phi) |
---|
179 | |
---|
180 | params["Phi"] = self.main_line.theta |
---|
181 | params["Delta_Phi"] = math.fabs(self.left_line.phi) |
---|
182 | return params |
---|
183 | |
---|
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 | @param params: a dictionary containing name of slicer parameters and |
---|
190 | values the user assigned to the slicer. |
---|
191 | """ |
---|
192 | main = params["Phi"] |
---|
193 | phi = math.fabs(params["Delta_Phi"] ) |
---|
194 | |
---|
195 | self.main_line.theta= main |
---|
196 | ## Reset the slicer parameters |
---|
197 | self.main_line.update() |
---|
198 | self.right_line.update( phi=phi,delta=None, mline=self.main_line, |
---|
199 | side=True, right=True ) |
---|
200 | self.left_line.update( phi=phi, delta=None, mline=self.main_line, side=True ) |
---|
201 | ## post the new corresponding data |
---|
202 | self._post_data() |
---|
203 | |
---|
204 | |
---|
205 | def freeze_axes(self): |
---|
206 | self.base.freeze_axes() |
---|
207 | |
---|
208 | |
---|
209 | def thaw_axes(self): |
---|
210 | self.base.thaw_axes() |
---|
211 | |
---|
212 | |
---|
213 | def draw(self): |
---|
214 | self.base.update() |
---|
215 | |
---|