def RankEigvecDist(graph): """ Calculates the degree - clustering coefficient distribution of a given graph. The graph must be in snap format. ... Parameters ---------- graph : an instance of SNAP.TUNGraph()/SNAP.TNGraph() for undirected/directed graph The graph for which the degree distribution is to be calculated Returns ------- eigvec_dist : 2D numpy array with shape (2, :) The calculated eigenvector distribution for graph. Here, the eigenvector refers to the principal eigenvector, which should be positive definite due to Perrin-Frobenius theorem. eigvec_dist[0, :] : rank of the eigenvector eigvec_dist[1, :] : value of the element of the first eigenvector, sorted in the order of descending value. """ EigVec = snap.TFltV() snap.GetEigVec(graph,EigVec) eigvec = np.abs(np.array(EigVec)) N = len(eigvec) eigvec.reshape((1, N)) rank = np.arange(N).reshape((1, N)) eigvec = eigvec[np.argsort(-eigvec)] eigvec_dist = np.vstack([rank, eigvec]) return eigvec_dist
def TopKRankEigvalDist(graph, K): N = graph.GetNodes() assert K<=N, "K must be smaller than the number of nodes in the graph" flag = 0 N_try = min(2*K, N) # The code is written in this convoluted way, because for some unknown reasons regarding the snap library, the function snap.GetEigVals does not properly return the same number of the top eigenvalues as requested while flag < K: EigVal = snap.TFltV() snap.GetEigVals(graph, N_try, EigVal) eig = np.array(EigVal) flag = len(eig) N_try = min(2*N_try, N) eig = np.abs(eig) return np.vstack([np.arange(K).reshape((1, K)), eig[:K].reshape((1, K))])
def getNodeFeatures(graph, node): # The snap vectors that will house the raw data intVals = snap.TIntV() intNames = snap.TStrV() fltVals = snap.TFltV() fltNames = snap.TStrV() strVals = snap.TStrV() strNames = snap.TStrV() # Load the raw data from the node graph.IntAttrValueNI(node, intVals) graph.IntAttrNameNI(node, intNames) graph.FltAttrValueNI(node, fltVals) graph.FltAttrNameNI(node, fltNames) graph.StrAttrValueNI(node, strVals) graph.StrAttrNameNI(node, strNames) # Put the data into a dictionary to be returned return _aggregate_vecs(intNames, intVals, fltNames, fltVals, strNames, strVals)
def get_basic_feature(graph, node_id): NI = graph.GetNI(node_id) v_1 = NI.GetDeg() nbrs = NI.GetOutEdges() nbr_vec = snap.TIntV() nbrs = [nbr_vec.Add(nbr) for nbr in nbrs] nbr_vec.Add(node_id) subgraph = snap.GetSubGraph(graph, nbr_vec) v_2 = subgraph.GetEdges() total_edges = 0 for node in subgraph.Nodes(): orig_NI = graph.GetNI(node.GetId()) total_edges += orig_NI.GetDeg() v_3 = total_edges - 2 * v_2 feature = snap.TFltV() feature.Add(v_1) feature.Add(v_2) feature.Add(v_3) return feature
def run_rolx_iteration(graph, features): new_features = snap.TIntFltVH() iterator = features.BegI() while not iterator.IsEnd(): node_id = iterator.GetKey() nodeI = graph.GetNI(node_id) node_nbrs = [nbr for nbr in nodeI.GetOutEdges()] sum_aggregate = np.zeros(features[0].Len()) num_nbrs = len(node_nbrs) for node_nbr in node_nbrs: sum_aggregate += np.asarray(features[node_nbr], dtype=np.float32) new_feature = snap.TFltV(features[node_id]) if num_nbrs == 0: mean_aggregate = np.zeros(features[0].Len()) sum_aggregate = np.zeros(features[0].Len()) else: mean_aggregate = sum_aggregate / len(node_nbrs) for i in range(mean_aggregate.size): new_feature.Add(mean_aggregate[i]) for i in range(sum_aggregate.size): new_feature.Add(sum_aggregate[i]) new_features[node_id] = new_feature iterator.Next() return new_features
CntV = snap.TIntPrV() snap.GetWccSzCnt(G9, CntV) for p in CntV: print "size %d: count %d" % (p.GetVal1(), p.GetVal2()) # get degree distribution pairs (out-degree, count): snap.GetOutDegCnt(G9, CntV) for p in CntV: print "degree %d: count %d" % (p.GetVal1(), p.GetVal2()) # generate a Preferential Attachment graph on 100 nodes and out-degree of 3 G10 = snap.GenPrefAttach(100, 3) print "G10: Nodes %d, Edges %d" % (G10.GetNodes(), G10.GetEdges()) # define a vector of floats and get first eigenvector of graph adjacency matrix EigV = snap.TFltV() snap.GetEigVec(G10, EigV) nr = 0 for f in EigV: nr += 1 print "%d: %.6f" % (nr, f) # get an approximation of graph diameter diam = snap.GetBfsFullDiam(G10, 10) print "diam", diam # count the number of triads: triads = snap.GetTriads(G10) print "triads", triads # get the clustering coefficient
def intro(): # create a graph PNGraph G1 = snap.TNGraph.New() G1.AddNode(1) G1.AddNode(5) G1.AddNode(32) G1.AddEdge(1, 5) G1.AddEdge(5, 1) G1.AddEdge(5, 32) print("G1: Nodes %d, Edges %d" % (G1.GetNodes(), G1.GetEdges())) # create a directed random graph on 100 nodes and 1k edges G2 = snap.GenRndGnm(snap.PNGraph, 100, 1000) print("G2: Nodes %d, Edges %d" % (G2.GetNodes(), G2.GetEdges())) # traverse the nodes for NI in G2.Nodes(): print("node id %d with out-degree %d and in-degree %d" % (NI.GetId(), NI.GetOutDeg(), NI.GetInDeg())) # traverse the edges for EI in G2.Edges(): print("edge (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId())) # traverse the edges by nodes for NI in G2.Nodes(): for Id in NI.GetOutEdges(): print("edge (%d %d)" % (NI.GetId(), Id)) # generate a network using Forest Fire model G3 = snap.GenForestFire(1000, 0.35, 0.35) print("G3: Nodes %d, Edges %d" % (G3.GetNodes(), G3.GetEdges())) # save and load binary FOut = snap.TFOut("test.graph") G3.Save(FOut) FOut.Flush() FIn = snap.TFIn("test.graph") G4 = snap.TNGraph.Load(FIn) print("G4: Nodes %d, Edges %d" % (G4.GetNodes(), G4.GetEdges())) # save and load from a text file snap.SaveEdgeList(G4, "test.txt", "Save as tab-separated list of edges") G5 = snap.LoadEdgeList(snap.PNGraph, "test.txt", 0, 1) print("G5: Nodes %d, Edges %d" % (G5.GetNodes(), G5.GetEdges())) # generate a network using Forest Fire model G6 = snap.GenForestFire(1000, 0.35, 0.35) print("G6: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges())) # convert to undirected graph G7 = snap.ConvertGraph(snap.PUNGraph, G6) print("G7: Nodes %d, Edges %d" % (G7.GetNodes(), G7.GetEdges())) # get largest weakly connected component of G WccG = snap.GetMxWcc(G6) # get a subgraph induced on nodes {0,1,2,3,4,5} SubG = snap.GetSubGraph(G6, snap.TIntV.GetV(0, 1, 2, 3, 4)) # get 3-core of G Core3 = snap.GetKCore(G6, 3) # delete nodes of out degree 10 and in degree 5 snap.DelDegKNodes(G6, 10, 5) print("G6a: Nodes %d, Edges %d" % (G6.GetNodes(), G6.GetEdges())) # generate a Preferential Attachment graph on 1000 nodes and node out degree of 3 G8 = snap.GenPrefAttach(1000, 3) print("G8: Nodes %d, Edges %d" % (G8.GetNodes(), G8.GetEdges())) # vector of pairs of integers (size, count) CntV = snap.TIntPrV() # get distribution of connected components (component size, count) snap.GetWccSzCnt(G8, CntV) # get degree distribution pairs (degree, count) snap.GetOutDegCnt(G8, CntV) # vector of floats EigV = snap.TFltV() # get first eigenvector of graph adjacency matrix snap.GetEigVec(G8, EigV) # get diameter of G8 snap.GetBfsFullDiam(G8, 100) # count the number of triads in G8, get the clustering coefficient of G8 snap.GetTriads(G8) snap.GetClustCf(G8)
def ManipulateAttributesIter(): ''' Test node, edge attribute functionality using iterators ''' NNodes = 1000 NEdges = 1000 Graph = snap.TNEANet.New() t = Graph.Empty() # create the nodes for i in range(0, NNodes): Graph.AddNode(i) t = Graph.Empty() n = Graph.GetNodes() # create random edges NCount = NEdges while NCount > 0: x = int(random.random() * NNodes) y = int(random.random() * NNodes) # skip the loops in this test if x != y and not Graph.IsEdge(x,y): n = Graph.AddEdge(x, y) NCount -= 1 print("Added nodes") # create attributes and fill all nodes #attr1 = TStr("str") #attr2 = TStr("int") #attr3 = TStr("float") #attr4 = TStr("default") attr1 = "STR" attr2 = "INT" attr3 = "FLOAT" attr4 = "DEFAULT" # Test column int iterator for node 3, 50, 700, 900 # Check if we can set defaults to 0 fordata. Graph.AddIntAttrN(attr2, 0) NI3 = Graph.GetNI(3) NI50 = Graph.GetNI(50) NI700 = Graph.GetNI(700) NI900 = Graph.GetNI(900) Graph.AddIntAttrDatN(NI3, 3*2, attr2) Graph.AddIntAttrDatN(NI50, 50*2, attr2) Graph.AddIntAttrDatN(NI700, 700*2, attr2) Graph.AddIntAttrDatN(NI900, 900*2, attr2) print("Added attributes") NodeId = 0 NI = Graph.BegNAIntI(attr2) while NI < Graph.EndNAIntI(attr2): if NI.GetDat() != 0: print("Attribute1: %s, Node: %i, Val: %d" % (attr2, NodeId, NI.GetDat())) #print("Attribute: %s, Node: %i, Val: %d" % (attr2(), NodeId, NI.GetDat())) NodeId += 1 NI.Next() # Test column flt iterator for node 3, 50, 700, 900 NI5 = Graph.GetNI(5) NI50 = Graph.GetNI(50) NI300 = Graph.GetNI(300) NI653 = Graph.GetNI(653) Graph.AddFltAttrDatN(NI5, 3.41, attr3) Graph.AddFltAttrDatN(NI50, 2.718, attr3) Graph.AddFltAttrDatN(NI300, 150.0, attr3) Graph.AddFltAttrDatN(NI653, 653, attr3) NodeId = 0 NCount = 0 NI = Graph.BegNI() while NI < Graph.EndNI(): NCount += 1 NI.Next() NI = Graph.BegNAFltI(attr3) NodeId = 0 while NI < Graph.EndNAFltI(attr3): if NI.GetDat() != snap.TFlt.Mn: print("Attribute2: %s, Node: %i, Val: %f" % (attr3, NodeId, NI.GetDat())) #print("Attribute: %s, Node: %i, Val: %f" % (attr3(), NodeId, NI.GetDat())) NodeId += 1 NI.Next() # Test column str iterator for node 3, 50, 700, 900 NI10 = Graph.GetNI(10) NI20 = Graph.GetNI(20) NI400 = Graph.GetNI(400) #Graph.AddStrAttrDatN(10, TStr("abc"), attr1) #Graph.AddStrAttrDatN(20, TStr("def"), attr1) #Graph.AddStrAttrDatN(400, TStr("ghi"), attr1) Graph.AddStrAttrDatN(NI10, "abc", attr1) Graph.AddStrAttrDatN(NI20, "def", attr1) Graph.AddStrAttrDatN(NI400, "ghi", attr1) # this does not show since ""=null #Graph.AddStrAttrDatN(455, TStr(""), attr1) # TODO Graph.AddStrAttrDatN(455, "", attr1) NodeId = 0 NI = Graph.BegNAStrI(attr1) NodeId = 0 while NI < Graph.EndNAStrI(attr1): if NI.GetDat() != snap.TStr.GetNullStr(): print("Attribute3: %s, Node: %i, Val: %s" % (attr1, NodeId, NI.GetDat())) #print("Attribute: %s, Node: %i, Val: %s" % (attr1(), NodeId, NI.GetDat())) NodeId += 1 NI.Next() # Test column iterator over many types (must skip default/deleted attr) NId = 55 NI55 = Graph.GetNI(55) NI80 = Graph.GetNI(80) #Graph.AddStrAttrDatN(NId, TStr("aaa"), attr1) Graph.AddStrAttrDatN(NI55, "aaa", attr1) Graph.AddIntAttrDatN(NI55, 3*2, attr2) Graph.AddFltAttrDatN(NI55, 3.41, attr3) #Graph.AddStrAttrDatN(80, TStr("dont appear"), attr4) # should not show up Graph.AddStrAttrDatN(NI80, "dont appear", attr4) # should not show up attr1idx = Graph.GetAttrIndN(attr1) attr2idx = Graph.GetAttrIndN(attr2) attr3idx = Graph.GetAttrIndN(attr3) attr4idx = Graph.GetAttrIndN(attr4) print("Node attribute indexes: %s %d, %s %d, %s %d, %s %d" % ( attr1, attr1idx, attr2, attr2idx, attr3, attr3idx, attr4, attr4idx)) NI = Graph.GetNI(NId) print("NI attributes: %i, %s %d %.2f" % ( NI.GetId(), Graph.GetStrAttrDatN(NI, attr1), Graph.GetIntAttrDatN(NI, attr2), Graph.GetFltAttrDatN(NI, attr3))) print("ind attributes: %i, %s %d %.2f" % ( NI.GetId(), Graph.GetStrAttrIndDatN(NI, attr1idx), Graph.GetIntAttrIndDatN(NI, attr2idx), Graph.GetFltAttrIndDatN(NI, attr3idx))) NIdAttrName = snap.TStrV() NIdAttrValue = snap.TStrV() NIdIntAttrValue = snap.TIntV() NIdFltAttrValue = snap.TFltV() NIdStrAttrValue = snap.TStrV() Graph.AttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() for i in range(AttrLen): print("Vertical Node1: %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())) NIdAttrName = snap.TStrV() Graph.IntAttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() for i in range(AttrLen): print("Vertical Node11 (int): %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())) NIdAttrName = snap.TStrV() Graph.FltAttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() for i in range(AttrLen): print("Vertical Node12 (flt): %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())) NIdAttrName = snap.TStrV() Graph.StrAttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() for i in range(AttrLen): print("Vertical Node13 (str): %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())) Graph.IntAttrValueNI(NId, NIdIntAttrValue) AttrLen = NIdIntAttrValue.Len() for i in range(AttrLen): print("Vertical Node14 (int): %i, Attr_Val: %d" % (NId, NIdIntAttrValue.GetI(i)())) Graph.FltAttrValueNI(NId, NIdFltAttrValue) AttrLen = NIdFltAttrValue.Len() for i in range(AttrLen): print("Vertical Node15 (flt): %i, Attr_Val: %.2f" % (NId, NIdFltAttrValue.GetI(i)())) Graph.StrAttrValueNI(NId, NIdStrAttrValue) AttrLen = NIdStrAttrValue.Len() for i in range(AttrLen): print("Vertical Node16 (str): %i, Attr_Val: %s" % (NId, NIdStrAttrValue.GetI(i)())) Graph.DelAttrDatN(NId, attr2) Graph.AttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() for i in range(AttrLen): print("Vertical Node2 (no int) : %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())) Graph.AddIntAttrDatN(NId, 3*2, attr2) Graph.DelAttrN(attr1) Graph.AttrNameNI(NId, NIdAttrName) AttrLen = NIdAttrName.Len() for i in range(AttrLen): print("Vertical Node3 (no str) : %i, Attr: %s" % (NId, NIdAttrName.GetI(i)())) Graph.AttrValueNI(NId, NIdAttrValue) AttrLen = NIdAttrValue.Len() for i in range(AttrLen): print("Vertical Node4 (no str) : %i, Attr_Val: %s" % (NId, NIdAttrValue.GetI(i)())) for i in range(NNodes): Graph.AddIntAttrDatN(i, 70, attr2) total = 0 NI = Graph.BegNAIntI(attr2) while NI < Graph.EndNAIntI(attr2): total += NI.GetDat() NI.Next() print("Average: %i (should be 70)" % (total/NNodes)) if total/NNodes != 70: print("*** Error3") # Test column iterator for edge EI3 = Graph.GetEI(3) EI55 = Graph.GetEI(55) EI705 = Graph.GetEI(705) EI905 = Graph.GetEI(905) Graph.AddIntAttrDatE(EI3, 3*2, attr2) Graph.AddIntAttrDatE(EI55, 55*2, attr2) Graph.AddIntAttrDatE(EI705, 705*2, attr2) Graph.AddIntAttrDatE(EI905, 905*2, attr2) EdgeId = 0 EI = Graph.BegEAIntI(attr2) while EI < Graph.EndEAIntI(attr2): if EI.GetDat() != snap.TInt.Mn: print("E Attribute1: %s, Edge: %i, Val: %i" % ( attr2, EdgeId, EI.GetDat())) #% (attr2(), EdgeId, EI.GetDat()) EdgeId += 1 EI.Next() # Test column flt iterator for edge Graph.AddFltAttrE(attr3, 0.00) EI5 = Graph.GetEI(5) EI50 = Graph.GetEI(50) EI300 = Graph.GetEI(300) EI653 = Graph.GetEI(653) Graph.AddFltAttrDatE(EI5, 4.41, attr3) Graph.AddFltAttrDatE(EI50, 3.718, attr3) Graph.AddFltAttrDatE(EI300, 151.0, attr3) Graph.AddFltAttrDatE(EI653, 654, attr3) EdgeId = 0 EI = Graph.BegEAFltI(attr3) while EI < Graph.EndEAFltI(attr3): # Check if defaults are set to 0. if EI.GetDat() != 0: print("E Attribute2: %s, Edge: %i, Val: %f" % ( attr3, EdgeId, EI.GetDat())) #(attr3(), EdgeId, EI.GetDat()) EdgeId += 1 EI.Next() # Test column str iterator for edge #Graph.AddStrAttrDatE(10, TStr("abc"), attr1) #Graph.AddStrAttrDatE(20, TStr("def"), attr1) #Graph.AddStrAttrDatE(400, TStr("ghi"), attr1) EI10 = Graph.GetEI(10) EI20 = Graph.GetEI(20) EI400 = Graph.GetEI(400) Graph.AddStrAttrDatE(EI10, "abc", attr1) Graph.AddStrAttrDatE(EI20, "def", attr1) Graph.AddStrAttrDatE(EI400, "ghi", attr1) # this does not show since ""=null #Graph.AddStrAttrDatE(455, TStr(""), attr1) # TODO Graph.AddStrAttrDatE(455, "", attr1) EdgeId = 0 EI = Graph.BegEAStrI(attr1) while EI < Graph.EndEAStrI(attr1): if EI.GetDat() != snap.TStr.GetNullStr(): print("E Attribute3: %s, Edge: %i, Val: %s" % ( attr1, EdgeId, EI.GetDat())) #(attr1(), EdgeId, EI.GetDat()) EdgeId += 1 EI.Next() # Test column iterator over many types (must skip default/deleted attr) EId = 55 EI55 = Graph.GetEI(55) EI80 = Graph.GetEI(80) #Graph.AddStrAttrDatE(EId, TStr("aaa"), attr1) Graph.AddStrAttrDatE(EI55, "aaa", attr1) Graph.AddIntAttrDatE(EI55, 3*2, attr2) Graph.AddFltAttrDatE(EI55, 3.41, attr3) #Graph.AddStrAttrDatE(80, TStr("dont appear"), attr4) # should not show up Graph.AddStrAttrDatE(EI80, "dont appear", attr4) # should not show up attr1idx = Graph.GetAttrIndE(attr1) attr2idx = Graph.GetAttrIndE(attr2) attr3idx = Graph.GetAttrIndE(attr3) attr4idx = Graph.GetAttrIndE(attr4) print("Edge attribute indexes: %s %d, %s %d, %s %d, %s %d" % ( attr1, attr1idx, attr2, attr2idx, attr3, attr3idx, attr4, attr4idx)) EI = Graph.GetEI(EId) print("EI attributes: %i, %s %d %.2f" % ( EI.GetId(), Graph.GetStrAttrDatE(EI, attr1), Graph.GetIntAttrDatE(EI, attr2), Graph.GetFltAttrDatE(EI, attr3))) print("ind attributes: %i, %s %d %.2f" % ( EI.GetId(), Graph.GetStrAttrIndDatE(EI, attr1idx), Graph.GetIntAttrIndDatE(EI, attr2idx), Graph.GetFltAttrIndDatE(EI, attr3idx))) EIdAttrName = snap.TStrV() EIdAttrValue = snap.TStrV() Graph.AttrNameEI(EId, EIdAttrName) AttrLen = EIdAttrName.Len() for i in range(AttrLen): print("Vertical Edge1: %i, Attr: %s" % (EId, EIdAttrName.GetI(i)())) Graph.DelAttrDatE(EId, attr2) Graph.AttrNameEI(EId, EIdAttrName) AttrLen = EIdAttrName.Len() for i in range(AttrLen): print("Vertical Edge2 (no int) : %i, Attr: %s" % (EId, EIdAttrName.GetI(i)())) Graph.AddIntAttrDatE(EId, 3*2, attr2) Graph.DelAttrE(attr1) Graph.AttrNameEI(EId, EIdAttrName) AttrLen = EIdAttrName.Len() for i in range(AttrLen): print("Vertical Edge3 (no str) : %i, Attr: %s" % (EId, EIdAttrName.GetI(i)())) Graph.AttrValueEI(EId, EIdAttrValue) AttrLen = EIdAttrValue.Len() for i in range(AttrLen): print("Vertical Edge4 (no str) : %i, Attr_Val: %s" % (EId, EIdAttrValue.GetI(i)())) for i in range(NEdges): Graph.AddIntAttrDatE(i, 70, attr2) total = 0 EI = Graph.BegNAIntI(attr2) while EI < Graph.EndNAIntI(attr2): total += EI.GetDat() EI.Next() print("Average: %i (should be 70)" % (total/NEdges)) if total/NNodes != 70: print("*** Error4") Graph.Clr()
def run_model_and_save(sv, nv, cv, nodes, avgdeg, taa, tbb, iterations, run_name, folder): """ sv: 2d vector with s parameter groups a and b """ #TODO: Save a network after it has been simulated trnd = snap.TRnd( 0 ) #from the clock? The default value seems to mean that the random seed is always the same sa, sb = sv iterations = int(iterations) edges = int((nodes * avgdeg) / 2.) sizes = snap.TFltV() asize = int(nv * nodes) bsize = nodes - asize sizes.Add(asize) sizes.Add(bsize) try: network = nc.create_network_from_t(avgdeg, sizes, taa, tbb, trnd) except ValueError: print( "Could not create network with {} and {} nodes and average degree {}" .format(sizes[0], sizes[1], avgdeg)) biasVec = snap.TFltV() biasVec.Add(sv[0]) biasVec.Add(sv[1]) Paas, Pbbs = [], [] instds, outstds = [], [] for ite in range(iterations): snap.RunBiasedTriadicModel(network, edges, cv, sizes, biasVec, trnd) modelstr = " ".join( map(str, [sv[0], sv[1], nv, cv, nodes, avgdeg, iterations, taa, tbb])) Pab = snap.GetPMatrixElement(0, 1, network, sizes) Paa = snap.GetPMatrixElement(0, 0, network, sizes) Pbb = snap.GetPMatrixElement(1, 1, network, sizes) #1 - Pab - Paa biases_a, degs_a = [], [] biases_b, degs_b = [], [] true_fraction = (asize + 0.) / (asize + bsize) def classify_node(node): if node < asize: return 1 # If in group a, it is minority else: return 0 # If in group b, it's majority #Iterate over nodes for NI in network.Nodes(): deg = 0 perc = 0 #Iterate over neighbors for Id in NI.GetOutEdges(): perc += classify_node(Id) deg += 1 if deg > 0: bias = (perc + 0.) / (deg * true_fraction) else: bias = 0. if classify_node(NI.GetId()) == 1: biases_a.append(bias) degs_a.append(deg) else: biases_b.append(bias) degs_b.append(deg) bias_a = numpy.mean(biases_a) bias_b = numpy.mean(biases_b) if not os.path.exists(folder): os.mkdir(folder) summary_name = 'summary.txt' if not os.path.exists(os.path.join(folder, summary_name)): res = 'name sa sb na cv taa tbb nodes avgdeg iter bias_a bias_b\n' save_summary(res, os.path.join(folder, summary_name)) #max_file = 0 #else: # max_file = max([int(f.split('.')[0]) for f in os.listdir(folder)]) res = ' '.join(['{}'] * 12) + '\n' path_name = os.path.join(folder, run_name + '.p') if os.path.exists(path_name): run_name += '_0' path_name = os.path.join(folder, run_name + '.p') res = res.format(run_name, sa, sb, nv, cv, taa, tbb, nodes, avgdeg, iterations, bias_a, bias_b) save_summary(res, os.path.join(folder, 'summary.txt')) print(res) dic = { 'sa': sa, 'sb': sb, 'na': nv, 'cv': cv, 'taa': taa, 'tbb': tbb, 'nodes': nodes, 'avgdeg': avgdeg, 'iterations': iterations, 'biases_a': biases_a, 'biases_b': biases_b, 'degs_a': degs_a, 'degs_b': degs_b } with open(path_name, 'wb') as w: pickle.dump(dic, w)
def svalsSNAP( graph, n_sv = 20): SngValV = snap.TFltV() snap.GetSngVals(graph, n_sv, SngValV) svArr = [ sv for sv in SngValV ] return svArr