GraSIF / office /create_pkl.py
Onisci's picture
Load data
7cec401 verified
'''
This script processes `office_changes.json` to generate a dataset of office manipulation tasks.
The `CHANGES_DESCRIPTION_FILE` file contains a list of tasks, each with:
- "name": A natural language description.
- "init_conditions": An array of conditions to define the starting world state. null if no changes
- "goal_conditions": An array of conditions that describe the target world state.
- "actions": A sequence of actions to transition from the initial to the goal state.
'''
import json
import pickle
from office_graph import office_graph, add_edges
import copy
CHANGES_DESCRIPTION_FILE = 'office_changes.json'
OUT_FILENAME = 'office29.pkl'
def change_node(node, condition, graph):
if condition.get("holding", False):
graph.nodes[node]['holding'] = (condition["holding"]['name'], condition["holding"]['id'])
if condition["relation"]:
graph.nodes[node]['related_to'] = (condition["related_to"]["name"], condition["related_to"]["id"])
graph.nodes[node]['relation'] = condition["relation"]
if condition["states"]:
states = condition.get("states", [])
for state in states:
if state not in graph.nodes[node]['states']:
graph.nodes[node]['states'].append(state)
if state == 'off':
graph.nodes[node]['states'].remove('on')
if state == 'on':
graph.nodes[node]['states'].remove('off')
if state == 'closed':
graph.nodes[node]['states'].remove('open')
if state == 'open':
graph.nodes[node]['states'].remove('closed')
if __name__ == "__main__":
# Load the JSON file
with open(CHANGES_DESCRIPTION_FILE, "r") as file:
tasks = json.load(file)
dataset = []
i=0
for task in tasks:
i+=1
name = task["name"]
init_graph = copy.deepcopy(office_graph)
goal_graph = copy.deepcopy(office_graph)
# Process initial conditions
if task['init_conditions']:
for condition in task.get("init_conditions", []):
node = (condition["node"]["name"], condition["node"]["id"])
change_node(node, condition, init_graph)
# Process goal conditions
for condition in task.get("goal_conditions", []):
node = (condition["node"]["name"], condition["node"]["id"])
change_node(node, condition, goal_graph)
# Process actions
actions = []
for action in task.get("actions", []):
actions.append((action['action'], (action['node']["name"],action['node']["id"])))
# Reset graph edges
init_graph.remove_edges_from(list(office_graph.edges))
goal_graph.remove_edges_from(list(office_graph.edges))
add_edges(init_graph)
add_edges(goal_graph)
entity = {
"id": i,
"name": name,
"task": name,
"init": init_graph,
"goal": goal_graph,
"actions": actions,
}
dataset.append(entity)
with open(OUT_FILENAME, "wb") as file:
pickle.dump(dataset, file)