def run_script(script_filepath, config_filepath, **kwargs): """Method to run experiment (defined by a script file) Args: script_filepath (str): input script filepath config_filepath (str): input configuration filepath """ # Add config path and current working directory to sys.path to correctly load the configuration sys.path.insert(0, Path(script_filepath).resolve().parent.as_posix()) sys.path.insert(0, Path(config_filepath).resolve().parent.as_posix()) sys.path.insert(0, os.getcwd()) module = load_module(script_filepath) _check_script(module) exp_name = module.__name__ run_fn = module.__dict__['run'] # Setup configuration if kwargs.get('manual_config_load', False): config = _ConfigObject(config_filepath, script_filepath) else: config = _setup_config(config_filepath, script_filepath) logger = logging.getLogger(exp_name) log_level = logging.INFO setup_logger(logger, log_level) try: run_fn(config, logger=logger, **kwargs) except KeyboardInterrupt: logger.info("Catched KeyboardInterrupt -> exit") except Exception as e: # noqa logger.exception("") raise e
def run_script(script_file: str, config_file: str, **kwargs: Any) -> None: """Method to run experiment (defined by a script file) Args: script_filepath: input script filepath. Script should contain ``run(config, **kwargs)`` method. config_filepath: input configuration filepath """ # Add config path and current working directory to sys.path to correctly load the configuration script_filepath = Path(script_file) config_filepath = Path(config_file) sys.path.insert(0, script_filepath.resolve().parent.as_posix()) sys.path.insert(0, config_filepath.resolve().parent.as_posix()) sys.path.insert(0, os.getcwd()) module = load_module(script_filepath) _check_script(module) run_fn = module.__dict__["run"] # Lazy setup configuration config = ConfigObject(config_filepath, script_filepath=script_filepath) run_fn(config, **kwargs)
def test_load_module(dirname): import numpy as np filepath = os.path.join(dirname, "custom_module.py") s = """ import numpy as np a = 123 b = np.array([1, 2, 3]) """ with open(filepath, "w") as h: h.write(s) custom_module = load_module(filepath) assert "a" in custom_module.__dict__ assert custom_module.a == 123 assert "b" in custom_module.__dict__ assert np.all(custom_module.b == np.array([1, 2, 3]))
def _setup_config(config_filepath, script_filepath): config = load_module(config_filepath) config.config_filepath = Path(config_filepath) config.script_filepath = Path(script_filepath) return config
def test_load_module_inexisting_file(): filepath = "/tmp/tmp.py" with pytest.raises(ValueError, match="is not found"): load_module(filepath)