MCPcopy Create free account
hub / github.com/InternScience/InternAgent / vis_tree

Function vis_tree

internagent/vis_tree.py:38–179  ·  view source on GitHub ↗

Visualize hypothesis tree from a JSON file. Parameters: ----------- json_file : str Path to the JSON file containing hypothesis data output_pdf : str Path for the output PDF file Returns: -------- str or None Path to the generated PDF file,

(json_file, output_pdf)

Source from the content-addressed store, hash-verified

36 print(f"Warning: Child ID {child_id} referenced by {hyp_id} not found in map.")
37
38def vis_tree(json_file, output_pdf):
39 """
40 Visualize hypothesis tree from a JSON file.
41
42 Parameters:
43 -----------
44 json_file : str
45 Path to the JSON file containing hypothesis data
46 output_pdf : str
47 Path for the output PDF file
48
49 Returns:
50 --------
51 str or None
52 Path to the generated PDF file, or None if visualization failed
53 """
54 # --- 1. Load and Prepare Data ---
55 try:
56 with open(json_file, 'r', encoding='utf-8') as f:
57 data = json.load(f)
58 except FileNotFoundError:
59 print(f"Error: JSON file '{json_file}' not found.")
60 return None
61 except json.JSONDecodeError:
62 print(f"Error: Could not decode JSON from '{json_file}'.")
63 return None
64
65 hypotheses = data.get('hypotheses', [])
66 if not hypotheses:
67 print("No hypotheses found in the JSON file.")
68 return None
69
70 # Create maps for easy lookup
71 hyp_map = {hyp['id']: hyp for hyp in hypotheses}
72 child_map = defaultdict(list)
73 roots = []
74
75 # Identify roots (iteration 0) and build child map
76 for hyp in hypotheses:
77 parent_id = hyp.get('parent_id')
78 if parent_id:
79 # Check if parent exists before adding to child_map
80 if parent_id in hyp_map:
81 child_map[parent_id].append(hyp['id'])
82 else:
83 print(f"Warning: Parent ID {parent_id} for hypothesis {hyp['id']} not found. Skipping child mapping.")
84 # Consider iteration 0 as roots even if they have a parent_id (though unlikely based on structure)
85 if hyp.get('iteration') == 0:
86 # Check if this root already identified by null parent_id, avoid duplicates
87 if not parent_id and hyp['id'] not in roots:
88 roots.append(hyp['id'])
89 elif parent_id and hyp['id'] not in roots:
90 # If iteration 0 has a parent_id, still treat as a root for visualization start
91 print(f"Info: Treating Iteration 0 hypothesis {hyp['id']} with parent {parent_id} as a visualization root.")
92 roots.append(hyp['id'])
93 elif not parent_id and hyp['id'] not in roots: # Catch roots that might not have iteration field or are not 0
94 print(f"Info: Treating hypothesis {hyp['id']} with null parent_id as a root.")
95 roots.append(hyp['id'])

Callers 2

generate_ideasMethod · 0.90
mainFunction · 0.85

Calls 6

build_graph_recursiveFunction · 0.85
writeMethod · 0.80
loadMethod · 0.45
getMethod · 0.45
closeMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected