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

How to handle errors due to changing the underlying graph with node/edge properties given as vectors? #92

Open
liuyxpp opened this issue Nov 8, 2022 · 2 comments

Comments

@liuyxpp
Copy link

liuyxpp commented Nov 8, 2022

I am trying to build a small app which supports user add/remove nodes and edges, or loading new graphs from somewhere else. Some node and edge properties are given as vectors. Meanwhile, I do not want to re-plot such that will invalidate all my controls (Sliders and Toggles).

In the docstring of graphplot recipe, it states that

Most of the arguments can be either given as a vector of length of the edges/nodes or as a single value. One might run into errors when changing the underlying graph and therefore changing the number of Edges/Nodes.

Does it mean I will encounter such errors when I change the underlying graphs. Indeed, when I simply do

p[:graph] = new_graph_with_different_number_of_nodes_edges

GraphMakie emits errors. I notice that these errors are mainly caused by lines below

disc = [Observable{Vector{PT}}() for i in 1:N]

update_discretized!(disc, p[:paths][]) # first call to initialize
on(p[:paths]) do paths # update if pathes change
    update_discretized!(disc, paths)
end

It seems that disc is not an Observable (N is not). Therefore, when p[:paths] updates, disc is still the old one, thus its length is wrong.

Is there any simple way to avoid such errors? Or GraphMakie has to be rewritten in a fundamental way to support my use case?

@hexaeder
Copy link
Collaborator

hexaeder commented Nov 8, 2022

Very good question! Short answer: no.

Longer answer: changing the size of objects is often much more complicated because of the dependency graph between the observables. For example, if you change the number of edges that will trigger a replot of the graph which will read the color value for every edge. So you need to change the color vector first. But when you change the color vector this will trigger a update of the graph which migh errors because now you have more colors than edges.

I'd like this all to work but is very tedious to implement and to test because there are so many possible combinations. Feel free to make PRs or if you don't really now you can also post self contained test code here and i'll try to have a look on how to fix that.

The error you just showed is a bit more complicated: if you use "curvy" lines, internally every edge is draw using a separat lines plot. If one changes the number of lines one needs to add one of those plots or remove it from the axis. Looking at the source again it seems like i was aware of that limitation but didn't bother :D

TODO: Beziersegments plot won't work if the number of pathes changes.

The good news is: one would only need to update the beziergraphs recipe. One would probably need keep track of all the line plots and delete! and push! according to the changes.

MWE to start working on that error would be

using GraphMakie
using GraphMakie: Line, beziersegments

pathes=Observable([Line(Point2f(0,0), Point2f(1,0)),
                   Line(Point2f(0,1), Point2f(1,1))])
fig, ax, p = beziersegments(pathes)
push!(pathes[], GraphMakie.Line(Point2f(0,.5), Point2f(0,.5)))
notify(pathes) # errors

EDIT: A more pragmatic simpler solution might be to detect changes in the number and then delete ALL line plots and reinitialize the complete beziersegments plot. Or even more pragmatic: delete! the graphplot from the axis and create a new graphplop whenever you change the graph. You don't need to reopen the whole figure for that.

@liuyxpp
Copy link
Author

liuyxpp commented Nov 9, 2022

Thank you! This is really helpful. Actually, I have tried your approach on deleting the entire graphplot object (say p) and re-plot it. However, as my controls depends on the p instance. When I replot, there is no way (as I know) to preserve p (inplace modification), but a new graphplot will be returned by the graphplot! method. Therefore, all my controls lose track of the new graphplot object.

Now I know that GraphMakie does not support this kind of usage. Maybe I will try the most straightforwad way: replot and re-construct my whole control panel, as my graph is really small (no more than 1000 nodes, typical number of nodes is 5 - 20).

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

No branches or pull requests

2 participants