def write_explained(train1, train1_dir, train2, train2_dir, algtype): """ Writes portion of data explained by algorithm to csv file for R script. Format: ------------------------------ | DATE | VICTIM | DELAYER | -------- --------------------- | date_1 | time_11 | time_21 | | date_2 | time_12 | time_22 | | . | . | . | | . | . | . | | . | . | . | | date_n | time_1n | time_2n | ------------------------------ Also writes the algorithm result for the pair (train2, train1) to csv file """ data = get_matching_delay_dates(train1, train2, t1_arr=train1_dir, t2_arr=train2_dir) t1_dir = 'ARR' if train1_dir else 'DEP' t2_dir = 'ARR' if train2_dir else 'DEP' res = None all_results = None if algtype == 'waiting': all_results = csv.reader(open('ALGRES/' + t1_dir + '_VS_' + t2_dir + '_waiting.csv'), delimiter=',') else: all_results = csv.reader(open('ALGRES/' + t1_dir + '_VS_' + t2_dir + '_blocking.csv'), delimiter=',') for row in all_results: if row[0] == train1: if row[1] == train2: res = (row[4], row[5], row[6]) break if not res: print ('WARNING: Train ' + train1 + ' and ' + train2 + ' have scheduled arrival(s)/departure(s) more than'+ ' 7 minutes and 30 seconds apart.') if algtype == 'waiting': res = algorithm1_mod(data[2], data[1]) else: res = algorithm2(data[2], data[1]) filewriter = csv.writer(open(train1 + '_' + train2 + '_explained.csv', 'w'), delimiter=',') filewriter.writerow(['DATE', 'VICTIM', 'DELAYER']) for i in range(len(data[0])): if (int(float(data[1][i])) > int(float(res[1])) and int(float(data[1][i])) < int(float(res[2]))): filewriter.writerow([data[0][i][:10], data[1][i], data[2][i]]) #write algoresult filewriter = csv.writer(open(train1 + '_' + train2 + '_result.csv', 'w'), delimiter=',') filewriter.writerow(['k', 's', 'e']) filewriter.writerow([res[0], res[1], res[2]])
CSV_WRITER2 = csv.writer(open('ALGRES/' + FILENAME[11:21] + '_blocking' + '.csv', 'w'), delimiter=',') #write header CSV_WRITER1.writerow(['VICTIM', 'DELAYER', 'ISVICTIMARRIVING', 'ISDELAYERARRIVING', 'NUMOFPOINTS', 'START', 'END']) CSV_WRITER2.writerow(['VICTIM', 'DELAYER', 'ISVICTIMARRIVING', 'ISDELAYERARRIVING', 'NUMOFPOINTS', 'START', 'END']) for train in DATASTRUCTURE: for delayer in DATASTRUCTURE[train]: delayer_arr = FILENAME[18:21] == 'ARR' train_arr = FILENAME[11:14] == 'ARR' delay = get_matching_delay(delayer, train, delayer_arr, train_arr) #run algorithms res1 = algorithm1(array(delay[0]), array(delay[1])) res2 = algorithm2(array(delay[0]), array(delay[1])) if res1[2] - res1[1] > 90: CSV_WRITER1.writerow([train, delayer, int(train_arr), int(delayer_arr), res1[0], res1[1], res1[2]]) if res2[2] - res2[1] > 120: CSV_WRITER2.writerow([train, delayer, int(train_arr), int(delayer_arr), res2[0], res2[1], res2[2]])
y_cor = ([int(100*math.log(i+1)*math.cos(random.random())) for i in range(25)] + [int(1000*math.log(i+1)*math.sin(random.random())) for i in range(25)] + [int(1000*random.uniform(-0.5, 1)) for i in range(15)] + map(lambda x: x + int(random.random()*100),#pylint:disable=bad-builtin,deprecated-lambda [linear[i] - 360 for i in range(40)])) return x_cor, y_cor if __name__ == '__main__': PTS = test_data() RES1 = algorithm1_mod3(PTS[0], PTS[1]) RES2 = algorithm1(PTS[0], PTS[1]) RES3 = algorithm2(PTS[0], PTS[1]) SCORE1 = delaycounter(PTS[0], PTS[1], (RES1[1], RES1[2]), 'waiting') SCORE2 = delaycounter(PTS[0], PTS[1], (RES2[1], RES2[2]), 'waiting') SCORE3 = delaycounter(PTS[0], PTS[1], (RES3[1], RES3[2]), 'blocking') print SCORE1 print RES1 print SCORE2 print RES2 print SCORE3 print RES3 scatter_trains(PTS[0], PTS[1], RES1) scatter_trains(PTS[0], PTS[1], RES2) scatter_trains(PTS[0], PTS[1], RES3, False) plt.show()