source: trunk/bin/graph @ 10

Last change on this file since 10 was 10, checked in by tim, 9 years ago

.

  • Property svn:executable set to *
File size: 11.5 KB
Line 
1#!/usr/bin/env python3
2
3import sys
4import os
5import time
6import random
7import tempfile
8import argparse
9import socket
10import json
11
12import numpy
13import matplotlib.mlab as mlab
14import matplotlib.pyplot as plt
15
16
17VERSION = "{DEVELOPMENT}"
18if VERSION == "{DEVELOPMENT}":
19    script_dir = '.'
20    try:
21        script_dir = os.path.dirname(os.path.realpath(__file__))
22    except:
23        try:
24            script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
25        except:
26            pass
27    sys.path.append("%s/../lib" % script_dir)
28
29from nanownlib import *
30from nanownlib.stats import *
31import nanownlib.storage
32
33
34parser = argparse.ArgumentParser(
35    description="")
36parser.add_argument('db_file', default=None,
37                    help='')
38options = parser.parse_args()
39db = nanownlib.storage.db(options.db_file)
40
41
42def differences(db, unusual_case, column='packet_rtt'):
43    cursor = db.conn.cursor()
44    query="""
45      SELECT %(column)s-(SELECT avg(%(column)s) FROM probes,analysis
46                         WHERE analysis.probe_id=probes.id AND probes.test_case!=:unusual_case AND probes.type in ('train','test') AND sample=u.sample)
47      FROM (SELECT probes.sample,%(column)s FROM probes,analysis
48                         WHERE analysis.probe_id=probes.id AND probes.test_case =:unusual_case AND probes.type in ('train','test')) u
49      """ % {"column":column}
50    params = {"unusual_case":unusual_case}
51    cursor.execute(query, params)
52    for row in cursor:
53        yield row[0]
54
55
56def timeSeries(db, probe_type, unusual_case):
57    cursor = db.conn.cursor()
58    query="""
59      SELECT time_of_day,packet_rtt AS uc,(SELECT avg(packet_rtt) FROM probes,analysis
60                                           WHERE analysis.probe_id=probes.id AND probes.test_case!=:unusual_case AND probes.type=:probe_type AND sample=u.sample) AS oc
61      FROM (SELECT time_of_day,probes.sample,packet_rtt FROM probes,analysis
62                                           WHERE analysis.probe_id=probes.id AND probes.test_case =:unusual_case AND probes.type=:probe_type) u
63    """
64   
65    params = {"probe_type":probe_type,"unusual_case":unusual_case}
66    cursor.execute(query, params)
67    for row in cursor:
68        yield {'time_of_day':row['time_of_day'],unusual_case:row['uc'],'other_cases':row['oc']}
69#samples,derived,null_derived = parse_data(input1)
70
71#trust = trustValues(derived, sum)
72#weights = linearWeights(derived, trust, 0.25)
73#print('(test): %f' % weightedMean(derived,weights))
74
75diffs = list(differences(db, 'long'))
76reported_diffs = list(differences(db, 'long', 'reported'))
77#shorts = [s['packet_rtt'] for s in samples.values() if s['test_case']=='short']
78#longs = [s['packet_rtt'] for s in samples.values() if s['test_case']=='long']
79
80short_overtime = [(sample['time_of_day'],sample['short']) for sample in timeSeries(db,'train','short')]
81long_overtime = [(sample['time_of_day'],sample['long']) for sample in timeSeries(db,'train','long')]
82diff_overtime = [(sample['time_of_day'],sample['long']-sample['other_cases']) for sample in timeSeries(db,'train','long')]
83short_overtime.sort()
84long_overtime.sort()
85diff_overtime.sort()
86
87print('packet_rtt diff median: %f' % statistics.median(diffs))
88print('packet_rtt diff midhinge: %f' % midsummary(diffs))
89print('packet_rtt diff trimean: %f' % trimean(diffs))
90print('packet_rtt diff quadsummary: %f' % quadsummary(diffs))
91print('packet_rtt diff ubersummary: %f' % ubersummary(diffs))
92print('packet_rtt diff MAD: %f' % mad(diffs))
93print('reported diff trimean: %f' % trimean(reported_diffs))
94print('reported diff quadsummary: %f' % quadsummary(reported_diffs))
95print('reported diff ubersummary: %f' % ubersummary(reported_diffs))
96print('reported diff MAD: %f' % mad(reported_diffs))
97
98import cProfile
99kresults = kfilter({},diffs)
100#print('packet_rtt diff kfilter: ', numpy.mean(kresults['est']), kresults['var'])
101print('packet_rtt diff kfilter: ', kresults['est'][-1], kresults['var'][-1])
102kresults = kfilter({},reported_diffs)
103#print('reported diff kfilter: ', numpy.mean(kresults['est']), kresults['var'][-1])
104print('reported diff kfilter: ', kresults['est'][-1], kresults['var'][-1])
105
106
107#all_data = longs+shorts
108#all_data.sort()
109#cut_off_low = all_data[0]
110#cut_off_high = all_data[int(len(all_data)*0.997)]
111
112
113plt.clf()
114plt.title("Packet RTT over time")
115plt.xlabel('Time of Day')
116plt.ylabel('RTT')
117s = plt.scatter([t for t,rtt in short_overtime], [rtt for t,rtt in short_overtime], s=1, color='red', alpha=0.6)
118l = plt.scatter([t for t,rtt in long_overtime], [rtt for t,rtt in long_overtime], s=1, color='blue', alpha=0.6)
119d = plt.scatter([t for t,rtt in diff_overtime], [rtt for t,rtt in diff_overtime], s=1, color='purple', alpha=0.6)
120plt.legend((s,l,d), ('short','long','difference'), scatterpoints=1)
121#plt.savefig('paper/figures/comcast-powerboost1.png')
122plt.show()
123
124short_overtime,long_overtime,diff_overtime = None,None,None
125
126
127num_bins = 300
128reported_diffs.sort()
129cut_off_low = reported_diffs[int(len(diffs)*0.003)]
130cut_off_high = reported_diffs[int(len(diffs)*0.997)]
131
132plt.clf()
133# the histogram of the data
134n, bins, patches = plt.hist(reported_diffs, num_bins, normed=1, color='black', histtype='step', alpha=0.8,
135                            range=(cut_off_low,cut_off_high))
136plt.xlabel('RTT Difference')
137plt.ylabel('Probability')
138plt.title(r'Histogram - distribution of differences')
139
140# Tweak spacing to prevent clipping of ylabel
141plt.subplots_adjust(left=0.15)
142#plt.legend()
143plt.show()
144#plt.savefig('paper/graphs/dists-vs-dist-of-diffs2.svg')
145
146
147
148
149num_bins = 300
150diffs.sort()
151cut_off_low = diffs[int(len(diffs)*0.003)]
152cut_off_high = diffs[int(len(diffs)*0.997)]
153
154plt.clf()
155# the histogram of the data
156n, bins, patches = plt.hist(diffs, num_bins, normed=1, color='purple', histtype='step', alpha=0.8,
157                            range=(cut_off_low,cut_off_high))
158plt.xlabel('RTT Difference')
159plt.ylabel('Probability')
160plt.title(r'Histogram - distribution of differences')
161
162# Tweak spacing to prevent clipping of ylabel
163plt.subplots_adjust(left=0.15)
164#plt.legend()
165plt.show()
166#plt.savefig('paper/graphs/dists-vs-dist-of-diffs2.svg')
167
168sys.exit(0)
169
170
171
172num_bins = 150
173# the histogram of the data
174n, bins, patches = plt.hist((shorts,longs), num_bins, normed=1, label=['short', 'long'], color=['red','blue'], histtype='step', alpha=0.8,
175                            range=(cut_off_low,cut_off_high))
176#n, bins, patches = plt.hist(shorts2+longs2, num_bins, normed=1, facecolor='blue', histtype='step', alpha=0.3)
177# add a 'best fit' line
178#y = mlab.normpdf(bins, mu, sigma)
179#plt.plot(bins, y, 'r--')
180plt.xlabel('packet_rtt')
181plt.ylabel('Probability')
182plt.title(r'Histogram - RTT short and long')
183
184# Tweak spacing to prevent clipping of ylabel
185plt.subplots_adjust(left=0.15)
186plt.legend()
187#plt.show()
188plt.savefig('paper/figures/comcast-powerboost2.svg')
189
190
191
192
193num_trials = 200
194
195
196subsample_sizes = (50,150,300,500,700,1000,2000,3000,5000,7000,10000,15000,20000)
197estimator = functools.partial(boxTest, 0.07, 0.08)
198performance = []
199for subsample_size in subsample_sizes:
200    estimates = bootstrap(derived, subsample_size, num_trials, estimator)
201    performance.append(100.0*len([e for e in estimates if e == 1])/num_trials)
202
203null_performance = []
204for subsample_size in subsample_sizes:
205    null_estimates = bootstrap(null_derived, subsample_size, num_trials, estimator)
206    null_performance.append(100.0*len([e for e in null_estimates if e == 0])/num_trials)
207
208plt.clf()
209plt.title("boxTest bootstrap")
210plt.xlabel('sample size')
211plt.ylabel('performance')
212plt.scatter(subsample_sizes, performance, s=2, color='red', alpha=0.6)
213plt.scatter(subsample_sizes, null_performance, s=2, color='blue', alpha=0.6)
214plt.show()
215
216
217
218subsample_sizes = (50,150,300,400,500,700,1000,2000,3000,4000,5000,7000,10000)
219estimator = diffMedian
220performance = []
221for subsample_size in subsample_sizes:
222    estimates = bootstrap(derived, subsample_size, num_trials, estimator)
223    performance.append(100.0*len([e for e in estimates if e > expected_mean*0.9 and e < expected_mean*1.1])/num_trials)
224
225plt.clf()
226plt.title("diff median bootstrap")
227plt.xlabel('sample size')
228plt.ylabel('performance')
229plt.scatter(subsample_sizes, performance, s=1, color='red', alpha=0.6)
230plt.show()
231
232
233
234
235subsample_sizes = (50,150,300,400,500,700,1000,2000,3000,4000,5000,7000,10000)
236weight_funcs = (linearWeights, prunedWeights)
237for wf in weight_funcs:
238    estimator = functools.partial(estimateMean, hypotenuse, wf, 0.40)
239    performance = []
240    for subsample_size in subsample_sizes:
241        estimates = bootstrap(derived, subsample_size, num_trials, estimator)
242        performance.append(100.0*len([e for e in estimates if e > expected_mean*0.9 and e < expected_mean*1.1])/num_trials)
243
244    plt.clf()
245    plt.title(repr(wf))
246    plt.xlabel('sample size')
247    plt.ylabel('performance')
248    plt.scatter(subsample_sizes, performance, s=1, color='red', alpha=0.6)
249    plt.show()
250
251
252
253num_bins = 300
254# the histogram of the data
255n, bins, patches = plt.hist((tsshorts,tslongs), num_bins, normed=1, label=['short', 'long'], color=['red','blue'], histtype='step', alpha=0.8)
256#n, bins, patches = plt.hist(shorts2+longs2, num_bins, normed=1, facecolor='blue', histtype='step', alpha=0.3)
257# add a 'best fit' line
258#y = mlab.normpdf(bins, mu, sigma)
259#plt.plot(bins, y, 'r--')
260plt.xlabel('packet_rtt')
261plt.ylabel('Probability')
262plt.title(r'Histogram - tsval_rtt short vs long')
263
264# Tweak spacing to prevent clipping of ylabel
265plt.subplots_adjust(left=0.15)
266plt.legend()
267plt.show()
268
269
270
271   
272####
273#trust_methods = [min,max,sum,difference,product]
274trust_methods = [sum,product,hypotenuse]
275colors = ['red','blue','green','purple','orange','black']
276weight_methods = [prunedWeights, linearWeights]
277alphas = [i/100.0 for i in range(0,100,2)]
278
279
280
281
282plt.clf()
283plt.title(r'Trust Method Comparison - Linear')
284plt.xlabel('Alpha')
285plt.ylabel('Mean error')
286paths = []
287for tm in trust_methods:
288    trust = trustValues(derived, tm)
289    series = []
290    for alpha in alphas:
291        weights = linearWeights(derived, trust, alpha)
292        series.append(weightedMean(derived, weights) - expected_mean)
293
294    paths.append(plt.scatter(alphas, series, s=1, color=colors[len(paths)],alpha=0.6))
295
296plt.legend(paths, [repr(tm) for tm in trust_methods], scatterpoints=1)
297plt.show()
298
299
300
301plt.clf()
302plt.title(r'Trust Method Comparison - Pruned')
303plt.xlabel('Alpha')
304plt.ylabel('Mean error')
305paths = []
306for tm in trust_methods:
307    trust = trustValues(derived, tm)
308    series = []
309    for alpha in alphas:
310        weights = prunedWeights(derived, trust, alpha)
311        series.append(weightedMean(derived, weights) - expected_mean)
312
313    paths.append(plt.scatter(alphas, series, s=1, color=colors[len(paths)],alpha=0.6))
314
315plt.legend(paths, [repr(tm) for tm in trust_methods], scatterpoints=1)
316plt.show()
317
318
319sys.exit(0)
320
321plt.clf()
322plt.title(r'Trust Method Comparison - Inverted')
323plt.xlabel('Alpha')
324plt.ylabel('Mean error')
325paths = []
326for tm in trust_methods:
327    trust = trustValues(derived, tm)
328    series = []
329    for alpha in alphas:
330        weights = invertedWeights(derived, trust, alpha)
331        series.append(weightedMean(derived, weights) - expected_mean)
332
333    paths.append(plt.scatter(alphas, series, s=1, color=colors[len(paths)],alpha=0.6))
334
335plt.legend(paths, [repr(tm) for tm in trust_methods], scatterpoints=1)
336plt.show()
337
338
339plt.clf()
340plt.title(r'Trust Method Comparison - Arctangent')
341plt.xlabel('Alpha')
342plt.ylabel('Mean error')
343paths = []
344for tm in trust_methods:
345    trust = trustValues(derived, tm)
346    series = []
347    for alpha in alphas:
348        weights = arctanWeights(derived, trust, alpha)
349        series.append(weightedMean(derived, weights) - expected_mean)
350
351    paths.append(plt.scatter(alphas, series, s=1, color=colors[len(paths)],alpha=0.6))
352
353plt.legend(paths, [repr(tm) for tm in trust_methods], scatterpoints=1)
354plt.show()
Note: See TracBrowser for help on using the repository browser.