def draw(options): files = [f for f in os.listdir(options['outdir']) if f.endswith('.data')] degrees = list() diameters = list() velocities = list() for f in files: fin = open(options['outdir']+'/'+f, 'r') ts = -1 for line in fin: if line.startswith('#'): continue time, degree, diameter, velocity = [t.strip() for t in line.split(',')] time = int(time) assert(ts == time-1) ts = time try: degrees[time].append(float(degree)) diameters[time].append(int(diameter)) velocities[time].append(float(velocity)) except IndexError: degrees.append([float(degree)]) diameters.append([int(diameter)]) velocities.append([float(velocity)]) polies = list() times = range(len(degrees)) times2 = times + times[::-1] degrees_conf_upper = [confidence(d)[0] for d in degrees] degrees_conf_lower = [confidence(d)[1] for d in degrees] polies.append(conf2poly(times, degrees_conf_upper, degrees_conf_lower, color='blue')) diameters_conf_upper = [confidence(d)[0] for d in diameters] diameters_conf_lower = [confidence(d)[1] for d in diameters] polies.append(conf2poly(times, diameters_conf_upper, diameters_conf_lower, color='blue')) velocities_conf_upper = [confidence(d)[0] for d in velocities] velocities_conf_lower = [confidence(d)[1] for d in velocities] polies.append(conf2poly(times, velocities_conf_upper, velocities_conf_lower, color='green')) velocities = [scipy.mean(d) for d in velocities] diameters = [scipy.mean(d) for d in diameters] degrees = [scipy.mean(d) for d in degrees] fig = MyFig(options, figsize=(10, 8), xlabel='Time [s]', ylabel='Metric', grid=False, legend=True, aspect='auto', legend_pos='upper right') patch_collection = PatchCollection(polies, match_original=True) patch_collection.set_alpha(0.3) patch_collection.set_linestyle('dashed') fig.ax.add_collection(patch_collection) fig.ax.plot(times, degrees, label='Mean degree', color='blue') fig.ax.plot(times, diameters, label='Diameter', color='red') fig.ax.plot(times, velocities, label='Mean velocity $[m/s]$', color='green') fig.ax.set_xlim(0, options['duration']) y_max = max(max(degrees), max(diameters), max(velocities)) fig.ax.set_ylim(0, y_max+10) fig.save('metrics', fileformat='pdf')
def plot(options): fig = MyFig(options, figsize=(10, 8), xlabel=r'Timeout $\tau$ [s]', ylabel=r'Reachability $\reachability$', aspect='auto', legend=True, grid=False) taus = numpy.arange(0, options['max_tau'], 0.01) Rqs = list() for tau in taus: Rqs.append(qs(tau, options)) Rqe = list() for tau in taus: Rqe.append(qe(tau, options)) fig.ax.plot(taus, Rqs, label='$q_s$ (at least one)', color='blue') fig.ax.plot(taus, Rqe, label='$q_e$ (exactly one)', color='red') fig.ax.set_ylim(0, 1.1) fig.save('model-N=%d-qd=%.2f-ES=%.2f-m=%d-T=%.2f' % (options['nodes'], options['qd'], options['ES'], options['threshold'], options['deadline']))
def draw(options, H, points, convex=None): fig = MyFig(options, figsize=(10, 8), xlabel='', ylabel='', legend=False, grid=False, aspect='auto') poly = Polygon(H, edgecolor='red', facecolor='red', closed=True, alpha=0.3) patch_collection = PatchCollection([poly], match_original=True) patch_collection.zorder = -2 fig.ax.add_collection(patch_collection) for x, y in H: fig.ax.plot(x, y, marker='o', color='red') for x, y in [p for p in points if p not in H]: fig.ax.plot(x, y, marker='o', color='black') if convex != None: poly = Polygon(convex, edgecolor='blue', facecolor='none', closed=True, alpha=0.3) patch_collection = PatchCollection([poly], match_original=True) patch_collection.zorder = -2 fig.ax.add_collection(patch_collection) fig.ax.axis((0, 1, 0, 1)) fig.save('test')
def plot_wire_surface_pcolor(self): """ Plot the fraction of executions as a function of the fraction of nodes for each source. Three plots are created: wireframe, surface, and pseudocolor. """ logging.debug('') if self.dimension > 0: return array = self._data_to_array() x = pylab.linspace(0.0, 1.0, len(array[0])+1) y = pylab.linspace(0.0, 1.0, len(array)+1) X, Y = pylab.meshgrid(x, y) #fig_wire = MyFig(self.options, xlabel='Probability p', ylabel='Fraction of Nodes', ThreeD=True) #fig_surf = MyFig(self.options, xlabel='Probability p', ylabel='Fraction of Nodes', ThreeD=True) fig_pcol = MyFig(self.options, xlabel='Probability p', ylabel='Fraction of Nodes') #fig_wire.ax.plot_wireframe(X, Y, array) #fig_surf.ax.plot_surface(X, Y, array, rstride=1, cstride=1, linewidth=1, antialiased=True) pcolor = fig_pcol.ax.pcolor(X, Y, array, cmap=cm.jet, vmin=0.0, vmax=1.0) cbar = fig_pcol.fig.colorbar(pcolor, shrink=0.8, aspect=10) cbar.ax.set_yticklabels(pylab.linspace(0.0, 1.0, 11), fontsize=0.8*self.options['fontsize']) #for ax in [fig_wire.ax, fig_surf.ax]: #ax.set_zlim3d(0.0, 1.01) #ax.set_xlim3d(0.0, 1.01) #ax.set_ylim3d(0.0, 1.01) #self.figures['wireframe'] = fig_wire.save('wireframe_' + str(self.data_filter)) #self.figures['surface'] = fig_surf.save('surface_' + str(self.data_filter)) self.figures['pcolor'] = fig_pcol.save('pcolor_' + str(self.data_filter))
def _plot_bar3d(self): logging.debug('') if self.dimension > 0: return array = self._data_to_array() # width, depth, and height of the bars (array == height values == dz) dx = list(numpy.array([1.0/len(array[0]) for x in range(0, array.shape[1])])) dy = list(numpy.array([1.0/len(array) for x in range(0, array.shape[0])])) dx *= len(array) dy *= len(array[0]) dz = array.flatten()+0.00000001 # dirty hack to cirumvent ValueError # x,y,z position of each bar x = pylab.linspace(0.0, 1.0, len(array[0]), endpoint=False) y = pylab.linspace(0.0, 1.0, len(array), endpoint=False) xpos, ypos = pylab.meshgrid(x, y) xpos = xpos.flatten() ypos = ypos.flatten() zpos = numpy.zeros(array.shape).flatten() fig = MyFig(self.options, xlabel='Probability p', ylabel='Fraction of Nodes', zlabel='Fraction of Executions', ThreeD=True) fig.ax.set_zlim3d(0.0, 1.01) fig.ax.set_xlim3d(0.0, 1.01) fig.ax.set_ylim3d(0.0, 1.01) fig.ax.set_autoscale_on(False) assert(len(dx) == len(dy) == len(array.flatten()) == len(xpos) == len(ypos) == len(zpos)) fig.ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=['#CCBBDD']) try: self.figures['bar3d'] = fig.save('bar3d' + str(self.data_filter)) except ValueError, err: logging.warning('%s', err)
def plot_statistics(options): if not options['statistics']: return fig = MyFig(options, legend=True, grid=True, xlabel=r'Building', ylabel='Distance [m]', aspect='auto', legend_pos='best') statistics = options['statistics'] colors = options['color'](pylab.linspace(0, 1.0, len(statistics['sad'])+1)) keys = ['a3', 'a6', 't9', 'all'] bar_width = 0.2 for i, key in enumerate(keys): sad = statistics['sad'][key] aad = statistics['aad'][key] mad = statistics['mad'][key] fig.ax.bar(i-bar_width*1.5, sad, width=bar_width, color=colors[0]) fig.ax.bar(i-bar_width*0.5, aad[0], width=bar_width, color=colors[1]) fig.ax.errorbar(i, aad[0], yerr=aad[1], ecolor='red') fig.ax.bar(i+bar_width*0.5, mad[0], width=bar_width, color=colors[2]) fig.ax.errorbar(i+bar_width, mad[0], yerr=mad[1], ecolor='red') fig.ax.plot(0, -1, color=colors[0], label='Statistical Average Distance (SAD)') fig.ax.plot(0, -1, color=colors[1], label='Actual Average Distance (AAD)') fig.ax.plot(0, -1, color=colors[2], label='MST Average Distance (MAD)') fig.ax.plot(0, -1, color='red', label='Standard deviation $\sigma$') fig.ax.set_ylim(0, 120) fig.ax.set_xticks(range(0, len(statistics['sad']))) fig.ax.set_xticklabels(keys) options['prefix'] = 'all' fig.save('statistics')
def draw_graph(G, time): if not options['movie']: return fig = MyFig(options) for (n1, n2) in G.edges(): fig.ax.plot([nodes[n1].cur_pos()[0], nodes[n2].cur_pos()[0]], [nodes[n1].cur_pos()[1], nodes[n2].cur_pos()[1]], color='black', linestyle='solid', alpha=0.2) for i,n in enumerate(nodes): fig.ax.plot(n.cur_pos()[0], n.cur_pos()[1], marker='o', color=colors[i]) fig.ax.set_xlim(0, 1000) fig.ax.set_ylim(0, 1000) fig.save('graph-%04d-%04d' % (num, time), fileformat='png')
def plot_box(self): """ Plots the fraction of nodes that received a particular packet from the source as a box-and-whisker with the probability p on the x-axis. """ logging.debug('') configurations_per_variant = self.configurations_per_variant gossip_variants_count = len(self.configurations['gossip']) colors = self.options['color'](pylab.linspace(0, 0.8, configurations_per_variant)) labels = [] for li_of_frac in self.label_info: s = str() for i, (param, value) in enumerate(li_of_frac): if i > 0: s += '\n' s += '%s=%s' % (_cname_to_latex(param), value) labels.append(s) labels *= len(self.configurations['p']) ps = list(pylab.flatten(self.configurations_per_p*[p] for p in self.configurations['p'])) ################################################################# # box plot ################################################################# array = numpy.zeros([len(self.fraction_of_nodes[0]), self.length()]) for i, fracs in enumerate(self.fraction_of_nodes): array[:, i] = fracs fig = MyFig(self.options, rect=[0.1, 0.2, 0.8, 0.75], figsize=(max(self.length(), 10), 10), xlabel='Probability p', ylabel='Fraction of Nodes', aspect='auto') fig.ax.set_ylim(0, 1) box_dict = fig.ax.boxplot(array, notch=1, sym='rx', vert=1) #box_dict = fig.ax.boxplot(array, notch=1, sym='rx', vert=1, patch_artist=False) for j, box in enumerate(box_dict['boxes']): j = (j % self.configurations_per_p) box.set_color(colors[j]) for _flier in box_dict['fliers']: _flier.set_color('lightgrey') fig.ax.set_xticklabels(ps, fontsize=self.options['fontsize']*0.6) # draw vertical line to visually mark different probabilities for x in range(0, self.length(), self.configurations_per_p): fig.ax.plot([x+0.5, x+0.5], [0.0, 1.0], linestyle='dotted', color='red', alpha=0.8) ################################################################# # create some dummy elements for the legend ################################################################# if configurations_per_variant > 1: proxies = [] for i in range(0, configurations_per_variant): r = Rectangle((0, 0), 1, 1, edgecolor=colors[i % configurations_per_variant], facecolor='white') proxies.append((r, labels[i])) fig.ax.legend([proxy for proxy, label in proxies], [label for proxy, label in proxies], loc='lower right') self.figures['boxplot'] = fig.save('boxplot_' + str(self.data_filter))
def plot_histogram(self): configurations_per_variant = self.configurations_per_variant colors = self.options['color'](pylab.linspace(0, 0.8, configurations_per_variant)) labels = [] for li_of_frac in self.label_info: s = str() for i, (param, value) in enumerate(li_of_frac): if i > 0: s += '\n' s += '%s=%s' % (_cname_to_latex(param), value) labels.append(s) labels *= len(self.configurations['p']) ps = list(pylab.flatten(self.configurations_per_p*[p] for p in self.configurations['p'])) ################################################################# # histogram plot ################################################################# fig2 = MyFig(self.options, rect=[0.1, 0.2, 0.8, 0.75], figsize=(max(self.length(), 10), 10), xlabel='Probability p', ylabel='Fraction of Nodes', aspect='auto') patches = [] for i, fracs in enumerate(self.fraction_of_nodes): hist, bin_edges = numpy.histogram(fracs, bins=pylab.linspace(0, 1, self.options['bins'])) hist_norm = hist/float(len(fracs))*0.4 yvals = list(bin_edges.repeat(2))[1:-1] yvals.extend(list(bin_edges.repeat(2))[1:-1][::-1]) xvals = list((i+1+hist_norm).repeat(2)) xvals.extend(list((i+1-hist_norm).repeat(2))[::-1]) i = (i % self.configurations_per_p) poly = Polygon(zip(xvals, yvals), edgecolor='black', facecolor=colors[i], closed=True) patches.append(poly) patch_collection = PatchCollection(patches, match_original=True) fig2.ax.add_collection(patch_collection) fig2.ax.set_xticks(range(1, self.length() + 1)) fig2.ax.set_xticklabels(ps, fontsize=self.options['fontsize']*0.6) for x in range(0, self.length(), self.configurations_per_p): fig2.ax.plot([x+0.5, x+0.5], [0.0, 1.0], linestyle='dotted', color='red', alpha=0.8) fig2.ax.set_ylim(0, 1) ################################################################# # create some dummy elements for the legend ################################################################# if configurations_per_variant > 1: proxies = [] #for i in range(0, len(self.configurations['gossip'])): for i in range(0, configurations_per_variant): r = Rectangle((0, 0), 1, 1, facecolor=colors[i % configurations_per_variant], edgecolor='black') proxies.append((r, labels[i])) fig2.ax.legend([proxy for proxy, label in proxies], [label for proxy, label in proxies], loc='lower right') self.figures['histogram'] = fig2.save('histogram_' + str(self.data_filter))
def draw_graph(G, time): if not options['movie']: return fig = MyFig(options) for (n1, n2) in G.edges(): fig.ax.plot([nodes[n1].cur_pos()[0], nodes[n2].cur_pos()[0]], [nodes[n1].cur_pos()[1], nodes[n2].cur_pos()[1]], color='black', linestyle='solid', alpha=0.2) for i, n in enumerate(nodes): fig.ax.plot(n.cur_pos()[0], n.cur_pos()[1], marker='o', color=colors[i]) fig.ax.set_xlim(0, 1000) fig.ax.set_ylim(0, 1000) fig.save('graph-%04d-%04d' % (num, time), fileformat='png')
def plot_wire_surface_pcolor(self): """ Plot the fraction of executions as a function of the fraction of nodes for each source. Three plots are created: wireframe, surface, and pseudocolor. """ logging.debug('') if self.dimension > 0: return array = self._data_to_array() x = pylab.linspace(0.0, 1.0, len(array[0]) + 1) y = pylab.linspace(0.0, 1.0, len(array) + 1) X, Y = pylab.meshgrid(x, y) #fig_wire = MyFig(self.options, xlabel='Probability p', ylabel='Fraction of Nodes', ThreeD=True) #fig_surf = MyFig(self.options, xlabel='Probability p', ylabel='Fraction of Nodes', ThreeD=True) fig_pcol = MyFig(self.options, xlabel='Probability p', ylabel='Fraction of Nodes') #fig_wire.ax.plot_wireframe(X, Y, array) #fig_surf.ax.plot_surface(X, Y, array, rstride=1, cstride=1, linewidth=1, antialiased=True) pcolor = fig_pcol.ax.pcolor(X, Y, array, cmap=cm.jet, vmin=0.0, vmax=1.0) cbar = fig_pcol.fig.colorbar(pcolor, shrink=0.8, aspect=10) cbar.ax.set_yticklabels(pylab.linspace(0.0, 1.0, 11), fontsize=0.8 * self.options['fontsize']) #for ax in [fig_wire.ax, fig_surf.ax]: #ax.set_zlim3d(0.0, 1.01) #ax.set_xlim3d(0.0, 1.01) #ax.set_ylim3d(0.0, 1.01) #self.figures['wireframe'] = fig_wire.save('wireframe_' + str(self.data_filter)) #self.figures['surface'] = fig_surf.save('surface_' + str(self.data_filter)) self.figures['pcolor'] = fig_pcol.save('pcolor_' + str(self.data_filter))
def _plot_bar3d(self): logging.debug('') if self.dimension > 0: return array = self._data_to_array() # width, depth, and height of the bars (array == height values == dz) dx = list( numpy.array( [1.0 / len(array[0]) for x in range(0, array.shape[1])])) dy = list( numpy.array([1.0 / len(array) for x in range(0, array.shape[0])])) dx *= len(array) dy *= len(array[0]) dz = array.flatten() + 0.00000001 # dirty hack to cirumvent ValueError # x,y,z position of each bar x = pylab.linspace(0.0, 1.0, len(array[0]), endpoint=False) y = pylab.linspace(0.0, 1.0, len(array), endpoint=False) xpos, ypos = pylab.meshgrid(x, y) xpos = xpos.flatten() ypos = ypos.flatten() zpos = numpy.zeros(array.shape).flatten() fig = MyFig(self.options, xlabel='Probability p', ylabel='Fraction of Nodes', zlabel='Fraction of Executions', ThreeD=True) fig.ax.set_zlim3d(0.0, 1.01) fig.ax.set_xlim3d(0.0, 1.01) fig.ax.set_ylim3d(0.0, 1.01) fig.ax.set_autoscale_on(False) assert (len(dx) == len(dy) == len(array.flatten()) == len(xpos) == len(ypos) == len(zpos)) fig.ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=['#CCBBDD']) try: self.figures['bar3d'] = fig.save('bar3d' + str(self.data_filter)) except ValueError, err: logging.warning('%s', err)
def draw(options, H, points, convex=None): fig = MyFig(options, figsize=(10,8), xlabel='', ylabel='', legend=False, grid=False, aspect='auto') poly = Polygon(H, edgecolor='red', facecolor='red', closed=True, alpha=0.3) patch_collection = PatchCollection([poly], match_original=True) patch_collection.zorder = -2 fig.ax.add_collection(patch_collection) for x, y in H: fig.ax.plot(x, y, marker='o', color='red') for x, y in [p for p in points if p not in H]: fig.ax.plot(x, y, marker='o', color='black') if convex != None: poly = Polygon(convex, edgecolor='blue', facecolor='none', closed=True, alpha=0.3) patch_collection = PatchCollection([poly], match_original=True) patch_collection.zorder = -2 fig.ax.add_collection(patch_collection) fig.ax.axis((0, 1, 0, 1)) fig.save('test')
def plot_distance(options): for j, src in enumerate(options['src']): logging.info('src=%s (%d/%d)', src, j+1, len(options['src'])) options['prefix'] = src fontsize = options['fontsize'] cursor = options['db_conn'].cursor() cursor.execute('''SELECT host FROM addr WHERE NOT host=? ORDER BY host''', (src,)) data = [] max_dist = 0 for host, in cursor.fetchall(): dist = cursor.execute('''SELECT dist FROM eval_distance WHERE host=? and src=? ORDER BY dist''', (host, src)).fetchall() if len(dist) == 0: continue dist = [x[0] for x in dist] data.append((host, dist, 0.0)) max_dist = max(max_dist, max(dist)) for i, d in enumerate(data): hist, bin_edges = numpy.histogram(d[1], bins=max_dist, range=(0, max_dist), normed=False) hist = [float(x)/sum(hist) for x in hist] data[i] = (d[0], d[1], hist) if len(data) == 0: continue fig1 = MyFig(options, xlabel='Distance from %s [Hops]' % src, ylabel='Host Names', figsize=(19, 30), aspect='auto') fig2 = MyFig(options, xlabel='Distance from %s [Hops]' % src, ylabel='Host Names', figsize=(19, 40), aspect='auto', grid=True) bp = fig1.ax.boxplot([x[1] for x in data], 1, 'ro', 0) plt.setp(bp['fliers'], markersize=0.5) circ_max = 40 spacing = 40 for i, d in enumerate([x[2] for x in data]): for j, e in enumerate(d): if e == 0: continue fig2.ax.plot(j, i*spacing+spacing, 'o', color='red', ms=max(float(e)*circ_max,1)) fig1.ax.set_yticklabels([x[0] for x in data], rotation=1, size=fontsize*0.8) fig2.ax.set_yticks(range(0,len(data)*spacing+1,spacing)) fig2.ax.set_yticklabels(['']+[x[0] for x in data], rotation=1 , size=fontsize*0.8) fig1.save('distance-boxplot') fig2.save('distance-circleplot')
def plot_distribution(self): logging.debug('') if self.dimension > 0: return probabilities = pylab.linspace(0, 1, 20) distributions = {} for name, func, extra_args in [('data', None, []), ('normal', stats.norm, [])]: #('chi2', stats.chi2), ('gamma', stats.gamma)]: fig_cdf = MyFig(self.options, xlabel='Fraction of Nodes', ylabel='CDF', grid=True, legend=True) fig_cdf.ax.plot([0.0, 1.0], [0.0, 1.0], color='black', linestyle='solid') fig_qq = MyFig(self.options, xlabel='Fraction of Nodes', ylabel='%s Distribution' % name, grid=True, legend=True) fig_qq.ax.plot([0.0, 1.0], [0.0, 1.0], color='black', linestyle='solid') fig_qq.ax.set_xlim(0.0, 1.0) fig_qq.ax.set_ylim(0.0, 1.0) distributions[name] = (func, fig_cdf, fig_qq, extra_args) #fig_cdf_data = MyFig(self.options, xlabel='Fraction of Nodes', ylabel='CDF', grid=True, legend=True) #fig_cdf_data.ax.plot([0.0, 1.2], [0.0, 1.2], color='black', linestyle='solid') ######################################### #fig_qq_normal = MyFig(self.options, xlabel='Fraction of Nodes', ylabel='Normal Distribution', grid=True, legend=True) #fig_qq_normal.ax.plot([0.0, 1.0], [0.0, 1.0], color='black', linestyle='solid') #fig_qq_normal.ax.set_xlim(0.0, 1.0) #fig_qq_normal.ax.set_ylim(0.0, 1.0) #fig_cdf_normal = MyFig(self.options, xlabel='x', ylabel='CDF', grid=True, legend=True) #fig_cdf_normal.ax.plot([0.0, 1.2], [0.0, 1.2], color='black', linestyle='solid') ######################################### #fig_qq_gamma = MyFig(self.options, xlabel='Fraction of Nodes', ylabel='Gamma Distribution', grid=True, legend=True) #fig_qq_gamma.ax.plot([0.0, 1.0], [0.0, 1.0], color='black', linestyle='solid') #fig_qq_gamma.ax.set_xlim(0.0, 1.0) #fig_qq_gamma.ax.set_ylim(0.0, 1.0) #fig_cdf_gamma = MyFig(self.options, xlabel='x', ylabel='CDF', grid=True, legend=True, aspect='auto') #fig_cdf_gamma.ax.set_xlim(0.0, 1.0) ######################################### #fig_qq_2normal = MyFig(self.options, xlabel='Fraction of Nodes', ylabel='Normal Distribution', grid=True, legend=True) #fig_qq_2normal.ax.plot([0.0, 1.0], [0.0, 1.0], color='black', linestyle='solid') #fig_qq_2normal.ax.set_xlim(0.0, 1.0) #fig_qq_2normal.ax.set_ylim(0.0, 1.0) #fig_cdf_2normal = MyFig(self.options, xlabel='x', ylabel='CDF', grid=True, legend=True) #fig_cdf_2normal.ax.plot([0.0, 1.2], [0.0, 1.2], color='black', linestyle='solid') ######################################### #fig_qq_2chi2 = MyFig(self.options, xlabel='Fraction of Nodes', ylabel='2x Chi Square Distribution', grid=True, legend=True) #fig_qq_2chi2.ax.plot([0.0, 1.0], [0.0, 1.0], color='black', linestyle='solid') #fig_qq_2chi2.ax.set_xlim(0.0, 1.0) #fig_qq_2chi2.ax.set_ylim(0.0, 1.0) #fig_cdf_2chi2 = MyFig(self.options, xlabel='x', ylabel='CDF', grid=True, legend=True) #fig_cdf_2chi2.ax.plot([0.0, 1.2], [0.0, 1.2], color='black', linestyle='solid') colors = self.options['color'](pylab.linspace(0, 0.8, self.length())) markers = self.options['markers'] for j, data in enumerate(self.fraction_of_nodes): label = 'p=%.2f' % self.configurations['p'][j] avr = scipy.average(data) sigma = scipy.std(data) quantiles_data = stats.mstats.mquantiles(data, prob=probabilities) for name in distributions: func, fig_cdf, fig_qq, extra_args = distributions[name] if func: quantiles_stat = func.ppf(probabilities, *extra_args, loc=avr, scale=sigma) fig_qq.ax.plot(quantiles_data, quantiles_stat, 'o', color=colors[j], linestyle='-', label=label, marker=markers[j]) else: quantiles_stat = quantiles_data fig_cdf.ax.plot(quantiles_stat, probabilities, 'o', color=colors[j], linestyle='-', label=label, marker=markers[j]) #fig_cdf_data.ax.plot(quantiles_data, probabilities, 'o', color=colors[j], linestyle='-', label=label) ########################################################################### # Normal Distribution ########################################################################### #quantiles_normal = stats.norm.ppf(probabilities, loc=avr, scale=sigma) #fig_cdf_normal.ax.plot(quantiles_normal, probabilities, 'o', color=colors[j], linestyle='-', label=label) #fig_qq_normal.ax.plot(quantiles_data, quantiles_normal, 'o', color=colors[j], linestyle='-', label=label) ########################################################################### # Gamma Distribution ########################################################################### #quantiles_gamma = stats.gamma.ppf(probabilities, 0.4, loc=avr, scale=sigma) #_quantiles_gamma = [] #for x in quantiles_gamma: #if x != numpy.infty: #_quantiles_gamma.append(min(1.0,x)) #else: #_quantiles_gamma.append(x) #quantiles_gamma = numpy.array(_quantiles_gamma) #fig_cdf_gamma.ax.plot(quantiles_gamma, probabilities, 'o', color=colors[j], linestyle='-', label=label) #fig_qq_gamma.ax.plot(quantiles_data, quantiles_gamma, 'o', color=colors[j], linestyle='-', label=label) ########################################################################### # 2x Chi Square Distribution ########################################################################### #data_2chi2 = [] #for i in range(0, 5000): #sigma_2chi2 = p*0.5 #dv = 0.5 #if random.normalvariate(0.6, 0.1) < p: #exp_2chi2 = 1.0 #d = stats.chi2.rvs(dv, loc=exp_2chi2, scale=sigma_2chi2, size=1) #d = exp_2chi2-abs(exp_2chi2-d) #else: #exp_2chi2 = 0.05 #d = stats.chi2.rvs(dv, loc=exp_2chi2, scale=sigma_2chi2, size=1) #data_2chi2.append(max(min(d[0], 1.0), 0.0)) #quantiles_data_2chi2 = stats.mstats.mquantiles(data_2chi2, prob=probabilities) #fig_cdf_2chi2.ax.plot(quantiles_data_2chi2, probabilities, 'o', linestyle='-', label='p=%.2f' % p, color=colors[j]) #fig_qq_2chi2.ax.plot(quantiles_data, quantiles_data_2chi2, 'o', color=colors[j], linestyle='-', label=label) ########################################################################### # 2x Normal Distribution ########################################################################### #data_2normal = [] #for i in range(0, 2000): #if random.uniform(0.0, 1.0) < p: #exp_2normal = 1.0 #sigma_2normal = 0.05 #d = stats.norm.rvs(loc=exp_2normal, scale=sigma_2normal, size=1) #d = exp_2normal-abs(exp_2normal-d) #else: #exp_2normal = 0.05 #sigma_2normal = 0.05 #d = stats.norm.rvs(loc=exp_2normal, scale=sigma_2normal, size=1) #data_2normal.append(max(min(d[0], 1.0), 0.0)) #quantiles_data_2normal = stats.mstats.mquantiles(data_2normal, prob=probabilities) #fig_cdf_2normal.ax.plot(quantiles_data_2normal, probabilities, 'o', linestyle='-', label='p=%.2f' % p, color=colors[j]) #fig_qq_2normal.ax.plot(quantiles_data, quantiles_data_2normal, 'o', color=colors[j], linestyle='-', label=label) for name in distributions: func, fig_cdf, fig_qq, extra_args = distributions[name] if func: fig_qq.save('distribution-qq_%s_%s' % (name, str(self.data_filter))) fig_cdf.save('distribution-cdf_%s_%s' % (name, str(self.data_filter)))
def plot_redundancy(self): """ Plot the fraction of (unique) packets received from the source (0.0 - 1.0) over the total number of packets received as fraction (0.0 - infty). A cubic curve is fit to the data points. """ if self.dimension > 0: return cursor = self.options['db_conn'].cursor() max_total, = cursor.execute(''' SELECT MAX(total) FROM eval_fracsOfHosts ''').fetchone() fig_all = MyFig(self.options, xlabel='Fraction of rx Packets (incl. duplicates)', ylabel='Fraction of rx Packets sent by %s' % self.src, legend=True, aspect='auto') fig_all.ax.plot([1, 1], [0, 1], linestyle='dashed', color='grey') fig_all_median = MyFig( self.options, xlabel='Fraction of rx Packets (incl. duplicates)', ylabel='Fraction of rx Packets sent by %s' % self.src, legend=True, aspect='auto') fig_all_median.ax.plot([1, 1], [0, 1], linestyle='dashed', color='grey') colors = self.options['color'](pylab.linspace(0, 0.8, len(self.tag_keys))) markers = self.options['markers'] max_x = 0 ellipses = [] for j, tag_key in enumerate(self.tag_keys): fig = MyFig(self.options, xlabel='Fraction of rx Packets (incl. duplicates)', ylabel='Fraction of rx Packets sent by %s' % self.src, aspect='auto') results = cursor.execute( ''' SELECT total, frac FROM eval_fracsOfHosts WHERE src=? AND tag_key=? ''', (self.src, tag_key)).fetchall() assert (len(results)) fig.ax.plot([1, 1], [0, 1], linestyle='-', color='grey') xvals = [x[0] for x in results] yvals = [y[1] for y in results] label = 'p=%.2f' % self.configurations['p'][j] fig.ax.scatter(xvals, yvals, s=15, color=colors[j]) fig.ax.set_xlim((0, max_total)) max_x = max(max_x, max_total) fig.ax.set_ylim((0, 1)) z = numpy.polyfit(xvals, yvals, 3) poly = numpy.poly1d(z) median_y = scipy.median(yvals) mean_y = scipy.mean(yvals) ci_y = confidence(yvals) median_x = scipy.median(xvals) mean_x = scipy.mean(xvals) ci_x = confidence(xvals) ellipse = Ellipse((mean_x, mean_y), ci_x[1] - ci_x[0], ci_y[1] - ci_y[0], edgecolor=colors[j], facecolor=colors[j], alpha=0.5) ellipses.append(ellipse) selected_xvals = numpy.arange(min(xvals), max(xvals), 0.4) fig.ax.plot(selected_xvals, poly(selected_xvals), "-", color=colors[j]) fig.ax.plot([0.0, 10], [median_y, median_y], linestyle="dashed", color=colors[j]) fig_all.ax.plot(selected_xvals, poly(selected_xvals), "-", color=colors[j], label=label, marker=markers[j]) fig_all.ax.plot([0.0, 10], [median_y, median_y], linestyle="dashed", color=colors[j], alpha=0.6, marker=markers[j]) fig_all_median.ax.plot(ci_x, [mean_y, mean_y], color=colors[j], label=label, marker=markers[j]) fig_all_median.ax.plot([mean_x, mean_x], ci_y, color=colors[j], marker=markers[j]) fig.save('redundancy_%s_%s' % (label, str(self.data_filter))) fig_all.ax.axis((0, max(max_x, 1), 0, 1)) fig_all_median.ax.axis((0, max(max_x, 1), 0, 1)) patch_collection = PatchCollection(ellipses, match_original=True) fig_all_median.ax.add_collection(patch_collection) fig_all.save('redundancy_%s' % str(self.data_filter)) fig_all_median.save('redundancy_median_%s' % str(self.data_filter))
def plot_histogram(self): configurations_per_variant = self.configurations_per_variant colors = self.options['color'](pylab.linspace( 0, 0.8, configurations_per_variant)) labels = [] for li_of_frac in self.label_info: s = str() for i, (param, value) in enumerate(li_of_frac): if i > 0: s += '\n' s += '%s=%s' % (_cname_to_latex(param), value) labels.append(s) labels *= len(self.configurations['p']) ps = list( pylab.flatten(self.configurations_per_p * [p] for p in self.configurations['p'])) ################################################################# # histogram plot ################################################################# fig2 = MyFig(self.options, rect=[0.1, 0.2, 0.8, 0.75], figsize=(max(self.length(), 10), 10), xlabel='Probability p', ylabel='Fraction of Nodes', aspect='auto') patches = [] for i, fracs in enumerate(self.fraction_of_nodes): hist, bin_edges = numpy.histogram(fracs, bins=pylab.linspace( 0, 1, self.options['bins'])) hist_norm = hist / float(len(fracs)) * 0.4 yvals = list(bin_edges.repeat(2))[1:-1] yvals.extend(list(bin_edges.repeat(2))[1:-1][::-1]) xvals = list((i + 1 + hist_norm).repeat(2)) xvals.extend(list((i + 1 - hist_norm).repeat(2))[::-1]) i = (i % self.configurations_per_p) poly = Polygon(zip(xvals, yvals), edgecolor='black', facecolor=colors[i], closed=True) patches.append(poly) patch_collection = PatchCollection(patches, match_original=True) fig2.ax.add_collection(patch_collection) fig2.ax.set_xticks(range(1, self.length() + 1)) fig2.ax.set_xticklabels(ps, fontsize=self.options['fontsize'] * 0.6) for x in range(0, self.length(), self.configurations_per_p): fig2.ax.plot([x + 0.5, x + 0.5], [0.0, 1.0], linestyle='dotted', color='red', alpha=0.8) fig2.ax.set_ylim(0, 1) ################################################################# # create some dummy elements for the legend ################################################################# if configurations_per_variant > 1: proxies = [] #for i in range(0, len(self.configurations['gossip'])): for i in range(0, configurations_per_variant): r = Rectangle((0, 0), 1, 1, facecolor=colors[i % configurations_per_variant], edgecolor='black') proxies.append((r, labels[i])) fig2.ax.legend([proxy for proxy, label in proxies], [label for proxy, label in proxies], loc='lower right') self.figures['histogram'] = fig2.save('histogram_' + str(self.data_filter))
def test_scaler(): def parse(in_file): rlist = [0] for line in in_file: if line.startswith('#') or line.isspace(): continue tokens = line.split(';') try: rlist[int(tokens[0].strip())].append(float(tokens[1].strip())) except IndexError: rlist.append([float(tokens[1].strip())]) return rlist[1:] parser = argparse.ArgumentParser(description='Run graph scaler example') parser.add_argument('--outdir', default='./', help='output directory for the results') parser.add_argument('--nodes', '-n', type=int, default=100, help='number of nodes') parser.add_argument('--processes', '-p', type=int, default=1, help='number of processes') parser.add_argument('--fontsize', '-f', type=int, default=24, help='fontsize') parser.add_argument('--degree', '-d', type=int, default=12, help='target avr. node degree') parser.add_argument('--graphs', '-g', type=int, default=20, help='number of graphs') parser.add_argument('--max_scale', '-s', type=int, default=20, help='maximum scale factor') parser.add_argument('--mode', '-m', default='swap-add', help='mode to connected layers') args = parser.parse_args() options = dict() options['nodes'] = args.nodes options['degree'] = args.degree options['graphs'] = args.graphs options['scale_mode'] = args.mode options['processes'] = args.processes options['fontsize'] = args.fontsize options['outdir'] = args.outdir options['max_scale'] = args.max_scale try: os.remove(options['outdir']+'/scaled_graph_diameter.data') except OSError: pass out_file_diameter = open(options['outdir']+'/scaled_graph_diameter.data', 'w') out_file_diameter.write('# scale_factor; diameter\n') out_file_diameter.close() try: os.remove(options['outdir']+'/scaled_graph_degree.data') except OSError: pass out_file_degree = open(options['outdir']+'/scaled_graph_degree.data', 'w') out_file_degree.write('# scale_factor; avr. degree\n') out_file_degree.close() factors = range(2, options['max_scale']+1) graphs = create_random(options) pool = Pool(processes=options['processes']) manager = Manager() lock = manager.Lock() data = [(G, options, factors, lock) for i, G in enumerate(graphs)] pool.map(smp_scale_eval, data) in_file_diameter = open(options['outdir']+'/scaled_graph_diameter.data', 'r') diameters = parse(in_file_diameter, diameters) in_file_degree = open(options['outdir']+'/scaled_graph_degree.data', 'r') degrees = parse(in_file_degree, degrees) options['prefix'] = 'scale' fig = MyFig(options, figsize=(10,8), xlabel = 'Scale factor', ylabel='Metric', legend=True, grid=True, aspect='auto') factors.insert(0, 1) diameters_means = [scipy.mean(d) for d in diameters] diameters_yerr = [confidence(d)[2] for d in diameters] fig.ax.plot(factors, diameters_means, color='red', label='Diameter') fig.ax.errorbar(factors, diameters_means, yerr=diameters_yerr, color='red') degrees_means = [scipy.mean(d) for d in degrees] degrees_yerr = [confidence(d)[2] for d in degrees] fig.ax.plot(factors, degrees_means, color='blue', label='Mean degree') fig.ax.errorbar(factors, degrees_means, yerr=degrees_yerr, color='blue') fig.ax.set_ylim(0, max(diameters_means)+1) fig.save('test')
def test_scaler(): def parse(in_file): rlist = [0] for line in in_file: if line.startswith('#') or line.isspace(): continue tokens = line.split(';') try: rlist[int(tokens[0].strip())].append(float(tokens[1].strip())) except IndexError: rlist.append([float(tokens[1].strip())]) return rlist[1:] parser = argparse.ArgumentParser(description='Run graph scaler example') parser.add_argument('--outdir', default='./', help='output directory for the results') parser.add_argument('--nodes', '-n', type=int, default=100, help='number of nodes') parser.add_argument('--processes', '-p', type=int, default=1, help='number of processes') parser.add_argument('--fontsize', '-f', type=int, default=24, help='fontsize') parser.add_argument('--degree', '-d', type=int, default=12, help='target avr. node degree') parser.add_argument('--graphs', '-g', type=int, default=20, help='number of graphs') parser.add_argument('--max_scale', '-s', type=int, default=20, help='maximum scale factor') parser.add_argument('--mode', '-m', default='swap-add', help='mode to connected layers') args = parser.parse_args() options = dict() options['nodes'] = args.nodes options['degree'] = args.degree options['graphs'] = args.graphs options['scale_mode'] = args.mode options['processes'] = args.processes options['fontsize'] = args.fontsize options['outdir'] = args.outdir options['max_scale'] = args.max_scale try: os.remove(options['outdir'] + '/scaled_graph_diameter.data') except OSError: pass out_file_diameter = open(options['outdir'] + '/scaled_graph_diameter.data', 'w') out_file_diameter.write('# scale_factor; diameter\n') out_file_diameter.close() try: os.remove(options['outdir'] + '/scaled_graph_degree.data') except OSError: pass out_file_degree = open(options['outdir'] + '/scaled_graph_degree.data', 'w') out_file_degree.write('# scale_factor; avr. degree\n') out_file_degree.close() factors = range(2, options['max_scale'] + 1) graphs = create_random(options) pool = Pool(processes=options['processes']) manager = Manager() lock = manager.Lock() data = [(G, options, factors, lock) for i, G in enumerate(graphs)] pool.map(smp_scale_eval, data) in_file_diameter = open(options['outdir'] + '/scaled_graph_diameter.data', 'r') diameters = parse(in_file_diameter, diameters) in_file_degree = open(options['outdir'] + '/scaled_graph_degree.data', 'r') degrees = parse(in_file_degree, degrees) options['prefix'] = 'scale' fig = MyFig(options, figsize=(10, 8), xlabel='Scale factor', ylabel='Metric', legend=True, grid=True, aspect='auto') factors.insert(0, 1) diameters_means = [scipy.mean(d) for d in diameters] diameters_yerr = [confidence(d)[2] for d in diameters] fig.ax.plot(factors, diameters_means, color='red', label='Diameter') fig.ax.errorbar(factors, diameters_means, yerr=diameters_yerr, color='red') degrees_means = [scipy.mean(d) for d in degrees] degrees_yerr = [confidence(d)[2] for d in degrees] fig.ax.plot(factors, degrees_means, color='blue', label='Mean degree') fig.ax.errorbar(factors, degrees_means, yerr=degrees_yerr, color='blue') fig.ax.set_ylim(0, max(diameters_means) + 1) fig.save('test')
def plot_box(self): """ Plots the fraction of nodes that received a particular packet from the source as a box-and-whisker with the probability p on the x-axis. """ logging.debug('') configurations_per_variant = self.configurations_per_variant gossip_variants_count = len(self.configurations['gossip']) colors = self.options['color'](pylab.linspace( 0, 0.8, configurations_per_variant)) labels = [] for li_of_frac in self.label_info: s = str() for i, (param, value) in enumerate(li_of_frac): if i > 0: s += '\n' s += '%s=%s' % (_cname_to_latex(param), value) labels.append(s) labels *= len(self.configurations['p']) ps = list( pylab.flatten(self.configurations_per_p * [p] for p in self.configurations['p'])) ################################################################# # box plot ################################################################# array = numpy.zeros([len(self.fraction_of_nodes[0]), self.length()]) for i, fracs in enumerate(self.fraction_of_nodes): array[:, i] = fracs fig = MyFig(self.options, rect=[0.1, 0.2, 0.8, 0.75], figsize=(max(self.length(), 10), 10), xlabel='Probability p', ylabel='Fraction of Nodes', aspect='auto') fig.ax.set_ylim(0, 1) box_dict = fig.ax.boxplot(array, notch=1, sym='rx', vert=1) #box_dict = fig.ax.boxplot(array, notch=1, sym='rx', vert=1, patch_artist=False) for j, box in enumerate(box_dict['boxes']): j = (j % self.configurations_per_p) box.set_color(colors[j]) for _flier in box_dict['fliers']: _flier.set_color('lightgrey') fig.ax.set_xticklabels(ps, fontsize=self.options['fontsize'] * 0.6) # draw vertical line to visually mark different probabilities for x in range(0, self.length(), self.configurations_per_p): fig.ax.plot([x + 0.5, x + 0.5], [0.0, 1.0], linestyle='dotted', color='red', alpha=0.8) ################################################################# # create some dummy elements for the legend ################################################################# if configurations_per_variant > 1: proxies = [] for i in range(0, configurations_per_variant): r = Rectangle((0, 0), 1, 1, edgecolor=colors[i % configurations_per_variant], facecolor='white') proxies.append((r, labels[i])) fig.ax.legend([proxy for proxy, label in proxies], [label for proxy, label in proxies], loc='lower right') self.figures['boxplot'] = fig.save('boxplot_' + str(self.data_filter))
def plot_distance(options): for j, src in enumerate(options['src']): logging.info('src=%s (%d/%d)', src, j + 1, len(options['src'])) options['prefix'] = src fontsize = options['fontsize'] cursor = options['db_conn'].cursor() cursor.execute( '''SELECT host FROM addr WHERE NOT host=? ORDER BY host''', (src, )) data = [] max_dist = 0 for host, in cursor.fetchall(): dist = cursor.execute( '''SELECT dist FROM eval_distance WHERE host=? and src=? ORDER BY dist''', (host, src)).fetchall() if len(dist) == 0: continue dist = [x[0] for x in dist] data.append((host, dist, 0.0)) max_dist = max(max_dist, max(dist)) for i, d in enumerate(data): hist, bin_edges = numpy.histogram(d[1], bins=max_dist, range=(0, max_dist), normed=False) hist = [float(x) / sum(hist) for x in hist] data[i] = (d[0], d[1], hist) if len(data) == 0: continue fig1 = MyFig(options, xlabel='Distance from %s [Hops]' % src, ylabel='Host Names', figsize=(19, 30), aspect='auto') fig2 = MyFig(options, xlabel='Distance from %s [Hops]' % src, ylabel='Host Names', figsize=(19, 40), aspect='auto', grid=True) bp = fig1.ax.boxplot([x[1] for x in data], 1, 'ro', 0) plt.setp(bp['fliers'], markersize=0.5) circ_max = 40 spacing = 40 for i, d in enumerate([x[2] for x in data]): for j, e in enumerate(d): if e == 0: continue fig2.ax.plot(j, i * spacing + spacing, 'o', color='red', ms=max(float(e) * circ_max, 1)) fig1.ax.set_yticklabels([x[0] for x in data], rotation=1, size=fontsize * 0.8) fig2.ax.set_yticks(range(0, len(data) * spacing + 1, spacing)) fig2.ax.set_yticklabels([''] + [x[0] for x in data], rotation=1, size=fontsize * 0.8) fig1.save('distance-boxplot') fig2.save('distance-circleplot')
def plot_pathlen(options): ''' Plot the distances from each node to each other node ''' cursor = options['db_conn'].cursor() options['prefix'] = 'all' steps = 1.0 / options['bins'] quantiles = numpy.arange(0, 1.0 + steps * 0.9, steps) fig_avr_len = MyFig(options, grid=True, legend=True, xlabel='Hops', ylabel='CDF of Average Distances', aspect='auto') fig_max_len = MyFig(options, grid=True, legend=True, xlabel='Hops', ylabel='CDF of Maximum Distances', aspect='auto') fig_max_avr_len_meds = MyFig(options, grid=True, xlabel='Maximum Distance', ylabel='Median of Average Distance') cursor.execute('''SELECT host FROM addr''') hosts = cursor.fetchall() vertices = digraph() for host, in hosts: vertices.add_node(host) tag_ids = cursor.execute( 'SELECT DISTINCT(id), helloSize FROM tag ORDER BY helloSize').fetchall( ) colors = cm.hsv(pylab.linspace(0, 0.8, len(tag_ids))) for i, (tag_id, hello_size) in enumerate(tag_ids): logging.info('tag_id=\"%s\" (%d/%d)', tag_id, i + 1, len(tag_ids)) fig_max_avr_len = MyFig(options, grid=True, xlabel='Maximum Distance', ylabel='Average Distance') links = cursor.execute( 'SELECT src,host,pdr FROM eval_helloPDR WHERE tag_id=?' '', (tag_id, )).fetchall() lens = [] maxl = [] max_avr_len = [] graph = deepcopy(vertices) for src, host, pdr in links: graph.add_edge((src, host), wt=1) for host, in hosts: stp, distances = minmax.shortest_path(graph, host) if len(distances) > 1: vals = [t for t in distances.values() if t > 0] lens.append(scipy.average(vals)) maxl.append(max(vals)) max_avr_len.append((maxl[-1], lens[-1])) avr_values = stats.mstats.mquantiles(lens, prob=quantiles) max_values = stats.mstats.mquantiles(maxl, prob=quantiles) fig_avr_len.ax.plot(avr_values, quantiles, '-', color=colors[i], label=str(hello_size) + ' bytes') fig_max_len.ax.plot(max_values, quantiles, '-', color=colors[i], label=str(hello_size) + ' bytes') points = [] for max_avr_dist in sorted(set([x[0] for x in max_avr_len])): median = scipy.median( [y[1] for y in max_avr_len if y[0] == max_avr_dist]) fig_max_avr_len.ax.plot(max_avr_dist, median, 'o', color='black', label='') points.append((max_avr_dist, median)) fig_max_avr_len_meds.ax.plot([x[0] for x in points], [y[1] for y in points], color=colors[i], label=str(hello_size) + ' bytes') fig_max_avr_len.ax.scatter([x[0] for x in max_avr_len], [y[1] for y in max_avr_len], color=colors[i], label='') fig_max_avr_len.ax.axis((0, max([x[0] for x in max_avr_len]) + 1, 0, max([y[1] for y in max_avr_len]) + 1)) fig_max_avr_len.save('dist-average_over_max_%d' % (hello_size)) for _ax in [fig_avr_len.ax, fig_max_len.ax]: _ax.set_yticks(numpy.arange(0.1, 1.1, 0.1)) fig_avr_len.save('dist-cdf_avr') fig_max_len.save('dist-cdf_max') fig_max_avr_len_meds.save('dist-median_of_averages_over_max')
def plot_redundancy(self): """ Plot the fraction of (unique) packets received from the source (0.0 - 1.0) over the total number of packets received as fraction (0.0 - infty). A cubic curve is fit to the data points. """ if self.dimension > 0: return cursor = self.options['db_conn'].cursor() max_total, = cursor.execute(''' SELECT MAX(total) FROM eval_fracsOfHosts ''').fetchone() fig_all = MyFig(self.options, xlabel='Fraction of rx Packets (incl. duplicates)', ylabel='Fraction of rx Packets sent by %s' % self.src, legend=True, aspect='auto') fig_all.ax.plot([1, 1], [0, 1], linestyle='dashed', color='grey') fig_all_median = MyFig(self.options, xlabel='Fraction of rx Packets (incl. duplicates)', ylabel='Fraction of rx Packets sent by %s' % self.src, legend=True, aspect='auto') fig_all_median.ax.plot([1, 1], [0, 1], linestyle='dashed', color='grey') colors = self.options['color'](pylab.linspace(0, 0.8, len(self.tag_keys))) markers = self.options['markers'] max_x = 0 ellipses = [] for j, tag_key in enumerate(self.tag_keys): fig = MyFig(self.options, xlabel='Fraction of rx Packets (incl. duplicates)', ylabel='Fraction of rx Packets sent by %s' % self.src, aspect='auto') results = cursor.execute(''' SELECT total, frac FROM eval_fracsOfHosts WHERE src=? AND tag_key=? ''', (self.src, tag_key)).fetchall() assert(len(results)) fig.ax.plot([1, 1], [0, 1], linestyle='-', color='grey') xvals = [x[0] for x in results] yvals = [y[1] for y in results] label = 'p=%.2f' % self.configurations['p'][j] fig.ax.scatter(xvals, yvals, s=15, color=colors[j]) fig.ax.set_xlim((0, max_total)) max_x = max(max_x, max_total) fig.ax.set_ylim((0, 1)) z = numpy.polyfit(xvals, yvals, 3) poly = numpy.poly1d(z) median_y = scipy.median(yvals) mean_y = scipy.mean(yvals) ci_y = confidence(yvals) median_x = scipy.median(xvals) mean_x = scipy.mean(xvals) ci_x = confidence(xvals) ellipse = Ellipse((mean_x, mean_y), ci_x[1]-ci_x[0], ci_y[1]-ci_y[0], edgecolor=colors[j], facecolor=colors[j], alpha=0.5) ellipses.append(ellipse) selected_xvals = numpy.arange(min(xvals), max(xvals), 0.4) fig.ax.plot(selected_xvals, poly(selected_xvals),"-", color=colors[j]) fig.ax.plot([0.0, 10], [median_y, median_y], linestyle="dashed", color=colors[j]) fig_all.ax.plot(selected_xvals, poly(selected_xvals),"-", color=colors[j], label=label, marker=markers[j]) fig_all.ax.plot([0.0, 10], [median_y, median_y], linestyle="dashed", color=colors[j], alpha=0.6, marker=markers[j]) fig_all_median.ax.plot(ci_x, [mean_y, mean_y], color=colors[j], label=label, marker=markers[j]) fig_all_median.ax.plot([mean_x, mean_x], ci_y, color=colors[j], marker=markers[j]) fig.save('redundancy_%s_%s' % (label, str(self.data_filter))) fig_all.ax.axis((0, max(max_x, 1), 0, 1)) fig_all_median.ax.axis((0, max(max_x, 1), 0, 1)) patch_collection = PatchCollection(ellipses, match_original=True) fig_all_median.ax.add_collection(patch_collection) fig_all.save('redundancy_%s' % str(self.data_filter)) fig_all_median.save('redundancy_median_%s' % str(self.data_filter))
def run((options, num)): def connect_graph(G, range): for n in G.nodes(): for m in G.nodes(): if m <= n: continue distance = scipy.sqrt( sum((nodes[n].cur_pos() - nodes[m].cur_pos())**2)) if distance <= range: G.add_edge(n, m) def eval_graph(G, l): components = nx.connected_components(G) diameter = nx.algorithms.diameter(G.subgraph(components[0])) mean_velocity = scipy.mean([n.cur_velocity() for n in nodes]) mean_degree = scipy.mean(G.degree().values()) l.append((time, mean_degree, diameter, mean_velocity)) def draw_graph(G, time): if not options['movie']: return fig = MyFig(options) for (n1, n2) in G.edges(): fig.ax.plot([nodes[n1].cur_pos()[0], nodes[n2].cur_pos()[0]], [nodes[n1].cur_pos()[1], nodes[n2].cur_pos()[1]], color='black', linestyle='solid', alpha=0.2) for i, n in enumerate(nodes): fig.ax.plot(n.cur_pos()[0], n.cur_pos()[1], marker='o', color=colors[i]) fig.ax.set_xlim(0, 1000) fig.ax.set_ylim(0, 1000) fig.save('graph-%04d-%04d' % (num, time), fileformat='png') scipy.random.seed() nodes = [Node(options['speed'][1], i) for i in range(0, options['nodes'])] colors = options['color'](pylab.linspace(0, 1, options['nodes'])) time = 0 values = list() metrics_out = open('%s/%04d-metrics.data' % (options['outdir'], num), 'w') metrics_out.write( '# Metrics of a network where nodes move as defined by the random waypoint model\n' ) metrics_out.write('#\n') metrics_out.write('# %-11s: %8d\n' % ('nodes', options['nodes'])) metrics_out.write('# %-11s: %8d\n' % ('min speed', options['speed'][0])) metrics_out.write('# %-11s: %8d\n' % ('max speed', options['speed'][1])) metrics_out.write('# %-11s: %8d\n' % ('radio range', options['range'])) metrics_out.write('# %-11s: %8d\n' % ('duration', options['duration'])) metrics_out.write('# %-11s: %8d\n' % ('steps', options['steps'])) metrics_out.write('#\n') metrics_out.write('# column 0: time since start of movement [s]\n') metrics_out.write('# column 1: computed mean node degree\n') metrics_out.write( '# column 2: computed network diameter [hops] (of largest component if partitioned)\n' ) metrics_out.write('# column 3: computed mean velocity [m/s]\n') metrics_out.write('#\n') metrics_out.write('# time, degree, diameter, velocity\n') G = nx.Graph() G.add_nodes_from([n.name for n in nodes]) connect_graph(G, options['range']) eval_graph(G, values) draw_graph(G, 0) graphs = list() while (time + options['steps']) <= options['duration']: time += options['steps'] for n in nodes: n.walk(options['steps']) G = nx.Graph() G.add_nodes_from([n.name for n in nodes]) if options['export']: for n in nodes: npos = n.cur_pos() G.node[n.name]['pos'] = npos connect_graph(G, options['range']) eval_graph(G, values) draw_graph(G, time) if options['export']: graphs.append(G) if options['export']: print 'exporting' options['graph'] = graphs export.graphToNEDMixim(options, '%04d-' % num) times = [t for (t, m, d, v) in values] degrees = [m for (t, m, d, v) in values] diameters = [d for (t, m, d, v) in values] velocities = [v for (t, m, d, v) in values] y_max = max([max(degrees), max(diameters), max(velocities)]) for i, t in enumerate(times): metrics_out.write('%4d, %f, %2d, %f\n' % (t, degrees[i], diameters[i], velocities[i])) if options['movie']: fig = MyFig(options, figsize=(10, 8), xlabel='Time [s]', ylabel='Metric', grid=False, legend=True, aspect='auto', legend_pos='upper right') fig.ax.plot(times[0:i + 1], degrees[0:i + 1], label='Mean degree') fig.ax.plot(times[0:i + 1], diameters[0:i + 1], label='Diameter') fig.ax.plot(times[0:i + 1], velocities[0:i + 1], label='Mean velocity $[m/s]$') fig.ax.set_xlim(0, options['duration']) fig.ax.set_ylim(0, y_max + 10) fig.save('%s/metrics-%04d' % (options['outdir'], t), fileformat='png') os.system( 'montage -geometry 1280x720 -tile 2x1 -background white -depth 8 -mode Concatenate %s/%s-graph-%04d.png %s/%s-metrics-%04d.png %s/%s-%04d.png' % (options['outdir'], options['prefix'], i, options['outdir'], options['prefix'], i, options['outdir'], options['prefix'], i)) if options['movie']: cmd = 'ffmpeg -y -qscale 5 -r 10 -b 9600 -i %s/%s-%04d-%%04d.png %s/%04d-movie.mp4' % ( options['outdir'], options['prefix'], num, options['outdir'], num) print cmd os.system(cmd) os.system('rm %s/random_waypoint-*.png' % options['outdir']) print 'finished: %d' % (num)
def draw(options): files = [f for f in os.listdir(options['outdir']) if f.endswith('.data')] degrees = list() diameters = list() velocities = list() for f in files: fin = open(options['outdir'] + '/' + f, 'r') ts = -1 for line in fin: if line.startswith('#'): continue time, degree, diameter, velocity = [ t.strip() for t in line.split(',') ] time = int(time) assert (ts == time - 1) ts = time try: degrees[time].append(float(degree)) diameters[time].append(int(diameter)) velocities[time].append(float(velocity)) except IndexError: degrees.append([float(degree)]) diameters.append([int(diameter)]) velocities.append([float(velocity)]) polies = list() times = range(len(degrees)) times2 = times + times[::-1] degrees_conf_upper = [confidence(d)[0] for d in degrees] degrees_conf_lower = [confidence(d)[1] for d in degrees] polies.append( conf2poly(times, degrees_conf_upper, degrees_conf_lower, color='blue')) diameters_conf_upper = [confidence(d)[0] for d in diameters] diameters_conf_lower = [confidence(d)[1] for d in diameters] polies.append( conf2poly(times, diameters_conf_upper, diameters_conf_lower, color='blue')) velocities_conf_upper = [confidence(d)[0] for d in velocities] velocities_conf_lower = [confidence(d)[1] for d in velocities] polies.append( conf2poly(times, velocities_conf_upper, velocities_conf_lower, color='green')) velocities = [scipy.mean(d) for d in velocities] diameters = [scipy.mean(d) for d in diameters] degrees = [scipy.mean(d) for d in degrees] fig = MyFig(options, figsize=(10, 8), xlabel='Time [s]', ylabel='Metric', grid=False, legend=True, aspect='auto', legend_pos='upper right') patch_collection = PatchCollection(polies, match_original=True) patch_collection.set_alpha(0.3) patch_collection.set_linestyle('dashed') fig.ax.add_collection(patch_collection) fig.ax.plot(times, degrees, label='Mean degree', color='blue') fig.ax.plot(times, diameters, label='Diameter', color='red') fig.ax.plot(times, velocities, label='Mean velocity $[m/s]$', color='green') fig.ax.set_xlim(0, options['duration']) y_max = max(max(degrees), max(diameters), max(velocities)) fig.ax.set_ylim(0, y_max + 10) fig.save('metrics', fileformat='pdf')
def _plot_propagation(options): """ Plot the fraction of packets each router received from its neighbors. This can be used to (visually) detect routers that depend on a particular neighbors. tags: Can be used with any tag/configuration. One plot is generated for each! """ locs = options['locs'] cursor = options['db_conn'].cursor() colors = options['color2'](pylab.linspace(0, 1, 101)) units2meter = options['units2meter'] ################################################################################# ## Get mapping of hosts and interface addresses ################################################################################# cursor.execute(''' SELECT DISTINCT(host), rx_if FROM rx ''') addr2host = {} for host, rx_if in cursor.fetchall(): addr2host[rx_if] = host ################################################################################ # Evaluate for all sources ################################################################################ for i, src in enumerate(options['src']): logging.info('src=%s (%d/%d)', src, i+1, len(options['src'])) options['prefix'] = src ################################################################################# ## Get all hostnames ################################################################################# #cursor.execute('SELECT host FROM addr') #dsts = sorted([str(d[0]) for d in cursor.fetchall()]) ################################################################################ # Evaluate received packets for each tag for each node ################################################################################ tags = cursor.execute(''' SELECT key, id FROM tag ''').fetchall() for j, (tag_key, tag_id) in enumerate(tags): logging.info('\ttag=%s (%d/%d)', tag_id, j+1, len(tags)) results = cursor.execute(''' SELECT host, total, frac FROM eval_fracsOfHosts WHERE src=? AND tag_key=? ''', (src, tag_key)).fetchall() ################################################################################ # Draw figure for current tag ################################################################################ fig = MyFig(options, rect=[0.1, 0.1, 0.8, 0.7], xlabel='x Coordinate', ylabel='y Coordinate') fig3d = MyFig(options, rect=[0.1, 0.1, 0.8, 0.7], xlabel='x Coordinate', ylabel='y Coordinate', zlabel='z Coordinate', ThreeD=True) fig.ax.set_autoscalex_on(False) fig.ax.set_autoscaley_on(False) min_x = min_y = numpy.infty max_x = max_y = max_z = 0 circ_max = 5 line_max = 10 floor_factor = 2 floor_skew = -0.25 line_min = 1 # first draw the links.... for host, _total, _frac in results: try: xpos, ypos, zpos = locs[host] except KeyError: logging.warning('no position found for node %s', host) continue xpos = xpos*units2meter ypos = ypos*units2meter zpos = zpos*units2meter prevs = cursor.execute(''' SELECT prev, frac FROM eval_prevHopFraction WHERE src=? AND tag_key=? and cur=? ''', (src, tag_key, host)).fetchall() for prev, frac in prevs: try: prev_xpos, prev_ypos, prev_zpos = locs[addr2host[prev]] except KeyError: logging.warning('no position found for node %s', prev) continue prev_xpos = prev_xpos*units2meter prev_ypos = prev_ypos*units2meter prev_zpos = prev_zpos*units2meter fig.ax.plot( [xpos+zpos*floor_skew*floor_factor, prev_xpos+prev_zpos*floor_skew*floor_factor], [ypos+zpos*floor_factor, prev_ypos+prev_zpos*floor_factor], linestyle='-', color=colors[frac*100], linewidth=max(line_max*frac, line_min), alpha=0.3) fig3d.ax.plot( [xpos, prev_xpos], [ypos, prev_ypos], [zpos, prev_zpos], linestyle='-', color=colors[frac*100], linewidth=max(line_max*frac, line_min), alpha=0.3) # ...then draw the nodes for host, _total, frac in results: try: xpos, ypos, zpos = locs[host] except KeyError: logging.warning('no position found for node %s', host) continue xpos = xpos*units2meter ypos = ypos*units2meter zpos = zpos*units2meter max_x = max(xpos, max_x) max_y = max(ypos, max_y) min_x = min(xpos, min_x) min_y = min(ypos, min_y) max_z = max(zpos, max_z) fig.ax.plot( xpos+zpos*floor_skew*floor_factor, ypos+zpos*floor_factor, 'o', color=colors[int(frac*100)], ms=max(frac*circ_max, 1)) fig3d.ax.plot( [xpos], [ypos], [zpos], 'o', color=colors[int(frac*100)], ms=max(frac*circ_max, 1)) drawBuildingContours(fig3d.ax, options) fig.ax.axis((min_x-10, max_x+10, min_y-10, max_y+10+max_z*floor_factor+10)) colorbar_ax = fig.fig.add_axes([0.1, 0.875, 0.8, 0.025]) colorbar_ax3d = fig3d.fig.add_axes([0.1, 0.875, 0.8, 0.025]) alinspace = numpy.linspace(0, 1, 100) alinspace = numpy.vstack((alinspace, alinspace)) for tax in [colorbar_ax, colorbar_ax3d]: tax.imshow(alinspace, aspect='auto', cmap=options['color2']) tax.set_xticks(range(0, 101, 25)) tax.set_xticklabels(numpy.arange(0.0, 101.0, 0.25), fontsize=0.8*options['fontsize']) tax.set_yticks([]) tax.set_title(tag_id, size=options['fontsize']) fig.save('propagation2d_%s' % tag_id) fig3d.save('propagation3d_%s' % tag_id)
def plot_pathlen(options): ''' Plot the distances from each node to each other node ''' cursor = options['db_conn'].cursor() options['prefix'] = 'all' steps = 1.0/options['bins'] quantiles = numpy.arange(0, 1.0+steps*0.9, steps) fig_avr_len = MyFig(options, grid=True, legend=True, xlabel='Hops', ylabel='CDF of Average Distances', aspect='auto') fig_max_len = MyFig(options, grid=True, legend=True, xlabel='Hops', ylabel='CDF of Maximum Distances', aspect='auto') fig_max_avr_len_meds = MyFig(options, grid=True, xlabel='Maximum Distance', ylabel='Median of Average Distance') cursor.execute('''SELECT host FROM addr''') hosts = cursor.fetchall() vertices = digraph() for host, in hosts: vertices.add_node(host) tag_ids = cursor.execute('SELECT DISTINCT(id), helloSize FROM tag ORDER BY helloSize').fetchall() colors = cm.hsv(pylab.linspace(0, 0.8, len(tag_ids))) for i, (tag_id, hello_size) in enumerate(tag_ids): logging.info('tag_id=\"%s\" (%d/%d)', tag_id, i+1, len(tag_ids)) fig_max_avr_len = MyFig(options, grid=True, xlabel='Maximum Distance', ylabel='Average Distance') links = cursor.execute('SELECT src,host,pdr FROM eval_helloPDR WHERE tag_id=?''', (tag_id,)).fetchall() lens = [] maxl = [] max_avr_len = [] graph = deepcopy(vertices) for src, host, pdr in links: graph.add_edge((src, host), wt=1) for host, in hosts: stp, distances = minmax.shortest_path(graph, host) if len(distances) > 1: vals = [t for t in distances.values() if t>0] lens.append(scipy.average(vals)) maxl.append(max(vals)) max_avr_len.append((maxl[-1], lens[-1])) avr_values = stats.mstats.mquantiles(lens, prob=quantiles) max_values = stats.mstats.mquantiles(maxl, prob=quantiles) fig_avr_len.ax.plot(avr_values, quantiles, '-', color=colors[i], label=str(hello_size)+' bytes') fig_max_len.ax.plot(max_values, quantiles, '-', color=colors[i], label=str(hello_size)+' bytes') points = [] for max_avr_dist in sorted(set([x[0] for x in max_avr_len])): median = scipy.median([y[1] for y in max_avr_len if y[0]==max_avr_dist]) fig_max_avr_len.ax.plot(max_avr_dist, median, 'o', color='black', label='') points.append((max_avr_dist, median)) fig_max_avr_len_meds.ax.plot([x[0] for x in points], [y[1] for y in points], color=colors[i], label=str(hello_size)+' bytes') fig_max_avr_len.ax.scatter([x[0] for x in max_avr_len], [y[1] for y in max_avr_len], color=colors[i], label='') fig_max_avr_len.ax.axis((0, max([x[0] for x in max_avr_len])+1, 0, max([y[1] for y in max_avr_len])+1)) fig_max_avr_len.save('dist-average_over_max_%d' % (hello_size)) for _ax in [fig_avr_len.ax, fig_max_len.ax]: _ax.set_yticks(numpy.arange(0.1, 1.1, 0.1)) fig_avr_len.save('dist-cdf_avr') fig_max_len.save('dist-cdf_max') fig_max_avr_len_meds.save('dist-median_of_averages_over_max')
def _plot_propagation(options): """ Plot the fraction of packets each router received from its neighbors. This can be used to (visually) detect routers that depend on a particular neighbors. tags: Can be used with any tag/configuration. One plot is generated for each! """ locs = options['locs'] cursor = options['db_conn'].cursor() colors = options['color2'](pylab.linspace(0, 1, 101)) units2meter = options['units2meter'] ################################################################################# ## Get mapping of hosts and interface addresses ################################################################################# cursor.execute(''' SELECT DISTINCT(host), rx_if FROM rx ''') addr2host = {} for host, rx_if in cursor.fetchall(): addr2host[rx_if] = host ################################################################################ # Evaluate for all sources ################################################################################ for i, src in enumerate(options['src']): logging.info('src=%s (%d/%d)', src, i + 1, len(options['src'])) options['prefix'] = src ################################################################################# ## Get all hostnames ################################################################################# #cursor.execute('SELECT host FROM addr') #dsts = sorted([str(d[0]) for d in cursor.fetchall()]) ################################################################################ # Evaluate received packets for each tag for each node ################################################################################ tags = cursor.execute(''' SELECT key, id FROM tag ''').fetchall() for j, (tag_key, tag_id) in enumerate(tags): logging.info('\ttag=%s (%d/%d)', tag_id, j + 1, len(tags)) results = cursor.execute( ''' SELECT host, total, frac FROM eval_fracsOfHosts WHERE src=? AND tag_key=? ''', (src, tag_key)).fetchall() ################################################################################ # Draw figure for current tag ################################################################################ fig = MyFig(options, rect=[0.1, 0.1, 0.8, 0.7], xlabel='x Coordinate', ylabel='y Coordinate') fig3d = MyFig(options, rect=[0.1, 0.1, 0.8, 0.7], xlabel='x Coordinate', ylabel='y Coordinate', zlabel='z Coordinate', ThreeD=True) fig.ax.set_autoscalex_on(False) fig.ax.set_autoscaley_on(False) min_x = min_y = numpy.infty max_x = max_y = max_z = 0 circ_max = 5 line_max = 10 floor_factor = 2 floor_skew = -0.25 line_min = 1 # first draw the links.... for host, _total, _frac in results: try: xpos, ypos, zpos = locs[host] except KeyError: logging.warning('no position found for node %s', host) continue xpos = xpos * units2meter ypos = ypos * units2meter zpos = zpos * units2meter prevs = cursor.execute( ''' SELECT prev, frac FROM eval_prevHopFraction WHERE src=? AND tag_key=? and cur=? ''', (src, tag_key, host)).fetchall() for prev, frac in prevs: try: prev_xpos, prev_ypos, prev_zpos = locs[addr2host[prev]] except KeyError: logging.warning('no position found for node %s', prev) continue prev_xpos = prev_xpos * units2meter prev_ypos = prev_ypos * units2meter prev_zpos = prev_zpos * units2meter fig.ax.plot([ xpos + zpos * floor_skew * floor_factor, prev_xpos + prev_zpos * floor_skew * floor_factor ], [ ypos + zpos * floor_factor, prev_ypos + prev_zpos * floor_factor ], linestyle='-', color=colors[frac * 100], linewidth=max(line_max * frac, line_min), alpha=0.3) fig3d.ax.plot([xpos, prev_xpos], [ypos, prev_ypos], [zpos, prev_zpos], linestyle='-', color=colors[frac * 100], linewidth=max(line_max * frac, line_min), alpha=0.3) # ...then draw the nodes for host, _total, frac in results: try: xpos, ypos, zpos = locs[host] except KeyError: logging.warning('no position found for node %s', host) continue xpos = xpos * units2meter ypos = ypos * units2meter zpos = zpos * units2meter max_x = max(xpos, max_x) max_y = max(ypos, max_y) min_x = min(xpos, min_x) min_y = min(ypos, min_y) max_z = max(zpos, max_z) fig.ax.plot(xpos + zpos * floor_skew * floor_factor, ypos + zpos * floor_factor, 'o', color=colors[int(frac * 100)], ms=max(frac * circ_max, 1)) fig3d.ax.plot([xpos], [ypos], [zpos], 'o', color=colors[int(frac * 100)], ms=max(frac * circ_max, 1)) drawBuildingContours(fig3d.ax, options) fig.ax.axis((min_x - 10, max_x + 10, min_y - 10, max_y + 10 + max_z * floor_factor + 10)) colorbar_ax = fig.fig.add_axes([0.1, 0.875, 0.8, 0.025]) colorbar_ax3d = fig3d.fig.add_axes([0.1, 0.875, 0.8, 0.025]) alinspace = numpy.linspace(0, 1, 100) alinspace = numpy.vstack((alinspace, alinspace)) for tax in [colorbar_ax, colorbar_ax3d]: tax.imshow(alinspace, aspect='auto', cmap=options['color2']) tax.set_xticks(range(0, 101, 25)) tax.set_xticklabels(numpy.arange(0.0, 101.0, 0.25), fontsize=0.8 * options['fontsize']) tax.set_yticks([]) tax.set_title(tag_id, size=options['fontsize']) fig.save('propagation2d_%s' % tag_id) fig3d.save('propagation3d_%s' % tag_id)
def plot(options): def configure_axes(figs, protocols, protocol, i=0): for fig in figs: if len(protocols) > 1: fig.ax.set_xticks(range(0, len(protocols))) fig.ax.set_xlim(-1, len(protocols)) fig.ax.set_xticklabels(protocols, rotation=90) else: fig.ax.set_xticks([i - 1, i, i + 1]) fig.ax.set_xlim(i - 1, i + 1) fig.ax.set_xticklabels(['', protocol, '']) if protocol == 'udp': fig.ax.set_ylim(0, 1.05) fig.ax.set_yticks(pylab.linspace(0, 1, 11)) else: fig.ax.set_ybound(lower=0.0) if hasattr(fig, 'ax_2nd'): fig.ax_2nd.set_ylabel('Fraction of Successful Sessions', fontsize=options['fontsize']) fig.ax_2nd.set_ylim(0, 1.05) fig.ax_2nd.set_yticks(pylab.linspace(0, 1, 11)) cursor = options['db_conn'].cursor() options['prefix'] = 'all' color = options['color'] protocols = [ p[0] for p in cursor.execute(''' SELECT DISTINCT(protocol) FROM server_results WHERE not protocol="des-dsr-hc" ORDER BY protocol ''').fetchall() ] if options['protocol'] == 'udp': sessions = [ s[0] for s in cursor.execute(''' SELECT COUNT(protocol) FROM client_stats WHERE not protocol="des-dsr-hc" GROUP BY protocol ''').fetchall() ] #assert(len(set(sessions)) == 1) sessions = max(sessions) ylabel = 'Normalized Throughput (UDP 1 Mbps CBR)' else: sessions = 25 * 24.0 ylabel = 'TCP Throughput [Mbps]' fig_box_all = MyFig(options, xlabel='', ylabel=ylabel, aspect='auto', grid=True, twinx=True) fig_scatter_all = MyFig(options, xlabel='', ylabel=ylabel, aspect='auto', twinx=True, grid=True) color = options['color'] fig_box_only = MyFig(options, xlabel='', ylabel=ylabel, aspect='auto', grid=True) fig_scatter_only = MyFig(options, xlabel='', ylabel=ylabel, aspect='auto', grid=True) fig_sessions_only = MyFig(options, xlabel='', ylabel='Fraction of Successful Sessions', aspect='auto', grid=True) for i, protocol in enumerate(protocols): fig_proto_box = MyFig(options, xlabel='', ylabel=ylabel, aspect='auto', twinx=True, grid=True) fig_proto_scatter = MyFig(options, xlabel='', ylabel=ylabel, aspect='auto', twinx=True, grid=True) if options['protocol'] == 'udp': # get the normalized throughput for UDP throughput_raw = cursor.execute( ''' SELECT CAST(r.throughput AS REAL)/l.throughput FROM client_stats AS l LEFT JOIN server_results AS r ON r.protocol = l.protocol AND l.server_ip = r.server_ip AND l.client_ip = r.client_ip WHERE l.protocol = ? ''', (protocol, )).fetchall() throughput_mbps = numpy.array( [min(t, 1.0) for t, in throughput_raw if t]) else: # just get the throughput for UDP throughput_raw = cursor.execute( ''' SELECT throughput FROM server_results WHERE protocol = ? ''', (protocol, )).fetchall() throughput_mbps = numpy.array( [t / 10.0**6 for t, in throughput_raw]) count = cursor.execute( ''' SELECT COUNT(throughput) FROM server_results WHERE protocol = ? ''', (protocol, )).fetchone()[0] if not len(throughput_mbps): logging.warn('no results for protocol %s', protocol) continue X = numpy.array([i for x in xrange(0, len(throughput_mbps))]) bar_width = 0.21 # draw scatter plot for fig in [fig_scatter_all, fig_scatter_only, fig_proto_scatter]: _bar_width = bar_width if not hasattr(fig, 'ax_2nd'): _bar_width = 0 fig.ax.scatter(X - _bar_width, throughput_mbps, marker='x', color=fu_blue) # add fraction of sessions to second y-axis for fig in [ fig_scatter_all, fig_box_all, fig_proto_box, fig_proto_scatter ]: _bar_width = bar_width if not hasattr(fig, 'ax_2nd'): _bar_width = 0 fig.ax_2nd.bar(i + _bar_width, float(count) / sessions, width=bar_width, alpha=0.5, color='white', edgecolor='blue', facecolor=fu_blue) fig_sessions_only.ax.bar(i - 0.5 * _bar_width, float(count) / sessions, width=bar_width, alpha=0.5, color='white', edgecolor='blue', facecolor=fu_blue) # draw box plot for fig in [fig_box_all, fig_box_only, fig_proto_box]: _bar_width = bar_width if not hasattr(fig, 'ax_2nd'): _bar_width = 0 r = fig.ax.boxplot(throughput_mbps, positions=[i - _bar_width], notch=1, widths=bar_width, sym='rx', vert=1, patch_artist=False) for b in r['boxes']: b.set_color(fu_blue) b.set_fillstyle('full') for w in r['whiskers']: w.set_color(fu_blue) for m in r['medians']: m.set_color(fu_red) for f in r['fliers']: f.set_color(fu_red) configure_axes([fig_proto_box, fig_proto_scatter], [protocol], options['protocol'], i) fig_proto_box.save('throughput-%s-box' % protocol) fig_proto_scatter.save('throughput-%s-scatter' % protocol) configure_axes([ fig_box_all, fig_scatter_all, fig_box_only, fig_scatter_only, fig_sessions_only ], protocols, options['protocol']) fig_box_all.save('throughput-box') fig_box_only.save('throughput-box-only') fig_scatter_all.save('throughput-scatter') fig_scatter_only.save('throughput-scatter-only') fig_sessions_only.save('sessions-only')
def run((options, num)): def connect_graph(G, range): for n in G.nodes(): for m in G.nodes(): if m <= n: continue distance = scipy.sqrt(sum((nodes[n].cur_pos() - nodes[m].cur_pos())**2)) if distance <= range: G.add_edge(n, m) def eval_graph(G, l): components = nx.connected_components(G) diameter = nx.algorithms.diameter(G.subgraph(components[0])) mean_velocity = scipy.mean([n.cur_velocity() for n in nodes]) mean_degree = scipy.mean(G.degree().values()) l.append((time, mean_degree, diameter, mean_velocity)) def draw_graph(G, time): if not options['movie']: return fig = MyFig(options) for (n1, n2) in G.edges(): fig.ax.plot([nodes[n1].cur_pos()[0], nodes[n2].cur_pos()[0]], [nodes[n1].cur_pos()[1], nodes[n2].cur_pos()[1]], color='black', linestyle='solid', alpha=0.2) for i,n in enumerate(nodes): fig.ax.plot(n.cur_pos()[0], n.cur_pos()[1], marker='o', color=colors[i]) fig.ax.set_xlim(0, 1000) fig.ax.set_ylim(0, 1000) fig.save('graph-%04d-%04d' % (num, time), fileformat='png') scipy.random.seed() nodes = [Node(options['speed'][1], i) for i in range(0, options['nodes'])] colors = options['color'](pylab.linspace(0, 1, options['nodes'])) time = 0 values = list() metrics_out = open('%s/%04d-metrics.data' % (options['outdir'], num), 'w') metrics_out.write('# Metrics of a network where nodes move as defined by the random waypoint model\n') metrics_out.write('#\n') metrics_out.write('# %-11s: %8d\n' % ('nodes', options['nodes'])) metrics_out.write('# %-11s: %8d\n' % ('min speed', options['speed'][0])) metrics_out.write('# %-11s: %8d\n' % ('max speed', options['speed'][1])) metrics_out.write('# %-11s: %8d\n' % ('radio range', options['range'])) metrics_out.write('# %-11s: %8d\n' % ('duration', options['duration'])) metrics_out.write('# %-11s: %8d\n' % ('steps', options['steps'])) metrics_out.write('#\n') metrics_out.write('# column 0: time since start of movement [s]\n') metrics_out.write('# column 1: computed mean node degree\n') metrics_out.write('# column 2: computed network diameter [hops] (of largest component if partitioned)\n') metrics_out.write('# column 3: computed mean velocity [m/s]\n') metrics_out.write('#\n') metrics_out.write('# time, degree, diameter, velocity\n') G = nx.Graph() G.add_nodes_from([n.name for n in nodes]) connect_graph(G, options['range']) eval_graph(G, values) draw_graph(G, 0) graphs = list() while (time + options['steps']) <= options['duration']: time += options['steps'] for n in nodes: n.walk(options['steps']) G = nx.Graph() G.add_nodes_from([n.name for n in nodes]) if options['export']: for n in nodes: npos = n.cur_pos() G.node[n.name]['pos'] = npos connect_graph(G, options['range']) eval_graph(G, values) draw_graph(G, time) if options['export']: graphs.append(G) if options['export']: print 'exporting' options['graph'] = graphs export.graphToNEDMixim(options, '%04d-' % num) times = [t for (t, m, d, v) in values] degrees = [m for (t, m, d, v) in values] diameters = [d for (t, m, d, v) in values] velocities = [v for (t, m, d, v) in values] y_max = max([max(degrees), max(diameters), max(velocities)]) for i, t in enumerate(times): metrics_out.write('%4d, %f, %2d, %f\n' % (t, degrees[i], diameters[i], velocities[i])) if options['movie']: fig = MyFig(options, figsize=(10, 8), xlabel='Time [s]', ylabel='Metric', grid=False, legend=True, aspect='auto', legend_pos='upper right') fig.ax.plot(times[0:i+1], degrees[0:i+1], label='Mean degree') fig.ax.plot(times[0:i+1], diameters[0:i+1], label='Diameter') fig.ax.plot(times[0:i+1], velocities[0:i+1], label='Mean velocity $[m/s]$') fig.ax.set_xlim(0, options['duration']) fig.ax.set_ylim(0, y_max+10) fig.save('%s/metrics-%04d' % (options['outdir'], t), fileformat='png') os.system('montage -geometry 1280x720 -tile 2x1 -background white -depth 8 -mode Concatenate %s/%s-graph-%04d.png %s/%s-metrics-%04d.png %s/%s-%04d.png' % (options['outdir'], options['prefix'], i, options['outdir'], options['prefix'], i, options['outdir'], options['prefix'], i)) if options['movie']: cmd = 'ffmpeg -y -qscale 5 -r 10 -b 9600 -i %s/%s-%04d-%%04d.png %s/%04d-movie.mp4' % (options['outdir'], options['prefix'], num, options['outdir'], num) print cmd os.system(cmd) os.system('rm %s/random_waypoint-*.png' % options['outdir']) print 'finished: %d' % (num)