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

Added support for comparing AbstractImageAnnotations and Labels #14

Merged
merged 2 commits into from
Aug 30, 2023
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
3 changes: 1 addition & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ jobs:
fail-fast: false
matrix:
version:
- '1.3'
- '1.6'
- '1.9'
- '1'
os:
- ubuntu-latest
- macOS-latest
Expand Down
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test:
stage: test
parallel:
matrix:
- CI_JULIA_VERSION: ["1.3", "1.6", "1.9"]
- CI_JULIA_VERSION: ["1.6", "1"]
extends:
- .julia.setup
- .julia.test
1 change: 1 addition & 0 deletions src/ImageAnnotations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export StaticObjectAnnotator
include("label.jl")
include("concept.jl")

include("abstract_image_annotation.jl")
include("image_annotation.jl")

include("abstract_object_annotation.jl")
Expand Down
49 changes: 49 additions & 0 deletions src/abstract_image_annotation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
abstract type AbstractImageAnnotation{L} end

function get_label end
function get_confidence end
function get_annotator_name end

function Base.isless(a::T, b::U) where {T <: AbstractImageAnnotation, U <: AbstractImageAnnotation}
if Symbol(T) < Symbol(U)
return true
elseif Symbol(U) < Symbol(T)
return false
end
if get_label(a) !== get_label(b)
return get_label(a) < get_label(b)
end
if get_confidence(a) === nothing
if get_confidence(b) === nothing
if get_annotator_name(a) === nothing
return get_annotator_name(b) !== nothing
else
if get_annotator_name(b) === nothing
return false

Check warning on line 22 in src/abstract_image_annotation.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_image_annotation.jl#L21-L22

Added lines #L21 - L22 were not covered by tests
else
return get_annotator_name(a) < get_annotator_name(b)

Check warning on line 24 in src/abstract_image_annotation.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_image_annotation.jl#L24

Added line #L24 was not covered by tests
end
end
else
return true
end
else
if get_confidence(b) === nothing
return false

Check warning on line 32 in src/abstract_image_annotation.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_image_annotation.jl#L32

Added line #L32 was not covered by tests
else
if get_confidence(a) !== get_confidence(b)
return get_confidence(a) < get_confidence(b)
else
if get_annotator_name(a) === nothing
return get_annotator_name(b) !== nothing
else
if get_annotator_name(b) === nothing
return false

Check warning on line 41 in src/abstract_image_annotation.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_image_annotation.jl#L41

Added line #L41 was not covered by tests
else
return get_annotator_name(a) < get_annotator_name(b)
end
end
end
end
end
end
41 changes: 0 additions & 41 deletions src/image_annotation.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
abstract type AbstractImageAnnotation{L} end

mutable struct ImageAnnotation{L} <: AbstractImageAnnotation{L}
label::L
confidence::Union{Float32, Nothing}
Expand All @@ -18,45 +16,6 @@ function Base.:(==)(a::ImageAnnotation, b::ImageAnnotation)
return a.label == b.label && a.confidence == b.confidence && a.annotator_name == b.annotator_name
end

function Base.isless(a::ImageAnnotation, b::ImageAnnotation)
if a.label !== b.label
return a.label < b.label
end
if a.confidence === nothing
if b.confidence === nothing
if a.annotator_name === nothing
return b.annotator_name !== nothing
else
if b.annotator_name === nothing
return false
else
return a.annotator_name < b.annotator_name
end
end
else
return true
end
else
if b.confidence === nothing
return false
else
if a.confidence !== b.confidence
return a.confidence < b.confidence
else
if a.annotator_name === nothing
return b.annotator_name !== nothing
else
if b.annotator_name === nothing
return false
else
return a.annotator_name < b.annotator_name
end
end
end
end
end
end

get_label(annotation::ImageAnnotation) = annotation.label
get_confidence(annotation::ImageAnnotation) = annotation.confidence
get_annotator_name(annotation::ImageAnnotation) = annotation.annotator_name
8 changes: 8 additions & 0 deletions src/label.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ end
Label(value::T) where {T} = Label{T}(value)

Base.:(==)(a::Label, b::Label) = a.value == b.value && a.attributes == b.attributes

function Base.isless(a::Label{T}, b::Label{T}) where {T}
if a.value != b.value
return a.value < b.value
else
return length(a.attributes) < length(b.attributes)
end
end
14 changes: 14 additions & 0 deletions test/abstract_image_annotation_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using GeometryBasics
using ImageAnnotations
using Test

@testset "AbstractImageAnnotation" begin
@testset "Base.isless" begin
@testset "Annotations are sorted by their type name" begin
a = BoundingBoxAnnotation(Point2(0, 1), 2, 3, "car")
b = ImageAnnotation("car")
@test isless(a, b)
@test !isless(b, a)
end
end
end
9 changes: 9 additions & 0 deletions test/label_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ using Test
end
end
end
@testset "Base.isless" begin
@testset "Labels are sorted by value, then number of attributes" begin
a = Label(1)
b = Label(2)
c = Label(2, Dict{String, Any}("a" => 3))
@test isless(a, b)
@test isless(b, c)
end
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Test

@testset "ImageAnnotations" begin
include("abstract_image_annotation_tests.jl")
include("concept_tests.jl")
include("image_annotation_tests.jl")
include("label_tests.jl")
Expand Down
Loading