Skip to content

Commit 1bbf666

Browse files
authored
Nice display for ReshapedOneD (#230)
While `ReshapedOneD` is not itself an AbstractArray (due to needing to handle IIR types too), it often contains one. In such ases, there are obvious mechanisms to improve the display. With this PR, we get: ```julia julia> kern1, kern2 = kernelfactors((centered([1, 2, 1]), [-1, 0, 1])); julia> kern1 Reshaped 1d stencil with axes (-1:1, 0:0): 1 2 1 julia> kern2 Reshaped 1d stencil with axes (0:0, 1:3): -1 0 1 ```
1 parent 3edc9d3 commit 1bbf666

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/kernelfactors.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ Base.isempty(A::ReshapedOneD) = isempty(A.data)
7676

7777
@inline Base.axes(A::ReshapedOneD{_,N,Npre}) where {_,N,Npre} = Base.fill_to_length((Base.ntuple(d->0:0, Val(Npre))..., UnitRange(Base.axes1(A.data))), 0:0, Val(N))
7878

79+
function Base.show(io::IO, ::MIME"text/plain", A::ReshapedOneD{T,N,Npre,<:AbstractVector}) where {T,N,Npre}
80+
println(io, "Reshaped 1d stencil with axes ", axes(A), ":")
81+
if ndims(A) <= 2
82+
# print in matrix style
83+
a = OffsetArray{T}(undef, axes(A)...)
84+
for i in CartesianIndices(axes(A))
85+
a[i] = A[i]
86+
end
87+
Base.print_array(io, a)
88+
else
89+
println(io, " with data: ", A.data)
90+
end
91+
end
92+
7993
Base.iterate(A::ReshapedOneD) = iterate(A.data)
8094
Base.iterate(A::ReshapedOneD, state) = iterate(A.data, state)
8195

test/basic.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ using AxisArrays: AxisArray, Axis
3333
@test KernelFactors.kernelfactors(([0,3], [1,7])) == (reshape([0,3], 1:2, 0:0), reshape([1,7], 0:0, 1:2))
3434
@test KernelFactors.kernelfactors(([0,3], [1,7]')) == (reshape([0,3], 2, 1), reshape([1,7], 1, 2))
3535

36+
io = IOBuffer()
37+
kern1, kern2 = kernelfactors((centered([1, 2, 1]), [-1, 0, 1]))
38+
show(io, MIME("text/plain"), kern1)
39+
str = String(take!(io))
40+
@test occursin("Reshaped 1d stencil with axes (-1:1, 0:0)", str)
41+
@test occursin(r"1\n\s?2\n\s?1"s, str)
42+
show(io, MIME("text/plain"), kern2)
43+
str = String(take!(io))
44+
@test occursin("Reshaped 1d stencil with axes (0:0, 1:3)", str)
45+
@test occursin(r"-1\s+0\s+1"s, str)
46+
kern1, kern2, kern3 = kernelfactors((centered([1, 2, 1]), [-1, 0, 1], [1, 1, 1]))
47+
show(io, MIME("text/plain"), kern1)
48+
str = String(take!(io))
49+
@test occursin("Reshaped 1d stencil with axes (-1:1, 0:0, 0:0)", str)
50+
@test occursin("with data: [1, 2, 1]", str)
51+
show(io, MIME("text/plain"), kern3)
52+
str = String(take!(io))
53+
@test occursin("Reshaped 1d stencil with axes (0:0, 0:0, 1:3)", str)
54+
@test occursin("with data: [1, 1, 1]", str)
55+
3656
tiles = ImageFiltering.tile_allocate(Float32, (rand(3),rand(3)'))
3757
@test isa(tiles, Vector{Matrix{Float32}})
3858
@test length(tiles) == Threads.nthreads()

0 commit comments

Comments
 (0)