Skip to content

Commit

Permalink
Fix convenience constructors and visual tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Sep 18, 2018
1 parent 328d0c1 commit 144dc00
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
12 changes: 6 additions & 6 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 <: 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 <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Cubic(bc)), OnGrid()), range), extrapolation_bc)
LinearInterpolation(range::T, vs; extrapolation_bc = Throw()) where {T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Linear())), range), extrapolation_bc)
LinearInterpolation(range::T, vs; extrapolation_bc = Throw()) where {T <: AbstractArray} = extrapolate(interpolate((range, ), vs, Gridded(Linear())), extrapolation_bc)
CubicSplineInterpolation(range::T, vs; bc = Line(OnGrid()), extrapolation_bc = Throw()) where {T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Cubic(bc))), range), extrapolation_bc)

# multivariate versions
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 <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Cubic(bc)), OnGrid()), ranges...), extrapolation_bc)
LinearInterpolation(ranges::NTuple{N,T}, vs; extrapolation_bc = Throw()) where {N,T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Linear())), ranges...), extrapolation_bc)
LinearInterpolation(ranges::NTuple{N,T}, vs; extrapolation_bc = Throw()) where {N,T <: AbstractArray} = extrapolate(interpolate(ranges, vs, Gridded(Linear())), extrapolation_bc)
CubicSplineInterpolation(ranges::NTuple{N,T}, vs; bc = Line(OnGrid()), extrapolation_bc = Throw()) where {N,T <: AbstractRange} = extrapolate(scale(interpolate(vs, BSpline(Cubic(bc))), ranges...), extrapolation_bc)
20 changes: 10 additions & 10 deletions test/convenience-constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
f(x) = log(x)
A = [f(x) for x in xs]
interp = LinearInterpolation(xs, A) # using convenience constructor
interp_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs), Interpolations.Throw()) # using full constructor
interp_full = extrapolate(scale(interpolate(A, BSpline(Linear())), xs), Throw()) # using full constructor

@test typeof(interp) == typeof(interp_full)
@test interp(XMIN) f(XMIN)
Expand All @@ -37,7 +37,7 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
f(x) = log(x)
A = [f(x) for x in xs]
interp = CubicSplineInterpolation(xs, A)
interp_full = extrapolate(scale(interpolate(A, BSpline(Cubic(Line())), OnGrid()), xs), Interpolations.Throw())
interp_full = extrapolate(scale(interpolate(A, BSpline(Cubic(Line(OnGrid())))), xs), Throw())

@test typeof(interp) == typeof(interp_full)
@test interp(XMIN) f(XMIN)
Expand All @@ -56,7 +56,7 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
f(x) = log(x)
A = [f(x) for x in xs]
interp = LinearInterpolation(xs, A)
interp_full = extrapolate(interpolate((xs, ), A, Gridded(Linear())), Interpolations.Throw())
interp_full = extrapolate(interpolate((xs, ), A, Gridded(Linear())), Throw())

@test typeof(interp) == typeof(interp_full)
@test interp(xmin) f(xmin)
Expand All @@ -76,8 +76,8 @@ YLEN = convert(Integer, floor((YMAX - YMIN)/ΔY) + 1)
x_lower = XMIN - ΔX
x_higher = XMAX + ΔX

extrap = LinearInterpolation(xs, A, extrapolation_bc = Interpolations.Linear())
extrap_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs), Interpolations.Linear())
extrap = LinearInterpolation(xs, A, extrapolation_bc = Line())
extrap_full = extrapolate(scale(interpolate(A, BSpline(Linear())), xs), Line())

@test typeof(extrap) == typeof(extrap_full)
@test extrap(x_lower) A[1] - ΔA_l
Expand All @@ -92,7 +92,7 @@ end
f(x, y) = log(x+y)
A = [f(x,y) for x in xs, y in ys]
interp = LinearInterpolation((xs, ys), A)
interp_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs, ys), Interpolations.Throw())
interp_full = extrapolate(scale(interpolate(A, BSpline(Linear())), xs, ys), Throw())

@test typeof(interp) == typeof(interp_full)
@test interp(XMIN,YMIN) f(XMIN,YMIN)
Expand All @@ -115,7 +115,7 @@ end
f(x, y) = log(x+y)
A = [f(x,y) for x in xs, y in ys]
interp = CubicSplineInterpolation((xs, ys), A)
interp_full = extrapolate(scale(interpolate(A, BSpline(Cubic(Line())), OnGrid()), xs, ys), Interpolations.Throw())
interp_full = extrapolate(scale(interpolate(A, BSpline(Cubic(Line(OnGrid())))), xs, ys), Throw())

@test typeof(interp) == typeof(interp_full)
@test interp(XMIN,YMIN) f(XMIN,YMIN)
Expand All @@ -142,7 +142,7 @@ end
f(x, y) = log(x+y)
A = [f(x,y) for x in xs, y in ys]
interp = LinearInterpolation((xs, ys), A)
interp_full = extrapolate(interpolate((xs, ys), A, Gridded(Linear())), Interpolations.Throw())
interp_full = extrapolate(interpolate((xs, ys), A, Gridded(Linear())), Throw())

@test typeof(interp) == typeof(interp_full)
@test interp(xmin,ymin) f(xmin,ymin)
Expand Down Expand Up @@ -171,8 +171,8 @@ end
y_lower = YMIN - ΔY
y_higher = YMAX + ΔY

extrap = LinearInterpolation((xs, ys), A, extrapolation_bc = (Interpolations.Linear(), Interpolations.Flat()))
extrap_full = extrapolate(scale(interpolate(A, BSpline(Linear()), OnGrid()), xs, ys), (Interpolations.Linear(), Interpolations.Flat()))
extrap = LinearInterpolation((xs, ys), A, extrapolation_bc = (Line(), Flat()))
extrap_full = extrapolate(scale(interpolate(A, BSpline(Linear())), xs, ys), (Line(), Flat()))

@test typeof(extrap) == typeof(extrap_full)
@test extrap(x_lower, y_lower) A[1, 1] - ΔA_l
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ using Interpolations
include("issues/runtests.jl")

include("io.jl")
# include("convenience-constructors.jl")
include("convenience-constructors.jl")
include("readme-examples.jl")

end
22 changes: 11 additions & 11 deletions test/visual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ p = plot()
if true

Btypes = (Periodic, Flat, Line, Free, Reflect)
Itypes = (
Constant, Linear,
[Quadratic{T} for T in Btypes]...,
[Cubic{T} for T in Btypes[1:end-1]]..., # no Reflect for Cubic
)
Etypes = (Flat, Linear, Reflect, Periodic)
Gtypes = (OnCell, OnGrid)
degrees = (
Constant(), Linear(),
[Quadratic(T(G())) for T in Btypes, G in Gtypes]...,
[Cubic(T(G())) for T in Btypes[1:end-1], G in Gtypes]..., # no Reflect for Cubic
)
Etypes = (Flat, Line, Reflect, Periodic)

for IT in Itypes, GT in Gtypes, ET in Etypes
itp = extrapolate(interpolate(y1, BSpline(IT()), GT()), ET())
for deg in degrees, ET in Etypes
itp = extrapolate(interpolate(y1, BSpline(deg)), ET())

stuff = Any[]

push!(stuff, layer(x=xg,y=y1,Geom.point,Theme(default_color=colorant"green")))
push!(stuff, layer(x=xf,y=[itp[x] for x in xf],Geom.path,Theme(point_size=2px)))
title = "$(IT.name.name){$(join(map(t -> t.name.name, IT.parameters), ','))}, $(ET.name.name)"
title = "$deg, $(ET.name.name)"
push!(stuff, Guide.title(title))
display(plot(stuff...))
end
Expand All @@ -45,11 +45,11 @@ zg = f2.(xg, yg')
xf = -1:.1:nx+1
yf = -1:.1:ny+1

itp2 = extrapolate(interpolate(zg, BSpline(Quadratic{Flat}()), OnCell()), Linear())
itp2 = extrapolate(interpolate(zg, BSpline(Quadratic(Flat(OnCell()))), Line())

display(plot(
layer(x=xf,y=yf,z=[itp2[x,y] for x in xf, y in yf], Geom.contour),
Guide.title("Quadratic{Flat}, Oncell, Linear")
Guide.title("Quadratic(Flat(OnCell)), Line")
))

end
Expand Down

0 comments on commit 144dc00

Please sign in to comment.