Skip to content

Input formats

s.ciccolella edited this page Jun 25, 2020 · 3 revisions

MP3 can read files and string in DOT format or it can build the structure from a networkx AGraph tree.

Multi-labeled trees and poly-occurring labels are always automatically handled. However the user need to specify wether the tree is fully-labeled, i.e. all nodes are labeled, or partially labeled, i.e. only some of the nodes are labeled.

Fully-labeled trees

A fully-labeled tree is a tree where all the nodes are labeled.

The input file/string must be a valid Graphviz format with the following assumptions:

  • each label in a node must be separated by a , (comma).
  • if a label attribute is missing the labeling of the will default to it's ID name

Example with label attributes:

digraph Tree {
    1 [label="A"];
    2 [label="B,G"];
    3 [label="C"];
    4 [label="D"];
    5 [label="E"];
    6 [label="F"];
    1 -> 2;
    1 -> 3;
    2 -> 4;
    2 -> 5;
    3 -> 6;
}

Example without label attributes:

digraph G {
	0->1;
	1->"2,3";
	"2,3"->4;
	1->"6,5";
}

Load trees from Command Line Interface (CLI)

mp3treesim path/to/tree1.gv path/to/tree2.gv

Load trees in Module

import mp3treesim as mp3

# Read file
tree1 = mp3.read_dotfile('path/to/tree.gv')

# Read from string
gv2 = '''
    digraph G {
        0->1;
        1->"2,3";
        "2,3"->4;
        1->"6,5";
    }
'''
tree2 = mp3.read_dotstring(gv2)

Partially-labeled trees

A partially labeled tree is a tree where not all nodes are labeled. The input must have a label attribute for each labeled node, while the tree without the attribute will be considered unlabeled.

It is necessary to use the labeled-only/labeled_only option as described below.

Example:

digraph G {
    1 [label="A,B,C"];
    3 [label="D"];
    5 [label="E,F,G"]
	0->1;
	1->3;
	3->4;
	0->5;
}

Load trees from Command Line Interface (CLI)

mp3treesim path/to/tree1.gv path/to/tree2.gv --labeled-only

NOTE: both tree1 and tree2 will be considered as partially-labeled.

Load trees in Module

import mp3treesim as mp3

# Read file
tree1 = mp3.read_dotfile('path/to/tree.gv', labeled_only=True)

# Read from string
gv2 = '''
    digraph G {
        0->1;
        1->"2,3";
        "2,3"->4;
        1->"6,5";
    }
'''
tree2 = mp3.read_dotstring(gv2, labeled_only=True)

Networkx AGraph

It is possible, but not recommended, to build the structure directly from a networkx AGraph, please note that the previous consideration for the label attribute still apply here. This is only possible using MP3 as a Python module.

import mp3treesim as mp3

# T is a networkx.nx_agraph

# Fully-labeled tree
tree = mp3.build_tree(T)

#Partially-labeld tree
tree = build_tree(T, labeled_only=True)

Clone this wiki locally