Revision 3673c3e8 fiddle/heuristic-betweenness-centrality/utility.py

View differences:

fiddle/heuristic-betweenness-centrality/utility.py
1
import json
1 2
import os
2 3
import networkx as nx
3 4
import matplotlib.pyplot as plt
......
17 18
    if not os.path.isdir(visualization_dir):
18 19
        os.makedirs(visualization_dir)
19 20

  
20
def weight_betweenness_centrality(filepath, file_suffix):
21
    graph = nx.read_weighted_edgelist(filepath)
22

  
21
def weight_betweenness_centrality(graph, file_suffix):
23 22
    score = centrality.weight_betweenness_centrality(graph, rescale=True)
24 23
    filepath = '/output/weight_basic_%s.csv'  % file_suffix
25 24

  
......
27 26
        for node, bc in score.iteritems():
28 27
            output.write('%s, %s\n' % (node, bc))
29 28

  
30
def brandes_betweenness_centrality(filepath, file_suffix):
31
    graph = nx.read_weighted_edgelist(filepath)
32

  
33
    score = nx.betweenness_centrality(graph)
29
def brandes_betweenness_centrality(graph, file_suffix):
30
    score = nx.betweenness_centrality(graph, endpoints=False)
31
    # score = nx.betweenness_centrality(graph, endpoints=True)
34 32
    filepath = '/output/networkx_%s.csv'  % file_suffix
35 33

  
36 34
    with open(MAIN_CODE_DIR + filepath, 'w') as output:
37 35
        for node, bc in score.iteritems():
38 36
            output.write('%s, %s\n' % (node, bc))
39 37

  
40
def brandes_betweenness_centrality_endpoints(filepath, file_suffix):
41
    graph = nx.read_weighted_edgelist(filepath)
42

  
38
def brandes_betweenness_centrality_endpoints(graph, file_suffix):
43 39
    score = nx.betweenness_centrality(graph, endpoints=True)
44 40
    filepath = '/output/heuristic_%s.csv'  % file_suffix
45 41

  
......
47 43
        for node, bc in score.iteritems():
48 44
            output.write('%s, %s\n' % (node, bc))
49 45

  
46
def read_complex_json(filepath):
47
    """Returns a graph object
48
    """
49
    edges = []
50
    with open(filepath) as f:
51
        doc = json.loads(f.read())
52
        topologies = doc["topology"]
53
        for connection in topologies:
54
            source = connection['lastHopIP']
55
            target = connection['destinationIP']
56
            cost = connection['neighborLinkQuality']
57
            edges.append((source, target, {'weight': cost}))
58

  
59
    graph = nx.Graph()
60
    print "Edges = %i\n" % len(edges)
61
    graph.add_edges_from(edges)
62
    return graph
63

  
50 64

  
51 65
def plot_graph(output_filepath, graph, title=""):
52 66
    plt.figure(1, figsize=(15, 10))

Also available in: Unified diff