Skip to content
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
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "yas"
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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₀
Expand Down
9 changes: 5 additions & 4 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)]
Expand Down
10 changes: 5 additions & 5 deletions test/metrics.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions test/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
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π))),
0.0; atol=0.01)
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)
Expand Down