-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-bulk-insert.py
More file actions
224 lines (192 loc) · 7.93 KB
/
Copy pathquery-bulk-insert.py
File metadata and controls
224 lines (192 loc) · 7.93 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
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
import copy
import itertools
import json
import logging as log
import os
import typing
import pandas as pd
import common
BROKER_IP = '127.0.0.1'
BROKER_PORT = '9999'
ENDPOINT = f'{BROKER_IP}:{BROKER_PORT}'
# Note: targetSensorAttributes: should be list of list
DATASET_DEPENDENT_SETTING = {
'METR-LA': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['speed']],
'inputLength': [12], 'outputLength': [12],
# 'inputLength': [6, 12, 18, 24], 'outputLength': [6, 12, 18, 24],
},
'PEMS-BAY': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['speed']],
'inputLength': [12], 'outputLength': [12],
# 'inputLength': [6, 12, 18, 24], 'outputLength': [6, 12, 18, 24],
},
'PEMSD7': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['speed']],
'inputLength': [12], 'outputLength': [12],
# 'inputLength': [6, 12, 18, 24], 'outputLength': [6, 12, 18, 24],
},
'PEMS03': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['flow']],
'inputLength': [12], 'outputLength': [12],
},
'PEMS04': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['flow']],
'inputLength': [12], 'outputLength': [12],
},
'PEMS07': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['flow']],
'inputLength': [12], 'outputLength': [12],
# 'inputLength': [6, 12, 18, 24], 'outputLength': [6, 12, 18, 24],
},
'PEMS08': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['flow']],
'inputLength': [12], 'outputLength': [12],
},
'Chickenpox': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['cases']],
'inputLength': [8], 'outputLength': [10],
},
'JP-Pref': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['cases']],
'inputLength': [20], 'outputLength': [15],
},
'US-Regions': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['cases']],
'inputLength': [20], 'outputLength': [15],
},
'US-States': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['cases']],
'inputLength': [20], 'outputLength': [15],
},
'Uzel2022': {
'additionalTemporalEmbed': ['timestamp_in_minute'],
'targetSensorAttributes': [['intensity']],
'inputLength': [15], 'outputLength': [15],
# 'inputLength': [7, 15, 22, 30], 'outputLength': [7, 15, 22, 30],
},
'NOAA1000': {
'additionalTemporalEmbed': ['timestamp_in_day'],
'targetSensorAttributes': [['temperature']],
'inputLength': [7], 'outputLength': [7],
# 'inputLength': [3, 7, 11, 14], 'outputLength': [3, 7, 11, 14],
},
}
MODEL_DEPENDENT_SETTING = {
'ARMA': {'adjacencyMatrixLaplacianMatrix': [None]},
'VAR': {'adjacencyMatrixLaplacianMatrix': [None]},
'Seq2Seq_RNN': {'adjacencyMatrixLaplacianMatrix': [None]},
'Seq2Seq_LSTM': {'adjacencyMatrixLaplacianMatrix': [None]},
'Seq2Seq_GRU': {'adjacencyMatrixLaplacianMatrix': [None]},
'DCRNN': {'adjacencyMatrixLaplacianMatrix': ['scaled_lap_sym']},
'ASTGCN': {'adjacencyMatrixLaplacianMatrix': ['cheb_poly'], },
'GWNet': {'adjacencyMatrixLaplacianMatrix': ['dual_rand_mat_asym']},
'AGCRN': {'adjacencyMatrixLaplacianMatrix': [None]},
'DSTAGNN': {'adjacencyMatrixLaplacianMatrix': ['cheb_poly']},
'STGODE': {'adjacencyMatrixLaplacianMatrix': ['raw_thresholded_gaussian']},
'DGCRN': {'adjacencyMatrixLaplacianMatrix': ['dual_rand_mat_asym']},
'TGCRN': {'adjacencyMatrixLaplacianMatrix': [None]},
}
DEFAULT = {
# about dataset
'trainTestRatio': 0.7,
'adjacencyMatrixThresholdValue': 0.7,
# about model
'maxEpoch': 100,
# 'maxEpoch': 1, # for SPATS testing
'batchSize': 64,
# 'batchSize': 64, # for SPATS testing
# about loss function
'lossFunction': ["MAE", "MSE", "MAAPE"], # should be list
'targetLossFunction': "MSE",
# about optimizer
'optimizer': 'Adam',
'learningRate': 0.001,
'weightDecay': 0.0005,
}
VARIATION = {
'datasetName': ['METR-LA', 'PEMS-BAY', 'PEMSD7', 'PEMS03', 'PEMS04', 'PEMS07', 'PEMS08', 'Chickenpox', 'JP-Pref', 'US-Regions', 'US-States', 'Uzel2022', 'NOAA1000'],
'modelName': ['AGCRN', 'ASTGCN', 'DCRNN', 'DGCRN', 'DSTAGNN', 'GWNet', 'STGODE', 'TGCRN', 'Seq2Seq_RNN', 'Seq2Seq_LSTM', 'Seq2Seq_GRU', 'ARMA', 'VAR'],
# 'adjacencyMatrixThresholdValue': [0.0, 0.2, 0.4, 0.6, 0.8],
}
def stdin_generator(variation: dict) -> typing.Generator[dict, None, None]:
# result = copy.deepcopy(default)
variation_flat = pd.json_normalize(
variation, sep='.').to_dict(orient='records')[0]
# ignore list size is 0
variation_flat = {key: value for (
key, value) in variation_flat.items() if len(value) > 0}
variation_flat_keys = [key for key in variation_flat.keys()]
values = [values for (_, values) in variation_flat.items()]
for element in itertools.product(*values):
result = {}
for (key, new_value) in zip(variation_flat_keys, element):
if key in result.keys():
if result[key] == new_value:
continue
result[key] = new_value
yield result
else:
result[key] = new_value
yield result
def dependent_setting_generator(dataset_name: str, model_name: str) -> typing.Generator[typing.Tuple[str, typing.Any], None, None]:
d_list = list(stdin_generator(DATASET_DEPENDENT_SETTING[dataset_name]))
m_list = list(stdin_generator(MODEL_DEPENDENT_SETTING[model_name]))
for d_setting, m_setting in itertools.product(d_list, m_list):
# combine two dicts
yield {**d_setting, **m_setting}
def main():
log.basicConfig(
format="[%(levelname)s][%(asctime)s.%(msecs)03d][%(filename)s:%(lineno)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=log.INFO)
# condition check for dataset
for dataset_name in VARIATION['datasetName']:
assert dataset_name in DATASET_DEPENDENT_SETTING, \
f'Error: datasetName {dataset_name} is not supported'
# condition check for model
for model_name in VARIATION['modelName']:
assert model_name in MODEL_DEPENDENT_SETTING, \
f'Error: modelName {model_name} is not supported'
# generate all stdin from complete_variation
for stdin in stdin_generator(VARIATION):
stdin = {**DEFAULT, **stdin}
dataset_name = stdin['datasetName']
model_name = stdin['modelName']
for dep_setting in dependent_setting_generator(dataset_name, model_name):
stdin = {**stdin, **dep_setting}
model_config_path = f"./common/model/{stdin['modelName']}.yaml"
if os.path.exists(model_config_path):
model_config = common.util.load_yaml(model_config_path)
stdin['modelConfig'] = copy.deepcopy(model_config)
else:
stdin['modelConfig'] = {}
# STDIN is generated.
# for debugging, add print(stdin) here and comment out the following code.
res = common.util.rest_json(
'POST', ENDPOINT, '/api/job/control',
query={
'request': 'insert',
},
body={
'argument': json.dumps(['training']),
'stdin': json.dumps(stdin),
})
if (res is None) or (res.status_code != 200):
log.fatal('Broker seems down.')
return
res = res.json()
log.info(f'Successfully insert job {res["jobID"]}')
if __name__ == '__main__':
main()