Esempio n. 1
0
def coefficient(output_dir):
    random_G, random_bara_G, generated_network, original_network, GAE_network, NetGAN_network = load_data(
        output_dir)
    print('Start computing graph coefficient')
    a = np.array(
        sorted([x[1] for x in nx.clustering(original_network).items()]))
    mean = np.mean(a)
    max = np.max(a)
    original_network_coef = coef(original_network, mean, max)
    random_G_coef = coef(random_G, mean, max)
    random_bara_G_coef = coef(random_bara_G, mean, max)
    generated_network_coef = coef(generated_network, mean, max)
    GAE_network_coef = coef(GAE_network, mean, max)
    # NetGAN_network_coef = coef(NetGAN_network, mean, max)

    data_1 = kl(generated_network_coef, original_network_coef)
    data_2 = kl(original_network_coef, random_G_coef)
    data_3 = kl(original_network_coef, random_bara_G_coef)
    data_4 = kl(GAE_network_coef, original_network_coef)
    # data_5 = kl(GAE_network_coef, NetGAN_network_coef)
    print(
        'KL divergence between original network and the network generated by Music_GAN: {}'
        .format(data_1))
    print(
        'KL divergence between original network and the network generated by Random E-R: {}'
        .format(data_2))
    print(
        'KL divergence between original network and the network generated by Random B-A: {}'
        .format(data_3))
    print(
        'KL divergence between original network and the network generated by GAE: {}'
        .format(data_4))
Esempio n. 2
0
    def __init__(self, operationCount, peerCount, graphInput):
        print("Inside Simulation init")

        self._op_count = int(operationCount)
        self._peer_count = int(peerCount)

        plt.axis('off')

        graph = nx.read_weighted_edgelist(graphInput)

        print(graph)

        # some properties
        print("node degree clustering")
        for v in nx.nodes(graph):
            print('%s %d %f' % (v, nx.degree(graph, v), nx.clustering(graph, v)))

        # print the adjacency list to terminal
        try:
            nx.write_adjlist(graph, sys.stdout)
        except TypeError:
            nx.write_adjlist(graph, sys.stdout.buffer)

        # node_pos = nx.spring_layout(graph)
        #
        # edge_weight = nx.get_edge_attributes(graph, 'weight')
        # # Draw the nodes
        # nx.draw_networkx(graph, node_pos, node_color='grey', node_size=100)
        # # Draw the edges
        # nx.draw_networkx_edges(graph, node_pos, edge_color='black')
        # # Draw the edge labels
        # nx.draw_networkx_edge_labels(graph, node_pos, edge_color='red', edge_labels=edge_weight)

        # plt.show()

        paths_for_diameter = nx.shortest_path_length(graph, weight='weight')
        ecc = nx.eccentricity(graph, sp=dict(paths_for_diameter))
        self._diameter = nx.diameter(graph, e=ecc)

        print('The graph diameter is ', self._diameter)
        self._height_of_cluster = math.ceil(math.log(self._diameter, 2)) + 1

        if not os.path.exists(graphInput+'_network'):
            print("CREATING NEW NETWORK")
            self._network = Network(graph)
            self._setup_peers(graph)
            # self._build_clusters_exactly_logn_membership(graph)
            self._build_clusters_at_least_one_and_at_most_logn(graph)
            # self._build_clusters_no_overlapping(graph)
            self._build_tree(graph)
            self.save_network(graphInput + '_network')
            exit(0)

        else:
            print("LOADING NETWORK FROM INPUT FILE")
            #     load network
            self.load_network(graphInput + '_network')
    def addCentralities(self):
        """Add centrality coeficients for the final version of the graph.

        Centralities include : Kalz centrality, global clustering,
        square clustering, closeness centrality, harmonic centrality,
        betweenness centrality.
        From all centralities we choose the max or the mean. More about
        these centralities can be found in networkX algorithms.
        """
        self.kalz_coef = max(list(nx.katz_centrality(self.graph).values()))
        self.glob_clust_coef = mean(list(nx.clustering(self.graph).values()))
        self.square_clustering_coef = mean(
            list(nx.square_clustering(self.graph).values()))
        self.harmonic_coef = max(
            list(nx.harmonic_centrality(self.graph).values()))
        self.betweenness_coef = max(
            list(nx.betweenness_centrality(self.graph).values()))
        self.closeness_coef = max(
            list(nx.closeness_centrality(self.graph).values()))
Esempio n. 4
0
        source_node = next(
            iter(sorted(nx.connected_components(G), key=len, reverse=True)[i]))
        dest_node = next(
            iter(sorted(nx.connected_components(G), key=len, reverse=True)[0]))
        print("SOURCE NODEE", source_node)
        print("DEST NODEE", dest_node)
        G.add_edge(int(source_node), int(dest_node), weight=randint(1, 1))

assert nx.is_connected(G)

print(sorted(sorted(nx.connected_components(G), key=len, reverse=True)))

# some properties
print("node degree clustering")
for v in nx.nodes(G):
    print('%s %d %f' % (v, nx.degree(G, v), nx.clustering(G, v)))

# G.add_edge(0, 3, weight=4)
for e in G.edges:
    a = e[0]
    b = e[1]

    w = randint(1, 1)
    G.add_edge(int(e[0]), int(e[1]), weight=w)
# print (a)

# Drawing the graph
node_pos = nx.spring_layout(G)

edge_weight = nx.get_edge_attributes(G, 'weight')
 def calculate(self, graph, params):
     nx_graph = util.to_networkx(graph)
     return nx.clustering(nx_graph)
Esempio n. 6
0
#    Copyright (C) 2004-2017 by
#    Aric Hagberg <*****@*****.**>
#    Dan Schult <*****@*****.**>
#    Pieter Swart <*****@*****.**>
#    All rights reserved.
#    BSD license.

import sys

import matplotlib.pyplot as plt
from networkx import nx

n = 10  # 10 nodes
m = 20  # 20 edges

G = nx.gnm_random_graph(n, m)

# some properties
print("node degree clustering")
for v in nx.nodes(G):
    print('%s %d %f' % (v, nx.degree(G, v), nx.clustering(G, v)))

# print the adjacency list to terminal
try:
    nx.write_adjlist(G, sys.stdout)
except TypeError:  # Python 3.x
    nx.write_adjlist(G, sys.stdout.buffer)

nx.draw(G)
plt.show()
 def calculate(self, graph, params):
     nx_graph = util.to_networkx(graph)
     return nx.clustering(nx_graph)
Esempio n. 8
0
import sys

import matplotlib.pyplot as plt
from networkx import nx, json_graph

plt.axis('off')

# graph = nx.read_gml("test_gml")
graph = nx.read_weighted_edgelist("8_0.01diamter3_newtest.weighted.edgelist")

# some properties
print("node degree clustering")
for v in nx.nodes(graph):
    print('%s %d %f' % (v, nx.degree(graph, v), nx.clustering(graph, v)))

# print the adjacency list to terminal
try:
    nx.write_adjlist(graph, sys.stdout)
except TypeError:
    nx.write_adjlist(graph, sys.stdout.buffer)

node_pos = nx.spring_layout(graph)

edge_weight = nx.get_edge_attributes(graph, 'weight')
# Draw the nodes
nx.draw_networkx(graph, node_pos, node_color='grey', node_size=100)

# Draw the edges
nx.draw_networkx_edges(graph, node_pos, edge_color='black')

# Draw the edge labels
Esempio n. 9
0
def coef(graph, mean, max):
    coef = np.array(sorted([x[1] for x in nx.clustering(graph).items()]))
    return softmax(coef, mean, max)