Fix calc_stats dataset mutation causing incorrect diff_std computation#103
Open
RajdeepKushwaha5 wants to merge 1 commit into
Open
Fix calc_stats dataset mutation causing incorrect diff_std computation#103RajdeepKushwaha5 wants to merge 1 commit into
RajdeepKushwaha5 wants to merge 1 commit into
Conversation
The calc_stats function mutated the ds variable inside the loop over statistics_config.ops. When a diff_* operation was encountered, the original dataset was overwritten with ds.diff(), causing subsequent diff_* operations to apply diff() on already-diffed data. With ops=[mean, std, diff_mean, diff_std], diff_std incorrectly computed std(diff(diff(ds))) instead of std(diff(ds)). Fix: use a per-iteration local variable ds_op instead of mutating ds. Closes mllam#102
There was a problem hiding this comment.
Pull request overview
Fixes a logic error in calc_stats where encountering a diff_* op would overwrite the dataset used for subsequent ops, leading to incorrect results (e.g., diff_std computing a double diff when diff_mean appears earlier in the same ops list). This aligns behavior with the default YAML configs that compute both base and diff-based statistics.
Changes:
- Avoid reassigning/mutating the loop dataset by introducing a per-iteration
ds_opvariable incalc_stats. - Add regression tests covering multiple
diff_*ops in a singleopslist (verifiesdiff_stduses a single diff of the original dataset). - Document the fix in the unreleased CHANGELOG (links to issue #102).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
mllam_data_prep/ops/statistics.py |
Ensures each statistic op runs against the intended (original) dataset by using a per-iteration dataset variable (ds_op). |
tests/test_calc_stats_fix.py |
Adds regression coverage to prevent reintroducing the incorrect diff_std behavior. |
CHANGELOG.md |
Records the bugfix under “Fixes” in the unreleased section. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Describe your changes
The
calc_statsfunction inmllam_data_prep/ops/statistics.pymutates thedsvariable inside the loop overstatistics_config.ops. When adiff_*operation is encountered, the original dataset is overwritten:This means any subsequent
diff_*operation in the same loop applies.diff()on an already-diffed dataset instead of the original.With the default config shipped in all example YAML files (
ops: [mean, std, diff_mean, diff_std]):mean→ds.mean(...)✅std→ds.std(...)✅diff_mean→ mutatesdstods.diff(...), thenmean(...)✅ (but corruptsds)diff_std→ applies.diff()again on already-diffed data → computesstd(diff(diff(ds)))❌Expected:
diff_stdshould computestd(diff(ds)).Actual:
diff_stdcomputesstd(diff(diff(ds))).Fix: Use a per-iteration local variable
ds_opinstead of mutatingds, so each iteration always starts from the original dataset.No dependencies required for this change.
Issue Link
Closes #102
Type of change
Checklist before requesting a review
pullwith--rebaseoption if possible).Checklist for reviewers
Each PR comes with its own improvements and flaws. The reviewer should check the following:
Author checklist after completed review
reflecting type of change (add section where missing):
Checklist for assignee