-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathimport_cim_dumps.py
291 lines (217 loc) · 8.73 KB
/
import_cim_dumps.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
import os
import pickle
from argparse import ArgumentParser
import numpy as np
import yaml
def import_from_snapshot_dump(streamit, folder: str, npy_name: str, meta_name: str, category: str):
"""Import specified category from snapshot dump file into data service.
Args:
streamit (streamit) : Streamit instance.
folder (str): Folder name of snapshot dump file.
npy_name (str): Name of .npy file that hold dumped numpy array data.
meta_name (str): File name of the meta file.
category (str): Category name to save into database.
"""
npy_path = os.path.join(folder, npy_name)
meta_path = os.path.join(folder, meta_name)
# Read meta file to get names and length of each field.
with open(meta_path, "r") as fp:
field_name_list = fp.readline().split(",")
field_length_list = [int(line) for line in fp.readline().split(",")]
instance_list: np.ndarray = np.load(npy_path)
# Instance number will be same for numpy backend.
instance_number = len(instance_list[0])
for tick in range(len(instance_list)):
streamit.tick(tick)
for instance_index in range(instance_number):
field_dict = {}
field_slot_index = 0
for field_index in range(len(field_name_list)):
field_name = field_name_list[field_index].strip()
field_length = field_length_list[field_index]
field_dict["index"] = instance_index
if field_length == 1:
field_dict[field_name] = instance_list[tick][instance_index][field_name].item()
else:
field_dict[field_name] = list(
[v.item() for v in instance_list[tick][instance_index][field_name]],
)
field_slot_index += field_length
streamit.data(category, **field_dict)
return instance_number
def import_port_details(streamit, folder: str):
"""Import port details into database from specified folder.
Args:
streamit (streamit) : Streamit instance.
folder (str): Folder path that contains the port detail file.
"""
port_npy_name = "ports.npy"
port_meta_name = "ports.meta"
category = "port_details"
return import_from_snapshot_dump(streamit, folder, port_npy_name, port_meta_name, category)
def import_vessel_details(streamit, folder: str):
"""Import vessel details into database.
Args:
streamit (streamit) : Streamit instance.
folder (str): Folder path that contains vessel details.
"""
vessels_npy_name = "vessels.npy"
vessels_meta_name = "vessels.meta"
category = "vessel_details"
return import_from_snapshot_dump(streamit, folder, vessels_npy_name, vessels_meta_name, category)
def import_full_on_ports(streamit, data: np.ndarray, port_number: int):
"""Import full_on_ports information into database.
Args:
streamit (streamit) : Streamit instance.
data (numpy.ndarray): Data of full_on_ports.
port_number (int): Number of ports.
"""
for tick in range(len(data)):
streamit.tick(tick)
m = data[tick][0].reshape(port_number, -1)
# We only save cells that value > 0.
a, b = np.where(m > 0)
for from_port_index, to_port_index in list(zip(a, b)):
streamit.data(
"full_on_ports",
from_port_index=from_port_index,
dest_port_index=to_port_index,
quantity=m[from_port_index, to_port_index],
)
def import_full_on_vessels(streamit, data: np.ndarray, port_number: int, vessel_number: int):
"""Import full_on_vessels data into database.
Args:
streamit (streamit) : Streamit instance.
data (numpy.ndarray): Data that contains full_on_vessels matrix.
port_number (int): Number of ports.
vessel_number (int): Number of vessels.
"""
for tick in range(len(data)):
streamit.tick(tick)
m = data[tick][0].reshape(vessel_number, port_number)
a, b = np.where(m > 0)
for vessel_index, port_index in list(zip(a, b)):
streamit.data(
"full_on_vessels",
vessel_index=vessel_index,
port_index=port_index,
quantity=m[vessel_index, port_index],
)
def import_vessel_plans(streamit, data: np.ndarray, port_number: int, vessel_number: int):
"""Import vessel_plans matrix into database.
Args:
streamit (streamit) : Streamit instance.
data (numpy.ndarray): Data that contains vessel_plans matrix.
port_number (int): Number of ports.
vessel_number (int): Number of vessels.
"""
for tick in range(len(data)):
streamit.tick(tick)
m = data[tick][0].reshape(vessel_number, port_number)
a, b = np.where(m > -1)
for vessel_index, port_index in list(zip(a, b)):
streamit.data(
"vessel_plans",
vessel_index=vessel_index,
port_index=port_index,
planed_arrival_tick=m[vessel_index, port_index],
)
def import_metrics(streamit, epoch_full_path: str, port_number: int, vessel_number: int):
"""Import matrix into database.
Args:
streamit (streamit) : Streamit instance.
epoch_full_path (str): Path that for target epoch.
port_number (int): Number of ports.
vessel_number (int): Number of vessels.
"""
matrics_path = os.path.join(epoch_full_path, "matrices.npy")
matrics = np.load(matrics_path)
import_full_on_ports(streamit, matrics["full_on_ports"], port_number)
import_full_on_vessels(streamit, matrics["full_on_vessels"], port_number, vessel_number)
import_vessel_plans(streamit, matrics["vessel_plans"], port_number, vessel_number)
def import_attention(streamit, atts_path: str):
"""Import attaention data.
Args:
streamit (streamit) : Streamit instance.
atts_path (str): Path to attention file.
"""
with open(atts_path, "rb") as fp:
attentions = pickle.load(fp)
attention_index = -1
# List of tuple (tick, attention dict contains:"p2p", "p2v", "v2p").
for tick, attention in attentions:
attention_index += 1
tick = int(tick)
streamit.tick(tick)
streamit.complex("attentions", attention)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"--name",
required=True,
help="Experiment name show in database",
)
parser.add_argument(
"--scenario",
required=True,
help="Scenario name of import experiment",
)
parser.add_argument(
"--topology",
required=True,
help="Topology of target scenario",
)
parser.add_argument(
"--durations",
required=True,
type=int,
help="Durations of each episode",
)
parser.add_argument(
"--episodes",
required=True,
type=int,
help="Total episode of this experiment",
)
parser.add_argument(
"--dir",
required=True,
help="Root folder of dump files",
)
parser.add_argument(
"--ssdir",
help="Folder that contains snapshots data that with epoch_x sub-folders",
)
parser.add_argument(
"--host",
default="127.0.0.1",
help="Host of questdb server",
)
args = parser.parse_args()
assert os.path.exists(args.dir)
assert os.path.exists(args.ssdir)
# Force enable streamit.
os.environ["MARO_STREAMIT_ENABLED"] = "true"
os.environ["MARO_STREAMIT_EXPERIMENT_NAME"] = args.name
from maro.streamit import streamit
with streamit:
# experiment name
with open(os.path.join(args.dir, "config.yml"), "r") as fp:
config = yaml.safe_load(fp)
# streamit.info(args.scenario, args.topology, args.durations, args.episodes)
streamit.complex("config", config)
for episode in range(args.episodes):
epoch_folder = f"epoch_{episode}"
epoch_full_path = os.path.join(args.ssdir, epoch_folder)
# ensure epoch folder exist
if os.path.exists(epoch_full_path):
streamit.episode(episode)
# import for each category
port_number = import_port_details(streamit, epoch_full_path)
vessel_number = import_vessel_details(streamit, epoch_full_path)
import_metrics(streamit, epoch_full_path, port_number, vessel_number)
# NOTE: we only have one attention file for now, so hard coded here
streamit.episode(0)
import_attention(streamit, os.path.join(args.dir, "atts_1"))