Skip to content

Commit

Permalink
Add more tests (#22)
Browse files Browse the repository at this point in the history
* Add more tests

* Small fixes
  • Loading branch information
dannys4 authored May 14, 2024
1 parent cc654c9 commit 6bf86c4
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/MParT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,12 @@ All possible keyword arguments are in example, with some important arguments des
# Example
```jldoctest

Check failure on line 136 in src/MParT.jl

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in ~/work/MParT.jl/MParT.jl/src/MParT.jl:136-153 ```jldoctest 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 quadAbsTol = 1e-06 quadRelTol = 1e-06 quadMaxSub = 30 quadMinSub = 0 quadPts = 5 contDeriv = true nugget = 0 ``` Subexpression: MapOptions(basisType="HermiteFunctions", basisLB=-3., basisUB=3., sigmoidBasisSumType="Constant") Evaluated output: ERROR: UndefVarError: `__Constant` not defined Stacktrace: [1] top-level scope @ :0 [2] eval @ ./boot.jl:370 [inlined] [3] eval @ ~/work/MParT.jl/MParT.jl/src/MParT.jl:2 [inlined] [4] MapOptions(; kwargs::Base.Pairs{Symbol, Any, NTuple{4, Symbol}, NamedTuple{(:basisType, :basisLB, :basisUB, :sigmoidBasisSumType), Tuple{String, Float64, Float64, String}}}) @ MParT ~/work/MParT.jl/MParT.jl/src/MParT.jl:163 [5] top-level scope @ none:1 Expected output: basisType = HermiteFunctions basisLB = -3 basisUB = 3 edgeType = SoftPlus sigmoidBasisSumType = Constant basisNorm = true posFuncType = SoftPlus quadType = AdaptiveSimpson quadAbsTol = 1e-06 quadRelTol = 1e-06 quadMaxSub = 30 quadMinSub = 0 quadPts = 5 contDeriv = true nugget = 0 diff = Warning: Diff output requires color. basisType = HermiteFunctions basisLB = -3 basisUB = 3 edgeType = SoftPlus sigmoidBasisSumType = Constant basisNorm = true posFuncType = SoftPlus quadType = AdaptiveSimpson quadAbsTol = 1e-06 quadRelTol = 1e-06 quadMaxSub = 30 quadMinSub = 0 quadPts = 5 contDeriv = true nugget = 0ERROR: UndefVarError: `__Constant` not defined Stacktrace: [1] top-level scope @ :0 [2] eval @ ./boot.jl:370 [inlined] [3] eval @ ~/work/MParT.jl/MParT.jl/src/MParT.jl:2 [inlined] [4] MapOptions(; kwargs::Base.Pairs{Symbol, Any, NTuple{4, Symbol}, NamedTuple{(:basisType, :basisLB, :basisUB, :sigmoidBasisSumType), Tuple{String, Float64, Float64, String}}}) @ MParT ~/work/MParT.jl/MParT.jl/src/MParT.jl:163 [5] top-level scope @ none:1
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
Expand Down Expand Up @@ -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
Expand Down
113 changes: 113 additions & 0 deletions test/mapFactory.jl
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6bf86c4

Please sign in to comment.