Repository navigation
Make split_by_patient independent of sample order - #1286
Merged
jhnwu3 merged 1 commit intoOct 11, 2026
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
split_by_patientshuffleslist(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_balancedand custom pipelines all pass samples through in whatever order they arrive.PatientSplit(theset_task(split=...)path) already sorts patient IDs before its shuffle; thesplit_by_*functions did not.The same pattern was in
split_by_patient_conformal,split_by_patient_conformal_tuhandsplit_by_patient_tuh.What
key=str, asPatientSplitdoes) before the seeded shuffle in those four functions. The rest of each function is unchanged.docs/api/datasets/pyhealth.datasets.splitter.rst.examples/split_by_patient_order.pybuilds 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_visitshuffle sample positions, so they depend on sample order by design and are left alone.Tests
tests/core/test_split_by_patient_order.py(synthetic samples only): the same samples in two orders give the same patients per part forsplit_by_patientandsplit_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.pyandtests/core/test_xgboost_model.py(which usessplit_by_patient) pass.tools/check_pr_rules.pyand ruff pass.PR #1251 also edits
splitter.py(ratio validation); the two changes touch different lines.🤖 Generated with Claude Code