def main(argv): if len(argv) != 3: print "Usage: python plot_ping_suffix.py mode file_suffix json_file" exit() mode = argv[0] if mode not in MODE: print "mode %s not recognized, set to default mode 'avg', all possible modes are %s" % (mode, MODE) mode = 'avg' suffix = argv[1] measure_id = argv[-1] filename = measure_id + '.json' if not os.path.isfile(filename): print"Measurement results for #%s doesn't exist." % measure_id exit() trace_dict = at.readPingJSON(filename) pdId_dict = read_by_suffix(suffix) for f in pdId_dict: fig = plt.figure() ax = fig.add_subplot(111, projection='3d') y_lable = [] yi=1 for probe in pdId_dict[f]: #print trace_dict[probe][mode] #time.sleep(10) trace = [-1 if x < 0 else x for x in trace_dict[probe][mode]] ax.plot(xs=trace_dict[probe]['time_md'], ys=[yi]*len(trace_dict[probe]['time_md']),zs=trace, ls='-', lw=0.5, color='black', marker='None') y_lable.append(probe) yi += 1 ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=1440)) ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d')) ax.yaxis.set_major_locator(MultipleLocator(1)) ax.set_yticklabels(y_lable) ax.set_ylabel('Probe ID', fontsize=16) ax.set_xlabel('Time', fontsize=16) ax.set_zlabel(MODE_LABEL[mode], fontsize=16) fig.set_size_inches(12, 9) #plt.show() fig_name = os.path.join(os.getcwd(), 'rtt3d_%s.pdf'% f) plt.savefig(fig_name, bbox_inches='tight', format='pdf') plt.close()
def main(argv): traceflag =False if len(argv) != 2: print "Usage: python cleaning.py t/p(trace/ping) filename" exit() trace = argv[0] if trace in TRACE: traceflag = True filename = argv[1] if not os.path.isfile(filename): print "Measurement file %s doesn't exist." % filename exit() pb_to_rm = set([]) if traceflag: print "Traceroute trace.\n" + \ "An ip-path\n" + \ "- ends with *;\n" + \ "- or contains more than %f *;\n" % INV_PATH + \ "- or contains five or more consecutive *" + \ "is considered invalid." trace_dict = at.readTraceJSON(filename) min_len = LEN_P * TRACE_LEN max_intv = INTV_MX * TRACE_INTV inv_len = INV_TRACE * TRACE_LEN #fsave = 'trace_rm.txt' fsave = PROBE_ID_TRACE_RM_FILE fval = PROBE_ID_TRACE_VAL_FILE for pbid in trace_dict: path_val_flag = [] for p in trace_dict[pbid]['ip_path']: if path_val(p, INV_PATH): path_val_flag.append(1) else: path_val_flag.append(-1) trace_dict[pbid]['path_val'] = path_val_flag val_check = 'path_val' else: print "Ping trace\n" + \ "An RTT measurement\n" + \ "- equals -1; \n" + \ "- missing value;\n" + \ "- contains err field; \n" + \ "is considered invalid." trace_dict = at.readPingJSON(filename) min_len = LEN_P * PING_LEN max_intv = INTV_MX * PING_INTV inv_len = INV_PING * PING_LEN val_check = 'avg' #fsave = 'ping_rm.txt' fsave = PROBE_ID_PING_RM_FILE fval = PROBE_ID_PING_VAL_FILE print "\nCleaning criteria:\n\ Minimum length: %f,\n\ Maximum neighbour interval: %f,\n\ Maximum invalid values: %f." % (min_len, max_intv, inv_len) for pbid in trace_dict: if not plft_stab(trace_dict[pbid]['time_epc'], max_intv, min_len): pb_to_rm.add(pbid) if trace_dict[pbid][val_check].count(-1) > inv_len: pb_to_rm.add(pbid) if pb_to_rm: print "Probes to be removed:" print "{id:<7}{len_:>10}{intv:>10}{invd:>10}".format(id='ID', len_='Len.', intv='Max. Intv', invd='# Invd.') for pb in pb_to_rm: print "{id:>7d}{len_:>10d}{intv:>10d}{invd:>10d}".format(id=pb, len_=len(trace_dict[pb]['time_epc']), intv=max(interv(trace_dict[pb]['time_epc'])), invd=trace_dict[pb][val_check].count(-1)) clean_trace = removekey(trace_dict, list(pb_to_rm)) else: clean_trace = trace_dict.copy() print "%d probes in all, %d probes after cleaning" % (len(trace_dict), len(clean_trace)) f = open(fsave, 'w') for pb in list(pb_to_rm): f.write("%d\n" % pb) f.close() f = open(fval, 'w') for pb in clean_trace: f.write("%d\n" % pb) f.close()
def main(argv): mode = 'avg' if len(argv) != 1: print "Usage: python alignRTT.py mode\n\ all possible modes are %s." % MODE exit() mode = argv[0] if mode not in MODE: print "mode %s not recognized, set to default mode 'avg', \ all possible modes are %s" % (mode, MODE) mode = 'avg' valid_id = read_pdid(PROBE_ID_VALID_FILE) alltrace = at.readPingJSON(MES_PING_FILE) pbAct = alltrace.keys() pbVal = list(set(valid_id) & set(pbAct)) clean_trace = {k: alltrace[k] for k in pbVal} print len(clean_trace) #Quantify measurment dis-sync in time with k-means len_sort = sorted(clean_trace.items(), key=lambda s:len(s[1]['time_epc']), reverse=True) #find the probe with maximum data length max_len_pb = len_sort[0][0] max_length = len(len_sort[0][1]['time_epc']) all_time_epc = [] for pb in clean_trace: for t in clean_trace[pb]['time_epc']: all_time_epc.append(t) all_time_epc = np.array(all_time_epc).astype(float).reshape(len(all_time_epc),1) # use the timestamps of the probe with max data length as the beginning centroids book = np.array(clean_trace[max_len_pb]['time_epc']).astype(float).reshape(max_length,1) alTP, dist = kmeans(all_time_epc, book) alTP = sorted(alTP) alTP = [i[0] for i in alTP] tpIntv = interv(alTP) #print "In average measurements are dis-synchronized by %.3fsec." % dist print "Calculating 'aligned' timestamps that minimize time distortion after alignment" print "Aligned timestamps have:\n\ - mean interval of %.3fsec;\n\ - interval std of %.3fsec." % (np.mean(tpIntv), np.std(tpIntv)) print "RTT measurements of each probe will be aligned to the cloest 'aligned' timestamp." filename = 'pingAL_'+mode+'.csv' f = open(filename, 'w') line = 'id,' + ','.join(['%d'%round(t) for t in alTP]) + '\n' f.write(line) mean_time_dist = 0 for pb in clean_trace: align = np.zeros(max_length) # max_length equals the length of alTP # for i in range(len(clean_trace[pb]['time_epc'])): # distance = [] # for c in alTP: # distance.append(np.linalg.norm(c - clean_trace[pb]['time_epc'][i])) # lb = np.argmin(distance) # # assign RTT measuremrnt to the cloest aligend timestamp # align[lb] = clean_trace[pb][mode][i] # it actually sounds like Dynamic Time Wrapping... last_match = 0 time_dis = [] for i in range(len(clean_trace[pb]['time_epc'])): last_dist = 10e6 last_pose = -1 for j in range(last_match, max_length, 1): # begin the search from last matched value distance = abs(alTP[j] - clean_trace[pb]['time_epc'][i]) if distance < last_dist: # if the distance is deacreasing, continue last_dist = distance last_pose = j else: # if otherwise, last searched al timestamp must be the closest break align[last_pose] = clean_trace[pb][mode][i] # assigne the measurment to the aligend postion last_match = last_pose # update last match position time_dis.append(last_dist) if last_match == (max_length - 1): # if already at the end of aligned ts, stop the procedure, even if there are still values left break clean_trace[pb]['time_dis'] = time_dis print "Probe %d, time dist: avg. %8.3f, max. %8.3f at %d" % (pb, np.mean(time_dis), np.max(time_dis), np.argmax(time_dis)) mean_time_dist += np.mean(time_dis) # in case 0 or -1 align list, # assigne the cloest (in time both direction) valide value for i in range(max_length): #if align[i] == 0 or align[i] == -1: if align[i] <= 0: left_candi = -1 right_candi = -1 for j in range(1,5,1): # neighbour search range if left_candi == -1: if (i-j >= 0) and (align[i-j] != 0 and align[i-j] != -1): left_candi = i-j if right_candi == -1: if (i+j < max_length) and (align[i+j] != 0 and align[i+j] != -1): right_candi = i+j if left_candi + right_candi > -2: #at least one candidate break if left_candi == -1: align[i] = align[right_candi] elif right_candi == -1: align[i] = align[left_candi] elif (alTP[i] - alTP[left_candi]) <= (alTP[right_candi] - alTP[i]): align[i] = align[left_candi] else: align[i] = align[right_candi] clean_trace[pb]['align'] = align line = "%s," % pb + ",".join(map(str, clean_trace[pb]['align'])) + '\n' f.write(line) f.close() print "mean time dist of all probes: %.3f" % (float(mean_time_dist)/len(clean_trace))