Example #1
0
def main(options):
    readers = {}
    flowcols = options.flowcols.split(',')
    tMin = None
    tMax = None
    for flowcol in flowcols:
        detReader = detector.DetectorReader(options.detfile, LaneMap())
        tMin, tMax = detReader.findTimes(options.flowfile, tMin, tMax)
        hasData = detReader.readFlows(options.flowfile, flow=flowcol, time="Time", timeVal=0, timeMax=1440)
        if options.verbose:
            print("flowColumn: %s hasData: %s" % (flowcol, hasData))
        readers[flowcol] = detReader

    if options.verbose:
        print("found data from minute %s to %s" % (int(tMin), int(tMax)))

    beginM = int(sumolib.miscutils.parseTime(options.begin, 60) / 60)
    intervalM = int(sumolib.miscutils.parseTime(options.interval, 60) / 60)
    endM = min(int(sumolib.miscutils.parseTime(options.end, 60) / 60), tMax)

    with open(options.output, "w") as outf:
        root = "measurements" if options.cadyts else "data"
        sumolib.xml.writeHeader(outf)
        outf.write('<%s>\n' % root)
        while beginM <= endM:
            iEndM = beginM + intervalM
            edges = defaultdict(dict)  # edge : {attr:val}
            maxGroups = defaultdict(lambda: 0)  # edge : nGroups

            for flowcol in flowcols:
                detReader = detector.DetectorReader(options.detfile, LaneMap())
                detReader.readFlows(options.flowfile, flow=flowcol, time="Time", timeVal=beginM, timeMax=iEndM,
                                    addDetectors=(options.detfile is None))
                for edge, detData in detReader._edge2DetData.items():
                    maxFlow = 0
                    nGroups = 0
                    for group in detData:
                        if group.isValid:
                            maxFlow = max(maxFlow, group.totalFlow)
                            nGroups += 1
                    # if options.verbose:
                    #    print("flowColumn: %s edge: %s flow: %s groups: %s" % (
                    #        flowcol, edge, maxFlow, nGroups))
                    edges[edge][flowcol] = maxFlow
                    maxGroups[edge] = max(maxGroups[edge], nGroups)

            if options.cadyts:
                for edge in sorted(edges.keys()):
                    print('    <singlelink link="%s" start="%s" end="%s" value="%s" stddev="8" type="COUNT_VEH"/>' %
                          (edge, beginM * 60, iEndM * 60, sum(edges[edge].values())), file=outf)
            else:
                outf.write('    <interval id="flowdata" begin="%s" end="%s">\n' % (beginM * 60, iEndM * 60))
                for edge in sorted(edges.keys()):
                    attrs = ' '.join(['%s="%s"' % (k, v) for k, v in sorted(edges[edge].items())])
                    outf.write('        <edge id="%s" %s groups="%s"/>\n' % (edge, attrs, nGroups))
                outf.write('    </interval>\n')
            beginM += intervalM
        outf.write('</%s>\n' % root)
def main(options):
    readers = {}
    flowcols = options.flowcols.split(',')
    tMin = None
    tMax = None
    for flowcol in flowcols:
        detReader = detector.DetectorReader(options.detfile, LaneMap())
        tMin, tMax = detReader.findTimes(options.flowfile, tMin, tMax)
        hasData = detReader.readFlows(options.flowfile, flow=flowcol, time="Time", timeVal=0, timeMax=1440)
        if options.verbose:
            print("flowColumn: %s hasData: %s" % (flowcol, hasData))
        readers[flowcol] = detReader

    if options.verbose:
        print("found data from minute %s to %s" % (int(tMin), int(tMax)))

    beginM = int(sumolib.miscutils.parseTime(options.begin, 60) / 60)
    intervalM = int(sumolib.miscutils.parseTime(options.interval, 60) / 60)
    endM = min(int(sumolib.miscutils.parseTime(options.end, 60) / 60), tMax)

    with open(options.output, "w") as outf:
        sumolib.writeXMLHeader(outf, "$Id$")  # noqa
        outf.write('<data>\n')
        while beginM <= endM:
            iEndM = beginM + intervalM
            edges = defaultdict(dict)  # edge : {attr:val}
            maxGroups = defaultdict(lambda: 0)  # edge : nGroups

            for flowcol in flowcols:
                detReader = detector.DetectorReader(options.detfile, LaneMap())
                detReader.readFlows(options.flowfile, flow=flowcol, time="Time", timeVal=beginM, timeMax=iEndM)
                for edge, detData in detReader._edge2DetData.items():
                    maxFlow = 0
                    nGroups = 0
                    for group in detData:
                        if group.isValid:
                            maxFlow = max(maxFlow, group.totalFlow)
                            nGroups += 1
                    # if options.verbose:
                    #    print("flowColumn: %s edge: %s flow: %s groups: %s" % (
                    #        flowcol, edge, maxFlow, nGroups))
                    edges[edge][flowcol] = maxFlow
                    maxGroups[edge] = max(maxGroups[edge], nGroups)

            outf.write('    <interval id="flowdata" begin="%s" end="%s">\n' % (beginM * 60, iEndM * 60))
            for edge in sorted(edges.keys()):
                attrs = ' '.join(['%s="%s"' % (k, v) for k, v in edges[edge].items()])
                outf.write('        <edge id="%s" %s groups="%s"/>\n' % (edge, attrs, nGroups))
            outf.write('    </interval>\n')
            beginM += intervalM
        outf.write('</data>\n')
Example #3
0
 def __init__(self, detFile):
     self._routes = {}
     self._detReader = detector.DetectorReader(detFile, LaneMap())
     self._edgeFlow = {}
     self._parser = make_parser()
     self._parser.setContentHandler(self)
     self._begin = None
     self._end = None
Example #4
0
 def __init__(self, detFile):
     self._routeID = ''
     self._routeString = ''
     self._routes = {}
     self._detReader = detector.DetectorReader(detFile, LaneMap())
     self._edgeFlow = {}
     self._parser = make_parser()
     self._parser.setContentHandler(self)
Example #5
0
def main(options):
    if options.output is None:
        options.outfile = sys.stdout
    else:
        options.outfile = open(options.output, 'w')
    if options.flowout:
        with open(options.flowout, 'w') as f:
            f.write("Detector;Time;qPKW;vPKW\n")
        options.begin = None
        for interval in parse(options.edgeDataFile,
                              "interval",
                              attr_conversions={
                                  "begin": float,
                                  "end": float
                              }):
            if options.begin is None:
                options.begin = interval.begin / 60
            options.end = interval.end / 60

    detReader = detector.DetectorReader(options.detfile, LaneMap())
    time = options.begin * 60
    haveDetFlows = True
    while ((options.end is None and haveDetFlows)
           or (options.end is not None and time < options.end * 60)):
        intervalBeginM = time / 60
        intervalEndM = intervalBeginM + options.interval
        if options.end is not None:
            intervalEndM = min(intervalEndM, options.end)
        if options.flowfile:
            if options.verbose:
                print("Reading flows")
            haveDetFlows = detReader.readFlows(options.flowfile,
                                               flow=options.flowcol,
                                               time="Time",
                                               timeVal=intervalBeginM,
                                               timeMax=intervalEndM)
        if options.verbose:
            print("Reading edgeData")
        edgeFlow = readEdgeData(options.edgeDataFile, time, intervalEndM * 60,
                                detReader, options.flowout)
        if haveDetFlows and options.flowfile:
            printFlows(options, edgeFlow, detReader)
            calcStatistics(options, intervalBeginM, edgeFlow, detReader)
            detReader.clearFlows()
        time += options.interval * 60
    options.outfile.close()
Example #6
0
def main(options):
    readers = {}
    for flowcol in options.flowcols.split(','):
        detReader = detector.DetectorReader(options.detfile, LaneMap())
        hasData = detReader.readFlows(options.flowfile,
                                      flow=flowcol,
                                      time="Time",
                                      timeVal=0,
                                      timeMax=1440)
        if options.verbose:
            print("flowColumn: %s hasData: %s" % (flowcol, hasData))
        readers[flowcol] = detReader

    with open(options.output, "w") as outf:
        edges = defaultdict(dict)  # edge : {attr:val}
        maxGroups = defaultdict(lambda: 0)  # edge : nGroups

        for flowcol, detReader in readers.items():
            for edge, detData in detReader._edge2DetData.items():
                maxFlow = 0
                nGroups = 0
                for group in detData:
                    if group.isValid:
                        maxFlow = max(maxFlow, group.totalFlow)
                        nGroups += 1
                #if options.verbose:
                #    print("flowColumn: %s edge: %s flow: %s groups: %s" % (
                #        flowcol, edge, maxFlow, nGroups))
                edges[edge][flowcol] = maxFlow
                maxGroups[edge] = max(maxGroups[edge], nGroups)

        sumolib.writeXMLHeader(outf, "$Id$")  # noqa
        outf.write('<data>\n')
        outf.write('    <interval id="flowdata" begin="0" end="3600">\n')
        for edge in sorted(edges.keys()):
            attrs = ' '.join(
                ['%s="%s"' % (k, v) for k, v in edges[edge].items()])
            outf.write('        <edge id="%s" %s groups="%s"/>\n' %
                       (edge, attrs, nGroups))
        outf.write('    </interval>\n')
        outf.write('</data>\n')
Example #7
0
 def readDetectors(self, detFile):
     self._detReader = detector.DetectorReader(detFile, self._lane2edge)
     for edge, detGroups in self._detReader._edge2DetData.items():
         for group in detGroups:
             if group.isValid:
                 self._net.getEdge(edge).detGroup.append(group)
     sources = set()
     sinks = set()
     for det in sumolib.xml.parse(detFile, ["detectorDefinition", "e1Detector"]):
         if hasattr(det, "type"):
             if det.type == "source":
                 if options.lanebased:
                     sources.add(det.lane)
                 else:
                     sources.add(det.lane[:det.lane.rfind("_")])
             if det.type == "sink":
                 if options.lanebased:
                     sinks.add(det.lane)
                 else:
                     sinks.add(det.lane[:det.lane.rfind("_")])
     return sources, sinks
Example #8
0
def main(options):
    detReaders = [
        detector.DetectorReader(options.detfile, detector.LaneMap())
        for f in options.flowfiles
    ]
    for f in options.flowfiles:
        options.begin, options.end = detReaders[0].findTimes(
            f, options.begin, options.end)
    if options.verbose:
        print("begin=%s, end=%s" % (options.begin, options.end))
    for detReader, f in zip(detReaders, options.flowfiles):
        if options.verbose:
            print("reading %s" % f)
        detReader.clearFlows(options.begin, options.interval)  # initialize
        detReader.readFlowsTimeline(f,
                                    options.interval,
                                    begin=options.begin,
                                    end=options.end,
                                    flowCol=options.flowcol)
    # aggregated detectors
    if options.singleplot:
        plt.figure(figsize=(14, 9), dpi=100)
    if options.groupby == "all":
        allData = []  # one list for each file
        for detReader, f in zip(detReaders, options.flowfiles):
            data = initDataList(options.begin, options.end, options.interval)
            for edge, group in detReader.getGroups():
                if options.idfilter is not None and options.idfilter not in group.ids[
                        0]:
                    continue
                assert (len(group.timeline) <= len(data))
                for i, (flow, speed) in enumerate(group.timeline):
                    addToDataList(data, i, flow)
            allData.append(data)
        plot(options, allData)

    elif options.groupby == "type":
        for detType, linestyle in [("source", "-"), ("sink", ":"),
                                   ("between", "--")]:
            allData = []  # one list for each file
            for detReader, f in zip(detReaders, options.flowfiles):
                data = initDataList(options.begin, options.end,
                                    options.interval)
                for edge, group in detReader.getGroups():
                    if options.idfilter is not None and options.idfilter not in group.ids[
                            0]:
                        continue
                    assert (len(group.timeline) <= len(data))
                    if group.type == detType:
                        for i, (flow, speed) in enumerate(group.timeline):
                            addToDataList(data, i, flow)
                allData.append(data)
            plot(options, allData, detType, linestyle)

    elif options.groupby == "none":
        for det, groupName in [(g.ids[0], g.getName(options.longnames))
                               for e, g in detReaders[0].getGroups()]:
            allData = []  # one list for each file
            for detReader, f in zip(detReaders, options.flowfiles):
                data = initDataList(options.begin, options.end,
                                    options.interval)
                group = detReader.getGroup(det)
                if options.typefilter is not None and group.type != options.typefilter:
                    continue
                if options.idfilter is not None and options.idfilter not in group.ids[
                        0]:
                    continue
                assert (len(group.timeline) <= len(data))
                for i, (flow, speed) in enumerate(group.timeline):
                    addToDataList(data, i, flow)
                allData.append(data)
            plot(options, allData, "%s_%s" % (groupName, group.type))

    else:
        raise RuntimeError("group-by '%s' not supported" % options.groupby)

    if options.singleplot:
        plt.savefig("singlePlot." + options.extension)
    if options.show:
        plt.show()
Example #9
0
 def readDetectors(self, detFile):
     self._detReader = detector.DetectorReader(detFile, self._lane2edge)
     for edge, detGroups in self._detReader._edge2DetData.iteritems():
         for group in detGroups:
             if group.isValid:
                 self._net.getEdge(edge).detGroup.append(group)