-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_graph.py
205 lines (168 loc) · 6.48 KB
/
create_graph.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
import json
from os import path as osp
from glob import glob
import numpy as np
import pandas as pd
import networkx as nx
from networkx.drawing.nx_pydot import write_dot, graphviz_layout
from networkx.readwrite.gpickle import write_gpickle
def create_knowledge_G(df):
G = nx.Graph()
for index, row in df.iterrows():
obj = row['ObjectType'].lower()
if obj == 'targetcircle' or obj.endswith('*'):
continue
props = row['Actionable Properties']
if type(props) is str and 'Pickupable' not in props.split(','):
t = 'recep'
else:
t = 'object'
G.add_node(obj, t=t)
scenes = row['Scenes']
scenes = [s.split('(')[0].strip().replace(' ', '')[:-1].lower() for s in scenes.split('\n')]
for s in scenes:
G.add_node(s, t='room')
G.add_edge(obj, s)
receps = row['Default Compatible Receptacles']
if type(receps) is str:
receps = [r.strip().lower() for r in receps.split(',')]
for r in receps:
G.add_node(r, t='recep')
G.add_edge(obj, r)
for s in scenes:
G.add_edge(s, r)
return G
def metrics(pos, pos_ref):
dpos = pos - pos_ref
r = np.linalg.norm(dpos)
pitch = np.arctan2(dpos[1], dpos[2])
yaw = np.arctan2(dpos[2], dpos[0])
return {'dist': r, 'pitch': pitch, 'yaw': yaw}
def create_scene_G_floor(data, KG=None):
G = nx.Graph()
pos_ref = np.array([0.0, 1.0, 0.0])
G.add_node('floor', x=pos_ref[0], y=pos_ref[1], z=pos_ref[2])
for entry in data:
ent = entry['id'].split('|')[0].lower()
pos = np.array([
entry['position']['x'],
entry['position']['y'],
entry['position']['z']
], dtype=float)
G.add_node(ent, x=pos[0], y=pos[1], z=pos[2])
# edges describe relation in polar coordinates
if KG is not None:
if ent not in KG.nodes:
continue
# connect receptacles with objects
for n in KG.neighbors(ent):
if KG.nodes[n]['t'] != 'room' and G.has_node(n):
pos_n = np.array([
G.nodes[n]['x'],
G.nodes[n]['y'],
G.nodes[n]['z']
], dtype=float)
G.add_edge(ent, n, **metrics(pos, pos_n))
# connect agent with receptacles
if KG.nodes[ent]['t'] == 'recep':
G.add_edge('floor', ent, **metrics(pos, pos_ref))
else:
G.add_edge('floor', ent, **metrics(pos, pos_ref))
# connected isolated nodes with floor
for n in nx.isolates(G):
pos_n = np.array([
G.nodes[n]['x'],
G.nodes[n]['y'],
G.nodes[n]['z']
], dtype=float)
G.add_edge('floor', n, **metrics(pos, pos_ref))
return G
def create_scene_G(data, KG=None):
G = nx.Graph()
pos_ref = np.zeros(3)
#pos_ref = np.array([0.0, 1.0, 0.0])
#G.add_node('floor', x=pos_ref[0], y=pos_ref[1], z=pos_ref[2])
for entry in data:
if entry['id'] == 'agent':
pos_ref = np.array([
entry['position']['x'],
entry['position']['y'],
entry['position']['z']
], dtype=float)
G.add_node('agent', x=pos_ref[0], y=pos_ref[1], z=pos_ref[2])
else:
ent = entry['id'].split('|')[0].lower()
pos = np.array([
entry['position']['x'],
entry['position']['y'],
entry['position']['z']
], dtype=float)
G.add_node(ent, x=pos[0], y=pos[1], z=pos[2])
# edges describe relation in polar coordinates
if KG is not None:
if ent not in KG.nodes:
continue
# connect receptacles with objects
for n in KG.neighbors(ent):
if KG.nodes[n]['t'] != 'room' and G.has_node(n):
pos_n = np.array([
G.nodes[n]['x'],
G.nodes[n]['y'],
G.nodes[n]['z']
], dtype=float)
G.add_edge(ent, n, **metrics(pos, pos_n))
# connect agent with receptacles
if KG.nodes[ent]['t'] == 'recep':
G.add_edge('agent', ent, **metrics(pos, pos_ref))
else:
G.add_edge('agent', ent, **metrics(pos, pos_ref))
# connected isolated nodes with agent
for n in nx.isolates(G):
pos_n = np.array([
G.nodes[n]['x'],
G.nodes[n]['y'],
G.nodes[n]['z']
], dtype=float)
G.add_edge('agent', n, **metrics(pos, pos_ref))
return G
def test_metrics():
m = [
metrics(np.array([0, 0, 0]), np.array([0, 0, 0])), # --> 0 on
metrics(np.array([0, 1, 0]), np.array([0, 0, 0])), # --> + roof
metrics(np.array([0, 0, 0]), np.array([0, 1, 0])), # --> - floor
metrics(np.array([1, 0, 0]), np.array([0, 0, 0])), # --> 0 front
metrics(np.array([0, 0, 0]), np.array([1, 0, 0])), # --> -0 front
metrics(np.array([0, 0, 1]), np.array([0, 0, 0])), # --> + right
metrics(np.array([0, 0, 0]), np.array([0, 0, 1])), # --> - left
]
for x in m:
print(x)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('infile')
parser.add_argument('outfile')
parser.add_argument('--kg', help='path to domain knowledge')
args = parser.parse_args()
test_metrics()
KG = None
if args.kg:
df = pd.read_csv(args.kg)
KG = create_knowledge_G(df)
#nx.draw(KG, graphviz_layout(G))
#write_dot(KG, args.outfile)
if osp.isdir(args.infile):
for path in glob(osp.join(args.infile, "*/*/graphs/000000000.json")):
parent_dir = osp.dirname(osp.dirname(path))
with open(path) as f:
G = create_scene_G(json.load(f), KG=KG)
#nx.draw(G, graphviz_layout(G))
#write_dot(G, osp.join(parent_dir, args.outfile))
write_gpickle(G, osp.join(parent_dir, args.outfile))
nx.draw(G, graphviz_layout(G))
write_dot(G, "scene.dot")
else:
with open(args.infile) as f:
G = create_scene_G(json.load(f), KG=KG)
nx.draw(G, graphviz_layout(G))
write_dot(G, args.outfile)