annotate numerics/smooth.py @ 66:7dd1b18c9f78

minor cleanup and better error
author Jeff Hammel <k0scist@gmail.com>
date Sat, 28 Feb 2015 13:51:40 -0800
parents c2c92c8da611
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 smoothing
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 # imports
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import os
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import subprocess
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import sys
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 # module globals
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 __all__ = ['main', 'smooth', 'SmoothingParser']
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 def smooth(iterations, *values):
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 assert len(values) >= 2
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 while iterations > 0:
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 inner = [sum(i) for i in zip(values[1:], values[:-1])]
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 left = [inner[0]] + inner[:]
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 right = inner[:] + [inner[-1]]
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 values = [0.25*sum(i) for i in zip(left, right)]
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 iterations -= 1
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 return values
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 class SmoothingParser(argparse.ArgumentParser):
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 """`smooth` CLI option parser"""
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 def __init__(self, **kwargs):
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 kwargs.setdefault('description', __doc__)
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 argparse.ArgumentParser.__init__(self, **kwargs)
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 self.add_argument('input', nargs='?',
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 type=argparse.FileType('r'), default=sys.stdin,
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 help='input file, or read from stdin if ommitted')
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 self.add_argument('-o', '--output', dest='output',
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 type=argparse.FileType('a'), default=sys.stdout,
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 help="output file to write to, or stdout")
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 self.add_argument('-n', '--iterations', dest='iterations',
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 type=int, default=1,
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 help="number of iterations to apply [DEFAULT: %(default)s]")
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 self.options = None
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 def parse_args(self, *args, **kw):
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 self.validate(options)
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 self.options = options
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 return options
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 def validate(self, options):
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 """validate options"""
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 def main(args=sys.argv[1:]):
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 """CLI"""
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 # parse command line options
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 parser = SmoothingParser()
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 options = parser.parse_args(args)
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 # read data
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 data = options.input.read().strip().split()
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 data = [float(i) for i in data]
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 smoothed = smooth(options.iterations, *data)
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 # write data
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66 options.output.write('\n'.join([str(i) for i in smoothed]))
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 if __name__ == '__main__':
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69 main()
c2c92c8da611 add simple smoother
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70