Skip to content

Commit 8f0a156

Browse files
committed
Use DomainErrors with informative messages
1 parent 496f606 commit 8f0a156

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

src/model.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ partitioning the empirical distribution of p-values. Bioinformatics 19,
4343
4444
"""
4545
function BetaUniformMixtureModel(π0::AbstractFloat, α::AbstractFloat = 0.5, β::AbstractFloat = 1.0)
46-
if !isin(π0, 0., 1.)
47-
throw(DomainError())
48-
end
49-
MixtureModel([Beta(α, β), Uniform()], [1-π0, π0])
46+
isin(π0, 0, 1) || throw(DomainError("π0 must be in [0, 1]"))
47+
bum = MixtureModel([Beta(α, β), Uniform()], [1-π0, π0])
48+
return bum
5049
end

src/pi0-estimators.jl

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ Series B (Statistical Methodology) 66, 187–205.
8484
struct Storey <: Pi0Estimator
8585
λ::Float64
8686

87-
Storey(λ) = isin(λ, 0, 1) ? new(λ) : throw(DomainError())
87+
function Storey(λ)
88+
isin(λ, 0, 1) || throw(DomainError("λ must be in [0, 1]"))
89+
return new(λ)
90+
end
8891
end
8992

9093
Storey() = Storey(0.1)
@@ -131,8 +134,11 @@ struct StoreyBootstrap <: Pi0Estimator
131134
λseq::Vector{Float64}
132135
q ::Float64
133136

134-
StoreyBootstrap(λseq, q) =
135-
isin(λseq, 0, 1) && isin(q, 0, 1) ? new(λseq, q) : throw(DomainError())
137+
function StoreyBootstrap(λseq, q)
138+
isin(λseq, 0, 1) || throw(DomainError("λseq must be in [0, 1]"))
139+
isin(q, 0, 1) || throw(DomainError("q must be in [0, 1]"))
140+
return new(λseq, q)
141+
end
136142
end
137143

138144
StoreyBootstrap() = StoreyBootstrap(0.05:0.05:0.95, 0.1)
@@ -229,7 +235,10 @@ julia> estimate_pi0(pvals, Oracle(0.5)) # a bit boring...
229235
struct Oracle <: Pi0Estimator
230236
π0::Float64
231237

232-
Oracle(π0) = isin(π0, 0, 1) ? new(π0) : throw(DomainError())
238+
function Oracle(π0)
239+
isin(π0, 0, 1) || throw(DomainError("π0 must be in [0, 1]"))
240+
return new(π0)
241+
end
233242
end
234243

235244
Oracle() = Oracle(1.0)
@@ -269,7 +278,10 @@ struct TwoStep <: Pi0Estimator
269278
α::Float64
270279
adjustment::PValueAdjustment
271280

272-
TwoStep(α, method) = isin(α, 0, 1) ? new(α, method) : throw(DomainError())
281+
function TwoStep(α, method)
282+
isin(α, 0, 1) || throw(DomainError("α must be in [0, 1]"))
283+
return new(α, method)
284+
end
273285
end
274286

275287
TwoStep() = TwoStep(0.05)
@@ -313,8 +325,10 @@ Statistical Society: Series B (Statistical Methodology) 74, 163–182.
313325
struct RightBoundary <: Pi0Estimator
314326
λseq::Vector{Float64}
315327

316-
RightBoundary(λseq) =
317-
isin(λseq, 0, 1) ? new(λseq) : throw(DomainError())
328+
function RightBoundary(λseq)
329+
isin(λseq, 0, 1) || throw(DomainError("λseq must be in [0, 1]"))
330+
return new(λseq)
331+
end
318332
end
319333

320334
# λseq used in Liang, Nettleton 2012
@@ -366,11 +380,11 @@ struct CensoredBUM <: Pi0Estimator
366380
maxiter::Int64
367381

368382
function CensoredBUM(γ0, λ, xtol, maxiter)
369-
if isin(γ0, 0, 1) && isin(λ, 0, 1) && isin(xtol, 0, 1) && maxiter > 0
370-
new(γ0, λ, xtol, maxiter)
371-
else
372-
throw(DomainError())
373-
end
383+
isin(γ0, 0, 1) || throw(DomainError("γ0 must be in [0, 1]"))
384+
isin(λ, 0, 1) || throw(DomainError("λ must be in [0, 1]"))
385+
isin(xtol, 0, 1) || throw(DomainError("xtol must be in [0, 1]"))
386+
maxiter > 0 || throw(DomainError("maxiter must be a positive number"))
387+
return new(γ0, λ, xtol, maxiter)
374388
end
375389
end
376390

@@ -495,11 +509,10 @@ struct BUM <: Pi0Estimator
495509
maxiter::Int64
496510

497511
function BUM(γ0, xtol, maxiter)
498-
if isin(γ0, 0, 1) && isin(xtol, 0, 1)
499-
new(γ0, xtol, maxiter)
500-
else
501-
throw(DomainError())
502-
end
512+
isin(γ0, 0, 1) || throw(DomainError("γ0 must be in [0, 1]"))
513+
isin(xtol, 0, 1) || throw(DomainError("xtol must be in [0, 1]"))
514+
maxiter > 0 || throw(DomainError("maxiter must be a positive number"))
515+
return new(γ0, xtol, maxiter)
503516
end
504517
end
505518

@@ -620,11 +633,10 @@ struct ConvexDecreasing <: Pi0Estimator
620633
maxiter::Int64
621634

622635
function ConvexDecreasing(gridsize, xtol, maxiter)
623-
if gridsize > 0 && isin(xtol, 0, 1) && maxiter > 0
624-
new(gridsize, xtol, maxiter)
625-
else
626-
throw(DomainError())
627-
end
636+
gridsize > 0 || throw(DomainError("gridsize must be a positive number"))
637+
isin(xtol, 0, 1) || throw(DomainError("xtol must be in [0, 1]"))
638+
maxiter > 0 || throw(DomainError("maxiter must be a positive number"))
639+
return new(gridsize, xtol, maxiter)
628640
end
629641
end
630642

src/types.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ struct PValues{T<:AbstractFloat} <: AbstractVector{T}
2121
function(::Type{PValues})(values::AbstractVector{T}) where T
2222
min, max = extrema(values)
2323
if min < zero(T) || max > one(T)
24-
throw(DomainError())
24+
throw(DomainError("p-values must all be in [0, 1]"))
2525
end
26-
new{T}(copy(values), min, max)
26+
return new{T}(copy(values), min, max)
2727
end
2828
end
2929

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ end
4141

4242
function valid_pvalues(x::AbstractVector{T}) where T<:AbstractFloat
4343
if !isin(x)
44-
throw(DomainError())
44+
throw(DomainError("p-values must all be in [0, 1]"))
4545
end
4646
end
4747

0 commit comments

Comments
 (0)