Skip to content
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

Add support for unitful #70

Merged
merged 5 commits into from
Aug 29, 2024
Merged

Add support for unitful #70

merged 5 commits into from
Aug 29, 2024

Conversation

Sbozzolo
Copy link
Member

@Sbozzolo Sbozzolo commented Aug 29, 2024

This commit adds support for physical units managed by Unitful.jl.

Unitful is added as an extension. Currently, ClimaAnalysis.jl always includes this extension (and Unitful is a hard dependency of
ClimaAnalysis), but this opens a pathway to making Unitful an optional dependency in the future if we decide to do so. (I am taking this idea from Unitful itself.)

The technical choice on how to integrate Unitful is the following:

In the constructor, ClimaAnalysis scans for the units keyword in the attributes. When found, we try to parse it with
Unitful.uparse. If possible, we replace the value for units in attributes with the Unitful version. If it is not possible, we leave the string.

When units are requested with ClimaAnalysis.units, we always return a string.

The choice being made here is that we modify the input given (from String to Unitful), instead of creating a separate box where to put this information. The reason for this choice is that it removes the need to keep information synced and provide a single ground truth of what units are.

We also decide to not attach units directly to data to avoid performance penalties with manipulating large arrays.

This PR also cleans up a couple of things about extensions along the way.

Closes #53 (except for dimensions)

@Sbozzolo Sbozzolo force-pushed the gb/unitufl branch 2 times, most recently from 354fda3 to e9f582e Compare August 29, 2024 02:06
Copy link

codecov bot commented Aug 29, 2024

Codecov Report

Attention: Patch coverage is 97.56098% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.35%. Comparing base (06b04db) to head (52416ea).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
ext/ClimaAnalysisUnitfulExt.jl 96.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #70      +/-   ##
==========================================
+ Coverage   96.31%   96.35%   +0.03%     
==========================================
  Files           8        9       +1     
  Lines         516      548      +32     
==========================================
+ Hits          497      528      +31     
- Misses         19       20       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Sbozzolo Sbozzolo force-pushed the gb/unitufl branch 2 times, most recently from d69aed4 to 3a36abf Compare August 29, 2024 03:23
This commit adds support for physical units managed by Unitful.jl.

Unitful is added as an extension. Currently, ClimaAnalysis.jl always
includes this extension (and Unitful is a hard dependency of
ClimaAnalysis), but this opens a pathway to making Unitful an optional
dependency in the future if we decide to do so. (I am taking this idea
from Unitful itself.)

The technical choice on how to integrate Unitful is the following:

In the constructor, ClimaAnalysis scans for the `units` keyword in the
attributes. When found, we try to parse it with `Unitful.uparse`. If
possible, we replace the value for `units` in `attributes` with the
`Unitful` version. If it is not possible, we leave the string.

When units are requested with `ClimaAnalysis.units`, we always return a
string.

The choice being made here is that we modify the input given (from
`String` to `Unitful`), instead of creating a separate box where to put
this information. The reason for this choice is that it removes the need
to keep information synced and provide a single ground truth of what
units are.

We also decide to not attach units directly to `data` to avoid
performance penalties with manipulating large arrays.
Extensions could have name collisions, adding ClimaAnalysis in front
dramatically reduces the likelihood and follows conventions.
Sbozzolo added a commit to CliMA/ClimaAtmos.jl that referenced this pull request Aug 29, 2024
CliMA/ClimaAnalysis.jl#70 is _slightly_ breaking if
units are accessed directly
Sbozzolo added a commit to CliMA/ClimaAtmos.jl that referenced this pull request Aug 29, 2024
CliMA/ClimaAnalysis.jl#70 is _slightly_ breaking if
units are accessed directly
Sbozzolo added a commit to CliMA/ClimaAtmos.jl that referenced this pull request Aug 29, 2024
CliMA/ClimaAnalysis.jl#70 is _slightly_ breaking if
units are accessed directly
Sbozzolo added a commit to CliMA/ClimaAtmos.jl that referenced this pull request Aug 29, 2024
CliMA/ClimaAnalysis.jl#70 is _slightly_ breaking if
units are accessed directly
Sbozzolo added a commit to CliMA/ClimaAtmos.jl that referenced this pull request Aug 29, 2024
CliMA/ClimaAnalysis.jl#70 is _slightly_ breaking if
units are accessed directly
Copy link
Member

@AlexisRenchon AlexisRenchon left a comment

Choose a reason for hiding this comment

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

This a beautiful PR!
I have one question (curiosity) but no request

Why did you define convert_units docstring in Var.jl, and the actual function in ext/ClimaAnalysisUnitfulExt.jl?

Is it to keep ClimaAnalysisUnitfulExt focused on actual code, for better readability?

@Sbozzolo
Copy link
Member Author

This a beautiful PR! I have one question (curiosity) but no request

Why did you define convert_units docstring in Var.jl, and the actual function in ext/ClimaAnalysisUnitfulExt.jl?

Is it to keep ClimaAnalysisUnitfulExt focused on actual code, for better readability?

I had it there originally, but Documenter wouldn't find the docstring.

@Sbozzolo Sbozzolo merged commit 0a024c7 into main Aug 29, 2024
6 checks passed
oalcabes pushed a commit to CliMA/ClimaAtmos.jl that referenced this pull request Aug 30, 2024
CliMA/ClimaAnalysis.jl#70 is _slightly_ breaking if
units are accessed directly
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.

Add support for Unitful
2 participants