Skip to content

Commit

Permalink
Add tolerances in deserialization (#703)
Browse files Browse the repository at this point in the history
* support passing tolerances to restoreoptsum!

* test

* NEWS

* version bump

* JuliaFormatter

---------

Co-authored-by: Douglas Bates <[email protected]>
  • Loading branch information
palday and dmbates authored Aug 8, 2023
1 parent 8d71961 commit 5ade8c4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
MixedModels v4.16.0 Release Notes
==============================
* Support for check tolerances in deserialization. [#703]

MixedModels v4.15.0 Release Notes
==============================
* Support for different optimization criteria during the bootstrap. [#694]
Expand Down Expand Up @@ -442,3 +446,4 @@ Package dependencies
[#681]: https://github.com/JuliaStats/MixedModels.jl/issues/681
[#682]: https://github.com/JuliaStats/MixedModels.jl/issues/682
[#694]: https://github.com/JuliaStats/MixedModels.jl/issues/694
[#703]: https://github.com/JuliaStats/MixedModels.jl/issues/703
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MixedModels"
uuid = "ff71e718-51f3-5ec2-a782-8ffcbfa3c316"
author = ["Phillip Alday <[email protected]>", "Douglas Bates <[email protected]>", "Jose Bayoan Santiago Calderon <[email protected]>"]
version = "4.15.0"
version = "4.16.0"

[deps]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
Expand Down
17 changes: 11 additions & 6 deletions src/serialization.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""
restoreoptsum!(m::LinearMixedModel, io::IO)
restoreoptsum!(m::LinearMixedModel, filename)
restoreoptsum!(m::LinearMixedModel, io::IO; atol::Real=0, rtol::Real=atol>0 ? 0 : √eps)
restoreoptsum!(m::LinearMixedModel, filename; atol::Real=0, rtol::Real=atol>0 ? 0 : √eps)
Read, check, and restore the `optsum` field from a JSON stream or filename.
"""
function restoreoptsum!(m::LinearMixedModel{T}, io::IO) where {T}
function restoreoptsum!(
m::LinearMixedModel{T}, io::IO; atol::Real=zero(T),
rtol::Real=atol > 0 ? zero(T) : eps(T),
) where {T}
dict = JSON3.read(io)
ops = m.optsum
okay =
Expand All @@ -22,7 +25,9 @@ function restoreoptsum!(m::LinearMixedModel{T}, io::IO) where {T}
copyto!(ops.initial, dict.initial)
copyto!(ops.final, dict.final)
for (v, f) in (:initial => :finitial, :final => :fmin)
if !isapprox(objective(updateL!(setθ!(m, getfield(ops, v)))), getfield(ops, f))
if !isapprox(
objective(updateL!(setθ!(m, getfield(ops, v)))), getfield(ops, f); rtol, atol
)
throw(ArgumentError("model m at $v does not give stored $f"))
end
end
Expand All @@ -40,9 +45,9 @@ function restoreoptsum!(m::LinearMixedModel{T}, io::IO) where {T}
return m
end

function restoreoptsum!(m::LinearMixedModel{T}, filename) where {T}
function restoreoptsum!(m::LinearMixedModel{T}, filename; kwargs...) where {T}
open(filename, "r") do io
restoreoptsum!(m, io)
restoreoptsum!(m, io; kwargs...)
end
end

Expand Down
16 changes: 13 additions & 3 deletions test/pls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ end
sigma0row = only(filter(r -> r.p == && iszero(r.ζ), dspr01.tbl))
@test sigma0row.σ dspr01.m.σ
@test sigma0row.β1 only(dspr01.m.β)
@test sigma0row.θ1 only(dspr01.m.θ)
@test sigma0row.θ1 only(dspr01.m.θ)
end
end

Expand Down Expand Up @@ -500,14 +500,23 @@ end

@testset "optsumJSON" begin
fm = last(models(:sleepstudy))
# using a IOBuffer for saving JSON
# using a IOBuffer for saving JSON
saveoptsum(seekstart(io), fm)
m = LinearMixedModel(fm.formula, MixedModels.dataset(:sleepstudy))
restoreoptsum!(m, seekstart(io))
@test loglikelihood(fm) loglikelihood(m)
@test bic(fm) bic(m)
@test coef(fm) coef(m)
# using a temporary file for saving JSON

fm_mod = deepcopy(fm)
fm_mod.optsum.fmin += 1
saveoptsum(seekstart(io), fm_mod)
@test_throws(ArgumentError("model m at final does not give stored fmin"),
restoreoptsum!(m, seekstart(io)))
restoreoptsum!(m, seekstart(io); atol=1)
@test m.optsum.fmin - fm.optsum.fmin 1

# using a temporary file for saving JSON
fnm = first(mktemp())
saveoptsum(fnm, fm)
m = LinearMixedModel(fm.formula, MixedModels.dataset(:sleepstudy))
Expand All @@ -516,6 +525,7 @@ end
@test bic(fm) bic(m)
@test coef(fm) coef(m)
end

@testset "profile" begin
pr = profile(last(models(:sleepstudy)))
tbl = pr.tbl
Expand Down

2 comments on commit 5ade8c4

@palday
Copy link
Member Author

@palday palday commented on 5ade8c4 Aug 8, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/89276

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v4.16.0 -m "<description of version>" 5ade8c4613f52656bde992cc861d8396286c971d
git push origin v4.16.0

Please sign in to comment.