Skip to content

Commit

Permalink
add finally block for restoring original model in profiling (#795)
Browse files Browse the repository at this point in the history
* add finally block for restoring original model in profiling

* version bump + NEWS

* scope is hard

* test

* bump macos runner version

* Update src/profile/profile.jl

Co-authored-by: Alex Arslan <[email protected]>

---------

Co-authored-by: Alex Arslan <[email protected]>
  • Loading branch information
palday and ararslan authored Dec 10, 2024
1 parent 2f92408 commit 7103ee1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/minimum.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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
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.27.0"
version = "4.27.1"

[deps]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
Expand Down
39 changes: 23 additions & 16 deletions src/profile/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,30 @@ 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)
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
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)
MixedModelProfile(m, Table(val.tbl), val.fwd, val.rev)
catch ex
@error "Exception occurred in profiling; aborting..."
rethrow()
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)
return profile
end

"""
Expand Down
7 changes: 7 additions & 0 deletions test/pls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit 7103ee1

@palday
Copy link
Member Author

@palday palday commented on 7103ee1 Dec 10, 2024

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/121072

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.27.1 -m "<description of version>" 7103ee1276b9bf0c060fcd977fc63cdfa5f9d4e1
git push origin v4.27.1

Please sign in to comment.