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

GraphMakie update #1115

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

GraphMakie update #1115

wants to merge 5 commits into from

Conversation

vyudu
Copy link
Collaborator

@vyudu vyudu commented Nov 14, 2024

Refactoring the Graphviz code into GraphMakie. Some plots generated:

Brusselator species-reaction graph
fig1
Brusselator complex graph
fig2
Repressilator species-reaction graph
fig3

Might be worth playing around with the layouts a bit more to see if we can make the edge-lengths for these graphs more consistent and have the layouts be more horizontal. Will work on updating the documentation in a follow-up.

@isaacsas
Copy link
Member

isaacsas commented Nov 14, 2024

The first and third look great! The second is a bit strange. Do you want to tweak the layouts more in the PR, or get it merged as is and update later? (I ask as I'll review it sooner in the latter case, or wait till you say you are done in the former case).

@isaacsas
Copy link
Member

Also, is this based on the current master? I thought I had removed BifurcationKit there so the docs build but this seems to be crashing on it.

@vyudu
Copy link
Collaborator Author

vyudu commented Nov 14, 2024

Probably just merge this one as is and I'll look more at it in a follow-up. Also just rebased it, it seems like the remote branch was based on an earlier master commit

@isaacsas
Copy link
Member

It may still fail -- I have another PR that is completely removing Bifkit, so it may need one more rebasing once that is merged (but if tests pass and docs build as is I'll merge after reviewing).

Copy link
Member

@isaacsas isaacsas left a comment

Choose a reason for hiding this comment

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

Is there any way to add some tests for this?

Comment on lines +6 to +7
import Graphs: AbstractGraph, SimpleGraph, SimpleDiGraph, SimpleEdge, src, dst, ne, nv
import Catalyst: speciesreactiongraph, incidencematgraph
Copy link
Member

Choose a reason for hiding this comment

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

Why import and not using? Generally I thought there isn't much reason to import unless adding an unqualified dispatch, i.e. src(rn::ReactionSystem) vs. Graphs.src(rn::ReactionSystem). And if you are dispatching I think it is better to qualify with the package name anyways (i.e. Graph.src) (though we haven't been good about this in the past).

Comment on lines +117 to +133
# Create the SimpleDiGraph corresponding to the species and reactions
function SRGraphWrap(rn::ReactionSystem)
srg = speciesreactiongraph(rn)
rateedges = Vector{SimpleEdge}()
sm = speciesmap(rn); specs = species(rn)

for (i, rx) in enumerate(reactions(rn))
deps = get_variables(rx.rate, specs)
if deps != Any[]
for spec in deps
specidx = sm[spec]
push!(rateedges, SimpleEdge(specidx, i + length(specs)))
end
end
end
SRGraphWrap(srg, rateedges)
end
Copy link
Member

Choose a reason for hiding this comment

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

This should probably go right after the type def since it is a constructor for the type.

end
speciesreactiongraph(sir)
"""
function speciesreactiongraph(rn::ReactionSystem)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
function speciesreactiongraph(rn::ReactionSystem)
function species_reaction_graph(rn::ReactionSystem)

Getting too long not to split probably.

Comment on lines +340 to +348
adjmat = zeros(Int64, nv, nv)
for (i, rx) in enumerate(rxs)
for (spec, stoich) in zip(rx.substrates, rx.substoich)
adjmat[sm[spec], s+i] = stoich
end
for (spec, stoich) in zip(rx.products, rx.prodstoich)
adjmat[s+i, sm[spec]] = stoich
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Can you not just collect the index pairs and build an edge list? The matrix doesn't seem needed, and the edge list approach should work for large sparse systems too.

- The `interactive` flag sets the ability to interactively drag nodes and edges in the generated plot.
Only allowed if `GLMakie` is the loaded Makie backend.
"""
function ComplexGraph(rn::ReactionSystem; interactive = false)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
function ComplexGraph(rn::ReactionSystem; interactive = false)
function plot_complex_graph(rn::ReactionSystem; interactive = false)

Since this isn't a struct we should use a more standard function naming style.

push!(labels, complexelemtostr(complex[1], specstrs))
else
elems = map(c -> complexelemtostr(c, specstrs), complex)
str = reduce((e1, e2) -> *(e1, " + ", e2), elems[2:end]; init = elems[1])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
str = reduce((e1, e2) -> *(e1, " + ", e2), elems[2:end]; init = elems[1])
str = reduce((e1, e2) -> *(e1, " + ", e2), @view elems[2:end]; init = elems[1])

- The `interactive` flag sets the ability to interactively drag nodes and edges in the generated plot.
Only allowed if `GLMakie` is the loaded Makie backend.
"""
function SRGraph(rn::ReactionSystem; interactive = false)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
function SRGraph(rn::ReactionSystem; interactive = false)
function plot_speciesreaction_graph(rn::ReactionSystem; interactive = false)

Comment on lines +89 to +92
elist[i] == elist[i-1] && begin
edgecolors[i] = :red
insert!(edgelabels, i, "")
end
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
elist[i] == elist[i-1] && begin
edgecolors[i] = :red
insert!(edgelabels, i, "")
end
if elist[i] == elist[i-1]
edgecolors[i] = :red
insert!(edgelabels, i, "")
end

Comment on lines +123 to +125
for (i, rx) in enumerate(reactions(rn))
deps = get_variables(rx.rate, specs)
if deps != Any[]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for (i, rx) in enumerate(reactions(rn))
deps = get_variables(rx.rate, specs)
if deps != Any[]
deps = Set()
for (i, rx) in enumerate(reactions(rn))
empty!(deps)
get_variables!(deps, rx.rate, specs)
if !isempty(deps)

Comment on lines +28 to +29
edgeorder = sortperm(edgelist)
edgelist = edgelist[edgeorder]
Copy link
Member

Choose a reason for hiding this comment

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

Why not just use sort!?

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.

2 participants