forked from aistairc/virtualhome_aist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request aistairc#19 from aistairc/fix_state_value
Fix state value
- Loading branch information
Showing
4 changed files
with
367 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import sys\n", | ||
"import os\n", | ||
"import re\n", | ||
"import json\n", | ||
"import glob\n", | ||
"import copy\n", | ||
"import time\n", | ||
"import random\n", | ||
"import shutil\n", | ||
"from pathlib import Path\n", | ||
"\n", | ||
"sys.path.append('../simulation/')\n", | ||
"sys.path.append('../dataset_utils/')\n", | ||
"\n", | ||
"import augmentation_utils" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_program_file(file_path):\n", | ||
" program_name = ''\n", | ||
" list_of_steps = []\n", | ||
" i = 1\n", | ||
" with open(file_path, \"r\") as f:\n", | ||
" lines = f.readlines()\n", | ||
" program_name = lines[0].replace(\"\\n\", \"\")\n", | ||
" for line in lines[2:]:\n", | ||
" line = line.replace(\"\\n\", \"\")\n", | ||
" if line.startswith(\"<\"):\n", | ||
" line = line.replace(\"<char0> \",\"\")\n", | ||
" i+=1\n", | ||
" temp_dict = {}\n", | ||
" temp_dict['step']=i\n", | ||
" action, objs, indexs = augmentation_utils.parseStrBlock(line)\n", | ||
" temp_dict['action']=action\n", | ||
" if len(objs) > 0:\n", | ||
" temp_dict['obj1']=objs[0]\n", | ||
" temp_dict['index1']=indexs[0]\n", | ||
" if len(objs) > 1:\n", | ||
" temp_dict['obj2']=objs[1]\n", | ||
" temp_dict['index2']=indexs[1]\n", | ||
" list_of_steps.append(temp_dict)\n", | ||
" else:\n", | ||
" pass\n", | ||
"\n", | ||
" return program_name, list_of_steps" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def read_and_write(script_dict,path,scene,program_name,item,start_step_index=None,put_id=None):\n", | ||
" if start_step_index is None:\n", | ||
" json_file = \"/activityList-graph-state-{:03d}.json\".format(script_dict['step'])\n", | ||
" else:\n", | ||
" json_file = \"/activityList-graph-state-{:03d}.json\".format(start_step_index)\n", | ||
" json_path = path+\"/\"+scene+\"/graph states/\"+program_name+json_file\n", | ||
" new_data={}\n", | ||
" with open(json_path, \"r\") as f:\n", | ||
" json_data = json.load(f)\n", | ||
" for node in json_data['nodes']:\n", | ||
" if start_step_index is None:\n", | ||
" if node['id']==int(script_dict['index1']) and node['class_name']==script_dict['obj1']:\n", | ||
" if 'GRABED' in node['states']:\n", | ||
" node['states'].remove('GRABED')\n", | ||
" else:\n", | ||
" if node['id']==int(put_id):\n", | ||
" if 'GRABED' in node['states']:\n", | ||
" node['states'].remove('GRABED')\n", | ||
" new_data=json_data\n", | ||
" output_path = \"../simulation/Output/edited jsons/\"+item+\"/\"+scene+\"/graph states/\"+program_name\n", | ||
" if os.path.exists(output_path) is False:\n", | ||
" os.makedirs(output_path)\n", | ||
" output_json_path = output_path+json_file\n", | ||
" with open(output_json_path, \"w\") as out_file:\n", | ||
" json.dump(new_data, out_file, indent=2)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data_path = \"E:/件名:納品物/4(3)の生成データ 一式/データセット2(部屋から移動あり)/\"\n", | ||
"for item in os.listdir(data_path):\n", | ||
" category_path = data_path+item\n", | ||
" for scene in os.listdir(category_path+\"/executed_program/\"):\n", | ||
" program_path = category_path+\"/executed_program/\"+scene+\"/\"\n", | ||
" for program in os.listdir(program_path):\n", | ||
" path=program_path+program\n", | ||
" program_name, script_info = get_program_file(path)\n", | ||
" keep_change_list=[]\n", | ||
" for script_dict in script_info:\n", | ||
" if script_dict['action'] in ['GRAB']:\n", | ||
" for dict in keep_change_list:\n", | ||
" if script_dict['index1'] == dict['put_id']:\n", | ||
" keep_change_list.remove(dict)\n", | ||
" # we need to change JSON file until program end or agent pick same obj again\n", | ||
" if len(keep_change_list) > 0:\n", | ||
" existing_path=category_path\n", | ||
" for dict in keep_change_list:\n", | ||
" dict['start_index'] = dict['start_index']+1\n", | ||
" read_and_write(script_dict,existing_path,scene,program_name,item, dict['start_index'],dict['put_id'])\n", | ||
" existing_path=\"../simulation/Output/edited jsons/\"+item\n", | ||
" json_file = \"/activityList-graph-state-{:03d}.json\".format(dict['start_index'])\n", | ||
" if os.path.exists(existing_path+\"/\"+scene+\"/graph states/\"+program_name+json_file) is False:\n", | ||
" print('edit new',program_name+json_file)\n", | ||
" existing_path=category_path\n", | ||
" if script_dict['action'] in ['PUTBACK','PUTOBJBACK','PUTIN']:\n", | ||
" keep_change_dict={}\n", | ||
" keep_change_dict['start_index']=script_dict['step']\n", | ||
" keep_change_dict['put_id']=script_dict['index1']\n", | ||
" keep_change_list.append(keep_change_dict)\n", | ||
" if len(keep_change_list) > 1:\n", | ||
" existing_path=\"../simulation/Output/edited jsons/\"+item\n", | ||
" json_file = \"/activityList-graph-state-{:03d}.json\".format(script_dict['step'])\n", | ||
" if os.path.exists(existing_path+\"/\"+scene+\"/graph states/\"+program_name+json_file):\n", | ||
" read_and_write(script_dict,existing_path,scene,program_name,item)\n", | ||
" else:\n", | ||
" read_and_write(script_dict,category_path,scene,program_name,item)\n", | ||
" else:\n", | ||
" read_and_write(script_dict,category_path,scene,program_name,item)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.