Skip to content

Commit

Permalink
Merge pull request #2112 from HideakiImamura/refactoring/mo-plot-contour
Browse files Browse the repository at this point in the history
Raise `ValueError` if `target` is None and `study` is for multi-objective optimization for `plot_contour`
  • Loading branch information
hvy committed Dec 18, 2020
2 parents bb0fb30 + 865636c commit f65e66b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
17 changes: 15 additions & 2 deletions optuna/visualization/_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,29 @@ def objective(trial):
params:
Parameter list to visualize. The default is all parameters.
target:
A function to specify the value to display. If it is :obj:`None`, the objective values
are plotted.
A function to specify the value to display. If it is :obj:`None` and ``study`` is being
used for single-objective optimization, the objective values are plotted.
.. note::
Specify this argument if ``study`` is being used for multi-objective optimization.
target_name:
Target's name to display on the color bar.
Returns:
A :class:`plotly.graph_objs.Figure` object.
Raises:
:exc:`ValueError`:
If ``target`` is :obj:`None` and ``study`` is being used for multi-objective
optimization.
"""

_imports.check()
if target is None and len(study.directions) > 1:
raise ValueError(
"If the `study` is being used for multi-objective optimization, "
"please specify the `target`."
)
return _get_contour_plot(study, params, target, target_name)


Expand Down
17 changes: 15 additions & 2 deletions optuna/visualization/matplotlib/_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,29 @@ def objective(trial):
params:
Parameter list to visualize. The default is all parameters.
target:
A function to specify the value to display. If it is :obj:`None`, the objective values
are plotted.
A function to specify the value to display. If it is :obj:`None` and ``study`` is being
used for single-objective optimization, the objective values are plotted.
.. note::
Specify this argument if ``study`` is being used for multi-objective optimization.
target_name:
Target's name to display on the color bar.
Returns:
A :class:`matplotlib.axes.Axes` object.
Raises:
:exc:`ValueError`:
If ``target`` is :obj:`None` and ``study`` is being used for multi-objective
optimization.
"""

_imports.check()
if target is None and len(study.directions) > 1:
raise ValueError(
"If the `study` is being used for multi-objective optimization, "
"please specify the `target`."
)
_logger.warning(
"Output figures of this Matplotlib-based `plot_contour` function would be different from "
"those of the Plotly-based `plot_contour`."
Expand Down
7 changes: 7 additions & 0 deletions tests/visualization_tests/matplotlib_tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
from optuna.visualization.matplotlib import plot_contour


def test_target_is_none_and_study_is_multi_obj() -> None:

study = create_study(directions=["minimize", "minimize"])
with pytest.raises(ValueError):
plot_contour(study)


@pytest.mark.parametrize(
"params",
[
Expand Down
7 changes: 7 additions & 0 deletions tests/visualization_tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
RANGE_TYPE = Union[Tuple[str, str], Tuple[float, float]]


def test_target_is_none_and_study_is_multi_obj() -> None:

study = create_study(directions=["minimize", "minimize"])
with pytest.raises(ValueError):
plot_contour(study)


@pytest.mark.parametrize(
"params",
[
Expand Down

0 comments on commit f65e66b

Please sign in to comment.