[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 | """ |
---|
[83f4445] | 14 | BoxMask Class: determine 2 rectangular area to find the pixel of |
---|
| 15 | a Data inside of box. |
---|
| 16 | |
---|
| 17 | Uses PointerInteractor , VerticalDoubleLine,HorizontalDoubleLine. |
---|
| 18 | |
---|
| 19 | :param zorder: Artists with lower zorder values are drawn first. |
---|
| 20 | :param x_min: the minimum value of the x coordinate |
---|
| 21 | :param x_max: the maximum value of the x coordinate |
---|
| 22 | :param y_min: the minimum value of the y coordinate |
---|
| 23 | :param y_max: the maximum value of the y coordinate |
---|
[c5874f2] | 24 | |
---|
| 25 | """ |
---|
| 26 | def __init__(self,base,axes,color='black', zorder=3, side=None, |
---|
| 27 | x_min=0.008, x_max=0.008, y_min=0.0025, y_max=0.0025): |
---|
| 28 | |
---|
| 29 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 30 | ## class initialization |
---|
| 31 | ## list of Boxmask markers |
---|
| 32 | self.markers = [] |
---|
| 33 | self.axes = axes |
---|
| 34 | self.is_inside = side |
---|
| 35 | ## connect the artist for the motion |
---|
| 36 | self.connect = self.base.connect |
---|
| 37 | ## when qmax is reached the selected line is reset the its previous value |
---|
| 38 | self.qmax = min(self.base.data.xmax, self.base.data.xmin) |
---|
| 39 | ## Define the box limits |
---|
| 40 | self.xmin= -1* 0.5*min(math.fabs(self.base.data.xmax),math.fabs( self.base.data.xmin)) |
---|
| 41 | self.ymin= -1* 0.5*min(math.fabs(self.base.data.xmax),math.fabs( self.base.data.xmin)) |
---|
| 42 | |
---|
| 43 | self.xmax= 0.5*min(math.fabs(self.base.data.xmax),math.fabs( self.base.data.xmin)) |
---|
| 44 | self.ymax= 0.5*min(math.fabs(self.base.data.xmax),math.fabs( self.base.data.xmin)) |
---|
| 45 | ## center of the box |
---|
| 46 | self.center_x= 0.0002 |
---|
| 47 | self.center_y= 0.0003 |
---|
| 48 | ## Number of points on the plot |
---|
| 49 | self.nbins = 20 |
---|
| 50 | ## Define initial result the summation |
---|
| 51 | self.count=0 |
---|
| 52 | self.error=0 |
---|
| 53 | self.data = self.base.data |
---|
| 54 | ## Flag to determine if the current figure has moved |
---|
| 55 | ## set to False == no motion , set to True== motion |
---|
| 56 | self.has_move= False |
---|
| 57 | ## Create Box edges |
---|
| 58 | self.horizontal_lines= HorizontalDoubleLine(self, self.base.subplot,color='blue', |
---|
| 59 | zorder=zorder, |
---|
| 60 | y= self.ymax, |
---|
| 61 | x= self.xmax, |
---|
| 62 | center_x= self.center_x, |
---|
| 63 | center_y= self.center_y) |
---|
| 64 | self.horizontal_lines.qmax = self.qmax |
---|
| 65 | |
---|
[aef2cf2] | 66 | self.vertical_lines= VerticalDoubleLine(self, self.base.subplot,color='grey', |
---|
[c5874f2] | 67 | zorder=zorder, |
---|
| 68 | y= self.ymax, |
---|
| 69 | x= self.xmax, |
---|
| 70 | center_x= self.center_x, |
---|
| 71 | center_y= self.center_y) |
---|
| 72 | self.vertical_lines.qmax = self.qmax |
---|
| 73 | |
---|
| 74 | self.center= PointInteractor(self, self.base.subplot,color='grey', |
---|
| 75 | zorder=zorder, |
---|
| 76 | center_x= self.center_x, |
---|
| 77 | center_y= self.center_y) |
---|
| 78 | ## Save the name of the slicer panel associate with this slicer |
---|
| 79 | self.panel_name="" |
---|
| 80 | ## Update and post slicer parameters |
---|
| 81 | self.update() |
---|
| 82 | self._post_data() |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | def clear(self): |
---|
| 86 | """ |
---|
[83f4445] | 87 | Clear the slicer and all connected events related to this slicer |
---|
[c5874f2] | 88 | """ |
---|
| 89 | self.clear_markers() |
---|
| 90 | self.horizontal_lines.clear() |
---|
| 91 | self.vertical_lines.clear() |
---|
| 92 | self.center.clear() |
---|
| 93 | self.base.connect.clearall() |
---|
| 94 | #self.base.Unbind(EVT_SLICER_PARS) |
---|
[83f4445] | 95 | |
---|
[c5874f2] | 96 | def update(self): |
---|
| 97 | """ |
---|
[83f4445] | 98 | Respond to changes in the model by recalculating the profiles and |
---|
| 99 | resetting the widgets. |
---|
[c5874f2] | 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 | """ |
---|
[83f4445] | 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 |
---|
[c5874f2] | 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 |
---|
[83f4445] | 153 | return out |
---|
| 154 | |
---|
[c5874f2] | 155 | def moveend(self, ev): |
---|
| 156 | """ |
---|
[83f4445] | 157 | After a dragging motion this function is called to compute |
---|
| 158 | the error and the sum of pixel of a given data 2D |
---|
[c5874f2] | 159 | """ |
---|
| 160 | self.base.thaw_axes() |
---|
| 161 | ## post |
---|
| 162 | self._post_data() |
---|
| 163 | |
---|
| 164 | def restore(self): |
---|
| 165 | """ |
---|
| 166 | Restore the roughness for this layer. |
---|
| 167 | """ |
---|
| 168 | self.horizontal_lines.restore() |
---|
| 169 | self.vertical_lines.restore() |
---|
| 170 | self.center.restore() |
---|
| 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 | def set_cursor(self, x, y): |
---|
| 179 | pass |
---|
| 180 | |
---|
| 181 | def get_params(self): |
---|
| 182 | """ |
---|
[83f4445] | 183 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
| 184 | |
---|
| 185 | :return params: the dictionary created |
---|
| 186 | |
---|
[c5874f2] | 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 | return params |
---|
| 194 | |
---|
| 195 | def get_mask(self): |
---|
| 196 | """ |
---|
[83f4445] | 197 | return mask as a result of boxcut |
---|
[c5874f2] | 198 | """ |
---|
| 199 | mask = self.mask |
---|
| 200 | return mask |
---|
| 201 | |
---|
| 202 | def set_params(self, params): |
---|
| 203 | """ |
---|
[83f4445] | 204 | Receive a dictionary and reset the slicer with values contained |
---|
| 205 | in the values of the dictionary. |
---|
| 206 | |
---|
| 207 | :param params: a dictionary containing name of slicer parameters and |
---|
| 208 | values the user assigned to the slicer. |
---|
[c5874f2] | 209 | """ |
---|
| 210 | x_max = math.fabs(params["Width"] )/2 |
---|
| 211 | y_max = math.fabs(params["Height"] )/2 |
---|
| 212 | |
---|
| 213 | self.center_x=params["center_x"] |
---|
| 214 | self.center_y=params["center_y"] |
---|
| 215 | #update the slicer given values of params |
---|
| 216 | self.center.update(center_x=self.center_x,center_y=self.center_y) |
---|
| 217 | self.horizontal_lines.update(center= self.center, |
---|
| 218 | width=x_max, |
---|
| 219 | height=y_max) |
---|
| 220 | self.vertical_lines.update(center= self.center, |
---|
| 221 | width=x_max, |
---|
| 222 | height=y_max) |
---|
| 223 | #compute the new error and sum given values of params |
---|
| 224 | self._post_data() |
---|
| 225 | |
---|
| 226 | def freeze_axes(self): |
---|
| 227 | self.base.freeze_axes() |
---|
| 228 | |
---|
| 229 | def thaw_axes(self): |
---|
| 230 | self.base.thaw_axes() |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | def draw(self): |
---|
| 234 | self.base.update() |
---|
| 235 | |
---|
| 236 | class inner_BoxMask(BoxMask): |
---|
| 237 | def __call__(self): |
---|
| 238 | self.base.data.mask=(self._post_data()==False) |
---|
| 239 | |
---|