Example #1
0
def get_pos(G):
    node = get_root(G)
    pos = {n: [None, None] for n in G.nodes}
    start = time.time()
    tree = buchheim(G, node)
    extract_pos(tree, pos)
    #normalize x,y coords to interval [0,1]
    x_list, y_list = list(zip(*list(pos.values())))
    max_x, max_y = max(x_list), max(y_list)
    min_x, min_y = min(x_list), min(y_list)
    for n in pos:
        x, y = pos[n]
        pos[n] = ((x - min_x) / (max_x - min_x), (y - min_y) / (max_y - min_y))
    print('Layout algorithm excecuted in:', time.time() - start, 's')
    return (pos)
Example #2
0
def status():
    user = login_user(session)
    if user is None:
        return ""

    client = Client(APP_KEY, APP_SECRET, CALLBACK_URL)
    client.set_token(user["access_token"])

    id = request.args.get('id', '')
    since_id = request.args.get('since_id', 0)

    reposts, source_weibo, since_id = load_reposts(app, weibo2db, r, client, id, since_id)
    if len(reposts) == 0:
        return ""

    #root
    tree_nodes = []
    node = source_weibo["user"]["name"]
    location = source_weibo["user"]["location"]
    datetime = source_weibo["created_at"]
    img_url = source_weibo["user"]["profile_image_url"]
    weibo_url = "http://weibo.com/" + \
        str(source_weibo["user"]["id"]) + \
        "/" + base62.mid_to_str(source_weibo["mid"])

    tree_nodes.append(Tree(node, location, datetime, int(id), img_url, weibo_url))

    for repost in reposts:
        try:
            node = repost["user"]["name"]
            wid = repost["id"]
            img_url = repost["user"]["profile_image_url"]
            location = repost["user"]["location"]
            datetime = repost['created_at']
            weibo_url = "http://weibo.com/" + \
                str(repost["user"]["id"]) + \
                "/" + base62.mid_to_str(repost["mid"])
            tree_nodes.append(Tree(node, location, datetime, wid, img_url, weibo_url))
        except:
            app.logger.error(repost)
            continue

        repost_users = re.findall(r'//@(\S+?):', repost["text"])
        if len(repost_users):
            flag = True
            for node in tree_nodes[::-1]:
                if node.node == repost_users[0]:
                    node.append_child(tree_nodes[-1])
                    flag = False
                    break

            if flag:
                tree_nodes[0].append_child(tree_nodes[-1])
        else:
            tree_nodes[0].append_child(tree_nodes[-1])

    dt, max_width = buchheim.buchheim(tree_nodes[0])

    gexf = Gexf("MOON_CLJ", "haha")
    graph = gexf.addGraph("directed", "static", "weibo graph")
    graph.addNodeAttribute("img_url", type="URI", force_id="img_url")
    graph.addNodeAttribute("name", type="string", force_id="name")
    graph.addNodeAttribute("location", type="string", force_id="location")
    graph.addNodeAttribute("datetime", type="string", force_id="datetime")
    graph.addNodeAttribute("repost_num", type="integer", force_id="repost_num")
    graph.addNodeAttribute("weibo_url", type="URI", force_id="weibo_url")

    rank = node_rank(tree_nodes[0])
    add_node_edge(dt, graph, rank, Count(), max_width=max_width)

    return etree.tostring(gexf.getXML(), pretty_print=True, encoding='utf-8', xml_declaration=True)
Example #3
0
from gen import Tree
from demo_trees import trees
import reingold_thread; reload(reingold_thread)
from reingold_thread import reingold_tilford as rt
#from reingold_naive import reingold_tilford as rt
from buchheim import buchheim

#print trees[5][0]
t = buchheim(trees[5])
#t = rt(trees[5])

r = 30
rh = r*1.5
rw = r*1.5
stroke(0)

def drawt(root, depth):
    global r
    oval(root.x * rw, depth * rh, r, r)
    for child in root.children:
        drawt(child, depth+1)

def drawconn(root, depth):
    for child in root.children:
        line(root.x * rw + (r/2), depth * rh + (r/2),
             child.x * rw + (r/2), (depth+1) * rh + (r/2))
        drawconn(child, depth+1)

def sign(x):
    if x == 0: return 0
    if x > 0:  return 1
Example #4
0
from tree import Tree
from demo_trees import trees
import reingold_thread
reload(reingold_thread)
from reingold_thread import reingold_tilford as rt
from buchheim import buchheim

#print trees[5][0]
t = buchheim(trees[5])
#t = rt(trees[5])

r = 30
rh = r * 1.5
rw = r * 1.5
stroke(0)


def drawt(root, depth):
    global r
    oval(root.x * rw, depth * rh, r, r)
    for child in root.children:
        drawt(child, depth + 1)


def drawconn(root, depth):
    for child in root.children:
        line(root.x * rw + (r / 2), depth * rh + (r / 2),
             child.x * rw + (r / 2), (depth + 1) * rh + (r / 2))
        drawconn(child, depth + 1)

Example #5
0
def query():
    query = request.form['query']
    parser = Parser()
    tree = parser.execute(query)
    drawTree = buchheim(tree)
    return jsonify(drawTree.serialize())
Example #6
0
# print('heloooooo')
childList = []
for k in range (len(g1.tree_level_dictionary[y])-1):

    child=g1.tree_level_dictionary[y][k+1]
    childList.append(treeDict[child][0])
    # print(treeDict[child][0])

# print(node.get_label(), childList)
theNewGeneratedTree=Tree(node.get_label(), *childList)
# bishoyToBucDict[node]=theNewGeneratedTree
treeDict[node]=[]
treeDict[node].append(theNewGeneratedTree)
# print('---------------------------------')

bucTree= buchheim(theNewGeneratedTree)
# print(bucTree.children[0].parent," hwa da")
# print(bucTree)
# print(bucTree.children)
# print(bucTree.children[0].children)
# print(bucTree.children[1].children)
# print(bucTree.children[1].children[1].children)
# print(bucTree.children[1].children[1].children[0].children)
# print(bucTree.children.x)
# p=bucTree


nodeToXcoordDict=dict()
levelToBucObjectDict=dict()
treeLevelNodeToBucNodeDict=dict()
Example #7
0
from gen import Tree
from demo_trees import trees
from buchheim import buchheim

t = buchheim(trees[6])

r = 30
rh = r * 1.5
rw = r * 1.5
stroke(0)


def drawt(root, depth):
    global r
    oval(root.x * rw, depth * rh, r, r)
    for child in root.children:
        drawt(child, depth + 1)


def drawconn(root, depth):
    for child in root.children:
        line(root.x * rw + (r / 2), depth * rh + (r / 2),
             child.x * rw + (r / 2), (depth + 1) * rh + (r / 2))
        drawconn(child, depth + 1)


size(1000, 500)
translate(2, 2)
stroke(0)
drawconn(t, 0)
fill(1, 1, 1)
Example #8
0
    def createTree(g1):

        # if (len(g1.tree_level_dictionary))
        x = len(g1.tree_level_dictionary) - 1
        # parentLevelDict = dict()

        # y = len(g1.tree_level_dictionary)

        # while y >= 1:
        #    print("y= ", y)
        #    print(len(g1.tree_level_dictionary[y]))
        #    for i in range(len(g1.tree_level_dictionary[y])):
        #        # p = Tree(g1.tree_level_dictionary[y][i].get_label())
        #        node = g1.tree_level_dictionary[y][i]
        #        key = g1.tree_level_dictionary[y][i].get_parent()
        #        if parentLevelDict.get(key) == None:
        #            parentLevelDict[key] = []
        #            parentLevelDict[key].append(node)
        #        else:
        #            parentLevelDict[key].append(node)

        #    y = y - 1

        treeDict = dict()

        y = len(g1.tree_level_dictionary)
        while y > 1:

            for i in range(len(g1.tree_level_dictionary[y])):

                node = g1.tree_level_dictionary[y][i]
                key = node.get_parent()
                if node not in g1.parent_dictionary.keys():
                    print("Y LEVELLLLLL=", y)
                    p = Tree(node.get_label())
                    # bishoyToBucDict[node]=p
                    treeDict[node] = []
                    treeDict[node].append(p)
                else:
                    childList = []
                    for k in g1.get_children_nodes(node):
                        child = k
                        childList.append(treeDict[child][0])
                        # print(treeDict[child][0])

                    theNewGeneratedTree = Tree(node.get_label(), *childList)
                    treeDict[node] = []
                    treeDict[node].append(theNewGeneratedTree)

            y = y - 1

        y = len(g1.tree_level_dictionary[1])
        # print('----------------------------------------------------------')
        # while y > 0:

        for i in range(1, len(g1.tree_level_dictionary[1])):
            print("index in level 1:", i)
            node = g1.tree_level_dictionary[1][i]
            key = node.get_parent()
            if node not in g1.parent_dictionary.keys():

                p = Tree(node.get_label())
                treeDict[node] = []
                treeDict[node].append(p)
            else:
                childList = []
                for k in g1.get_children_nodes(node):
                    child = k
                    childList.append(treeDict[child][0])
                    # print(treeDict[child][0])

                # print(node.get_label(), childList)
                print('--------------------')
                print(childList)
                print('--------------------')
                # childList.reverse()
                # print(revList,'rev list')
                theNewGeneratedTree = Tree(node.get_label(), *childList)

                # print(node.get_label(),'  : ',theNewGeneratedTree,'---------------09 ')
                treeDict[node] = []
                treeDict[node].append(theNewGeneratedTree)

            # y = y - 1

        y = 1

        node = g1.tree_level_dictionary[y][0]
        key = node.get_parent()

        childList = []
        for k in range(len(g1.tree_level_dictionary[y]) - 1):
            child = g1.tree_level_dictionary[y][k + 1]
            childList.append(treeDict[child][0])
            # print(treeDict[child][0])

        # print(node.get_label(), childList)
        theNewGeneratedTree = Tree(node.get_label(), *childList)
        # bishoyToBucDict[node]=theNewGeneratedTree
        treeDict[node] = []
        treeDict[node].append(theNewGeneratedTree)
        # print('---------------------------------')

        bucTree = buchheim(theNewGeneratedTree)

        nodeToXcoordDict = dict()
        levelToBucObjectDict = dict()
        treeLevelNodeToBucNodeDict = dict()

        levelToBucObjectDict[0] = []
        for key in g1.tree_level_dictionary:
            levelToBucObjectDict[key] = []

        def generate_BucLevel_BucRow_Dict(bucNode):
            # if (len(bucNode.children) == 0):
            if not (isinstance(bucNode.children, list)):
                nodeToXcoordDict[bucNode] = bucNode.x
                levelToBucObjectDict[bucNode.y].append(bucNode)
                return
            else:
                for children in bucNode.children:
                    generate_BucLevel_BucRow_Dict(children)
                nodeToXcoordDict[bucNode] = bucNode.x
                levelToBucObjectDict[bucNode.y].append(bucNode)

        generate_BucLevel_BucRow_Dict(bucTree)

        treeLevelNodeToBucNodeDict[g1.tree_level_dictionary[1]
                                   [0]] = levelToBucObjectDict[0][0]

        for i in range(len(g1.tree_level_dictionary[1]) - 1):
            treeLevelNodeToBucNodeDict[g1.tree_level_dictionary[1][
                i + 1]] = levelToBucObjectDict[1][i]

        y = len(g1.tree_level_dictionary)
        while y > 1:
            for i in range(len(g1.tree_level_dictionary[y])):
                print("level:", y)
                print("index:", i)
                print(g1.tree_level_dictionary)
                print(levelToBucObjectDict)
                print(len(levelToBucObjectDict[y]))
                treeLevelNodeToBucNodeDict[g1.tree_level_dictionary[y]
                                           [i]] = levelToBucObjectDict[y][i]

            y = y - 1

        treeSequence = g1.tree_draw_sequence

        treeVisual.treeVisualCall(treeLevelNodeToBucNodeDict, treeSequence)