Ignore:
Timestamp:
Feb 16, 2009, 4:00:20 PM (15 years ago)
Author:
rwilson
Message:

GUI version of plotcsv

File:
1 edited

Legend:

Unmodified
Added
Removed
  • misc/tools/plotcsv/plotcsv.py

    r6344 r6348  
    5252MARGIN = 10
    5353
    54 LAB_CTRL_OFFSET = 3
     54LAB_CTRL_OFFSET = 5
    5555
    5656BOX_WIDTH = 400
     
    6464TXT_CSVFILE_HEIGHT = 200
    6565
    66 COLLAB_X_OFFSET = 20
     66COLLAB_X_OFFSET = 25
    6767
    6868CHBOX_HEIGHT = 30
     
    145145    "zNHrrf9oCIxPMrO3N4LXSYQhEMeNpmcCKVywaA43JUYjwPeBTqcx3BIIOYTHHi74YjP4F4OI"
    146146    "IIgI9JOI2kS0JKI/m33TfzU+ASG0bvT61fpzAAAAAElFTkSuQmCC")
     147
     148
     149def error(msg):
     150    print msg
     151    sys.exit(10)
    147152
    148153
     
    254259
    255260##
    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 range
    266     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 ##
    277261# @brief Get CSV data from a file.
    278262# @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.
     265def getCSVData(filename, x_hdr, y_hdr):
    283266    # get contents of data file
    284267    # after this, 'header' is list of column header strings
     
    293276    del data[0]         # get rid of header in dataset
    294277
    295     # convert column specifiers to 'int' if required
     278    # get int index values for column headers
    296279    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)
    304283
    305284    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':
    327292        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)
    333294
    334295    return (x_data, y_data)
     
    338299# @brief Plot data files.
    339300# @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.
     303def 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
    352309    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)
    356311        pylab.plot(x_data, y_data)
    357312
     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
    358319    pylab.show()
     320#    pylab.close()
    359321
    360322
     
    397359        Y_OFFSET += BUTTON_HEIGHT + MARGIN
    398360
    399         wx.StaticBox(p, -1, 'Plot details', (3, Y_OFFSET),
     361        wx.StaticBox(p, -1, 'Plot files', (3, Y_OFFSET),
    400362                     size=(BOX_PLOT_WIDTH, BOX_PLOT_HEIGHT))
    401363        Y_OFFSET += GEN_DELTAY
    402364        wx.StaticText(p, -1, 'X-Column',
    403365                      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),
    405367                                   size=(80, -1))
    406368        self.XColHdr = []
     
    409371                           Y_OFFSET+LAB_CTRL_OFFSET), style=wx.ALIGN_LEFT)
    410372        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))
    412374        self.YColHdr = []
    413375
     
    491453            self.XColHdr = []
    492454        # -1 here means 'no selection'
    493         self.XColHdrSelection = self.cfg['XColHdrSelection', -1]
     455        self.XColHdrSelection = self.cfg['XColHdrSelection']
    494456        self.YColHdr = self.cfg['YColHdr']
    495457        if self.YColHdr is None:
    496458            self.YColHdr = []
    497459        # -1 here means 'no selection'
    498         self.YColHdrSelection = self.cfg['YColHdrSelection', -1]
     460        self.YColHdrSelection = self.cfg['YColHdrSelection']
    499461        self.file_dir = self.cfg['file_dir']
    500462        if self.file_dir is None:
     
    507469            self.headers.append(headers)
    508470            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)
    509478
    510479    def getHeaders(self, filename):
     
    526495        '''Update choice controls with header lists.
    527496
    528         Keep initially visible strings visible afterwards, if posssible.
     497        Keep initially visible strings visible afterwards, if possible.
    529498        '''
    530499
     
    578547
    579548        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)
    588550           
    589551    def error(self, msg):
Note: See TracChangeset for help on using the changeset viewer.