Browse files

Partition command-line options into groups. Add an option to plot num…

…bers from columnar data files.
  • Loading branch information...
1 parent e220f84 commit 23fb11da2b65656d09c8ff1abf79ea5fb99c041c Leif Johnson committed May 3, 2012
Showing with 73 additions and 43 deletions.
  1. +73 −43 scripts/py-grep-plot
View
116 scripts/py-grep-plot
@@ -36,36 +36,47 @@ from matplotlib import pyplot
FLAGS = optparse.OptionParser('Usage: py-grep-plot [OPTIONS] [FILE...]')
-FLAGS.add_option('-a', '--alpha', type=float, default=0.9, metavar='N',
- help='plot series with alpha N (0.9)')
-FLAGS.add_option('-b', '--batch', type=int, metavar='N',
- help='batch data into groups of N points and plot mean + std')
-FLAGS.add_option('-c', '--colors', default='k,r,#888,c,b,m,g,y', metavar='S,S,...',
- help='cycle through the given colors (k,r,#888,c,b,m,g,y)')
-FLAGS.add_option('-g', '--grid', default=False, action='store_true',
- help='include a grid (False)')
-FLAGS.add_option('-L', '--legend', metavar='[tl|cl|bl|tr|cr|br]',
- help='include a legend (None)')
-FLAGS.add_option('-l', '--log', default='', metavar='[x|y|xy]',
- help='use a log scale on the specified axes')
-FLAGS.add_option('-o', '--output', metavar='FILE',
- help='save to FILE instead of displaying on screen')
-FLAGS.add_option('-p', '--points', default='o-', metavar='S,S,...',
- help='cycle through the given line/point styles (o-)')
FLAGS.add_option('-r', '--regex', default=r'([-+eE.\d]+)', metavar='RE',
help='extract data points from FILEs with RE ([-+eE.\d]+)')
-FLAGS.add_option('-s', '--smooth', type=int, default=0, metavar='N',
- help='smooth across N points before plotting (0)')
-FLAGS.add_option('-t', '--title', metavar='S',
- help='use S as the plot title')
-FLAGS.add_option('-x', '--xlabel', metavar='S',
- help='use S as the label for the x-axis')
-FLAGS.add_option('-y', '--ylabel', metavar='S',
- help='use S as the label for the y-axis')
-FLAGS.add_option('-X', '--xlim', metavar='A,B',
- help='use (A,B) as the range for the x-axis')
-FLAGS.add_option('-Y', '--ylim', metavar='A,B',
- help='use (A,B) as the range for the y-axis')
+FLAGS.add_option('-k', '--column', action='append', type=int, metavar='K',
+ help='extract data from the Kth space-separated column')
+FLAGS.add_option('-o', '--output', metavar='FILE',
+ help='save to FILE instead of displaying on screen')
+
+g = optparse.OptionGroup(FLAGS, 'Data Options')
+g.add_option('-b', '--batch', type=int, metavar='N',
+ help='batch data into groups of N points and plot mean + std')
+g.add_option('-s', '--smooth', type=int, default=0, metavar='N',
+ help='smooth across N points before plotting (0)')
+FLAGS.add_option_group(g)
+
+g = optparse.OptionGroup(FLAGS, 'Series Options')
+g.add_option('-a', '--alpha', type=float, default=0.9, metavar='N',
+ help='plot series with alpha N (0.9)')
+g.add_option('-c', '--colors', default='k,r,#666666,c,b,m,g,y', metavar='S,S,...',
+ help='cycle through the given colors (k,r,#666666,c,b,m,g,y)')
+g.add_option('-p', '--points', default='o-', metavar='S,S,...',
+ help='cycle through the given line/point styles (o-)')
+FLAGS.add_option_group(g)
+
+g = optparse.OptionGroup(FLAGS, 'Axis Options')
+g.add_option('-g', '--grid', default=False, action='store_true',
+ help='include a grid (False)')
+g.add_option('-L', '--legend', metavar='[tl|cl|bl|tr|cr|br]',
+ help='include a legend (None)')
+g.add_option('-l', '--log', default='', metavar='[x|y|xy]',
+ help='use a log scale on the specified axes')
+g.add_option('-t', '--title', metavar='S',
+ help='use S as the plot title')
+g.add_option('-x', '--xlabel', metavar='S',
+ help='use S as the label for the x-axis')
+g.add_option('-y', '--ylabel', metavar='S',
+ help='use S as the label for the y-axis')
+g.add_option('-X', '--xlim', metavar='A,B',
+ help='use (A,B) as the range for the x-axis')
+g.add_option('-Y', '--ylim', metavar='A,B',
+ help='use (A,B) as the range for the y-axis')
+FLAGS.add_option_group(g)
LEGEND = {
@@ -82,6 +93,19 @@ LEGEND = {
}
+def extract_columns(data, columns, x, y, ey):
+ '''Pull specific column values out to plot.'''
+ if len(columns) == 1:
+ y.append(float(data[columns[0]]))
+ if len(columns) == 2:
+ x.append(float(data[columns[0]]))
+ y.append(float(data[columns[1]]))
+ if len(columns) == 3:
+ x.append(float(data[columns[0]]))
+ y.append(float(data[columns[1]]))
+ ey.append(float(data[columns[2]]))
+
+
def extract_groupdict(g, x, y, ey):
'''We've matched a line with named groups. Extract data from them.'''
logging.debug('group dict: %r', g)
@@ -99,20 +123,7 @@ def extract_groupdict(g, x, y, ey):
ey.append(float(g['ey']))
-def search_line(line, regex, x, y, ey):
- '''Search an input line for groups matching the given regex.
-
- Extracted data will be added to the mutable x, y, and/or ey sequences.
- '''
- m = regex.search(line)
- if not m:
- return
-
- g = m.groupdict()
- if g:
- return extract_groupdict(g, x, y, ey)
-
- g = m.groups()
+def extract_groups(g, x, y, ey):
logging.debug('group matches: %r', g)
if len(g) > 3:
FLAGS.error('REGEX cannot match more than 3 values')
@@ -133,6 +144,25 @@ def search_line(line, regex, x, y, ey):
y.append(float(g[0]))
+def search_line(line, regex, columns, x, y, ey):
+ '''Search an input line for groups matching the given regex.
+
+ Extracted data will be added to the mutable x, y, and/or ey sequences.
+ '''
+ if columns:
+ return extract_columns(line.split(), columns, x, y, ey)
+
+ m = regex.search(line)
+ if not m:
+ return
+
+ g = m.groupdict()
+ if g:
+ return extract_groupdict(g, x, y, ey)
+
+ extract_groups(m.groups(), x, y, ey)
+
+
def read_input(args):
'''Given input pattern arguments, open up corresponding files.'''
if not args:
@@ -236,7 +266,7 @@ def main(opts, args):
for label, lines in read_input(args):
x, y, ey = [], [], []
for line in lines:
- search_line(line, regex, x, y, ey)
+ search_line(line, regex, opts.column, x, y, ey)
plot(label, x, y, ey)
c = (c + 1) % len(colors)
p = (p + 1) % len(points)

0 comments on commit 23fb11d

Please sign in to comment.