From e2803e096ba47927e60441ff9eddd6578d014031 Mon Sep 17 00:00:00 2001 From: you-n-g Date: Mon, 10 Feb 2025 09:03:13 +0800 Subject: [PATCH] refactor: Use file dictionaries for diff generation in feedback module (#574) --- rdagent/scenarios/data_science/dev/feedback.py | 18 +++++------------- rdagent/utils/repo/diff.py | 9 +++------ 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/rdagent/scenarios/data_science/dev/feedback.py b/rdagent/scenarios/data_science/dev/feedback.py index 7e794eb5..d4d8d617 100644 --- a/rdagent/scenarios/data_science/dev/feedback.py +++ b/rdagent/scenarios/data_science/dev/feedback.py @@ -14,7 +14,7 @@ from rdagent.scenarios.data_science.experiment.experiment import DSExperiment from rdagent.scenarios.data_science.proposal.exp_gen import DSTrace from rdagent.utils import convert2bool, remove_path_info_from_str from rdagent.utils.agent.tpl import T -from rdagent.utils.repo.diff import generate_diff +from rdagent.utils.repo.diff import generate_diff_from_dict class DSExperiment2Feedback(Experiment2Feedback): @@ -40,22 +40,14 @@ class DSExperiment2Feedback(Experiment2Feedback): # Retrieve the last experiment from the history last_exp = trace.hist[-1][0] if trace.hist else None - if last_exp: - last_workspace_path = last_exp.experiment_workspace.workspace_path - current_workspace_path = exp.experiment_workspace.workspace_path + if last_exp and last_exp.experiment_workspace and exp.experiment_workspace: # Generate a diff between the two workspaces - diff_edition = generate_diff(last_workspace_path, current_workspace_path) + last_exp_files = last_exp.experiment_workspace.file_dict + current_exp_files = exp.experiment_workspace.file_dict + diff_edition = generate_diff_from_dict(last_exp_files, current_exp_files) else: diff_edition = [] - diff_edition = [ - remove_path_info_from_str( - exp.experiment_workspace.workspace_path, - remove_path_info_from_str(last_exp.experiment_workspace.workspace_path, line), - ) - for line in diff_edition - ] - # assumption: # The feedback should focus on experiment **improving**. # Assume that all the the sota exp is based on the previous sota experiment diff --git a/rdagent/utils/repo/diff.py b/rdagent/utils/repo/diff.py index 4ecc0dc3..6c17a277 100644 --- a/rdagent/utils/repo/diff.py +++ b/rdagent/utils/repo/diff.py @@ -3,9 +3,6 @@ import fnmatch from pathlib import Path -# support two interfaces. add another one like def generate_diff(file_dict1: dict, file_dict2: dict) -> List[str]: -# the file_dict1 and file_dict2 are like {file_path: file_content}. -# the two shuld share code. def generate_diff(dir1: str, dir2: str, file_pattern: str = "*.py") -> list[str]: """ Generate a diff between two directories (from dir1 to dir2) using files that match the specified file pattern. @@ -20,8 +17,8 @@ def generate_diff(dir1: str, dir2: str, file_pattern: str = "*.py") -> list[str] list[str]: A list of diffs for files that differ between the two directories. """ - dir1_files = {f.relative_to(dir1) for f in Path(dir1).rglob("*") if f.is_file()} - dir2_files = {f.relative_to(dir2) for f in Path(dir2).rglob("*") if f.is_file()} + dir1_files = {f.relative_to(dir1) for f in Path(dir1).rglob(file_pattern) if f.is_file()} + dir2_files = {f.relative_to(dir2) for f in Path(dir2).rglob(file_pattern) if f.is_file()} all_files = dir1_files.union(dir2_files) file_dict1 = {} @@ -39,7 +36,7 @@ def generate_diff(dir1: str, dir2: str, file_pattern: str = "*.py") -> list[str] file_dict2[str(file)] = f2.read() else: file_dict2[str(file)] = "" - return generate_diff_from_dict(file_dict1, file_dict2, file_pattern=file_pattern) + return generate_diff_from_dict(file_dict1, file_dict2, file_pattern="*") def generate_diff_from_dict(file_dict1: dict, file_dict2: dict, file_pattern: str = "*.py") -> list[str]: