Skip to content

Make split_by_patient independent of sample order - #1286

Merged
jhnwu3 merged 1 commit into
sunlabuiuc:masterfrom
solarsys:fix/deterministic-patient-split
Oct 11, 2026
Merged

jhnwu3 merged 1 commit into
sunlabuiuc:masterfrom
solarsys:fix/deterministic-patient-split

Conversation

@solarsys

@solarsys solarsys commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

Why

split_by_patient shuffles list(dataset.patient_to_index.keys()), so the patients it picks for a seed depend on the order of the samples, not only on the seed. Build the same samples in a different order and the same seed gives a different train/val/test assignment. create_sample_dataset, subset, sample_balanced and custom pipelines all pass samples through in whatever order they arrive. PatientSplit (the set_task(split=...) path) already sorts patient IDs before its shuffle; the split_by_* functions did not.

The same pattern was in split_by_patient_conformal, split_by_patient_conformal_tuh and split_by_patient_tuh.

What

  • Sort patient IDs (key=str, as PatientSplit does) before the seeded shuffle in those four functions. The rest of each function is unchanged.
  • Docs: a short note on docs/api/datasets/pyhealth.datasets.splitter.rst.
  • Example: examples/split_by_patient_order.py builds the same samples in two orders and shows the test patients match.

Behaviour change: for a given seed, these functions can now select different patients than earlier releases did. Results published with a fixed seed will not reproduce exactly across this change; after it, they reproduce regardless of sample order.

split_by_sample / split_by_visit shuffle sample positions, so they depend on sample order by design and are left alone.

Tests

  • New tests/core/test_split_by_patient_order.py (synthetic samples only): the same samples in two orders give the same patients per part for split_by_patient and split_by_patient_conformal, and a different seed still changes the split. On master the two order tests fail; with this change they pass.
  • tests/core/test_split_set_task.py and tests/core/test_xgboost_model.py (which uses split_by_patient) pass.
  • tools/check_pr_rules.py and ruff pass.

PR #1251 also edits splitter.py (ratio validation); the two changes touch different lines.

🤖 Generated with Claude Code

split_by_patient, split_by_patient_conformal and the two TUH patient
splitters shuffled patients in dict order, i.e. the order of the
samples. The same seed therefore picked different patients whenever the
samples arrived in another order. Sort the IDs first, as PatientSplit
already does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@solarsys
solarsys requested a review from jhnwu3 October 9, 2026 17:18

@jhnwu3 jhnwu3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@jhnwu3
jhnwu3 merged commit bd256a4 into sunlabuiuc:master Oct 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants