cormullion / Karnak.jl

graph plotting and drawing networks with Julia, using Luxor graphics
https://cormullion.github.io/Karnak.jl/
MIT License
70 stars 1 forks source link

error when trying to visualize disjoint graphs #14

Closed mantzaris closed 1 year ago

mantzaris commented 1 year ago

Whenever I try to visualize a graph that is disjoint (not every vertex is reachable from every other vertex), an error appears. Are such graphs supported?

g = star_graph(20);
rem_vertex!(g, 1)
@drawsvg begin
    background("black")
    sethue("white")
    drawgraph(g, layout=stress)
end
AssertionError: isfinite(s)

Stacktrace:
 [1] stress(positions::Vector{Point2{Float64}}, d::Matrix{Float64}, weights::Matrix{Float64})
   @ NetworkLayout ~/.julia/packages/NetworkLayout/bV9ek/src/stress.jl:191
 [2] iterate(iter::LayoutIterator{Stress{2, Float64, Symbol, Float64, Matrix{Float64}}, SparseArrays.SparseMatrixCSC{Int64, Int64}})
   @ NetworkLayout ~/.julia/packages/NetworkLayout/bV9ek/src/stress.jl:136
 [3] layout(alg::Stress{2, Float64, Symbol, Float64, Matrix{Float64}}, adj_matrix::SparseArrays.SparseMatrixCSC{Int64, Int64})
   @ NetworkLayout ~/.julia/packages/NetworkLayout/bV9ek/src/NetworkLayout.jl:79
 [4] layout(l::Stress{2, Float64, Symbol, Float64, Matrix{Float64}}, g::SimpleGraph{Int64})
   @ NetworkLayout ~/.julia/packages/NetworkLayout/bV9ek/src/NetworkLayout.jl:100
 [5] stress(g::SimpleGraph{Int64}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ NetworkLayout ~/.julia/packages/NetworkLayout/bV9ek/src/NetworkLayout.jl:234
 [6] stress
   @ ~/.julia/packages/NetworkLayout/bV9ek/src/NetworkLayout.jl:234 [inlined]
 [7] drawgraph(g::SimpleGraph{Int64}; boundingbox::Luxor.BoundingBox, layout::typeof(stress), margin::Int64, vertexfunction::Nothing, vertexlabels::Nothing, vertexshapes::Nothing, vertexshapesizes::Nothing, vertexshaperotations::Nothing, vertexstrokecolors::Nothing, vertexstrokeweights::Nothing, vertexfillcolors::Nothing, vertexlabeltextcolors::Nothing, vertexlabelfontsizes::Nothing, vertexlabelfontfaces::Nothing, vertexlabelrotations::Nothing, vertexlabeloffsetangles::Nothing, vertexlabeloffsetdistances::Nothing, edgelist::Nothing, edgefunction::Nothing, edgelabels::Nothing, edgelines::Nothing, edgecurvature::Float64, edgestrokecolors::Nothing, edgelabelcolors::Nothing, edgelabelfontsizes::Nothing, edgelabelfontfaces::Nothing, edgestrokeweights::Nothing, edgedashpatterns::Nothing, edgegaps::Nothing, edgelabelrotations::Nothing)
   @ Karnak ~/.julia/packages/Karnak/QQChO/src/drawgraph.jl:1042
 [8] macro expansion
   @ In[90]:4 [inlined]
 [9] top-level scope
   @ ~/.julia/packages/Luxor/tNuXK/src/drawings.jl:1593
cormullion commented 1 year ago

HI! I'm fairly sure this is a limitation of NetworkLayout.jl - it won't lay out certain graphs with certain algorithms. I think you could trigger this error with:

using Graphs, NetworkLayout
g = star_graph(20)
rem_vertex!(g, 1)
NetworkLayout.stress(g)

without Karnak.jl loaded.

JackieChenYH commented 1 year ago

Hello! I adjusted the example from the NetworkLayout.jl doc. I can still plot the graph even having a node without connection to others.

using Graphs, GraphMakie.NetworkLayout

g = star_graph(20);
rem_edge!(g, 1, 6)
rem_vertex!(g, 19)
f, ax, p = graphplot(g)

Then I tried to use the

@drawsvg begin
    drawgraph(g)
end

It gives the same error code:

AssertionError: isfinite(s)

Is Karnak,jl support for graph that has disjoint node?

Thank you!

I've attached some screenshots for your reference.

Screenshot 2023-08-21 231803 Screenshot 2023-08-21 184814

The screenshot for the graph I am trying to visualize by using Karnak.jl:

Screenshot 2023-08-21 184157
cormullion commented 1 year ago

Hi Jackie!

Your code doesn't error:

Screenshot 2023-08-22 at 08 36 13

It can't emit that particular AssertionError, because nothing is calling NetworkLayout.stress().

If you ask for a spring layout, the call to NetworkLayout.spring works fine:

@drawsvg begin
    background("white")
    drawgraph(g, layout=spring)
end 400 400
Screenshot 2023-08-22 at 08 39 14

squaregrid will also work, as will drawgraph(g, layout=SFDP(Ptype=Float32)), but the stress layout won't, as far as I can tell.

I'm not a graph theorist, so I don't know whether "stress layouts" are not available for disjoint graphs, or whether it's a restriction of NetworkLayout.jl.

mantzaris commented 1 year ago

Works great! thanks