changeset 101:b7d4b7f84883

simple script to compute means
author Jeff Hammel <k0scist@gmail.com>
date Fri, 06 Mar 2015 09:29:29 -0800
parents e1fec09da207
children 1b0854ee78e0
files numerics/mean.py numerics/read.py setup.py tests/simple_data.csv
diffstat 4 files changed, 34 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/numerics/mean.py	Fri Mar 06 08:20:53 2015 -0800
+++ b/numerics/mean.py	Fri Mar 06 09:29:29 2015 -0800
@@ -2,66 +2,45 @@
 # -*- coding: utf-8 -*-
 
 """
-compute mean of columns
+compute mean of data
 """
 
 # imports
 import argparse
+import csv
 import os
-import subprocess
 import sys
-import time
-
-# module globals
-__all__ = ['main', 'Parser']
-here = os.path.dirname(os.path.realpath(__file__))
-string = (str, unicode)
-
-def ensure_dir(directory):
-    """ensure a directory exists"""
-    if os.path.exists(directory):
-        if not os.path.isdir(directory):
-            raise OSError("Not a directory: '{}'".format(directory))
-        return directory
-    os.makedirs(directory)
-    return directory
-
+from .data import transpose
+from .filters import mean
+from .read import CSVParser
+from .write import CSVWriter
 
-class Parser(argparse.ArgumentParser):
-    """CLI option parser"""
-    def __init__(self, **kwargs):
-        kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
-        kwargs.setdefault('description', __doc__)
-        argparse.ArgumentParser.__init__(self, **kwargs)
-        self.add_argument('--monitor', dest='monitor',
-                          type=float, metavar='SLEEP',
-                          help="run in monitor mode")
-        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 = Parser()
+    parser = CSVParser()
     options = parser.parse_args(args)
 
-    try:
-        while True:
-            if options.monitor:
-                time.sleep(options.monitor)
-            else:
-                break
-    except KeyboardInterrupt:
-        pass
+    # read data
+    data = parser.read()
+    if not data:
+        parser.error("No data given")
+
+    # transpose to columns
+    columns = transpose(data)
+
+    # cast to float
+    columns = [[float(value) for value in column]
+               for column in columns]
+
+    # means
+    means = [mean(column) for column in columns]
+
+    # write CSV
+    writer = CSVWriter(options.output)
+    writer.write([means])
 
 if __name__ == '__main__':
     main()
--- a/numerics/read.py	Fri Mar 06 08:20:53 2015 -0800
+++ b/numerics/read.py	Fri Mar 06 09:29:29 2015 -0800
@@ -115,6 +115,9 @@
     def validate(self, options):
         """validate options"""
 
+        if not options.csv:
+            options.csv = [sys.stdin]
+
     def read(self):
         """read and process CSV"""
 
--- a/setup.py	Fri Mar 06 08:20:53 2015 -0800
+++ b/setup.py	Fri Mar 06 09:29:29 2015 -0800
@@ -21,6 +21,7 @@
     histogram = numerics.histogram:main
     interpolate = numerics.interpolation:main
     manipulate = numerics.manipulate:main
+    mean = numerics.mean:main
     plot = numerics.plot:main
     read-csv = numerics.read:main
     smooth = numerics.smooth:main
@@ -29,7 +30,7 @@
 """
 # TODO:
 #   cleanse = numerics.clean:main  # cleans up outliers
-#   fold = numerics.fold:main
+#   fold = numerics.fold:main  # fold data through functions (reduce)
     kw['install_requires'] = dependencies
 except ImportError:
     from distutils.core import setup
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/simple_data.csv	Fri Mar 06 09:29:29 2015 -0800
@@ -0,0 +1,4 @@
+1.2
+2.4
+3.6
+4.8