Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rikhuijzer committed Feb 21, 2021
1 parent 9d2128d commit 429b063
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 88 deletions.
2 changes: 2 additions & 0 deletions src/core/ad.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# const FDM = FiniteDifferences

##############################
# Global variables/constants #
##############################
Expand Down
5 changes: 0 additions & 5 deletions test/contrib/inference/dynamichmc.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using Turing, Test
import Random

include(project_root*"/test/test_utils/AllUtils.jl")

@stage_testset "dynamichmc" "dynamichmc.jl" begin
import DynamicHMC
Random.seed!(100)
Expand Down
7 changes: 0 additions & 7 deletions test/contrib/inference/sghmc.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
using Turing
using LinearAlgebra
using Random
using Test

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "sghmc.jl" begin
@numerical_testset "sghmc inference" begin
Random.seed!(125)
Expand Down
21 changes: 4 additions & 17 deletions test/core/ad.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
using ForwardDiff, Distributions, FiniteDifferences, Tracker, Random, LinearAlgebra
using PDMats, Zygote
using Turing: Turing, invlink, link, SampleFromPrior,
TrackerAD, ZygoteAD
using DynamicPPL: getval
using Turing.Core: TuringDenseMvNormal, TuringDiagMvNormal
using ForwardDiff: Dual
using StatsFuns: binomlogpdf, logsumexp
using Test, LinearAlgebra
const FDM = FiniteDifferences

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "ad.jl" begin
@turing_testset "adr" begin
ad_test_f = gdemo_default
vi = Turing.VarInfo(ad_test_f)
ad_test_f(vi, SampleFromPrior())
ad_test_f(vi, Turing.SampleFromPrior())
svn = vi.metadata.s.vns[1]
mvn = vi.metadata.m.vns[1]
_s = getval(vi, svn)[1]
Expand All @@ -26,7 +13,7 @@ include(project_root*"/test/test_utils/AllUtils.jl")
# Hand-written logp
function logp(x::Vector)
s = x[2]
# s = invlink(dist_s, s)
# s = Turing.invlink(dist_s, s)
m = x[1]
lik_dist = Normal(m, sqrt(s))
lp = logpdf(dist_s, s) + logpdf(Normal(0,sqrt(s)), m)
Expand All @@ -36,11 +23,11 @@ include(project_root*"/test/test_utils/AllUtils.jl")

# Call ForwardDiff's AD
g = x -> ForwardDiff.gradient(logp, x);
# _s = link(dist_s, _s)
# _s = Turing.link(dist_s, _s)
_x = [_m, _s]
grad_FWAD = sort(g(_x))

x = map(x->Float64(x), vi[SampleFromPrior()])
x = map(x->Float64(x), vi[Turing.SampleFromPrior()])
∇E1 = gradient_logp(TrackerAD(), x, vi, ad_test_f)[2]
@test sort(∇E1) grad_FWAD atol=1e-9

Expand Down
1 change: 0 additions & 1 deletion test/inference/AdvancedSMC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ using AdvancedPS: ResampleWithESSThreshold, resample_systematic, resample_multin

using Random

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "SMC" begin
@turing_testset "constructor" begin
Expand Down
1 change: 0 additions & 1 deletion test/inference/Inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import MCMCChains

using Random

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "io.jl" begin
# Only test threading if 1.3+.
Expand Down
1 change: 0 additions & 1 deletion test/inference/emcee.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ using Turing, Random, Test
import Turing.Inference
import AdvancedMH

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "emcee.jl" begin
@testset "gdemo" begin
Expand Down
1 change: 0 additions & 1 deletion test/inference/ess.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Turing, Random, Test

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "ESS" begin
@model demo(x) = begin
Expand Down
4 changes: 0 additions & 4 deletions test/inference/gibbs.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Random, Turing, Test
import AbstractMCMC
import MCMCChains
import Turing.Inference
using Turing.RandomMeasures

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "gibbs.jl" begin
@turing_testset "gibbs constructor" begin
Expand Down
1 change: 0 additions & 1 deletion test/inference/gibbs_conditional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ using Random, Turing, Test
using StatsFuns
using Clustering

include(project_root*"/test/test_utils/AllUtils.jl")


@turing_testset "gibbs conditionals" begin
Expand Down
9 changes: 0 additions & 9 deletions test/inference/hmc.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
using Turing, Random, Test
using Turing: Sampler, NUTS
using MCMCChains: Chains
using StatsFuns: logistic

using LinearAlgebra

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "hmc.jl" begin
@numerical_testset "constrained bounded" begin
# Set a seed
Expand Down
1 change: 0 additions & 1 deletion test/inference/is.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ using StatsFuns
using Random
using Test

include(project_root*"/test/test_utils/AllUtils.jl")

@turing_testset "is.jl" begin
function reference(n)
Expand Down
1 change: 0 additions & 1 deletion test/inference/mh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ using Turing, Random, Test
import Turing.Inference
import AdvancedMH

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "mh.jl" begin
@turing_testset "mh constructor" begin
Expand Down
1 change: 0 additions & 1 deletion test/modes/ModeEstimation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ using Random
using LinearAlgebra
using Zygote

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "ModeEstimation.jl" begin
@testset "MLE" begin
Expand Down
86 changes: 51 additions & 35 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
##########################################
# Master file for running all test cases #
##########################################
using Zygote, ReverseDiff, Memoization, Turing
using Pkg

using AbstractMCMC
using Distributions
using FiniteDifferences
using ForwardDiff
using Memoization
using Random
using ReverseDiff
using PDMats
using Tracker
using Turing
using Turing.RandomMeasures
using Zygote

# Julia base.
using LinearAlgebra
using Pkg
using Test

using DynamicPPL: getval
using ForwardDiff: Dual
using MCMCChains: Chains
using StatsFuns: binomlogpdf, logistic, logsumexp
using Turing: Sampler, SampleFromPrior, NUTS, TrackerAD, ZygoteAD
using Turing.Core: TuringDenseMvNormal, TuringDiagMvNormal

setprogress!(false)

include("test_utils/AllUtils.jl")

const project_root = dirname(dirname(pathof(Turing)))

# Begin testing.
@testset "Turing" begin
@testset "core" begin
Expand All @@ -24,39 +40,39 @@ const project_root = dirname(dirname(pathof(Turing)))
@testset "inference: $adbackend" begin
@testset "samplers" begin
include("inference/gibbs.jl")
include("inference/gibbs_conditional.jl")
include("inference/hmc.jl")
include("inference/is.jl")
include("inference/mh.jl")
include("inference/ess.jl")
include("inference/emcee.jl")
include("inference/AdvancedSMC.jl")
include("inference/Inference.jl")
include("contrib/inference/dynamichmc.jl")
include("contrib/inference/sghmc.jl")
# include("inference/gibbs_conditional.jl")
# include("inference/hmc.jl")
# include("inference/is.jl")
# include("inference/mh.jl")
# include("inference/ess.jl")
# include("inference/emcee.jl")
# include("inference/AdvancedSMC.jl")
# include("inference/Inference.jl")
# include("contrib/inference/dynamichmc.jl")
# include("contrib/inference/sghmc.jl")
end
end

@testset "variational algorithms : $adbackend" begin
include("variational/advi.jl")
end
# @testset "variational algorithms : $adbackend" begin
# include("variational/advi.jl")
# end

@testset "modes" begin
include("modes/ModeEstimation.jl")
end
end
@testset "variational optimisers" begin
include("variational/optimisers.jl")
# @testset "modes" begin
# include("modes/ModeEstimation.jl")
# end
end
# @testset "variational optimisers" begin
# include("variational/optimisers.jl")
# end

Turing.setadbackend(:forwarddiff)
@testset "stdlib" begin
include("stdlib/distributions.jl")
include("stdlib/RandomMeasures.jl")
end
# Turing.setadbackend(:forwarddiff)
# @testset "stdlib" begin
# include("stdlib/distributions.jl")
# include("stdlib/RandomMeasures.jl")
# end

@testset "utilities" begin
# include("utilities/stan-interface.jl")
include("inference/utilities.jl")
end
# @testset "utilities" begin
# # include("utilities/stan-interface.jl")
# include("inference/utilities.jl")
# end
end
1 change: 0 additions & 1 deletion test/stdlib/RandomMeasures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ using Turing.RandomMeasures
using Test
using Random

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "RandomMeasures.jl" begin
@testset "Infinite Mixture Model" begin
Expand Down
1 change: 0 additions & 1 deletion test/stdlib/distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ using Turing: BinomialLogit, NUTS
using Distributions: Binomial, logpdf
using StatsFuns: logistic

include(project_root*"/test/test_utils/AllUtils.jl")

@testset "distributions.jl" begin
@turing_testset "distributions functions" begin
Expand Down
1 change: 0 additions & 1 deletion test/utilities/stan-interface.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Turing, Random, Test

include(project_root*"/test/test_utils/AllUtils.jl")

@stage_testset "stan" "stan-interface.jl" begin
using CmdStan
Expand Down

0 comments on commit 429b063

Please sign in to comment.