Ejemplo n.º 1
0
    def add_node(self, name=None, id=None, flag=0, expanded_icon=None,
                 collapsed_icon=None, selected=False, expanded=False,
                 full_parent_id=()):
        """overridden method (Tree)"""
        Tree.add_node(self, name, id, flag, expanded_icon, collapsed_icon)

        fullParentPath = self.separater.join(full_parent_id)

        if fullParentPath not in self.savedNodesMap.keys():
            self.savedNodesMap[fullParentPath] = []

        savedNode = self._getSavedNode(fullParentPath, name)
        if savedNode is not None:
            self.new_nodes[-1].selected = savedNode.selected
            self.new_nodes[-1].expanded = savedNode.expanded

            return

        savedParentNode = self._getSavedNode(self.separater.join(full_parent_id[0:len(full_parent_id) - 1]), full_parent_id[-1])

        node = Struct()
        node.name = name
        node.expandable = flag
        node.fullPath = self.separater.join(full_parent_id + (id,))
        node.expanded = expanded
        node.selected = savedParentNode.selected

        self.savedNodesMap[fullParentPath].append(node)

        self.new_nodes[-1].selected = node.selected
        self.new_nodes[-1].expanded = node.expanded
Ejemplo n.º 2
0
def main():
    """
    Gets all the input from the file and then passes it on to the bfts search
    :return:
    """
    start_pts = {}
    end_pts = {}
    puz_file = input("Name of the puzzle file: ")
    with open(puz_file) as f:
        height, num_colors = [int(x) for x in f.readline().split()]
        grid = []
        x_loc = 0
        for line in f:
            row = []
            y = 0
            for x in line.split():
                if x != "e":
                    # It's  number so we should analyze it
                    if str(x) in start_pts:
                        end_pts[x] = (int(x_loc), int(y))
                    else:
                        start_pts[x] = (int(x_loc), int(y))
                    row.append(x)
                else:
                    row.append(-1)
                y += 1
            grid.append(row)
            x_loc += 1

    tree = Tree(num_colors, height, grid, start_pts, end_pts)
    tree.bfts()
Ejemplo n.º 3
0
    def move_cursor(self, node):
        """overridden method (Tree)"""

        # save the old position
        self.oldPos = self.pos

        Tree.move_cursor(self, node)
Ejemplo n.º 4
0
def timeEvict():
    ORAMsize = (1 << 13) - 1
    z = 4000
    
    start = time.clock()
    
    t = Tree(ORAMsize, z)
    
    timetaken = time.clock() - start
    print("Took %.4f seconds to create tree" % (timetaken))
    
    exp = 10
    
    leaves = t._leaves
    
    input = random.sample(leaves, exp)
    
    num = 400 #number of tests to average
    
    for i in range(num/10):
        start = time.clock()
        
        for i in range(10):
            input = random.sample(leaves, exp)
            t.evictAll(input)
            #print(t._buckets)
        
        timetaken = time.clock() - start
        
        print("%.2f" % (timetaken/10))
    def word_ladder(self, origin, destination):
        """Constructs a word ladder between the given words using the fetched vocabulary. A word ladder is a sequence of
        words, from origin to destination, where each intermediary word changes exactly one letter in the previous word.
        All intermediate words in the ladder must be real words.
        Constructing a word ladder loosely follows the methodology of A* path finding. A tree data structure is used to
        store a collection of words and the paths between them. The tree is filled first with the destination word and
        is then traversed breadth first adding each word's legal one character substitutions. Traversal ends when any
        path has reached the origin and that path's ancestry is returned.
        The tree is traversed breadth first so that the shortest path is found in all cases. A tree begins at the
        destination and works backwards to the origin so that the chosen path's ancestry is in the correct order.
        :param origin: The starting word to construct a word ladder from.
        :param destination: The word that the ladder traverses to.
        :return: A sequence of words that constitutes a word ladder.
        """
        paths = Tree()                        # tree stores all possible paths
        paths.add_root(destination)           # start at destination so that ancestry path is in the correct order
        visited = set()                       # no need for ANY branch to revisit a word that another branch has been to

        for node in paths.breadth_first():
            if node.data == origin:                     # if node is origin, the word ladder is complete
                path = []
                for ancestor in node.ancestor_data():   # construct a path from this nodes ancestors.
                    path.append(ancestor)
                return path
            else:
                for word in self.similar(node.data):        # add each similar word to this nodes path...
                    if word not in visited:                 # ...only if it hasn't been visited by ANY other tree path
                        node.add(word)
                        visited.add(word)
        return []                                           # no path was found
Ejemplo n.º 6
0
 def __init__(self, leaf_width=15, leaf_height=0, padding=3, header_height=8, margin_x=10,
              margin_y=20):
     self._inc_tree = Tree()
     self._inc_tree_nodes = {}
     self._adj_graph = Digraph.Digraph()
     self._relation_graph = Digraph.Digraph()
     self._ordered_graph = Digraph.Digraph()
     self._compound_layer_tree = Tree()
     self._compound_layer_tree_nodes = {}
     self._inverted_edges = set()
     self._fake_vertices = set()
     self._dummy_vertices = set()
     self._adj_left = {}
     self._adj_right = {}
     self._order_service_graph = Digraph.Digraph()
     self._order_index = {}
     self._barycenter = {}
     self._x = {}
     self._y = {}
     self._width = {}
     self._height = {}
     self._leaf_width = leaf_width
     self._leaf_height = leaf_height
     self._left_top_x = 1
     self._left_top_y = 1
     self._header_height = header_height
     self._padding = padding
     self._margin_y = margin_y
     self._margin_x = margin_x
     self._drawer = DrawingFramework.DrawingFramework()
Ejemplo n.º 7
0
 def test_tree_insertion_equal_node(self):
     tree = Tree()
     tree.insert(Node.factory(12))
     tree.insert(Node.factory(12))
     self.assertIsNotNone(tree.root_node)
     self.assertIsNotNone(tree.root_node.value)
     self.assertEqual(12, tree.root_node.value)
     self.assertIsNone(tree.root_node.left.value)
     self.assertIsNone(tree.root_node.right.value)
Ejemplo n.º 8
0
def database_indexer(k,threshold, nucleotides, probabilities):
    """
    k : length of k - mer
    t : threshold value
    nucleotides : string which is the filename of our list of nucleotides
    prob : string which is the filename of our list of probabilites
    """
    t = log(threshold)
    nuc = []
    #The probabilities in string format.
    prob_string = []
    probable_emissions = {}
    
    
    with open (nucleotides) as f:
        for line in f:
            for char in line:
                nuc.append(char)
    
    with open(probabilities) as f:
        for line in f:
            prob_string = line.split(' ')
    #Convert the probability values from strings to floating point numbers
    prob = [float(i) for i in prob_string]
    
    distributions = []
    for i in range(len(nuc)):
        distributions.append(get_ith_distribution(i, nuc, prob))
    #A list which has as the element at index i the log of the maximum probability of an instantiated suffix of S starting at index i. We currently don't make any calls to this
    max_word_probability = []
    for i in range(len(distributions)):
        max_word_probability.insert(i, prob_upper_bound(distributions, i, k))
                      
    for start_idx in range(len(nuc)- k +1):
        #This is "window" of the probabilistic sequence of length k from which we will look for k - mers instantiating with probability > t
        distributions_window = distributions[start_idx:start_idx + k ]
        """
        Set up the root of the k mer tree. It currently has no children, a probability of 1, the distribution
        of the first letter of the probabilistic sequence S, and a string of k"-" characters, showing that
        all letters in the k - mer are not instantiated
        """
        base_string = list('-'*k)
    
        # Look for characters in S which are certain. This will help us minimize the number of nodes created
        for i in range(len(distributions_window)):
            if distributions[i][0][1] == 1:
                base_string[i] = distributions_window[i][0][0]

        root_distro = distributions_window[0]
        root = Node(None, [], 0, root_distro, base_string, 0)
        #Set up the tree using Gardener
        gardener(t, root, 0, k, distributions_window)
        #Once nodes have been linked, consolidate to a tree
        k_mer_tree = Tree(root)
        probable_emissions[start_idx] = k_mer_tree.harvest()
    
    return probable_emissions
Ejemplo n.º 9
0
class Surpasser:
    def __init__(self):
        self.__tree__ = None
        self.__numArray__ = list()
        self.__maxSurpasser__ = 0

    def setTree(self,tree):
        self.__tree__ = tree

    def setArray(self,numArray):
        self.__numArray__ = numArray

    def createNode(self,value):
        node = Node()
        node.setValue(value)
        return node

    def addNode(self,node,startNode):

        if node.getValue() <= startNode.getValue():
            if startNode.getLeft() == None:
                startNode.setLeft(node)
            else:
                self.addNode(node,startNode.getLeft())
        else:
            if startNode.getRight() == None:
                startNode.setRight(node)
            else:
                self.addNode(node,startNode.getRight())

    def buildTree(self):
        for i in self.__numArray__:
            if self.__tree__ == None:
                self.__tree__=Tree()
                self.__tree__.setRoot(self.createNode(i))
            else:
                startNode=self.__tree__.getRoot()
                node=self.createNode(i)
                self.addNode(node,startNode)

    def findMaxSurpasser(self,startNode):
        if startNode == None:
            return
        else:
            rightNode=startNode.getRight()
            maxNum=self.findTreeElementNum(rightNode)
            if maxNum>self.__maxSurpasser__:
                self.__maxSurpasser__=maxNum
            self.findMaxSurpasser(startNode.getLeft())


    def findTreeElementNum(self,node):
        if node == None:
            return 0
        else:
            return 1+ self.findTreeElementNum(node.getLeft())+self.findTreeElementNum(node.getRight())
Ejemplo n.º 10
0
class TextFile:
    def __init__(self, file_path):
        self.tree = Tree()
        with open(file_path) as f:
            for line in f:
                for word in line.split():
                    self.tree.insert(Node.factory(WordWrapper(word)))

    def word_count(self):
        return self.tree.size()
Ejemplo n.º 11
0
	def setUp(self):
		self.root = Tree()
		self.c1 = Tree(self.root, 1)
		self.c2 = Tree(self.root, 2)
		self.n1 = Tree(self.root)
		self.c3 = Tree(self.n1, 3)
		self.c4 = Tree(self.n1, 4)
		self.n2 = Tree(self.n1)
		self.c5 = Tree(self.n2, 5)
		self.c6 = Tree(self.n2, 6)
Ejemplo n.º 12
0
def testCORAM():
	ORAMsize = (1 << 8) - 1;
	z = 600;
	t = Tree(ORAMsize, z);

	A = 100;	
	N = A * ORAMsize / 4;

	print(z, A)
	
	for k in range(int(N/A)):
		root = [t.randomLeaf() for i in range(A)]
		t.evictAll(root)
	
	counter = 0
	while True:
		count = 0;	
		for i in range(A):
			count += t.readBlock(t.randomLeaf())

		root = [t.randomLeaf() for i in range(count)]
		
		t.evictAll(root)
		counter = counter + 1
		if (counter % 100 == 0):
			print(counter)
Ejemplo n.º 13
0
    def __init__(self, value, children, parent=None):
        """
        Create a tree whose root has specific value (a string)
        children is a list of references to the roots of the children.
        parent (if specified) is a reference to the tree's parent node
        """

        Tree.__init__(self, value, children)
        self._parent = parent
        for child in self._children:
            child._parent = self
Ejemplo n.º 14
0
 def add_tree(self, penn_tree, _id):
     """
     Makes a root node, fills up its structure from PTB format and adds it to the list of trees
     """
     tree = Tree(_id=_id)
     _class = Model.targets[_id]
     tree.set_target(_class)
     tree.root = Node()
     tree.root.read(penn_tree, 0, True)
     # Chaipi for life
     tree.root = tree.root.children[0]
     self.trees.append(tree)
Ejemplo n.º 15
0
 def create_full_tree(depth):
     if depth == 0:
         terminal = Terminal.random_terminal()
         node = Node("terminal", terminal)
         return Tree(node)
     else:
         function = Function.random_function()
         root_node = Node("function", function)
         result = Tree(root_node)
         for i in range(2):  #function.arity()
             result.add_child(Initializer.create_full_tree(depth - 1))
         return result
Ejemplo n.º 16
0
def min_height_bst(arr):
    if len(arr) == 0:
        return None
    elif len(arr) == 1:
        return Tree(arr[0])
    root = Tree(arr[len(arr) / 2])
    left = min_height_bst(arr[: len(arr) / 2])
    right = min_height_bst(arr[len(arr) / 2 + 1 :])
    if left != None:
        root.left = left
    if right != None:
        root.right = right
    return root
Ejemplo n.º 17
0
def testTrees():
	a = ArtificialPhylogeny(size=20,numChromosomes=5)
	while len(a.tree.getTips()) < 6:
		a.evolve()
	tipScores = [tip.getScore() for tip in a.tree.getTips()]
	s = a.tree.getScore()
	tipScores = [score -s for score in tipScores]
	for score in tipScores:
		assert score == 0

	copyTree = Tree(a.tree)

	if len(a.tree) != len(copyTree):
		print( len(a.tree) )
		print( len(copyTree))
		print( a.tree.getTips())
		print(copyTree.getTips())
		print(a.tree)
		print(copyTree)
		print(a.tree.genome.getName())
		print(copyTree.genome.getName())
		raise Exception("Trees are not the same size")

	sub = a.tree.subs[0]
	a.tree.breakConnection( sub )

	if len(copyTree.subs) not in (1,3):
		print( len(copyTree.subs))
		assert False

	a.tree.addConnection( sub)

	aTips = a.tree.getTips()
	for cTip in copyTree.getTips():
		assert cTip not in aTips

	tipScores = [tip.getScore() for tip in a.tree.getTips()]
	s = a.tree.getScore()
	tipScores = [score -s for score in tipScores]
	for score in tipScores:
		assert score == 0

	aTips = a.tree.getTips()
	subTips = a.tree.subs[0].getTips()
	for tip in aTips:
		assert tip in subTips

	assert 2*len(copyTree.getTips()) - 2 == len(copyTree)

	print("All tree tests passed.")
Ejemplo n.º 18
0
    def __init__(self, master, root_id, root_label='',
                 get_contents_callback=None, dist_x=15, dist_y=15,
                 text_offset=10, line_flag=1, expanded_icon=None,
                 collapsed_icon=None, regular_icon=None, plus_icon=None,
                 minus_icon=None, unchecked_icon=None, checked_icon=None,
                 greyed_checked_icon=None,node_class=MultiSelectNode, drop_callback=None,
                 node_select_callback=None,
                 *args, **kw_args):

        self.separater = ' -> '

        self.pos = None

        self.rootId = root_id
        self.realDistX = dist_x

        self.nodeSelectCallback = node_select_callback

        if checked_icon == None:
            self.checkedIcon=PhotoImage(file='checkedIcon.gif')
        else:
            self.checkedIcon=checked_icon
        if unchecked_icon == None:
            self.uncheckedIcon=PhotoImage(file='uncheckedIcon.gif')
        else:
            self.uncheckedIcon=unchecked_icon
        if greyed_checked_icon == None:
            self.greyedCheckedIcon=PhotoImage(file='greyedIcon.gif')
        else:
            self.greyedCheckedIcon=greyed_checked_icon

        if get_contents_callback == None:
            get_contents_callback = self._getContents

        savedNode = Struct()
        savedNode.name = self.rootId
        savedNode.expandable = True
        savedNode.fullPath = self.rootId
        savedNode.expanded = False
        savedNode.selected = False

        # This structure is used to remember selected nodes and their selected/expanded states
        self.savedNodesMap = {'': [savedNode], savedNode.fullPath: []}

        Tree.__init__(self, master, root_id, root_label,
                 get_contents_callback, dist_x * 2, dist_y,
                 text_offset, line_flag, expanded_icon,
                 collapsed_icon, regular_icon, plus_icon,
                 minus_icon, node_class, drop_callback,
                 *args, **kw_args)
Ejemplo n.º 19
0
	def GetTreeOfMoves ( self, piece, board ) :
		
		# Create the tree and the root
		tree = Tree()
		tree.addRoot( ( piece.line, piece.collum ) )
		children = piece.canKill( board )
		for c in children:
			tree.root.addChild( c )

		# Create the childs
		if len( tree.root.child ) > 0:
			self.GenerateTree( tree.root, board )

		return tree
Ejemplo n.º 20
0
def readfile():
    lines = []
    read = open("game2.txt","r")
    line=read.readline()
    while True:
        line = read.readline()
        print line
        lines.append(line)
        if len(line) == 0:
            break
    print lines
    read.close()
    Tree.set_head(lines[0],lines[1],lines[2]) #"set_head"
    Tree.make_tree(lines) #"Make the tree"
	def constructGraph(self, v,w, D_T_T, D_F_F):
		G = Graph()
		V = {}
		V['s'] = 0
		
		i = 1
		for v_i in v.getChildren():
			V["v" + str(v_i.getHashNum())] = i
			i += 1
			
		V['e_i'] = i
		i += 1
			
		for w_i in w.getChildren():
			V["w" + str(w_i.getHashNum())] = i
			i += 1
		
		V['e_j'] = i
		i += 1
		V['t'] = i
		
		G.V = range(len(V))
		
		G.capacity = numpy.zeros([len(V),len(V)])
		
		for v_i in v.getChildren():
			G.capacity[V['s'],V["v" + str(v_i.getHashNum())]] = 1	
			for w_i in w.getChildren():
				G.capacity[V["v" + str(v_i.getHashNum())],V["w" + str(w_i.getHashNum())]] = 1
			G.capacity[V["v" + str(v_i.getHashNum())],V['e_j']] = 1
				
		for w_i in w.getChildren():
			G.capacity[V["w" + str(w_i.getHashNum())], V['t']] = 1
			G.capacity[V['e_i'],V["w" + str(w_i.getHashNum())]] = 1
			
		G.capacity[V['s'],V['e_i']] = n_j = len(w.getChildren())
		G.capacity[V['e_j'],V['t']] = n_i = len(v.getChildren())
		G.capacity[V['e_i'],V['e_j']] = max(n_i, n_j) - min(n_i, n_j)
		
		G.cost = numpy.zeros([len(V),len(V)])
		for v_i in v.getChildren():
			for w_i in w.getChildren():
				G.cost[V["v" + str(v_i.getHashNum())], V["w" + str(w_i.getHashNum())]] = self.getConstrainedTED(v_i, w_i, D_T_T, D_F_F)
				G.cost[V['e_i'], V["w" + str(w_i.getHashNum())]] = self.getConstrainedTED(Tree.getEmptyTree(), w_i, D_T_T, D_F_F)
			G.cost[V["v" + str(v_i.getHashNum())], V['e_j']] = self.getConstrainedTED(v_i, Tree.getEmptyTree(), D_T_T, D_F_F)
		
		G.init_edges()
		G.init_cost_rev()
		return (G,V)
Ejemplo n.º 22
0
 def add_tree(self, penn_tree, _id):
     """
     Makes a root node, fills up its structure from PTB format and adds it to the list of trees
     """
     tree = Tree(_id)
     # _id == 1 is used as a sentinel value for the website
     if _id != -1:
         _class = Model.targets[_id]
         self.set_target(_class)
         tree.set_target(_class)
     tree.root = Node()
     tree.root.read(penn_tree, 0, True)
     # Chaipi for life
     tree.root = tree.root.children[0]
     self.trees.append(tree)
Ejemplo n.º 23
0
def testOverflow():
    ORAMsize = (1 << 10) - 1
    z = 1000
    t = Tree(ORAMsize, z)
    exp = 500
    
    
    leaves = t._leaves
    counter = 0
    
    while True:
            input = random.sample(leaves, exp)
            t.evictAll(input)
            counter = counter + 1
            print(str(counter) + " evictions of " + str(exp) + " blocks completed")
Ejemplo n.º 24
0
 def __init__(self):
     self.__currentLevel= list()
     self.__nextLevel = list()
     self.__maxLevel = int()
     self.__findNodeValue = int()
     self.__tree = Tree()
     self.__visitedNodes=dict()
Ejemplo n.º 25
0
 def setUp(self):
     self.treeToArray = TreeToArray()
     tree = Tree()
     node1=Node()
     node1.setValue(1)
     node2=Node()
     node2.setValue(2)
     node3=Node()
     node3.setValue(3)
     node4=Node()
     node4.setValue(4)
     tree.setRoot(node1)
     node1.setLeft(node2)
     node1.setRight(node3)
     node3.setRight(node4)
     self.treeToArray.setTree(tree)
Ejemplo n.º 26
0
def parseTreeFromXML(root, PL):
    children = root.getchildren()
    if root.tag[0] == "A":
        t = Tree("Basic", Sigma(root.tag, []), [], [], PL)
        t._isComplete=True
        return t
    else:
        tree = Tree("Complex", NT(root.tag, []), [], [], PL)
        isComplete=True
        for child in children:
            treeChild = parseTreeFromXML(child, PL)
            if not treeChild.isComplete():
                isComplete=False
            tree._children.append(treeChild)
        tree._isComplete=isComplete
        return tree 
Ejemplo n.º 27
0
	def __init__ (self):
		QtGui.QMainWindow.__init__ (self)

		self.btnCol = 1
		self.radioCol = 0

		self.upDirShortCutKey = "/"
		self.homeDirShortCutKey = "-"

		self.pyapplaunchConfigDir = ConfigManager.getPyapplaunchConfigDir()

		self.configManager = ConfigManager.ConfigManager()
		self.executionManager = ExecutionDelegateManager (self.configManager)

		self.scriptDatabasePath = self.pyapplaunchConfigDir + os.sep + "scripts.xml"

		self.tree = Tree (self.scriptDatabasePath)
		
		self.upButton = None
		self.homeButton = None

		self.initUI()
		self.buildAppButtons()
		self.createSysTray()
		self.restoreWindowPos()

		# This is an adaptor to call "self.tree.substituteEveryOccurrenceOfValue"
		# with the right parameters, since the signal we're connecting to can't
		# connect directly to the function.
		nameChangeAdaptor = lambda s1, s2: \
			self.tree.substituteEveryOccurrenceOfValue (s1, s2, "exec_with")
		self.executionManager.delegateNameChanged.connect (nameChangeAdaptor)
Ejemplo n.º 28
0
	def calculate(self):
		if len( self.trees) == 1:
			self.tree = self.trees[0]

		else:
			print("! upgma - in !")
			while len(self.trees) > 2:
				t,g = len(self.trees), len(self.genomes)
				i,j = getMatrixMin(self.distances)

				newGenome = self.grimm.midGenome(self.genomes[i], self.genomes[j])

				newTree = Tree( newGenome )
				newTree.addConnection( self.trees[i] )
				newTree.addConnection( self.trees[j] )

				self.trees[i] = newTree
				self.trees.pop(j)

				self.genomes[i] = newGenome
				self.genomes.pop(j)

				# self.distances = self.grimm.getUpdatedDistMatrix( self.genomes, self.distances, (i,j) )
				self.distances = self.grimm.getDistMatrix( self.genomes )
				if t - 1 != len(self.trees) or g - 1 != len(self.genomes):
					print newGenome
					print newTree
					print (i,j)
					print self.genomes
					print self.trees
					raise Exception
				print(";")
			print("! upgma - out !")

			if len( self.trees) == 2:
				self.tree = self.trees[0]
				self.tree.addConnection( self.trees[1] )

			else:
				print newGenome
				print newTree
				print (i,j)
				print self.trees
				print self.genomes
				raise Exception

			assert self.tree.isBinary()
Ejemplo n.º 29
0
 def test_tree_multi_level_insertion(self):
     tree = Tree()
     tree.insert(Node.factory(12))
     tree.insert(Node.factory(12))
     tree.insert(Node.factory(10))
     tree.insert(Node.factory(11))
     tree.insert(Node.factory(1))
     tree.insert(Node.factory(100))
     self.assertEqual(12, tree.root_node.value)
     self.assertEqual(100, tree.root_node.right.value)
     self.assertEqual(10, tree.root_node.left.value)
     self.assertEqual(11, tree.root_node.left.right.value)
     self.assertEqual(1, tree.root_node.left.left.value)
Ejemplo n.º 30
0
 def fit(self, X, y):
     self.base = np.mean(y)
     self.get_first_entries(X, y)
     self.trees = []
     n_features, n_bins = self.transformer.get_sizes()
     for _ in range(self.n_estimators):
         tree = Tree(self.depth, self.loss, self.entries, n_features,
                     n_bins, self.lambda_val, self.gamma)
         tree.construct()
         self.trees.append(tree)
         if tree.real_depth < 1:
             break
         for i in range(len(self.entries)):
             prediction = tree.predict(self.entries[i].x)
             self.entries[i].prediction += self.eps * prediction
             self.entries[i].g, self.entries[i].h = \
                 self.loss(self.entries[i].y, self.entries[i].prediction)
Ejemplo n.º 31
0
def create_tree_4():
    t = Tree(13)
    import random
    arr = list(range(1, 26))
    random.shuffle(arr)
    for x in arr:
        insert(t, x)
    return t
Ejemplo n.º 32
0
def splice_nodes_helper(tree, filter_func):
    spliced_children = []
    for child in tree.children:
        spliced_child_list = splice_nodes_helper(child, filter_func)
        spliced_children.extend(spliced_child_list)
    if filter_func(tree.label):
        return spliced_children
    return [Tree(tree.label, spliced_children)]
Ejemplo n.º 33
0
 def __init__(self,
              minSamples=5,
              maxDepth=5,
              usedTrees=5,
              learningRate=0.05):
     super().__init__(minSamples=minSamples, maxDepth=maxDepth)
     self.decisionTrees = [Tree() for _ in range(usedTrees + 1)]
     self.learningRate = learningRate
Ejemplo n.º 34
0
def init_tree() -> Tree:
    """
    Generates a tree that looks like
                    6
                   / \
                  5   8
                 /\   /\
                2    7  9 
               /\       /\
              1  3       10
                 /\
                   4      
    """
    tree = Tree()
    children = [6, 8, 9, 5, 2, 1, 3, 7, 10, 4]
    [tree.add_child(child) for child in children]
    return tree
Ejemplo n.º 35
0
def buildTree():

    #start = raw_input("Please enter the log file name: ")

    myFile = file("log.txt", "r")
    lines = myFile.readlines()
    myFile.close()

    t = Tree()
    for l in lines:
        tokens = l.split()
        if tokens[0] == "folder":
            t.create_node(tokens[1], tokens[0])
        else:
            t.create_node(tokens[1], tokens[0], tokens[2])

    return t
 def read_root_tree(self):
     try:
         self.read_whitespace()
         if not TreeReader.is_left_paren(self.peek()):
             return None
         return Tree(ROOT_LABEL, [self.read_tree(False)])
     except IOError:
         raise Exception("Error reading tree: %s\n" % self.ff.name)
 def read_tree(self, is_root):
     self.read_left_paren()
     label = self.read_label()
     if len(label) == 0 and is_root:
         label = ROOT_LABEL
     children = self.read_children()
     self.read_right_paren()
     return Tree(label, children)
 def __init__(self,
              minSamples=5,
              maxDepth=5,
              usedTrees=5,
              percentageOfDatapointsPerTree=0.7):
     super().__init__(minSamples=minSamples, maxDepth=maxDepth)
     self.decisionTrees = [Tree() for _ in range(usedTrees)]
     self.percentageOfDatapointsPerTree = percentageOfDatapointsPerTree
Ejemplo n.º 39
0
    def __init__(self, alignment_file, gene_to_orlg_file, cdna,  # Data input
                 tree_newick, DupLosList,  # Tree input
                 x_js, pm_model, IGC_pm, rate_variation,  # JSModel input
                 node_to_pos, terminal_node_list,  # Configuration input
                 save_file,  # Auto save file
                 force=None,  # Parameter value constraint
                 seq_index_file=None,  # Seq index file
                 nsites=None):

        self.tree = Tree(tree_newick, DupLosList, terminal_node_list, node_to_pos)
        self.data = Data(alignment_file, gene_to_orlg_file, seq_index_file=seq_index_file, cdna=cdna)
        # added new variable for jsmodel
        conf_list = count_process(self.tree.node_to_conf)
        accessible_orlg_pair = get_accessible_orlg_pair(conf_list)

        self.jsmodel = JSModel(self.tree.n_js, x_js, pm_model, self.tree.n_orlg, IGC_pm, rate_variation,
                               accessible_orlg_pair, force)
        self.node_to_pos = node_to_pos
        self.terminal_node_list = terminal_node_list
        self.root_by_dup = self.tree.is_duplication_node(self.tree.phylo_tree.root.name)
        self.save_file = save_file
        self.force = force
        if os.path.isfile(self.save_file):
            self.initialize_by_save()
            print('Loaded paramters from ' + self.save_file)
        else:
            if self.root_by_dup:
                self.x = np.concatenate((x_js, np.log([0.01] * len(self.tree.edge_list))))
            else:
                self.x = np.concatenate((x_js, np.log([0.01] * (len(self.tree.edge_list) - 1))))
            self.unpack_x(self.x)

        assert (self.jsmodel.n_orlg == self.tree.n_orlg)  # make sure n_orlg got updated
        self.ll = None  # used to store log likelihood
        self.ExpectedGeneconv = None  # Used to store expectedNumGeneconv
        self.auto_save = 0  # used to control auto save frequency

        self.nsites = nsites
        self.iid_observations = None  # Store iid_observations which only needs one time calculation
        self.observable_nodes = None
        self.observable_axes = None

        self.is_mle = False

        assert (self.self_check())
Ejemplo n.º 40
0
class TreeIterator:
    def __init__(self):
        self.__tree__ = Tree()
        self.__currentNode__ = Node()
        self.__isFirst = True

    def setTree(self,tree):
        self.__tree__=tree

    def next(self):
        tempNode=Node()
        if self.__tree__ == None:
            return False
        if self.__tree__.getRoot() == None:
            return False
        if self.__isFirst:
            self.__isFirst = False
            if not self.__tree__.getRoot().getLeft() == None:
                self.__currentNode__ = self.getLeftMostChild(self.__tree__.getRoot().getLeft())
            else:
                self.__currentNode__ = self.__tree__.getRoot()
        else:
            if not self.__currentNode__.getRight() == None:
                self.__currentNode__=self.getLeftMostChild(self.__currentNode__.getRight())
            elif self.__currentNode__.getParent() == None:
                return False
            elif self.__currentNode__ == self.__currentNode__.getParent().getLeft():
                self.__currentNode__ = self.__currentNode__.getParent()
            elif self.__currentNode__ == self.__currentNode__.getParent().getRight():
                tempNode=self.__currentNode__
                while not tempNode == None:
                    if tempNode == tempNode.getParent().getLeft():
                        self.__currentNode__ = tempNode.getParent()
                        return self.__currentNode__
                    else:
                        tempNode = tempNode.getParent()
                else:
                    return False
        return self.__currentNode__

    def getLeftMostChild(self,node):
        if not node.getLeft() == None:
            return self.getLeftMostChild(node.getLeft())
        else:
            return node
Ejemplo n.º 41
0
def construct_tree(in_order, pre_order, start_index, end_index):

    #Ensure no index error
    if start_index > end_index:
        return None

    #Constructing node
    node = Tree(pre_order[construct_tree.starter])
    
    #Updating starting index
    construct_tree.starter += 1

    #When list ends returns current node
    if start_index == end_index:
        return node

    #calculating dividing index based on pre-order list
    index = search_value(in_order, start_index, end_index, node.getValue())

    #Creating left subtree
    node.setLeft(construct_tree(in_order, pre_order, start_index, index-1))

    #Creating right subtree
    node.setRight(construct_tree(in_order, pre_order, index+1, end_index))
    
    #return the root node
    return node
Ejemplo n.º 42
0
    def test(self):
        tree = Tree()
        root = Node(label="root")
        A = Node(label="a")
        B = Node(label="b")
        C = Node(label="c")
        D = Node(label="d")

        D.add_child(A)
        D.add_child(B)
        root.add_child(D)
        root.add_child(C)

        A.set_height(0.0)
        B.set_height(0.0)
        C.set_height(0.0)
        D.set_height(1.0)
        root.set_height(2.0)

        tree.set_root(root)

        if (tree.get_leaf_count() == 3):
            print("True")
        else:
            print(tree.get_leaf_count())

        if (tree.get_newick() == "((a:1.0,b:1.0)d:1.0,c:2.0)root:0.0;"):
            print("True")
def main():
	# Read data and split into train, cross-test and test as 3:1:1
	datafile = "./Sogou_data/Sogou_webpage.mat"
	data = scio.loadmat(datafile)
	doclabel = data['doclabel']
	wordMat = data['wordMat']
	doc_number = wordMat.shape[0]
	key_number = wordMat.shape[1]
	traindata = [wordMat[i] for i in range(doc_number) if i % 5 == 0 or i % 5 == 1 or i % 5 == 2]
	trainlabel = [doclabel[i] for i in range(doc_number) if i % 5 == 0 or i % 5 == 1 or i % 5 == 2]
	crossdata = [wordMat[i] for i in range(doc_number) if i % 5 == 3]
	crosslabel = [doclabel[i] for i in range(doc_number) if i % 5 == 3]
	testdata = [wordMat[i] for i in range(doc_number) if i % 5 == 4]
	testlabel = [doclabel[i] for i in range(doc_number) if i % 5 == 4]

	time_start = time.time()
	T = Tree(traindata, trainlabel, 0)
	de_threshold = 0.001
	depth_threshold = 100
	T.Impurity()
	T.GenerateTree(de_threshold, depth_threshold)
	T.Prune(crossdata, crosslabel)
	accuracy = T.Decision(testdata, testlabel)
	accuracy_train = T.Decision(traindata, trainlabel)
	time_end = time.time()
	print("超参数(de_threshold={0}, depth_threshold={1})下的训练集正确率为{2},测试集正确率为{3},耗时{4}s".format(de_threshold,
																			  depth_threshold, accuracy_train, accuracy,
																			  time_end - time_start))
Ejemplo n.º 44
0
def HiddenScript():
    filepath = os.getcwd(
    )  #note to self: this is how you get a file's location (most of the time, at least)
    while True:
        t = Tree(filepath)
        try:
            break
        except IndexError:
            filepath = os.path.dirname(filepath)
Ejemplo n.º 45
0
    def Run(self, rawData, target, source):
        self.ProcessRawInput(rawData)
        self._tree = Tree(self._productBank)
        self._tree.SetRoot(self.InsertIntoTree(self._tree, target, None))

        def NodeVisitorFunc(node):
            NodeVisitorFunc.productionBank = self._productBank
            nodeName = node.GetName()
            if (nodeName not in NodeVisitorFunc.productionBank.keys()):
                NodeVisitorFunc.productionBank[nodeName] = (0, 0, 0)

        self._tree._nodeVisitedFunc = NodeVisitorFunc

        #self._tree.PrintTree(200, [self._tree._root])
        self.TraverseOnce(self._tree._root)

        oreNeeded = self.CalculateOre(self._tree)
        print(f"Ore needed for 1 fuel: {oreNeeded}")
Ejemplo n.º 46
0
    def __init__(
        self, treeSize, z, segmentSize, maxStashSize
    ):  # grow/shrink triggered by ratio (buckets * z) / (# of segments)
        self._z = z
        self._tree = Tree(
            treeSize, z, self._A, segmentSize
        )  #changed to fix the existing constructor on the tree class I (akiva) wrote
        self._stash = Stash.Stash(z)
        self._posMap = PosMap.PosMap()
        self._c = maxStashSize

        self._segCounter = 0

        self.useVCache = True
        self.debug = False

        self.VCacheCounter = 0
        self.totalCounter = 0
Ejemplo n.º 47
0
 def solve(self):
     # instancia classe de arvore
     t = Tree()
     # preenche a fila com pos ordem
     t.postOrder(self.tree, self.queue)
     # caminha na fila
     for i in self.queue:
         # verifica se é um operador
         if t.isOperator(i):
             # se for empilha ele
             self.stack.append(i)
             # e faz a operação
             self.verifyOperation()
         else:
             # senão, apenas empilha
             self.stack.append(i)
     # no fim retorna o resultado
     return self.stack[0]
Ejemplo n.º 48
0
def solve_it(input_data):
    read(input_data)
    root = Tree(0, int(capacity), estimate(items), None)
    makeTree(items, root, [])
    #root.printPreorder(root,'')
    traverse(root)
    ancestors(root, solution())
    print(output())
    return
Ejemplo n.º 49
0
    def aStarSearch(self, board):
        # Node(Parent Node, List of children nodes, heuristic value, depth value, board object, index, totalHeuristic)
        h = heuristicValue(board, heuristicOption)
        head = Node(None, [], h, 1, board, "0", h + 1)
        tree = Tree(head)
        self.touchOptions = self.createTouchOptions(board)
        tree.openlist.append(head)

        return self.startSearch(tree)
Ejemplo n.º 50
0
 def generateTrees(self):
     for nodeDict in self.allNodes:
         for nodeIndex in nodeDict:
             nodeDict[nodeIndex].findParent(nodeDict)
     for node in self.rootTreeNode.children:
         for treeRoot in node.children:
             t = Tree(treeRoot)
             self.allTrees.append(t)
     return self.allTrees
Ejemplo n.º 51
0
    def dfsSearch(self, board):
        # Node(Parent Node, List of children nodes, heuristic value, depth value, board object)
        # The first Node does not have a parent, no children for now, DFS does not have a heuristic,
        # a depth of 1, the initial state of the board and an empty move.
        head = Node(None, [], 0, 1, board, "0", 0)
        tree = Tree(head)
        self.touchOptions = self.createTouchOptions(board)
        tree.openlist.append(head)

        return self.startSearch(tree)
Ejemplo n.º 52
0
 def setUp(self):
     self.treeIterator=TreeIterator()
     tree=Tree()
     node1=Node()
     node2=Node()
     node3=Node()
     node4=Node()
     node5=Node()
     node6=Node()
     node7=Node()
     node8=Node()
     node9=Node()
     node12=Node()
     node2.setValue(2)
     node3.setValue(3)
     node1.setValue(1)
     node4.setValue(4)
     node5.setValue(5)
     node6.setValue(6)
     node7.setValue(7)
     node8.setValue(8)
     node9.setValue(9)
     node12.setValue(12)
     tree.setRoot(node12)
     node12.setLeft(node1)
     node12.setRight(node9)
     node9.setLeft(node2)
     node9.setRight(node4)
     node2.setRight(node8)
     node8.setLeft(node5)
     node8.setRight(node6)
     node4.setLeft(node3)
     node4.setRight(node7)
     node3.setParent(node4)
     node7.setParent(node4)
     node4.setParent(node9)
     node9.setParent(node12)
     node1.setParent(node12)
     node2.setParent(node9)
     node8.setParent(node2)
     node5.setParent(node8)
     node6.setParent(node8)
     self.treeIterator.setTree(tree)
Ejemplo n.º 53
0
    def __init__(self, alignment_file, gene_to_orlg_file, # Data input
                 seq_index_file, cdna, allow_same_codon,  # Data input                 
                 tree_newick, DupLosList,                 # Tree input
                 x_js, pm_model, IGC_pm, rate_variation,  # PSJSModel input
                 node_to_pos, terminal_node_list,         # Configuration input
                 save_file, log_file,                     # Auto save files
#                root_by_dup = False,                     # JSModel input
                 force = None,                            # PSJS Parameter value constraint
                 nsites = None, space_list = None
                 ):  

        
        self.tree = Tree(tree_newick, DupLosList, terminal_node_list, node_to_pos)
        self.data = Data(alignment_file, gene_to_orlg_file, seq_index_file = seq_index_file, two_sites = True, space_list = space_list,
                         cdna = cdna, allow_same_codon = allow_same_codon)        
        self.psjsmodel = PSJSModel(x_js, pm_model, self.tree.n_orlg, IGC_pm, rate_variation, force)
        self.node_to_pos = node_to_pos
        self.terminal_node_list = terminal_node_list
        self.root_by_dup = self.tree.is_duplication_node(self.tree.phylo_tree.root.name)
        self.save_file = save_file
        self.log_file  = log_file
        self.force = force
        self.x     = None
        if os.path.isfile(self.save_file):
            self.initialize_by_save()
            print ('Loaded paramters from ' + self.save_file)
        else:            
            if self.root_by_dup:
                self.x = np.concatenate((x_js, np.log([0.01] * len(self.tree.edge_list))))
            else:
                self.x = np.concatenate((x_js, np.log([0.01] * (len(self.tree.edge_list) - 1))))

        assert(self.psjsmodel.n_orlg == self.tree.n_orlg)  # make sure n_orlg got updated
        self.ll   = None              # used to store log likelihood
        self.ExpectedGeneconv = None  # Used to store expectedNumGeneconv
        self.auto_save = 0            # used to control auto save frequency

        self.nsites = nsites
        self.iid_observations = None  # Store iid_observations which only needs one time calculation
        self.observable_nodes = None
        self.observable_axes  = None

        assert(self.self_check())
 def transform_tree(cls, tree):
     label = tree.label
     children = tree.children
     while len(children) == 1 and not children[0].is_leaf() \
             and label == children[0].label:
         children = children[0].children
     transformed_children = []
     for child in children:
         transformed_children.append(XOverXRemover.transform_tree(child))
     return Tree(label, transformed_children)
Ejemplo n.º 55
0
    def bestFirstSearch(self, board):
        # Node(Parent Node, List of children nodes, heuristic value, depth value, board object, index, totalHeuristic)
        h = heuristicValue(board, heuristicOption)
        # Since BFS doesnt care about the position in the search space, total f = h
        head = Node(None, [], h, 1, board, "0", h)
        tree = Tree(head)
        self.touchOptions = self.createTouchOptions(board)
        tree.openlist.append(head)

        return self.startSearch(tree)
Ejemplo n.º 56
0
def parse_xml(xml_content):
    xml_content = xml_content.strip()
    if xml_content == '':
        return []
    elif not ('<' in xml_content and '>' in xml_content):
        return [Tree(xml_content)]
    # print('--------------')
    # print(xml_content[:100])
    assert xml_content[0] == '<', xml_content[:100]
    res = []
    idx = 0
    # for idx, char in xml_content:
    # while idx < len(xml_content):
    para = re.search('>', xml_content)
    if para is None:
        return Node(xml_content)
    else:
        idx = para.start()
        # print(idx, char)

        # print(xml_content[1:idx])
        node = parse_xml_tag(xml_content[1:idx].strip())
        # print(node)
        # print(node.datum)
        if node.datum[0] in ['?', '!'] or xml_content[idx - 1] == '/':
            res.append(Tree(node))
            res.extend(parse_xml(xml_content[idx + 1:]))
        else:
            m = re.search('</%s>' % node.datum, xml_content[idx:])
            # print(xml_content[idx:100])
            # print('</%s>'%node.datum)
            # print(m)
            end, new_start = m.start(), m.end()
            # print(end, new_start)
            # print(xml_content[idx+end:idx+new_start+1])
            # if xml_content[idx+1:end-1] != '':
            children = parse_xml(xml_content[idx + 1:idx + end])
            res.append(Tree(node, children))
            # else:
            # res.append(Tree(node))
            res.extend(parse_xml(xml_content[idx + new_start + 1:]))

    return res
Ejemplo n.º 57
0
def createMap(n, m):
    tree = Tree(n, m)
    graph = tree.create_graph()
    graph_ = nx.algorithms.tree.mst.minimum_spanning_tree(graph)

    array = lines_to_array(tree.graph_to_lines(graph_), n, m)

    # tree.visualize_all(graph_, array, field_width, field_height)

    plt.imshow(array, interpolation='none', cmap='gray')

    plt.xlim(0, field_width)
    plt.ylim(0, field_height)

    plt.grid(True)

    plt.show()

    return array
Ejemplo n.º 58
0
def createTree(depth, memoryLength):
    funcNodes = ['AND', 'OR', 'NOT', 'XOR']
    agents = ['P', 'O']
    tree_list = []

    # Initialize a tree
    tree = Tree()

    if depth == 1:
        # Obtain the two things needed to make a leaf
        agent = random.choice(agents)
        num = random.randrange(1, (memoryLength + 1))

        # The termination node created
        leaf = agent + str(num)

        # Set the value equal to the created leaf
        tree.add(leaf)
        tree_list.append(leaf)
    else:
        for level in range(depth):
            if level == 0:
                value = random.choice(funcNodes)

                tree.add(value)
                tree_list.append(value)
            elif level is not (depth - 1):
                for node in range(2**level):
                    value = random.choice(funcNodes)

                    tree.add(value)
                    tree_list.append(value)
            else:
                for node in range(2**level):
                    agent = random.choice(agents)
                    num = random.randrange(1, (memoryLength + 1))

                    leaf = agent + str(num)

                    tree.add(leaf)
                    tree_list.append(leaf)

    return tree, tree_list
Ejemplo n.º 59
0
class QuadTreeCodec:
    def __init__(self, quad_tree_size, min_cell_snr, debug=False):
        self.quad_tree_size = quad_tree_size
        self.min_cell_snr = min_cell_snr
        self.debug = debug
        self.tree = None

    # TODO Comment
    def get_codec_shape(self):
        return [self.quad_tree_size, self.quad_tree_size]

    # TODO Comment
    def compress(self, image: np.ndarray):
        root = QuadTreeNode(
            image, Rect(0, 0, self.quad_tree_size, self.quad_tree_size))
        self.tree = Tree(root)
        leaf_factory = CompressedLeafFactory(self.min_cell_snr)
        self.tree.top_down(leaf_factory)

    # TODO Comment
    # noinspection PyMethodMayBeStatic
    def uncompress(self, cell: np.ndarray):
        self.tree.draw(cell, self.debug)

    # TODO Comment
    def encode(self, stream: BitStream):
        self.encode_node(self.tree.root, stream)
        return stream

    # TODO Comment
    def encode_node(self, node, stream: BitStream):
        is_leaf = node.is_leaf()
        stream.write(is_leaf)
        if is_leaf:
            node.get_leaf_data().encode(stream)
        else:
            for child in node.get_children():
                self.encode_node(child, stream)
        return stream

    # TODO Comment
    def decode(self, stream: BitStream):
        return stream
Ejemplo n.º 60
0
 def setUp(self):
     self.bfs = BFS()
     tree = Tree()
     node1 = Node()
     node1.setValue(1)
     node2 = Node()
     node2.setValue(2)
     node3 = Node()
     node3.setValue(3)
     node4 = Node()
     node4.setValue(4)
     node5 = Node()
     node5.setValue(5)
     node1.addChild(node2)
     node1.addChild(node3)
     node2.addChild(node4)
     node3.addChild(node5)
     node4.addChild(node2)
     tree.setRoot(node1)
     self.bfs.setTree(tree)