From df8386f75b0538075d72d52693836bb8878f505b Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Mon, 19 Oct 2015 16:23:00 +0100 Subject: First commit of MAMMULT code --- structure/metrics/aggregate_layers_w.py | 37 ++++++++++++++++ structure/metrics/avg_edge_overlap.py | 47 ++++++++++++++++++++ structure/metrics/cartography_from_columns.py | 44 ++++++++++++++++++ structure/metrics/cartography_from_deg_vectors.py | 37 ++++++++++++++++ structure/metrics/cartography_from_layers.py | 54 +++++++++++++++++++++++ structure/metrics/edge_overlap.py | 43 ++++++++++++++++++ structure/metrics/intersect_layers.py | 47 ++++++++++++++++++++ structure/metrics/overlap_degree.py | 47 ++++++++++++++++++++ 8 files changed, 356 insertions(+) create mode 100644 structure/metrics/aggregate_layers_w.py create mode 100644 structure/metrics/avg_edge_overlap.py create mode 100644 structure/metrics/cartography_from_columns.py create mode 100644 structure/metrics/cartography_from_deg_vectors.py create mode 100644 structure/metrics/cartography_from_layers.py create mode 100644 structure/metrics/edge_overlap.py create mode 100644 structure/metrics/intersect_layers.py create mode 100644 structure/metrics/overlap_degree.py (limited to 'structure/metrics') diff --git a/structure/metrics/aggregate_layers_w.py b/structure/metrics/aggregate_layers_w.py new file mode 100644 index 0000000..cd5ea1d --- /dev/null +++ b/structure/metrics/aggregate_layers_w.py @@ -0,0 +1,37 @@ +#### +## +## Aggregate the layers of a multiplex, weigthing each edge according +## to the number of times it occurs in the different layers +## + +import sys +import networkx as net + + +if len(sys.argv) < 3: + print "Usage: %s [....]" % sys.argv(0) + sys.exit(1) + +G = net.Graph() + +lines = open(sys.argv[1], "r").readlines() + +for l in lines: + s,d = [int(x) for x in l.strip(" \n").split(" ")[:2]] + G.add_edge(s,d) + G[s][d]['weigth'] = 1 + +for f in sys.argv[2:]: + lines = open(f, "r").readlines() + for l in lines: + s,d = [int(x) for x in l.strip(" \n").split(" ")[:2]] + if (G.has_edge(s,d)): + G[s][d]['weigth'] += 1 + else: + G.add_edge(s,d) + G[s][d]['weigth'] = 1 + +for s,d in G.edges(): + print s,d, G[s][d]['weigth'] + + diff --git a/structure/metrics/avg_edge_overlap.py b/structure/metrics/avg_edge_overlap.py new file mode 100644 index 0000000..e68fd8b --- /dev/null +++ b/structure/metrics/avg_edge_overlap.py @@ -0,0 +1,47 @@ +#### +## +## Compute the average edge overlap of a multiplex, i.e. the average +## number of layers in which an edge is present +## +## + +import sys + + +if len(sys.argv) < 2: + print "Usage: %s [...]" % sys.argv[0] + sys.exit(1) + +max_N = -1 + +all_edges = {} + +layer_ID = -1 + +for layer in sys.argv[1:]: + layer_ID += 1 + with open(layer, "r") as lines: + for l in lines: + if l[0] == "#": + continue + s, d = [int(x) for x in l.strip(" \n").split(" ")[:2]] + if s > d: + tmp = s + s = d + d = tmp + if (s,d) in all_edges: + all_edges[(s,d)].append(layer_ID) + else: + all_edges[(s,d)] = [layer_ID] + +K = len(all_edges.keys()) +M = len(sys.argv) - 1 + +numer = 0 + +for k in all_edges.keys(): + numer += len(set(all_edges[(k)])) + + +print 1.0 * numer / K, 1.0 * numer / (K * M) + diff --git a/structure/metrics/cartography_from_columns.py b/structure/metrics/cartography_from_columns.py new file mode 100644 index 0000000..a2343ed --- /dev/null +++ b/structure/metrics/cartography_from_columns.py @@ -0,0 +1,44 @@ +#### +## +## Make a cartography (i.e., sum and participation coefficient) based +## on the values found at the given column numbers of the file given +## as input, e.g.: +## +## cartography_cols.py FILEIN 1 5 8 14 +## +## makes the assumption that the system has 4 layers, and that the +## quantities involved in the cartography are at the 2nd, 6th, 9th and +## 15th column of FILEIN +## +## +## + +import sys + +if len(sys.argv) < 4: + print "Usage: %s [ ...]" % sys.argv[0] + sys.exit(1) + +filein=sys.argv[1] +cols = [int(x) for x in sys.argv[2:]] + +M = len(cols) + +with open(filein,"r") as f: + for l in f: + if l[0] == "#": + continue + elems = [float(x) if "e" in x or "." in x else int(x) for x in l.strip(" \n").split(" ")] + sum_elems = 0 + part = 0 + for c in cols: + val = elems[c] + sum_elems += val + part += val*val + if sum_elems > 0: + part = M * 1.0 / (M -1) * (1 - part * 1.0 / (sum_elems * sum_elems)) + else: + part = 0.0 + print elems[0], sum_elems, part + + diff --git a/structure/metrics/cartography_from_deg_vectors.py b/structure/metrics/cartography_from_deg_vectors.py new file mode 100644 index 0000000..c40d701 --- /dev/null +++ b/structure/metrics/cartography_from_deg_vectors.py @@ -0,0 +1,37 @@ +#### +## +## Take as input a file containing, on each line, the degree vector of +## a node of the multiplex, and compute the multiplex cartography +## diagram +## +## + +import sys + +if len(sys.argv) < 2: + print "Usage: %s " % sys.argv[0] + sys.exit(1) + +filein=sys.argv[1] + +M = -1 + +with open(filein,"r") as lines: + for l in lines: + if l[0] == "#": + continue + elems = [int(x) for x in l.strip(" \n").split(" ")] + if (M == -1): + M = len(elems) + sum_elems = 0 + part = 0 + for val in elems: + sum_elems += val + part += val*val + if sum_elems > 0: + part = M * 1.0 / (M -1) * (1 - part * 1.0 / (sum_elems * sum_elems)) + else: + part = 0.0 + print sum_elems, part + + diff --git a/structure/metrics/cartography_from_layers.py b/structure/metrics/cartography_from_layers.py new file mode 100644 index 0000000..b9c4584 --- /dev/null +++ b/structure/metrics/cartography_from_layers.py @@ -0,0 +1,54 @@ +#### +## +## Compute the participation coefficient of each node of a multiplex +## +## + +import sys +import networkx as net +import collections +from compiler.ast import flatten + + + +if len(sys.argv) < 3: + print "Usage: %s [...]" % sys.argv[0] + sys.exit(1) + + +layers = [] + +for f in sys.argv[1:]: + G = net.Graph(net.read_edgelist(f, nodetype=int)) + layers.append(G) + +nodes = flatten([x for j in layers for x in j.nodes()]) +#nodes.sort() +nodes = set(nodes) + +M = len(layers) + +#print nodes + +for n in nodes: + deg_alpha_square = 0 + deg = 0 + col = 0 + print n, + for l in layers: + val = l.degree([n]) + if not val: + col = 2* col + continue + col *= 2 + col += 1 + val = val[n] + deg += val + deg_alpha_square += val*val + if deg > 0: + print deg, 1.0 * M / (M-1) * (1.0 - 1.0 * deg_alpha_square/(deg * deg)) , col + else: + print 0 , 0, col + + + diff --git a/structure/metrics/edge_overlap.py b/structure/metrics/edge_overlap.py new file mode 100644 index 0000000..1b27f55 --- /dev/null +++ b/structure/metrics/edge_overlap.py @@ -0,0 +1,43 @@ +#### +## +## Compute the edge overlap of each edge of the multiplex, i.e. the +## number of times that each edge appears in the multiplex +## +## + +import sys + + +if len(sys.argv) < 2: + print "Usage: %s [...]" % sys.argv[0] + sys.exit(1) + +max_N = -1 + +all_edges = {} + +layer_ID = -1 + +for layer in sys.argv[1:]: + layer_ID += 1 + with open(layer, "r") as lines: + for l in lines: + if l[0] == "#": + continue + s, d = [int(x) for x in l.strip(" \n").split(" ")[:2]] + if s > d: + tmp = s + s = d + d = tmp + if (s,d) in all_edges: + all_edges[(s,d)].append(layer_ID) + else: + all_edges[(s,d)] = [layer_ID] + +K = len(all_edges.keys()) + +for k in all_edges.keys(): + val = len(set(all_edges[(k)])) + s,d = k + print s, d, val + diff --git a/structure/metrics/intersect_layers.py b/structure/metrics/intersect_layers.py new file mode 100644 index 0000000..a9da00c --- /dev/null +++ b/structure/metrics/intersect_layers.py @@ -0,0 +1,47 @@ +#### +## +## Take a certain number of networks as input and give as output the +## corresponding intersection graph, i.e. the graph in which an edge +## exists between i and j if and only if it exists in ALL the graphs +## given as input +## + + +import sys + + +if len(sys.argv)< 3: + print "Usage: %s [....]" % sys.argv[0] + sys.exit(1) + + +graphs = {} + +num = 0 + +for fname in sys.argv[1:]: + + lines = open(fname).readlines() + graphs[num] = [] + for l in lines: + s,d = [int(x) for x in l.strip(" \n").split(" ")][:2] + if s > d: + graphs[num].append((d,s)) + else: + graphs[num].append((d,s)) + num += 1 + +#print graphs + + +for edge in graphs[0]: + to_print = True + for i in range(1, num): + if edge not in graphs[i]: + to_print = False + break + if to_print: + s,d = edge + print s,d + + diff --git a/structure/metrics/overlap_degree.py b/structure/metrics/overlap_degree.py new file mode 100644 index 0000000..fa69434 --- /dev/null +++ b/structure/metrics/overlap_degree.py @@ -0,0 +1,47 @@ +#### +## +## Compute the overlapping degree for each node and the corresponding +## z-score +## +## + +import sys +import numpy + + +if len(sys.argv) < 2: + print "Usage: %s [...]" % sys.argv[0] + sys.exit(1) + + +nodes = {} + +for f in sys.argv[1:]: + + lines = open(f).readlines() + + for l in lines: + if l[0] == "#": + continue + s,d = [int(x) for x in l.strip(" \n").split(" ")] + if nodes.has_key(s): + nodes[s] +=1 + else: + nodes[s] = 1 + if nodes.has_key(d): + nodes[d] +=1 + else: + nodes[d] = 1 + + +degrees = nodes.values() +avg_deg = numpy.mean(degrees) +std_deg = numpy.std(degrees) + +#print avg_deg, std_deg + +keys = nodes.keys() +keys.sort() + +for n in keys: + print n, nodes[n], (nodes[n] - avg_deg)/std_deg -- cgit v1.2.3