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