Skip to content

Fix get_integer_time precision and zero handling#494

Open
Saptami191 wants to merge 2 commits into
mllam:mainfrom
Saptami191:fix-get-integer-time-v2
Open

Fix get_integer_time precision and zero handling#494
Saptami191 wants to merge 2 commits into
mllam:mainfrom
Saptami191:fix-get-integer-time-v2

Conversation

@Saptami191

Copy link
Copy Markdown
Contributor

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

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📖 Documentation (Addition or improvements to documentation)

Checklist before requesting a review

  • My branch is up-to-date with the target branch - if not update your fork with the changes from the target branch (use pull with --rebase option if possible).
  • I have performed a self-review of my code
  • For any new/modified functions/classes I have added docstrings that clearly describe its purpose, expected inputs and returned values
  • I have placed in-line comments to clarify the intent of any hard-to-understand passages of my code
  • I have updated the README to cover introduced code changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have given the PR a name that clearly describes the change, written in imperative form
  • I have requested a reviewer and an assignee

Checklist for reviewers

Each PR comes with its own improvements and flaws. The reviewer should check the following:

  • the code is readable
  • the code is well tested
  • the code is documented (including return types and parameters)
  • the code is easy to maintain

Author checklist after completed review

  • I have added a line to the CHANGELOG describing this change, in a section
    reflecting type of change:
    • fixes

Checklist for assignee

  • PR is up to date with the base branch
  • the tests pass
  • (if the PR is not just maintenance/bugfix) the PR is assigned to the next milestone. If it is not, propose it for a future milestone.
  • author has added an entry to the changelog (and designated the change as added, changed, fixed or maintenance)
  • Once the PR is ready to be merged, squash commits and merge the PR.

@Saptami191

Copy link
Copy Markdown
Contributor Author

@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 kshirajahere 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.

I think the integer-microseconds direction is the right fix, but I think there is still few thing worth addressing before merge.

Comment thread neural_lam/utils.py
(0, 'seconds')
"""
total_seconds = tdelta.total_seconds()
total_microseconds = (

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.

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.

Comment thread tests/test_utils.py Outdated
assert get_integer_time(timedelta(0)) == (0, "seconds")


def test_milliseconds():

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.

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.

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.

This is actually already tested in the docstring.

Comment thread CHANGELOG.md Outdated

- 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

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.

Shud point to #494 instead of #469

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review! I hve addressed all three points.

@sadamov sadamov self-requested a review March 23, 2026 05:10

@observingClouds observingClouds 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.

Thanks for this PR. I left a few comments and suggest a few changes.

Comment thread tests/test_utils.py Outdated
assert get_integer_time(timedelta(0)) == (0, "seconds")


def test_milliseconds():

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.

This is actually already tested in the docstring.

Comment thread tests/test_utils.py Outdated

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.

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.

@Saptami191 Saptami191 Mar 23, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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 observingClouds self-assigned this Mar 23, 2026
@sadamov sadamov requested review from kshirajahere and observingClouds and removed request for kshirajahere and sadamov April 1, 2026 08:32
@sadamov sadamov added the bug Something isn't working label Apr 13, 2026
@Saptami191

Copy link
Copy Markdown
Contributor Author

@observingClouds Should get_integer_time explicitly support negative timedeltas (e.g., timedelta(days=-7) -> (-1, "weeks")), or is that out of scope for this PR?

Comment thread tests/test_utils.py Outdated
Comment on lines +24 to +25
def test_negative():
assert get_integer_time(timedelta(days=-7)) == (-1, "weeks")

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.

Yes, please add this test to the doctest and then all is covered by the doctest and we can remove all tests here

@Saptami191

Copy link
Copy Markdown
Contributor Author

@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>
@sadamov sadamov force-pushed the fix-get-integer-time-v2 branch from 70af30f to 9e2d2cb Compare June 5, 2026 07:13
@sadamov sadamov requested a review from observingClouds June 5, 2026 07:35

@sadamov sadamov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@observingClouds @Saptami191 I just rebased this PR onto main. Feel free to have a look and merge if you agree.

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.

Improve get_integer_time: handle zero timedelta and avoid float precision issues

4 participants