Skip to content

Commit

Permalink
Improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle committed Sep 30, 2024
1 parent 284189f commit d68fcf5
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 8 deletions.
2 changes: 2 additions & 0 deletions examples/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ hcat(initialization(hmm_est_concat), initialization(hmm))

# ## Tests #src

@test startswith(string(hmm), "Hidden") #src
@test length.(values(rand(hmm, T))) == (T, T); #src
control_seq = fill(nothing, last(seq_ends)); #src
test_coherent_algorithms(rng, hmm, control_seq; seq_ends, hmm_guess) #src
test_type_stability(rng, hmm, control_seq; seq_ends, hmm_guess) #src
1 change: 1 addition & 0 deletions examples/temporal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,6 @@ map(mean, hcat(obs_distributions(hmm_est, 2), obs_distributions(hmm, 2)))
# ## Tests #src

@test mean(obs_seqs[1][1:2:end]) < 0 < mean(obs_seqs[1][2:2:end]) #src
@test length.(values(rand(hmm, control_seq))) == (10, 10); #src
test_coherent_algorithms(rng, hmm, control_seq; seq_ends, hmm_guess, init=false) #src
test_type_stability(rng, hmm, control_seq; seq_ends, hmm_guess) #src
5 changes: 5 additions & 0 deletions ext/HiddenMarkovModelsDistributionsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function HiddenMarkovModels.fit_in_sequence!(
return dists[i] = fit(typeof(dists[i]), reduce(hcat, x_vecs), w)
end

#=
# Matrix distribution fitting not supported by Distributions.jl at the moment
function HiddenMarkovModels.fit_in_sequence!(
dists::AbstractVector{<:MatrixDistribution},
i::Integer,
Expand All @@ -37,5 +41,6 @@ function HiddenMarkovModels.fit_in_sequence!(
end
dcat(M1, M2) = cat(M1, M2; dims=3)
=#

end
1 change: 0 additions & 1 deletion src/inference/forward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct ForwardBackwardStorage{R,M<:AbstractMatrix{R}}
::Matrix{R}
end

Base.eltype(::ForwardStorage{R}) where {R} = R
Base.eltype(::ForwardBackwardStorage{R}) where {R} = R

const ForwardOrForwardBackwardStorage{R} = Union{
Expand Down
2 changes: 0 additions & 2 deletions src/inference/viterbi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ struct ViterbiStorage{R}
ψ::Matrix{Int}
end

Base.eltype(::ViterbiStorage{R}) where {R} = R

"""
$(SIGNATURES)
"""
Expand Down
4 changes: 0 additions & 4 deletions src/types/hmm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ struct HMM{
end
end

function Base.copy(hmm::HMM)
return HMM(copy(hmm.init), copy(hmm.trans), copy(hmm.dists))
end

function Base.show(io::IO, hmm::HMM)
return print(
io,
Expand Down
28 changes: 27 additions & 1 deletion test/distributions.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Distributions
using HiddenMarkovModels: LightCategorical, LightDiagNormal, logdensityof, rand_prob_vec
using HiddenMarkovModels:
LightCategorical, LightDiagNormal, logdensityof, rand_prob_vec, rand_trans_mat
using LinearAlgebra
using Statistics
using StatsAPI: fit!
Expand All @@ -8,6 +9,29 @@ using Test

rng = StableRNG(63)

function test_randprobvec(p)
@test all(>=(0), p)
@test sum(p) 1
end

function test_randtransmat(A)
foreach(eachrow(A)) do p
test_randprobvec(p)
end
end

@testset "Rand prob" begin
n = 10
test_randprobvec(rand_prob_vec(n))
test_randprobvec(rand_prob_vec(rng, n))
test_randprobvec(rand_prob_vec(Float32, n))
test_randprobvec(rand_prob_vec(rng, Float32, n))
test_randtransmat(rand_trans_mat(n))
test_randtransmat(rand_trans_mat(rng, n))
test_randtransmat(rand_trans_mat(Float32, n))
test_randtransmat(rand_trans_mat(rng, Float32, n))
end

function test_fit_allocs(dist, x, w)
dist_copy = deepcopy(dist)
allocs = @allocated fit!(dist_copy, x, w)
Expand All @@ -17,6 +41,7 @@ end
@testset "LightCategorical" begin
p = rand_prob_vec(rng, 10)
dist = LightCategorical(p)
@test startswith(string(dist), "LightCategorical")
x = [(@inferred rand(rng, dist)) for _ in 1:100_000]
# Simulation
val_count = zeros(Int, length(p))
Expand All @@ -38,6 +63,7 @@ end
μ = randn(rng, 10)
σ = rand(rng, 10)
dist = LightDiagNormal(μ, σ)
@test startswith(string(dist), "LightDiagNormal")
x = [(@inferred rand(rng, dist)) for _ in 1:100_000]
# Simulation
@test mean(x) μ atol = 2e-2
Expand Down

0 comments on commit d68fcf5

Please sign in to comment.