Skip to content

Commit

Permalink
Merge pull request #17 from simonschoelly/split_into_submodules
Browse files Browse the repository at this point in the history
Split GraphIO into submodules
  • Loading branch information
sbromberger authored Oct 6, 2018
2 parents 5678e32 + 577c444 commit 5e6207f
Show file tree
Hide file tree
Showing 25 changed files with 358 additions and 251 deletions.
7 changes: 2 additions & 5 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
julia 0.7
LightGraphs 0.9.5
EzXML 0.9.0
ParserCombinator 2.0.0
#JLD
LightGraphs 1.1.0
SimpleTraits
CodecZlib
Requires
12 changes: 12 additions & 0 deletions src/cdf.jl → src/CDF/Cdf.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
module CDF

# loads a graph from a IEEE CDF file.
# http://www2.ee.washington.edu/research/pstca/formats/cdf.txt
# http://www2.ee.washington.edu/research/pstca/pf30/ieee30cdf.txt

using LightGraphs
using LightGraphs: AbstractGraphFormat

import LightGraphs: loadgraph, loadgraphs, savegraph

export CDFFormat


struct CDFFormat <: AbstractGraphFormat end

function _loadcdf(io::IO)
Expand Down Expand Up @@ -45,3 +55,5 @@ end
loadcdf(io::IO, gname::String) = _loadcdf(io)
loadgraph(io::IO, gname::String, ::CDFFormat) = loadcdf(io, gname)
loadgraphs(io::IO, ::CDFFormat) = Dict("graph" => loadcdf(io, "graph"))

end # module
29 changes: 20 additions & 9 deletions src/dot.jl → src/DOT/Dot.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
module DOT

using GraphIO.ParserCombinator.Parsers
using LightGraphs
using LightGraphs: AbstractGraphFormat

import LightGraphs: loadgraph, loadgraphs

export DOTFormat

struct DOTFormat <: AbstractGraphFormat end
# TODO: implement save

function _dot_read_one_graph(pg::DOT.Graph)
function _dot_read_one_graph(pg::Parsers.DOT.Graph)
isdir = pg.directed
nvg = length(DOT.nodes(pg))
nodedict = Dict(zip(collect(DOT.nodes(pg)), 1:nvg))
nvg = length(Parsers.DOT.nodes(pg))
nodedict = Dict(zip(collect(Parsers.DOT.nodes(pg)), 1:nvg))
if isdir
g = LightGraphs.DiGraph(nvg)
else
g = LightGraphs.Graph(nvg)
end
for es in DOT.edges(pg)
for es in Parsers.DOT.edges(pg)
s = nodedict[es[1]]
d = nodedict[es[2]]
add_edge!(g, s, d)
Expand All @@ -20,24 +29,24 @@ function _dot_read_one_graph(pg::DOT.Graph)
end

function loaddot(io::IO, gname::String)
p = DOT.parse_dot(read(io, String))
p = Parsers.DOT.parse_dot(read(io, String))
for pg in p
isdir = pg.directed
possname = isdir ? DOT.StringID("digraph") : DOT.StringID("graph")
possname = isdir ? Parsers.DOT.StringID("digraph") : Parsers.DOT.StringID("graph")
name = get(pg.id, possname).id
name == gname && return _dot_read_one_graph(pg)
end
error("Graph $gname not found")
end

function loaddot_mult(io::IO)
p = DOT.parse_dot(read(io, String))
p = Parsers.DOT.parse_dot(read(io, String))

graphs = Dict{String,LightGraphs.AbstractGraph}()
graphs = Dict{String,AbstractGraph}()

for pg in p
isdir = pg.directed
possname = isdir ? DOT.StringID("digraph") : DOT.StringID("graph")
possname = isdir ? Parsers.DOT.StringID("digraph") : Parsers.DOT.StringID("graph")
name = get(pg.id, possname).id
graphs[name] = _dot_read_one_graph(pg)
end
Expand All @@ -46,3 +55,5 @@ end

loadgraph(io::IO, gname::String, ::DOTFormat) = loaddot(io, gname)
loadgraphs(io::IO, ::DOTFormat) = loaddot_mult(io)

end #module
11 changes: 11 additions & 0 deletions src/edgelist.jl → src/Edgelist/Edgelist.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
module EdgeList

# loads a graph from an edge list format (list of srcs and dsts separated
# by commas or whitespace. Will only read the first two elements on
# each line. Will return a directed graph.

using DelimitedFiles: writedlm
using LightGraphs
using LightGraphs: AbstractGraphFormat

import LightGraphs: loadgraph, loadgraphs, savegraph

export EdgeListFormat


struct EdgeListFormat <: AbstractGraphFormat end

Expand Down Expand Up @@ -42,3 +51,5 @@ end
loadgraph(io::IO, gname::String, ::EdgeListFormat) = loadedgelist(io, gname)
loadgraphs(io::IO, ::EdgeListFormat) = Dict("graph" => loadedgelist(io, "graph"))
savegraph(io::IO, g::AbstractGraph, gname::String, ::EdgeListFormat) = saveedgelist(io, g, gname)

end
11 changes: 11 additions & 0 deletions src/gexf.jl → src/GEXF/Gexf.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
module GEXF

using GraphIO.EzXML
using LightGraphs
using LightGraphs: AbstractGraph, AbstractGraphFormat

import LightGraphs: savegraph

export GEXFFormat

# TODO: implement readgexf
struct GEXFFormat <: AbstractGraphFormat end
Expand Down Expand Up @@ -42,3 +51,5 @@ function savegexf(io::IO, g::LightGraphs.AbstractGraph, gname::String)
end

savegraph(io::IO, g::AbstractGraph, gname::String, ::GEXFFormat) = savegexf(io, g, gname)

end #module
17 changes: 15 additions & 2 deletions src/gml.jl → src/GML/Gml.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
module GML

using GraphIO.ParserCombinator.Parsers
using LightGraphs
using LightGraphs: AbstractGraphFormat

import LightGraphs: loadgraph, loadgraphs, savegraph

export GMLFormat


struct GMLFormat <: AbstractGraphFormat end

function _gml_read_one_graph(gs, dir)
Expand All @@ -19,7 +30,7 @@ function _gml_read_one_graph(gs, dir)
end

function loadgml(io::IO, gname::String)
p = GML.parse_dict(read(io, String))
p = Parsers.GML.parse_dict(read(io, String))
for gs in p[:graph]
dir = Bool(get(gs, :directed, 0))
graphname = get(gs, :label, dir ? "digraph" : "graph")
Expand All @@ -30,7 +41,7 @@ function loadgml(io::IO, gname::String)
end

function loadgml_mult(io::IO)
p = GML.parse_dict(read(io, String))
p = Parsers.GML.parse_dict(read(io, String))
graphs = Dict{String,LightGraphs.AbstractGraph}()
for gs in p[:graph]
dir = Bool(get(gs, :directed, 0))
Expand Down Expand Up @@ -85,3 +96,5 @@ loadgraph(io::IO, gname::String, ::GMLFormat) = loadgml(io, gname)
loadgraphs(io::IO, ::GMLFormat) = loadgml_mult(io)
savegraph(io::IO, g::AbstractGraph, gname::String, ::GMLFormat) = savegml(io, g, gname)
savegraph(io::IO, d::Dict, ::GMLFormat) = savegml_mult(io, d)

end # module
12 changes: 12 additions & 0 deletions src/graph6.jl → src/Graph6/Graph6.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
module Graph6

using SimpleTraits
using LightGraphs
using LightGraphs: AbstractGraphFormat

import LightGraphs: loadgraph, loadgraphs, savegraph

export Graph6Format

struct Graph6Format <: AbstractGraphFormat end

function _bv2int(x::BitVector)
Expand Down Expand Up @@ -158,3 +168,5 @@ loadgraph(io::IO, gname::String, ::Graph6Format) = loadgraph6(io, gname)
loadgraphs(io::IO, ::Graph6Format) = loadgraph6_mult(io)
savegraph(io::IO, g::AbstractGraph, gname::String, ::Graph6Format) = savegraph6(io, g, gname)
savegraph(io::IO, d::Dict, ::Graph6Format) = savegraph6_mult(io, d)

end # module
45 changes: 24 additions & 21 deletions src/GraphIO.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
module GraphIO

using LightGraphs
using SimpleTraits
using Requires

import LightGraphs: loadgraph, loadgraphs, savegraph, AbstractGraphFormat
using EzXML
using ParserCombinator.Parsers.DOT
using ParserCombinator.Parsers.GML
using CodecZlib
#=
NOTE: This is a temporary fix until we can have multiple sub-packages with their own
requirements in a single repository.
=#
function __init__()
@require CodecZlib="944b1d66-785c-5afd-91f1-9de20f533193" begin
include("LGCompressed/LGCompressed.jl")
end
@require EzXML="8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615" begin
include("GEXF/Gexf.jl")
include("GraphML/GraphML.jl")
end
@require ParserCombinator="fae87a5f-d1ad-5cf0-8f61-c941e1580b46" begin
include("DOT/Dot.jl")
include("GML/Gml.jl")
end
end

export DOTFormat, GEXFFormat, GMLFormat, Graph6Format,
GraphMLFormat, NETFormat, EdgeListFormat, CDFFormat,
LGCompressedFormat
# package code goes here

#include("jld.jl")
include("lgcompressed.jl")
include("dot.jl")
include("gexf.jl")
include("gml.jl")
include("graph6.jl")
include("graphml.jl")
include("net.jl")
include("edgelist.jl")
include("cdf.jl")
include("Graph6/Graph6.jl")
include("NET/Net.jl")
include("Edgelist/Edgelist.jl")
include("CDF/Cdf.jl")

include("deprecations.jl")

end # module
13 changes: 13 additions & 0 deletions src/graphml.jl → src/GraphML/GraphML.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
module GraphML

using GraphIO.EzXML
using LightGraphs
using LightGraphs: AbstractGraphFormat

import LightGraphs: loadgraph, loadgraphs, savegraph

export GraphMLFormat


# TODO: implement writing a dict of graphs

struct GraphMLFormat <: AbstractGraphFormat end
Expand Down Expand Up @@ -123,3 +134,5 @@ loadgraph(io::IO, gname::String, ::GraphMLFormat) = loadgraphml(io, gname)
loadgraphs(io::IO, ::GraphMLFormat) = loadgraphml_mult(io)
savegraph(io::IO, g::AbstractGraph, gname::String, ::GraphMLFormat) = savegraphml(io, g, gname)
savegraph(io::IO, d::Dict, ::GraphMLFormat) = savegraphml_mult(io, d)

end # module
14 changes: 13 additions & 1 deletion src/lgcompressed.jl → src/LGCompressed/LGCompressed.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
module LGCompressed

using GraphIO.CodecZlib
using LightGraphs
using LightGraphs: AbstractGraphFormat

import LightGraphs: loadgraph, loadgraphs, savegraph

export LGCompressedFormat

struct LGCompressedFormat <: AbstractGraphFormat end

function savegraph(fn::AbstractString, g::AbstractGraph, gname::AbstractString,
Expand Down Expand Up @@ -36,4 +46,6 @@ loadgraph(fn::AbstractString, gname::AbstractString, format::LGCompressedFormat)

loadgraph(fn::AbstractString, format::LGCompressedFormat) = loadgraph(fn, "graph", LightGraphs.LGFormat())

loadgraphs(fn::AbstractString, format::LGCompressedFormat) = loadgraphs(fn, LGFormat())
loadgraphs(fn::AbstractString, format::LGCompressedFormat) = loadgraphs(fn, LGFormat())

end # module
14 changes: 13 additions & 1 deletion src/net.jl → src/NET/Net.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
module NET

import LightGraphs
using LightGraphs
using LightGraphs: AbstractGraphFormat

import LightGraphs: loadgraph, loadgraphs, savegraph

export NETFormat


struct NETFormat <: AbstractGraphFormat end
"""
savenet(io, g, gname="g")
Expand All @@ -19,7 +30,6 @@ function savenet(io::IO, g::LightGraphs.AbstractGraph, gname::String = "g")
return 1
end


"""
loadnet(io::IO, gname="graph")
Expand Down Expand Up @@ -66,3 +76,5 @@ end
loadgraph(io::IO, gname::String, ::NETFormat) = loadnet(io, gname)
loadgraphs(io::IO, ::NETFormat) = Dict("graph" => loadnet(io, "graph"))
savegraph(io::IO, g::AbstractGraph, gname::String, ::NETFormat) = savenet(io, g, gname)

end # module
Loading

0 comments on commit 5e6207f

Please sign in to comment.