def net_structure(dataset_dir, output_dir, net, IsDir, weight):
    print(
        "\n######################################################################\n"
    )
    if os.path.isfile(str(output_dir) + str(net) + "_connected_comp.json"):
        print("Arquivo já existe: " + str(output_dir) + str(net) +
              "_connected_comp.json")
    else:

        print("Componentes conectados - " + str(dataset_dir))

        cc = [
        ]  # Vetor com tamanho de todos os componentes conectados por modelo
        cc_normal = [
        ]  # Vetor com tamanho de todos os componentes conectados por modelo - Cada tamanho é normalizado pelo número de vértices da rede em que está o componente.
        n_cc = []  # Número de componentes conectados por modelo
        i = 0

        for file in os.listdir(dataset_dir):

            i += 1
            print(
                str(output_dir) + str(net) + "/" + str(file) +
                " - Calculando propriedades para o ego " + str(i) + ": " +
                str(file))
            if IsDir is True:
                G = snap.LoadEdgeList(
                    snap.PNGraph, dataset_dir + file, 0, 1
                )  # load from a text file - pode exigir um separador.: snap.LoadEdgeList(snap.PNGraph, file, 0, 1, '\t')
            else:
                G = snap.LoadEdgeList(
                    snap.PUNGraph, dataset_dir + file, 0, 1
                )  # load from a text file - pode exigir um separador.: snap.LoadEdgeList(snap.PNGraph, file, 0, 1, '\t')


#			G.Dump()
#			time.sleep(5)

#####################################################################################

            n_nodes = G.GetNodes()  # Número de vértices
            n_edges = G.GetEdges()  # Número de arestas

            #####################################################################################
            if n_edges == 0:
                a = 0
                cc.append(a)
                cc_normal.append(a)
                print("Nenhuma aresta encontrada para a rede-ego " + str(i) +
                      " - (" + str(file))
            else:
                Components = snap.TCnComV()
                snap.GetWccs(G, Components)
                n_cc.append(len(Components))
                for CnCom in Components:
                    cc.append(CnCom.Len())
                    b = float(CnCom.Len()) / float(n_nodes)
                    cc_normal.append(b)

        N_CC = calc.calcular_full(
            n_cc
        )  # Número de componentes conectados dividido pelo número de egos, pra saber a média de componentes conectados por ego.
        CC = calc.calcular_full(cc)
        CC_NORMAL = calc.calcular_full(cc_normal)

        overview = {}
        overview['Len_ConnectedComponents'] = CC
        overview['Len_ConnectedComponents_Normal'] = CC_NORMAL
        overview['N_ConnectedComponents'] = N_CC

        with open(str(output_dir) + str(net) + "_connected_comp.json",
                  'w') as f:
            f.write(json.dumps(overview))

        with open(str(output_dir) + str(net) + "_connected_comp.txt",
                  'w') as f:
            f.write(
                "\n######################################################################\n"
            )
            f.write(
                "Number_Connected_Comp: Média: %5.3f -- Var:%5.3f -- Des. Padrão: %5.3f \n"
                % (N_CC['media'], CC['variancia'], N_CC['desvio_padrao']))
            f.write(
                "Length_Connected_Comp: Média: %5.3f -- Var:%5.3f -- Des. Padrão: %5.3f \n"
                % (CC['media'], CC['variancia'], CC['desvio_padrao']))
            f.write(
                "Length_Connected_Comp_Normalized: Média: %5.3f -- Var:%5.3f -- Des. Padrão: %5.3f \n"
                % (CC_NORMAL['media'], CC_NORMAL['variancia'],
                   CC_NORMAL['desvio_padrao']))
            f.write(
                "\n######################################################################\n"
            )

        print(
            "\n######################################################################\n"
        )
        print(
            "Number_Connected_Comp: Média: %5.3f -- Var:%5.3f -- Des. Padrão: %5.3f \n"
            % (N_CC['media'], CC['variancia'], N_CC['desvio_padrao']))
        print(
            "Length_Connected_Comp: Média: %5.3f -- Var:%5.3f -- Des. Padrão: %5.3f \n"
            % (CC['media'], CC['variancia'], CC['desvio_padrao']))
        print(
            "Length_Connected_Comp_Normalized: Média: %5.3f -- Var:%5.3f -- Des. Padrão: %5.3f \n"
            % (CC_NORMAL['media'], CC_NORMAL['variancia'],
               CC_NORMAL['desvio_padrao']))
        print(
            "\n######################################################################\n"
        )
# pickle_out.close()

# pickle_out = open(USERID_SET_PICKLE_PATH,"wb")
# pickle.dump(userid_set, pickle_out)
# pickle_out.close()

# pickle_out = open(POSTID_SET_PICKLE_PATH,"wb")
# pickle.dump(postid_set, pickle_out)
# pickle_out.close()

# # ### Fold Bipartite Graphs to create User-id and Post-id Graphs

# # In[40]:

# # Load the graphs in SNAP.
userid_ngram_bipartite_graph = snap.LoadEdgeList(snap.PUNGraph,
                                                 USERID_NGRAM_TSV_PATH, 0, 1)
postid_ngram_bipartite_graph = snap.LoadEdgeList(snap.PUNGraph,
                                                 POSTID_NGRAM_TSV_PATH, 0, 1)

# # In[44]:

# # Load pickled datastructures.
ngramid_dict = pickle.load(open(NGRAMID_DICT_PICKLE_PATH, "rb"))
userid_set = pickle.load(open(USERID_SET_PICKLE_PATH, "rb"))
postid_set = pickle.load(open(POSTID_SET_PICKLE_PATH, "rb"))

# # In[42]:

# # Basic userid ngram bipartite graph statistics.
# print "Nodes", userid_ngram_bipartite_graph.GetNodes()
# print "Edges", userid_ngram_bipartite_graph.GetEdges()
Пример #3
0
import snap
import matplotlib.pyplot as plt

# Global Variable
betweeness = 0
ModularityList = []
CommunityList = []
CheckModularity = 0

# Load File Data
Graphkarateclub = snap.LoadEdgeList(snap.PUNGraph, "karateclub.txt", 0, 1)
GraphkarateclubMaintainForDeleteEdges = snap.LoadEdgeList(
    snap.PUNGraph, "karateclub.txt", 0, 1)

# Display Nodes and Edges of Graph
TotalNumberOfNodes = Graphkarateclub.GetNodes()
TotalBumberOfEdges = Graphkarateclub.GetEdges()
print("Total # of Nodes in Graph %d", TotalNumberOfNodes)
print("Total # of Edges in Graph %d", TotalBumberOfEdges)

# Iterate No. Of Edges time
while GraphkarateclubMaintainForDeleteEdges.GetEdges() != 0:
    betweennessList = []
    Nodes = snap.TIntFltH()
    Edges = snap.TIntPrFltH()
    ''' Compute the edge betweeness of all the edges in the graph '''
    snap.GetBetweennessCentr(GraphkarateclubMaintainForDeleteEdges, Nodes,
                             Edges, 1.0)
    # Prepare BetweenessList Of List
    for edge in Edges:
        betweennessSubList = [edge.GetVal1(), edge.GetVal2(), Edges[edge]]
Пример #4
0
    list_prank = []
    for item in PRankH:
        list_prank.append([item, PRankH[item]])
    
    list_prank = sorted(list_prank, key=lambda x: x[1]) # Sort by page rank
    remove_node = list_prank[: num_remove]

    for node in remove_node:
        graph.DelNode(node[0])
    snap.SaveEdgeList(graph, edges_extracted_file)


# prepare_data("bigdata/friends_list.yaml", "bigdata/dict_bigdata.csv", "bigdata/edges_bigdata.csv")

edges_file = "edges_1000.csv"
num_nodes = 1000
graph_name = "graph-1000nodes.png"


extract_top_nodes("bigdata/edges_bigdata.csv", edges_file, num_nodes)
graph = snap.LoadEdgeList(snap.PUNGraph, edges_file, 0, 1, '\t')
# graph.Dump()  #show graph information

list_comunity , modularity = comunityDetect(graph)

print ("list comunity: ", list_comunity)
print ("len list comunity: ", len(list_comunity))
print ("Modularity of the network: %f" % modularity)

# # visualize_graph(edges_file, graph_name, list_comunity)
Пример #5
0
from __future__ import division

import snap
import math

G = snap.LoadEdgeList(snap.PUNGraph, 'predict_delete.txt', 0, 1)
d_common = {}
d_jaccard = {}
d_adamic = {}

for N1 in G.Nodes():
    #print node_list
    for N2 in G.Nodes():
        if ((int(N1.GetId()) < int(N2.GetId()))
                and not ((N1.GetId() == N2.GetId()) or
                         (N1.IsNbrNId(N2.GetId())))):
            node_list_all = set([])
            value_adamic = 0
            for Id in N1.GetOutEdges():
                #save the neighbors
                node_list_all.add(Id)

            #sage the same neighbors of n1 and n2
            node_list_same = set([])
            for Id in N2.GetOutEdges():

                #save the all neighbors of n1 and n2
                if (Id in node_list_all):
                    node_list_same.add(Id)
                    node_list_n = set([])
                    for N_n in G.GetNI(Id).GetOutEdges():
Пример #6
0
 def load(self):
     graph = snap.LoadEdgeList(snap.PUNGraph, self.root, 0, 1,
                               self.separator)
     return graph
Пример #7
0
        p = r / r.sum()
        # r = [random.uniform(0,1) for i in range(out_d)]
        # sum_ = sum(r)
        # p = [i/sum_ for i in r]
        v = node_v.GetId()
        for idx in range(node_v.GetInDeg()):
            u = node_v.GetInNId(idx)
            probs[(u, v)] = p[idx]
    return probs


if __name__ == '__main__':

    # load config
    conf = Config()
    sina_network = snap.LoadEdgeList(snap.PNEANet, conf.network_file)

    with open(conf.edge_result, 'rb') as f:
        edge_dist = pickle.load(f)

    with open(conf.node_result, 'rb') as f:
        node_dist = pickle.load(f)

    print('Sampling probabilities...')

    # Can use empty dict for sigma here, if choose deterministic
    NodeStat.sample_probability(sina_network, 0, node_dist['mu'],
                                node_dist['sigma'])

    EdgeStat.sample_probability(sina_network, 1, edge_dist['mu'],
                                edge_dist['sigma'])
Пример #8
0
# Rakshith Singh Assignment 1

import snap


#Helper function
def graph_info(graph):
    for node in graph.Nodes():
        print("node id", node.GetId(), " out degree ", node.GetOutDeg(),
              " in degree ", node.GetInDeg())


wiki_graph = snap.LoadEdgeList(snap.PNGraph, "wiki-Vote.txt")
#graph_info(wiki_graph)

#Question 1 - The number of nodes in the network
number_nodes = wiki_graph.GetNodes()
print("Question 1 - Number of Nodes in the graph is ", number_nodes)

#Question 2 - The number of nodes with a self-edge (self-loop)
self_edge_nodes = 0
for edge in wiki_graph.Edges():
    if edge.GetSrcNId() == edge.GetDstNId():
        self_edge_nodes += 1
print("Question 2 - Number of Nodes with a self loop is ", self_edge_nodes)

#Question 3 - The number of directed edges in the network
directed_edges = wiki_graph.GetEdges()
print("Question 3 - Number of directed edges is ", directed_edges)

#Question 4 - The number of undirected edges in the network
Пример #9
0
import snap
import json
from sys import argv

script, directory, item = argv

graphList = ['Items','Users']

for graph in graphList:

	G = snap.LoadEdgeList(snap.PUNGraph, directory + 'Edge_List_' + graph + '_' + item +'.txt', 0, 1, '\t')

	dict1 = {}
	PRankH = snap.TIntFltH()
	snap.GetPageRank(G, PRankH)
	for i in PRankH:
	    dict1[i] = PRankH[i]

	with open(directory + 'Pagerank_' + graph + '_' + item +'.txt', 'w') as outfile1:
		json.dump(dict1, outfile1)
		
	dict2 = {}
	NIdEigenH = snap.TIntFltH()
	snap.GetEigenVectorCentr(G, NIdEigenH)
	for i in NIdEigenH:
		dict2[i] = NIdEigenH[i]

	with open(directory + 'Eigen_Value_' + graph + '_' + item +'.txt', 'w') as outfile2:
		json.dump(dict2, outfile2)
Пример #10
0
    node_betweenness_centr = {}
    for item in Nodes:
        node_betweenness_centr[item] = Nodes[item]
    node_betweenness_centr = sorted(node_betweenness_centr.items(), key=operator.itemgetter(1), reverse=True)
    id, value = zip(*node_betweenness_centr)
    for i in range(len(id)):
        node_betweenness_centr_file.write(str(id[i]) + '\t' + str(value[i]) + '\n')
    for i in range(100):
        node_betweenness_centr_top_file.write(str(id[i]) + '\t' + str(value[i]) + '\n')
    node_betweenness_centr_file.close()
    node_betweenness_centr_top_file.close()
    return id, value


if __name__ == '__main__':
    # parameters
    # filepath = '../input/Test/toy_graph'
    filepath = "/home/carlons/workspace_python/network_data_analysis/knowledge_graph/output/YAGO3/facts-id-pair"
    label = "facts-id-pair"
    outpath = './output/' + label + '/'

    # check whether the output directory exists
    if not os.path.exists(outpath):
        os.mkdir(outpath)
    net = snap.LoadEdgeList(snap.PNGraph, filepath, 0, 1)
    paras = {'net': net, 'label': label, 'outpath': outpath}
    get_pagerank(**paras)
    get_hits(**paras)
    net = snap.LoadEdgeList(snap.PUNGraph, filepath, 0, 1)
    paras = {'net': net, 'label': label, 'outpath': outpath}
    get_eigen_vector_centr(**paras)
Пример #11
0
    # Main training loop
    with tf.GradientTape() as tape:
        for epoch in range(num_epochs):
            print("Epoch {}".format(epoch))
            optimizer.minimize(likelihood, var_list=kernels)

    return kernels


if __name__ == '__main__':

    # Run the main script
    target_graph = 'ENZYMES_g123'
    G = snap.LoadEdgeList(snap.PUNGraph,
                          target_graph + "/" + target_graph + ".txt", 0, 1,
                          ' ')
    print("Loaded {} with {} nodes".format(target_graph, G.GetNodes()))

    kernel_sizes = [2, 3, 3, 5]

    print('Finding the optimal kernels')
    optimized_kernels = optimize(G, kernel_sizes)
    final_graph = construct_graph(optimized_kernels)

    generated = snap.TUNGraph.New()
    for i in range(len(final_graph)):
        generated.AddNode(i)

    for i in range(len(final_graph)):
        for j in range(i, len(final_graph)):
Пример #12
0
import snap

fw = open('results.txt', 'w')

# load the edge list using snap library
Graph = snap.LoadEdgeList(snap.PUNGraph, "data/edges.txt", 0, 1)

for NI in Graph.Nodes():
    # calculate the closeness centrality value
    closeness_value = snap.GetClosenessCentr(Graph, NI.GetId())
    # write the results into a txt file
    fw.write("Node:{0}, Value: {1} \n".format(NI.GetId(), closeness_value))
    fw.flush()
fw.close()
Пример #13
0
def getDirAttribute(filename, node_num, weighted=None, param=1.0):
    Graph = snap.LoadEdgeList(snap.PNGraph, filename, 0, 1)

    attributeNames = [
        'Graph', 'Id', 'Degree', 'InDegree', 'OutDegree',
        'NodeBetweennessCentrality', 'PageRank', 'EgonetDegree',
        'EgonetInDegree', 'EgonetOutDegree', 'AvgNeighborDeg',
        'AvgNeighborInDeg', 'AvgNeighborOutDeg', 'EgonetConnectivity'
    ]
    if weighted:
        attributeNames += [
            'WeightedDegree', 'WeightedInDegree', 'WeightedOutDegree',
            'EgoWeightedDegree', 'AvgWeightedNeighborDeg',
            'EgonetWeightedConnectivity', 'EgoWeightedInDegree',
            'EgoWeightedOutDegree', 'AvgWeightedNeighborInDeg',
            'AvgWeightedNeighborOutDeg'
        ]

    attributes = pd.DataFrame(np.zeros((node_num, len(attributeNames))),
                              columns=attributeNames)

    attributes['Graph'] = [filename.split('/')[-1].split('.')[0]] * node_num
    attributes['Id'] = range(0, node_num)

    # Degree
    degree = np.zeros((node_num, ))
    InDegV = snap.TIntPrV()
    snap.GetNodeInDegV(Graph, InDegV)
    for item in InDegV:
        degree[item.GetVal1()] = item.GetVal2()
    attributes['Degree'] += degree
    attributes['InDegree'] = degree

    degree = np.zeros((node_num, ))
    OutDegV = snap.TIntPrV()
    snap.GetNodeOutDegV(Graph, OutDegV)
    for item in OutDegV:
        degree[item.GetVal1()] = item.GetVal2()
    attributes['Degree'] += degree
    attributes['OutDegree'] = degree

    getEgoAttr(Graph, node_num, attributes)

    if weighted:
        df = getWeightedDegree(filename, node_num, attributes, directed=True)
        getWeightedEgoAttr(Graph, node_num, attributes, df, directed=True)

    # Betweenness Centrality
    betCentr = np.zeros((node_num, ))
    Nodes = snap.TIntFltH()
    Edges = snap.TIntPrFltH()
    snap.GetBetweennessCentr(Graph, Nodes, Edges, param, True)
    for node in Nodes:
        betCentr[node] = Nodes[node]
    attributes['NodeBetweennessCentrality'] = betCentr

    # PageRank
    pgRank = np.zeros((node_num, ))
    PRankH = snap.TIntFltH()
    snap.GetPageRank(Graph, PRankH)
    for item in PRankH:
        pgRank[item] = PRankH[item]
    attributes['PageRank'] = pgRank

    return attributes
Пример #14
0
#!/usr/bin/env python

from __future__ import division
import sys
import os
import snap
import math

file = sys.argv[1]
filename = file[:-9] + "er" + file[-10:]

inGraph = snap.LoadEdgeList(snap.PUNGraph, file, 0, 1)
nodes = inGraph.GetNodes()
edges = 0

with open(file, "r") as f:
    for x in f:
        edges += 1
print "nodes: %d, edges: %d" % (nodes, edges)

path = os.path.join(r"p3_data", filename[3:])

print "starting ER graph\n"
edgeList = []
UGraph = snap.GenRndGnm(snap.PUNGraph, nodes, edges)
for EI in UGraph.Edges():
    edgeList.append([EI.GetSrcNId(), EI.GetDstNId()])
with open(path, "w") as f:
    for x in edgeList:
        f.write('{0:4d} {1:9d}\n'.format(x[0], x[1]))
print "finished ER graph\n"