forked from msr-ds3/subway-flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutFlows.py
105 lines (81 loc) · 2.93 KB
/
OutFlows.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#Author : Eiman Ahmed
import sys
import fileinput
import networkx as nx
import matplotlib.pyplot as plt
import csv
#change this directory to wherever you located the TrainTravel.csv file
openingfile = open("/home/ewahmed/subway-flow/SingularTrainFlow.csv")
traindata = openingfile.readlines()
openingfile.close()
#initializing the lists(features) we are going to need to graph with
#You can also use a dictionary to store in the following format - {"TrainName",[FromStation,ToStation,TravelTime]}
trains=[]
stations=[]
traveltime=[]
traindata.pop(0)
#Extracting data into select initalized lists ^
for line in traindata:
traintravel = line.rstrip('\n').split(',')
trains.append(traintravel[1])
stations.append(traintravel[4])
traveltime.append(int(traintravel[5]))
#initializing a graph to represent the connections on
G= nx.DiGraph()
#Connecting all stations with one another on the graph
length = xrange(1, len(trains))
for i in length:
if(trains[i-1]==trains[i]):
G.add_edge(stations[i-1],stations[i],weight=traveltime[i])
G.add_edge(stations[i],stations[i-1],weight=traveltime[i])
#nx.draw_spring(G, with_labels=True, node_color='w', node_size=300, font_size=6)
#plt.show()
files = ['f_latenight.csv','f_morning.csv','f_noon2.csv','f_evening.csv','f_night.csv','f_latemorning.csv','f_am.csv','f_pm.csv','f_allday.csv']
for name in files:
openingfile = open("/home/ewahmed/subway-flow/PrePres/"+name)
noondata = openingfile.readlines()
openingfile.close()
name= name[2:len(name)-4]
#Extracting data into select initalized lists ^
noondata.pop(0)
total = 0
for line in noondata:
_, _, station, exits, entries,stationid = line.rstrip('\n').split(',')
G.node[station]["entries"] = int(entries)
G.node[station]["exits"] = int(exits)
G.node[station]["demand"]=int(exits)-int(entries)
G.node[station]["stationid"]=stationid
total += int(exits) - int(entries)
for n in G.nodes():
if "demand" not in G.node[n]:
G.remove_node(n)
turnstile_stations = [record.strip().split(',')[2] for record in noondata]
gtfs_stations = G.nodes()
#print set(turnstile_stations) - set(gtfs_stations)
extra_nodes = set(gtfs_stations) - set(turnstile_stations)
nx.draw_spring(G, with_labels=True, node_color='w', node_size=350, font_size=7)
#plt.show()
flow = nx.min_cost_flow(G)
inflowstation=[]
outflow=[]
for x in G.nodes():
total = 0
inflowstation.append(x)
for p in G.successors(x):
total = total+ flow[p][x]
total = total + G.node[x]["demand"]
outflow.append(int(total))
print type(outflow[2])
length= range(0,len(inflowstation))
# for i in length:
# total = outflow[i]
# adding = 0
# for x in flow[inflowstation[i]]:
# adding = flow[inflowstation[i][x]]
# total = total + int(adding)
rows = zip(inflowstation,outflow)
out= open("/home/ewahmed/subway-flow/PrePres/OutFlows/" + name+"outflows.csv", "wb")
out.write('\n')
for i in length:
out.write(inflowstation[i] +',' + str(outflow[i]) + '\n')
out.close()