1*6ccd8248SMilanka Ringwald#!/usr/bin/env python3 28caefee3SMatthias Ringwald 38caefee3SMatthias Ringwaldimport matplotlib.pyplot as plt 48caefee3SMatthias Ringwald#from pylab import * 5*6ccd8248SMilanka Ringwaldimport pickle 68caefee3SMatthias Ringwaldimport pylab as P 78caefee3SMatthias Ringwaldimport numpy as np 88caefee3SMatthias Ringwaldfrom matplotlib.backends.backend_pdf import PdfPages 98caefee3SMatthias Ringwaldfrom matplotlib.patches import Polygon 108caefee3SMatthias Ringwaldimport itertools 118caefee3SMatthias Ringwaldimport os 128caefee3SMatthias Ringwald 138caefee3SMatthias Ringwald 148caefee3SMatthias Ringwald 158caefee3SMatthias Ringwalddef histplot(data,labels, colors, x_label, y_label, title, fig_name, cdf): 168caefee3SMatthias Ringwald fig, ax = plt.subplots() 178caefee3SMatthias Ringwald if cdf: 188caefee3SMatthias Ringwald n, bins, patches = ax.hist(data, 20, weights=None, histtype='step', normed=True, cumulative=True, label= labels, color = colors) 198caefee3SMatthias Ringwald legend = ax.legend(loc='lower left', shadow=False) 208caefee3SMatthias Ringwald ax.grid(True) 218caefee3SMatthias Ringwald 228caefee3SMatthias Ringwald else: 238caefee3SMatthias Ringwald n, bins, patches = ax.hist( data, 20, weights=None, histtype='bar', label= labels, color = colors) 248caefee3SMatthias Ringwald legend = ax.legend(loc='upper right', shadow=False) 258caefee3SMatthias Ringwald 268caefee3SMatthias Ringwald for line in ax.get_lines(): 278caefee3SMatthias Ringwald line.set_linewidth(1.5) 288caefee3SMatthias Ringwald 298caefee3SMatthias Ringwald ax.set_xlabel(x_label) 308caefee3SMatthias Ringwald ax.set_ylabel(y_label) 318caefee3SMatthias Ringwald for label in legend.get_texts(): 328caefee3SMatthias Ringwald label.set_fontsize('small') 338caefee3SMatthias Ringwald 348caefee3SMatthias Ringwald for label in legend.get_lines(): 358caefee3SMatthias Ringwald label.set_linewidth(1.5) # the legend line width 368caefee3SMatthias Ringwald 378caefee3SMatthias Ringwald fig.suptitle(title, fontsize=12) 388caefee3SMatthias Ringwald 398caefee3SMatthias Ringwald #plt.show() 408caefee3SMatthias Ringwald pp = PdfPages(fig_name) 418caefee3SMatthias Ringwald pp.savefig(fig) 428caefee3SMatthias Ringwald pp.close() 438caefee3SMatthias Ringwald return [n, bins, patches] 448caefee3SMatthias Ringwald 458caefee3SMatthias Ringwalddef accplot(data, labels, colors, x_label, y_label, title, fig_name, annotation): 468caefee3SMatthias Ringwald mean = np.zeros(len(data)) 478caefee3SMatthias Ringwald for i in range(len(data)): 488caefee3SMatthias Ringwald if len(data[i]) > 0: 498caefee3SMatthias Ringwald mean[i] = len(data[i]) /(1.0*max(data[i])) 508caefee3SMatthias Ringwald 518caefee3SMatthias Ringwald mean = round(mean) 528caefee3SMatthias Ringwald 538caefee3SMatthias Ringwald fig, ax = plt.subplots() 548caefee3SMatthias Ringwald for i in range(len(data)): 558caefee3SMatthias Ringwald if len(data[i]) > 0: 568caefee3SMatthias Ringwald ax.plot(data[i], range(len(data[i])), colors[i], label= labels[i]+', '+mean[i]+' adv/s, total nr. '+str(len(data[i]))) 578caefee3SMatthias Ringwald 588caefee3SMatthias Ringwald ax.set_xlabel(x_label) 598caefee3SMatthias Ringwald ax.set_ylabel(y_label) 608caefee3SMatthias Ringwald for tl in ax.get_yticklabels(): 618caefee3SMatthias Ringwald tl.set_color('k') 628caefee3SMatthias Ringwald 638caefee3SMatthias Ringwald legend = ax.legend(loc='upper left', shadow=False) 648caefee3SMatthias Ringwald 658caefee3SMatthias Ringwald for label in legend.get_texts(): 668caefee3SMatthias Ringwald label.set_fontsize('small') 678caefee3SMatthias Ringwald 688caefee3SMatthias Ringwald for label in legend.get_lines(): 698caefee3SMatthias Ringwald label.set_linewidth(1.5) # the legend line width 708caefee3SMatthias Ringwald 718caefee3SMatthias Ringwald for line in ax.get_lines(): 728caefee3SMatthias Ringwald line.set_linewidth(1.5) 738caefee3SMatthias Ringwald 748caefee3SMatthias Ringwald fig.suptitle(title, fontsize=12) 758caefee3SMatthias Ringwald ax.text(400, 5000, annotation , style='italic', 768caefee3SMatthias Ringwald bbox={'facecolor':'gray', 'alpha':0.5, 'pad':10}) 778caefee3SMatthias Ringwald 788caefee3SMatthias Ringwald #plt.show() 798caefee3SMatthias Ringwald pp = PdfPages(fig_name) 808caefee3SMatthias Ringwald pp.savefig(fig) 818caefee3SMatthias Ringwald pp.close() 828caefee3SMatthias Ringwald 838caefee3SMatthias Ringwald return fig 848caefee3SMatthias Ringwald 858caefee3SMatthias Ringwalddef mean_common_len(data): 868caefee3SMatthias Ringwald mcl = 0 878caefee3SMatthias Ringwald for i in range(len(data) - 1): 888caefee3SMatthias Ringwald if len(data[i]) > 0: 898caefee3SMatthias Ringwald if mcl == 0: 908caefee3SMatthias Ringwald mcl = len(data[i]) 918caefee3SMatthias Ringwald else: 928caefee3SMatthias Ringwald mcl = min(mcl, len(data[i])) 938caefee3SMatthias Ringwald return mcl 948caefee3SMatthias Ringwald 958caefee3SMatthias Ringwalddef mean_common_time(data): 968caefee3SMatthias Ringwald mct = 0 978caefee3SMatthias Ringwald for i in range(len(data) - 1): 988caefee3SMatthias Ringwald if len(data[i]) > 0: 998caefee3SMatthias Ringwald if mct == 0: 1008caefee3SMatthias Ringwald mct = max(data[i]) 1018caefee3SMatthias Ringwald else: 1028caefee3SMatthias Ringwald mct = min(mct, max(data[i])) 1038caefee3SMatthias Ringwald return mct 1048caefee3SMatthias Ringwald 1058caefee3SMatthias Ringwalddef normalize(s): 1068caefee3SMatthias Ringwald return map(lambda x: (x - s[0]), s) 1078caefee3SMatthias Ringwald 1088caefee3SMatthias Ringwalddef delta(s): 1098caefee3SMatthias Ringwald rs = list() 1108caefee3SMatthias Ringwald for i in range(len(s)-1): 1118caefee3SMatthias Ringwald rs.append(s[i+1] - s[i]) 1128caefee3SMatthias Ringwald return rs 1138caefee3SMatthias Ringwald 1148caefee3SMatthias Ringwalddef round(s): 1158caefee3SMatthias Ringwald return map(lambda x: "{0:.4f}".format(x), s) 1168caefee3SMatthias Ringwald 1178caefee3SMatthias Ringwalddef cut(s, V): 1188caefee3SMatthias Ringwald r = list() 1198caefee3SMatthias Ringwald for i in range(len(s)): 1208caefee3SMatthias Ringwald if s[i] <= V: 1218caefee3SMatthias Ringwald r.append(s[i]) 1228caefee3SMatthias Ringwald return r 1238caefee3SMatthias Ringwald 1248caefee3SMatthias Ringwalddef prepare_data(exp_name, sensor_name): 1258caefee3SMatthias Ringwald prefix = '../data/processed/' 1268caefee3SMatthias Ringwald 1278caefee3SMatthias Ringwald scanning_type = exp_name+'_continuous' 128*6ccd8248SMilanka Ringwald mn = pickle.load(open(prefix+scanning_type+'_mac_'+sensor_name+'.data', 'rb')) # mac nio, 129*6ccd8248SMilanka Ringwald mm = pickle.load(open(prefix+scanning_type+'_mac_mac.data', 'rb')) # mac mac, 130*6ccd8248SMilanka Ringwald rn = pickle.load(open(prefix+scanning_type+'_rug_'+sensor_name+'.data', 'rb')) # ruggear nio, 131*6ccd8248SMilanka Ringwald rm = pickle.load(open(prefix+scanning_type+'_rug_mac.data', 'rb')) # ruggear mac, 1328caefee3SMatthias Ringwald 1338caefee3SMatthias Ringwald scanning_type = exp_name+'_normal' 1348caefee3SMatthias Ringwald try: 135*6ccd8248SMilanka Ringwald normal_rn = pickle.load(open(prefix + scanning_type+'_rug_'+sensor_name+'.data', 'rb')) # ruggear mac, normal 1368caefee3SMatthias Ringwald except: 1378caefee3SMatthias Ringwald normal_rn = list() 1388caefee3SMatthias Ringwald 1398caefee3SMatthias Ringwald try: 140*6ccd8248SMilanka Ringwald normal_mn = pickle.load(open(prefix + scanning_type+'_mac_'+sensor_name+'.data', 'rb')) # ruggear mac, normal 1418caefee3SMatthias Ringwald except: 1428caefee3SMatthias Ringwald normal_mn = list() 1438caefee3SMatthias Ringwald 1448caefee3SMatthias Ringwald try: 145*6ccd8248SMilanka Ringwald normal_rm = pickle.load(open(prefix + scanning_type+'_rug_mac.data', 'rb')) # ruggear mac, normal 1468caefee3SMatthias Ringwald except: 1478caefee3SMatthias Ringwald normal_rm = list() 1488caefee3SMatthias Ringwald 1498caefee3SMatthias Ringwald try: 150*6ccd8248SMilanka Ringwald normal_mm = pickle.load(open(prefix + scanning_type+'_mac_mac.data', 'rb')) # ruggear mac, normal 1518caefee3SMatthias Ringwald except: 1528caefee3SMatthias Ringwald normal_mm = list() 1538caefee3SMatthias Ringwald 1548caefee3SMatthias Ringwald 1558caefee3SMatthias Ringwald T = mean_common_time([mm, mn, rm, rn, normal_rm, normal_rn, normal_mm, normal_mn]) 1568caefee3SMatthias Ringwald L = mean_common_len([mm, mn, rm, rn, normal_rm, normal_rn, normal_mm, normal_mn]) 1578caefee3SMatthias Ringwald Z = 15 1588caefee3SMatthias Ringwald 159*6ccd8248SMilanka Ringwald print("mct %d, mcl %d" % (T,L)) 1608caefee3SMatthias Ringwald mac_mac = normalize(mm) 1618caefee3SMatthias Ringwald mac_nio = normalize(mn) 1628caefee3SMatthias Ringwald ruggeer_mac = normalize(rm) 1638caefee3SMatthias Ringwald ruggeer_nio = normalize(rn) 1648caefee3SMatthias Ringwald 1658caefee3SMatthias Ringwald ruggeer_nio_normal = normalize(normal_rn) 1668caefee3SMatthias Ringwald ruggeer_mac_normal = normalize(normal_rm) 1678caefee3SMatthias Ringwald mac_mac_normal = normalize(normal_mm) 1688caefee3SMatthias Ringwald mac_nio_normal = normalize(normal_mn) 1698caefee3SMatthias Ringwald 1708caefee3SMatthias Ringwald 1718caefee3SMatthias Ringwald delta_mn = delta(mac_nio) 1728caefee3SMatthias Ringwald delta_mm = delta(mac_mac) 1738caefee3SMatthias Ringwald delta_rn = delta(ruggeer_nio) 1748caefee3SMatthias Ringwald delta_rm = delta(ruggeer_mac) 1758caefee3SMatthias Ringwald 1768caefee3SMatthias Ringwald rn_delays = list() 1778caefee3SMatthias Ringwald for i in range(len(delta_rn)): 1788caefee3SMatthias Ringwald rn_delays.append(range(delta_rn[i])) 1798caefee3SMatthias Ringwald 1808caefee3SMatthias Ringwald flattened_rn_delays = list(itertools.chain.from_iterable(rn_delays)) 1818caefee3SMatthias Ringwald 1828caefee3SMatthias Ringwald plot_data = [cut(mac_mac,T), cut(mac_nio,T), cut(ruggeer_mac,T), cut(ruggeer_nio,T)] 1838caefee3SMatthias Ringwald plot_data_normal = [cut(mac_mac_normal,T), cut(mac_nio_normal,T), cut(ruggeer_mac_normal,T), cut(ruggeer_nio_normal,T)] 1848caefee3SMatthias Ringwald 1858caefee3SMatthias Ringwald hist_data = [delta_mm[0:L], delta_mn[0:L], delta_rm[0:L], delta_rn[0:L]] 1868caefee3SMatthias Ringwald 1878caefee3SMatthias Ringwald zoomed_hist_data = list() 1888caefee3SMatthias Ringwald if len(hist_data[0]) >= Z and len(hist_data[1]) >= Z and len(hist_data[2]) >= Z and len(hist_data[3]) >= Z : 1898caefee3SMatthias Ringwald zoomed_hist_data = [cut(hist_data[0],Z), cut(hist_data[1],Z), cut(hist_data[2],Z), cut(hist_data[3],Z)] 1908caefee3SMatthias Ringwald 1918caefee3SMatthias Ringwald return [plot_data, hist_data, zoomed_hist_data, flattened_rn_delays, plot_data_normal] 1928caefee3SMatthias Ringwald 1938caefee3SMatthias Ringwalddef plot(exp_name, sensor_name, sensor_title, prefix): 1948caefee3SMatthias Ringwald [plot_data, hist_data, zoomed_hist_data, rn_delays, plot_data_normal] = prepare_data(exp_name, sensor_name) 1958caefee3SMatthias Ringwald labels = ['Scan. BCM, Adv. BCM', 'Scan. BCM, Adv. '+ sensor_title, 'Scan. RugGear, Adv. BCM', 'Scan. RugGear, Adv. '+sensor_title] 1968caefee3SMatthias Ringwald plot_colors = ['r-','k-','b-','g-'] 1978caefee3SMatthias Ringwald hist_colors = ['red','black','blue','green'] 1988caefee3SMatthias Ringwald 1998caefee3SMatthias Ringwald title = 'Continuous scanning over time' 2008caefee3SMatthias Ringwald annotation = 'scan window 30ms, scan interval 30ms' 2018caefee3SMatthias Ringwald 2028caefee3SMatthias Ringwald x_label = 'Time [s]' 2038caefee3SMatthias Ringwald y_label = 'Number of advertisements' 2048caefee3SMatthias Ringwald accplot(plot_data, labels, plot_colors, x_label, y_label, title, prefix+sensor_name+'_acc_number_of_advertisements_continuous_scanning.pdf', annotation) 2058caefee3SMatthias Ringwald 2068caefee3SMatthias Ringwald x_label = 'Time interval between two advertisements [s]' 2078caefee3SMatthias Ringwald title = 'Continuous scanning - interval distribution' 2088caefee3SMatthias Ringwald histplot(hist_data, labels, hist_colors, x_label, y_label, title, prefix+sensor_name+'_histogram_advertisements_time_delay.pdf', 0) 2098caefee3SMatthias Ringwald 2108caefee3SMatthias Ringwald 2118caefee3SMatthias Ringwald #if len(zoomed_hist_data) > 0: 2128caefee3SMatthias Ringwald # title = 'Continuous scanning - interval distribution [0-15s]' 2138caefee3SMatthias Ringwald # histplot(zoomed_hist_data, labels, hist_colors, x_label, y_label, title, prefix+sensor_name+'_histogram_advertisements_time_delay_zoomed.pdf', 0) 2148caefee3SMatthias Ringwald 2158caefee3SMatthias Ringwald title = 'Continuous scanning - expected waiting time' 2168caefee3SMatthias Ringwald x_label = 'Expected waiting time until first scan [s]' 2178caefee3SMatthias Ringwald [n, bins, patches] = histplot([rn_delays], [labels[3]], [hist_colors[3]], x_label, y_label, title, prefix+sensor_name+'_ruggear_expected_scan_response.pdf', 0) 2188caefee3SMatthias Ringwald 2198caefee3SMatthias Ringwald title = 'Continuous scanning - expected waiting time probability distribution' 2208caefee3SMatthias Ringwald y_label = 'Advertisement probability' 2218caefee3SMatthias Ringwald x_label = 'Time until first scan [s]' 2228caefee3SMatthias Ringwald [n, bins, patches] = histplot([rn_delays], [labels[3]], [hist_colors[3]], x_label, y_label, title, prefix+sensor_name+'_ruggear_cdf.pdf', 1) 2238caefee3SMatthias Ringwald 2248caefee3SMatthias Ringwald 2258caefee3SMatthias Ringwald title = 'Normal scanning over time' 2268caefee3SMatthias Ringwald annotation = 'scan window 30ms, scan interval 300ms' 2278caefee3SMatthias Ringwald 2288caefee3SMatthias Ringwald x_label = 'Time [s]' 2298caefee3SMatthias Ringwald y_label = 'Number of advertisements' 2308caefee3SMatthias Ringwald accplot(plot_data_normal, labels, plot_colors, x_label, y_label, title, prefix+sensor_name+'_acc_number_of_advertisements_normal_scanning.pdf', annotation) 2318caefee3SMatthias Ringwald 2328caefee3SMatthias Ringwald 2338caefee3SMatthias Ringwaldpicts_folder = "../picts_experiments/" 2348caefee3SMatthias Ringwaldif not os.access(picts_folder, os.F_OK): 2358caefee3SMatthias Ringwald os.mkdir(picts_folder) 2368caefee3SMatthias Ringwald 2378caefee3SMatthias Ringwaldplot('exp1','nio', 'Nio', picts_folder) 2388caefee3SMatthias Ringwaldplot('exp2','xg2', 'XG', picts_folder) 239