{{ message }}
Repository navigation
Conversation
SingleMetricAnnotation.sample with stratify_key computed per-class counts as int(np.round(proportion * n)). With balanced classes and odd n, both classes round up (0.5 * 3 -> 2 each), so the subset contained n + 1 samples. The rounding-discrepancy loop only topped up shortfalls, never trimmed overshoot. Trim the excess so exactly n samples are returned.
1 of 2 tasks
This branch has not been deployed
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.

Issue Link / Problem Description
SingleMetricAnnotation.sample(n, stratify_key=...)can return more thannsamples, violating the documented contract ("A subset of the dataset withnsamples").Root cause:
src/ragas/dataset_schema.py:719computes each class's count asint(np.round(proportion * n)). With two balanced classes and an oddnwhere the half rounds up (e.g.n=3→np.round(0.5 * 3) = 2per class), the per-class counts sum ton + 1. The rounding-discrepancy loop atsrc/ragas/dataset_schema.py:726only tops up shortfalls (while len(sampled_indices) < n); it never trims an overshoot. So on a balanced datasetsample(3, stratify_key=...)returns 4 samples,sample(19, ...)returns 20, etc.Reproduce on
main:Changes Made
src/ragas/dataset_schema.py: after the shortfall top-up loop, trimsampled_indicesdown tonwhen the rounded per-class counts overshot, so the stratified path returns exactlynsamples as documented. (Non-stratified path already usedrandom.sample(..., n)and was correct.)Testing
How to Test
Added
test_loader_sample_stratified_returns_exactly_nintests/unit/test_dataset_schema.py, parametrized overn ∈ [1, 2, 3, 4, 5, 7, 9, 11, 15, 19]on a balanced 10/10 dataset — covering both the overshoot cases (3, 7, 11, 15, 19) and the exact/shortfall cases.Commands and results:
main:pytest tests/unit/test_dataset_schema.py -k stratified_returns_exactly_n→5 failed, 5 passed(e.g.assert 4 == 3…assert 20 == 19)10 passedpytest tests/unit/test_dataset_schema.py→28 passedpytest --nbmake tests/unit→779 passed, 105 skipped, 3 failed, 8 errors— those 3 failures + 8 errors are pre-existing on unpatchedmain(missing optional deps:sacrebleu,haystack, …) and unrelated to this changeruff check src tests docs→All checks passed!;ruff format --checkon the two touched files → already formattedpyright src/ragas/dataset_schema.py→0 errors, 0 warningsReferences
SingleMetricAnnotation.sampledocstring insrc/ragas/dataset_schema.pyScreenshots/Examples (if applicable)
n/a