Skip to content

Keep infeasible categorical trials in plot_slice - #6932

Merged
not522 merged 2 commits into
optuna:masterfrom
kokyouu:fix/slice-infeasible-categories
Nov 11, 2026
Merged

not522 merged 2 commits into
optuna:masterfrom
kokyouu:fix/slice-infeasible-categories

Conversation

@kokyouu

@kokyouu kokyouu commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

plot_slice silently omits infeasible trials on categorical axes, including when every trial is infeasible. The Matplotlib backend retains these points.

Description of the changes

Keep infeasible trial numbers alongside their x and y values. Categorical plotting zips these three lists to group points in category order; the empty trial-number list currently discards every infeasible point.

Add a focused regression test through the public plotting API with one feasible and one infeasible trial on a categorical axis.

Validation

  • The focused regression case fails on unmodified master (f9618295) and passes with the fix.
  • pytest tests/visualization_tests/test_slice.py -q: 53 passed, including both plotting backends.
  • Repository-wide Ruff lint and format checks pass.
  • Mypy passes for both changed files with --follow-imports=silent. Full mypy . reports one unrelated error at optuna/visualization/_intermediate_values.py:95; the identical error reproduces on unmodified master with the same environment.

Tested on Windows with Python 3.12.3, Plotly 7.1.0 and Matplotlib 3.11.2.

Checklist

  • I used an LLM while working on this PR.

@c-bata c-bata assigned gen740 and unassigned gen740 Oct 26, 2026
@c-bata

c-bata commented Oct 26, 2026

Copy link
Copy Markdown
Member

Could you add a minimal reproducible example (code and the resulting plot) showing infeasible trials missing from plot_slice with a categorical parameter? A before-and-after plot would help clarify the visual change.

@kokyouu

kokyouu commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for taking a look. Here is a deterministic reproduction with one categorical parameter and two completed trials. Trial 0 is feasible (constraint=-1.0); trial 1 is infeasible (constraint=+1.0). No sampling or external dataset is needed.

import optuna
from optuna.distributions import CategoricalDistribution
from optuna.trial import create_trial

study = optuna.create_study(direction="minimize")
distribution = CategoricalDistribution(["linear", "tree"])
for model, value, constraint in [("linear", 0.2, -1.0), ("tree", 0.8, 1.0)]:
    study.add_trial(
        create_trial(
            value=value,
            params={"model": model},
            distributions={"model": distribution},
            constraints={"valid_model": constraint},
        )
    )

fig = optuna.visualization.plot_slice(study, params=["model"])
# Use the same axes for the before/after comparison.
fig.update_xaxes(range=[-0.5, 1.5])
fig.update_yaxes(range=[0.0, 1.0])
for trace in fig.data:
    print(trace.name, list(trace.x), list(trace.y))
fig.show()

I ran this against the unmodified base commit f9618295 and this PR's head c763c24, using Python 3.12.3 and Plotly 7.1.0. Both use Optuna's 5.1.0.dev source, including the create_trial(..., constraints=...) API.

Before (f9618295), the tree point is missing and no infeasible trace is generated:

Feasible Trial ["'linear'"] [0.2]

After (c763c24), the same data also produces the gray infeasible point at ('tree', 0.8):

Feasible Trial ["'linear'"] [0.2]
Infeasible Trial ["'tree'"] [0.8]

The comparison below shows the actual Plotly output from each revision with identical axes:

plot-slice-before-after

The cause is that categorical grouping zips the x values, y values and trial numbers. The base version collects the infeasible x/y values but leaves their trial-number list empty, so the zip drops every infeasible point. Appending the trial number keeps those lists aligned.

I also reran pytest tests/visualization_tests/test_slice.py -q with MPLBACKEND=Agg: 64 passed, including the 12 regression cases in this PR.

@c-bata

c-bata commented Oct 27, 2026

Copy link
Copy Markdown
Member

@not522 @yen-0 Could you review this PR?

@not522 not522 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@not522 not522 added the bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. label Oct 28, 2026
@not522 not522 removed their assignment Oct 28, 2026

@yen-0 yen-0 left a comment •

Copy link
Copy Markdown
Collaborator

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 test demonstrates the correct behavior, but currently runs 12 parameter combinations. Since they all exercise the same regression path, could we reduce this (for example, mixed feasible and infeasible categorical trials) to keep the test lightweight and make its intent clearer?

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions Bot added the stale Exempt from stale bot labeling. label Nov 4, 2026
@kokyouu

kokyouu commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback. I reduced the parameterized matrix to a single focused mixed case with one feasible categorical trial (linear, constraint -1.0) and one infeasible trial (tree, constraint +1.0). The test asserts both traces and their plotted points. On the unmodified base (f9618295), the same data only produces the feasible trace; with this PR's fix, both traces are present. pytest tests/visualization_tests/test_slice.py -q now reports 53 passed. The change is in commit 248885e.

@github-actions github-actions Bot removed the stale Exempt from stale bot labeling. label Nov 5, 2026

@yen-0 yen-0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

@not522
not522 merged commit b94563d into optuna:master Nov 11, 2026
12 checks passed
@not522 not522 added this to the v5.1.0 milestone Nov 11, 2026
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