[78ed1ad] | 1 | #TODO: the line slicer should listen to all 2DREFRESH events, get the data and slice it |
---|
| 2 | # before pushing a new 1D data update. |
---|
| 3 | |
---|
| 4 | # |
---|
| 5 | #TODO: NEED MAJOR REFACTOR |
---|
| 6 | # |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | # Debug printout |
---|
| 10 | #from config import printEVT |
---|
| 11 | from BaseInteractor import _BaseInteractor |
---|
| 12 | from copy import deepcopy |
---|
| 13 | import math |
---|
| 14 | |
---|
| 15 | #from Plotter1D import AddPlotEvent |
---|
| 16 | import SlicerParameters |
---|
| 17 | import wx |
---|
| 18 | |
---|
| 19 | class BoxSum(_BaseInteractor): |
---|
| 20 | """ |
---|
| 21 | Select an annulus through a 2D plot |
---|
| 22 | """ |
---|
| 23 | def __init__(self,base,axes,color='black', zorder=3, x_min=0.0025, x_max=0.0025, y_min=0.0025, y_max=0.0025): |
---|
| 24 | |
---|
| 25 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 26 | self.markers = [] |
---|
| 27 | self.axes = axes |
---|
| 28 | self.qmax = self.base.qmax |
---|
| 29 | self.connect = self.base.connect |
---|
| 30 | |
---|
| 31 | self.xmin= -1* x_min |
---|
| 32 | self.ymin= -1* y_min |
---|
| 33 | self.xmax= x_max |
---|
| 34 | self.ymax= y_max |
---|
| 35 | # center of the figure |
---|
| 36 | self.center_x= 0.0005 |
---|
| 37 | self.center_y= 0.0005 |
---|
| 38 | |
---|
| 39 | ## Number of points on the plot |
---|
| 40 | self.nbins = 20 |
---|
| 41 | self.count=0 |
---|
| 42 | self.error=0 |
---|
| 43 | |
---|
| 44 | self.left_line = VerticalLine(self, self.base.subplot,color='blue', zorder=zorder, |
---|
| 45 | ymin= self.ymin, ymax= self.ymax, |
---|
| 46 | x= self.xmin, |
---|
| 47 | center_x= self.center_x, |
---|
| 48 | center_y= self.center_y) |
---|
| 49 | self.left_line.qmax = self.base.qmax |
---|
| 50 | |
---|
| 51 | self.right_line= VerticalLine(self, self.base.subplot,color='black', zorder=zorder, |
---|
| 52 | ymin= self.ymin, ymax= self.ymax, |
---|
| 53 | x=self.xmax, |
---|
| 54 | center_x= self.center_x, |
---|
| 55 | center_y= self.center_y) |
---|
| 56 | self.right_line.qmax = self.base.qmax |
---|
| 57 | |
---|
| 58 | self.top_line= HorizontalLine(self, self.base.subplot,color='green', zorder=zorder, |
---|
| 59 | y= self.ymax, |
---|
| 60 | xmin= self.xmin, xmax= self.xmax, |
---|
| 61 | center_x= self.center_x, |
---|
| 62 | center_y= self.center_y) |
---|
| 63 | self.top_line.qmax = self.base.qmax |
---|
| 64 | |
---|
| 65 | self.bottom_line= HorizontalLine(self, self.base.subplot,color='red', zorder=zorder, |
---|
| 66 | y =self.ymin, |
---|
| 67 | xmin= self.xmin, xmax= self.xmax, |
---|
| 68 | center_x= self.center_x, |
---|
| 69 | center_y= self.center_y) |
---|
| 70 | self.bottom_line.qmax = self.base.qmax |
---|
| 71 | #self.outer_circle.set_cursor(self.base.qmax/1.8, 0) |
---|
| 72 | |
---|
| 73 | self.connect_markers([]) |
---|
| 74 | |
---|
| 75 | self.update() |
---|
| 76 | #self._post_data() |
---|
| 77 | |
---|
| 78 | # Bind to slice parameter events |
---|
| 79 | self.base.parent.Bind(SlicerParameters.EVT_SLICER_PARS, self._onEVT_SLICER_PARS) |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | def _onEVT_SLICER_PARS(self, event): |
---|
| 83 | #printEVT("AnnulusSlicer._onEVT_SLICER_PARS") |
---|
| 84 | event.Skip() |
---|
| 85 | if event.type == self.__class__.__name__: |
---|
| 86 | #self.set_params(event.params) |
---|
| 87 | self.base.update() |
---|
| 88 | |
---|
| 89 | def update_and_post(self): |
---|
| 90 | self.update() |
---|
| 91 | self._post_data() |
---|
| 92 | |
---|
| 93 | def save_data(self, path, image, x, y): |
---|
| 94 | output = open(path, 'w') |
---|
| 95 | |
---|
| 96 | data_x, data_y = self.get_data(image, x, y) |
---|
| 97 | |
---|
| 98 | output.write("<phi> <average>\n") |
---|
| 99 | for i in range(len(data_x)): |
---|
| 100 | output.write("%g %g\n" % (data_x[i], data_y[i])) |
---|
| 101 | output.close() |
---|
| 102 | |
---|
| 103 | def set_layer(self, n): |
---|
| 104 | self.layernum = n |
---|
| 105 | self.update() |
---|
| 106 | |
---|
| 107 | def clear(self): |
---|
| 108 | self.clear_markers() |
---|
| 109 | self.left_line.clear() |
---|
| 110 | self.right_line.clear() |
---|
| 111 | self.top_line.clear() |
---|
| 112 | self.bottom_line.clear() |
---|
| 113 | |
---|
| 114 | #self.base.connect.disconnect() |
---|
| 115 | self.base.parent.Unbind(SlicerParameters.EVT_SLICER_PARS) |
---|
| 116 | |
---|
| 117 | def update(self): |
---|
| 118 | """ |
---|
| 119 | Respond to changes in the model by recalculating the profiles and |
---|
| 120 | resetting the widgets. |
---|
| 121 | """ |
---|
| 122 | if self.left_line.has_move: |
---|
| 123 | print "left has moved" |
---|
| 124 | self.left_line.update(mline=[self.center_x, self.center_y],translation=True) |
---|
| 125 | self.right_line.update(mline= [self.center_x, self.center_y],translation=True) |
---|
| 126 | self.top_line.update( xmin= self.left_line.x ,xmax= self.right_line.x, |
---|
| 127 | mline= [self.center_x, self.center_y],translation=True) |
---|
| 128 | self.bottom_line.update(xmin= self.left_line.x ,xmax= self.right_line.x, |
---|
| 129 | mline= [self.center_x, self.center_y],translation=True) |
---|
| 130 | if self.right_line.has_move: |
---|
| 131 | print "right has moved" |
---|
| 132 | self.right_line.update(mline= [self.center_x, self.center_y],translation=True) |
---|
| 133 | self.left_line.update(mline=[self.center_x, self.center_y],translation=True) |
---|
| 134 | #self.left_line.update(xmin= self.right_line.x ,xmax=-1*self.right_line.x) |
---|
| 135 | self.top_line.update( xmin= self.left_line.x ,xmax= self.right_line.x, |
---|
| 136 | mline= [self.center_x, self.center_y],translation=True) |
---|
| 137 | self.bottom_line.update(xmin= self.left_line.x ,xmax= self.right_line.x, |
---|
| 138 | mline= [self.center_x, self.center_y],translation=True) |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | if self.bottom_line.has_move: |
---|
| 142 | print "bottom has moved" |
---|
| 143 | self.bottom_line.update(mline= [self.center_x, self.center_y],translation=True) |
---|
| 144 | self.top_line.update(y= -1*self.top_line.y,translation=True) |
---|
| 145 | self.left_line.update( ymin= self.bottom_line.y ,ymax= self.top_line.y, |
---|
| 146 | mline= [self.center_x, self.center_y],translation=True) |
---|
| 147 | self.right_line.update(ymin= self.bottom_line.y ,ymax= self.top_line.y, |
---|
| 148 | mline= [self.center_x, self.center_y],translation=True) |
---|
| 149 | |
---|
| 150 | if self.top_line.has_move: |
---|
| 151 | print "top has moved" |
---|
| 152 | self.top_line.update(mline= [self.center_x, self.center_y],translation=True) |
---|
| 153 | #self.bottom_line.update()xmin=None, xmax=None,y=None, mline=None): |
---|
| 154 | self.bottom_line.update(y= -1*self.top_line.y,mline= [self.center_x, self.center_y], |
---|
| 155 | translation=True ) |
---|
| 156 | self.left_line.update(ymin= self.bottom_line.y ,ymax= self.top_line.y, |
---|
| 157 | mline= [self.center_x, self.center_y],translation=True) |
---|
| 158 | self.right_line.update(ymin= self.bottom_line.y ,ymax= self.top_line.y, |
---|
| 159 | mline= [self.center_x, self.center_y],translation=True) |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | def save(self, ev): |
---|
| 163 | """ |
---|
| 164 | Remember the roughness for this layer and the next so that we |
---|
| 165 | can restore on Esc. |
---|
| 166 | """ |
---|
| 167 | self.base.freeze_axes() |
---|
| 168 | self.inner_circle.save(ev) |
---|
| 169 | self.outer_circle.save(ev) |
---|
| 170 | |
---|
| 171 | def _post_data(self): |
---|
| 172 | # Compute data |
---|
| 173 | data = self.base.data2D |
---|
| 174 | from DataLoader.manipulations import Boxavg |
---|
| 175 | radius = math.sqrt(math.pow(self.qmax,2)+math.pow(self.qmax,2)) |
---|
| 176 | x_min= self.left_line.x |
---|
| 177 | x_max= self.right_line.x |
---|
| 178 | y_min= self.bottom_line.y |
---|
| 179 | y_max= self.top_line.y |
---|
| 180 | box = Boxavg (x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max) |
---|
| 181 | |
---|
| 182 | self.count, self.error= box(self.base.data2D) |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | def moveend(self, ev): |
---|
| 188 | self.base.thaw_axes() |
---|
| 189 | |
---|
| 190 | # Post paramters |
---|
| 191 | event = SlicerParameters.SlicerParameterEvent() |
---|
| 192 | event.type = self.__class__.__name__ |
---|
| 193 | #event.params = self.get_params() |
---|
| 194 | wx.PostEvent(self.base.parent, event) |
---|
| 195 | |
---|
| 196 | self._post_data() |
---|
| 197 | |
---|
| 198 | def restore(self): |
---|
| 199 | """ |
---|
| 200 | Restore the roughness for this layer. |
---|
| 201 | """ |
---|
| 202 | self.inner_circle.restore() |
---|
| 203 | self.outer_circle.restore() |
---|
| 204 | |
---|
| 205 | def move(self, x, y, ev): |
---|
| 206 | """ |
---|
| 207 | Process move to a new position, making sure that the move is allowed. |
---|
| 208 | """ |
---|
| 209 | print "in move" |
---|
| 210 | if self.xmin <= x and x<= self.xmax: |
---|
| 211 | print "has move whole", x |
---|
| 212 | |
---|
| 213 | def set_cursor(self, x, y): |
---|
| 214 | pass |
---|
| 215 | |
---|
| 216 | def get_params(self): |
---|
| 217 | params = {} |
---|
| 218 | params["x_min"] = self.left_line.x |
---|
| 219 | params["x_max"] = self.right_line.x |
---|
| 220 | params["y_min"] = self.bottom_line.y |
---|
| 221 | params["y_max"] = self.top_line.y |
---|
| 222 | params["count"] = self.count |
---|
| 223 | params["error"] = self.error |
---|
| 224 | params["center_x"] = self.center_x |
---|
| 225 | params["center_y"] = self.center_y |
---|
| 226 | return params |
---|
| 227 | |
---|
| 228 | def set_params(self, params): |
---|
| 229 | |
---|
| 230 | x_min = params["x_min"] |
---|
| 231 | x_max = params["x_max"] |
---|
| 232 | y_min = params["y_min"] |
---|
| 233 | y_max = params["y_max"] |
---|
| 234 | theta = params["theta"] |
---|
| 235 | center_x = params["center_x"] |
---|
| 236 | center_y = params["center_y"] |
---|
| 237 | |
---|
| 238 | self.left_line.update(mline= [center_x, center_y],ymin= y_min ,ymax= y_max) |
---|
| 239 | self.right_line.update(mline= [center_x, center_y],ymin= y_min ,ymax= y_max) |
---|
| 240 | self.top_line.update(mline= [center_x, center_y], xmin= x_min ,xmax= xmax) |
---|
| 241 | self.bottom_line.update(mline= [center_x, center_y],xmin= xmin ,xmax= xmax) |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | self._post_data() |
---|
| 245 | def freeze_axes(self): |
---|
| 246 | self.base.freeze_axes() |
---|
| 247 | |
---|
| 248 | def thaw_axes(self): |
---|
| 249 | self.base.thaw_axes() |
---|
| 250 | |
---|
| 251 | def draw(self): |
---|
| 252 | self.base.draw() |
---|
| 253 | |
---|
| 254 | class HorizontalLine(_BaseInteractor): |
---|
| 255 | """ |
---|
| 256 | Select an annulus through a 2D plot |
---|
| 257 | """ |
---|
| 258 | def __init__(self,base,axes,color='black', zorder=5, y=0.5, |
---|
| 259 | xmin=0.0,xmax=0.5, |
---|
| 260 | center_x= 0, |
---|
| 261 | center_y= 0): |
---|
| 262 | |
---|
| 263 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 264 | self.markers = [] |
---|
| 265 | self.axes = axes |
---|
| 266 | # center |
---|
| 267 | self.center_x = center_x |
---|
| 268 | self.center_y = center_y |
---|
| 269 | |
---|
| 270 | self.y= y - self.center_y |
---|
| 271 | self.save_y= y- self.center_y |
---|
| 272 | |
---|
| 273 | self.xmin = xmin - self.center_x |
---|
| 274 | self.save_xmin = xmin - self.center_x |
---|
| 275 | self.xmax = xmax - self.center_x |
---|
| 276 | self.save_xmax = xmax - self.center_x |
---|
| 277 | |
---|
| 278 | |
---|
| 279 | self.line = self.axes.plot([self.xmin,self.xmax],[self.y,self.y], |
---|
| 280 | linestyle='-', marker='', |
---|
| 281 | color=self.color, |
---|
| 282 | visible=True)[0] |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | self.npts = 20 |
---|
| 286 | self.has_move=False |
---|
| 287 | self.connect_markers([self.line]) |
---|
| 288 | self.update() |
---|
| 289 | |
---|
| 290 | def set_layer(self, n): |
---|
| 291 | self.layernum = n |
---|
| 292 | self.update() |
---|
| 293 | |
---|
| 294 | def clear(self): |
---|
| 295 | self.clear_markers() |
---|
| 296 | try: |
---|
| 297 | |
---|
| 298 | self.line.remove() |
---|
| 299 | except: |
---|
| 300 | # Old version of matplotlib |
---|
| 301 | for item in range(len(self.axes.lines)): |
---|
| 302 | del self.axes.lines[0] |
---|
| 303 | |
---|
| 304 | def get_radius(self): |
---|
| 305 | |
---|
| 306 | return 0 |
---|
| 307 | |
---|
| 308 | def update(self,xmin=None, xmax=None,y=None, mline=None,translation=False): |
---|
| 309 | """ |
---|
| 310 | Draw the new roughness on the graph. |
---|
| 311 | """ |
---|
| 312 | #print "update main line", self.has_move |
---|
| 313 | if xmin !=None: |
---|
| 314 | self.xmin = xmin # - self.center_x |
---|
| 315 | if xmax !=None: |
---|
| 316 | self.xmax = xmax #- self.center_x |
---|
| 317 | if y !=None: |
---|
| 318 | self.y = y #- self.center_y |
---|
| 319 | |
---|
| 320 | self.line.set(xdata=[self.xmin,self.xmax], ydata=[self.y,self.y]) |
---|
| 321 | |
---|
| 322 | |
---|
| 323 | |
---|
| 324 | def save(self, ev): |
---|
| 325 | """ |
---|
| 326 | Remember the roughness for this layer and the next so that we |
---|
| 327 | can restore on Esc. |
---|
| 328 | """ |
---|
| 329 | self.save_xmin= self.xmin |
---|
| 330 | self.save_xmax= self.xmax |
---|
| 331 | |
---|
| 332 | self.save_y= self.y |
---|
| 333 | self.base.freeze_axes() |
---|
| 334 | |
---|
| 335 | def moveend(self, ev): |
---|
| 336 | |
---|
| 337 | self.has_move=False |
---|
| 338 | self.base.moveend(ev) |
---|
| 339 | |
---|
| 340 | def restore(self): |
---|
| 341 | """ |
---|
| 342 | Restore the roughness for this layer. |
---|
| 343 | """ |
---|
| 344 | self.ymin = self.save_ymin |
---|
| 345 | self.ymax = self.save_ymax |
---|
| 346 | |
---|
| 347 | def move(self, x, y, ev): |
---|
| 348 | """ |
---|
| 349 | Process move to a new position, making sure that the move is allowed. |
---|
| 350 | """ |
---|
| 351 | self.y= y - self.center_y |
---|
| 352 | |
---|
| 353 | self.has_move=True |
---|
| 354 | self.base.base.update() |
---|
| 355 | |
---|
| 356 | def set_cursor(self, x, y): |
---|
| 357 | self.move(x, y, None) |
---|
| 358 | self.update() |
---|
| 359 | |
---|
| 360 | |
---|
| 361 | def get_params(self): |
---|
| 362 | params = {} |
---|
| 363 | params["radius"] = self.xmin |
---|
| 364 | params["theta"] = self.xmax |
---|
| 365 | return params |
---|
| 366 | |
---|
| 367 | def set_params(self, params): |
---|
| 368 | |
---|
| 369 | x = params["radius"] |
---|
| 370 | self.set_cursor(x, self._inner_mouse_y) |
---|
| 371 | |
---|
| 372 | |
---|
| 373 | |
---|
| 374 | |
---|
| 375 | class VerticalLine(_BaseInteractor): |
---|
| 376 | """ |
---|
| 377 | Select an annulus through a 2D plot |
---|
| 378 | """ |
---|
| 379 | def __init__(self,base,axes,color='black', zorder=5, ymin=0.0, |
---|
| 380 | ymax=0.5,x= 0.5, |
---|
| 381 | center_x= 0, |
---|
| 382 | center_y= 0): |
---|
| 383 | |
---|
| 384 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 385 | self.markers = [] |
---|
| 386 | self.axes = axes |
---|
| 387 | # x coordinate of the vertical line |
---|
| 388 | self.center_x= center_x |
---|
| 389 | self.center_y= center_y |
---|
| 390 | self.x = x - center_x |
---|
| 391 | self.save_x = x - center_x |
---|
| 392 | # minimum value of y coordinate of the vertical line |
---|
| 393 | self.ymin = ymin - center_y |
---|
| 394 | self.save_ymin = ymin - center_y |
---|
| 395 | # maximum value of y coordinate of the vertical line |
---|
| 396 | self.ymax= ymax - center_y |
---|
| 397 | self.save_ymax= ymax - center_y |
---|
| 398 | |
---|
| 399 | # Draw vertical line |
---|
| 400 | self.line = self.axes.plot([self.x,self.x],[self.ymin,self.ymax], |
---|
| 401 | linestyle='-', marker='', |
---|
| 402 | color=self.color, |
---|
| 403 | visible=True)[0] |
---|
| 404 | |
---|
| 405 | self.npts = 20 |
---|
| 406 | # Check vertical line motion |
---|
| 407 | self.has_move=False |
---|
| 408 | self.connect_markers([self.line]) |
---|
| 409 | self.update() |
---|
| 410 | |
---|
| 411 | def set_layer(self, n): |
---|
| 412 | self.layernum = n |
---|
| 413 | self.update() |
---|
| 414 | |
---|
| 415 | def clear(self): |
---|
| 416 | self.clear_markers() |
---|
| 417 | try: |
---|
| 418 | |
---|
| 419 | self.line.remove() |
---|
| 420 | except: |
---|
| 421 | # Old version of matplotlib |
---|
| 422 | for item in range(len(self.axes.lines)): |
---|
| 423 | del self.axes.lines[0] |
---|
| 424 | |
---|
| 425 | def get_radius(self): |
---|
| 426 | return 0 |
---|
| 427 | |
---|
| 428 | def update(self,x=None,ymin=None, ymax=None, mline=None,translation=False): |
---|
| 429 | """ |
---|
| 430 | Draw the new roughness on the graph. |
---|
| 431 | """ |
---|
| 432 | if x!=None: |
---|
| 433 | self.x = x #-self.center_x |
---|
| 434 | if ymin !=None: |
---|
| 435 | self.ymin = ymin #- self.center_y |
---|
| 436 | if ymax !=None: |
---|
| 437 | self.ymax = ymax #- self.center_y |
---|
| 438 | |
---|
| 439 | self.line.set(xdata=[self.x,self.x], ydata=[self.ymin,self.ymax]) |
---|
| 440 | |
---|
| 441 | |
---|
| 442 | |
---|
| 443 | def save(self, ev): |
---|
| 444 | """ |
---|
| 445 | Remember the roughness for this layer and the next so that we |
---|
| 446 | can restore on Esc. |
---|
| 447 | """ |
---|
| 448 | self.save_x= self.x |
---|
| 449 | self.save_ymin= self.ymin |
---|
| 450 | self.save_ymax= self.ymax |
---|
| 451 | |
---|
| 452 | self.base.freeze_axes() |
---|
| 453 | |
---|
| 454 | def moveend(self, ev): |
---|
| 455 | |
---|
| 456 | self.has_move=False |
---|
| 457 | self.base.moveend(ev) |
---|
| 458 | |
---|
| 459 | def restore(self): |
---|
| 460 | """ |
---|
| 461 | Restore the roughness for this layer. |
---|
| 462 | """ |
---|
| 463 | self.x = self.save_x |
---|
| 464 | |
---|
| 465 | self.ymin=self.save_ymin |
---|
| 466 | self.ymax=self.save_ymax |
---|
| 467 | def move(self, x, y, ev): |
---|
| 468 | """ |
---|
| 469 | Process move to a new position, making sure that the move is allowed. |
---|
| 470 | """ |
---|
| 471 | self.x = x - self.center_x |
---|
| 472 | |
---|
| 473 | self.has_move=True |
---|
| 474 | self.base.base.update() |
---|
| 475 | |
---|
| 476 | |
---|
| 477 | def set_cursor(self, x, y): |
---|
| 478 | self.move(x, y, None) |
---|
| 479 | self.update() |
---|
| 480 | |
---|
| 481 | |
---|
| 482 | def get_params(self): |
---|
| 483 | params = {} |
---|
| 484 | params["x"] = self.xmin |
---|
| 485 | params["ymin"] = self.ymin |
---|
| 486 | params["ymax"] = self.ymax |
---|
| 487 | return params |
---|
| 488 | |
---|
| 489 | def set_params(self, params): |
---|
| 490 | """ |
---|
| 491 | Draw a vertical line given some value of params |
---|
| 492 | @param params: a dictionary containing value for x, ymin , ymax to draw |
---|
| 493 | a vertical line |
---|
| 494 | """ |
---|
| 495 | x = params["x"] |
---|
| 496 | ymin = params["ymin"] |
---|
| 497 | ymax = params["ymax"] |
---|
| 498 | #self.set_cursor(x, self._inner_mouse_y) |
---|
| 499 | self.update(self,x =x,ymin =ymin, ymax =ymax) |
---|
| 500 | |
---|
| 501 | |
---|
| 502 | |
---|