Browse Source

Merge pull request #380 from olynch/visualize-presentation

ENH: Add function to visualize presentation
pull/383/head
Evan Patterson 2 weeks ago
committed by GitHub
parent
commit
f21c1597f5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 1 deletions
  1. +13
    -0
      docs/literate/graphics/graphviz_schema_visualization.jl
  2. +1
    -0
      docs/make.jl
  3. +5
    -1
      src/core/Present.jl
  4. +48
    -0
      src/graphics/GraphvizGraphs.jl

+ 13
- 0
docs/literate/graphics/graphviz_schema_visualization.jl View File

@ -0,0 +1,13 @@
# # Visualizing Acset Schemas with Graphviz
#
#md # [![](https://img.shields.io/badge/show-nbviewer-579ACA.svg)](@__NBVIEWER_ROOT_URL__/generated/graphics/graphviz_wiring_diagrams.ipynb)
#
# It is convenient to visualize schemas in a non-textual way; often schemas can have many objects, homs and attributes, and it is hard to keep these all in your head.
#
# For this reason, we provide a function which takes a schema and produces a visual representation of it, using the Catlab Graphviz interface.
#
# This is as simple as the following code. These figures are by no means publication-quality, but they can be useful for interactive development.
using Catlab.Present, Catlab.Graphics, Catlab.Graphs
to_graphviz(Graphs.BasicGraphs.TheoryWeightedGraph)

+ 1
- 0
docs/make.jl View File

@ -58,6 +58,7 @@ makedocs(
"generated/graphics/graphviz_wiring_diagrams.md",
"generated/graphics/tikz_wiring_diagrams.md",
"generated/graphics/layouts_vs_drawings.md",
"generated/graphics/graphviz_schema_visualization.md",
],
],
"Modules" => Any[


+ 5
- 1
src/core/Present.jl View File

@ -9,7 +9,7 @@ in applications like knowledge representation.
"""
module Present
export @present, Presentation, generator, generators, has_generator, equations,
add_generator!, add_generators!, add_definition!, add_equation!
add_generator!, add_generators!, add_definition!, add_equation!, generator_index
using Base.Meta: ParseError
using Compat
@ -117,6 +117,10 @@ function add_definition!(pres::Presentation, name::Symbol, rhs::GATExpr)
generator
end
""" Get the index of a generator
"""
generator_index(pres::Presentation, x::Symbol) = pres.generator_name_index[x].second
# Presentation macro
####################


+ 48
- 0
src/graphics/GraphvizGraphs.jl View File

@ -7,6 +7,8 @@ using Compat: isnothing
using StaticArrays: StaticVector, SVector
using ...Graphs
using ...Present
using ...Theories
import ..Graphviz
# Property graphs
@ -276,4 +278,50 @@ function to_graphviz(g::AbstractHalfEdgeGraph;
to_graphviz(pg)
end
# Schemas
#########
""" Construct a graph representing the schema
"""
function to_graphviz_graph(pres::Presentation{Schema})
obs,homs,datas,attrs = generators.(Ref(pres), [:Ob,:Hom,:Data,:Attr])
g = PropertyGraph{Any}()
add_vertices!(g,length(obs))
for (i,ob) in enumerate(obs)
set_vprop!(g,i,:label,string(nameof(ob)))
set_vprop!(g,i,:shape,"plain")
set_vprop!(g,i,:margin,"2")
end
add_vertices!(g,length(datas))
for (i,data) in enumerate(datas)
set_vprop!(g,i+length(obs),:label,string(nameof(data)))
end
add_edges!(g,
generator_index.(Ref(pres), nameof.(dom.(homs))),
generator_index.(Ref(pres), nameof.(codom.(homs))))
for (i,hom) in enumerate(homs)
set_eprop!(g,i,:label,string(nameof(hom)))
set_eprop!(g,i,:len,"2")
end
add_edges!(g,
generator_index.(Ref(pres), nameof.(dom.(attrs))),
length(obs) .+ generator_index.(Ref(pres), nameof.(codom.(attrs))))
for (i,attr) in enumerate(attrs)
set_eprop!(g,i+length(homs),:label,string(nameof(attr)))
set_eprop!(g,i+length(homs),:len,"2")
end
set_gprop!(g,:graph,Dict(:rankdir => "LR"))
set_gprop!(g,:prog,"neato")
g
end
to_graphviz(pres::Presentation{Schema}) = to_graphviz(to_graphviz_graph(pres))
end

Loading…
Cancel
Save