changeset 76:02f586a9defe

module for centroids
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 13:33:56 -0800
parents a17d43d7bf1b
children 20a2970d4510
files tvii/centroid.py
diffstat 1 files changed, 42 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tvii/centroid.py	Sun Dec 17 13:33:56 2017 -0800
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+"""
+find centroid of a set of points
+"""
+
+import csv
+import sys
+from .cli import NettwerkParser
+from .read import read
+from .transpose import transpose
+
+def mean(x):
+    """arithemtic mean"""
+    return sum(x)/float(len(x))
+
+
+def centroid(*points):
+    """find centroid of a set of points"""
+
+    return [mean(dimension) for dimension in transpose(points)]
+
+
+def main(args=sys.argv[1:]):
+    """CLI"""
+
+    # parse command line
+    parser = NettwerkParser(description=__doc__)
+    parser.add_argument('points', type=read,
+                        help="set of points")
+    options = parser.parse_args(args)
+
+    # find centroid
+    c = centroid(*options.points)
+
+    # output
+    writer = csv.writer(sys.stdout)
+    writer.writerow(c)
+
+
+if __name__ == '__main__':
+    main()