def createGraphML(): global g g = Graph() c.execute('select uid from userdata') dataList = c.fetchall() gnodes=[] edges=[] for i in dataList: i=str(i) i = i.replace("(","").replace(",)","").replace("L","") i= int(i) c.execute('select distinct low from graphdata where high=?', (i,)) relate=c.fetchall() if not i in gnodes: g.add_node(i) gnodes.append(i) for e in relate: e=str(e) e = e.replace("(","").replace(",)","").replace("L","") e=int(e) if not e in gnodes: g.add_node(e) gnodes.append(e) g.add_edge_by_label(str(i), str(e)) parser = GraphMLParser() parser.write(g, "myGraph.graphml")
def createGraphML(): global g g = Graph() c.execute('select uid from userdata') dataList = c.fetchall() gnodes = [] edges = [] for i in dataList: i = str(i) i = i.replace("(", "").replace(",)", "").replace("L", "") i = int(i) c.execute('select distinct low from graphdata where high=?', (i, )) relate = c.fetchall() if not i in gnodes: g.add_node(i) gnodes.append(i) for e in relate: e = str(e) e = e.replace("(", "").replace(",)", "").replace("L", "") e = int(e) if not e in gnodes: g.add_node(e) gnodes.append(e) # edges.append(i) # edges.append(e) #1 if edges2.count(e) > 1: g.add_edge_by_label(str(i), str(e)) parser = GraphMLParser() parser.write(g, "myGraph.graphml")
def __write_graphML(self): """Writes the .graphml file. Uses class Graph from pygraphml library to create the graph. Also it uses class GraphMLParser from pygraphml library to write the file. """ gr = Graph() # Adding nodes in the graph with properties. for sub in self.nodes_prop.keys(): n = gr.add_node(sub) for pair_prop in self.nodes_prop[sub]: n[pair_prop[0]] = pair_prop[1] # Adding nodes in the graph without properties. for node in self.nodes.values(): if node not in self.nodes_prop.keys(): gr.add_node(node) # Checking the relations between nodes and creating respective edges. for relation in self.relations: source = self.nodes[relation[0]] target = self.nodes[relation[2]] edge = gr.add_edge_by_label(source, target) edge.set_directed(True) edge['model'] = relation[1] # Writting the file. parser = GraphMLParser() file_name = self.file_name.split(".") file_name = file_name[0] parser.write(gr, file_name + ".graphml") print("File " + file_name + ".graphml is succesfully written")
def generate_graph(similarity, attr): from pygraphml import Graph items = similarity.items() labels = np.array([artist for artist, obj in items]) network = np.zeros((labels.size, labels.size)) g = Graph() for artist in labels: g.add_node(artist) for artist_id, x in enumerate(items): network[artist_id, artist_id] = 1 artist, obj = x if attr == "emotion": for idx, score in enumerate(obj["emotion_sim"]): if network[artist_id, idx] == 0 and network[idx, artist_id] == 0: edge = g.add_edge_by_label(labels[artist_id], labels[idx]) if score == 0: edge["weight"] = 0.001 else: edge["weight"] = score network[artist_id, idx] = 1 network[idx, artist_id] = 1 elif attr == "topic": for idx, score in enumerate(obj["topic_sim"]): if network[artist_id, idx] == 0 and network[idx, artist_id] == 0: edge = g.add_edge_by_label(labels[artist_id], labels[idx]) if score == 0: edge["weight"] = 0.001 else: edge["weight"] = score network[artist_id, idx] = 1 network[idx, artist_id] = 1 return g
def generate_graph_with_clusters(summary, attr): from pygraphml import Graph items = summary.items() labels = np.array([artist for artist, obj in items]) g = Graph() for artist in labels: g.add_node(artist) if attr == "emotion": for category in EMOTION_CATEGORIES: g.add_node(category) elif attr == "topic": for category in TOPIC_CATEGORIES: g.add_node(category) for artist_id, x in enumerate(items): arist, obj = x if attr == "emotion": for idx, score in enumerate(obj["emotions"]): edge = g.add_edge_by_label(EMOTION_CATEGORIES[idx], labels[artist_id]) if score == 0: edge["weight"] = 0.001 # Set to very small value else: edge["weight"] = score elif attr == "topic": for idx, score in enumerate(obj["topics"]): edge = g.add_edge_by_label(TOPIC_CATEGORIES[idx], labels[artist_id]) if score == 0: edge["weight"] = 0.001 # Set to very small value else: edge["weight"] = score return g