예제 #1
0
    def RetrieveStatsForStop(stop_id, rtdirtuple):
        '''For a given stop, query the stoptimedict {stop_id: [[trip_id, stop_time]]}
        and return the NumTrips, NumTripsPerHr, MaxWaitTime, and AvgHeadway given a
        specific route_id and direction'''

        try:
            stoptimedict = stoptimedict_rtdirpair[rtdirtuple]
        except KeyError:
            # We will get a KeyError if there were no trips found for the route/direction
            # pair, which usually happens if the wrong SQL database was selected.
            stoptimedict = {}

        # Make a list of stop_times
        StopTimesAtThisPoint = []
        try:
            for trip in stoptimedict[stop_id]:
                StopTimesAtThisPoint.append(trip[1])
        except KeyError:
            pass
        StopTimesAtThisPoint.sort()

        # Calculate the number of trips
        NumTrips = len(StopTimesAtThisPoint)
        NumTripsPerHr = float(NumTrips) / TimeWindowLength

        # Get the max wait time and the average headway
        MaxWaitTime = BBB_SharedFunctions.CalculateMaxWaitTime(StopTimesAtThisPoint, start_sec, end_sec)
        AvgHeadway = BBB_SharedFunctions.CalculateAvgHeadway(StopTimesAtThisPoint)

        return NumTrips, NumTripsPerHr, MaxWaitTime, AvgHeadway
예제 #2
0
    def RetrieveFrequencyStatsForStop(stop_id, stoptimedict, start_sec,
                                      end_sec):
        '''For a given stop, query the dictionary
        and return the NumTrips, NumTripsPerHr, MaxWaitTime, and AvgHeadway given a
        specific route_id and direction. If snap to nearest five minutes is true, then
        this function will return headways snapped to the closest 5 minute interval.'''
        # Make a list of stop_times
        StopTimesAtThisPoint = []
        try:
            for trip in stoptimedict[stop_id]:
                StopTimesAtThisPoint.append(trip[1])
        except KeyError:
            pass
        StopTimesAtThisPoint.sort()

        # Calculate the number of trips
        NumTrips = len(StopTimesAtThisPoint)
        NumTripsPerHr = round(
            float(NumTrips) / ((end_sec - start_sec) / 3600), 2)
        # Get the max wait time and the average headway
        MaxWaitTime = BBB_SharedFunctions.CalculateMaxWaitTime(
            StopTimesAtThisPoint, start_sec, end_sec)
        if snap_to_nearest_5_minutes:
            round_to = 5
        else:
            round_to = None
        AvgHeadway = BBB_SharedFunctions.CalculateAvgHeadway(
            StopTimesAtThisPoint, round_to)
        return NumTrips, NumTripsPerHr, MaxWaitTime, AvgHeadway
예제 #3
0
    def RetrieveFrequencyStatsForStop(stop_id,
                                      rtdirtuple,
                                      snap_to_nearest_5_minutes=False):
        '''For a given stop, query the stop_time_dictionaries {stop_id: [[trip_id, stop_time]]}
        and return the NumTrips, NumTripsPerHr, MaxWaitTime, and AvgHeadway given a
        specific route_id and direction. If snap to nearest five minutes is true, then
        this function will return headways snapped to the closest 5 minute interval.'''
        try:
            stop_time_dictionaries = stoptimedict_rtedirpair[rtdirtuple]
        except KeyError:
            # We will get a KeyError if there were no trips found for the route/direction
            # pair, which usually happens if the wrong SQL database was selected.
            stop_time_dictionaries = {}

        # Make a list of stop_times
        StopTimesAtThisPoint = []
        try:
            for trip in stop_time_dictionaries[stop_id]:
                StopTimesAtThisPoint.append(trip[1])
        except KeyError:
            pass
        StopTimesAtThisPoint.sort()

        # Calculate the number of trips
        NumTrips = len(StopTimesAtThisPoint)
        NumTripsPerHr = float(NumTrips) / TimeWindowLength
        # Get the max wait time and the average headway
        MaxWaitTime = BBB_SharedFunctions.CalculateMaxWaitTime(
            StopTimesAtThisPoint, start_sec, end_sec)
        AvgHeadway = None
        if NumTrips > 1:
            AvgHeadway = max(
                1,
                int(
                    round(
                        float(
                            sum(
                                abs(x - y)
                                for (x, y) in zip(StopTimesAtThisPoint[1:],
                                                  StopTimesAtThisPoint[:-1])) /
                            (len(StopTimesAtThisPoint) - 1)) / 60,
                        0)))  # minutes
            if snap_to_nearest_5_minutes:
                AvgHeadway = round(AvgHeadway / 5.0) * 5
        return NumTrips, NumTripsPerHr, MaxWaitTime, AvgHeadway