Merge feature/tea-eeg-analysis into feature/tea-eeg-baseline-bundles
This commit is contained in:
@@ -4,6 +4,7 @@ Each baseline is a trained `tea` biomarker module registered as `tea-<scenario>`
|
||||
one in production a metric keeps its raw value and no deviation, the same as a model never configured.
|
||||
"""
|
||||
import math
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
|
||||
import numpy as np
|
||||
@@ -29,6 +30,23 @@ def _number(value) -> float | None:
|
||||
return number if math.isfinite(number) else None
|
||||
|
||||
|
||||
def _load(model_uri: str):
|
||||
"""The library's biomarker module of a registered baseline, leaving `sys.path` as it was.
|
||||
|
||||
The model bundles a partial copy of `tea` and MLflow puts it first on `sys.path` while loading. Left there, a Dask
|
||||
worker spawned afterwards inherits it and can't import `tea.pipeline`. The installed `tea` is imported first so
|
||||
the copy is never the package.
|
||||
"""
|
||||
import tea # noqa: F401
|
||||
from tea.serving import load_biomarker
|
||||
|
||||
path = list(sys.path)
|
||||
try:
|
||||
return load_biomarker(model_uri)
|
||||
finally:
|
||||
sys.path[:] = path
|
||||
|
||||
|
||||
def _feature_baseline(module, feature: str):
|
||||
baselines = getattr(module, "baselines", None)
|
||||
return baselines[feature] if baselines is not None else module.baseline
|
||||
@@ -82,9 +100,7 @@ class Baselines:
|
||||
if self._loader is not None:
|
||||
self._loaded[scenario] = self._loader(resolved.model_uri)
|
||||
else:
|
||||
from tea.serving import load_biomarker
|
||||
|
||||
self._loaded[scenario] = load_biomarker(resolved.model_uri)
|
||||
self._loaded[scenario] = _load(resolved.model_uri)
|
||||
return self._loaded[scenario]
|
||||
|
||||
def deviation(self, scenario: str, feature: str, value: float | None, age_months: float | None) -> Deviation | None:
|
||||
|
||||
@@ -125,6 +125,37 @@ def test_a_promoted_baseline_scores_the_analysis(mlflow_tracking, reference):
|
||||
assert baselines.label("eeg-posterior-rhythm") == f"tea-eeg-posterior-rhythm/{version}"
|
||||
|
||||
|
||||
def test_loading_a_baseline_leaves_the_import_path_alone(mlflow_tracking, reference):
|
||||
"""Pins down: loading a promoted baseline leaves `sys.path` as it was and `tea` the installed package, so a Dask
|
||||
worker spawned after the first batch still imports `tea.pipeline`.
|
||||
|
||||
Fails if `Baselines` loads through MLflow without restoring `sys.path`: the model's bundled partial `tea` would
|
||||
shadow the library in every worker spawned afterwards, and each of their studies would fail with
|
||||
`No module named 'tea.pipeline'`.
|
||||
"""
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import tea
|
||||
|
||||
from models.io.registry import registry
|
||||
from models.tea.eeg.baselines import Baselines
|
||||
|
||||
found, studies = reference
|
||||
(version, _) = train.train_all(settings(), "PROD", found=found, studies=studies)["eeg-posterior-rhythm"]
|
||||
registry.attach_reports("tea", "eeg-posterior-rhythm", version, technical_validation_report_uri="r://t",
|
||||
clinical_validation_report_uri="r://c", interpretability_report_uri="r://i")
|
||||
registry.promote("tea", "eeg-posterior-rhythm", version)
|
||||
before, package = list(sys.path), tea.__file__
|
||||
|
||||
assert Baselines("tea").module("eeg-posterior-rhythm") is not None
|
||||
assert sys.path == before
|
||||
assert sys.modules["tea"].__file__ == package
|
||||
spawned = subprocess.run([sys.executable, "-c", "import sys, json; sys.path[:] = json.loads(sys.argv[1]); "
|
||||
"import tea.pipeline", __import__("json").dumps(sys.path)], capture_output=True)
|
||||
assert spawned.returncode == 0, spawned.stderr.decode()[-300:]
|
||||
|
||||
|
||||
def test_run_takes_only_train_and_promote_for_the_baselines(mlflow_tracking):
|
||||
"""Pins down: `models run tea eeg` runs the baselines' own steps: a cohort step is refused, and promoting with
|
||||
nothing trained says so and fails.
|
||||
|
||||
Reference in New Issue
Block a user