Skip to content

Commit dc3682e

Browse files
committed
update to julia 1.1
* maybe_copy doesn't explain its usage * replace Slice by OffsetArrays.IdentityUnitRange JuliaArrays/OffsetArrays.jl#62
1 parent 23ddbd1 commit dc3682e

File tree

10 files changed

+63
-45
lines changed

10 files changed

+63
-45
lines changed

src/Augmentor.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ using CoordinateTransformations
99
using Rotations
1010
using Interpolations
1111
using StaticArrays
12-
using OffsetArrays
1312
using IdentityRanges
1413
using MLDataPattern
1514
using ComputationalResources
1615
using FileIO
1716
using Base.PermutedDimsArrays: PermutedDimsArray
1817
using LinearAlgebra
18+
using OffsetArrays
19+
20+
# axes(::OffsetArray) changes from Base.Slice to Base.IdentityUnitRange in julia 1.1
21+
# https://github.com/JuliaArrays/OffsetArrays.jl/pull/62
22+
# TODO: switch to Base.IdentityUnitRange when we decide to drop 1.0 compatibility
23+
using OffsetArrays: IdentityUnitRange
1924

2025
export
2126

src/operation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ for FUN in (:applyeager, :applylazy, :applypermute,
6363
end
6464

6565
function applyeager(op::Operation, img::AbstractArray, param)
66-
maybe_copy(applylazy(op, img, param))
66+
contiguous(applylazy(op, img, param))
6767
end
6868

6969
function applyaffineview(op::Operation, img::AbstractArray, param)

src/operations/cache.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pl = ElasticDistortion(3,3) |> zeros(20,20) |> Rotate(-10:10)
5353
"""
5454
struct CacheImage <: ImageOperation end
5555

56-
applyeager(op::CacheImage, img::AbstractArray, param) = maybe_copy(img)
56+
applyeager(op::CacheImage, img::AbstractArray, param) = contiguous(img)
5757

5858
function showconstruction(io::IO, op::CacheImage)
5959
print(io, typeof(op).name.name, "()")

src/operations/convert.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252
@inline supports_lazy(::Type{<:ConvertEltype}) = true
5353

5454
function applyeager(op::ConvertEltype{T}, img::AbstractArray, param) where T
55-
maybe_copy(convert(AbstractArray{T}, img))
55+
contiguous(convert(AbstractArray{T}, img))
5656
end
5757

5858
function applylazy(op::ConvertEltype{T}, img::AbstractArray, param) where T

src/operations/mapfun.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ end
5151
@inline supports_lazy(::Type{<:MapFun}) = true
5252

5353
function applyeager(op::MapFun, img::AbstractArray, param)
54-
maybe_copy(map(op.fun, img))
54+
contiguous(map(op.fun, img))
5555
end
5656

5757
function applylazy(op::MapFun, img::AbstractArray, param)
@@ -130,7 +130,7 @@ end
130130

131131
function applyeager(op::AggregateThenMapFun, img::AbstractArray, param)
132132
agg = op.aggfun(img)
133-
maybe_copy(map(x -> op.mapfun(x, agg), img))
133+
contiguous(map(x -> op.mapfun(x, agg), img))
134134
end
135135

136136
function applylazy(op::AggregateThenMapFun, img::AbstractArray, param)

src/operations/noop.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct NoOp <: AffineOperation end
1515

1616
# TODO: implement method for n-dim arrays
1717
toaffinemap(::NoOp, img::AbstractMatrix) = AffineMap(@SMatrix([1. 0; 0 1.]), @SVector([0.,0.]))
18-
applyeager(::NoOp, img::AbstractArray, param) = maybe_copy(img)
18+
applyeager(::NoOp, img::AbstractArray, param) = contiguous(img)
1919
applylazy(::NoOp, img::AbstractArray, param) = img
2020

2121
function applyview(::NoOp, img::AbstractArray, param)

src/utils.jl

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,21 @@ function safe_rand(args...)
3232
end
3333

3434
# --------------------------------------------------------------------
35+
"""
36+
contiguous(A::AbstractArray)
37+
contiguous(A::Tuple)
38+
39+
Return a memory contiguous array for better performance.
3540
36-
@inline maybe_copy(A::OffsetArray) = A
37-
@inline maybe_copy(A::Array) = A
38-
@inline maybe_copy(A::SArray) = A
39-
@inline maybe_copy(A::MArray) = A
40-
@inline maybe_copy(A::AbstractArray) = match_idx(collect(A), axes(A))
41-
@inline maybe_copy(A::Tuple) = map(maybe_copy, A)
41+
Data copy only happens when necessary. For example, views returned by `view`,
42+
`permuteddimsview` are such cases.
43+
"""
44+
@inline contiguous(A::OffsetArray) = A
45+
@inline contiguous(A::Array) = A
46+
@inline contiguous(A::SArray) = A
47+
@inline contiguous(A::MArray) = A
48+
@inline contiguous(A::AbstractArray) = match_idx(collect(A), axes(A))
49+
@inline contiguous(A::Tuple) = map(contiguous, A)
4250

4351
# --------------------------------------------------------------------
4452

@@ -48,10 +56,15 @@ end
4856
@inline _plain_array(A::MArray) = A
4957
@inline _plain_array(A::Tuple) = map(_plain_array, A)
5058
# avoid recursion
51-
@inline plain_array(A) = _plain_array(maybe_copy(A))
59+
@inline plain_array(A) = _plain_array(contiguous(A))
5260

5361
# --------------------------------------------------------------------
5462

63+
"""
64+
plain_axes(A::AbstractArray)
65+
66+
Generate a 1-based array from `A` without data copy.
67+
"""
5568
@inline plain_axes(A::Array) = A
5669
@inline plain_axes(A::OffsetArray) = parent(A)
5770
@inline plain_axes(A::AbstractArray) = _plain_axes(A, axes(A))
@@ -65,7 +78,7 @@ end
6578
view(A, axes(A)...)
6679
end
6780

68-
@inline function _plain_axes(A::AbstractArray{T,N}, ids::NTuple{N,Base.Slice}) where {T, N}
81+
@inline function _plain_axes(A::AbstractArray{T,N}, ids::NTuple{N, IdentityUnitRange}) where {T, N}
6982
view(A, map(i->i.indices, ids)...)
7083
end
7184

@@ -76,7 +89,7 @@ end
7689
# --------------------------------------------------------------------
7790

7891
@inline match_idx(buffer::AbstractArray, inds::Tuple) = buffer
79-
@inline match_idx(buffer::Union{Array,SubArray}, inds::NTuple{N,Union{UnitRange,Base.Slice{<:UnitRange}}}) where {N} =
92+
@inline match_idx(buffer::Union{Array,SubArray}, inds::NTuple{N,Union{UnitRange, IdentityUnitRange}}) where {N} =
8093
OffsetArray(buffer, inds)
8194

8295
# --------------------------------------------------------------------

test/operations/tst_convert.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
let img = @inferred(Augmentor.applylazy(ConvertEltype(Gray{Float32}), OffsetArray(rect,-2,-1)))
5858
@test parent(parent(img)) === rect
5959
@test typeof(img) <: ReadonlyMappedArray{Gray{Float32},2}
60-
@test axes(img) === (Base.Slice(-1:0), Base.Slice(0:2))
60+
@test axes(img) === (OffsetArrays.IdentityUnitRange(-1:0), OffsetArrays.IdentityUnitRange(0:2))
6161
@test img[0,0] isa Gray{Float32}
6262
@test collect(img) == convert.(Gray{Float32}, rect)
6363
end

test/operations/tst_mapfun.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
res = @inferred(Augmentor.applylazy(MapFun(x->x-RGB(.1,.1,.1)), rgb_rect))
5555
@test res == mappedarray(x->x-RGB(.1,.1,.1), rgb_rect)
5656
res = @inferred(Augmentor.applylazy(MapFun(x->x-RGB(.1,.1,.1)), OffsetArray(rgb_rect,-2,-1)))
57-
@test axes(res) === Base.Slice.((-1:0, 0:2))
57+
@test axes(res) === OffsetArrays.IdentityUnitRange.((-1:0, 0:2))
5858
@test @inferred(getindex(res,0,0)) isa RGB{Float64}
5959
@test res == mappedarray(x->x-RGB(.1,.1,.1), OffsetArray(rgb_rect,-2,-1))
6060
@test typeof(res) <: MappedArrays.ReadonlyMappedArray{ColorTypes.RGB{Float64}}

test/tst_utils.jl

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,53 +23,53 @@ end
2323
@test typeof(num) <: Vector{Float64}
2424
end
2525

26-
@testset "maybe_copy" begin
26+
@testset "contiguous" begin
2727
A = [1 2 3; 4 5 6; 7 8 9]
2828
Ao = OffsetArray(A, (-2,-1))
29-
@test @inferred(Augmentor.maybe_copy(A)) === A
30-
@test @inferred(Augmentor.maybe_copy(Ao)) === Ao
29+
@test @inferred(Augmentor.contiguous(A)) === A
30+
@test @inferred(Augmentor.contiguous(Ao)) === Ao
3131
let Ast = @SMatrix [1 2 3; 4 5 6; 7 8 9]
32-
@test @inferred(Augmentor.maybe_copy(Ast)) === Ast
32+
@test @inferred(Augmentor.contiguous(Ast)) === Ast
3333
end
3434
let v = view(A, 2:3, 1:2)
35-
@test @inferred(Augmentor.maybe_copy(v)) == A[2:3, 1:2]
36-
@test typeof(Augmentor.maybe_copy(v)) <: Array
35+
@test @inferred(Augmentor.contiguous(v)) == A[2:3, 1:2]
36+
@test typeof(Augmentor.contiguous(v)) <: Array
3737
end
3838
let v = view(OffsetArray(A, (-2,-1)), 0:1, 0:1)
39-
@test @inferred(Augmentor.maybe_copy(v)) == A[2:3, 1:2]
40-
@test typeof(Augmentor.maybe_copy(v)) <: Array
39+
@test @inferred(Augmentor.contiguous(v)) == A[2:3, 1:2]
40+
@test typeof(Augmentor.contiguous(v)) <: Array
4141
end
4242
let v = view(A, IdentityRange(2:3), IdentityRange(1:2))
43-
@test @inferred(Augmentor.maybe_copy(v)) == OffsetArray(A[2:3,1:2], (1, 0))
44-
@test typeof(Augmentor.maybe_copy(v)) <: OffsetArray
43+
@test @inferred(Augmentor.contiguous(v)) == OffsetArray(A[2:3,1:2], (1, 0))
44+
@test typeof(Augmentor.contiguous(v)) <: OffsetArray
4545
end
4646
let v = channelview(rect)
47-
@test @inferred(Augmentor.maybe_copy(v)) == channelview(rect)
48-
@test typeof(Augmentor.maybe_copy(v)) <: Array
47+
@test @inferred(Augmentor.contiguous(v)) == channelview(rect)
48+
@test typeof(Augmentor.contiguous(v)) <: Array
4949
end
5050
let p = permuteddimsview(A, (2,1))
51-
@test @inferred(Augmentor.maybe_copy(p)) == A'
52-
@test typeof(Augmentor.maybe_copy(p)) <: Array
51+
@test @inferred(Augmentor.contiguous(p)) == A'
52+
@test typeof(Augmentor.contiguous(p)) <: Array
5353
end
5454
let p = permuteddimsview(Ao, (2,1))
55-
@test @inferred(Augmentor.maybe_copy(p)) == Ao'
56-
@test typeof(Augmentor.maybe_copy(p)) <: OffsetArray
55+
@test @inferred(Augmentor.contiguous(p)) == Ao'
56+
@test typeof(Augmentor.contiguous(p)) <: OffsetArray
5757
end
5858
let p = view(permuteddimsview(A, (2,1)), IdentityRange(2:3), IdentityRange(1:2))
59-
@test @inferred(Augmentor.maybe_copy(p)) == OffsetArray(A'[2:3, 1:2],1,0)
60-
@test typeof(Augmentor.maybe_copy(p)) <: OffsetArray
59+
@test @inferred(Augmentor.contiguous(p)) == OffsetArray(A'[2:3, 1:2],1,0)
60+
@test typeof(Augmentor.contiguous(p)) <: OffsetArray
6161
end
6262
let Aa = Augmentor.prepareaffine(A)
63-
@test @inferred(Augmentor.maybe_copy(Aa)) == OffsetArray(A, (0,0))
64-
@test typeof(Augmentor.maybe_copy(Aa)) <: OffsetArray
63+
@test @inferred(Augmentor.contiguous(Aa)) == OffsetArray(A, (0,0))
64+
@test typeof(Augmentor.contiguous(Aa)) <: OffsetArray
6565
end
6666
let Ar = reshape(view(A,:,:),1,3,3)
67-
@test @inferred(Augmentor.maybe_copy(Ar)) == reshape(A,1,3,3)
68-
@test typeof(Augmentor.maybe_copy(Ar)) <: Array{Int,3}
67+
@test @inferred(Augmentor.contiguous(Ar)) == reshape(A,1,3,3)
68+
@test typeof(Augmentor.contiguous(Ar)) <: Array{Int,3}
6969
end
7070
let Ar = reshape(view(Ao,:,:),1,3,3)
71-
@test @inferred(Augmentor.maybe_copy(Ar)) == reshape(A,1,3,3)
72-
@test typeof(Augmentor.maybe_copy(Ar)) <: Array{Int,3}
71+
@test @inferred(Augmentor.contiguous(Ar)) == reshape(A,1,3,3)
72+
@test typeof(Augmentor.contiguous(Ar)) <: Array{Int,3}
7373
end
7474
end
7575

@@ -157,19 +157,19 @@ end
157157
A = [1 2 3; 4 5 6; 7 8 9]
158158
@test @inferred(Augmentor.match_idx(A, axes(A))) === A
159159
let img = @inferred Augmentor.match_idx(A, (2:4, 2:4))
160-
@test axes(img) === Base.Slice.((2:4, 2:4))
160+
@test axes(img) === OffsetArrays.IdentityUnitRange.((2:4, 2:4))
161161
@test typeof(img) <: OffsetArray
162162
end
163163
let B = view(A,1:3,1:3)
164164
@test @inferred(Augmentor.match_idx(B, axes(B))) === B
165165
end
166166
let B = view(A,1:3,1:3)
167167
img = @inferred(Augmentor.match_idx(B, B.indices))
168-
@test axes(img) === Base.Slice.((1:3, 1:3))
168+
@test axes(img) === OffsetArrays.IdentityUnitRange.((1:3, 1:3))
169169
@test typeof(img) <: OffsetArray
170170
end
171171
let img = @inferred Augmentor.match_idx(view(A,1:3,1:3), (2:4,2:4))
172-
@test axes(img) === Base.Slice.((2:4, 2:4))
172+
@test axes(img) === OffsetArrays.IdentityUnitRange.((2:4, 2:4))
173173
@test typeof(img) <: OffsetArray
174174
end
175175
let C = Augmentor.prepareaffine(A)

0 commit comments

Comments
 (0)