import os from evaluate_code.load_datasets import load_data from evaluate_code.graph_embed import GraphEmbedder from evaluate_code.extract import AnswerExtractor from evaluate_code.llm_api import LLMCaller from evaluate_code.evaluate import Evaluator TASK_DATASET_MAPPING = { # Simple Tasks "S_0D": "datasets/Simple_Tasks/0D_Component_Counting", # 0D component counting "S_1D": "datasets/Simple_Tasks/1D_Simplex_Counting", # 1D simplex counting "S_Modification": "datasets/Simple_Tasks/Component_Reduction", # Component reduction task # Medium Tasks "M_Merge": "datasets/Medium_Tasks/Component_Merge_Time", # Persistent homology calculation "M_Birth": "datasets/Medium_Tasks/Simplex_Birth_Time", # Birth time calculation "M_Filtration": "datasets/Medium_Tasks/Component_Count_Under_Filtration", # Filtration feature counting # Hard Tasks "H_Selection": "datasets/Hard_Tasks/Optimal_Filtration_Selection", # Filtration method selection "H_Generation": "datasets/Hard_Tasks/Non-Uniform_Filtration_Generation", # Filtration value selection # Real World Tasks "R_Selection": "datasets/Real_World_Tasks/Filtration_Selection_for_Classification", # Real data filtration method selection "R_Generation": "datasets/Real_World_Tasks/Filtration_Sequence_Generation_for_Classification", # Real data filtration value selection "R_Directly": "datasets/Real_World_Tasks/Direct_Classification" # Direct classification } class LLMEvaluator: def __init__(self, task_name, model_name="gpt-4o"): """ Initialize LLM evaluator Args: dataset_name (str): Name of the dataset to evaluate model_name (str): Name of the model to use """ self.task_name = task_name # Initialize LLM caller self.llm_caller = LLMCaller(model_name) # Initialize graph embedder and evaluator self.graph_embedder = GraphEmbedder(self.task_name) self.extractor = AnswerExtractor(self.task_name) self.evaluator = Evaluator(self.task_name) # Load dataset self.dataset = self._load_dataset() def _load_dataset(self): """Load dataset based on task name""" dataset_path = TASK_DATASET_MAPPING[self.task_name] data = {} # Check if path exists if not os.path.exists(dataset_path): raise FileNotFoundError(f"Dataset path not found: {dataset_path}") # Load all files in the dataset directory for file_name in os.listdir(dataset_path): file_path = os.path.join(dataset_path, file_name) if os.path.isfile(file_path): file_data = load_data(file_path) data[file_name] = file_data return data def process_single_data(self, graph_data): """ Process a single graph through the complete pipeline: 1. Generate prompt 2. Get LLM response 3. Extract and evaluate answer Args: graph_data: A single graph data object Returns: dict: Dictionary containing: - prompt: Generated prompt - response: Raw LLM response - extracted_answer: Processed answer - evaluation: Evaluation results """ # try: # Step 1: Generate prompt for single graph prompt = self.graph_embedder.embed_graph(graph_data) # Step 2: Get LLM response response = self.llm_caller.call(prompt) # Step 3: Extract answer extracted_answer = self.extractor.extract_answers(response) return extracted_answer def process_dataset(self): """ Process all graphs in all parquet files in the dataset. Returns: list: List of results for each file, where each file's results is a list of results for each graph """ all_results = {} if self.task_name in ["S_0D", "S_1D", "S_Modification", "M_Merge", "M_Birth", "M_Filtration"]: # Process each parquet file in the dataset for file_idx, (file_name, file_data) in enumerate(self.dataset.items()): print(f"\nProcessing file {file_idx + 1}/{len(self.dataset)}") # Process each graph in the current file (DataFrame) graph_datas = [] answers = [] for idx, row in file_data.iterrows(): # if idx >= 3: # Only process first 3 graphs # break print(f"Processing graph {idx + 1}/{len(file_data)} in file {file_idx + 1}") # Convert DataFrame row to dict graph_data = row.to_dict() graph_datas.append(graph_data) answer = self.process_single_data(graph_data) answers.append(answer) evaluation = self.evaluator.evaluate(graph_datas, answers) all_results[file_name] = evaluation return all_results elif self.task_name in ["H_Selection", "H_Generation"]: # Process each parquet file in the dataset for file_idx, (file_name, file_data) in enumerate(self.dataset.items()): print(f"\nProcessing file {file_idx + 1}/{len(self.dataset)}") # Group data by pair_id pairs = {} for idx, row in file_data.iterrows(): pair_id = row['pair_id'] if pair_id not in pairs: pairs[pair_id] = [] pairs[pair_id].append(row.to_dict()) # Process each pair graph_datas = [] answers = [] pair_count = 0 for pair_id, pair_data in pairs.items(): if len(pair_data) != 2: # Skip if pair is incomplete continue # if pair_count >= 3: # Only process first 3 pairs # break # print(f"Processing pair {pair_id}") # Sort by graph_position to ensure correct order pair_data.sort(key=lambda x: x['graph_position']) graph_datas.append(pair_data) answer = self.process_single_data(pair_data) answers.append(answer) pair_count += 1 evaluation = self.evaluator.evaluate(graph_datas, answers) all_results[file_name] = evaluation return all_results elif self.task_name in ["R_Selection", "R_Generation"]: # Process each parquet file in the dataset for file_idx, (file_name, file_data) in enumerate(self.dataset.items()): print(f"\nProcessing file {file_idx + 1}/{len(self.dataset)}") # Group data by group_id groups = {} for idx, row in file_data.iterrows(): group_id = row['group_id'] if group_id not in groups: groups[group_id] = [] groups[group_id].append(row.to_dict()) # Process each group graph_datas = [] answers = [] group_count = 0 for group_id, group_data in groups.items(): if len(group_data) != 4: # Skip if group is incomplete continue # if group_count >= 3: # Only process first 3 groups # break # Sort by graph_position to ensure correct order group_data.sort(key=lambda x: x['graph_position']) graph_datas.append(group_data) answer = self.process_single_data(group_data) answers.append(answer) group_count += 1 evaluation = self.evaluator.evaluate(graph_datas, answers) all_results[file_name] = evaluation return all_results