Skip to content

fix(dataset): keep the train item that float rounding drops in split - #2969

Merged
Borda merged 9 commits into
roboflow:developfrom
avanish-garg:fix/split-ratio-exact-integer
Nov 24, 2026
Merged

Borda merged 9 commits into
roboflow:developfrom
avanish-garg:fix/split-ratio-exact-integer

Conversation

@avanish-garg

Copy link
Copy Markdown
Contributor

Description

train_test_split (and so DetectionDataset.split and ClassificationDataset.split) computes the train size as int(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 at split_ratio=0.7 give 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.

from supervision.dataset.utils import train_test_split

train, test = train_test_split(list(range(90)), train_ratio=0.7, shuffle=False)
len(train), len(test)  # (62, 28) on develop, (63, 27) with this change

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

  • Add _train_split_index, which reads the ratio as its shortest decimal (Fraction(str(float(ratio))); NumPy floats use their own str) and multiplies it exactly, rounding down as before.
  • train_test_split uses the helper. Shuffle, RNG behaviour and ordering are unchanged.
  • Tests for the known-bad pairs, a grid check against exact Fraction arithmetic (every length 0 to 200 at every integer percent), NumPy scalar ratios, shuffle semantics, and DetectionDataset.split / ClassificationDataset.split (90 images at 0.7 give 63 / 27).
  • Changelog entry under Unreleased.
  • Out of scope: any change to the accepted ratio range or the error messages.

Testing

  • python -m pytest tests/dataset/test_utils.py tests/dataset/test_core.py -k "Exact or float_rounding"

    • with this change: 123 passed
    • on 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_drops split tests. The already-correct cases (10 at 0.3, 7 at 0.5, 3 at 0.8) and np.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-commit hooks 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/metrics needs the faster_coco_eval/pycocotools extras, 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.md under Unreleased

Additional Notes

  • The ratio is interpreted as the decimal that str(float) prints, not as its binary value. A ratio such as 0.1 + 0.2 is read as 0.30000000000000004 and floors normally.
  • NumPy float scalars use their own str, so np.float32(0.7) reads as 0.7 and keeps giving 63 (a plain float() cast would give 0.699999988 and regress it to 62).
  • Considered and rejected: adding an epsilon before flooring, or round(), which would change behaviour for ratios that are not whole-number products.

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.
@Borda
Borda requested a balanced review from Copilot November 13, 2026 06:58
@Borda Borda added the bug Something isn't working label Nov 13, 2026
@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95%. Comparing base (effb67d) to head (e92ab07).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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

Comment thread src/supervision/dataset/utils.py Resolved
Comment thread docs/changelog.md Resolved
Comment thread docs/changelog.md Resolved
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
Borda and others added 2 commits November 23, 2026 23:13
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
Borda merged commit 2530183 into roboflow:develop Nov 24, 2026
37 checks passed
@Borda Borda mentioned this pull request Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DetectionDataset.split loses a train item when len(images) * split_ratio is a whole number (90 images at 0.7 give 62/28, not 63/27)

3 participants