def test_05_creates_a_svg_document_with_name_of_root_node(self):
     mock_dp = Mock(spec=DependencyGraph)
     mock_dp.hierarchy = Tree()
     mock_dp.hierarchy.create_node('test', 'test')
     svgf = SvgFormatter(mock_dp)
     test_svg = svgf.convert_hierarchy_to_svg()
     test_svg_fn = test_svg.filename
     self.assertEqual(
         test_svg_fn, 'test_dependency_graph.svg',
         'Did not create a SVG doc with right height for number of nested dicts - actual = {0}'
         .format(test_svg_fn))
Example #2
0
    def test_modify_node_identifier_directly_failed(self):
        tree = Tree()
        tree.create_node("Harry", "harry")
        tree.create_node("Jane", "jane", parent="harry")
        n = tree.get_node("jane")
        self.assertTrue(n.identifier == 'jane')

        # Failed to modify
        n.identifier = "xyz"
        self.assertTrue(tree.get_node("xyz") is None)
        self.assertTrue(tree.get_node("jane").identifier == 'xyz')
Example #3
0
 def test_root_removal(self):
     t = Tree()
     t.create_node(identifier="root-A")
     self.assertEqual(len(t.nodes.keys()), 1)
     self.assertEqual(t.root, 'root-A')
     t.remove_node(identifier="root-A")
     self.assertEqual(len(t.nodes.keys()), 0)
     self.assertEqual(t.root, None)
     t.create_node(identifier="root-B")
     self.assertEqual(len(t.nodes.keys()), 1)
     self.assertEqual(t.root, 'root-B')
Example #4
0
def visualise_tree_from_slice(data):
    tree = Tree()
    for index, item in data.iterrows():
        if not tree.contains(item['id']):
            if item['parent_id'] == 1 or item['parent_id'] ==2:
                tree.create_node(item['answer'], item['id'])
            else:
                tree.create_node(item['answer'], item['id'], parent=item['parent_id'])
    tree.show()
    return tree
    pass
Example #5
0
def new_string_tree(s, id):
    """
    This function creates new tree for a string beggining.
    The function returns the created tree.
    """
    new_tree = Tree()
    new_tree.create_node(s, id)

    tree_list.append(new_tree)
    all_strings.append(s)
    return new_tree
Example #6
0
 def setUp(self):
     tree = Tree()
     tree.create_node("Harry", "harry")
     tree.create_node("Jane", "jane", parent="harry")
     tree.create_node("Bill", "bill", parent="harry")
     tree.create_node("Diane", "diane", parent="jane")
     tree.create_node("George", "george", parent="diane")
     tree.create_node("Mary", "mary", parent="diane")
     tree.create_node("Jill", "jill", parent="george")
     tree.create_node("Mark", "mark", parent="jane")
     self.tree = tree
Example #7
0
    def test_modify_node_identifier_recursively(self):
        tree = Tree()
        tree.create_node("Harry", "harry")
        tree.create_node("Jane", "jane", parent="harry")
        n = tree.get_node("jane")
        self.assertTrue(n.identifier == 'jane')

        # Success to modify
        tree.update_node(n.identifier, identifier='xyz')
        self.assertTrue(tree.get_node("jane") is None)
        self.assertTrue(tree.get_node("xyz").identifier == 'xyz')
Example #8
0
def main():
    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument('--json', help='filename')
    parser.add_argument('--dotoutput', help='filename')
    parser.add_argument('--pngoutput', help='filename', default='output.png')
    url_prefix = os.getenv('URL_PREFIX')
    parser.add_argument('--url', help='filename', default=url_prefix)

    args = parser.parse_args()

    tree = Tree()

    data = read_json_data(args.json)
    if 'nodes' in data:
        for x in data['nodes']:
            name = x['name']
            key = ''
            parent = None
            if 'key' in x:
                key = get_key(name, x['key'])
            else:
                key = get_key(name)
            if 'parent' in x:
                parent = get_key(x['parent'])
                #print (name, key, parent, x['parent'])
            tree.create_node(name, key, parent)
    print(map_keys)
    tree.show()
    x = recursive(tree.to_dict())
    if 'edges' in data:
        for d in data['edges']:
            src = get_key(d['src'])
            dst = get_key(d['dst'])
            color = 'black'
            if 'color' in d:
                color = d['color']
            x.edge(src, dst, color=color)
    #print (x.source)
    if args.dotoutput:
        #print (json.dumps(data))
        with open(args.dotoutput, 'w') as f:
            f.write(x.source)
    if args.url:
        url = '%s' % (args.url)
        r = requests.post(url, json={"datatype": "dotty", "data": x.source})
        js = json.loads(r.content)
        id = js['uuid']
        url = '%s/%s/png' % (args.url, id)
        print(url)
        r = requests.get(url, stream=True)
        print(r)
        if r.status_code == 200:
            with open(args.pngoutput, 'wb') as out_file:
                shutil.copyfileobj(r.raw, out_file)
Example #9
0
def make_tree(df):
    tree = Tree()
    df = set_chief_level(df)
    df = df.sort_values(by='chief_level')
    df.reset_index(inplace=True)

    for _, row in df.iterrows():
        if row['chief_level'] == 0:
            tree.create_node(tag=row['name'], identifier=row['name'])
        else:
            tree.create_node(tag=row['name'], identifier=row['name'], parent=row['chief'])
    return tree
Example #10
0
def Test1():
    tree = Tree()
    tree.create_node("NodeRoot", "root", data="root data",
                     parent=None)  # root node
    tree.create_node("Branch1", "node1", data="node1 data", parent="root")
    tree.create_node("Branch2", "node2", parent="root")
    tree.create_node("Branch11", "node11", parent="node1")
    tree.create_node("Branch121", "node12", parent="node1")
    tree.create_node("Branch111", "node111", parent="node12")

    data = [{
        "name":
        "root",
        "value":
        1,
        "children": [{
            "name": "root-child1",
            "value": 2
        }, {
            "name": "root-child2",
            "value": 3,
            "children": [{
                "name": "root-child2-child1",
                "value": 4
            }]
        }]
    }]
    data3 = [{
        'name':
        'A',
        'children': [{
            'name':
            'B',
            'children': [{
                'name': 'bar',
                'children': [{
                    'name': 'bar'
                }, {
                    'name': 'Bar'
                }]
            }, {
                'name': 'Bar'
            }]
        }, {
            'name': 'C'
        }]
    }]

    tree.show()
    # tree.
    # print(tree)
    store(tree, "test.dat")
    return tree
    def build_tree(self, resource_tree):
        """
        Build ASCI tree and upload to S3.
        """

        try:
            os.chdir(tempfile.gettempdir())
            tree = Tree()
            
            for aws in resource_tree:
                aws_key = aws
                tree.create_node(aws, aws_key)

                for region in resource_tree.get(aws):
                    region_key = aws_key + region
                    tree.create_node(region, region_key, parent=aws_key)

                    for service in resource_tree.get(aws).get(region):
                        service_key = region_key + service
                        tree.create_node(service, service_key, parent=region_key)

                        for resource_type in resource_tree.get(aws).get(region).get(service):
                            resource_type_key = service_key + resource_type
                            tree.create_node(resource_type, resource_type_key, parent=service_key)

                            for resource in resource_tree.get(aws).get(region).get(service).get(resource_type):
                                resource_key = resource_type_key + resource
                                tree.create_node(resource, resource_key, parent=resource_type_key)
            
            try:
                _, temp_file = tempfile.mkstemp()
            
                try:
                    tree.save2file(temp_file)
                except:
                    self.logging.error("Could not generate resource tree.")
                    return None

                client = boto3.client('s3')
                bucket = os.environ['RESOURCETREEBUCKET']
                key = 'resource_tree_%s.txt' % datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
                
                try:
                    client.upload_file(temp_file, bucket, key)
                except:
                    self.logging.error("Could not upload resource tree to S3 's3://%s/%s'." % (bucket, key))
                    return None

                self.logging.info("Resource tree has been built and uploaded to S3 's3://%s/%s'." % (bucket, key))
            finally:
                os.remove(temp_file)
        except:
            self.logging.critical(str(sys.exc_info()))
Example #12
0
    def retrieve_dependencies(self, jarName):
        if jarName is None:
            root = self.tree.get_node(self.tree.root)
            root = root.data.jarName
        else:
            root = jarName

        tgfOutput = subprocess.Popen('dosocs2 dependencies ' + root, stdout=subprocess.PIPE, shell=True)
        count = 0
        tree = Tree()
        dependencies = []
        relationships = []
        while True:
            line = tgfOutput.stdout.readline()

            if not line:
                break

            match = re.match(r"(\d+) - (.*)", line)
            if match:
                if count == 0:
                    count = count + 1
                    tree.create_node(match.group(2), match.group(1))
                else:
                    dependencies.append((match.group(2), match.group(1)))

            match = re.match(r"(\d+) (\d+)", line)

            if match:
                relationships.append((match.group(1), match.group(2)))

        if not relationships:
            print("No child relationships for " + jarName)
            return None

        while relationships:
            for item in relationships:
                node = tree.get_node(item[0])

                if node is not None:
                    rel = [item for item in relationships if int(item[0]) == int(node.identifier)]
                    if rel is not None:
                        rel = rel[0]
                        dep = [item for item in dependencies if int(item[1]) == int(rel[1])]
                        if dep is not None:
                            dep = dep[0]
                            tree.create_node(dep[0], dep[1], parent=node.identifier)
                            relationships.remove(rel)
                            dependencies.remove(dep)

        tree.show()
        if jarName is None:
            os.chdir(os.pardir)
Example #13
0
def generateTree(rootFile):
    files = rootFile.load()

    rootId = rootFile.id
    rootTag = rootFile.tag()
    myTree = Tree()
    myTree.create_node(rootTag, rootId)

    for file in files:
        myTree.paste(rootId, generateTree(file))

    return myTree
Example #14
0
 def init_tree(self):
     """create the tree. Add a root; available action will add the first level of children"""
     # self.traversed_ceiling = 1
     self.tree = Tree()
     root = self.tree.create_node('root',
                                  identifier='root',
                                  data={
                                      'traversed': 0,
                                      'ev': 0,
                                      'stats': 1,
                                      'cum_stats': 1
                                  })
def treeify_full_path(q: Queryable) -> None:
    tree = Tree()
    events = q.to_list()
    for event in events:
        path = list(reversed(event.data.full_path))
        for idx, node in enumerate(path):
            if tree.get_node(node):
                continue
            parent = path[idx - 1] if idx > 0 else None
            parent_node = tree.get_node(parent)
            tree.create_node(node, node, parent_node)
    tree.show(line_type="ascii-em")
Example #16
0
    def test_export_to_dot_empty_tree(self):
        empty_tree = Tree()
        export_to_dot(empty_tree, 'tree.dot')

        expected = """\
digraph tree {
}"""
        self.assertTrue(os.path.isfile('tree.dot'), "The file tree.dot could not be found.")
        generated = self.read_generated_output('tree.dot')

        self.assertEqual(expected, generated, 'The generated output for an empty tree is not empty')
        os.remove('tree.dot')
Example #17
0
 def test_all_nodes_itr(self):
     """
     tests: Tree.all_nodes_iter
     Added by: William Rusnack
     """
     new_tree = Tree()
     self.assertEqual(len(new_tree.all_nodes_itr()), 0)
     nodes = list()
     nodes.append(new_tree.create_node('root_node'))
     nodes.append(new_tree.create_node('second', parent=new_tree.root))
     for nd in new_tree.all_nodes_itr():
         self.assertTrue(nd in nodes)
Example #18
0
        def parse_totree(self,html_list):
            title_flag=True
            self.tree = Tree()
            tree = self.tree
            tree.create_node('root', 'root', data='partition')
            j = 0
            head=0

            h_level = 1
            id_key=None
            for i in range(0, len(html_list)):
                word = html_list[i]
                # 判断是否满足正则表达式,可否作为节点
                flag, key = self.is_node(self.level_words, word)

                # 这里说明一下:之前是认为层级是固定的,即如果一级是一。二级是1.那么二。3)这种就会报错,因为3)不会分为二级
                # 这里改过来了之后,前提条件是[一二三四五六七八九十]+、这个是每个文档都不会改变的,然后每一个层级都根据当前一级标题重新构造
                if key==r'[一二三四五六七八九十壱弐参四伍〇]+、':
                    h_level=1
                    self.level_key = {0: ['root']}
                    self.key_level = {}
                    self.level_one.append(key + str(j))



                if flag:
                    # 根据j来构建独一无二的id
                    id_key = key + str(j)
                    # 确定属于第几层
                    if key not in self.key_level:
                        self.key_level[key] = h_level
                        h_level += 1
                    c_level = self.key_level[key]
                    # 把第几层的id存起来
                    if c_level not in self.level_key:
                        self.level_key[c_level] = [id_key]
                    else:
                        self.level_key[c_level].append(id_key)
                    # 归属父节点即当前上一层最后一个id,即最近的上一层ID
                    tree.create_node(word, id_key, self.level_key[c_level - 1][-1], data=[word])
                    j += 1
                else:
                    # 如果当前不是一个节点,则把内容归属上一次最近的节点
                    if id_key:
                        tree.get_node(id_key).data.append(word)
                        tree.get_node(id_key).tag+=word
                    else:
                        # 记录文章标题,即第一出现的不是节点
                        if title_flag:
                            self.title=word
                            title_flag=False
                        tree.create_node(word,key+str(head),'root', data=[word])
                        head+=1
def generate_tree(number_of_nodes):
    tree = Tree()

    for i in range(0, number_of_nodes):
        if i == 0:
            tree.create_node("0", "0")  # create the root first
        else:
            # pick a random previous node tobe the parent
            parent_node = random.randint(0, i - 1)
            tree.create_node(str(i), str(i), parent=str(parent_node))

    return tree
Example #20
0
 def _construct_node_tree(self):
     self.node_tree = Tree()
     root = 'ROOT_ALL_ORG_NODE'
     self.node_tree.create_node(tag='', identifier=root, parent=None)
     for index, node in enumerate(self.nodes):
         tag = "{}.{}({})".format(index + 1, node.name, node.assets_amount)
         key = node.key
         parent_key = key[:node.key.rfind(':')] or root
         self.node_tree.create_node(tag=tag,
                                    identifier=key,
                                    data=node,
                                    parent=parent_key)
def permute_leaves(T):
    tree = Tree(tree=T, deep=True)
    leaves = get_leaf_node_ids(tree)
    shuf_leaves = leaves[:]
    random.shuffle(shuf_leaves)
    for k, nid in enumerate(leaves):
        new_nid = shuf_leaves[k]
        tree.update_node(nid, tag=new_nid, identifier='L' + str(k))
    for k, nid in enumerate(leaves):
        new_nid = shuf_leaves[k]
        tree.update_node('L' + str(k), identifier=new_nid)
    return tree
Example #22
0
    def test_import(self):
        # filename = "../data/test_et_v03_fails.xml"
        filename = "data/test_et_v03_fails.xml"

        # read file
        xml_root = ET.parse(filename).getroot()
        tree = Tree()
        tree_root = tree.create_node('root')
        importer = OPSA_Importer()
        importer.parse(xml_root, tree, tree_root)

        self.assertTrue(isinstance(tree, Tree))
Example #23
0
        def build_tree(target_path):
            """创建树节点.

                :param target_path: 指定目录
            """

            if not self._build_tree:
                return

            self._tree = Tree()
            parent_name = os.path.basename(target_path)
            self._tree.create_node(parent_name, parent_name)
Example #24
0
def collatz_tree(n):
    tree = Tree()
    tree.create_node("1", 1)
    step_list = [n]
    while (n != 1):
        n = collatz(n)
        step_list.insert(0, n)
    for i in range(1, len(step_list)):
        tree.create_node(str(step_list[i]),
                         step_list[i],
                         parent=step_list[i - 1])
    tree.show()
def parse_tree(fname):
    trees = read(fname)
    new_tr = Tree()
    new_tr.create_node("root", 1)
    size = 2 * len(trees[0].get_leaves()) + 1
    st_ids = []
    for i in range(2, size + 2):
        st_ids.append(i)

    parse_newick(trees[0].descendants, new_tr, 1, st_ids)

    return new_tr
Example #26
0
def build_parser_tree(org_str, parse_str):
    level_dict = defaultdict(list)
    punctuation = "',.!?[]()%#@&1234567890"
    parse_tree = Tree()
    parse_list = parse_str.replace("(", " ( ").replace(")", " ) ").strip().split()
    #print(parse_list)
    #print(org_str)
    org_str_list = nltk.word_tokenize(org_str)
    left_bracket_counter = 0
    right_bracket_counter = 0
    level = 0
    for index, item in enumerate(parse_list):
        if item == "(":
            left_bracket_counter = left_bracket_counter + 1
            continue
        if item == ")":
            right_bracket_counter = right_bracket_counter + 1
            continue
        level = left_bracket_counter - right_bracket_counter
        try:
            if (item in ConstituencyParse and item not in punctuation) \
                    or (item not in org_str and item.isupper()):
                # 创建非叶子节点,如:ROOT、S、VP、NP
                if item == "ROOT":
                    parse_tree.create_node(item, str(index))
                    level_dict[str(level)].append(str(index))
                else:
                    parse_tree.create_node(item, str(index), parent=level_dict[str(level-1)][-1])
                    level_dict[str(level)].append(str(index))
            if item in org_str_list and item not in punctuation:
                # 创建叶子节点,每一个叶子节点都是句子中的一个单词
                parse_tree.create_node(item, str(index), parent=level_dict[str(level)][-1])
                level_dict[str(level+1)].append(str(index))
            elif item in punctuation:
                # 创建标点符号的相关节点
                if index > 0 and parse_list[index-1] == "(":
                    parse_tree.create_node(item, str(index), parent=level_dict[str(level-1)][-1])
                    level_dict[str(level)].append(str(index))
                else:
                    parse_tree.create_node(item, str(index), parent=level_dict[str(level)][-1])
                    level_dict[str(level+1)].append(str(index))
        except Exception as e:
            #print(str(e))
            return None, parse_list
    #parse_tree.show()

    # 叶子节点中有词性标签,说明建树出错
    for node in parse_tree.leaves():
        if node.tag in ConstituencyParse:
            print("建立句法树出错!")
            return None, parse_list
    return parse_tree, parse_list
def createRootnode(Cmax, parent):
    maxsubpatterntreeinstance = Tree()
    # parameters nodename, nodetype, data contained in node nodeID = 0
    onset = None
    occurence = None
    timepointsperiodicities = None
    maxsubpatterntreeinstance.create_node('Cmax',
                                          'root',
                                          parent=parent,
                                          data=subpattern(
                                              Cmax, 1, onset, occurence,
                                              timepointsperiodicities))
    return maxsubpatterntreeinstance
Example #28
0
    def __init__(self, root_path):
        """
        :root_path: root path of login info
        """
        self._login_info_nodes_cache = {}
        self._login_info_nodes_id_cache = {}
        self._root_path = root_path
        self._login_info_root_node = LoginInfoNode(self._root_path)
        self._login_info_node_tree = Tree()
        self._walk_through()

        self._login_info_nodes_ids = sorted(
            self._login_info_nodes_id_cache.keys())
Example #29
0
def create_tree():
    # It builds the tree starting from the root node (n1) with the key 1
    tree = Tree()
    tree.create_node('root(n1)', 1, data=[keys['1']])  # root node
    for a in range(int(levels)):
        for n in range(pow(2, a + 1)):
            id = pow(2, a + 1) + n
            tree.create_node('n' + str(id),
                             id,
                             parent=int(id / 2),
                             data=[keys[str(id)]])
    tree.show()
    return tree
Example #30
0
def tree_from_token(root):
    def add_children(root, tree):
        for c in root.children:
            tree.create_node("{}:{}/{}".format(c.dep_, c, c.pos_),
                             hash(c),
                             parent=hash(root),
                             data=c)
            add_children(c, tree)

    tree = Tree()
    tree.create_node("{}/{}".format(root, root.pos_), hash(root), data=root)
    add_children(root, tree)
    return tree