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 build_gexf(path, out_name, p_sample = 1): if not Gexf_loaded: print 'Could not import Gexf from gexf module.' return graphs = {} gexfs = {} call(['mkdir', 'data/graphs/' + out_name]) files = os.listdir(path) for (fi, file) in enumerate(files): print file if not re.match('part-.-[0-9]{5}',file): print 'Filename', file, 'not valid data. Skipping...' continue if os.path.isdir(file): print 'File', file, 'is directory. Skipping...' continue f = open(os.path.join(path,file), 'r') line = f.readline() while line: if np.random.rand() > p_sample: line = f.readline() continue if np.random.rand() < 0.0001: print sorted([ (len(graphs[t]._nodes), len(graphs[t]._edges), t) for t in graphs ]) fields = line_to_fields(line) if len(fields) != 12: # print 'Bad line', line, '. Skipping...' line = f.readline() continue dst_status_id = fields[0] src_status_id = fields[4] time = fields[5] topic = fields[8] # Clean non ASCII chars from topic name, since XML complains. topic = ''.join(c for c in topic if ord(c) < 128) if topic not in gexfs: gexfs[topic] = Gexf("Stanislav Nikolov", topic) graphs[topic] = gexfs[topic].addGraph("directed", "dynamic", topic) if not graphs[topic].nodeExists(src_status_id): graphs[topic].addNode(id = str(src_status_id), label = "", start = time, end = "Infinity") if not graphs[topic].nodeExists(dst_status_id): graphs[topic].addNode(id = str(dst_status_id), label = "", start = time, end = "Infinity") graphs[topic].addEdge(id = "(" + str(src_status_id) + "," + str(dst_status_id) + ")", source = src_status_id, target = dst_status_id, start = time, end = "Infinity") line = f.readline() for topic in gexfs.keys(): clean_topic = ''.join([ c for c in topic if (c != '#' and c != ' ') ]) out = open("data/graphs/" + out_name + "/" + clean_topic + ".gexf", "w") gexfs[topic].write(out) out.close()
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 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 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 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 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
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
if 'e+' in time: 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(' ')
Content-Type: text/html\n <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)
from gexf import Gexf, GexfImport #gephx is xml, which does not support accents, so we need to remove them def remove_accents(word): return ''.join(x for x in unicodedata.normalize('NFKD', word) if x in string.ascii_letters) df = pd.read_csv('authors_counts.csv', delimiter=',', index_col=0) rows = list(df.index) cols = list(df.columns.values) num = len(rows) gexf = Gexf("emCOMP", "Authors") G = gexf.addGraph("directed", "static", "Authors") unreadable_names = {} #add nodes for r in rows: #gephx needs unicode, make sure the string is unicode if isinstance(unicode(r, "utf-8"), str): print "ordinary string" elif isinstance(unicode(r, "utf-8"), unicode): print "unicode string" #Checking myself print(r) print(remove_accents(unicode(r, "utf-8"))) new_string = remove_accents(unicode(r, "utf-8")) count = 0
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)
import sys sys.path.append('../gexf') from gexf import Gexf, GexfImport df = pd.read_csv('article_similarity_filtered.csv', delimiter=',', index_col=0) dates = pd.read_csv('master_articles-scraped_filtered_aritcles.csv', delimiter=',') rows = list(df.index) rows.remove('Article URL') cols = list(df.columns.values) num = len(rows) cols.remove('Article URL.1') gexf = Gexf("emCOMP", "Article Similarity") graph = gexf.addGraph("undirected", "static", "Article Similarity") #add nodes for r in rows: graph.addNode(r, dates.loc[r, "updated_domain"]) #add edges count = 0 count_r = 0 for r in rows: print("r" + str(count_r)) for c in cols: #can be adapted for directed graph if (df.loc[r, c] > .85): r_date = datetime.datetime.strptime(str(dates.loc[r, "first_ts"]),
DB_NAME = "picbrother" 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:
#!/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)
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
# 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}}):
#!/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)
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 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 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)
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)
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'
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))
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")
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')
# # 根据输入文件构建国家交互网络 # def graphBuild(nodeList, edgeList): # # for indexs in nodeList.index: # # graphCon.add_node(nodeList.node[indexs].strip()) # # 上面注释了的代码是添加节点,不过也可以不需要,因为下面添加边的时候,边两端的节点会自动添加到网络中 # for indexs in edgeList.index: # countryGraph.add_edge(edgeList.edgeNode1[indexs].strip(), edgeList.edgeNode2[indexs].strip(), # weight=edgeList.numCount[indexs]) # 添加边与权重 # graphBuild(nodeList, edgeList) # 运行网络构建函数 # # 输出网络 # nx.write_gexf(countryGraph, gexfPath + 'graph_' + dataTime + 'c1.gexf') # 输出为Gephi格式 # print("ss") # print(gexf) gexf = Gexf("Gephi.org", "A Web network") graph = gexf.addGraph("undirected", "static", "A Web network") atr1 = graph.addNodeAttribute('Modularity_class', force_id='modularity_class', type='integer') atr2 = graph.addNodeAttribute('country', force_id='country', type='string') atr3 = graph.addNodeAttribute('nodenumber', force_id='nodenumber', type='integer') # atr3 = graph.addNodeAttribute('frog',type='boolean',defaultValue='true') eatr1 = graph.addEdgeAttribute('country1', force_id='country1', type='string', defaultValue='country1') eatr2 = graph.addEdgeAttribute('country2', force_id='country2',