Statistics
| Branch: | Revision:

nepatest_popbabel / scripts / freifunk.py @ 60ba786f

History | View | Annotate | Download (3.34 KB)

1
#!/usr/bin/python
2
import networkx as nx
3
import glob
4
import sys
5
import geopy.distance as gd
6
import tabulate
7
from collections import Counter
8
import code
9

    
10
class ff:
11
    def __init__(self, folder):
12
        self.extension = "graphml"
13
        self.load_files(folder)
14

    
15
    def load_files(self, folder):
16
        self.data = {}
17
        for f in glob.glob(folder+"/*."+self.extension):
18
            g = nx.Graph(nx.read_graphml(f))
19
            self.data[f] = g
20

    
21
    def print_summary(self):
22
        for k, g in sorted(self.data.items(), key=lambda x: len(x[1].nodes())):
23
            print k, len(sorted(nx.connected_components(g), key=len,
24
                         reverse=True)[0]),
25
            print("%.0f" % self.compute_link_length(g))
26
            #print("%.0f" % self.compute_max_distance(g))
27

    
28
    def clean_graphs(self):
29
        edge_types = set()
30
        for k, g in self.data.items()[:]:
31
            c = Counter()
32
            good_link_types = ["wireless","wifi"]
33
            for src, dest, attr in g.edges(data=True):
34
                if attr["physicalType"] not in good_link_types:
35
                    g.remove_edge(src, dest)
36
                c[attr["physicalType"]] += 1
37
            graphs = filter(lambda x: len(x) > 10, sorted(
38
                list(nx.connected_component_subgraphs(g)), reverse=True,
39
                            key=len))
40
            if graphs:
41
                
42
                self.data[k] = graphs[0]
43
            else:
44
                self.data[k] = None
45
            print k, len(graphs), [len(x) for x in graphs]
46
        self.data = {k: v for k, v in self.data.items()[:] if v}
47

    
48
        print "\n\n#############\n\n"
49
        for k, v in self.data.items():
50
            #print len(v), k
51
            if(len(v)>50):
52
                name="bigcompo"+k.split("/")[-1]
53
                print name, "size greates comp:", len(v)
54
                #code.interact(local=dict(globals(), **locals()))
55
                g = nx.convert_node_labels_to_integers(v) 
56
                nx.write_weighted_edgelist(g, name)
57

    
58
    def compute_link_length(self, g):
59
        len_sum = 0.0
60
        edge_number = 0
61
        max_len = 0
62
        for edge in g.edges():
63
            try:
64
                left = (g.node[edge[0]]["Latitude"],
65
                        g.node[edge[0]]["Longitude"])
66
                right = (g.node[edge[1]]["Latitude"],
67
                         g.node[edge[1]]["Longitude"])
68
                link_len = gd.vincenty(left, right).meters
69
                len_sum += link_len
70
                if max_len < link_len:
71
                    max_len = link_len
72
                edge_number += 1
73
            except KeyError:
74
                pass
75
        if edge_number:
76
            return 0.001*len_sum/edge_number
77
        return 0
78

    
79
    def compute_max_distance(self, g):
80
        max_distance = 0
81
        for i in range(len(g.nodes())):
82
            for j in range(i, len(g.nodes())):
83
                try:
84
                    left = (g.nodes(data=True)[i][1]["Latitude"],
85
                            g.nodes(data=True)[i][1]["Longitude"])
86
                    right = (g.nodes(data=True)[j][1]["Latitude"],
87
                             g.nodes(data=True)[j][1]["Longitude"])
88
                    link_len = gd.vincenty(left, right).meters
89
                    if link_len > max_distance:
90
                        max_distance = link_len
91
                except KeyError:
92
                    pass
93
        return max_distance*0.001