Changeset 904830e in sasview for sansguiframe/src/sans/guiframe
- Timestamp:
- Sep 8, 2011 11:20:59 AM (13 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 5637362
- Parents:
- a0106ac
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansguiframe/src/sans/guiframe/data_processor.py
r7ad194fa r904830e 4 4 import wx 5 5 import numpy 6 import math 7 import re 6 8 import sys 7 9 import copy … … 17 19 from sans.guiframe.dataFitting import Data1D 18 20 21 FUNC_DICT = {"sqrt": "math.sqrt", 22 "pow": "math.sqrt"} 23 def parse_string(sentence, list): 24 """ 25 Return a dictionary of column label and index or row selected 26 :param sentence: String to parse 27 :param list: list of columns label 28 """ 29 toks = [] 30 p2 = re.compile(r'\d+') 31 p = re.compile(r'[\+\-\*\%\/]') 32 labels = p.split(sentence) 33 col_dict = {} 34 for elt in labels: 35 rang = None 36 temp_arr = [] 37 for label in list: 38 label_pos = elt.find(label) 39 if label_pos != -1: 40 if elt.count(',') > 0: 41 new_temp = [] 42 temp = elt.split(label) 43 for item in temp: 44 range_pos = item.find(":") 45 if range_pos != -1: 46 rang = p2.findall(item) 47 for i in xrange(int(rang[0]), int(rang[1])+1 ): 48 new_temp.append(i) 49 temp_arr += new_temp 50 else: 51 temp = elt.split(label) 52 for item in temp: 53 range_pos = item.find(":") 54 if range_pos != -1: 55 rang = p2.findall(item) 56 for i in xrange(int(rang[0]), int(rang[1])+1 ): 57 temp_arr.append(i) 58 col_dict[elt] = (label, temp_arr) 59 return col_dict 19 60 20 61 class GridPage(sheet.CSheet): … … 50 91 """ 51 92 flag = event.CmdDown() or event.ControlDown() 52 cell = (event.GetRow(), event.GetCol()) 93 row, col = event.GetRow(), event.GetCol() 94 cell = (row, col) 53 95 if not flag: 54 96 self.selected_cells = [] 97 self.axis_value = [] 98 self.axis_label = "" 55 99 if cell in self.selected_cells: 56 100 self.selected_cells.remove(cell) 57 101 else: 58 102 self.selected_cells.append(cell) 103 if row > 1: 104 if (cell) in self.selected_cells: 105 self.axis_value.append(self.GetCellValue(row, col)) 106 self.axis_label = self.GetCellValue(row, col) 59 107 event.Skip() 60 108 … … 66 114 col = event.GetCol() 67 115 if not flag: 68 self.selected_cols = [] 116 self.selected_cols = [] 117 self.axis_value = [] 118 self.axis_label = "" 69 119 if col not in self.selected_cols: 70 120 self.selected_cols.append(col) 121 if not flag : 122 for row in range(2, self.GetNumberRows()+ 1): 123 self.axis_value.append(self.GetCellValue(row, col)) 124 row = 1 125 self.axis_label = self.GetCellValue(row, col) 71 126 event.Skip() 72 127 … … 209 264 return list_of_cells 210 265 266 def get_column_labels(self): 267 """ 268 return dictionary of columns labels of the current page 269 """ 270 pos = self.GetSelection() 271 grid = self.GetPage(pos) 272 labels = {} 273 row = 0 274 for col in range(grid.GetNumberCols()): 275 label = grid.GetCellValue(row, col) 276 if label.strip() != "" : 277 labels[label.strip()] = col 278 return labels 279 211 280 def create_axis_label(self, cell_list): 212 281 """ … … 229 298 for element in temp: 230 299 temp_list.remove(element) 231 row_min, _= temp_list[0]300 row_min, col = temp_list[0] 232 301 row_max = row_min 302 col_name = grid.GetCellValue(0, col) 233 303 label += str(col_name) + "[" + str(row_min) + ":" 234 print "temp_list", temp_list235 304 for index in xrange(len(temp_list)): 236 305 prev = index - 1 … … 243 312 label += "[" + str(row_min) + ":" 244 313 row_max = row 245 246 314 if (index == len(temp_list)- 1): 247 label += str(row_max) + "]" 248 print "here" 249 print "label ", label 250 return label 315 label += str(row_max) + "]" 316 return label, col_name 251 317 252 318 def on_close_page(self, event): … … 295 361 296 362 class GridPanel(SPanel): 297 def __init__(self, parent, data=None, *args, **kwds):363 def __init__(self, parent, data=None, *args, **kwds): 298 364 SPanel.__init__(self, parent , *args, **kwds) 299 365 self.vbox = wx.BoxSizer(wx.VERTICAL) … … 310 376 self.x_axis_label = None 311 377 self.y_axis_label = None 312 self.x_axis_ value = None313 self.y_axis_ value = None378 self.x_axis_title = None 379 self.y_axis_title = None 314 380 self.x_axis_unit = None 315 381 self.y_axis_unit = None 316 382 self.plot_button = None 317 self. grid= None383 self.notebook = None 318 384 self.layout_grid() 319 385 self.layout_plotting_area() … … 323 389 """ 324 390 """ 325 if self.grid is not None: 326 self.grid.set_data(data) 327 328 def set_xaxis(self, label="", x=None) : 391 if self.notebook is not None: 392 self.notebook.set_data(data) 393 394 def set_xaxis(self, label="", x=None): 395 """ 396 """ 329 397 if x is None: 330 398 x = [] 331 399 self.x = x 332 self.x_axis_value.SetValue("%s[:]" % str(label)) 333 self.x_axis_label.SetValue(str(label)) 334 335 def set_yaxis(self, label="", y=None) : 400 self.x_axis_label.SetValue("%s[:]" % str(label)) 401 self.x_axis_title.SetValue(str(label)) 402 403 def set_yaxis(self, label="", y=None): 404 """ 405 """ 336 406 if y is None: 337 407 y = [] 338 408 self.y = y 339 self.y_axis_value.SetValue("%s[:]" % str(label)) 340 self.y_axis_label.SetValue(str(label)) 341 409 self.y_axis_label.SetValue("%s[:]" % str(label)) 410 self.y_axis_title.SetValue(str(label)) 411 412 def get_plot_axis(self, col, list): 413 """ 414 415 """ 416 axis = [] 417 pos = self.notebook.GetSelection() 418 grid = self.notebook.GetPage(pos) 419 for row in list: 420 label = grid.GetCellValue(row - 1, col) 421 if label.strip() != "": 422 axis.append(float(label.strip())) 423 return axis 424 342 425 def on_plot(self, event): 343 426 """ 344 plotting427 Evaluate the contains of textcrtl and plot result 345 428 """ 346 new_plot = Data1D(x=self.x, y=self.y) 429 pos = self.notebook.GetSelection() 430 grid = self.notebook.GetPage(pos) 431 column_names = {} 432 if grid is not None: 433 column_names = self.notebook.get_column_labels() 434 #evalue x 435 sentence = self.x_axis_label.GetValue() 436 if sentence.strip() == "": 437 msg = "select value for x axis" 438 raise ValueError, msg 439 dict = parse_string(sentence, column_names.keys()) 440 for tok, (col_name, list) in dict.iteritems(): 441 col = column_names[col_name] 442 xaxis = self.get_plot_axis(col, list) 443 sentence = sentence.replace(tok, 444 "numpy.array(%s)" % str(xaxis)) 445 for key, value in FUNC_DICT.iteritems(): 446 sentence = sentence.replace(key.lower(), value) 447 x = eval(sentence) 448 #evaluate y 449 sentence = self.y_axis_label.GetValue() 450 if sentence.strip() == "": 451 msg = "select value for y axis" 452 raise ValueError, msg 453 dict = parse_string(sentence, column_names.keys()) 454 for tok, (col_name, list) in dict.iteritems(): 455 col = column_names[col_name] 456 yaxis = self.get_plot_axis(col, list) 457 sentence = sentence.replace(tok, 458 "numpy.array(%s)" % str(yaxis)) 459 for key, value in FUNC_DICT.iteritems(): 460 sentence = sentence.replace(key, value) 461 y = eval(sentence) 462 #plotting 463 new_plot = Data1D(x=x, y=y) 347 464 new_plot.id = wx.NewId() 348 465 new_plot.group_id = wx.NewId() 349 title = "%s vs %s" % (self.y_axis_label.GetValue(), self.x_axis_label.GetValue()) 350 new_plot.xaxis(self.x_axis_label.GetValue(), self.x_axis_unit.GetValue()) 351 new_plot.yaxis(self.y_axis_label.GetValue(), self.y_axis_unit.GetValue()) 352 wx.PostEvent(self.parent.parent, 466 title = "%s vs %s" % (self.y_axis_title.GetValue(), 467 self.x_axis_title.GetValue()) 468 new_plot.xaxis(self.x_axis_title.GetValue(), self.x_axis_unit.GetValue()) 469 new_plot.yaxis(self.y_axis_title.GetValue(), self.y_axis_unit.GetValue()) 470 try: 471 title = self.notebook.GetPageText(pos) 472 wx.PostEvent(self.parent.parent, 353 473 NewPlotEvent(plot=new_plot, 354 group_id=str(new_plot.group_id), title ="batch")) 474 group_id=str(new_plot.group_id), title =title)) 475 except: 476 pass 477 355 478 def layout_grid(self): 356 479 """ 357 480 Draw the area related to the grid 358 481 """ 359 self. grid= Notebook(parent=self)360 self. grid.set_data(self._data)361 self.grid_sizer.Add(self. grid, 1, wx.EXPAND, 0)482 self.notebook = Notebook(parent=self) 483 self.notebook.set_data(self._data) 484 self.grid_sizer.Add(self.notebook, 1, wx.EXPAND, 0) 362 485 363 486 def layout_plotting_area(self): … … 365 488 Draw area containing options to plot 366 489 """ 367 self.x_axis_ label= wx.TextCtrl(self, -1)368 self.y_axis_ label= wx.TextCtrl(self, -1)369 self.x_axis_ value= wx.TextCtrl(self, -1, size=(200, -1))370 self.y_axis_ value= wx.TextCtrl(self, -1, size=(200, -1))490 self.x_axis_title = wx.TextCtrl(self, -1) 491 self.y_axis_title = wx.TextCtrl(self, -1) 492 self.x_axis_label = wx.TextCtrl(self, -1, size=(200, -1)) 493 self.y_axis_label = wx.TextCtrl(self, -1, size=(200, -1)) 371 494 self.x_axis_add = wx.Button(self, -1, "Add") 372 495 self.x_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, … … 380 503 wx.EVT_BUTTON(self, self.plot_button.GetId(), self.on_plot) 381 504 self.plotting_sizer.AddMany([ 382 (wx.StaticText(self, -1, "x-axis label"), 1, wx.LEFT, 10), 383 (self.x_axis_label, wx.TOP|wx.BOTTOM|wx.LEFT, 10), 384 (wx.StaticText(self, -1, "x-axis value"), 1, wx.LEFT, 10), 385 (self.x_axis_value, wx.TOP|wx.BOTTOM|wx.LEFT, 10), 386 (self.x_axis_add, 1, wx.LEFT|wx.RIGHT, 0), 387 (wx.StaticText(self, -1 , "unit"), 1, wx.LEFT|wx.RIGHT, 0), 388 (self.x_axis_unit, 0, wx.LEFT, 0), 389 (wx.StaticText(self, -1, "y-axis label"), 1, wx.LEFT, 10), 390 (self.y_axis_label, wx.TOP|wx.BOTTOM|wx.LEFT, 10), 391 (wx.StaticText(self, -1, "y-axis value"), 1, wx.LEFT, 10), 392 (self.y_axis_value, wx.TOP|wx.BOTTOM|wx.LEFT, 10), 393 (self.y_axis_add, 1, wx.LEFT|wx.RIGHT, 0), 394 (wx.StaticText(self, -1 , "unit"), 1, wx.LEFT|wx.RIGHT, 0), 395 (self.y_axis_unit, 0, wx.LEFT, 0), 505 (wx.StaticText(self, -1, "x-axis label"), 1, 506 wx.TOP|wx.BOTTOM|wx.LEFT, 10), 507 (self.x_axis_label, 1, wx.TOP|wx.BOTTOM, 10), 508 (self.x_axis_add, 1, wx.TOP|wx.BOTTOM|wx.RIGHT, 10), 509 (wx.StaticText(self, -1, "x-axis title"), 1, 510 wx.TOP|wx.BOTTOM|wx.LEFT, 10), 511 (self.x_axis_title, 1, wx.TOP|wx.BOTTOM, 10), 512 (wx.StaticText(self, -1 , "unit"), 1, 513 wx.TOP|wx.BOTTOM, 10), 514 (self.x_axis_unit, 1, wx.TOP|wx.BOTTOM, 10), 515 (wx.StaticText(self, -1, "y-axis label"), 1, 516 wx.BOTTOM|wx.LEFT, 10), 517 (self.y_axis_label, wx.BOTTOM, 10), 518 (self.y_axis_add, 1, wx.BOTTOM|wx.RIGHT, 10), 519 (wx.StaticText(self, -1, "y-axis title"), 1, 520 wx.BOTTOM|wx.LEFT, 10), 521 (self.y_axis_title, wx.BOTTOM, 10), 522 (wx.StaticText(self, -1 , "unit"), 1, wx.BOTTOM, 10), 523 (self.y_axis_unit, 1, wx.BOTTOM, 10), 396 524 (-1, -1), 397 525 (-1, -1), … … 400 528 (-1, -1), 401 529 (-1, -1), 402 (self.plot_button, 1, wx.LEFT ,0)])530 (self.plot_button, 1, wx.LEFT|wx.BOTTOM, 10)]) 403 531 404 532 def on_edit_axis(self, event): … … 406 534 Get the selected column on the visible grid and set values for axis 407 535 """ 408 cell_list = self.grid.on_edit_axis() 409 self.create_axis_label(cell_list) 410 536 cell_list = self.notebook.on_edit_axis() 537 label, title = self.create_axis_label(cell_list) 538 tcrtl = event.GetEventObject() 539 if tcrtl == self.x_axis_add: 540 self.edit_axis_helper(self.x_axis_label, self.x_axis_title, 541 label, title) 542 elif tcrtl == self.y_axis_add: 543 self.edit_axis_helper(self.y_axis_label, self.y_axis_title, 544 label, title) 545 411 546 def create_axis_label(self, cell_list): 412 547 """ … … 416 551 417 552 """ 418 if self. gridis not None:419 return self. grid.create_axis_label(cell_list)553 if self.notebook is not None: 554 return self.notebook.create_axis_label(cell_list) 420 555 421 def edit_axis_helper(self, tcrtl_label, tcrtl_value): 422 """ 423 """ 556 def edit_axis_helper(self, tcrtl_label, tcrtl_title, label, title): 557 """ 558 get controls to modify 559 """ 560 tcrtl_label.SetValue(str(label)) 561 tcrtl_title.SetValue(str(title)) 562 424 563 def add_column(self): 425 564 """ 426 565 """ 427 if self. gridis not None:428 self. grid.add_column()566 if self.notebook is not None: 567 self.notebook.add_column() 429 568 430 569 def on_remove_column(self): 431 570 """ 432 571 """ 433 if self. gridis not None:434 self. grid.on_remove_column()572 if self.notebook is not None: 573 self.notebook.on_remove_column() 435 574 436 575
Note: See TracChangeset
for help on using the changeset viewer.