Esempio n. 1
0
def makeTree(data, label, attributes, level, max_depth):
    level += 1
    default = majorityLabel(data[:, label])

    # Max depth check
    if max_depth == level:
        return Tree.Tree(default, level)

    if len(attributes) <= 0:
        return Tree.Tree(default, level)
    elif sameLabel(data[:, label]):
        return Tree.Tree(default, level)
    else:
        bestAttribute = chooseAttr(data, label, attributes)
        tree = Tree.Tree(bestAttribute, level)
        newAttributes = attributes
        newAttributes.remove(bestAttribute)

        for i in range(1, 11):
            examples = getExamples(data, bestAttribute, i)
            if examples[:, 1].size == 0:
                tree.addChild(Tree.Tree(default, level + 1), i)
            else:
                tree.addChild(
                    makeTree(examples, label, newAttributes, level, max_depth),
                    i)

    return tree
Esempio n. 2
0
    def fromSKLearn(self, forest, roundSplit=False):
        # TODO: Add AdaBoostRegressor
        # TODO: Add RandomForestRegressor

        if (issubclass(type(forest), AdaBoostClassifier)):
            sumW = sum([w for w in forest.estimator_weights_])

            # https://github.com/scikit-learn/scikit-learn/blob/a24c8b46/sklearn/ensemble/weight_boosting.py#L297
            # see: decision_function
            if (forest.algorithm == "SAMME"):
                for e, w in zip(forest.estimators_, forest.estimator_weights_):
                    tree = Tree.Tree()
                    tree.fromSKLearn(e, roundSplit, "SAMME", w / sumW)
                    self.trees.append(tree)
            else:
                for e in forest.estimators_:
                    tree = Tree.Tree()
                    tree.fromSKLearn(e, roundSplit, "SAMME.R", 1.0 / sumW)
                    self.trees.append(tree)
        elif (issubclass(type(forest), RandomForestClassifier)) or (issubclass(
                type(forest), ExtraTreesClassifier)):
            for e in forest.estimators_:
                tree = Tree.Tree()
                tree.fromSKLearn(e, roundSplit, "RandomForest",
                                 1.0 / len(forest.estimators_))
                self.trees.append(tree)
        else:
            raise NotImplementedError(
                "fromSKLearn() is not implemented for class ", type(forest))
Esempio n. 3
0
    def fit(self, training_file, output_file, word_map_file='word_map.bin'):

        if not os.path.exists(word_map_file):
            tr.build_word_map(training_file, word_map_file)
        self.word_map_file = word_map_file

        self.trees = tr.load_trees(self.data_folder + training_file,
                                   self.word_map_file)

        self.num_words = len(tr.load_word_map(self.word_map_file))

        self.rntn = RNTN.RNTN(self.vect_dim, self.output_dim, self.num_words,
                              self.mini_batch_size)
        self.sgd = SGD.SGD(self.rntn, self.learning_rate, self.mini_batch_size)

        for e in range(self.optim_epochs):

            # Fit model
            # --------------------------

            start = time.time()
            print "Running epoch %d" % e

            self.sgd.optimize(self.trees)

            end = time.time()
            print "\nTime per epoch : %f" % (end - start)

            # Save model specifications
            with open(output_file, 'w+') as fid:
                pickle.dump([(i, self.values[i]) for i in self.args][1:], fid)
                pickle.dump(self.sgd.cost_list, fid)
                pickle.dump(self.rntn.stack, fid)
Esempio n. 4
0
	def buildDecisionTree(self, data, threshold, attributes):
		if self.binary:
			root = Tree.BinarySplitTree()
		else:
			root = Tree.MultiSplitTree()
		mostLabel = self.mostLabel(data)
		if self.isPure(data) or len(attributes) == 0 or data.shape[0] < threshold:
			root.isLeaf = True
			root.label = mostLabel
			return root
		else:
			root.isLeaf = False
			root.entropy = self.entropy(data)
			root.mostLabel = mostLabel
			attribute, value = self.bestAttribute(data, root.entropy, attributes)
			root.attribute = attribute
			newAttributes = attributes[:]
			newAttributes.remove(attribute)
			if self.binary:
				root.value = value
				root.leftChild = self.getChild(data.loc[data[attribute] < value], threshold, newAttributes, mostLabel)
				root.rightChild = self.getChild(data.loc[data[attribute] >= value], threshold, newAttributes, mostLabel)
			else:
				for newValue in data[attribute].unique():
					dataset = data.loc[data[attribute] == newValue]
					child = self.buildDecisionTree(dataset, threshold, newAttributes)
					child.value = newValue
					root.children.append(child)
		return root
Esempio n. 5
0
    def __generate_test_case(self):
        tmp_path = self.p.get_current_test_case_path() + SETTINGS.get(
            "test_case_folder_name") + "_" + str(
                self.p.get_current_test_case_nb()) + ".test_case"
        if self.verbose:
            print("Current path: " +
                  misc.color(self.p.get_current_test_case_path(), "cyan"))
        if misc.check_file(tmp_path, self.verbose):
            if self.verbose:
                print("Parsing existing test case: " +
                      misc.color(tmp_path, "cyan"))
            self.tree.read_test_case(tmp_path)
        else:
            if self.verbose:
                print("Create new test case: " + misc.color(
                    SETTINGS.get("test_case_folder_name") +
                    ".test_case", "cyan"))

        counter = 0
        while not self.tree.generate(self.verbose):
            print(counter)
            self.tree = Tree(
                SETTINGS.get("template_path") +
                SETTINGS.get("template_file_name"))
            counter += 1
        if self.verbose:
            self.tree.print_current_node()
        self.tree.write_test_case(tmp_path)
Esempio n. 6
0
def check_perms(s, set, handles, **kwargs):
    ask_flag = kwargs.get('Ask', True)
    if not ask_flag:
        if not MyUtil.get_yn('!!! Warning !!! ask = False: Will not ask to make changes, okay? Enter N to Exit, Y to Continue:'):
            print('exiting...')
            sys.exit(0)
    for handle in handles:
        if 'Document-' in handle:
            fd = DCC.prop_get(s, handle, InfoSet = 'DocBasic')
        elif 'Collection-' in handle:
            fd = DCC.prop_get(s, handle, InfoSet = 'CollData')
        else:
            fd = DCC.prop_get(s, handle, InfoSet = 'Title')    
        fd['permissions'] = DCC.prop_get(s, handle, InfoSet = 'Perms')
#         print(fd['handle'], ':', fd['title'])    
        
        print('\n>>>>>>>>>>>>>>       DCC Information       <<<<<<<<<<<<<<')
        if 'Document-' in handle:
            DCC.print_doc_basic(fd)
        elif 'Collection-' in handle:
            DCC.print_coll_data(fd)
        else:
            print('Not Document or Collection:', handle, ':', fd['title'])   
        print('\n\tDoc Properties URL: ',Tree.url_view(handle))
        print('\tPermissions URL: ',Tree.url_perm(handle))
        print('\tGet Document URL: ',Tree.url_access(handle))

        
        print()
    
        fix_objact(s, fd, handle, set, **kwargs)
        fix_permact(s, fd, handle, set, **kwargs)
Esempio n. 7
0
    def __init__(self, actionList, learningRate, discountFactor):
        self.actionList = actionList
        self.learningRate = learningRate
        self.discountFactor = discountFactor

        #Contains information of what states+actions lead to what states
        self.TransitionTable = {}

        #Contains Q-values for state-action pairs
        self.QTable = {}

        #Contains average reward for state-action pairs
        self.ETable = {}

        #A tree structure of rewarded sequences. Each sequence is stored in reverse to quickly find equivalent sequences
        self.rewardedSequences = Tree()

        #Same as above but not stored in reverse
        self.rewardedSequencesForward = Tree()
        self.rewardedSequencesForward.node = None

        #A set with all rewarded sequences
        self.rewardedSet = set()

        #A set with all punished sequences
        self.inputSet = set()
Esempio n. 8
0
    def Format(self, data):
        rootNode = Tree.Node(nodeLabel='[' + '/'.join(self.groupByAttrs) + ']',
                             children=[],
                             nodesDict={})

        for item in data:
            node = rootNode
            for groupByAttr in self.groupByAttrs:
                groupByValue = str(rec_getattr(item, groupByAttr))
                nodesDict = node.nodesDict
                oldNode = node
                node = nodesDict.get(groupByValue, None)
                if not node:
                    node = Tree.Node(nodeLabel=groupByValue,
                                     children=[],
                                     nodesDict={})
                    oldNode.nodesDict[groupByValue] = node
                    oldNode.children.append(node)

            node.children.append(item)

        print Tree.FormatAsTree(data=rootNode,
                                labelAttr='nodeLabel',
                                labelFunc=self.labelFunc,
                                childrenAttr='children',
                                useVt100LineDrawingChars=True)
        return ''
Esempio n. 9
0
File: Main.py Progetto: iron-fe/RNTN
    def fit(self, training_file, output_file, word_map_file='word_map.bin'):

        if not os.path.exists(word_map_file):
            tr.build_word_map(training_file, word_map_file)
        self.word_map_file = word_map_file

        self.trees = tr.load_trees(self.data_folder + training_file, self.word_map_file)

        self.num_words = len(tr.load_word_map(self.word_map_file))

        self.rntn = RNTN.RNTN(self.vect_dim, self.output_dim, self.num_words, self.mini_batch_size)
        self.sgd = SGD.SGD(self.rntn, self.learning_rate, self.mini_batch_size)

        for e in range(self.optim_epochs):

            # Fit model
            # --------------------------

            start = time.time()
            print "Running epoch %d" % e

            self.sgd.optimize(self.trees)

            end = time.time()
            print "\nTime per epoch : %f" % (end-start)

            # Save model specifications
            with open(output_file, 'w+') as fid:
                pickle.dump([(i, self.values[i]) for i in self.args][1:], fid)
                pickle.dump(self.sgd.cost_list, fid)
                pickle.dump(self.rntn.stack, fid)
Esempio n. 10
0
def build_ann(exset, num_hidden, weight_decay_coef, num_iter):
    learning_rate = 0.01
    num_examples = exset.shape[0]
    num_nodes_l1 = exset.shape[1] - 2
    num_nodes_l3 = 1

    if num_hidden != 0:
        num_nodes_l2 = num_hidden
        num_nodes_per_layer = [num_nodes_l1, num_nodes_l2, num_nodes_l3]
    else:
        num_nodes_per_layer = [num_nodes_l1, num_nodes_l3]
    tree = Tree(num_nodes_per_layer)
    if num_iter < 1:  # train until convergence
        eps = 0.01  # convergence test
        delta_w = 1  # initialize delta larger than eps
        iteration = 0

        while delta_w > eps:
            w_start = tree.compute_all_weights()
            w_final = iterate_example_set(tree, exset, learning_rate,
                                          weight_decay_coef)
            iteration += np.shape(exset)[0]
            delta_w = abs(w_start - w_final)
        print iteration

    else:
        iteration = 0
        while iteration < num_iter:
            iterate_example_set(tree, exset, learning_rate, weight_decay_coef)
            iteration += np.shape(exset)[0]
    return tree
Esempio n. 11
0
    def addRewardedSequence(self, sequence, reward=None, action=None):
        nextChar = sequence[-1]
        branch = self.rewardedSequences
        for x in range(len(sequence)):
            char = nextChar
            if x < len(sequence) - 1:
                nextChar = sequence[-(x + 2)]
            else:
                nextChar = 1
            if char not in branch.connections:
                branch.connections[char] = Tree()
                branch.connections[char].node = False
            if nextChar == 1:
                branch.connections[char].node = True
            branch = branch.connections[char]

        nextChar = sequence[0]
        branch = self.rewardedSequencesForward
        for x in range(len(sequence)):
            char = nextChar
            if x < len(sequence) - 1:
                nextChar = sequence[x + 1]
            else:
                nextChar = 1
            if char not in branch.connections:
                branch.connections[char] = Tree()
                branch.connections[char].node = None
            if nextChar == 1:
                branch.connections[char].node = (action, reward)
            branch = branch.connections[char]
Esempio n. 12
0
def AddToTree(board19, Candidate, player):
    temp = []
    if (player == 10):
        enemy = 20
    else:
        enemy = 10
    for i in board19:
        temp += [i]
    evalTree = Tree(temp)
    for i in Candidate:
        for q in i[1]:
            tempboard = genNewBoard(temp, [i[0], q], player)
            tempTree = genTree(tempboard)
            tempCandidate = CandidateMoves(tempboard, enemy)
            for a in tempCandidate:
                for b in a[1]:
                    temp2 = genNewBoard(tempboard, [a[0], b], enemy)
                    temp2Tree = genTree(temp2)
                    Candidate2 = CandidateMoves(temp2, player)
                    for c in Candidate2:
                        for d in c[1]:
                            temp3 = genNewBoard(temp2, [c[0], d], player)
                            temp3Tree = genTree(temp3)
                            temp2Tree.AddSuccessor(temp3Tree)
                    tempTree.AddSuccessor(temp2Tree)
            evalTree.AddSuccessor(tempTree)
    return evalTree
def main():
    array = list(range(7))
    array = [1, 2, 4, 4, 5, 6, 7, 8]
    tree = Tree(array)
    tree.print_tree()

    print(validate_BST(tree.root))
Esempio n. 14
0
    def DisplayDevicesAsTree(self, vm, attrs, useVt100Chars):
        """
      Display virtual devices as a tree.
      """
        def FormatRecord(record):
            return ', '.join(
                [str(rec_getattr(record, attr)) for attr in attrs]).rstrip()

        def GetDevices(obj):
            if isinstance(obj, Tree.Node):
                return [
                    device for device in vm.GetAllDevices()
                    if device.controller is None
                ]
            elif hasattr(obj, 'managedObject'):
                if hasattr(obj.managedObject, 'device'):
                    return [
                        vm.GetDevice(key) for key in obj.managedObject.device
                    ]
                else:
                    return []

        rootNode = Tree.Node(nodeLabel=vm.name)

        print Tree.FormatAsTree(data=rootNode,
                                labelAttr='nodeLabel',
                                labelFunc=FormatRecord,
                                childrenFunc=GetDevices,
                                useVt100LineDrawingChars=useVt100Chars)
Esempio n. 15
0
    def compileSubroutineBody(self, subTreeNode):
        token = self.tokenizer.getNextToken()
        if token.getToken() != "{":
            raise ValueError(
                "Expeced \'{\' at start of subroutine body, but received: " +
                token.getToken())
        subTreeNode.addChild(token)

        # handle 0 or more varDecs
        nextToken = self.tokenizer.peekNextToken()
        while nextToken.getToken() == 'var':
            varDecSubTreeNode = Tree.Node("varDec", subTreeNode.depth + 1)
            self.compileVarDec(varDecSubTreeNode)
            subTreeNode.addChildTree(varDecSubTreeNode)

            nextToken = self.tokenizer.peekNextToken()

        # handle 0 or more statements
        statementsSubTreeNode = Tree.Node("statements", subTreeNode.depth + 1)
        self.compileStatements(statementsSubTreeNode)
        subTreeNode.addChildTree(statementsSubTreeNode)

        token = self.tokenizer.getNextToken()
        if token.getToken() != '}':
            raise ValueError(
                "Expeced \'}\' at end of subroutine body, but got " +
                token.getToken() + " instead.")
        subTreeNode.addChild(token)

        return subTreeNode
def background():
    """Creates the general background scenery"""
    #Creates random clouds in the sky
    for r in range(0,cloudNum):
        #Initialize variables
        x = randint(-100,1200)
        y = randint(-20,100)
        width = randint(60,90)
        height = randint(20,50)
        cloudColor = choice(["gray92","gray95","gray98"])
        
        #Create lots of ovals to represent clouds
        for i in range(0,20):
            x1 = x - randint(1, width)
            y1 = y - randint(1, height)
            x2 = x + randint(1, width)
            y2 = y + randint(1, height)
            oval = screen.create_oval(x1, y1, x2, y2, fill=cloudColor, outline=cloudColor)
        
    #Ground behind the trees
    screen.create_oval(-100,320,1100,600, fill ="gainsboro", outline ="gainsboro")

    #Trees from Tree.py
    Tree.tree(screen)
    
    #Snowy Ground
    screen.create_rectangle (0,500,1000,800,fill="snow",outline="snow")
    screen.create_polygon (0,500,50,500,100,450,250,470,400,430,500,450,650,420,700,450,800,460,1020,470,1000,600, fill="snow",outline="snow",smooth="true")

    #Scorebox
    screen.create_rectangle( 320,100,680,350, fill="#80d5ff", outline="white", width="8")
Esempio n. 17
0
    def do_generate(self, arg):
        if self.tree is None:
            print("A template must be parsed first")
            return

        if not self.p.check_path(self.auto, self.verbose):
            print("file hierarchy is incomplete!")
            return

        while True:
            start = time.time()
            self.__generate_test_case()
            end = time.time()

            with open(self.p.get_experiment_path() + "time", "a") as f:
                f.write(str(end - start) + "\n")
            print("time: " + str(end - start))

            while True:
                if self.verbose:
                    print("generate test artifact")
                self.__generate_test_artifact()

                if not self.p.next_test_artifact():
                    if not self.p.next_path():
                        return
                    break
            self.tree = Tree(
                SETTINGS.get("template_path") +
                SETTINGS.get("template_file_name"))
Esempio n. 18
0
    def compileWhileStatement(self, subTreeNode):
        token = self.tokenizer.getNextToken()
        if token.getToken() != "while":
            raise ValueError("Expected \'while\'.")
        subTreeNode.addChild(token)

        token = self.tokenizer.getNextToken()
        if token.getToken() != "(":
            raise ValueError("Expected \'(\'.")
        subTreeNode.addChild(token)

        exprSubTreeNode = Tree.Node("expression", subTreeNode.depth + 1)
        self.compileExpression(exprSubTreeNode)
        subTreeNode.addChildTree(exprSubTreeNode)

        token = self.tokenizer.getNextToken()
        if token.getToken() != ")":
            raise ValueError("Expected \')\'.")
        subTreeNode.addChild(token)

        token = self.tokenizer.getNextToken()
        if token.getToken() != "{":
            raise ValueError("Expected \'{\'.")
        subTreeNode.addChild(token)        

        whileStatementsSubTreeNode = Tree.Node("statements", subTreeNode.depth + 1)
        self.compileStatements(whileStatementsSubTreeNode)
        subTreeNode.addChildTree(whileStatementsSubTreeNode)

        token = self.tokenizer.getNextToken()
        if token.getToken() != "}":
            raise ValueError("Expected \'}\'.")
        subTreeNode.addChild(token)

        return subTreeNode
Esempio n. 19
0
def isBalanced(bt):
    if bt is None:
        return True
    d = Tree.getHeight(bt.left) - Tree.getHeight(bt.right)
    if abs(d) > 1:
        return False
    else:
        return isBalanced(bt.left) and isBalanced(bt.right)
Esempio n. 20
0
def main():
    array = list(range(7))
    tree = Tree(array)
    tree.print_tree()

    print(
        get_first_common_ancestor(tree.root.left_child.left_child,
                                  tree.root.right_child))
Esempio n. 21
0
def isBalanced(bt):
    if bt is None:
        return True
    d = Tree.getHeight(bt.left) - Tree.getHeight(bt.right)
    if abs(d) > 1:
        return False
    else:
        return isBalanced(bt.left) and isBalanced(bt.right)
Esempio n. 22
0
	def __init__(self, iface,user):
		# Save reference to the QGIS interface
		self.iface = iface
		self.message=None
		self.tree=Tree(user)
		self.message="selected"
		self.progressBar=ProgressBarWindow(True,True)# apro la progress bar con due barre
		self.progressBar.setMaximumOverall(4)
		self.ui_tree=MainWindowAlbero(iface,self.tree)
def fib_tree(n):
    if n == 1:
        return Tree(0)
    elif n == 2:
        return Tree(1)
    else:
        left = fib_tree(n-2)
        right = fib_tree(n-1)
        return Tree(left.label + right.label, (left, right))
Esempio n. 24
0
 def print_to_file(self, folder, is_pruned):
     for i in range(0, len(self.trees)):
         # Tree.print_tree(self.trees[i].root_node)
         if (is_pruned):
             Tree.write_tree_to_file(self.pruned_trees[i].root_node, i + 1,
                                     folder, is_pruned)
         else:
             Tree.write_tree_to_file(self.trees[i].root_node, i + 1, folder,
                                     is_pruned)
Esempio n. 25
0
    def Read(self):
        # yarab.program()
        # print(yarab.token_type)
        # print(yarab.token_value)
        # print(yarab.error_type)
        # print(yarab.error_type)
        # if yarab.error_type != "":
        #     Tree.process(token_value,token_type)
        # else:
        #     print('error')
        token_value = []
        string_t_value = ''

        token_type = []
        string_t_type = ''
        bool_colon_found = 0
        j = 0
        token_len = 0
        set = stat('test.txt').st_size == 0

        if set != True:
            with open('test.txt') as fp:
                for cnt, line in enumerate(fp):
                    print(line)

                    for i in line:
                        if i == ' ':
                            pass
                        elif i == ',':
                            bool_colon_found = 1

                        elif bool_colon_found == 0:
                            string_t_value += i
                        elif (bool_colon_found == 1) and (i != '\n'):
                            string_t_type += i
                    token_value.append(string_t_value)
                    token_type.append(string_t_type)
                    token_len = len(token_type)
                    bool_colon_found = 0
                    string_t_value = ''
                    string_t_type = ''
            # print(token_value)
            # print(token_type)
            yarab.token_value = token_value
            yarab.token_type = token_type
            yarab.token_len = token_len
            yarab.program()
            if yarab.error_type == "":
                Tree.process(token_value, token_type)
            else:
                QMessageBox.about(self, "error Messsage", yarab.error_type)
                print(yarab.error_type)
                # //give me the msg box for error_type
        else:
            error_type = "the file is empty"
            QMessageBox.about(self, "error Messsage", error_type)
            print(error_type)
Esempio n. 26
0
class Page(Base.Page):
    def childFactory(self, ctx, seg):
        set = {
            "Edit":         Edit.editPage,
            "Groups":       Group.editGroups,
            "GroupMod":     Group.editGroupsByGroup,
            "GroupAdd":     Group.addGroups,
            "Delete":       Delete.deletePage,
            "Add":          Add.addPage,
            "DomainAdd":    Domains.addDomain,
            "DomainDel":    Domains.delDomain,
            "Bulk":         Bulk.bulkUsers,
            "Failed":       Base.FailurePage
        }
        
        if seg in set.keys():
            return set[seg](self.avatarId, db=self.db)
        else:
            return Page(self.avatarId, self.db, seg)
    
    def render_editContent(self, ctx, data):
        return ctx.tag[
            tags.h2[self.text.userManagement],
            tags.p[self.text.usersBeginInstruction],
            tags.br,
            tags.img(src="/images/truck.png", alt="Bulk", style="vertical-align: middle"),
            " ",
            tags.a(href="Bulk/")["Bulk modifications"]
        ]

    def render_treeView(self, ctx, data):
        try:
            T = Tree.Tree("r", "Domains")
            l = Tree.retrieveTree(Settings.LDAPServer, Settings.LDAPManager, Settings.LDAPPass, 'o='+Settings.LDAPBase)
            flatL = Tree.flattenTree(l, 'o='+Settings.LDAPBase)
            flatL.sort()
            self.flatFil = []
        except Exception, e:
            flatL = []
            Utils.exceptionOccured(e)
        if not self.avatarId.isAdmin:
            for nod in flatL:
                for d in self.avatarId.domains:
                    if (d in nod):
                        self.flatFil.append(nod)
                    elif not "dm=" in nod:
                        self.flatFil.append(nod)
        else:
            self.flatFil = flatL
                    
        for node in self.flatFil:
            Tree.addPath(node, T)
    
        return ctx.tag[
            tags.div(id="TreeCont")[Tree.StanTree(T, self.cid)],
        ]
Esempio n. 27
0
    def compileIfStatement(self, subTreeNode):
        token = self.tokenizer.getNextToken()
        if token.getToken() != "if":
            raise ValueError("Expected if.")
        subTreeNode.addChild(token)

        token = self.tokenizer.getNextToken()
        if token.getToken() != "(":
            raise ValueError("Expected \'(\'.")
        subTreeNode.addChild(token)

        ifExprSubTreeNode = Tree.Node("expression", subTreeNode.depth + 1)
        self.compileExpression(ifExprSubTreeNode)
        subTreeNode.addChildTree(ifExprSubTreeNode)

        token = self.tokenizer.getNextToken()
        if token.getToken() != ")":
            raise ValueError("Expected \')\'.")
        subTreeNode.addChild(token)

        token = self.tokenizer.getNextToken()
        if token.getToken() != "{":
            raise ValueError("Expected \'{\'.")
        subTreeNode.addChild(token)

        ifStatementsSubTreeNode = Tree.Node("statements",
                                            subTreeNode.depth + 1)
        self.compileStatements(ifStatementsSubTreeNode)
        subTreeNode.addChildTree(ifStatementsSubTreeNode)

        token = self.tokenizer.getNextToken()
        if token.getToken() != "}":
            raise ValueError("Expected \'}\'.")
        subTreeNode.addChild(token)

        nextToken = self.tokenizer.peekNextToken()
        if nextToken.getToken() == "else":
            token = self.tokenizer.getNextToken()
            subTreeNode.addChild(token)

            token = self.tokenizer.getNextToken()
            if token.getToken() != "{":
                raise ValueError("Expected \'{\'.")
            subTreeNode.addChild(token)

            elseStatementsSubTreeNode = Tree.Node("statements",
                                                  subTreeNode.depth + 1)
            self.compileStatements(elseStatementsSubTreeNode)
            subTreeNode.addChildTree(elseStatementsSubTreeNode)

            token = self.tokenizer.getNextToken()
            if token.getToken() != "}":
                raise ValueError("Expected \'}\'.")
            subTreeNode.addChild(token)

        return subTreeNode
Esempio n. 28
0
def draw_picture():
    # window

    wn.clearscreen()
    wn.bgcolor("light blue")

    # sun
    sun_size_range = [100,200,150,125,175]
    random_number = random.randint(0,4)
    sun_size = sun_size_range[random_number]
    sun_pos_range = [150 ,100, 0, -100, -150]
    sun_pos = sun_pos_range[random_number]
    sun = Sun()
    sun.pen.penup()
    sun.pen.goto(sun_pos, 100)
    sun.draw(sun_size, "yellow")

    # trees
    positions = [-250, -100, 0, 100, 200]
    sizes = [100, 150, 200, 125, 175]
    tree_amount_range = [1,3,4,5,6,]
    random_number = random.randint(0,4)
    tree_amount = tree_amount_range[random_number]
    random_track = 0
    for i in range(tree_amount):
        random_number = random.randint(0, 4)
        random_track = random_number
        while (random_number == random_track):
            random_number = random.randint(0, 4)
        random_position = positions[random_number]
        tree_size = sizes[random_number]

        tree = Tree()
        tree.pen.goto(random_position, -300)
        tree.draw(tree_size)

    # Houses
    house = House()
    house.pen.penup()
    house.pen.goto(-300, -300)
    size = [100, 50, 200, 25, 75]
    space = [50, 100, 10, 25, 75]
    house_amount_range = [1,2,3,4,5]
    random_number = random.randint(0,4)
    house_amount = house_amount_range[random_number]
    color = ["blue", "orange", "cyan", "purple", "pink"]
    for i in range(house_amount):
        random_number = random.randint(0, 4)
        house_color = color[random_number]
        house_size = size[random_number]
        house_space = space[random_number]
        house.draw(house_size, house_color, house_space)

    wn.listen()
    wn.onkeypress(draw_picture, 'Up')
    wn.mainloop()
Esempio n. 29
0
    def createTreeForRule(self, rule):
        children = []
        for child in rule._alpha:
            if child._type == 'Sigma':
                children.append(Tree.Tree("Basic", child, (), [], self))
            else:
                # DEBUG: if here with a bug, you should consider that there might be a problem with the rules of the extended child"
                children.append(Tree.Tree("Complex", child, [], [], self))

        return Tree.Tree("Complex", rule._A, rule, children, self)
Esempio n. 30
0
 def render_treeView(self, ctx, data):
     try:
         T = Tree.Tree("r", "Domains")
         l = Tree.retrieveTree(Settings.LDAPServer, Settings.LDAPManager, Settings.LDAPPass, 'o='+Settings.LDAPBase)
         flatL = Tree.flattenTree(l, 'o='+Settings.LDAPBase)
         flatL.sort()
         self.flatFil = []
     except Exception, e:
         flatL = []
         Utils.exceptionOccured(e)
Esempio n. 31
0
def check_perms(s, set, handle, treename, **kwargs):
    ask_flag = kwargs.get('Ask', True)
    if not ask_flag:
        if not MyUtil.get_yn('!!! Warning !!! ask = False: Will not ask to make changes, okay? Enter N to Exit, Y to Continue:'):
            print('exiting...')
            sys.exit(0)
    tr = Tree.return_tree(s, handle, treename)
    handles = Tree.get_flat_tree(tr)
    for handle in handles:
        fix_set(s, handle, set, **kwargs)
Esempio n. 32
0
 def __init__(self, type, key_words, secondary_words, excluding_words):
     self.type_name = type
     key_words_tree = Tree.AVLTree()
     self.key_words = key_words_tree.insert_array(key_words)
     secondary_words_tree = Tree.AVLTree()
     self.secondary_words = secondary_words_tree.insert_array(
         secondary_words)
     excluding_Words_tree = Tree.AVLTree()
     self.excluding_words = excluding_Words_tree.insert_array(
         excluding_words)
Esempio n. 33
0
    def lightrun(self, list_surface=[Surface()], light_in=Light(), i_t=1):
        # i_t is reflection times and trans times
        # this function will simulate the ray, and return a tree and list_surface
        tree_light = Tree()
        tree_light.add(light_in)
        # this will be a for command for i_t
        for i in range(i_t):
            # calcute for list_surface
            # get myQueue length
            q_length = len(tree_light.myQueue)
            # run over myQueue,to add new light in tree, but we build a new list, queue is dynamic
            # select light
            list_light_temp = []
            for index in range(q_length):
                light_temp = tree_light.myQueue[index].elem
                list_light_temp.append(light_temp)

            for light_temp in list_light_temp:
                if light_temp == None:
                    # no light to reflect
                    tree_light.add(None)
                    tree_light.add(None)
                else:
                    # get p,t,surface
                    list_mpts = self.get_mp_t0_and_surface(
                        list_surface, light_temp)
                    mp_t0 = list_mpts[0]
                    surface_m = list_mpts[1]
                    # judge if surface is None
                    if surface_m == None:
                        # no reflect and transmisson
                        tree_light.add(None)
                        tree_light.add(None)
                    else:
                        # set the current light_in time
                        # find light_temp index in tree, through it's dynamic, bud no influence, cause if light_temp is finish , we dont need that
                        for i in range(len(tree_light.myQueue)):
                            if tree_light.myQueue[i].elem == light_temp:
                                index = i

                        tree_light.myQueue[index].elem.set_t(
                            np.arange(0, mp_t0[3] + mp_t0[3] / 10,
                                      mp_t0[3] / 10))
                        # set the point in surface is meeting point to save the value to plot
                        list_surface_index = list_surface.index(surface_m)
                        # mp = [mp_t0[0], mp_t0[1], mp_t0[2]]
                        # list_surface[list_surface_index].set_p(np.array(mp))

                        # calcute trans and ref
                        light_t_temp = self.transmisson2(surface_m, light_temp)
                        light_r_temp = self.reflection(surface_m, light_temp)
                        # add to tree
                        tree_light.add(light_t_temp)
                        tree_light.add(light_r_temp)
        return [tree_light, list_surface]
Esempio n. 34
0
    def compileSubroutineDec(self, subTreeNode):
        # handle constuctor, function, method
        token = self.tokenizer.getNextToken()
        if token.getToken() not in ['constructor', 'function', 'method']:
            raise ValueError(
                "Subroutine declaration must begin with \'constructor\', \'function\', \'method\'."
            )
        subTreeNode.addChild(token)

        # handle 'void' | type
        token = self.tokenizer.getNextToken()
        if token.getToken() == "void":
            subTreeNode.addChild(token)
        elif self.validateType(
                token):  # if not validateType, then we necessarily throw error
            subTreeNode.addChild(token)

        # handle subRoutineName
        token = self.tokenizer.getNextToken()
        self.validateSubroutineName(token)
        subTreeNode.addChild(token)

        # handle '('
        token = self.tokenizer.getNextToken()
        if token.getToken() != '(':
            raise ValueError("Expected \'(\' before parameter list.")
        subTreeNode.addChild(token)

        # handle parameterList
        pListSubTreeNode = Tree.Node("parameterList", subTreeNode.depth + 1)
        self.compileParameterList(
            pListSubTreeNode
        )  # needs to printed like <parameterList></paremeterList> (even if no children)
        subTreeNode.addChildTree(pListSubTreeNode)

        # handle ')'
        token = self.tokenizer.getNextToken()
        if token.getToken() != ')':
            raise ValueError("Expected \')\' after parameter list, but got " +
                             token.getToken() + " instead.")
        subTreeNode.addChild(token)

        # handle subroutineBody
        nextToken = self.tokenizer.peekNextToken()
        if nextToken.getToken() != '{':
            raise ValueError(
                "Expeced \'{\' at start of subroutine body, but received: " +
                nextToken.getToken())

        subroutineBodySubTreeNode = Tree.Node("subroutineBody",
                                              subTreeNode.depth + 1)
        self.compileSubroutineBody(subroutineBodySubTreeNode)
        subTreeNode.addChildTree(subroutineBodySubTreeNode)

        return subTreeNode
Esempio n. 35
0
def imprimir_predicciones(tree, test, header):
    os.system("cls")
    print("Imprimiendo...\n")
    os.system("pause")
    os.system("cls")
    for row in test:
        print("Actual: %s. Predicted: %s" %
              (row[-1], Tree.print_prediction(Tree.classify(row, tree.root))))
    print()
    os.system("pause")
    pass
Esempio n. 36
0
def makeParseTreeNode(p, value):
    '''Returns a Tree object containing
         as children p[1:] and a value of value'''
    toReturn = Tree()
    for element in p[1:]:
        if type(element) == type(toReturn):
            toReturn.children.append(element)
            toReturn.errors += element.errors
        else:
            # the element is not a tree. wrap it in a tree
            newElement = Tree()
            if isinstance(element, tuple):
                newElement.value = element[0]
                newElement.type = element[1]
            else:
                newElement.value = element
            toReturn.children.append(newElement)

    if isinstance(value, tuple):
        toReturn.value = value[0]
        toReturn.type = value[1]
    else:
        toReturn.value = value
    if value == "error":
        errorMessage = str(p[1][2]) + ": " + p[1][0]
        toReturn.errors.append(errorMessage)

    return toReturn
Esempio n. 37
0
def optimize_branch_fast(tlobj, tprime, children_index_list):
	"""
	Quick optimization of subset of branches (indicated by <children_index_list>)
	 in tprime while using tlobj for parameters

	children_index_list --- list of (i, j) indicating that we want to iteratively 
	         refine the branch of node label i --- node label j

	Most likely will be the 3 local branches at the new insertion point of a subtree.
	For fast optimization, relax the branch length variation to 0.1.

	Returns: final (positive) log likelihood of tprime

	NOTE: this only changes branch lengths in tprime. tlobj not affected!!
	NOTE: reversible_subtree_func currently cheats by only recalc-ing the
	      entries of parent and up, to ensure this works, I think making sure
		  copy_{S|P} are correct is important and therefore the two g() calls.
	"""
	g = MyMat.calc_likelihood
	meat = scipy.optimize.fmin_l_bfgs_b

	def reversible_subtree_func(copy_S, copy_P, parent, child, t_a):
		#assert len(t_a) == 1

		child.edge_length = t_a[0]
		order = Tree.postorder_cheat_traverse(parent)

		L_single = g(tlobj.single_model.gtr.R, copy_S, tlobj.log_freq_single, \
				order, range(tlobj.ncol), tlobj.nnode, tlobj.ncol, tlobj.nbase)
		L_paired = g(tlobj.paired_model.gtr.R, copy_P, tlobj.log_freq_paired, \
				order, range(tlobj.ncol_p), tlobj.nnode_p, tlobj.ncol_p, tlobj.nbase_p)

		ans = -(L_single.sum() + L_paired.sum())
		return ans

	# TODO: make this more efficient!
	copy_S = tlobj.S.copy()
	copy_P = tlobj.P.copy() 
	order = Tree.postorder_assign_then_traverse(tprime, None, False)
	g(tlobj.single_model.gtr.R, copy_S, tlobj.log_freq_single, \
		order, range(tlobj.ncol), tlobj.nnode, tlobj.ncol, tlobj.nbase)
	g(tlobj.paired_model.gtr.R, copy_P, tlobj.log_freq_paired, \
		order, range(tlobj.ncol_p), tlobj.nnode_p, tlobj.ncol_p, tlobj.nbase_p)

	changed = True
	while changed:
		changed = False
		for i, j in children_index_list:
			parent = tprime.find_node_with_label(i)
			child = tprime.find_node_with_label(j)
			old_t_a = child.edge_length
			func = lambda x: reversible_subtree_func(copy_S, copy_P, parent, child, x)
			x, fx, d = meat(func, [old_t_a], approx_grad=True, \
					bounds=[(1e-3, 10)], pgtol=1e-2)
#			print "calling func {0}--{1} done".format(i,j), x, fx, d, x[0], old_t_a, abs(x[0] - old_t_a)
			if d['warnflag'] != 0:
				return None, None # handle this appropriately!
			if abs(x[0] - old_t_a) > 0.1:
				changed = True
	return fx, tprime
Esempio n. 38
0
def NNI(t):
	"""
	Randomly select an internal node to do NNI
	alters the tree <t>

	Returns: t, (new) order
	"""
	while True:
		parent = random.choice(t.internal_nodes())
		# choose one of the kids as target
		# and another as sibling
		target, sibling = random.sample(parent.child_nodes(), 2)
		if target.is_leaf():
			continue
		else:
			# select one children from target to swap w/ sibling
			child = random.choice(target.child_nodes())
			break

	print >> sys.stderr, "NNI: parent {0}, target {1}, sibling {2}, child {3}".format(\
			parent.label, target.label, sibling.label, child.label)

	# swap child & sibling in tree
	new_child_branch = child.edge_length + target.edge_length
	new_sibling_branch = sibling.edge_length - target.edge_length
	parent.remove_child(sibling)
	target.remove_child(child)
	parent.add_child(child, new_child_branch)
	target.add_child(sibling, new_sibling_branch)

	# obtain new order via postorder traversal (should be fast enough)
	order = Tree.postorder_assign_then_traverse(t, None, do_assign=False)

	return t, order
Esempio n. 39
0
	def optimize_branch_func(self, parent, child, t_a):
		"""
		<t_a> is the new branch length between <parent> --- <child>
		(t_a is dressed in an array...should always be just [t_a])

		We can cheat & speed up likelihood calculation
		 by re-using likelihods of subtrees below child and T'
		 where T' is the subtree under root that does not contain parent/child

		Returns: POSITIVE sum of log likelihood (so we try to minimize it)

		currently wrapped up by ::optimize_branch::
		NOTE: it is ::optimize_branch::'s responsibility to update
		      self.order and self.like when all is done!!!

		p.s. potentialy speed-up by simultaneously optimizing two child branches
		     of the same parent??
		"""
		#assert len(t_a) == 1
		g = MyMat.calc_likelihood
		
		child.edge_length = t_a[0]
		# TODO: make this more efficient!
		cheat_order = Tree.postorder_cheat_traverse(parent)

		L_single = g(self.single_model.gtr.R, self.S, self.log_freq_single, \
				cheat_order, range(self.ncol), self.nnode, self.ncol, self.nbase)
		L_paired = g(self.paired_model.gtr.R, self.P, self.log_freq_paired, \
				cheat_order, range(self.ncol_p), self.nnode_p, self.ncol_p, self.nbase_p)
		return -(L_single.sum() + L_paired.sum())
Esempio n. 40
0
	def __init__(self, msa, tree, single_model, paired_model, treat_gap_as_missing):
		"""
		Input:

		msa  --- MSA object (the alignment)
		tree --- initially is the starting tree (dendropy.Tree)
		single/paired model -- EvoModel.{single|paired}model objects

		Also has the following attributes:
		single_cols --- list of unpaired positions
		paired_cols --- list of paired positions (i, j)
		order --- postorder traversal (often changes! beware!)
		like --- positive log likelihood (often changes! beware!)
		nnode, ncol, nbase for single/paired parameters...

		NOTE: ENFORCES tree TO BE BINARY
		"""
		self.msa = msa
		self.tree = tree
		self.single_model = single_model
		self.paired_model = paired_model
		self.treat_gap_as_missing = treat_gap_as_missing
		self.log_freq_single = log(self.single_model.Frequency)
		self.log_freq_paired = log(self.paired_model.Frequency)

		self.single_cols = msa.single_cols()
		self.paired_cols = msa.BP.items()
		self.paired_cols.sort()

		self.nnode = 2*msa.nseq + 1
		self.ncol = len(self.single_cols)
		self.nbase = 5
		self.nnode_p = self.nnode
		self.ncol_p = len(self.paired_cols)
		self.nbase_p = 25

		Tree.make_tree_binary(self.tree) # must preceded self.order!

		self.order = Tree.postorder_assign_then_traverse(tree, list(msa.ids))
		self.like = None # should be the positive log likelihood

		self.S = None # likelihood matrix for single positions
		self.P = None # likelihood matrix for paired positions
Esempio n. 41
0
def setupDefaultTree( idx, pname, listCodeList ):
	""" 프로젝트가 생성될시 디폴트 트리를 구성합니다 """
	
	# 프로젝트 정보 트리에 등록 
	treeList = [{ "text": pname, "pos":"000", "pidx": idx, "place": "PROJECT", "ntype": "PAGE", "icon":"ico-project", "gourl": "/project/view2/"+ str(idx), "issystem": True }]
	
	# 기본 리스트 트리에 등록	  
	for nListCode in listCodeList:
		alist = gLister[nListCode]
		treeList.append({
			"text":  alist.Title, 
			"pos":	 "001", 
			"pidx":  idx,
			"place": "PROJECT", 
			"icon":  alist.IconCls,
			"ntype": "LIST", 
			"gourl": "/lister/list2/%s/%s" % (nListCode, idx),
			"issystem": True})
	
	Tree.save( treeList )
	return
Esempio n. 42
0
    def reset_trees(self):
        self.__canvas = CanvasApp(self, 6, 8)
        self.__tpa = Tree(PointerAlgorithm())
        self.__ms1 = MH(1)
        self.__ms2 = MH(2)

        fill_tree(self.__tpa, self.__canvas)

        self.__ms1.add_gui(self.__canvas.add_circle(0, 0, 0, fill="white", outline="black", width=2, name="MS 1"))
        self.__tpa.put_ms_into_node_name(self.__ms1, 7)
        self.__ms2.add_gui(self.__canvas.add_circle(0, 0, 0, fill="white", outline="black", width=2, name="MS 2"))
        self.__tpa.put_ms_into_node_name(self.__ms2, 18)
Esempio n. 43
0
	def reversible_subtree_func(copy_S, copy_P, parent, child, t_a):
		#assert len(t_a) == 1

		child.edge_length = t_a[0]
		order = Tree.postorder_cheat_traverse(parent)

		L_single = g(tlobj.single_model.gtr.R, copy_S, tlobj.log_freq_single, \
				order, range(tlobj.ncol), tlobj.nnode, tlobj.ncol, tlobj.nbase)
		L_paired = g(tlobj.paired_model.gtr.R, copy_P, tlobj.log_freq_paired, \
				order, range(tlobj.ncol_p), tlobj.nnode_p, tlobj.ncol_p, tlobj.nbase_p)

		ans = -(L_single.sum() + L_paired.sum())
		return ans
Esempio n. 44
0
File: Main.py Progetto: iron-fe/RNTN
    def test(self, specs_file, data_test_file):

        trees = tr.load_trees(self.data_folder + data_test_file)
        assert specs_file is not None, "Please provide a model to test."

        with open(specs_file, 'r') as fid:
            _ = pickle.load(fid)
            _ = pickle.load(fid)
            rntn = RNTN.RNTN(self.output_dim, self.num_words, self.mini_batch_size)
            rntn.stack = pickle.load(fid)

        print "Testing..."
        cost, correct, total = rntn.cost_and_gradients(trees, test=True)
        print "\nCost : :%f \nCorrectly labelled : %d/%d \nAccuracy : %f." % (cost, correct, total, correct / float(total))
Esempio n. 45
0
File: Main.py Progetto: iron-fe/RNTN
    def fit(self, word_map_file='word_map.bin'):

        data_train_file = self.opts.training_file

        print '================\nTRAINING\n================'

        # If the word map file does not exist, create it.
        if not os.path.exists(word_map_file):
            tr.build_word_map(data_train_file, word_map_file)
        self.word_map_file = word_map_file

        # Load trees and set the RNTN.
        self.trees = tr.load_trees(self.opts.data_folder + data_train_file, self.word_map_file)
        self.num_words = len(tr.load_word_map(self.word_map_file))

        self.rntn = RNTN.RNTN(vec_dim=self.opts.vect_dim,
                              output_dim=self.opts.output_dim,
                              num_words=self.num_words,
                              mini_batch_size=self.opts.mini_batch_size)

        self.sgd = optimizer.SGD(self.rntn, self.opts.learning_rate, self.opts.mini_batch_size)

        for e in range(self.opts.optim_epochs):

            # Fit model.
            # After the training phase, model specifications
            # are in self.rntn.stack.
            # --------------------------

            start = time.time()
            print "Running epoch %d" % e

            self.sgd.optimize(self.trees)

            end = time.time()
            print "Time per epoch : %f" % (end-start)
Esempio n. 46
0
File: Main.py Progetto: iron-fe/RNTN
    def test(self):

        data_test_file = self.opts.testing_file

        print '\n================\nTESTING\n================'

        trees = tr.load_trees(self.opts.data_folder + data_test_file)

        # Load the test RNTN.
        test_rntn = RNTN.RNTN(self.opts.vect_dim,
                              self.opts.output_dim,
                              self.num_words,
                              self.opts.mini_batch_size)

        test_rntn.params = self.rntn.params
        test_rntn.V, test_rntn.W, test_rntn.b, test_rntn.W_s, test_rntn.b_s = \
            self.rntn.V, self.rntn.W, self.rntn.b, self.rntn.W_s, self.rntn.b_s
        test_rntn.L = self.rntn.L

        cost, correct, total = test_rntn.cost_and_updates(trees, 1, test=True)
        print "Cost : %f, Correct : %d/%d, Acc %f %%" % (cost, correct, total, 100 * correct / float(total))
Esempio n. 47
0
	def rearr(t, log_l, rL, rU):
		for target in t.postorder_node_iter():
			if target.parent_node is None:
				break
			# first make a deep copy of the tree
			t_prime = dendropy.Tree(t)
			print "finding subtree rearrangement for {0}".format(target.label)
			dest = cello.subtree_rearrangement(t_prime, target, rL, rU)
			print "inserting subtree {0} into branch above {1}".format(target.label, dest.label)
			try:
				# attach the subtree of <target> to branch above <dest>
				t_prime, affected = Subtree.move_subtree(t_prime, target.label, dest.label)
			except Exception:
				print "could not succesfully do the move, forget it! move on!"
				continue
			cheat_order = Tree.postorder_traverse_with_cheat(t_prime, affected)
			# the three local branche we need to optimize are edges of
			# target, dest, and parent of target
			lst = []
			for i, (k, bunch) in enumerate(cheat_order):
				for j, (a, t_a) in enumerate(bunch):
					if a in (target.label, dest.label, target.parent_node.label):
						lst.append((i, j))
			print 'list is', lst
			log_l_prime = MyMat.optimize_branch_fast(lst, cheat_order, msa, single_model, paired_model, S, P)
			if log_l_prime < log_l:
				print "subtree rearrangement improved log like from {0}-->{1}, keep!".format(log_l, log_l_prime)
				t = t_prime
				log_l = log_l_prime
				# put in the fastly optimized branch lengths
				for i, j in lst:
					t.find_node_with_label(cheat_order[i][1][j][0]).edge_length = cheat_order[i][1][j][1]
				raw_input("continue?")
				return
			else:
				print "subtree rearrangment made log likelihood worse. Move on"
				raw_input("continue")
				return
		return t, log_l
Esempio n. 48
0
def p_empty(p):
    '''empty :'''
    p[0] = Tree()
    p[0].value = "empty"
Esempio n. 49
0
from Tree import *
from Node import *
from numpy import random
from random import randint
from math import log
import numpy as np


c = Counter()
count = []
logn = []
for k in range(1,50):
  n = int(5*((1.2)**k))
  j = randint(1,n)
  N = Tree(c)
  logn.append(log(n))
  for i in range(1,n+1):
    j = randint(1,n)
    N.insert(j)
  #N.delete(N.root)
  avg = N.c.query()/n
  N.c.reset()
  count.append(avg)
  #inorder(N.root)
  #print(count)


import matplotlib.pyplot as plt

logn = np.array(logn)
count = np.array(count)
Esempio n. 50
0
"""
Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height.
"""

import Tree

def createMBST_a(ary, start, end):
    if end < start:
        return None
    mid = int((start + end) / 2)
    n = Tree.Node(ary[mid])
    n.left = createMBST_a(ary, start, mid - 1)
    n.right = createMBST_a(ary, mid + 1, end)
    return n

def createMBST(ary):
    return createMBST_a(ary, 0, len(ary) - 1)

ary = [0, 1, 2, 3, 4, 5, 6]
n = createMBST(ary)
Tree.levelorderTraversal(n)
Esempio n. 51
0
def isBST2(bt):
    return isBST2_a(bt, -99999, 99999)

def isBST2_a(bt, min, max):
    if bt is None:
        return True
    if bt.data <= min or bt.data > max:
        return False
    if not isBST2_a(bt.left, min, bt.data) or not isBST2_a(bt.right, bt.data, max):
        return False
    return True

bt = Tree.Node(1)
bt.left = Tree.Node(0)
bt.right = Tree.Node(20)
Tree.levelorderTraversal(bt)
print()
print(isBST(bt))
print(isBST2(bt))

bt2 = Tree.Node(10)
bt2.left = bt
Tree.levelorderTraversal(bt2)
print()
print(isBST(bt2))   # wrong
print(isBST2(bt2))

bt2 = Tree.Node(10)
bt2.left = bt
bt2.right = Tree.Node(9)
Tree.levelorderTraversal(bt2)
Esempio n. 52
0
 def visualize(self, filename):
     Tree.eteVisualize(self.toEteTree(), filename)
Esempio n. 53
0
For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.
"""

import Tree

def isBalanced(bt):
    if bt is None:
        return True
    d = Tree.getHeight(bt.left) - Tree.getHeight(bt.right)
    if abs(d) > 1:
        return False
    else:
        return isBalanced(bt.left) and isBalanced(bt.right)

# Balanced tree
bt = Tree.createPerfectTree(3)
Tree.levelorderTraversal(bt)
print()
print(Tree.getHeight(bt))
print(isBalanced(bt))

# Unbalanced tree
bt = Tree.Node(0)
bt.left = Tree.Node(1)
bt.right = Tree.Node(2)
bt2 = Tree.Node(3)
bt2.left = bt
Tree.levelorderTraversal(bt2)
print()
print(Tree.getHeight(bt2))
print(isBalanced(bt2))
Esempio n. 54
0
def create_DCC_mirror(s, dcc_handle, dirpath, **kwargs):
    tr = Tree.get_tree(s,dcc_handle)
    Tree.print_tree(s, tr)
    docList = Tree.get_flat_tree(tr)
    traverse(s, tr, tr['root']['collections'][0], dirpath, **kwargs)
Esempio n. 55
0
def p_statement_newline(p):
    '''simple_statement : newline_opt_comment'''
    p[0] = Tree()
    p[0].value = "empty"
Esempio n. 56
0
def main():
	s = RandomFSSequence( no_of_shifts=2, min_length=50, max_length=100 )
	s.generate()
	
	print s.info( "without UGA" )
	
	# a Sequence object
	#t = BiologicalSequence( s.sequence )
	#t = BiologicalSequence( "GCTGGTGGGGTAGCAGGTGTTTCTGTTGACTTGATATTATTTCCTCTGGATACCATTAAAACCAGGCTGCAGAGTCCCCAAGGATTTAGTAAGGCTGGTGGTTTTCATGGAATATATGCTGGCGTTCCTTCTGCTGCTATTGGATCCTTTCCTAATG" )
	t = BiologicalSequence( "AAATGACGAACACAGAGGAAAGAAGAGAGGCAACTGCTGAGGTCCCCTAGGCCTTTGAGAAAACGGAGTTGTACCTTTGGCAACATAAGTGCATATCTACAAGAAAGGCGATAATGTAGACACCAAGGGAATGGGTACTGTCCAAAAAGAAATGCCTCACAAATGTCACCATGGCAAAACTAAAAGAGTCTACAAAGTTACCTAGCATGCTGTTGGCATCATTGTAAACAAACAAGTTAAGGGCAAGATTCTTGCCAAGAGAATTAATATGCATATTGGGCATATTAAGCACTCTAAGAGCCAAGATGATTTCCTGAAAGTGTGTGAAGGAAAATAACCAGCATAAAGAGGGAAGCTAAAGAGAAACCTGAAGCTGCAGCCTGTTCCACCCAGAGAAGCACACTTTGTAAGAACCAATGAAAAGGAGCCTGAGCTGCTGGAGTCTATTAACTGAATTCATGGT" )
	#t = RandomSequence( 100 )
	#t.generate()
	# t.stops.append( "UGA" )
		
	print 
	print t.info()
	for i in xrange( 3 ):
		print t.colour_frame( i, sep="" )
	print "         |"*(( s.length )//10 )
	
	t.get_stop_sequence()
	
	print "The raw stop sequence (%s)..." % len( t.stop_sequence )
	print t.stop_sequence
	print
	
	# now to create paths
	p = Paths( t.stop_sequence )
	
	print "The sanitised stop sequence (%d)..." % len( p.unique_frame_sequence )
	print p.unique_frame_sequence
	print
	
	print "Create the branches from the stop sequence..."
	p.create_branches()
	print
	
	print "Create a tree..."
	T = Tree()
	
	print "View and graft the branches..."
	for B in p.branches:
		#print B
		T.graft( B )
	
	print T
	
	print "Build the paths..."
	
	paths = T.get_paths( simplify=True )
	print paths
	print
	
	for frame in xrange( 3 ):
		print "Frameshift sequences for frame %d:" % frame
		for j in T.get_frame_paths( frame ):
			print len( j ), " : ", j
		print
	
	"""
	frameshifted_sequence, fragments = t.frameshift_from_path( all_paths[0] )
	q = BiologicalSequence( s.sequence )
	print s.info()
	for i in xrange( 3 ):
		print q.colour_frame( i, sep="" )
	print "         |"*(( s.length )//10 )
	print
	
	print " ".join( fragments )
	print
	
	print t.path
	print t.colour_frameshifted_sequence( sep="" )
	print "         |"*(( s.length )//10 )
	"""
	
	for i in xrange( 3 ):
		all_paths = T.get_frame_paths( i )
		for a in all_paths:
			frameshifted_sequence, fragments, frameshift_signals = t.frameshift_from_path( a )
			print t.path
			print t.colour_frameshifted_sequence( sep="" )
			print " ".join( fragments )
			print " ".join( frameshift_signals )
			print
Esempio n. 57
0
def checkPerms(target, permissions):
    # Login to DCC
    s = DCC.login(CF.dcc_url + CF.dcc_login)

    if 'Collection' in target:
        tr = Tree.get_tree(s,target)
        Tree.print_tree(s, tr)
        docList = Tree.get_flat_tree(tr)
        
    else:
        docList = [target]
    
    printCheckCriteria(target, permissions)
    passList = []
    failList = []

    for doc in docList:
        checkFlag = True
        if 'Document' in doc:
            fd = DCC.prop_get(s, doc, InfoSet = 'DocBasic')
            fd['permissions'] = DCC.prop_get(s, doc, InfoSet = 'Perms', Depth = '0')
    
            print("\n\n*** Document Entry", fd['handle'], "***")
            print("DCC Name: \"",fd['title'],"\"",sep="")
            print("TMT Document Number: ", fd['tmtnum'])
            print("https://docushare.tmt.org/docushare/dsweb/ServicesLib/" + fd['handle'] + "/view")
        elif 'Collection' in doc:
            fd = DCC.prop_get(s, doc, InfoSet = 'CollData')
            fd['permissions'] = DCC.prop_get(s, doc, InfoSet = 'Perms', Depth = '0')
            print("\n\n*** Collection Entry", fd['handle'], "***")
            print("https://docushare.tmt.org/docushare/dsweb/ServicesLib/" + fd['handle'] + "/view")
        else:
            checkFlag = False
            print("\nNot checking permissions on object that is not a Collection or Document):",doc) 

        if checkFlag:
            OkayPerms = []
            for perm in sorted(fd["permissions"]["perms"], key = lambda x: x["handle"]):
                # Go through each set and create a dictionary of entries that have perms okay
                for sets in permissions:
                    for item,plist in sets.items():
                        if perm["handle"] == item:
                            if printCheckPerms(perm,plist) == True:
                                OkayPerms.append(item)
            permFlag = False
            for sets in permissions:
                testFlag = True
                for item in sets:
                    if not item in OkayPerms:
                        testFlag = False
                if testFlag == True:
                    permFlag = True
                        
            if permFlag == True:
                print("*** PERMISSIONS MEET CRITERIA ***")
                passList.append(doc)
            else:
                print("!!! PERMISSIONS DO NOT MEET CRITERIA !!!")
            failList.append(doc)
            
    return([passList,failList])
Esempio n. 58
0
                           default=0)
    parser.add_argument('--maxChildren', '-M',
                           help="The maximum number of children by nodes",
                           type=int,
                           default=8)
    parser.add_argument('--alpha', '-a',
                           help="The 'a' parameter of the Weibull distribution "
                           "used to create the task length.",
                           type=float,
                           default=1)
    parser.add_argument('--scale', '-s',
                           help="The mean time of each task.",
                           type = float,
                           default=1)
    parser.add_argument('--filename', '-f',
                           help="The filename to save the tree.",
                           default="tree.txt")

    args = parser.parse_args()
    
    if args.minChildren > args.maxChildren:
        args.maxChildren = args.minChildren + 1
    
    tree = Tree.Tree(args.height,
                args.minChildren,
                args.maxChildren,
                args.alpha,
                *Tree.calibrate(args.scale))
    Tree.exportTree(tree, args.filename)
    print("Generated :\n{0}".format(tree))
Esempio n. 59
0
        tree = None,
        branch_scale = 0,
        height_scale = 0,
        )

    (options, args) = Experiment.Start( parser, add_pipe_options = True )

    if options.filename_tree:
        tree_lines = open(options.filename_tree, "r").readlines()
    elif options.tree:
        tree_lines = options.tree
    else:
        raise "please supply a species tree."

    nexus = TreeTools.Newick2Nexus( tree_lines )
    Tree.updateNexus( nexus )
    tree = nexus.trees[0]

    if options.loglevel >= 2:
        tree.display()
    
    plot = SVGTree( tree )

    plot.setBranchScale( options.branch_scale )
    plot.setHeightScale( options.height_scale )    
    
    if options.colour_by_species:
        rx = re.compile(options.species_regex)
        extract_species = lambda x: rx.search( x ).groups()[0]
        plot.setDecoratorExternalNodes( NodeDecoratorBySpecies( tree, extract_species= extract_species ) )