def getBasicInfo(strPath, net): G = snap.LoadEdgeList(snap.PUNGraph,strPath,0,1) GraphInfo = {} GraphInfo['nodes'] = G.GetNodes() GraphInfo['edges'] = G.GetEdges() GraphInfo['zeroDegNodes'] = snap.CntDegNodes(G, 0) GraphInfo['zeroInDegNodes'] = snap.CntInDegNodes(G, 0) GraphInfo['zeroOutDegNodes'] = snap.CntOutDegNodes(G, 0) GraphInfo['nonZeroIn-OutDegNodes'] = snap.CntNonZNodes(G) GraphInfo['uniqueDirectedEdges'] = snap.CntUniqDirEdges(G) GraphInfo['uniqueUndirectedEdges'] = snap.CntUniqUndirEdges(G) GraphInfo['selfEdges'] = snap.CntSelfEdges(G) GraphInfo['biDirEdges'] = snap.CntUniqBiDirEdges(G) NTestNodes = 10 IsDir = False GraphInfo['approxFullDiameter'] = snap.GetBfsEffDiam(G, NTestNodes, IsDir) GraphInfo['90effectiveDiameter'] = snap.GetAnfEffDiam(G) DegToCntV = snap.TIntPrV() snap.GetDegCnt(G, DegToCntV) sumofNode = G.GetNodes() L = [item.GetVal1()*item.GetVal2() for item in DegToCntV] GraphInfo['averageDegree'] = float(sum(L))/(sumofNode) (DegreeCountMax ,Degree, DegreeCount, CluDegree, Clu) = getGraphInfo(G) # creatNet(G,net) return GraphInfo,DegreeCountMax , Degree, DegreeCount, CluDegree, Clu
def directed_get_densification(df): years = sorted(df['year'].unique()) out_num_nodes = [] out_num_edges = [] out_anf_diameters = [] G = snap.TNEANet.New() for year in years: add_df_to_G(df[df['year'] == year], G, directed=True) out_num_nodes.append(G.GetNodes()) out_num_edges.append(G.GetEdges()) out_anf_diameters.append(snap.GetAnfEffDiam(G)) return out_num_nodes, out_num_edges, out_anf_diameters, years
def cumulative_get_densification(df): years = sorted(df['year'].unique()) out_num_nodes = [] out_num_edges = [] out_anf_diameters = [] G = snap.TUNGraph.New() for year in years: add_df_to_G(df[df['year'] == year], G) out_num_nodes.append(G.GetNodes()) out_num_edges.append(G.GetEdges()) out_anf_diameters.append(snap.GetAnfEffDiam(G)) return out_num_nodes, out_num_edges, out_anf_diameters, years
def get_densification(df): years = sorted(df['year'].unique()) out_num_nodes = [] out_num_edges = [] # out_bfs_diameters = [] out_anf_diameters = [] for year in years: G = get_graph(df[df['year'] == year]) out_num_nodes.append(G.GetNodes()) out_num_edges.append(G.GetEdges()) scc = snap.GetMxScc(G) out_anf_diameters.append(snap.GetAnfEffDiam(scc)) return out_num_nodes, out_num_edges, out_anf_diameters, years
def approx_neighborhood_function_statistics(G, n_nodes, n_approx=64, approx_type=APPROX_BFS_IGRAPH): aG = convert_networkx_to_SNAP(G) print "convert to SNAP graph: DONE" # TEST (fixed) # s_Diam = 20 # youtube # diameter s_Diam (lowerbound) start = time.clock() if approx_type == APPROX_ANF: s_Diam = sn.GetAnfEffDiam(aG, False, 0.99, n_approx) elif approx_type == APPROX_BFS: s_Diam = sn.GetBfsFullDiam(aG, N_BFS, False) # elif approx_type == APPROX_BFS_IGRAPH: s_APD_i, s_EDiam_i, s_CL_i, s_Diam = bfs_samples(G) # _i: igraph else: print "Wrong <approx_type> !" print "compute s_Diam, elapsed :", time.clock() - start # average distance s_APD DistNbrsV = sn.TIntFltKdV() MxDist = int(math.ceil(s_Diam)) print "MxDist =", MxDist start = time.clock() sn.GetAnf(aG, DistNbrsV, MxDist, False, n_approx) # n_approx=32, 64... print "GetAnf, elapsed :", time.clock() - start # for item in DistNbrsV: # print item.Key(), "-", item.Dat() sum_APD = 0.0 dist_list = [] # list of pairs for item in DistNbrsV: dist_list.append([item.Key(), item.Dat()]) num_APD = dist_list[-1][1] # WAY 2 - compute s_EDiam from dist_list s_EDiam = 0 for i in range(len(dist_list)): if dist_list[i][1] >= 0.9 * num_APD: s_EDiam = dist_list[i][0] break for i in range(len(dist_list) - 1, 1, -1): # do not subtract [0] from [1] ! dist_list[i][1] = dist_list[i][1] - dist_list[i - 1][ 1] # compute differences sum_APD += dist_list[i][0] * dist_list[i][1] s_APD = sum_APD / num_APD print "num_APD =", num_APD # for s_PDD print "s_PDD :" d_list = [] for item in dist_list: # print item[0], "-", item[1] d_list.append(item[1]) print d_list # WAY 1 - effective diameter s_EDiam ( SNAP) # start = time.clock() # if approx_type == APPROX_ANF: # s_EDiam = sn.GetAnfEffDiam(aG, False, 0.9, n_approx) # 90% # elif approx_type == APPROX_BFS: # s_EDiam = sn.GetBfsEffDiam(aG, 1000, False) # else: # print "Wrong <approx_type> !" # print "compute s_EDiam, elapsed :", time.clock() - start # connectivity length s_CL sum_CL = 0.0 for item in dist_list: if item[0] > 0: sum_CL += item[1] / item[0] # s_CL = n_nodes*(n_nodes-1)/sum_CL s_CL = num_APD / sum_CL # return s_APD, float(s_EDiam), s_CL, float(s_Diam), s_APD_i, float( s_EDiam_i), s_CL_i, dist_list
diameter = [] inter_edges = [] intra_edges = [] inter_intra = [] disassort_degree = [] disassort_cluster = [] edge_connectivity = [] avg_path_length = [] robustness = [] for j in range(samp): g1 = read("uniform_noise_railway_perm" + str(i) + str(j) + ".graphml").as_undirected() clustering.append(g1.transitivity_undirected()) g1.write("temp.txt", format="edgelist") G = snap.LoadEdgeList(snap.PUNGraph, "temp.txt", 0, 1) effective_dia.append(snap.GetAnfEffDiam(G)) os.system("rm temp.txt") diameter.append(g1.diameter()) disassort_degree.append(g1.assortativity_degree()) avg_path_length.append(g1.average_path_length()) original_d = inverse_distance(g1) robustness.append(original_d / d_o) edge_connectivity.append(g1.edge_connectivity()) inter_eg = 0.0 intra_eg = 0.0 ii = modularity(g1) inter_edges.append(1.0 * ii[1]) intra_edges.append(1.0 * ii[0]) inter_intra.append(1.0 * ii[1] / ii[0]) out.write(