JuliaMath / JuliaMath/Interpolations.jl
inconsistent axes for interpolated object as an AbstractArray
- Dominant language
- Julia
- Stars
- 575
- Forks
- 117
- PR merge metrics
- No merged PRs in 30d
Description
With the MWE
```julia
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
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
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.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.