Skip to content

Commit 892396a

Browse files
committed
Move unsort/unorder to tests
1 parent e08d360 commit 892396a

File tree

7 files changed

+54
-50
lines changed

7 files changed

+54
-50
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ version = "0.5.0"
77

88
[deps]
99
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
10-
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1110
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
1211
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1312

@@ -18,7 +17,8 @@ StatsBase = "0.24.0 - 0.33"
1817
julia = "1.4"
1918

2019
[extras]
20+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2121
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2222

2323
[targets]
24-
test = ["Test"]
24+
test = ["Test", "Random"]

src/MultipleTesting.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import Distributions: estimate
1616

1717
import SpecialFunctions: digamma
1818

19-
import Random: shuffle!
20-
2119
export PValues,
2220
adjust,
2321
PValueAdjustment,

src/utils.jl

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
11
## utility functions ##
22

3-
function unsort(x; kws...)
4-
y = copy(x)
5-
while issorted(y; kws...)
6-
shuffle!(y)
7-
end
8-
return y
9-
end
10-
11-
function unorder(x; kws...)
12-
ord = collect(1:length(x))
13-
while issorted(x[ord]; kws...)
14-
shuffle!(ord)
15-
end
16-
return ord
17-
end
18-
19-
203
function valid_pvalues(x::AbstractVector{T}) where T <: AbstractFloat
214
if !isin(x)
225
throw(DomainError("p-values must all be in [0, 1]"))

test/test-pi0-estimators.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ using MultipleTesting
55
using Test
66
using StatsBase
77

8+
include("utils.jl")
9+
810

911
@testset "π₀ estimators" begin
1012

@@ -16,8 +18,6 @@ using StatsBase
1618

1719
lambdas = collect(0.05:0.05:0.95)
1820

19-
unsort = MultipleTesting.unsort
20-
2121

2222
@testset "Storey π₀" begin
2323

test/test-pval-adjustment.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Test_pval_adjustment
44
using MultipleTesting
55
using Test
66

7+
include("utils.jl")
8+
79

810
@testset "p-Value adjustment" begin
911

@@ -75,7 +77,7 @@ using Test
7577

7678
# unsorted inputs
7779
for i in 1:10
78-
ord = MultipleTesting.unorder(pval1)
80+
ord = unorder(pval1)
7981
@test isapprox(adjust(pval1[ord], method()), ref1[method][ord], atol = 1e-9)
8082
end
8183

@@ -88,15 +90,15 @@ using Test
8890
# this test is not valid for ForwardStop
8991
if method != ForwardStop
9092
for i in 1:10
91-
ord = MultipleTesting.unorder(ref2[method])
93+
ord = unorder(ref2[method])
9294
@test isapprox(adjust(pval2[ord], method()), ref2[method][ord], atol = 1e-9) # FIXME
9395
end
9496
end
9597

9698
## sorting order does not play a role
9799
for i in 1:10
98100
pval4 = sort(rand(10)) # all under H0
99-
ord = MultipleTesting.unorder(pval4)
101+
ord = unorder(pval4)
100102
@test adjust(pval4[ord], method()) == adjust(pval4, method())[ord]
101103
end
102104

@@ -112,7 +114,7 @@ using Test
112114

113115
# unsorted inputs
114116
for i in 1:10
115-
ord = MultipleTesting.unorder(pval3)
117+
ord = unorder(pval3)
116118
@test adjust(pval3[ord], n3, method()) == (adjust(pval3pad, n3, method())[1:k3])[ord]
117119
end
118120

test/test-utils.jl

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Test_utils
44
using MultipleTesting
55
using Test
66

7+
include("utils.jl")
8+
79

810
@testset "Utility functions" begin
911

@@ -57,6 +59,29 @@ using Test
5759
end
5860

5961

62+
@testset "harmonic_number" begin
63+
64+
# Exact computation as reference
65+
harm_n_exact(n::Integer) = sum([Rational(1, i) for i in 1:BigInt(n)])
66+
67+
n = [1:100; 200:200:1000; 10000]
68+
69+
max_d = 0.0
70+
for i in n
71+
hn1 = MultipleTesting.harmonic_number(i)
72+
hn2 = harm_n_exact(i)
73+
max_d = max(abs(hn1 - hn2), max_d)
74+
end
75+
# approximation error in the range of floating point inaccuracy
76+
@test max_d < (10 * eps())
77+
78+
end
79+
80+
end
81+
82+
83+
@testset "Helper functions for testing" begin
84+
6085
@testset "unsort" begin
6186

6287
n = 20
@@ -65,8 +90,6 @@ using Test
6590
xu = xs[[1:2:n - 1; 2:2:n]] # unsorted
6691
@test !issorted(xu)
6792

68-
unsort = MultipleTesting.unsort
69-
7093
@test issorted(xs)
7194
@test !issorted(unsort(xs))
7295

@@ -90,8 +113,6 @@ using Test
90113
xu = xs[[1:2:n - 1; 2:2:n]] # unsorted
91114
@test !issorted(xu)
92115

93-
unorder = MultipleTesting.unorder
94-
95116
@test issorted(xs)
96117
ord = unorder(xs)
97118
@test !issorted(ord)
@@ -110,25 +131,6 @@ using Test
110131

111132
end
112133

113-
114-
@testset "harmonic_number" begin
115-
116-
# Exact computation as reference
117-
harm_n_exact(n::Integer) = sum([Rational(1, i) for i in 1:BigInt(n)])
118-
119-
n = [1:100; 200:200:1000; 10000]
120-
121-
max_d = 0.0
122-
for i in n
123-
hn1 = MultipleTesting.harmonic_number(i)
124-
hn2 = harm_n_exact(i)
125-
max_d = max(abs(hn1 - hn2), max_d)
126-
end
127-
# approximation error in the range of floating point inaccuracy
128-
@test max_d < (10 * eps())
129-
130-
end
131-
132134
end
133135

134136
end

test/utils.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# helper functions
2+
3+
import Random: shuffle!
4+
5+
function unsort(x; kws...)
6+
y = copy(x)
7+
while issorted(y; kws...)
8+
shuffle!(y)
9+
end
10+
return y
11+
end
12+
13+
function unorder(x; kws...)
14+
ord = collect(1:length(x))
15+
while issorted(x[ord]; kws...)
16+
shuffle!(ord)
17+
end
18+
return ord
19+
end

0 commit comments

Comments
 (0)