Exemplo n.º 1
0
 def iterCountPeriods(self, timeStepInMin=None):
     """Return an iterator to the time periods the links has counts. A time 
     period is a tuple of (startTimeInMin, endTimeInMin)"""
     if not timeStepInMin:
         if self._obsCount:
             for start, end in  sorted(self._obsCount.iterkeys()):
                 yield (start, end)
         countPeriods = set()
         for movement in self.iterOutMovements():
             for countPeriod in movement.iterCountPeriods():
                 countPeriods.add(countPeriod)
         for start, end in iter(sorted(countPeriods)):
             yield (start, end)
     else:
         if self._obsCount:
             minTime = min(self._obsCount.keys())[0]
             maxTime = max(self._obsCount.keys())[1]
             for start, end in pairwise(range(minTime, maxTime + 1, timeStepInMin)):
                 yield (start, end)
         else:
             countPeriods = set()
             for movement in self.iterOutMovements():
                 for countPeriod in movement.iterCountPeriods():
                     countPeriods.add(countPeriod)
             minTime = min(countPeriods)[0]
             maxTime = max(countPeriods)[1]
             for start, end in pairwise(range(minTime, maxTime + 1, timeStepInMin)):
                 yield (start, end)
Exemplo n.º 2
0
    def getObsCount(self, startTimeInMin, endTimeInMin):
        """Return the observed number of vehicles executing the
        movement in the input time span
        Attention!
        If there are Zero vehicles the function returns None
        """

        self._validateInputTimes(startTimeInMin, endTimeInMin)
        self._checkOutputTimeStep(startTimeInMin, endTimeInMin)

        try:
            return self._obsCount[startTimeInMin, endTimeInMin]            
        except KeyError:
            result = 0 
            simTimeStep = self.simTimeStepInMin
            if not simTimeStep:
                raise SimError("To compute the count you need to set "
                                    "simulation time step")

            for i in range((endTimeInMin - startTimeInMin) / simTimeStep):
                multipleOfSimTimeStep = (i + 1) * simTimeStep
                result = 0

                for startTime, endTime in pairwise(range(startTimeInMin,
                                                     endTimeInMin + 1, multipleOfSimTimeStep)):
                    try:
                        result += self._obsCount[startTime, endTime]
                    except KeyError, e:
                        result = 0
                        break
                else:
                    return result
                    
            return result if result > 0 else None
Exemplo n.º 3
0
    def getObsCount(self, startTimeInMin, endTimeInMin):

        self._validateInputTimes(startTimeInMin, endTimeInMin)
        self._checkOutputTimeStep(startTimeInMin, endTimeInMin)

        allMovementCounts = self._aggregateAllMovementCounts(startTimeInMin, endTimeInMin)
        if allMovementCounts:
            return allMovementCounts
        else:
            result = 0
            simTimeStep = self.simTimeStepInMin
            if not simTimeStep:
                raise SimError("To return a count you need to set the simulation time step")

            if (startTimeInMin, endTimeInMin) in self._obsCount:
                return self._obsCount[startTimeInMin, endTimeInMin]

            for i in range((endTimeInMin - startTimeInMin) / simTimeStep):
                multipleOfSimTimeStep = (i + 1) * simTimeStep
                result = 0
                for startTime, endTime in pairwise(range(startTimeInMin, endTimeInMin + 1, multipleOfSimTimeStep)):
                    try:
                        result += self._obsCount[startTime, endTime]
                    except KeyError:
                        result = 0
                        break
                else:
                    return result

            return result if result > 0 else None
Exemplo n.º 4
0
    def readTimeVaryingMovementAttribute(self, fileName, attrName, startTimeInMin, endTimeInMin, timeStepInMin, hasHeader=True):
        """Reads a list of values from each line in the input fileName and assigns
        each value to the corresponding time interval specified
        by the last three input arguments

        the movement attribute can be accesse as mov.attrName[startInMin, endInMin]

        """

        assert self.simStartTimeInMin <= startTimeInMin < self.simEndTimeInMin
        assert self.simStartTimeInMin < endTimeInMin <= self.simEndTimeInMin
        assert timeStepInMin % self.simTimeStepInMin == 0

        self._createTimeVaryingMovementAttribute(attrName)

        inputStream = open(fileName, 'r')
        if hasHeader:
            inputStream.next()

        numTimeIntervals = len(range(startTimeInMin, endTimeInMin, timeStepInMin))

        for line in inputStream:

            fields = line.strip().split(",")
            nodeAid, nodeBid, nodeCid = fields[0].split()

            if len(fields[1:]) != numTimeIntervals:
                raise GraphError('The number of time intervals from %d to %d using time step %d'
                                ' are not the same as the number of columns in %s' %
                                (startTimeInMin, endTimeInMin, timeStepInMin, fileName))

            attrValue = {}
            i = 1
            allValuesAreZero = True
            for start, end in pairwise(range(startTimeInMin, endTimeInMin + 1, timeStepInMin)):
                singleValue = float(fields[i])
                if singleValue != 0:
                    allValuesAreZero = False
                attrValue[start, end] = singleValue
                i += 1

            if allValuesAreZero:
                continue

            try:
                edge = self.getEdge(nodeAid, nodeBid)
                movement = edge.getOutMovement(nodeCid)

                if movement.isUTurn():
                    continue
                
            except BaseNetworkError, e:
                if nodeCid == nodeAid:
                    continue
                logging.error(str(e))
                continue
            except DynameqError, e:
                logging.error(str(e))
                continue
Exemplo n.º 5
0
 def iterCountPeriods(self, timeStepInMin=None):
     """Return the periods as tuples of start end endTime (in min)
     for which there is a count"""
     if not timeStepInMin:
         for start, end in sorted(self._obsCount.keys()):
             yield (start, end)
     else:
         minTime = min(self._obsCount.keys())[0]
         maxTime = max(self._obsCount.keys())[1]
         for start, end in pairwise(range(minTime, maxTime + 1, timeStepInMin)):
             yield (start, end)
Exemplo n.º 6
0
    def getSimVolume(self, startTimeInMin, endTimeInMin):
        """GEt the simulated volume for the specified time
        period """

        self._validateInputTimes(startTimeInMin, endTimeInMin)
        self._checkOutputTimeStep(startTimeInMin, endTimeInMin)

        result = 0
        for stTime, enTime in pairwise(range(startTimeInMin, endTimeInMin + 1, 
                                             self.simTimeStepInMin)):
            result += self._simVolume[stTime, enTime]

        return result 
Exemplo n.º 7
0
    def getSimVolume(self, startTimeInMin, endTimeInMin):
        """Return the volume on the link from startTimeInMin to endTimeInMin"""

        self._validateInputTimes(startTimeInMin, endTimeInMin)
        self._checkOutputTimeStep(startTimeInMin, endTimeInMin)

        if self.getNumOutMovements() > 0:
            return sum([mov.getSimVolume(startTimeInMin, endTimeInMin) 
                        for mov in self.iterOutMovements()])
        else:
            result = 0
            for stTime, enTime in pairwise(range(startTimeInMin, endTimeInMin + 1, 
                                                 self.simTimeStepInMin)):
                result += self._simVolume[stTime, enTime]
            return result
Exemplo n.º 8
0
    def iterVehicles(self, startTimeInMin, endTimeInMin):
        """Return an iterator to all the vehicles that leave the edge in the 
        input time period"""
        self._validateInputTimes(startTimeInMin, endTimeInMin)
        self._checkOutputTimeStep(startTimeInMin, endTimeInMin)

        if self.getNumOutMovements() == 0:
            result = []
            for stTime, enTime in pairwise(range(startTimeInMin, endTimeInMin + 1, 
                                                 self.simTimeStepInMin)):
                result.append(self._vehicles[stTime, enTime])

            return chain(*result)
        else:            
            return chain(*[mov.iterVehicles(startTimeInMin, endTimeInMin) for mov in 
                         self.iterOutMovements()])
Exemplo n.º 9
0
    def readTimeVaryingEdgeAttribute(self, fileName, attrName, startTimeInMin, endTimeInMin, timeStepInMin, hasHeader=True):

        assert self.simStartTimeInMin <= startTimeInMin < self.simEndTimeInMin
        assert self.simStartTimeInMin < endTimeInMin <= self.simEndTimeInMin
        assert timeStepInMin % self.simTimeStepInMin == 0

        self._createTimeVaryingEdgeAttribute(attrName)

        numTimeIntervals = len(range(startTimeInMin, endTimeInMin, timeStepInMin))

        inputStream = open(fileName, 'r')
        if hasHeader:
            inputStream.next() # this is the header 
        
        for line in inputStream:
            fields = line.strip().split(",")
            nodeAid, nodeBid = fields[:2]

            try:
                edge = self.getEdge(nodeAid, nodeBid)
            except Graph, e:
                logging.error(str(e))
                continue

            attrValue = {}
            i = 2

            if len(fields[2:]) != numTimeIntervals:
                raise Graph('The number of time intervals from %d to %d using time step %d'
                                ' are not the same as the number of columns in %s' %
                                (startTimeInMin, endTimeInMin, timeStepInMin, fileName))

            for start, end in pairwise(range(startTimeInMin, endTimeInMin + 1, timeStepInMin)):
                attrValue[start, end] = float(fields[i])
                i += 1
            setattr(edge, attrName, attrValue)