Files
NexQuant/rdagent/scenarios/qlib/experiment/workspace.py
T
Yuante Li 667af3e1ea feat: added running time statistics for the DS scenario experiment (#1007)
* added running time statistics for the DS scenario experiment

* update execute_ret_code to return running_time

* fix

* fix

* update describe

* add EnvResult

* update corresponding calls

* add RunningInfo class

* fix

* fix

* fix

* fix ci

* rename function name

* fix ci

* fix

* refine running_time logic

* fix ci
2025-07-02 15:11:18 +08:00

61 lines
2.4 KiB
Python

import re
from pathlib import Path
from typing import Any
import pandas as pd
from rdagent.components.coder.model_coder.conf import MODEL_COSTEER_SETTINGS
from rdagent.core.experiment import FBWorkspace
from rdagent.log import rdagent_logger as logger
from rdagent.utils.env import QlibCondaConf, QlibCondaEnv, QTDockerEnv
class QlibFBWorkspace(FBWorkspace):
def __init__(self, template_folder_path: Path, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.inject_code_from_folder(template_folder_path)
def execute(self, qlib_config_name: str = "conf.yaml", run_env: dict = {}, *args, **kwargs) -> str:
if MODEL_COSTEER_SETTINGS.env_type == "docker":
qtde = QTDockerEnv()
elif MODEL_COSTEER_SETTINGS.env_type == "conda":
qtde = QlibCondaEnv(conf=QlibCondaConf())
else:
logger.error(f"Unknown env_type: {MODEL_COSTEER_SETTINGS.env_type}")
return None, "Unknown environment type"
qtde.prepare()
# Run the Qlib backtest
execute_qlib_log = qtde.check_output(
local_path=str(self.workspace_path),
entry=f"qrun {qlib_config_name}",
env=run_env,
)
logger.log_object(execute_qlib_log, tag="Qlib_execute_log")
# TODO: We should handle the case when Docker times out.
execute_log = qtde.check_output(
local_path=str(self.workspace_path),
entry="python read_exp_res.py",
env=run_env,
)
pattern = r"(Epoch\d+: train -[0-9\.]+, valid -[0-9\.]+|best score: -[0-9\.]+ @ \d+ epoch)"
matches = re.findall(pattern, execute_qlib_log)
execute_qlib_log = "\n".join(matches)
quantitative_backtesting_chart_path = self.workspace_path / "ret.pkl"
if quantitative_backtesting_chart_path.exists():
ret_df = pd.read_pickle(quantitative_backtesting_chart_path)
logger.log_object(ret_df, tag="Quantitative Backtesting Chart")
else:
logger.error("No result file found.")
return None, execute_qlib_log
qlib_res_path = self.workspace_path / "qlib_res.csv"
if qlib_res_path.exists():
return pd.read_csv(qlib_res_path, index_col=0).iloc[:, 0], execute_qlib_log
else:
logger.error(f"File {qlib_res_path} does not exist.")
return None, execute_qlib_log