Upload deal_allytree.py with huggingface_hub
Browse files- deal_allytree.py +81 -0
deal_allytree.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
def read_json(file_path):
|
| 5 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 6 |
+
data = json.load(file)
|
| 7 |
+
return data
|
| 8 |
+
|
| 9 |
+
def write_json(file_path, data):
|
| 10 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
| 11 |
+
json.dump(data, file, ensure_ascii=False, indent=4)
|
| 12 |
+
|
| 13 |
+
data = read_json('/inspire/hdd/ws-ba572160-47f8-4ca1-984e-d6bcdeb95dbb/a100-maybe/wangbaode/NIPS_2025/Agent/Dataset/android_env/intermediate_data_8k.json')
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def parse_node(node, nodes, depth=0, parent_bounds=None):
|
| 18 |
+
# 提取节点信息
|
| 19 |
+
class_name = node.get("className", "Unknown")
|
| 20 |
+
# 获取 viewIdResourceName 并简化输出,只保留资源名部分
|
| 21 |
+
view_id = node.get("viewIdResourceName", "")
|
| 22 |
+
simplified_view_id = view_id.split(":")[-1] if view_id else ""
|
| 23 |
+
simplified_view_id = ' '.join(simplified_view_id.split('_'))
|
| 24 |
+
simplified_view_id = simplified_view_id.replace('id/','')
|
| 25 |
+
|
| 26 |
+
bounds = node.get("boundsInScreen", {})
|
| 27 |
+
# 简化 Bounds 表示
|
| 28 |
+
bounds_str = f"[{bounds.get('left', 0)}, {bounds.get('top', 0)}, {bounds.get('right', 0)}, {bounds.get('bottom', 0)}]"
|
| 29 |
+
|
| 30 |
+
# 如果与父节点相同,不显示 Bounds,避免重复
|
| 31 |
+
if parent_bounds and bounds == parent_bounds:
|
| 32 |
+
bounds_str = ""
|
| 33 |
+
|
| 34 |
+
# 构造当前节点的字符串,只保留简化后的视图ID
|
| 35 |
+
node_str = f"{' ' * depth}└── {simplified_view_id} {f'({bounds_str})' if bounds_str else ''}"
|
| 36 |
+
|
| 37 |
+
# 查找子节点并递归解析
|
| 38 |
+
children_str = ""
|
| 39 |
+
if "childIds" in node:
|
| 40 |
+
children = [child for child in nodes if 'uniqueId' in child and child['uniqueId'] in node['childIds']]
|
| 41 |
+
for child in children:
|
| 42 |
+
children_str += "\n" + parse_node(child, nodes, depth + 1, bounds)
|
| 43 |
+
|
| 44 |
+
return node_str + children_str
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def parse_tree(window_data):
|
| 48 |
+
# 获取树的根节点(Window信息)
|
| 49 |
+
window_info = window_data['windows'][0]
|
| 50 |
+
window_type = window_info.get('windowType', 'Unknown')
|
| 51 |
+
root_node_str = f"Window ({window_type})"
|
| 52 |
+
nodes = window_info['tree']['nodes']
|
| 53 |
+
root_node_str += "\n" + parse_node(nodes[0], nodes, 1)
|
| 54 |
+
return root_node_str
|
| 55 |
+
|
| 56 |
+
json_dict = json.loads(data[0]['accessibility_trees'][0])
|
| 57 |
+
# 调用函数生成树形结构
|
| 58 |
+
tree_output = parse_tree(json_dict)
|
| 59 |
+
|
| 60 |
+
# # 输出树形结构
|
| 61 |
+
# print(tree_output)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
from tqdm import tqdm
|
| 65 |
+
|
| 66 |
+
for i in tqdm(data):
|
| 67 |
+
accessibility_trees = []
|
| 68 |
+
for trees in i['accessibility_trees']:
|
| 69 |
+
if len(trees) < 20:
|
| 70 |
+
continue
|
| 71 |
+
|
| 72 |
+
json_dict = json.loads(trees)
|
| 73 |
+
tree_output = parse_tree(json_dict)
|
| 74 |
+
# print(tree_output)
|
| 75 |
+
accessibility_trees.append(tree_output)
|
| 76 |
+
|
| 77 |
+
i['accessibility_trees'] = accessibility_trees
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
write_json('/inspire/hdd/ws-ba572160-47f8-4ca1-984e-d6bcdeb95dbb/a100-maybe/wangbaode/NIPS_2025/Agent/Dataset/android_env/intermediate_data.json', data)
|