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

Inferencedata dimarray tests #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 20 additions & 17 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
CompatHelperLocal = "5224ae11-6099-4aaa-941d-3aab004bd678"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
InferenceObjects = "b5cf5a8d-e756-4ee3-b014-01d49d192c00"
Expand All @@ -29,13 +30,30 @@ TableOperations = "ab02a1b2-a7df-11e8-156e-fb1833f50b87"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[weakdeps]
AxisKeys = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
BridgeStan = "c88b6f0a-829e-4b0b-94b7-f06ab5908f5a"
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
InferenceObjects = "b5cf5a8d-e756-4ee3-b014-01d49d192c00"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
MonteCarloMeasurements = "0987c9cc-fe09-11e8-30f0-b96dd679fdca"

[extensions]
AxisKeysExt = "AxisKeys"
BridgeStanExt = "BridgeStan"
DimensionalDataExt = "DimensionalData"
InferenceObjectsExt = "InferenceObjects"
MCMCChainsExt = "MCMCChains"
MonteCarloMeasurementsExt = "MonteCarloMeasurements"

[compat]
AxisKeys = "0.2"
BridgeStan = "1.0.1"
CSV = "0.10"
CompatHelperLocal = "0.1"
DataFrames = "1.3"
DelimitedFiles = "1.8, 1.9"
DimensionalData = "0.24.2"
DocStringExtensions = "0.8, 0.9"
InferenceObjects = "0.3.2"
JSON = "0.21"
Expand All @@ -52,17 +70,11 @@ TableOperations = "1.0"
Tables = "1.5"
julia = "1.8"

[extensions]
AxisKeysExt = "AxisKeys"
BridgeStanExt = "BridgeStan"
InferenceObjectsExt = "InferenceObjects"
MCMCChainsExt = "MCMCChains"
MonteCarloMeasurementsExt = "MonteCarloMeasurements"

[extras]
AxisKeys = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
BridgeStan = "c88b6f0a-829e-4b0b-94b7-f06ab5908f5a"
CPUSummary = "2a0fbf3d-bb9c-48f3-b0a9-814d99fd7ab9"
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
InferenceObjects = "b5cf5a8d-e756-4ee3-b014-01d49d192c00"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand All @@ -74,13 +86,4 @@ StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["AxisKeys", "BridgeStan", "Distributions", "Statistics", "LinearAlgebra", "MCMCChains",
"MonteCarloMeasurements", "InferenceObjects", "StatsBase", "StatsFuns", "Test"]

[weakdeps]
AxisKeys = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
BridgeStan = "c88b6f0a-829e-4b0b-94b7-f06ab5908f5a"
InferenceObjects = "b5cf5a8d-e756-4ee3-b014-01d49d192c00"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
MonteCarloMeasurements = "0987c9cc-fe09-11e8-30f0-b96dd679fdca"

test = ["AxisKeys", "BridgeStan", "DimensionalData", "Distributions", "Statistics", "LinearAlgebra", "MCMCChains", "MonteCarloMeasurements", "InferenceObjects", "StatsBase", "StatsFuns", "Test"]
49 changes: 49 additions & 0 deletions ext/DimensionalDataExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module DimensionalDataExt

using StanSample, DocStringExtensions

StanSample.EXTENSIONS_SUPPORTED ? (using DimensionalData) : (using ..DimensionalData)

import StanSample: convert_a3d, matrix
import DimensionalData: @dim, XDim, YDim, ZDim, DimArray, Dimensions, dims, At

@dim iteration XDim "iterations"
@dim chain YDim "chains"
@dim param ZDim "parameters"

function convert_a3d(a3d_array, cnames, ::Val{:dimarrays})
psymbols= Symbol.(cnames)
pa = permutedims(a3d_array, [1, 3, 2])

DimArray(pa, (iteration, chain, param(psymbols)); name=:draws)
end

function convert_a3d(a3d_array, cnames, ::Val{:dimarray})
psymbols= Symbol.(cnames)

# Permute [draws, params, chains] to [draws, chains, params]
a3dp = permutedims(a3d_array, [1, 3, 2])

# Append all chains
iters, chains, pars = size(a3dp)
a3dpa = reshape(a3dp, iters*chains, pars)

# Create the DimArray
DimArray(a3dpa, (iteration, param(psymbols)); name=:draws)
end

function matrix(da::DimArray, sym::Union{Symbol, String})
n = string.(dims(da, :param).val)
syms = string(sym)
sel = String[]
for (i, s) in enumerate(n)
if length(s) > length(syms) && syms == n[i][1:length(syms)] &&
n[i][length(syms)+1] in ['[', '.', '_']
append!(sel, [n[i]])
end
end
length(sel) == 0 && error("$syms not in $n")
da[param=At(Symbol.(sel))]
end

end
1 change: 1 addition & 0 deletions src/StanSample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function __init__()
@require MCMCChains="c7f686f2-ff18-58e9-bc7b-31028e88f75d" include("../ext/MCMCChainsExt.jl")
@require AxisKeys="94b1ba4f-4ee9-5380-92f1-94cde586c3c5" include("../ext/AxisKeysExt.jl")
@require InferenceObjects="b5cf5a8d-e756-4ee3-b014-01d49d192c00" include("../ext/InferenceObjectsExt.jl")
@require DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0" include("../ext/DimensionalDataExt.jl")
end
end

Expand Down
30 changes: 12 additions & 18 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ if haskey(ENV, "CMDSTAN") || haskey(ENV, "JULIA_CMDSTAN_HOME")

end

#if Int(VERSION.minor) > 8

test_bridgestan = [
"test_bridgestan/test_bridgestan.jl",
]
Expand All @@ -70,9 +68,19 @@ if haskey(ENV, "CMDSTAN") || haskey(ENV, "JULIA_CMDSTAN_HOME")
end
println()
end

#end

test_dimensionaldata = [
"test_dimensionaldata/test_dimensionaldata.jl",
]

@testset "DimensionalData interface" begin
for test in test_dimensionaldata
println("\nTesting: $test.")
include(joinpath(TestDir, test))
end
println()
end

println()
test_inferencedata = [
"test_inferencedata/test_inferencedata.jl",
Expand Down Expand Up @@ -175,20 +183,6 @@ if haskey(ENV, "CMDSTAN") || haskey(ENV, "JULIA_CMDSTAN_HOME")
println()
end

test_dimensionaldata = [
"test_dimensionaldata/test_dimensionaldata.jl",
]

#=
@testset "DimensionalData interface" begin
for test in test_dimensionaldata
println("\nTesting: $test.")
include(joinpath(TestDir, test))
end
println()
end
=#

test_keywords = [
"test_keywords/test_bernoulli_keyedarray_01.jl",
"test_keywords/test_bernoulli_keyedarray_02.jl",
Expand Down