Skip to content

Commit

Permalink
Add metric_names getter to complement get_metric_names
Browse files Browse the repository at this point in the history
  • Loading branch information
xadrianzetx committed Sep 17, 2023
1 parent 68ed591 commit 7a1522f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions optuna/study/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,19 @@ def system_attrs(self) -> dict[str, Any]:

return copy.deepcopy(self._storage.get_study_system_attrs(self._study_id))

@property
@experimental_func("3.4.0")
def metric_names(self) -> list[str] | None:
"""Return metric names.
.. note::
Use :meth:`~optuna.study.Study.set_metric_names` to set the metric names first.
Returns:
A list with names for each dimension of the returned values of the objective function.
"""
return self._storage.get_study_system_attrs(self._study_id).get(_SYSTEM_ATTR_METRIC_NAMES)

def optimize(
self,
func: ObjectiveFuncType,
Expand Down
15 changes: 15 additions & 0 deletions tests/study_tests/test_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -1601,3 +1601,18 @@ def test_set_invalid_metric_names() -> None:
study = create_study(directions=["minimize", "minimize"])
with pytest.raises(ValueError):
study.set_metric_names(metric_names)


def test_get_metric_names() -> None:
study = create_study()
assert study.metric_names is None
study.set_metric_names(["v0"])
assert study.metric_names == ["v0"]
study.set_metric_names(["v1"])
assert study.metric_names == ["v1"]


def test_get_metric_names_experimental_warning() -> None:
study = create_study()
with pytest.warns(ExperimentalWarning):
study.metric_names

0 comments on commit 7a1522f

Please sign in to comment.