Skip to content

Commit

Permalink
Merge pull request #538 from epatters/print-sets
Browse files Browse the repository at this point in the history
Pretty-printing of tabular sets and acsets
  • Loading branch information
epatters authored Oct 28, 2021
2 parents 629c462 + cb6cf12 commit 0f03cde
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/categorical_algebra/ACSetInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,20 +285,20 @@ end

pretty_tables(acs::ACSet; kw...) = pretty_tables(stdout, acs; kw...)

function Base.show(io::IO, ::MIME"text/plain", acs::ACSet)
print(io, "ACSet")
function Base.show(io::IO, ::MIME"text/plain", acs::T) where T <: ACSet
print(io, T)
print(io, " with elements ")
join(io, ["$ob = 1:$(nparts(acs,ob))" for ob in keys(tables(acs))], ", ")
join(io, ["$ob = $(parts(acs,ob))" for ob in keys(tables(acs))], ", ")
println(io)
pretty_tables(io, acs)
end

function Base.show(io::IO, ::MIME"text/html", acs::ACSet)
function Base.show(io::IO, ::MIME"text/html", acs::T) where T <: ACSet
println(io, "<div class=\"c-set\">")
print(io, "<span class=\"c-set-summary\">")
print(io, "ACSet")
print(io, T)
print(io, " with elements ")
join(io, ["$ob = 1:$(nparts(acs,ob))" for ob in keys(tables(acs))], ", ")
join(io, ["$ob = $(parts(acs,ob))" for ob in keys(tables(acs))], ", ")
println(io, "</span>")
pretty_tables(io, acs, backend=Val(:html), standalone=false)
println(io, "</div>")
Expand Down
31 changes: 17 additions & 14 deletions src/categorical_algebra/CSetDataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export @acset_type, @abstract_acset_type, @declare_schema, StructACSet, StructCS
using MLStyle
using StaticArrays
using Reexport
import Tables

@reexport using ..ACSetInterface
using ..IndexUtils
Expand All @@ -13,8 +14,6 @@ using ...Theories: SchemaDesc, SchemaDescType, CSetSchemaDescType, SchemaDescTyp
ob_num, codom_num, attr, attrtype
@reexport using ...Theories: FreeSchema
using ...Meta: strip_lines
import Tables


# StructACSet Struct Generation
###############################
Expand Down Expand Up @@ -601,19 +600,23 @@ end
# Printing
##########

function Base.show(io::IO, acs::StructACSet{S,Ts,idxed}) where {S,Ts,idxed}
function Base.show(io::IO, acs::T) where {S,T<:StructACSet{S}}
s = SchemaDesc(S)
print(io, "ACSet")
println(io, "(")
join(io, vcat(
[ " $ob = 1:$(nparts(acs,ob))" for ob in s.obs ],
[ " $attr_type = $(Ts.parameters[i])" for (i, attr_type) in enumerate(s.attrtypes) ],
[ " $f : $(s.doms[f])$(s.codoms[f]) = $(subpart(acs,f))"
for f in s.homs ],
[ " $a : $(s.doms[a])$(s.codoms[a]) = $(subpart(acs,a))"
for a in s.attrs ],
), ",\n")
print(io, ")")
if get(io, :compact, false)
print(io, nameof(T))
print(io, ": ")
join(io, ("$ob = $(nparts(acs,ob))" for ob in s.obs), ", ")
else
print(io, T)
println(io, ":")
join(io, Iterators.flatten((
(" $ob = $(parts(acs,ob))" for ob in s.obs),
(" $f : $(s.doms[f])$(s.codoms[f]) = $(subpart(acs,f))"
for f in s.homs),
(" $a : $(s.doms[a])$(s.codoms[a]) = $(subpart(acs,a))"
for a in s.attrs),
)), "\n")
end
end

# TODO: implement Tables interface
Expand Down
12 changes: 11 additions & 1 deletion src/categorical_algebra/FinSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using DataStructures: OrderedDict, IntDisjointSets, union!, find_root!
using Reexport
import StaticArrays
using StaticArrays: StaticVector, SVector, SizedVector, similar_type
import Tables
import Tables, PrettyTables

@reexport using ..Sets
using ...GAT, ...Theories, ...CSetDataStructures, ...Graphs
Expand Down Expand Up @@ -86,6 +86,16 @@ FinSet(nt::NamedTuple) = TabularSet(nt)
Base.iterate(set::TabularSet, args...) = iterate(Tables.rows(set.table), args...)
Base.length(set::TabularSet) = Tables.rowcount(set.table)

Base.show(io::IO, set::TabularSet) = print(io, "TabularSet($(set.table))")

function Base.show(io::IO, ::MIME"text/plain", set::TabularSet{T}) where T
print(io, "$(length(set))-element TabularSet{$T}")
if !get(io, :compact, false)
println(io, ":")
PrettyTables.pretty_table(io, set.table, nosubheader=true)
end
end

# Discrete categories
#--------------------

Expand Down
16 changes: 9 additions & 7 deletions test/categorical_algebra/CSetDataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,20 @@ rem_parts!(dds, :X, [1,4])
@test incident(dds, 1, ) == [1,2]
@test incident(dds, 2, ) == []

## Pretty printing.

# Pretty printing.
dds = DDS()
add_parts!(dds, :X, 3, Φ=[2,3,3])
s = sprint(show, dds)
@test startswith(s, "ACSet")
@test occursin("DDS:", s)
@test occursin("X = 1:3", s)
@test occursin("Φ : X → X = ", s)
s = sprint(show, dds, context=:compact => true)
@test occursin("DDS:", s)
@test !occursin("\n", s)
@test occursin("X = 3", s)

s = sprint(show, MIME"text/plain"(), dds)
@test startswith(s, "ACSet")
@test occursin("DDS", s)
@test occursin("X = 1:3", s)
@test occursin("│ X │", s)

Expand Down Expand Up @@ -199,12 +202,11 @@ du = disjoint_union(d, d2)

# Pretty printing of data attributes.
s = sprint(show, d)
@test startswith(s, "ACSet")
@test occursin("R = Int64", s)
@test occursin("Dendrogram{Int64}:", s)
@test occursin("height : X → R = ", s)

s = sprint(show, MIME"text/plain"(), d)
@test startswith(s, "ACSet")
@test occursin("Dendrogram{Int64}", s)

# Allow type inheritance for data attributes.
d_abs = Dendrogram{Number}()
Expand Down
2 changes: 2 additions & 0 deletions test/categorical_algebra/FinSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ set = FinSet(Set(1:2:5))
set = FinSet((x=[1,3,5], y=['a','b','c']))
@test length(set) == 3
@test map(NamedTuple, set) == [(x=1, y='a'), (x=3, y='b'), (x=5, y='c')]
@test startswith(sshow(set), "TabularSet(")
@test startswith(sprint(show, MIME("text/plain"), set), "3-element TabularSet")

# Discrete categories
#####################
Expand Down

0 comments on commit 0f03cde

Please sign in to comment.