Skip to content

Commit 92da210

Browse files
authored
Use new struct transforms from TV 0.8.21 (#21)
* Update for TransformVars after adding struct transform * Add kwarg constructor for RpFormFit * Update compat entry for TransformVariables * Clear out ConstWrapTV, which relied on internals of TV anyway * Update docs * Test robustness; use up-to-date JET kwargs
1 parent 82e054f commit 92da210

File tree

9 files changed

+19
-38
lines changed

9 files changed

+19
-38
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Reexport = "1"
4343
Roots = "2"
4444
SavitzkyGolay = "0.9.1"
4545
SpecialFunctions = "1, 2"
46-
TransformVariables = "0.8.19"
46+
TransformVariables = "0.8.21"
4747
TypedTables = "1.4"
4848
Unitful = "1.20"
4949
julia = "1.10"

docs/example/fitting_mannitol.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,16 @@ savefig("modelpre.svg"); #md #hide
163163
# Optimization algorithms are happiest when they can run across all real numbers.
164164
# So we use TransformVariables.jl to map all reals to positive values of our parameters, with sensible scales.
165165
# The `TVExp` transform maps all real numbers to positive values, and the `TVScale` transform scales the value to a more reasonable range.
166-
# The transform `ConstWrapTV` is defined in LyoPronto, and makes a constant callable function from a value.
167166

168-
# Kshf needs to be callable.
169-
# Rp needs to be a callable, and the `RpFormFit` struct does that; by passing the new values
170-
# with Rp as a NamedTuple, the constructor for `ParamObjPikal` will unpack it.
167+
# Kshf needs to be callable, so we wrap it in ConstPhysProp.
168+
# Rp needs to be a callable, and the `RpFormFit` struct with fields R0, A1, and A2 does that.
169+
# This `as` function uses TransformVariables to create a transform that maps from 4 real
170+
# numbers to callable Kshf and Rp, with appropriate scaling.
171171

172-
trans_KRp = as((Kshf = ConstWrapTV() TVScale(Kshf(0)) TVExp(),
173-
Rp=as((R0 = TVScale(R0) TVExp(),
172+
trans_KRp = as((Kshf = as(ConstPhysProp, (TVScale(Kshf(0)) TVExp(),)),
173+
Rp=as(RpFormFit, as((R0 = TVScale(R0) TVExp(),
174174
A1 = TVScale(A1) TVExp(),
175-
A2 = TVScale(A2) TVExp(),))))
175+
A2 = TVScale(A2) TVExp(),)))))
176176
## Or, using a convenience function for the same,
177177
trans_KRp = KRp_transform_basic(Kshf(0), R0, A1, A2)
178178
trans_Rp = Rp_transform_basic(R0, A1, A2)

src/LyoPronto.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export gen_sol_pd, obj_pd, gen_nsol_pd, objn_pd
5858
export KRp_transform_basic, K_transform_basic, Rp_transform_basic, KBB_transform_basic
5959
export KBB_transform_bounded
6060
export obj_expT, err_expT, err_expT!, num_errs, nls_pd, nls_pd!
61-
export ConstWrapTV
6261
# plotting tools (mostly already done by macros)
6362
export qrf_integrate
6463
# End of primary drying

src/paramfits.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ end
1414
Construct a typical transform for fitting Rp.
1515
"""
1616
function Rp_transform_basic(R0g, A1g, A2g)
17-
tr = as((
18-
Rp = as((
17+
tr = as((;
18+
Rp = as(RpFormFit, as((;
1919
R0 = TVScale(R0g) TVExp(),
2020
A1 = TVScale(A1g) TVExp(),
2121
A2 = TVScale(A2g) TVExp(),
22-
)),
22+
)),)
2323
))
2424
return tr
2525
end
@@ -29,7 +29,7 @@ end
2929
Construct a typical transform for fitting Kshf (a.k.a. Kv).
3030
"""
3131
function K_transform_basic(Kshfg)
32-
tr = as((Kshf = ConstWrapTV() TVScale(Kshfg) TVExp(),))
32+
tr = as((;Kshf = as(ConstPhysProp, (TVScale(Kshfg) TVExp(),))))
3333
return tr
3434
end
3535
"""
@@ -72,6 +72,7 @@ This small function runs
7272
```
7373
fitprm = transform(tr, fitlog)
7474
new_params = setproperties(po, fitprm)
75+
!isnothing(badprms) && badprms(new_params) && return NaN
7576
prob = ODEProblem(new_params; tspan=(0.0, 1000.0))
7677
sol = solve(prob, Rodas4(autodiff=AutoForwardDiff(chunksize=2)); saveat, kwargs...)
7778
```

src/pikal_model.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,6 @@ function ParamObjPikal(tuptup)
144144
return ParamObjPikal(tuptup[1]..., tuptup[2]..., tuptup[3]...)
145145
end
146146

147-
# This constructor catches if Rp is passed as a tuple or NamedTuple and constructs an appropriate object
148-
function ParamObjPikal(Rp::Union{NamedTuple, Tuple},
149-
hf0, csolid, ρsolution, Kshf, Av, Ap, pch, Tsh)
150-
return ParamObjPikal(RpFormFit(Rp...), hf0, csolid, ρsolution, Kshf, Av, Ap, pch, Tsh)
151-
end
152-
153147
function Base.getindex(p::ParamObjPikal, i::Int)
154148
if i == 1
155149
return (p.Rp, p.hf0, p.csolid, p.ρsolution)

src/rf_lumcap_model.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,6 @@ function ParamObjRF(tuptup::Tuple)
182182
end
183183
Base.size(po::ParamObjRF) = (6,)
184184

185-
# This constructor catches if Rp is passed as a tuple or NamedTuple and constructs an appropriate object
186-
function ParamObjRF(Rp::Union{NamedTuple, Tuple}, args...)
187-
return ParamObjRF(RpFormFit(Rp...), args...)
188-
end
189-
190185
function Base.getindex(po::ParamObjRF, i)
191186
if i == 1
192187
return (po.Rp, po.hf0, po.csolid, po.ρsolution)

src/structs.jl

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
A1
55
A2
66
end
7+
RpFormFit(;R0, A1, A2) = RpFormFit(R0, A1, A2)
78
@doc """
89
A convenience type for dealing with the common functional form given to Rp and Kv.
910
@@ -267,13 +268,3 @@ function Base.:(==)(p1::PrimaryDryFit, p2::PrimaryDryFit)
267268
cond6 = ismissing(p1.t_end) ? ismissing(p2.t_end) : (p1.t_end == p2.t_end)
268269
return all([cond1, cond2, cond3, cond4, cond5, cond6])
269270
end
270-
271-
# Add a little bit of sugar to our transforms
272-
struct ConstWrapTV <: TransformVariables.ScalarTransform end
273-
TransformVariables.transform(::ConstWrapTV, x) = ConstPhysProp(x)
274-
TransformVariables.inverse(::ConstWrapTV, x) = x.value
275-
276-
# These create method ambiguities with other TransformVariables methods
277-
# I don't think they were needed, but if so can be brought back with more specificity
278-
# TransformVariables.transform(t::TVScale{ConstPhysProp}, x) = ConstPhysProp(t.scale.value*x)
279-
# TransformVariables.inverse(t::TVScale{ConstPhysProp}, x) = x.value/t.scale.value

test/test_KRp_opt.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ pdfit = PrimaryDryFit(t, T; t_end)
4848
obj = OptimizationFunction(obj_pd, AutoForwardDiff(chunksize=4))
4949
opt = solve(OptimizationProblem(obj, pg, pass), optalg)
5050
vals = transform(tr, opt.u)
51+
@test all(opt.u .!= 0)
5152
@test vals.Kshf(pch(0)) Kshf(pch(0)) rtol=0.3
5253
@test vals.Rp.R0 R0 rtol=0.1
53-
@test vals.Rp.A1 A1 rtol=0.1
54-
@test vals.Rp.A2 A2 rtol=0.1
54+
@test vals.Rp.A1 A1 rtol=0.3
55+
@test vals.Rp.A2 A2 rtol=0.5
5556
end
5657

5758
@testset "Only Rp" begin
@@ -66,7 +67,7 @@ end
6667
opt = solve(OptimizationProblem(obj, pg, pass), optalg)
6768
vals = transform(tr, opt.u)
6869
@test vals.Rp.R0 R0 rtol=0.1
69-
@test vals.Rp.A1 A1 rtol=0.1
70+
@test vals.Rp.A1 A1 rtol=0.2
7071
@test vals.Rp.A2 A2 rtol=0.5
7172
end
7273

test/test_jet.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
using JET
22
# Can't use `mode = :basic` in CI because the Plots.jl recipe system triggers lots of false
33
# positives for JET. Not a bad idea to periodically check that manually, though.
4-
test_package(LyoPronto; mode = :typo, target_defined_modules=true)
4+
test_package(LyoPronto; mode = :typo, target_modules=(LyoPronto,))

0 commit comments

Comments
 (0)