Skip to content

Fix get_integer_time precision and zero handling#494

Open
Saptami191 wants to merge 4 commits intomllam:mainfrom
Saptami191:fix-get-integer-time-v2
Open

Fix get_integer_time precision and zero handling#494
Saptami191 wants to merge 4 commits intomllam: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.

Copy link
Copy Markdown
Contributor

@kshirajahere kshirajahere left a comment

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.

(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.

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.

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
Copy link
Copy Markdown
Contributor

@observingClouds observingClouds left a comment

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.

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.

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.

Copy link
Copy Markdown
Contributor Author

@Saptami191 Saptami191 Mar 23, 2026

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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

3 participants