"""TEA's EEG baselines as a model of the platform: `python -m models run tea eeg --train [--promote]`. A baseline isn't trained on a cohort build of the platform but on the reference cohort (`train.py`), so this scenario runs its own steps (`run_steps`) in place of `run`'s cohort-file ones: training every scenario's baseline, then promoting each through the registry's checks. `--reference-dir` trains on a folder of EEGs on this machine instead of the cohort of `model.toml` (`reference.py`), to export the result as a bundle (`bundle.py`). """ import argparse import os import sys from models.tea.eeg.settings import settings SETTINGS = settings() CLUSTERS = ("auto", "local", "remote", "sync") def _cluster() -> str: """Where training measures the studies: `EEG_ANALYSIS_CLUSTER` when it names a runner, else `auto`.""" chosen = os.environ.get("EEG_ANALYSIS_CLUSTER", "auto") return chosen if chosen in CLUSTERS else "auto" def run_steps(args: argparse.Namespace) -> int: from models.io.registry import PRODUCTION_ALIAS, PromotionBlocked, registered_name, registry from models.tea.eeg import train if args.export or args.estimate or args.store or args.version: print("tea/eeg trains on its reference cohort: only --train and --promote apply", file=sys.stderr) return 2 environment = args.environment.upper() s = SETTINGS if getattr(args, "reference_dir", None): from models.tea.eeg.reference import local s = local(SETTINGS, folder=args.reference_dir, ages_table=args.reference_ages, name=args.reference_name or "local", line_hz=args.line_hz or 60.0) elif getattr(args, "reference_ages", None) or getattr(args, "line_hz", None): print("--reference-ages and --line-hz go with --reference-dir", file=sys.stderr) return 2 versions: dict[str, str] = {} if args.train: for scenario, (version, new) in train.train_all(s, environment, cluster=_cluster()).items(): print(f"{scenario}: {'trained' if new else 'already trained'}, version {version}") versions[scenario] = version if not args.promote: return 0 code = 0 for scenario in SETTINGS.scenarios: version = versions.get(scenario) or getattr(train.latest(scenario, environment), "version", None) if version is None: print(f"{scenario}: no {environment} version to promote; use --train", file=sys.stderr) code = 1 continue try: current = registry.client.get_model_version_by_alias(registered_name("tea", scenario), PRODUCTION_ALIAS) except Exception: # noqa: BLE001 -- no version in production yet current = None if current is not None and str(current.version) == str(version): print(f"{scenario}: version {version} already in production") continue try: registry.promote("tea", scenario, str(version)) print(f"{scenario}: version {version} promoted") except PromotionBlocked as blocked: failed = ", ".join(f"{c.code} ({c.requirement})" for c in blocked.checks if not c.passed) print(f"{scenario}: version {version} not promoted, missing: {failed}", file=sys.stderr) code = 1 return code