def flow2speed_2p(inSpace, currentIter, FL, LIMIT): ''' This function takes detector flow and convert to detector level speed. Outputs to outCSV = inSpace+'detspd'+str(currentIter)+'-P.csv' This code contains suggestion from Gabriel on 12/15/2014, to take the supply speed simply as a smoothed supply speed and not weighted Demand+supply. ''' import sys import numpy as np from math import exp try: # READ FLOW (PEAK) cff_p = inSpace + "CSV/detflow" + str(currentIter) + "-P.csv" fdtype = [('idstn', 'i8'), ('flow', 'f8')] cff_p = readcsv(cff_p, fdtype, incol=2, sort=[0], header=None) # READ FLOW (Off PEAK) cff_op = inSpace + "CSV/detflow" + str(currentIter) + "-OP.csv" fdtype = [('idstn', 'i8'), ('flow', 'f8')] cff_op = readcsv(cff_op, fdtype, incol=2, sort=[0], header=None) numBack = 9 # How many iteration back to include in the exp. +1 = total number for exp # Simple weight for now avgwt = 1. / (np.arange(currentIter) + 1) # Truncate the weight to at most 10 avgwt = avgwt[0:numBack] avgwt = avgwt[::-1] print avgwt prior = np.arange(currentIter) prior = prior[-numBack:] # Read Previous Speeds (PEAK) print "prior:", prior a = np.zeros([ numBack, 1813 ]) # TODO: HARDCODED 1813 here. NEED FIX WHEN UPDATE TO FULL LA AREA j = 0 for i in prior: inPspd = inSpace + "detspdSY" + str(i) + "-P.csv" fdtype = [('idstn', 'i8'), ('speed', 'f8')] ps1 = readcsv(inPspd, fdtype, incol=2, sort=[0], header=True) print "a[", j, "]", "=", inPspd a[j] = ps1['speed'] j += 1 b = np.average(a[:j], axis=0) ps1['speed'] = np.copy(b) ps_p = np.copy(ps1) del a, b, ps1 # Read Previous Speeds (offPEAK) print "prior:", prior a = np.zeros([ numBack, 1813 ]) # TODO: HARDCODED 1813 here. NEED FIX WHEN UPDATE TO FULL LA AREA j = 0 for i in prior: inPspd = inSpace + "detspdSY" + str(i) + "-OP.csv" ps1 = readcsv(inPspd, fdtype, incol=2, sort=[0], header=True) print "a[", j, "]", "=", inPspd a[j] = ps1['speed'] j += 1 b = np.average(a[:j], axis=0) ps1['speed'] = np.copy(b) ps_op = np.copy(ps1) del a, b, ps1 #AF = Actual Daily TOTAL FLOW (average pre expo) inAF = inSpace + "AF.csv" fdtype = [('idstn', 'i8'), ('center', 'i8'), ('lane', 'i8'), ('flow', 'f8')] af = readcsv(inAF, fdtype, incol=4, sort=0, header=True) #Convert from GF to the peak time flow F (F = GF x 2 x 6%) #cff['flow'] = cff['flow']*0.12 #CHECK TO MAKE SURE THE ARRAYS MATCHES x1 = af['idstn'] x2 = cff_p['idstn'] xeq = x1 == x2 if xeq[xeq != True].size != 0: raise Exception( "AF and Current detector flow idstn DOES NOT MATCH!") #Calculate AF/GF ratio for center detectors np.seterr(divide='ignore') aft = af['center'] * af['flow'] aft = aft.sum() cft_p = af['center'] * cff_p['flow'] cft_p = cft_p.sum() cft_op = af['center'] * cff_op['flow'] cft_op = cft_op.sum() ratio = aft / (cft_p + cft_op) print "AF/GF Ratio:", ratio #SAVE DETFLOW WITH ADJUSTMENT #PEAK detFlow = np.copy(cff_p) detFlow['flow'] = detFlow['flow'] * ratio outCSVcff = inSpace + 'CSV/detflow_adj' + str(currentIter) + '-P.csv' np.savetxt(outCSVcff, detFlow, delimiter=',', fmt='%7.0f, %7.10f') #OffPEAK detFlow = np.copy(cff_op) detFlow['flow'] = detFlow['flow'] * ratio outCSVcff = inSpace + 'CSV/detflow_adj' + str(currentIter) + '-OP.csv' np.savetxt(outCSVcff, detFlow, delimiter=',', fmt='%7.0f, %7.10f') #Convert from GF to the peak time flow F = AF/GF*6%*GF # cff['flow'] = ratio*0.06*(cff['flow'])/af['lane'] # TODO: Ratio of 6% still ok??? cff_p['flow'] = ratio * 0.6 * (cff_p['flow']) / af['lane'] cff_op['flow'] = ratio * 0.6 * (cff_op['flow']) / af['lane'] #Update supply side speed using supply side equation #PEAK for x in np.nditer(cff_p['flow'], op_flags=['readwrite']): if x > FL: x[...] = LIMIT * exp(-0.000191 * (x - FL)) else: x[...] = LIMIT #OffPEAK for x in np.nditer(cff_op['flow'], op_flags=['readwrite']): if x > FL: x[...] = LIMIT * exp(-0.000191 * (x - FL)) else: x[...] = LIMIT cff_p.dtype = ([('idstn', '<i8'), ('speed', '<f8')]) cff_op.dtype = ([('idstn', '<i8'), ('speed', '<f8')]) #SAVE THE SUPPLY SIDE SPEED outCSV = inSpace + 'detspdSY' + str(currentIter) + '-P.csv' print "speed updated. Writing to", outCSV with open(outCSV, 'wb') as f: f.write(b'id_stn,speed\n') np.savetxt(f, cff_p, delimiter=',', fmt='%7.0f, %7.10f') outCSV = inSpace + 'detspdSY' + str(currentIter) + '-OP.csv' print "speed updated. Writing to", outCSV with open(outCSV, 'wb') as f: f.write(b'id_stn,speed\n') np.savetxt(f, cff_op, delimiter=',', fmt='%7.0f, %7.10f') #Set new supply side speed as linear combination of prev and current #supply side speed. Weight wt is set in the beginning. #Construct weight vector #HINT: PEAK weight = cff_p['speed'] - ps_p['speed'] weight = abs(weight) #FULL CONTROL: weight is 10% of absolute speed difference #Top code at 7.5mph #Old equation actually works without modification for x in np.nditer(weight, op_flags=['readwrite']): if x > 25: x[...] = 0.5 elif x > 0.25: x[...] = x**0.5 / 10 else: x[...] = 0 cff_p['speed'] = weight * cff_p['speed'] + (1 - weight) * ps_p['speed'] outCSV = inSpace + 'detspd' + str(currentIter) + '-P.csv' print "speed updated. Writing to", outCSV with open(outCSV, 'wb') as f: f.write(b'id_stn,speed\n') np.savetxt(f, cff_p, delimiter=',', fmt='%7.0f, %7.10f') #HINT: OffPEAK weight = cff_op['speed'] - ps_op['speed'] weight = abs(weight) for x in np.nditer(weight, op_flags=['readwrite']): if x > 25: x[...] = 0.5 elif x > 0.25: x[...] = x**0.5 / 10 else: x[...] = 0 cff_op['speed'] = weight * cff_op['speed'] + (1 - weight) * ps_op['speed'] outCSV = inSpace + 'detspd' + str(currentIter) + '-OP.csv' print "speed updated. Writing to", outCSV with open(outCSV, 'wb') as f: f.write(b'id_stn,speed\n') np.savetxt(f, cff_op, delimiter=',', fmt='%7.0f, %7.10f') except Exception as e: tb = sys.exc_info()[2] print "Exception in ModSpeedCalc" print "An error occurred in ModSpeedCalc line %i" % tb.tb_lineno print e.message
def alloc_trans(inSpace, inFlow, currentIter): ''' Base on alloc(), but changed the hard coded items for transit matrices ''' import numpy as np from ModIO import readcsv from math import sqrt inTT = inSpace+"CSV/TT.csv" inTD = inSpace+"CSV/TD.csv" inDT = inSpace+"CSV/DT.csv" outCSV = inSpace+'CSV/Transdetflow'+str(currentIter)+'.csv' #Test: 50 TAZs, 1813 DET #Full: 2241 TAZs, 1813 DETs nTAZ = 50 nDET = 1813 dttype = [('oid','i8'),('did','i8'),('name','S20'),('cost','f8')] tttype = [('oid','i8'),('did','i8'),('name','S20'),('cost','f8'),('length','f8'),('dps','f8'),('metro','f8'),('bus','f8')] fdtype = [('oid','i8'),('did','i8'),('postflow','f8')] print "importing various matrices" #Read TAZ-TAZ flow ff = readcsv(inFlow, fdtype, incol = 3, sort = [0,1], header = None) #Read TD cost td = readcsv(inTD, dttype, incol = 4, sort = [1,0], header = None) #Read DT cost dt = readcsv(inDT, dttype, incol = 4, sort = [0,1], header = None) #Read TT cost tt = readcsv(inTT, tttype, incol = 14, sort = [0,1], header = None) nTAZ = int(sqrt(tt.size)) if nTAZ != sqrt(tt.size): print "ERROR: TAZ SIZE NOT SQUARE!" if dt.size != td.size: print "ERROR: TD and DT SIZE NOT MATCH!" nDET = dt.size/nTAZ print "import completed, TAZ:", nTAZ, "DET:", nDET print "reshaping..." ctd = np.reshape(td['cost'],(nDET, nTAZ)) cdt = np.reshape(dt['cost'],(nDET, nTAZ)) ndt = np.reshape(dt['name'],(nDET, nTAZ)) ctt = np.reshape(tt['cost'],(nTAZ, nTAZ)) ftt = np.reshape(ff['postflow'],(nTAZ, nTAZ)) # FLOW MATRIX print "begin flow allocation" x3 = np.matrix(ctt) i = 0 count = 0 detFlow = {999999: 0} while i < nDET: #Extract current detector id_stn det = ndt[i,1][0:6] # Extract cost vectors x1 = np.matrix(ctd[i]) x1 = x1.T x2 = np.matrix(cdt[i]) tddt=x1+x2 #Precise matching isflow = x3 == tddt #Element multiply by flow flow = np.multiply(isflow, ftt) totalflow = np.nansum(flow) #print "Current iteration:", i if totalflow > 0: #print "stn", i, "flow:", totalflow count +=1 #Record flow in dictionary detFlow[det] = totalflow i+=1 del detFlow[999999] print "Number of detectors with match:", count dtype = [('idstn','i8'),('flow','f8')] detFlow= np.array(detFlow.items(), dtype=dtype) np.savetxt(outCSV, detFlow, delimiter=',', fmt='%7.0f, %7.10f') return detFlow
def update_step1(inSpace, currentIter, inTTp): ''' Output both TT flow for driving and transit ''' from math import exp, sqrt, pi import numpy as np from ModIO import readcsv #print "Code starts on ", time.strftime("%d/%m/%Y - %H:%M:%S") #Parameters c1 = 5.45 c2 = -5.05 G = exp(-3.289) beta1 = 0.535 beta2 = 0.589 tau = -2.077 #import various matrices fdtype = [('oid','i8'),('did','i8'),('flow','f8')] tttype = [('oid','i8'),('did','i8'),('name','S20'),('cost','f8')] petype = [('oid','i8'),('emp','f8'),('pop','f8')] areatype = [('oid','i8'),('area','f8')] #import from inFlow #This is used as OUTPUT TEMPLATE outFTT = inSpace + "inFlow.csv" outFTT = readcsv(outFTT, fdtype, incol = 3, sort = [0,1], header = None) #TT Cost (pub) #Post/PRE as SEPARATE CSV FILE print "importing TTcost for Transit" ttp = readcsv(inTTp, tttype, incol = 4, sort = [0,1], header = True) print "importing TTcost for driving (current)" #TT Cost (driving, CURRENT) inTTd = inSpace+"CSV/TT.csv" #inTTd = inSpace+"TTdrv.csv" ttd = readcsv(inTTd, tttype, incol = 4, sort = [0,1], header = None) #print "check sorting" #make sure both TT costs are sorted correctly: #if any(ttp['oid'] != ttd['oid']) or any(ttp['did'] != ttd['did']): #if any(ttp['oid'] != ttd['oid']) or any(ttp['did'] != ttd['did']): # raise Exception('Driving and Transit TT not match!') #print "sorting is fine" print "importing census" #Population & employment inPE = inSpace+"census.csv" pe = readcsv(inPE, petype, incol = 3, sort = [0], header = True) print "importing TAZ area" inArea = inSpace+"TAZarea.csv" area = readcsv(inArea, areatype, incol=2, sort=[0], header = True) area = area['area'] print "testing squareness" #Test square of TTcost and TTcostpub, test same size of pop/emp ttsize = sqrt(np.size(ttd)) if ttsize != int(ttsize): raise Exception('Driving TT cost not square!!!') ttsize = sqrt(np.size(ttp)) if ttsize != int(ttsize): raise Exception('Transit TT cost not square!!!') pesize = np.size(pe) if pesize != int(ttsize): raise Exception('Population/Employment vector not same size as TAZ!!!') nTAZ = ttsize print "square is fine, import completed" print "creating internal cost" #Internal cost: cost of ppl going to work within own TAZ # replace drvcost = 21.46*[(2/3)*(area_dest/_pi)^0.5]/15 # +3.752*[(2/3)*(area_dest/_pi)^0.5]/20 if oID_TAZ12A== dID_TAZ12A # replace with 1 if lower than 1 dist = (2./3) * (area/pi)**0.5 intci = vot * dist / 15 + 0.469 * dist intcp = vot * dist / 3 #make diagonal (Origin = destination) I = np.identity(nTAZ) intci = intci*I intcp = intcp*I print "reshaping..." ttcosti = np.reshape(ttd['cost'],(nTAZ, nTAZ)) + intci ttcostp = np.reshape(ttp['cost'],(nTAZ, nTAZ)) + intcp print "calculate Sij" #New transit share deltaC = ttcostp/ttcosti exx = np.exp(c1+c2*deltaC) sij = exx/(1+exx) outSij = sij.reshape(nTAZ**2,1) outCSVsij = inSpace+'CSV/Sij'+str(currentIter)+'.csv' print "Writing transit share to", outCSVsij with open(outCSVsij, 'wb') as f: np.savetxt(f, outSij, delimiter=',', fmt='%7.10f') print "population and employment" #Gravity prediction pop = pe['pop'] ** beta1 pop = np.matrix(pop) emp = pe['emp'] ** beta2 emp = np.matrix(emp) print "final matrix calculation" FTT = G * np.array(pop.T * emp) * (sij*ttcostp + (1-sij)*ttcosti)**tau pFTT= FTT * (1-sij) pFTT= pFTT.reshape(1,nTAZ**2) tFTT= FTT * (sij) tFTT= tFTT.reshape(1,nTAZ**2) FTT = np.reshape(FTT,(1,nTAZ**2)) outFTT['flow'] = pFTT outCSV = inSpace+'CSV/TTflow'+str(currentIter)+'.csv' print "Writing output to", outCSV with open(outCSV, 'wb') as f: np.savetxt(f, outFTT, delimiter=',', fmt='%7.0f, %7.0f, %7.10f') outFTT['flow'] = tFTT outCSV = inSpace+'CSV/TransTTflow'+str(currentIter)+'.csv' print "Writing output to", outCSV with open(outCSV, 'wb') as f: np.savetxt(f, outFTT, delimiter=',', fmt='%7.0f, %7.0f, %7.10f') outFTT['flow'] = FTT outCSV = inSpace+'CSV/TotalTTflow'+str(currentIter)+'.csv' print "Writing output to", outCSV with open(outCSV, 'wb') as f: np.savetxt(f, outFTT, delimiter=',', fmt='%7.0f, %7.0f, %7.10f')
# # cff.dtype = ([('idstn', '<i8'), ('speed', '<f8')]) # b=np.copy(cff) if __name__ == "__main__": import numpy as np from math import exp inSpace = "C:\Users\Dennis\Desktop\Results\Pre_1219\Test/" currentIter = 12 FL = 500 LIMIT = 600 # READ FLOW (PEAK) cff_p = inSpace + "CSV/detflow" + str(currentIter) + "-P.csv" fdtype = [('idstn', 'i8'), ('flow', 'f8')] cff_p = readcsv(cff_p, fdtype, incol=2, sort=[0], header=None) # READ FLOW (Off PEAK) cff_op = inSpace + "CSV/detflow" + str(currentIter) + "-OP.csv" fdtype = [('idstn', 'i8'), ('flow', 'f8')] cff_op = readcsv(cff_op, fdtype, incol=2, sort=[0], header=None) numBack = 9 # How many iteration back to include in the exp. +1 = total number for exp # Simple weight for now # avgwt = 1./(np.arange(currentIter)+1) # # # Truncate the weight to at most 10 # avgwt = avgwt[0:numBack] # avgwt = avgwt[::-1] # # print avgwt
if __name__ == '__main__': # Want Link objective, not just detectors. # i.e. minimize the aggregate cost travelling on the highway network's links # Need : link distance, cost = (11.5375 * dist / inSpd + 0.469 * dist) inSpace = "/Users/SWOT/Desktop/TestBisection/" currentIter = 10 ratio = 10 FL = 500 LIMIT = 65 import numpy as np cff_op = inSpace + "detflow_adj" + str(currentIter) + ".csv" fdtype = [('idstn', 'i8'), ('flow', 'f8')] x1 = readcsv(cff_op, fdtype, incol=2, sort=[0], header=None) cff_op = inSpace + "detflow_adj" + str(currentIter + 1) + ".csv" fdtype = [('idstn', 'i8'), ('flow', 'f8')] y1 = readcsv(cff_op, fdtype, incol=2, sort=[0], header=None) inAF = inSpace + "AF.csv" fdtype = [('idstn', 'i8'), ('center', 'i8'), ('lane', 'i8'), ('flow', 'f8')] af = readcsv(inAF, fdtype, incol=4, sort=0, header=True) # Make sure the idstn align checksum = x1['idstn'] != y1['idstn'] print 'number of detectors out of order:', checksum.sum() if checksum.sum() > 0: print 'OH SHIT'
def dta_1p(inSpace, inTTflow, fl, limit): ''' This script runs the DTA loop. This script also contains the travelcost subfunction Inputs ------ inSpace : str Path inTTflow : str Full path to TT flow of current iteration fl : int flow per lane limit : int speed limit ''' import numpy as np # XXX Begin sub function block def bisection(a, b, tol, x, y, vd): ''' Bisection method to find root. hardcoded in objective function as flowGradient, and value function as travelcost. Input ----- a : value min search value b : value max search value tol : value tolerance x,y : vector flow vectors (prev, predicted) vd : vector distance vector from AF Return ------ Alpha of root. ''' def f(alpha): return flowGradient(x, y, alpha, vd) c = (a+b)/2.0 while (b-a)/2.0 > tol: if f(c) == 0: print 'fc=0' return c elif f(a)*f(c) < 0: b = c else : a = c c = (a+b)/2.0 return c def flowGradient(x, y, alpha, vdist): ''' Calculate the gradient value based on value function and various inputs (y1-x1)*t(x1+a*(y1-x1)) Contains a short, no-output version of flow2spd, and travelcost calculation Input ----- x : vector Prev flow y : vector Expected flow alpha : value Alpha value for move. See Sheffi p. 119. Supplied from bisection vdist : vector vector of distance from AF reading Output ------ Grad(Z) : Value Value of the gradient, for use in bisection method ''' def flow2spd(x, FL, LIMIT): # given x as PER LANE flow, FL, LIMIT, returns the resulting speed. from math import exp if x > FL: return LIMIT * exp(-0.000191*(x-FL)) else: return LIMIT def travelcost(x, dist): # segment length to be implimented vflow2spd = np.vectorize(flow2spd, otypes=[np.float]) travelcost = (11.5375 *dist / vflow2spd(x, fl, limit)) + 0.469 * dist travelcost = (1/vflow2spd(x, fl, limit))**4 travelcost = 65**.2 - vflow2spd(x, fl, limit)**.2 return travelcost grad = (y-x)*travelcost(x+alpha*(y-x), vdist) return grad.sum() # XXX end sub function block try: print "bisection starts on ", time.strftime("%H:%M:%S") # Create temp speed x1path = "CSV/xdetflow.csv" outCSV = "detspd-temp.csv" ratio_x = ModSpeedCalc.flow2speed(inSpace, x1path, fl, limit, outCSV, dta=True) #detspd-temp.csv # Perform all or nothing allocation print "DTA scratch version" ModSetupWorker.clearOld(base,tempP) print "GIS operation" inSpeed = "detspd-temp.csv" ModGIS.GISops_dta(inSpace, inGdb, fcTAZ, fcDet, inSpeed) # TT, DT, TD print "allocation" y1path = "CSV/ydetflow.csv" # inFlow is global ModAlloc.alloc(inSpace, inFlow, inSpace+y1path, period = "P") # Gives detflow y1 ratio_y = ModSpeedCalc.flow2speed(inSpace, y1path, fl, limit, outCSV="", dta=True) # ratio_x = 10.0550935465 # ratio_y = 10.7308926556 # Find Alpha fdtype = [('idstn','i8'),('flow','f8')] x1 = readcsv(inSpace+x1path, fdtype, incol = 2, sort = [0], header = None) y1 = readcsv(inSpace+y1path, fdtype, incol = 2, sort = [0], header = None) inAF = inSpace+"AF.csv" fdtype = [('idstn','i8'),('center','i8'),('lane','i8'),('flow','f8'),('dist','f8')] af = readcsv(inAF, fdtype, incol = 5, sort=0, header = True) checksum = x1['idstn'] != y1['idstn'] print 'number of detectors out of order:', checksum.sum() if checksum.sum() > 0: raise('OH NOES!!, XY not match') checksum = x1['idstn'] != af['idstn'] print 'number of detectors out of order:', checksum.sum() if checksum.sum() > 0: print 'OH NOES!!, AF not match' vx1 = ratio_x * .06 * x1['flow'] / af['lane'] vy1 = ratio_y * .06 * y1['flow'] / af['lane'] vd = af['dist'] alfa = bisection(0, 1, 0.0001, vx1, vy1, vd) print "bisection result:", alfa x1back = x1path[:-4]+str(alfa)+x1path[-4:] y1back = y1path[:-4]+str(alfa)+y1path[-4:] shutil.copy(inSpace+x1path,inSpace+x1back) shutil.copy(inSpace+y1path,inSpace+y1back) # Create det flow x2 x2flow = x1['flow']+alfa*(y1['flow']-x1['flow']) x2 = np.copy(x1) x2['flow'] = x2flow outX2 = inSpace+'CSV/xdetflow.csv' np.savetxt(outX2, x2, delimiter=',', fmt='%7.0f, %7.10f') print "bisection ends on ", time.strftime("%H:%M:%S"), "found alpha =", alfa return alfa except Exception as e: # If an error occurred, print line number and error message import sys tb = sys.exc_info()[2] print "An error occurred in dta_1p line %i" % tb.tb_lineno print str(e)
outCSV = inSpace + 'ODthrough' + detdesired + '.csv' inFlow = inSpace + "CSV/TTflow" + str(currentIter) + ".csv" #Test: 50 TAZs, 1813 DET #Full: 2241 TAZs, 1813 DETs nTAZ = 50 nDET = 1813 #fields = ["OriginID", "DestinationID", "Name", "Total_Cost"] datatype = [('oid', 'i8'), ('did', 'i8'), ('name', 'S20'), ('cost', 'f8')] #fdtype = [('oid','i8'),('did','i8'),('preflow','f8'),('postflow','f8')] fdtype = [('oid', 'i8'), ('did', 'i8'), ('postflow', 'f8')] print "importing various matrices" #Read TAZ-TAZ flow ff = readcsv(inFlow, fdtype, incol=3, sort=[0, 1], header=None) #Read TD cost td = readcsv(inTD, datatype, incol=4, sort=[1, 0], header=None) #Read DT cost dt = readcsv(inDT, datatype, incol=4, sort=[0, 1], header=None) #Read TT cost tt = readcsv(inTT, datatype, incol=4, sort=[0, 1], header=None) nTAZ = int(sqrt(tt.size)) if nTAZ != sqrt(tt.size): print "ERROR: TAZ SIZE NOT SQUARE!" if dt.size != td.size: print "ERROR: TD and DT SIZE NOT MATCH!"
if __name__ == '__main__': import ModSpeedCalc from ModIO import readcsv import os maxa = 1. inSpace = "C:\Users\Dennis\Desktop\Results\DTA_Logit_test\Post/" # Change inSpace to where the current py file is located. # Should also have AF and xdetflow, ydetflow files. # inSpace = str(os.path.dirname(__file__)+'/') x1path = "xdetflow0.186218261719.csv" y1path = "ydetflow0.186218261719.csv" fdtype = [('idstn', 'i8'), ('flow', 'f8')] x1 = readcsv(inSpace + x1path, fdtype, incol=2, sort=[0], header=None) y1 = readcsv(inSpace + y1path, fdtype, incol=2, sort=[0], header=None) inAF = inSpace + "AF.csv" fdtype = [('idstn', 'i8'), ('center', 'i8'), ('lane', 'i8'), ('flow', 'f8'), ('dist', 'f8')] af = readcsv(inAF, fdtype, incol=5, sort=0, header=True) ratio_x = ModSpeedCalc.flow2speed(inSpace, x1path, fl, limit, outCSV="", dta=True) #detspd-temp.csv ratio_y = ModSpeedCalc.flow2speed(inSpace, y1path,
def flow2speed(inSpace, inFlow, FL, LIMIT, outCSV, dta): ''' Given flow vector, calculate speed vector. Returns ratio at the end. input: ------ inSpace : str Working path inFlow : str Name of DETECTOR flow vector, ending in CSV, in program inFlow = inSpace+inFlow FL : value Flow / lane cutoff LIMIT : value Freeflow speed / speed limit (65) outCSV : str outCSV should be without path in inSpace outCSV = inSpace+outCSV outCSV = inSpace+'detspd-temp.csv' outCSV = inSpace+'detspd'+str(currentIter)+'.csv' , IF EMPTY, return ratio dta : bool if False, output detflow_adj in CSV folder output: ------- detspd-temp.csv if dta; if not dta detspdi.csv, detflow_adji.csv ''' import sys import numpy as np try: # read AF for ratio calculation inAF = inSpace + "AF.csv" fdtype = [('idstn', 'i8'), ('center', 'i8'), ('lane', 'i8'), ('flow', 'f8'), ('dist', 'f8')] af = readcsv(inAF, fdtype, incol=5, sort=0, header=True) # read flow inFlow = inSpace + inFlow fdtype = [('idstn', 'i8'), ('flow', 'f8')] cflow = readcsv(inFlow, fdtype, incol=2, sort=0, header=None) # CHECK TO MAKE SURE THE ARRAYS MATCHES x1 = af['idstn'] x2 = cflow['idstn'] xeq = x1 != x2 if xeq.sum() > 0: raise Exception( "AF and Current detector flow idstn DOES NOT MATCH!") # Calculate AF/GF ratio for center detectors aft = af['center'] * af['flow'] cft = af['center'] * cflow['flow'] ratio = aft.sum() / cft.sum() print "AF/GF Ratio:", ratio if outCSV == "": return ratio if not dta: #SAVE DETFLOW WITH ADJUSTMENT if not a DTA round detFlow = np.copy(cflow) detFlow['flow'] = detFlow['flow'] * ratio outCSVcff = inSpace + 'CSV/detflow_adj' + outCSV[6] + '.csv' np.savetxt(outCSVcff, detFlow, delimiter=',', fmt='%7.0f, %7.10f') #Convert from GF to the peak time flow F = AF/GF*6%*GF thisflow = ratio * .06 * cflow['flow'] / af['lane'] vfeedback = np.vectorize(congestion_feedback, otypes=[np.float]) yflow = vfeedback(thisflow, FL, LIMIT) cflow1 = np.copy(cflow) cflow1['flow'] = yflow cflow1.dtype = ([('idstn', '<i8'), ('speed', '<f8')]) # Save temp speed if DTA round, save regular speed if not outCSV = inSpace + outCSV print "speed updated. Writing to", outCSV with open(outCSV, 'wb') as f: f.write(b'id_stn,speed\n') np.savetxt(f, cflow1, delimiter=',', fmt='%7.0f, %7.10f') return ratio except Exception as e: tb = sys.exc_info()[2] print "Exception in ModSpeedCalc" print "An error occurred in ModSpeedCalc.flow2speed line %i" % tb.tb_lineno print e.message