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 Base.get method for ModeResult #2269

Merged
merged 3 commits into from
Jun 23, 2024
Merged

Add Base.get method for ModeResult #2269

merged 3 commits into from
Jun 23, 2024

Conversation

mhauru
Copy link
Member

@mhauru mhauru commented Jun 20, 2024

Answers the feature request from #1417

The return type here is a bit questionable. It currently mimics the NamedTuples that MCMCChains returns for Base.get, but I also considered returning NamedArrays like the ModeResult.values field. Opinions on this are welcome.

Fix #1417

using Random: Random
using Optimization
using Optimization: Optimization
using OptimizationBBO: OptimizationBBO
using OptimizationNLopt: OptimizationNLopt
using OptimizationOptimJL: OptimizationOptimJL
using ReverseDiff: ReverseDiff
Copy link
Member Author

Choose a reason for hiding this comment

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

This is unrelated to the other changes in this PR. Not having this here before was just a plain bug.

Copy link
Member

@yebai yebai left a comment

Choose a reason for hiding this comment

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

Thanks @mhauru -- looks very good to me.

Base.get(m::ModeResult, var_symbols::Vector{Symbol})

Return the values of all the variables with the symbol(s) `var_symbol` in the mode result
`m`. The return value is a `NamedTuple` with `var_symbols` as the key(s).
Copy link
Member

Choose a reason for hiding this comment

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

Intuitively I would assume that indexing with a Tuple might (possibly) return a NamedTuple. But returning a NamedTuple for Vector inputs seems quite surprising, and also means that not even the keys of the NamedTuple return type can be inferred.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I changed this so that the second argument can be any iterator type, and made the single symbol version use a singleton tuple. I also stabilised the array types a bit. Let me know if you are happy with this.

@mhauru
Copy link
Member Author

mhauru commented Jun 21, 2024

As a fun side effect of this PR, I ran into this case of bizarrely slow type inference: JuliaLang/julia#54879

@yebai yebai merged commit 7b2869f into master Jun 23, 2024
57 checks passed
@yebai yebai deleted the mhauru/moderesult-get branch June 23, 2024 19:23
@yebai
Copy link
Member

yebai commented Jun 23, 2024

Thanks @mhauru and @devmotion!

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.

Feature Request: Better Indexing for ModeResult object
3 participants