Skip to content

Commit 2178718

Browse files
committed
address new and old fitlog formats in serialization
1 parent cdb4fa8 commit 2178718

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/serialization.jl

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function restoreoptsum!(
7373
end
7474

7575
function restoreoptsum!(ops::OptSummary{T}, dict::AbstractDict) where {T}
76+
warn_old_version = true
7677
allowed_missing = (
7778
:lowerbd, # never saved, -Inf not allowed in JSON
7879
:xtol_zero_abs, # added in v4.25.0
@@ -91,7 +92,9 @@ function restoreoptsum!(ops::OptSummary{T}, dict::AbstractDict) where {T}
9192
throw(ArgumentError(string("optsum names: ", nmdiff, " not found in io")))
9293
end
9394
if length(setdiff(allowed_missing, keys(dict))) > 1 # 1 because :lowerbd
94-
@warn "optsum was saved with an older version of MixedModels.jl: consider resaving."
95+
@debug "" setdiff(allowed_missing, keys(dict))
96+
warn_old_version && @warn "optsum was saved with an older version of MixedModels.jl: consider resaving."
97+
warn_old_version = false
9598
end
9699

97100
if any(ops.lowerbd .> dict.initial) || any(ops.lowerbd .> dict.final)
@@ -116,13 +119,26 @@ function restoreoptsum!(ops::OptSummary{T}, dict::AbstractDict) where {T}
116119
fitlog = get(dict, :fitlog, nothing)
117120
ops.fitlog = if isnothing(fitlog)
118121
# compat with fits saved before fitlog
119-
[(ops.initial, ops.finitial), (ops.final, ops.fmin)]
122+
Table(; θ=(ops.initial, ops.finitial), objective=(ops.final, ops.fmin))
120123
else
121-
[(convert(Vector{T}, first(entry)), T(last(entry))) for entry in fitlog]
124+
_deserialize_fitlog(fitlog, T, warn_old_version)
122125
end
123126
return ops
124127
end
125128

129+
# fitlog structure in MixedModels 4.x
130+
function _deserialize_fitlog(fitlog, T, warn_old_version::Bool)
131+
warn_old_version && @warn "optsum was saved with an older version of MixedModels.jl: consider resaving."
132+
warn_old_version = false
133+
return Table(( (; θ=convert(Vector{T}, first(entry)),
134+
objective=T(last(entry))) for entry in fitlog))
135+
end
136+
137+
function _deserialize_fitlog(fitlog::JSON3.Array{JSON3.Object}, T, ::Bool)
138+
return Table(( (; θ=convert(Vector{T}, entry.θ),
139+
objective=T(entry.objective)) for entry in fitlog))
140+
end
141+
126142
StructTypes.StructType(::Type{<:OptSummary}) = StructTypes.Mutable()
127143
StructTypes.excludes(::Type{<:OptSummary}) = (:lowerbd,)
128144

test/pls.jl

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,41 @@ end
626626
)
627627
@test_throws(ArgumentError("optsum names: [:ftol_abs] not found in io"),
628628
restoreoptsum!(m, seekstart(iob)))
629-
629+
# note that this contains a fitlog from an older version!
630+
iob = IOBuffer(
631+
"""
632+
{
633+
"initial":[1.0,0.0,1.0],
634+
"finitial":1784.642296192436,
635+
"ftol_rel":1.0e-12,
636+
"ftol_abs":1.0e-8,
637+
"xtol_rel":0.0,
638+
"xtol_abs":[1.0e-10,1.0e-10,1.0e-10],
639+
"rhobeg":1.0,
640+
"rhoend":1.0e-6,
641+
"xtol_zero_abs":0.001,
642+
"ftol_zero_abs":1.0e-5,
643+
"backend": "nlopt",
644+
"initial_step":[0.75,1.0,0.75],
645+
"maxfeval":-1,
646+
"maxtime":-1.0,
647+
"feval":57,
648+
"final":[0.9292213195402981,0.01816837807519162,0.22264487477788353],
649+
"fmin":1751.9393444646712,
650+
"optimizer":"LN_BOBYQA",
651+
"returnvalue":"FTOL_REACHED",
652+
"nAGQ":1,
653+
"REML":false,
654+
"sigma":null,
655+
"fitlog":[[[1.0,0.0,1.0],1784.642296192436]]
656+
}
657+
""",
658+
)
659+
@test_logs(
660+
(:warn,
661+
r"optsum was saved with an older version of MixedModels.jl: consider resaving",
662+
),
663+
restoreoptsum!(m, seekstart(iob)))
630664
# iob = IOBuffer(
631665
# """
632666
# {

0 commit comments

Comments
 (0)