Skip to content

Commit

Permalink
Fix many problems on julia 0.6
Browse files Browse the repository at this point in the history
- typealias, abstract, and inner constructor depwarns
- change in NTuple{N} specificity
- deprecation of .+ as separate functions for broadcasting

Also runs ambiguity tests only on 0.5
  • Loading branch information
timholy committed Feb 18, 2017
1 parent 06624f0 commit b1d4df2
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 42 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ FFTViews
ComputationalResources
DataStructures 0.4.6
TiledIteration
Compat 0.18
30 changes: 16 additions & 14 deletions src/ImageFiltering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ module ImageFiltering

using Colors, FixedPointNumbers, ImageCore, MappedArrays, FFTViews, OffsetArrays, StaticArrays, ComputationalResources, TiledIteration
using ColorVectorSpace # for filtering RGB arrays
using Compat
using Base: Indices, tail, fill_to_length, @pure, depwarn

export Kernel, KernelFactors, Pad, Fill, Inner, NA, NoPad, Algorithm, imfilter, imfilter!, mapwindow, imgradients, padarray, centered, kernelfactors, reflect

typealias FixedColorant{T<:Normed} Colorant{T}
typealias StaticOffsetArray{T,N,A<:StaticArray} OffsetArray{T,N,A}
typealias OffsetVector{T} OffsetArray{T,1}
@compat FixedColorant{T<:Normed} = Colorant{T}
@compat StaticOffsetArray{T,N,A<:StaticArray} = OffsetArray{T,N,A}
@compat OffsetVector{T} = OffsetArray{T,1}

# Needed for type-stability
function Base.transpose{T}(A::StaticOffsetArray{T,2})
Expand All @@ -21,7 +22,8 @@ end
module Algorithm
# deliberately don't export these, but it's expected that they
# will be used as Algorithm.FFT(), etc.
abstract Alg
using Compat
@compat abstract type Alg end
"Filter using the Fast Fourier Transform" immutable FFT <: Alg end
"Filter using a direct algorithm" immutable FIR <: Alg end
"Cache-efficient filtering using tiles" immutable FIRTiled{N} <: Alg
Expand All @@ -40,27 +42,27 @@ include("utils.jl")
include("kernelfactors.jl")
using .KernelFactors: TriggsSdika, IIRFilter, ReshapedOneD, iterdims, kernelfactors

typealias ReshapedVector{T,N,Npre,V<:AbstractVector} ReshapedOneD{T,N,Npre,V}
typealias ArrayType{T} Union{AbstractArray{T}, ReshapedVector{T}}
typealias ReshapedIIR{T,N,Npre,V<:IIRFilter} ReshapedOneD{T,N,Npre,V}
typealias AnyIIR{T} Union{IIRFilter{T}, ReshapedIIR{T}}
typealias ArrayLike{T} Union{ArrayType{T}, AnyIIR{T}}
@compat ReshapedVector{T,N,Npre,V<:AbstractVector} = ReshapedOneD{T,N,Npre,V}
@compat ArrayType{T} = Union{AbstractArray{T}, ReshapedVector{T}}
@compat ReshapedIIR{T,N,Npre,V<:IIRFilter} = ReshapedOneD{T,N,Npre,V}
@compat AnyIIR{T} = Union{IIRFilter{T}, ReshapedIIR{T}}
@compat ArrayLike{T} = Union{ArrayType{T}, AnyIIR{T}}

include("kernel.jl")
using .Kernel
using .Kernel: Laplacian, reflect

typealias NDimKernel{N,K} Union{AbstractArray{K,N},ReshapedOneD{K,N},Laplacian{N}}
@compat NDimKernel{N,K} = Union{AbstractArray{K,N},ReshapedOneD{K,N},Laplacian{N}}

include("border.jl")

typealias BorderSpec{T} Union{Pad{0}, Fill{T,0}, Inner{0}}
typealias BorderSpecNoNa{T} Union{Pad{0}, Fill{T,0}, Inner{0}}
typealias BorderSpecAny Union{BorderSpec,NA,NoPad}
@compat BorderSpec{T} = Union{Pad{0}, Fill{T,0}, Inner{0}}
@compat BorderSpecNoNa{T} = Union{Pad{0}, Fill{T,0}, Inner{0}}
const BorderSpecAny = Union{BorderSpec,NA,NoPad}

include("deprecated.jl")

typealias ProcessedKernel Tuple
const ProcessedKernel = Tuple

include("imfilter.jl")
include("specialty.jl")
Expand Down
6 changes: 3 additions & 3 deletions src/border.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using OffsetArrays, CatIndices

abstract AbstractBorder
@compat abstract type AbstractBorder end

immutable NoPad{T} <: AbstractBorder
border::T
Expand Down Expand Up @@ -249,8 +249,8 @@ immutable Fill{T,N} <: AbstractBorder
lo::Dims{N}
hi::Dims{N}

Fill(value::T) = new(value)
Fill(value::T, lo::Dims{N}, hi::Dims{N}) = new(value, lo, hi)
(::Type{Fill{T,N}}){T,N}(value::T) = new{T,N}(value)
(::Type{Fill{T,N}}){T,N}(value::T, lo::Dims{N}, hi::Dims{N}) = new{T,N}(value, lo, hi)
end

Fill{T}(value::T) = Fill{T,0}(value)
Expand Down
2 changes: 1 addition & 1 deletion src/imfilter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ function normalize_separable!{N}(r::AbstractResource, A, kernels::NTuple{N,Resha
normalize_separable!(r, A, map(_vec, kernels), border)
end

function normalize_separable!{N}(r::AbstractResource, A, kernels::NTuple{N}, border)
function normalize_separable!{N}(r::AbstractResource, A, kernels::NTuple{N,Any}, border)
inds = indices(A)
filtdims = ntuple(d->imfilter(r, similar(dims->ones(dims), inds[d]), _vec(kernels[d]), border), Val{N})
normalize_dims!(A, filtdims)
Expand Down
4 changes: 2 additions & 2 deletions src/kernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ immutable Laplacian{N}
flags::NTuple{N,Bool}
offsets::Vector{CartesianIndex{N}}

function Laplacian(flags::NTuple{N,Bool})
function (::Type{Laplacian{N}}){N}(flags::NTuple{N,Bool})
offsets = Array{CartesianIndex{N}}(0)
for i = 1:N
if flags[i]
push!(offsets,
CartesianIndex{N}((ntuple(d->0, i-1)..., 1, ntuple(d->0, N-i)...)))
end
end
new(flags, offsets)
new{N}(flags, offsets)
end
end

Expand Down
43 changes: 29 additions & 14 deletions src/kernelfactors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ using ..ImageFiltering: centered, dummyind
import ..ImageFiltering: _reshape, _vec, nextendeddims
using Base: tail, Indices, @pure, checkbounds_indices, throw_boundserror

abstract IIRFilter{T}
using Compat

@compat abstract type IIRFilter{T} end

Base.eltype{T}(kernel::IIRFilter{T}) = T

Expand All @@ -26,9 +28,9 @@ ReshapedOneDs allow one to specify a "filtering dimension" for a
immutable ReshapedOneD{T,N,Npre,V} # not <: AbstractArray{T,N} (more general, incl. IIR)
data::V

function ReshapedOneD(data::V)
function (::Type{ReshapedOneD{T,N,Npre,V}}){T,N,Npre,V}(data::V)
ndims(V) == 1 || throw(DimensionMismatch("must be one dimensional, got $(ndims(V))"))
new(data)
new{T,N,Npre,V}(data)
end
end

Expand Down Expand Up @@ -73,17 +75,29 @@ import Base: ==
==(A::ReshapedOneD, B::AbstractArray) = convert(AbstractArray, A) == B
==(A::AbstractArray, B::ReshapedOneD) = A == convert(AbstractArray, B)

import Base: .+, .-, .*, ./
for op in (:+, :-, :*, :/)
opdot = Symbol(:., op)
@eval begin
($opdot)(A::ReshapedOneD, B::ReshapedOneD) = broadcast($op, A, B)
@inline ($opdot)(As::ReshapedOneD...) = broadcast($op, As...)
if VERSION < v"0.6.0-dev.1839"
import Base: .+, .-, .*, ./
for op in (:+, :-, :*, :/)
opdot = Symbol(:., op)
@eval begin
($opdot)(A::ReshapedOneD, B::ReshapedOneD) = broadcast($op, A, B)
@inline ($opdot)(As::ReshapedOneD...) = broadcast($op, As...)
end
end
# for broadcasting
@pure Base.promote_eltype_op{S,T}(op, ::ReshapedOneD{S}, ::ReshapedOneD{T}) = Base.promote_op(op, S, T)
@pure Base.promote_eltype_op{S,T}(op, ::Type{S}, ::ReshapedOneD{T}) = Base.promote_op(op, S, T)
else
import Base: +, -, *, /
for op in (:+, :-, :*, :/)
@eval begin
($op)(A::ReshapedOneD, B::ReshapedOneD) =
broadcast($op, convert(AbstractArray, A), convert(AbstractArray, B))
@inline ($op)(As::ReshapedOneD...) =
broadcast($op, map(A->convert(AbstractArray, A), As)...)
end
end
end
# for broadcasting
@pure Base.promote_eltype_op{S,T}(op, ::ReshapedOneD{S}, ::ReshapedOneD{T}) = Base.promote_op(op, S, T)
@pure Base.promote_eltype_op{S,T}(op, ::Type{S}, ::ReshapedOneD{T}) = Base.promote_op(op, S, T)
if VERSION >= v"0.6.0-dev.693"
Base.Broadcast.containertype{T<:ReshapedOneD}(::Type{T}) = Array
end
Expand Down Expand Up @@ -298,7 +312,8 @@ immutable TriggsSdika{T,k,l,L} <: IIRFilter{T}
asum::T
bsum::T

TriggsSdika(a, b, scale, M) = new(a, b, scale, M, sum(a), sum(b))
(::Type{TriggsSdika{T,k,l,L}}){T,k,l,L}(a, b, scale, M) =
new{T,k,l,L}(a, b, scale, M, sum(a), sum(b))
end
"""
TriggsSdika(a, b, scale, M)
Expand Down Expand Up @@ -414,7 +429,7 @@ appropriately along its "leading" dimensions; the dimensionality of each is
"""
kernelfactors{N}(factors::NTuple{N,AbstractVector}) = _kernelfactors((), ntuple(d->true,Val{N}), (), factors)

_kernelfactors(pre, post, out::NTuple, ::Tuple{}) = out
_kernelfactors(pre, post, out, ::Tuple{}) = out
@inline function _kernelfactors(pre, post, out, factors)
# L+M=N
f = factors[1]
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ to_ranges(R::CartesianRange) = map((b,e)->b:e, R.start.I, R.stop.I)
# ensure that overflow is detected, by ensuring that it doesn't happen
# at intermediate stages of the computation
accumfilter(pixelval, filterval) = pixelval * filterval
typealias SmallInts Union{UInt8,Int8,UInt16,Int16}
const SmallInts = Union{UInt8,Int8,UInt16,Int16}
accumfilter(pixelval::SmallInts, filterval::SmallInts) = Int(pixelval)*Int(filterval)
# advice: don't use FixedPoint for the kernel
accumfilter(pixelval::N0f8, filterval::N0f8) = Float32(pixelval)*Float32(filterval)
Expand Down
10 changes: 6 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using ImageFiltering, Base.Test

aif = detect_ambiguities(ImageFiltering, Kernel, KernelFactors, Base)
# Because StaticArrays has ambiguities with Base, we have to "subtract" these
asa = detect_ambiguities(StaticArrays, Base)
@test isempty(setdiff(aif, asa))
if VERSION < v"0.6.0-dev"
aif = detect_ambiguities(ImageFiltering, Kernel, KernelFactors, Base)
# Because StaticArrays has ambiguities with Base, we have to "subtract" these
asa = detect_ambiguities(StaticArrays, Base)
@test isempty(setdiff(aif, asa))
end

include("border.jl")
include("nd.jl")
Expand Down
6 changes: 3 additions & 3 deletions test/triggs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using Base.Test
for (i,c) in enumerate(x)
a = zeros(l)
a[c] = 1
af = exp(-((1:l)-c).^2/(2*σ^2))/*√(2π))
af = exp.(-((1:l)-c).^2/(2*σ^2))/*√(2π))
@inferred(imfilter!(a, a, (kernel,), Fill(0)))
@test norm(a-af) < 0.1*norm(af)
# aσ[i][:,2n-1:2n] = [af a]
Expand All @@ -46,11 +46,11 @@ using Base.Test
y = (-3:3)'
kernel = KernelFactors.IIRGaussian((σ, σ))
for img in (imgf, imgg, imgc)
imgcmp = img[3,4]*exp(-(x.^2 .+ y.^2)/(2*σ^2))/^2*2*pi)
imgcmp = img[3,4]*exp.(-(x.^2 .+ y.^2)/(2*σ^2))/^2*2*pi)
border = Fill(zero(eltype(img)))
img0 = copy(img)
imgfilt = @inferred(imfilter(img, kernel, border))
@test sumabs2(imgcmp - imgfilt) < 0.2^2*sumabs2(imgcmp)
@test sum(abs2, imgcmp - imgfilt) < 0.2^2*sum(abs2, imgcmp)
@test img == img0
@test imgfilt != img
end
Expand Down

0 comments on commit b1d4df2

Please sign in to comment.