-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstats.py
More file actions
executable file
·95 lines (70 loc) · 2.22 KB
/
stats.py
File metadata and controls
executable file
·95 lines (70 loc) · 2.22 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
DEFAULT_DEGREE = 3
DEFAULT_ESTIMATE = 27
PLOT_RESOLUTION = 100
def fit(data, deg=DEFAULT_DEGREE, xaxis=None):
print xaxis
print data
if xaxis is not None:
x = np.array(xaxis)
else:
x = np.arange(len(data))
y = data
deg = min(deg, len(x)-1)
poly = np.polyfit(x, y, deg)
return np.poly1d(poly)
def plot_fit(data, project=None, deg=DEFAULT_DEGREE, xaxis=None):
if xaxis is not None:
x = np.array(xaxis)
else:
x = np.arange(len(data))
fig = plt.figure()
p = fig.add_subplot(111)
fit_end = project or x[-1]*2
print "Projecting to:", fit_end, "project:", project
p.set_xlim([0, fit_end])
x_fit = np.linspace(0, fit_end, PLOT_RESOLUTION)
y_fit = fit(data, deg, xaxis)(x_fit)
p.plot(xaxis or np.arange(len(data)), data, 'yo', x_fit, y_fit, '-r' )
return fig
def _at(lst, index, default=None):
return next(iter(lst[index:]), default)
def _main():
"""
usage
script_name {plot [image_file_name [projection_max]] | estimate [estimate length]} [degree]
"""
import sys
sys.argv.pop(0)
_tmp = zip(*[map(float, l.split()) for l in sys.stdin.readlines()])
if len(_tmp) > 1:
data = _at(_tmp, 1)
xdata = _at(_tmp, 0)
else:
data = _at(_tmp, 0)
xdata = None
cmd = _at(sys.argv, 0, 'estimate')
deg = _at(sys.argv, 3, DEFAULT_DEGREE)
if not data:
sys.stderr.write("No data provided\n")
exit(1)
if cmd == 'plot':
fimg = _at(sys.argv, 1)
prj_max = _at(sys.argv, 2)
if prj_max:
prj_max = int(prj_max)
print "Image file:", fimg, "projection:", prj_max
if fimg and fimg != '-':
plot_fit(data, project=prj_max, xaxis=xdata, deg=deg).savefig(fimg)
else:
plot_fit(data, project=prj_max, deg=deg, xaxis=xdata).show()
import signal; signal.pause()
elif cmd == 'estimate':
x = _at(sys.argv, 1, DEFAULT_ESTIMATE)
sys.stdout.write("%f\n" % fit(data, xaxis=xdata, deg=deg)(int(x)))
else:
sys.stderr.write("Unknown command %s" % cmd)
if __name__ == "__main__":
_main()