[c5874f2] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | import math |
---|
[32c0841] | 5 | #import wx |
---|
| 6 | #from copy import deepcopy |
---|
[c5874f2] | 7 | from BaseInteractor import _BaseInteractor |
---|
[32c0841] | 8 | from boxSum import PointInteractor |
---|
| 9 | from boxSum import VerticalDoubleLine |
---|
| 10 | from boxSum import HorizontalDoubleLine |
---|
[55a0dc1] | 11 | #from sans.guiframe.events import SlicerParamUpdateEvent |
---|
[c5874f2] | 12 | |
---|
| 13 | |
---|
| 14 | class BoxMask(_BaseInteractor): |
---|
| 15 | """ |
---|
[83f4445] | 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 |
---|
[c5874f2] | 26 | |
---|
| 27 | """ |
---|
[32c0841] | 28 | def __init__(self, base, axes, color='black', zorder=3, side=None, |
---|
[c5874f2] | 29 | x_min=0.008, x_max=0.008, y_min=0.0025, y_max=0.0025): |
---|
[32c0841] | 30 | """ |
---|
| 31 | """ |
---|
[c5874f2] | 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 |
---|
[32c0841] | 40 | ## when qmax is reached the selected line is reset |
---|
| 41 | #the its previous value |
---|
[c5874f2] | 42 | self.qmax = min(self.base.data.xmax, self.base.data.xmin) |
---|
| 43 | ## Define the box limits |
---|
[32c0841] | 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)) |
---|
[c5874f2] | 52 | ## center of the box |
---|
[32c0841] | 53 | self.center_x = 0.0002 |
---|
| 54 | self.center_y = 0.0003 |
---|
[c5874f2] | 55 | ## Number of points on the plot |
---|
| 56 | self.nbins = 20 |
---|
| 57 | ## Define initial result the summation |
---|
[32c0841] | 58 | self.count = 0 |
---|
| 59 | self.error = 0 |
---|
[c5874f2] | 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 |
---|
[32c0841] | 63 | self.has_move = False |
---|
[c5874f2] | 64 | ## Create Box edges |
---|
[32c0841] | 65 | self.horizontal_lines = HorizontalDoubleLine(self, |
---|
| 66 | self.base.subplot, |
---|
| 67 | color='blue', |
---|
[c5874f2] | 68 | zorder=zorder, |
---|
[32c0841] | 69 | y=self.ymax, |
---|
| 70 | x=self.xmax, |
---|
| 71 | center_x=self.center_x, |
---|
| 72 | center_y=self.center_y) |
---|
[c5874f2] | 73 | self.horizontal_lines.qmax = self.qmax |
---|
| 74 | |
---|
[32c0841] | 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) |
---|
[c5874f2] | 83 | self.vertical_lines.qmax = self.qmax |
---|
| 84 | |
---|
[32c0841] | 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) |
---|
[c5874f2] | 90 | ## Save the name of the slicer panel associate with this slicer |
---|
[32c0841] | 91 | self.panel_name = "" |
---|
[c5874f2] | 92 | ## Update and post slicer parameters |
---|
| 93 | self.update() |
---|
| 94 | self._post_data() |
---|
[32c0841] | 95 | |
---|
[c5874f2] | 96 | def clear(self): |
---|
| 97 | """ |
---|
[83f4445] | 98 | Clear the slicer and all connected events related to this slicer |
---|
[c5874f2] | 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) |
---|
[83f4445] | 106 | |
---|
[c5874f2] | 107 | def update(self): |
---|
| 108 | """ |
---|
[83f4445] | 109 | Respond to changes in the model by recalculating the profiles and |
---|
| 110 | resetting the widgets. |
---|
[c5874f2] | 111 | """ |
---|
[32c0841] | 112 | # check if the center point has moved and update the figure accordingly |
---|
[c5874f2] | 113 | if self.center.has_move: |
---|
| 114 | self.center.update() |
---|
[32c0841] | 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 |
---|
[c5874f2] | 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, |
---|
[32c0841] | 123 | height=self.horizontal_lines.half_height) |
---|
| 124 | ## check if the vertical lines have moved and update |
---|
| 125 | # the figure accordingly |
---|
[c5874f2] | 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 | """ |
---|
[83f4445] | 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 |
---|
[c5874f2] | 149 | """ |
---|
| 150 | from 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 |
---|
[32c0841] | 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 |
---|
[c5874f2] | 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: |
---|
[32c0841] | 162 | out = (mask(data) == False) |
---|
[c5874f2] | 163 | else: |
---|
| 164 | out = (mask(data)) |
---|
| 165 | #self.base.data.mask=out |
---|
[83f4445] | 166 | return out |
---|
| 167 | |
---|
[c5874f2] | 168 | def moveend(self, ev): |
---|
| 169 | """ |
---|
[83f4445] | 170 | After a dragging motion this function is called to compute |
---|
| 171 | the error and the sum of pixel of a given data 2D |
---|
[c5874f2] | 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 | """ |
---|
[83f4445] | 196 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
| 197 | |
---|
| 198 | :return params: the dictionary created |
---|
| 199 | |
---|
[c5874f2] | 200 | """ |
---|
| 201 | params = {} |
---|
[32c0841] | 202 | params["Width"] = math.fabs(self.vertical_lines.half_width) * 2 |
---|
| 203 | params["Height"] = math.fabs(self.horizontal_lines.half_height) * 2 |
---|
[c5874f2] | 204 | params["center_x"] = self.center.x |
---|
[32c0841] | 205 | params["center_y"] = self.center.y |
---|
[c5874f2] | 206 | return params |
---|
| 207 | |
---|
| 208 | def get_mask(self): |
---|
| 209 | """ |
---|
[83f4445] | 210 | return mask as a result of boxcut |
---|
[c5874f2] | 211 | """ |
---|
| 212 | mask = self.mask |
---|
| 213 | return mask |
---|
| 214 | |
---|
| 215 | def set_params(self, params): |
---|
| 216 | """ |
---|
[83f4445] | 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. |
---|
[c5874f2] | 222 | """ |
---|
| 223 | x_max = math.fabs(params["Width"] )/2 |
---|
[32c0841] | 224 | y_max = math.fabs(params["Height"])/2 |
---|
[c5874f2] | 225 | |
---|
[32c0841] | 226 | self.center_x = params["center_x"] |
---|
| 227 | self.center_y = params["center_y"] |
---|
[c5874f2] | 228 | #update the slicer given values of params |
---|
[32c0841] | 229 | self.center.update(center_x=self.center_x, center_y=self.center_y) |
---|
| 230 | self.horizontal_lines.update(center=self.center, |
---|
[c5874f2] | 231 | width=x_max, |
---|
| 232 | height=y_max) |
---|
[32c0841] | 233 | self.vertical_lines.update(center=self.center, |
---|
[c5874f2] | 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): |
---|
[32c0841] | 250 | self.base.data.mask = (self._post_data()==False) |
---|
| 251 | |
---|
| 252 | |
---|