Skip to content

Commit

Permalink
Update flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
paschermayr committed Jul 27, 2022
1 parent 4b8fd6a commit 833936e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Baytes"
uuid = "72ddfcfc-6e9d-43df-829b-7aed7c549d4f"
authors = ["Patrick Aschermayr <p.aschermayr@gmail.com>"]
version = "0.1.6"
version = "0.1.7"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
26 changes: 21 additions & 5 deletions src/sampling/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ struct TraceTransform{T<:Tagged, P}
return new{T,P}(tagged, paramnames, chains, algorithms, burnin, thinning, maxiterations, effective_iterations)
end
end
function TraceTransform(trace::Trace, model::ModelWrapper)
tagged = Tagged(model, trace.info.sampling.printedparam.printed)
function TraceTransform(
trace::Trace,
model::ModelWrapper,
tagged::Tagged = Tagged(model, trace.info.sampling.printedparam.printed)
)
paramnames = ModelWrappers.paramnames(
tagged.info.reconstruct.default, tagged.info.constraint, subset(model.val, tagged.parameter)
)
Expand Down Expand Up @@ -148,9 +151,22 @@ Flatten Vector of Parameter NamedTuples into a Matrix, where each row represents
```
"""
function flatten_chainvals(trace::Trace, transform::TraceTransform)
@unpack tagged = transform
return reduce(hcat, map(x -> flatten(tagged.info.reconstruct, x), merge_chainvals(trace, transform) ) )
function flatten_chainvals(
trace::Trace,
transform::TraceTransform
)
## Get trace information
@unpack tagged, chains, effective_iterations = transform
## Preallocate array
mcmcchain = [ [ zeros(tagged.info.reconstruct.default.output, length(tagged)) for _ in eachindex(effective_iterations) ] for _ in eachindex(chains) ]
## Flatten corresponding parameter
Threads.@threads for (idx, chain) in collect(enumerate(chains))
for (iter0, iterburnin) in enumerate(effective_iterations)
mcmcchain[idx][iter0] .= flatten(tagged.info.reconstruct, subset(trace.val[chain][iterburnin], tagged.parameter))
end
end
## Return MCMCChain
return mcmcchain
end

################################################################################
Expand Down
2 changes: 1 addition & 1 deletion test/test-construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ tempermethod = tempermethods[iter]
g_vals = get_chainvals(trace, transform)
m_vals = merge_chainvals(trace, transform)
f_vals = flatten_chainvals(trace, transform)
@test sum( map(val -> length(val), g_vals) ) == length(m_vals) == size(f_vals,2)
@test sum( map(val -> length(val), g_vals) ) == length(m_vals) == sum( map(val -> length(val), f_vals) )

get_chaindiagnostics(trace, transform)

Expand Down

2 comments on commit 833936e

@paschermayr
Copy link
Owner Author

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

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 v0.1.7 -m "<description of version>" 833936ef0806893036386f2263bb6be6be617b39
git push origin v0.1.7

Please sign in to comment.