Repository navigation
fix(dataset): keep the train item that float rounding drops in split - #2969
Merged
Borda merged 9 commits intoNov 24, 2026
Merged
Conversation
train_test_split multiplied the length by the float ratio and floored, so when the exact product is a whole number the float landed just below it and one train item was lost: 90 items at 0.7 gave 62/28 instead of 63/27. The ratio is now read as the decimal it prints as and multiplied exactly, then floored as before.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2969 +/- ##
=======================================
Coverage 95% 95%
=======================================
Files 84 84
Lines 15548 15556 +8
=======================================
+ Hits 14814 14822 +8
Misses 734 734 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Zero-dimensional float32 ratios still produce 62 training items instead of 63, and the changelog contains a broken placeholder link and a duplicate already-released entry.
3 open issues
What changed in this PR
This PR addresses #2968 by correcting dataset split sizes when floating-point rounding drops a training item from an exact decimal product.
Changes:
- Uses exact decimal-based arithmetic for the shared train split calculation while preserving flooring and shuffle behavior.
- Adds regression coverage for affected ratios, NumPy inputs, ordering, and both dataset split APIs.
- Adds an Unreleased changelog entry and a metrics-fix note.
| File | Description |
|---|---|
tests/dataset/test_utils.py |
Tests exact split sizes, percentage ratios, NumPy inputs, and shuffle behavior. |
tests/dataset/test_core.py |
Adds detection and classification dataset split regressions. |
src/supervision/dataset/utils.py |
Introduces exact ratio arithmetic for the train split index. |
docs/changelog.md |
Documents the split fix and adds a metrics-fix entry. |
Review effort: Balanced
A zero-dimensional float32 array skipped the NumPy-scalar branch and was widened through float(), so np.array(0.7, dtype=np.float32) on 90 items still gave 62 train items instead of 63. Unwrap it to its scalar first and cover it with a regression case. Also link the changelog entry to this PR and drop the roboflow#2941 entry that the merge of develop re-added under Unreleased although it is already released in 0.31.7.
…exact-integer # Conflicts: # docs/changelog.md
…exact-integer # Conflicts: # docs/changelog.md
Changes: - Integrate the current target changes for image handling, annotation, model adapters, metrics, documentation, dependencies, and their tests. - Preserve the dataset split changelog entry alongside the target release notes. Impact: - Keep the PR's split behavior while incorporating the target branch's current fixes and compatibility changes. Verification: - Dataset split integration tests: 150 passed. - Staged merge diff check passed and no unmerged paths remain. Residual limits: - Review findings for computed ratios and changelog coverage remain pending selection. --- Co-authored-by: Codex <[email protected]>
Changes: - Preserve the previous floating-point split size when decimal arithmetic would reduce it, while retaining the decimal rounding correction. - Add computed-thirds regressions and document the keypoint dataset split and evaluated-ratio behavior. Impact: - Computed ratios such as one-third keep their prior training size, while decimal ratios such as 0.7 retain the corrected split. Verification: - Focused dataset tests: 382 passed. - Full suite: 7718 passed, 296 skipped; lint, format, types, and diff checks passed. Residual limits: - Skipped tests and other platforms were not exercised locally. --- Co-authored-by: Codex <[email protected]>
Borda
approved these changes
Nov 24, 2026
Merged
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.


Description
train_test_split(and soDetectionDataset.splitandClassificationDataset.split) computes the train size asint(len(data) * train_ratio). When the exact product is a whole number, the float product can land just below it and the floor drops one train item. For example, 90 images atsplit_ratio=0.7give 62 train / 28 test instead of 63 / 27. Other affected pairs: 100 at 0.57, 100 at 0.29, 180 at 0.35, 100 at 0.58.Motivation and Context
A ratio typed as a decimal should split exactly as that decimal says. Today an item silently moves from train to test for certain (length, ratio) pairs.
Closes #2968
Changes Made
_train_split_index, which reads the ratio as its shortest decimal (Fraction(str(float(ratio))); NumPy floats use their ownstr) and multiplies it exactly, rounding down as before.train_test_splituses the helper. Shuffle, RNG behaviour and ordering are unchanged.Fractionarithmetic (every length 0 to 200 at every integer percent), NumPy scalar ratios, shuffle semantics, andDetectionDataset.split/ClassificationDataset.split(90 images at 0.7 give 63 / 27).Testing
python -m pytest tests/dataset/test_utils.py tests/dataset/test_core.py -k "Exact or float_rounding"develop(utils.py restored): 21 failed, 102 passed. Failing examples:assert (62, 28) == (63, 27), the integer-percent grid for ratios 29, 35, 57, 58, 70 and 82, and both...keeps_image_that_float_rounding_dropssplit tests. The already-correct cases (10 at 0.3, 7 at 0.5, 3 at 0.8) andnp.float32(0.7)pass on both, so existing behaviour is unchanged.pytest tests/dataset src/supervision/dataset(includes doctests): 805 passed.Differential check outside the repo: over every length 0 to 300 and every ratio k/1000 the new code equals exact
n*k//1000(0 mismatches); it differs from the old code in 20 cells and never gives a smaller train part than before.pre-commithooks on the changed files (ruff, ruff-format, docformatter, mdformat, mypy, codespell): pass.Added/updated tests. The dataset suite passes locally; the full suite was not run because
tests/metricsneeds thefaster_coco_eval/pycocotoolsextras, which are not installed here.Updated docs (docstrings / mkdocs entry) for new or changed public API (no public API change)
Added a changelog entry in
docs/changelog.mdunderUnreleasedAdditional Notes
str(float)prints, not as its binary value. A ratio such as0.1 + 0.2is read as0.30000000000000004and floors normally.str, sonp.float32(0.7)reads as0.7and keeps giving 63 (a plainfloat()cast would give 0.699999988 and regress it to 62).round(), which would change behaviour for ratios that are not whole-number products.