Skip to content

Commit 882f2f7

Browse files
authored
Improve code coverage (#115)
* Improve code coverage * Fix
1 parent 284189f commit 882f2f7

File tree

6 files changed

+34
-8
lines changed

6 files changed

+34
-8
lines changed

examples/basics.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ hcat(initialization(hmm_est_concat), initialization(hmm))
257257

258258
# ## Tests #src
259259

260+
@test startswith(string(hmm), "Hidden") #src
261+
@test length.(values(rand(hmm, T))) == (T, T); #src
260262
control_seq = fill(nothing, last(seq_ends)); #src
261263
test_coherent_algorithms(rng, hmm, control_seq; seq_ends, hmm_guess) #src
262264
test_type_stability(rng, hmm, control_seq; seq_ends, hmm_guess) #src

ext/HiddenMarkovModelsDistributionsExt.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ function HiddenMarkovModels.fit_in_sequence!(
2727
return dists[i] = fit(typeof(dists[i]), reduce(hcat, x_vecs), w)
2828
end
2929

30+
#=
31+
32+
# Matrix distribution fitting not supported by Distributions.jl at the moment
33+
3034
function HiddenMarkovModels.fit_in_sequence!(
3135
dists::AbstractVector{<:MatrixDistribution},
3236
i::Integer,
@@ -37,5 +41,6 @@ function HiddenMarkovModels.fit_in_sequence!(
3741
end
3842
3943
dcat(M1, M2) = cat(M1, M2; dims=3)
44+
=#
4045

4146
end

src/inference/forward.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ struct ForwardBackwardStorage{R,M<:AbstractMatrix{R}}
3939
::Matrix{R}
4040
end
4141

42-
Base.eltype(::ForwardStorage{R}) where {R} = R
4342
Base.eltype(::ForwardBackwardStorage{R}) where {R} = R
4443

4544
const ForwardOrForwardBackwardStorage{R} = Union{

src/inference/viterbi.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ struct ViterbiStorage{R}
1717
ψ::Matrix{Int}
1818
end
1919

20-
Base.eltype(::ViterbiStorage{R}) where {R} = R
21-
2220
"""
2321
$(SIGNATURES)
2422
"""

src/types/hmm.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ struct HMM{
3838
end
3939
end
4040

41-
function Base.copy(hmm::HMM)
42-
return HMM(copy(hmm.init), copy(hmm.trans), copy(hmm.dists))
43-
end
44-
4541
function Base.show(io::IO, hmm::HMM)
4642
return print(
4743
io,

test/distributions.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Distributions
2-
using HiddenMarkovModels: LightCategorical, LightDiagNormal, logdensityof, rand_prob_vec
2+
using HiddenMarkovModels:
3+
LightCategorical, LightDiagNormal, logdensityof, rand_prob_vec, rand_trans_mat
34
using LinearAlgebra
45
using Statistics
56
using StatsAPI: fit!
@@ -8,6 +9,29 @@ using Test
89

910
rng = StableRNG(63)
1011

12+
function test_randprobvec(p)
13+
@test all(>=(0), p)
14+
@test sum(p) 1
15+
end
16+
17+
function test_randtransmat(A)
18+
foreach(eachrow(A)) do p
19+
test_randprobvec(p)
20+
end
21+
end
22+
23+
@testset "Rand prob" begin
24+
n = 10
25+
test_randprobvec(rand_prob_vec(n))
26+
test_randprobvec(rand_prob_vec(rng, n))
27+
test_randprobvec(rand_prob_vec(Float32, n))
28+
test_randprobvec(rand_prob_vec(rng, Float32, n))
29+
test_randtransmat(rand_trans_mat(n))
30+
test_randtransmat(rand_trans_mat(rng, n))
31+
test_randtransmat(rand_trans_mat(Float32, n))
32+
test_randtransmat(rand_trans_mat(rng, Float32, n))
33+
end
34+
1135
function test_fit_allocs(dist, x, w)
1236
dist_copy = deepcopy(dist)
1337
allocs = @allocated fit!(dist_copy, x, w)
@@ -17,6 +41,7 @@ end
1741
@testset "LightCategorical" begin
1842
p = rand_prob_vec(rng, 10)
1943
dist = LightCategorical(p)
44+
@test startswith(string(dist), "LightCategorical")
2045
x = [(@inferred rand(rng, dist)) for _ in 1:100_000]
2146
# Simulation
2247
val_count = zeros(Int, length(p))
@@ -38,6 +63,7 @@ end
3863
μ = randn(rng, 10)
3964
σ = rand(rng, 10)
4065
dist = LightDiagNormal(μ, σ)
66+
@test startswith(string(dist), "LightDiagNormal")
4167
x = [(@inferred rand(rng, dist)) for _ in 1:100_000]
4268
# Simulation
4369
@test mean(x) μ atol = 2e-2

0 commit comments

Comments
 (0)