Skip to content
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

Conversation

eukaryo
Copy link
Collaborator

@eukaryo eukaryo commented Mar 23, 2023

Motivation

cf: #4470

In addition to plotly, I want to implement timeline plot using matplotlib as backend.

Description of the changes

  • Add timeline plot using matplotlib in optuna/visualization/matplotlib/_timeline.py.
  • Add unit tests in tests/visualization_tests/matplotlib_tests/test_timeline.py.

test-fig-matplotlib

@github-actions github-actions bot added the optuna.visualization Related to the `optuna.visualization` submodule. This is automatically labeled by github-actions. label Mar 23, 2023
@codecov-commenter
Copy link

codecov-commenter commented Mar 23, 2023

Codecov Report

Merging #4538 (5d16671) into master (2d0093a) will increase coverage by 0.08%.
The diff coverage is 100.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master    #4538      +/-   ##
==========================================
+ Coverage   90.81%   90.89%   +0.08%     
==========================================
  Files         187      184       -3     
  Lines       13972    13954      -18     
==========================================
- Hits        12688    12683       -5     
+ Misses       1284     1271      -13     
Impacted Files Coverage Δ
optuna/visualization/matplotlib/__init__.py 100.00% <100.00%> (ø)
optuna/visualization/matplotlib/_timeline.py 100.00% <100.00%> (ø)

... and 40 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@c-bata
Copy link
Member

c-bata commented Mar 24, 2023

@amylase @HideakiImamura Could you review this PR if you have time?

Copy link
Member

@HideakiImamura HideakiImamura left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. I have several comments. PTAL.

By the way, can we check the plot appearance using the visual regression test? The CI job does not generate the appropriate link.

optuna/visualization/matplotlib/_timeline.py Outdated Show resolved Hide resolved
optuna/visualization/matplotlib/_timeline.py Outdated Show resolved Hide resolved
@toshihikoyanase toshihikoyanase added the feature Change that does not break compatibility, but affects the public interfaces. label Mar 28, 2023
@eukaryo
Copy link
Collaborator Author

eukaryo commented Mar 28, 2023

Here is an example of plot with the current modified code.
test-fig-matplotlib-2

Copy link
Member

@toshihikoyanase toshihikoyanase left a comment

Choose a reason for hiding this comment

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

Could you update the list of plot functions in the reference, please?

optuna.visualization.matplotlib.plot_contour
optuna.visualization.matplotlib.plot_edf
optuna.visualization.matplotlib.plot_intermediate_values
optuna.visualization.matplotlib.plot_optimization_history
optuna.visualization.matplotlib.plot_parallel_coordinate
optuna.visualization.matplotlib.plot_param_importances
optuna.visualization.matplotlib.plot_pareto_front
optuna.visualization.matplotlib.plot_slice
optuna.visualization.matplotlib.is_available

@eukaryo
Copy link
Collaborator Author

eukaryo commented Mar 29, 2023

I have updated optuna/docs/source/reference/visualization/matplotlib.rst and fixed docstring in plot_timeline function,

@github-actions
Copy link
Contributor

github-actions bot commented Mar 30, 2023

@github-actions github-actions bot temporarily deployed to pull request March 30, 2023 09:33 Inactive
@eukaryo
Copy link
Collaborator Author

eukaryo commented Mar 30, 2023

Oh, timeline_plot is not rendered... Maybe the reason is that optuna/optuna-visualization-regression-tests#8 is not merged yet. I have resolved comments in that PR. I think we can merge that PR now. (although there is still some room for follow-up)

@toshihikoyanase
Copy link
Member

optuna/optuna-visualization-regression-tests#8 was merged! Let me re-render the plot.

@toshihikoyanase
Copy link
Member

toshihikoyanase commented Mar 31, 2023

The timeline plots of Plotly backend were rendered as expected, but those of matplotlib backend weren't. This is because the CI checked out the current master instead of the PR head.

/usr/bin/git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin +01aa6d9733269aa58e0ae8931a1bd28b662d7942:refs/remotes/origin/master

I think we need #4555 to render plots using the code in PRs.

https://642648d03da4640e33ad5d1b--effervescent-dieffenbachia-ab819a.netlify.app/plot_timeline.html
image

Copy link
Member

@HideakiImamura HideakiImamura left a comment

Choose a reason for hiding this comment

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

Thanks for the update. Almost LGTM. I have a minor comment. PTAL.

optuna/visualization/matplotlib/_timeline.py Outdated Show resolved Hide resolved
@HideakiImamura
Copy link
Member

I confirmed that the matplotlib version by using the following code.

import time
import datetime

import optuna
import matplotlib.pyplot as plt


def objective(trial):
    if trial.number % 3 == 0:
        time.sleep(0.003 * trial.number)
        return 1
    elif trial.number % 3 == 1:
        time.sleep(0.002 * trial.number)
        raise optuna.TrialPruned()
    else:
        time.sleep(0.001 * trial.number)
        raise ValueError("FAILED!")


study = optuna.create_study()
study.optimize(objective, n_trials=100, catch=(ValueError,))

start = datetime.datetime.now()
complete = start - datetime.timedelta(seconds=1.0)
study.add_trial(
    optuna.trial.FrozenTrial(
        number=100,
        trial_id=-1,
        state=optuna.trial.TrialState.COMPLETE,
        value=0.0,
        values=None,
        datetime_start=start,
        datetime_complete=complete,
        params={},
        distributions={},
        user_attrs={},
        system_attrs={},
        intermediate_values={},
    )
)

plotly_fig = optuna.visualization.plot_timeline(study)
plotly_fig.write_image("hoge_plotly.png")

plt_fig = optuna.visualization.matplotlib.plot_timeline(study)
plt.savefig("hoge_plot.png")

The generated plots as follows.

Plotly version Matplotlib version
hoge_plotly hoge_plot

@toshihikoyanase toshihikoyanase self-assigned this Apr 11, 2023
@eukaryo
Copy link
Collaborator Author

eukaryo commented Apr 11, 2023

The current code generates the plots below.

Plotly version Matplotlib version
hoge_plotly hoge_plot

Copy link
Member

@HideakiImamura HideakiImamura left a comment

Choose a reason for hiding this comment

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

Looks very good!

@HideakiImamura HideakiImamura removed their assignment Apr 12, 2023
removed customized DateFormatter class
added import matplotlib instead of dates, ticker, and patches
remove dates, patches, and ticker
@toshihikoyanase
Copy link
Member

Thank you for your update. Let me check again.

Copy link
Member

@toshihikoyanase toshihikoyanase left a comment

Choose a reason for hiding this comment

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

Thank you for your update. Looks solid to me.

@@ -18,4 +18,5 @@ optuna.visualization.matplotlib
optuna.visualization.matplotlib.plot_param_importances
optuna.visualization.matplotlib.plot_pareto_front
optuna.visualization.matplotlib.plot_slice
optuna.visualization.matplotlib.plot_timeline
Copy link
Member

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:
image

@toshihikoyanase toshihikoyanase added this to the v3.2.0 milestone Apr 18, 2023
@toshihikoyanase
Copy link
Member

@amylase Let me merge this PR since it got two approvals.

@toshihikoyanase toshihikoyanase merged commit 93bc04d into optuna:master Apr 18, 2023
31 checks passed
@eukaryo eukaryo deleted the 2023-03-matplotlib-timeline-plot branch September 6, 2023 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Change that does not break compatibility, but affects the public interfaces. optuna.visualization Related to the `optuna.visualization` submodule. This is automatically labeled by github-actions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants