Skip to content

Commit

Permalink
Add Aqua tests and fix method ambiguity (#92)
Browse files Browse the repository at this point in the history
* Add Aqua tests and fix method ambiguity

* Fix Julia 1.6 compatibility
  • Loading branch information
devmotion authored Aug 3, 2023
1 parent a91374c commit fece47d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MCMCDiagnosticTools"
uuid = "be115224-59cd-429b-ad48-344e309966f0"
authors = ["David Widmann"]
version = "0.3.4"
version = "0.3.5"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![Build Status](https://github.com/TuringLang/MCMCDiagnosticTools.jl/workflows/CI/badge.svg?branch=main)](https://github.com/TuringLang/MCMCDiagnosticTools.jl/actions?query=workflow%3ACI+branch%3Amain)
[![Coverage](https://codecov.io/gh/TuringLang/MCMCDiagnosticTools.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/TuringLang/MCMCDiagnosticTools.jl)
[![Coverage](https://coveralls.io/repos/github/TuringLang/MCMCDiagnosticTools.jl/badge.svg?branch=main)](https://coveralls.io/github/TuringLang/MCMCDiagnosticTools.jl?branch=main)
[![Aqua QA](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl)
[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://github.com/SciML/ColPrac)

Expand Down
11 changes: 7 additions & 4 deletions src/rstar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,20 @@ julia> round(value; digits=2)
Lambert, B., & Vehtari, A. (2020). ``R^*``: A robust MCMC convergence diagnostic with uncertainty using decision tree classifiers.
"""
function rstar(rng::Random.AbstractRNG, classifier, x::AbstractArray; kwargs...)
return rstar(rng, classifier, _params_array(x); kwargs...)
end
function rstar(rng::Random.AbstractRNG, classifier, x::AbstractArray{<:Any,3}; kwargs...)
samples = reshape(x, :, size(x, 3))
samples = reshape(x, size(x, 1) * size(x, 2), :)
chain_inds = repeat(axes(x, 2); inner=size(x, 1))
return rstar(rng, classifier, samples, chain_inds; kwargs...)
end

function rstar(classifier, x, y::AbstractVector{Int}; kwargs...)
return rstar(Random.default_rng(), classifier, x, y; kwargs...)
end
# Fix method ambiguity issue
function rstar(rng::Random.AbstractRNG, classifier, x::AbstractVector{Int}; kwargs...)
samples = reshape(x, length(x), :)
chain_inds = ones(Int, length(x))
return rstar(rng, classifier, samples, chain_inds; kwargs...)
end

function rstar(classifier, x::AbstractArray; kwargs...)
return rstar(Random.default_rng(), classifier, x; kwargs...)
Expand Down
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DynamicHMC = "bbc10e6e-7c05-544b-b16e-64fede858acb"
EvoTrees = "f6006082-12f8-11e9-0c9c-0d5d367ab1e5"
Expand All @@ -19,6 +20,7 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
Aqua = "0.6.5"
Distributions = "0.25"
DynamicHMC = "3"
EvoTrees = "0.14.7, 0.15"
Expand Down
16 changes: 16 additions & 0 deletions test/aqua.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using MCMCDiagnosticTools
using Aqua
using Test

@testset "Aqua" begin
# Test ambiguities separately without Base and Core
# Ref: https://github.com/JuliaTesting/Aqua.jl/issues/77
# Only test Project.toml formatting on Julia > 1.6 when running Github action
# Ref: https://github.com/JuliaTesting/Aqua.jl/issues/105
Aqua.test_all(
MCMCDiagnosticTools;
ambiguities=false,
project_toml_formatting=VERSION >= v"1.7" || !haskey(ENV, "GITHUB_ACTIONS"),
)
Aqua.test_ambiguities([MCMCDiagnosticTools])
end
10 changes: 10 additions & 0 deletions test/rstar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,14 @@ end
)
@test_throws ArgumentError MCMCDiagnosticTools._rstar(1.0, rand(2), rand(2))
end

@testset "single chain: method ambiguity issue" begin
samples = rand(1:5, N)
rng = MersenneTwister(42)
dist = rstar(rng, DecisionTreeClassifier(), samples)
@test mean(dist) 1 atol = 0.15
Random.seed!(rng, 42)
dist2 = rstar(rng, DecisionTreeClassifier(), samples, ones(Int, N))
@test dist2 == dist
end
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Random.seed!(1)
@testset "MCMCDiagnosticTools.jl" begin
include("helpers.jl")

@testset "Aqua" begin
include("aqua.jl")
end

@testset "utils" begin
include("utils.jl")
end
Expand Down

2 comments on commit fece47d

@devmotion
Copy link
Member Author

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/88968

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.3.5 -m "<description of version>" fece47dcdf7a6d907287059d62cc368c9f724d32
git push origin v0.3.5

Please sign in to comment.