Skip to content

Commit 734f4aa

Browse files
authored
Merge pull request #148 from Evizero/showit
use ShowItLikeYouBuildIt for Base.show
2 parents ade419b + f7acd6c commit 734f4aa

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
julia 0.5
22

3+
ShowItLikeYouBuildIt
34
WoodburyMatrices 0.1.5
45
Ratios
56
AxisAlgorithms

src/Interpolations.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,6 @@ include("gridded/gridded.jl")
9797
include("extrapolation/extrapolation.jl")
9898
include("scaling/scaling.jl")
9999
include("utils.jl")
100+
include("io.jl")
100101

101102
end # module

src/io.jl

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using ShowItLikeYouBuildIt
2+
3+
Base.summary(A::AbstractInterpolation) = summary_build(A)
4+
5+
function ShowItLikeYouBuildIt.showarg{T,N,TW,ST,GT}(io::IO, A::BSplineInterpolation{T,N,TW,ST,GT})
6+
print(io, "interpolate(")
7+
showarg(io, A.coefs)
8+
print(io, ", ")
9+
_showtypeparam(io, ST)
10+
print(io, ", ")
11+
_showtypeparam(io, GT)
12+
print(io, ')')
13+
end
14+
15+
function ShowItLikeYouBuildIt.showarg{T,N,TC,ST,K}(io::IO, A::GriddedInterpolation{T,N,TC,ST,K})
16+
print(io, "interpolate(")
17+
_showknots(io, A.knots)
18+
print(io, ", ")
19+
showarg(io, A.coefs)
20+
print(io, ", ")
21+
_showtypeparam(io, ST)
22+
print(io, ')')
23+
end
24+
25+
_showknots(io, A) = showarg(io, A)
26+
function _showknots{N}(io, tup::NTuple{N,Any})
27+
print(io, '(')
28+
for (i, A) in enumerate(tup)
29+
showarg(io, A)
30+
i < N && print(io, ',')
31+
end
32+
N == 1 && print(io, ',')
33+
print(io, ')')
34+
end
35+
36+
function ShowItLikeYouBuildIt.showarg(io::IO, A::ScaledInterpolation)
37+
print(io, "scale(")
38+
showarg(io, A.itp)
39+
print(io, ", ", A.ranges, ')')
40+
end
41+
42+
function ShowItLikeYouBuildIt.showarg{T,N,TI,IT,GT,ET}(io::IO, A::Extrapolation{T,N,TI,IT,GT,ET})
43+
print(io, "extrapolate(")
44+
showarg(io, A.itp)
45+
print(io, ", ")
46+
_showtypeparam(io, ET)
47+
print(io, ')')
48+
end
49+
50+
function ShowItLikeYouBuildIt.showarg{T,N,TI,IT,GT}(io::IO, A::FilledExtrapolation{T,N,TI,IT,GT})
51+
print(io, "extrapolate(")
52+
showarg(io, A.itp)
53+
print(io, ", ", A.fillvalue, ')')
54+
end
55+
56+
_showtypeparam{T}(io, ::Type{T}) =
57+
print(io, T.name.name, "()")
58+
_showtypeparam{T}(io, ::Type{Quadratic{T}}) =
59+
print(io, "Quadratic(", T.name.name, "())")
60+
_showtypeparam{T}(io, ::Type{Cubic{T}}) =
61+
print(io, "Cubic(", T.name.name, "())")
62+
63+
function _showtypeparam{T}(io, ::Type{BSpline{T}})
64+
print(io, "BSpline(")
65+
_showtypeparam(io, T)
66+
print(io, ')')
67+
end
68+
69+
function _showtypeparam{T}(io, ::Type{Gridded{T}})
70+
print(io, "Gridded(")
71+
_showtypeparam(io, T)
72+
print(io, ')')
73+
end
74+
75+
function _showtypeparam{TTup<:Tuple}(io, types::Type{TTup})
76+
print(io, '(')
77+
N = length(types.types)
78+
for (i, T) in enumerate(types.types)
79+
_showtypeparam(io, T)
80+
i < N && print(io, ", ")
81+
end
82+
print(io, ')')
83+
end

test/io.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module IOTests
2+
3+
using Base.Test
4+
using Interpolations
5+
6+
SPACE = if VERSION < v"0.6.0-dev.2505" # julia PR #20288
7+
""
8+
else
9+
" "
10+
end
11+
12+
@testset "BSpline" begin
13+
A = rand(8,20)
14+
15+
itp = interpolate(A, BSpline(Constant()), OnCell())
16+
@test summary(itp) == "8×20 interpolate(::Array{Float64,2}, BSpline(Constant()), OnCell()) with element type Float64"
17+
18+
itp = interpolate(A, BSpline(Constant()), OnGrid())
19+
@test summary(itp) == "8×20 interpolate(::Array{Float64,2}, BSpline(Constant()), OnGrid()) with element type Float64"
20+
21+
itp = interpolate(A, BSpline(Linear()), OnGrid())
22+
@test summary(itp) == "8×20 interpolate(::Array{Float64,2}, BSpline(Linear()), OnGrid()) with element type Float64"
23+
24+
itp = interpolate(A, BSpline(Quadratic(Reflect())), OnCell())
25+
@test summary(itp) == "8×20 interpolate(::Array{Float64,2}, BSpline(Quadratic(Reflect())), OnCell()) with element type Float64"
26+
27+
itp = interpolate(A, (BSpline(Linear()), NoInterp()), OnGrid())
28+
@test summary(itp) == "8×20 interpolate(::Array{Float64,2}, (BSpline(Linear()), NoInterp()), OnGrid()) with element type Float64"
29+
30+
itp = interpolate!(copy(A), BSpline(Quadratic(InPlace())), OnCell())
31+
@test summary(itp) == "8×20 interpolate(::Array{Float64,2}, BSpline(Quadratic(InPlace())), OnCell()) with element type Float64"
32+
end
33+
34+
@testset "Gridded" begin
35+
A = rand(20)
36+
A_x = collect(1.0:2.0:40.0)
37+
knots = (A_x,)
38+
itp = interpolate(knots, A, Gridded(Linear()))
39+
@test summary(itp) == "20-element interpolate((::Array{Float64,1},), ::Array{Float64,1}, Gridded(Linear())) with element type Float64"
40+
41+
A = rand(8,20)
42+
knots = ([x^2 for x = 1:8], [0.2y for y = 1:20])
43+
itp = interpolate(knots, A, Gridded(Linear()))
44+
@test summary(itp) == "8×20 interpolate((::Array{Int64,1},::Array{Float64,1}), ::Array{Float64,2}, Gridded(Linear())) with element type Float64"
45+
46+
itp = interpolate(knots, A, (Gridded(Linear()),Gridded(Constant())))
47+
@test summary(itp) == "8×20 interpolate((::Array{Int64,1},::Array{Float64,1}), ::Array{Float64,2}, (Gridded(Linear()), Gridded(Constant()))) with element type Float64"
48+
end
49+
50+
@testset "scaled" begin
51+
itp = interpolate(1:1.0:10, BSpline(Linear()), OnGrid())
52+
sitp = scale(itp, -3:.5:1.5)
53+
@test summary(sitp) == "10-element scale(interpolate(::Array{Float64,1}, BSpline(Linear()), OnGrid()), (-3.0:0.5:1.5,)) with element type Float64"
54+
55+
gauss(phi, mu, sigma) = exp(-(phi-mu)^2 / (2sigma)^2)
56+
testfunction(x,y) = gauss(x, 0.5, 4) * gauss(y, -.5, 2)
57+
xs = -5:.5:5
58+
ys = -4:.2:4
59+
zs = Float64[testfunction(x,y) for x in xs, y in ys]
60+
itp2 = interpolate(zs, BSpline(Quadratic(Flat())), OnGrid())
61+
sitp2 = scale(itp2, xs, ys)
62+
@test summary(sitp2) == "21×41 scale(interpolate(::Array{Float64,2}, BSpline(Quadratic(Flat())), OnGrid()), (-5.0:0.5:5.0,$SPACE-4.0:0.2:4.0)) with element type Float64"
63+
end
64+
65+
@testset "Extrapolation" begin
66+
A = rand(8,20)
67+
68+
itpg = interpolate(A, BSpline(Linear()), OnGrid())
69+
etpg = extrapolate(itpg, Flat())
70+
@test summary(etpg) == "8×20 extrapolate(interpolate(::Array{Float64,2}, BSpline(Linear()), OnGrid()), Flat()) with element type Float64"
71+
72+
etpf = extrapolate(itpg, NaN)
73+
@test summary(etpf) == "8×20 extrapolate(interpolate(::Array{Float64,2}, BSpline(Linear()), OnGrid()), NaN) with element type Float64"
74+
end
75+
76+
77+
end # Module

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ include("typing.jl")
2626

2727
include("issues/runtests.jl")
2828

29+
include("io.jl")
30+
2931
end
3032

3133
nothing

0 commit comments

Comments
 (0)