| 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 = { |
| |
| "S_0D": "datasets/Simple_Tasks/0D_Component_Counting", |
| "S_1D": "datasets/Simple_Tasks/1D_Simplex_Counting", |
| "S_Modification": "datasets/Simple_Tasks/Component_Reduction", |
| |
| |
| "M_Merge": "datasets/Medium_Tasks/Component_Merge_Time", |
| "M_Birth": "datasets/Medium_Tasks/Simplex_Birth_Time", |
| "M_Filtration": "datasets/Medium_Tasks/Component_Count_Under_Filtration", |
| |
| |
| "H_Selection": "datasets/Hard_Tasks/Optimal_Filtration_Selection", |
| "H_Generation": "datasets/Hard_Tasks/Non-Uniform_Filtration_Generation", |
| |
| |
| "R_Selection": "datasets/Real_World_Tasks/Filtration_Selection_for_Classification", |
| "R_Generation": "datasets/Real_World_Tasks/Filtration_Sequence_Generation_for_Classification", |
| "R_Directly": "datasets/Real_World_Tasks/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 |
| |
| |
| self.llm_caller = LLMCaller(model_name) |
| |
| |
| self.graph_embedder = GraphEmbedder(self.task_name) |
| self.extractor = AnswerExtractor(self.task_name) |
| self.evaluator = Evaluator(self.task_name) |
| |
| |
| self.dataset = self._load_dataset() |
| |
| def _load_dataset(self): |
| """Load dataset based on task name""" |
| dataset_path = TASK_DATASET_MAPPING[self.task_name] |
| data = {} |
| |
| |
| if not os.path.exists(dataset_path): |
| raise FileNotFoundError(f"Dataset path not found: {dataset_path}") |
| |
| |
| 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 |
| """ |
| |
| |
| prompt = self.graph_embedder.embed_graph(graph_data) |
| |
| |
| response = self.llm_caller.call(prompt) |
| |
| |
| 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"]: |
| |
| for file_idx, (file_name, file_data) in enumerate(self.dataset.items()): |
| print(f"\nProcessing file {file_idx + 1}/{len(self.dataset)}") |
| |
| |
| graph_datas = [] |
| answers = [] |
| for idx, row in file_data.iterrows(): |
| |
| |
| print(f"Processing graph {idx + 1}/{len(file_data)} in file {file_idx + 1}") |
| |
| |
| 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"]: |
| |
| for file_idx, (file_name, file_data) in enumerate(self.dataset.items()): |
| print(f"\nProcessing file {file_idx + 1}/{len(self.dataset)}") |
| |
| |
| 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()) |
| |
| |
| graph_datas = [] |
| answers = [] |
| pair_count = 0 |
| for pair_id, pair_data in pairs.items(): |
| if len(pair_data) != 2: |
| continue |
| |
| |
| |
| |
| 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"]: |
| |
| for file_idx, (file_name, file_data) in enumerate(self.dataset.items()): |
| print(f"\nProcessing file {file_idx + 1}/{len(self.dataset)}") |
| |
| |
| 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()) |
| |
| |
| graph_datas = [] |
| answers = [] |
| group_count = 0 |
| for group_id, group_data in groups.items(): |
| if len(group_data) != 4: |
| continue |
| |
| |
| |
| |
| 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 |
|
|