From d5ad906e0ad2d646a2ae9cd6ea5a876623694f57 Mon Sep 17 00:00:00 2001 From: Eric Martin Feltham Date: Thu, 2 Nov 2023 21:52:13 -0400 Subject: [PATCH 1/6] Create DataFrame.jl dataframe from metagraph --- src/DataFrame.jl | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/DataFrame.jl diff --git a/src/DataFrame.jl b/src/DataFrame.jl new file mode 100644 index 0000000..66741ac --- /dev/null +++ b/src/DataFrame.jl @@ -0,0 +1,44 @@ +import DataFrames.DataFrame + +""" + DataFrame(gr::T; type = :node) where T <:AbstractMetaGraph + +Construct a DataFrame from a MetaGraph from either its node or edge properties. +""" +function DataFrame(gr::T; type = :node) where T <:AbstractMetaGraph + fl, prps, en, nu = if type == :node + :node => Int[], gr.vprops, vertices, nv + elseif type == :edge + :edge => Edge[], gr.eprops, edges, ne + else + error("specify :node or :edge") + end + + dx = DataFrame(fl) + + x = unique(reduce(vcat, values(prps))) + for y in x + for (k, v) in y + dx[!, k] = typeof(v)[] # may be a problem if type is not consistent in y? + allowmissing!(dx, k) + end + end + + dx = similar(dx, nu(gr)) + + for (i, e) in (enumerate∘en)(gr) + dx[i, type] = e + pr = props(gr, e) + for (nme, val) in pr + dx[i, nme] = val + end + end + + # remove missing when possible + for v in Symbol.(names(dx)) + if !any(ismissing.(dx[!, v])) + disallowmissing!(dx, v) + end + end + return dx +end From 76a81586c41da58abc517c09c84bd345031e72a4 Mon Sep 17 00:00:00 2001 From: Eric Martin Feltham Date: Thu, 2 Nov 2023 21:52:54 -0400 Subject: [PATCH 2/6] Update GraphDataFrameBridge.jl --- src/GraphDataFrameBridge.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/GraphDataFrameBridge.jl b/src/GraphDataFrameBridge.jl index aa50023..fb7c5b8 100644 --- a/src/GraphDataFrameBridge.jl +++ b/src/GraphDataFrameBridge.jl @@ -7,6 +7,8 @@ export MetaGraph, MetaDiGraph import MetaGraphs.MetaGraph import MetaGraphs.MetaDiGraph +include("DataFrame.jl") +export DataFrame function MetaGraph( df::T, From 71d74fd238e0427a120b04696e37c52955c930ef Mon Sep 17 00:00:00 2001 From: Eric Martin Feltham Date: Thu, 2 Nov 2023 21:55:18 -0400 Subject: [PATCH 3/6] Update DataFrame.jl add description --- src/DataFrame.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/DataFrame.jl b/src/DataFrame.jl index 66741ac..0fd3fa6 100644 --- a/src/DataFrame.jl +++ b/src/DataFrame.jl @@ -4,6 +4,9 @@ import DataFrames.DataFrame DataFrame(gr::T; type = :node) where T <:AbstractMetaGraph Construct a DataFrame from a MetaGraph from either its node or edge properties. +`gr` is a MetaGraph. +`type` is a Symbol valued either :node or :edge such that the DataFrame is populated with node or edge +properties stored in `gr`. """ function DataFrame(gr::T; type = :node) where T <:AbstractMetaGraph fl, prps, en, nu = if type == :node From 29aba6bf5a340e1e25670ad2f09ecc4501d48b2c Mon Sep 17 00:00:00 2001 From: Eric Martin Feltham Date: Thu, 2 Nov 2023 21:56:54 -0400 Subject: [PATCH 4/6] Update DataFrame.jl improve error message --- src/DataFrame.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DataFrame.jl b/src/DataFrame.jl index 0fd3fa6..ee0938b 100644 --- a/src/DataFrame.jl +++ b/src/DataFrame.jl @@ -14,7 +14,7 @@ function DataFrame(gr::T; type = :node) where T <:AbstractMetaGraph elseif type == :edge :edge => Edge[], gr.eprops, edges, ne else - error("specify :node or :edge") + error("specify type equal to :node or :edge") end dx = DataFrame(fl) From 3153a2760228b939d6c88e37a69a3fe430b5333a Mon Sep 17 00:00:00 2001 From: Eric Martin Feltham Date: Thu, 2 Nov 2023 21:57:42 -0400 Subject: [PATCH 5/6] Update DataFrame.jl format docs --- src/DataFrame.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/DataFrame.jl b/src/DataFrame.jl index ee0938b..dd330f1 100644 --- a/src/DataFrame.jl +++ b/src/DataFrame.jl @@ -4,9 +4,11 @@ import DataFrames.DataFrame DataFrame(gr::T; type = :node) where T <:AbstractMetaGraph Construct a DataFrame from a MetaGraph from either its node or edge properties. + `gr` is a MetaGraph. `type` is a Symbol valued either :node or :edge such that the DataFrame is populated with node or edge properties stored in `gr`. + """ function DataFrame(gr::T; type = :node) where T <:AbstractMetaGraph fl, prps, en, nu = if type == :node From b5c4605aa091941113c1bc0a631822368f4947b3 Mon Sep 17 00:00:00 2001 From: Eric Martin Feltham Date: Thu, 2 Nov 2023 21:58:48 -0400 Subject: [PATCH 6/6] Update DataFrame.jl improve docs --- src/DataFrame.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/DataFrame.jl b/src/DataFrame.jl index dd330f1..79dcc29 100644 --- a/src/DataFrame.jl +++ b/src/DataFrame.jl @@ -6,8 +6,11 @@ import DataFrames.DataFrame Construct a DataFrame from a MetaGraph from either its node or edge properties. `gr` is a MetaGraph. + +Optional keyword arguments: + `type` is a Symbol valued either :node or :edge such that the DataFrame is populated with node or edge -properties stored in `gr`. +properties stored in `gr`. Default is :node. """ function DataFrame(gr::T; type = :node) where T <:AbstractMetaGraph