-
Notifications
You must be signed in to change notification settings - Fork 20
Fix off-by-one in OrderedRingBuffer.count_covered and add consistency test
#1343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix off-by-one in OrderedRingBuffer.count_covered and add consistency test
#1343
Conversation
llucax
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! LGTM, only a couple of minor comments.
Also the CI is not passing, it is weird because the issues look unrelated to this PR, but maybe you can fix them (removing the type: ignore should be enough, maybe mypy or numpy got some hidden improvement in some patch release).
nox > mypy
src/frequenz/sdk/timeseries/_periodic_feature_extractor.py:412: error: Unused "type: ignore" comment [unused-ignore]
benchmarks/timeseries/periodic_feature_extractor.py:75: error: Unused "type: ignore" comment [unused-ignore]
ac1d92d to
78ed90d
Compare
|
Very weird, now tests are failing in some apparently unrelated tests (I don't think these tests use the moving window). 🤔 |
|
I just re-run the tests and they fail the same, so that's good news (I was more scare about this being a flaky failure). I guess the next step would be to verify tests pass without these changes. If the failure is consistent, then probably these changes are somehow affecting those tests and we need to find why and fix it. @malteschaaf are the tests passing locally? |
The tests fail locally as well but no matter the change (they fail with and without any of my changes) |
|
OK, this is definitely some indirect dependency bump issue. Locally using @malteschaaf do you think you can take care of this? |
|
Seems like I'm not sure how to proceed from here though... |
I'll have a look to see if there is anything obvious in the new version that is wrong. Thanks for the research! |
|
OK, it looks like v0.3.4 introduced breaking changes (coming from the rust library v0.4.0):
I will mark that release as broken and wait for Sahas to come back to see how we move forward (we probably need to re-release it as v0.4.0 and then update the SDK tests to pass using the new logic). |
Signed-off-by: Malte Schaaf <malte.schaaf@frequenz.com>
…y-one Previously, floating-point division could produce off-by-one errors for count_covered due to rounding issues (e.g., 1.0 // 0.1 == 9). This change uses `timedelta // timedelta`, which operates on integer nanoseconds, ensuring the returned sample count is exact and preventing count_valid from exceeding count_covered during initial window fill. Signed-off-by: Malte Schaaf <malte.schaaf@frequenz.com>
Adds a new test `test_moving_window_length` that checks the moving window length without resampling. The test ensures that `count_valid` never exceeds `count_covered` during the initial fill and that the final count matches the expected window capacity. This helps prevent regressions related to off-by-one errors in window count calculations. Signed-off-by: Malte Schaaf <malte.schaaf@frequenz.com>
Signed-off-by: Malte Schaaf <malte.schaaf@frequenz.com>
Signed-off-by: Malte Schaaf <malte.schaaf@frequenz.com>
…ing changes - Updated the maximum version of "frequenz-microgrid-component-graph" from `< 0.3.4` to `<= 0.3.3`. - This change ensures compatibility, as version 0.3.4 introduces breaking changes. Signed-off-by: Malte Schaaf <malte.schaaf@frequenz.com>
7e9fbb9 to
039de13
Compare
| # (plugins.mkdocstrings.handlers.python.import) | ||
| "frequenz-client-microgrid >= 0.18.1, < 0.19.0", | ||
| "frequenz-microgrid-component-graph >= 0.3.2, < 0.4", | ||
| "frequenz-microgrid-component-graph >= 0.3.2, < 0.3.4", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have not be committed 😭
We are finally not going this route. I made a follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries and keep the contributions coming!
Summary
This PR fixes a subtle off-by-one issue in the
OrderedRingBuffer.count_coveredmethod and adds a test to ensure window count consistency inMovingWindow.Changes
Fix
count_coveredcalculationtimedeltadivision (timedelta // timedelta).Add
test_moving_window_lengthMotivation
Floating-point division could produce off-by-one errors for certain window sizes and sampling periods, causing
count_coveredto be inaccurate. Switching to integer-basedtimedeltadivision ensures deterministic and exact counts. The new test provides coverage to catch similar issues.