|
1 | | -#!/usr/bin/python |
| 1 | +#!/usr/bin/env python |
| 2 | + |
2 | 3 | import math |
3 | 4 | import optparse |
| 5 | +import os |
| 6 | +from os.path import dirname |
4 | 7 | import sys |
5 | 8 | from utils.helpers import * |
6 | 9 | from utils.commandhelp import hist |
@@ -29,32 +32,47 @@ def read_numbers(numbers): |
29 | 32 | for n in open(numbers): |
30 | 33 | yield float(n.strip()) |
31 | 34 |
|
| 35 | + |
32 | 36 | def run_demo(): |
33 | | - "demo the product" |
| 37 | + """ |
| 38 | + Run a demonstration |
| 39 | + """ |
| 40 | + module_dir = dirname(dirname(os.path.realpath(__file__))) |
| 41 | + demo_file = os.path.join(module_dir, 'examples/data/exp.txt') |
| 42 | + |
| 43 | + if not os.path.isfile(demo_file): |
| 44 | + sys.stderr.write("demo input file not found!\n") |
| 45 | + sys.stderr.write("run the downloaddata.sh script in the example first\n") |
| 46 | + sys.exit(1) |
| 47 | + |
34 | 48 | #plotting a histogram |
35 | 49 | print "plotting a basic histogram" |
36 | | - print "plot_hist('./data/exp.txt')" |
37 | | - print "hist -f ./data/exp.txt" |
38 | | - print "cat ./data/exp.txt | hist" |
39 | | - plot_hist('./data/exp.txt') |
40 | | - print "*"*80 |
| 50 | + print "plot_hist('%s')" % demo_file |
| 51 | + print "hist -f %s" % demo_file |
| 52 | + print "cat %s | hist" % demo_file |
| 53 | + plot_hist(demo_file) |
| 54 | + print "*" * 80 |
| 55 | + |
41 | 56 | #with colors |
42 | 57 | print "histogram with colors" |
43 | | - print "plot_hist('./data/exp.txt', colour='blue')" |
44 | | - print "hist -f ./data/exp.txt -c blue" |
45 | | - plot_hist('./data/exp.txt', colour='blue') |
46 | | - print "*"*80 |
| 58 | + print "plot_hist('%s', colour='blue')" % demo_file |
| 59 | + print "hist -f %s -c blue" % demo_file |
| 60 | + plot_hist(demo_file, colour='blue') |
| 61 | + print "*" * 80 |
| 62 | + |
47 | 63 | #changing the shape of the point |
48 | 64 | print "changing the shape of the bars" |
49 | | - print "plot_hist('./data/exp.txt', pch='.')" |
50 | | - print "hist -f ./data/exp.txt -p ." |
51 | | - plot_hist('./data/exp.txt', pch='.') |
52 | | - print "*"*80 |
| 65 | + print "plot_hist('%s', pch='.')" % demo_file |
| 66 | + print "hist -f %s -p ." % demo_file |
| 67 | + plot_hist(demo_file, pch='.') |
| 68 | + print "*" * 80 |
| 69 | + |
53 | 70 | #changing the size of the plot |
54 | 71 | print "changing the size of the plot" |
55 | | - print "plot_hist('./data/exp.txt', height=35.0, bincount=40)" |
56 | | - print "hist -f ./data/exp.txt -s 35.0 -b 40" |
57 | | - plot_hist('./data/exp.txt', height=35.0, bincount=40) |
| 72 | + print "plot_hist('%s', height=35.0, bincount=40)" % demo_file |
| 73 | + print "hist -f %s -s 35.0 -b 40" % demo_file |
| 74 | + plot_hist(demo_file, height=35.0, bincount=40) |
| 75 | + |
58 | 76 |
|
59 | 77 | def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="white", title="", xlab=None, showSummary=False, regular=False): |
60 | 78 | """make a histogram |
@@ -171,7 +189,7 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="whi |
171 | 189 | print summary |
172 | 190 |
|
173 | 191 |
|
174 | | -if __name__=="__main__": |
| 192 | +def main(): |
175 | 193 |
|
176 | 194 | parser = optparse.OptionParser(usage=hist['usage']) |
177 | 195 |
|
@@ -208,3 +226,6 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="whi |
208 | 226 | else: |
209 | 227 | print "nothing to plot!" |
210 | 228 |
|
| 229 | + |
| 230 | +if __name__=="__main__": |
| 231 | + main() |
0 commit comments