Skip to content

Fix print bugs#176

Open
adranka wants to merge 4 commits intomcabbott:masterfrom
adranka:fix_print_bug
Open

Fix print bugs#176
adranka wants to merge 4 commits intomcabbott:masterfrom
adranka:fix_print_bug

Conversation

@adranka
Copy link
Contributor

@adranka adranka commented Jan 29, 2026

Fixes two printing bugs:


First on #98, the problem occurs in this section:

AxisKeys.jl/src/show.jl

Lines 195 to 200 in 9c855d5

# If there are many slices, and they can't all fit, then we will print just 3
if prod(length, tailinds) > 3 && (2+size(a,1)) * prod(length, tailinds) > displaysize(io)[1]
midI = CartesianIndex(map(ax -> ax[firstindex(ax) + length(ax)÷2], tailinds))
fewpanels = [CartesianIndex(first.(tailinds)), midI, CartesianIndex(last.(tailinds)) ]
printstyled(io, "[showing 3 of $(prod(length, tailinds)) slices]\n", color=c3)
else

When there are too many slices, we always try to show 3. But in this case:

julia> rand(2,2,2,2,2,2,2) |> wrapdims
7-dimensional KeyedArray(...) with keys:
   2-element OneTo{Int}
   2-element OneTo{Int}
◪   2-element OneTo{Int}
▨   2-element OneTo{Int}
▨   2-element OneTo{Int}
▨   2-element OneTo{Int}
▨   2-element OneTo{Int}
And data, 2×2×2×2×2×2×2 Array{Float64, 7}:
[showing 3 of 32 slices]
[:, :, 1, 1, 1, 1, 1] ~ (:, :, 1, 1, 1, 1, 1):
      (1)          (2)
 (1)    0.0906723    0.232434
 (2)    0.0182875    0.798998

[:, :, 2, 2, 2, 2, 2] ~ (:, :, 2, 2, 2, 2, 2):
      (1)         (2)
 (1)    0.769257    0.0917172
 (2)    0.163933    0.674037[:, :, 2, 2, 2, 2, 2] ~ (:, :, 2, 2, 2, 2, 2):
      (1)         (2)
 (1)    0.769257    0.0917172
 (2)    0.163933    0.674037

We only have 2 slices to show in that dimension. This fix ensures the vector of indices in fewpanels is unique, so that we don't have duplicate panels. For the above example, this is what it looks before making each entry unique:

julia> [CartesianIndex(first.(tailinds)), midI, CartesianIndex(last.(tailinds))]
3-element Vector{CartesianIndex{5}}:
 CartesianIndex(1, 1, 1, 1, 1)
 CartesianIndex(2, 2, 2, 2, 2)
 CartesianIndex(2, 2, 2, 2, 2)

Eventually it goes to this line:

print(io, idxs == map(last,tailinds) ? "" : "\n\n")

And it won't hit the false condition, so no newlines are printed.

Changes fix this:

julia> rand(2,2,2,2,2,2,2) |> wrapdims
7-dimensional KeyedArray(...) with keys:
   2-element OneTo{Int}
   2-element OneTo{Int}
◪   2-element OneTo{Int}
▨   2-element OneTo{Int}
▨   2-element OneTo{Int}
▨   2-element OneTo{Int}
▨   2-element OneTo{Int}
And data, 2×2×2×2×2×2×2 Array{Float64, 7}:
[showing 2 of 32 slices]
[:, :, 1, 1, 1, 1, 1] ~ (:, :, 1, 1, 1, 1, 1):
      (1)         (2)
 (1)    0.617879    0.455045
 (2)    0.372811    0.985605

[:, :, 2, 2, 2, 2, 2] ~ (:, :, 2, 2, 2, 2, 2):
      (1)         (2)
 (1)    0.798686    0.245574
 (2)    0.777141    0.453238

Makes rendering a tiny bit inconsistent in terms of how many slices are shown depending on dimension size, but it does print correctly now 😄


On #173, I believe the problem is that we need to add checks on the :limit property in keyed_print_matrix.

show(io::IOBuffer, ::MIME{Symbol("text/plain")}, X:::KeyedArray) calls Base.print_matrix which the package overloads for KeyedArrays:

Base.print_matrix(io::IO, A::KeyedArray) = keyed_print_matrix(io, A, true)

It doesn't go through show_nd, so it doesn't access the :limit property. Note, as mentioned in the issue, the string is constructed from Matrix{Any}, which is a bit costly. But output is now consistent with how arrays are treated.


@mcabbott, let me know if these changes look good, if you'd like changes, or if you'd like these two in separate pull requests. Thanks!

Closes #98, #173

@adranka adranka mentioned this pull request Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Display formatting error

1 participant