Ejemplo n.º 1
0
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]])
Ejemplo n.º 2
0
#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_mod(array(delay[0]), array(delay[1]))
        res2 = algorithm2(array(delay[0]), array(delay[1]))

        if res1[1] != float('inf') and res1[2] != float('inf'):
            if (res1[2] - res1[1]) > 90 and res1[0] > 20:
                CSV_WRITER1.writerow([train, delayer, int(train_arr),
                                      int(delayer_arr), res1[0], res1[1],
                                      res1[2]])

        if res2[1] != float('inf') and res2[2] != float('inf'):
            if (res2[2] - res2[1]) > 120 and res2[0] > 20:
                CSV_WRITER2.writerow([train, delayer, int(train_arr),
                                      int(delayer_arr), res2[0], res2[1],
                                      res2[2]])