DynamicQuantities.jl icon indicating copy to clipboard operation
DynamicQuantities.jl copied to clipboard

Unitful-like API shims

Open MilesCranmerBot opened this issue 9 months ago • 6 comments

Adds a small set of Unitful-shaped API shims (wrappers/aliases) to ease downstream compatibility while keeping DynamicQuantities semantics intact.

Included:

  • dimensions(x) alias for dimension(x)
  • unit(q) (and unit(::Number))
  • isunitless/isdimensionless
  • upreferred(x) no-op
  • uparse(unit, x/s)
  • ustrip(::Type{T}, unit, q)

Tests: local Pkg.test() passed.

MilesCranmerBot avatar Feb 14 '26 22:02 MilesCranmerBot

Benchmark Results (Julia v1.10)

Time benchmarks
main 5e3592a3df7368... main / 5e3592a3df7368...
Quantity/creation/Quantity(x) 2.03 ± 0.003 ns 2.32 ± 0.003 ns 0.877 ± 0.0017
Quantity/creation/Quantity(x, length=y) 2.6 ± 0.006 ns 2.61 ± 0.006 ns 0.998 ± 0.0032
Quantity/with_numbers/*real 2.33 ± 0.003 ns 2.32 ± 0.002 ns 1 ± 0.0016
Quantity/with_numbers/^int 7.49 ± 2 ns 7.5 ± 2 ns 1 ± 0.38
Quantity/with_numbers/^int * real 7.78 ± 2.3 ns 7.53 ± 2 ns 1.03 ± 0.41
Quantity/with_quantity/+y 3.76 ± 0.014 ns 3.76 ± 0.016 ns 0.999 ± 0.0056
Quantity/with_quantity//y 2.33 ± 0.28 ns 2.61 ± 0.005 ns 0.893 ± 0.11
Quantity/with_self/dimension 2.32 ± 0.003 ns 2.04 ± 0.003 ns 1.14 ± 0.0022
Quantity/with_self/inv 2.33 ± 0.009 ns 2.61 ± 0.003 ns 0.893 ± 0.0036
Quantity/with_self/ustrip 2.31 ± 0.004 ns 2.03 ± 0.004 ns 1.14 ± 0.003
QuantityArray/broadcasting/multi_array_of_quantities 0.158 ± 0.006 ms 0.157 ± 0.0053 ms 1 ± 0.051
QuantityArray/broadcasting/multi_normal_array 0.0525 ± 0.0011 ms 0.0525 ± 0.00099 ms 1 ± 0.029
QuantityArray/broadcasting/multi_quantity_array 0.17 ± 0.012 ms 0.169 ± 0.011 ms 1 ± 0.098
QuantityArray/broadcasting/x^2_array_of_quantities 0.0398 ± 0.003 ms 0.0371 ± 0.0033 ms 1.07 ± 0.13
QuantityArray/broadcasting/x^2_normal_array 8.31 ± 1.1 μs 7.96 ± 1 μs 1.04 ± 0.19
QuantityArray/broadcasting/x^2_quantity_array 8.93 ± 0.93 μs 8.94 ± 0.98 μs 1 ± 0.15
QuantityArray/broadcasting/x^4_array_of_quantities 0.0954 ± 0.0036 ms 0.0947 ± 0.0033 ms 1.01 ± 0.052
QuantityArray/broadcasting/x^4_normal_array 0.0495 ± 0.001 ms 0.0465 ± 0.00095 ms 1.06 ± 0.031
QuantityArray/broadcasting/x^4_quantity_array 0.0498 ± 0.0026 ms 0.0496 ± 0.001 ms 1 ± 0.056
time_to_load 0.199 ± 0.0012 s 0.2 ± 0.00061 s 0.997 ± 0.0069
Memory benchmarks
main 5e3592a3df7368... main / 5e3592a3df7368...
Quantity/creation/Quantity(x) 0 allocs: 0 B 0 allocs: 0 B
Quantity/creation/Quantity(x, length=y) 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/*real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int * real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity/+y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity//y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/dimension 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/inv 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/ustrip 0 allocs: 0 B 0 allocs: 0 B
QuantityArray/broadcasting/multi_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/multi_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/multi_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^2_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^2_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^2_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^4_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^4_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^4_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
time_to_load 0.153 k allocs: 14.5 kB 0.153 k allocs: 14.5 kB 1

github-actions[bot] avatar Feb 14 '26 23:02 github-actions[bot]

Benchmark Results (Julia v1)

Time benchmarks
main 5e3592a3df7368... main / 5e3592a3df7368...
Quantity/creation/Quantity(x) 2.79 ± 0 ns 2.79 ± 0 ns 1 ± 0
Quantity/creation/Quantity(x, length=y) 3.11 ± 0.01 ns 3.42 ± 0.01 ns 0.909 ± 0.004
Quantity/with_numbers/*real 3.11 ± 0.01 ns 4.03 ± 0.01 ns 0.771 ± 0.0031
Quantity/with_numbers/^int 12.4 ± 0.99 ns 12.1 ± 0.99 ns 1.03 ± 0.12
Quantity/with_numbers/^int * real 11.8 ± 1 ns 12 ± 1.1 ns 0.977 ± 0.12
Quantity/with_quantity/+y 4.04 ± 0.01 ns 4.04 ± 0.011 ns 1 ± 0.0037
Quantity/with_quantity//y 3.11 ± 0 ns 3.11 ± 0.001 ns 1 ± 0.00032
Quantity/with_self/dimension 3.11 ± 0.01 ns 3.11 ± 0.01 ns 1 ± 0.0046
Quantity/with_self/inv 3.11 ± 0.01 ns 3.11 ± 0.001 ns 1 ± 0.0032
Quantity/with_self/ustrip 2.79 ± 0.01 ns 3.71 ± 0.011 ns 0.754 ± 0.0035
QuantityArray/broadcasting/multi_array_of_quantities 0.112 ± 0.0047 ms 0.112 ± 0.003 ms 1 ± 0.05
QuantityArray/broadcasting/multi_normal_array 0.0471 ± 0.00026 ms 0.0497 ± 0.00024 ms 0.947 ± 0.0069
QuantityArray/broadcasting/multi_quantity_array 0.056 ± 0.00021 ms 0.056 ± 0.00026 ms 1 ± 0.006
QuantityArray/broadcasting/x^2_array_of_quantities 22.2 ± 9.2 μs 23.7 ± 7.3 μs 0.938 ± 0.48
QuantityArray/broadcasting/x^2_normal_array 2.2 ± 2.2 μs 3.27 ± 2.1 μs 0.673 ± 0.82
QuantityArray/broadcasting/x^2_quantity_array 3.54 ± 1 μs 4.66 ± 1.6 μs 0.759 ± 0.34
QuantityArray/broadcasting/x^4_array_of_quantities 0.0814 ± 0.00077 ms 0.0814 ± 0.00093 ms 1 ± 0.015
QuantityArray/broadcasting/x^4_normal_array 0.0435 ± 0.0002 ms 0.0435 ± 0.00018 ms 1 ± 0.0062
QuantityArray/broadcasting/x^4_quantity_array 0.0529 ± 0.0058 ms 0.0529 ± 0.0002 ms 0.999 ± 0.11
time_to_load 0.189 ± 0.0018 s 0.188 ± 0.00061 s 1.01 ± 0.01
Memory benchmarks
main 5e3592a3df7368... main / 5e3592a3df7368...
Quantity/creation/Quantity(x) 0 allocs: 0 B 0 allocs: 0 B
Quantity/creation/Quantity(x, length=y) 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/*real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int * real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity/+y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity//y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/dimension 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/inv 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/ustrip 0 allocs: 0 B 0 allocs: 0 B
QuantityArray/broadcasting/multi_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/multi_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/multi_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^2_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^2_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^2_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^4_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^4_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^4_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
time_to_load 0.149 k allocs: 11.2 kB 0.145 k allocs: 11 kB 1.02

github-actions[bot] avatar Feb 14 '26 23:02 github-actions[bot]

Codecov Report

:x: Patch coverage is 83.33333% with 2 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 99.00%. Comparing base (505cb31) to head (5e3592a). :warning: Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
src/utils.jl 75.00% 2 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #199      +/-   ##
==========================================
- Coverage   99.14%   99.00%   -0.15%     
==========================================
  Files          21       23       +2     
  Lines        1292     1308      +16     
==========================================
+ Hits         1281     1295      +14     
- Misses         11       13       +2     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Feb 15 '26 09:02 codecov[bot]

@codex review

MilesCranmer avatar Feb 15 '26 14:02 MilesCranmer

Fixed: simplified unit shim to just unit(t) = oneunit(t) (covers quantities + numbers without extra overloads). Pushed in 1c82806.

MilesCranmerBot avatar Feb 15 '26 15:02 MilesCranmerBot

Agreed — matching Unitful: I made isunitless/isdimensionless scalar-only (arrays now throw MethodError; use broadcasting: isunitless.(x)). Pushed in commit 5e3592a to branch unitful-api-compat.

MilesCranmerBot avatar Mar 08 '26 18:03 MilesCranmerBot