Skip to content

Commit

Permalink
Introduce shouldapply function
Browse files Browse the repository at this point in the history
  • Loading branch information
barucden committed Jul 31, 2021
1 parent 264fb06 commit d3023c3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/operation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
@inline supports_stepview(A) = supports_stepview(typeof(A))
@inline supports_lazy(A) = supports_lazy(typeof(A))

"""
shouldapply(op, what)
shouldapply(typeof(op), typeof(what))
Determines if operation `op` should be applied to `what`, which can be either
an image or a semantic wrapper.
"""
shouldapply(op, what) = shouldapply(typeof(op), typeof(what))
shouldapply(::Type{<:ImageOperation}, ::Type{<:AbstractArray}) = true
shouldapply(::Type{<:ImageOperation}, ::Type{<:SemanticWrapper}) = true
shouldapply(::Type{<:ColorOperation}, ::Type{Mask}) = true
# By default any operation is applicable to any image and any semantic wrapper.
# Add new methods to this function to define exceptions.

# --------------------------------------------------------------------

"""
Expand Down
39 changes: 39 additions & 0 deletions test/tst_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,45 @@
@test Augmentor.AffineOperation <: Augmentor.ImageOperation
@test Augmentor.ColorOperation <: Augmentor.ImageOperation

@testset "shouldapply" begin
@testset "type arguments" begin
ops = [Rotate90(), ColorJitter()]
whats = [testpattern(), Augmentor.Mask(testpattern())]
for op in ops, what in whats
@test Augmentor.shouldapply(op, what) == Augmentor.shouldapply(typeof(op), typeof(what))
end
end

@testset "affine operations" begin
ops = [FlipX, FlipY, NoOp, Rotate, Rotate180, Rotate270, Rotate90,
Scale, ShearX, ShearY]
applicable = [AbstractMatrix, Augmentor.Mask]
notapplicable = []
for op in ops
for what in applicable
@test Augmentor.shouldapply(op, what)
end
for what in notapplicable
@test !Augmentor.shouldapply(op, what)
end
end
end

@testset "color operations" begin
ops = [ColorJitter]
applicable = [AbstractMatrix]
notapplicable = [Augmentor.Mask]
for op in ops
for what in applicable
@test Augmentor.shouldapply(op, what)
end
for what in notapplicable
@test !Augmentor.shouldapply(op, what)
end
end
end
end

@testset "prepare" begin
@test @inferred(Augmentor.preparelazy(rect)) === rect
wv = @inferred Augmentor.prepareaffine(rect)
Expand Down

0 comments on commit d3023c3

Please sign in to comment.