From 9250d2b5390669dc9da585b06b450c9fe898f15f Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Mon, 9 Dec 2024 14:21:25 -0600 Subject: [PATCH 1/6] add finally block for restoring original model in profiling --- src/profile/profile.jl | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/profile/profile.jl b/src/profile/profile.jl index e3cd52abc..608a71b86 100644 --- a/src/profile/profile.jl +++ b/src/profile/profile.jl @@ -35,22 +35,28 @@ value of ζ exceeds `threshold`. function profile(m::LinearMixedModel; threshold=4) isfitted(m) || refit!(m) final = copy(m.optsum.final) - tc = TableColumns(m) - val = profileσ(m, tc; threshold) # FIXME: defer creating the splines until the whole table is constructed - objective!(m, final) # restore the parameter estimates - for s in filter(s -> startswith(string(s), 'β'), keys(first(val.tbl))) - profileβj!(val, tc, s; threshold) - end - copyto!(m.optsum.final, final) - m.optsum.fmin = objective!(m, final) - for s in filter(s -> startswith(string(s), 'θ'), keys(first(val.tbl))) - profileθj!(val, s, tc; threshold) + try + tc = TableColumns(m) + val = profileσ(m, tc; threshold) # FIXME: defer creating the splines until the whole table is constructed + objective!(m, final) # restore the parameter estimates + for s in filter(s -> startswith(string(s), 'β'), keys(first(val.tbl))) + profileβj!(val, tc, s; threshold) + end + copyto!(m.optsum.final, final) + m.optsum.fmin = objective!(m, final) + for s in filter(s -> startswith(string(s), 'θ'), keys(first(val.tbl))) + profileθj!(val, s, tc; threshold) + end + profileσs!(val, tc) + catch ex + @error "Exception occurred in profiling; aborting..." + rethrow(ex) + finally + objective!(m, final) # restore the parameter estimates + copyto!(m.optsum.final, final) + m.optsum.fmin = objective(m) + m.optsum.sigma = nothing end - profileσs!(val, tc) - objective!(m, final) # restore the parameter estimates - copyto!(m.optsum.final, final) - m.optsum.fmin = objective(m) - m.optsum.sigma = nothing return MixedModelProfile(m, Table(val.tbl), val.fwd, val.rev) end From e4d00891651d73331bde5e35a7cf0f0c8f2ab28f Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Mon, 9 Dec 2024 14:23:37 -0600 Subject: [PATCH 2/6] version bump + NEWS --- NEWS.md | 5 +++++ Project.toml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 5fc9f88f7..647d85eb5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +MixedModels v4.27.1 Release Notes +============================== +- `profile` now includes a `finally` block to restore the original model even if an error occurs before profiling is complete [#795] + MixedModels v4.27.0 Release Notes ============================== - `saveoptsum` and `restoreoptsum!` now support `GeneralizedLinearMixedModel`s [#791] @@ -575,3 +579,4 @@ Package dependencies [#783]: https://github.com/JuliaStats/MixedModels.jl/issues/783 [#785]: https://github.com/JuliaStats/MixedModels.jl/issues/785 [#791]: https://github.com/JuliaStats/MixedModels.jl/issues/791 +[#795]: https://github.com/JuliaStats/MixedModels.jl/issues/795 diff --git a/Project.toml b/Project.toml index 42348b1eb..7c2c65bc5 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "MixedModels" uuid = "ff71e718-51f3-5ec2-a782-8ffcbfa3c316" author = ["Phillip Alday ", "Douglas Bates ", "Jose Bayoan Santiago Calderon "] -version = "4.27.0" +version = "4.27.1" [deps] Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45" From c9c5fe268f4bd86508ecc0d165d7fcd117c7cf2c Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Mon, 9 Dec 2024 14:44:17 -0600 Subject: [PATCH 3/6] scope is hard --- src/profile/profile.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/profile/profile.jl b/src/profile/profile.jl index 608a71b86..0069936f0 100644 --- a/src/profile/profile.jl +++ b/src/profile/profile.jl @@ -35,7 +35,7 @@ value of ζ exceeds `threshold`. function profile(m::LinearMixedModel; threshold=4) isfitted(m) || refit!(m) final = copy(m.optsum.final) - try + profile = try tc = TableColumns(m) val = profileσ(m, tc; threshold) # FIXME: defer creating the splines until the whole table is constructed objective!(m, final) # restore the parameter estimates @@ -48,6 +48,7 @@ function profile(m::LinearMixedModel; threshold=4) profileθj!(val, s, tc; threshold) end profileσs!(val, tc) + MixedModelProfile(m, Table(val.tbl), val.fwd, val.rev) catch ex @error "Exception occurred in profiling; aborting..." rethrow(ex) @@ -57,7 +58,7 @@ function profile(m::LinearMixedModel; threshold=4) m.optsum.fmin = objective(m) m.optsum.sigma = nothing end - return MixedModelProfile(m, Table(val.tbl), val.fwd, val.rev) + return profile end """ From a1b8ad224df70951cca2ba50518f7e0bc177e526 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Mon, 9 Dec 2024 16:18:09 -0600 Subject: [PATCH 4/6] test --- test/pls.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/pls.jl b/test/pls.jl index 98d239ec3..95b1e877b 100644 --- a/test/pls.jl +++ b/test/pls.jl @@ -725,6 +725,13 @@ end # very loose tolerance for unstable fit # but this is a convenient test of rankUpdate!(::UniformBlockDiagonal) @test isapprox(m.θ, θnlopt; atol=5e-2) + + @testset "profile" begin + # TODO: actually handle the case here so that it doesn't error and + # create a separate test of the error handling code + @test_logs((:error, "Exception occurred in profiling; aborting..."), + @test_throws Exception profile(last(models(:oxide)))) + end end @testset "Rank deficient" begin From 2f09cd8875436592984208b1ad68f0292600b857 Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Mon, 9 Dec 2024 16:53:26 -0600 Subject: [PATCH 5/6] bump macos runner version --- .github/workflows/minimum.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/minimum.yml b/.github/workflows/minimum.yml index 459f72983..0685927c0 100644 --- a/.github/workflows/minimum.yml +++ b/.github/workflows/minimum.yml @@ -22,7 +22,7 @@ jobs: matrix: julia-version: [min] julia-arch: [x64] - os: [ubuntu-22.04, macos-12, windows-2019] + os: [ubuntu-22.04, macos-13, windows-2019] steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 From 8a67eff04dff0d23c5df516c0d966f3960b0af2b Mon Sep 17 00:00:00 2001 From: Phillip Alday Date: Tue, 10 Dec 2024 03:48:40 +0000 Subject: [PATCH 6/6] Update src/profile/profile.jl Co-authored-by: Alex Arslan --- src/profile/profile.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/profile/profile.jl b/src/profile/profile.jl index 0069936f0..82d71944c 100644 --- a/src/profile/profile.jl +++ b/src/profile/profile.jl @@ -51,7 +51,7 @@ function profile(m::LinearMixedModel; threshold=4) MixedModelProfile(m, Table(val.tbl), val.fwd, val.rev) catch ex @error "Exception occurred in profiling; aborting..." - rethrow(ex) + rethrow() finally objective!(m, final) # restore the parameter estimates copyto!(m.optsum.final, final)