def graph(): fh = open ('data/jamia/data_out2.json','r') data = json.load(fh) nodelist = set() for d in data: nodelist.add(d['source']) nodelist.add(d['target']) gexf = Gexf("Yiye Zhang","Test graph") graph=gexf.addGraph("directed", "static","testing graph") for i in nodelist: graph.addNode(i,i) k=0 for d in data: graph.addEdge(k,d['source'],d['target'], weight=d['weight']) k=k+1 output_file=open("app/static/g.gexf","w") gexf.write(output_file) return render_template('graph.html')
def write_gexf(G, fpath): """Convert an igraph graph to a gexf file.""" gexf = Gexf("QNATool", config.project_name) gexf_graph = gexf.addGraph( 'directed', 'dynamic', config.project_name, timeformat='date') # add custom attributes to edges and nodes gexf_graph.addEdgeAttribute('article_path', 0, type='string', force_id='article_path') gexf_graph.addEdgeAttribute('sentiment', 0, force_id='sentiment') gexf_graph.addEdgeAttribute('sentences', 0, type='string', force_id='sentences') gexf_graph.addNodeAttribute('community', 0, force_id='community') gexf_graph.addNodeAttribute('kind', 0, type='string', force_id='kind') gexf_graph.addNodeAttribute('frequency', 0, force_id='frequency') for vertex in G.vs: add_gexf_node(vertex.index, G, gexf_graph) for source_id, target_id in G.get_edgelist(): add_gexf_edge(source_id, target_id, G, gexf_graph) f = open(fpath, 'w') gexf.write(f)
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 build_gexf(edges, out_name, p_sample = 1): if not Gexf_loaded: print 'Could not load Gexf from module gexf.' return gexf = Gexf("snikolov", out_name) graph = gexf.addGraph('directed', 'dynamic', out_name) end = str(max([edge[2] for edge in edges])) for (src, dst, time) in edges: if np.random.rand() > p_sample: continue # Assumes time is in epoch seconds #d = datetime.datetime.fromtimestamp(int(time)) #date = d.isoformat() start = str(time) if src != -1: if not graph.nodeExists(src) or start < graph._nodes[src].start: graph.addNode(id = src, label = '', start = start, end = end) if not graph.nodeExists(dst) or start < graph._nodes[dst].start: graph.addNode(id = dst, label = '', start = start, end = end) graph.addEdge(id = str(src) + ',' + str(dst), source = src, target = dst, start = start, end = end) else: if not graph.nodeExists(dst): graph.addNode(id = dst, label = '', start = start, end = end) out = open('/Users/snikolov/projects/twesearch/data/graphs/' + out_name + '.gexf', 'w') gexf.write(out) out.close()
def main(): gexf = Gexf("Politikon","Autoreferences graph") graph=gexf.addGraph("directed","static","a hello world graph") tree = ET.parse(INPUT_WP_XML) root = tree.getroot() orphan = [] for item in root[0].findall('item'): if re.match("http://politikon.es/\d\d\d\d/\d\d/\d\d/([\w*-]*)/", item.find('link').text): post_id = re.match("http://politikon.es/\d\d\d\d/\d\d/\d\d/([\w*-]*)/", item.find('link').text).group(1) post_link = item.find('link').text r,g,b = get_color(item.find('{http://purl.org/dc/elements/1.1/}creator').text) graph.addNode(id=str(post_id),label=str(post_link), r=str(r), g=str(g), b=str(b)) links = get_post_links(item.find('{http://purl.org/rss/1.0/modules/content/}encoded').text) if links: for link in links: link_id = re.match("http://politikon.es/\d\d\d\d/\d\d/[\d\d/]*([\w*-]*)/", link).group(1) if link_id in orphan: orphan.remove(link_id) if graph.nodeExists(link_id): graph.addEdge(post_id+"->"+link_id, post_id, link_id) else: orphan.append(post_id) for o in orphan: del graph._nodes[o] output_file=open(GEXF_OUTPUT_FILE,"w") gexf.write(output_file)
def main(): if len(sys.argv) != 4: sys.exit("Format: parse_output.py infopath-output.txt timesteps.txt timestamp") infopath_output_file = sys.argv[1] timesteps_file = sys.argv[2] ts_to_gen = int(sys.argv[3]) out_file = "%s-at-%d.gexf" % (infopath_output_file.split('.')[0], ts_to_gen) fin = open(infopath_output_file, 'r') fts = open(timesteps_file, 'r') fout = open(out_file, 'w') gexf = Gexf('tribhu_infopath','31-03-2014') graph = gexf.addGraph('directed','dynamic','31-03-2014') # Add all users as nodes for line in fin: if ',' not in line: break uid, uname = line.split(',') uid = int(uid) # graph.addNode(str(uid), uname[-1]) timesteps = map(lambda x: float(x.strip()), fts.readlines()) first_ts = timesteps[0] last_ts = timesteps[-1] # # Create a dict: timstamp -> [ (source, target, weight), ()...] # ts_to_edges_dct = defaultdict(list) for line in fin: vals = map(float, line.split(',')) source, target = map(int, vals[0:2]) # Obtain a list of pairs <timestamp, weight> edge_weights = [(int(vals[i]), float(vals[i+1])) for i in range(2, len(vals[2:])-1, 2)] for ts, weight in edge_weights: ts_to_edges_dct[ts] += [(source, target, weight), ] # Dump the required timestamps into an output file added_nodes = set() for source, target, weight in ts_to_edges_dct[ts_to_gen]: if source not in added_nodes: added_nodes.add(source) graph.addNode(str(source), str(source)) if target not in added_nodes: added_nodes.add(target) graph.addNode(str(target), str(target)) edge_id = "%d_to_%d_at_%f" % (source, target, ts) graph.addEdge(edge_id, str(source), str(target), weight=weight, label=edge_id) gexf.write(fout)
def generate_graph(): gexf = Gexf("sna - meetup.com", "A meetup.com social network analysis") graph = gexf.addGraph("undirected", "static", "Meetup groups graph") # attributes organizer_attr = graph.addNodeAttribute('organizer', 'None', 'string') type_attr = graph.addNodeAttribute('type', 'None', 'string') category_attr = graph.addNodeAttribute('category', 'None', 'integer') groups = get_data('data/groups.json') members = get_data('data/members.json') # print groups[0] # print members[0] groups = list(filter(lambda x: int(x['members']) <= 200, groups)) print "Total number of groups: %s" % len(groups) for index, group in enumerate(groups): node = graph.addNode( "G-%s" % group.get('id'), group.get('urlname') ) if group.get('organizer'): node.addAttribute(organizer_attr, str(group.get('organizer').get('member_id'))) if group.get('category'): node.addAttribute(category_attr, str(group.get('category').get('id'))) node.addAttribute(type_attr, 'Group') members = list(filter(lambda x: (x['group_id']) in list(map(lambda x: x['id'] ,groups)),members)) print "Total number of members: %s" % len(members) for index, member in enumerate(members): # print index + 1 node = graph.addNode( "M-%s" % member.get('member_id'), get_member_name(member.get('name')) ) node.addAttribute(type_attr, 'Member') graph.addEdge( index, "G-%s" % member.get('group_id'), "M-%s" % member.get('member_id'), ) return gexf
def perm_to_app_graph_all(apps, p_maps, categories): print 'perm_to_app_graph start ', 'all' gexf_a = Gexf('Jianhua Shao', 'all') graph_a = gexf_a.addGraph('directed', 'static', 'all') attr_node_a = graph_a.addNodeAttribute('node_type', 'app', 'string') attr_edge_a = graph_a.addEdgeAttribute('edge_type', 'no', 'string') for p_id in p_maps: p_label = p_maps[p_id] n_a = graph_a.addNode(p_id, '%s_%s'%(str(p_id), p_label)) n_a.addAttribute(attr_node_a, 'p') app_ids_a = {} i_t = len(apps) i_i = 0 p = 0 for app_id in apps: p, i_i = p_percent(p, i_i, i_t, 10) app = apps[app_id] if not app.has_key('developer'): continue developer_href = app['developer'] if developer_href == '' or developer_href == None: continue if not app.has_key('perms'): continue installs = app['installs'] installs, install_min, install_max, install_average = installs_c(installs) if int(install_max) <= 5: continue rating_average = app['rating_average'] if float(rating_average) == 0: continue category = app['category'].lower().strip() category_id = categories[category] award_editor = 'no' award_developer = 'no' if app.has_key('awards'): for award in app['awards']: award = award.strip().lower() if award == 'top developer': award_developer = 'yes' if award == "editors' choice": award_editor = 'yes' if not app_ids_a.has_key(app_id): app_ids_a[app_id] = 1 n_a = graph_a.addNode(app_id, app_id) n_a.addAttribute(attr_node_a, 'a') ps = app['perms'] for p_id in ps: e_a = graph_a.addEdge('%s_%s'%(str(p_id), app_id), p_id, app_id) e_a.addAttribute(attr_edge_a, award_editor) output_file_a = open('./txt/graph/all.gexf', 'w') gexf_a.write(output_file_a) print 'perm_to_app_graph end all '
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 generateXml(G, layers): gexf = Gexf("lzu.edu", "network based on IPv6") graph = gexf.addGraph("undirected", "static", "network based on IPv6") # atr1 = graph.addNodeAttribute('ip address',type='string',defaultValue='true') atr1 = graph.addNodeAttribute(force_id='modularity_class', title='Modularity Class', type='integer') f = faker.Faker(locale='zh-CN') print('ipv6:{}'.format(f.ipv6())) nodes = list(G.nodes) edges = list(G.edges) print('nodes:{}'.format(nodes)) print('edges:{}'.format(edges)) activate_nodes = [] for i in range(len(layers)): for j in range(len(layers[i])): activate_nodes.append(layers[i][j]) for i in range(len(nodes)): tmp = graph.addNode(nodes[i], f.ipv6()) attribute_flag = 0 for j in range(len(layers)): if nodes[i] in layers[j]: attribute_flag = j + 1 break tmp.addAttribute(atr1, str(attribute_flag)) for i in range(len(edges)): graph.addEdge(str(i), str(edges[i][0]), str(edges[i][1])) # tmp = graph.addNode("0","Gephi") # tmp.addAttribute(atr1,"http://gephi.org") # tmp = graph.addNode("1","Webatlas") # tmp.addAttribute(atr1,"http://webatlas.fr") # tmp = graph.addNode("2","RTGI") # tmp.addAttribute(atr1,"http://rtgi.fr") # tmp = graph.addNode("3","BarabasiLab") # tmp.addAttribute(atr1,"http://barabasilab.com") # graph.addEdge("0","0","1",weight='1') # graph.addEdge("1","0","2",weight='1') # graph.addEdge("2","1","0",weight='1') # graph.addEdge("3","2","1",weight='1') # graph.addEdge("4","0","3",weight='1') xml_file = os.getcwd( ) + os.sep + 'app' + os.sep + 'static' + os.sep + 'data' + os.sep + 'xml' + os.sep + 'data.xml' output_file = open(xml_file, "wb") gexf.write(output_file)
def generate_graph(): gexf = Gexf("sna - meetup.com", "A meetup.com social network analysis") graph = gexf.addGraph("undirected", "static", "Meetup groups graph") # attributes organizer_attr = graph.addNodeAttribute('organizer', 'None', 'string') type_attr = graph.addNodeAttribute('type', 'None', 'string') category_attr = graph.addNodeAttribute('category', 'None', 'integer') groups = get_data('data/groups.json') members = get_data('data/members.json') # print groups[0] # print members[0] groups = list(filter(lambda x: int(x['members']) <= 200, groups)) print "Total number of groups: %s" % len(groups) for index, group in enumerate(groups): node = graph.addNode("G-%s" % group.get('id'), group.get('urlname')) if group.get('organizer'): node.addAttribute(organizer_attr, str(group.get('organizer').get('member_id'))) if group.get('category'): node.addAttribute(category_attr, str(group.get('category').get('id'))) node.addAttribute(type_attr, 'Group') members = list( filter( lambda x: (x['group_id']) in list(map(lambda x: x['id'], groups)), members)) print "Total number of members: %s" % len(members) for index, member in enumerate(members): # print index + 1 node = graph.addNode("M-%s" % member.get('member_id'), get_member_name(member.get('name'))) node.addAttribute(type_attr, 'Member') graph.addEdge( index, "G-%s" % member.get('group_id'), "M-%s" % member.get('member_id'), ) return gexf
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 getgexf(g): gexf = Gexf('hxq', 'test graph') graph = gexf.addGraph('undirected graph') node_counter = 0 for node in g.nodes(): graph.addNode(node_counter, node) node_couter += 1 edge_counter = 0 for edge in g.edges(): start, end = edge graph.addEdge(edge_counter, start, end) output_file = open('graph.gexf', 'w') gexf.write(output_file)
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 convert(self, nodes, ties): gexf = Gexf(config.CREATOR, config.DESCRIPTION) graph = gexf.addGraph(config.DEFAULTEDGETYPE, config.MODE, config.LABEL) # Percorre todos os nós for node in nodes.keys(): for item in nodes[node]: graph.addNode(nodes[node][item]["id"], item) for i in xrange(len(ties)): graph.addEdge(i, ties[i][0], ties[i][1]) output = open(config.OUTPUT_FILE, "w") gexf.write(output) output.close()
def convert(self,nodes,ties): gexf = Gexf(config.CREATOR, config.DESCRIPTION) graph = gexf.addGraph(config.DEFAULTEDGETYPE, config.MODE, config.LABEL) # Percorre todos os nós for node in nodes.keys(): for item in nodes[node]: graph.addNode(nodes[node][item]['id'], item) for i in xrange(len(ties)): graph.addEdge(i, ties[i][0], ties[i][1]) output = open(config.OUTPUT_FILE,'w') gexf.write(output) output.close()
def derived_x_to_y(): txt = open("./dot/derived_x_to_y.gv", "w") txt.write("//%s\n" % (str(datetime.now()))) txt.write("digraph graphname {\n") gexf = Gexf("Jianhua Shao", "Thingiver derivation mapping") graph = gexf.addGraph("directed", "static", "derivation_x_y") # attr_node = graph.addNodeAttribute('url', '', 'string') attr_edge = graph.addEdgeAttribute("c_days", "", "string") # sql = sql_derived_x_to_y sql = sql_derived_x_to_y_created_time param = () c = conn.cursor() c.execute(sql, param) for r in c.fetchall(): print r x = r[1] y = r[2] x_ctime = r[3] y_ctime = r[4] x = x.strip("\n\t/thing:") # x = int(x) y = y.strip("\n\t/thing:") # y = int(y) # print type(x), type(y) # print x, y x_ctime = datetime.strptime(x_ctime, "%b %d, %Y") y_ctime = datetime.strptime(y_ctime, "%b %d, %Y") duration = (y_ctime - x_ctime).days # print duration dot_line = "\t{%s} -> {%s} [label=%s];\n" % (x, y, str(duration)) print dot_line txt.write(dot_line) n_x = graph.addNode(str(x), str(x)) n_y = graph.addNode(str(y), str(y)) # n_x.addAttribute(attr_node, 'string') # n_y.addAttribute(attr_node, 'string') e = graph.addEdge("%s_%s" % (str(x), str(y)), x, y) e.addAttribute(attr_edge, str(duration)) # print e c.close() txt.write("}") txt.close() gexf_file = open("./dot/derived_x_to_y.gexf", "w") gexf.write(gexf_file) print "finish"
def derived_x_to_y(): txt = open('./dot/derived_x_to_y.gv', 'w') txt.write('//%s\n' % (str(datetime.now()))) txt.write('digraph graphname {\n') gexf = Gexf('Jianhua Shao', 'Thingiver derivation mapping') graph = gexf.addGraph('directed', 'static', 'derivation_x_y') #attr_node = graph.addNodeAttribute('url', '', 'string') attr_edge = graph.addEdgeAttribute('c_days', '', 'string') #sql = sql_derived_x_to_y sql = sql_derived_x_to_y_created_time param = () c = conn.cursor() c.execute(sql, param) for r in c.fetchall(): print r x = r[1] y = r[2] x_ctime = r[3] y_ctime = r[4] x = x.strip('\n\t/thing:') #x = int(x) y = y.strip('\n\t/thing:') #y = int(y) #print type(x), type(y) #print x, y x_ctime = datetime.strptime(x_ctime, '%b %d, %Y') y_ctime = datetime.strptime(y_ctime, '%b %d, %Y') duration = (y_ctime - x_ctime).days #print duration dot_line = '\t{%s} -> {%s} [label=%s];\n' % (x, y, str(duration)) print dot_line txt.write(dot_line) n_x = graph.addNode(str(x), str(x)) n_y = graph.addNode(str(y), str(y)) #n_x.addAttribute(attr_node, 'string') #n_y.addAttribute(attr_node, 'string') e = graph.addEdge('%s_%s' % (str(x), str(y)), x, y) e.addAttribute(attr_edge, str(duration)) #print e c.close() txt.write('}') txt.close() gexf_file = open('./dot/derived_x_to_y.gexf', 'w') gexf.write(gexf_file) print "finish"
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 developer_to_api_o(): global conn gexf = Gexf("Jianhua Shao", "programaleweb developer to api") graph = gexf.addGraph("directed", "static", "ecosystem") attr_node = graph.addNodeAttribute("n_type", "mashup", "string") gexf_node_developer( developer_to_api_distinct_developer, (), graph, attr_node) gexf_node_api( developer_to_api_distinct_api, (), graph, attr_node) gexf_edge_developer_to_api( developer_to_api, (), graph) output_file = open(gexf_path+"pgw_developer_to_api.gexf", 'w') gexf.write(output_file)
def gen_gexf(ofile='./../gexf/test2.gexf'): import sys, pprint from gexf import Gexf # test helloworld.gexf gexf = Gexf("Gephi.org", "A Web network") graph = gexf.addGraph("directed", "static", "A Web network") atr2 = graph.addNodeAttribute('modularity_class', 'Modularity Class', 'integer') tmp = graph.addNode("0", "A") tmp.addAttribute(atr2, "0") tmp = graph.addNode("1", "B") tmp.addAttribute(atr2, "0") graph.addEdge("0", "0", "1", weight='1') output_file = open(ofile, "w") gexf.write(output_file)
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 perm_to_app_graph_each(apps_t, p_maps, categories): for category in apps_t: print 'perm_to_app_graph start ', category category_id = categories[category] apps = apps_t[category] gexf = Gexf('Jianhua Shao', category) graph = gexf.addGraph('directed', 'static', category) attr_node = graph.addNodeAttribute('node_type', 'app', 'string') attr_edge = graph.addEdgeAttribute('edge_type', 'no', 'string') #attr_node_app_award = graph.addNodeAttribute('app_award', 'no', 'string') for p_id in p_maps: p_label = p_maps[p_id] n = graph.addNode(p_id, '%s_%s'%(str(p_id), p_label)) n.addAttribute(attr_node, 'p') app_ids = {} for app_id in apps: app = apps[app_id] award_editor = 'no' award_developer = 'no' if app.has_key('awards'): for award in app['awards']: award = award.strip().lower() if award == 'top developer': award_developer = 'yes' if award == "editors' choice": award_editor = 'yes' if not app_ids.has_key(app_id): app_ids[app_id] = 1 n = graph.addNode(app_id, app_id) n.addAttribute(attr_node, 'a') ps = app['perms'] for p_id in ps: e = graph.addEdge('%s_%s'%(str(p_id), app_id), p_id, app_id) e.addAttribute(attr_edge, award_editor) output_file = open('./txt/graph/%s_%s.gexf'%(str(category_id), category), 'w') gexf.write(output_file) print 'perm_to_app_graph end ', category
import sqlite3 as lite from gexf import Gexf ## connect database directory = "..." db_path = directory + "duang.db" con = lite.connect(db_path) gexf = Gexf("Duang", "A GEXF file about Tweets") graph = gexf.addGraph(type="directed", mode="dynamic", label="Tweets Graph") # , timeformat="dateTime") ## read database with con: curs = con.cursor() ## read table nodes curs.execute( "SELECT n.user_id, n.user_label, n.created_at FROM nodes AS n INNER JOIN links as l ON l.source = n.user_id OR l.target = n.user_id" ) nodes = curs.fetchall() nodes = set(nodes) ## read table links curs.execute("SELECT * FROM links") links = curs.fetchall() ## create gexf if nodes is not None: ## write nodes into one file of gexf format for node in nodes: graph.addNode(id=str(node[0].encode("utf-8")), label=str(node[1].encode("utf-8")), start=str(node[2])) if links is not None: ## write links into the file
<html> <head> <meta charset="utf-8" /> <link rel="stylesheet" href="http://tools.christopherkullenberg.se/style.css"> <title>Results</title> </head> <body> <p>Processing...</p> """ + message) thefile = open("upload/" + fn, 'r', encoding="utf-8") tsv = csv.reader(thefile, delimiter='\t') next(tsv) #remove headers gexf = Gexf("Citation Network", "File:" + fn + ".") graph = gexf.addGraph("directed", "static", "Web of Science Citation network") citationnetworkdict = {} for t in tsv: AU = t[1] CR = t[29] citationnetworkdict.update({AU: CR.split('; ')}) numberofedges = 0 for key, value in citationnetworkdict.items(): graph.addNode(key, key) for v in value: graph.addNode(v, v) #print(str(numberofedges) + "***" + key + "***" + v)
from gexf import Gexf try: gexf = Gexf ("Jianhua Shao", "hello world description") graph = gexf.addGraph ("directed", "static", "hello world hints") attribute_node = graph.addNodeAttribute("name", "default_value", "type like string") attribute_edge = graph.addEdgeAttribute("name", "default_value", "type like boolean") n = graph.addNode("0", "node_name") n1=graph.addNode("1", "node_name1") n1=graph.addNode("2", "node_name2") n1=graph.addNode("3", "node_name3") #n.addAttribute(attribute_node, "mashup") e = graph.addEdge("name", "0", "1") e.addAttribute(attribute_edge, "true") e1 = graph.addEdge("name", "0", "2") e2 = graph.addEdge("name", "1", "2") e3 = graph.addEdge("name", "2", "3") output_file = open("D://programableweb.gexf", "w") gexf.write(output_file) #outputfile.close() except: print "error" pass """import networkx as nx import enchant import matplotlib.pyplot as plt g=nx.Graph() g.add_node(1) g.add_node(2) g.add_edge(1,2) #nx.draw(g)
try: conn = MySQLdb.connect (host = DB_HOST, user = DB_USER, passwd = DB_PSWD, db = DB_NAME, use_unicode=True) except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) sys.exit (1) cursor = conn.cursor () if __name__ == '__main__': gexf = Gexf("PIC BROTHER","Le graph du pic !") graph = gexf.addGraph("undirected","static","Picbrother graph") cursor.execute ("SELECT id, fb_id, first_name, last_name FROM `T_USER` ") rows = cursor.fetchall () for row in rows: print "%s, %s, %s, %s" % (row[0], row[1], row[2], row[3]) #graph.addNode(row[0],u"#%s => %s %s" % (row[1],row[2],row[3])) # ==> Probléme d'accent... Comment faire ? graph.addNode(str(row[0]),unac.unac_string("%s %s" % (row[2],row[3]), "utf-8")) print "Nombre de user ajoute : %d" % cursor.rowcount cursor.execute ("SELECT U1.user_id, U2.user_id, COUNT( * ) weight FROM `J_PHOTO_USER` U1, `J_PHOTO_USER` U2 WHERE U1.photo_id = U2.photo_id AND U1.user_id != U2.user_id GROUP BY U1.user_id, U2.user_id") rows = cursor.fetchall () edge_id = 0 for row in rows: print "%s, %s, %s, %s" % (edge_id,row[0], row[1], row[2]) graph.addEdge(str(edge_id),str(row[0]),str(row[1]),str(row[2])) edge_id+=1 print "Nombre de liens ajoute : %d" % cursor.rowcount
import sys, pprint from gexf import Gexf # test helloworld.gexf gexf = Gexf("course system", "neo4j graph") graph = gexf.addGraph("directed", "static", "A Web network") atr1 = graph.addNodeAttribute('url', type='string', defaultValue='123', force_id='testid') atr2 = graph.addNodeAttribute('indegree', type='float', defaultValue='true', force_id='modularity_class') atr3 = graph.addNodeAttribute('frog', type='boolean', defaultValue='true') tmp = graph.addNode("0", "Gephi") tmp.addAttribute(atr1, "http://gephi.org") tmp.addAttribute(atr2, '1') tmp = graph.addNode("1", "Webatlas") tmp.addAttribute(atr1, "http://webatlas.fr") tmp.addAttribute(atr2, '2') tmp = graph.addNode("2", "RTGI") tmp.addAttribute(atr1, "http://rtgi.fr") tmp.addAttribute(atr2, '3') tmp = graph.addNode("3", "BarabasiLab") tmp.addAttribute(atr1, "http://barabasilab.com")
try: mongo = MongoClient(host) except: print "Error starting MongoDB" raise db = mongo[database] collection = db[term] # db.drop_collection(mapcollection) if mapcollection not in db.collection_names(): mapF = Code(open('../mapReduce/mapGraph.js', 'r').read()) reduceF = Code(open('../mapReduce/reduceGraph.js', 'r').read()) collection.map_reduce(mapF, reduceF, query=get_date_query(start, end), out=mapcollection) gexf = Gexf(creator="lomo", description="Relations") graph = gexf.addGraph(type="directed", mode="static", label="relations") giid = graph.addNodeAttribute("Global Indegree", "0", type="float") goid = graph.addNodeAttribute("Global Outdegree", "0", type="float") gort = graph.addNodeAttribute("Retweets", "0", type="float") gomt = graph.addNodeAttribute("Mentions", "0", type="float") userMap = {} userNodeMap = {} for user in db[mapcollection].find().sort([('value.indegree', -1)]): userMap[user['_id']] = user['value'] node = graph.addNode(user['_id'], user['_id']) node.addAttribute(giid, str(user['value']['indegree'])) node.addAttribute(goid, str(user['value']['outdegree'])) node.addAttribute(gort, str(user['value']['rts'])) node.addAttribute(gomt, str(user['value']['mts'])) userNodeMap[user['_id']] = node
from gexf import Gexf import cjdnsmap import sys if len(sys.argv) != 2: print "usage: " + sys.argv[0] + " output.gexf" sys.exit() output_filename = sys.argv[1] nodes, edges = cjdnsmap.get_map() gexf = Gexf("cjdns mapper", "A map of cjdns network") graph = gexf.addGraph("undirected", "static", "cjdns network graph") attr_ip = graph.addNodeAttribute("IP", type="string") attr_version = graph.addNodeAttribute("Version", type="integer") attr_connections = graph.addNodeAttribute("Connections", type="integer") attr_active_connections = graph.addNodeAttribute("Active connections", type="integer") attr_quality = graph.addEdgeAttribute("Quality", 0, type="float") for node in nodes: n = graph.addNode(node.ip, node.name) n.addAttribute(attr_ip, node.ip) n.addAttribute(attr_version, str(node.version)) n.addAttribute(attr_connections, str(node.connections)) n.addAttribute(attr_active_connections, str(node.active_connections))
# Main if __name__ == "__main__": # MongoDB connection client = MongoClient('localhost', 27017) db = client.ccl mails = db.mails threads = db.threads authors = db.authors # Create a graph g = nx.Graph() # Create a graph gexf = Gexf("Anne L'Hote","A bipartite graph of threads vs. authors") graph = gexf.addGraph("directed","static","ccl") output_file = open("data/ccl.gexf","w") for author in authors.find(): # Add a node by author g.add_node(str(author['_id']), { 'viz': { 'color': {'r': authorColor['r'], 'g': authorColor['g'], 'b': authorColor['b']}, 'position': {'y': random.random(), 'x': random.random(), 'z': 0.0}, 'size': author['count']}, 'label': author['email'] }) for thread in threads.find({'count':{'$gte':3}}): # Add a node by thread
def create_gexf_graph(tensor, needEvaluationDetailDict=None): needs = tensor.getNeedIndices() offers = tensor.getOfferIndices() wants = tensor.getWantIndices() date_time = strftime("%Y-%m-%d_%H%M%S") gexf = Gexf(os.path.basename(__file__), date_time) graph = gexf.addGraph('undirected','static','generated need graph') need_type_attr = graph.addNodeAttribute("need type", "undefined", "string") subject_attr = graph.addNodeAttribute("subject attributes", "", "string") content_attr = graph.addNodeAttribute("content attributes", "", "string") category_attr = graph.addNodeAttribute("category attributes", "", "string") if needEvaluationDetailDict: TP_attr = graph.addNodeAttribute("TP", "", "integer") TN_attr = graph.addNodeAttribute("TN", "", "integer") FP_attr = graph.addNodeAttribute("FP", "", "integer") FN_attr = graph.addNodeAttribute("FN", "", "integer") precision_attr = graph.addNodeAttribute("precision", "", "float") recall_attr = graph.addNodeAttribute("recall", "", "float") accuracy_attr = graph.addNodeAttribute("accuracy", "", "float") f0_5score_attr = graph.addNodeAttribute("f0.5score", "", "float") f1score_attr = graph.addNodeAttribute("f1score", "", "float") # add the nodes for need in needs: # add a node for each need node = graph.addNode(need, tensor.getNeedLabel(need)) # set the need type to each node as an attribute if need in offers: node.addAttribute(need_type_attr, "OFFER") if need in wants: node.addAttribute(need_type_attr, "WANT") # get the attributes for each need attr = tensor.getAttributesForNeed(need, SparseTensor.ATTR_SUBJECT_SLICE) node.addAttribute(subject_attr, ', '.join(attr)) attr = tensor.getAttributesForNeed(need, SparseTensor.ATTR_CONTENT_SLICE) node.addAttribute(content_attr, ', '.join(attr)) attr = tensor.getAttributesForNeed(need, SparseTensor.CATEGORY_SLICE) attr.sort() node.addAttribute(category_attr, ', '.join(attr)) # add the statistical detail data to every node if needEvaluationDetailDict: needDetail = needEvaluationDetailDict.retrieveNeedDetails(need) node.addAttribute(TP_attr, str(needDetail.TP)) node.addAttribute(TN_attr, str(needDetail.TN)) node.addAttribute(FN_attr, str(needDetail.FN)) node.addAttribute(FP_attr, str(needDetail.FP)) node.addAttribute(precision_attr, str(needDetail.getPrecision())) node.addAttribute(recall_attr, str(needDetail.getRecall())) node.addAttribute(accuracy_attr, str(needDetail.getAccuracy())) node.addAttribute(f0_5score_attr, str(needDetail.getFScore(0.5))) node.addAttribute(f1score_attr, str(needDetail.getFScore(1))) # add the connections as edges between nodes (needs) nz = tensor.getSliceMatrix(SparseTensor.CONNECTION_SLICE).nonzero() for i in range(len(nz[0])): if nz[0][i] < nz[1][i]: graph.addEdge(str(nz[0][i]) + "_" + str(nz[1][i]), nz[0][i], nz[1][i]) return gexf
import sys import pprint from gexf import Gexf # test helloworld.gexf gexf = Gexf("Home With Kids", "A Demo Graph") graph = gexf.addGraph("directed", "static", "A Demo Graph") atr1 = graph.addNodeAttribute('ID', type='string') atr2 = graph.addNodeAttribute('adult', type='boolean', defaultValue='true') tmp = graph.addNode("0", "刘梅") tmp.addAttribute(atr1, "123456") tmp.addAttribute(atr2, 'true') tmp = graph.addNode("1", "夏东海") tmp.addAttribute(atr1, "534523") tmp.addAttribute(atr2, 'true') tmp = graph.addNode("2", "夏雪") tmp.addAttribute(atr1, "2312311") tmp.addAttribute(atr2, 'false') tmp = graph.addNode("3", "刘星") tmp.addAttribute(atr1, "1231231") tmp.addAttribute(atr2, 'false') tmp = graph.addNode("4", "夏雨") tmp.addAttribute(atr1, "2314889") tmp.addAttribute(atr2, 'false')
from gexf import Gexf from lxml import etree from model.school_chanye import search_school, search_chanye, search_school_chanye, search_school_id import faker f = faker.Faker(locale='zh-CN') school = search_school() chanye = search_chanye() school_chanye = search_school_chanye() school_id = search_school_id() gexf = Gexf("school", "chanye") graph = gexf.addGraph("school", "jiaoyu", "chanye") atr1 = graph.addNodeAttribute(force_id='modularity_class', title='schoolce', defaultValue='true', type='integer') atr2 = graph.addNodeAttribute(force_id='chanye', title='chan_ye', defaultValue='true', type='string') for node in school: node_type = 'school' tmp_node = graph.addNode(str(node[0]), str(node[1])) tmp_node.addAttribute(atr1, str(node[0] - 1)) for node in chanye: node_type = 'chanye'
def mkgexfile(filepath, pathinfo): gexf = Gexf("NJFE_wlw", "A traceability network") graph = gexf.addGraph("directed", "", "Back or Foward traceability") atr1 = graph.addNodeAttribute('commodity_name', mode="dynamic", defaultValue='', type='string', force_id="commodity_name") atr2 = graph.addNodeAttribute('time', mode="dynamic", defaultValue='', type='string', force_id="time") atr3 = graph.addNodeAttribute('nodename', mode="dynamic", type='string', defaultValue='', force_id="nodename") # 节点名称 atr4 = graph.addNodeAttribute( 'nodetype', mode="dynamic", type='integer', defaultValue='', force_id="nodetype") # supplier、repertory和consumer # atr5 = graph.addNodeAttribute('') prodtnames = pathinfo[0] # 产品出厂信息: 产品名称、出厂时间、(还需要补充商家信息) repo = pathinfo[1] # 产品仓库信息:仓库名称、入库时间 consumers = pathinfo[2] # 消费者信息:购买的产品名称、购买的时间 # 厂家节点 for i, p in enumerate(prodtnames): tmp = graph.addNode('s_' + ` i `, "suppliers_%s" % i) tmp.addAttribute(atr1, p[0]) # 产品名称 tmp.addAttribute(atr2, p[1]) # 出厂日期 tmp.addAttribute(atr4, "0") # 节点类型:"supplier" # 仓库节点(销售地点) tmp = graph.addNode("R_1", "Repertory") tmp.addAttribute(atr4, "1") # "repetory" tmp.addAttribute(atr3, repo) for i, c in enumerate(consumers): tmp = graph.addNode("c_" + ` i `, "consumer_%s" % i) tmp.addAttribute(atr1, c[0]) # 产品名称 tmp.addAttribute(atr2, c[1]) # 消费者购买日期 tmp.addAttribute(atr3, "consumer_%s" % i) # 节点名称 tmp.addAttribute(atr4, "2") # 节点类型 "consumer") # print "finished nodes" for i, _ in enumerate(prodtnames): graph.addEdge( ` i `, 's_' + ` i `, "R_1", weight='1') skiplen = len(prodtnames) for i, _ in enumerate(consumers): graph.addEdge( ` (i + skiplen) `, "R_1", "c_" + ` i `, weight='1') # print "finied edges" output_file = open(filepath, "w") gexf.write(output_file) pass
def make_ds_gexf(gexf_name_1, gexf_name_2, G, node_degree, pr_key_users, all_uid_pr, ds_pr_data, partition, ds_new_attribute_dict): gexf = Gexf(gexf_name_1, gexf_name_2) 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='string', force_id='timestamp') graph.addNodeAttribute('pagerank', type='float', force_id='pagerank') #graph.addNodeAttribute('trendsetter_rank', type='float', force_id='trendsetter_rank') graph.addNodeAttribute('acategory', type='string', force_id='acategory') graph.addNodeAttribute('text', type='string', force_id='text') graph.addNodeAttribute('reposts_count', type='string', force_id='reposts_count') graph.addNodeAttribute('comments_count', type='string', force_id='comments_count') graph.addNodeAttribute('attitude_count', type='string', force_id='attitude_count') graph.addNodeAttribute('rank_pr', type='string', force_id='rank_pr') #graph.addNodeAttribute('rank_tr', type='string', force_id='rank_tr') 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 pr_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)) cluster_id = str(partition[node]) _node.addAttribute('acategory', cluster_id) pr = str(all_uid_pr[str(uid)]) _node.addAttribute('pagerank', pr) #print 'all_uid_tr:', all_uid_tr #print 'all_uid_pr:', all_uid_pr #tr = str(all_uid_tr[str(uid)]) #_node.addAttribute('trendsetter_rank', tr) rank_pr = ds_pr_data[uid] _node.addAttribute('rank_pr', str(rank_pr)) #rank_tr = ds_tr_data[uid] #_node.addAttribute('rank_tr', str(rank_tr)) try: text_add = ds_new_attribute_dict[uid][0][0] _node.addAttribute('text', json.dumps(text_add)) reposts_count_add = i2u(ds_new_attribute_dict[uid][0][1]) _node.addAttribute('reposts_count', reposts_count_add) comment_count_add = i2u(ds_new_attribute_dict[uid][0][2]) _node.addAttribute('comments_count', comment_count_add) attitude_count_add = i2u(ds_new_attribute_dict[uid][0][3]) if attitude_count_add == None: attitude_count_add = u'未知' _node.addAttribute('attitude_count', i2u(attitude_count_add)) timestamp_add = i2u(ds_new_attribute_dict[uid][0][4]) _node.addAttribute('timestamp', timestamp_add) except KeyError: _node.addAttribute('text', u'未知') _node.addAttribute('reposts_count', u'未知') _node.addAttribute('comments_count', u'未知') _node.addAttribute('attitude_count', u'未知') _node.addAttribute('timestamp', u'未知') 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', u'未知') _node.addAttribute('location', u'未知') for edge in G.edges(): start, end = edge # (repost_uid, source_uid) 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 gexf
fileitem = form['filename'] fn = os.path.basename(fileitem.filename) open('upload/' + fn, 'wb').write(fileitem.file.read()) except KeyError: printkeywordstogexf() print('<p>No file was uploaded</p>') sys.exit() printkeywordstogexf() print('<p>The file "' + fn + '" was uploaded successfully</p>') thefile = open("upload/" + fn, 'r', encoding="utf-8") tsv = csv.reader(thefile, delimiter='\t') next(tsv) # remove headers gexf = Gexf("Keyword Co-occurrence Network", "File:" + fn + ".") graph = gexf.addGraph("undirected", "static", "Web of Science Keyword network") keywords = [] keywordset = [] for t in tsv: DE = t[19] keywordset.append(DE.split('; ')) for keyw in DE.split('; '): if len(keyw) < 1: # removes empty keywords continue else: graph.addNode(keyw.lower(), keyw.lower()) # add nodes to graph edgelist = [] for k in keywordset: cooccurrence = list(combinations(k, 2))
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)
print("new node is %s,logical nodes is:%s, %s" % (new_node.label, new_node.logical_nodes[0].label, new_node.logical_nodes[1].label)) graph.round_plus() #graph.clear() print("---") #for node in Node.node_list: # print(node) #for edge in Edge.edge_list: # print(edge) gexf = Gexf("myc", "DLG") gephi_graph = gexf.addGraph("directed", "dynamic", "DLG") atr1 = gephi_graph.addNodeAttribute('weight') atr2 = gephi_graph.addNodeAttribute('logical', type="string") for node in Node.node_list: if node.end > 0: tmp = gephi_graph.addNode(id=str(node.id), label=node.label, start=str(node.start), end=str(node.end)) tmp.addAttribute(atr1, str(node.weight)) if node.logical_nodes: logical = "%d %d" % (node.logical_nodes[0].id, node.logical_nodes[1].id) tmp.addAttribute(atr2, logical)
nodeSet = set() edgeSet = {} newNode = set(['color']) for i in range(3): edges = findBSiblings(newNode) currentSiblingSet = set() for edge in edges: currentSiblingSet.add(edge[0]) currentSiblingSet.add(edge[1]) if edgeSet.has_key(edge[0]+"_"+edge[1]) == False and edgeSet.has_key(edge[1]+"_"+edge[0]) == False: edgeSet[edge[0]+"_"+edge[1]] = edge newNode = currentSiblingSet - nodeSet nodeSet = nodeSet | newNode gexf = Gexf("Husonchen", "A hello world! file") graph = gexf.addGraph("undirected", "static", "categories") for node in nodeSet: graph.addNode(node, node) i = 0 for edge in edgeSet.values(): graph.addEdge(str(i),edge[0] , edge[1]) i += 1 output_file = open("helloworld.gexf", "w") gexf.write(output_file)
def make_gexf(gexf_name_1, gexf_name_2, G, node_degree, key_users, all_uid_pr, pr_data, partition, new_attribute_dict): gexf = Gexf(gexf_name_1, gexf_name_2) 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') graph.addNodeAttribute('pagerank', type='string', force_id='pagerank') graph.addNodeAttribute('acategory', type='string', force_id='acategory') graph.addNodeAttribute('text', type='string', force_id='text') graph.addNodeAttribute('reposts_count', type='string', force_id='reposts_count') # 新添加的属性 graph.addNodeAttribute('comments_count', type='string', force_id='comments_count') graph.addNodeAttribute('attitude_count', type='string', force_id='attitude_count') graph.addNodeAttribute('rank_pr', type='string', force_id='rank_pr') # 用户的pagerank值对应的排名 pos = nx.spring_layout(G) # 定义一个布局 pos={node:[v...]/(v...)} node_counter = 0 edge_counter = 0 for node in G.nodes(): x, y = pos[node] # 返回位置(x,y) degree = node_degree[node] if node not in node_id: # {node:排名} 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)) cluster_id = str(partition[node]) _node.addAttribute('acategory', cluster_id) #print 'al_uid_pr:', all_uid_pr pr = str(all_uid_pr[str(uid)]) _node.addAttribute('pagerank', pr) rank = pr_data[uid] _node.addAttribute('rank_pr', str(rank)) #print 'pagarank_uid:', uid try: text_add = new_attribute_dict[uid][0][0] # 添加节点属性--text _node.addAttribute('text', json.dumps(text_add)) reposts_count_add = i2u(new_attribute_dict[uid][0][1]) _node.addAttribute('reposts_count', reposts_count_add) # 添加节点属性--reposts_count comment_count_add = i2u(new_attribute_dict[uid][0][2]) _node.addAttribute('comments_count', comment_count_add) # 添加节点属性--comment_count attitude_count_add = i2u(new_attribute_dict[uid][0][3]) if attitude_count_add == None: attitude_count_add = u'未知' _node.addAttribute('attitude_count', i2u(attitude_count_add)) # 添加节点属性--attitude_count except KeyError: _node.addAttribute('text', u'未知') _node.addAttribute('reposts_count', u'未知') _node.addAttribute('comments_count', u'未知') _node.addAttribute('attitude_count', u'未知') 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', u'未知') _node.addAttribute('location', u'未知') #_node.addAttribute('timestamp', str(uid_ts[uid])) for edge in G.edges(): start, end = edge # (repost_uid, source_uid) 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 gexf
from gexf import Gexf from py2neo import cypher, neo4j __author__ = 'Trevor Grant' # test helloworld.gexf gexf = Gexf(__author__,__title__) graph=gexf.addGraph("directed","static",__desc__) graph_db = neo4j.GraphDatabaseService() query = neo4j.CypherQuery(graph_db, "MATCH (n) WHERE n.on_path=1 RETURN n" ) results = query.execute() # Query for relationships where both nodes have .on_path for r in results: quite =graph.addNode(str(r[0]._id),r[0]['KEYWORD_TEXT']) # if r[0]._id == 2418: print 'yup' RESULTS_PER_PAGE= 1000 # Query for nodes with .on_path paging = False skip=0 query = neo4j.CypherQuery(graph_db, "MATCH (m {on_path:1})-[r]->(n {on_path:1}) RETURN r ORDER BY ID(r) SKIP %i LIMIT %i" % (skip,RESULTS_PER_PAGE )) results = query.execute() if len(results)==1000: paging=True skip += RESULTS_PER_PAGE # Query for relationships where both nodes have .on_path
def convert(team_name, channels_list, graph='mention_based_graph_info', user='******', pwd='FluoBySusTech', port=3306, host='10.20.13.209', dbname='rowdata'): from gexf import Gexf from textblob import TextBlob import random import pymysql import pandas as pd try: con = pymysql.Connect( host=host, # 远程登录主机的ip地址 port=port, # mysql服务器的端口 user=user, # mysql的用户 passwd=pwd, # 密码 db=dbname, # 需要的数据库名称 ) # 获取本次的游标 cur = con.cursor() except pymysql.Error as e: print("Error %d: %s" % (e.args[0], e.args[1])) cur.execute('select * from team_channel_relation ') team_to_channel = cur.fetchall() team_to_channel = list(map(list, zip(*team_to_channel))) team = team_to_channel[0][:] channel = team_to_channel[1][:] team_to_channel = {'team': team, 'channel': channel} team_to_channel = pd.DataFrame(team_to_channel) del team del channel for channel_file in channels_list: gexf = Gexf("Gephi.org", "A Web network") output = gexf.addGraph("directed", "static", "A Web network") cur.execute('select * from people_channel_relation where channel_id = \'' + channel_file + '\' ') person_and_channel = cur.fetchall() if len(person_and_channel) == 0: output_file = open(".\graphdata_of_" + channel_file + ".gexf", "wb") gexf.write(output_file) output_file.close() else: person_and_channel = list(map(list, zip(*person_and_channel))) person = person_and_channel[0][:] channel = person_and_channel[1][:] person_and_channel = {'person': person, 'channel': channel} person_and_channel = pd.DataFrame(person_and_channel) del person del channel person_list = person_and_channel['person'] # print(person_and_channel) channel_node = output.addNodeAttribute(force_id="Channel", title="channel", type="String") team_node = output.addNodeAttribute(force_id="Team", title="team", type="String") weight_node = output.addNodeAttribute(force_id="Weight", title="weight", type="float") people_id = dict() # id_list = ['people'] # for id in id_list: # cur.execute('select * from ' + id) # data = cur.fetchall() # for i in data: # exec(id + '_id[\'' + i[0] + '\']=i[1]') # print('id', id) person_set = set(person_list) person_to_channel = [] for tem_person in person_set: cur.execute('select * from people_channel_relation where people_id = \'' + tem_person + '\' ') person_to_channel = person_to_channel + list(cur.fetchall()) person_to_channel = list(map(list, zip(*person_to_channel))) person = person_to_channel[0][:] channel = person_to_channel[1][:] person_to_channel = {'person': person, 'channel': channel} person_to_channel = pd.DataFrame(person_to_channel) # print(person_to_channel) cc = 0 num2333 = len(person_set) for tem_id in person_set: # print(cc / num2333) try: tem_name = people_id[tem_id] except KeyError: tem_name = "Null" tem_channel_list = set(person_to_channel[person_to_channel['person'] == tem_id]['channel']) tmp_node = output.addNode(tem_id, tem_name) tmp_node.addAttribute(weight_node, str(int(100 * random.random()))) tem_team_list = set() for tem_channel in tem_channel_list: # cur.execute('select team_id from team_channel_relation where channel_id = \'' + tem_channel + '\'') # tem_team_list = cur.fetchall() tem_team_list = tem_team_list | set( team_to_channel[team_to_channel['channel'] == tem_channel]['team']) for tem_team in tem_team_list: tmp_node.addAttribute(team_node, tem_team) for tem_channel in tem_channel_list: tmp_node.addAttribute(channel_node, tem_channel) cc = cc + 1 m = 'mention_based_graph_info' cur.execute('select * from ' + m + ' where channel_id = \'' + channel_file + '\' ') data = cur.fetchall() msg_att = output.addEdgeAttribute(force_id="Message", title="message", type='String', defaultValue='None') weight_att = output.addEdgeAttribute(force_id="Weight", title="weight", type='float', defaultValue='0') date_att = output.addEdgeAttribute(force_id="Date", title="date", type='float', defaultValue='None') channel_att = output.addEdgeAttribute(force_id="Channel", title="channel", type='String', defaultValue='None') team_att = output.addEdgeAttribute(force_id="Team", title="team", type='String', defaultValue='None') cc = 0 numhehe = len(data) for tem_m in data: # print(cc / numhehe) sender, receiver, text, channel_id, team_id, ts = tem_m blob = TextBlob(text) weight = str(blob.sentiment.polarity) try: tem_edge = output.addEdge(sender + receiver + str(cc), sender, receiver, weight=weight) except Exception: try: tmp_node = output.addNode(receiver, 'Null') tem_edge = output.addEdge(sender + receiver + str(cc), sender, receiver, weight=weight) except Exception: tmp_node = output.addNode(sender, 'Null') tem_edge = output.addEdge(sender + receiver + str(cc), sender, receiver, weight=weight) cc = cc + 1 tem_edge.addAttribute(msg_att, text) tem_edge.addAttribute(weight_att, weight) tem_edge.addAttribute(date_att, str(ts)) tem_edge.addAttribute(team_att, team_id) tem_edge.addAttribute(channel_att, channel_id) output_file = open(dir_path + "/mention_based/{}_{}.gexf".format(team_name, channel_file), "wb") gexf.write(output_file) output_file.close()
from gexf import Gexf from lxml import etree from model.school_xuke import search_school, search_xueke, search_school_xuke, search_school_id import faker f = faker.Faker(locale='zh-CN') school = search_school() xuke = search_xueke() school_xuke = search_school_xuke() school_id = search_school_id() gexf = Gexf("school", "xueke") graph = gexf.addGraph("school", "jiaoyu", "xueke") atr1 = graph.addNodeAttribute(force_id='modularity_class', title='schoolce', defaultValue='true', type='integer') atr2 = graph.addNodeAttribute(force_id='xueke', title='xu_ke', defaultValue='true', type='string') for node in school: node_type = 'school' tmp_node = graph.addNode(str(node[0]), str(node[1])) tmp_node.addAttribute(atr1, str(node[0] - 1)) for node in xuke: node_type = 'xuke'
#!/usr/bin/python from gexf import Gexf,GexfImport # test helloworld.gexf gexf = Gexf("Paul Girard","A hello world! file") graph=gexf.addGraph("directed","static","a hello world graph") graph.addNode("0","hello") graph.addNode("1","World") graph.addEdge("0","0","1") output_file=open("hellowrld.gexf","w") gexf.write(output_file) # test GexfImport f = open("exp.gexf") o = open("exp_bootstrap_bootstrap.gexf", "w+") gexf_import = GexfImport(f).gexf() gexf_import.write(o)
bts = {} temp = {} # CSV bts parsing for row in reader1: array = row[0].split(";") id = str(array[0]) lat = array[1] lon = array[2] flux = array[3] bts[id] = (lat, lon, flux) # create graph gexf = Gexf("Quentin", "foreigner people") graph = gexf.addGraph("directed", "static", "world graph map") # add Attribute latitude = graph.addNodeAttribute("lat", "0", "double") longitude = graph.addNodeAttribute("lon", "0", "double") flux_interne = graph.addNodeAttribute("flux_interne", "0", "double") edgeid = 1 for row in reader2: # CSV edge parsing array = row[0].split(";") source = array[0] target = array[1] weight = array[2] if source != "source" and target != "target":
try: conn = MySQLdb.connect(host=DB_HOST, user=DB_USER, passwd=DB_PSWD, db=DB_NAME, use_unicode=True) except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) sys.exit(1) cursor = conn.cursor() if __name__ == '__main__': gexf = Gexf("PIC BROTHER", "Le graph du pic !") graph = gexf.addGraph("undirected", "static", "Picbrother graph") cursor.execute("SELECT id, fb_id, first_name, last_name FROM `T_USER` ") rows = cursor.fetchall() for row in rows: print "%s, %s, %s, %s" % (row[0], row[1], row[2], row[3]) #graph.addNode(row[0],u"#%s => %s %s" % (row[1],row[2],row[3])) # ==> Probléme d'accent... Comment faire ? graph.addNode(str(row[0]), unac.unac_string("%s %s" % (row[2], row[3]), "utf-8")) print "Nombre de user ajoute : %d" % cursor.rowcount cursor.execute( "SELECT U1.user_id, U2.user_id, COUNT( * ) weight FROM `J_PHOTO_USER` U1, `J_PHOTO_USER` U2 WHERE U1.photo_id = U2.photo_id AND U1.user_id != U2.user_id GROUP BY U1.user_id, U2.user_id" ) rows = cursor.fetchall() edge_id = 0 for row in rows: print "%s, %s, %s, %s" % (edge_id, row[0], row[1], row[2])
import sys, pprint from gexf import Gexf import codecs input_file = codecs.open("pr-lpa2.txt", encoding="utf-8") gexf = Gexf("2018st27", "LPA") graph = gexf.addGraph("undirected", "static", "LPA") class_attr = graph.addNodeAttribute("Class", defaultValue=0, type="integer", force_id="class") pr_attr = graph.addNodeAttribute('PageRank', defaultValue=0.0, type="double", force_id="pageranks") i = 0 library = {} for line in input_file: strs = line.split("\t") label_name = strs[0].split("#") library[label_name[1]] = i node = graph.addNode(i, label_name[1]) node.addAttribute(class_attr, label_name[0]) node.addAttribute(pr_attr, label_name[2]) i += 1 i = 0 input_file.seek(0)
#!/usr/bin/python2.7 import pygeoip as geoip from gexf import Gexf db = geoip.Database('GeoIP.dat') gexf = Gexf("Alexander Konovalov", "The internet map") graph = gexf.addGraph("directed", "static", "The internet map") attrid = graph.addNodeAttribute("country", "??", type="string") def addIP(ip): if not graph.nodeExists(ip): info = db.lookup(ip) country = "??" if info.country: country = info.country n = graph.addNode(ip, ip) n.addAttribute(attrid, country) n = 0 with open("out.csv") as f: for line in f: ips = tuple(line.strip().replace("-", ".").split('\t')) if len(ips) < 2: continue ip1, ip2 = ips addIP(ip1) addIP(ip2)
calctime = time.split('e+') time = str(float(calctime[0]) * pow(10, int(calctime[1]))) elif 'e-' in time: calctime = time.split('e-') time = str(float(calctime[0]) * pow(10, int(calctime[1]) * (-1))) return time lsArgs = sys.argv trace_file = open(lsArgs[1], 'r') trace_lines = trace_file.readlines() trace_file.close() gexf = Gexf("Pedro and David", "Dynamic graph") graph = gexf.addGraph("directed", "dynamic", "Graph") idTotalNodeDuration = graph.addNodeAttribute("Total Duration", "0.0", "float") #name, default, type idTotalEdgeDuration = graph.addEdgeAttribute("Total Duration", "0.0", "float") idLinkType = graph.addEdgeAttribute("Type", "None", "string") idNumMsgs = graph.addEdgeAttribute("Number of messages", "0", "integer") dicEvents = {} i = 0 while (trace_lines[i].startswith('%') == True): if ('%EventDef' in trace_lines[i]): lsLine = trace_lines[i].split(' ') eventName = str(lsLine[1])
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)
#!/usr/bin/python from gexf import Gexf, GexfImport # test helloworld.gexf gexf = Gexf("Paul Girard", "A hello world! file") graph = gexf.addGraph("directed", "static", "a hello world graph") graph.addNode("0", "hello") graph.addNode("1", "World") graph.addEdge("0", "0", "1") output_file = open("hellowrld.gexf", "w") gexf.write(output_file) # test GexfImport f = open("exp.gexf") o = open("exp_bootstrap_bootstrap.gexf", "w+") gexf_import = GexfImport(f).gexf() gexf_import.write(o)