-
Notifications
You must be signed in to change notification settings - Fork 112
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
make vector indexing work with all extrapolations #143
Conversation
@marius311 thanks for being willing to open a pull request here. Can you provide an example of what didn't work before? Is the no-op implementation of |
Sure, here's the above output before this fix (on 0.6), julia> itp = extrapolate(interpolate([1,2],BSpline(Linear()),OnGrid()),0)
2-element Interpolations.FilledExtrapolation{Float64,1,Interpolations.BSplineInterpolation{Float64,1,Array{Float64,1},Interpolations.BSpline{Interpolations.Linear},Interpolations.OnGrid,0},Interpolations.BSpline{Interpolations.Linear},Interpolations.OnGrid,Float64}:
1.0
2.0
julia> itp[[0,1,2,3]]
ERROR: BoundsError: attempt to access 2-element Interpolations.FilledExtrapolation{Float64,1,Interpolations.BSplineInterpolation{Float64,1,Array{Float64,1},Interpolations.BSpline{Interpolations.Linear},Interpolations.OnGrid,0},Interpolations.BSpline{Interpolations.Linear},Interpolations.OnGrid,Float64} at index [[0, 1, 2, 3]]
Stacktrace:
[1] throw_boundserror(::Interpolations.FilledExtrapolation{Float64,1,Interpolations.BSplineInterpolation{Float64,1,Array{Float64,1},Interpolations.BSpline{Interpolations.Linear},Interpolations.OnGrid,0},Interpolations.BSpline{Interpolations.Linear},Interpolations.OnGrid,Float64}, ::Tuple{Array{Int64,1}}) at ./abstractarray.jl:426
[2] checkbounds at ./abstractarray.jl:355 [inlined]
[3] macro expansion at ./multidimensional.jl:441 [inlined]
[4] _getindex at ./multidimensional.jl:438 [inlined]
[5] getindex(::Interpolations.FilledExtrapolation{Float64,1,Interpolations.BSplineInterpolation{Float64,1,Array{Float64,1},Interpolations.BSpline{Interpolations.Linear},Interpolations.OnGrid,0},Interpolations.BSpline{Interpolations.Linear},Interpolations.OnGrid,Float64}, ::Array{Int64,1}) at ./abstractarray.jl:875 And yea, that checkbounds overrides the default one which throws the error you see above. Also, I think I need to fix that 0.4 unittest problem, do you happen to know what's wrong with the |
Ahh I see. Make sense. Let's remove the Also I think that 0.4 might be having a hard time recognizing the |
That has to be there actually, so that the call to
Good call, that could well be it. I'll give it a go. |
Ok I think that fixed 0.4 but the 0.5 tests fail now, but maybe just randomly stalled? Its fine locally for me. |
I've restarted the tests on 0.5. I would like to have the opinion of at least one other person regarding the addition of the |
Ahh I understand the concern now. FWIW, glancing through the source code most of the custom |
I do agree it'd be nice to allow any generic object, although that's probably beyond this PR/me. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely the right idea. I'm not certain the Base mechanisms will work entirely, but this seems to be a step in the right direction.
One category where we'd be better off writing our own vector-indexing methods is for irregular grids; we can save ourselves some lookup time in that case. But I vaguely remember we might have already done that.
@@ -54,10 +54,12 @@ function getindex_impl{T,N,ITPT,IT,GT,ET}(etp::Type{Extrapolation{T,N,ITPT,IT,GT | |||
end | |||
end | |||
|
|||
@generated function getindex{T,N,ITPT,IT,GT,ET}(etp::Extrapolation{T,N,ITPT,IT,GT,ET}, xs...) | |||
@generated function getindex{T,N,ITPT,IT,GT,ET}(etp::Extrapolation{T,N,ITPT,IT,GT,ET}, xs::Number...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should change this now to xs::Vararg{Number,N}
(insist that N
xs
are provided).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I'd take that back. By putting on the Vararg
constraint, you'd suddenly allow trailing 1s and indexing with fewer indices than dimensions (Base machinery will adjust the inputs to produce N outputs). So by not constraining to N
, you ensure that this method "intercepts" the call.
test/extrapolation/runtests.jl
Outdated
|
||
# check all extrapolations work with vectorized indexing | ||
for E in [0,Flat(),Linear(),Periodic(),Reflect()] | ||
@test (@inferred(getindex(extrapolate(interpolate([0,0],BSpline(Linear()),OnGrid()),E),[3]))) == [0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it work with a less trivial test? E.g.,
etp = extrapolate(interpolate([0,0],BSpline(Linear()),OnGrid()),E)
@test @inferred(getindex(etp, [1.2, 1.8, 3.1])) === [0.0, 0.0, 0.0]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does, I added this as a better test.
With regards to allowing non-numbers, there's a challenge in structuring dispatch in such a way that you can differentiate between scalar and non-scalar indexing. In 0.6, you can use Base's non-scalar indexing fallbacks with custom scalar indices with a setup like the following:
It's a little oblique, but the idea is that This becomes significantly harder if you want to use |
Personally I'd be fine with dropping support for julia-0.4. Might be the most expedient way to get this passing tests on all supported versions. |
Thanks @marius311 ! |
Thanks for the help/comments! |
Makes e.g. this work,
with all extrapolation types (previously only some worked).
I just let
getindex
fall through to the default implementation, and added acheckbounds
which always succeeds since we are extrapolating to no-matter-where. Tests seem to be passing including the new ones I've written, but I'm not at all familiar with this code-base, so someone please check me!I think this also fully closes #128