Skip to content

Commit

Permalink
Switch to 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 71d5632
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 11 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 = "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
1 change: 1 addition & 0 deletions src/Augmentor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ using OffsetArrays
# https://github.com/JuliaArrays/OffsetArrays.jl/pull/62
# TODO: switch to Base.IdentityUnitRange when we decide to drop 1.0 compatibility
using OffsetArrays: IdentityUnitRange
using OffsetArrays: IdOffsetRange

using InteractiveUtils: methodswith

Expand Down
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,Union{UnitRange, IdentityUnitRange, IdOffsetRange}}) 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) == (OffsetArrays.IdOffsetRange(-1:0), OffsetArrays.IdOffsetRange(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) == OffsetArrays.IdOffsetRange.((-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
4 changes: 2 additions & 2 deletions test/tst_augment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ 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
@test collect(RGB{Float64}, wv1) img
@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) == OffsetArrays.IdOffsetRange.((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) == OffsetArrays.IdOffsetRange.((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) == OffsetArrays.IdOffsetRange.((2:4, 2:4))
@test typeof(img) <: OffsetArray
end
let C = Augmentor.prepareaffine(A)
Expand Down

0 comments on commit 71d5632

Please sign in to comment.