Skip to content

Commit

Permalink
Support OffsetArrays 1
Browse files Browse the repository at this point in the history
Also fixes some issues for plain_array:
* recursively unwrap OffsetArray because the real data might be nested deeply
* provide an inefficient fallback method for generic AbstractArray to make sure
  this function works as expected
  • Loading branch information
johnnychen94 committed Feb 24, 2020
1 parent bb270dc commit 0999234
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ImageTransformations = "0.5, 0.6, 0.7, 0.8"
Interpolations = "0.8, 0.9, 0.10, 0.11, 0.12"
MLDataPattern = "0.4, 0.5"
MappedArrays = "0.1, 0.2"
OffsetArrays = "0.8, 0.9, 0.10, 0.11"
OffsetArrays = "0.8, 0.9, 0.10, 0.11, 1"
Rotations = "0.7, 0.8, 0.9, 0.10, 0.11, 0.12, 0.13"
StaticArrays = "0.8, 0.9, 0.10, 0.11, 0.12"
julia = "1"
Expand Down
8 changes: 8 additions & 0 deletions src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ if all(map((FixedPoint, Normed)) do T
# Reached when upstream package (e.g., FixedPoint) doesn't provide a specialization
Interpolations.tweight(A::AbstractArray{T}) where T<:FixedPoint = T
end

# backward support for OffsetArrays v0
if isdefined(OffsetArrays, :IdOffsetRange)
# introduced by OffsetArrays v1.0.0
OffsetRange = Union{UnitRange, IdentityUnitRange, OffsetArrays.IdOffsetRange}
else
OffsetRange = Union{UnitRange, IdentityUnitRange}
end
2 changes: 2 additions & 0 deletions src/operation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ for FUN in (:applyeager, :applylazy, :applypermute,
end

function applyeager(op::Operation, img::AbstractArray, param)
# fallback for operations that doesn't treat eager and lazy mode differently
# i.e., for op that supports_eager(typeof(op)) == false
plain_array(applylazy(op, img, param))
end

Expand Down
5 changes: 3 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ See also: [`plain_array`](@ref), [`plain_axes`](@ref)

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

@inline _plain_array(A::OffsetArray) = parent(A)
@inline _plain_array(A::OffsetArray) = _plain_array(parent(A))
@inline _plain_array(A::Array) = A
@inline _plain_array(A::SArray) = A
@inline _plain_array(A::MArray) = A
@inline _plain_array(A::AbstractArray) = collect(A)
@inline _plain_array(A::Tuple) = map(_plain_array, A)

"""
Expand Down Expand Up @@ -89,7 +90,7 @@ end
# --------------------------------------------------------------------

@inline match_idx(buffer::AbstractArray, inds::Tuple) = buffer
@inline match_idx(buffer::Union{Array,SubArray}, inds::NTuple{N,Union{UnitRange, IdentityUnitRange}}) where {N} =
@inline match_idx(buffer::Union{Array,SubArray}, inds::NTuple{N,OffsetRange}) where {N} =
OffsetArray(buffer, inds)

# --------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion test/operations/tst_channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
img_out = f(img_in)
res = @inferred(Augmentor.applylazy(SplitChannels(), img_in))
@test res == img_out
@test res == Augmentor.applyeager(SplitChannels(), img_in)
@test collect(res) == Augmentor.applyeager(SplitChannels(), img_in)
@test typeof(res) == typeof(img_out)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/operations/tst_convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
let img = @inferred(Augmentor.applylazy(ConvertEltype(Gray{Float32}), OffsetArray(rect,-2,-1)))
@test parent(parent(img)) === rect
@test typeof(img) <: ReadonlyMappedArray{Gray{Float32},2}
@test axes(img) === (OffsetArrays.IdentityUnitRange(-1:0), OffsetArrays.IdentityUnitRange(0:2))
@test axes(img) == (OffsetRange(-1:0), OffsetRange(0:2))
@test img[0,0] isa Gray{Float32}
@test collect(img) == convert.(Gray{Float32}, rect)
end
Expand Down
2 changes: 1 addition & 1 deletion test/operations/tst_mapfun.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
res = @inferred(Augmentor.applylazy(MapFun(x->x-RGB(.1,.1,.1)), rgb_rect))
@test res == mappedarray(x->x-RGB(.1,.1,.1), rgb_rect)
res = @inferred(Augmentor.applylazy(MapFun(x->x-RGB(.1,.1,.1)), OffsetArray(rgb_rect,-2,-1)))
@test axes(res) === OffsetArrays.IdentityUnitRange.((-1:0, 0:2))
@test axes(res) == OffsetRange.((-1:0, 0:2))
@test @inferred(getindex(res,0,0)) isa RGB{Float64}
@test res == mappedarray(x->x-RGB(.1,.1,.1), OffsetArray(rgb_rect,-2,-1))
@test typeof(res) <: MappedArrays.ReadonlyMappedArray{ColorTypes.RGB{Float64}}
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ using MLDataPattern
using OffsetArrays, StaticArrays, IdentityRanges, MappedArrays, ComputationalResources
using ReferenceTests, Test, TestImages, ImageDistances, Statistics

if isdefined(OffsetArrays, :IdOffsetRange)
OffsetRange = OffsetArrays.IdOffsetRange
else
OffsetRange = OffsetArrays.IdentityUnitRange
end

# check for ambiguities
refambs = detect_ambiguities(ImageTransformations, Base, Core)
using Augmentor
Expand Down
12 changes: 9 additions & 3 deletions test/tst_augment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ ops = (Scale(.1,.2),CropRatio())
@test typeof(wv.indices) <: Tuple{Vararg{IdentityRange}}
@test typeof(parent(wv)) <: InvWarpedView
@test parent(parent(wv)).itp.coefs === camera
@test_reference "reference/scale_cropratio.txt" wv
@test_reference "reference/scale_cropratio.txt" collect(wv)
img = @inferred augment(camera, ops)
@test img == parent(copy(wv))
@test typeof(img) <: Array
Expand Down Expand Up @@ -333,8 +333,14 @@ ops = (Rotate(45),CropSize(200,200),Zoom(1.1),ConvertEltype(RGB{Float64}),SplitC
@test wv3 == wv4
@test typeof(wv3) == typeof(wv4)
img = colorview(RGB{Float64}, wv3)
@test RGB{Float64}.(collect(wv1)) img # collect(RGB{Float64}, wv1) returns OffsetArray
@test wv2 == img
if axes(wv1) == axes(img)
# OffsetArray v1
@test RGB{Float64}.(wv1) img
else
# OffsetArray v0
@test RGB{Float64}.(collect(wv1)) img
end
@test wv2 == collect(img)
@test_reference "reference/rot45_crop_zoom_convert.txt" wv2
end

Expand Down
10 changes: 7 additions & 3 deletions test/tst_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ end
@test @inferred(Augmentor.plain_array(Ar)) == reshape(A,1,3,3)
@test typeof(Augmentor.plain_array(Ar)) <: Array{Int,3}
end
let Ac = channelview(Gray{Float64}.(A))
@test @inferred(Augmentor.plain_array(Ac)) == A
@test typeof(Augmentor.plain_array(Ac)) <: Array{Float64, 2}
end
end

@testset "plain_axes" begin
Expand Down Expand Up @@ -141,19 +145,19 @@ end
A = [1 2 3; 4 5 6; 7 8 9]
@test @inferred(Augmentor.match_idx(A, axes(A))) === A
let img = @inferred Augmentor.match_idx(A, (2:4, 2:4))
@test axes(img) === OffsetArrays.IdentityUnitRange.((2:4, 2:4))
@test axes(img) == OffsetRange.((2:4, 2:4))
@test typeof(img) <: OffsetArray
end
let B = view(A,1:3,1:3)
@test @inferred(Augmentor.match_idx(B, axes(B))) === B
end
let B = view(A,1:3,1:3)
img = @inferred(Augmentor.match_idx(B, B.indices))
@test axes(img) === OffsetArrays.IdentityUnitRange.((1:3, 1:3))
@test axes(img) == OffsetRange.((1:3, 1:3))
@test typeof(img) <: OffsetArray
end
let img = @inferred Augmentor.match_idx(view(A,1:3,1:3), (2:4,2:4))
@test axes(img) === OffsetArrays.IdentityUnitRange.((2:4, 2:4))
@test axes(img) == OffsetRange.((2:4, 2:4))
@test typeof(img) <: OffsetArray
end
let C = Augmentor.prepareaffine(A)
Expand Down

0 comments on commit 0999234

Please sign in to comment.