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