Skip to content

Commit 1944ef0

Browse files
authored
Merge pull request #50 from JuliaGraphs/dev
Add label listing utilities
2 parents 0c5d4d1 + 6ac2235 commit 1944ef0

File tree

5 files changed

+104
-30
lines changed

5 files changed

+104
-30
lines changed

src/MetaGraphsNext.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ using Graphs
55
using SimpleTraits
66

77
export MetaGraph
8-
export label_for, code_for, labels
8+
export label_for, code_for
9+
export labels, edge_labels, neighbor_labels, outneighbor_labels, inneighbor_labels
910
export set_data!
1011
export weighttype, default_weight, get_weight_function
1112
export MGFormat, DOTFormat

src/graphs.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,59 @@ function Base.issubset(meta_graph::MetaGraph, h::MetaGraph)
4747
return issubset(meta_graph.graph, h.graph)
4848
end
4949

50+
## List labels
51+
52+
"""
53+
labels(meta_graph)
54+
55+
Iterate through all vertex labels, in the same order as the codes obtained by `vertices(meta_graph)`.
56+
"""
57+
function labels(meta_graph::MetaGraph)
58+
return (label_for(meta_graph, code) for code in vertices(meta_graph))
59+
end
60+
61+
"""
62+
edge_labels(meta_graph)
63+
64+
Iterate through all tuples of edge labels, in the same order as the tuples of codes obtained by `edges(meta_graph)`.
65+
"""
66+
function edge_labels(meta_graph::MetaGraph)
67+
return (
68+
(label_for(meta_graph, src(ed)), label_for(meta_graph, dst(ed))) for
69+
ed in edges(meta_graph)
70+
)
71+
end
72+
73+
"""
74+
neighbor_labels(meta_graph, label)
75+
76+
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)`.
77+
"""
78+
function neighbor_labels(meta_graph::MetaGraph, label)
79+
code_1 = code_for(meta_graph, label)
80+
return (label_for(meta_graph, code_2) for code_2 in neighbors(meta_graph, code_1))
81+
end
82+
83+
"""
84+
outneighbor_labels(meta_graph, label)
85+
86+
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)`.
87+
"""
88+
function outneighbor_labels(meta_graph::MetaGraph, label)
89+
code_1 = code_for(meta_graph, label)
90+
return (label_for(meta_graph, code_2) for code_2 in outneighbors(meta_graph, code_1))
91+
end
92+
93+
"""
94+
inneighbor_labels(meta_graph, label)
95+
96+
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)`.
97+
"""
98+
function inneighbor_labels(meta_graph::MetaGraph, label)
99+
code_2 = code_for(meta_graph, label)
100+
return (label_for(meta_graph, code_1) for code_1 in inneighbors(meta_graph, code_2))
101+
end
102+
50103
## Set vertex and edge data
51104

52105
"""

src/metagraph.jl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,3 @@ This can be useful to interpret the results of methods inherited from `Graphs`.
251251
function label_for(meta_graph::MetaGraph, code::Integer)
252252
return meta_graph.vertex_labels[code]
253253
end
254-
255-
"""
256-
labels(meta_graph::MetaGraph)
257-
258-
List all vertex labels, *not necessarily in the same order as the codes!*
259-
"""
260-
function labels(meta_graph::MetaGraph)
261-
return values(meta_graph.vertex_labels)
262-
end

test/misc.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
function test_labels_codes(mg::MetaGraph)
2+
for code in vertices(mg)
3+
@test code_for(mg, label_for(mg, code)) == code
4+
end
5+
for label in labels(mg)
6+
@test label_for(mg, code_for(mg, label)) == label
7+
end
8+
for (label_1, label_2) in edge_labels(mg)
9+
@test has_edge(mg, code_for(mg, label_1), code_for(mg, label_2))
10+
end
11+
for label_1 in labels(mg)
12+
for label_2 in outneighbor_labels(mg, label_1)
13+
@test has_edge(mg, code_for(mg, label_1), code_for(mg, label_2))
14+
end
15+
for label_0 in inneighbor_labels(mg, label_1)
16+
@test has_edge(mg, code_for(mg, label_0), code_for(mg, label_1))
17+
end
18+
end
19+
end
20+
21+
@testset verbose = true "Coherence of labels and codes" begin
22+
graph = Graph(Edge.([(1, 2), (1, 3), (2, 3)]))
23+
vertices_description = [
24+
:red => (255, 0, 0), :green => (0, 255, 0), :blue => (0, 0, 255)
25+
]
26+
edges_description = [
27+
(:red, :green) => :yellow, (:red, :blue) => :magenta, (:green, :blue) => :cyan
28+
]
29+
30+
colors = MetaGraph(graph, vertices_description, edges_description, "additive colors")
31+
test_labels_codes(colors)
32+
33+
# Delete vertex in a copy and test again
34+
35+
colors_copy = copy(colors)
36+
rem_vertex!(colors_copy, 1)
37+
test_labels_codes(colors)
38+
end
39+
140
@testset verbose = true "Short-form add_vertex!/add_edge!" begin
241
# short-form
342
mg = MetaGraph(

test/tutorial/1_basics.jl

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,18 @@ label_for(colors, 1)
102102
label_for(colors, 3)
103103
@test label_for(colors, 3) == :blue #src
104104

105-
# Test coherence #src
105+
# ## Listing labels
106106

107-
for label in labels(colors) #src
108-
@test label_for(colors, code_for(colors, label)) == label #src
109-
end #src
107+
# 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.
110108

111-
for code in vertices(colors) #src
112-
@test code_for(colors, label_for(colors, code)) == code #src
113-
end #src
114-
115-
# Delete vertex in a copy and test again #src
116-
117-
colors_copy = copy(colors) #src
118-
rem_vertex!(colors_copy, 1) #src
119-
120-
for label in labels(colors_copy) #src
121-
@test label_for(colors_copy, code_for(colors_copy, label)) == label #src
122-
end #src
123-
124-
for code in vertices(colors_copy) #src
125-
@test code_for(colors_copy, label_for(colors_copy, code)) == code #src
126-
end #src
109+
collect(labels(colors))
110+
@test collect(labels(colors)) == [:red, :green, :blue] #src
111+
#-
112+
collect(edge_labels(colors))
113+
@test collect(edge_labels(colors)) == [(:red, :green), (:red, :blue), (:green, :blue)] #src
114+
#-
115+
collect(neighbor_labels(colors, :red))
116+
@test collect(neighbor_labels(colors, :red)) == [:green, :blue] #src
127117

128118
# ## Handling weights
129119

0 commit comments

Comments
 (0)