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

Fix DataGraph vertex type conversion #31

Merged
merged 7 commits into from
Apr 24, 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 <[email protected]> and contributors"]
version = "0.2.1"
version = "0.2.2"

[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.4"
NamedGraphs = "0.5.1"
PackageExtensionCompat = "1"
SimpleTraits = "0.9"
julia = "1.7"
Expand Down
10 changes: 6 additions & 4 deletions src/abstractdatagraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ using Dictionaries: set!, unset!
using Graphs:
Graphs, AbstractEdge, AbstractGraph, IsDirected, add_edge!, edges, ne, nv, vertices
using NamedGraphs.GraphsExtensions: GraphsExtensions, incident_edges, vertextype
using SimpleTraits: SimpleTraits, @traitfn
using NamedGraphs.SimilarType: similar_type
using SimpleTraits: SimpleTraits, Not, @traitfn

abstract type AbstractDataGraph{V,VD,ED} <: AbstractGraph{V} end

# Minimal interface
# TODO: Define for `AbstractGraph` as a `DataGraphInterface`.
underlying_graph(::AbstractDataGraph) = not_implemented()
underlying_graph_type(::Type{<:AbstractDataGraph}) = not_implemented()
vertex_data(::AbstractDataGraph) = not_implemented()
Expand Down Expand Up @@ -273,7 +275,7 @@ end

function map_vertex_data(f, graph::AbstractDataGraph; vertices=nothing)
graph′ = copy(graph)
vs = isnothing(vertices) ? vertices(graph) : vertices
vs = isnothing(vertices) ? Graphs.vertices(graph) : vertices
for v in vs
graph′[v] = f(graph[v])
end
Expand All @@ -282,7 +284,7 @@ end

function map_edge_data(f, graph::AbstractDataGraph; edges=nothing)
graph′ = copy(graph)
es = isnothing(edges) ? edges(graph) : edges
es = isnothing(edges) ? Graphs.edges(graph) : edges
for e in es
if isassigned(graph, e)
graph′[e] = f(graph[e])
Expand Down Expand Up @@ -368,7 +370,7 @@ end

function Graphs.induced_subgraph(graph::AbstractDataGraph, subvertices)
underlying_subgraph, vlist = Graphs.induced_subgraph(underlying_graph(graph), subvertices)
subgraph = typeof(graph)(underlying_subgraph)
subgraph = similar_type(graph)(underlying_subgraph)
for v in vertices(subgraph)
if isassigned(graph, v)
subgraph[v] = graph[v]
Expand Down
2 changes: 1 addition & 1 deletion src/arrange.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Graphs: IsDirected, src, dst
using SimpleTraits: SimpleTraits, @traitfn
using SimpleTraits: SimpleTraits, Not, @traitfn

# TODO: Use a function `arrange` like in MetaGraphsNext:
# https://github.com/JuliaGraphs/MetaGraphsNext.jl/blob/1539095ee6088aba0d5b1cb057c339ad92557889/src/metagraph.jl#L75-L80
Expand Down
9 changes: 6 additions & 3 deletions src/datagraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,17 @@ DataGraph{V,VD,ED,G}(graph::DataGraph{V,VD,ED,G}) where {V,VD,ED,G} = copy(graph
DataGraph{V,VD,ED}(graph::DataGraph{V,VD,ED}) where {V,VD,ED} = copy(graph)
DataGraph{V,VD}(graph::DataGraph{V,VD}) where {V,VD} = copy(graph)
DataGraph{V}(graph::DataGraph{V}) where {V} = copy(graph)

function DataGraph{V}(graph::DataGraph) where {V}
# TODO: Make sure this properly copies
converted_underlying_graph = convert_vertextype(V, underlying_graph(graph))
converted_vertex_data = Dictionary{V}(vertex_data(graph))
converted_edge_data = Dictionary{edgetype(converted_underlying_graph)}(edge_data(graph))
return DataGraph{V}(
converted_underlying_graph, converted_vertex_data, converted_edge_data
# This doesn't convert properly.
# converted_edge_data = Dictionary{edgetype(converted_underlying_graph)}(edge_data(graph))
converted_edge_data = Dictionary(
edgetype(converted_underlying_graph).(keys(edge_data(graph))), values(edge_data(graph))
)
return _DataGraph(converted_underlying_graph, converted_vertex_data, converted_edge_data)
end

GraphsExtensions.convert_vertextype(::Type{V}, graph::DataGraph{V}) where {V} = graph
Expand Down
2 changes: 1 addition & 1 deletion src/traits/isunderlyinggraph.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using SimpleTraits: SimpleTraits, @traitdef, @traitimpl
using SimpleTraits: SimpleTraits, Not, @traitdef, @traitimpl

@traitdef IsUnderlyingGraph{X}
#! format: off
Expand Down
16 changes: 15 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,27 @@ using DataGraphs: is_arranged
@test dg[2] == "V2"
@test dg[3] == "V3"
@test dg[4] == "V4"

@test dg[1 => 2] == "E12"
@test dg[2 => 3] == "E23"
@test dg[3 => 4] == "E34"

@test DataGraph(g) isa DataGraph{Int,Any,Any,SimpleGraph{Int},SimpleEdge{Int}}

dg_uint16 = DataGraph{UInt16}(dg)
@test dg_uint16 isa
DataGraph{UInt16,String,String,SimpleGraph{UInt16},SimpleEdge{UInt16}}
@test vertextype(dg_uint16) === UInt16
@test edgetype(dg_uint16) === SimpleEdge{UInt16}
@test vertex_data_eltype(dg_uint16) === String
@test edge_data_eltype(dg_uint16) === String
@test dg_uint16[1] == "V1"
@test dg_uint16[2] == "V2"
@test dg_uint16[3] == "V3"
@test dg_uint16[4] == "V4"
@test dg_uint16[1 => 2] == "E12"
@test dg_uint16[2 => 3] == "E23"
@test dg_uint16[3 => 4] == "E34"

# Vertices with mixed types
dg = DataGraph(NamedGraph(grid((4,)), [1, "X", 2, "Y"]))
@test nv(dg) == 4
Expand Down
Loading