-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic.py
323 lines (263 loc) · 11.8 KB
/
traffic.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
#%%
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('dataset', help='Which dataset to choose', choices=['la','bay','air'])
parser.add_argument('algorithm', help='Which algorithm to use', choices=['gabriel','kmeans','optics','knn_weighted','knn_unweighted','minmax','relative_neighborhood','gaussian','dtw','mic','correlation'])
parser.add_argument('gnn_model', help='Which model to choose', choices=['timethenspace','dcrnn'])
args = parser.parse_args()
print(args.dataset)
print(args.algorithm)
print(args.gnn_model)
print()
#%%
import torch
import pandas as pd
from pytorch_lightning.loggers import TensorBoardLogger
import datetime as dt
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
from omegaconf import DictConfig
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping
from pytorch_lightning.loggers import TensorBoardLogger, WandbLogger
from tsl.nn.blocks.encoders import RNN
from tsl.nn.blocks.decoders import GCNDecoder
from all_models import adj_to_edge_index
from typing import Optional
from einops import rearrange
from torch import nn, Tensor
from torch_geometric.typing import Adj, OptTensor
from tsl.nn.blocks.decoders.mlp_decoder import MLPDecoder
from tsl.nn.blocks.encoders import ConditionalBlock
from tsl.nn.blocks.encoders.dcrnn import DCRNN
from tsl.ops.connectivity import edge_index_to_adj
import datetime
def print_time():
parser = datetime.datetime.now()
return parser.strftime("%d-%m-%Y %H:%M:%S")
from all_models import *
from tsl.metrics.torch import MaskedMAE, MaskedMAPE, MaskedMSE, MaskedMSE
from tsl import logger
from tsl.data import SpatioTemporalDataset, SpatioTemporalDataModule
from tsl.data.preprocessing import StandardScaler
from tsl.datasets import MetrLA, PemsBay, AirQuality
from tsl.datasets.pems_benchmarks import PeMS03, PeMS04, PeMS07, PeMS08
from tsl.experiment import Experiment
from tsl.engines import Predictor
from tsl.metrics import torch as torch_metrics, numpy as numpy_metrics
from tsl.nn import models
from tsl.utils.casting import torch_to_numpy
from tsl.datasets.pems_benchmarks import PeMS03, PeMS04, PeMS07, PeMS08
import pandas as pd
import networkx as nx
from typing import Optional, Tuple, Union, List
from tsl.typing import TensArray, OptTensArray, SparseTensArray, DataArray, ScipySparseMatrix
from types import ModuleType
from torch_sparse import SparseTensor, fill_diag
import torch_sparse
from torch import Tensor
import numpy as np
import random
from geopy.distance import geodesic
import os
from geoconnector.graph_maker import graph_maker_function
from geoconnector.newest_graph_maker import graph_generator
from pytorch_lightning import seed_everything
import sys
if args.algorithm == 'gabriel':
options_list = [0]
if args.algorithm == 'relative_neighborhood':
options_list = [0]
if args.algorithm == 'kmeans':
options_list = [i for i in range(2,40)]
if args.algorithm == 'optics':
options_list = [i for i in range(2,40)]
if args.algorithm == 'knn_weighted':
options_list = [i for i in range(2,40)]
if args.algorithm == 'knn_unweighted':
options_list = [i for i in range(2,40)]
if args.algorithm == 'gaussian':
options_list = [round(i,2) for i in np.arange(0.05,0.95,0.05)]
if args.algorithm == 'minmax':
options_list = [round(i,2) for i in np.arange(0.05,0.95,0.05)]
if args.algorithm == 'dtw':
options_list = [round(i,2) for i in np.arange(0.05,0.95,0.05)]
if args.algorithm == 'mic':
options_list = [round(i,2) for i in np.arange(0.05,0.95,0.05)]
if args.algorithm == 'correlation':
options_list = [round(i,2) for i in np.arange(0.05,0.95,0.05)]
print(f'went for {args.dataset} and {args.algorithm} \n')
print(f' options are = {options_list}')
graph_generator_obj = graph_generator()
for i in options_list:
seed = 0
os.environ['PYTHONHASHSEED']=str(seed)
random.seed(seed)
np.random.seed(seed)
np.random.permutation(seed)
os.environ['PYTHONHASHSEED']=str(seed)# 2. Set `python` built-in pseudo-random generator at a fixed value
np.random.RandomState(seed)
#pip install tensorflow-determinism needed
os.environ['TF_DETERMINISTIC_OPS'] = '1'
os.environ['TF_CUDNN_DETERMINISTIC'] = '1'
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
seed_everything(seed)
print('came here?')
if args.dataset == 'bay':
dataset = PemsBay()
if args.dataset == 'la':
dataset = MetrLA()
if args.dataset == 'air':
dataset = AirQuality(small=True)
print(f'current combination = {i} with {args.dataset} and {args.algorithm}')
graph_generator_obj = graph_generator()
if args.algorithm == 'gabriel':
graph_generator_obj.gabriel(f'sensor_locations/sensor_locations_{args.dataset}.csv')
if args.algorithm == 'optics':
graph_generator_obj.optics(f'sensor_locations/sensor_locations_{args.dataset}.csv', min_samples=i)
if args.algorithm == 'knn_weighted':
graph_generator_obj.knn_weighted(f'sensor_locations/sensor_locations_{args.dataset}.csv', k=i)
if args.algorithm == 'knn_unweighted':
graph_generator_obj.knn_unweighted(f'sensor_locations/sensor_locations_{args.dataset}.csv', k=i)
if args.algorithm == 'minmax':
graph_generator_obj.minmax(f'sensor_locations/sensor_locations_{args.dataset}.csv', cutoff=i)
# if args.algorithm == 'dbscan':
# graph_generator_obj.dbscan(f'sensor_locations/sensor_locations_{args.dataset}.csv', eps=1, min_samples=i)
if args.algorithm == 'gaussian':
graph_generator_obj.gaussian(f'sensor_locations/sensor_locations_{args.dataset}.csv', normalized_k=i)
if args.algorithm == 'relative_neighborhood':
graph_generator_obj.relative_neighborhood(f'sensor_locations/sensor_locations_{args.dataset}.csv')
if args.algorithm == 'kmeans':
graph_generator_obj.kmeans(f'sensor_locations/sensor_locations_{args.dataset}.csv', num_clusters=i)
if args.algorithm == 'dtw' or args.algorithm == 'correlation' or args.algorithm == 'mic' and args.dataset in ['la','bay','air']:
print(f'went for clips')
graph_generator_obj.from_signal(f'sensor_locations/sensor_locations_{args.dataset}.csv', f'sensor_locations/inputs_{args.dataset}.npy', variant=args.algorithm, clips=True,threshold=i)
graph_generator_obj.create_adjacency_matrix(fill_diagonal = True)
graph_generator_obj.summary_statistics()
print(f'look here steef')
print(graph_generator_obj.data.shape[0])
print(nx.number_of_edges(graph_generator_obj.networkx_graph))
if graph_generator_obj.number_of_edges == 0:
print('no edges so no solution')
print('\n' * 5)
continue
if graph_generator_obj.data.shape[0] >= graph_generator_obj.number_of_edges:
print('only self edges')
print('\n' * 5)
continue
else:
print(f'number of edges is > {graph_generator_obj.data.shape[0]}, namely {graph_generator_obj.number_of_edges}')
print('\n' * 5)
print(f"Sampling period: {dataset.freq}\n"
f"Has missing values: {dataset.has_mask}\n"
# f"Percentage of missing values: {(1 - dataset.mask.mean()) * 100:.2f}%\n"
f"Has dataset exogenous variables: {dataset.has_covariates}\n"
f"Relevant attributes: {', '.join(dataset.attributes.keys())}")
adj2 = graph_generator_obj.adjacency_matrix
print(graph_generator_obj.networkx_graph)
print(adj2)
print(type(adj2))
adj2 = adj_to_edge_index(adj2)
print(adj2)
covariates = {'u': dataset.datetime_encoded('day').values}
target, idx = dataset.numpy(return_idx=True)
print()
connectivity = dataset.get_connectivity(threshold=0.1,
include_self=False,
normalize_axis=1,
layout="edge_index")
perform_classic = True
if perform_classic == True:
print('went for classic gaussian with extra info version')
connectivity = dataset.get_connectivity(threshold=0.05,
include_self=True,
normalize_axis=1,
layout="edge_index")
edge_index, edge_weight = connectivity
print(f'edge_index {edge_index.shape}:\n', edge_index)
print(f'edge_weight {edge_weight.shape}:\n', edge_weight)
adj = edge_index_to_adj(edge_index, edge_weight)
print(f'A {adj.shape}:')
torch_dataset = SpatioTemporalDataset(target=target,
index=idx,
connectivity=connectivity,
mask=dataset.mask,
horizon=12,
window=12,
stride=1)
else:
print('went for experiment version')
torch_dataset = SpatioTemporalDataset(target=target,
index=idx,
connectivity=adj2,
mask=dataset.mask,
horizon=12,
window=12,
stride=1)
print(torch_dataset)
scalers = {'target': StandardScaler(axis=(0, 1))}
splitter = dataset.get_splitter(val_len=0.1, test_len=0.2)
dm = SpatioTemporalDataModule(
dataset=torch_dataset,
scalers=scalers,
splitter=splitter,
batch_size=64,
)
dm.setup()
print(dm)
model_kwargs_timethenspace = dict(n_nodes=torch_dataset.n_nodes,
input_size=torch_dataset.n_channels,
output_size=torch_dataset.n_channels,
horizon=torch_dataset.horizon)
loss_fn = MaskedMAE()
metrics = {'mae': MaskedMAE(),
'mape': MaskedMAPE(),
'mse': MaskedMSE(),
'mape': MaskedMAPE(),
}
model_kwargs_timethenspace = {
'input_size': dm.n_channels, # 1 channel
'horizon': dm.horizon, # 12, the number of steps ahead to forecast
'hidden_size': 16,
'rnn_layers': 1,
'gcn_layers': 2
}
if args.gnn_model == 'timethenspace':
max_epochs = 100
predictor = Predictor(
model_class=TimeThenSpaceModel,
model_kwargs=model_kwargs_timethenspace,
optim_class=torch.optim.Adam,
optim_kwargs={'lr': 0.003},
loss_fn=loss_fn,
metrics=metrics
)
checkpoint_callback = ModelCheckpoint(
dirpath='logs',
save_top_k=1,
monitor='val_mae',
mode='min',
)
max_epochs = 100
trainer = pl.Trainer(max_epochs=max_epochs,
# logger=logger,
gpus=1 if torch.cuda.is_available() else None,
limit_train_batches=100,
callbacks=[checkpoint_callback],
enable_model_summary=True)
print('look above')
trainer.fit(predictor, datamodule=dm)
predictor.load_model(checkpoint_callback.best_model_path)
predictor.freeze()
performance = trainer.test(predictor, datamodule=dm)
output = trainer.predict(predictor, dataloaders=dm.val_dataloader())
print('done ')
print(output['y'].shape)
df = pd.DataFrame(performance)
print(df)
del trainer
with open(f"epoch100.csv", "a") as text_file:
print(f'{print_time()},{args.dataset},{args.algorithm},{i},{df["test_mse"].item():.5f},{df["test_mae"].item():.5f},{df["test_mape"].item():.5f},{graph_generator_obj.number_of_edges}', file=text_file)