Files
rarelens/ml/tests/test_train.py
T
Kemal Yaylali 197975cc42 feat(ml): train a real model, and report the number that matters rather than the flattering one
"Variants are unscored" was accurate: nothing was ever trained, so a quarter of every rank was
dead weight and the UI leaked a connection error at the reader.

- scripts/make-training-set.sh derives a training table from ClinVar directly. ClinVar already
  carries the molecular consequence, the gene and an allele frequency, which is the feature set
  serving sends, so this avoids running VEP over hundreds of thousands of variants. 2-star
  records only.
- train.py now holds out whole genes (GroupShuffleSplit). docs/data.md had said to do this since
  the data pass; the code was still doing a random split, which is the leak Grimm 2015 describes.
- evaluate() reports missense on its own. On the last run: AUROC 0.986 over 74,239 held-out
  variants, but 0.872 over the 13,553 missense ones, and the docs say plainly why even that is
  flattered — within missense the only live feature is allele frequency, and ClinVar's benign
  calls often use allele frequency as evidence (ACMG BA1/BS1), so the feature partly caused the
  label.
- the 503 now names what is missing (model@alias via tracking URI) and leaves the exception in
  the server log instead of the UI.
- make training-set / make train; the 58 MB table is gitignored.

Verified end to end: model registered as v2, the simulated NF2 case scores 0.999 on the planted
variant, and it now ranks 1.00 with all four components live.

Tests: api 77, ml 22, loader 16, web 32; ruff, mypy, svelte-check clean.
2026-09-12 09:13:54 +01:00

169 lines
6.0 KiB
Python

from pathlib import Path
import numpy as np
import pandas as pd
import pytest
from rarelens_ml.train import label, read_vep_tab
HEADER = [
"Uploaded_variation", "Location", "Allele", "Consequence", "IMPACT", "SYMBOL",
"gnomADe_AF", "CLIN_SIG", "CADD_PHRED", "am_pathogenicity",
]
def write_vep_tab(path: Path, rows: list[list[str]]) -> Path:
lines = [
"## ENSEMBL VARIANT EFFECT PREDICTOR v113.0",
"## Column descriptions:",
"#" + "\t".join(HEADER),
*("\t".join(r) for r in rows),
]
path.write_text("\n".join(lines) + "\n")
return path
@pytest.mark.parametrize(
("clin_sig", "expected"),
[
# VEP writes lowercase, comma-separated terms from co-located ClinVar records.
("pathogenic", 1),
("pathogenic,likely_pathogenic", 1),
("likely_benign", 0),
("benign,likely_benign", 0),
# ClinVar VCF CLNSIG spelling must keep working too.
("Pathogenic/Likely_pathogenic", 1),
("Benign", 0),
("uncertain_significance", None),
("pathogenic,benign", None), # conflicting evidence is not a label
("-", None),
("", None),
(np.nan, None),
],
)
def test_label(clin_sig: object, expected: int | None) -> None:
assert label(clin_sig) == expected
def test_read_vep_tab_uses_the_hash_header_and_keeps_dashes(tmp_path: Path) -> None:
tsv = write_vep_tab(
tmp_path / "x.vep.tsv",
[["22_1_A_G", "22:1", "G", "missense_variant", "MODERATE", "TBX1", "-", "pathogenic", "28", "0.9"]],
)
df = read_vep_tab(tsv)
assert list(df.columns) == HEADER
assert df.loc[0, "gnomADe_AF"] == "-"
assert df.loc[0, "CLIN_SIG"] == "pathogenic"
def test_load_returns_raw_serving_columns_and_labels(tmp_path: Path) -> None:
from rarelens_ml.features import RAW_COLUMNS
from rarelens_ml.train import load
tsv = write_vep_tab(
tmp_path / "x.vep.tsv",
[
["a", "22:1", "G", "missense_variant", "MODERATE", "TBX1", "0.0001", "pathogenic", "28", "0.9"],
["b", "22:2", "A", "synonymous_variant", "LOW", "CHEK2", "0.12", "benign", "3", "-"],
["c", "22:3", "T", "intron_variant", "MODIFIER", "CHEK2", "0.3", "uncertain_significance", "1", "-"],
],
)
X, y, genes = load(str(tsv))
assert list(X.columns) == RAW_COLUMNS
assert y.tolist() == [1, 0] # the VUS row is dropped
assert list(genes) == ["TBX1", "CHEK2"]
def test_logged_model_returns_probabilities_from_raw_columns(tmp_path: Path) -> None:
"""The registered model must take the raw columns serving sends and return P(pathogenic)."""
import mlflow
from rarelens_ml.train import fit, log_and_register
rng = np.random.default_rng(0)
n = 400
impact = rng.choice(["HIGH", "MODERATE", "LOW", "MODIFIER"], n)
y = pd.Series(((impact == "HIGH") | (rng.random(n) < 0.1)).astype(int))
X = pd.DataFrame({
"impact": impact,
"consequence": rng.choice(["stop_gained", "missense_variant", "intron_variant"], n),
"gnomad_af": rng.random(n).round(4).astype(str), # strings, as read from the DB
"cadd_phred": (rng.random(n) * 40).round(1).astype(str),
"am_pathogenicity": "-",
})
mlflow.set_tracking_uri(f"sqlite:///{tmp_path}/mlflow.db")
mlflow.set_experiment("test")
clf = fit(X, y)
version = log_and_register(clf, model_name="rarelens-test", alias="production")
model = mlflow.pyfunc.load_model("models:/rarelens-test@production")
scores = np.asarray(model.predict(X.head(50)))
assert version == "1"
assert scores.shape == (50,)
assert ((scores >= 0) & (scores <= 1)).all()
assert not set(np.unique(scores)) <= {0.0, 1.0}, "got class labels, expected probabilities"
def test_load_returns_the_gene_of_each_row_for_grouping(tmp_path: Path) -> None:
from rarelens_ml.train import load
tsv = write_vep_tab(
tmp_path / "x.vep.tsv",
[
["a", "22:1", "G", "missense_variant", "MODERATE", "NF2", "0.0001", "pathogenic", "28", "0.9"],
["b", "22:2", "A", "synonymous_variant", "LOW", "CHEK2", "0.12", "benign", "3", "-"],
],
)
_, _, genes = load(str(tsv))
assert list(genes) == ["NF2", "CHEK2"]
def test_the_split_never_puts_one_gene_on_both_sides(tmp_path: Path) -> None:
"""Random splits leak: a model can learn the gene instead of the variant (Grimm 2015)."""
import numpy as np
import pandas as pd
from rarelens_ml.train import split_by_gene
genes = pd.Series([f"GENE{i // 4}" for i in range(40)])
X = pd.DataFrame({"impact": ["HIGH"] * 40, "consequence": ["stop_gained"] * 40,
"gnomad_af": ["0"] * 40, "cadd_phred": ["10"] * 40, "am_pathogenicity": ["-"] * 40})
y = pd.Series(np.tile([1, 0], 20))
Xtr, Xte, _ytr, yte, train_genes, test_genes = split_by_gene(X, y, genes, test_size=0.3)
assert set(train_genes) & set(test_genes) == set()
assert len(Xtr) + len(Xte) == 40
assert len(yte) > 0
def test_evaluate_reports_missense_separately() -> None:
"""Overall AUROC flatters a consequence-based model; missense is where the problem is."""
import numpy as np
import pandas as pd
from rarelens_ml.train import evaluate
rng = np.random.default_rng(0)
consequence = ["missense_variant"] * 200 + ["stop_gained"] * 200
X = pd.DataFrame({"consequence": consequence})
y = pd.Series([*rng.integers(0, 2, 200), *([1] * 200)])
proba = np.concatenate([rng.random(200), rng.uniform(0.8, 1.0, 200)])
metrics = evaluate(X, y, proba)
assert metrics["missense_variants"] == 200
assert "auroc_missense" in metrics
assert metrics["auroc"] > metrics["auroc_missense"] # the easy class inflates the headline
def test_evaluate_omits_the_missense_metric_when_there_is_nothing_to_measure() -> None:
import numpy as np
import pandas as pd
from rarelens_ml.train import evaluate
X = pd.DataFrame({"consequence": ["stop_gained"] * 100})
y = pd.Series([1] * 50 + [0] * 50)
metrics = evaluate(X, y, np.linspace(0, 1, 100))
assert "auroc_missense" not in metrics