Skip to content

Commit

Permalink
fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
WinstonLiyt committed Dec 20, 2024
1 parent c2ed6e1 commit 02ddf81
Show file tree
Hide file tree
Showing 23 changed files with 156 additions and 149 deletions.
31 changes: 14 additions & 17 deletions rdagent/app/data_science/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
from typing import Any, Literal

import fire
from rdagent.scenarios.data_science.experiment.experiment import DSExperiment

from rdagent.app.data_science.conf import DS_RD_SETTING
from rdagent.components.coder.data_science.raw_data_loader import DataLoaderCoSTEER
from rdagent.components.coder.data_science.ensemble import EnsembleCoSTEER
from rdagent.components.coder.data_science.ensemble.exp import EnsembleTask
from rdagent.components.coder.data_science.feature import FeatureCoSTEER
from rdagent.components.coder.data_science.feature.exp import FeatureTask
from rdagent.components.coder.data_science.model import ModelCoSTEER
from rdagent.components.coder.data_science.ensemble import EnsembleCoSTEER
from rdagent.components.coder.data_science.model.exp import ModelTask
from rdagent.components.coder.data_science.raw_data_loader import DataLoaderCoSTEER
from rdagent.components.coder.data_science.raw_data_loader.exp import DataLoaderTask
from rdagent.components.coder.data_science.workflow import WorkflowCoSTEER
from rdagent.components.coder.data_science.workflow.exp import WorkflowTask
from rdagent.components.workflow.conf import BasePropSetting
from rdagent.components.workflow.rd_loop import NextLoopException, RDLoop
from rdagent.core.exception import FactorEmptyError, ModelEmptyError
Expand All @@ -23,15 +27,13 @@
from rdagent.core.scenario import Scenario
from rdagent.core.utils import import_class
from rdagent.log import rdagent_logger as logger
from rdagent.scenarios.data_science.proposal.exp_gen import DSTrace, DSExpGen
from rdagent.scenarios.kaggle.kaggle_crawler import download_data
from rdagent.components.coder.data_science.ensemble.exp import EnsembleTask
from rdagent.components.coder.data_science.feature.exp import FeatureTask
from rdagent.components.coder.data_science.model.exp import ModelTask
from rdagent.components.coder.data_science.raw_data_loader.exp import DataLoaderTask
from rdagent.components.coder.data_science.workflow.exp import WorkflowTask
from rdagent.scenarios.data_science.dev.runner import DSRunner
from rdagent.scenarios.data_science.dev.feedback import DSExperiment2Feedback
from rdagent.scenarios.data_science.dev.runner import DSRunner
from rdagent.scenarios.data_science.experiment.experiment import DSExperiment
from rdagent.scenarios.data_science.proposal.exp_gen import DSExpGen, DSTrace
from rdagent.scenarios.kaggle.kaggle_crawler import download_data


class DataScienceRDLoop(RDLoop):
skip_loop_error = (NextLoopException,)

Expand Down Expand Up @@ -103,7 +105,7 @@ def feedback(self, prev_out: dict[str, Any]):
prev_out["running"], prev_out["direct_exp_gen"].hypothesis, self.trace
)
self.trace.hist.append((prev_out["direct_exp_gen"].hypothesis, prev_out["running"], feedback))


def main(path=None, step_n=None, competition=None):
"""
Expand All @@ -129,8 +131,3 @@ def main(path=None, step_n=None, competition=None):

if __name__ == "__main__":
fire.Fire(main)





39 changes: 11 additions & 28 deletions rdagent/components/coder/data_science/ensemble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@
from rdagent.components.coder.CoSTEER.knowledge_management import (
CoSTEERQueriedKnowledge,
)
from rdagent.components.coder.data_science.ensemble.eval import (
EnsembleCoSTEEREvaluator,
)
from rdagent.components.coder.data_science.ensemble.eval import EnsembleCoSTEEREvaluator
from rdagent.components.coder.data_science.ensemble.exp import EnsembleTask
from rdagent.core.experiment import FBWorkspace
from rdagent.core.scenario import Scenario
from rdagent.oai.llm_utils import APIBackend
from rdagent.utils.agent.tpl import T

from rdagent.core.experiment import FBWorkspace


class EnsembleMultiProcessEvolvingStrategy(MultiProcessEvolvingStrategy):
def implement_one_task(
Expand All @@ -42,7 +39,7 @@ def implement_one_task(
) -> dict[str, str]:
# Get task information for knowledge querying
ensemble_information_str = target_task.get_task_information()

# Query knowledge
queried_similar_successful_knowledge = (
queried_knowledge.task_to_similar_task_successful_knowledge[ensemble_information_str]
Expand All @@ -60,24 +57,22 @@ def implement_one_task(
system_prompt = T(".prompts:ensemble_coder.system").r(
competition_info=competition_info,
queried_similar_successful_knowledge=queried_similar_successful_knowledge,
queried_former_failed_knowledge=queried_former_failed_knowledge[0] if queried_former_failed_knowledge else None
)
user_prompt = T(".prompts:ensemble_coder.user").r(
ensemble_spec=workspace.code_dict["spec/ensemble.md"]
queried_former_failed_knowledge=(
queried_former_failed_knowledge[0] if queried_former_failed_knowledge else None
),
)
user_prompt = T(".prompts:ensemble_coder.user").r(ensemble_spec=workspace.code_dict["spec/ensemble.md"])

ensemble_code = json.loads(
APIBackend().build_messages_and_create_chat_completion(
user_prompt=user_prompt,
system_prompt=system_prompt,
json_mode=True
user_prompt=user_prompt, system_prompt=system_prompt, json_mode=True
)
)["code"]

return {
"ensemble.py": ensemble_code,
}

def assign_code_list_to_evo(self, code_list: list[dict[str, str]], evo):
"""
Assign the code list to the evolving item.
Expand All @@ -102,19 +97,7 @@ def __init__(
*args,
**kwargs,
) -> None:
eva = CoSTEERMultiEvaluator(
EnsembleCoSTEEREvaluator(scen=scen), scen=scen
)
eva = CoSTEERMultiEvaluator(EnsembleCoSTEEREvaluator(scen=scen), scen=scen)
es = EnsembleMultiProcessEvolvingStrategy(scen=scen, settings=CoSTEER_SETTINGS)

super().__init__(
*args,
settings=CoSTEER_SETTINGS,
eva=eva,
es=es,
evolving_version=2,
scen=scen,
**kwargs
)


super().__init__(*args, settings=CoSTEER_SETTINGS, eva=eva, es=es, evolving_version=2, scen=scen, **kwargs)
2 changes: 1 addition & 1 deletion rdagent/components/coder/data_science/ensemble/conf.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Configuration file for ensemble component
# Currently empty as no specific configuration is needed
# Currently empty as no specific configuration is needed
18 changes: 10 additions & 8 deletions rdagent/components/coder/data_science/ensemble/eval.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from dataclasses import dataclass
from pathlib import Path

import numpy as np

from rdagent.components.coder.CoSTEER.evaluators import (
Expand All @@ -12,12 +13,13 @@
from rdagent.core.experiment import FBWorkspace, Task
from rdagent.oai.llm_utils import APIBackend
from rdagent.utils.agent.tpl import T
from rdagent.utils.env import DSDockerConf, DockerEnv
from rdagent.utils.env import DockerEnv, DSDockerConf

DIRNAME = Path(__file__).absolute().resolve().parent

EnsembleEvalFeedback = CoSTEERSingleFeedback


class EnsembleCoSTEEREvaluator(CoSTEEREvaluator):
def evaluate(
self,
Expand All @@ -27,10 +29,12 @@ def evaluate(
queried_knowledge: QueriedKnowledge = None,
**kwargs,
) -> EnsembleEvalFeedback:

target_task_information = target_task.get_task_information()
if (queried_knowledge is not None and
target_task_information in queried_knowledge.success_task_to_knowledge_dict):
if (
queried_knowledge is not None
and target_task_information in queried_knowledge.success_task_to_knowledge_dict
):
return queried_knowledge.success_task_to_knowledge_dict[target_task_information].feedback
elif queried_knowledge is not None and target_task_information in queried_knowledge.failed_task_info_set:
return EnsembleEvalFeedback(
Expand All @@ -51,7 +55,5 @@ def evaluate(
system_prompt = T(".prompts:ensemble_eval.system").r(test_code=test_code)
user_prompt = T(".prompts:ensemble_eval.user").r(stdout=stdout)

resp = APIBackend().build_messages_and_create_chat_completion(
user_prompt, system_prompt, json_mode=True
)
return EnsembleEvalFeedback(**json.loads(resp))
resp = APIBackend().build_messages_and_create_chat_completion(user_prompt, system_prompt, json_mode=True)
return EnsembleEvalFeedback(**json.loads(resp))
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
- Have correct shapes for inputs and outputs
- Use validation data appropriately
"""
import numpy as np

import logging

import numpy as np
from ensemble import ens_and_decision

# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

# Create test data
n_models = 3
Expand All @@ -24,17 +26,17 @@
# Run ensemble
try:
final_predictions = ens_and_decision(test_pred_l, val_pred_l, val_label)

# Check shape
assert final_predictions.shape == (n_samples, 1), "Wrong output shape"

# Check binary values
assert np.all(np.isin(final_predictions, [0, 1])), "Predictions must be binary (0 or 1)"

logging.info("Ensemble test passed successfully.")
logging.info(f"Output shape: {final_predictions.shape}")
logging.info(f"Unique values in predictions: {np.unique(final_predictions)}")

except Exception as e:
logging.error(f"Test failed: {str(e)}")
raise
raise
27 changes: 16 additions & 11 deletions rdagent/components/coder/data_science/ensemble/test.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
"""
Helper functions for testing the ensemble coder(CoSTEER-based) component.
"""

import sys
from pathlib import Path

from rdagent.components.coder.data_science.ensemble import EnsembleCoSTEER
from rdagent.components.coder.data_science.ensemble.exp import EnsembleTask
from rdagent.scenarios.data_science.scen import DataScienceScen
from rdagent.scenarios.data_science.experiment.experiment import DSExperiment
from rdagent.scenarios.data_science.scen import DataScienceScen

# Add the competition folder to path
COMPETITION_PATH = Path(__file__).parent.parent.parent.parent.parent / "scenarios" / "kaggle" / "tpl_ex" / "aerial-cactus-identification"
COMPETITION_PATH = (
Path(__file__).parent.parent.parent.parent.parent
/ "scenarios"
/ "kaggle"
/ "tpl_ex"
/ "aerial-cactus-identification"
)
sys.path.append(str(COMPETITION_PATH))

EnsembleExperiment = DSExperiment
EnsembleExperiment = DSExperiment


def load_ensemble_spec():
spec_path = COMPETITION_PATH / "spec" / "ensemble.md"
with open(spec_path, 'r') as f:
with open(spec_path, "r") as f:
return f.read()


Expand All @@ -31,15 +39,12 @@ def develop_ensemble():
# Create the ensemble task with actual data context and specification
task = EnsembleTask(
name="EnsembleTask",
description=
"""
description="""
Implement ensemble and decision making for model predictions.
"""
""",
)

exp = EnsembleExperiment(
sub_tasks=[task]
)
exp = EnsembleExperiment(sub_tasks=[task])

# Injecting the corresponding specification
exp.experiment_workspace.inject_code(**{"spec/ensemble.md": ensemble_spec})
Expand All @@ -50,4 +55,4 @@ def develop_ensemble():


if __name__ == "__main__":
develop_ensemble()
develop_ensemble()
6 changes: 4 additions & 2 deletions rdagent/components/coder/data_science/feature/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ def implement_one_task(
)

# 2. code
system_prompt = T(".prompts:feature.system").r(queried_similar_successful_knowledge=queried_similar_successful_knowledge,
queried_former_failed_knowledge=queried_former_failed_knowledge[0])
system_prompt = T(".prompts:feature.system").r(
queried_similar_successful_knowledge=queried_similar_successful_knowledge,
queried_former_failed_knowledge=queried_former_failed_knowledge[0],
)
user_prompt = T(".prompts:feature.user").r(
feature_spec=workspace.code_dict["spec/feature.md"],
latest_code=workspace.code_dict.get("feat01.py"),
Expand Down
4 changes: 1 addition & 3 deletions rdagent/components/coder/data_science/model/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def evaluate(
implementation.inject_code(**{fname: test_code})
stdout = implementation.execute(env=de, entry=f"python {fname}")
system_prompt = T(".prompts:model_eval.system").r(
test_code=test_code,
scenario="No scenario information yet.",
spec=implementation.code_dict["spec/model.md"]
test_code=test_code, scenario="No scenario information yet.", spec=implementation.code_dict["spec/model.md"]
)
user_prompt = T(".prompts:model_eval.user").r(
stdout=stdout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
print("The first execution begins.\n")
# Call model_workflow
val_pred, test_pred, hypers = model_workflow(
X=train_X, y=train_y, val_X=val_X, val_y=val_y, test_X=None,
X=train_X,
y=train_y,
val_X=val_X,
val_y=val_y,
test_X=None,
)
# val_pred = np.random.rand(8, 1)
# test_pred = np.random.rand(8, 1)
Expand Down
22 changes: 14 additions & 8 deletions rdagent/components/coder/data_science/raw_data_loader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ def implement_one_task(
if queried_knowledge is not None
else []
)

# 1. specifications
# TODO: Why is queried_former_failed_knowledge[0] used here?
system_prompt = T(".prompts:spec.system").r(competition_info=competition_info,
queried_similar_successful_knowledge=queried_similar_successful_knowledge,
queried_former_failed_knowledge=queried_former_failed_knowledge[0])
data_loader_prompt = T(".prompts:spec.user.data_loader").r(latest_spec=workspace.code_dict.get("spec/data_loader.md"))
system_prompt = T(".prompts:spec.system").r(
competition_info=competition_info,
queried_similar_successful_knowledge=queried_similar_successful_knowledge,
queried_former_failed_knowledge=queried_former_failed_knowledge[0],
)
data_loader_prompt = T(".prompts:spec.user.data_loader").r(
latest_spec=workspace.code_dict.get("spec/data_loader.md")
)
feature_prompt = T(".prompts:spec.user.feature").r(latest_spec=workspace.code_dict.get("spec/feature.md"))
model_prompt = T(".prompts:spec.user.model").r(latest_spec=workspace.code_dict.get("spec/model.md"))
ensemble_prompt = T(".prompts:spec.user.ensemble").r(latest_spec=workspace.code_dict.get("spec/ensemble.md"))
Expand All @@ -97,10 +101,12 @@ def implement_one_task(
# 2. code
system_prompt = T(".prompts:data_loader_coder.system").r(
queried_similar_successful_knowledge=queried_similar_successful_knowledge,
queried_former_failed_knowledge=queried_former_failed_knowledge[0])
queried_former_failed_knowledge=queried_former_failed_knowledge[0],
)
user_prompt = T(".prompts:data_loader_coder.user").r(
competition_info=competition_info, data_loader_spec=data_loader_spec,
latest_code=workspace.code_dict.get("load_data.py")
competition_info=competition_info,
data_loader_spec=data_loader_spec,
latest_code=workspace.code_dict.get("load_data.py"),
)

data_loader_code = json.loads(
Expand Down
8 changes: 6 additions & 2 deletions rdagent/components/coder/data_science/raw_data_loader/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
CoSTEEREvaluator,
CoSTEERSingleFeedback,
)
from rdagent.components.coder.CoSTEER.knowledge_management import CoSTEERQueriedKnowledgeV2
from rdagent.components.coder.CoSTEER.knowledge_management import (
CoSTEERQueriedKnowledgeV2,
)
from rdagent.core.evaluation import Feedback
from rdagent.core.experiment import FBWorkspace, Task, Workspace
from rdagent.oai.llm_utils import APIBackend
Expand Down Expand Up @@ -56,7 +58,9 @@ def evaluate(
implementation.inject_code(**{fname: test_code})
stdout = implementation.execute(env=de, entry=f"python {fname}")

system_prompt = T(".prompts:data_loader_eval.system").r(test_code=test_code, code=implementation.code_dict["load_data.py"])
system_prompt = T(".prompts:data_loader_eval.system").r(
test_code=test_code, code=implementation.code_dict["load_data.py"]
)
user_prompt = T(".prompts:data_loader_eval.user").r(stdout=stdout)

resp = APIBackend().build_messages_and_create_chat_completion(user_prompt, system_prompt, json_mode=True)
Expand Down
Loading

0 comments on commit 02ddf81

Please sign in to comment.