From e58b00d5a0923c897d2d89b0b282b9db16d3e881 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 24 Apr 2024 17:38:08 -0400 Subject: [PATCH 1/3] Update to NamedGraphs 0.6 --- Project.toml | 4 ++-- ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index b86c17f..0950d49 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "DataGraphs" uuid = "b5a273c3-7e6c-41f6-98bd-8d7f1525a36a" authors = ["Matthew Fishman and contributors"] -version = "0.2.2" +version = "0.2.3" [deps] Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" @@ -20,7 +20,7 @@ DataGraphsGraphsFlowsExt = "GraphsFlows" Dictionaries = "0.4" Graphs = "1" GraphsFlows = "0.1.1" -NamedGraphs = "0.5.1" +NamedGraphs = "0.6.0" PackageExtensionCompat = "1" SimpleTraits = "0.9" julia = "1.7" diff --git a/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl b/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl index 136db5e..3f491ea 100644 --- a/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl +++ b/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl @@ -4,7 +4,7 @@ using NamedGraphs: NamedGraphs, AbstractNamedGraph DataGraphs.is_underlying_graph(::Type{<:AbstractNamedGraph}) = true -for f in [:(NamedGraphs.parent_graph), :(NamedGraphs.parent_vertices_to_vertices)] +for f in [:(NamedGraphs.ordinal_graph), :(NamedGraphs.ordinal_vertex_to_vertex)] @eval begin function $f(graph::AbstractDataGraph, args...; kwargs...) return $f(underlying_graph(graph), args...; kwargs...) From 94ebb023e6da8589da0b5016ddb0380775a36fc2 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 26 Apr 2024 12:11:22 -0400 Subject: [PATCH 2/3] Update --- ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl | 6 +++--- test/runtests.jl | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl b/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl index 3f491ea..37e8ff8 100644 --- a/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl +++ b/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl @@ -4,10 +4,10 @@ using NamedGraphs: NamedGraphs, AbstractNamedGraph DataGraphs.is_underlying_graph(::Type{<:AbstractNamedGraph}) = true -for f in [:(NamedGraphs.ordinal_graph), :(NamedGraphs.ordinal_vertex_to_vertex)] +for f in [:(NamedGraphs.position_graph), :(NamedGraphs.vertex_positions)] @eval begin - function $f(graph::AbstractDataGraph, args...; kwargs...) - return $f(underlying_graph(graph), args...; kwargs...) + function $f(graph::AbstractDataGraph) + return $f(underlying_graph(graph)) end end end diff --git a/test/runtests.jl b/test/runtests.jl index 22a3b4f..210d6ac 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,7 +7,7 @@ using DataGraphs: is_arranged, vertex_data, vertex_data_eltype -using Dictionaries: Dictionary, Indices, dictionary +using Dictionaries: AbstractIndices, Dictionary, Indices, dictionary using Graphs: add_edge!, bfs_tree, @@ -208,7 +208,7 @@ using DataGraphs: is_arranged @test vertex_data_eltype(dg) === String @test edge_data_eltype(dg) === Symbol @test issetequal(vertices(dg), Float64.(1:4)) - @test vertices(dg) isa Indices{Float64} + @test vertices(dg) isa AbstractIndices{Float64} @test eltype(vertices(dg)) === Float64 @test has_edge(dg, 1.0 => 2.0) @test has_edge(dg, 2.0 => 3.0) From 3d2cb9cb7af583e1d95d932ac975a7f391e1f7c8 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 26 Apr 2024 13:15:12 -0400 Subject: [PATCH 3/3] Enable ordinal indexing of DataGraph --- .../DataGraphsNamedGraphsExt.jl | 26 ++++++++++++++++ test/runtests.jl | 30 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl b/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl index 37e8ff8..6253408 100644 --- a/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl +++ b/ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl @@ -11,4 +11,30 @@ for f in [:(NamedGraphs.position_graph), :(NamedGraphs.vertex_positions)] end end end + +using Graphs: edgetype, vertices +using NamedGraphs.OrdinalIndexing: OrdinalSuffixedInteger +# TODO: Define through some intermediate `to_vertex` function +# (analagous to Julia's `to_indices`) instead of through +# overloading `Base.getindex`. +function Base.getindex(graph::AbstractDataGraph, vertex::OrdinalSuffixedInteger) + return graph[vertices(graph)[vertex]] +end +function Base.getindex( + graph::AbstractDataGraph, edge::Pair{<:OrdinalSuffixedInteger,<:OrdinalSuffixedInteger} +) + return graph[edgetype(graph)(vertices(graph)[edge[1]], vertices(graph)[edge[2]])] +end +function Base.setindex!(graph::AbstractDataGraph, value, vertex::OrdinalSuffixedInteger) + graph[vertices(graph)[vertex]] = value + return graph +end +function Base.setindex!( + graph::AbstractDataGraph, + value, + edge::Pair{<:OrdinalSuffixedInteger,<:OrdinalSuffixedInteger}, +) + graph[edgetype(graph)(vertices(graph)[edge[1]], vertices(graph)[edge[2]])] = value + return graph +end end diff --git a/test/runtests.jl b/test/runtests.jl index 210d6ac..ab1faae 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -33,6 +33,7 @@ using GraphsFlows: GraphsFlows using NamedGraphs: NamedDiGraph, NamedEdge, NamedGraph using NamedGraphs.GraphsExtensions: ⊔, rename_vertices, vertextype using NamedGraphs.NamedGraphGenerators: named_grid, named_path_graph +using NamedGraphs.OrdinalIndexing: nd, st, rd, th using Test: @test, @test_broken, @testset using DataGraphs: is_arranged @@ -398,5 +399,34 @@ using DataGraphs: is_arranged @test verts[4] ∈ part2 @test flow == 1 end + @testset "OrdinalIndexing" begin + g = DataGraph( + NamedGraph(path_graph(3), ["a", "b", "c"]); + vertex_data_eltype=String, + edge_data_eltype=Symbol, + ) + g[1st] = "v_a" + g[2nd] = "v_b" + g[3rd] = "v_c" + g[1st => 2nd] = :e_ab + g[2nd => 3rd] = :e_bc + @test g["a"] == "v_a" + @test g["b"] == "v_b" + @test g["c"] == "v_c" + @test g["a" => "b"] === :e_ab + @test g["b" => "a"] === :e_ab + @test g["b" => "c"] === :e_bc + @test g["c" => "b"] === :e_bc + @test g[1st] == "v_a" + @test g[1th] == "v_a" + @test g[2nd] == "v_b" + @test g[2th] == "v_b" + @test g[3rd] == "v_c" + @test g[3th] == "v_c" + @test g[1st => 2nd] === :e_ab + @test g[2nd => 1st] === :e_ab + @test g[2nd => 3rd] === :e_bc + @test g[3rd => 2nd] === :e_bc + end end end