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