Converting MTGs

We can do a lot using the MTG format, but sometimes we want our data in another format.

That's why MultiScaleTreeGraph.jl provide functions to convert an MTG into a DataFrame or into a Graph.

MTG to DataFrame

To convert an MTG into a DataFrame, you can simply use this command:

df = DataFrame(mtg, :Width)
7×8 DataFrame
Rowtreeidsymbolscaleindexparent_idlinkWidth
String?Int64?String?Int64?Int64?Int64?String?Float64?
1/ 1: Scene1Scene00missing/missing
2└─ / 2: Individual2Individual101/missing
3 └─ / 3: Axis3Axis202/missing
4 └─ / 4: Internode4Internode303/0.02
5 ├─ + 5: Leaf5Leaf304+0.1
6 └─ < 6: Internode6Internode314<0.02
7 └─ + 7: Leaf7Leaf306+0.1

This will convert your MTG into a DataFrame along with the selected variable (here the Width). The node MTG is always reported in new columns:

  • tree: a pretty-printing of the MTG
  • id: the unique ID of the node in the whole MTG
  • symbol: the node symbol
  • scale: the node scale
  • index: the node index
  • parent_id: the node's parent id
  • link: the link between the node and its parent

It is also possible to get several attributes as columns by passing their names as a vector:

DataFrame(mtg, [:Width, :Length])
7×9 DataFrame
Rowtreeidsymbolscaleindexparent_idlinkWidthLength
String?Int64?String?Int64?Int64?Int64?String?Float64?Float64?
1/ 1: Scene1Scene00missing/missingmissing
2└─ / 2: Individual2Individual101/missingmissing
3 └─ / 3: Axis3Axis202/missingmissing
4 └─ / 4: Internode4Internode303/0.020.1
5 ├─ + 5: Leaf5Leaf304+0.10.2
6 └─ < 6: Internode6Internode314<0.020.1
7 └─ + 7: Leaf7Leaf306+0.10.2

MTG to MetaGraph

We can convert an MTG into a MetaGraph using MetaGraph():

mg = MetaGraph(mtg)
Meta graph based on a Graphs.SimpleGraphs.SimpleDiGraph{Int64} with vertex labels of type Int64, vertex metadata of type Dict{Symbol, Any}, edge metadata of type String, graph metadata given by "MTG", and default weight 1.0

This is particularly useful if you want to benefit from the algorithm provided by Graphs.jl and MetaGraphsNext.jl, such as writing into more standard formats such as DOTFormat or MGFormat (or any other available from GraphIO.jl), plotting with GraphPlot.jl or NetworkLayout.jl, or computing e.g. flow with GraphsFlows.jl.