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