AndControl / deal_allytree.py
Wendy-Fly's picture
Upload deal_allytree.py with huggingface_hub
0197974 verified
import json
def read_json(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
return data
def write_json(file_path, data):
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
data = read_json('/inspire/hdd/ws-ba572160-47f8-4ca1-984e-d6bcdeb95dbb/a100-maybe/wangbaode/NIPS_2025/Agent/Dataset/android_env/intermediate_data_8k.json')
def parse_node(node, nodes, depth=0, parent_bounds=None):
# 提取节点信息
class_name = node.get("className", "Unknown")
# 获取 viewIdResourceName 并简化输出,只保留资源名部分
view_id = node.get("viewIdResourceName", "")
simplified_view_id = view_id.split(":")[-1] if view_id else ""
simplified_view_id = ' '.join(simplified_view_id.split('_'))
simplified_view_id = simplified_view_id.replace('id/','')
bounds = node.get("boundsInScreen", {})
# 简化 Bounds 表示
bounds_str = f"[{bounds.get('left', 0)}, {bounds.get('top', 0)}, {bounds.get('right', 0)}, {bounds.get('bottom', 0)}]"
# 如果与父节点相同,不显示 Bounds,避免重复
if parent_bounds and bounds == parent_bounds:
bounds_str = ""
# 构造当前节点的字符串,只保留简化后的视图ID
node_str = f"{' ' * depth}└── {simplified_view_id} {f'({bounds_str})' if bounds_str else ''}"
# 查找子节点并递归解析
children_str = ""
if "childIds" in node:
children = [child for child in nodes if 'uniqueId' in child and child['uniqueId'] in node['childIds']]
for child in children:
children_str += "\n" + parse_node(child, nodes, depth + 1, bounds)
return node_str + children_str
def parse_tree(window_data):
# 获取树的根节点(Window信息)
window_info = window_data['windows'][0]
window_type = window_info.get('windowType', 'Unknown')
root_node_str = f"Window ({window_type})"
nodes = window_info['tree']['nodes']
root_node_str += "\n" + parse_node(nodes[0], nodes, 1)
return root_node_str
json_dict = json.loads(data[0]['accessibility_trees'][0])
# 调用函数生成树形结构
tree_output = parse_tree(json_dict)
# # 输出树形结构
# print(tree_output)
from tqdm import tqdm
for i in tqdm(data):
accessibility_trees = []
for trees in i['accessibility_trees']:
if len(trees) < 20:
continue
json_dict = json.loads(trees)
tree_output = parse_tree(json_dict)
# print(tree_output)
accessibility_trees.append(tree_output)
i['accessibility_trees'] = accessibility_trees
write_json('/inspire/hdd/ws-ba572160-47f8-4ca1-984e-d6bcdeb95dbb/a100-maybe/wangbaode/NIPS_2025/Agent/Dataset/android_env/intermediate_data.json', data)