Ignore:
Timestamp:
Jun 12, 2009, 4:19:24 PM (15 years ago)
Author:
rwilson
Message:

Back-merge of Numeric stuff to numpy branch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/numpy_misc/tools/plotcsv/plotcsv.py

    r6906 r7200  
    1515import string
    1616try:
    17     import cpickle
     17    import cpickle as pickle
    1818except:
    1919    import pickle
     
    5454# program name and version
    5555APP_NAME = 'plotcsv'
    56 APP_VERSION = '0.5'
     56APP_VERSION = '0.7'
    5757
    5858# name of the configuration filename
     
    7373BOX_CSV_HEIGHT = 265
    7474BOX_PLOT_WIDTH = 400
    75 BOX_PLOT_HEIGHT = 140
     75BOX_PLOT_HEIGHT = 140+25+25+30
    7676
    7777TXT_CSVFILE_WIDTH = 390
     
    8484
    8585FORM_WIDTH = BOX_WIDTH + 15
    86 FORM_HEIGHT = 450
     86FORM_HEIGHT = 450+25+25+30
    8787
    8888START_YOFFSET = 7
     
    206206        self.changed = True
    207207
     208    def get(self, key, default=None):
     209        """Override to allow: <var> = cfg.get(<key>, default=None)"""
     210       
     211        return self.cfgdict.get(key, default)
     212       
    208213    def __getitem__(self, key):
    209214        """Override to allow: <var> = cfg[<key>]"""
     
    306311# @brief Plot data files.
    307312# @param filenames List of full pathnames to plot.
    308 # @param x_hdr The X axis label string.
    309 # @param y_hdr The Y axis label string.
     313# @param x_hdr The X axis data header string.
     314# @param y_hdr The Y axis data header string.
    310315# @param title The string used to title the graph.
    311316# @param legend True if a legend is to be displayed.
    312317# @param legend_path True if legend is to contain full file paths.
     318# @param grid True if grid is to be displayed.
     319# @param fontsize Point size of font.
     320# @param plot_width Width of plot lines.
     321# @param x_label Override X axis label.
     322# @param y_label Override Y axis label.
    313323def plot_files(filenames, x_hdr, y_hdr, title='', legend=False,
    314                legend_path=False):
     324               legend_path=False, grid=False, fontsize=10,
     325               plot_width=2, x_label=None, y_label=None):
     326    if x_label is None:
     327        x_label = x_hdr
     328    if y_label is None:
     329        y_label = y_hdr
     330       
    315331    pylab.rc('axes', linewidth=2)
    316     pylab.xlabel(x_hdr.title())
    317     pylab.ylabel(y_hdr.title())
    318     pylab.grid(True)
     332    pylab.rc('lines', linewidth=float(plot_width))
     333    pylab.rc(('xtick', 'ytick'), labelsize=float(fontsize)*0.75,)
     334    pylab.rc(('xtick.major', 'ytick.major'), size=5, pad=5)
     335    pylab.rc('axes', labelsize=float(fontsize))
     336    pylab.xlabel(x_label)
     337    pylab.ylabel(y_label)
     338
     339    pylab.grid(grid)
    319340
    320341    for f in filenames:
     
    328349
    329350    if title:
    330         pylab.title(title)
     351        pylab.title(title, fontsize=float(fontsize)*1.5)
     352       
    331353    if legend:
     354        pylab.rc('legend', fancybox=True)
    332355        (min_y, max_y) = pylab.ylim()
    333356        range_y = max_y - min_y
     
    383406                                     size=(100, BUTTON_HEIGHT))
    384407        x = FORM_WIDTH/2 + DOUBLE_BUTTON_OFFSET
    385         self.btnDelCSVFile = wx.Button(p, label="Delete", pos=(x, Y_OFFSET),
     408        self.btnDelCSVFile = wx.Button(p, label="Delete All", pos=(x, Y_OFFSET),
    386409                                     size=(100, BUTTON_HEIGHT))
    387410        Y_OFFSET += BUTTON_HEIGHT + MARGIN
     
    390413                     size=(BOX_PLOT_WIDTH, BOX_PLOT_HEIGHT))
    391414        Y_OFFSET += GEN_DELTAY
     415
    392416        wx.StaticText(p, -1, 'X-Column',
    393417                      pos=(COLLAB_X_OFFSET, Y_OFFSET+LAB_CTRL_OFFSET), style=wx.ALIGN_LEFT)
    394         self.cbXColHdr = wx.Choice(p, -1, pos=(COLLAB_X_OFFSET+65, Y_OFFSET),
     418        self.cbXColHdr = wx.Choice(p, -1, pos=(COLLAB_X_OFFSET+50, Y_OFFSET),
    395419                                   size=(80, -1))
    396420        self.XColHdr = []
    397421        wx.StaticText(p, -1, 'Y-Column',
    398                       pos=(FORM_WIDTH/2+COLLAB_X_OFFSET,
     422                      pos=(FORM_WIDTH/2,
    399423                           Y_OFFSET+LAB_CTRL_OFFSET), style=wx.ALIGN_LEFT)
    400         x = FORM_WIDTH/2 + COLLAB_X_OFFSET
    401         self.cbYColHdr = wx.Choice(p, -1, pos=(x+65, Y_OFFSET), size=(80, -1))
     424        x = FORM_WIDTH/2+50
     425        self.cbYColHdr = wx.Choice(p, -1, pos=(x, Y_OFFSET), size=(80, -1))
    402426        self.YColHdr = []
    403         Y_OFFSET += GEN_DELTAY*2
     427        Y_OFFSET += GEN_DELTAY*1.5
     428
     429        wx.StaticText(p, -1, 'X-Label',
     430                      pos=(COLLAB_X_OFFSET, Y_OFFSET+LAB_CTRL_OFFSET), style=wx.ALIGN_LEFT)
     431        self.txtXLabel = wx.TextCtrl(p, -1, "", size=(100, -1),
     432                                     pos=(COLLAB_X_OFFSET+50,
     433                                          Y_OFFSET+LAB_CTRL_OFFSET),
     434                                     style=wx.ALIGN_LEFT)
     435        wx.StaticText(p, -1, 'Y-Label',
     436                      pos=(FORM_WIDTH/2,
     437                           Y_OFFSET+LAB_CTRL_OFFSET), style=wx.ALIGN_LEFT)
     438        x = FORM_WIDTH/2 + 50
     439        self.txtYLabel = wx.TextCtrl(p, -1, "", size=(100, -1),
     440                                     pos=(x,
     441                                          Y_OFFSET+LAB_CTRL_OFFSET),
     442                                     style=wx.ALIGN_LEFT)
     443        Y_OFFSET += GEN_DELTAY*2.5
    404444
    405445        self.chkLegend = wx.CheckBox(p, -1, " Show graph legend",
     
    407447        self.chkLegendPath = wx.CheckBox(p, -1, " Full file path in legend",
    408448                                         pos=(COLLAB_X_OFFSET+180, Y_OFFSET))       
     449        Y_OFFSET += GEN_DELTAY*2
     450
     451        font_sizes = ['10', '12', '14', '16', '18', '20',
     452                      '22', '24', '26', '28', '30', '32']
     453        self.cbLabFontSize = wx.ComboBox(p, -1, "10",
     454                                         (COLLAB_X_OFFSET+20, Y_OFFSET-5),
     455                                         (47, -1), font_sizes,
     456                                        wx.CB_DROPDOWN)
     457        wx.StaticText(p, -1, 'Label fontsize',
     458                      pos=(COLLAB_X_OFFSET+72, Y_OFFSET),
     459                      style=wx.ALIGN_LEFT)
     460
     461        self.chkShowGrid = wx.CheckBox(p, -1, " Show grid",
     462                                       pos=(COLLAB_X_OFFSET+180, Y_OFFSET))       
     463       
     464        Y_OFFSET += GEN_DELTAY*2
     465
     466        plot_widths = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
     467        self.cbPlotWidth = wx.ComboBox(p, -1, "2",
     468                                       (COLLAB_X_OFFSET+20, Y_OFFSET-5),
     469                                       (47, -1), plot_widths,
     470                                       wx.CB_DROPDOWN)
     471        wx.StaticText(p, -1, 'Plot width',
     472                      pos=(COLLAB_X_OFFSET+72, Y_OFFSET),
     473                      style=wx.ALIGN_LEFT)
    409474        Y_OFFSET += GEN_DELTAY + 3
    410475       
     
    415480                                         Y_OFFSET+LAB_CTRL_OFFSET),
    416481                                    style=wx.ALIGN_LEFT)
    417        
    418         Y_OFFSET += BUTTON_HEIGHT + MARGIN
     482        Y_OFFSET += BUTTON_HEIGHT # + MARGIN
    419483        x = FORM_WIDTH/2 - BUTTON_WIDTH/2
    420484        self.btnPlot = wx.Button(p, label="Plot", pos=(x, Y_OFFSET),
     
    426490        self.Bind(wx.EVT_BUTTON, self.PlotFiles, self.btnPlot)
    427491        self.Bind(wx.EVT_CHECKBOX, self.ChangeLegend, self.chkLegend)
     492        self.Bind(wx.EVT_CHOICE, self.ChangeXLabel, self.cbXColHdr)
     493        self.Bind(wx.EVT_CHOICE, self.ChangeYLabel, self.cbYColHdr)
    428494       
    429495        self.Bind(wx.EVT_CLOSE, self.doStateSave)
     
    434500        self.doStateRestore()
    435501        self.updateHdrChoices()
    436        
     502
     503    def ChangeXLabel(self, event):
     504        sel = self.cbXColHdr.GetCurrentSelection()
     505        self.txtXLabel.SetValue(self.cbXColHdr.GetItems()[sel].title())
     506   
     507    def ChangeYLabel(self, event):
     508        sel = self.cbYColHdr.GetCurrentSelection()
     509        self.txtYLabel.SetValue(self.cbYColHdr.GetItems()[sel].title())
     510   
    437511    def AddCSVFile(self, event):
    438512        """Add a CSV file to the listbox"""
     
    442516                            defaultFile='',
    443517                            wildcard=CSVWildcard,
    444                             style=wx.OPEN | wx.CHANGE_DIR)
     518                            style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR)
    445519
    446520        if dlg.ShowModal() == wx.ID_OK:
    447521            # This returns a list of  files that were selected.
    448             path = dlg.GetPath()
     522            files = dlg.GetFilenames()
    449523            self.file_dir = dlg.GetDirectory()
    450524            dlg.Destroy()
    451             if path not in self.CSVFiles:
    452                 headers = self.getHeaders(path)
    453                 if headers:
    454                     common_headers = self.orHeaders(headers)
    455                     if not common_headers or len(common_headers) < 2:
    456                         self.error("Sorry, file '%s' doesn't have enough headers in common with current files" % path)
     525            for f in files:
     526                path = os.path.join(self.file_dir, f)
     527                if path not in self.CSVFiles:
     528                    headers = self.getHeaders(path)
     529                    if headers:
     530                        common_headers = self.orHeaders(headers)
     531                        if not common_headers or len(common_headers) < 2:
     532                            self.error("Sorry, file '%s' doesn't have enough headers in common with current files" % path)
     533                        else:
     534                            self.headers.append(headers)
     535                            self.common_headers = common_headers
     536                            self.CSVFiles.append(path)
     537                            self.txtCSVFiles.Append(path)
    457538                    else:
    458                         self.headers.append(headers)
    459                         self.common_headers = common_headers
    460                         self.CSVFiles.append(path)
    461                         self.txtCSVFiles.Append(path)
    462                 else:
    463                     self.error("Sorry, file '%s' doesn't appear to be a CSV file" % path)
    464                 self.updateHdrChoices()
     539                        self.error("Sorry, file '%s' doesn't appear to be a CSV file" % path)
     540                    self.updateHdrChoices()
    465541
    466542    def DelCSVFile(self, event):
    467         """Delete a CSV file from the listbox"""
    468 
    469         sel = self.txtCSVFiles.GetSelections()
    470         if sel:
    471             sel = sel[0]
    472             self.txtCSVFiles.Delete(sel)
    473             del self.CSVFiles[sel]
    474             del self.headers[sel]
    475             self.common_headers = self.GenCommonHeaders(self.headers)
    476             self.updateHdrChoices()
    477 
     543        """Delete all CSV files from the listbox"""
     544       
     545        self.txtCSVFiles.Clear()
     546        self.CSVFiles = []
     547        self.common_headers = None
     548        self.updateHdrChoices()
     549       
    478550    def ChangeLegend(self, event):
    479551        if self.chkLegend.GetValue():
     
    494566        self.cfg['GraphLegend'] = self.chkLegend.GetValue()
    495567        self.cfg['LegendPath'] = self.chkLegendPath.GetValue()
     568        self.cfg['ShowGrid'] = self.chkShowGrid.GetValue()
     569        self.cfg['FontSize'] = self.cbLabFontSize.GetValue()
     570        self.cfg['PlotWidth'] = self.cbPlotWidth.GetValue()
     571        self.cfg['XLabel'] = self.txtXLabel.GetValue()
     572        self.cfg['YLabel'] = self.txtYLabel.GetValue()
    496573
    497574        self.cfg.save()
     
    529606        if self.YColHdrSelection >= 0:
    530607            self.cbYColHdr.SetSelection(self.YColHdrSelection)
    531         title = self.cfg['GraphTitle']
    532         if title is None:
    533             title = ''
    534         self.txtTitle.SetValue(title)
    535         if self.cfg['GraphLegend']:
    536             self.chkLegend.SetValue(True)
    537         if self.cfg['LegendPath']:
    538             self.chkLegendPath.SetValue(True)
     608        self.txtTitle.SetValue(self.cfg.get('GraphTitle', ''))
     609        self.chkLegend.SetValue(self.cfg.get('GraphLegend', False))
     610        self.chkLegendPath.SetValue(self.cfg.get('LegendPath', False))
     611        self.cbLabFontSize.SetValue(self.cfg.get('FontSize', '14'))
     612        self.cbPlotWidth.SetValue(self.cfg.get('PlotWidth', '1'))
     613        self.chkShowGrid.SetValue(self.cfg.get('ShowGrid', True))
     614        self.txtXLabel.SetValue(self.cfg.get('XLabel', ''))
     615        self.txtYLabel.SetValue(self.cfg.get('YLabel', ''))
    539616        self.ChangeLegend(None)
    540617       
     
    581658                index = self.common_headers.index(selected_y)
    582659                self.cbYColHdr.SetSelection(index)
     660            self.txtXLabel.Enable()
     661            self.txtYLabel.Enable()
    583662        else:
    584663            self.cbXColHdr.Disable()
    585664            self.cbYColHdr.Disable()
     665            self.txtXLabel.Disable()
     666            self.txtYLabel.Disable()
    586667            self.btnPlot.Disable()
    587668
     
    620701        selected_y = self.cbYColHdr.GetStringSelection()
    621702
     703        x_label = self.txtXLabel.GetValue()
     704        y_label = self.txtYLabel.GetValue()
     705
    622706        if selected_x and selected_y:
     707            grid_on = self.chkShowGrid.GetValue()
     708            fontsize = self.cbLabFontSize.GetValue()
     709            plot_width = self.cbPlotWidth.GetValue()
    623710            plot_files(self.CSVFiles, selected_x, selected_y,
     711                       x_label=x_label, y_label=y_label,
    624712                       title=self.txtTitle.GetValue(),
    625713                       legend=self.chkLegend.GetValue(),
    626                        legend_path=self.chkLegendPath.GetValue())
     714                       legend_path=self.chkLegendPath.GetValue(),
     715                       grid=grid_on, fontsize=fontsize,
     716                       plot_width = plot_width)
     717
    627718            # hide problem with wxPython and matplotlib - close app!
    628719            self.Close(True)
     720        else:
     721            self.error('Sorry, you must select X- and Y-column data values')
    629722           
    630723    def error(self, msg):
Note: See TracChangeset for help on using the changeset viewer.