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