"""What this module covers: the spikes biomarker's baselines around the per-electrode and per-zone features: training from a feature table that predates them, and loading a baseline saved before they existed. Both implementations, modern and legacy. Fixtures: `old_table`, a DataFrame with only the original seven study-wide spikes features and an age column, as a reference cohort measured before the density features existed; `full_table`, the same rows plus every current feature. """ import importlib import json import numpy as np import pandas as pd import pytest ORIGINAL = ("spike_rate_awake", "spike_rate_nrem", "spike_nrem_activation", "spike_focus_frac", "spike_laterality", "spike_frac_generalized", "spike_index_nrem") MODULES = (("tea.biomarkers.spikes.api", "Spikes"), ("tea.legacy.biomarkers.spikes.api", "LegacySpikes")) @pytest.fixture(scope="module") def old_table(): """Twelve synthetic children with only the study-wide spikes features and their ages.""" rng = np.random.default_rng(0) t = pd.DataFrame({f: rng.uniform(0.0, 2.0, size=12) for f in ORIGINAL}) t["age_months"] = np.linspace(16.0, 36.0, 12) return t @pytest.fixture(scope="module") def full_table(old_table): """`old_table` plus every per-electrode, per-zone and inherited feature the module has now.""" rng = np.random.default_rng(1) t = old_table.copy() cls = getattr(importlib.import_module(MODULES[0][0]), MODULES[0][1]) for f in cls.features: if f not in t.columns: t[f] = rng.uniform(0.0, 2.0, size=len(t)) return t @pytest.mark.parametrize("path,name", MODULES) def test_training_skips_features_the_table_lacks(path, name, old_table, tmp_path): """Pins down: training from a table without the new columns works, fits only the columns it has, and saving then loading that baseline keeps predicting those features only. Fails if `train` starts requiring the new columns, or if save/load stops round-tripping a baseline that knows nothing about them. """ cls = getattr(importlib.import_module(path), name) module = cls().train(old_table) out = module.predict(old_table) assert set(out.feature) == set(ORIGINAL) module.save(tmp_path) loaded = cls.load(tmp_path) assert set(loaded.predict(old_table).feature) == set(ORIGINAL) assert len(cls.features) > len(ORIGINAL) @pytest.mark.parametrize("path,name", MODULES) def test_a_baseline_saved_before_the_new_features_loads_and_predicts(path, name, old_table, full_table, tmp_path): """Pins down: a baseline bundle written before the new features existed (no new parameters in params.json, none of the new features in baseline.json) loads into the current module and predicts; the new features simply have no baseline and are not evaluated. Fails if load starts requiring the new parameters, if a saved baseline stops being readable after the feature list grows, or if predict evaluates a feature that has no fitted baseline. """ cls = getattr(importlib.import_module(path), name) cls().train(old_table).save(tmp_path) params = json.loads((tmp_path / "params.json").read_text()) for k in ("vent_s", "rechazar_transitorio", "herencia_s"): params.pop(k, None) (tmp_path / "params.json").write_text(json.dumps(params)) loaded = cls.load(tmp_path) out = loaded.predict(full_table) assert set(out.feature) == set(ORIGINAL) assert len(out) == len(full_table) * len(ORIGINAL) @pytest.mark.parametrize("path,name", MODULES) def test_a_full_baseline_round_trips(path, name, full_table, tmp_path): """Pins down: with every column present, train fits all features and save/load predicts the same deviations. Fails if the per-electrode and per-zone features stop getting their own baselines when the training table has them. """ cls = getattr(importlib.import_module(path), name) module = cls().train(full_table) before = module.predict(full_table) assert set(before.feature) == set(cls.features) module.save(tmp_path) after = cls.load(tmp_path).predict(full_table) pd.testing.assert_frame_equal(before, after)