-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbindings_demo.py
187 lines (146 loc) · 5.34 KB
/
bindings_demo.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
# Copyright 2022, Massachusetts Institute of Technology.
# All Rights Reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Research was sponsored by the United States Air Force Research Laboratory and
# the United States Air Force Artificial Intelligence Accelerator and was
# accomplished under Cooperative Agreement Number FA8750-19-2-1000. The views
# and conclusions contained in this document are those of the authors and should
# not be interpreted as representing the official policies, either expressed or
# implied, of the United States Air Force or the U.S. Government. The U.S.
# Government is authorized to reproduce and distribute reprints for Government
# purposes notwithstanding any copyright notation herein.
#
#
# -*- coding: utf-8 -*-
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.13.4
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %%
import spark_dsg as dsg
import pathlib
# %%
path_to_dsg = "~/uh2_office_dsg.json"
path_to_dsg = pathlib.Path(path_to_dsg).expanduser().absolute()
# %%
G = dsg.DynamicSceneGraph.load(str(path_to_dsg))
# %%
fig = dsg.plot_scene_graph(G, marker_size=6)
if fig is not None:
fig.show(renderer="notebook")
# %% [markdown]
# ## Node Access and Node Attributes
# %%
def print_node_if_exists(node_id):
"""Grab node attributes and display if the graph contains the node."""
test_str = "yes" if G.has_node(node_id.value) else "no"
print(f"Graph contains node {node_id}: {test_str}")
if not G.has_node(node_id.value):
return
node = G.get_node(node_id.value)
print(f"Node {node_id} attributes:\n{node.attributes}")
# object node
print("")
print_node_if_exists(dsg.NodeSymbol("O", 4))
# places node
print("")
print_node_if_exists(dsg.NodeSymbol("p", 135))
# room node
print("")
print_node_if_exists(dsg.NodeSymbol("R", 19))
# agent node
print("")
print_node_if_exists(dsg.NodeSymbol("a", 5))
# %% [markdown]
# ## Layer Iteration and Access
# %%
print("Layers:")
for layer in G.layers:
print(f" - {layer.id}")
print("")
# %%
room_layer = G.get_layer(dsg.DsgLayers.ROOMS)
room_node_strs = [f"{x.id}" for x in room_layer.nodes]
print(f"Rooms: {room_node_strs}")
room_edge_strs = [f" - {x}" for x in room_layer.edges]
print("Room Edges:")
print("\n".join(room_edge_strs))
print("")
# %% [markdown]
# ## Interlayer Edge Access
# %%
layer_edge_counts = {}
for edge in G.interlayer_edges:
source_layer = G.get_node(edge.source).layer
target_layer = G.get_node(edge.target).layer
if source_layer not in layer_edge_counts:
layer_edge_counts[source_layer] = {}
if target_layer not in layer_edge_counts[source_layer]:
layer_edge_counts[source_layer][target_layer] = 0
layer_edge_counts[source_layer][target_layer] += 1
print("Interlayer Edges:")
for source_layer in layer_edge_counts:
print(f" - {source_layer} -> {layer_edge_counts[source_layer]}")
print("")
# %% [markdown]
# ## Entire Graph Access
# %%
node_type_counts = {}
for node in G.nodes:
if node.id.category not in node_type_counts:
node_type_counts[node.id.category] = 0
node_type_counts[node.id.category] += 1
print("Node Types:")
for category, count in node_type_counts.items():
print(f" - {category}: {count}")
edge_counts = {}
for edge in G.edges:
source_layer = G.get_node(edge.source).layer
target_layer = G.get_node(edge.target).layer
if source_layer not in edge_counts:
edge_counts[source_layer] = {}
if target_layer not in edge_counts[source_layer]:
edge_counts[source_layer][target_layer] = 0
edge_counts[source_layer][target_layer] += 1
print("All Edges:")
for source_layer in edge_counts:
print(f" - {source_layer} -> {edge_counts[source_layer]}")
print("")
# %% [markdown]
# ## Bounding boxes from nodes in lower layers
# %%
dsg.add_bounding_boxes_to_layer(G, dsg.DsgLayers.ROOMS)
print("Room bounding boxes:")
for node in G.get_layer(dsg.DsgLayers.ROOMS).nodes:
print(f" - {node.id}: {node.attributes.bounding_box}")