From 6bf86c4bab5621928dc5921271b2eae3342a8ac2 Mon Sep 17 00:00:00 2001 From: Daniel <43151183+dannys4@users.noreply.github.com> Date: Tue, 14 May 2024 15:38:12 -0400 Subject: [PATCH] Add more tests (#22) * Add more tests * Small fixes --- src/MParT.jl | 6 ++- test/mapFactory.jl | 113 +++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 3 ++ 3 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 test/mapFactory.jl diff --git a/src/MParT.jl b/src/MParT.jl index 6a20ae5..6708e4e 100644 --- a/src/MParT.jl +++ b/src/MParT.jl @@ -134,10 +134,12 @@ All possible keyword arguments are in example, with some important arguments des # Example ```jldoctest -julia> MapOptions(basisType="HermiteFunctions", basisLB=-3., basisUB=3.) +julia> MapOptions(basisType="HermiteFunctions", basisLB=-3., basisUB=3., sigmoidBasisSumType="Constant") basisType = HermiteFunctions basisLB = -3 basisUB = 3 +edgeType = SoftPlus +sigmoidBasisSumType = Constant basisNorm = true posFuncType = SoftPlus quadType = AdaptiveSimpson @@ -411,7 +413,7 @@ export AffineMap, AffineFunction # ComposedMap-related exports export ComposedMap # MapFactory-related exports -export CreateComponent, CreateTriangular +export CreateComponent, CreateTriangular, CreateSigmoidComponent, CreateSigmoidTriangular # MapOptions-related exports export MapOptions # Serialization-related exports diff --git a/test/mapFactory.jl b/test/mapFactory.jl new file mode 100644 index 0000000..ec0c564 --- /dev/null +++ b/test/mapFactory.jl @@ -0,0 +1,113 @@ +mset = CreateTotalOrder(1,3) +opts = MapOptions() + + +function test_CreateComponent_basisType() + for bType in ["HermiteFunctions", "PhysicistHermite", "ProbabilistHermite"] + opts = MapOptions(basisType = bType) + component = CreateComponent(Fix(mset), opts) + @test numCoeffs(component) == 4 + @test all(CoeffMap(component) .== 0.) + end +end + +function test_CreateComponent_posFuncType() + for pfType in ["Exp", "SoftPlus"] + opts = MapOptions(posFuncType = pfType) + component = CreateComponent(Fix(mset), opts) + @test numCoeffs(component) == 4 + @test all(CoeffMap(component) .== 0.) + end +end + +function test_CreateComponent_quadTypes() + for qType in ["AdaptiveSimpson", "ClenshawCurtis", "AdaptiveClenshawCurtis"] + # AdaptiveSimpson + opts = MapOptions(quadType = qType) + component = CreateComponent(Fix(mset), opts) + @test numCoeffs(component) == 4 + @test all(CoeffMap(component) .== 0.) + end +end + +function test_CreateTriangular() + triangular = CreateTriangular(2,2,2,opts) + @test numCoeffs(triangular) == 2 + 7 +end + +function test_CreateSingleEntryMap_1() + dim = 7 + activeInd = 1 + + mset_csemap = CreateTotalOrder(activeInd, 3) + component = CreateComponent(Fix(mset_csemap), opts) + + single_entry_map = CreateSingleEntryMap(dim, activeInd, component) + @test numCoeffs(single_entry_map) == numCoeffs(component) +end + +function test_CreateSingleEntryMap_2() + dim = 7 + activeInd = 7 + + mset_csemap = CreateTotalOrder(activeInd, 3) + component = CreateComponent(Fix(mset_csemap), opts) + + single_entry_map = CreateSingleEntryMap(dim, activeInd, component) + @test numCoeffs(single_entry_map) == numCoeffs(component) +end + +function test_CreateSingleEntryMap_3() + dim = 7 + activeInd = 4 + + mset_csemap = CreateTotalOrder(activeInd, 3) + component = CreateComponent(Fix(mset_csemap), opts) + + single_entry_map = CreateSingleEntryMap(dim, activeInd, component) + @test numCoeffs(single_entry_map) == numCoeffs(component) +end + +function test_CreateSigmoidMaps() + input_dim = 6 + num_sigmoid = 4 + sigmoid_terms = num_sigmoid+3 + centers_len = 2+(num_sigmoid*(num_sigmoid+1)) รท 2 + max_degree = 3 + centers = zeros(centers_len) + center_idx = 1 + bound = 3. + # Edge terms + centers[1] = -bound + centers[2] = bound + centers[3] = 0. + # Sigmoid terms + for order in 4:num_sigmoid + for j in 0:order-1 + centers[center_idx] = 1.9*bound*(j-(order-1)/2)/(order-1) + center_idx += 1 + end + end + opts = MapOptions(basisType="HermiteFunctions") + sig = CreateSigmoidComponent(input_dim, max_degree, centers, opts) + expected_num_coeffs = binomial(input_dim-1+max_degree, input_dim-1)*(sigmoid_terms+1) + @test numCoeffs(sig) == expected_num_coeffs + mset = FixedMultiIndexSet(input_dim, max_degree) + sig_mset = CreateSigmoidComponent(mset, centers, opts) + @test numCoeffs(sig_mset) == size(mset) + output_dim = input_dim + centers_total = reduce(hcat, centers for _ in 1:output_dim) + sig_trimap = CreateSigmoidTriangular(input_dim, output_dim, max_degree, centers_total, opts) + expected_num_coeffs_dim = d->(sigmoid_terms+1)*binomial(d-1+max_degree, d-1) + expected_num_coeffs = sum(expected_num_coeffs_dim.(1:output_dim)) + @test numCoeffs(sig_trimap) == expected_num_coeffs +end + +test_CreateComponent_basisType() +test_CreateComponent_posFuncType() +test_CreateComponent_quadTypes() +test_CreateTriangular() +# test_CreateSingleEntryMap_1() +# test_CreateSingleEntryMap_2() +# test_CreateSingleEntryMap_3() +test_CreateSigmoidMaps() \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index f728acd..0877c33 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,6 +7,9 @@ using Test, MParT @testset "MultiIndex" begin include("multiindex.jl") end + @testset "MapFactory" begin + include("mapFactory.jl") + end @testset "Monotone Least Squares" begin include("MLS.jl") end