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 tolerances in deserialization #703

Merged
merged 7 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
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 <me@phillipalday.com>", "Douglas Bates <dmbates@gmail.com>", "Jose Bayoan Santiago Calderon <jbs3hp@virginia.edu>"]
version = "4.15.0"
version = "4.16.0"

[deps]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
Expand Down
16 changes: 10 additions & 6 deletions src/serialization.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""
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=0, rtol::Real=atol > 0 ? 0 : √eps(T)
palday marked this conversation as resolved.
Show resolved Hide resolved
) where {T}
dict = JSON3.read(io)
ops = m.optsum
okay =
Expand All @@ -22,7 +24,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 +44,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
Loading