Fix get_integer_time precision and zero handling#494
Conversation
|
@joeloskarsson Recreated this PR after fixing earlier issues with the template and workflow. The code, tests, and linting are now clean and aligned with project guidelines. Would appreciate a review. |
kshirajahere
left a comment
There was a problem hiding this comment.
I think the integer-microseconds direction is the right fix, but I think there is still few thing worth addressing before merge.
| (0, 'seconds') | ||
| """ | ||
| total_seconds = tdelta.total_seconds() | ||
| total_microseconds = ( |
There was a problem hiding this comment.
The new implementation is fine, but this helper’s contract is still inconsistent across the tree: compute_standardization_stats.py still calls get_integer_time(step_length.total_seconds()), i.e. with a float rather than a timedelta. I think we should either update that caller in the same slice or explicitly tighten/document the contract here.
| assert get_integer_time(timedelta(0)) == (0, "seconds") | ||
|
|
||
|
|
||
| def test_milliseconds(): |
There was a problem hiding this comment.
Since this PR is specifically about float-precision avoidance, I think it would be good to add one direct regression test for the issue-body example (timedelta(days=0.001) -> (86400, "milliseconds")) here as well, rather than leaving that behavior covered only by the doctest.
There was a problem hiding this comment.
This is actually already tested in the docstring.
|
|
||
| - Fix typo in `ar_model.py` that causes `AttributeError` during evaluation [\#204](https://github.com/mllam/neural-lam/pull/204) @ritinikhil | ||
|
|
||
| - Fix `get_integer_time` to avoid floating-point precision issues and correctly handle zero timedelta [#469](https://github.com/mllam/neural-lam/pull/469) @Saptami191 |
There was a problem hiding this comment.
Thanks for the review! I hve addressed all three points.
observingClouds
left a comment
There was a problem hiding this comment.
Thanks for this PR. I left a few comments and suggest a few changes.
| assert get_integer_time(timedelta(0)) == (0, "seconds") | ||
|
|
||
|
|
||
| def test_milliseconds(): |
There was a problem hiding this comment.
This is actually already tested in the docstring.
There was a problem hiding this comment.
All these newly added tests are already covered by the doctest. No need to replicate them here. If you think a case is missing in the docstring, please add them there.
There was a problem hiding this comment.
@observingClouds thanks for the review! yeah all are tested in docstring. i will remove this test file .actually it was part of proving the fix is correct.
Should I also add a doctest for negative timedelta
get_integer_time(timedelta(days=-7))
(-1, 'weeks')
it will represent a duration in the past 7 days ago.
or is that out of scope for this PR?
|
@observingClouds Should |
| def test_negative(): | ||
| assert get_integer_time(timedelta(days=-7)) == (-1, "weeks") |
There was a problem hiding this comment.
Yes, please add this test to the doctest and then all is covered by the doctest and we can remove all tests here
|
@observingClouds Hii! Requested changes are done and all tests have been passed |
Re-applies @Saptami191's PR mllam#494 onto current main with these adjustments: - Dropped the compute_standardization_stats.py edit: the second caller it was fixing (`get_original_window_indices`) no longer exists in current main, and the surviving caller already passes a timedelta (not a float) - Added thousands separators (1_000_000 etc.) to the microsecond constants for readability - Added a one-line comment explaining why we avoid total_seconds() floats - Placed CHANGELOG entry in the current [unreleased] section under Fixed Co-Authored-By: Shoko <Saptami191@users.noreply.github.com> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
70af30f to
9e2d2cb
Compare
sadamov
left a comment
There was a problem hiding this comment.
@observingClouds @Saptami191 I just rebased this PR onto main. Feel free to have a look and merge if you agree.
Describe your changes
Fix get_integer_time precision issues and zero timedelta handling.
Uses integer microseconds instead of float computation to avoid rounding errors and ensure correct unit detection.
No additional dependencies.
Issue Link
Closes #468
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:
Checklist for assignee