-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
141 lines (106 loc) · 4.26 KB
/
test.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
import datetime
import json
import random
import pandas as pd
import osm2gmns as og
from tqdm import tqdm
import geopandas as gpd
from shapely import wkt
def gen_network_data():
net = og.getNetFromFile('./data/porto_edge1/porto_1.osm', default_lanes=True, default_speed=True, POI=True)
og.outputNetToCSV(net, './data/porto_edge1/road')
def generateID(lng, lat, col_num, row_num):
if lng != '':
lng = float(lng)
if lat != '':
lat = float(lat)
# beijing
lngMax = 116.5018476
lngMin = 116.2500662
latMax = 40.0006224
latMin = 39.7955236
# porto
# lngMax = -8.5472841
# lngMin = -8.69681086
# latMax = 41.19714406
# latMin = 41.126014
if lng < lngMin or lng > lngMax or lat < latMin or lat > latMax:
return -1
col = (lngMax - lngMin) / col_num
row = (latMax - latMin) / row_num
return int((lng - lngMin) / col) + 1 + int((lat - latMin) / row) * col_num
def generate_enhanced_dataset_beijing():
# time shift ( +1 and -1 ) (30%)
# trajectory shift (neighbor) (30%)
# trajectory delete (0.05 - 0.1) 20%
neb = {} # neighbor
df_rel = pd.read_csv('data/beijing/bj_roadmap_edge/bj_roadmap_edge.rel')
for j in tqdm(range(len(df_rel))):
origin_id = int(df_rel['origin_id'][j])
destination_id = int(df_rel['destination_id'][j])
neb.setdefault(origin_id, []).append(destination_id)
df = pd.read_csv('data/beijing/train_bj.csv')
ad = [1, -1]
for i in tqdm(range(len(df))):
trj = eval(df['path'][i])
tm = eval(df['tlist'][i])
if random.random() < 0.2:
# delete
len_del = int(len(trj) * random.uniform(0.05, 0.1))
if random.random() < 0.5:
trj = trj[len_del:]
tm = tm[len_del:]
else:
trj = trj[:-len_del]
tm = tm[:-len_del]
for j in range(len(trj)):
if random.random() < 0.3:
tm[j] += ad[random.randint(0, 1)]
if random.random() < 0.3:
if trj[j] in neb:
if neb[trj[j]]:
trj[j] = random.choice(neb[trj[j]])
df.at[i, 'path'] = str(trj)
df.at[i, 'tlist'] = str(tm)
df.to_csv('./data/beijing/train_bj_enhanced.csv', index=False)
def generate_enhanced_dataset_porto():
# time shift ( +1 and -1 ) (30%)
# trajectory shift (neighbor) (30%)
# trajectory delete (0.05 - 0.1) 20%
neb = {} # neighbor
# df_rel = pd.read_csv('data/porto_edge1/process_data/rn_porto/edges_with_neighbors.csv')
with open('data/porto_edge1/process_data/rn_porto/neighbor.json') as f:
neighbor = json.load(f)
with open('data/porto_edge1/process_data/rn_porto/edge2id.json') as f:
edge2id = json.load(f)
# for j in tqdm(range(len(df_rel))):
# origin_id = int(df_rel['origin_id'][j])
# destination_id = int(df_rel['destination_id'][j])
# neb.setdefault(origin_id, []).append(destination_id)
df = pd.read_csv('data/porto_edge1/process_data/train.csv')
ad = [1, -1]
for i in tqdm(range(len(df))):
trj = eval(df['rel_token'][i])
tm = eval(df['timestamp'][i])
if random.random() < 0.2:
# delete
len_del = int(len(trj) * random.uniform(0.05, 0.1))
if random.random() < 0.5:
trj = trj[len_del:]
tm = tm[len_del:]
else:
trj = trj[:-len_del]
tm = tm[:-len_del]
for j in range(len(trj)):
if random.random() < 0.3:
index = random.randint(0, 1)
tm[j] += ad[index]
if random.random() < 0.3:
if str(trj[j]) in neighbor and neighbor[str(trj[j])] != '-1':
tar = str(random.choice(eval(neighbor[str(trj[j])])))
if tar not in edge2id:
continue
trj[j] = edge2id[tar]
df.at[i, 'rel_token'] = str(trj)
df.at[i, 'timestamp'] = str(tm)
df.to_csv('./data/porto_edge1/process_data/train_porto_enhanced.csv', index=False)