def _read_all_data(self): """ Read all data into dict all_data """ for train in self.all_trains: self.all_data[train] = [] for filename in self.filelist: infile = csv.reader(open(filename), delimiter=self.delim) #TODO(Simon): verify code snippet: #/start snippet values = {} for train in self.all_trains: values[train] = False # put rows into variable all_data for row in infile: if (row[0] and #skip empty rows not str_eq(row[0], 'Tognr.')): # skip legend self.all_data[row[0]].append(row) values[row[0]] = True #remember which trains had data #list missing values for key in values: if not values[key]: self.all_data[key].append('missing')
def main(args): """ Main function for module train_sorting.py """ if len(args) > 1 and str_eq(args[1], 'test'): test()
def main(args): """ Main function of module train_io.py """ if len(args) > 1 and str_eq(args[1], 'test'): inout = TrainIo('Oslo') inout.test()
def _cache_windows(self, scheduled, dep_windows, arr_windows): """ Finds windows for a dict/list of trains re """ for sched in scheduled: for train in self.get_all_trains(): if not str_eq(sched, train): #Spags? done = False while not done: for row in self.all_data[train]: if not str_eq(row, 'missing'): #Check if train departs within a 15 min window #of scheds departure/arrival if(row[self.legend['Pl.avg.']] and abs(time_diff(row[self.legend['Pl.avg.']], scheduled[sched])) < self.windowsize/2.): if not sched in dep_windows: dep_windows[sched] = [] dep_windows[sched].append(train) done = True #Check if train arrives within a 15 min window #of scheds departure/arrival if(row[self.legend['Pl.ank.']] and abs(time_diff(row[self.legend['Pl.ank.']] , scheduled[sched])) < self.windowsize/2.): if not sched in arr_windows: arr_windows[sched] = [] arr_windows[sched].append(train) done = True if done: """ Here I make the assumption that if a train has a scheduled departure/arrival time then it is stored in all the csv files. Also that the planned departure/arrival time is the same for all days, which is not always the case, but for most trains it is. """#pylint:disable=pointless-string-statement break done = True return dep_windows, arr_windows
def _list_all_trains(self): """ List all the trains """ for filename in self.filelist: infile = csv.reader(open(filename), delimiter=self.delim) for row in infile: if row[0] and not str_eq(row[0], 'Tognr.'): if row[0] not in self.all_trains: self.all_trains.append(row[0])
def _cache_sched(self): """ Reads all_data to find all scheduled departures and arrivals """ for train in self.get_all_trains(): for row in self.all_data[train]: if not str_eq(row, 'missing'): #Remember trains arr time if scheduled if row[self.legend['Pl.ank.']]: self.sched_arrs[train] = row[self.legend['Pl.ank.']] #Remember trains dep time if scheduled if row[self.legend['Pl.avg.']]: self.sched_deps[train] = row[self.legend['Pl.avg.']]
def wrt_trains(self, train1, train2, t1_dir='Departure', t2_dir='Departure'): """ Writes data from train1 and train2 to a file named train1_train2.txt """ t1_arr = None t2_arr = None #TODO(Simon): raise errors if str_eq(t1_dir, 'Arrival'): if str_eq(t2_dir, 'Arrival'): t1_arr = self.data_to_array(train1, 'Pl.ank.', 'Fakt.ank.') t2_arr = self.data_to_array(train2, 'Pl.ank.', 'Fakt.ank.') elif str_eq(t2_dir, 'Departure'): t1_arr = self.data_to_array(train1, 'Pl.ank.', 'Fakt.ank.') t2_arr = self.data_to_array(train2, 'Pl.avg.', 'Fakt.avg.') else: #Raise some kind of error pass elif str_eq(t1_dir, 'Departure'): if str_eq(t2_dir, 'Arrival'): t1_arr = self.data_to_array(train1, 'Pl.avg.', 'Fakt.avg.') t2_arr = self.data_to_array(train2, 'Pl.ank.', 'Fakt.ank.') elif str_eq(t2_dir, 'Departure'): t1_arr = self.data_to_array(train1, 'Pl.avg.', 'Fakt.avg.') t2_arr = self.data_to_array(train2, 'Pl.avg.', 'Fakt.avg.') else: #Raise some knd of error pass else: #Raise some kind of error pass #Filter out days with missing values(what does this do?) outdata = train_sorting.sort_out_complete_pairs(t1_arr, t2_arr) outfile = open(train1 + '_' + train2 + '.txt', 'w') outfile.write(train1 + ' ' + train2 + '\n') for i in range(len(outdata[0])): outfile.write(str(outdata[0][i]) + ' ' + str(outdata[1][i]) + '\n') outfile.close()
def data_to_array(self, train, plan, act): """ Stores the delay times for a specified trainnumber read from dict all_data """ train_data = self.all_data[train] delay_list = [] for row in train_data: if str_eq('missing', row): delay_list.append(float('Nan')) else: try: delay_list.append(time_diff(row[self.legend[act]], row[self.legend[plan]])) except ValueError: delay_list.append(float('Nan')) return numpy.array(delay_list) #pylint:disable=no-member
def main(args): """ Main """ if len(args) > 1 and str_eq(args[1], 'test'): test()
def main(args): """ Main function for train_dependencies.py """ if len(args) > 1 and str_eq(args[1], 'test'): test()
def main(args): """ Main function of train_algos.py """ if len(args) > 1 and str_eq(args[1], 'test'): test()