Repository navigation
Conversation
trial_name_creator sets the name that Tune prints in the "Trial name" column of its console output, but the dataframe returned by ResultGrid.get_dataframe() had no column with that name. Its only identifier was trial_id, which is a unique ID and is not meant to change, so rows could not be matched to the names in the console. ExperimentAnalysis.dataframe() now adds a trial_name column set to str(trial). This is the same value that ray.tune.get_context().get_trial_name() returns, and it is the custom name from trial_name_creator when one is set. It also works for results loaded from storage, because the custom name is saved with the trial state. Signed-off-by: zakky8 <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request adds a "trial_name" column to the dataframe returned by ExperimentAnalysis and ResultGrid, which captures the name of each trial (including custom names from a trial_name_creator). It also includes corresponding unit tests and documentation updates. The review feedback suggests optimizing the trial name mapping by lazily evaluating the string representation of trials and combining dictionary updates to improve efficiency and readability.
| trial_names = {trial.trial_id: str(trial) for trial in self.trials} | ||
| for path, config in all_configs.items(): | ||
| if path in rows: | ||
| rows[path].update(config) | ||
| rows[path].update(logdir=path) | ||
| rows[path].update(logdir=path, trial_name=trial_names[path]) |
There was a problem hiding this comment.
Efficiency & Readability Improvement
Instead of pre-computing str(trial) for all trials in self.trials (which can be expensive if there are many trials), you can build a mapping of trial_id to the trial object itself. This allows you to only compute the string representation str(trial) lazily for the trials that are actually present in rows.
Additionally, the two .update() calls on rows[path] can be combined into a single call to reduce dictionary update overhead and improve readability.
| trial_names = {trial.trial_id: str(trial) for trial in self.trials} | |
| for path, config in all_configs.items(): | |
| if path in rows: | |
| rows[path].update(config) | |
| rows[path].update(logdir=path) | |
| rows[path].update(logdir=path, trial_name=trial_names[path]) | |
| trial_map = {trial.trial_id: trial for trial in self.trials} | |
| for path, config in all_configs.items(): | |
| if path in rows: | |
| rows[path].update(config, logdir=path, trial_name=str(trial_map[path])) |
Description
TuneConfig(trial_name_creator=...)sets the name that Tune shows in the "Trial name" column of its console output, butResultGrid.get_dataframe()had no column with that name. The only identifier in the dataframe wastrial_id, which is a unique ID and should not change (custom names do not have to be unique), so rows could not be matched to the names in the console.This PR adds a
trial_namecolumn inExperimentAnalysis.dataframe(), whichResultGrid.get_dataframe()returns. The value isstr(trial), the same asray.tune.get_context().get_trial_name()and the MLflow integration'strial_nametag. It is the custom name whentrial_name_creatoris set and the default name, such as<trainable>_<trial_id>, otherwise. It also works for results loaded from storage withTuner.restore(...).get_results(), because the custom name is saved with the trial state.trial_iddoes not change.With the script from the issue (16 trials, 4 names),
df.sort_values("trial_id")[["trial_id", "trial_name", "config/lr"]].head(5)now prints:Related issues
Fixes #50635
Additional information
test_result_grid_df_trial_namechecks the names fromtrial_name_creator, also afterTuner.restore(...).get_results().test_result_grid_dfnow also checks the default names. Both fail withKeyError: 'trial_name'without this change.trial_name, the column now holdsstr(trial)instead, the same waylogdirand theconfig/*columns are set today.AI-assisted contribution, as asked in
AGENTS.md:gh pr list --repo ray-project/ray --state open --search "50635 in:body"and searches fortrial_name_creator,get_dataframeandtrial_name in:titlereturn no open PRs.python/ray/setup-dev.py, in the activated venv, withRAY_TRAIN_V2_ENABLED=1as inpython/ray/tune/BUILD.bazel:python -m pytest -v --import-mode=importlib python/ray/tune/tests/test_result_grid.py python/ray/tune/tests/test_experiment_analysis.py: 31 passed, 1 failed, 11 errors.test_result_grid_apifails on Windows because it compares a/path with a\path, and the 11[cloud]cases oftest_experiment_analysis.pyerror because the fixture builds ans3://URI with\on Windows. Both fail the same way on the base commit without this change.python -m pytest -v --import-mode=importlib python/ray/tune/tests/test_commands.py: 4 passed. (test_timechecks CLI speed; in an earlier run on a busier machine it failed with a 5.97s average, and with 5.99s on the base commit.)pre-commit run <hook> --files <changed files>for ruff, black, pydoclint, check-ast, trailing-whitespace, end-of-file-fixer, check-added-large-files, python-no-log-warn and python-check-mock-methods: all pass.ci/lint/check-docstyle.sh,ci/lint/check_import_order.pyandci/lint/check_ray_repo_links.pyalso pass.