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