Repository navigation
Conversation
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
|
@Sevans711 @cmdupuis3 @erogluorhan, this fixes #1856: units whitespace is stripped, and |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the calculus unit-labeling changes.
No open issues 🎉
What changed in this PR
Fixes calculus unit labels for unit-sphere derivatives, addressing #1856 without changing numerical scaling.
Changes:
- Labels radius-one gradient, curl, and divergence results per radian and trims whitespace from input units.
- Adds regression coverage for unit formatting, radius scaling, and missing-radius warnings.
- Expands the vector calculus guide with unit-sphere and physical-radius examples.
| File | Description |
|---|---|
uxarray/core/gradient.py |
Trims input units and uses per-radian labels for radius-one results. |
uxarray/core/dataarray.py |
Corrects spelling in gradient and divergence docstrings. |
test/core/test_calculus_units.py |
Tests unit labels and numerical scaling across calculus methods. |
docs/user-guide/vector_calculus.ipynb |
Explains unit conventions and demonstrates scaling behavior. |
Review effort: Balanced
Sevans711
left a comment
There was a problem hiding this comment.
Thank you for working on this and making this PR. I left a few different inline comments, mostly related to making things more concise / easier to understand / easier to maintain. The actual fixes to the code itself do indeed solve the original issue.
Also, please confirm whether you personally believe it makes sense to strip surrounding whitespace from input units, and if you have any strong arguments one way or another. Right now we have @cmdupuis3 in favor, but I was not yet convinced that it's a good idea (as clarified in #1856). Since I currently don't see any clear arguments in favor of it here, I can't tell for sure if you (or your LLMs) actually considered whether it was a good idea in the first place.
| "- By default (`scale_by_radius=True`), a radius other than `1.0` divides the unit-sphere result by that radius, with output units `[data units]/m`. The physical radius is interpreted in meters.\n", | ||
| "- With `sphere_radius=1.0`, results retain per-radian units (`[data units]/rad`), even when `scale_by_radius=True`.\n", | ||
| "- With `scale_by_radius=False`, results always remain on the unit sphere, regardless of the stored radius.\n", | ||
| "- If the grid has no `sphere_radius` attribute, the default call falls back to unit-sphere output and emits a `UserWarning`.\n", |
There was a problem hiding this comment.
This feels too detailed for a user guide. It can go in docstrings if needed. The user guide should give the simplest conceptual understanding, rather than walking through all these details; that's what the docstrings are for. Especially here I think some of this is redundant or could be made much more concise?
| "- With `scale_by_radius=False`, results always remain on the unit sphere, regardless of the stored radius.\n", | ||
| "- If the grid has no `sphere_radius` attribute, the default call falls back to unit-sphere output and emits a `UserWarning`.\n", | ||
| "\n", | ||
| "Leading and trailing whitespace is stripped from input units. Missing or blank units become `1`; ratio units are parenthesized, for example `(m/s)/m` (dimensionally equivalent to `1/s`), without symbolic simplification.\n", |
There was a problem hiding this comment.
Also not sure if this needs to be specified here…
| "metadata": {}, | ||
| "source": [ | ||
| "> **Units.** By default, `gradient()`, `curl()`, and `divergence()` divide by `uxgrid.sphere_radius` so derivatives carry physical units (e.g. `[data units]/m`, `1/s` for velocity curl). Pass `scale_by_radius=False` to keep results on the unit sphere (per radian). If the grid has no `sphere_radius` attribute, the call falls back to unit-sphere output and emits a `UserWarning`." | ||
| "### Units and the unit-sphere convention\n", |
There was a problem hiding this comment.
The entire units section is very confusing / jarring here; it breaks the narrative flow without warning and then the non-units content resumes without warning afterwards. If keeping the new units demo example which you added here, please relocate the section. Putting it immediately after the "Plotting Gradients" section would be one reasonable option.
| "\n", | ||
| "Leading and trailing whitespace is stripped from input units. Missing or blank units become `1`; ratio units are parenthesized, for example `(m/s)/m` (dimensionally equivalent to `1/s`), without symbolic simplification.\n", | ||
| "\n", | ||
| "The following example reuses the Gaussian field loaded above. A separate grid copy keeps the physical radius used by the rest of this notebook unchanged." |
There was a problem hiding this comment.
This entire sentence could be removed; these are code details, not narratively interesting and not particularly elucidating either. The note about using a grid copy could be added as a comment in the code block below.
| "\n", | ||
| "units_grid.sphere_radius = 1.0\n", | ||
| "grad_unit = q_units.gradient()[\"meridional_gradient\"]\n", | ||
| "assert grad_unit.attrs[\"units\"] == \"K s-1/rad\"\n", |
There was a problem hiding this comment.
You've written this code block like a pytest test, not like something that goes in a user guide. Instead of asserting, you should make it so that a quick glance at the outputs alone make it clear what is being demonstrated here.
| ")\n", | ||
| "np.testing.assert_allclose(grad_unscaled.values, grad_unit.values, equal_nan=True)\n", | ||
| "print(\"Physical radius:\", grad_physical.attrs[\"units\"])\n", | ||
| "print(\"Scaling disabled:\", grad_unscaled.attrs[\"units\"])" |
There was a problem hiding this comment.
Overall I would say you should either completely remove the new units demo, make it much more concise, or update it to make it much clearer what it is trying to demonstrate / show it in a more compelling way. It currently looks very dense and just prints:
Radius 1: K s-1/rad
Physical radius: K s-1/m
Scaling disabled: K s-1/rad
without any commentary on what you would expect, or whether that is a good thing or not. It takes too much effort to parse the existing demo, the effort currently outweighs the benefits it is providing.
| def test_calculus_units_and_radius_scaling( | ||
| gridpath, datasetpath, method, radius, scale_by_radius, units, base_units | ||
| ): |
There was a problem hiding this comment.
You're basically just re-coding the existing units logic here. While it's quite comprehensive, I'm not sure how useful the test is in this form. I don't necessarily have any requests for what exactly to change about this test in particular, but there are so many logical branches internally in here, so it's not particularly simple to quickly figure out some concrete examples. One way to proceed could be to split this up into a few different tests instead of parametrizing 4 different parameters. Another way could be to swap some of the parametrize calls for internal loops, and hard-code what you expect inside those loops. Another way could be to add another test which just does a spot check for a few of these cases.
It just feels a bit wrong to me to add 3x4x2x4 = 96 tests to the pytest suite just to check that a units attribute is being filled correctly. Having more hard-coded tests also makes things easier to debug in my opinion.
| @@ -0,0 +1,65 @@ | |||
| """Regression coverage for calculus units and unit-sphere scaling (#1856).""" | |||
There was a problem hiding this comment.
Please move these into test/core/test_vector_calculus.py; I don't think it's large enough to warrant splitting off into its own test file.
| "\n", | ||
| "UXarray computes spherical geometry and derivatives internally on a unit sphere. `uxgrid.sphere_radius` stores the physical radius for optional output scaling; it does not change that internal convention.\n", | ||
| "\n", | ||
| "For `gradient()`, `curl()`, and `divergence()`:\n", |
There was a problem hiding this comment.
One more quick comment: if you end up keeping this line, please update it to include laplacian() as well, now that #1873 has merged!
Closes #1856. Gradient, curl, and divergence now label radius-one results per radian and strip surrounding whitespace from input units, without changing numerical scaling. Adds regression coverage and expands the existing vector calculus guide with a unit-sphere versus physical-radius example.