-
-
Notifications
You must be signed in to change notification settings - Fork 985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add timeline plot with matplotlib as backend #4538
Merged
toshihikoyanase
merged 16 commits into
optuna:master
from
eukaryo:2023-03-matplotlib-timeline-plot
Apr 18, 2023
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8f122d6
Create _timeline.py
eukaryo e83b82a
Update __init__.py
eukaryo 9d63b62
Update _matplotlib_imports.py
eukaryo 97dca43
Create test_timeline.py
eukaryo 0892b5a
Update _timeline.py
eukaryo f1518cd
Merge branch 'optuna:master' into 2023-03-matplotlib-timeline-plot
eukaryo fa50046
Update test_timeline.py
eukaryo 2d650da
Update _timeline.py
eukaryo 6690269
Merge branch 'optuna:master' into 2023-03-matplotlib-timeline-plot
eukaryo 2ea2d35
Merge branch 'optuna:master' into 2023-03-matplotlib-timeline-plot
eukaryo 264fa95
Update matplotlib.rst
eukaryo 828a4c4
Update _timeline.py
eukaryo 4074a73
Update _timeline.py
eukaryo 78c1cd3
Update _timeline.py
eukaryo 40a00b8
Update _timeline.py
eukaryo 5d16671
Update _matplotlib_imports.py
eukaryo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from typing import Any | ||
|
||
from optuna._experimental import experimental_func | ||
from optuna.study import Study | ||
from optuna.trial import TrialState | ||
from optuna.visualization._timeline import _get_timeline_info | ||
from optuna.visualization._timeline import _TimelineInfo | ||
from optuna.visualization.matplotlib._matplotlib_imports import _imports | ||
|
||
|
||
if _imports.is_successful(): | ||
from optuna.visualization.matplotlib._matplotlib_imports import Axes | ||
from optuna.visualization.matplotlib._matplotlib_imports import dates | ||
from optuna.visualization.matplotlib._matplotlib_imports import patches | ||
from optuna.visualization.matplotlib._matplotlib_imports import plt | ||
from optuna.visualization.matplotlib._matplotlib_imports import ticker | ||
|
||
|
||
class _DateFormatter_Millisecond(ticker.Formatter): | ||
eukaryo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def __init__(self) -> None: | ||
pass | ||
|
||
def __call__(self, x: float, pos: Any = 0) -> str: | ||
return dates.num2date(x).strftime("%H:%M:%S.%f")[:-3] | ||
eukaryo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@experimental_func("3.2.0") | ||
def plot_timeline(study: Study) -> "Axes": | ||
"""Plot the timeline of a study. | ||
|
||
.. seealso:: | ||
Please refer to :func:`optuna.visualization.plot_timeline` for an example. | ||
|
||
Example: | ||
|
||
The following code snippet shows how to plot the timeline of a study. | ||
|
||
.. plot:: | ||
|
||
import time | ||
|
||
import optuna | ||
|
||
|
||
def objective(trial): | ||
x = trial.suggest_float("x", 0, 1) | ||
time.sleep(x * 0.1) | ||
if x > 0.8: | ||
raise ValueError() | ||
if x > 0.4: | ||
raise optuna.TrialPruned() | ||
return x ** 2 | ||
|
||
|
||
study = optuna.create_study(direction="minimize") | ||
study.optimize( | ||
objective, n_trials=50, n_jobs=2, catch=(ValueError,) | ||
) | ||
|
||
optuna.visualization.matplotlib.plot_timeline(study) | ||
|
||
Args: | ||
study: | ||
A :class:`~optuna.study.Study` object whose trials are plotted with | ||
their lifetime. | ||
|
||
Returns: | ||
A :class:`matplotlib.axes.Axes` object. | ||
""" | ||
_imports.check() | ||
info = _get_timeline_info(study) | ||
return _get_timeline_plot(info) | ||
|
||
|
||
def _get_timeline_plot(info: _TimelineInfo) -> "Axes": | ||
_cm = { | ||
TrialState.COMPLETE: "tab:blue", | ||
TrialState.FAIL: "tab:red", | ||
TrialState.PRUNED: "tab:orange", | ||
TrialState.RUNNING: "tab:green", | ||
TrialState.WAITING: "tab:gray", | ||
} | ||
|
||
# Set up the graph style. | ||
plt.style.use("ggplot") # Use ggplot style sheet for similar outputs to plotly. | ||
fig, ax = plt.subplots() | ||
ax.set_title("Timeline Plot") | ||
ax.set_xlabel("Datetime") | ||
ax.set_ylabel("Trial") | ||
|
||
if len(info.bars) == 0: | ||
return ax | ||
|
||
ax.barh( | ||
y=[b.number for b in info.bars], | ||
width=[b.complete - b.start for b in info.bars], | ||
left=[b.start for b in info.bars], | ||
color=[_cm[b.state] for b in info.bars], | ||
) | ||
|
||
# There are 5 types of TrialState in total. | ||
# However, the legend depicts only types present in the arguments. | ||
legend_handles = [] | ||
for state, color in _cm.items(): | ||
if len([b for b in info.bars if b.state == state]) > 0: | ||
legend_handles.append(patches.Patch(color=color, label=state.name)) | ||
ax.legend(handles=legend_handles, loc="upper left", bbox_to_anchor=(1.05, 1.0)) | ||
fig.tight_layout() | ||
|
||
assert len(info.bars) > 0 | ||
start_time = min([b.start for b in info.bars]) | ||
complete_time = max([b.complete for b in info.bars]) | ||
margin = (complete_time - start_time) * 0.05 | ||
|
||
ax.set_xlim(right=complete_time + margin, left=start_time - margin) | ||
ax.yaxis.set_major_locator(ticker.MaxNLocator(integer=True)) | ||
ax.xaxis.set_major_formatter(_DateFormatter_Millisecond()) | ||
plt.gcf().autofmt_xdate() | ||
return ax |
23 changes: 23 additions & 0 deletions
23
tests/visualization_tests/matplotlib_tests/test_timeline.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from __future__ import annotations | ||
|
||
from io import BytesIO | ||
|
||
import pytest | ||
|
||
from optuna.trial import TrialState | ||
from optuna.visualization.matplotlib._timeline import plot_timeline | ||
from tests.visualization_tests.test_timeline import _create_study | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"trial_states_list", | ||
[ | ||
[], | ||
[TrialState.COMPLETE, TrialState.PRUNED, TrialState.FAIL], | ||
[TrialState.FAIL, TrialState.PRUNED, TrialState.COMPLETE], | ||
], | ||
) | ||
def test_get_timeline_plot(trial_states_list: list[TrialState]) -> None: | ||
study = _create_study(trial_states_list) | ||
fig = plot_timeline(study) | ||
fig.get_figure().savefig(BytesIO()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Notes] The reference page was rendered as expected: