forked from georgek/bio-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaverage.py
executable file
·73 lines (62 loc) · 2.09 KB
/
average.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
# gives average of selected column
import sys
import os.path
import argparse
def mean(list):
return sum(list)/len(list)
def median(list):
if len(list)%2:
return sorted(list)[len(list)/2]
else:
return (sorted(list)[len(list)/2-1]+sorted(list)[len(list)/2])/2
def mode(list):
mode = None
modecount = 0
current = None
currentcount = 0
for item in sorted(list):
if item == current:
currentcount += 1
else:
if currentcount > modecount:
mode = current
modecount = currentcount
current = item
currentcount = 1
return mode if currentcount < modecount else current
# ----- command line parsing -----
parser = argparse.ArgumentParser(
description="Calculates average (mean, median, or mode) of column.")
parser.add_argument("column_number", type=int, help="The column number.")
parser.add_argument("-d", "--delimiter", default='\t')
parser.add_argument("-f", "--default", type=float, default=0.0)
avg_type = parser.add_mutually_exclusive_group()
avg_type.add_argument("-a", "--mean",
action="store_const", dest="avg_type", const="mean",
help="Calculate mean.")
avg_type.add_argument("-e", "--median",
action="store_const", dest="avg_type", const="median",
help="Calculate median.")
avg_type.add_argument("-o", "--mode",
action="store_const", dest="avg_type", const="mode",
help="Calculate mode.")
parser.set_defaults(avg_type="mean")
args = parser.parse_args()
# ----- end command line parsing -----
dl = args.delimiter.decode("string_escape")
values = []
for line in sys.stdin:
if len(line) > 1:
try:
values.append(float(line[:-1].split(dl)[args.column_number - 1]))
except IndexError as e:
values.append(args.default)
if len(values) == 0:
print 0
elif args.avg_type == "median":
print median(values)
elif args.avg_type == "mode":
print mode(values)
else:
print mean(values)