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

force type Vector{Colorant} for ManualDiscreteKey #1470

Closed

Conversation

dehann
Copy link

@dehann dehann commented Aug 10, 2020

Fixes an unexpected dispatch problem with a minimal code change. Seemed like the best solution after various attempts to fix downstream with user code, but didn't work. This issue also seems to be a regression since the user code has been working for quite some time.

ERROR: LoadError: LoadError: TypeError: in new, expected Array{Colorant,1}, got a value of type Array{RGB{Normed{UInt8,8}},1}
Stacktrace:
 [1] Gadfly.Guide.ManualDiscreteKey(::String, ::Array{String,1}, ::Array{Any,1}, ::Array{RGB{Normed{UInt8,8}},1}, ::Array{Function,1}, ::Array{Measures.Measure,1}, ::Bool) at /home/dehann/.julia/dev/Gadfly/src/guide/keys.jl:267
 [2] Gadfly.Guide.ManualDiscreteKey(; title::String, labels::Array{String,1}, pos::Array{Any,1}, color::Array{String,1}, shape::Array{Function,1}, size::Array{Measures.Measure,1}) at /home/dehann/.julia/dev/Gadfly/src/guide/keys.jl:295
 [3] #manual_color_key#131 at /home/dehann/.julia/dev/Gadfly/src/guide/keys.jl:321 [inlined]
 [4] manual_color_key at /home/dehann/.julia/dev/Gadfly/src/guide/keys.jl:321 [inlined]
 [5] plotKDE(::Array{BallTreeDensity,1}; c::Array{String,1}, N::Int64, rmax::Float64, rmin::Float64, axis::Nothing, dims::Nothing, xlbl::String, title::String, legend::Array{String,1}, dimLbls::Nothing, levels::Int64, fill::Bool, points::Bool, layers::Bool, overlay::Nothing) at /home/dehann/.julia/packages/KernelDensityEstimatePlotting/49JcX/src/KernelDensityEstimatePlotting.jl:356

Not sure if this is related, but:

julia> using Colors
julia> RGB isa Colorant
false
julia> RGB <: Colorant
true

Contributor checklist:

  • N/A I've updated the documentation to reflect these changes
  • I've added an entry to NEWS.md
  • N/A I've added and/or updated the unit tests
  • I've run the regression tests
  • I've squash'ed or fixup'ed junk commits with git-rebase
  • I've built the docs and confirmed these changes don't cause new errors

Proposed changes

  • force type Vector{Colorant}for ManualDiscreteKey in guide/keys.jl

@dehann dehann changed the title force type Vector{Colorant} in for ManualDiscreteKey force type Vector{Colorant} for ManualDiscreteKey Aug 10, 2020
@codecov-commenter
Copy link

codecov-commenter commented Aug 10, 2020

Codecov Report

Merging #1470 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1470   +/-   ##
=======================================
  Coverage   89.32%   89.32%           
=======================================
  Files          39       39           
  Lines        4395     4395           
=======================================
  Hits         3926     3926           
  Misses        469      469           
Impacted Files Coverage Δ
src/guide/keys.jl 82.69% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d507b20...5d4d151. Read the comment docs.

@Mattriks
Copy link
Member

Is this due to a type piracy issue by another package like in #1466? i.e. Can you demonstrate that the above error is caused by using Gadfly alone, without some other package being the root cause?

@dehann
Copy link
Author

dehann commented Aug 10, 2020

Hi ah, this is likely the same problem then:

This PR should be pretty harmless, since it only changes colors object type a little earlier in the stack, rather than an automated convert when new is called. I think what's happening is this PR is calling the convert inside the Gadfly context, which avoids the user code from calling the convert function in Main context, which opens the door for outside converters that don't dispatch properly to cause the error.

@Mattriks
Copy link
Member

Can you give an example of your plot which uses Guide.manual_color_key. There may be a more automated way to do the plot now with recent developments on Gadfly#master (e.g. #1459)

@dehann
Copy link
Author

dehann commented Aug 10, 2020

yes, on Gadfly#master---and doesn't error without Images---but this does

using Gadfly, Images
Guide.manual_color_key("Legend", ["red"; "blue"], parse.(Colorant, ["red";"blue"]))

ERROR: TypeError: in new, expected Array{Colorant,1}, got a value of type Array{RGB{Normed{UInt8,8}},1}
Stacktrace:
 [1] ManualDiscreteKey at /home/dehann/.julia/packages/Gadfly/asb1x/src/guide/keys.jl:267 [inlined]
 [2] Gadfly.Guide.ManualDiscreteKey(; title::String, labels::Array{String,1}, pos::Array{Any,1}, color::Array{RGB{Normed{UInt8,8}},1}, shape::Array{Function,1}, size::Array{Measures.Measure,1}) at /home/dehann/.julia/packages/Gadfly/asb1x/src/guide/keys.jl:295
 [3] #manual_color_key#131 at /home/dehann/.julia/packages/Gadfly/asb1x/src/guide/keys.jl:321 [inlined]
 [4] manual_color_key(::String, ::Array{String,1}, ::Array{RGB{Normed{UInt8,8}},1}) at /home/dehann/.julia/packages/Gadfly/asb1x/src/guide/keys.jl:321
 [5] top-level scope at REPL[16]:1

EDIT, to better answer your question, the code works as follows:

function draw1D!(....; legend=nothing,....)
  # ...
  ptArr = []
  for bla in blabla
    push!(ptArr, Gadfly.layer(...))
  end
  if legend != nothing
    push!(ptArr, legend)
  end
  # ...
  return Gadfly.plot(ptArr...)
end


vecStrings = ["red";"blue"]
lg = Guide.manual_color_key("Legend", legend, parse.(Colorant, vecStrings))
H=draw1D!(....,legend=lg,....)

REF:

https://github.com/JuliaRobotics/KernelDensityEstimatePlotting.jl/blob/2b092417f0f842265ade11860c2a424bc2fe1efe/src/KernelDensityEstimatePlotting.jl#L101

@dehann
Copy link
Author

dehann commented Aug 11, 2020

An alternative suggestion here might be to force the express field type colors::Vector{Colorant} with a bespoke inner constructor that converts the type after allowing a broader incoming dispatch on ::Vector{<:Colorant}, until the type piracy in ImageCore.jl is fixed:

ManualDiscreteKey(title, labels, pos, clrs::Vector{<:Colorant}, shps, szs,  vis) = new(title, labels, pos, Vector{Colorant}(clrs), shps, szs, vis)

This might be more fitting than only 'fixing' the type inside the convenience constructor manual_color_key(..). Just a thought.

This would likely not have any performance impact, since the struct type is already ::Vector{Colorant}.

@Mattriks
Copy link
Member

Thanks for the extra info about your style of plot. It is possible to automate the colorkey, and we are working to further increase support for this new style syntax. Here are some examples:

Feedback welcome!

@dehann
Copy link
Author

dehann commented Aug 16, 2020

Thanks, will work through the examples you listed, and more automation on colorkey is great news! Thanks again.

Regarding the type piracy problem from ImageCore.jl, perhaps we should leave this PR open so others can find it more easily, but then close this PR when JuliaImages/ImageCore.jl#131 is merged and tagged?

@dehann
Copy link
Author

dehann commented Sep 19, 2020

Hi, I just tried this again and looks to be resolved (following recent ImageCore.jl tag). I tried with latest Gadfly#master and with updated packages. I will close this PR then.

Consequently @Mattriks , perhaps I could ask for a new tag on Gadfly.jl if possible please? We are having Pkg compat issues downstream which likely follow from this type piracy problem -- we think that a new release tag on Gadfly will help resolve the problem.

cc @Affie

EDIT: I should add that the compat issue seems to be a combination recent Optim v1.0.0 and ImageCore tags. It's a bit hard to debug, but installing Gadfly#master definitely fixes the problem. Here is an example of what the Pkg error dump looks like:

@dehann dehann closed this Sep 19, 2020
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.

3 participants