Skip to content

Commit

Permalink
Add template for reading experiment (#48)
Browse files Browse the repository at this point in the history
* Add template for reading experiment

* add code

* read latest exp

---------

Co-authored-by: Young <[email protected]>
  • Loading branch information
SunsetWolf and you-n-g authored Jul 9, 2024
1 parent deeb83d commit 44e5610
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
53 changes: 53 additions & 0 deletions test/utils/env_tpl/read_exp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import qlib
from mlflow.tracking import MlflowClient
from mlflow.entities import ViewType
qlib.init()

from qlib.workflow import R
# here is the documents of the https://qlib.readthedocs.io/en/latest/component/recorder.html

# TODO: list all the recorder and metrics

# Assuming you have already listed the experiments
experiments = R.list_experiments()

# Iterate through each experiment to list its recorders and metrics
experiment_name = None
for experiment in experiments:
print(f"Experiment: {experiment}")
recorders = R.list_recorders(experiment_name=experiment)
# print(recorders)
for recorder_id in recorders:
if recorder_id is not None:
experiment_name = experiment
print(f"Recorder ID: {recorder_id}")
recorder = R.get_recorder(recorder_id=recorder_id, experiment_name=experiment)
metrics = recorder.list_metrics()
print(f"Metrics: {metrics}")

# TODO: get the latest recorder

recorder_list = R.list_recorders(experiment_name="workflow")
end_times = {key: value.info['end_time'] for key, value in recorder_list.items()}
sorted_end_times = dict(sorted(end_times.items(), key=lambda item: item[1], reverse=True))

latest_recorder_id = next(iter(sorted_end_times))
print(f"Latest recorder ID: {latest_recorder_id}")

latest_recorder = R.get_recorder(experiment_name=experiment_name, recorder_id=latest_recorder_id)
print(f"Latest recorder: {latest_recorder}")

pred_df = latest_recorder.load_object("pred.pkl")
print("pred_df", pred_df)

ic_df = latest_recorder.load_object("sig_analysis/ic.pkl")
print("ic_df: ", ic_df)

ric_df = latest_recorder.load_object("sig_analysis/ric.pkl")
print("ric_df: ", ric_df)

print("list_metrics: ", latest_recorder.list_metrics())
print("IC: ", latest_recorder.list_metrics()["IC"])
print("ICIR: ", latest_recorder.list_metrics()["ICIR"])
print("Rank IC: ", latest_recorder.list_metrics()["Rank IC"])
print("Rank ICIR: ", latest_recorder.list_metrics()["Rank ICIR"])
8 changes: 6 additions & 2 deletions test/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ def test_docker(self):
qtde.prepare() # you can prepare for multiple times. It is expected to handle it correctly
# the stdout are returned as result
result = qtde.run(local_path=str(DIRNAME / "env_tpl"), entry="qrun conf.yaml")

mlrun_p = DIRNAME / "env_tpl" / "mlruns"
mlrun_p = DIRNAME / "env_tpl" / "mlruns"
self.assertTrue(mlrun_p.exists(), f"Expected output file {mlrun_p} not found")

# read experiment
result = qtde.run(local_path=str(DIRNAME / "env_tpl"), entry="python read_exp.py")
print(result)


if __name__ == "__main__":
unittest.main()

0 comments on commit 44e5610

Please sign in to comment.