~caravone/+junk/data

« back to all changes in this revision

Viewing changes to scatter.py

  • Committer: Curtis Caravone
  • Date: 2012-08-21 22:51:30 UTC
  • Revision ID: curtis.caravone@canonical.com-20120821225130-er7x5smsq7e3tu24
Added scripts for generating graphs of filesync performance

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Take two graphite data files and scatter plot x vs. y.
 
3
"""
 
4
 
 
5
import datetime
 
6
import math
 
7
import pytz
 
8
import re
 
9
import sys
 
10
 
 
11
from pylab import *
 
12
from scipy import stats
 
13
 
 
14
import fetcher
 
15
 
 
16
def parse_raw(raw_lines):
 
17
    lines = raw_lines.split("\n")
 
18
 
 
19
    # Sort by numerical values found in the dataset names (if any)
 
20
    def get_nums(s):
 
21
        return [int(x) for x in re.findall(r'\d+', s)]
 
22
 
 
23
    lines.sort(key=get_nums)
 
24
 
 
25
    # print "lines", lines
 
26
    # for line in lines:
 
27
    #     print "\t", line
 
28
 
 
29
    names = []
 
30
    start_end_step = None
 
31
    data = []
 
32
    for raw in [l for l in lines if len(l.strip()) > 0]:
 
33
 
 
34
        # print "raw line", raw
 
35
 
 
36
        (a, b) = raw.split("|")
 
37
        araw = a.split(",")
 
38
        name = araw[0]
 
39
 
 
40
        def to_float(s):
 
41
            try:
 
42
                return float(s)
 
43
            except:
 
44
                return None
 
45
 
 
46
        (start, end, step) = [float(s) for s in araw[-3:]]
 
47
        data1 = [to_float(s) for s in b.split(",")]
 
48
        names.append(name)
 
49
        if start_end_step is not None:
 
50
            assert start_end_step == (start, end, step)
 
51
        else:
 
52
            start_end_step = (start, end, step)
 
53
        data += data1
 
54
    name = ", ".join(names)
 
55
    (start, end, step) = start_end_step
 
56
    return ((name, start, end, step), data)
 
57
 
 
58
def do_plot(xfilespec, yfilespec, label_time):
 
59
    print "xfilespec", xfilespec, "yfilespec", yfilespec
 
60
 
 
61
    # Returns label, scale, ticks, filename
 
62
    def get_params(filespec):
 
63
        params = filespec.split(":")
 
64
        if len(params) < 4:
 
65
            retval = filespec, "1", None, filespec
 
66
        else:
 
67
            retval = params[0:2] + [int(params[2]), ":".join(params[3:])]
 
68
        print "params", retval
 
69
        return retval
 
70
 
 
71
    x_label, x_scale, x_ticks, xfile = get_params(xfilespec)
 
72
    y_label, y_scale, y_ticks, yfile = get_params(yfilespec)
 
73
 
 
74
    print "x,y", (x_label, x_scale, x_ticks, xfile), (y_label, y_scale, y_ticks, yfile)
 
75
 
 
76
    xraw = open(xfile).read()
 
77
    yraw = open(yfile).read()
 
78
 
 
79
    # print "xraw", xraw, "yraw", yraw
 
80
 
 
81
 
 
82
    xdata = parse_raw(xraw)
 
83
    ydata = parse_raw(yraw)
 
84
 
 
85
    # print "xdata", repr(xdata), "ydata", repr(ydata)
 
86
 
 
87
    print "xrange", xdata[0]
 
88
    print "yrange", ydata[0]
 
89
 
 
90
    x = array([n / float(eval(x_scale)) if n else 0.0 for n in xdata[1]])
 
91
    y = array([n / float(eval(y_scale)) if n else 0.0 for n in ydata[1]])
 
92
 
 
93
    print "len(x)", len(x)
 
94
    print "len(y)", len(y)
 
95
 
 
96
    assert xdata[0][1:] == ydata[0][1:]
 
97
    assert len(x) == len(y)
 
98
 
 
99
    t1 = xdata[0][1]
 
100
    t2 = xdata[0][2]
 
101
 
 
102
    # print "t1", t1, "t2", t2
 
103
 
 
104
    t1date = datetime.datetime.fromtimestamp(t1, pytz.timezone("UTC"))
 
105
    t2date = datetime.datetime.fromtimestamp(t2, pytz.timezone("UTC"))
 
106
 
 
107
    drange = "%s to %s" % (t1date, t2date)
 
108
 
 
109
    slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
 
110
 
 
111
    print 'r value', r_value
 
112
    print  'p_value', p_value
 
113
    print 'standard error', std_err
 
114
    print '%s = %s * %s + %s' % (y_label, slope, x_label, intercept)
 
115
 
 
116
    line = slope*x+intercept
 
117
    # plot(x,line,'r-',xi,y,'o')
 
118
    # show()
 
119
 
 
120
    line_label = "y = %.2g + %.2gx (r=%0.2f)" % (intercept, slope, r_value)
 
121
    print "line_label", line_label
 
122
 
 
123
    x_min, x_max, y_min, y_max = amin(x), amax(x), amin(y), amax(y)
 
124
 
 
125
    # scatter([1,2,3], [2,4,5], s=1, facecolor='0.5', lw = 0)
 
126
    plot(x, y, 'k.', alpha=0.1)
 
127
    plot(x, line, 'r-')
 
128
    axis([x_min, x_max, y_min, y_max])
 
129
 
 
130
    def filter_ticks(t, n, min_val, max_val):
 
131
        rawloc, lab = t
 
132
 
 
133
        print "rawloc", rawloc
 
134
        print "labels", lab
 
135
 
 
136
        loc = [x for x in rawloc if min_val <= x <= max_val]
 
137
 
 
138
        print "filtered loc", loc
 
139
 
 
140
        inc = max(len(rawloc) / n, 1)
 
141
 
 
142
        print "inc", inc
 
143
 
 
144
        print "filtered loc", loc[::inc]
 
145
 
 
146
        return loc[::inc]
 
147
 
 
148
    if x_ticks:
 
149
        xticks(filter_ticks(xticks(), x_ticks, x_min, x_max))
 
150
    if y_ticks:
 
151
        yticks(filter_ticks(yticks(), y_ticks, y_min, y_max))
 
152
 
 
153
    t = "%s vs. %s" % (y_label, x_label)
 
154
    if label_time:
 
155
        t = "%s\n%s" % (drange, t)
 
156
        title(t)
 
157
 
 
158
    # if label_time:
 
159
    #     title(drange + "\nfilesync activity")
 
160
 
 
161
    text(.98 * x_min + .02 * x_max, .93 * y_max + .07 * y_min, line_label,
 
162
         color="r", weight="semibold", size="9")
 
163
    xlabel(x_label)
 
164
    ylabel(y_label)
 
165
 
 
166
def main(argv):
 
167
    if len(argv) % 2 != 1:
 
168
        print "Usage: %s file1 file2 ..." % argv[0]
 
169
        print "Or:    %s label:scale:ticks:file1..."
 
170
        sys.exit(1)
 
171
 
 
172
    pairs = zip(argv[1::2], argv[2::2])
 
173
 
 
174
    print "Plotting pairs", pairs
 
175
 
 
176
    rows = int(math.sqrt(len(pairs)))
 
177
    columns = math.ceil(len(pairs) / float(rows))
 
178
    plotnum = 1
 
179
 
 
180
    # figure(figsize=(17,8.8))
 
181
    figure(figsize=(17*.8,8.8*.8))
 
182
 
 
183
    label_time = True
 
184
    for i, j in pairs:
 
185
 
 
186
        print "Adding plot %s on %s x %s grid" % (plotnum, rows, columns)
 
187
 
 
188
        subplot(rows, columns, plotnum)
 
189
        do_plot(i, j, label_time)
 
190
        plotnum += 1
 
191
        label_time = False
 
192
 
 
193
    subplots_adjust(hspace=.35, wspace=.35)
 
194
 
 
195
if __name__ == '__main__':
 
196
    main(sys.argv)
 
197
    show()
 
198