def updatePath(self, node: PathNode): id = node.getPathID() if self.paths.__contains__(id): old = self.paths[id] old.setCount(node.getCount() + old.getCount()) self.paths[id] = old else: self.paths[id] = node
def contains(self, pathNode: PathNode): """returns true if the time node already contains the trip node passed""" # todo if pathNode.getPathID() in self.paths: return True else: return False
paths = {} '''read the next line in the file''' nl = outputTextFile1.readline().strip() # changed the output of the hashtable to be correct if nl.__contains__("Paths"): nl = outputTextFile1.readline().strip() while nl.__contains__("PathID"): '''get the PathNode attributes''' countIndex = nl.index("Count:") path = nl[8:countIndex - 1] count = nl[countIndex + 7:] '''create the pathNode''' pathNode = PathNode.PathNode(path) pathNode.setCount(int(count)) '''add the PathNode to the dict of PathNodes''' paths[path] = pathNode nl = outputTextFile1.readline().strip() vt = nl '''set the paths of the time node''' vtimenode.setPaths(paths) vt = nl '''add the time node to the list of times in od node''' vodnode.addTimeNode(vtimenode) if vt.__contains__("Index"):
def addPath(self, node: PathNode): """takes the passed trip node and adds it to the list of trips""" self.paths[node.getPathID()] = node
def findPath(self, pathNode: PathNode): """this method will return the node passed that is in the list of trips""" # todo return self.paths[pathNode.getPathID()]
def regionTimePeriodPathCountAnalysis(regions17: list, regions18: list, amLow: str, amHigh: str, pmLow: str, pmHigh: str): """ :param regions17: the 2017 regions :param regions18: the 2018 regions :param amLow: the start of the am period :param amHigh: the end of the am period :param pmLow: the start of the pm period :param pmHigh: the end of the pm period :return: a list of lists containing the origin, destination, time period (morning or afternoon), path id, z score, and p value """ # this is the list of lists to be returned masterList = [] masterList.append( ["origin", 'destination', 'period', 'path', 'zscore', 'pvalue']) size17 = 0 for i in regions17: size17 += i.getCount() size18 = 0 for i in regions18: size18 += i.getCount() # create a list of path counts for each year and each time period for a in regions17: for b in regions18: am17Paths = {} pm17Paths = {} am18Paths = {} pm18Paths = {} if a.origin == b.origin and a.destination == b.destination: rejectCount = 0 AMRejectCount = 0 PMRejectCount = 0 # get all the path counts from 17 times = a.getTimes() am = am17Paths pm = pm17Paths for t in times: timeNode = times[t] time = timeNode.getTimeID() paths = timeNode.getPaths() # if am if timeToInt(time) > timeToInt(amLow) and timeToInt( time) < timeToInt(amHigh): for p in paths: # if we have already seen this path if am.__contains__(p): masterPath = am[p] path = paths[p] masterPath.setCount(masterPath.getCount() + path.getCount()) # if we have not seen this path else: am[p] = paths[p] # if pm if timeToInt(time) > timeToInt(pmLow) and timeToInt( time) < timeToInt(pmHigh): for p in paths: # if we have already seen this path if pm.__contains__(p): masterPath = pm[p] path = paths[p] masterPath.setCount(masterPath.getCount() + path.getCount()) # if we have not seen this path else: pm[p] = paths[p] # get all the path counts from 18 times = b.getTimes() am = am18Paths pm = pm18Paths for t in times: timeNode = times[t] time = timeNode.getTimeID() paths = timeNode.getPaths() # if am if timeToInt(time) > timeToInt(amLow) and timeToInt( time) < timeToInt(amHigh): for p in paths: # if we have already seen this path if am.__contains__(p): masterPath = am[p] path = paths[p] masterPath.setCount(masterPath.getCount() + path.getCount()) # if we have not seen this path else: am[p] = paths[p] # if pm if timeToInt(time) > timeToInt(pmLow) and timeToInt( time) < timeToInt(pmHigh): for p in paths: # if we have already seen this path if pm.__contains__(p): masterPath = pm[p] path = paths[p] masterPath.setCount(masterPath.getCount() + path.getCount()) # if we have not seen this path else: pm[p] = paths[p] # make it so each path list contains all the same paths (am to am and pm to pm) # make it so each list of paths contains the same paths for e in am17Paths: if am18Paths.__contains__(e) is False: am18Paths[e] = PathNode.PathNode(e) for f in am18Paths: if am17Paths.__contains__(f) is False: am17Paths[f] = PathNode.PathNode(f) for e in pm17Paths: if pm18Paths.__contains__(e) is False: pm18Paths[e] = PathNode.PathNode(e) for f in pm18Paths: if pm17Paths.__contains__(f) is False: pm17Paths[f] = PathNode.PathNode(f) # am analysis for p in am17Paths: pathNode17 = am17Paths[p] pathNode18 = am18Paths[p] zscore, pvalue = two_proprotions_test( pathNode17.getCount(), a.getCount(), pathNode18.getCount(), b.getCount()) smallList = [ a.origin, a.destination, "AM", p, zscore, pvalue ] masterList.append(smallList) if pvalue <= 0.05: rejectCount += 1 AMRejectCount += 1 # pm analysis for p in pm17Paths: pathNode17 = pm17Paths[p] pathNode18 = pm18Paths[p] zscore, pvalue = two_proprotions_test( pathNode17.getCount(), a.getCount(), pathNode18.getCount(), b.getCount()) smallList = [ a.origin, a.destination, "PM", p, zscore, pvalue ] masterList.append(smallList) if pvalue <= 0.05: rejectCount += 1 PMRejectCount += 1 if pm17Paths.__len__() > 0 and am17Paths.__len__() > 0: print(amLow + "," + a.origin + "," + a.destination + "," + str(AMRejectCount / am17Paths.__len__()) + "," + str(PMRejectCount / pm17Paths.__len__())) return masterList
'''if this trip can be mapped to a path''' if a: '''if this path is already a part of the time nodes path''' if b: '''increment the count''' oldPaths = t.getPaths() newPaths = oldPaths newPaths[path].inc() t.setPaths(newPaths) elif b is False: '''if this path has not been seen yet''' oldPaths = t.getPaths() newPaths = oldPaths newPaths[path] = PathNode.PathNode(path) newPaths[path].inc() t.setPaths(newPaths) inputFile.close() # end the timer endTime = time.time() totalTime = float(endTime - startTime) # print progression statement if printProgramProgression: print(str(totalTime)[:5] + " seconds to analyze input file") # for performance testing purposes only if testPerformance is True:
def regionPathCountAnalysis(regions17: list, regions18: list): """ :param regions17: the 2017 regions :param regions18: the 2018 regions :return: a list of lists containing the origin, destination, path id, z score, and p value """ # this is the list of lists to be returned masterList = [] masterList.append(["origin", 'destination', 'path', 'zscore', 'pvalue']) size17 = 0 for i in regions17: size17 += i.getCount() size18 = 0 for i in regions18: size18 += i.getCount() for a in regions17: for b in regions18: rejectCount = 0 masterpaths17 = {} masterpaths18 = {} # if we are looking at the same region if a.origin == b.origin and a.destination == b.destination: times17 = a.getTimes() times18 = b.getTimes() for t in times17.values(): paths = t.getPaths() for p in paths: # if we have already seen this path if masterpaths17.__contains__(p): # update the path masterPathNode = masterpaths17[p] pathNode = paths[p] masterPathNode.setCount(masterPathNode.getCount() + pathNode.getCount()) # if we have not already seen this path else: masterpaths17[p] = paths[p] for t in times18.values(): paths = t.getPaths() for p in paths: # if we have already seen this path if masterpaths18.__contains__(p): # update the path masterPathNode = masterpaths18[p] pathNode = paths[p] masterPathNode.setCount(masterPathNode.getCount() + pathNode.getCount()) # if we have not seen the path else: masterpaths18[p] = paths[p] # make it so each list of paths contains the same paths for e in masterpaths17: if masterpaths18.__contains__(e) is False: masterpaths18[e] = PathNode.PathNode(e) for f in masterpaths18: if masterpaths17.__contains__(f) is False: masterpaths17[f] = PathNode.PathNode(f) for e in masterpaths17: pathNode17 = masterpaths17[e] pathNode18 = masterpaths18[e] zscore, pvalue = two_proprotions_test( pathNode17.getCount(), a.getCount(), pathNode18.getCount(), b.getCount()) smallList = [ a.origin, a.destination, pathNode17.getPathID(), zscore, pvalue ] if pvalue <= 0.05: rejectCount += 1 masterList.append(smallList) ''' if masterpaths18.__len__() > 0: print(a.origin + "," + a.destination + "," + str(rejectCount) + "," + str(masterpaths18.__len__()) + "," + str(rejectCount / masterpaths18.__len__())) ''' return masterList
def completePathCountAnalysis(regions17: list, regions18: list): """ :param regions17: the 2017 regions :param regions18: the 2018 regions :return: a list of lists containing the origin, destination, day of week, time of day, path id, z score, and p value """ # this is the list of lists to be returned masterList = [] masterList.append([ "origin", 'destination', 'weekday', 'time', 'path', 'zscore', 'pvalue' ]) size17 = 0 for i in regions17: size17 += i.getCount() size18 = 0 for i in regions18: size18 += i.getCount() for a in regions17: for b in regions18: # if we are looking at the same region if a.origin == b.origin and a.destination == b.destination: times17 = a.getTimes() times18 = b.getTimes() for c in times17.values(): for d in times18.values(): # if we are looking at the same time node if c.getWeekDay() == d.getWeekDay(): if c.getTimeID() == d.getTimeID(): paths17 = c.getPaths() paths18 = d.getPaths() # make it so each list of paths contains the same paths for e in paths17: if paths18.__contains__(e) is False: paths18[e] = PathNode.PathNode(e) for f in paths18: if paths17.__contains__(f) is False: paths17[f] = PathNode.PathNode(f) for e in paths17: pathNode17 = paths17[e] pathNode18 = paths18[e] zscore, pvalue = two_proprotions_test( pathNode17.getCount(), a.getCount(), pathNode18.getCount(), b.getCount()) smallList = [ a.origin, a.destination, c.getWeekDay(), c.getTimeID(), pathNode17.getPathID(), zscore, pvalue ] masterList.append(smallList) return masterList
def pathCountAnalysis(regions17: list, regions18: list): """ :param regions17: :param regions18: :return: """ masterList = [] masterList.append(["Path", "zscore", "pvalue"]) paths17 = {} pathCount17 = 0 paths18 = {} pathCount18 = 0 for r in regions17: times = r.getTimes() for t in times: paths = times[t].getPaths() for p in paths: pathCount17 += paths[p].getCount() if paths17.__contains__(p): master = paths17[p] other = paths[p] master.setCount(master.getCount() + other.getCount()) else: paths17[p] = paths[p] for r in regions18: times = r.getTimes() for t in times: paths = times[t].getPaths() for p in paths: pathCount18 += paths[p].getCount() if paths18.__contains__(p): master = paths18[p] other = paths[p] master.setCount(master.getCount() + other.getCount()) else: paths18[p] = paths[p] # make it so each list of paths contains the same paths for e in paths17: if paths18.__contains__(e) is False: paths18[e] = PathNode.PathNode(e) for f in paths18: if paths17.__contains__(f) is False: paths17[f] = PathNode.PathNode(f) for p in paths17: pathNode17 = paths17[p] pathNode18 = paths18[p] zscore, pvalue = two_proprotions_test(pathNode17.getCount(), pathCount17, pathNode18.getCount(), pathCount18) smallList = [p, zscore, pvalue] masterList.append(smallList) return masterList
def getODNodesFromFile(fileName: str, timeInterval: int): """ :param fileName: the name of the file to get the info :return: a list of all the od nodes the file contained """ '''list of od nodes''' ODNodes = [] with open(fileName, 'r') as inputfile17: for line in inputfile17: line = line.strip() '''get the general attributes from the outputfile''' if line.__contains__("Total Trips"): totalTripCount = int(line[13:]) if line.__contains__("Int Trips"): interestTripCount = int(line[13:]) if line.__contains__("OD Maps"): odMapCount = int(line[13:]) if line.__contains__("Time Maps"): timeMapCount = int(line[13:]) if line.__contains__("Path Maps"): pathMapCount = int(line[13:]) '''if we are looking at a new od node''' while line.__contains__("Index"): '''get the origin of the od node''' voriginLine = inputfile17.readline().strip() if voriginLine == '': break vorigin = voriginLine[8:] vcommaIndex = vorigin.index(",") voriginLat = vorigin[:vcommaIndex] voriginLong = vorigin[vcommaIndex + 1:] voriginC = Coordinate.Coordinate(float(voriginLat), float(voriginLong)) '''get the destination of the od node''' vdestLine = inputfile17.readline().strip() vdestination = vdestLine[13:] vcommaIndex = vdestination.index(",") vdestlat = vdestination[:vcommaIndex] vdestlong = vdestination[vcommaIndex + 1:] vdestC = Coordinate.Coordinate(float(vdestlat), float(vdestlong)) '''readout the direction''' direction = inputfile17.readline() '''get the count of the od node''' vcountLine = inputfile17.readline().strip() vcount = vcountLine[7:] '''get the am count of the od node''' vamCountLine = inputfile17.readline().strip() vamCount = vamCountLine[9:] '''get the pm count of the od node''' vpmCountLine = inputfile17.readline().strip() vpmCount = vpmCountLine[9:] '''create the od node''' vodnode = ODNode.ODNode(voriginC, vdestC, int(vcount)) '''update the period counts of the od node''' vodnode.setAmCount(int(vamCount)) vodnode.setPmCount(int(vpmCount)) '''set the direction of the node''' if direction.__contains__("In-"): vodnode.setInbound(True) else: vodnode.setInbound(False) '''readout the "Times: "''' readOut = inputfile17.readline().strip() '''update the times of the od node''' vt = inputfile17.readline().strip() '''for all the timenodes listed''' while vt.__contains__("WeekDay"): '''get the weekday, time, and count''' weekday = int(vt[9:11]) vtime = vt[19:24] vcount = int(vt[32:]) '''create the TimeNode''' vdt = datetime.strptime(vtime, "%H:%M") vtimenode = TimeNode.TimeNode(vdt, weekday, timeInterval) vtimenode.setCount(vcount) '''the dict to be used as the dict of paths for timenode''' paths = {} '''read the next line in the file''' nl = inputfile17.readline().strip() # changed the output of the hashtable to be correct if nl.__contains__("Paths"): nl = inputfile17.readline().strip() while nl.__contains__("PathID"): '''get the PathNode attributes''' countIndex = nl.index("Count:") path = nl[8:countIndex - 1] count = nl[countIndex + 7:] '''create the pathNode''' pathNode = PathNode.PathNode(path) pathNode.setCount(int(count)) '''add the PathNode to the dict of PathNodes''' paths[path] = pathNode nl = inputfile17.readline().strip() vt = nl '''set the paths of the time node''' vtimenode.setPaths(paths) vt = nl '''add the time node to the list of times in od node''' vodnode.addTimeNode(vtimenode) if vt.__contains__("Index"): line = vt '''add the od node to the list of od nodes''' '''only if it is not an empty node''' e = Coordinate.Coordinate(0, 0) empty = ODNode.ODNode(e, e, 0) if vodnode.__eq__(empty) is False: ODNodes.append(vodnode) inputfile17.close() return ODNodes
'''read the next line in the file''' nl = outputTextFile1.readline().strip() # changed the output of the hashtable to be correct if nl.__contains__("Paths"): nl = outputTextFile1.readline().strip() while nl.__contains__("PathID"): '''get the PathNode attributes''' countIndex = nl.index("Count:") path = nl[8:countIndex - 1] count = nl[countIndex + 7:] '''create the pathNode''' pathNode = PathNode.PathNode(path) pathNode.setCount(int(count)) '''add the PathNode to the dict of PathNodes''' paths[path] = pathNode nl = outputTextFile1.readline().strip() vt = nl '''set the paths of the time node''' vtimenode.setPaths(paths) vt = nl '''add the time node to the list of times in od node''' vodnode.addTimeNode(vtimenode)