Beispiel #1
0
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
Beispiel #2
0
def quick_properties(graph, name, dic_path):
    """Get quick properties of the graph "name". dic_path is the path of the dict {players: id} """
    n_edges = graph.GetEdges()
    n_nodes = graph.GetNodes()
    print("##########")
    print("Quick overview of {} Network".format(name))
    print("##########")
    print("{} Nodes, {} Edges").format(n_nodes, n_edges)
    print("{} Self-edges ".format(snap.CntSelfEdges(graph)))
    print("{} Directed edges, {} Undirected edges".format(
        snap.CntUniqDirEdges(graph), snap.CntUniqUndirEdges(graph)))
    print("{} Reciprocated edges".format(snap.CntUniqBiDirEdges(graph)))
    print("{} 0-out-degree nodes, {} 0-in-degree nodes".format(
        snap.CntOutDegNodes(graph, 0), snap.CntInDegNodes(graph, 0)))
    node_in = graph.GetNI(snap.GetMxInDegNId(graph))
    node_out = graph.GetNI(snap.GetMxOutDegNId(graph))
    print("Maximum node in-degree: {}, maximum node out-degree: {}".format(
        node_in.GetDeg(), node_out.GetDeg()))
    print("###")
    components = snap.TCnComV()
    snap.GetWccs(graph, components)
    max_wcc = snap.GetMxWcc(graph)
    print "{} Weakly connected components".format(components.Len())
    print "Largest Wcc: {} Nodes, {} Edges".format(max_wcc.GetNodes(),
                                                   max_wcc.GetEdges())
    prankH = snap.TIntFltH()
    snap.GetPageRank(graph, prankH)
    sorted_prankH = sorted(prankH, key=lambda key: prankH[key], reverse=True)
    NIdHubH = snap.TIntFltH()
    NIdAuthH = snap.TIntFltH()
    snap.GetHits(graph, NIdHubH, NIdAuthH)
    sorted_NIdHubH = sorted(NIdHubH,
                            key=lambda key: NIdHubH[key],
                            reverse=True)
    sorted_NIdAuthH = sorted(NIdAuthH,
                             key=lambda key: NIdAuthH[key],
                             reverse=True)
    with open(dic_path, 'rb') as dic_id:
        mydict = pickle.load(dic_id)
        print("3 most central players by PageRank scores: {}, {}, {}".format(
            list(mydict.keys())[list(mydict.values()).index(sorted_prankH[0])],
            list(mydict.keys())[list(mydict.values()).index(sorted_prankH[1])],
            list(mydict.keys())[list(mydict.values()).index(
                sorted_prankH[2])]))
        print("Top 3 hubs: {}, {}, {}".format(
            list(mydict.keys())[list(mydict.values()).index(
                sorted_NIdHubH[0])],
            list(mydict.keys())[list(mydict.values()).index(
                sorted_NIdHubH[1])],
            list(mydict.keys())[list(mydict.values()).index(
                sorted_NIdHubH[2])]))
        print("Top 3 authorities: {}, {}, {}".format(
            list(mydict.keys())[list(mydict.values()).index(
                sorted_NIdAuthH[0])],
            list(mydict.keys())[list(mydict.values()).index(
                sorted_NIdAuthH[1])],
            list(mydict.keys())[list(mydict.values()).index(
                sorted_NIdAuthH[2])]))
Beispiel #3
0
Count = snap.CntOutDegNodes(graph, 0)
print "Count of nodes with out-degree 0 is %d" % Count
#no of nodes with zero in-degree
Count = snap.CntInDegNodes(graph, 0)
print "Count of nodes with in-degree 0 is %d" % Count
#no of directed edges
Count = snap.CntUniqDirEdges(graph)
print "Count of directed edges is %d" % Count
#no of undirected edges
Count = snap.CntUniqUndirEdges(graph)
print "Count of undirected edges is %d" % Count
#no of self edges
Count = snap.CntSelfEdges(graph)
print "Count of self edges is %d" % Count
#no of unique bi-directional/reciprocated edges
Count = snap.CntUniqBiDirEdges(graph)
print "Count of unique bidirectional edges is %d" % Count

#no of nodes with out-degree greater than 10
OutDegV = snap.TIntPrV()
snap.GetNodeOutDegV(graph, OutDegV)
count_od = 0
for item in OutDegV:
    if (item.GetVal2() > 10):
        count_od = count_od + 1
print "Count of nodes with more than 10 outgoing edges %d" % count_od

#no of nodes with in-degree greater than 10
InDegV = snap.TIntPrV()
snap.GetNodeInDegV(graph, InDegV)
count_in = 0
Beispiel #4
0
print snap.CntUniqDirEdges(g)
print '\n'

# cc=0;
# ug=snap.ConvertGraph(snap.PUNGraph,g);
# for e in ug.Edges():
# 	if(e.GetSrcNId()!=e.GetDstNId()):
# 		cc=cc+1;
# print cc;
# print "\n";
print snap.CntUniqUndirEdges(g)
print '\n'

# print c-cc;
# print "\n";
print snap.CntUniqBiDirEdges(g)
print '\n'

c = 0
for n in g.Nodes():
    if (n.GetOutDeg() == 0):
        c = c + 1
print c
print "\n"

c = 0
for n in g.Nodes():
    if (n.GetInDeg() == 0):
        c = c + 1
print c
print "\n"
Beispiel #5
0
def quick_properties(graph, name, dic_path):
    """Get quick properties of the graph "name". dic_path is the path of the dict {players: id} """
    results = {}
    n_edges = graph.GetEdges()
    n_nodes = graph.GetNodes()
    n_self_edges = snap.CntSelfEdges(graph)
    n_directed_edges, n_undirected_edges = snap.CntUniqDirEdges(
        graph), snap.CntUniqUndirEdges(graph)
    n_reciprocated_edges = snap.CntUniqBiDirEdges(graph)
    n_zero_out_nodes, n_zero_in_nodes = snap.CntOutDegNodes(
        graph, 0), snap.CntInDegNodes(graph, 0)
    max_node_in = graph.GetNI(snap.GetMxInDegNId(graph)).GetDeg()
    max_node_out = graph.GetNI(snap.GetMxOutDegNId(graph)).GetDeg()
    components = snap.TCnComV()
    snap.GetWccs(graph, components)
    max_wcc = snap.GetMxWcc(graph)
    results["a. Nodes"] = n_nodes
    results["b. Edges"] = n_edges
    results["c. Self-edges"] = n_self_edges
    results["d. Directed edges"] = n_directed_edges
    results["e. Undirected edges"] = n_undirected_edges
    results["f. Reciprocated edges"] = n_reciprocated_edges
    results["g. 0 out-degree nodes"] = n_zero_out_nodes
    results["h. 0 in-degree nodes"] = n_zero_in_nodes
    results["i. Maximum node out-degree"] = max_node_out
    results["j. Maximum node in-degree"] = max_node_in
    results["k. Weakly connected components"] = components.Len()
    results["l. Nodes, edges of largest WCC"] = (max_wcc.GetNodes(),
                                                 max_wcc.GetEdges())
    print("##########")
    print("Quick overview of {} Network".format(name))
    print("##########")
    print("{} Nodes, {} Edges".format(n_nodes, n_edges))
    print("{} Self-edges ".format(n_self_edges))
    print("{} Directed edges, {} Undirected edges".format(
        n_directed_edges, n_undirected_edges))
    print("{} Reciprocated edges".format(n_reciprocated_edges))
    print("{} 0-out-degree nodes, {} 0-in-degree nodes".format(
        n_zero_out_nodes, n_zero_in_nodes))
    print("Maximum node in-degree: {}, maximum node out-degree: {}".format(
        max_node_in, max_node_out))
    print("###")
    print "{} Weakly connected components".format(components.Len())
    print "Largest Wcc: {} Nodes, {} Edges".format(max_wcc.GetNodes(),
                                                   max_wcc.GetEdges())

    prankH = snap.TIntFltH()
    snap.GetPageRank(graph, prankH)
    sorted_prankH = sorted(prankH, key=lambda key: prankH[key], reverse=True)
    NIdHubH = snap.TIntFltH()
    NIdAuthH = snap.TIntFltH()
    snap.GetHits(graph, NIdHubH, NIdAuthH)
    sorted_NIdHubH = sorted(NIdHubH,
                            key=lambda key: NIdHubH[key],
                            reverse=True)
    sorted_NIdAuthH = sorted(NIdAuthH,
                             key=lambda key: NIdAuthH[key],
                             reverse=True)
    with open(dic_path, 'rb') as dic_id:
        mydict = pickle.load(dic_id)
        print("3 most central players by PageRank scores: {}, {}, {}".format(
            name_from_index(sorted_prankH, 0, mydict),
            name_from_index(sorted_prankH, 1, mydict),
            name_from_index(sorted_prankH, 2, mydict)))
        print("Top 3 hubs: {}, {}, {}".format(
            name_from_index(sorted_NIdHubH, 0, mydict),
            name_from_index(sorted_NIdHubH, 1, mydict),
            name_from_index(sorted_NIdHubH, 2, mydict)))
        print("Top 3 authorities: {}, {}, {}".format(
            name_from_index(sorted_NIdAuthH, 0, mydict),
            name_from_index(sorted_NIdAuthH, 1, mydict),
            name_from_index(sorted_NIdAuthH, 2, mydict)))
        results["m. Three top PageRank"] = (name_from_index(
            sorted_prankH, 0, mydict), name_from_index(
                sorted_prankH, 1,
                mydict), name_from_index(sorted_prankH, 2, mydict))
        results["n. Three top hubs"] = (name_from_index(
            sorted_NIdHubH, 0,
            mydict), name_from_index(sorted_NIdHubH, 1, mydict),
                                        name_from_index(
                                            sorted_NIdHubH, 2, mydict))
        results["o. Three top authorities"] = (name_from_index(
            sorted_NIdAuthH, 0,
            mydict), name_from_index(sorted_NIdAuthH, 1, mydict),
                                               name_from_index(
                                                   sorted_NIdAuthH, 2, mydict))
    return results
def main():

    parentDir = os.getcwd()
    os.chdir(parentDir + "/subgraphs")
    sub_graph = snap.LoadEdgeList(snap.PUNGraph, sys.argv[1], 0, 1)
    subGraphName = sys.argv[1].split(".")[0]
    os.chdir(parentDir)

    #### 1 ########
    node_count = 0
    for node in sub_graph.Nodes():
        node_count = node_count + 1

    printWithOutNewLine("Number of nodes:", node_count)
    printWithOutNewLine("Number of edges:", snap.CntUniqBiDirEdges(sub_graph))

    #### 2 ########
    printWithOutNewLine("Number of nodes with degree=7:",
                        snap.CntDegNodes(sub_graph, 7))

    rndMaxDegNId = snap.GetMxDegNId(sub_graph)
    nodeDegPairs = snap.TIntPrV()
    snap.GetNodeInDegV(sub_graph, nodeDegPairs)
    maxDegVal = 0

    for pair in nodeDegPairs:
        if (pair.GetVal1() == rndMaxDegNId):
            maxDegVal = pair.GetVal2()
            break

    maxDegNodes = []
    for pair in nodeDegPairs:
        if (pair.GetVal2() == maxDegVal):
            maxDegNodes.append(pair.GetVal1())

    print("Node id(s) with highest degree:", end=" ")
    print(*maxDegNodes, sep=',')

    #### 3 ########
    sampledFullDiam = []
    sampledFullDiam.append(snap.GetBfsFullDiam(sub_graph, 10, False))
    sampledFullDiam.append(snap.GetBfsFullDiam(sub_graph, 100, False))
    sampledFullDiam.append(snap.GetBfsFullDiam(sub_graph, 1000, False))

    sampledFullDiamStats = []
    sampledFullDiamStats.append(round(statistics.mean(sampledFullDiam), 4))
    sampledFullDiamStats.append(round(statistics.variance(sampledFullDiam), 4))

    printWithOutNewLine("Approximate full diameter by sampling 10 nodes:",
                        sampledFullDiam[0])
    printWithOutNewLine("Approximate full diameter by sampling 100 nodes:",
                        sampledFullDiam[1])
    printWithOutNewLine("Approximate full diameter by sampling 1000 nodes:",
                        sampledFullDiam[2])
    print("Approximate full diameter (mean and variance):", end=" ")
    print(*sampledFullDiamStats, sep=',')

    sampledEffDiam = []
    sampledEffDiam.append(round(snap.GetBfsEffDiam(sub_graph, 10, False), 4))
    sampledEffDiam.append(round(snap.GetBfsEffDiam(sub_graph, 100, False), 4))
    sampledEffDiam.append(round(snap.GetBfsEffDiam(sub_graph, 1000, False), 4))

    sampledEffDiamStats = []
    sampledEffDiamStats.append(round(statistics.mean(sampledEffDiam), 4))
    sampledEffDiamStats.append(round(statistics.variance(sampledEffDiam), 4))

    printWithOutNewLine("Approximate effective diameter by sampling 10 nodes:",
                        sampledEffDiam[0])
    printWithOutNewLine(
        "Approximate effective diameter by sampling 100 nodes:",
        sampledEffDiam[1])
    printWithOutNewLine(
        "Approximate effective diameter by sampling 1000 nodes:",
        sampledEffDiam[2])
    print("Approximate effective diameter (mean and variance):", end=" ")
    print(*sampledEffDiamStats, sep=',')

    #### 4 ########
    printWithOutNewLine("Fraction of nodes in largest connected component:",
                        round(snap.GetMxSccSz(sub_graph), 4))

    bridgeEdges = snap.TIntPrV()
    snap.GetEdgeBridges(sub_graph, bridgeEdges)
    printWithOutNewLine("Number of edge bridges:", len(bridgeEdges))

    articulationPoints = snap.TIntV()
    snap.GetArtPoints(sub_graph, articulationPoints)
    printWithOutNewLine("Number of articulation points:",
                        len(articulationPoints))

    #### 5 ########
    printWithOutNewLine("Average clustering coefficient:",
                        round(snap.GetClustCf(sub_graph, -1), 4))

    printWithOutNewLine("Number of triads:", snap.GetTriads(sub_graph, -1))

    randomNodeId = sub_graph.GetRndNId()
    nodeIdCcfMap = snap.TIntFltH()
    snap.GetNodeClustCf(sub_graph, nodeIdCcfMap)

    print("Clustering coefficient of random node", end=" ")
    print(randomNodeId, end=": ")
    print(round(nodeIdCcfMap[randomNodeId], 4))

    print("Number of triads random node", end=" ")
    print(randomNodeId, end=" participates: ")
    print(snap.GetNodeTriads(sub_graph, randomNodeId))

    printWithOutNewLine(
        "Number of edges that participate in at least one triad:",
        snap.GetTriadEdges(sub_graph, -1))

    #### plots ########
    if not os.path.isdir('plots'):
        os.makedirs('plots')

    os.chdir(parentDir + "/plots")
    plotsDir = os.getcwd()

    snap.PlotOutDegDistr(sub_graph, subGraphName,
                         subGraphName + " Subgraph Degree Distribution")
    snap.PlotShortPathDistr(
        sub_graph, subGraphName,
        subGraphName + " Subgraph Shortest Path Lengths Distribution")
    snap.PlotSccDistr(
        sub_graph, subGraphName,
        subGraphName + " Subgraph Connected Components Size Distribution")
    snap.PlotClustCf(
        sub_graph, subGraphName,
        subGraphName + " Subgraph Clustering Coefficient Distribution")

    files = os.listdir(plotsDir)

    for file in files:
        if not file.endswith(".png"):
            os.remove(os.path.join(plotsDir, file))

    plots = os.listdir(plotsDir)
    filePrefix = "filename"
    for file in plots:
        nameSplit = file.split(".")
        if (len(nameSplit) == 2):
            continue
        if (nameSplit[0] == "ccf"):
            filePrefix = "clustering_coeff_"
        elif (nameSplit[0] == "outDeg"):
            filePrefix = "deg_dist_"
        elif (nameSplit[0] == "diam"):
            filePrefix = "shortest_path_"
        elif (nameSplit[0] == "scc"):
            filePrefix = "connected_comp_"

        os.rename(file, filePrefix + nameSplit[1] + "." + nameSplit[2])

    os.chdir(parentDir)
Beispiel #7
0
from twython import Twython
import sys

if __name__ == '__main__':
    CONSUMER_KEY, CONSUMER_SECRET = open('twitapikeys.txt').read().split()[:2]
    twitterapi = Twython(CONSUMER_KEY, CONSUMER_SECRET)

    filename = sys.argv[1]
    repliesgraph = snap.LoadEdgeList(snap.PNGraph, filename, 0, 1)
    snap.PrintInfo(repliesgraph, "Twitter replies network")
    print

    #reciprocity
    num_dir_edges = snap.CntUniqDirEdges(repliesgraph)
    print "{0:.2f}% of directed edges are reciprocal".format(
        snap.CntUniqBiDirEdges(repliesgraph) * 2 * 100 / num_dir_edges)

    #clustering coefficient
    print "The clustering coefficient is {0:.2f}%".format(
        snap.GetClustCf(repliesgraph) * 100)

    #strongly and weakly connected components
    CntV = snap.TIntPrV()
    snap.GetSccSzCnt(repliesgraph, CntV)
    num_cc = 0
    for p in CntV:
        print "{0} strongly connected component(s) of size {1}".format(
            p.GetVal2(), p.GetVal1())
        num_cc += p.GetVal2()
    print num_cc, "total strongly connected components"
    print
Beispiel #8
0
    #     if src == dst:
    #         self_loop_v_cnt += 1
    #     else:
    #         direct_edge_cnt += 1

    # Answer 2:
    print('There are {} self-loop nodes'.format(snap.CntSelfEdges(G1)))

    # Answer 3:
    print('Thre are {} directed edges'.format(snap.CntUniqDirEdges(G1)))

    # Answer 4:
    print('There are {} undirect edges'.format(snap.CntUniqUndirEdges(G1)))

    # Answer 5:
    print('There are {} reciprocated edges'.format(snap.CntUniqBiDirEdges(G1)))

    # Answer 6:
    print('There are {} nodes having 0 out-degree'.format(
        snap.CntOutDegNodes(G1, 0)))

    # Answer 7:
    print('There are {} nodes having 0 in-degree'.format(
        snap.CntInDegNodes(G1, 0)))

    # Answer 8:
    DegToCntV = snap.TIntPrV()
    snap.GetOutDegCnt(G1, DegToCntV)
    Out10_cnt = 0
    for item in DegToCntV:
        if item.GetVal1() > 10:
Beispiel #9
0
wiki = snap.LoadEdgeList(snap.PNGraph, "wiki-Vote.txt", 0, 1, '\t')

# number of nodes
print("Node number:", wiki.GetNodes())

# number of self-edge
print("Self Edges:", snap.CntSelfEdges(wiki))

# number of directed edges
print("Directed Edges:", wiki.GetEdges() - snap.CntSelfEdges(wiki))

# number of undirected edges (A->B and B->A counts a single undirected edge)
print("Undirected Edges:", snap.CntUniqUndirEdges(wiki))

# number of reciprocated edges
print("Reciprocated Edges:", snap.CntUniqBiDirEdges(wiki))

# nubmber of nodes of zero out-degree
print("# of zero out-degree:", snap.CntOutDegNodes(wiki, 0))

# number of nodes of zero in-degree
print("# of zero in-degree:", snap.CntInDegNodes(wiki, 0))

# number of nodes with out-degree > 10
count = 0
for i in range(11):
    count += snap.CntOutDegNodes(wiki, i)
temp = wiki.GetNodes() - count
print("Nodes with out-degree > 10:", temp)

# number of nodes with in-degree < 10
import snap

if __name__ == "__main__":
    print("Loading graph...")
    FIn = snap.TFIn("results/snap-follow.graph")
    graph = snap.TNGraph.Load(FIn)

    bidirectional = snap.CntUniqBiDirEdges(graph)
    print("Bidirectional Edges:", bidirectional)
    print("Total Edges:", graph.GetEdges())

    print("Reciprocated Ratio:", float(bidirectional) / graph.GetEdges())
Beispiel #11
0
 def numUniqueBidirectionalEdges(self):
     return snap.CntUniqBiDirEdges(self.rawGraph)