In [133]:
import numpy as np
import pandas as pd
import json
import os
import copy
from itertools import chain

In [134]:
tree = [{"name":"A",
        "children":[{"name":"B","children":[{"name":"E","children":[{"name":"Q","children":None},
                                                                    {"name":"R","children":[{"name":"S","children":None},
                                                                                            {"name":"T","children":[{"name":"U","children":None},
                                                                                                                    {"name":"V","children":None}]}]}]},
                                            {"name":"F","children":None},
                                            {"name":"G","children":[{"name":"O","children":None},{"name":"P","children":None}]}]},
                    {"name":"C","children":[{"name":"H","children":[{"name":"M","children":None},
                                                                    {"name":"N","children":None}]},
                                            {"name":"I","children":[{"name":"K","children":None},
                                                                    {"name":"L","children":None}]},
                                            {"name":"J","children":None}]},
                   {"name":"D","children":None}]}]
json.dump(tree[0],open("../visualization/myTree.json","w"))

Store All Branches


In [135]:
class myTree:
    def __init__(self,name,children=None,childrenSet=False):
        self.name = name
        self.children = children
        self.myBranch = []
        self.childrenSet = childrenSet

In [136]:
def myDf(nodes,Leaves):
    if nodes is not None:
        for node in nodes:
            print "node name ", node["name"]
            if(node["children"] == None):
                Leaves.append(node["name"])
            yield node
            for child in myDf(node['children'],Leaves):
                yield child

def setObjects(nodes,Nodes):
    """
    Define intial set of nodes which have empty children
    """
    if nodes is not None:
        for node in nodes:
            if(node["children"]==None):
                Nodes.append(myTree(node["name"],childrenSet=True))
            else:
                Nodes.append(myTree(node["name"]))
            yield node
            for child in setObjects(node['children'],Nodes):
                yield child
                
def startNodes(Nodes):
    toIntegrate = [node for node in Nodes if node.childrenSet==True]
    return toIntegrate

def setChildren(nodes):
    if nodes is not None:
        for node in nodes:
            
            #CHECKING IF ALL CHILDRENS ARE READY=================
            childrensReady = []
            l = []
            if node["children"] is not None:
                for cN in node["children"]:
                    if cN["name"] in readyNodesStrings0:
                        childrensReady.append(True)
                        for a in readyNodes0:
                            if a.name == cN["name"]:
                                l.append(a)
                    else:
                        childrensReady.append(False)
            #=====================================================
            
            
            #HERE WE JOIN KNOWING THAT ALL CHILDRENS ARE READY
            if(False not in childrensReady and childrensReady != []):
                print node["name"]
                readyNodes.append(myTree(node["name"],l[:],childrenSet=True))
                readyNodesStrings.append(node["name"])
                joined.extend([n["name"] for n in node["children"]])
                print joined
        
            #HERE WE KEEP SINCE IT WAS READY BEFORE
            elif(node["name"] in readyNodesStrings0 and node["name"] not in joined):
                for a in readyNodes0:
                    if a.name == node["name"]:
                        readyNodesStrings.append(node["name"])
                        readyNodes.append(a)
            for child in setChildren(node['children']):
                yield child

Full Set Of Objects


In [137]:
Nodes = []
for a in setObjects(tree,Nodes):
    pass

Nodes0 = Nodes[:]
readyNodes0 = startNodes(Nodes)
readyNodesStrings0 = [n.name for n in startNodes(Nodes)]

while len(readyNodes0) != 1:
    readyNodes = []
    readyNodesStrings = []
    joined = []
    for a in setChildren(tree):
        pass

    readyNodes0 = readyNodes[:]
    readyNodesStrings0 = readyNodesStrings[:]


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

Reverse Task


In [35]:
def setDictionary(nodes,initialDict,initialNames):
    if nodes is not None:
        for node in nodes:
            if node.children == None:
                initialDict.append({"name":node.name,"children":node.children})
                initialNames.append(node.name)
            for child in setDictionary(node.children,initialDict,initialNames):
                yield child

In [100]:
joined = []
initialDict = []
initialNames = []
def fillDictionary(nodes):
    if nodes is not None:
        for node in nodes:
            try:
                numberOfChildren = len(set(node.children))
                childrenNames = [a.name for a in node.children]
                if len(set(childrenNames).intersection(set(initialNames0))) == numberOfChildren:
                    print node.name
                
                    thisDict = []
                    for nodeDict in initialDict0:
                        if nodeDict["name"] in childrenNames:
                            thisDict.append(nodeDict)

                    initialDict.append({"name":node.name,"children":thisDict})
                    initialNames.append(node.name)
                    joined.extend(childrenNames)
                elif(node.name in initialNames0 and node["name"] not in joined):
                    for nodeDict in initialDict0:
                        if nodeDict["name"] == node.name:
                            initialDict.append(nodeDict)
                            initialNames.append(node.name)
            except:
                if(node.name in initialNames0 and node.name not in joined):
                    for nodeDict in initialDict0:
                        if nodeDict["name"] == node.name:
                            initialDict.append(nodeDict)
                            initialNames.append(node.name)
                yield node
            for child in fillDictionary(node.children):
                yield child

In [101]:
initialDict0 = []
initialNames0 = []

for a in setDictionary(readyNodes,initialDict0,initialNames0):
    pass

In [108]:
initialDict = []
initialNames = []
for a in fillDictionary(readyNodes):
    pass
initialDict0 = initialDict[:]
initialNames0 = initialNames[:]


A

In [110]:
json.dump(initialDict[0],open("../visualization/myTree2.json","w"))

Set Branches


In [142]:
def transverseTree(nodes):
    if nodes is not None:
        for node in nodes:
            print node.name
            print node.myBranch
            yield node
            for child in transverseTree(node.children):
                yield child

In [139]:
def setBranch(nodes):
    if nodes is not None:
        for node in nodes:
            try:
                nodeChildrens = [n.name for n in node.children]
                branch = node.myBranch[:]
                branch.append(node.name)
                for n in node.children:
                    n.myBranch.extend(branch)
                print nodeChildrens
            except:
                print None
            yield node
            for child in setBranch(node.children):
                yield child

In [140]:
for a in setBranch(readyNodes):
    pass


['B', 'C', 'D']
['E', 'F', 'G']
['Q', 'R']
None
['S', 'T']
None
['U', 'V']
None
None
None
['O', 'P']
None
None
['H', 'I', 'J']
['M', 'N']
None
None
['K', 'L']
None
None
None
None

In [143]:
for a in transverseTree(readyNodes):
    pass


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

In [ ]: