def makeGexf(): raw_file_name = "gexf_format_test.csv" #raw_file_name = "HKALL_tags_RT10count.csv" gexf = Gexf("Ching-Ya Lin", "Keyword relation by hours.") graph = gexf.addGraph("undirected", "dynamic", "graph!", "datetime") wid = graph.addNodeAttribute("Weight", "1", "integer", "dynamic") ewid = graph.addEdgeAttribute("EdgeWeight", "1", "integer", "dynamic") if (1): f = codecs.open(raw_file_name, "r", "utf-8") #with open(raw_file_name, 'r') as f: reader = csv.reader(f) w_d = defaultdict() #weight_dictionary for all keywords e_w_d = defaultdict() #edge for row in reader: #row = [x.decode('utf8') for x in rowx] tt = time.strptime(row[0], "%Y-%m-%d %H") rtt = time.strftime("%Y-%m-%d %H:00:00", tt) words = row[1].split('/') templist = [] #one row list to help addEdge for w in words: # useless : w = w.encode('utf8') #print w #print chardet.detect(w) templist.append(w) if graph.nodeExists(w): n = graph.nodes[w] v = w_d[w] + 1 #v = int(n.attributes[int(wid)]["value"]) #n.attributes will get the initial value which we don't want. n.addAttribute(wid, str(v), rtt) w_d[w] = v else: new = graph.addNode(w, w, rtt) new.addAttribute(wid, "1", rtt) w_d[w] = 1 for w1 in templist: for w2 in templist: if w1 != w2: s1 = w1 + w2 s2 = w2 + w1 if graph.edgeExists(s1): e = graph.edges[s1] val = e_w_d[s1] + 1 e.addAttribute(ewid, str(val), rtt) e_w_d[s1] = val elif graph.edgeExists(s2): e = graph.edges[s2] val = e_w_d[s2] + 1 e.addAttribute(ewid, str(val), rtt) e_w_d[s2] = val else: enew = graph.addEdge(s1, w1, w2, 1, rtt) enew.addAttribute(ewid, "1", rtt) e_w_d[s1] = 1 templist.remove(w1) output_file = open("eventhr.gexf", "w") gexf.write(output_file)
def createGexfNodes(tweetList): """ Creates the Nodes from the given list of Tweets """ gexf = Gexf("Ronan Kervella / Karim Hamidou","Graph for USA 2012 Presidential elections") graph = gexf.addGraph("directed","static","Graph for USA 2012 Presidential elections") createAttributes(graph) graphIndex = 0 for tweet in tweetList: graph.addNode(graphIndex,tweet.userName) graph.nodes[str(graphIndex)].addAttribute(str(1),str(tweet.userId)) graph.nodes[str(graphIndex)].addAttribute(str(2),tweet.userName) graph.nodes[str(graphIndex)].addAttribute(str(3),tweet.date) graph.nodes[str(graphIndex)].addAttribute(str(4),tweet.text) graph.nodes[str(graphIndex)].addAttribute(str(5),tweet.profileImgUrlHttp) graph.nodes[str(graphIndex)].addAttribute(str(6),tweet.source) graphIndex += 1 return gexf
class MainClient: DESTINATION_TEMPLATE = 'parsed-output-{}.gexf' def __init__(self, input_file, version, cloud_storage_client, creator, name): self.version = version self.input_file = input_file self.cloud_storage_client = cloud_storage_client self.gexf = Gexf(creator, name) self.graph = self.gexf.addGraph("directed", "static", name) def md5(self, string): return hashlib.md5(string.encode('utf-8')).hexdigest() def start(self): DESTINATION_NAME = self.DESTINATION_TEMPLATE.format(self.version) input_content = self.cloud_storage_client.download_string(self.input_file) rows = [x for x in csv.DictReader([x for x in input_content.splitlines() if len(x) > 0])] used_nodes = {} used_edges = {} last_node_id = 1 last_edge_id = 1 for line in rows: source_url = line['SourceURL'] target_url = line['TargetURL'] if not used_nodes.get(source_url): used_nodes[source_url] = last_node_id self.graph.addNode(str(last_node_id), source_url) last_node_id += 1 if not used_nodes.get(target_url): used_nodes[target_url] = last_node_id self.graph.addNode(str(last_node_id), target_url) last_node_id += 1 edge_md5 = self.md5('{}{}'.format(source_url, target_url)) if not used_edges.get(edge_md5): used_edges[edge_md5] = last_edge_id self.graph.addEdge(str(last_edge_id), str(used_nodes[source_url]), str(used_nodes[target_url])) last_edge_id += 1 log(self.gexf.getXML()) result_str = lxml.etree.tostring(self.gexf.getXML(), pretty_print=True, encoding='utf-8', xml_declaration=True).decode('utf-8') self.cloud_storage_client.upload_string(result_str, DESTINATION_NAME)
def __init__(self, input_file, version, cloud_storage_client, creator, name): self.version = version self.input_file = input_file self.cloud_storage_client = cloud_storage_client self.gexf = Gexf(creator, name) self.graph = self.gexf.addGraph("directed", "static", name)
if len(bankList.keys())!=0: for bankID in bankList: graph.nodes[bankID].addAttribute('state',value = bankList[bankID][1], start =str(bankList[bankID][0]), end=str(EndTime)) for bankID in diff(range(len(graph.nodes.keys())),bankList.keys()): graph.nodes[bankID].addAttribute('state',value = 1, start ='0', end=str(EndTime)) return graph # Creating toy example # state = 1 => solvant # state = 2 => exposed # state = 3 => feail # state = 4 => dead List = [] # It is important that the list is ordered by time List.append((1,0,1)) #(time, bankID, state) List.append((1,1,4)) List.append((2,0,4)) List.append((3,2,2)) List.append((4,2,3)) List.append((5,2,4)) # Creating list of edges to be cut at a specific time EdgeList = {} EdgeList[(2,20)]=3 EdgeList[(2,21)]=4 EdgeList[(2,6)]=5 gexf = Gexf("Financial Contagion","A dynamic network model") G0 = pickle.load(open('/Users/linnlii/Documents/GitHub/pygexf/test/ToyGraph.p', 'rb')) graph = convertGraphs(G0,EdgeList) graph = addAttributes(graph,List) output_file=open("DynamicNetworks.gexf","w") gexf.write(output_file)
import sys,pprint sys.path.append('../gexf') from _gexf import Gexf, GexfImport #test ME274_2015.gexf f = open("ME274_2016.gexf") gexf_import = Gexf.importXML(f) f.close() f = open("ME274_2016.gexf") gexf_import2 = Gexf.importXML(f) f.close() print "Text gexf comparision "+ str(gexf_import == gexf_import2) graph = gexf_import.graphs[0] #Display node list for node_id, node in graph.nodes.iteritems(): print node.label pprint.pprint(node.getAttributes(), indent = 1, width = 1) #Display edges list for edgeid, edge in graph.edges.iteritems() : print str(graph.nodes[edge.source]) + " -> "+str(graph.nodes[edge.target]) pprint.pprint(edge.getAttributes(), indent = 1, width = 1) o = open("ME274_2016.gexf","w") gexf_import.write(o)
import sys,pprint sys.path.append('../gexf') from _gexf import Gexf, GexfImport #test ME274_2015.gexf f = open("ME274_2015.gexf") gexf_import = Gexf.importXML(f) f.close() f = open("ME274_2015.gexf") gexf_import2 = Gexf.importXML(f) f.close() print "text gexf comparision "+ str(gexf_import == gexf_import2) graph = gexf_import.graphs[0] #Display node list for node_id, node in graph.nodes.iteritems(): print node.label pprint.pprint(node.getAttributes(), indent = 1, width = 1) #Display edges list for edgeid, edge in graph.edges.iteritems() : print str(graph.nodes[edge.source]) + " -> "+str(graph.nodes[edge.target]) pprint.pprint(edge.getAttributes(), indent = 1, width = 1) o = open("ME274_2015.gexf","w") gexf_import.write(o)
#!/usr/bin/python import sys, pprint sys.path.append('../gexf') 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("helloworld.gexf", "w") gexf.write(output_file) # test GexfImport f = open("gexf.net.dynamics_openintervals.gexf") gexf_import = Gexf.importXML(f) f.close() f = open("gexf.net.dynamics_openintervals.gexf") gexf_import2 = Gexf.importXML(f) f.close() print "test gexf comparision " + str(gexf_import == gexf_import2) graph = gexf_import.graphs[0] # display nodes list for node_id, node in graph.nodes.iteritems(): print node.label
endtime = time.strptime("20131014080000","%Y%m%d%H%M%S") # event type matching the @type to be returned from elasticsearch evttype = "apache-access" # field and time period to be used for edge detection relatedfield = "clientip" relatetimeout = 5 # debug messages ? verbose = True # create the graph gexf = Gexf("es2gexf","logstash graph") graph=gexf.addGraph("directed","dynamic","logstash graph","dateTime") # define the node attributes in the graph attrType = graph.addNodeAttribute("type", "null", "String","static","type") attrMessage = graph.addNodeAttribute("message", "null", "String","static","message") attrSource_host = graph.addNodeAttribute("source_host", "null", "String","static","source_host") attrSource_path = graph.addNodeAttribute("source_path", "null", "String","static","source_path") attrClientip = graph.addNodeAttribute("clientip", "null", "String","static","clientip") attrVerb = graph.addNodeAttribute("verb", "null", "String","static","verb") attrResponse = graph.addNodeAttribute("response", "null", "integer","static","response") attrRequest = graph.addNodeAttribute("request", "null", "String","static","request") attrBytes = graph.addNodeAttribute("bytes", "null", "integer","static","bytes") attrAgent = graph.addNodeAttribute("agent", "null", "String","static","agent") attrReferrer = graph.addNodeAttribute("referrer", "null", "String","static","referrer")
# # gexf_converter - A part of PPAS implementation # Convert the Apriori results into gexf format. # # Code written by Xander Wang on 17th April, 2018. # # Institute of Distributed Computing, Chuzhou University. from _gexf import Gexf # test helloworld.gexf gexf = Gexf("Gephi.org", "A Web network") graph = gexf.addGraph("directed", "static", "A Web network") atr1 = graph.addNodeAttribute('url', type='string') atr2 = graph.addNodeAttribute('indegree', type='float') 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, '1') tmp = graph.addNode("3", "BarabasiLab")
self.edge_obj.addAttribute(id=id, value=value, start=start, end=end,startopen=startopen,endopen=endopen) def gexf(self) : return self.gexf_obj ########NEW FILE######## __FILENAME__ = test #!/usr/bin/python import sys,pprint sys.path.append('../gexf') 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("helloworld.gexf","w") gexf.write(output_file) # test GexfImport f = open("gexf.net.dynamics_openintervals.gexf") gexf_import = Gexf.importXML(f) f.close() f = open("gexf.net.dynamics_openintervals.gexf") gexf_import2 = Gexf.importXML(f)