Skip to content

Commit

Permalink
Add show method that circumvents bounds errors (fixes #260)
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Nov 20, 2018
1 parent 21c1b0e commit 0a5b49e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ _showknots(io, A) = Base.showarg(io, A, false)
function _showknots(io, tup::NTuple{N,Any}) where N
print(io, '(')
for (i, A) in enumerate(tup)
Base.showarg(io, A, false)
if A isa AbstractRange
show(io, A)
else
Base.showarg(io, A, false)
end
i < N && print(io, ',')
end
N == 1 && print(io, ',')
Expand Down Expand Up @@ -77,3 +81,37 @@ function Base.showarg(io::IO, A::FilledExtrapolation{T,N,TI,IT}, toplevel) where
print(io, " with element type ",T)
end
end

"""
show_ranged(io, X, knots)
A replacement for the default array-`show` for types that may not have the canonical evaluation points.
`rngs` is the tuple of knots along each axis.
"""
function show_ranged(io::IO, X, knots)
summary(io, X)
isempty(X) && return
print(io, ":")
if !(haskey(io, :compact)) && length(axes(X, 2)) > 1
io = IOContext(io, :compact => true)
end
if get(io, :limit, false) && eltype(X) === Method
io = IOContext(io, :limit => false)
end
if get(io, :limit, false) && (displaysize(io))[1] - 4 <= 0
return print(io, "")
else
println(io)
end
io = IOContext(io, :typeinfo => eltype(X))
Base.print_array(io, [X(x...) for x in Iterators.product(knots...)])
end

getknots(X::BSplineInterpolation) = axes(X)
getknots(X::ScaledInterpolation) = X.ranges
getknots(X::GriddedInterpolation) = X.knots
getknots(X::AbstractExtrapolation) = getknots(parent(X))

Base.show(io::IO, ::MIME{Symbol("text/plain")}, X::ScaledInterpolation) = show_ranged(io, X, getknots(X))
Base.show(io::IO, ::MIME{Symbol("text/plain")}, X::GriddedInterpolation) = show_ranged(io, X, getknots(X))
Base.show(io::IO, ::MIME{Symbol("text/plain")}, X::AbstractExtrapolation) = show_ranged(io, X, getknots(X))
30 changes: 30 additions & 0 deletions test/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,24 @@ using Test

itp = interpolate(knots, A, (Gridded(Linear()),Gridded(Constant())))
@test summary(itp) == "8×20 interpolate((::Array{Int64,1},::Array{Float64,1}), ::Array{Float64,2}, (Gridded(Linear()), Gridded(Constant()))) with element type Float64"

# issue #260
A = (1:4)/4
itp = interpolate((range(0.0, stop=0.3, length=4),), A, Gridded(Linear()))
io = IOBuffer()
show(io, MIME("text/plain"), itp)
str = String(take!(io))
@test str == "4-element interpolate((0.0:0.1:0.3,), ::Array{Float64,1}, Gridded(Linear())) with element type Float64:\n 0.25\n 0.5 \n 0.75\n 1.0 "
end

@testset "scaled" begin
itp = interpolate(1:1.0:10, BSpline(Linear()))
sitp = scale(itp, -3:.5:1.5)
@test summary(sitp) == "10-element scale(interpolate(::Array{Float64,1}, BSpline(Linear())), (-3.0:0.5:1.5,)) with element type Float64"
io = IOBuffer()
show(io, MIME("text/plain"), sitp)
str = String(take!(io))
@test str == "10-element scale(interpolate(::Array{Float64,1}, BSpline(Linear())), (-3.0:0.5:1.5,)) with element type Float64:\n 1.0\n 2.0\n 3.0\n 4.0\n 5.0\n 6.0\n 7.0\n 8.0\n 9.0\n 10.0"

gauss(phi, mu, sigma) = exp(-(phi-mu)^2 / (2sigma)^2)
testfunction(x,y) = gauss(x, 0.5, 4) * gauss(y, -.5, 2)
Expand Down Expand Up @@ -74,4 +86,22 @@ using Test
etpf = extrapolate(itpg, NaN)
@test summary(etpf) == "8×20 extrapolate(interpolate(::Array{Float64,2}, BSpline(Linear())), NaN) with element type Float64"
end

@testset "Combinations" begin
xs = 1.0:0.5:2.0
ys = xs.^3
interp_cubic = CubicSplineInterpolation(xs, ys)
io = IOBuffer()
show(io, MIME("text/plain"), interp_cubic)
str = String(take!(io))
@test occursin("3.375", str)
end

@testset "Issue #260" begin
itp = scale(interpolate(zeros(3,3,3),BSpline(Cubic(Free(OnGrid())))),2:4,2:4,2:4)
io = IOBuffer()
show(io, MIME("text/plain"), itp)
str = String(take!(io))
@test str == "3×3×3 scale(interpolate(OffsetArray(::Array{Float64,3}, 0:4, 0:4, 0:4), BSpline(Cubic(Free(OnGrid())))), (2:4, 2:4, 2:4)) with element type Float64:\n[:, :, 1] =\n 0.0 0.0 0.0\n 0.0 0.0 0.0\n 0.0 0.0 0.0\n\n[:, :, 2] =\n 0.0 0.0 0.0\n 0.0 0.0 0.0\n 0.0 0.0 0.0\n\n[:, :, 3] =\n 0.0 0.0 0.0\n 0.0 0.0 0.0\n 0.0 0.0 0.0"
end
end # testset

0 comments on commit 0a5b49e

Please sign in to comment.