Skip to content

Commit

Permalink
Bugfix in function to_dot_str
Browse files Browse the repository at this point in the history
  • Loading branch information
dcerkoney committed Dec 20, 2023
1 parent 69b8d02 commit ad2b25d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 66 deletions.
149 changes: 84 additions & 65 deletions src/backend/to_dot.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
function to_dotstatic(operator::Type, id::Int, factor, subgraphs::AbstractVector{<:AbstractGraph}, subgraph_factors::AbstractVector)
error(
"Static representation for computational graph nodes with operator $(operator) not yet implemented! "
"Static representation for computational graph nodes with operator $(operator) not yet implemented! "
)
end

function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F,subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
node_temp = ""
arrow_temp = ""
if factor != 1
opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
node_temp *= opr_fac * node_str
else
opr_name = "g$id"
end
opr_node = opr_name * "[shape=box, label = \"Add\", style=filled, fillcolor=cyan,]\n"
node_temp *= opr_node
for (gix,(g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors))
if gfactor!= 1
factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n"
for (gix, (g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors))
if gfactor != 1
factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n"
subg_str = "g$(g.id)_$(id)_$gix[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
node_temp *= factor_str * subg_str
arrow_temp *= "factor$(g.id)_$(id)_$gix->g$(g.id)_$(id)_$gix[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)_$gix[arrowhead=vee,]\n"
Expand All @@ -29,35 +29,35 @@ function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F,subgra
arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n"
end
end
return node_temp,arrow_temp
return node_temp, arrow_temp
end

function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
node_temp = ""
arrow_temp = ""
if factor != 1
opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
node_temp *= opr_fac * node_str
else
opr_name = "g$id"
end
opr_node = opr_name * "[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
opr_node = opr_name * "[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
node_temp *= opr_node
if length(subgraphs) == 1
if subgraph_factors[1] ==1
if subgraph_factors[1] == 1
arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n"
else
factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n"
factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n"
node_temp *= factor_str
arrow_temp *= "factor$(subgraphs[1].id)_$(id)->$opr_name[arrowhead=vee,]\ng$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n"
end
else
for (gix,(g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors))
if gfactor!= 1
factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n"
for (gix, (g, gfactor)) in enumerate(zip(subgraphs, subgraph_factors))
if gfactor != 1
factor_str = "factor$(g.id)_$(id)_$gix[label=$(gfactor), style=filled, fillcolor=lavender]\n"
subg_str = "g$(g.id)_$(id)_$gix[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
node_temp *= factor_str * subg_str
arrow_temp *= "factor$(g.id)_$(id)_$gix->g$(g.id)_$(id)_$gix[arrowhead=vee,]\ng$(g.id)->g$(g.id)_$(id)_$gix[arrowhead=vee,]\n"
Expand All @@ -67,27 +67,27 @@ function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subg
end
end
end
return node_temp,arrow_temp
return node_temp, arrow_temp
end

function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F, subgraphs::Vector{Graph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W}
node_temp = ""
arrow_temp = ""
if factor != 1
opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
node_temp *= opr_fac * node_str
else
opr_name = "g$id"
end
opr_node = opr_name * "[shape=box, label = \"Pow\", style=filled, fillcolor=darkolivegreen,]\n"
opr_node = opr_name * "[shape=box, label = \"Pow\", style=filled, fillcolor=darkolivegreen,]\n"
order_node = "order$(id)[label=$N, style=filled, fillcolor=lavender]\n"
node_temp *= opr_node * order_node
arrow_temp*= "order$(id)->$opr_name[arrowhead=vee,]\n"
arrow_temp *= "order$(id)->$opr_name[arrowhead=vee,]\n"
if subgraph_factors[1] != 1
factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n"
factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n"
subg_str = "g$(subgraphs[1].id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
node_temp *= factor_str * subg_str
arrow_temp *= "factor$(subgraphs[1].id)_$(id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\ng$(subgraphs[1].id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\n"
Expand All @@ -98,14 +98,14 @@ function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F,
return node_temp, arrow_temp
end

function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F,subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
node_temp = ""
arrow_temp = ""
if factor != 1
opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
node_temp *= opr_fac * node_str
else
opr_name = "g$id"
Expand All @@ -123,28 +123,28 @@ function to_dotstatic(::Type{ComputationalGraphs.Sum}, id::Int, factor::F,subgr
arrow_temp *= "g$(g.id)->$opr_name[arrowhead=vee,]\n"
end
end
return node_temp,arrow_temp
return node_temp, arrow_temp
end

function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {F,W}
node_temp = ""
arrow_temp = ""
if factor != 1
opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
node_temp *= opr_fac * node_str
else
opr_name = "g$id"
end
opr_node = opr_name * "[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
opr_node = opr_name * "[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
node_temp *= opr_node
if length(subgraphs) == 1
if subgraph_factors[1] ==1
if subgraph_factors[1] == 1
arrow_temp *= "g$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n"
else
factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n"
factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n"
node_temp *= factor_str
arrow_temp *= "factor$(subgraphs[1].id)_$(id)->$opr_name[arrowhead=vee,]\ng$(subgraphs[1].id)->$opr_name[arrowhead=vee,]\n"
end
Expand All @@ -161,27 +161,27 @@ function to_dotstatic(::Type{ComputationalGraphs.Prod}, id::Int, factor::F, subg
end
end
end
return node_temp,arrow_temp
return node_temp, arrow_temp
end

function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F, subgraphs::Vector{FeynmanGraph{F,W}}, subgraph_factors::Vector{F}) where {N,F,W}
node_temp = ""
arrow_temp = ""
if factor != 1
opr_fac = "factor$(id)[label=$(factor), style=filled, fillcolor=lavender]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp*= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
opr_name = "g$(id)_t"
node_str = "g$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
arrow_temp *= "factor$(id)->g$(id)[arrowhead=vee,]\ng$(id)_t->g$(id)[arrowhead=vee,]\n"
node_temp *= opr_fac * node_str
else
opr_name = "g$id"
end
opr_node = opr_name * "[shape=box, label = \"Pow\", style=filled, fillcolor=darkolivegreen,]\n"
opr_node = opr_name * "[shape=box, label = \"Pow\", style=filled, fillcolor=darkolivegreen,]\n"
order_node = "order$(id)[label=$N, style=filled, fillcolor=lavender]\n"
node_temp *= opr_node * order_node
arrow_temp*= "order$(id)->$opr_name[arrowhead=vee,]\n"
arrow_temp *= "order$(id)->$opr_name[arrowhead=vee,]\n"
if subgraph_factors[1] != 1
factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n"
factor_str = "factor$(subgraphs[1].id)_$(id)[label=$(subgraph_factors[1]), style=filled, fillcolor=lavender]\n"
subg_str = "g$(subgraphs[1].id)_$(id)[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
node_temp *= factor_str * subg_str
arrow_temp *= "factor$(subgraphs[1].id)_$(id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\ng$(subgraphs[1].id)->g$(subgraphs[1].id)_$(id)[arrowhead=vee,]\n"
Expand All @@ -193,13 +193,15 @@ function to_dotstatic(::Type{ComputationalGraphs.Power{N}}, id::Int, factor::F,
end

"""
function to_dot_str(graphs::AbstractVector{<:AbstractGraph})
function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="", diagram_id_map=nothing)
Compile a list of graphs into a string for dot language.
# Arguments:
- `graphs` vector of computational graphs
- `title` The name of the complied function (defaults to `"ComputationalGraph"`)
- `title` The name of the compiled function (defaults to nothing)
"""
function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="ComputationalGraph")
function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="", diagram_id_map=nothing)
head = "digraph ComputationalGraph { \nlabel=\"$name\"\n"
head *= "ReturnNode[shape=box, label = \"Return\", style=filled, fillcolor=darkorange,]\n"
body_node = ""
Expand All @@ -218,12 +220,12 @@ function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="Compu
end
if isempty(subgraphs(g)) #leaf
g_id in inds_visitedleaf && continue
leafname = getname(g.properties, leafidx)
leafname = get_leafname(g, leafidx, diagram_id_map)
if factor(g) == 1
gnode_str = "g$g_id[label=$leafname, style=filled, fillcolor=paleturquoise]\n"
body_node *= gnode_str
else
factor_str = "factor$(leafidx)_inp[label=$(factor(g)), style=filled, fillcolor=lavender]\n"
factor_str = "factor$(leafidx)_inp[label=$(factor(g)), style=filled, fillcolor=lavender]\n"
leaf_node = "l$(leafidx)[label=$leafname, style=filled, fillcolor=paleturquoise]\n"
gnode_str = "g$g_id[shape=box, label = \"Mul\", style=filled, fillcolor=cornsilk,]\n"
body_node *= factor_str * leaf_node * gnode_str
Expand All @@ -233,14 +235,14 @@ function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="Compu
push!(inds_visitedleaf, g_id)
else
g_id in inds_visitednode && continue
temp_node,temp_arrow = to_dotstatic(operator(g), g_id, factor(g), subgraphs(g), subgraph_factors(g))
body_node *=temp_node
temp_node, temp_arrow = to_dotstatic(operator(g), g_id, factor(g), subgraphs(g), subgraph_factors(g))
body_node *= temp_node
body_arrow *= temp_arrow
push!(inds_visitednode, g_id)
end
if isroot
body_arrow *= "g$(g_id)->ReturnNode[arrowhead=vee,]\n"
rootidx +=1
rootidx += 1
end
end
end
Expand All @@ -250,25 +252,42 @@ function to_dot_str(graphs::AbstractVector{<:AbstractGraph}, name::String="Compu
return expr
end

function compile_dot(graphs::AbstractVector{<:AbstractGraph}, filename::String; graph_name="ComputationalGraph")
dot_string = to_dot_str(graphs, graph_name)
function compile_dot(graphs::AbstractVector{<:AbstractGraph}, filename::String; graph_name="", diagram_id_map=nothing)
dot_string = to_dot_str(graphs, graph_name, diagram_id_map)
open(filename, "w") do f
write(f, dot_string)
end
end

function getname(properties,leafidx)
if properties isa BareGreenId
lfname = "<<i>G</i><sub>$leafidx</sub>>"
elseif properties isa BareInteractionId
lfname = "<<i>V</i><sub>$leafidx</sub>>"
elseif typeof(properties) == FeynmanProperties && properties.diagtype == ComputationalGraphs.Propagator
lfname = "<<i>G</i><sub>$leafidx</sub>>"
elseif typeof(properties) == FeynmanProperties && properties.diagtype == ComputationalGraphs.Interaction
lfname = "<<i>V</i><sub>$leafidx</sub>>"
function get_leafname(g, leafidx, diagram_id_map=nothing)
println(typeof(g))
leaftype = Nothing
if g isa FeynmanGraph
leaftype = g.properties.diagtype
elseif g isa Graph
if isnothing(diagram_id_map) == false
leaftype = typeof(diagram_id_map[g.id])
end
else
error("Unknown graph type: $(typeof(g))")
end
if leaftype in [BareGreenId, ComputationalGraphs.Propagator]
leafname = "<<i>G</i><sub>$leafidx</sub>>"
elseif leaftype in [BareInteractionId, ComputationalGraphs.Interaction]
leafname = "<<i>V</i><sub>$leafidx</sub>>"
elseif leaftype == PolarId
leafname = "<&Pi;<sub>$leafidx</sub>>"
elseif leaftype == Ver3Id
leafname = "<&Gamma;<sup>(3)</sup><sub>$leafidx</sub>>"
elseif leaftype == Ver4Id
leafname = "<&Gamma;<sup>(4)</sup><sub>$leafidx</sub>>"
else
lfname = "<L<sub>$leafidx</sub>>"
leafname = "<L<sub>$leafidx</sub>>"
end
return lfname
println()
println(g)
println(leaftype)
println(leafname)
println()
return leafname
end

2 changes: 1 addition & 1 deletion src/computational_graph/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ end
function flatten_chains(g::AbstractGraph)
Recursively flattens chains of subgraphs within a given graph `g` by merging certain trivial unary subgraphs into their parent graphs,
This function returns a new graph with flatten chains, dervied from the input graph `g` remaining unchanged.
This function returns a new graph with flatten chains, derived from the input graph `g` remaining unchanged.
# Arguments:
- `g::AbstractGraph`: graph to be modified
Expand Down

0 comments on commit ad2b25d

Please sign in to comment.