Changeset 6348
- Timestamp:
- Feb 16, 2009, 4:00:20 PM (16 years ago)
- Location:
- misc/tools/plotcsv
- Files:
-
- 6 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
misc/tools/plotcsv/README
r6338 r6348 1 First version of plotcsv was command-line only. Doesn't play 2 well in the Windows environment. 3 1 4 Plan B is to create a GUI program that will have two areas: 2 5 1. Select a set of CSV files -
misc/tools/plotcsv/plotcsv.py
r6344 r6348 52 52 MARGIN = 10 53 53 54 LAB_CTRL_OFFSET = 354 LAB_CTRL_OFFSET = 5 55 55 56 56 BOX_WIDTH = 400 … … 64 64 TXT_CSVFILE_HEIGHT = 200 65 65 66 COLLAB_X_OFFSET = 2 066 COLLAB_X_OFFSET = 25 67 67 68 68 CHBOX_HEIGHT = 30 … … 145 145 "zNHrrf9oCIxPMrO3N4LXSYQhEMeNpmcCKVywaA43JUYjwPeBTqcx3BIIOYTHHi74YjP4F4OI" 146 146 "IIgI9JOI2kS0JKI/m33TfzU+ASG0bvT61fpzAAAAAElFTkSuQmCC") 147 148 149 def error(msg): 150 print msg 151 sys.exit(10) 147 152 148 153 … … 254 259 255 260 ## 256 # @brief Plot a sequence of data.257 # @param x_data The X data sequence to plot.258 # @param y_data The Y data sequence to plot.259 # @param options A dictionary of plot options.260 def plot_data(x_data, y_data, options):261 print 'x_data=%s' % str(x_data)262 print 'y_data=%s' % str(y_data)263 pylab.plot(x_data, y_data)264 265 # if user requested a particular Y range266 if not options[Y_RANGE] is None:267 try:268 (minimum, maximum) = options[Y_RANGE].split(',')269 minimum = float(minimum)270 maximum = float(maximum)271 except:272 error('Sorry, got a bad value for Y range: %s' % options[Y_RANGE])273 pylab.ylim(ymin=minimum, ymax=maximum)274 275 276 ##277 261 # @brief Get CSV data from a file. 278 262 # @param filename Path to the data file to plot. 279 # @param x_hdr The X axis title string (or index). 280 # @param y_hdr The Y axis title string (or index). 281 # @param options A dictionary of options. 282 def getCSVData(filename, x_hdr, y_hdr, options): 263 # @param x_hdr The X axis title string. 264 # @param y_hdr The Y axis title string. 265 def getCSVData(filename, x_hdr, y_hdr): 283 266 # get contents of data file 284 267 # after this, 'header' is list of column header strings … … 293 276 del data[0] # get rid of header in dataset 294 277 295 # convert column specifiers to 'int' if required278 # get int index values for column headers 296 279 try: 297 index = int(x_hdr) 298 except: 299 try: 300 index = header.index(x_hdr) 301 except ValueError: 302 error("Sorry, X column header '%s' isn't in the data file." % x_hdr) 303 options[X_DATACOL] = index 280 x_index = header.index(x_hdr) 281 except ValueError: 282 error("Sorry, X column header '%s' isn't in the data file." % x_hdr) 304 283 305 284 try: 306 index = int(y_hdr) 307 except: 308 try: 309 index = header.index(y_hdr) 310 except ValueError: 311 error("Sorry, Y column header '%s' isn't in the data file." % y_hdr) 312 options[Y_DATACOL] = index 313 314 # extract required columns from the data (int at this point) 315 x_col = options[X_DATACOL] 316 y_col = options[Y_DATACOL] 317 318 # get max column number, check requested columns 319 max_col = len(header) 320 if x_col >= max_col or y_col >= max_col: 321 error('Sorry, maximum column number for that file is %d.' % (max_col-1)) 322 323 x_label = header[x_col].title() 324 x_data = map(lambda x: x[x_col], data) 325 if x_label == 'Time': 326 x_label = 'Time (hours)' 285 y_index = header.index(y_hdr) 286 except ValueError: 287 error("Sorry, Y column header '%s' isn't in the data file." % y_hdr) 288 289 # get appropriate columns from data[] 290 x_data = map(lambda x: x[x_index], data) 291 if x_hdr == 'time': 327 292 x_data = map(lambda x: float(x)/3600., x_data) 328 y_data = map(lambda x: x[y_col], data) 329 y_label = header[y_col].title() 330 331 options[X_LABEL] = x_label 332 options[Y_LABEL] = y_label 293 y_data = map(lambda x: x[y_index], data) 333 294 334 295 return (x_data, y_data) … … 338 299 # @brief Plot data files. 339 300 # @param filenames List of full pathnames to plot. 340 # @param options A dictionary of options. 341 def plot_files(filenames, options=None): 342 # set options defaults 343 opts = options 344 if opts is None: 345 opts = {} 346 347 ## pylab.title(options.get(TITLE, '')) 348 ## pylab.xlabel(options.get(X_LABEL, '')) 349 ## pylab.ylabel(options.get(Y_LABEL, '')) 350 ## pylab.grid(True) 351 ## 301 # @param x_hdr The X axis label string. 302 # @param y_hdr The Y axis label string. 303 def plot_files(filenames, x_hdr, y_hdr): 304 pylab.rc('axes', linewidth=2) 305 pylab.xlabel(x_hdr.title()) 306 pylab.ylabel(y_hdr.title()) 307 pylab.grid(True) 308 352 309 for f in filenames: 353 (x_data, y_data) = getCSVData(f, opts[X_LABEL], opts[Y_LABEL], options) 354 print 'x_data=%s' % str(x_data) 355 print 'y_data=%s' % str(y_data) 310 (x_data, y_data) = getCSVData(f, x_hdr, y_hdr) 356 311 pylab.plot(x_data, y_data) 357 312 313 (min_y, max_y) = pylab.ylim() 314 range_y = max_y - min_y 315 add_y = float(range_y) / 5 316 pylab.ylim((min_y, max_y+add_y)) 317 pylab.legend(filenames, 'upper left') 318 358 319 pylab.show() 320 # pylab.close() 359 321 360 322 … … 397 359 Y_OFFSET += BUTTON_HEIGHT + MARGIN 398 360 399 wx.StaticBox(p, -1, 'Plot details', (3, Y_OFFSET),361 wx.StaticBox(p, -1, 'Plot files', (3, Y_OFFSET), 400 362 size=(BOX_PLOT_WIDTH, BOX_PLOT_HEIGHT)) 401 363 Y_OFFSET += GEN_DELTAY 402 364 wx.StaticText(p, -1, 'X-Column', 403 365 pos=(COLLAB_X_OFFSET, Y_OFFSET+LAB_CTRL_OFFSET), style=wx.ALIGN_LEFT) 404 self.cbXColHdr = wx.Choice(p, -1, pos=(COLLAB_X_OFFSET+ 50, Y_OFFSET),366 self.cbXColHdr = wx.Choice(p, -1, pos=(COLLAB_X_OFFSET+65, Y_OFFSET), 405 367 size=(80, -1)) 406 368 self.XColHdr = [] … … 409 371 Y_OFFSET+LAB_CTRL_OFFSET), style=wx.ALIGN_LEFT) 410 372 x = FORM_WIDTH/2 + COLLAB_X_OFFSET 411 self.cbYColHdr = wx.Choice(p, -1, pos=(x+ 50, Y_OFFSET), size=(80, -1))373 self.cbYColHdr = wx.Choice(p, -1, pos=(x+65, Y_OFFSET), size=(80, -1)) 412 374 self.YColHdr = [] 413 375 … … 491 453 self.XColHdr = [] 492 454 # -1 here means 'no selection' 493 self.XColHdrSelection = self.cfg['XColHdrSelection' , -1]455 self.XColHdrSelection = self.cfg['XColHdrSelection'] 494 456 self.YColHdr = self.cfg['YColHdr'] 495 457 if self.YColHdr is None: 496 458 self.YColHdr = [] 497 459 # -1 here means 'no selection' 498 self.YColHdrSelection = self.cfg['YColHdrSelection' , -1]460 self.YColHdrSelection = self.cfg['YColHdrSelection'] 499 461 self.file_dir = self.cfg['file_dir'] 500 462 if self.file_dir is None: … … 507 469 self.headers.append(headers) 508 470 self.txtCSVFiles.Insert(f, i) 471 self.common_headers = self.GenCommonHeaders(self.headers) 472 self.updateHdrChoices() 473 474 if self.XColHdrSelection: 475 self.cbXColHdr.SetSelection(self.XColHdrSelection) 476 if self.YColHdrSelection: 477 self.cbYColHdr.SetSelection(self.YColHdrSelection) 509 478 510 479 def getHeaders(self, filename): … … 526 495 '''Update choice controls with header lists. 527 496 528 Keep initially visible strings visible afterwards, if poss sible.497 Keep initially visible strings visible afterwards, if possible. 529 498 ''' 530 499 … … 578 547 579 548 if selected_x and selected_y: 580 options = {} 581 # set X and Y axis labels 582 options[X_LABEL] = selected_x 583 options[Y_LABEL] = selected_y 584 options[SIZE] = '800,600' 585 options[TITLE] = 'TITLE' 586 587 plot_files(self.CSVFiles, options) 549 plot_files(self.CSVFiles, selected_x, selected_y) 588 550 589 551 def error(self, msg):
Note: See TracChangeset
for help on using the changeset viewer.