# snktagarwal/IRN

Change Zonal Traffic/Delay calculation

1 parent 5d17763 commit 9b90c303f09de05c67eecd7ee0c05e1578aa8dcf Sanket Agarwal committed Apr 5, 2012
Showing with 105 additions and 37 deletions.
1. +105 −37 delay_profile/TrainModel.py
 @@ -563,8 +563,31 @@ def hourlyAverageTrafficPS(self): # print self.hourly_traffic_dict + def hourlyAverageTrafficPZ(self): + + """ This function takes the day's snapshot and finds the average + traffic in hour slots caused in a zone """ + + self.hourly_traffic_dict_z = [] + + for zone in Segments.all_belts: + + # Extract the list of segments in the zone + seg_zone = filter(lambda x: x.seg in zone, self.seg_set) + + # Now average hours over all these segments + t1 = {} + for k in seg_zone[0].hourly_delay.keys(): + t1[k] = sum([sum(seg.hourly_delay[k].values()) for seg in seg_zone]) + + self.hourly_traffic_dict_z.append(t1) + + def hourlyAverageDelayPS(self): + """ This function takes the day's snapshot and finds the average + delay per train in hour slots caused in a segment """ + self.hourly_delay_dict = [] t_delay= map(lambda x: x.hourly_delay, self.seg_set) @@ -576,6 +599,32 @@ def hourlyAverageDelayPS(self): else: t1[k] = 0 self.hourly_delay_dict.append(t1) + def hourlyAverageDelayPZ(self): + + """ This function takes the day's snapshot and finds the average + delay per train in hour slots caused in a zone """ + + self.hourly_delay_dict_z = [] + + for zone in Segments.all_belts: + + # Extract the list of segments in the zone + seg_zone = filter(lambda x: x.seg in zone, self.seg_set) + + # Now average hours over all these segments + t1 = {} + for k in seg_zone[0].hourly_delay.keys(): + num = 0 + den = 0 + for seg in seg_zone: + num = num + sum(seg.hourly_delay[k].values()) + den = den + len(seg.hourly_delay[k].keys()) + if den > 0: + t1[k] = float(num)/den + else: t1[k] = 0 + + self.hourly_delay_dict_z.append(t1) + def constructTrainIndex(self, blk_size): """ Construct an Index from files read everyday using running information """ @@ -661,74 +710,93 @@ def index(self): idx = Indexing(fn) idx.constructTrainIndex(10) idx.constructSegmentsIndex() - #idx.normalizeDelay() self.idx_list.append(idx) - def hourVsSegmentDelayMat(self, p = True): + def avgOfMatrices(self, mat_list, m, n): - """ Constructs a segment vs hours matrix ( 54 x 12 ) which contains - Delay metrics """ + """ Averages m x n matrices specified in mat_list. For example if + m1 = [[1,2],[1,2]] and m2 = [[2,2],[3,3]] then avg(m1, m2) is but: + m_avg = [[1.5 ,2],[2 ,2.5]]""" - self.seg_hour_delay_mat = None + # Initialize + m_avg = copy.deepcopy(mat_list[0]) + l = len(mat_list) - for idx in self.idx_list: + for mat in mat_list[1:]: + for i in range(m): + for j in range(n): + m_avg[i][j] += mat[i][j] - # Create a copy if still none - if not self.seg_hour_delay_mat: - self.seg_hour_delay_mat = copy.deepcopy(idx.hourly_delay_dict) - continue + for i in range(m): + for j in range(n): + m_avg[i][j] = float(m_avg[i][j])/l - # Update - for i in range(len(Segments.all_segments)): - for j in range(len(self._hours)): - self.seg_hour_delay_mat[i][j] += idx.hourly_delay_dict[i][j] + return m_avg - # Average - for i in range(len(Segments.all_segments)): - for j in range(len(self._hours)): - self.seg_hour_delay_mat[i][j] = float(self.seg_hour_delay_mat[i][j])/len(self.idx_list) + + def hourVsSegmentDelayMat(self, p = False): + + """ Constructs a segment vs hours matrix ( 54 x 12 ) which contains + Delay metrics """ + + mat_list = map(lambda x: x.hourly_delay_dict, self.idx_list) + mat_avg = self.avgOfMatrices(mat_list, len(Segments.all_segments), len(self._hours)) + + self.seg_hour_delay_mat = mat_avg # If pickle is true then we save the matrix in a picked file if p: pickle.dump(self.seg_hour_delay_mat, open('pickled/SegHourDelayMat.p','w')) - def hourVsSegmentTrafficMat(self, p = True): - """ Constructs a segment vs hours matrix ( 54 x 12 ) which contains + def hourVsZoneDelayMat(self, p = False): + + """ Constructs a zone vs hours matrix ( 6 x 12 ) which contains Delay metrics """ - self.seg_hour_traffic_mat = None + mat_list = map(lambda x: x.hourly_delay_dict_z, self.idx_list) + mat_avg = self.avgOfMatrices(mat_list, len(Segments.all_belts), len(self._hours)) - for idx in self.idx_list: + self.zone_hour_delay_mat = mat_avg - # Create a copy if still none - if not self.seg_hour_traffic_mat: - self.seg_hour_traffic_mat = copy.deepcopy(idx.hourly_traffic_dict) - continue + # If pickle is true then we save the matrix in a picked file + if p: + pickle.dump(self.zone_hour_delay_mat, open('pickled/ZoneHourDelayMat.p','w')) - # Update - for i in range(len(Segments.all_segments)): - for j in range(len(self._hours)): - self.seg_hour_traffic_mat[i][j] += idx.hourly_traffic_dict[i][j] - # Average - for i in range(len(Segments.all_segments)): - for j in range(len(self._hours)): - self.seg_hour_traffic_mat[i][j] = float(self.seg_hour_traffic_mat[i][j])/len(self.idx_list) + def hourVsSegmentTrafficMat(self, p = False): + + """ Constructs a segment vs hours matrix ( 54 x 12 ) which contains + Delay metrics """ + + mat_list = map(lambda x: x.hourly_traffic_dict, self.idx_list) + mat_avg = self.avgOfMatrices(mat_list, len(Segments.all_segments), len(self._hours)) + self.seg_hour_traffic_mat = mat_avg if p: pickle.dump(self.seg_hour_traffic_mat ,open('pickled/SegHourTrafficMat.p','w')) + def hourVsZoneTrafficMat(self, p = False): + """ Constructs a zone vs hours matrix ( 54 x 12 ) which contains + Traffic metrics """ + + mat_list = map(lambda x: x.hourly_traffic_dict_z, self.idx_list) + mat_avg = self.avgOfMatrices(mat_list, len(Segments.all_belts), len(self._hours)) + self.zone_hour_traffic_mat = mat_avg + + # If pickle is true then we save the matrix in a picked file + if p: + pickle.dump(self.zone_hour_traffic_mat, open('pickled/ZoneHourTrafficMat.p','w')) + def averageTrafficPS(self): - """ Total hourly traffic for each hour slot using the master matrix """ + """ Total hourly traffic for each segment using the master matrix """ self.total_average_traffic_ps = \ map(lambda x: sum(x.itervalues()), self.seg_hour_traffic_mat) if p: pickle.dump(file('SegHourDelayMat.p','w'), self.seg_hour_delay_mat) - def averageTrafficPHS(self): """ Total hourly traffic for each hour slot using the master matrix """ @@ -743,7 +811,7 @@ def averageTrafficPHS(self): self.total_average_traffic_phs.append(val) def averageDelayPS(self): - """ Total hourly traffic for each hour slot using the master matrix """ + """ Total hourly delay for each segment using the master matrix """ self.total_average_delay_ps = \ map(lambda x: sum(x.itervalues()), self.seg_hour_delay_mat)