Skip to content

Add importance evaluator test cases to optuna.testing - #6765

Merged
kAIto47802 merged 4 commits into
optuna:masterfrom
y0z:feature/importance-test-fixtures
Jul 24, 2026
Merged

kAIto47802 merged 4 commits into
optuna:masterfrom
y0z:feature/importance-test-fixtures

Conversation

@y0z

@y0z y0z commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

Motivation

Optuna already provides reusable test cases for storages and samplers through optuna.testing (#6369 and #6424). This PR introduces the same testing pattern for importance evaluators.

Description of the changes

  • Add optuna.testing.pytest_importance with the following reusable test cases:
    • BasicImportanceEvaluatorTestCase
    • ConditionalImportanceEvaluatorTestCase
    • NonConditionalImportanceEvaluatorTestCase
    • MultiObjectiveImportanceEvaluatorTestCase
  • Refactor the existing importance evaluator tests to use parametrized evaluator fixtures.
  • Organize tests according to evaluator capabilities, such as conditional search-space support and multi-objective support.
  • Register assertion rewriting for optuna.testing.pytest_importance.
  • Preserve the existing test coverage for Optuna's built-in importance evaluators.

A third-party evaluator can use the test cases by inheriting the applicable classes and overriding the evaluator fixture:

class TestMyImportanceEvaluator(
    BasicImportanceEvaluatorTestCase,
    ConditionalImportanceEvaluatorTestCase,
    MultiObjectiveImportanceEvaluatorTestCase,
):
    @pytest.fixture
    def evaluator(self) -> Callable[..., BaseImportanceEvaluator]:
        return MyImportanceEvaluator

@y0z y0z added the test Unit test. label Jul 22, 2026
@y0z y0z added this to the v5.0.0 milestone Jul 22, 2026
@y0z

y0z commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

@kAIto47802 @c-bata Could you review this PR?

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

Looks almost good to me. I left one suggestion though.

Comment thread optuna/testing/pytest_importance.py Resolved

@c-bata c-bata 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. Thank you for the update.

@c-bata c-bata removed their assignment Jul 24, 2026
assert param_importance["x"] > 0.0
assert param_importance["y"] == 0.0
class TestMultiObjectiveImportanceEvaluator(MultiObjectiveImportanceEvaluatorTestCase):
@pytest.fixture(params=ALL_EVALUATORS[2:])

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.

Currently, it is inconsistent with the way PedAnovaImportanceEvaluator is handled in TestConditionalImportanceEvaluator.
We should use the explicit construction here as well (cf. #6765 (comment))

Suggested change
@pytest.fixture(params=ALL_EVALUATORS[2:])
@pytest.fixture(params=[pytest.param(_ped_anova_evaluator, id="PedAnovaImportanceEvaluator")])

evaluator_cls: Callable[[], BaseImportanceEvaluator], normalize: bool
) -> None:
def test_get_param_importances_unnormalized_experimental() -> None:
def objective(trial: Trial) -> float:

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.

What about using

study = _get_study(seed=0, n_trials=3, is_multi_obj=False)

here as well?

study = create_study()
study.optimize(objective, n_trials=3)

with pytest.raises(ValueError):

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.

The original comment has gone away

Suggested change
with pytest.raises(ValueError):
# None of the trials with `x2` are completed.
with pytest.raises(ValueError):


with pytest.raises(ValueError):
get_param_importances(study, evaluator=evaluator(), params=["x2"])
with pytest.raises(ValueError):

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.

ditto

Suggested change
with pytest.raises(ValueError):
# None of the trials with `x2` are completed. Adding "x1" should not matter.
with pytest.raises(ValueError):

get_param_importances(study, evaluator=evaluator(), params=["x2"])
with pytest.raises(ValueError):
get_param_importances(study, evaluator=evaluator(), params=["x1", "x2"])
with pytest.raises(ValueError):

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.

ditto

Suggested change
with pytest.raises(ValueError):
# None of the trials contain `x3`.
with pytest.raises(ValueError):

@kAIto47802

Copy link
Copy Markdown
Collaborator

Thank you for the PR. It almost LGTM, with some minor comments listed above :octocat:

- Use explicit param for PedAnovaImportanceEvaluator in
  TestMultiObjectiveImportanceEvaluator for consistency.
- Reuse _get_study in test_get_param_importances_unnormalized_experimental.
- Restore comments in test_get_param_importances_invalid_no_completed_trials_params.

Co-Authored-By: kAIto47802 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
@y0z

y0z commented Jul 24, 2026

Copy link
Copy Markdown
Member Author

I applied the suggestions.

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

Thank you for the update. LGTM

@kAIto47802
kAIto47802 merged commit d147fd5 into optuna:master Jul 24, 2026
15 of 16 checks passed
@y0z
y0z deleted the feature/importance-test-fixtures branch July 24, 2026 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test Unit test.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants