[78ed1ad] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | import math |
---|
| 5 | import wx |
---|
[eba08f1a] | 6 | from copy import deepcopy |
---|
| 7 | from BaseInteractor import _BaseInteractor |
---|
[18eba35] | 8 | from sans.guicomm.events import SlicerParamUpdateEvent,EVT_SLICER_PARS,StatusEvent |
---|
[eba08f1a] | 9 | |
---|
| 10 | |
---|
[78ed1ad] | 11 | class BoxSum(_BaseInteractor): |
---|
| 12 | """ |
---|
[eba08f1a] | 13 | Boxsum Class: determine 2 rectangular area to compute the sum of pixel of |
---|
| 14 | a Data. |
---|
| 15 | Uses PointerInteractor , VerticalDoubleLine,HorizontalDoubleLine. |
---|
| 16 | @param zorder: Artists with lower zorder values are drawn first. |
---|
| 17 | @param x_min: the minimum value of the x coordinate |
---|
| 18 | @param x_max: the maximum value of the x coordinate |
---|
| 19 | @param y_min: the minimum value of the y coordinate |
---|
| 20 | @param y_max: the maximum value of the y coordinate |
---|
| 21 | |
---|
[78ed1ad] | 22 | """ |
---|
[eba08f1a] | 23 | def __init__(self,base,axes,color='black', zorder=3, x_min=0.008, |
---|
| 24 | x_max=0.008, y_min=0.0025, y_max=0.0025): |
---|
[78ed1ad] | 25 | |
---|
| 26 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
[eba08f1a] | 27 | ## class initialization |
---|
| 28 | ## list of Boxsmun markers |
---|
[78ed1ad] | 29 | self.markers = [] |
---|
| 30 | self.axes = axes |
---|
[eba08f1a] | 31 | ## connect the artist for the motion |
---|
[78ed1ad] | 32 | self.connect = self.base.connect |
---|
[eba08f1a] | 33 | ## when qmax is reached the selected line is reset the its previous value |
---|
| 34 | self.qmax = min(self.base.data2D.xmax, self.base.data2D.xmin) |
---|
| 35 | ## Define the boxsum limits |
---|
[1f3655a] | 36 | self.xmin= -1* 0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin)) |
---|
| 37 | self.ymin= -1* 0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin)) |
---|
[eba08f1a] | 38 | |
---|
[1f3655a] | 39 | self.xmax= 0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin)) |
---|
| 40 | self.ymax= 0.5*min(math.fabs(self.base.data2D.xmax),math.fabs( self.base.data2D.xmin)) |
---|
[eba08f1a] | 41 | ## center of the boxSum |
---|
[1f3655a] | 42 | self.center_x= 0.0002 |
---|
| 43 | self.center_y= 0.0003 |
---|
[78ed1ad] | 44 | ## Number of points on the plot |
---|
| 45 | self.nbins = 20 |
---|
[eba08f1a] | 46 | ## Define initial result the summation |
---|
[78ed1ad] | 47 | self.count=0 |
---|
| 48 | self.error=0 |
---|
[eba08f1a] | 49 | ## Flag to determine if the current figure has moved |
---|
| 50 | ## set to False == no motion , set to True== motion |
---|
[2a3a890] | 51 | self.has_move= False |
---|
[eba08f1a] | 52 | ## Create Boxsum edges |
---|
| 53 | self.horizontal_lines= HorizontalDoubleLine(self, self.base.subplot,color='blue', |
---|
[2a3a890] | 54 | zorder=zorder, |
---|
[78ed1ad] | 55 | y= self.ymax, |
---|
[2a3a890] | 56 | x= self.xmax, |
---|
[78ed1ad] | 57 | center_x= self.center_x, |
---|
| 58 | center_y= self.center_y) |
---|
[2a3a890] | 59 | self.horizontal_lines.qmax = self.qmax |
---|
[78ed1ad] | 60 | |
---|
[eba08f1a] | 61 | self.vertical_lines= VerticalDoubleLine(self, self.base.subplot,color='black', |
---|
[2a3a890] | 62 | zorder=zorder, |
---|
| 63 | y= self.ymax, |
---|
| 64 | x= self.xmax, |
---|
[78ed1ad] | 65 | center_x= self.center_x, |
---|
| 66 | center_y= self.center_y) |
---|
[2a3a890] | 67 | self.vertical_lines.qmax = self.qmax |
---|
[eba08f1a] | 68 | |
---|
[2a3a890] | 69 | self.center= PointInteractor(self, self.base.subplot,color='grey', |
---|
| 70 | zorder=zorder, |
---|
| 71 | center_x= self.center_x, |
---|
| 72 | center_y= self.center_y) |
---|
[eba08f1a] | 73 | ## Save the name of the slicer panel associate with this slicer |
---|
| 74 | self.panel_name="" |
---|
| 75 | ## Update and post slicer parameters |
---|
[78ed1ad] | 76 | self.update() |
---|
[92c2345] | 77 | self._post_data() |
---|
[eba08f1a] | 78 | ## Bind to slice parameter events |
---|
[18eba35] | 79 | self.base.Bind(EVT_SLICER_PARS, self._onEVT_SLICER_PARS) |
---|
[6d920cd] | 80 | |
---|
[eba08f1a] | 81 | |
---|
[0bd2cd8] | 82 | def set_panel_name(self, name): |
---|
[eba08f1a] | 83 | """ |
---|
| 84 | Store the name of the panel associated to this slicer |
---|
| 85 | @param name: the name of this panel |
---|
| 86 | """ |
---|
[0bd2cd8] | 87 | self.panel_name= name |
---|
[6d920cd] | 88 | |
---|
| 89 | |
---|
[78ed1ad] | 90 | def _onEVT_SLICER_PARS(self, event): |
---|
[eba08f1a] | 91 | """ |
---|
| 92 | receive an event containing parameters values to reset the slicer |
---|
| 93 | @param event: event of type SlicerParameterEvent with params as |
---|
| 94 | attribute |
---|
| 95 | """ |
---|
| 96 | ## Post e message to declare what kind of event has being received |
---|
[18eba35] | 97 | wx.PostEvent(self.base.parent, StatusEvent(status="Boxsum._onEVT_SLICER_PARS")) |
---|
[78ed1ad] | 98 | event.Skip() |
---|
[eba08f1a] | 99 | ## reset the slicer with the values contains the event.params dictionary |
---|
[78ed1ad] | 100 | if event.type == self.__class__.__name__: |
---|
[92c2345] | 101 | self.set_params(event.params) |
---|
[78ed1ad] | 102 | self.base.update() |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | def set_layer(self, n): |
---|
[eba08f1a] | 106 | """ |
---|
| 107 | Allow adding plot to the same panel |
---|
| 108 | @param n: the number of layer |
---|
| 109 | """ |
---|
[78ed1ad] | 110 | self.layernum = n |
---|
| 111 | self.update() |
---|
| 112 | |
---|
[eba08f1a] | 113 | |
---|
[78ed1ad] | 114 | def clear(self): |
---|
[eba08f1a] | 115 | """ |
---|
| 116 | Clear the slicer and all connected events related to this slicer |
---|
| 117 | """ |
---|
[78ed1ad] | 118 | self.clear_markers() |
---|
[2a3a890] | 119 | self.horizontal_lines.clear() |
---|
| 120 | self.vertical_lines.clear() |
---|
| 121 | self.center.clear() |
---|
[18eba35] | 122 | self.base.connect.clearall() |
---|
| 123 | self.base.Unbind(EVT_SLICER_PARS) |
---|
[78ed1ad] | 124 | |
---|
[eba08f1a] | 125 | |
---|
[78ed1ad] | 126 | def update(self): |
---|
| 127 | """ |
---|
[eba08f1a] | 128 | Respond to changes in the model by recalculating the profiles and |
---|
| 129 | resetting the widgets. |
---|
[78ed1ad] | 130 | """ |
---|
[eba08f1a] | 131 | ## check if the center point has moved and update the figure accordingly |
---|
[2a3a890] | 132 | if self.center.has_move: |
---|
| 133 | self.center.update() |
---|
| 134 | self.horizontal_lines.update( center= self.center) |
---|
| 135 | self.vertical_lines.update( center= self.center) |
---|
[eba08f1a] | 136 | ## check if the horizontal lines have moved and update the figure accordingly |
---|
[2a3a890] | 137 | if self.horizontal_lines.has_move: |
---|
| 138 | self.horizontal_lines.update() |
---|
| 139 | self.vertical_lines.update(y1=self.horizontal_lines.y1, |
---|
| 140 | y2=self.horizontal_lines.y2, |
---|
| 141 | height= self.horizontal_lines.half_height ) |
---|
[eba08f1a] | 142 | ## check if the vertical lines have moved and update the figure accordingly |
---|
[2a3a890] | 143 | if self.vertical_lines.has_move: |
---|
| 144 | self.vertical_lines.update() |
---|
| 145 | self.horizontal_lines.update(x1=self.vertical_lines.x1, |
---|
| 146 | x2=self.vertical_lines.x2, |
---|
| 147 | width=self.vertical_lines.half_width) |
---|
[78ed1ad] | 148 | |
---|
[2a3a890] | 149 | |
---|
[78ed1ad] | 150 | def save(self, ev): |
---|
| 151 | """ |
---|
| 152 | Remember the roughness for this layer and the next so that we |
---|
| 153 | can restore on Esc. |
---|
| 154 | """ |
---|
| 155 | self.base.freeze_axes() |
---|
[2a3a890] | 156 | self.horizontal_lines.save(ev) |
---|
| 157 | self.vertical_lines.save(ev) |
---|
| 158 | self.center.save(ev) |
---|
| 159 | |
---|
[78ed1ad] | 160 | def _post_data(self): |
---|
[eba08f1a] | 161 | """ |
---|
| 162 | Get the limits of the boxsum and compute the sum of the pixel |
---|
| 163 | contained in that region and the error on that sum |
---|
| 164 | """ |
---|
| 165 | ## Data 2D for which the pixel will be summed |
---|
[78ed1ad] | 166 | data = self.base.data2D |
---|
[eba08f1a] | 167 | ## the region of the summation |
---|
[ee69839] | 168 | x_min= self.horizontal_lines.x2 |
---|
| 169 | x_max= self.horizontal_lines.x1 |
---|
[54cc36a] | 170 | y_min= self.vertical_lines.y2 |
---|
| 171 | y_max= self.vertical_lines.y1 |
---|
[eba08f1a] | 172 | ##computation of the sum and its error |
---|
| 173 | from DataLoader.manipulations import Boxavg |
---|
[78ed1ad] | 174 | box = Boxavg (x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max) |
---|
[92c2345] | 175 | self.count, self.error = box(self.base.data2D) |
---|
[eba08f1a] | 176 | |
---|
[54cc36a] | 177 | |
---|
[78ed1ad] | 178 | def moveend(self, ev): |
---|
[eba08f1a] | 179 | """ |
---|
| 180 | After a dragging motion this function is called to compute |
---|
| 181 | the error and the sum of pixel of a given data 2D |
---|
| 182 | """ |
---|
[78ed1ad] | 183 | self.base.thaw_axes() |
---|
[eba08f1a] | 184 | ## compute error an d sum of data's pixel |
---|
[54cc36a] | 185 | self._post_data() |
---|
[eba08f1a] | 186 | ## Create and event ( posted to guiframe)that set the |
---|
| 187 | ##current slicer parameter to a panel of name self.panel_name |
---|
[54cc36a] | 188 | self.type= self.__class__.__name__ |
---|
| 189 | params= self.get_params() |
---|
[0bd2cd8] | 190 | event = SlicerParamUpdateEvent(type=self.type, params=params, |
---|
| 191 | panel_name= self.panel_name) |
---|
| 192 | wx.PostEvent(self.base.parent, event) |
---|
[92c2345] | 193 | |
---|
[78ed1ad] | 194 | |
---|
| 195 | def restore(self): |
---|
| 196 | """ |
---|
| 197 | Restore the roughness for this layer. |
---|
| 198 | """ |
---|
[2a3a890] | 199 | self.horizontal_lines.restore() |
---|
| 200 | self.vertical_lines.restore() |
---|
| 201 | self.center.restore() |
---|
[eba08f1a] | 202 | |
---|
| 203 | |
---|
[78ed1ad] | 204 | def move(self, x, y, ev): |
---|
| 205 | """ |
---|
| 206 | Process move to a new position, making sure that the move is allowed. |
---|
| 207 | """ |
---|
[2a3a890] | 208 | pass |
---|
| 209 | |
---|
[eba08f1a] | 210 | |
---|
[78ed1ad] | 211 | def set_cursor(self, x, y): |
---|
| 212 | pass |
---|
| 213 | |
---|
[eba08f1a] | 214 | |
---|
[78ed1ad] | 215 | def get_params(self): |
---|
[eba08f1a] | 216 | """ |
---|
| 217 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
| 218 | @return params: the dictionary created |
---|
| 219 | """ |
---|
[78ed1ad] | 220 | params = {} |
---|
[ee69839] | 221 | params["Width"] = math.fabs(self.vertical_lines.half_width)*2 |
---|
| 222 | params["Height"] = math.fabs(self.horizontal_lines.half_height)*2 |
---|
[2a3a890] | 223 | params["center_x"] = self.center.x |
---|
| 224 | params["center_y"] =self.center.y |
---|
[0f6d05f8] | 225 | params["count"] = self.count |
---|
| 226 | params["errors"]= self.error |
---|
[78ed1ad] | 227 | return params |
---|
| 228 | |
---|
[92c2345] | 229 | |
---|
| 230 | def get_result(self): |
---|
| 231 | """ |
---|
| 232 | return the result of box summation |
---|
| 233 | """ |
---|
| 234 | result={} |
---|
| 235 | result["count"] = self.count |
---|
| 236 | result["error"] = self.error |
---|
| 237 | return result |
---|
| 238 | |
---|
| 239 | |
---|
[78ed1ad] | 240 | def set_params(self, params): |
---|
[eba08f1a] | 241 | """ |
---|
| 242 | Receive a dictionary and reset the slicer with values contained |
---|
| 243 | in the values of the dictionary. |
---|
| 244 | @param params: a dictionary containing name of slicer parameters and |
---|
| 245 | values the user assigned to the slicer. |
---|
| 246 | """ |
---|
[ee69839] | 247 | x_max = math.fabs(params["Width"] )/2 |
---|
| 248 | y_max = math.fabs(params["Height"] )/2 |
---|
[78ed1ad] | 249 | |
---|
[2a3a890] | 250 | self.center_x=params["center_x"] |
---|
| 251 | self.center_y=params["center_y"] |
---|
[eba08f1a] | 252 | #update the slicer given values of params |
---|
[2a3a890] | 253 | self.center.update(center_x=self.center_x,center_y=self.center_y) |
---|
| 254 | self.horizontal_lines.update(center= self.center, |
---|
| 255 | width=x_max, |
---|
| 256 | height=y_max) |
---|
| 257 | self.vertical_lines.update(center= self.center, |
---|
| 258 | width=x_max, |
---|
| 259 | height=y_max) |
---|
[eba08f1a] | 260 | #compute the new error and sum given values of params |
---|
[78ed1ad] | 261 | self._post_data() |
---|
[2a3a890] | 262 | |
---|
[18eba35] | 263 | |
---|
[2a3a890] | 264 | |
---|
[78ed1ad] | 265 | def freeze_axes(self): |
---|
| 266 | self.base.freeze_axes() |
---|
| 267 | |
---|
[eba08f1a] | 268 | |
---|
[78ed1ad] | 269 | def thaw_axes(self): |
---|
| 270 | self.base.thaw_axes() |
---|
| 271 | |
---|
[eba08f1a] | 272 | |
---|
[78ed1ad] | 273 | def draw(self): |
---|
| 274 | self.base.draw() |
---|
[eba08f1a] | 275 | |
---|
| 276 | |
---|
| 277 | |
---|
[2a3a890] | 278 | class PointInteractor(_BaseInteractor): |
---|
[78ed1ad] | 279 | """ |
---|
[eba08f1a] | 280 | Draw a point that can be dragged with the marker. |
---|
| 281 | this class controls the motion the center of the BoxSum |
---|
[78ed1ad] | 282 | """ |
---|
[2a3a890] | 283 | def __init__(self,base,axes,color='black', zorder=5, |
---|
[9b05b2f] | 284 | center_x= 0.0, |
---|
| 285 | center_y= 0.0): |
---|
[78ed1ad] | 286 | |
---|
| 287 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
[eba08f1a] | 288 | ## Initialization the class |
---|
[78ed1ad] | 289 | self.markers = [] |
---|
| 290 | self.axes = axes |
---|
[eba08f1a] | 291 | # center coordinates |
---|
[2a3a890] | 292 | self.x = center_x |
---|
| 293 | self.y = center_y |
---|
[eba08f1a] | 294 | ## saved value of the center coordinates |
---|
[2a3a890] | 295 | self.save_x = center_x |
---|
| 296 | self.save_y = center_y |
---|
[eba08f1a] | 297 | ## Create a marker |
---|
[2a3a890] | 298 | try: |
---|
| 299 | self.center_marker = self.axes.plot([self.x],[self.y], linestyle='', |
---|
| 300 | marker='s', markersize=10, |
---|
| 301 | color=self.color, alpha=0.6, |
---|
| 302 | pickradius=5, label="pick", |
---|
| 303 | zorder=zorder, # Prefer this to other lines |
---|
| 304 | visible=True)[0] |
---|
| 305 | except: |
---|
| 306 | self.center_marker = self.axes.plot([self.x],[self.y], linestyle='', |
---|
| 307 | marker='s', markersize=10, |
---|
| 308 | color=self.color, alpha=0.6, |
---|
| 309 | label="pick", |
---|
| 310 | visible=True)[0] |
---|
| 311 | message = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n" |
---|
| 312 | message += "Get the SVN version that is at least as recent as June 1, 2007" |
---|
[eba08f1a] | 313 | owner=self.base.base.parent |
---|
| 314 | wx.PostEvent(owner, StatusEvent(status="AnnulusSlicer: %s"%message)) |
---|
[2a3a890] | 315 | |
---|
[eba08f1a] | 316 | ## Draw a point |
---|
[2a3a890] | 317 | self.center = self.axes.plot([self.x],[self.y], |
---|
[78ed1ad] | 318 | linestyle='-', marker='', |
---|
| 319 | color=self.color, |
---|
| 320 | visible=True)[0] |
---|
[eba08f1a] | 321 | ## Flag to determine the motion this point |
---|
| 322 | self.has_move=False |
---|
| 323 | ## connecting the marker to allow them to move |
---|
[2a3a890] | 324 | self.connect_markers([self.center_marker]) |
---|
[eba08f1a] | 325 | ## Update the figure |
---|
[78ed1ad] | 326 | self.update() |
---|
[54cc36a] | 327 | |
---|
[eba08f1a] | 328 | |
---|
[78ed1ad] | 329 | def set_layer(self, n): |
---|
[eba08f1a] | 330 | """ |
---|
| 331 | Allow adding plot to the same panel |
---|
| 332 | @param n: the number of layer |
---|
| 333 | """ |
---|
[78ed1ad] | 334 | self.layernum = n |
---|
| 335 | self.update() |
---|
| 336 | |
---|
[eba08f1a] | 337 | |
---|
[78ed1ad] | 338 | def clear(self): |
---|
[eba08f1a] | 339 | """ |
---|
| 340 | Clear this figure and its markers |
---|
| 341 | """ |
---|
[78ed1ad] | 342 | self.clear_markers() |
---|
| 343 | try: |
---|
[2a3a890] | 344 | self.center.remove() |
---|
[54cc36a] | 345 | self.center_marker.remove() |
---|
[78ed1ad] | 346 | except: |
---|
| 347 | # Old version of matplotlib |
---|
| 348 | for item in range(len(self.axes.lines)): |
---|
| 349 | del self.axes.lines[0] |
---|
[eba08f1a] | 350 | |
---|
[78ed1ad] | 351 | |
---|
[2a3a890] | 352 | def update(self, center_x=None,center_y=None): |
---|
[78ed1ad] | 353 | """ |
---|
[eba08f1a] | 354 | Draw the new roughness on the graph. |
---|
[78ed1ad] | 355 | """ |
---|
[2a3a890] | 356 | if center_x !=None: self.x= center_x |
---|
| 357 | if center_y !=None: self.y= center_y |
---|
| 358 | |
---|
| 359 | self.center_marker.set(xdata=[self.x], ydata=[self.y]) |
---|
| 360 | self.center.set(xdata=[self.x], ydata=[self.y]) |
---|
| 361 | |
---|
[78ed1ad] | 362 | |
---|
| 363 | def save(self, ev): |
---|
| 364 | """ |
---|
| 365 | Remember the roughness for this layer and the next so that we |
---|
| 366 | can restore on Esc. |
---|
| 367 | """ |
---|
[2a3a890] | 368 | self.save_x= self.x |
---|
[78ed1ad] | 369 | self.save_y= self.y |
---|
| 370 | self.base.freeze_axes() |
---|
| 371 | |
---|
[eba08f1a] | 372 | |
---|
[78ed1ad] | 373 | def moveend(self, ev): |
---|
| 374 | |
---|
| 375 | self.has_move=False |
---|
| 376 | self.base.moveend(ev) |
---|
| 377 | |
---|
| 378 | def restore(self): |
---|
| 379 | """ |
---|
| 380 | Restore the roughness for this layer. |
---|
| 381 | """ |
---|
[9b05b2f] | 382 | self.y= self.save_y |
---|
[2a3a890] | 383 | self.x= self.save_x |
---|
| 384 | |
---|
[eba08f1a] | 385 | |
---|
[78ed1ad] | 386 | def move(self, x, y, ev): |
---|
| 387 | """ |
---|
| 388 | Process move to a new position, making sure that the move is allowed. |
---|
| 389 | """ |
---|
[2a3a890] | 390 | self.x= x |
---|
| 391 | self.y= y |
---|
| 392 | |
---|
[78ed1ad] | 393 | self.has_move=True |
---|
| 394 | self.base.base.update() |
---|
| 395 | |
---|
[eba08f1a] | 396 | |
---|
[78ed1ad] | 397 | def set_cursor(self, x, y): |
---|
| 398 | self.move(x, y, None) |
---|
| 399 | self.update() |
---|
| 400 | |
---|
| 401 | |
---|
| 402 | def get_params(self): |
---|
| 403 | params = {} |
---|
[2a3a890] | 404 | params["x"] = self.x |
---|
| 405 | params["y"] = self.y |
---|
[78ed1ad] | 406 | return params |
---|
| 407 | |
---|
[eba08f1a] | 408 | |
---|
[78ed1ad] | 409 | def set_params(self, params): |
---|
[2a3a890] | 410 | center_x = params["x"] |
---|
| 411 | center_y = params["y"] |
---|
| 412 | self.update(center_x=center_x,center_y=center_y) |
---|
| 413 | |
---|
[78ed1ad] | 414 | |
---|
[eba08f1a] | 415 | class VerticalDoubleLine(_BaseInteractor): |
---|
[78ed1ad] | 416 | """ |
---|
[eba08f1a] | 417 | Draw 2 vertical lines moving in opposite direction and centered on |
---|
| 418 | a point (PointInteractor) |
---|
[78ed1ad] | 419 | """ |
---|
[2a3a890] | 420 | def __init__(self,base,axes,color='black', zorder=5, x=0.5,y=0.5, |
---|
| 421 | center_x= 0.0, |
---|
| 422 | center_y= 0.0): |
---|
[78ed1ad] | 423 | |
---|
| 424 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
[eba08f1a] | 425 | ## Initialization the class |
---|
[78ed1ad] | 426 | self.markers = [] |
---|
| 427 | self.axes = axes |
---|
[eba08f1a] | 428 | ## Center coordinates |
---|
[2a3a890] | 429 | self.center_x = center_x |
---|
| 430 | self.center_y = center_y |
---|
[eba08f1a] | 431 | ## defined end points vertical lignes and their saved values |
---|
[2a3a890] | 432 | self.y1 = y + self.center_y |
---|
| 433 | self.save_y1= self.y1 |
---|
| 434 | |
---|
| 435 | delta= self.y1- self.center_y |
---|
| 436 | self.y2= self.center_y - delta |
---|
| 437 | self.save_y2= self.y2 |
---|
| 438 | |
---|
| 439 | self.x1 = x + self.center_x |
---|
| 440 | self.save_x1 = self.x1 |
---|
| 441 | |
---|
| 442 | delta= self.x1- self.center_x |
---|
| 443 | self.x2= self.center_x - delta |
---|
| 444 | self.save_x2 = self.x2 |
---|
[eba08f1a] | 445 | ## save the color of the line |
---|
| 446 | self.color = color |
---|
| 447 | ## the height of the rectangle |
---|
[2a3a890] | 448 | self.half_height= math.fabs(y) |
---|
| 449 | self.save_half_height= math.fabs(y) |
---|
[eba08f1a] | 450 | ## the with of the rectangle |
---|
[ee69839] | 451 | self.half_width= math.fabs(self.x1- self.x2)/2 |
---|
| 452 | self.save_half_width=math.fabs(self.x1- self.x2)/2 |
---|
[eba08f1a] | 453 | ## Create marker |
---|
[2a3a890] | 454 | try: |
---|
| 455 | self.right_marker = self.axes.plot([self.x1],[0], linestyle='', |
---|
| 456 | marker='s', markersize=10, |
---|
| 457 | color=self.color, alpha=0.6, |
---|
| 458 | pickradius=5, label="pick", |
---|
| 459 | zorder=zorder, # Prefer this to other lines |
---|
| 460 | visible=True)[0] |
---|
| 461 | except: |
---|
| 462 | self.right_marker = self.axes.plot([self.x1],[0], linestyle='', |
---|
| 463 | marker='s', markersize=10, |
---|
| 464 | color=self.color, alpha=0.6, |
---|
| 465 | label="pick", |
---|
| 466 | visible=True)[0] |
---|
| 467 | message = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n" |
---|
| 468 | message += "Get the SVN version that is at least as recent as June 1, 2007" |
---|
[eba08f1a] | 469 | owner=self.base.base.parent |
---|
| 470 | wx.PostEvent(owner, StatusEvent(status="AnnulusSlicer: %s"%message)) |
---|
[2a3a890] | 471 | |
---|
[eba08f1a] | 472 | ## define the left and right lines of the rectangle |
---|
[2a3a890] | 473 | self.right_line = self.axes.plot([self.x1,self.x1],[self.y1,self.y2], |
---|
[78ed1ad] | 474 | linestyle='-', marker='', |
---|
| 475 | color=self.color, |
---|
| 476 | visible=True)[0] |
---|
[2a3a890] | 477 | self.left_line = self.axes.plot([self.x2,self.x2],[self.y1,self.y2], |
---|
| 478 | linestyle='-', marker='', |
---|
| 479 | color=self.color, |
---|
| 480 | visible=True)[0] |
---|
[eba08f1a] | 481 | ## Flag to determine if the lines have moved |
---|
| 482 | self.has_move=False |
---|
| 483 | ## connection the marker and draw the pictures |
---|
[2a3a890] | 484 | self.connect_markers([self.right_marker]) |
---|
[78ed1ad] | 485 | self.update() |
---|
| 486 | |
---|
[2a3a890] | 487 | |
---|
[78ed1ad] | 488 | def set_layer(self, n): |
---|
[eba08f1a] | 489 | """ |
---|
| 490 | Allow adding plot to the same panel |
---|
| 491 | @param n: the number of layer |
---|
| 492 | """ |
---|
[78ed1ad] | 493 | self.layernum = n |
---|
| 494 | self.update() |
---|
| 495 | |
---|
[eba08f1a] | 496 | |
---|
[78ed1ad] | 497 | def clear(self): |
---|
[eba08f1a] | 498 | """ |
---|
[6c0568b] | 499 | Clear this slicer and its markers |
---|
[eba08f1a] | 500 | """ |
---|
[78ed1ad] | 501 | self.clear_markers() |
---|
| 502 | try: |
---|
[54cc36a] | 503 | self.right_marker.remove() |
---|
[2a3a890] | 504 | self.right_line.remove() |
---|
| 505 | self.left_line.remove() |
---|
[78ed1ad] | 506 | except: |
---|
| 507 | # Old version of matplotlib |
---|
| 508 | for item in range(len(self.axes.lines)): |
---|
| 509 | del self.axes.lines[0] |
---|
[eba08f1a] | 510 | |
---|
[2a3a890] | 511 | |
---|
| 512 | def update(self,x1=None,x2=None, y1=None,y2=None,width=None, height=None, center=None): |
---|
[78ed1ad] | 513 | """ |
---|
[eba08f1a] | 514 | Draw the new roughness on the graph. |
---|
| 515 | @param x1: new maximum value of x coordinates |
---|
| 516 | @param x2: new minimum value of x coordinates |
---|
| 517 | @param y1: new maximum value of y coordinates |
---|
| 518 | @param y2: new minimum value of y coordinates |
---|
| 519 | @param width: is the width of the new rectangle |
---|
| 520 | @param height: is the height of the new rectangle |
---|
| 521 | @param center: provided x, y coordinates of the center point |
---|
[78ed1ad] | 522 | """ |
---|
[eba08f1a] | 523 | ## save the new height, witdh of the rectangle if given as a param |
---|
[2a3a890] | 524 | if width!=None: |
---|
| 525 | self.half_width= width |
---|
| 526 | if height!=None: |
---|
| 527 | self.half_height= height |
---|
[eba08f1a] | 528 | ## If new center coordinates are given draw the rectangle |
---|
| 529 | ##given these value |
---|
[2a3a890] | 530 | if center!=None: |
---|
| 531 | self.center_x= center.x |
---|
| 532 | self.center_y= center.y |
---|
[eba08f1a] | 533 | |
---|
[2a3a890] | 534 | self.x1 = self.half_width + self.center_x |
---|
| 535 | self.x2= -self.half_width + self.center_x |
---|
| 536 | |
---|
| 537 | self.y1 = self.half_height + self.center_y |
---|
| 538 | self.y2= -self.half_height + self.center_y |
---|
| 539 | |
---|
| 540 | self.right_marker.set(xdata=[self.x1],ydata=[self.center_y]) |
---|
| 541 | self.right_line.set(xdata=[self.x1,self.x1], ydata=[self.y1,self.y2]) |
---|
| 542 | self.left_line.set(xdata=[self.x2,self.x2], ydata=[self.y1,self.y2]) |
---|
| 543 | return |
---|
[eba08f1a] | 544 | ## if x1, y1, y2, y3 are given draw the rectangle with this value |
---|
[2a3a890] | 545 | if x1 !=None: |
---|
| 546 | self.x1= x1 |
---|
| 547 | if x2 !=None: |
---|
| 548 | self.x2= x2 |
---|
| 549 | if y1 !=None: |
---|
| 550 | self.y1= y1 |
---|
| 551 | if y2 !=None: |
---|
| 552 | self.y2= y2 |
---|
[eba08f1a] | 553 | ## Draw 2 vertical lines and a marker |
---|
[2a3a890] | 554 | self.right_marker.set(xdata=[self.x1],ydata=[self.center_y]) |
---|
| 555 | self.right_line.set(xdata=[self.x1,self.x1], ydata=[self.y1,self.y2]) |
---|
| 556 | self.left_line.set(xdata=[self.x2,self.x2], ydata=[self.y1,self.y2]) |
---|
[78ed1ad] | 557 | |
---|
| 558 | |
---|
| 559 | def save(self, ev): |
---|
| 560 | """ |
---|
| 561 | Remember the roughness for this layer and the next so that we |
---|
| 562 | can restore on Esc. |
---|
| 563 | """ |
---|
[2a3a890] | 564 | self.save_x2= self.x2 |
---|
| 565 | self.save_y2= self.y2 |
---|
[78ed1ad] | 566 | |
---|
[2a3a890] | 567 | self.save_x1= self.x1 |
---|
| 568 | self.save_y1= self.y1 |
---|
| 569 | |
---|
| 570 | self.save_half_height= self.half_height |
---|
[ee69839] | 571 | self.save_half_width = self.half_width |
---|
[eba08f1a] | 572 | |
---|
[78ed1ad] | 573 | self.base.freeze_axes() |
---|
| 574 | |
---|
[eba08f1a] | 575 | |
---|
[78ed1ad] | 576 | def moveend(self, ev): |
---|
[eba08f1a] | 577 | """ |
---|
| 578 | After a dragging motion reset the flag self.has_move to False |
---|
| 579 | """ |
---|
[78ed1ad] | 580 | self.has_move=False |
---|
| 581 | self.base.moveend(ev) |
---|
| 582 | |
---|
[eba08f1a] | 583 | |
---|
[78ed1ad] | 584 | def restore(self): |
---|
| 585 | """ |
---|
| 586 | Restore the roughness for this layer. |
---|
| 587 | """ |
---|
[2a3a890] | 588 | self.y2= self.save_y2 |
---|
| 589 | self.x2= self.save_x2 |
---|
| 590 | |
---|
| 591 | self.y1= self.save_y1 |
---|
| 592 | self.x1= self.save_x1 |
---|
[78ed1ad] | 593 | |
---|
[2a3a890] | 594 | self.half_height= self.save_half_height |
---|
| 595 | self.half_width= self.save_half_width |
---|
| 596 | |
---|
[eba08f1a] | 597 | |
---|
[78ed1ad] | 598 | def move(self, x, y, ev): |
---|
| 599 | """ |
---|
| 600 | Process move to a new position, making sure that the move is allowed. |
---|
| 601 | """ |
---|
[2a3a890] | 602 | self.x1= x |
---|
| 603 | delta= self.x1- self.center_x |
---|
| 604 | self.x2= self.center_x - delta |
---|
| 605 | |
---|
[ee69839] | 606 | self.half_width= math.fabs(self.x1-self.x2)/2 |
---|
[eba08f1a] | 607 | |
---|
[78ed1ad] | 608 | self.has_move=True |
---|
| 609 | self.base.base.update() |
---|
| 610 | |
---|
[eba08f1a] | 611 | |
---|
[78ed1ad] | 612 | def set_cursor(self, x, y): |
---|
[eba08f1a] | 613 | """ |
---|
| 614 | Update the figure given x and y |
---|
| 615 | """ |
---|
[78ed1ad] | 616 | self.move(x, y, None) |
---|
| 617 | self.update() |
---|
| 618 | |
---|
| 619 | |
---|
| 620 | def get_params(self): |
---|
[eba08f1a] | 621 | """ |
---|
| 622 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
| 623 | @return params: the dictionary created |
---|
| 624 | """ |
---|
[78ed1ad] | 625 | params = {} |
---|
[2a3a890] | 626 | params["x"] = self.x1 |
---|
| 627 | params["y"] = self.y1 |
---|
| 628 | |
---|
[78ed1ad] | 629 | return params |
---|
| 630 | |
---|
| 631 | def set_params(self, params): |
---|
[eba08f1a] | 632 | """ |
---|
| 633 | Receive a dictionary and reset the slicer with values contained |
---|
| 634 | in the values of the dictionary. |
---|
| 635 | @param params: a dictionary containing name of slicer parameters and |
---|
| 636 | values the user assigned to the slicer. |
---|
| 637 | """ |
---|
[2a3a890] | 638 | x = params["x"] |
---|
| 639 | y = params["y"] |
---|
| 640 | self.update(x=x, y=y, center_x=None,center_y=None) |
---|
| 641 | |
---|
[eba08f1a] | 642 | |
---|
| 643 | class HorizontalDoubleLine(_BaseInteractor): |
---|
[2a3a890] | 644 | """ |
---|
| 645 | Select an annulus through a 2D plot |
---|
| 646 | """ |
---|
| 647 | def __init__(self,base,axes,color='black', zorder=5, x=0.5,y=0.5, |
---|
| 648 | center_x= 0.0, |
---|
| 649 | center_y= 0.0): |
---|
| 650 | |
---|
| 651 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
[eba08f1a] | 652 | ## Initialization the class |
---|
[2a3a890] | 653 | self.markers = [] |
---|
| 654 | self.axes = axes |
---|
[eba08f1a] | 655 | ## Center coordinates |
---|
[2a3a890] | 656 | self.center_x = center_x |
---|
| 657 | self.center_y = center_y |
---|
| 658 | |
---|
| 659 | self.y1 = y + self.center_y |
---|
| 660 | self.save_y1= self.y1 |
---|
| 661 | |
---|
| 662 | delta= self.y1- self.center_y |
---|
| 663 | self.y2= self.center_y - delta |
---|
| 664 | self.save_y2= self.y2 |
---|
| 665 | |
---|
| 666 | self.x1 = x + self.center_x |
---|
| 667 | self.save_x1 = self.x1 |
---|
| 668 | |
---|
| 669 | delta= self.x1- self.center_x |
---|
| 670 | self.x2= self.center_x - delta |
---|
| 671 | self.save_x2 = self.x2 |
---|
| 672 | |
---|
| 673 | self.color=color |
---|
| 674 | |
---|
| 675 | self.half_height= math.fabs(y) |
---|
| 676 | self.save_half_height= math.fabs(y) |
---|
| 677 | |
---|
| 678 | self.half_width= math.fabs(x) |
---|
| 679 | self.save_half_width=math.fabs(x) |
---|
| 680 | |
---|
| 681 | try: |
---|
| 682 | self.top_marker = self.axes.plot([0],[self.y1], linestyle='', |
---|
| 683 | marker='s', markersize=10, |
---|
| 684 | color=self.color, alpha=0.6, |
---|
| 685 | pickradius=5, label="pick", |
---|
| 686 | zorder=zorder, # Prefer this to other lines |
---|
| 687 | visible=True)[0] |
---|
| 688 | except: |
---|
| 689 | self.top_marker = self.axes.plot([0],[self.y1], linestyle='', |
---|
| 690 | marker='s', markersize=10, |
---|
| 691 | color=self.color, alpha=0.6, |
---|
| 692 | label="pick", |
---|
| 693 | visible=True)[0] |
---|
| 694 | message = "\nTHIS PROTOTYPE NEEDS THE LATEST VERSION OF MATPLOTLIB\n" |
---|
| 695 | message += "Get the SVN version that is at least as recent as June 1, 2007" |
---|
[eba08f1a] | 696 | owner=self.base.base.parent |
---|
| 697 | wx.PostEvent(owner, StatusEvent(status="AnnulusSlicer: %s"%message)) |
---|
[2a3a890] | 698 | |
---|
[eba08f1a] | 699 | # Define 2 horizotnal lines |
---|
[2a3a890] | 700 | self.top_line = self.axes.plot([self.x1,-self.x1],[self.y1,self.y1], |
---|
| 701 | linestyle='-', marker='', |
---|
| 702 | color=self.color, |
---|
| 703 | visible=True)[0] |
---|
| 704 | self.bottom_line = self.axes.plot([self.x1,-self.x1],[self.y2,self.y2], |
---|
| 705 | linestyle='-', marker='', |
---|
| 706 | color=self.color, |
---|
| 707 | visible=True)[0] |
---|
[eba08f1a] | 708 | ## Flag to determine if the lines have moved |
---|
[2a3a890] | 709 | self.has_move=False |
---|
[eba08f1a] | 710 | ## connection the marker and draw the pictures |
---|
[2a3a890] | 711 | self.connect_markers([self.top_marker]) |
---|
| 712 | self.update() |
---|
| 713 | |
---|
| 714 | |
---|
| 715 | def set_layer(self, n): |
---|
[eba08f1a] | 716 | """ |
---|
| 717 | Allow adding plot to the same panel |
---|
| 718 | @param n: the number of layer |
---|
| 719 | """ |
---|
[2a3a890] | 720 | self.layernum = n |
---|
| 721 | self.update() |
---|
| 722 | |
---|
[eba08f1a] | 723 | |
---|
[2a3a890] | 724 | def clear(self): |
---|
[eba08f1a] | 725 | """ |
---|
| 726 | Clear this figure and its markers |
---|
| 727 | """ |
---|
[2a3a890] | 728 | self.clear_markers() |
---|
| 729 | try: |
---|
[54cc36a] | 730 | self.top_marker.remove() |
---|
[2a3a890] | 731 | self.bottom_line.remove() |
---|
| 732 | self.top_line.remove() |
---|
| 733 | except: |
---|
| 734 | # Old version of matplotlib |
---|
| 735 | for item in range(len(self.axes.lines)): |
---|
| 736 | del self.axes.lines[0] |
---|
[eba08f1a] | 737 | |
---|
[2a3a890] | 738 | |
---|
| 739 | def update(self,x1=None,x2=None, y1=None,y2=None,width=None,height=None, center=None): |
---|
[78ed1ad] | 740 | """ |
---|
[eba08f1a] | 741 | Draw the new roughness on the graph. |
---|
| 742 | @param x1: new maximum value of x coordinates |
---|
| 743 | @param x2: new minimum value of x coordinates |
---|
| 744 | @param y1: new maximum value of y coordinates |
---|
| 745 | @param y2: new minimum value of y coordinates |
---|
| 746 | @param width: is the width of the new rectangle |
---|
| 747 | @param height: is the height of the new rectangle |
---|
| 748 | @param center: provided x, y coordinates of the center point |
---|
| 749 | """ |
---|
| 750 | ## save the new height, witdh of the rectangle if given as a param |
---|
| 751 | if width != None: |
---|
| 752 | self.half_width = width |
---|
| 753 | if height!= None: |
---|
[2a3a890] | 754 | self.half_height= height |
---|
[eba08f1a] | 755 | ## If new center coordinates are given draw the rectangle |
---|
| 756 | ##given these value |
---|
[2a3a890] | 757 | if center!=None: |
---|
| 758 | self.center_x= center.x |
---|
| 759 | self.center_y= center.y |
---|
| 760 | |
---|
| 761 | self.x1 = self.half_width + self.center_x |
---|
| 762 | self.x2= -self.half_width + self.center_x |
---|
| 763 | |
---|
| 764 | self.y1 = self.half_height + self.center_y |
---|
| 765 | self.y2= -self.half_height + self.center_y |
---|
| 766 | |
---|
| 767 | self.top_marker.set(xdata=[self.center_x],ydata=[self.y1]) |
---|
| 768 | self.top_line.set(xdata=[self.x1,self.x2], ydata=[self.y1,self.y1]) |
---|
| 769 | self.bottom_line.set(xdata=[self.x1,self.x2], ydata=[self.y2,self.y2]) |
---|
| 770 | return |
---|
[eba08f1a] | 771 | ## if x1, y1, y2, y3 are given draw the rectangle with this value |
---|
[2a3a890] | 772 | if x1 !=None: |
---|
| 773 | self.x1= x1 |
---|
| 774 | if x2 !=None: |
---|
| 775 | self.x2= x2 |
---|
| 776 | if y1 !=None: |
---|
| 777 | self.y1= y1 |
---|
| 778 | if y2 !=None: |
---|
| 779 | self.y2= y2 |
---|
[eba08f1a] | 780 | ## Draw 2 vertical lines and a marker |
---|
[2a3a890] | 781 | self.top_marker.set(xdata=[self.center_x],ydata=[self.y1]) |
---|
| 782 | self.top_line.set(xdata=[self.x1,self.x2], ydata=[self.y1,self.y1]) |
---|
| 783 | self.bottom_line.set(xdata=[self.x1,self.x2], ydata=[self.y2,self.y2]) |
---|
| 784 | |
---|
| 785 | |
---|
| 786 | def save(self, ev): |
---|
| 787 | """ |
---|
| 788 | Remember the roughness for this layer and the next so that we |
---|
| 789 | can restore on Esc. |
---|
| 790 | """ |
---|
| 791 | self.save_x2= self.x2 |
---|
| 792 | self.save_y2= self.y2 |
---|
| 793 | |
---|
| 794 | self.save_x1= self.x1 |
---|
| 795 | self.save_y1= self.y1 |
---|
[78ed1ad] | 796 | |
---|
[2a3a890] | 797 | self.save_half_height= self.half_height |
---|
| 798 | self.save_half_width = self.half_width |
---|
| 799 | self.base.freeze_axes() |
---|
[78ed1ad] | 800 | |
---|
[2a3a890] | 801 | |
---|
| 802 | def moveend(self, ev): |
---|
[eba08f1a] | 803 | """ |
---|
| 804 | After a dragging motion reset the flag self.has_move to False |
---|
| 805 | """ |
---|
[2a3a890] | 806 | self.has_move=False |
---|
| 807 | self.base.moveend(ev) |
---|
[eba08f1a] | 808 | |
---|
[2a3a890] | 809 | |
---|
| 810 | def restore(self): |
---|
| 811 | """ |
---|
| 812 | Restore the roughness for this layer. |
---|
| 813 | """ |
---|
| 814 | self.y2= self.save_y2 |
---|
| 815 | self.x2= self.save_x2 |
---|
| 816 | |
---|
| 817 | self.y1= self.save_y1 |
---|
| 818 | self.x1= self.save_x1 |
---|
| 819 | self.half_height= self.save_half_height |
---|
| 820 | self.half_width= self.save_half_width |
---|
| 821 | |
---|
[eba08f1a] | 822 | |
---|
[2a3a890] | 823 | def move(self, x, y, ev): |
---|
| 824 | """ |
---|
| 825 | Process move to a new position, making sure that the move is allowed. |
---|
| 826 | """ |
---|
| 827 | self.y1= y |
---|
| 828 | delta= self.y1- self.center_y |
---|
| 829 | self.y2= self.center_y - delta |
---|
| 830 | self.half_height= math.fabs(self.y1)-self.center_y |
---|
| 831 | self.has_move=True |
---|
| 832 | self.base.base.update() |
---|
| 833 | |
---|
[eba08f1a] | 834 | |
---|
[2a3a890] | 835 | def set_cursor(self, x, y): |
---|
[eba08f1a] | 836 | """ |
---|
| 837 | Update the figure given x and y |
---|
| 838 | """ |
---|
[2a3a890] | 839 | self.move(x, y, None) |
---|
| 840 | self.update() |
---|
| 841 | |
---|
| 842 | |
---|
| 843 | def get_params(self): |
---|
[eba08f1a] | 844 | """ |
---|
| 845 | Store a copy of values of parameters of the slicer into a dictionary. |
---|
| 846 | @return params: the dictionary created |
---|
| 847 | """ |
---|
[2a3a890] | 848 | params = {} |
---|
| 849 | params["x"] = self.x |
---|
| 850 | params["y"] = self.y |
---|
| 851 | return params |
---|
| 852 | |
---|
[eba08f1a] | 853 | |
---|
[2a3a890] | 854 | def set_params(self, params): |
---|
[eba08f1a] | 855 | """ |
---|
| 856 | Receive a dictionary and reset the slicer with values contained |
---|
| 857 | in the values of the dictionary. |
---|
| 858 | @param params: a dictionary containing name of slicer parameters and |
---|
| 859 | values the user assigned to the slicer. |
---|
| 860 | """ |
---|
[2a3a890] | 861 | x = params["x"] |
---|
| 862 | y = params["y"] |
---|
| 863 | self.update(x=x, y=y, center_x=None,center_y=None) |
---|
[eba08f1a] | 864 | |
---|
| 865 | |
---|
[78ed1ad] | 866 | |
---|