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 error hint for MethodErrors on LogDensityModel #146

Merged
merged 6 commits into from
Sep 12, 2024

Conversation

penelopeysm
Copy link
Member

If a user creates their own AbstractMCMC sampler and passes it something that obeys the LogDensityProblems.jl interface, AbstractMCMC quietly wraps it in a LogDensityModel type which doesn't obey the same interface, which can be mildly confusing. (Well, it did catch me out, so there may be other new users out there too who bumped into it too :))

As this is a conscious decision (cf. #110 (comment)) this PR makes it such that if somebody tries to call LogDensityProblems.jl methods on a LogDensityModel, they get a friendly nudge in the right direction.

MWE:

julia> using LogDensityProblems

julia> using AbstractMCMC: LogDensityModel

julia> struct MyModel end

julia> # In practice one would define more methods, but this is the only one needed for AbstractMCMC to not complain

julia> LogDensityProblems.capabilities(::Type{MyModel}) = LogDensityProblems.LogDensityOrder{0}()

julia> lm = LogDensityModel(MyModel())
LogDensityModel{MyModel}(MyModel())

julia> LogDensityProblems.logdensity(lm, 0)
ERROR: MethodError: no method matching logdensity(::LogDensityModel{MyModel}, ::Int64)
AbstractMCMC.LogDensityModel is a wrapper and does not itself implement the LogDensityProblems.jl interface. To use LogDensityProblems.jl methods, access the inner type with (e.g.) `logdensity(model.logdensity, params)` instead of `logdensity(model, params)`.
Stacktrace:
 [1] top-level scope
   @ REPL[7]:1

src/AbstractMCMC.jl Outdated Show resolved Hide resolved
src/AbstractMCMC.jl Outdated Show resolved Hide resolved
@penelopeysm
Copy link
Member Author

(Thanks on both points :))

Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

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

Maybe it would be good to add a (very simple) test as well to ensure it works as intended?

Project.toml Outdated Show resolved Hide resolved
Copy link

codecov bot commented Sep 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.23%. Comparing base (2a77f53) to head (13ebdab).
Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #146      +/-   ##
==========================================
+ Coverage   97.19%   97.23%   +0.04%     
==========================================
  Files           8        8              
  Lines         321      326       +5     
==========================================
+ Hits          312      317       +5     
  Misses          9        9              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@penelopeysm penelopeysm merged commit 8431b31 into master Sep 12, 2024
28 checks passed
@penelopeysm penelopeysm deleted the pysm/error-hint branch September 12, 2024 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants