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 unitful extension with convenience dispatches #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ version = "0.4.2"
AbstractNumbers = "85c772de-338a-5e7f-b815-41e76c26ac1f"
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Flatten = "4c728ea3-d9ee-5c9a-9642-b6f7d7dc04fa"
PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[weakdeps]
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
ModelParametersUnitfulExt = ["Unitful"]

[compat]
AbstractNumbers = "0.2.5"
Aqua = "0.8"
Expand All @@ -19,11 +26,12 @@ ConstructionBase = "1"
ConstructionBaseExtras = "0.1"
DataFrames = "1"
Flatten = "0.4"
PackageExtensionCompat = "1"
PrettyTables = "0.8, 0.9, 0.10, 0.11, 0.12, 1, 2"
Setfield = "0.6, 0.7, 0.8, 1"
StaticArrays = "1"
Tables = "1"
Test = "1"
StaticArrays = "1"
Unitful = "1"
julia = "1"

Expand Down
21 changes: 21 additions & 0 deletions ext/ModelParametersUnitfulExt/ModelParametersUnitfulExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module ModelParametersUnitfulExt

using ModelParameters
using Unitful
using Setfield

ModelParameters.Param(val::Unitful.AbstractQuantity; kwargs...) = Param(ustrip(val), units=unit(val), kwargs...)

"""
Unitful.uconvert(u::Unitful.Units, p::Param)

Convert the unit of the `Param` `p` to `u`.
"""
function Unitful.uconvert(u::Unitful.Units, p::Param)
nt = parent(p)
@set! nt.val = ustrip(u, stripparams(p))
@set! nt.units = u
return Param(nt)
end

end
6 changes: 6 additions & 0 deletions src/ModelParameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import AbstractNumbers,

using Setfield

using PackageExtensionCompat

function __init__()
Copy link
Owner

Choose a reason for hiding this comment

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

Hmm I don't love __init__() blocks... can we just minimum bound to 1.10? It will be the LTS version very soon.

@require_extensions
end

export AbstractModel, Model, StaticModel

export AbstractParam, Param, RealParam
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ end
end
end

@testset "Unitful extensions" begin
p = Param(1.0u"m")
@test hasproperty(p, :units) && p.units == u"m"
p_cm = uconvert(u"cm", p)
@test p.units == u"cm" && p.val == 100.0
end

@testset "use Unitful units, with StaticModel" begin
s1 = S1(
Param(1.0; bounds=(5.0, 15.0)),
Expand Down
Loading