| """ |
| Temporal Split Verification Script |
| Ensures: |
| - No patient appears in both training and test sets |
| - Training and test weeks are disjoint per disease |
| - Test set for each disease contains at least 10 samples |
| """ |
|
|
| import pandas as pd |
| import json |
| from pathlib import Path |
| from collections import defaultdict |
|
|
| |
| |
| |
|
|
| METADATA_PATH = Path("../metadata.csv") |
| MANIFEST_PATH = Path("../data/manifest.json") |
|
|
| |
| |
| TEMPORAL_SPLITS = { |
| 'Z00': {9, 10, 11}, |
| 'E11': {1, 2, 3}, |
| 'K29': {1, 2, 3}, |
| 'K76': {2, 3, 4}, |
| 'B18': {2, 3, 10}, |
| 'C34': {8, 9, 10}, |
| 'N18': {2, 3, 4}, |
| 'J44': {5, 6, 7, 11}, |
| 'A15': {13} |
| } |
|
|
|
|
| |
| |
| |
|
|
| def load_metadata(): |
| """Load metadata CSV.""" |
| return pd.read_csv(METADATA_PATH) |
|
|
|
|
| def validate_patient_no_leakage(metadata): |
| """Check that no patient appears in both train and test sets.""" |
| errors = [] |
| |
| for disease in metadata['Diagnosis'].unique(): |
| disease_patients = metadata[metadata['Diagnosis'] == disease] |
| test_weeks = TEMPORAL_SPLITS.get(disease, set()) |
| |
| |
| test_patients = disease_patients[disease_patients['Week'].isin(test_weeks)]['Patient_id'].tolist() |
| |
| |
| train_patients = disease_patients[~disease_patients['Week'].isin(test_weeks)]['Patient_id'].tolist() |
| |
| |
| overlap = set(test_patients) & set(train_patients) |
| if overlap: |
| errors.append(f"Patient leakage in {disease}: patients {overlap} appear in both train and test") |
| |
| if errors: |
| print(f"❌ Patient leakage validation FAILED: {len(errors)} errors") |
| for err in errors[:5]: |
| print(f" {err}") |
| return False |
| |
| print("✅ Patient leakage validation PASSED: no patient appears in both train and test") |
| return True |
|
|
|
|
| def validate_train_test_weeks_disjoint(metadata): |
| """Check that training and test weeks are disjoint per disease.""" |
| errors = [] |
| |
| for disease in metadata['Diagnosis'].unique(): |
| disease_patients = metadata[metadata['Diagnosis'] == disease] |
| test_weeks = TEMPORAL_SPLITS.get(disease, set()) |
| all_weeks = set(disease_patients['Week'].unique()) |
| train_weeks = all_weeks - test_weeks |
| |
| |
| overlap = test_weeks & train_weeks |
| if overlap: |
| errors.append(f"Week overlap in {disease}: weeks {overlap} appear in both train and test") |
| |
| |
| missing_test_weeks = len(test_weeks - all_weeks)>2 |
| if missing_test_weeks: |
| errors.append(f"Missing test weeks in {disease}: {missing_test_weeks} not present in data") |
| |
| if errors: |
| print(f"❌ Week disjointness validation FAILED: {len(errors)} errors") |
| for err in errors[:5]: |
| print(f" {err}") |
| return False |
| |
| print("✅ Week disjointness validation PASSED: train and test weeks are disjoint per disease") |
| return True |
|
|
|
|
| def validate_test_set_size(metadata): |
| """Check that test set for each disease contains at least 10 samples.""" |
| errors = [] |
| |
| for disease in metadata['Diagnosis'].unique(): |
| disease_patients = metadata[metadata['Diagnosis'] == disease] |
| test_weeks = TEMPORAL_SPLITS.get(disease, set()) |
| |
| test_patients = disease_patients[disease_patients['Week'].isin(test_weeks)] |
| n_test = len(test_patients) |
| |
| if n_test < 10: |
| errors.append(f"Test set too small in {disease}: {n_test} samples (min 10 required)") |
| |
| if errors: |
| print(f"❌ Test set size validation FAILED: {len(errors)} errors") |
| for err in errors: |
| print(f" {err}") |
| return False |
| |
| |
| print("\nTest set sizes per disease:") |
| for disease in metadata['Diagnosis'].unique(): |
| test_weeks = TEMPORAL_SPLITS.get(disease, set()) |
| disease_patients = metadata[metadata['Diagnosis'] == disease] |
| test_patients = disease_patients[disease_patients['Week'].isin(test_weeks)] |
| print(f" {disease}: {len(test_patients)} samples") |
| |
| print("\n✅ Test set size validation PASSED: all diseases have ≥10 test samples") |
| return True |
|
|
|
|
| def validate_manifest_consistency(metadata): |
| """Check that all patients in metadata have a corresponding JSON file.""" |
| |
| with open(MANIFEST_PATH, 'r') as f: |
| manifest = json.load(f) |
| |
| manifest_ids = set(int(entry['patient_id']) for entry in manifest['files']) |
| metadata_ids = set(metadata['Patient_id']) |
| |
| |
| missing_in_manifest = metadata_ids - manifest_ids |
| if missing_in_manifest: |
| print(f"⚠️ {len(missing_in_manifest)} patients in metadata not found in manifest") |
| return False |
| |
| |
| extra_in_manifest = manifest_ids - metadata_ids |
| if extra_in_manifest: |
| print(f"⚠️ {len(extra_in_manifest)} patients in manifest not found in metadata") |
| return False |
| |
| print(f"✅ Manifest consistency PASSED: {len(manifest_ids)} patients match") |
| return True |
|
|
|
|
| def main(): |
| print("=" * 60) |
| print("S-OH TEMPORAL SPLIT VALIDATION") |
| print("=" * 60) |
| |
| metadata = load_metadata() |
| print(f"Loaded {len(metadata)} records") |
| |
| all_passed = True |
| |
| print("\n" + "-" * 40) |
| all_passed &= validate_manifest_consistency(metadata) |
| |
| print("\n" + "-" * 40) |
| all_passed &= validate_patient_no_leakage(metadata) |
| |
| print("\n" + "-" * 40) |
| all_passed &= validate_train_test_weeks_disjoint(metadata) |
| |
| print("\n" + "-" * 40) |
| all_passed &= validate_test_set_size(metadata) |
| |
| print("\n" + "=" * 60) |
| if all_passed: |
| print("✅ ALL CHECKS PASSED") |
| else: |
| print("❌ SOME CHECKS FAILED") |
| print("=" * 60) |
| |
| return all_passed |
|
|
|
|
| if __name__ == "__main__": |
| main() |