Read keeps the file and says its format; extract takes fields from columns; EEG batches carry their recording, on Dask where it is installed
This commit is contained in:
+10
-10
@@ -8,12 +8,12 @@ mains, with the table's age) in worker processes; `tables` gives each scenario's
|
||||
from the valid studies.
|
||||
"""
|
||||
|
||||
import multiprocessing
|
||||
from concurrent.futures import ProcessPoolExecutor
|
||||
from pathlib import Path
|
||||
|
||||
import pandas as pd
|
||||
from joblib import Parallel, delayed
|
||||
|
||||
from models import cluster
|
||||
from models.tea.eeg.analysis import analyze
|
||||
from models.tea.eeg.curves import SCENARIOS
|
||||
|
||||
@@ -34,9 +34,8 @@ def listing(folder: Path, ages_table: Path, ages=AGES) -> list[tuple[str, Path,
|
||||
return [(f"{NAME}-{k:04d}", path, age) for k, (path, age) in enumerate(found, 1)]
|
||||
|
||||
|
||||
def _one(study: tuple[str, Path, float]) -> dict:
|
||||
key, path, age = study
|
||||
a = analyze(Path(path).read_bytes(), age_months=age, mains_hz=MAINS_HZ)
|
||||
def _one(key: str, edf: bytes, age: float) -> dict:
|
||||
a = analyze(edf, age_months=age, mains_hz=MAINS_HZ)
|
||||
return {
|
||||
"key": key,
|
||||
"age_months": age,
|
||||
@@ -46,11 +45,12 @@ def _one(study: tuple[str, Path, float]) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def measure(studies, workers: int = 4) -> list[dict]:
|
||||
"""Each study's verdict and features, in listing order."""
|
||||
context = multiprocessing.get_context("spawn")
|
||||
with ProcessPoolExecutor(workers, mp_context=context) as pool:
|
||||
return list(pool.map(_one, studies))
|
||||
def measure(studies) -> list[dict]:
|
||||
"""Each study's verdict and features, in listing order, each a task on the cluster
|
||||
(models.cluster)."""
|
||||
with cluster.batch(): # each task carries its recording: workers see no local folder
|
||||
return Parallel(n_jobs=cluster.jobs())(delayed(_one)(key, Path(path).read_bytes(), age)
|
||||
for key, path, age in studies) # fmt: skip
|
||||
|
||||
|
||||
def tables(measured: list[dict]) -> dict[str, pd.DataFrame]:
|
||||
|
||||
+7
-9
@@ -16,8 +16,8 @@ from pathlib import Path
|
||||
import mlflow.pyfunc
|
||||
import pandas as pd
|
||||
from joblib import Parallel, delayed
|
||||
from joblib.externals.loky import get_reusable_executor
|
||||
|
||||
from models import cluster
|
||||
from models.tea.eeg import analysis, curves, reference
|
||||
from persistance import eeg, registry, silver
|
||||
|
||||
@@ -40,16 +40,14 @@ class Baseline(mlflow.pyfunc.PythonModel):
|
||||
for r in rows]) # fmt: skip
|
||||
|
||||
|
||||
def _analyze(edf: str, birth) -> tuple[str, analysis.Analysis]:
|
||||
return edf, analysis.analyze(eeg.signal(edf), birth)
|
||||
|
||||
|
||||
def analyze() -> int:
|
||||
"""Every hospital EEG not analysed yet; then gold's TEA tables are rebuilt."""
|
||||
pending = eeg.pending()
|
||||
found = dict(Parallel(n_jobs=-1)(delayed(_analyze)(r["edf"], r["birth_date"])
|
||||
for r in pending)) # fmt: skip
|
||||
get_reusable_executor().shutdown(wait=True)
|
||||
with cluster.batch(): # one task per EEG, carrying its recording (models.cluster)
|
||||
tasks = (delayed(analysis.analyze)(eeg.signal(r["edf"]), r["birth_date"])
|
||||
for r in pending) # fmt: skip
|
||||
done = Parallel(n_jobs=cluster.jobs())(tasks)
|
||||
found = dict(zip((r["edf"] for r in pending), done, strict=True))
|
||||
eeg.save_analyses(found)
|
||||
silver.build(["tea_eeg_analyses+", "tea_eeg_metrics+"])
|
||||
return len(found)
|
||||
@@ -65,7 +63,7 @@ def _reference() -> tuple[str, list[dict]]:
|
||||
path = Path(cache) / f"{reference.NAME}-{digest[:16]}.json" if cache else None
|
||||
if path and path.exists():
|
||||
return digest, json.loads(path.read_text())
|
||||
measured = reference.measure(studies, workers=os.cpu_count() or 4)
|
||||
measured = reference.measure(studies)
|
||||
measured = [{**m, "key": str(m["key"])} for m in measured]
|
||||
if path:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
Reference in New Issue
Block a user