FIX objective: guard heavy imports so benchopt can read metadata#50
Closed
felixdivo wants to merge 1 commit into
Closed
FIX objective: guard heavy imports so benchopt can read metadata#50felixdivo wants to merge 1 commit into
felixdivo wants to merge 1 commit into
Conversation
This was referenced Jun 13, 2026
…lvable `benchopt test` (benchopt_dev CI) failed with `Patterns ['simulated'] did not match any dataset`. Two issues, both from benchopt importing the benchmark in environments without the objective's requirements installed: 1. Dataset resolution under skip_import. When benchopt builds the test env it loads the objective under `skip_import_ctx` — it never imports the module, it statically AST-parses class attributes, and only extracts attributes that exist on `BaseObjective`. `test_config` is not a base attribute, so our `test_config['dataset']['name']` was invisible and resolution fell back to the non-existent `simulated` dataset. Fix: also set `test_dataset_name` (a base attribute, so it IS extracted statically) to a real dataset. 2. sklearn import at module load. `benchmark_utils.metrics` imported sklearn at module top level, so importing objective.py (and the AD datasets, which pull `AD_METRICS` for its keys) failed when sklearn was absent. Fix: import sklearn lazily inside the metric functions; the registries and metric names are now available with only numpy installed. The functions still run with sklearn present (it is an objective requirement, installed for real runs). This supersedes the earlier `safe_import_context` attempt, which made benchopt flag the whole objective as a failed import under `skip_import_ctx`. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1df5f08 to
8ebb5d4
Compare
Contributor
Author
|
This should not be needed once benchopt/benchopt#958 is merged upstream. |
Member
|
Indeed! sorry for the mess, I was making a lot of change to make this more robust, I thought I had a test but it was not testing what I thought... ^^ Thanks for the ping, this helped identifying what was missing. :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
benchopt_devCI fails withPatterns ['simulated'] did not match any dataset.Cause: objective.py imports
benchmark_utils.metrics, which imports sklearn atmodule level. benchopt reads the objective's metadata in an env without the
objective's requirements installed; on that failed import it returns a
FailedImport stub (no
test_config,test_dataset_name=None), which nowresolves to the non-existent
simulateddataset.The
simulatedfallback was introduced in benchopt/benchopt#951 (defaulttest_dataset_name'simulated'→Noneplus a['simulated']fallback),on top of the
test_configresolution added in benchopt/benchopt#942.Fix: wrap the metrics import in
safe_import_contextso the module imports formetadata without sklearn. The metric names are only used in method bodies.
🤖 Generated with Claude Code