Skip to content

Commit

Permalink
Improve code coverage (#115)
Browse files Browse the repository at this point in the history
* Improve code coverage

* Fix
  • Loading branch information
gdalle authored Sep 30, 2024
1 parent 284189f commit 882f2f7
Show file tree
Hide file tree
Showing 6 changed files with 34 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
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

2 comments on commit 882f2f7

@gdalle
Copy link
Owner Author

@gdalle gdalle commented on 882f2f7 Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/116309

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.4 -m "<description of version>" 882f2f76bad8947591b89cbdea29f31dff377b7b
git push origin v0.5.4

Please sign in to comment.