Skip to content

LK007 — label_definition

Labels that depend on information outside the outcome window.

default severity error
injector ehrlint.inject.inject_lk007

Why this matters

If a subject is labelled positive on the strength of an outcome code that falls outside the declared window, the label is not the label the task defines. The model is then trained toward a target nobody specified, and the reported task definition does not describe what was learned.

What it looks like in practice

A label built from a registry flag or a chart review that used the whole record, then attached to an index date two years earlier. The label is correct about the patient and wrong about the prediction problem.

The query it runs

WITH in_window AS (
  SELECT DISTINCT e.subject_id FROM events e
  JOIN index_dates i USING (subject_id)
  WHERE e.code IN outcome_codes AND e.time > i.index_date
    AND DATEDIFF('day', i.index_date, e.time) <= 730.5
)
SELECT * FROM labels l WHERE l.label = TRUE
  AND l.subject_id NOT IN (SELECT subject_id FROM in_window)

Worked example

On the clean synthetic cohort, which is clean by construction:

$ ehrlint audit --data clean/ --task task.yaml --checks LK007

LK007 did not fire.
  Every positive label is supported by an outcome code strictly after the index date and within 2y.
exit code 0

With the matching leak planted by ehrlint.inject.inject_lk007:

$ ehrlint audit --data leaky/ --task task.yaml --checks LK007

LK007 label_definition  [error]
  4 subject(s) are labelled positive with no outcome code strictly after the index date and within 2y of it, so the label was ascertained from information the task definition does not describe. An occurrence stamped at exactly the index date does not count here -- it is not predictable from that moment, and LK002 reports it.
  affected: 4 of 120 subject(s) (3.3%), 4 row(s)

  example rows:
    subject_id  label  outcome_in_window  horizon  why                     
    S000006     True   False              2y       positive label with no o
    S000060     True   False              2y       positive label with no o
    S000065     True   False              2y       positive label with no o

exit code 1

Reproduce both:

ehrlint synth --out clean/ --n-subjects 120 --seed 0
ehrlint synth --out leaky/ --n-subjects 120 --seed 0 --inject LK007
ehrlint audit --data leaky/ --task leaky/task.yaml \
  --labels leaky/labels.parquet --splits leaky/splits.json --checks LK007

What to do about it

Derive labels from events inside the declared window only, and keep the derivation in the same code path the audit sees.

When it skips

A skip is not a pass. This check reports a skip, with the reason, when its inputs are not available — see reading a report.