Skip to content

Commit

Permalink
Fix depwarn on 0.6 due to vectorized functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Sep 9, 2016
1 parent e843952 commit f9cbb52
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 67 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ SIUnits
Zlib
Graphics 0.1
FileIO
Compat 0.8.4
Compat 0.9.1
StatsBase # for histrange
54 changes: 28 additions & 26 deletions src/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ end
(./)(img1::AbstractImageDirect, img2::AbstractImageDirect) = shareproperties(img1, data(img1)./data(img2))
(./)(img::AbstractImageDirect, A::AbstractArray) = shareproperties(img, data(img)./A)
(.^)(img::AbstractImageDirect, p::Number) = shareproperties(img, data(img).^p)
sqrt(img::AbstractImageDirect) = shareproperties(img, sqrt(data(img)))
atan2(img1::AbstractImageDirect, img2::AbstractImageDirect) = shareproperties(img1, atan2(data(img1),data(img2)))
hypot(img1::AbstractImageDirect, img2::AbstractImageDirect) = shareproperties(img1, hypot(data(img1),data(img2)))
real(img::AbstractImageDirect) = shareproperties(img,real(data(img)))
imag(img::AbstractImageDirect) = shareproperties(img,imag(data(img)))
abs(img::AbstractImageDirect) = shareproperties(img,abs(data(img)))

@vectorize_2arg Gray atan2
@vectorize_2arg Gray hypot
sqrt(img::AbstractImageDirect) = @compat shareproperties(img, sqrt.(data(img)))
atan2(img1::AbstractImageDirect, img2::AbstractImageDirect) =
@compat shareproperties(img1, atan2.(data(img1),data(img2)))
hypot(img1::AbstractImageDirect, img2::AbstractImageDirect) =
@compat shareproperties(img1, hypot.(data(img1),data(img2)))
real(img::AbstractImageDirect) = @compat shareproperties(img,real.(data(img)))
imag(img::AbstractImageDirect) = @compat shareproperties(img,imag.(data(img)))
abs(img::AbstractImageDirect) = @compat shareproperties(img,abs.(data(img)))

Compat.@dep_vectorize_2arg Gray atan2
Compat.@dep_vectorize_2arg Gray hypot

function sum(img::AbstractImageDirect, region::Union{AbstractVector,Tuple,Integer})
f = prod(size(img)[[region...]])
Expand Down Expand Up @@ -133,15 +135,15 @@ end

# Entropy for grayscale (intensity) images
function _log(kind::Symbol)
if kind == :shannon
x -> log2(x)
elseif kind == :nat
x -> log(x)
elseif kind == :hartley
x -> log10(x)
else
throw(ArgumentError("Invalid entropy unit. (:shannon, :nat or :hartley)"))
end
@compat if kind == :shannon
x -> log2.(x)
elseif kind == :nat
x -> log.(x)
elseif kind == :hartley
x -> log10.(x)
else
throw(ArgumentError("Invalid entropy unit. (:shannon, :nat or :hartley)"))
end
end

"""
Expand All @@ -160,9 +162,9 @@ function entropy(img::AbstractArray; kind=:shannon)
logp = logᵦ(p)

# take care of empty bins
logp[isinf(logp)] = 0
logp[Bool[isinf(v) for v in logp]] = 0

-sum(p.*logp)
-sum(p .* logp)
end

function entropy(img::AbstractArray{Bool}; kind=:shannon)
Expand Down Expand Up @@ -849,7 +851,7 @@ function imfilter_gaussian{T<:AbstractFloat}(img::AbstractArray{T}, sigma::Vecto
return img
end
A = copy(data(img))
nanflag = isnan(A)
nanflag = @compat isnan.(A)
hasnans = any(nanflag)
if hasnans
A[nanflag] = zero(T)
Expand Down Expand Up @@ -1188,14 +1190,14 @@ function padindexes{T,n}(img::AbstractArray{T,n}, dim, prepad, postpad, border::
M = size(img, dim)
I = Array(Int, M + prepad + postpad)
I = [(1 - prepad):(M + postpad);]
if border == "replicate"
I = min(max(I, 1), M)
@compat if border == "replicate"
I = min.(max.(I, 1), M)
elseif border == "circular"
I = 1 .+ mod(I .- 1, M)
I = 1 .+ mod.(I .- 1, M)
elseif border == "symmetric"
I = [1:M; M:-1:1][1 .+ mod(I .- 1, 2 * M)]
I = [1:M; M:-1:1][1 .+ mod.(I .- 1, 2 * M)]
elseif border == "reflect"
I = [1:M; M-1:-1:2][1 .+ mod(I .- 1, 2 * M - 2)]
I = [1:M; M-1:-1:2][1 .+ mod.(I .- 1, 2 * M - 2)]
else
error("unknown border condition")
end
Expand Down
2 changes: 1 addition & 1 deletion src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ pixelspacing{T}(img::AbstractImage{T}) = @get img "pixelspacing" _pixelspacing(i
function _pixelspacing(img::AbstractImage)
if haskey(img, "spacedirections")
sd = img["spacedirections"]
return [maximum(abs(sd[i])) for i = 1:length(sd)]
return [maximum(@compat abs.(sd[i])) for i = 1:length(sd)]
end
ones(sdims(img))
end
Expand Down
13 changes: 7 additions & 6 deletions src/edge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ Equivalent to ``sqrt(grad_x.^2 + grad_y.^2)``.
Returns a magnitude image the same size as `grad_x` and `grad_y`.
"""
magnitude(grad_x::AbstractArray, grad_y::AbstractArray) = hypot(grad_x, grad_y)
magnitude(grad_x::AbstractArray, grad_y::AbstractArray) =
@compat hypot.(grad_x, grad_y)

# Phase (angle of steepest gradient ascent), calculated from X and Y gradient images
"""
Expand Down Expand Up @@ -381,13 +382,13 @@ function _calc_discrete_offsets(θ, radius, transposed)
# x and y offset of points at specified radius and angles
# from each reference position.

if transposed
@compat if transposed
# θ′ = -π/2 - θ
xoffs = [CoordOffset(-x) for x in radius*sin(angles)]
yoffs = [CoordOffset( y) for y in radius*cos(angles)]
xoffs = [CoordOffset(-x) for x in radius * sin.(angles)]
yoffs = [CoordOffset( y) for y in radius * cos.(angles)]
else
xoffs = [CoordOffset( x) for x in radius*cos(angles)]
yoffs = [CoordOffset(-y) for y in radius*sin(angles)]
xoffs = [CoordOffset( x) for x in radius * cos.(angles)]
yoffs = [CoordOffset(-y) for y in radius * sin.(angles)]
end

return θ, xoffs, yoffs
Expand Down
4 changes: 2 additions & 2 deletions src/exposure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ function _histmatch(img::AbstractArray, oedges::Range, ohist::AbstractArray{Int}
ocdf = cumsum(ohist)
norm_ocdf = ocdf / ocdf[end]
lookup_table = zeros(Int, length(norm_cdf))
for I in eachindex(cdf)
lookup_table[I] = indmin(abs(norm_ocdf - norm_cdf[I]))
@compat for I in eachindex(cdf)
lookup_table[I] = indmin(abs.(norm_ocdf .- norm_cdf[I]))
end
hist_matched_img = similar(img)
for I in eachindex(img)
Expand Down
36 changes: 18 additions & 18 deletions test/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ srand(1234)

facts("Algorithms") do
# Comparison of each element in arrays with a scalar
approx_equal(ar, v) = all(abs(ar.-v) .< sqrt(eps(v)))
approx_equal(ar, v) = @compat all(abs.(ar.-v) .< sqrt(eps(v)))
approx_equal(ar::Images.AbstractImage, v) = approx_equal(Images.data(ar), v)

context("Flip dimensions") do
Expand Down Expand Up @@ -213,10 +213,10 @@ facts("Algorithms") do

for p in pyramid
h, w = size(p)
@fact all([isapprox(v, 0, atol = 0.06) for v in p[1, :]]) --> true
@fact all([isapprox(v, 0, atol = 0.06) for v in p[:, 1]]) --> true
@fact all([isapprox(v, 0, atol = 0.06) for v in p[h, :]]) --> true
@fact all([isapprox(v, 0, atol = 0.06) for v in p[:, w]]) --> true
@fact all(Bool[isapprox(v, 0, atol = 0.06) for v in p[1, :]]) --> true
@fact all(Bool[isapprox(v, 0, atol = 0.06) for v in p[:, 1]]) --> true
@fact all(Bool[isapprox(v, 0, atol = 0.06) for v in p[h, :]]) --> true
@fact all(Bool[isapprox(v, 0, atol = 0.06) for v in p[:, w]]) --> true
end
end

Expand Down Expand Up @@ -268,18 +268,18 @@ facts("Algorithms") do
@fact isa(padarray(B, ones(Int,3), ones(Int,3), "replicate"), BitArray{3}) --> true
end

context("Filtering") do
@compat context("Filtering") do
EPS = 1e-14
imgcol = Images.colorim(rand(3,5,6))
imgcolf = convert(Images.Image{RGB{UFixed8}}, imgcol)
for T in (Float64, Int)
A = zeros(T,3,3); A[2,2] = 1
kern = rand(3,3)
@fact maximum(abs(Images.imfilter(A, kern) - rot180(kern))) --> less_than(EPS)
@fact maximum(abs.(Images.imfilter(A, kern) - rot180(kern))) --> less_than(EPS)
kern = rand(2,3)
@fact maximum(abs(Images.imfilter(A, kern)[1:2,:] - rot180(kern))) --> less_than(EPS)
@fact maximum(abs.(Images.imfilter(A, kern)[1:2,:] - rot180(kern))) --> less_than(EPS)
kern = rand(3,2)
@fact maximum(abs(Images.imfilter(A, kern)[:,1:2] - rot180(kern))) --> less_than(EPS)
@fact maximum(abs.(Images.imfilter(A, kern)[:,1:2] - rot180(kern))) --> less_than(EPS)
end
kern = zeros(3,3); kern[2,2] = 1
@fact maximum(map(abs, imgcol - Images.imfilter(imgcol, kern))) --> less_than(EPS)
Expand All @@ -288,19 +288,19 @@ facts("Algorithms") do
# Separable kernels
A = zeros(T,3,3); A[2,2] = 1
kern = rand(3).*rand(3)'
@fact maximum(abs(Images.imfilter(A, kern) - rot180(kern))) --> less_than(EPS)
@fact maximum(abs.(Images.imfilter(A, kern) - rot180(kern))) --> less_than(EPS)
kern = rand(2).*rand(3)'
@fact maximum(abs(Images.imfilter(A, kern)[1:2,:] - rot180(kern))) --> less_than(EPS)
@fact maximum(abs.(Images.imfilter(A, kern)[1:2,:] - rot180(kern))) --> less_than(EPS)
kern = rand(3).*rand(2)'
@fact maximum(abs(Images.imfilter(A, kern)[:,1:2] - rot180(kern))) --> less_than(EPS)
@fact maximum(abs.(Images.imfilter(A, kern)[:,1:2] - rot180(kern))) --> less_than(EPS)
end
A = zeros(3,3); A[2,2] = 1
kern = rand(3,3)
@fact maximum(abs(Images.imfilter_fft(A, kern) - rot180(kern))) --> less_than(EPS)
@fact maximum(abs.(Images.imfilter_fft(A, kern) - rot180(kern))) --> less_than(EPS)
kern = rand(2,3)
@fact maximum(abs(Images.imfilter_fft(A, kern)[1:2,:] - rot180(kern))) --> less_than(EPS)
@fact maximum(abs.(Images.imfilter_fft(A, kern)[1:2,:] - rot180(kern))) --> less_than(EPS)
kern = rand(3,2)
@fact maximum(abs(Images.imfilter_fft(A, kern)[:,1:2] - rot180(kern))) --> less_than(EPS)
@fact maximum(abs.(Images.imfilter_fft(A, kern)[:,1:2] - rot180(kern))) --> less_than(EPS)
kern = zeros(3,3); kern[2,2] = 1
@fact maximum(map(abs, imgcol - Images.imfilter_fft(imgcol, kern))) --> less_than(EPS)
@fact maximum(map(abs, imgcolf - Images.imfilter_fft(imgcolf, kern))) --> less_than(EPS)
Expand Down Expand Up @@ -329,7 +329,7 @@ facts("Algorithms") do
@fact Af --> roughly(Afft)
h = [0.24,0.87]
hfft = Images.imfilter_fft(eye(3), h, "inner")
hfft[abs(hfft) .< 3eps()] = 0
hfft[abs.(hfft) .< 3eps()] = 0
@fact Images.imfilter(eye(3), h, "inner") --> roughly(hfft) # issue #204

# circular
Expand All @@ -356,7 +356,7 @@ facts("Algorithms") do
@fact Images.imfilter_gaussian(A, [0,0]) --> exactly(A)
@test_approx_eq Images.imfilter_gaussian(A, [0,3]) A
B = copy(A)
B[isfinite(B)] = 2
B[isfinite.(B)] = 2
@test_approx_eq Images.imfilter_gaussian(A, [10^3,0]) B
@fact maximum(map(abs, Images.imfilter_gaussian(imgcol, [10^3,10^3]) - mean(imgcol))) --> less_than(1e-4)
@fact maximum(map(abs, Images.imfilter_gaussian(imgcolf, [10^3,10^3]) - mean(imgcolf))) --> less_than(1e-4)
Expand All @@ -370,7 +370,7 @@ facts("Algorithms") do
@fact reinterpret(Float64, Images.data(imgf)) --> roughly(Images.imfilter_gaussian(A, [0,2,2]))

A = zeros(Int, 9, 9); A[5, 5] = 1
@fact maximum(abs(Images.imfilter_LoG(A, [1,1]) - Images.imlog(1.0))) --> less_than(EPS)
@fact maximum(abs.(Images.imfilter_LoG(A, [1,1]) - Images.imlog(1.0))) --> less_than(EPS)
@fact maximum(Images.imfilter_LoG([0 0 0 0 1 0 0 0 0], [1,1]) - sum(Images.imlog(1.0),1)) --> less_than(EPS)
@fact maximum(Images.imfilter_LoG([0 0 0 0 1 0 0 0 0]', [1,1]) - sum(Images.imlog(1.0),2)) --> less_than(EPS)

Expand Down
33 changes: 21 additions & 12 deletions test/edge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ facts("Edge") do
#General Checks
img = zeros(10, 10)
edges = canny(img)
@fact eltype(edges.data) --> Gray{U8}
@fact eltype(edges) --> Gray{U8}
@fact all(edges .== 0.0) --> true

#Box Edges
Expand Down Expand Up @@ -116,7 +116,7 @@ facts("Edge") do
cb_image_rgb = convert(Image{RGB}, cb_image_xy)
cb_image_rgb2 = convert(Image{RGB{Float64}}, cb_image_xy)

context("Checkerboard") do
@compat context("Checkerboard") do
for method in ["sobel", "prewitt", "ando3", "ando4", "ando5", "ando4_sep", "ando5_sep"]
## Checkerboard array

Expand All @@ -138,7 +138,8 @@ facts("Edge") do

# Test that orientation is perpendicular to gradient
aorient = orientation(agx, agy)
@fact all((cos(agphase).*cos(aorient) .+ sin(agphase).*sin(aorient) .< EPS) |
@fact all((cos.(agphase) .* cos.(aorient) .+
sin.(agphase) .* sin.(aorient) .< EPS) |
((agphase .== 0.0) & (aorient .== 0.0))) --> true
# this part is where both are zero because there is no gradient

Expand All @@ -161,7 +162,8 @@ facts("Edge") do

# Test that orientation is perpendicular to gradient
orient = orientation(gx, gy)
@fact all((cos(gphase).*cos(orient) .+ sin(gphase).*sin(orient) .< EPS) |
@fact all((cos.(gphase) .* cos.(orient) .+
sin.(gphase) .* sin.(orient) .< EPS) |
((gphase .== 0.0) & (orient .== 0.0))) --> true
# this part is where both are zero because there is no gradient

Expand All @@ -185,7 +187,8 @@ facts("Edge") do

# Test that orientation is perpendicular to gradient
orient = orientation(gx, gy)
@fact all((cos(gphase).*cos(orient) .+ sin(gphase).*sin(orient) .< EPS) |
@fact all((cos.(gphase) .* cos.(orient) .+
sin.(gphase) .* sin.(orient) .< EPS) |
((gphase .== 0.0) & (orient .== 0.0))) --> true
# this part is where both are zero because there is no gradient

Expand All @@ -208,7 +211,8 @@ facts("Edge") do

# Test that orientation is perpendicular to gradient
orientg = orientation(gxg, gyg)
@fact all((cos(gphaseg).*cos(orientg) .+ sin(gphaseg).*sin(orientg) .< EPS) |
@fact all((cos.(gphaseg) .* cos.(orientg) .+
sin.(gphaseg) .* sin.(orientg) .< EPS) |
((gphaseg .== 0.0) & (orientg .== 0.0))) --> true
# this part is where both are zero because there is no gradient

Expand All @@ -231,7 +235,8 @@ facts("Edge") do

# Test that orientation is perpendicular to gradient
orient_rgb = orientation(gx_rgb, gy_rgb)
@fact all((cos(gphase_rgb).*cos(orient_rgb) .+ sin(gphase_rgb).*sin(orient_rgb) .< EPS) |
@fact all((cos.(gphase_rgb) .* cos.(orient_rgb) .+
sin.(gphase_rgb) .* sin.(orient_rgb) .< EPS) |
((gphase_rgb .== 0.0) & (orient_rgb .== 0.0))) --> true
# this part is where both are zero because there is no gradient

Expand All @@ -254,13 +259,14 @@ facts("Edge") do

# Test that orientation is perpendicular to gradient
orient_rgb = orientation(gx_rgb, gy_rgb)
@fact all((cos(gphase_rgb).*cos(orient_rgb) .+ sin(gphase_rgb).*sin(orient_rgb) .< EPS) |
@fact all((cos.(gphase_rgb) .* cos.(orient_rgb) .+
sin.(gphase_rgb) .* sin.(orient_rgb) .< EPS) |
((gphase_rgb .== 0.0) & (orient_rgb .== 0.0))) --> true
# this part is where both are zero because there is no gradient
end
end

context("Diagonals") do
@compat context("Diagonals") do
# Create an image with white along diagonals -2:2 and black elsewhere
m = zeros(UInt8, 20,20)
for i = -2:2; m[diagind(m,i)] = 0xff; end
Expand Down Expand Up @@ -288,7 +294,8 @@ facts("Edge") do

# Test that orientation is perpendicular to gradient
aorient = orientation(agx, agy)
@fact all((cos(agphase).*cos(aorient) .+ sin(agphase).*sin(aorient) .< EPS) |
@fact all((cos.(agphase) .* cos.(aorient) .+
sin.(agphase) .* sin.(aorient) .< EPS) |
((agphase .== 0.0) & (aorient .== 0.0))) --> true
# this part is where both are zero because there is no gradient

Expand All @@ -310,7 +317,8 @@ facts("Edge") do

# Test that orientation is perpendicular to gradient
orient = orientation(gx, gy)
@fact all((cos(gphase).*cos(orient) .+ sin(gphase).*sin(orient) .< EPS) |
@fact all((cos.(gphase) .* cos.(orient) .+
sin.(gphase) .* sin.(orient) .< EPS) |
((gphase .== 0.0) & (orient .== 0.0))) --> true
# this part is where both are zero because there is no gradient

Expand All @@ -332,7 +340,8 @@ facts("Edge") do

# Test that orientation is perpendicular to gradient
orient = orientation(gx, gy)
@fact all((cos(gphase).*cos(orient) .+ sin(gphase).*sin(orient) .< EPS) |
@fact all((cos.(gphase) .* cos.(orient) .+
sin.(gphase) .* sin.(orient) .< EPS) |
((gphase .== 0.0) & (orient .== 0.0))) --> true
# this part is where both are zero because there is no gradient
end
Expand Down
2 changes: 1 addition & 1 deletion test/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ facts("Map") do
context("ScaleAutoMinMax") do
mapi = ScaleAutoMinMax()
A = [100,550,1000]
@chk map(mapi, A) ufixed8([0.0,0.5,1.0])
@chk map(mapi, A) @compat ufixed8.([0.0,0.5,1.0])
mapi = ScaleAutoMinMax(RGB24)
@chk map(mapi, A) RGB24[0x00000000, 0x00808080, 0x00ffffff]

Expand Down

0 comments on commit f9cbb52

Please sign in to comment.