Skip to content

Commit

Permalink
Fix construction of Array from empty chains (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored Jul 9, 2021
1 parent 46b12c5 commit bb01f11
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
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 = "4.13.0"
version = "4.13.1"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
4 changes: 2 additions & 2 deletions src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ function Base.Array(
end

function to_matrix(chain::Chains)
return Matrix(reshape(permutedims(chain.value.data, (1, 3, 2)), :, size(chain, 2)))
x = permutedims(chain.value.data, (1, 3, 2))
return Matrix(reshape(x, size(x, 1) * size(x, 2), size(x, 3)))
end

function to_vector(chain::Chains)
Expand All @@ -79,4 +80,3 @@ function to_vector_of_matrices(chain::Chains)
data = chain.value.data
return [Matrix(data[:, :, i]) for i in axes(data, 3)]
end

8 changes: 4 additions & 4 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function merge_union(a::NamedTuple{an}, b::NamedTuple{bn}) where {an,bn}
:(getfield(b, $(QuoteNode(n))))
end
end

return :(NamedTuple{$names,$types}(($(values...),)))
else
names = Base.merge_names(an, bn)
Expand All @@ -113,7 +113,7 @@ function merge_union(a::NamedTuple{an}, b::NamedTuple{bn}) where {an,bn}
getfield(b, n)
end
end

return NamedTuple{names,types}(values)
end
end
Expand Down Expand Up @@ -179,8 +179,8 @@ function concretize(x::AbstractArray)
return x
else
xnew = map(concretize, x)
T = mapreduce(typeof, promote_type, xnew)
if T <: eltype(xnew)
T = mapreduce(typeof, promote_type, xnew; init=Union{})
if T <: eltype(xnew) && T !== Union{}
return convert(AbstractArray{T}, xnew)
else
return xnew
Expand Down
5 changes: 5 additions & 0 deletions test/arrayconstructor_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ using MCMCChains, Test
Array(chns[:a])
Array(chns, [:parameters])
Array(chns, [:parameters, :internals])

# empty chain: #317
empty_chain = chns[Symbol[]]
@test isempty(MCMCChains.to_matrix(empty_chain))
@test isempty(Array(empty_chain))
end
@testset "Accuracy" begin
nchains = 5
Expand Down

2 comments on commit bb01f11

@devmotion
Copy link
Member 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/40593

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.13.1 -m "<description of version>" bb01f11061510007130d877a1133c1bbcafb146c
git push origin v4.13.1

Please sign in to comment.