Skip to content

Commit

Permalink
Merge pull request #50 from JuliaGraphs/dev
Browse files Browse the repository at this point in the history
Add label listing utilities
  • Loading branch information
gdalle authored Mar 18, 2023
2 parents 0c5d4d1 + 6ac2235 commit 1944ef0
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/MetaGraphsNext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ using Graphs
using SimpleTraits

export MetaGraph
export label_for, code_for, labels
export label_for, code_for
export labels, edge_labels, neighbor_labels, outneighbor_labels, inneighbor_labels
export set_data!
export weighttype, default_weight, get_weight_function
export MGFormat, DOTFormat
Expand Down
53 changes: 53 additions & 0 deletions src/graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,59 @@ function Base.issubset(meta_graph::MetaGraph, h::MetaGraph)
return issubset(meta_graph.graph, h.graph)
end

## List labels

"""
labels(meta_graph)
Iterate through all vertex labels, in the same order as the codes obtained by `vertices(meta_graph)`.
"""
function labels(meta_graph::MetaGraph)
return (label_for(meta_graph, code) for code in vertices(meta_graph))
end

"""
edge_labels(meta_graph)
Iterate through all tuples of edge labels, in the same order as the tuples of codes obtained by `edges(meta_graph)`.
"""
function edge_labels(meta_graph::MetaGraph)
return (
(label_for(meta_graph, src(ed)), label_for(meta_graph, dst(ed))) for
ed in edges(meta_graph)
)
end

"""
neighbor_labels(meta_graph, label)
Iterate through all labels of neighbors of the vertex `code` with label `label`, in the same order as the codes obtained by `neighbors(meta_graph, code)`.
"""
function neighbor_labels(meta_graph::MetaGraph, label)
code_1 = code_for(meta_graph, label)
return (label_for(meta_graph, code_2) for code_2 in neighbors(meta_graph, code_1))
end

"""
outneighbor_labels(meta_graph, label)
Iterate through all labels of outneighbors of the vertex `code` with label `label`, in the same order as the codes obtained by `outneighbors(meta_graph, code)`.
"""
function outneighbor_labels(meta_graph::MetaGraph, label)
code_1 = code_for(meta_graph, label)
return (label_for(meta_graph, code_2) for code_2 in outneighbors(meta_graph, code_1))
end

"""
inneighbor_labels(meta_graph, label)
Iterate through all labels of inneighbors of the vertex `code` with label `label`, in the same order as the codes obtained by `inneighbors(meta_graph, code)`.
"""
function inneighbor_labels(meta_graph::MetaGraph, label)
code_2 = code_for(meta_graph, label)
return (label_for(meta_graph, code_1) for code_1 in inneighbors(meta_graph, code_2))
end

## Set vertex and edge data

"""
Expand Down
9 changes: 0 additions & 9 deletions src/metagraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,3 @@ This can be useful to interpret the results of methods inherited from `Graphs`.
function label_for(meta_graph::MetaGraph, code::Integer)
return meta_graph.vertex_labels[code]
end

"""
labels(meta_graph::MetaGraph)
List all vertex labels, *not necessarily in the same order as the codes!*
"""
function labels(meta_graph::MetaGraph)
return values(meta_graph.vertex_labels)
end
39 changes: 39 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
function test_labels_codes(mg::MetaGraph)
for code in vertices(mg)
@test code_for(mg, label_for(mg, code)) == code
end
for label in labels(mg)
@test label_for(mg, code_for(mg, label)) == label
end
for (label_1, label_2) in edge_labels(mg)
@test has_edge(mg, code_for(mg, label_1), code_for(mg, label_2))
end
for label_1 in labels(mg)
for label_2 in outneighbor_labels(mg, label_1)
@test has_edge(mg, code_for(mg, label_1), code_for(mg, label_2))
end
for label_0 in inneighbor_labels(mg, label_1)
@test has_edge(mg, code_for(mg, label_0), code_for(mg, label_1))
end
end
end

@testset verbose = true "Coherence of labels and codes" begin
graph = Graph(Edge.([(1, 2), (1, 3), (2, 3)]))
vertices_description = [
:red => (255, 0, 0), :green => (0, 255, 0), :blue => (0, 0, 255)
]
edges_description = [
(:red, :green) => :yellow, (:red, :blue) => :magenta, (:green, :blue) => :cyan
]

colors = MetaGraph(graph, vertices_description, edges_description, "additive colors")
test_labels_codes(colors)

# Delete vertex in a copy and test again

colors_copy = copy(colors)
rem_vertex!(colors_copy, 1)
test_labels_codes(colors)
end

@testset verbose = true "Short-form add_vertex!/add_edge!" begin
# short-form
mg = MetaGraph(
Expand Down
30 changes: 10 additions & 20 deletions test/tutorial/1_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,18 @@ label_for(colors, 1)
label_for(colors, 3)
@test label_for(colors, 3) == :blue #src

# Test coherence #src
# ## Listing labels

for label in labels(colors) #src
@test label_for(colors, code_for(colors, label)) == label #src
end #src
# The functions `labels`, `edge_labels`, `(in/out)neighbor_labels` iterate through labels the same way that `vertices`, `edges` and `(in/out)neighbors` iterate through codes.

for code in vertices(colors) #src
@test code_for(colors, label_for(colors, code)) == code #src
end #src

# Delete vertex in a copy and test again #src

colors_copy = copy(colors) #src
rem_vertex!(colors_copy, 1) #src

for label in labels(colors_copy) #src
@test label_for(colors_copy, code_for(colors_copy, label)) == label #src
end #src

for code in vertices(colors_copy) #src
@test code_for(colors_copy, label_for(colors_copy, code)) == code #src
end #src
collect(labels(colors))
@test collect(labels(colors)) == [:red, :green, :blue] #src
#-
collect(edge_labels(colors))
@test collect(edge_labels(colors)) == [(:red, :green), (:red, :blue), (:green, :blue)] #src
#-
collect(neighbor_labels(colors, :red))
@test collect(neighbor_labels(colors, :red)) == [:green, :blue] #src

# ## Handling weights

Expand Down

0 comments on commit 1944ef0

Please sign in to comment.