Skip to content

Commit

Permalink
Run extension tests in separate workflow (#317)
Browse files Browse the repository at this point in the history
* Use correct imports in helpers

* Define extension tests

* Delete old tests

* Remove weakdeps from extras

* Define workflow for testing extensions

* Run extension tests on older Julia versions

* Run MCMCChains tests separately

* Create Project for MCMCChains tests

* Run ArviZMCMCChainsExt tests

* Remove unused test deps

* Add missing DimensionalData compats

* Don't include helpers if unneeded

* Update extension test deps with CompatHelper

* Add back weak deps to extras
  • Loading branch information
sethaxen authored Dec 19, 2023
1 parent d835e74 commit f479d37
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/CompatHelper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ jobs:
COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }}
run: |
using CompatHelper
CompatHelper.main(; subdirs=["", "docs"])
subdirs = ["", "docs", readdir("test/ext"; join=true)...]
CompatHelper.main(; subdirs)
36 changes: 36 additions & 0 deletions .github/workflows/ExtensionTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: ExtensionTest
on:
push:
branches: [main]
tags: [v*]
pull_request:
jobs:
test:
name: ${{ matrix.extension }} - Julia ${{ matrix.julia-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
julia-version: [1.6, 1]
extension:
- ArviZMCMCChainsExt
- ArviZSampleChainsExt
- ArviZSampleChainsDynamicHMCExt
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.julia-version }}
arch: x64
- uses: julia-actions/julia-buildpkg@v1
- run: |
julia --code-coverage=user -e '
using Pkg
test_path = joinpath(pwd(), "test", "ext", "${{ matrix.extension }}")
Pkg.activate(test_path)
Pkg.develop(PackageSpec(path=pwd()))
include(joinpath(test_path, "runtests.jl"))'
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v2
with:
files: lcov.info
5 changes: 1 addition & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ StatsBase = "0.32, 0.33, 0.34"
julia = "1.6"

[extras]
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SampleChains = "754583d1-7fc4-4dab-93b5-5eaca5c9622e"
SampleChainsDynamicHMC = "6d9fd711-e8b2-4778-9c70-c1dfb499d4c4"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["DimensionalData", "MCMCChains", "OrderedCollections", "Random", "SampleChains", "SampleChainsDynamicHMC", "Test"]
test = ["Test"]
15 changes: 15 additions & 0 deletions test/ext/ArviZMCMCChainsExt/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[deps]
ArviZ = "131c737c-5715-5e2e-ad31-c244f01c1dc7"
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
ArviZ = "0.10.3"
DimensionalData = "0.24, 0.25"
MCMCChains = "6"
OrderedCollections = "1"
Random = "1.6"
Test = "1.6"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using ArviZ
using DimensionalData
using MCMCChains: MCMCChains
using OrderedCollections
using Random

function makechains(
names, ndraws, nchains, domains=[Float64 for _ in names]; seed=42, internal_names=[]
Expand Down
15 changes: 15 additions & 0 deletions test/ext/ArviZSampleChainsDynamicHMCExt/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[deps]
ArviZ = "131c737c-5715-5e2e-ad31-c244f01c1dc7"
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SampleChains = "754583d1-7fc4-4dab-93b5-5eaca5c9622e"
SampleChainsDynamicHMC = "6d9fd711-e8b2-4778-9c70-c1dfb499d4c4"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
ArviZ = "0.10.3"
DimensionalData = "0.24, 0.25"
Random = "1.6"
SampleChains = "0.5"
SampleChainsDynamicHMC = "0.3"
Test = "1.6"
38 changes: 38 additions & 0 deletions test/ext/ArviZSampleChainsDynamicHMCExt/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using ArviZ
using SampleChains: SampleChains
using SampleChains.TupleVectors: TupleVectors
using SampleChainsDynamicHMC: SampleChainsDynamicHMC
using SampleChainsDynamicHMC.TransformVariables
using Test

include("../../helpers.jl")

function samplechains_dynamichmc_sample(nchains, ndraws)
# μ ~ Normal(0, 1) |> iid(2, 3), σ ~ HalfNormal(0, 1), y[i] ~ Normal(μ[i], σ)
y = [0.74 0.15 -1.08; -0.42 1.08 -0.52]
function (nt)
μ = nt.μ
σ = nt.σ
return -(sum(abs2, μ) + σ^2 + sum((y .- μ) .^ 2) / σ^2) / 2 - length(y) * log(σ)
end
t = as((μ=as(Array, 2, 3), σ=asℝ₊))
chain = SampleChains.newchain(nchains, SampleChainsDynamicHMC.dynamichmc(), ℓ, t)
return SampleChains.sample!(chain, ndraws - 1)
end

@testset "SampleChainsDynamicHMC" begin
expected_stats_vars = (
:acceptance_rate, :n_steps, :diverging, :energy, :tree_depth, :turning
)

multichain = samplechains_dynamichmc_sample(4, 10)
idata = convert_to_inference_data(multichain)
@test InferenceObjects.groupnames(idata) === (:posterior, :sample_stats)
@test issubset(expected_stats_vars, keys(idata.sample_stats))
@test size(idata.posterior.μ) == (10, 4, 2, 3)
@test size(idata.posterior.σ) == (10, 4)

idata = convert_to_inference_data(multichain; group=:prior)
@test InferenceObjects.groupnames(idata) === (:prior, :sample_stats_prior)
@test issubset(expected_stats_vars, keys(idata.sample_stats_prior))
end
13 changes: 13 additions & 0 deletions test/ext/ArviZSampleChainsExt/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[deps]
ArviZ = "131c737c-5715-5e2e-ad31-c244f01c1dc7"
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SampleChains = "754583d1-7fc4-4dab-93b5-5eaca5c9622e"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
ArviZ = "0.10.3"
DimensionalData = "0.24, 0.25"
Random = "1.6"
SampleChains = "0.5"
Test = "1.6"
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using ArviZ
using DimensionalData
using SampleChains: SampleChains
using SampleChains.TupleVectors: TupleVectors
using SampleChainsDynamicHMC: SampleChainsDynamicHMC
using SampleChainsDynamicHMC.TransformVariables
using Test

include("../../helpers.jl")

# minimal AbstractChain implementation
struct TestChain{T} <: SampleChains.AbstractChain{T}
samples::TupleVectors.TupleVector{T}
Expand All @@ -14,19 +13,6 @@ end
SampleChains.samples(chain::TestChain) = getfield(chain, :samples)
SampleChains.info(chain::TestChain) = getfield(chain, :info)

function samplechains_dynamichmc_sample(nchains, ndraws)
# μ ~ Normal(0, 1) |> iid(2, 3), σ ~ HalfNormal(0, 1), y[i] ~ Normal(μ[i], σ)
y = [0.74 0.15 -1.08; -0.42 1.08 -0.52]
function (nt)
μ = nt.μ
σ = nt.σ
return -(sum(abs2, μ) + σ^2 + sum((y .- μ) .^ 2) / σ^2) / 2 - length(y) * log(σ)
end
t = as((μ=as(Array, 2, 3), σ=asℝ₊))
chain = SampleChains.newchain(nchains, SampleChainsDynamicHMC.dynamichmc(), ℓ, t)
return SampleChains.sample!(chain, ndraws - 1)
end

@testset "SampleChains" begin
@testset "TestChain with $nchains chains" for nchains in (1, 4)
ndraws = 10
Expand Down Expand Up @@ -70,20 +56,4 @@ end
end
end
end
@testset "SampleChainsDynamicHMC" begin
expected_stats_vars = (
:acceptance_rate, :n_steps, :diverging, :energy, :tree_depth, :turning
)

multichain = samplechains_dynamichmc_sample(4, 10)
idata = convert_to_inference_data(multichain)
@test InferenceObjects.groupnames(idata) === (:posterior, :sample_stats)
@test issubset(expected_stats_vars, keys(idata.sample_stats))
@test size(idata.posterior.μ) == (10, 4, 2, 3)
@test size(idata.posterior.σ) == (10, 4)

idata = convert_to_inference_data(multichain; group=:prior)
@test InferenceObjects.groupnames(idata) === (:prior, :sample_stats_prior)
@test issubset(expected_stats_vars, keys(idata.sample_stats_prior))
end
end
3 changes: 2 additions & 1 deletion test/helpers.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Random
using ArviZ
using DimensionalData
using Random

function random_dim_array(var_name, dims, coords, default_dims=())
_dims = (default_dims..., dims...)
Expand Down
3 changes: 0 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ using ArviZ
using Test

@testset "ArviZ" begin
include("helpers.jl")
include("test_utils.jl")
include("test_samplechains.jl")
include("test_mcmcchains.jl")
end

0 comments on commit f479d37

Please sign in to comment.