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)
Row | tree | id | symbol | scale | index | parent_id | link | Width |
---|---|---|---|---|---|---|---|---|
String? | Int64? | String? | Int64? | Int64? | Int64? | String? | Float64? | |
1 | / 1: Scene | 1 | Scene | 0 | 0 | missing | / | missing |
2 | └─ / 2: Individual | 2 | Individual | 1 | 0 | 1 | / | missing |
3 | └─ / 3: Axis | 3 | Axis | 2 | 0 | 2 | / | missing |
4 | └─ / 4: Internode | 4 | Internode | 3 | 0 | 3 | / | 0.02 |
5 | ├─ + 5: Leaf | 5 | Leaf | 3 | 0 | 4 | + | 0.1 |
6 | └─ < 6: Internode | 6 | Internode | 3 | 1 | 4 | < | 0.02 |
7 | └─ + 7: Leaf | 7 | Leaf | 3 | 0 | 6 | + | 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])
Row | tree | id | symbol | scale | index | parent_id | link | Width | Length |
---|---|---|---|---|---|---|---|---|---|
String? | Int64? | String? | Int64? | Int64? | Int64? | String? | Float64? | Float64? | |
1 | / 1: Scene | 1 | Scene | 0 | 0 | missing | / | missing | missing |
2 | └─ / 2: Individual | 2 | Individual | 1 | 0 | 1 | / | missing | missing |
3 | └─ / 3: Axis | 3 | Axis | 2 | 0 | 2 | / | missing | missing |
4 | └─ / 4: Internode | 4 | Internode | 3 | 0 | 3 | / | 0.02 | 0.1 |
5 | ├─ + 5: Leaf | 5 | Leaf | 3 | 0 | 4 | + | 0.1 | 0.2 |
6 | └─ < 6: Internode | 6 | Internode | 3 | 1 | 4 | < | 0.02 | 0.1 |
7 | └─ + 7: Leaf | 7 | Leaf | 3 | 0 | 6 | + | 0.1 | 0.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.