Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code coverage #115

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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}}
Bβ::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
Loading