Skip to content

Commit

Permalink
Fixes for julia 0.7 (#218)
Browse files Browse the repository at this point in the history
* Fixes for julia 0.7

except for the IO part, whose tests are disabled

* Simplify inference bug workaround

And limit its applicability range to before the fix.
Credit to @RalphAS, this is basically #219.

* Require AxisAlgorithms 0.3.0

* Change WebIO->Interpolations in travis script

* Add an at-static annotation
  • Loading branch information
carlobaldassi authored and sglyon committed Aug 2, 2018
1 parent b6ee0f3 commit 9c380b0
Show file tree
Hide file tree
Showing 51 changed files with 299 additions and 214 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ language: julia
sudo: false
julia:
- 0.6
- 0.7
- nightly
matrix:
allow_failures:
- julia: nightly
after_success:
# push coverage results to Coveralls
- julia -e 'cd(Pkg.dir("WebIO")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
- julia -e 'cd(Pkg.dir("Interpolations")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
# push coverage results to Codecov
- julia -e 'cd(Pkg.dir("WebIO")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
- julia -e 'cd(Pkg.dir("Interpolations")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
5 changes: 3 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ julia 0.6
ShowItLikeYouBuildIt
WoodburyMatrices 0.1.5
Ratios
AxisAlgorithms
Compat 0.49
AxisAlgorithms 0.3.0
OffsetArrays
Compat 0.59
17 changes: 9 additions & 8 deletions src/Interpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ export
# scaling/scaling.jl

using Compat
using Compat.LinearAlgebra
using WoodburyMatrices, Ratios, AxisAlgorithms
using Compat.LinearAlgebra, Compat.SparseArrays
using WoodburyMatrices, Ratios, AxisAlgorithms, OffsetArrays

import Base: convert, size, indices, getindex, gradient, promote_rule,
import Base: convert, size, getindex, promote_rule,
ndims, eltype, checkbounds

# Julia v0.5 compatibility
if isdefined(:scaling) import Base.scaling end
if isdefined(:scale) import Base.scale end
if !isdefined(Base, :oneunit)
const oneunit = one
@static if VERSION < v"0.7.0-DEV.3449"
import Base: gradient
else
import LinearAlgebra: gradient
end

import Compat: axes

abstract type Flag end
abstract type InterpolationType <: Flag end
struct NoInterp <: InterpolationType end
Expand Down
26 changes: 16 additions & 10 deletions src/b-splines/b-splines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ struct BSplineInterpolation{T,N,TCoefs<:AbstractArray,IT<:DimSpec{BSpline},GT<:D
coefs::TCoefs
end
function BSplineInterpolation(::Type{TWeights}, A::AbstractArray{Tel,N}, ::IT, ::GT, ::Val{pad}) where {N,Tel,TWeights<:Real,IT<:DimSpec{BSpline},GT<:DimSpec{GridType},pad}
isleaftype(IT) || error("The b-spline type must be a leaf type (was $IT)")
isleaftype(typeof(A)) || warn("For performance reasons, consider using an array of a concrete type (typeof(A) == $(typeof(A)))")
isconcretetype(IT) || error("The b-spline type must be a leaf type (was $IT)")
isconcretetype(typeof(A)) || warn("For performance reasons, consider using an array of a concrete type (typeof(A) == $(typeof(A)))")

c = zero(TWeights)
for _ in 2:N
Expand All @@ -42,13 +42,13 @@ padextract(pad::Integer, d) = pad
padextract(pad::Tuple{Vararg{Integer}}, d) = pad[d]

lbound(itp::BSplineInterpolation{T,N,TCoefs,IT,OnGrid}, d::Integer) where {T,N,TCoefs,IT} =
first(indices(itp, d))
first(axes(itp, d))
ubound(itp::BSplineInterpolation{T,N,TCoefs,IT,OnGrid}, d::Integer) where {T,N,TCoefs,IT} =
last(indices(itp, d))
last(axes(itp, d))
lbound(itp::BSplineInterpolation{T,N,TCoefs,IT,OnCell}, d::Integer) where {T,N,TCoefs,IT} =
first(indices(itp, d)) - 0.5
first(axes(itp, d)) - 0.5
ubound(itp::BSplineInterpolation{T,N,TCoefs,IT,OnCell}, d::Integer) where {T,N,TCoefs,IT} =
last(indices(itp, d))+0.5
last(axes(itp, d))+0.5

lbound(itp::BSplineInterpolation{T,N,TCoefs,IT,OnGrid}, d, inds) where {T,N,TCoefs,IT} =
first(inds)
Expand All @@ -65,11 +65,11 @@ function size(itp::BSplineInterpolation{T,N,TCoefs,IT,GT,pad}, d) where {T,N,TCo
d <= N ? size(itp.coefs, d) - 2*padextract(pad, d) : 1
end

@inline indices(itp::BSplineInterpolation{T,N,TCoefs,IT,GT,pad}) where {T,N,TCoefs,IT,GT,pad} =
indices_removepad.(indices(itp.coefs), pad)
@inline axes(itp::BSplineInterpolation{T,N,TCoefs,IT,GT,pad}) where {T,N,TCoefs,IT,GT,pad} =
indices_removepad.(axes(itp.coefs), pad)

function indices(itp::BSplineInterpolation{T,N,TCoefs,IT,GT,pad}, d) where {T,N,TCoefs,IT,GT,pad}
d <= N ? indices_removepad(indices(itp.coefs, d), padextract(pad, d)) : indices(itp.coefs, d)
function axes(itp::BSplineInterpolation{T,N,TCoefs,IT,GT,pad}, d) where {T,N,TCoefs,IT,GT,pad}
d <= N ? indices_removepad(axes(itp.coefs, d), padextract(pad, d)) : axes(itp.coefs, d)
end

function interpolate(::Type{TWeights}, ::Type{TC}, A, it::IT, gt::GT) where {TWeights,TC,IT<:DimSpec{BSpline},GT<:DimSpec{GridType}}
Expand Down Expand Up @@ -109,6 +109,12 @@ offsetsym(off, d) = off == -1 ? Symbol("ixm_", d) :
@inline indices_addpad(inds, pad) = oftype(inds, first(inds):last(inds) + 2*pad)
@inline indices_interior(inds, pad) = first(inds)+pad:last(inds)-pad

@static if VERSION < v"0.7.0-DEV.3449"
lut!(dl, d, du) = lufact!(Tridiagonal(dl, d, du), Val{false})
else
lut!(dl, d, du) = lu!(Tridiagonal(dl, d, du), Val(false))
end

include("constant.jl")
include("linear.jl")
include("quadratic.jl")
Expand Down
12 changes: 6 additions & 6 deletions src/b-splines/cubic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int,
# Now Woodbury correction to set `[1, 3], [n, n-2] ==> 1`
specs = WoodburyMatrices.sparse_factors(T, n, (1, 3, oneunit(T)), (n, n-2, oneunit(T)))

Woodbury(lufact!(Tridiagonal(dl, d, du), Val{false}), specs...), zeros(TC, n)
Woodbury(lut!(dl, d, du), specs...), zeros(TC, n)
end

"""
Expand Down Expand Up @@ -226,7 +226,7 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int,
(n, n-3, oneunit(T))
)

Woodbury(lufact!(Tridiagonal(dl, d, du), Val{false}), specs...), zeros(TC, n)
Woodbury(lut!(dl, d, du), specs...), zeros(TC, n)
end

"""
Expand Down Expand Up @@ -255,7 +255,7 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int,
(n, n-3, -oneunit(T))
)

Woodbury(lufact!(Tridiagonal(dl, d, du), Val{false}), specs...), zeros(TC, n)
Woodbury(lut!(dl, d, du), specs...), zeros(TC, n)
end

"""
Expand All @@ -277,7 +277,7 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int,
(n, n-2, oneunit(T)),
)

Woodbury(lufact!(Tridiagonal(dl, d, du), Val{false}), specs...), zeros(TC, n)
Woodbury(lut!(dl, d, du), specs...), zeros(TC, n)
end

"""
Expand All @@ -297,7 +297,7 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int,
(n, 1, dl[end])
)

Woodbury(lufact!(Tridiagonal(dl, d, du), Val{false}), specs...), zeros(TC, n)
Woodbury(lut!(dl, d, du), specs...), zeros(TC, n)
end

"""
Expand All @@ -316,5 +316,5 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int,
(n, 1, dl[end])
)

Woodbury(lufact!(Tridiagonal(dl, d, du), Val{false}), specs...), zeros(TC, n)
Woodbury(lut!(dl, d, du), specs...), zeros(TC, n)
end
21 changes: 14 additions & 7 deletions src/b-splines/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ function hessian_coefficients(::Type{IT}, N, dim1, dim2) where IT<:DimSpec{BSpli
Expr(:block, exs...)
end

index_gen(::Type{IT}, N::Integer, offsets...) where {IT} = index_gen(iextract(IT, min(length(offsets)+1, N)), IT, N, offsets...)
function index_gen(::Type{IT}, N::Integer, offsets...) where {IT}
idx = index_gen(iextract(IT, min(length(offsets)+1, N)), IT, N, offsets...)
@static if v"0.7-" VERSION < v"0.7.0-beta2.119"
# this is to avoid https://github.com/JuliaLang/julia/issues/27907
return convert(Union{Expr,Symbol}, idx)
end
return idx
end

function getindex_impl(itp::Type{BSplineInterpolation{T,N,TCoefs,IT,GT,Pad}}) where {T,N,TCoefs,IT<:DimSpec{BSpline},GT<:DimSpec{GridType},Pad}
meta = Expr(:meta, :inline)
quote
$meta
@nexprs $N d->(x_d = xs[d])
inds_itp = indices(itp)
inds_itp = axes(itp)

# Calculate the indices of all coefficients that will be used
# and define fx = x - xi in each dimension
Expand Down Expand Up @@ -60,7 +67,7 @@ function gradient_impl(itp::Type{BSplineInterpolation{T,N,TCoefs,IT,GT,Pad}}) wh
# For each component of the gradient, alternately calculate
# coefficients and set component
n = count_interp_dims(IT, N)
exs = Array{Expr, 1}(2n)
exs = Array{Expr, 1}(undef, 2n)
cntr = 0
for d = 1:N
if count_interp_dims(iextract(IT, d), 1) > 0
Expand All @@ -74,7 +81,7 @@ function gradient_impl(itp::Type{BSplineInterpolation{T,N,TCoefs,IT,GT,Pad}}) wh
$meta
length(g) == $n || throw(ArgumentError(string("The length of the provided gradient vector (", length(g), ") did not match the number of interpolating dimensions (", n, ")")))
@nexprs $N d->(x_d = xs[d])
inds_itp = indices(itp)
inds_itp = axes(itp)

# Calculate the indices of all coefficients that will be used
# and define fx = x - xi in each dimension
Expand Down Expand Up @@ -115,7 +122,7 @@ for R in [:Real, :Any]
xargs = [:(xs[$d]) for d in 1:length(xs)]
quote
Tg = $(Expr(:call, :promote_type, T, [x <: AbstractArray ? eltype(x) : x for x in xs]...))
gradient!(Array{Tg, 1}($n), itp, $(xargs...))
gradient!(Array{Tg, 1}(undef, $n), itp, $(xargs...))
end
end
end
Expand All @@ -142,7 +149,7 @@ function hessian_impl(itp::Type{BSplineInterpolation{T,N,TCoefs,IT,GT,Pad}}) whe
$meta
size(H) == ($n,$n) || throw(ArgumentError(string("The size of the provided Hessian matrix wasn't a square matrix of size ", size(H))))
@nexprs $N d->(x_d = xs[d])
inds_itp = indices(itp)
inds_itp = axes(itp)

$(define_indices(IT, N, Pad))

Expand All @@ -167,7 +174,7 @@ end
xargs = [:(xs[$d]) for d in 1:length(xs)]
quote
TH = $(Expr(:call, :promote_type, T, [x <: AbstractArray ? eltype(x) : x for x in xs]...))
hessian!(Array{TH, 2}($n,$n), itp, $(xargs...))
hessian!(Array{TH, 2}(undef, $n,$n), itp, $(xargs...))
end
end

Expand Down
33 changes: 24 additions & 9 deletions src/b-splines/prefiltering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,45 @@ padding(::Type{IT}) where {IT<:BSpline} = Val{0}()
end

@noinline function padded_index(indsA::NTuple{N,AbstractUnitRange{Int}}, ::Val{pad}) where {N,pad}
indspad = ntuple(i->indices_addpad(indsA[i], padextract(pad,i)), Val{N})
indscp = ntuple(i->indices_interior(indspad[i], padextract(pad,i)), Val{N})
@static if VERSION < v"0.7.0-DEV.843"
indspad = ntuple(i->indices_addpad(indsA[i], padextract(pad,i)), Val{N})
indscp = ntuple(i->indices_interior(indspad[i], padextract(pad,i)), Val{N})
else
indspad = ntuple(i->indices_addpad(indsA[i], padextract(pad,i)), Val(N))
indscp = ntuple(i->indices_interior(indspad[i], padextract(pad,i)), Val(N))
end
indscp, indspad
end

padded_similar(::Type{TC}, inds::Tuple{Vararg{Base.OneTo{Int}}}) where TC = Array{TC}(undef, length.(inds))
padded_similar(::Type{TC}, inds) where TC = OffsetArray{TC}(undef, inds)

# despite Compat, julia doesn't support 0.6 copy! with CartesianIndices argument
@static if isdefined(Base, :CartesianIndices)
ct!(coefs, indscp, A, indsA) = copyto!(coefs, CartesianIndices(indscp), A, CartesianIndices(indsA))
else
ct!(coefs, indscp, A, indsA) = copyto!(coefs, CartesianRange(indscp), A, CartesianRange(indsA))
end

copy_with_padding(A, ::Type{IT}) where {IT} = copy_with_padding(eltype(A), A, IT)
function copy_with_padding(::Type{TC}, A, ::Type{IT}) where {TC,IT<:DimSpec{InterpolationType}}
Pad = padding(IT)
indsA = indices(A)
indsA = axes(A)
indscp, indspad = padded_index(indsA, Pad)
coefs = similar(dims->Array{TC}(dims), indspad)
coefs = padded_similar(TC, indspad)
if indspad == indsA
coefs = copy!(coefs, A)
coefs = copyto!(coefs, A)
else
fill!(coefs, zero(TC))
copy!(coefs, CartesianRange(indscp), A, CartesianRange(indsA))
ct!(coefs, indscp, A, indsA)
end
coefs, Pad
end

prefilter!(::Type{TWeights}, A, ::Type{IT}, ::Type{GT}) where {TWeights, IT<:BSpline, GT<:GridType} = A
function prefilter(::Type{TWeights}, ::Type{TC}, A, ::Type{IT}, ::Type{GT}) where {TWeights, TC, IT<:BSpline, GT<:GridType}
coefs = similar(dims->Array{TC}(dims), indices(A))
prefilter!(TWeights, copy!(coefs, A), IT, GT), Val{0}()
coefs = padded_similar(TC, axes(A))
prefilter!(TWeights, copyto!(coefs, A), IT, GT), Val{0}()
end

function prefilter(
Expand All @@ -51,7 +66,7 @@ function prefilter!(
::Type{TWeights}, ret::TCoefs, ::Type{BSpline{IT}}, ::Type{GT}
) where {TWeights,TCoefs<:AbstractArray,IT<:Union{Quadratic,Cubic},GT<:GridType}
local buf, shape, retrs
sz = map(length, indices(ret))
sz = map(length, axes(ret))
first = true
for dim in 1:ndims(ret)
M, b = prefiltering_system(TWeights, eltype(TCoefs), sz[dim], IT, GT)
Expand Down
14 changes: 7 additions & 7 deletions src/b-splines/quadratic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int, ::Type{Quadratic{BC}
dl,d,du = inner_system_diags(T,n,Quadratic{BC})
d[1] = d[end] = -1
du[1] = dl[end] = 1
lufact!(Tridiagonal(dl, d, du), Val{false}), zeros(TC, n)
lut!(dl, d, du), zeros(TC, n)
end

function prefiltering_system(::Type{T}, ::Type{TC}, n::Int, ::Type{Quadratic{InPlace}}, ::Type{OnCell}) where {T,TC}
dl,d,du = inner_system_diags(T,n,Quadratic{InPlace})
d[1] = d[end] = convert(T, SimpleRatio(7,8))
lufact!(Tridiagonal(dl, d, du), Val{false}), zeros(TC, n)
lut!(dl, d, du), zeros(TC, n)
end

# InPlaceQ continues the quadratic at 2 all the way down to 1 (rather than 1.5)
Expand All @@ -183,7 +183,7 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int, ::Type{Quadratic{InP
valspec[1,1] = valspec[2,2] = SimpleRatio(1,8)
rowspec[1,1] = rowspec[n,2] = 1
colspec[1,3] = colspec[2,n-2] = 1
Woodbury(lufact!(Tridiagonal(dl, d, du), Val{false}), rowspec, valspec, colspec), zeros(TC, n)
Woodbury(lut!(dl, d, du), rowspec, valspec, colspec), zeros(TC, n)
end

"""
Expand All @@ -202,7 +202,7 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int, ::Type{Quadratic{BC}
(n, n-2, oneunit(T))
)

Woodbury(lufact!(Tridiagonal(dl, d, du), Val{false}), specs...), zeros(TC, n)
Woodbury(lut!(dl, d, du), specs...), zeros(TC, n)
end

"""
Expand All @@ -222,7 +222,7 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int, ::Type{Quadratic{Lin
(n, n-2, oneunit(T)),
)

Woodbury(lufact!(Tridiagonal(dl, d, du), Val{false}), specs...), zeros(TC, n)
Woodbury(lut!(dl, d, du), specs...), zeros(TC, n)
end

"""
Expand All @@ -243,7 +243,7 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int, ::Type{Quadratic{Fre
(n, n-2, 3),
(n, n-3, -1))

Woodbury(lufact!(Tridiagonal(dl, d, du), Val{false}), specs...), zeros(TC, n)
Woodbury(lut!(dl, d, du), specs...), zeros(TC, n)
end

"""
Expand All @@ -262,5 +262,5 @@ function prefiltering_system(::Type{T}, ::Type{TC}, n::Int, ::Type{Quadratic{Per
(n, 1, dl[end])
)

Woodbury(lufact!(Tridiagonal(dl, d, du), Val{false}), specs...), zeros(TC, n)
Woodbury(lut!(dl, d, du), specs...), zeros(TC, n)
end
8 changes: 4 additions & 4 deletions src/convenience-constructors.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# convenience copnstructors for linear / cubic spline interpolations
# 1D version
LinearInterpolation(range::T, vs; extrapolation_bc = Interpolations.Throw()) where {T <: Range} = extrapolate(scale(interpolate(vs, BSpline(Linear()), OnGrid()), range), extrapolation_bc)
LinearInterpolation(range::T, vs; extrapolation_bc = Interpolations.Throw()) where {T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Linear()), OnGrid()), range), extrapolation_bc)
LinearInterpolation(range::T, vs; extrapolation_bc = Interpolations.Throw()) where {T <: AbstractArray} = extrapolate(interpolate((range, ), vs, Gridded(Linear())), extrapolation_bc)
CubicSplineInterpolation(range::T, vs; bc = Interpolations.Line(), extrapolation_bc = Interpolations.Throw()) where {T <: Range} = extrapolate(scale(interpolate(vs, BSpline(Cubic(bc)), OnGrid()), range), extrapolation_bc)
CubicSplineInterpolation(range::T, vs; bc = Interpolations.Line(), extrapolation_bc = Interpolations.Throw()) where {T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Cubic(bc)), OnGrid()), range), extrapolation_bc)

# multivariate versions
LinearInterpolation(ranges::NTuple{N,T}, vs; extrapolation_bc = Interpolations.Throw()) where {N,T <: Range} = extrapolate(scale(interpolate(vs, BSpline(Linear()), OnGrid()), ranges...), extrapolation_bc)
LinearInterpolation(ranges::NTuple{N,T}, vs; extrapolation_bc = Interpolations.Throw()) where {N,T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Linear()), OnGrid()), ranges...), extrapolation_bc)
LinearInterpolation(ranges::NTuple{N,T}, vs; extrapolation_bc = Interpolations.Throw()) where {N,T <: AbstractArray} = extrapolate(interpolate(ranges, vs, Gridded(Linear())), extrapolation_bc)
CubicSplineInterpolation(ranges::NTuple{N,T}, vs; bc = Interpolations.Line(), extrapolation_bc = Interpolations.Throw()) where {N,T <: Range} = extrapolate(scale(interpolate(vs, BSpline(Cubic(bc)), OnGrid()), ranges...), extrapolation_bc)
CubicSplineInterpolation(ranges::NTuple{N,T}, vs; bc = Interpolations.Line(), extrapolation_bc = Interpolations.Throw()) where {N,T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Cubic(bc)), OnGrid()), ranges...), extrapolation_bc)
Loading

0 comments on commit 9c380b0

Please sign in to comment.