Mercurial > hg > numerics
changeset 29:c2c92c8da611
add simple smoother
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 09 Oct 2014 15:01:01 -0700 |
parents | 2cadb4349753 |
children | 75270e7a051b |
files | numerics/smooth.py setup.py tests/simple_data.txt |
diffstat | 3 files changed, 81 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/numerics/smooth.py Thu Oct 09 15:01:01 2014 -0700 @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +smoothing +""" + +# imports +import argparse +import os +import subprocess +import sys + +# module globals +__all__ = ['main', 'smooth', 'SmoothingParser'] + +def smooth(iterations, *values): + assert len(values) >= 2 + while iterations > 0: + inner = [sum(i) for i in zip(values[1:], values[:-1])] + left = [inner[0]] + inner[:] + right = inner[:] + [inner[-1]] + values = [0.25*sum(i) for i in zip(left, right)] + iterations -= 1 + return values + +class SmoothingParser(argparse.ArgumentParser): + """`smooth` CLI option parser""" + def __init__(self, **kwargs): + kwargs.setdefault('description', __doc__) + argparse.ArgumentParser.__init__(self, **kwargs) + self.add_argument('input', nargs='?', + type=argparse.FileType('r'), default=sys.stdin, + help='input file, or read from stdin if ommitted') + self.add_argument('-o', '--output', dest='output', + type=argparse.FileType('a'), default=sys.stdout, + help="output file to write to, or stdout") + self.add_argument('-n', '--iterations', dest='iterations', + type=int, default=1, + help="number of iterations to apply [DEFAULT: %(default)s]") + self.options = None + + def parse_args(self, *args, **kw): + options = argparse.ArgumentParser.parse_args(self, *args, **kw) + self.validate(options) + self.options = options + return options + + def validate(self, options): + """validate options""" + +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line options + parser = SmoothingParser() + options = parser.parse_args(args) + + # read data + data = options.input.read().strip().split() + data = [float(i) for i in data] + + smoothed = smooth(options.iterations, *data) + + # write data + options.output.write('\n'.join([str(i) for i in smoothed])) + +if __name__ == '__main__': + main() +
--- a/setup.py Wed Oct 08 11:12:28 2014 -0700 +++ b/setup.py Thu Oct 09 15:01:01 2014 -0700 @@ -19,6 +19,7 @@ interpolate = numerics.interpolation:main plot = numerics.plot:main read-csv = numerics.read:main + smooth = numerics.smooth:main types = numerics.convert:main """ kw['install_requires'] = dependencies