1 | |
---|
2 | |
---|
3 | |
---|
4 | import math |
---|
5 | #import wx |
---|
6 | #from copy import deepcopy |
---|
7 | from BaseInteractor import _BaseInteractor |
---|
8 | from boxSum import PointInteractor |
---|
9 | from boxSum import VerticalDoubleLine |
---|
10 | from boxSum import HorizontalDoubleLine |
---|
11 | #from sas.guiframe.events import SlicerParamUpdateEvent |
---|
12 | |
---|
13 | |
---|
14 | class BoxMask(_BaseInteractor): |
---|
15 | """ |
---|
16 | BoxMask Class: determine 2 rectangular area to find the pixel of |
---|
17 | a Data inside of box. |
---|
18 | |
---|
19 | Uses PointerInteractor , VerticalDoubleLine,HorizontalDoubleLine. |
---|
20 | |
---|
21 | :param zorder: Artists with lower zorder values are drawn first. |
---|
22 | :param x_min: the minimum value of the x coordinate |
---|
23 | :param x_max: the maximum value of the x coordinate |
---|
24 | :param y_min: the minimum value of the y coordinate |
---|
25 | :param y_max: the maximum value of the y coordinate |
---|
26 | |
---|
27 | """ |
---|
28 | def __init__(self, base, axes, color='black', zorder=3, side=None, |
---|
29 | x_min=0.008, x_max=0.008, y_min=0.0025, y_max=0.0025): |
---|
30 | """ |
---|
31 | """ |
---|
32 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
33 | ## class initialization |
---|
34 | ## list of Boxmask markers |
---|
35 | self.markers = [] |
---|
36 | self.axes = axes |
---|
37 | self.mask = None |
---|
38 | self.is_inside = side |
---|
39 | ## connect the artist for the motion |
---|
40 | self.connect = self.base.connect |
---|
41 | ## when qmax is reached the selected line is reset |
---|
42 | #the its previous value |
---|
43 | self.qmax = min(self.base.data.xmax, self.base.data.xmin) |
---|
44 | ## Define the box limits |
---|
45 | self.xmin = -1 * 0.5 * min(math.fabs(self.base.data.xmax), |
---|
46 | math.fabs(self.base.data.xmin)) |
---|
47 | self.ymin = -1 * 0.5 * min(math.fabs(self.base.data.xmax), |
---|
48 | math.fabs(self.base.data.xmin)) |
---|
49 | self.xmax = 0.5 * min(math.fabs(self.base.data.xmax), |
---|
50 | math.fabs(self.base.data.xmin)) |
---|
51 | self.ymax = 0.5 * min(math.fabs(self.base.data.xmax), |
---|
52 | math.fabs(self.base.data.xmin)) |
---|
53 | ## center of the box |
---|
54 | self.center_x = 0.0002 |
---|
55 | self.center_y = 0.0003 |
---|
56 | ## Number of points on the plot |
---|
57 | self.nbins = 20 |
---|
58 | ## Define initial result the summation |
---|
59 | self.count = 0 |
---|
60 | self.error = 0 |
---|
61 | self.data = self.base.data |
---|
62 | ## Flag to determine if the current figure has moved |
---|
63 | ## set to False == no motion , set to True== motion |
---|
64 | self.has_move = False |
---|
65 | ## Create Box edges |
---|
66 | self.horizontal_lines = HorizontalDoubleLine(self, |
---|
67 | self.base.subplot, |
---|
68 | color='blue', |
---|
69 | zorder=zorder, |
---|
70 | y=self.ymax, |
---|
71 | x=self.xmax, |
---|
72 | center_x=self.center_x, |
---|
73 | center_y=self.center_y) |
---|
74 | self.horizontal_lines.qmax = self.qmax |
---|
75 | |
---|
76 | self.vertical_lines = VerticalDoubleLine(self, |
---|
77 | self.base.subplot, |
---|
78 | color='grey', |
---|
79 | zorder=zorder, |
---|
80 | y=self.ymax, |
---|
81 | x=self.xmax, |
---|
82 | center_x=self.center_x, |
---|
83 | center_y=self.center_y) |
---|
84 | self.vertical_lines.qmax = self.qmax |
---|
85 | |
---|
86 | self.center = PointInteractor(self, |
---|
87 | self.base.subplot,color='grey', |
---|
88 | zorder=zorder, |
---|
89 | center_x=self.center_x, |
---|
90 | center_y=self.center_y) |
---|
91 | ## Save the name of the slicer panel associate with this slicer |
---|
92 | self.panel_name = "" |
---|
93 | ## Update and post slicer parameters |
---|
94 | self.update() |
---|
95 | self._post_data() |
---|
96 | |
---|
97 | def clear(self): |
---|
98 | """ |
---|
99 | Clear the slicer and all connected events related to this slicer |
---|
100 | """ |
---|
101 | self.clear_markers() |
---|
102 | self.horizontal_lines.clear() |
---|
103 | self.vertical_lines.clear() |
---|
104 | self.center.clear() |
---|
105 | self.base.connect.clearall() |
---|
106 | #self.base.Unbind(EVT_SLICER_PARS) |
---|
107 | |
---|
108 | def update(self): |
---|
109 | """ |
---|
110 | Respond to changes in the model by recalculating the profiles and |
---|
111 | resetting the widgets. |
---|
112 | """ |
---|
113 | # check if the center point has moved and update the figure accordingly |
---|
114 | if self.center.has_move: |
---|
115 | self.center.update() |
---|
116 | self.horizontal_lines.update(center=self.center) |
---|
117 | self.vertical_lines.update(center=self.center) |
---|
118 | ## check if the horizontal lines have moved and update |
---|
119 | # the figure accordingly |
---|
120 | if self.horizontal_lines.has_move: |
---|
121 | self.horizontal_lines.update() |
---|
122 | self.vertical_lines.update(y1=self.horizontal_lines.y1, |
---|
123 | y2=self.horizontal_lines.y2, |
---|
124 | height=self.horizontal_lines.half_height) |
---|
125 | ## check if the vertical lines have moved and update |
---|
126 | # the figure accordingly |
---|
127 | if self.vertical_lines.has_move: |
---|
128 | self.vertical_lines.update() |
---|
129 | self.horizontal_lines.update(x1=self.vertical_lines.x1, |
---|
130 | x2=self.vertical_lines.x2, |
---|
131 | width=self.vertical_lines.half_width) |
---|
132 | #if self.is_inside != None: |
---|
133 | out = self._post_data() |
---|
134 | return out |
---|
135 | |
---|
136 | def save(self, ev): |
---|
137 | """ |
---|
138 | Remember the roughness for this layer and the next so that we |
---|
139 | can restore on Esc. |
---|
140 | """ |
---|
141 | self.base.freeze_axes() |
---|
142 | self.horizontal_lines.save(ev) |
---|
143 | self.vertical_lines.save(ev) |
---|
144 | self.center.save(ev) |
---|
145 | |
---|
146 | def _post_data(self): |
---|
147 | """ |
---|
148 | Get the limits of the boxsum and compute the sum of the pixel |
---|
149 | contained in that region and the error on that sum |
---|
150 | """ |
---|
151 | from sas.dataloader.manipulations import Boxcut |
---|
152 | ## Data 2D for which the pixel will be summed |
---|
153 | data = self.base.data |
---|
154 | mask = data.mask |
---|
155 | ## the region of the summation |
---|
156 | x_min = self.horizontal_lines.x2 |
---|
157 | x_max = self.horizontal_lines.x1 |
---|
158 | y_min = self.vertical_lines.y2 |
---|
159 | y_max = self.vertical_lines.y1 |
---|
160 | mask = Boxcut(x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max) |
---|
161 | |
---|
162 | if self.is_inside: |
---|
163 | out = (mask(data) == False) |
---|
164 | else: |
---|
165 | out = (mask(data)) |
---|
166 | #self.base.data.mask=out |
---|
167 | self.mask = mask |
---|
168 | return out |
---|
169 | |
---|
170 | def moveend(self, ev): |
---|
171 | """ |
---|
172 | After a dragging motion this function is called to compute |
---|
173 | the error and the sum of pixel of a given data 2D |
---|
174 | """ |
---|
175 | self.base.thaw_axes() |
---|
176 | ## post |
---|
177 | self._post_data() |
---|
178 | |
---|
179 | def restore(self): |
---|
180 | """ |
---|
181 | Restore the roughness for this layer. |
---|
182 | """ |
---|
183 | self.horizontal_lines.restore() |
---|
184 | self.vertical_lines.restore() |
---|
185 | self.center.restore() |
---|
186 | |
---|
187 | def move(self, x, y, ev): |
---|
188 | """ |
---|
189 | Process move to a new position, making sure that the move is allowed. |
---|
190 | """ |
---|
191 | pass |
---|
192 | |
---|
193 | def set_cursor(self, x, y): |
---|
194 | pass |
---|
195 | |
---|
196 | def get_params(self): |
---|
197 | """ |
---|
198 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
199 | |
---|
200 | :return params: the dictionary created |
---|
201 | |
---|
202 | """ |
---|
203 | params = {} |
---|
204 | params["Width"] = math.fabs(self.vertical_lines.half_width) * 2 |
---|
205 | params["Height"] = math.fabs(self.horizontal_lines.half_height) * 2 |
---|
206 | params["center_x"] = self.center.x |
---|
207 | params["center_y"] = self.center.y |
---|
208 | return params |
---|
209 | |
---|
210 | def get_mask(self): |
---|
211 | """ |
---|
212 | return mask as a result of boxcut |
---|
213 | """ |
---|
214 | mask = self.mask |
---|
215 | return mask |
---|
216 | |
---|
217 | def set_params(self, params): |
---|
218 | """ |
---|
219 | Receive a dictionary and reset the slicer with values contained |
---|
220 | in the values of the dictionary. |
---|
221 | |
---|
222 | :param params: a dictionary containing name of slicer parameters and |
---|
223 | values the user assigned to the slicer. |
---|
224 | """ |
---|
225 | x_max = math.fabs(params["Width"] )/2 |
---|
226 | y_max = math.fabs(params["Height"])/2 |
---|
227 | |
---|
228 | self.center_x = params["center_x"] |
---|
229 | self.center_y = params["center_y"] |
---|
230 | #update the slicer given values of params |
---|
231 | self.center.update(center_x=self.center_x, center_y=self.center_y) |
---|
232 | self.horizontal_lines.update(center=self.center, |
---|
233 | width=x_max, |
---|
234 | height=y_max) |
---|
235 | self.vertical_lines.update(center=self.center, |
---|
236 | width=x_max, |
---|
237 | height=y_max) |
---|
238 | #compute the new error and sum given values of params |
---|
239 | self._post_data() |
---|
240 | |
---|
241 | def freeze_axes(self): |
---|
242 | self.base.freeze_axes() |
---|
243 | |
---|
244 | def thaw_axes(self): |
---|
245 | self.base.thaw_axes() |
---|
246 | |
---|
247 | def draw(self): |
---|
248 | self.base.update() |
---|
249 | |
---|
250 | class inner_BoxMask(BoxMask): |
---|
251 | def __call__(self): |
---|
252 | self.base.data.mask = (self._post_data()==False) |
---|
253 | |
---|
254 | |
---|