Coder Social home page Coder Social logo

Comments (8)

francois-rozet avatar francois-rozet commented on June 2, 2024 1

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

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.

Badr-MOUFAD avatar Badr-MOUFAD commented on June 2, 2024

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.

francois-rozet avatar francois-rozet commented on June 2, 2024

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.

agramfort avatar agramfort commented on June 2, 2024

it's not a typing issue I think but more that pytorch is not properly installed on the SBI benchmark.

from benchopt.

francois-rozet avatar francois-rozet commented on June 2, 2024

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.

agramfort avatar agramfort commented on June 2, 2024

from benchopt.

tomMoral avatar tomMoral commented on June 2, 2024

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.

francois-rozet avatar francois-rozet commented on June 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.