Skip to content

Commit 52de582

Browse files
committed
reorganize tests to clean-up outputs
1 parent 215685a commit 52de582

File tree

10 files changed

+772
-717
lines changed

10 files changed

+772
-717
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ julia = "1.6"
4040

4141
[extras]
4242
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
43+
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
4344
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4445

4546
[targets]
46-
test = ["DataFrames", "Test"]
47+
test = ["DataFrames", "Suppressor", "Test"]

test/estimators.jl

Lines changed: 114 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -19,145 +19,147 @@ using RobustModels:
1919
emp_norm(l::LossFunction) = 2 * quadgk(x -> exp(-RobustModels.rho(l, x)), 0, Inf)[1]
2020

2121

22+
@testset "Losses and estimators" begin
2223

23-
@testset "Methods loss functions: $(name)" for name in losses
24-
typeloss = getproperty(RobustModels, Symbol(name * "Loss"))
25-
l = typeloss()
26-
27-
@testset "Methods estimators: $(estimator)" for estimator in (
28-
nothing, "M", "S", "MM", "Tau", "GeneralizedQuantile"
29-
)
30-
# Check LossFunction methods
31-
if isnothing(estimator)
32-
estimator_name = "Loss function"
33-
typest = typeloss
34-
35-
# Check AbstractEstimator methods
36-
else
37-
estimator_name = "$(estimator) Estimator"
38-
T = getproperty(RobustModels, Symbol(estimator * "Estimator"))
39-
if estimator in ("S", "MM", "Tau")
40-
if !in(name, bounded_losses)
41-
@test_throws TypeError T{typeloss}
42-
continue
43-
end
44-
end
45-
typest = T{typeloss}
46-
end
47-
est = typest()
48-
@test_nowarn println(est)
49-
50-
if !isnothing(estimator)
51-
if estimator == "Tau"
52-
# @test isa(loss(est), Tuple{BoundedLossFunction, BoundedLossFunction})
53-
@test isa(loss(est), CompositeLossFunction)
54-
@test typeof(first(loss(est))) == typeloss
55-
@test typeof(last(loss(est))) == typeloss
56-
else
57-
@test typeof(loss(est)) == typeloss
58-
end
59-
end
24+
@testset "loss functions: $(name)" for name in losses
25+
typeloss = getproperty(RobustModels, Symbol(name * "Loss"))
26+
l = typeloss()
6027

61-
@testset "Bounded $(estimator_name): $(name)" begin
62-
if name in bounded_losses
63-
@test isbounded(est)
64-
else
65-
@test !isbounded(est)
66-
end
67-
end
28+
@testset "Methods estimators: $(estimator)" for estimator in (
29+
nothing, "M", "S", "MM", "Tau", "GeneralizedQuantile"
30+
)
31+
# Check LossFunction methods
32+
if isnothing(estimator)
33+
estimator_name = "Loss function"
34+
typest = typeloss
6835

69-
@testset "Convex $(estimator_name): $(name)" begin
70-
if name in convex_losses
71-
@test isconvex(est)
36+
# Check AbstractEstimator methods
7237
else
73-
@test !isconvex(est)
38+
estimator_name = "$(estimator) Estimator"
39+
T = getproperty(RobustModels, Symbol(estimator * "Estimator"))
40+
if estimator in ("S", "MM", "Tau")
41+
if !in(name, bounded_losses)
42+
@test_throws TypeError T{typeloss}
43+
continue
44+
end
45+
end
46+
typest = T{typeloss}
47+
end
48+
est = typest()
49+
@test_nowarn show(devnull, est)
50+
51+
if !isnothing(estimator)
52+
if estimator == "Tau"
53+
# @test isa(loss(est), Tuple{BoundedLossFunction, BoundedLossFunction})
54+
@test isa(loss(est), CompositeLossFunction)
55+
@test typeof(first(loss(est))) == typeloss
56+
@test typeof(last(loss(est))) == typeloss
57+
else
58+
@test typeof(loss(est)) == typeloss
59+
end
7460
end
75-
end
76-
77-
@testset "$(estimator_name) values: $(name)" begin
78-
ρ = rho(est, 1)
79-
ψ = psi(est, 1)
80-
ψp = psider(est, 1)
81-
w = weight(est, 1)
82-
83-
vals = estimator_values(est, 1)
84-
@test length(vals) == 3
85-
@test vals[1] ρ rtol = 1e-6
86-
@test vals[2] ψ rtol = 1e-6
87-
@test vals[3] w rtol = 1e-6
88-
end
8961

90-
# Only for LossFunction
91-
if isnothing(estimator)
92-
@testset "Loss methods: $(name)" begin
93-
@test loss(est) == est
62+
@testset "Bounded $(estimator_name): $(name)" begin
63+
if name in bounded_losses
64+
@test isbounded(est)
65+
else
66+
@test !isbounded(est)
67+
end
68+
end
9469

95-
# estimator_bound
96-
if !isconvex(est)
97-
@test isfinite(RobustModels.estimator_bound(est))
70+
@testset "Convex $(estimator_name): $(name)" begin
71+
if name in convex_losses
72+
@test isconvex(est)
9873
else
99-
@test !isfinite(RobustModels.estimator_bound(est))
74+
@test !isconvex(est)
10075
end
76+
end
77+
78+
@testset "$(estimator_name) values: $(name)" begin
79+
ρ = rho(est, 1)
80+
ψ = psi(est, 1)
81+
ψp = psider(est, 1)
82+
w = weight(est, 1)
83+
84+
vals = estimator_values(est, 1)
85+
@test length(vals) == 3
86+
@test vals[1] ρ rtol = 1e-6
87+
@test vals[2] ψ rtol = 1e-6
88+
@test vals[3] w rtol = 1e-6
89+
end
10190

102-
# tuning_constant
103-
@test isfinite(RobustModels.tuning_constant(est))
91+
# Only for LossFunction
92+
if isnothing(estimator)
93+
@testset "Loss methods: $(name)" begin
94+
@test loss(est) == est
10495

105-
@testset "Estimator norm: $(name)" begin
106-
if !isbounded(est)
107-
@test emp_norm(est) RobustModels.estimator_norm(est) rtol = 1e-5
96+
# estimator_bound
97+
if !isconvex(est)
98+
@test isfinite(RobustModels.estimator_bound(est))
10899
else
109-
@test !isfinite(RobustModels.estimator_norm(est))
100+
@test !isfinite(RobustModels.estimator_bound(est))
110101
end
111-
end
112102

113-
if !in(name, ("L2", "L1"))
114-
@testset "Estimator high efficiency: $(name)" begin
115-
vopt = estimator_high_efficiency_constant(typest)
116-
if name != "HardThreshold"
117-
v = efficiency_tuning_constant(typest; eff=0.95, c0=0.9 * vopt)
118-
@test isapprox(v, vopt; rtol=1e-3)
103+
# tuning_constant
104+
@test isfinite(RobustModels.tuning_constant(est))
105+
106+
@testset "Estimator norm: $(name)" begin
107+
if !isbounded(est)
108+
@test emp_norm(est) RobustModels.estimator_norm(est) rtol = 1e-5
109+
else
110+
@test !isfinite(RobustModels.estimator_norm(est))
119111
end
120112
end
121-
end
122113

123-
if isbounded(est)
124-
@testset "Estimator high breakdown point: $(name)" begin
125-
vopt = estimator_high_breakdown_point_constant(typest)
126-
v = breakdown_point_tuning_constant(typest; bp=0.5, c0=1.1 * vopt)
127-
@test isapprox(v, vopt; rtol=1e-3)
114+
if !in(name, ("L2", "L1"))
115+
@testset "Estimator high efficiency: $(name)" begin
116+
vopt = estimator_high_efficiency_constant(typest)
117+
if name != "HardThreshold"
118+
v = efficiency_tuning_constant(typest; eff=0.95, c0=0.9 * vopt)
119+
@test isapprox(v, vopt; rtol=1e-3)
120+
end
121+
end
128122
end
129123

130-
@testset "τ-Estimator high efficiency: $(name)" begin
131-
vopt = estimator_tau_efficient_constant(typest)
132-
if name != "HardThreshold"
133-
v = tau_efficiency_tuning_constant(typest; eff=0.95, c0=1.1 * vopt)
124+
if isbounded(est)
125+
@testset "Estimator high breakdown point: $(name)" begin
126+
vopt = estimator_high_breakdown_point_constant(typest)
127+
v = breakdown_point_tuning_constant(typest; bp=0.5, c0=1.1 * vopt)
134128
@test isapprox(v, vopt; rtol=1e-3)
135129
end
130+
131+
@testset "τ-Estimator high efficiency: $(name)" begin
132+
vopt = estimator_tau_efficient_constant(typest)
133+
if name != "HardThreshold"
134+
v = tau_efficiency_tuning_constant(typest; eff=0.95, c0=1.1 * vopt)
135+
@test isapprox(v, vopt; rtol=1e-3)
136+
end
137+
end
136138
end
137139
end
138140
end
139-
end
140141

141-
if typest <: AbstractQuantileEstimator
142-
@testset "MQuantile: $(name)" begin
143-
τ = 0.5
144-
qest1 = GeneralizedQuantileEstimator(l, τ)
145-
qest2 = GeneralizedQuantileEstimator{typeloss}(τ)
146-
@test qest1 == qest2
147-
if name == "L2"
148-
qest4 = ExpectileEstimator(τ)
149-
@test qest1 == qest4
150-
elseif name == "L1"
151-
qest4 = RobustModels.QuantileEstimator(τ)
152-
@test qest1 == qest4
153-
end
142+
if typest <: AbstractQuantileEstimator
143+
@testset "MQuantile: $(name)" begin
144+
τ = 0.5
145+
qest1 = GeneralizedQuantileEstimator(l, τ)
146+
qest2 = GeneralizedQuantileEstimator{typeloss}(τ)
147+
@test qest1 == qest2
148+
if name == "L2"
149+
qest4 = ExpectileEstimator(τ)
150+
@test qest1 == qest4
151+
elseif name == "L1"
152+
qest4 = RobustModels.QuantileEstimator(τ)
153+
@test qest1 == qest4
154+
end
154155

155-
@testset "Pass through of methods" for fun in (rho, psi, psider, weight)
156-
ρ1 = fun(est, 1)
157-
ρ2 = fun(qest1, 1)
158-
@test ρ1 == ρ2
159-
end
156+
@testset "Pass through of methods" for fun in (rho, psi, psider, weight)
157+
ρ1 = fun(est, 1)
158+
ρ2 = fun(qest1, 1)
159+
@test ρ1 == ρ2
160+
end
160161

162+
end
161163
end
162164
end
163165
end

test/interface.jl

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
using Tables
33
using Missings
4+
using Suppressor: @capture_err
45

56
m1 = fit(LinearModel, form, data)
67
λlm = dispersion(m1)
@@ -11,15 +12,15 @@ est1 = MEstimator(loss1)
1112
est2 = MEstimator(loss2)
1213

1314

14-
@testset "linear: L2 estimator" begin
15+
@testset "Interface" begin
1516
VERBOSE && println("\n\t\u25CF Estimator: L2")
1617

1718
# OLS
1819
VERBOSE && println(m1)
1920
VERBOSE && println(" lm : ", coef(m1))
2021

2122
# Formula, dense and sparse entry and methods :cg and :chol
22-
@testset "$(typeof(A)),\t$(method)" for (A, b) in data_tuples, method in nopen_methods
23+
@testset "interface: $(typeof(A)),\t$(method)" for (A, b) in data_tuples, method in nopen_methods
2324

2425
name = if (A == form)
2526
"formula"
@@ -152,7 +153,13 @@ est2 = MEstimator(loss2)
152153
else
153154
b_mod = nothing
154155
end
155-
@test_throws ArgumentError fit(RobustLinearModel, A, b_mod, est1)
156+
output = @capture_err begin
157+
@test_throws ArgumentError fit(RobustLinearModel, A, b_mod, est1)
158+
end
159+
if VERBOSE && length(output) > 0
160+
println(output)
161+
end
162+
156163
if typemod == "missing"
157164
@test_nowarn fit(RobustLinearModel, A, b_mod, est1; dropmissing=true)
158165
end
@@ -170,11 +177,17 @@ est2 = MEstimator(loss2)
170177
RobustLinearModel, A_mod, b_mod, est1; dropmissing=true
171178
)
172179
else
173-
@test_throws MethodError fit(RobustLinearModel, A_mod, b, est1)
174-
@test_throws MethodError fit(RobustLinearModel, A, b_mod, est1)
175-
@test_throws MethodError fit(RobustLinearModel, A_mod, b_mod, est1)
180+
output = @capture_err begin
181+
@test_throws MethodError fit(RobustLinearModel, A_mod, b, est1)
182+
@test_throws MethodError fit(RobustLinearModel, A, b_mod, est1)
183+
@test_throws MethodError fit(RobustLinearModel, A_mod, b_mod, est1)
184+
end
185+
if VERBOSE && length(output) > 0
186+
println(output)
187+
end
176188
end
177189
end
178190
end
179191
end
192+
180193
end

0 commit comments

Comments
 (0)