Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make regular expression a named instead of positional argument, and p…

…rovide a default that matches floating point numbers. Bump version number.
  • Loading branch information...
commit 8b3307fe0e5b0d38e0be5a014e2882b5fcdea590 1 parent 4189f3b
@lmjohns3 authored
Showing with 24 additions and 25 deletions.
  1. +13 −10 README.md
  2. +10 −14 scripts/py-grep-plot
  3. +1 −1  setup.py
View
23 README.md
@@ -38,12 +38,13 @@ see a nice little plot appear.
The general usage of the script is
- py-grep-plot [regexp] < file
+ py-grep-plot [--regex RE] < FILE
-Basically, you provide a bunch of data on stdin, and a regular expression that
-specifies how to extract data from the files. The plotting script will check the
-regular expression against each input line, parsing out numerical values from
-those that match. Each matched value will be included in the plot.
+Basically, you provide a bunch of data on stdin, and, optionally, a regular
+expression that specifies how to extract data from the files. The plotting
+script will check the regular expression against each input line, parsing out
+numerical values from those that match. Each matched value will be included in
+the plot.
### Multiple values
@@ -52,7 +53,8 @@ values will be plotted on the ordinate, in data-file order. If you want explicit
control over the abscissa, just include another match group in your regular
expression:
- nl ~/Experiments/tagger-beam1.log | py-grep-plot '^(\d+) .* training accuracy: ([.\d]+)'
+ nl ~/Experiments/tagger-beam1.log | \
+ py-grep-plot -r '^(\d+) .* training accuracy: ([.\d]+)'
(The `nl` utility numbers the lines of the input file.)
@@ -65,7 +67,7 @@ error bar along the ordinate.
You can also provide multiple input files, and the script will show multiple
data series on the same plot:
- py-grep-plot [regexp] [file]...
+ py-grep-plot [--regex RE] FILE...
Each file will use the same regular expression for matching data.
@@ -77,7 +79,7 @@ that contains `training accuracy: XX` and `evaluation accuracy: XX` lines that
you'd like to plot. You can use the nifty subshell redirection operator to sort
these two data sources:
- py-grep-plot 'accuracy: (\d+)' \
+ py-grep-plot -r 'accuracy: (\d+)' \
<(grep 'training acc' file) \
<(grep 'evaluation acc' file)
@@ -91,8 +93,9 @@ mean and standard deviation of each group.
### Other options
-There are several other command-line options, including control over the plot
-colors and styles, X- and Y-axis limits, ; use `--help` to get an overview.
+There are several other command-line options, including some basic controls for
+the plot colors and styles, X- and Y-axis limits, ; use `--help` to get an
+overview.
## License
View
24 scripts/py-grep-plot
@@ -34,14 +34,14 @@ import sys
from matplotlib import pyplot
-FLAGS = optparse.OptionParser('Usage: grep-plot [OPTIONS] REGEX FILE...')
+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,c,b,g,m,y', metavar='S,S,...',
- help='cycle through the given colors (k,r,c,b,g,m,y)')
+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]',
@@ -52,6 +52,8 @@ 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',
@@ -149,19 +151,13 @@ def read_input(args):
yield os.path.splitext(os.path.basename(filename))[0], handle
-def compile_regex(args):
+def compile_regex(opts):
'''Compile a regular expression pattern.'''
+ logging.debug('compiling REGEX %r', opts.regex)
try:
- pattern = args[0]
- del args[0]
- except IndexError:
- FLAGS.error('no REGEX supplied')
- sys.exit(-1)
- logging.debug('compiling REGEX %r', pattern)
- try:
- return re.compile(pattern)
+ return re.compile(opts.regex)
except:
- logging.critical('cannot compile REGEX %r', pattern)
+ logging.critical('cannot compile REGEX %r', opts.regex)
sys.exit(-2)
@@ -209,7 +205,7 @@ def main(opts, args):
colors = [c.strip() for c in opts.colors.split(',')]
points = [s.strip() for s in opts.points.split(',')]
- regex = compile_regex(args)
+ regex = compile_regex(opts)
ax = make_axes(opts)
plot_kwargs = dict(alpha=opts.alpha, aa=True)
View
2  setup.py
@@ -2,7 +2,7 @@
setuptools.setup(
name='lmj.plot',
- version='0.1',
+ version='0.2',
install_requires=['numpy', 'matplotlib'],
scripts=['scripts/py-grep-plot'],
author='Leif Johnson',
Please sign in to comment.
Something went wrong with that request. Please try again.