-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy patht_drive_small.py
400 lines (347 loc) · 12.8 KB
/
t_drive_small.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# link: https://www.microsoft.com/en-us/research/publication/t-drive-trajectory-data-sample/
import json
import pandas as pd
import os
from datetime import datetime
import numpy as np
import time
# total number of points:15 million
# the total distance of the trajectories: 9 million km
# average sampling interval: 177 seconds with a distance of about 623 meters.
# taxi_id, date_time, longitude, latitude
# 北京左上角(39.83°, 116.25°),右下角(40.12°, 116.64°),划分成了32 * 32的网格。
TAXI_NUM = 10357
LAT_MIN = 39.83
LAT_MAX = 40.12
LON_MIN = 116.25
LON_MAX = 116.64
old_time_format = '%Y-%m-%d %H:%M:%S'
new_time_format = '%Y-%m-%dT%H:%M:%SZ'
MIN_TIME = '2008-02-02 00:00:00'
MAX_TIME = '2008-02-09 00:00:00'
MIN_TIMESTAMP = float(
datetime.timestamp(
pd.to_datetime(MIN_TIME, utc=True, format=old_time_format)))
MAX_TIMESTAMP = float(
datetime.timestamp(
pd.to_datetime(MAX_TIME, utc=True, format=old_time_format)))
def judge_id(value, dividing_points, equally=True):
if equally:
min_v = dividing_points[0]
interval = dividing_points[1] - dividing_points[0]
idx = int((value - min_v) / interval)
max_id = len(dividing_points) - 2
return min(max_id, idx)
else:
for i, num in enumerate(dividing_points):
if value <= num:
return i - 1
return len(dividing_points)
def partition_to_grid(data_set, row_num, col_num):
"""
:param data_set: ['taxi_id', 'date_time', 'longitude', 'latitude']
:param row_num: # of rows
:param col_num: # of columns
:return: data_set_with_rc_id:
['taxi id', 'date_time', 'row_id', 'column_id']
:return: geo_data['geo_id', 'type', 'coordinates', 'row_id', 'column_id']
"""
# handle row/latitude
lat_diff = LAT_MAX - LAT_MIN
lat_dividing_points = \
[round(LAT_MIN + lat_diff / row_num * i, 3)
for i in range(row_num + 1)]
# print(len(lat_dividing_points))
data_set['row_id'] = data_set.apply(
lambda x: judge_id(x['latitude'], lat_dividing_points),
axis=1
)
# handle col/longitude
lon_diff = LON_MAX - LON_MIN
lon_dividing_points = \
[round(LON_MIN + lon_diff / col_num * i, 3)
for i in range(col_num + 1)]
data_set['column_id'] = data_set.apply(
lambda x: judge_id(x['longitude'], lon_dividing_points),
axis=1
)
# generate gird data (.geo)
geo_data = pd.DataFrame(
columns=['geo_id', 'type', 'coordinates', 'row_id', 'column_id'])
for i in range(row_num):
for j in range(col_num):
index = i * col_num + j
coordinates = [[
[lon_dividing_points[j], lat_dividing_points[i]],
[lon_dividing_points[j + 1], lat_dividing_points[i]],
[lon_dividing_points[j + 1], lat_dividing_points[i + 1]],
[lon_dividing_points[j], lat_dividing_points[i + 1]],
[lon_dividing_points[j], lat_dividing_points[i]]
]] # list of list of [lon, lat]
geo_data.loc[index] = [index, 'Polygon', coordinates, i, j]
return data_set[['taxi_id', 'date_time', 'row_id', 'column_id']], geo_data
def convert_time(df):
df['time'] = df.apply(
lambda x: x['date_time'].replace(' ', 'T') + 'Z',
axis=1)
df['timestamp'] = df.apply(
lambda x: float(datetime.timestamp(
pd.to_datetime(x['date_time'],
utc=True,
format=old_time_format))),
axis=1)
return df
def convert_to_trajectory(df):
"""
:param df: ['taxi id', 'date time', 'row_id', 'column_id']
:return: df: ['taxi id', 'time', 'row_id', 'column_id', 'timestamp']
"""
trajectory_data = convert_time(df)
return trajectory_data[
['taxi_id', 'time', 'row_id', 'column_id', 'timestamp']]
def add_previous_rc_id(tra_by_bike):
tra_by_bike = tra_by_bike.sort_values(by='time')
# tra_by_bike['prev_row_id'].astype("int")
# tra_by_bike['prev_column_id'].astype("int")
tra_by_bike['prev_row_id'] = tra_by_bike['row_id'].shift(1)
tra_by_bike['prev_column_id'] = tra_by_bike['column_id'].shift(1)
return tra_by_bike[1:]
def judge_time_id(df, time_dividing_point):
df['time_id'] = df.apply(
lambda x: judge_id(x['timestamp'], time_dividing_point),
axis=1
)
return df
def gen_flow_data1(trajectory, time_dividing_point):
"""
:param trajectory:
:param time_dividing_point:
:return: ['time', 'row_id', 'column_id', 'inflow', 'outflow']
"""
trajectory = trajectory[
(trajectory.prev_row_id != trajectory.row_id) |
(trajectory.prev_column_id != trajectory.column_id)]
tra_groups = trajectory.groupby(by='time_id')
for tra_group in tra_groups:
tra_group = tra_group[1]
# print(tra_group)
t = time_dividing_point[tra_group.iloc[0].loc['time_id']]
flow_in = tra_group.groupby(
by=[
'row_id',
'column_id']
)[['taxi_id']].count().sort_index()
flow_in.columns = ['inflow']
flow_out = tra_group.groupby(
by=[
'prev_row_id',
'prev_column_id']
)[['taxi_id']].count().sort_index()
flow_out.index.names = ['row_id', 'column_id']
flow_out.columns = ['outflow']
flow = flow_in.join(flow_out, how='outer', on=['row_id', 'column_id'])
flow = flow.reset_index()
# flow['time'] = util.timestamp_to_str(t)
# print(t)
flow['time'] = timestamp2str(t)
# print(timestamp2str(t))
yield flow
def timestamp2str(timestamp):
return pd.to_datetime(timestamp, unit='s').strftime(new_time_format)
def fill_empty_flow(flow_data, time_dividing_point, row_num, col_num):
# 主要通过生成一个全数据的data frame 与flow_data合并实现
row_ids = list(range(0, row_num))
col_ids = list(range(0, col_num))
time_ids = list(map(timestamp2str, time_dividing_point))
ids = [(x, y, z) for x in row_ids for y in col_ids for z in time_ids]
flow_keep = pd.DataFrame(ids, columns=['row_id', 'column_id', 'time'])
flow_keep = pd.merge(flow_keep, flow_data, how='outer')
flow_keep = flow_keep.fillna(value={'inflow': 0, 'outflow': 0})
return flow_keep
def calculate_flow(
trajectory_data, row_num, col_num, interval):
"""
:param trajectory_data:
['taxi_id', 'time', 'row_id', 'column_id', 'timestamp']
:param row_num
:param col_num
:param interval
:return: ['time', 'row_id', 'column_id', 'inflow', 'outflow']
"""
# 对taxi_id进行group
taxi_trajectory = trajectory_data.groupby(by='taxi_id')
# print(taxi_trajectory)
########################################
# 对taxi_trajectory添加上一个地点的区域:prev_col_id,prev_row_id
taxi_trajectory = pd.concat(
map(lambda x: add_previous_rc_id(x[1]), taxi_trajectory))
# 对新生成列的类型进行转换
taxi_trajectory['prev_row_id'] = \
taxi_trajectory['prev_row_id'].astype("int64")
taxi_trajectory['prev_column_id'] = \
taxi_trajectory['prev_column_id'].astype("int64")
# 若起点和终点位于一块,则drop这一行
taxi_trajectory = taxi_trajectory[
~((taxi_trajectory['row_id'] == taxi_trajectory['prev_row_id']) & (
taxi_trajectory['column_id'] ==
taxi_trajectory['prev_column_id']))]
# 时间戳的最小最大值,以interval为颗粒度。
min_timestamp = MIN_TIMESTAMP
max_timestamp = MAX_TIMESTAMP
time_dividing_point = \
list(np.arange(min_timestamp, max_timestamp, interval))
# print(time_dividing_point)
# 为taxi_trajectory加上time_id
taxi_trajectory = judge_time_id(taxi_trajectory, time_dividing_point)
# taxi_trajectory.to_csv('with_time.csv')
# 接下来需要根据taxi_trajectory和time_dividing_point数组统计出入流量
flow_data_part = gen_flow_data1(taxi_trajectory, time_dividing_point)
# print("type of data part:" + str(type(flow_data_part)))
flow_data = pd.concat(flow_data_part)
# ,row_id,column_id,inflow,outflow,time
# flow_data.to_csv('flow1.csv')
flow_data = fill_empty_flow(
flow_data, time_dividing_point, row_num, col_num)
# flow_data.to_csv('output/NYC_BIKE_flow_test/NYC_BIKE_flow_fill_empty.csv')
flow_data['type'] = 'state'
flow_data = flow_data.reset_index(drop=True)
flow_data['dyna_id'] = flow_data.index
flow_data = flow_data[
['dyna_id', 'type', 'time', 'row_id', 'column_id', 'inflow', 'outflow']
]
return flow_data
def t_drive_flow(
output_dir, output_name, data_set, row_num, col_num, interval=3600):
data_name = output_dir + "/" + output_name
# 1. calculate row_id and column_id for data_set and
# generate geo_data(.geo)
data_set_with_rc_id, geo_data = \
partition_to_grid(data_set, row_num, col_num)
geo_data.to_csv(data_name + '.geo', index=False)
print('finish geo')
# trajectory data, add timestamp
trajectory_data = convert_to_trajectory(data_set_with_rc_id)
# trajectory_data.to_csv('trajectory_data.geo', index=False)
print('finish trajectory')
# flow data
flow_data = calculate_flow(
trajectory_data,
row_num,
col_num,
interval=interval
)
flow_data.to_csv(data_name + '.grid', index=False)
print('finish flow')
def gen_config_geo():
geo = {"including_types": [
"Polygon"
],
"Polygon": {
"row_id": "num",
"column_id": "num"
}
}
return geo
def gen_config_grid(row_num, column_num):
grid = {
"including_types": [
"state"
],
"state": {
"row_id": row_num,
"column_id": column_num,
"inflow": "num",
"outflow": "num"
}
}
return grid
def gen_config_info(file_name, interval):
info = \
{
"data_col": [
"inflow",
"outflow"
],
"data_files": [
file_name
],
"geo_file": file_name,
"output_dim": 2,
"init_weight_inf_or_zero": "inf",
"set_weight_link_or_dist": "dist",
"calculate_weight_adj": False,
"weight_adj_epsilon": 0.1,
"time_intervals": interval,
}
return info
def gen_config(output_dir_flow, file_name, row_num, column_num, interval):
config = {}
data = json.loads(json.dumps(config))
data["geo"] = gen_config_geo()
data["grid"] = gen_config_grid(row_num, column_num)
data["info"] = gen_config_info(file_name, interval)
config = json.dumps(data)
with open(output_dir_flow + "/config.json", "w") as f:
json.dump(data, f, ensure_ascii=False, indent=1)
# print(config)
if __name__ == '__main__':
start_time = time.time()
# 参数
# 测试时选取的taxi数量
test_taxi_num = TAXI_NUM
# test_taxi_num = 500
# 时间间隔
interval = 3600
# 开始年月
(start_year, start_month, start_day) = (2008, 2, 2)
# 结束年月
(end_year, end_month, end_day) = (2008, 2, 8)
# 行数
row_num = 32
# 列数
column_num = 32
# 输出文件名称
file_name = 'T_DRIVE_SMALL'
# 输出文件夹名称
output_dir_flow = 'output/T_DRIVE_SMALL'
# 输入文件夹名称
input_dir_flow = 'input/T-Drive'
data_url = [
input_dir_flow + '/' + str(i + 1) + '.txt'
for i in range(test_taxi_num)]
print(data_url)
# 创建输出文件夹
if not os.path.exists(output_dir_flow):
os.makedirs(output_dir_flow)
# 对空文件进行过滤
data_urls = []
for i in data_url:
if os.path.getsize(i) != 0:
data_urls.append(i)
# 读入文件并实现拼接
data_set_t = pd.concat(
map(lambda x: pd.read_csv(x, header=None), data_urls), axis=0
) # 纵向拼接数据
data_set_t.reset_index(drop=True, inplace=True)
data_set_t.columns = ['taxi_id', 'date_time', 'longitude', 'latitude']
# 过滤超出北京范围的数据
data_set_t = data_set_t.loc[
data_set_t['longitude'].apply(lambda x:(LON_MIN <= x <= LON_MAX))]
data_set_t = data_set_t.loc[
data_set_t['latitude'].apply(lambda x: (LAT_MIN <= x <= LAT_MAX))]
print('finish read csv')
# data_set_t.to_csv('concat.csv', line_terminator='\n')
# 调用处理函数,生成.grid 和.geo文件
t_drive_flow(
output_dir_flow,
file_name,
data_set_t,
row_num,
column_num,
interval=interval
)
# 生成config.json文件
gen_config(output_dir_flow, file_name, row_num, column_num, interval)
print('finish config')
end_time = time.time()
print(end_time - start_time)