-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNYCGenerateDataset.py
More file actions
131 lines (117 loc) · 6.6 KB
/
NYCGenerateDataset.py
File metadata and controls
131 lines (117 loc) · 6.6 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pandas as pd
import math
from datetime import datetime,timedelta
import GenerateDatasets as GD
import numpy as np;
import networkx as nx
#NYC borders locations
MaxLongitude = -73.69
MinLongitude = -74.27
MaxLatitude = 40.93
MinLatitude = 40.48
#if i=j then returns node attributes
#if i!=j then returns an edge between two nodes
def FindNodeAttribute(Dataframe,i,j,LatRange,LongRange):
#find the location that we want to find the needed information of
LatcountPickup=int(i/int(math.sqrt(NodeNum)))
longcountPickup=i%(int(math.sqrt(NodeNum)))
LatcountDropoff=int(j/int(math.sqrt(NodeNum)))
longcountDropoff=j%(int(math.sqrt(NodeNum)))
try:
#find the samples that have trips between the two selected regions
NewDF=Dataframe[(Dataframe['pickup_latitude']>=((LatcountPickup*LatRange)+MinLatitude))& \
(Dataframe['pickup_latitude']<((LatcountPickup+1)*LatRange)+MinLatitude) & \
(Dataframe['pickup_longitude']>=((longcountPickup*LongRange)+MinLongitude))& \
(Dataframe['pickup_longitude']<(((longcountPickup+1)*LongRange)+MinLongitude)) & \
(Dataframe['dropoff_latitude']>=((LatcountDropoff*LatRange)+MinLatitude))& \
(Dataframe['dropoff_latitude']<((LatcountDropoff+1)*LatRange)+MinLatitude) & \
(Dataframe['dropoff_longitude']>=((longcountDropoff*LongRange)+MinLongitude))& \
(Dataframe['dropoff_longitude']<(((longcountDropoff+1)*LongRange)+MinLongitude))]
except Exception as e:
print(e)
#find the summation of the trips' times
return sum(np.array(NewDF['dropoff_datetime']-NewDF['pickup_datetime']))
def Str2Datetime(Input):
return datetime.strptime(Input, "%Y-%m-%d %H:%M:%S")
NodeNum=9
#generate dataset
dataset = GD.Dataset()
for iterat in range(1,13):
#read the trips file
df=pd.read_csv("dataset/trip_data_"+str(iterat)+".csv")
df.columns = df.columns.str.replace(' ', '')
print("Data loaded!\n")
#separate the trips with different vendorsId( that is the label here)
#eliminate trips with errors, they should be in a range of NYC borders:
CMTdf=df[df['vendor_id']=="CMT"][((df[df['vendor_id']=="CMT"]['pickup_longitude'])>-74.27) \
& ((df[df['vendor_id']=="CMT"]['pickup_longitude'])<-73.69) \
& ((df[df['vendor_id']=="CMT"]['pickup_latitude'])>40.48) \
& ((df[df['vendor_id']=="CMT"]['pickup_latitude'])<40.93) \
& ((df[df['vendor_id']=="CMT"]['dropoff_longitude'])>-74.27) \
& ((df[df['vendor_id']=="CMT"]['dropoff_longitude'])<-73.69) \
& ((df[df['vendor_id']=="CMT"]['dropoff_latitude'])>40.48) \
& ((df[df['vendor_id']=="CMT"]['dropoff_latitude'])<40.93)]
VTSdf=df[df['vendor_id']=="VTS"][((df[df['vendor_id']=="VTS"]['pickup_longitude'])>-74.27) \
& ((df[df['vendor_id']=="VTS"]['pickup_longitude'])<-73.69) \
& ((df[df['vendor_id']=="VTS"]['pickup_latitude'])>40.48) \
& ((df[df['vendor_id']=="VTS"]['pickup_latitude'])<40.93) \
& ((df[df['vendor_id']=="VTS"]['dropoff_longitude'])>-74.27) \
& ((df[df['vendor_id']=="VTS"]['dropoff_longitude'])<-73.69) \
& ((df[df['vendor_id']=="VTS"]['dropoff_latitude'])>40.48) \
& ((df[df['vendor_id']=="VTS"]['dropoff_latitude'])<40.93)]
#extract dates to have samples base on them
CMTdf['pickup_datetime']=CMTdf['pickup_datetime'].apply(Str2Datetime)
CMTdf['dropoff_datetime']=CMTdf['dropoff_datetime'].apply(Str2Datetime)
i=min(CMTdf['pickup_datetime'])
samples = []
OneDay=timedelta(days=1)
#Generate each sample as the trips of one day:
while i<(max(CMTdf['pickup_datetime'])-OneDay):
samples.append(CMTdf[(CMTdf['pickup_datetime']>=i) & (CMTdf['pickup_datetime']<i+OneDay)])
i+=OneDay
VTSdf['pickup_datetime']=VTSdf['pickup_datetime'].apply(Str2Datetime)
VTSdf['dropoff_datetime']=VTSdf['dropoff_datetime'].apply(Str2Datetime)
i=min(VTSdf['pickup_datetime'])
while i<(max(VTSdf['pickup_datetime'])-OneDay):
samples.append(VTSdf[(VTSdf['pickup_datetime']>=i) & (VTSdf['pickup_datetime']<i+OneDay)])
i+=OneDay
#each grid longitude and latitude length:
LongRange=(MaxLongitude-MinLongitude)/int(math.sqrt(NodeNum))
LatRange=(MaxLatitude-MinLatitude)/int(math.sqrt(NodeNum))
print("About to start building the dataset")
for item in samples:
if item.empty:
continue
try:
G = nx.Graph()
[G.add_node(it) for it in range(1, NodeNum + 1)] ###
#FindNodeAttribute finds the total time of the trips from i to j
attributes=[FindNodeAttribute(item,i,i,LatRange,LongRange) for i in range(NodeNum)]
#convert nanosecond to seconds, also have attributes as np.array for ease of use
attributes=np.array([float(it)/(1e+9) for it in attributes])
except Exception as e:
print(e)
try:
for node1 in range(NodeNum):
for node2 in range(NodeNum):
if node2==node1:
continue
try:
#Add edge between i and j if the total time of the trips from i to j is greater than zero
if ((FindNodeAttribute(item, node1, node2, LatRange, LongRange))>0):
G.add_edge(node1+1, node2+1, weight=1)
G.add_edge(node2+1, node1+1, weight=1)
except Exception as e:
print(e)
except Exception as e:
print(e)
try:
#add the generated sample to the dataset
dataset.add(G, attributes, 0 if (np.array(item.head(1)['vendor_id'])[0]=='CMT') else 1)
except Exception as e:
print(e)
testsize = int(dataset.getDatasetSize() * (1/6))
trainingDataset, testDataset = dataset.getTrainTest(testsize)
print("writing to file:")
GD.writeDataset(trainingDataset, "real-datasets/NYC-training-"+str(NodeNum)+"5-1-new.pkl" )
GD.writeDataset(testDataset, "real-datasets/NYC-testing-"+str(NodeNum)+"5-1-new.pkl" )