Skip to content

Commit 2fa23b5

Browse files
committed
fix
1 parent 47e435e commit 2fa23b5

File tree

4 files changed

+89
-116
lines changed

4 files changed

+89
-116
lines changed

src/MetaGraphs.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,13 @@ julia> using MetaGraphs
215215
216216
julia> using LightGraphs: Edge, Graph
217217
218-
julia> test = meta_graph(Graph(), AtEdge = Symbol);
218+
julia> test_graph = meta_graph(Graph(), AtEdge = Symbol);
219219
220-
julia> push!(test, nothing); push!(test, nothing); push!(test, nothing);
220+
julia> push!(test_graph, nothing); push!(test_graph, nothing); push!(test_graph, nothing);
221221
222-
julia> test[Edge(1, 2)] = :a; test[Edge(2, 3)] = :b;
222+
julia> test_graph[Edge(1, 2)] = :a; test_graph[Edge(2, 3)] = :b;
223223
224-
julia> filter_edges(test, isequal(:a))
224+
julia> filter_edges(test_graph, isequal(:a))
225225
1-element Array{LightGraphs.SimpleGraphs.SimpleEdge{Int64},1}:
226226
Edge 1 => 2
227227
```
@@ -240,11 +240,11 @@ julia> using MetaGraphs
240240
241241
julia> using LightGraphs: Graph
242242
243-
julia> test = meta_graph(Graph(), AtVertex = Symbol);
243+
julia> test_graph = meta_graph(Graph(), AtVertex = Symbol);
244244
245-
julia> push!(test, :a); push!(test, :b);
245+
julia> push!(test_graph, :a); push!(test_graph, :b);
246246
247-
julia> filter_vertices(test, isequal(:a))
247+
julia> filter_vertices(test_graph, isequal(:a))
248248
1-element Array{Int64,1}:
249249
1
250250
```

src/metagraph.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ metadata with multiple vertices.
3737
julia> push!(colors, :red)
3838
1
3939
40-
julia> push!(colors, :blue)
40+
julia> push!(colors, :yellow)
4141
2
4242
43-
julia> push!(colors, :yellow)
43+
julia> push!(colors, :blew)
4444
3
4545
```
4646
4747
You can access and change the metadata at a vertex using indexing:
4848
4949
```jldoctest example
50-
julia> colors[1] = :scarlet;
50+
julia> colors[3] = :blue;
5151
52-
julia> colors[1]
53-
:scarlet
52+
julia> colors[3]
53+
:blue
5454
```
5555
5656
You can access and change the metadata at an edge using indexing:
@@ -78,7 +78,7 @@ julia> delete!(colors, 3)
7878
julia> delete!(colors, 1)
7979
2 => 1
8080
81-
julia> filter_vertices(colors, isequal(:blue))
81+
julia> filter_vertices(colors, isequal(:yellow))
8282
1-element Array{Int64,1}:
8383
1
8484
```

src/persistence.jl

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@ julia> using MetaGraphs
1010
1111
julia> using LightGraphs: Edge, Graph, loadgraph, savegraph
1212
13-
julia> colors = meta_graph(Graph(), AtVertex = Symbol, AtEdge = Symbol);
14-
15-
julia> red = push!(colors, :red); blue = push!(colors, :blue); yellow = push!(colors, :yellow);
16-
17-
julia> colors[Edge(red, blue)] = :purple; colors[Edge(blue, yellow)] = :green; colors[Edge(yellow, red)] = :orange;
13+
julia> test_graph = meta_graph(Graph());
1814
1915
julia> mktemp() do file, io
20-
savegraph(file, colors)
21-
loadgraph(file, "something", MGFormat()) == colors
16+
savegraph(file, test_graph)
17+
loadgraph(file, "something", MGFormat()) == test_graph
2218
end
2319
true
2420
```
@@ -35,43 +31,27 @@ can save `AbstractMetaGraph`s in `DOTFormat`.
3531
```jldoctest DotFormat
3632
julia> using MetaGraphs
3733
38-
julia> using LightGraphs: Edge, DiGraph, Graph, savegraph, loadgraph
39-
40-
julia> test1 = meta_graph(DiGraph(), AtVertex = Dict{Symbol, String}, AtEdge = Dict{Symbol, String});
41-
42-
julia> a = push!(test1, Dict(:name => "a")); b = push!(test1, Dict(:name => "b"));
43-
44-
julia> test1[Edge(a, b)] = Dict(:name => "ab");
45-
46-
julia> mktemp() do file, io
47-
savegraph(file, test1, DOTFormat())
48-
print(read(file, String))
49-
end
50-
digraph {
51-
1 [name = \"a\"]
52-
2 [name = \"b\"]
53-
1 -> 2 [name = \"ab\"]
54-
}
34+
julia> using LightGraphs
5535
56-
julia> test2 = meta_graph(Graph(), AtEdge = Dict{Symbol, Any},
57-
graph_meta = (sugar = true, spice = true, everything_nice = true)
36+
julia> test_graph = meta_graph(DiGraph(),
37+
AtVertex = Dict{Symbol, String},
38+
AtEdge = Dict{Symbol, String},
39+
graph_meta = (tagged = true,)
5840
);
5941
60-
julia> a = push!(test2, nothing); b = push!(test2, nothing);
42+
julia> a = push!(test_graph, Dict(:name => "a")); b = push!(test_graph, Dict(:name => "b"));
6143
62-
julia> test2[Edge(a, b)] = Dict(:name => "ab", :in_order => true);
44+
julia> test_graph[Edge(a, b)] = Dict(:name => "ab");
6345
6446
julia> mktemp() do file, io
65-
savegraph(file, test2, DOTFormat(), )
47+
savegraph(file, test_graph, DOTFormat())
6648
print(read(file, String))
6749
end
68-
graph {
69-
sugar = true
70-
spice = true
71-
everything_nice = true
72-
1
73-
2
74-
1 -- 2 [name = "ab", in_order = true]
50+
digraph {
51+
tagged = true
52+
1 [name = "a"]
53+
2 [name = "b"]
54+
1 -> 2 [name = "ab"]
7555
}
7656
```
7757
"""
@@ -87,9 +67,8 @@ function savegraph(filename::AbstractString, meta::AbstractMetaGraph)
8767
return 1
8868
end
8969

90-
show_meta_list(io::IO, meta::Nothing) = nothing
91-
function show_meta_list(io::IO, meta::Union{AbstractDict, NamedTuple})
92-
if !isempty(meta)
70+
function show_meta_list(io::IO, meta)
71+
if meta !== nothing && !isempty(meta)
9372
print(io, " [")
9473
first_one = true
9574
for (key, value) in pairs(meta)
@@ -107,18 +86,6 @@ function show_meta_list(io::IO, meta::Union{AbstractDict, NamedTuple})
10786
return nothing
10887
end
10988

110-
show_meta(io::IO, meta::Nothing) = nothing
111-
function show_meta(io::IO, meta::Union{AbstractDict, NamedTuple})
112-
for (key, value) in pairs(meta)
113-
print(io, " ")
114-
print(io, key)
115-
print(io, " = ")
116-
show(io, value)
117-
print(io, '\n')
118-
end
119-
return nothing
120-
end
121-
12289
function savedot(io::IO, meta::AbstractMetaGraph)
12390
dash = if is_directed(meta)
12491
print(io, "digraph {\n")
@@ -128,7 +95,16 @@ function savedot(io::IO, meta::AbstractMetaGraph)
12895
"--"
12996
end
13097

131-
show_meta(io, meta.graph_meta)
98+
graph_meta = meta.graph_meta
99+
if graph_meta !== nothing
100+
for (key, value) in pairs(graph_meta)
101+
print(io, " ")
102+
print(io, key)
103+
print(io, " = ")
104+
show(io, value)
105+
print(io, '\n')
106+
end
107+
end
132108

133109
for vertex in vertices(meta)
134110
print(io, " ")

test/runtests.jl

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,66 @@ import Test: @testset, @test
66

77
doctest(MetaGraphs)
88

9-
test = meta_graph(DiGraph(), AtVertex = Symbol, AtEdge = Symbol)
10-
rock = push!(test, :rock)
11-
scissors = push!(test, :scissors)
12-
paper = push!(test, :paper)
9+
rock_paper_scissors = meta_graph(DiGraph(), AtVertex = Symbol, AtEdge = Symbol)
10+
rock = push!(rock_paper_scissors, :rock)
11+
paper = push!(rock_paper_scissors, :paper)
12+
scissors = push!(rock_paper_scissors, :scissors)
1313

14-
test[Edge(rock, scissors)] = :rock_beats_scissors
15-
test[Edge(scissors, paper)] = :scissors_beats_paper
16-
test[Edge(paper, rock)] = :paper_beats_rock
14+
rock_paper_scissors[Edge(rock, scissors)] = :rock_beats_scissors
15+
rock_paper_scissors[Edge(scissors, paper)] = :scissors_beats_paper
16+
rock_paper_scissors[Edge(paper, rock)] = :paper_beats_rock
1717

18-
test2 = induced_subgraph(test, [1, 2])
18+
rock_paper = induced_subgraph(rock_paper_scissors, [1, 2])
1919

2020
@testset "Miscellaneous" begin
21-
@test haskey(reverse(test), Edge(scissors, rock))
22-
@test nv(test2) == 2
23-
@test ne(test2) == 1
24-
@test haskey(test2, Edge(rock, scissors))
21+
@test haskey(reverse(rock_paper_scissors), Edge(scissors, rock))
22+
@test nv(rock_paper) == 2
23+
@test ne(rock_paper) == 1
24+
@test haskey(rock_paper, Edge(paper, rock))
25+
@test SimpleGraph(meta_graph(Graph())) isa SimpleGraph
2526
end
2627

28+
rock_paper_scissors_copy = copy(rock_paper_scissors)
29+
2730
@testset "Inheritance" begin
28-
@test copy(test) == test
29-
@test nv(zero(test)) == 0
30-
@test ne(test) == 3
31-
@test fadj(test, rock) == [scissors]
32-
@test badj(test, rock) == [paper]
33-
@test typeof(rock) == eltype(test)
34-
@test edgetype(test) == Edge{Int}
35-
@test vertices(test) == Base.OneTo(3)
36-
@test weight_type(test) == Float64
37-
@test has_edge(test, Edge(rock, scissors))
38-
@test has_vertex(test, rock)
39-
@test issubset(test2, test)
40-
@test SimpleDiGraph(test) isa SimpleDiGraph
31+
@test rock_paper_scissors_copy == rock_paper_scissors
32+
@test nv(zero(rock_paper_scissors)) == 0
33+
@test ne(rock_paper_scissors) == 3
34+
@test fadj(rock_paper_scissors, rock) == [scissors]
35+
@test badj(rock_paper_scissors, rock) == [paper]
36+
@test typeof(rock) == eltype(rock_paper_scissors)
37+
@test edgetype(rock_paper_scissors) == Edge{Int}
38+
@test vertices(rock_paper_scissors) == Base.OneTo(3)
39+
@test weight_type(rock_paper_scissors) == Float64
40+
@test has_edge(rock_paper_scissors, Edge(rock, scissors))
41+
@test has_vertex(rock_paper_scissors, rock)
42+
@test issubset(rock_paper, rock_paper_scissors)
43+
@test SimpleDiGraph(rock_paper_scissors) isa SimpleDiGraph
4144
end
4245

43-
test2 = copy(test)
44-
4546
@testset "Double check deletion" begin
46-
delete!(test, rock)
47-
@test ne(test) == 1
48-
@test filter_edges(test, isequal(:scissors_beats_paper)) == [Edge(2, 1)]
49-
delete!(test2, scissors)
50-
@test ne(test2) == 1
51-
@test filter_edges(test2, isequal(:paper_beats_rock)) == [Edge(2, 1)]
52-
rem_edge!(test, Edge(2, 1))
53-
@test ne(test) == 0
54-
rem_vertex!(test, 2)
55-
@test nv(test) == 1
47+
delete!(rock_paper_scissors, rock)
48+
@test ne(rock_paper_scissors) == 1
49+
@test filter_edges(rock_paper_scissors, isequal(:scissors_beats_paper)) == [Edge(1, 2)]
50+
delete!(rock_paper_scissors_copy, paper)
51+
@test ne(rock_paper_scissors_copy) == 1
52+
@test filter_edges(rock_paper_scissors_copy, isequal(:rock_beats_scissors)) == [Edge(1, 2)]
53+
rem_edge!(rock_paper_scissors, Edge(1, 2))
54+
@test ne(rock_paper_scissors) == 0
55+
rem_vertex!(rock_paper_scissors, 2)
56+
@test nv(rock_paper_scissors) == 1
5657
end
5758

58-
test = meta_graph(DiGraph(), AtVertex = Symbol, AtEdge = Float64, weight_function = identity)
59-
rock = push!(test, :rock)
60-
scissors = push!(test, :scissors)
61-
paper = push!(test, :paper)
62-
63-
test[Edge(rock, scissors)] = 1
64-
test[Edge(scissors, paper)] = 2
65-
w = weights(test)
59+
weighted_graph = meta_graph(DiGraph(), AtEdge = Float64, weight_function = identity)
60+
push!(weighted_graph, nothing)
61+
push!(weighted_graph, nothing)
62+
push!(weighted_graph, nothing)
63+
weighted_graph[Edge(1, 2)] = 1
64+
weighted_graph[Edge(2, 3)] = 2
65+
graph_weights = weights(weighted_graph)
6666

6767
@testset "Weights" begin
68-
@test string(w) == "metaweights"
69-
@test w[rock, paper] == 1.0
70-
@test size(w) == (3, 3)
68+
@test string(graph_weights) == "metaweights"
69+
@test graph_weights[rock, paper] == 1.0
70+
@test size(graph_weights) == (3, 3)
7171
end
72-
73-
test = meta_graph(Graph())
74-
@test SimpleGraph(test) isa SimpleGraph

0 commit comments

Comments
 (0)