Skip to content

[tune] Add a trial_name column to ResultGrid.get_dataframe() - #68647

Open
zakky8 wants to merge 1 commit into
ray-project:masterfrom
zakky8:fix/50635-trial-name-dataframe
Open

zakky8 wants to merge 1 commit into
ray-project:masterfrom
zakky8:fix/50635-trial-name-dataframe

Conversation

@zakky8

@zakky8 zakky8 commented Nov 29, 2026

Copy link
Copy Markdown

Description

TuneConfig(trial_name_creator=...) sets the name that Tune shows in the "Trial name" column of its console output, but ResultGrid.get_dataframe() had no column with that name. The only identifier in the dataframe was trial_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_name column in ExperimentAnalysis.dataframe(), which ResultGrid.get_dataframe() returns. The value is str(trial), the same as ray.tune.get_context().get_trial_name() and the MLflow integration's trial_name tag. It is the custom name when trial_name_creator is set and the default name, such as <trainable>_<trial_id>, otherwise. It also works for results loaded from storage with Tuner.restore(...).get_results(), because the custom name is saved with the trial state. trial_id does 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:

   trial_id trial_name  config/lr
c8627_00000       exp1     0.0010
c8627_00001       exp2     0.0100
c8627_00002       exp3     0.1000
c8627_00003       exp4     0.0001
c8627_00004       exp1     0.0010

Related issues

Fixes #50635

Additional information

  • Tests: the new test_result_grid_df_trial_name checks the names from trial_name_creator, also after Tuner.restore(...).get_results(). test_result_grid_df now also checks the default names. Both fail with KeyError: 'trial_name' without this change.
  • If a trainable reports its own metric named trial_name, the column now holds str(trial) instead, the same way logdir and the config/* columns are set today.

AI-assisted contribution, as asked in AGENTS.md:

  • AI assistance was used. An AI coding assistant was used to investigate the issue, write the change and tests, and draft this description. I reviewed every changed line and ran the tests below locally.
  • Not a duplicate: trial_name_creator does not change name in get_dataframe() #50635 has no comments, no assignee and no linked PR. gh pr list --repo ray-project/ray --state open --search "50635 in:body" and searches for trial_name_creator, get_dataframe and trial_name in:title return no open PRs.
  • Tests run on Windows 11 with Python 3.12, the nightly wheel built from the base commit and python/ray/setup-dev.py, in the activated venv, with RAY_TRAIN_V2_ENABLED=1 as in python/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_api fails on Windows because it compares a / path with a \ path, and the 11 [cloud] cases of test_experiment_analysis.py error because the fixture builds an s3:// 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_time checks 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.py and ci/lint/check_ray_repo_links.py also pass.

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]>
@zakky8
zakky8 requested a review from a team as a code owner November 29, 2026 09:57

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +407 to +411
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])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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]))

@ray-gardener ray-gardener Bot added tune Tune-related issues community-contribution Contributed by the community labels Nov 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community tune Tune-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

trial_name_creator does not change name in get_dataframe()

1 participant