Repository: optuna/optuna

Issue:
Reduce warnings in unit tests
### Motivation

The Optuna's unit tests raise many warnings, which make it hard to recognize unexpected warning messages.

### Description

We can filter expected warnings using `@pytest.mark.filterwarnings` or `warnings.simplefilter("ignore")`. Some warnings may be unexpected or unsuitable. We should fix tests or Optuna's codes in such cases.

This issue is contribution-welcome. We welcome any fixes, even how trivial.

### Alternatives (optional)

_No response_

### Additional context (optional)

_No response_


Discussion:
This issue has not seen any recent activity.
I will work on this.  
Hi @jmsykes83 thank you for letting us know your interest! I'm also working on this prioritised issue too since this issue affects many developers' experiences. Do you have specific modules you would like to resolve this issue? I'd like to avoid conflicts with your forthcoming pull request. 
I will let you take the lead on this one.  Was there any direction or ideas you had in approaching this.  
Thank you for your reply. How about some tests in `tests/visualization_tests`, maybe `tests/visualization_tests/test_utils.py` is the easiest one? The expected change will be similar to changes introduced by the linked PRs with this issue such as adding `@pytest.mark.filterwarnings` decorator to a test file.
I know I can do this:

```python
@pytest.mark.filterwarnings("ignore:Trial0")
def test_filter_nonfinite_with_invalid_target() -> None:
    study = prepare_study_with_trials()
    trials = study.get_trials(states=(TrialState.COMPLETE,))
    with pytest.raises(ValueError):
        _filter_nonfinite(trials, target=lambda t: "invalid target")  # type: ignore

```
But is this the solution you're looking for.  This seems like a solution that could become very cluttered in the long run.  This filters the only warning I am getting for `pytest tests/visualization_tests/test_utils.py`
In my understanding, the expected change is something like this. If we use stronger `filterwarnings`, we might miss unintended warning messages, so such a minimal filter looks enough. 
If these trials change in the future,  we might not want to filter them.  Maybe we can use a filter like: `ignore:Trial0.*float`?
This way if this trial ever changes it doesn't harm the new trial0.
> `ignore:Trial0.*float`?

The expected filter will be like this: `@pytest.mark.filterwarnings("ignore::UserWarning")`.
Did we need to filter the `user warnings` for `test_parallel_coordinate.py` as well?  If so, all of them or just the ones with the `  Attempting to set identical bottom == top == 29.999999999999996 results in singular transformations; automatically expanding?`
I'd suggest sending a PR modifying at most a few files such as only `tests/visualization_tests/test_utils.py` at first, not for all visualsation tests.
> I'd suggest sending a PR modifying at most a few files such as only `tests/visualization_tests/test_utils.py` at first, not for all visualsation tests.

This is what I am doing
So we don't work on the same test.  I will be sending in a pr once I finish some final tests.
These are the files I worked on:
```
        modified:   optuna/visualization/matplotlib/_parallel_coordinate.py
        modified:   tests/visualization_tests/matplotlib_tests/test_parallel_coordinate.py
        modified:   tests/visualization_tests/matplotlib_tests/test_pareto_front.py
        modified:   tests/visualization_tests/test_contour.py
        modified:   tests/visualization_tests/test_utils.py
```
Some tests will be updated largely by the ongoing PRs: https://github.com/optuna/optuna/pulls?q=is%3Apr+is%3Aopen+label%3Av3. So you might not need to work on them. (This was why I said 

> How about some tests in tests/visualization_tests, maybe tests/visualization_tests/test_utils.py is the easiest one? 

and

> only tests/visualization_tests/test_utils.py at first,

Resolving patch:
diff --git a/optuna/visualization/matplotlib/_matplotlib_imports.py b/optuna/visualization/matplotlib/_matplotlib_imports.py
--- a/optuna/visualization/matplotlib/_matplotlib_imports.py
+++ b/optuna/visualization/matplotlib/_matplotlib_imports.py
@@ -7,7 +7,6 @@
     # TODO(ytknzw): Add specific imports.
     import matplotlib  # NOQA
     from matplotlib import __version__ as matplotlib_version
-    from matplotlib import cm  # NOQA
     from matplotlib import pyplot as plt  # NOQA
     from matplotlib.axes._axes import Axes  # NOQA
     from matplotlib.collections import LineCollection  # NOQA
diff --git a/optuna/visualization/matplotlib/_param_importances.py b/optuna/visualization/matplotlib/_param_importances.py
--- a/optuna/visualization/matplotlib/_param_importances.py
+++ b/optuna/visualization/matplotlib/_param_importances.py
@@ -16,7 +16,6 @@
 
 if _imports.is_successful():
     from optuna.visualization.matplotlib._matplotlib_imports import Axes
-    from optuna.visualization.matplotlib._matplotlib_imports import cm
     from optuna.visualization.matplotlib._matplotlib_imports import plt
 
 
@@ -115,7 +114,7 @@ def _get_importances_plot(info: _ImportancesInfo) -> "Axes":
         pos,
         importance_values,
         align="center",
-        color=cm.get_cmap("tab20c")(0),
+        color=plt.get_cmap("tab20c")(0),
         tick_label=param_names,
     )
 


Test patch: