From 5b7a19bb12e5e2a30dc2bf33831106b6381f9235 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Mon, 21 Oct 2024 17:37:34 +0200 Subject: [PATCH 1/2] add importlib.reload --- bioimageio/core/digest_spec.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/bioimageio/core/digest_spec.py b/bioimageio/core/digest_spec.py index 1e229e53..2213d2ed 100644 --- a/bioimageio/core/digest_spec.py +++ b/bioimageio/core/digest_spec.py @@ -1,6 +1,7 @@ from __future__ import annotations import importlib.util +import sys from itertools import chain from pathlib import Path from typing import ( @@ -80,15 +81,21 @@ def _import_from_file_impl( source: FileSource, callable_name: str, **kwargs: Unpack[HashKwargs] ): local_file = download(source, **kwargs) - module_name = local_file.path.stem - importlib_spec = importlib.util.spec_from_file_location( - module_name, local_file.path - ) - if importlib_spec is None: - raise ImportError(f"Failed to import {module_name} from {source}.") + module_name = local_file.path.stem.replace("-", "_").replace(" ", "_") + if module_name in sys.modules: + logger.info(f"attempting to reload previously loaded '{module_name}'") + dep = sys.modules[module_name] + dep = importlib.reload(dep) # reload in case source file has changed + else: + importlib_spec = importlib.util.spec_from_file_location( + module_name, local_file.path + ) + if importlib_spec is None: + raise ImportError(f"Failed to import {module_name} from {source}.") + + dep = importlib.util.module_from_spec(importlib_spec) + importlib_spec.loader.exec_module(dep) # type: ignore # todo: possible to use "loader.load_module"? - dep = importlib.util.module_from_spec(importlib_spec) - importlib_spec.loader.exec_module(dep) # type: ignore # todo: possible to use "loader.load_module"? return getattr(dep, callable_name) From 18f34303be30b98eb9dbc67cfefccd15bd88499b Mon Sep 17 00:00:00 2001 From: fynnbe Date: Thu, 24 Oct 2024 14:42:51 +0200 Subject: [PATCH 2/2] log from one level higher --- bioimageio/core/cli.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bioimageio/core/cli.py b/bioimageio/core/cli.py index 4127dd17..61b889db 100644 --- a/bioimageio/core/cli.py +++ b/bioimageio/core/cli.py @@ -182,7 +182,9 @@ def _get_stat( req_dataset_meas, _ = get_required_dataset_measures(model_descr) if stats_path.exists(): - logger.info(f"loading precomputed dataset measures from {stats_path}") + logger.opt(depth=1).info( + f"loading precomputed dataset measures from {stats_path}" + ) stat = load_dataset_stat(stats_path) for m in req_dataset_meas: if m not in stat: