diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml new file mode 100644 index 0000000..857c3ae --- /dev/null +++ b/.JuliaFormatter.toml @@ -0,0 +1 @@ +style = "yas" diff --git a/Project.toml b/Project.toml index cdd8025..3ffcbf2 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Lighthouse" uuid = "ac2c24cd-07f0-4848-96b2-1b82c3ea0e59" authors = ["Beacon Biosignals, Inc."] -version = "0.13.3" +version = "0.13.4" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/metrics.jl b/src/metrics.jl index cc6f120..d0bc98c 100644 --- a/src/metrics.jl +++ b/src/metrics.jl @@ -105,7 +105,7 @@ Return `(κ, p₀)` where `κ` is Cohen's kappa and `p₀` percent agreement giv their equivalents in [`confusion_matrix`](@ref)). """ function cohens_kappa(class_count, hard_label_pairs) - @assert all(issubset(pair, 1:class_count) for pair in hard_label_pairs) + all(issubset(pair, 1:class_count) for pair in hard_label_pairs) || throw(ArgumentError("Unexpected class in `hard_label_pairs`.")) p₀ = accuracy(confusion_matrix(class_count, hard_label_pairs)) pₑ = _probability_of_chance_agreement(class_count, hard_label_pairs) return _cohens_kappa(p₀, pₑ), p₀ diff --git a/src/utilities.jl b/src/utilities.jl index 5e1ea71..7801a40 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -13,10 +13,11 @@ end area_under_curve(x, y) Calculates the area under the curve specified by the `x` vector and `y` vector -using the trapezoidal rule. +using the trapezoidal rule. If inputs are empty, return `missing`. """ function area_under_curve(x, y) - @assert length(x) == length(y) + length(x) == length(y) || throw(ArgumentError("Length of inputs must match.")) + length(x) == 0 && return missing auc = zero(middle(one(eltype(x)), one(eltype(y)))) perms = sortperm(x) sorted_x = view(x, perms) @@ -32,10 +33,10 @@ end area_under_curve_unit_square(x, y) Calculates the area under the curve specified by the `x` vector and `y` vector -for a unit square, using the trapezoidal rule. +for a unit square, using the trapezoidal rule. If inputs are empty, return `missing`. """ function area_under_curve_unit_square(x, y) - @assert length(x) == length(y) + length(x) == length(y) || throw(ArgumentError("Length of inputs must match.")) kept = [(i, j) for (i, j) in zip(x, y) if !(ismissing(i) || ismissing(j)) && (0 <= i <= 1 && 0 <= j <= 1)] diff --git a/test/metrics.jl b/test/metrics.jl index 74e8b76..2254afe 100644 --- a/test/metrics.jl +++ b/test/metrics.jl @@ -1,11 +1,9 @@ @testset "agreement/confusion matrix tests" begin hard_label_pairs = zip([1, 1, 3, 1, 3, 1, 2, 1], [2, 2, 1, 1, 3, 2, 3, 1]) c = confusion_matrix(3, hard_label_pairs) - @test c == [ - 2 3 0 - 0 0 1 - 1 0 1 - ] + @test c == [2 3 0 + 0 0 1 + 1 0 1] kappa, percent_agreement = cohens_kappa(3, hard_label_pairs) chance = Lighthouse._probability_of_chance_agreement(3, hard_label_pairs) @test chance == (5 * 3 + 1 * 3 + 2 * 2) / 8^2 @@ -91,6 +89,8 @@ @test Lighthouse._cohens_kappa(p, p / 2) > 0 end end + + @test_throws ArgumentError cohens_kappa(3, [(4, 5), (8, 2)]) end @testset "`calibration_curve`" begin diff --git a/test/utilities.jl b/test/utilities.jl index 2cbaeba..6f4a499 100644 --- a/test/utilities.jl +++ b/test/utilities.jl @@ -7,7 +7,8 @@ end @testset "`Lighthouse.area_under_curve`" begin - @test_throws AssertionError Lighthouse.area_under_curve([0, 1, 2], [0, 1]) + @test_throws ArgumentError Lighthouse.area_under_curve([0, 1, 2], [0, 1]) + @test ismissing(Lighthouse.area_under_curve([], [])) @test isapprox(Lighthouse.area_under_curve(collect(0:0.01:1), collect(0:0.01:1)), 0.5; atol=0.01) @test isapprox(Lighthouse.area_under_curve(collect(0:0.01:(2π)), sin.(0:0.01:(2π))), @@ -15,7 +16,8 @@ end end @testset "`Lighthouse.area_under_curve_unit_square`" begin - @test_throws AssertionError Lighthouse.area_under_curve_unit_square([0, 1, 2], [0, 1]) + @test_throws ArgumentError Lighthouse.area_under_curve_unit_square([0, 1, 2], [0, 1]) + @test ismissing(Lighthouse.area_under_curve_unit_square([], [])) @test isapprox(Lighthouse.area_under_curve_unit_square(collect(0:0.01:1), collect(0:0.01:1)), 0.5; atol=0.01)