def saveGraph(graph): path = 'temp' filePath = os.path.join(path, 'graph.dot') snap.SaveGViz(graph, filePath) return os.path.abspath(filePath)
if net.GetIntAttrDatN(nid, "state") == 0: NIdColorH[nid] = "green" elif net.GetIntAttrDatN(nid, "state") == 1: NIdColorH[nid] = "red" else: print "Error! There is infectious node in the network!" #plotting the population over time plt.plot(echo, susceptible, color='g') plt.plot(echo, infectious, color='r') plt.xlabel('Echos') plt.ylabel('Nodes') plt.title('SIS simulation over %d echos' % MAX_ECHO) plt.savefig("SIS_%dtl_%dpb_%decho.png" % (tl, contagion_probability_base, MAX_ECHO)) # Save the net in .dot file (plot with Gephi), snap.DrawGViz() too slow on big graph snap.SaveGViz(net, "SISnet.dot", "SIR simulation network", True, NIdColorH) #generate subgraph V = snap.TIntV() sub_node = [] for i in initial_list: for connectedNode in net.GetNI(i).GetOutEdges(): sub_node = list(set(sub_node) | set([connectedNode])) sub_node = list(set(sub_node) | set([i])) for nid in sub_node: V.Add(nid) sub = snap.ConvertSubGraph(snap.PNEANet, net, V) #Save subgraph snap.SaveGViz(sub, "SISsub.dot", "SIR simulation subnet", True, NIdColorH)
#properties of largest strongly connected component big_scc = snap.GetMxScc(repliesgraph) snap.PrintInfo(big_scc, "Largest strongly connected component") num_dir_edges = snap.CntUniqDirEdges(big_scc) print "{0:.2f}% of directed edges are reciprocal".format( snap.CntUniqBiDirEdges(big_scc) * 2 * 100 / num_dir_edges) print "The clustering coefficient is {0:.2f}%".format( snap.GetClustCf(big_scc) * 100) print "The diameter is approximately {0}".format( snap.GetBfsFullDiam(big_scc, 1000)) #store CC for graphviz snap.SaveGViz(big_scc, filename + ".dot", "Largest Connected Component") print "Saved GraphViz" #page rank over entire graph to detect "celebrities" PRankH = snap.TIntFltH() snap.GetPageRank(repliesgraph, PRankH) page_rank = {} for userid in PRankH: if PRankH[userid] > 0: page_rank[userid] = PRankH[userid] page_rank = sorted(page_rank.items(), key=lambda x: x[1], reverse=True) print "Nodes with the highest page rank:" celebs = map(lambda x: str(x[0]), page_rank[:100]) infos = twitterapi.lookup_user(user_id=','.join(celebs)) for info in infos: print info['name'], info['verified']
sys.path.append("/courses/tsks11/ht2019/snap-4.1.0-4.1-centos6.5-x64-py2.6/") TPATH = "/courses/tsks11/ht2019/data_and_fcns/session1/" # PNGraph, a directed graph; # PUNGraph, an undirected graph; # PNEANet, a directed network; # Generate a complete graph with 5 nodes (K5) and look at it via Graphviz G = snap.GenFull(GraphType=snap.PUNGraph, Nodes=5) # for EI in G.Edges(): # print "link from %d to %d" % (EI.GetSrcNId(), EI.GetDstNId()) # Saves Graph to the .DOT file format used by GraphViz snap.SaveGViz(Graph=G, OutFNm="_undirected-completely-connected.dot", Desc="Undirected Completely Connected Network", NodeLabels=True) # Draws undirected graphs using the generated .dot file os.system( "neato -Tpdf _undirected-completely-connected.dot >_undirected-completely-connected.pdf" ) # Generate a star graph and visualize in Graphviz G = snap.GenStar(GraphType=snap.PNGraph, Nodes=10, IsDir=True) # for EI in G.Edges(): # print "edge: (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId()) # Saves Graph to the .DOT file format used by GraphViz snap.SaveGViz(Graph=G, OutFNm="_directed-star.dot", Desc="Directed Star Graph",
degree = node.GetDeg() for i in range(degree): if not node.GetNbrNId(i) in NIdV and not node.GetNbrNId( i) in NNIdV and node.GetNbrNId(i) != nodefirst: NNIdV.Add(node.GetNbrNId(i)) Nlist.Add(node.GetNbrNId(i)) Nlist.Add(nodefirst) SubGraph = snap.GetSubGraph(Graph, Nlist) labels = snap.TIntStrH() for NI in SubGraph.Nodes(): labels[NI.GetId()] = word_of_dict[NI.GetId()] snap.SaveGViz(SubGraph, '..\\data\\subgraph\\subgraph{}.dot'.format(year), "三农中的科技词图{}".format(year), labels) alter( "..\\data\\subgraph\\subgraph{}.dot".format(year), 'shape=ellipse, width=0.3, height=0.3', 'shape=ellipse, width=0.3, height=0.3, fontname="SimSun" size=\"7,7\"' ) for i in NIdV: alter2("..\\data\\subgraph\\subgraph{}.dot".format(year), i, "#65934A") for i in NNIdV: alter2("..\\data\\subgraph\\subgraph{}.dot".format(year), i, "#A0BF7C") alter2("..\\data\\subgraph\\subgraph{}.dot".format(year), v, "#407434")