def gap_distribution_analysis(sessions, compressed_sessions, rules): print '-------Gap Distribution Analysis-------' speed_per_file = 20 with open('20_gap.txt', 'w') as file20, \ open('40_gap.txt', 'w') as file40, \ open('60_gap.txt', 'w') as file60, \ open('80_gap.txt', 'w') as file80, \ open('100_gap.txt', 'w') as file100: filelist = [file20, file40, file60, file80, file100] for session, compressed_session in zip(sessions, compressed_sessions): index = 0 agg_record = None speed = None last_record = None last_record_speed = None for record in session: while agg_record == None or agg_record.ID != record.agg_ID: agg_record = compressed_session[index] speed_range = get_speed(agg_record, rules) if speed_range != None: speed = speed_range[1] else: speed = None index += 1 if last_record_speed != None and speed != None \ and last_record_speed == speed: speed_index = min(int(speed/speed_per_file),4) gap = record.time - last_record.time filelist[speed_index].write( \ '{0}\n'.format(gap)) last_record = record last_record_speed = speed
def gap_distribution_analysis(sessions, compressed_sessions, rules): print '-------Gap Distribution Analysis-------' speed_per_file = 20 with open('20_gap.txt', 'w') as file20, \ open('40_gap.txt', 'w') as file40, \ open('60_gap.txt', 'w') as file60, \ open('80_gap.txt', 'w') as file80, \ open('100_gap.txt', 'w') as file100: filelist = [file20, file40, file60, file80, file100] for session, compressed_session in zip(sessions, compressed_sessions): index = 0 agg_record = None speed = None last_record = None last_record_speed = None for record in session: while agg_record == None or agg_record.ID != record.agg_ID: agg_record = compressed_session[index] speed_range = get_speed(agg_record, rules) if speed_range != None: speed = speed_range[1] else: speed = None index += 1 if last_record_speed != None and speed != None \ and last_record_speed == speed: speed_index = min(int(speed / speed_per_file), 4) gap = record.time - last_record.time filelist[speed_index].write( \ '{0}\n'.format(gap)) last_record = record last_record_speed = speed
def speed_filter_analysis(compressed_sessions, rules): with open('raw_speed.txt', 'w') as rs, open('filtered_speed.txt', 'w') as fs: for compressed_session in compressed_sessions: for agg_record in compressed_session: rs.write('{0}\n'.format(agg_record.speed[1])) filtered_speed = get_speed(agg_record, rules) if filtered_speed != None: fs.write('{0}\n'.format(filtered_speed[1]))
def estimated_speed_error_analysis(compressed_session, rules): print '-------Estimated Speed Error Analysis-------' # all unit are in number of records record_cnt = 0 record_westspeed_cnt = 0 record_passed_cnt = 0 max_error_bin_cnt = 10 error_per_bin = 0.1 speedest_error_dist = [0] * max_error_bin_cnt speedestalt_error_dist = [0] * max_error_bin_cnt passed_speedest_error_dist = [0] * max_error_bin_cnt passed_speedestalt_error_dist = [0] * max_error_bin_cnt for agg_record in compressed_session: record_cnt += agg_record.record_cnt # for estimated speed only speedest_error = 0 if agg_record.speed != (None, None): record_westspeed_cnt += agg_record.record_cnt speedest_error = abs(agg_record.speed[1] - agg_record.speed_gt) / agg_record.speed_gt # for i in range(agg_record.record_cnt): # if speedest_error < 1: # print speedest_error # else: # print 1 # for i in range(agg_record.record_cnt): # print abs(agg_record.speed[1] - agg_record.speed_gt) speedestalt_error = abs(agg_record.speed[1] - agg_record.speed_gt_alt) / agg_record.speed_gt_alt speedest_error_index = min(int(speedest_error /\ error_per_bin), max_error_bin_cnt - 1) speedestalt_error_index = min(int(speedestalt_error /\ error_per_bin), max_error_bin_cnt - 1) speedest_error_dist[speedest_error_index] += \ agg_record.record_cnt speedestalt_error_dist[speedestalt_error_index] += \ agg_record.record_cnt speed_range = get_speed(agg_record, rules) if speed_range != None: record_passed_cnt += agg_record.record_cnt for i in range(agg_record.record_cnt): print abs(agg_record.speed[1] - agg_record.speed_gt) passed_speedest_error_dist[speedest_error_index] += \ agg_record.record_cnt passed_speedestalt_error_dist[speedestalt_error_index] += \ agg_record.record_cnt print 'Counters: ', record_cnt, record_westspeed_cnt, record_passed_cnt print 'Speed estimation error distribution:' print speedest_error_dist print 'Alternative speed estimation error distribution:' print speedestalt_error_dist print 'Passed speed estimation error distribution:' print passed_speedest_error_dist print 'Passed alternative speed estimation error distribution:' print passed_speedestalt_error_dist
def speed_usage_pattern_analysis(sessions, compressed_sessions, rules): print '-------Speed Usage Pattern Analysis-------' speed_gap_dist = [] speed_vol_dist = [] speed_gap_sum_dist = [0] * max_speed_bin_cnt speed_vol_sum_dist = [0] * max_speed_bin_cnt speed_cnt_dist = [0] * max_speed_bin_cnt for session, compressed_session in zip(sessions, compressed_sessions): index = 0 agg_record = None speed = None last_record = None last_record_speed = None for record in session: while agg_record == None or agg_record.ID != record.agg_ID: agg_record = compressed_session[index] speed_range = get_speed(agg_record, rules) if speed_range != None: speed = speed_range[1] else: speed = None index += 1 if last_record_speed != None and speed != None and \ last_record_speed == speed: speed_index = int(speed / speed_per_bin) if speed_index > max_speed_bin_cnt - 1: continue speed_gap_sum_dist[speed_index] += \ record.time - last_record.time speed_vol_sum_dist[speed_index] += record.vol speed_cnt_dist[speed_index] += 1 last_record = record last_record_speed = speed for vol_sum, gap_sum, cnt in zip(\ speed_vol_sum_dist, speed_gap_sum_dist, speed_cnt_dist): if cnt != 0: speed_gap_dist.append(gap_sum / cnt) speed_vol_dist.append(vol_sum / cnt) else: print gap_sum speed_gap_dist.append(0) print 'Distribution of gaps for various speed:' print speed_gap_dist print 'Distribution of vols for various speed:' print speed_vol_dist
def gather_speed_appcat_stat(compressed_sessions, rules): speed_appcat_stat = {} for compressed_session in compressed_sessions: for agg_record in compressed_session: speed_range = get_speed(agg_record, rules) if speed_range == None: continue speed_est = speed_range[1] for app_cat in agg_record.app_access_cnt: if app_cat not in speed_appcat_stat: speed_appcat_stat[app_cat] = [] speed_appcat_stat[app_cat].append((speed_est, \ agg_record.app_access_cnt[app_cat], \ agg_record.app_access_vol[app_cat], \ agg_record.duration[1] - agg_record.duration[0])) return speed_appcat_stat
def speed_app_used_analysis(compressed_sessions, rules): print '-------Speed app used Analysis-------' speed_app_dist = [] speed_app_sum = [0] * max_speed_bin_cnt speed_app_cat_dist = [] speed_app_cat_sum = [0] * max_speed_bin_cnt speed_duration_sum = [0] * max_speed_bin_cnt for compressed_session in compressed_sessions: speed_index = None last_speed_index = None app = set() cat = set() start_time = None for agg_record in compressed_session: speed_range = get_speed(agg_record, rules) if speed_range != None: speed = speed_range[1] speed_index = int(speed / speed_per_bin) if speed_index != last_speed_index: if last_speed_index != None and \ last_speed_index <= max_speed_bin_cnt - 1: speed_app_sum[last_speed_index] += len(app) speed_app_cat_sum[last_speed_index] += len(cat) speed_duration_sum[last_speed_index] += \ agg_record.duration[0] - start_time app.clear() cat.clear() start_time = agg_record.duration[0] for app_cat in agg_record.app_access_cnt: app.add(app_cat) cat.add(app_cat[0]) last_speed_index = speed_index for app_sum, app_cat_sum, duration in zip( \ speed_app_sum, speed_app_cat_sum, speed_duration_sum): speed_app_dist.append(app_sum / duration) speed_app_cat_dist.append(app_cat_sum / duration) print 'Distribution of app used for various speed:' print speed_app_dist print 'Distribution of app category used for various speed:' print speed_app_cat_dist
def example_user_analysis(sessions, compressed_sessions, rules): print '-------Example User Analysis-------' eu_fn = 'results/example_user.txt' with open(eu_fn, 'w') as euf: for session, compressed_session in zip( \ sessions, compressed_sessions): tower_set = set() for record in session: tower_set.add(record.pos) if len(tower_set) > 7: index = 0 agg_record = None speed_range = (None, None) for record in session: while agg_record == None or \ agg_record.ID != record.agg_ID: agg_record = compressed_session[index] index += 1 speed_range = get_speed(agg_record, rules) euf.write('{0} {1} {2} {3}\n'.format(record.time, \ record.pos[0], record.pos[1], speed_range)) break
def speed_record_analysis(compressed_sessions, rules): print '-------Speed Record Analysis-------' speed_record_total = 0 speed_record_filtered_total = 0 speed_record_dist = [0] * max_detailed_bin_cnt speed_record_filtered_dist = [0] * max_detailed_bin_cnt for compressed_session in compressed_sessions: for agg_record in compressed_session: speed_range = get_speed(agg_record, rules) if speed_range == None: if agg_record.speed != (None, None): speed_est = min(agg_record.speed[1], \ max_detailed_bin_cnt - 1) speed_record_filtered_total += \ agg_record.record_cnt speed_record_filtered_dist[int(speed_est)] += \ agg_record.record_cnt else: speed_est = speed_range[1] if speed_est > max_detailed_bin_cnt - 1: continue speed_record_total += agg_record.record_cnt speed_record_dist[int(speed_est)] += agg_record.record_cnt ''' speed_record_dist = [item / float(speed_record_total) \ for item in speed_record_dist] speed_record_filtered_dist = [item / float(speed_record_filtered_total) \ for item in speed_record_filtered_dist] ''' print 'Number of records that have speed estimates: {0}'.\ format(speed_record_total) print 'Number of records that have been filtered: {0}'.\ format(speed_record_filtered_total) print 'Distribution of speed estimates:' print speed_record_dist print 'Distribution of filtered speed estimates:' print speed_record_filtered_dist
def speed_record_analysis(compressed_sessions, rules): print '-------Speed Record Analysis-------' speed_record_total = 0 speed_record_filtered_total = 0 speed_record_dist = [0] * max_detailed_bin_cnt speed_record_filtered_dist = [0] * max_detailed_bin_cnt for compressed_session in compressed_sessions: for agg_record in compressed_session: speed_range = get_speed(agg_record, rules) if speed_range == None: if agg_record.speed != (None, None): speed_est = min(agg_record.speed[1], \ max_detailed_bin_cnt - 1) speed_record_filtered_total += agg_record.record_cnt speed_record_filtered_dist[int(speed_est)] += \ agg_record.record_cnt else: speed_est = speed_range[1] if speed_est > max_detailed_bin_cnt - 1: continue speed_record_total += agg_record.record_cnt speed_record_dist[int(speed_est)] += agg_record.record_cnt ''' speed_record_dist = [item / float(speed_record_total) \ for item in speed_record_dist] speed_record_filtered_dist = [item / float(speed_record_filtered_total) \ for item in speed_record_filtered_dist] ''' print 'Number of records that have speed estimates: {0}'.\ format(speed_record_total) print 'Number of records that have been filtered: {0}'.\ format(speed_record_filtered_total) print 'Distribution of speed estimates:' print speed_record_dist print 'Distribution of filtered speed estimates:' print speed_record_filtered_dist
def estimated_speed_basic_analysis(compressed_sessions, rules): print '-------Estimated Speed Basic Analysis-------' # all unit are in number of records record_cnt = 0 record_wlb_cnt = 0 record_wzerolb_cnt = 0 record_westspeed_cnt = 0 record_wcompensatespeed_cnt = 0 record_wspeed_cnt = 0 speed_per_bin = 8 speed_est_dist = [0] * max_bin_cnt speed_lb_dist = [0] * max_bin_cnt speed_ratio_dist = [0] * max_bin_cnt speed_diff_per_bin = 5 speed_diff_dist = [0] * max_bin_cnt speed_est_pass_dist = [0] * max_bin_cnt speed_lb_pass_dist = [0] * max_bin_cnt for compressed_session in compressed_sessions: for agg_record in compressed_session: record_cnt += agg_record.record_cnt if agg_record.dist_lb != None: record_wlb_cnt += agg_record.record_cnt if agg_record.dist_lb == 0: record_wzerolb_cnt += agg_record.record_cnt if agg_record.speed != (None, None): record_westspeed_cnt += agg_record.record_cnt if agg_record.compensate_speed != (None, None): record_wcompensatespeed_cnt += agg_record.record_cnt if agg_record.speed != (None, None) or \ agg_record.compensate_speed != (None, None): record_wspeed_cnt += agg_record.record_cnt # for estimated speed only if agg_record.speed != (None, None): speed_est_index = min(int(agg_record.speed[1] /\ speed_per_bin), max_bin_cnt - 1) speed_est_dist[speed_est_index] += agg_record.record_cnt speed_lb_index = min(int(agg_record.speed[0] /\ speed_per_bin), max_bin_cnt - 1) speed_lb_dist[speed_lb_index] += agg_record.record_cnt if agg_record.speed[0] == 0: # speed[1] cloud not be 0 speed_ratio_index = max_bin_cnt - 1 else: speed_ratio_index = min(int(agg_record.speed[1] /\ agg_record.speed[0]), max_bin_cnt - 1) speed_ratio_dist[speed_ratio_index] += agg_record.record_cnt speed_diff = agg_record.speed[1] - agg_record.speed[0] speed_diff_index = min(int(speed_diff / speed_diff_per_bin), \ max_bin_cnt - 1) speed_diff_dist[speed_diff_index] += agg_record.record_cnt speed_range = get_speed(agg_record, rules) if speed_range != None: speed_est_pass_index = min(int(speed_range[1] /\ speed_per_bin), max_bin_cnt - 1) speed_est_pass_dist[speed_est_pass_index] += \ agg_record.record_cnt speed_lb_pass_index = min(int(speed_range[0] /\ speed_per_bin), max_bin_cnt - 1) speed_lb_pass_dist[speed_lb_pass_index] += \ agg_record.record_cnt print 'NOR with distance lower bound: {0}'.format(record_wlb_cnt) print 'NOR with zero distance lower bound: {0}'.format(record_wzerolb_cnt) print 'NOR with estimated speed: {0}'.format(record_westspeed_cnt) print 'NOR with compensate speed: {0}'.format(record_wcompensatespeed_cnt) print 'NOR with either speed: {0}'.format(record_wspeed_cnt) print 'Estimated speed distribution ({0} km/h per bin):'.format(\ speed_per_bin) print speed_est_dist print 'Speed lower bound distribution ({0} km/h per bin):'.format(\ speed_per_bin) print speed_lb_dist print 'Speed ratio distribution:' print speed_ratio_dist print 'Speed difference distribution ({0} km/h per bin):'.format(\ speed_diff_per_bin) print speed_diff_dist print 'Passed estimated speed distribution ({0} km/h per bin):'.format(\ speed_per_bin) print speed_est_pass_dist print 'Passed speed lower bound distribution ({0} km/h per bin):'.format(\ speed_per_bin) print speed_lb_pass_dist print ''
def speed_usage_pattern_analysis(sessions, compressed_sessions, rules): print '-------Speed Usage Pattern Analysis-------' speed_gap_dist = [] speed_vol_dist = [] speed_app_switch_dist = [] speed_app_cat_switch_dist = [] speed_app_div_dist = [] speed_app_cat_div_dist = [] speed_gap_sum = [0] * max_speed_bin_cnt speed_vol_sum = [0] * max_speed_bin_cnt speed_app_switch_sum = [0] * max_speed_bin_cnt speed_app_cat_switch_sum = [0] * max_speed_bin_cnt speed_app_div_sum = [0] * max_speed_bin_cnt speed_app_cat_div_sum = [0] * max_speed_bin_cnt speed_cnt_sum = [0] * max_speed_bin_cnt speed_agg_cnt_sum = [0] * max_speed_bin_cnt active_threshold = 30 # how long before we end the session speed_app_active_dist = [] speed_app_active_sum = [0] * max_speed_bin_cnt for session, compressed_session in \ zip(sessions, compressed_sessions): index = 0 agg_record = None speed = None last_record = None last_record_speed = None app_switch = set() app_cat_switch = set() speed_index = -1 active_set = {} # app name to last visit for record in session: while agg_record == None or agg_record.ID != record.agg_ID: if speed_index != -1 and \ speed_index <= max_speed_bin_cnt - 1: # dont need to do this for last agg record, # as it wont have speed est duration = agg_record.duration[1] - \ agg_record.duration[0] # if duration == 0: # duration = 1 speed_app_switch_sum[speed_index] += \ float(len(app_switch)) speed_app_cat_switch_sum[speed_index] += \ float(len(app_cat_switch)) speed_agg_cnt_sum[speed_index] += duration app_switch.clear() app_cat_switch.clear() speed_index = -1 agg_record = compressed_session[index] speed_range = get_speed(agg_record, rules) if speed_range != None: speed = speed_range[1] else: speed = None index += 1 if last_record_speed != None and speed != None and \ last_record_speed == speed: speed_index = int(speed / speed_per_bin) if speed_index > max_speed_bin_cnt - 1: continue speed_gap_sum[speed_index] += \ record.time - last_record.time speed_vol_sum[speed_index] += record.vol app_switch.add(record.app_cat); app_cat_switch.add(record.app_cat[0]); # if record.app_cat != last_record.app_cat: # speed_app_switch_sum[speed_index] += 1 # if record.app_cat[0] != last_record.app_cat[0]: # speed_app_cat_switch_sum[speed_index] += 1 # update active set for app_cat, last_visit in active_set.items(): if record.time - last_visit > active_threshold: del active_set[app_cat] # add new app to active set if record.app_cat[0] not in active_set: active_set[record.app_cat[0]] = record.time # count stack speed_app_active_sum[speed_index] += len(active_set) speed_cnt_sum[speed_index] += 1 last_record = record last_record_speed = speed for vol_sum, gap_sum, switch_sum, cat_switch_sum, active_sum, \ cnt, agg_cnt \ in zip(speed_vol_sum, speed_gap_sum, \ speed_app_switch_sum, speed_app_cat_switch_sum, \ speed_app_active_sum, \ speed_cnt_sum, speed_agg_cnt_sum): if cnt != 0: speed_gap_dist.append(gap_sum / cnt) speed_vol_dist.append(vol_sum / cnt) speed_app_switch_dist.append(switch_sum / agg_cnt) speed_app_cat_switch_dist.append( \ cat_switch_sum / agg_cnt) speed_app_active_dist.append( \ float(active_sum) / cnt) else: print 'ERROR: zero count' print gap_sum speed_gap_dist.append(0) # print 'Distribution of gaps for various speed:' # print speed_gap_dist # print 'Distribution of vols for various speed:' # print speed_vol_dist print 'Distribution of app switch for various speed:' print speed_app_switch_dist print 'Distribution of app category switch for various speed:' print speed_app_cat_switch_dist