Download scripts/check_leakage.py from Laibaff7/cda-emr-classifier: direct link, hf CLI and curl.
- Browser
- Download file 4.28 kB
-
https://huggingface.co/Laibaff7/cda-emr-classifier/resolve/main/scripts/check_leakage.py
- Command line
-
hf download hf://Laibaff7/cda-emr-classifier/scripts/check_leakage.py
-
curl -L -o check_leakage.py https://huggingface.co/Laibaff7/cda-emr-classifier/resolve/main/scripts/check_leakage.py
4.28 kB
| """ | |
| Data-leakage audit for the EMR classifier. | |
| Checks | |
| 1. POSITIVE LEAKAGE -- do any of the 34 training EMR spectra (by SCLK) appear | |
| in the 130,233-row inference pool? (must be 0) | |
| 2. NEGATIVE OVERLAP -- the 1000 training negatives were sampled FROM the | |
| inference pool. Quantify how many of the ranked candidates (top-100 and | |
| full list) fall inside that 1000-sample training-negative set. | |
| The 1000 negative row-indices are reproduced exactly from the training config | |
| (same seed / pool size / sample size imported from train_emr_classifier). | |
| """ | |
| import json | |
| import os | |
| import numpy as np | |
| import pandas as pd | |
| HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # EMR Classifier/ | |
| ROOT = os.path.dirname(HERE) | |
| import sys | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| from train_emr_classifier import DATA_SEED, N_NEGATIVES # noqa: E402 | |
| POS_PATH = os.path.join(ROOT, 'Data Parquet', 'training data for extended mass range.parquet') | |
| CAND_PATH = os.path.join(HERE, 'predictions', 'emr_candidates_unknown.csv') | |
| TOP_PATH = os.path.join(HERE, 'predictions', 'emr_top100.csv') | |
| OUT_PATH = os.path.join(HERE, 'predictions', 'leakage_report.json') | |
| def main(): | |
| cand = pd.read_csv(CAND_PATH) # original_parquet_index, sclk, emr_probability | |
| top100 = pd.read_csv(TOP_PATH) | |
| pos = pd.read_parquet(POS_PATH, columns=['sclk']) | |
| n_pool = len(cand) | |
| pos_sclks = set(pos['sclk'].tolist()) | |
| inf_sclks = set(cand['sclk'].tolist()) | |
| # Reproduce the exact 1000 training-negative row indices | |
| rng = np.random.default_rng(DATA_SEED) | |
| neg_idx = set(rng.choice(n_pool, size=N_NEGATIVES, replace=False).tolist()) | |
| # --- Check 1: positive leakage (SCLK overlap) --- | |
| pos_leak = sorted(pos_sclks & inf_sclks) | |
| # --- Check 2: negative overlap --- | |
| top100_in_neg = top100['original_parquet_index'].isin(neg_idx) | |
| all_in_neg = cand['original_parquet_index'].isin(neg_idx) | |
| # How the negative overlap correlates with the ranking | |
| cand = cand.copy() | |
| cand['was_training_negative'] = cand['original_parquet_index'].isin(neg_idx) | |
| mean_p_neg = cand.loc[cand['was_training_negative'], 'emr_probability'].mean() | |
| mean_p_pos = cand.loc[~cand['was_training_negative'], 'emr_probability'].mean() | |
| report = { | |
| 'pool_size': int(n_pool), | |
| 'n_positives': int(len(pos_sclks)), | |
| 'n_training_negatives': int(len(neg_idx)), | |
| 'check1_positive_sclk_overlap': { | |
| 'count': int(len(pos_leak)), | |
| 'sclks': pos_leak, | |
| 'status': 'PASS' if len(pos_leak) == 0 else 'FAIL', | |
| }, | |
| 'check2_negative_overlap': { | |
| 'top100_that_were_training_negatives': int(top100_in_neg.sum()), | |
| 'top100_total': int(len(top100)), | |
| 'full_list_that_were_training_negatives': int(all_in_neg.sum()), | |
| 'full_list_total': int(n_pool), | |
| 'mean_prob_training_negatives': float(mean_p_neg), | |
| 'mean_prob_other': float(mean_p_pos), | |
| }, | |
| } | |
| with open(OUT_PATH, 'w') as f: | |
| json.dump(report, f, indent=2) | |
| print('=' * 68) | |
| print('EMR CLASSIFIER -- DATA LEAKAGE AUDIT') | |
| print('=' * 68) | |
| print(f"Pool size : {n_pool:,}") | |
| print(f"Training positives : {len(pos_sclks)}") | |
| print(f"Training negatives : {len(neg_idx)} (sampled from the pool)") | |
| print('-' * 68) | |
| c1 = report['check1_positive_sclk_overlap'] | |
| print(f"[1] Positive SCLK overlap with inference pool : {c1['count']} -> {c1['status']}") | |
| if c1['sclks']: | |
| print(f' leaked SCLKs: {c1["sclks"]}') | |
| print('-' * 68) | |
| c2 = report['check2_negative_overlap'] | |
| print(f"[2] Top-100 candidates that were training negatives : " | |
| f"{c2['top100_that_were_training_negatives']}/{c2['top100_total']}") | |
| print(f" Full list that were training negatives : " | |
| f"{c2['full_list_that_were_training_negatives']:,}/{c2['full_list_total']:,} " | |
| f"(= the {len(neg_idx)} sampled, by construction)") | |
| print(f" Mean EMR prob -- training negatives : {mean_p_neg:.4f}") | |
| print(f" Mean EMR prob -- other rows : {mean_p_pos:.4f}") | |
| print('-' * 68) | |
| print(f'Report saved: {OUT_PATH}') | |
| if __name__ == '__main__': | |
| main() |