Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reload file based model architecture #427

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bioimageio/core/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 15 additions & 8 deletions bioimageio/core/digest_spec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import importlib.util
import sys
from itertools import chain
from pathlib import Path
from typing import (
Expand Down Expand Up @@ -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)


Expand Down
Loading