nf-core / deepmodeloptim

Stochastic Testing and Input Manipulation for Unbiased Learning Systems
https://nf-co.re/deepmodeloptim
MIT License
23 stars 9 forks source link

[feat] robust way of defining and importing the model class #158

Closed suzannejin closed 1 month ago

suzannejin commented 4 months ago

Currently, the code will just read the model .py file, and check for the first class that starts with 'Model'. However, this might lead to undesired behaviours (eg. the user may have defined many classes starting with Model, etc.). Need a more robust way of doing it, or force a nomenclature.

def import_class_from_file(file_path: str) -> type:

    # Extract directory path and file name
    directory, file_name = os.path.split(file_path)
    module_name = os.path.splitext(file_name)[0]  # Remove extension to get module name

    # Create a module from the file path
    # In summary, these three lines of code are responsible for creating a module specification based on a file location, creating a module object from that specification, and then executing the module's code to populate the module object with the definitions from the Python file.
    spec = importlib.util.spec_from_file_location(module_name, file_path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)

    # Find the class dynamically
    for name in dir(module):
        model_class = getattr(module, name)
        if isinstance(model_class, type) and name.startswith('Model'):
            return model_class

    # Class not found
    raise ImportError("No class starting with 'Model' found in the file.")
mathysgrapotte commented 1 month ago

to be adressed at stimulus-py