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

Escape regex in namesingroup properly #398

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
keywords = ["markov chain monte carlo", "probablistic programming"]
license = "MIT"
desc = "Chain types and utility functions for MCMC simulations."
version = "5.7.0"
version = "5.7.1"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
8 changes: 7 additions & 1 deletion src/chains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ julia> namesingroup(chn, :A)
2-element Vector{Symbol}:
Symbol("A[1]")
Symbol("A[2]")

julia> # Also works for specific elements.
namesingroup(chn, Symbol("A[1]"))
1-element Vector{Symbol}:
Symbol("A[1]")

```
```jldoctest
julia> chn = Chains(rand(100, 3, 2), ["A.1", "A.2", "B"]);
Expand All @@ -155,7 +161,7 @@ function namesingroup(chains::Chains, sym::Symbol; index_type::Symbol=:bracket)
idx_str = index_type == :bracket ? "[" : "."
# Start by looking up the symbols in the list of parameter names.
names_of_params = names(chains)
regex = Regex("^$sym\$|^$sym\\$idx_str")
regex = Regex("^\\Q$sym\\E\$|^\\Q$sym$idx_str\\E")
indices = findall(x -> match(regex, string(x)) !== nothing, names(chains))
return names_of_params[indices]
end
Expand Down