From 57978fd1d3d1a46d928c03e2d320472e5c50e17b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Baru=C4=8Di=C4=87?= Date: Mon, 9 Aug 2021 15:59:08 +0200 Subject: [PATCH 1/2] Use `of_eltype` instead of convert lambda MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As suggested by @timholy in #17, this commit changes `mappedarray(c->convert(T,c), img)` to `of_eltype(T, img)`. It results into `MappedArray` instead of `ReadonlyMappedArray`, and it is more descriptive too. This commit does not deal with #17, but I can confirm that the issue seems to be fixed already (which @johnnychen94 noticed before) and can be closed: ```julia julia> using Augmentor, Test julia> img = testpattern(); julia> @inferred(augment(img, ConvertEltype(Gray))) |> summary # Gray{Any} "300×400 Array{Gray,2} with eltype Gray" julia> pl = SplitChannels() |> ConvertEltype(Gray) 2-step Augmentor.ImmutablePipeline: 1.) Split colorant into its color channels 2.) Convert eltype to Gray julia> @inferred(augment(img, pl)) |> summary # Gray{N0f8} "4×300×400 Array{Gray{N0f8},3} with eltype Gray{FixedPointNumbers.N0f8}" ``` --- src/operations/convert.jl | 2 +- test/operations/tst_convert.jl | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/operations/convert.jl b/src/operations/convert.jl index e9d21c8a..05312d23 100644 --- a/src/operations/convert.jl +++ b/src/operations/convert.jl @@ -56,7 +56,7 @@ function applyeager(op::ConvertEltype{T}, img::AbstractArray, param) where T end function applylazy(op::ConvertEltype{T}, img::AbstractArray, param) where T - mappedarray(c->convert(T,c), img) + of_eltype(T, img) end function showconstruction(io::IO, op::ConvertEltype) diff --git a/test/operations/tst_convert.jl b/test/operations/tst_convert.jl index 180ce027..a5f1902b 100644 --- a/test/operations/tst_convert.jl +++ b/test/operations/tst_convert.jl @@ -57,21 +57,21 @@ @test @inferred(Augmentor.supports_lazy(typeof(ConvertEltype(Gray{N0f8})))) === true 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 typeof(img) <: MappedArray{Gray{Float32},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 let img = @inferred(Augmentor.applylazy(ConvertEltype(Gray{Float32}), Augmentor.prepareaffine(rect))) @test parent(parent(parent(parent(img)))) === rect - @test typeof(img) <: ReadonlyMappedArray{Gray{Float32},2} + @test typeof(img) <: MappedArray{Gray{Float32},2} @test axes(img) === (1:2, 1:3) @test img[1,1] isa Gray{Float32} @test collect(img) == convert.(Gray{Float32}, rect) end let img = @inferred(Augmentor.applylazy(ConvertEltype(Gray{Float32}), view(rect, IdentityRange(1:2), IdentityRange(1:3)))) @test parent(parent(img)) === rect - @test typeof(img) <: ReadonlyMappedArray{Gray{Float32},2} + @test typeof(img) <: MappedArray{Gray{Float32},2} @test axes(img) === (1:2, 1:3) @test img[1,1] isa Gray{Float32} @test collect(img) == convert.(Gray{Float32}, rect) @@ -79,23 +79,23 @@ let img = @inferred(Augmentor.applylazy(ConvertEltype(Gray{Float32}), rgb_rect)) @test parent(img) === rgb_rect @test axes(img) === (Base.OneTo(2), Base.OneTo(3)) - @test typeof(img) <: ReadonlyMappedArray{Gray{Float32},2} + @test typeof(img) <: MappedArray{Gray{Float32},2} @test img == convert.(Gray{Float32}, rgb_rect) end let img = @inferred(Augmentor.applylazy(ConvertEltype(Float32), checkers)) @test parent(img) === checkers @test axes(img) === (Base.OneTo(3), Base.OneTo(5)) - @test typeof(img) <: ReadonlyMappedArray{Float32,2} + @test typeof(img) <: MappedArray{Float32,2} @test img == convert(Array{Float32}, checkers) end let img = @inferred(Augmentor.applylazy(ConvertEltype(RGB{N0f8}), checkers)) @test parent(img) === checkers - @test typeof(img) <: ReadonlyMappedArray{RGB{N0f8},2} + @test typeof(img) <: MappedArray{RGB{N0f8},2} @test img == convert.(RGB, checkers) end let img = @inferred(Augmentor.applylazy(ConvertEltype(RGB{Float64}), checkers)) @test parent(img) === checkers - @test typeof(img) <: ReadonlyMappedArray{RGB{Float64},2} + @test typeof(img) <: MappedArray{RGB{Float64},2} @test img == convert.(RGB{Float64}, checkers) end end From adf9d3a119d2a9b46142a1a8f996c7d281781357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Baru=C4=8Di=C4=87?= Date: Mon, 9 Aug 2021 16:51:03 +0200 Subject: [PATCH 2/2] Add inference tests --- test/operations/tst_convert.jl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/operations/tst_convert.jl b/test/operations/tst_convert.jl index a5f1902b..f7bfbe24 100644 --- a/test/operations/tst_convert.jl +++ b/test/operations/tst_convert.jl @@ -108,4 +108,19 @@ @testset "permute" begin @test Augmentor.supports_permute(ConvertEltype) === false end + @testset "inference" begin + img = testpattern() + let pl = ConvertEltype(Gray) + aug_img = @inferred(augment(img, pl)) + @test eltype(aug_img) <: Gray + end + let pl = SplitChannels() |> ConvertEltype(Gray) + aug_img = @inferred(augment(img, pl)) + @test eltype(aug_img) <: Gray{N0f8} + end + let pl = ConvertEltype(Gray) |> SplitChannels() + aug_img = @inferred(augment(img, pl)) + @test eltype(aug_img) <: N0f8 + end + end end