-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
58 lines (45 loc) · 1.52 KB
/
utils.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
import torchgraphs as tg
import torch
import pickle, gzip, pickletools
import threading
def state_2_graph(obs):
node_features = obs['node_features']
edge_features = obs['edge_features']
global_features = obs['global_features']
edges_from = obs['edges_from']
edges_to = obs['edges_to']
g = tg.Graph(
node_features=torch.FloatTensor(node_features),
edge_features=torch.FloatTensor(edge_features),
global_features=torch.FloatTensor(global_features),
senders=torch.tensor(edges_from),
receivers=torch.tensor(edges_to)
)
return g
def state_2_graphbatch(obs):
node_features = obs['node_features']
edge_features = obs['edge_features']
global_features = obs['global_features']
edges_from = obs['edges_from']
edges_to = obs['edges_to']
g = tg.Graph(
node_features=torch.FloatTensor(node_features),
edge_features=torch.FloatTensor(edge_features),
global_features=torch.FloatTensor(global_features),
senders=torch.tensor(edges_from),
receivers=torch.tensor(edges_to)
)
return tg.GraphBatch.collate([g])
def save_object(obj, path):
with gzip.open(path, "wb") as f:
pickled = pickle.dumps(obj)
optimized_pickle = pickletools.optimize(pickled)
f.write(optimized_pickle)
def load_object(path):
# with open(path, 'rb') as f:
# obj = pickle.load(f)
# return obj
with gzip.open(path, 'rb') as f:
p = pickle.Unpickler(f)
obj = p.load()
return obj