def get(self,query): gexf = Gexf("Test","abc") graph=gexf.addGraph("directed","static","a test graph") gdb = GraphDatabase("http://localhost:7474/db/data/") srcs=[] i=0 l = (Q("name", contains=query)) for node in gdb.nodes.filter(l): src=graph.addNode(node.properties['name'],node.properties['name'],r='255',g='0',b='0') srcs.append(node.properties['name']) related_nodes = node.traverse() for related_node in related_nodes: dst=graph.addNode(related_node.properties['name'],related_node.properties['name'],r='0',g='255',b='255') graph.addEdge(i,related_node.properties['name'],node.properties['name'],weight=.01) i+=1 for item in [comb for comb in combinations(srcs, 2)]: graph.addEdge(i,item[1],item[0],weight=0.1) i+=1 out=etree.tostring(gexf.getXML(),pretty_print=True,encoding='utf-8') self.write(out)
def get_gexf(self): gexf = Gexf("Pallas", self.name) graph = gexf.addGraph("directed", "static", "Current site exploration") graph.addNode('start', 'start') for page in [page for page in self._pages if page != 'start']: graph.addNode(page, self._pages[page]._url) for id in [id for id in self._connections if self._connections[id]['from'] == "start"]: graph.addEdge(id, "start", self._connections[id]['to']) for page in self._pages: for id in [id for id in self._connections if self._connections[id]['from'] == page and self._connections[id]['explored']]: graph.addEdge(id, self._connections[id]['from'], self._connections[id]['to']) self._gexf_xml = gexf.getXML() return self._gexf_xml
def tree2graph(tree_nodes): dt, max_depth, max_width = buchheim.buchheim(tree_nodes[0]) gexf = Gexf('MOON_CLJ', 'simple') 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') add_node_and_edge(dt, graph, Count(), max_width=max_width) return etree.tostring(gexf.getXML(), pretty_print=False, encoding='utf-8', xml_declaration=True), max_depth, max_width
def tree2graph(tree_nodes): tree_xml = '' dt, max_depth, max_width = buchheim_weibospread.buchheim(tree_nodes[0]) gexf = Gexf('tree', 'simple') graph = gexf.addGraph('directed', 'static', 'weibo graph') graph.addNodeAttribute('photo_url', type='URI', force_id='photo_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='string', force_id='repost_num') graph.addNodeAttribute('weibo_url', type='URI', force_id='weibo_url') add_node_and_edge(dt, graph, Count(), max_width=max_width) return etree.tostring(gexf.getXML(), pretty_print=False, encoding='utf-8', xml_declaration=True), max_depth, max_width
def sigma_test_gexf(request): r = Raffle.objects.get(id='58') tl = Ticket.objects.filter(raffle=r.id).order_by('-is_root_ticket') gexf = Gexf('Sample','Testting Sigma JS') graph = gexf.addGraph('directed','static','a test graph') graph.addNodeAttribute('xcoord','0',"float") graph.addNodeAttribute('ycoord','0',"float") graph.addNodeAttribute('label','',"string") coords = r.graph() for i,t in enumerate(tl): n = graph.addNode(t.id,t.hash) n.addAttribute(0,str(coords[i][0])) n.addAttribute(1,str(coords[i][1])) n.addAttribute(2,t.hash) if t.parent_ticket_id>0: graph.addEdge(t.id,t.id,t.parent_ticket_id) response = etree.tostring(gexf.getXML(),pretty_print=True,encoding='utf-8',xml_declaration=True) print response return HttpResponse(response, content_type="application/json")
def forest_main(keyword,topic_id): first_start_ts, dataset, flag = load_data(keyword,topic_id) if flag == 0: return 0 height = 0 counter = 0 ts = [] colors = [] node_y_table = {} x_count = defaultdict(int) y_count = defaultdict(int) y_name = {} gexf = Gexf("Yang Han <*****@*****.**>", "Retweets evolution graph based on sail layout.") graph = gexf.addGraph("directed", "static", "retweets evolution graph") n = 0 for reposts in dataset: if not len(reposts): continue root, start_ts, end_ts, count, counter = build_tree(reposts, counter) _color = random_color(colors) build_graph(graph, root, start_ts, end_ts, node_y_table, x_count, y_count, y_name, x=ts2x(start_ts-first_start_ts), y=height, color=_color) height += 1 counter = build_x_chart(graph, x_count, counter, -SEG, first_start_ts) counter = build_y_chart(graph, y_count, y_name, counter, -SEG) n = n + 1 print n graph = etree.tostring(gexf.getXML(), pretty_print=False, encoding='utf-8', xml_declaration=True) save_weibo_tree(str(topic_id), graph) return 1
def sigma_gexf(request, ticket_hash): x = Ticket.objects.get(hash=ticket_hash) r = Raffle.objects.get(id=x.raffle.id) tl = Ticket.objects.filter(raffle=r.id).order_by('-is_root_ticket') gexf = Gexf('Tree For '+ticket_hash,'Sigma JS Graph') graph = gexf.addGraph('directed','static',ticket_hash) graph.addNodeAttribute('xcoord','0',"float") graph.addNodeAttribute('ycoord','0',"float") graph.addNodeAttribute('label','',"string") graph.addNodeAttribute('completion_size','1',"float") coords = r.graph() for i,t in enumerate(tl): n = graph.addNode(t.id,t.hash) n.addAttribute(0,str(coords[i][0])) n.addAttribute(1,str(coords[i][1]*4)) n.addAttribute(2,t.hash) cs = t.completion_count() n.addAttribute(3,str(cs)) for t in tl: if t.parent_ticket_id>0: graph.addEdge(t.id,t.id,t.parent_ticket_id) response = etree.tostring(gexf.getXML(),pretty_print=True,encoding='utf-8',xml_declaration=True) return HttpResponse(response, content_type="application/json")
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)
# gexf.writes(output_file) node_id_list = [] for i in school: node_id_list.append(str(i[0])) for j in xuke: node_id_list.append(j[0]) node_rgb_list = {} import random is_light = True for i in node_id_list: node_rgb_list.setdefault(i, []).append(f.rgb_color()) output_file = open('data.gexf', "wb") gexf_xml = gexf.getXML() for gexf_elem in gexf_xml: if gexf_elem.tag == 'graph': for gexf_nodes_links in gexf_elem: if gexf_nodes_links.tag == 'nodes': print("dealing with nodes viz") for node in gexf_nodes_links: tmp_id = node.get('id') # print(tmp_id) if tmp_id in school_id and int( tmp_id) in school_xuke.keys(): node_id = tmp_id node_rgb = node_rgb_list[node_id] # size_value = str(len(school_xuke[node_id])) print(str(len(school_xuke[int(node_id)]))) size_value = str(len(school_xuke[int(node_id)]))
def make_network_graph(current_date, topic_id, topic, window_size, key_user_labeled=True): date = current_date if key_user_labeled: key_users = read_key_users(current_date, window_size, topic, top_n=10) else: key_users = [] #topic = acquire_topic_name(topic_id) #if not topic: # return None G = make_network(topic, date, window_size) N = len(G.nodes()) if not N: return '' node_degree = nx.degree(G) G = cut_network(G, node_degree) gexf = Gexf("Yang Han", "Topic Network") node_id = {} graph = gexf.addGraph("directed", "static", "demp graph") graph.addNodeAttribute('name', type='string', force_id='name') graph.addNodeAttribute('location', type='string', force_id='location') graph.addNodeAttribute('timestamp', type='int', force_id='timestamp') pos = nx.spring_layout(G) node_counter = 0 edge_counter = 0 for node in G.nodes(): x, y = pos[node] degree = node_degree[node] if node not in node_id: node_id[node] = node_counter node_counter += 1 uid = node if uid in key_users: _node = graph.addNode(node_id[node], str(node), x=str(x), y=str(y), z='0', r='255', g='51', b='51', size=str(degree)) else: _node = graph.addNode(node_id[node], str(node), x=str(x), y=str(y), z='0', r='0', g='204', b='204', size=str(degree)) user_info = acquire_user_by_id(uid) if user_info: _node.addAttribute('name', user_info['name']) _node.addAttribute('location', user_info['location']) else: _node.addAttribute('name', 'Unknown') _node.addAttribute('location', 'Unknown') #_node.addAttribute('timestamp', str(uid_ts[uid])) for edge in G.edges(): start, end = edge start_id = node_id[start] end_id = node_id[end] graph.addEdge(str(edge_counter), str(start_id), str(end_id)) edge_counter += 1 return etree.tostring(gexf.getXML(), pretty_print=True, encoding='utf-8', xml_declaration=True)