Repository navigation
Fix partition_dataset losing items when rounded ratio sizes fall short - #9230
rtmalikian wants to merge 1 commit 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.
-
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.