Comments (8)
Currently benchopt install
skips the code that is within the context of safe_import_context
. This leads to some issues, like the one at hand, but also with imports from benchmark_utils
like base classes.
Some features of importlib
(like MetaPathFinder
) can be used to modify the behavior of import
, for instance to create "fake" modules instead of importing them. I think this could be used within safe_import_context
to solve these issues.
See
- https://python.plainenglish.io/metapathfinders-or-how-to-change-python-import-behavior-a1cf3b5a13ec
- https://docs.python.org/3/library/importlib.html#importlib.abc.MetaPathFinder.
What do you think @tomMoral, @mathurinm, @Badr-MOUFAD ? If you think this is worth it, I can try to write a minimal working example.
from benchopt.
Thanks for flagging this!
I doubt that typed parameters would impair detecting/installing requirements.
Could you pls provide a link to the CI error? Also, a link to the repository would be great!
from benchopt.
Hello, https://github.com/JuliaLinhart/benchmark_sbi/actions/runs/5461731758/jobs/9940110771.
Tensor
is imported in safe_import_context
and used as typing in set_data
.
from benchopt.
it's not a typing issue I think but more that pytorch is not properly installed on the SBI benchmark.
from benchopt.
It is a typing issue. Switching to the following (outside of the safe_import_context
) fixes the CI issue (but it is ugly).
try:
from torch import Tensor
from torch.distributions import Distribution
except:
Tensor = TypeVar("Tensor")
Distribution = TypeVar("Distribution")
The problem is that benchopt cannot install torch
as it cannot even define the class Objective
(as Tensor
does not exist yet).
from benchopt.
from benchopt.
This looks indeed promising, creating a fake object automatically when the module is not found would solve many issues.
If you can craft a minimal working example, that would be super helpful
from benchopt.
Here is a MWE to replace all imported members (packages, modules, ...) within a context by Mock
objects from unittest
. Accessing the attributes of a mock object returns a mock object (super cool!), so there is never an issue.
import sys
from contextlib import contextmanager
from importlib.abc import MetaPathFinder, Loader
from importlib.util import spec_from_loader
from unittest.mock import Mock
class MockLoader(Loader):
def __init__(self, name: str):
self.name = name
def create_module(self, spec):
return Mock(name=self.name)
def exec_module(self, module):
module.__path__ = []
class MockFinder(MetaPathFinder):
def find_spec(self, fullname, path, target=None):
return spec_from_loader(fullname, MockLoader(fullname))
@contextmanager
def mock_import():
try:
sys.meta_path.insert(0, MockFinder())
yield
finally:
sys.meta_path.pop(0)
with mock_import():
import notapackage # no error
from notapackage.notamodule import notamember
import notapackageeither # error
I think it is only necessary to activate this behavior in benchopt install
and when looking for the dataset/solver names and parameters.
from benchopt.
Related Issues (20)
- `from benchopt.base import BaseObjective, safe_import_context` found in doc snippets raise `ImportError`
- `pip install -U -i https://test.pypi.org/simple/benchopt` suggested in get started guide does not work HOT 2
- Example given in workflow documentation with installation from source doesn't actually feature an install from source
- [Feature request] Could it be possible to customize lookup paths in the CLI HOT 1
- [Feature request] Allow arbitrary naming of datasets, objectives and solvers classes ? HOT 1
- [Feature Request] CLI that prints all datasets, objectives, solvers, and available parameters, parameters than run by defaults, and accepted values for each parameters HOT 2
- BUG minimum version required HOT 4
- [BUG] clean command does not remove benchopt_run folder
- [Feature request] completely outsource the run
- Add Poisson regression benchmark HOT 2
- [BUG] ```benchopt install``` is unable to detect conda env HOT 3
- FEAT generate config.yml and attaching it in the HTML HOT 1
- BUG benchopt install fail for solver that depends on global requirements
- BUG neurips results are not available on benchopt.github.io/results HOT 1
- ENH expose results artifacts when submitting a PR to benchopt/results
- ENH Better error message for `get_result` return format
- ENH YAML config file with dictionaries
- ENH Easy access to result tensors
- BUG run_once strategy HOT 3
- Bug with Nested config files for solvers HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from benchopt.