Skip to content

Replace unit handling with Pint OpenwaterHealth/openlifu-python#153#454

Open
samueljwu wants to merge 1 commit into
OpenwaterHealth:mainfrom
samueljwu:units-pint
Open

Replace unit handling with Pint OpenwaterHealth/openlifu-python#153#454
samueljwu wants to merge 1 commit into
OpenwaterHealth:mainfrom
samueljwu:units-pint

Conversation

@samueljwu
Copy link
Copy Markdown

Summary

Replaces the unit handling logic in openlifu.util.units with Pint implementation while preserving the legacy API and expected behavior for supported units.

Fixes #153

Changes

  • Added pint as a project dependency
  • Udpated units.py to use pint.UnitRegistry for deminsionality checks and conversion factors
  • Preserved existing functions
  • Improved support for units

All existing tests pass
Pre-commit hooks pass

@ebrahimebrahim
Copy link
Copy Markdown
Collaborator

This is wonderful and much needed, thank you!

Adding @peterhollender to review. I can take it as well, @peterhollender let me know

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR replaces custom unit conversion logic in openlifu.util.units with Pint while keeping the existing public helpers used throughout geometry, simulation, and planning code.

Changes:

  • Adds Pint as a runtime dependency.
  • Reimplements unit normalization, type detection, SI scaling, and conversion through UnitRegistry.
  • Expands unit conversion/type tests for legacy units and new Pint-backed aliases.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/openlifu/util/units.py Replaces manual unit handling with Pint-backed normalization, dimensionality checks, and conversions.
tests/test_units.py Adds parametrized coverage for conversion factors, unit classification, and newly supported aliases.
pyproject.toml Adds pint to project dependencies.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +159 to 163
try:
scl = _quantity(from_unit).to(_normalize_unit(to_unit)).magnitude
except DimensionalityError as exc:
type0 = getunittype(from_unit)
type1 = getunittype(to_unit)
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.

Better library to handle units

3 participants