Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to NamedGraphs 0.6 #32

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DataGraphs"
uuid = "b5a273c3-7e6c-41f6-98bd-8d7f1525a36a"
authors = ["Matthew Fishman <mfishman@flatironinstitute.org> and contributors"]
version = "0.2.2"
version = "0.2.3"

[deps]
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
Expand All @@ -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"
Expand Down
32 changes: 29 additions & 3 deletions ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,37 @@ 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.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

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
34 changes: 32 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -208,7 +209,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)
Expand Down Expand Up @@ -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
Loading