Skip to content

Fix contour plot of matplotlib#5892

Merged
HideakiImamura merged 5 commits intooptuna:masterfrom
fusawa-yugo:fusawa-yugo/check-visualization
Jan 20, 2025
Merged

Fix contour plot of matplotlib#5892
HideakiImamura merged 5 commits intooptuna:masterfrom
fusawa-yugo:fusawa-yugo/check-visualization

Conversation

@fusawa-yugo
Copy link
Copy Markdown
Contributor

Motivation

Outputs of coutour plot are different between plotly and matplotlib.
Visualization of matplotlib is not working properly.

スクリーンショット 2024-12-27 17 27 32

In this case, if category is bar, x1 is missing as shown below.

Params: {'category': 'bar', 'x2': -6.85817929203591}
Params: {'category': 'foo', 'x1': 9.955852432587085}
Params: {'category': 'foo', 'x1': 8.553363427670403}

Now, when plotting a gragh related to x1, bar is dropped from category axis.
This is the same for foo and x1 .

Description of the changes

I solved this by including all values on the axis with a categorical distribution, regardless of whether the value on the other axis is None.

contour_mat_4 2dev

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jan 5, 2025

This pull request has not seen any recent activity.

@github-actions github-actions bot added the stale Exempt from stale bot labeling. label Jan 5, 2025
@HideakiImamura
Copy link
Copy Markdown
Member

@nabenabe0928 Could you review this PR?

Copy link
Copy Markdown
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. The change looks good to me. Is it possible to introduce the test cases to validate this change?

@nabenabe0928
Copy link
Copy Markdown
Contributor

The PR description could be better by the following revisions:

Describe how they look differently

In my eyes, the difference was not that obvious.
If I understand the difference correctly, each plot includes only a part of categories, making the dots only on the left side or the bottom of each plot in matplotlib.

Claim which figure is for matplotlib

In the first figure, it is not obvious which figure shows the result obtained by matplotlib, so it would be much more friendly with the guide.

Add a code block for smoother PR reviews

It is not that hard to add a code block, but it significantly makes reviews easier.

Demo Code
import optuna
import matplotlib.pyplot as plt


def objective(trial: optuna.Trial) -> float:
    category = trial.suggest_categorical("category", ["foo", "bar"])
    if category == "foo":
        return (trial.suggest_float("x1", 0, 10) - 2) ** 2
    else:
        return -((trial.suggest_float("x2", -10, 0) + 5) ** 2)


study = optuna.create_study()
study.optimize(objective, n_trials=50)
fig = optuna.visualization.plot_contour(study)
fig.show()
optuna.visualization.matplotlib.plot_contour(study)
plt.show()

@nabenabe0928
Copy link
Copy Markdown
Contributor

nabenabe0928 commented Jan 6, 2025

Note

Seems the interpolation algorithm is a bit different from plotly, but that is not a range of this PR, so I will leave it as is.
In plotly, the contour seems the interpolation is treated as a constant value along the axis with a missing category.
On the other hand, the contour looks diverging in matplotlib as getting closer to the line with the missing category.

@nabenabe0928 nabenabe0928 added bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. and removed stale Exempt from stale bot labeling. labels Jan 6, 2025
@fusawa-yugo
Copy link
Copy Markdown
Contributor Author

I fixed comments.
Could you please review it?

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

LGTM!

@HideakiImamura
Copy link
Copy Markdown
Member

@fusawa-yugo Could you add a unittest for this change?

@fusawa-yugo
Copy link
Copy Markdown
Contributor Author

@HideakiImamura
I discussed with @c-bata, and I think that translating the expected inputs and outputs of this private function into unit tests is not straightforward in this case.
Therefore, I feel if it might be reasonable to omit adding unit tests this time.

Copy link
Copy Markdown
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 discussion. I agree with you. It would be great to work on the test of matplotlib contour tests as a follow-up.

@HideakiImamura HideakiImamura merged commit 59bf669 into optuna:master Jan 20, 2025
@HideakiImamura HideakiImamura added this to the v4.3.0 milestone Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants