Skip to content

Commit 92084c1

Browse files
authored
initial files
1 parent bb60fd1 commit 92084c1

6 files changed

+1608
-0
lines changed

Instance_Creation_sequence1.py

+387
Large diffs are not rendered by default.
+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import numpy as np
2+
import pickle
3+
from datetime import datetime
4+
import os
5+
6+
# Change the current working directory to the location of 'Combined Trajectory_Label_Geolife' folder.
7+
os.chdir(r'/home/ubuntu/Transport-Mode-GPS-CNN/data/Combined_Trajectory_Label_Geolife')
8+
9+
# create 'daysDate' function to convert start and end time to a number of days
10+
11+
12+
def days_date(time_str):
13+
date_format = "%Y/%m/%d %H:%M:%S"
14+
current = datetime.strptime(time_str, date_format)
15+
date_format = "%Y/%m/%d"
16+
bench = datetime.strptime('1899/12/30', date_format)
17+
no_days = current - bench
18+
delta_time_days = no_days.days + current.hour / 24.0 + current.minute / (24. * 60.) + current.second / (24. * 3600.)
19+
return delta_time_days
20+
21+
# Change Mode Name to Mode index
22+
Mode_Index = {"walk": 0, "run": 9, "bike": 1, "bus": 2, "car": 3, "taxi": 3, "subway": 4, "railway": 4,
23+
"train": 4, "motocycle": 8, "boat": 9, "airplane": 9, "other": 9}
24+
25+
## 0: walk
26+
## 1: bike
27+
## 2: bus
28+
## 3: car, taxi
29+
## 4: subway, railway, train
30+
## 8: motocycle
31+
## 9: run, boat, airplane, other
32+
33+
# Ground modes are the modes that we use in this paper.
34+
Ground_Mode = ['walk', 'bike', 'bus', 'car', 'taxi', 'subway', 'railway', 'train']
35+
## Ground_Mode = [0, 1, 2, 3, 3, 4, 4, 4]
36+
37+
# Trajectory_Array and Label_Array are the final lists which each of its element is for one user
38+
Trajectory_Array = []
39+
Label_Array = []
40+
Trajectory_Label_Array = []
41+
UserNon = range(190) ## 0-179, might not be enough
42+
43+
# 1
44+
for k in UserNon:
45+
## will be the same
46+
#for k in range(len(UserNon)):
47+
48+
InputFile = "combined" + str(k) + ".plt"
49+
##InputFile = "combined" + str(UserNon[k]) + ".plt"
50+
table = []
51+
try:
52+
## with open(InputFile, 'r') as inp:
53+
with open(InputFile, 'rb') as inp:
54+
for row in inp:
55+
row = row.rstrip()
56+
row = row.decode("utf-8")
57+
row = row.split(',')
58+
if len(row) == 7:
59+
table.append(row)
60+
except IOError:
61+
continue
62+
#print(table)
63+
64+
# TrajectoryMatrix = contains lat, long, date in each column
65+
table_array = np.array(table, dtype=object)
66+
#print (table_array.shape)
67+
# for 2d array, axis = -1 is equal to axis = 1
68+
TrajectoryMatrix = np.stack((table_array[:, 0], table_array[:, 1], table_array[:, 4]), axis=-1)
69+
#print( TrajectoryMatrix.shape)
70+
for i in range(len(table_array[:, 0])):
71+
for j in range(3):
72+
TrajectoryMatrix[i, j] = float(TrajectoryMatrix[i, j])
73+
74+
Trajectory_Array.append(TrajectoryMatrix)
75+
76+
77+
# end1
78+
# 2.Modify the labels file and create array with start_time, end_time in days and labels
79+
InputFile = "labels" + str(k) + ".txt"
80+
table = []
81+
with open(InputFile, 'rb') as inp:
82+
for row in inp:
83+
row = row.rstrip()
84+
row = row.decode("utf-8")
85+
row = row.split('\t')
86+
if len(row) == 3:
87+
table.append(row)
88+
89+
LabelFile = np.array(table, dtype=object)
90+
91+
92+
# StartTime and EndTime in days after 1899/12/30 for each data point in labels.cv
93+
# Modify label for those rows that don't have any time and labels
94+
# LabelMatrix = the array that has Start time(days), End time(days), and labels
95+
96+
StartTime = []
97+
EndTime = []
98+
label = []
99+
Error = []
100+
101+
for i in range(len(LabelFile[:, 0])):
102+
try:
103+
if LabelFile[i, 2] in Ground_Mode:
104+
StartTime.append(days_date(LabelFile[i, 0]))
105+
EndTime.append(days_date(LabelFile[i, 1]))
106+
label.append(Mode_Index[LabelFile[i, 2]])
107+
except ValueError:
108+
Error.append(i)
109+
110+
LabelMatrix = (np.vstack((StartTime, EndTime, label))).T
111+
Label_Array.append(LabelMatrix)
112+
113+
# End2
114+
115+
116+
117+
# 3.Assign the labels to the trajectories
118+
# Trajectory = zip(lat, long, date)
119+
Dates = np.split(TrajectoryMatrix, 3, axis=-1)[2]
120+
Sec = 1 / (24.0 * 3600.0)
121+
# C_list: all the rows in the TrajectoryMatrix that should be picked up
122+
C_list = []
123+
# Mode_Trajectory: all labels
124+
Mode_Trajectory = []
125+
for index, row in enumerate(LabelMatrix):
126+
A = np.where(Dates >= (float(row[0]) - Sec))
127+
B = np.where(Dates <= (float(row[1]) + Sec))
128+
C = list(set(A[0]).intersection(B[0]))
129+
if len(C) == 0:
130+
print("error")
131+
[Mode_Trajectory.append(row[2]) for i in C]
132+
[C_list.append(i) for i in C]
133+
134+
TrajectoryMatrix = [TrajectoryMatrix[i, :] for i in C_list]
135+
TrajectoryMatrix = np.array(TrajectoryMatrix)
136+
Mode_Trajectory = np.array(Mode_Trajectory)
137+
Trajectory_Label = (np.vstack((TrajectoryMatrix.T, Mode_Trajectory))).T
138+
139+
Trajectory_Label_Array.append(Trajectory_Label)
140+
141+
# End3
142+
143+
##for i in Trajectory_Label_Array:
144+
## Shape = np.shape(i)
145+
## print(Shape, "+++")
146+
147+
# Save Trajectory_Array and Label_Array for all users
148+
with open("/home/ubuntu/Transport-Mode-GPS-CNN/data/1_Trajectory_Label_Array.pickle", 'wb') as f: # Python 3: open(..., 'wb')
149+
pickle.dump(Trajectory_Label_Array, f)

0 commit comments

Comments
 (0)