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

[hotfix] Fix the bug of matplotlib's plot_rank function #5133

Merged
merged 5 commits into from Dec 6, 2023

Conversation

c-bata
Copy link
Member

@c-bata c-bata commented Dec 1, 2023

Motivation

Fix #5115

Description of the changes

Fix the bug of matplotlib's plot_rank() function.

import optuna
from optuna.visualization.matplotlib import plot_rank
import matplotlib.pyplot as plt


def objective(trial):
    x = trial.suggest_uniform('x', -10, 10)
    y = trial.suggest_uniform('y', -10, 10)
    return (x - 2) ** 2 + y

def main():
    study = optuna.create_study()
    study.optimize(objective, n_trials=100)
    plot_rank(study)
    plt.savefig("./foo.png", bbox_inches="tight", dpi=100)

if __name__ == '__main__':
    main()

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

codecov bot commented Dec 1, 2023

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (bc4ee3e) 89.43% compared to head (d093d2f) 89.41%.
Report is 1 commits behind head on master.

Files Patch % Lines
optuna/visualization/_rank.py 71.42% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5133      +/-   ##
==========================================
- Coverage   89.43%   89.41%   -0.02%     
==========================================
  Files         205      205              
  Lines       15151    15160       +9     
==========================================
+ Hits        13550    13556       +6     
- Misses       1601     1604       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@not522
Copy link
Member

not522 commented Dec 1, 2023

@eukaryo @nabenabe0928 Could you review this PR?

@c-bata
Copy link
Member Author

c-bata commented Dec 4, 2023

The windows CI will be resolved by #5135 .

Copy link
Collaborator

@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.

Hi thank you for the quick fix!

I checked the color map array for matplotlib and plotly just in case with the following code:

import plotly
import matplotlib.pyplot as plt
import numpy as np


def get_color_scale_in_plotly(color_idxs: np.ndarray) -> np.ndarray:
    colormap = "RdYlBu_r"
    # sample_colorscale requires plotly >= 5.0.0.
    labeled_colors = plotly.colors.sample_colorscale(colormap, color_idxs)
    scaled_rgb_colors = np.array([plotly.colors.unlabel_rgb(cl) for cl in labeled_colors])
    return scaled_rgb_colors


def get_color_scale_in_matplotlib(color_idxs: np.ndarray) -> np.ndarray:
    colormap = "RdYlBu_r"
    cmap = plt.get_cmap(colormap)
    colors = cmap(color_idxs)[:, :3]
    rgb_colors = np.asarray(colors * 256, dtype=int)
    return rgb_colors


color_idxs = np.linspace(0, 1, 101)
cmap_plotly = get_color_scale_in_plotly(color_idxs)
cmap_matplotlib = get_color_scale_in_matplotlib(color_idxs)

Although they are not perfectly identical, I suppose the difference does not really matter from the user side, so I will leave it intact!

optuna/visualization/_rank.py Outdated Show resolved Hide resolved
@c-bata
Copy link
Member Author

c-bata commented Dec 5, 2023

Thank you for your review. PTAL.

Copy link
Collaborator

@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.

Thank you for the update! LGTM!

Copy link
Member

@contramundum53 contramundum53 left a comment

Choose a reason for hiding this comment

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

The code looks good to me. Let me check the behavior.

@contramundum53
Copy link
Member

contramundum53 commented Dec 5, 2023

The code works perfect. LGTM!

import optuna
study = optuna.create_study()

def objective(trial):
    x = trial.suggest_uniform('x', -10, 10)
    y = trial.suggest_uniform('y', -10, 10)
    return (x - 2) ** 2 + (y + 3) ** 2

study.optimize(objective, n_trials=100)

import optuna.visualization.matplotlib
optuna.visualization.matplotlib.plot_rank(study)

image

@c-bata c-bata added the bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. label Dec 6, 2023
@c-bata c-bata added this to the v3.5.0 milestone Dec 6, 2023
@c-bata
Copy link
Member Author

c-bata commented Dec 6, 2023

@contramundum53 @nabenabe0928 I've just noticed a minor bug where the RGB values could potentially reach 256, though they should be within the range [0, 255].

Could you please take a look at the following change?
d093d2f

Copy link
Collaborator

@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.

I checked the change!

@contramundum53
Copy link
Member

LGTM!

@contramundum53 contramundum53 merged commit 44ad029 into optuna:master Dec 6, 2023
20 of 22 checks passed
@c-bata c-bata deleted the fix-bug-matplotlib-gits branch January 26, 2024 05:56
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. 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.

matplotlib plot_rank bug
5 participants