Loading a TEA EEG baseline leaves sys.path as it was, so later Dask workers still import tea.pipeline
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:
|
||||
|
||||
Reference in New Issue
Block a user