Repository navigation
Fix partition_dataset losing items when rounded ratio sizes fall short - #9230
rtmalikian wants to merge 2 commits into
Conversation
…ounded partition_dataset rounded each partition size independently, so the rounded sizes could sum to less than len(data) and trailing items ended up in no partition. Assign any rounding remainder to the last partition. Fixes Project-MONAI#9220 Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
Walkthrough
Merge Risk:
|
There was a problem hiding this comment.
🔇 Additional comments: 1
monai/data/utils.py-1212-1212: 1
1212-1212:Minor · Functional Correctness · Quick win
⚠️ Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.Keep zero-ratio partitions empty.
With 10 items and ratios
[1, 1, 1, 0],sizesis[3, 3, 3, 0]. This line assigns the remaining item to the zero-ratio partition. Add the shortfall to the last partition with a positive ratio, and test this case. (raw.githubusercontent.com)As per path instructions, “Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these.”
Source: Path instructions

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Project-MONAI/MONAI
- Branch: dev
- File: .coderabbit.yaml
- Commit: e0cee14
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
f8b38bb3-6ea5-44ca-8642-82e1304678f7
Files selected for processing: 2-
monai/data/utils.pytests/data/test_partition_dataset.py
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
-
…ng remainder CodeRabbit review on Project-MONAI#9230 noted the remainder assignment goes to the last partition even when its ratio is 0 (ratios [1, 1, 1, 0] on 10 items rounds to 3 + 3 + 3 + 0, and the leftover item landed in the empty zero-ratio partition). The remainder now goes to the last partition with a positive ratio instead. Regression test added (TEST_CASE_13); 13/13 partition tests pass, and the new case fails on the pre-fix code. Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
|
Good catch, thank you — you are right that the remainder could land in a zero-ratio partition. Fixed in commit 5712ee0: the rounding remainder now goes to the last partition with a positive ratio, so a zero-ratio partition stays empty. Added a regression test for ratios [1, 1, 1, 0] on 10 items; all 13 partition tests pass, and the new case fails on the pre-fix code. |
There was a problem hiding this comment.
🔇 Additional comments: 1
monai/data/utils.py-1217-1217: 1
1217-1217:Major · Functional Correctness · Heavy lift
⚠️ Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.Distribute the shortfall across partitions.
With two items and ratios
[1, 1, 1, 1, 1, 0], this assigns both items to one partition, although two positive-ratio partitions could each receive one.The effect also compounds in
partition_dataset_classes: with one item per class and ratios[1, 1, 1, 0], every item goes to the third partition. The first two partitions are empty. Coordinate remainder allocation across classes as well as across partitions.

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Project-MONAI/MONAI
- Branch: dev
- File: .coderabbit.yaml
- Commit: e0cee14
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
ede5ccc7-4008-433f-98ab-e8d52adfd6ac
Files selected for processing: 2-
monai/data/utils.pytests/data/test_partition_dataset.py
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
-
|
Good point, and thanks for checking the small-data cases — I verified the arithmetic: with ratios [1, 1, 1, 1, 1, 0] on 2 items, the current fix does put both items in the last positive-ratio partition, and the same rounding concentrates each single-item class in the third partition for I kept this fix deliberately narrow — the reported issue was items being lost entirely, and the remainder-to-last-positive-partition rule guarantees every item lands in exactly one partition while keeping zero-ratio partitions empty. Spreading the shortfall (e.g. largest-remainder, or coordinating across classes) would change the allocation for the original cases too, so I'd rather not expand the scope on a bot suggestion alone. Maintainers: would you like the remainder distributed across positive-ratio partitions within this PR, or is the no-loss guarantee enough here, with the balancing as a follow-up? Happy to implement whichever you prefer. |
Fixes #9220.
Description
partition_datasetrounded each partition's size independently (int(r / rsum * data_len + 0.5)), so the rounded sizes could sum to less thanlen(data)and trailing items ended up in no partition — e.g. ratios[1, 1, 1]on 10 items gave[[0, 1, 2], [3, 4, 5], [6, 7, 8]](item 9 lost), and[0.8, 0.1, 0.1]on 63 items gave sizes[50, 6, 6](one case lost). The fix computes the rounded sizes first and, only when they fall short ofdata_len, assigns the remainder to the last partition, so every item lands in exactly one partition. All other behaviour is unchanged: the docstring example ([0.6, 0.2, 0.2]on 5 items) and the overshoot case (capped atdata_len) behave exactly as before.partition_dataset_classesbenefits automatically since it delegates to this function.Types of changes
./runtests.sh -f -u --net --coverage.make htmlcommand in thedocs/folder.Verification
PYTHONPATH=. python -m pytest tests/data/test_partition_dataset.py tests/data/test_partition_dataset_classes.py -q: 16 passed (12 partition cases incl. 4 new, 4 classes cases).[0.8, 0.1, 0.1]on 63 items (shuffled, seed 0) gives sizes[50, 6, 7]with zero lost items;[1, 1, 1]on 10 items gives[[0, 1, 2], [3, 4, 5], [6, 7, 8, 9]].[0.8, 0.1, 0.1],[1, 1, 1], and[1, 1, 1, 1, 1], every length from 1 to 1000 partitions with no lost or duplicated items.ruff checkclean on both touched files;ruff format --checkclean.Additional Notes
Author
Raphael Malikian, rtmalikian@gmail.com — If you'd like similar help with your own project or codebase, I'm open to collaborate — feel free to reach out.
This PR was prepared with AI assistance.