In [1]:
import numpy as np
import pandas as pd
import json
import os
import copy
from itertools import chain
In [2]:
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"))
In [3]:
class myTree:
def __init__(self,name,children=None,childrenSet=False):
self.name = name
self.children = children
self.myBranch = []
self.childrenSet = childrenSet
In [4]:
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
In [5]:
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[:]
In [8]:
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 [9]:
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 [10]:
In [17]:
initialDict0 = []
initialNames0 = []
for a in setDictionary(readyNodes,initialDict0,initialNames0):
pass
while len(initialDict0) != 1:
joined = []
initialDict = []
initialNames = []
for a in fillDictionary(readyNodes):
pass
initialDict0 = initialDict[:]
initialNames0 = initialNames[:]
In [16]:
json.dump(initialDict[0],open("../visualization/myTree2.json","w"))
In [157]:
def transverseTree(nodes):
if nodes is not None:
for node in nodes:
yield node
for child in transverseTree(node.children):
yield child
In [150]:
def setNames(nodes,someList):
if nodes is not None:
for node in nodes:
someList.append(node.name)
yield node
for child in setNames(node.children,someList):
yield child
In [147]:
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)
except:
pass
yield node
for child in setBranch(node.children):
yield child
In [148]:
for a in setBranch(readyNodes):
pass
In [152]:
for a in transverseTree(readyNodes):
pass
namesList = []
for a in setNames(readyNodes,namesList):
pass
In [153]:
namesList
Out[153]:
In [158]:
from random import choice,sample
In [172]:
numberOfTuples = 30
ntuples = []
for ik in range(numberOfTuples):
#TRANSVERSE TREE
selectedNode = choice(namesList)
print "choice",selectedNode
for a in transverseTree(readyNodes):
if a.name == selectedNode:
print a.myBranch
ntuples.append(sample(a.myBranch,np.random.randint(0,len(a.myBranch))))
In [174]:
len(ntuples)
Out[174]:
In [165]:
Out[165]:
In [ ]: