Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inconsistent axes for interpolated object as an AbstractArray #307

Open
tpapp opened this issue Mar 15, 2019 · 1 comment
Open

inconsistent axes for interpolated object as an AbstractArray #307

tpapp opened this issue Mar 15, 2019 · 1 comment

Comments

@tpapp
Copy link
Contributor

tpapp commented Mar 15, 2019

With the MWE

using Interpolations
a = range(2, 7; length = 10)    # > 1
J = 1:5
W = a .+ permutedims(J)
mapslices(S -> LinearInterpolation((a, ), S), W; dims = (1, ))

it is easy to run into something like a

julia> mapslices(S -> LinearInterpolation((a, ), S), W; dims = (1, ))
ERROR: BoundsError: attempt to access 10-element extrapolate(scale(interpolate(::Array{Float64,1}, BSpline(Linear())), (2.0:0.5555555555555556:7.0,)), Throw()) with element type Float64 at index [1]
Stacktrace:
 [1] throw_boundserror(::Interpolations.Extrapolation{Float64,1,ScaledInterpolation{Float64,1,Interpolations.BSplineInterpolation{Float64,1,Array{Float64,1},BSpline{Linear},Tuple{Base.OneTo{Int64}}},BSpline{Linear},Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}},BSpline{Linear},Throw{Nothing}}, ::Tuple{Int64}) at ./abstractarray.jl:493

The root cause seems to be that using getindex with interpolated calculation is not consistent with eg axes:

julia> slice1 = LinearInterpolation((a, ), W[:, 1]);

julia> slice1 isa AbstractArray
true

julia> axes(slice1)
(Base.OneTo(10),)

julia> slice1[1]                       # ouch
ERROR: BoundsError: attempt to access 10-element extrapolate(scale(interpolate(::Array{Float64,1}, BSpline(Linear())), (2.0:0.5555555555555556:7.0,)), Throw()) with element type Float64 at index [1]
Stacktrace:
 [1] throw_boundserror(::Interpolations.Extrapolation{Float64,1,ScaledInterpolation{Float64,1,Interpolations.BSplineInterpolation{Float64,1,Array{Float64,1},BSpline{Linear},Tuple{Base.OneTo{Int64}}},BSpline{Linear},Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}},BSpline{Linear},Throw{Nothing}}, ::Tuple{Int64}) at ./abstractarray.jl:493
 [2] inbounds_index at /home/tamas/.julia/dev/Interpolations/src/extrapolation/extrapolation.jl:102 [inlined]
 [3] inbounds_position at /home/tamas/.julia/dev/Interpolations/src/extrapolation/extrapolation.jl:93 [inlined]
 [4] Extrapolation at /home/tamas/.julia/dev/Interpolations/src/extrapolation/extrapolation.jl:43 [inlined]
 [5] getindex(::Interpolations.Extrapolation{Float64,1,ScaledInterpolation{Float64,1,Interpolations.BSplineInterpolation{Float64,1,Array{Float64,1},BSpline{Linear},Tuple{Base.OneTo{Int64}}},BSpline{Linear},Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}},BSpline{Linear},Throw{Nothing}}, ::Int64) at /home/tamas/.julia/dev/Interpolations/src/Interpolations.jl:403
 [6] top-level scope at REPL[30]:1

Suggestion: don't use getindex for this purpose.

@timholy
Copy link
Member

timholy commented Mar 21, 2019

We have a depwarn but it is only triggered for non-integer indexes:

julia> li = LinearInterpolation((a, ), W[:,1])
10-element extrapolate(scale(interpolate(::Array{Float64,1}, BSpline(Linear())), (2.0:0.5555555555555556:7.0,)), Throw()) with element type Float64:
 3.0              
 3.555555555555555
 4.111111111111111
 4.666666666666666
 5.222222222222222
 5.777777777777778
 6.333333333333333
 6.888888888888889
 7.444444444444445
 8.0              

julia> li.itp.itp[4.1]
┌ Warning: `getindex(itp::AbstractInterpolation{T, N}, i::Vararg{Number, N}) where {T, N}` is deprecated, use `itp(i...)` instead.
│   caller = top-level scope at none:0
└ @ Core none:0
4.722222222222221

Maybe it's time to turn it on for integers too. #242
I don't have time to tackle this in the foreseeable future, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants