JuliaGraphs / NetworkLayout.jl

Layout algorithms for graphs and trees in pure Julia.
Other
96 stars 22 forks source link

common interface between layouts #25

Closed hexaeder closed 3 years ago

hexaeder commented 3 years ago

I wanted to extend the Spring layout to handle fixed nodes and rng seeds. During this I got confused concerning the interplay between layout, the mutating (?) layout! and the occasional Layout struct with its constructor(s). For example, the default values for initialpositions are defined at multiple points in the code. Because of this, it is not that easy to extend the current functionality without either breaking the current interface or adding even more redundancy.

I was wondering, whether you'd be up for a breaking change to introduce a new common interface between those algorithms. I was thinking of something like

abstract type AbstractLayout end

(layouter::AbstractLayout)(adj_matrix) = layout(layouter, adj_matrix)

# where every layout has its own type to store parameters
struct SpringLayout <: AbstractLayout
    # parameters for spring layout
end

# even the ones without parameters
struct CircularLayout <: AbstractLayout end

# alongside an pure kw constructor where all of the default values
# for the parameters are defined
function SpringLayout(; p1=1, p2=2)
    # initialisation
end

# the layout function...
function layout(l::SpringLayout, adj_matrix)
    # do stuff... return array of Point
end

# and maybe something like this which would have the same structur as the
# current Spring.layout(...)
function spring_layout(adj_matrix; kwargs...)
    layout(SpringLayout(;kwargs...), adj_matrix)
end

Such common interface would be quite nice for graph plotting because different layout algorithms could be easily passed around and

a = SpringLayout(seed=0.1, fixed=Dict(1=>(0,0), 2=>(1,0)))
b = CircularLayout()
c = SFDPLayout(iterations=4, startpositions=...)

would all just act like functions f: adj_matrix -> list of points.

simonschoelly commented 3 years ago

Hi, I am not really involved with this package (more so with GraphPlot.jl which has some duplicate code with this one, but no depdency), but I just wanted to mention, that you maybe should crosscheck with https://github.com/JuliaPlots/GraphRecipes.jl, as that package depends on NetworkLayout, before you introduce a breaking change. For some weird reason that does not show up as a dependant on JuliaHub: https://juliahub.com/ui/Packages/NetworkLayout/XKrIU/0.3.0?t=2.

SimonDanisch commented 3 years ago

Yeah, should be easy to update the one usage + we'll of course release a breaking version, so that GraphRecipes can update when the maintainers have time to do so...