Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array constructor #416

Merged
merged 12 commits into from
Sep 4, 2024
Merged

Fix array constructor #416

merged 12 commits into from
Sep 4, 2024

Conversation

kescobo
Copy link
Contributor

@kescobo kescobo commented Aug 8, 2024

Closes #415

Is there a better way to test the show output? What I really want is the inverse of @test_throws, but this way at least it checks that this doesn't error

@kescobo
Copy link
Contributor Author

kescobo commented Aug 8, 2024

OK - something is weird here...

In a different project:

julia> using Colors, BlockArrays # released version

julia> img1 = rand(RGB, 3,2); img2 = rand(RGB, 3, 3);

julia> m = mortar([[img1] [img2]]);

julia> io = IOBuffer();

julia> show(io, "image/png", m) # this is the original error
ERROR: MethodError: Cannot `convert` an object of type RGB{Float64} to an object of type Float64

julia> function Base.Array(block_array::BlockArray{T, N, R}) where {T,N,R}
           arr = Array{T}(undef, size(block_array))
           for block_index in Iterators.product(blockaxes(block_array)...)
               indices = getindex.(axes(block_array), block_index)
               arr[indices...] = @view block_array[block_index...]
           end
           return arr
       end

julia> show(io, "image/png", m) # no error

julia>

But in this project

julia> using Colors, BlockArrays # dev version on this branch

julia> img1 = rand(RGB, 3,2); img2 = rand(RGB, 3, 3);

julia> m = mortar([[img1] [img2]]);

julia> io = IOBuffer();

julia> show(io, "image/png", m)
ERROR: MethodError: no method matching show(::IOBuffer, ::MIME{Symbol("image/png")}, ::BlockMatrix{RGB{Float64}, Matrix{Matrix{RGB{Float64}}}, Tuple{BlockedOneTo{Int64, Vector{Int64}}, BlockedOneTo{Int64, Vector{Int64}}}})

I don't understand why I'm getting a MethodError from show when I define this constructor in the package, but not when I define it in the REPL

@jishnub
Copy link
Member

jishnub commented Aug 9, 2024

I'm unable to reproduce the issue, using the master branch of BlockArrays:

julia> img1 = rand(RGB, 3,2); img2 = rand(RGB, 3, 3);

julia> m = mortar([[img1] [img2]]);

julia> io = IOBuffer();

julia> show(io, "image/png", m) # this is the original error
ERROR: MethodError: no method matching show(::IOBuffer, ::MIME{Symbol("image/png")}, ::BlockMatrix{RGB{Float64}, Matrix{Matrix{RGB{Float64}}}, Tuple{BlockedOneTo{Int64, Vector{Int64}}, BlockedOneTo{Int64, Vector{Int64}}}})

Closest candidates are:
  show(::IO, ::MIME{Symbol("text/plain")}, ::AbstractArray)
   @ Base arrayshow.jl:363
  show(::IO, ::MIME{Symbol("text/plain")}, ::Any)
   @ Base multimedia.jl:47
  show(::IO, ::MIME{Symbol("image/svg+xml")}, ::AbstractMatrix{T}; max_swatches) where T<:Color
   @ Colors ~/.julia/packages/Colors/E2qak/src/display.jl:99
  ...

Stacktrace:
 [1] show(io::IOBuffer, m::String, x::BlockMatrix{RGB{Float64}, Matrix{Matrix{RGB{Float64}}}, Tuple{BlockedOneTo{Int64, Vector{Int64}}, BlockedOneTo{Int64, Vector{Int64}}}})
   @ Base.Multimedia ./multimedia.jl:123
 [2] top-level scope
   @ REPL[5]:1

julia> function Base.Array(block_array::BlockArray{T, N, R}) where {T,N,R}
           arr = Array{T}(undef, size(block_array))
           for block_index in Iterators.product(blockaxes(block_array)...)
               indices = getindex.(axes(block_array), block_index)
               arr[indices...] = @view block_array[block_index...]
           end
           return arr
       end

julia> show(io, "image/png", m) # no error
ERROR: MethodError: no method matching show(::IOBuffer, ::MIME{Symbol("image/png")}, ::BlockMatrix{RGB{Float64}, Matrix{Matrix{RGB{Float64}}}, Tuple{BlockedOneTo{Int64, Vector{Int64}}, BlockedOneTo{Int64, Vector{Int64}}}})

Closest candidates are:
  show(::IO, ::MIME{Symbol("text/plain")}, ::AbstractArray)
   @ Base arrayshow.jl:363
  show(::IO, ::MIME{Symbol("text/plain")}, ::Any)
   @ Base multimedia.jl:47
  show(::IO, ::MIME{Symbol("image/svg+xml")}, ::AbstractMatrix{T}; max_swatches) where T<:Color
   @ Colors ~/.julia/packages/Colors/E2qak/src/display.jl:99
  ...

Stacktrace:
 [1] show(io::IOBuffer, m::String, x::BlockMatrix{RGB{Float64}, Matrix{Matrix{RGB{Float64}}}, Tuple{BlockedOneTo{Int64, Vector{Int64}}, BlockedOneTo{Int64, Vector{Int64}}}})
   @ Base.Multimedia ./multimedia.jl:123
 [2] top-level scope
   @ REPL[7]:1

Copy link

codecov bot commented Aug 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.66%. Comparing base (836da5c) to head (f104b0d).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #416   +/-   ##
=======================================
  Coverage   96.66%   96.66%           
=======================================
  Files          18       18           
  Lines        1649     1649           
=======================================
  Hits         1594     1594           
  Misses         55       55           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kescobo
Copy link
Contributor Author

kescobo commented Aug 9, 2024

I'm unable to reproduce the issue, using the master branch of BlockArrays

🤔 You're right. Seems like the show method is added by Images - I was trying to do too much at once across multiple repos. Apologies for the noise.

This seems to work locally - I had it this way a couple of commits ago, but thought it didn't work for some reason 🤷

@jishnub
Copy link
Member

jishnub commented Aug 12, 2024

Could you bump the patch version so that a release may be tagged?

@@ -549,7 +549,7 @@ function Base.Array(zerodim::BlockArray{T, 0}) where {T}
end

function Base.Array(block_array::BlockArray{T, N, R}) where {T,N,R}
arr = Array{eltype(T)}(undef, size(block_array))
arr = Array{T}(undef, size(block_array))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this is marked as untested?

test/test_blockarrays.jl Outdated Show resolved Hide resolved
@jishnub jishnub merged commit 24564ec into JuliaArrays:master Sep 4, 2024
15 checks passed
@kescobo kescobo deleted the block-img branch September 11, 2024 14:41
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.

Array conversion breaks image outputs?
2 participants