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