"""Generic train/test and cross-institution split helpers.""" from sklearn.model_selection import LeaveOneGroupOut, train_test_split from . import config def stratified_holdout(X, y, test_size=0.2, random_state=config.RANDOM_STATE): return train_test_split(X, y, test_size=test_size, stratify=y, random_state=random_state) def leave_one_institution_out(X, y, groups): """Yields (institution_name, train_idx, test_idx) for each held-out institution.""" logo = LeaveOneGroupOut() group_codes, group_names = groups.factorize() for train_idx, test_idx in logo.split(X, y, groups=group_codes): held_out = group_names[group_codes[test_idx[0]]] yield held_out, train_idx, test_idx