Tag2Hierarchy

Tag2Hierarchy is a simple python library which is able to generate trees or hierarchical structures from a set of n-tuples.


In [7]:
import sys
import json
sys.path.append("../")

from tag2hierarchy.hierarchy import HTMLPLOT
from tag2hierarchy.hierarchy import tree2Dict
from tag2hierarchy.hierarchy import treeHandlers

In [8]:
dictTree = json.load(open("../data/myTree.json"))

In [30]:
dictTree


Out[30]:
{u'children': [{u'children': [{u'children': [{u'children': None,
       u'name': u'Q'},
      {u'children': [{u'children': None, u'name': u'S'},
        {u'children': [{u'children': None, u'name': u'U'},
          {u'children': None, u'name': u'V'}],
         u'name': u'T'}],
       u'name': u'R'}],
     u'name': u'E'},
    {u'children': None, u'name': u'F'},
    {u'children': [{u'children': None, u'name': u'O'},
      {u'children': None, u'name': u'P'}],
     u'name': u'G'}],
   u'name': u'B'},
  {u'children': [{u'children': [{u'children': None, u'name': u'M'},
      {u'children': None, u'name': u'N'}],
     u'name': u'H'},
    {u'children': [{u'children': None, u'name': u'K'},
      {u'children': None, u'name': u'L'}],
     u'name': u'I'},
    {u'children': None, u'name': u'J'}],
   u'name': u'C'},
  {u'children': None, u'name': u'D'}],
 u'name': u'A'}

In [40]:
# We create an objectTree, this is a list of trees from 

objectTree = tree2Dict.fromDictTreeToObjectTree([dictTree])

# We can generate a plot for the tree with 

HTMLPLOT.vizualizeObjectTree("../visualization/", objectTree, plotName='fromNotebook', dynamic=False)

Obtaining Tree Structure


In [13]:
actual = treeHandlers.nodeNames(objectTree)
treeHandlers.setBranch(objectTree)
#Obtain a Branch for a particular node
branchesActual = {}
for n in treeHandlers.transverseTree(objectTree):
    branchesActual[n.name] = n.myBranch

branchesActual


Out[13]:
{u'A': [u'A'],
 u'B': [u'A', u'B'],
 u'C': [u'A', u'C'],
 u'D': [u'A', u'D'],
 u'E': [u'A', u'B', u'E'],
 u'F': [u'A', u'B', u'F'],
 u'G': [u'A', u'B', u'G'],
 u'H': [u'A', u'C', u'H'],
 u'I': [u'A', u'C', u'I'],
 u'J': [u'A', u'C', u'J'],
 u'K': [u'A', u'C', u'I', u'K'],
 u'L': [u'A', u'C', u'I', u'L'],
 u'M': [u'A', u'C', u'H', u'M'],
 u'N': [u'A', u'C', u'H', u'N'],
 u'O': [u'A', u'B', u'G', u'O'],
 u'P': [u'A', u'B', u'G', u'P'],
 u'Q': [u'A', u'B', u'E', u'Q'],
 u'R': [u'A', u'B', u'E', u'R'],
 u'S': [u'A', u'B', u'E', u'R', u'S'],
 u'T': [u'A', u'B', u'E', u'R', u'T'],
 u'U': [u'A', u'B', u'E', u'R', u'T', u'U'],
 u'V': [u'A', u'B', u'E', u'R', u'T', u'V']}

In [31]:
descendants = treeHandlers.obtainDescendantsFromNode(objectTree,"B")
nodesPerLevel = treeHandlers.obtainNodesPerLevel(objectTree)
nodesAtMyLevel = treeHandlers.obtainsNodesAtMyLevel(objectTree,"F",nodesPerLevel=None)
descendantsPerLevel = treeHandlers.obtainDescendantsPerLevel(objectTree,"A")
leaves =  treeHandlers.obtainLeavesFromNode(objectTree,"A")  
nonLeaves =  treeHandlers.obtainNonLeaves(objectTree)

In [32]:
descendantsActual


Out[32]:
{u'E', u'F', u'G', u'O', u'P', u'Q', u'R', u'S', u'T', u'U', u'V'}

In [33]:
nodesPerLevel


Out[33]:
{0: [u'A'],
 1: [u'B', u'C', u'D'],
 2: [u'E', u'F', u'G', u'H', u'I', u'J'],
 3: [u'Q', u'R', u'O', u'P', u'M', u'N', u'K', u'L'],
 4: [u'S', u'T'],
 5: [u'U', u'V']}

In [34]:
nodesAtMyLevel


Out[34]:
[u'E', u'G', u'H', u'I', u'J']

In [35]:
descendantsPerLevel


Out[35]:
{0: [u'A'],
 1: [u'B', u'C', u'D'],
 2: [u'E', u'F', u'G', u'H', u'I', u'J'],
 3: [u'Q', u'R', u'O', u'P', u'M', u'N', u'K', u'L'],
 4: [u'S', u'T'],
 5: [u'U', u'V']}

In [36]:
leaves


Out[36]:
[u'Q', u'S', u'U', u'V', u'F', u'O', u'P', u'M', u'N', u'K', u'L', u'J', u'D']

In [37]:
nonLeaves


Out[37]:
[u'A', u'B', u'E', u'R', u'T', u'G', u'C', u'H', u'I']