summaryrefslogtreecommitdiff
path: root/structure/metrics
diff options
context:
space:
mode:
authorKatolaZ <katolaz@yahoo.it>2015-10-19 16:23:00 +0100
committerKatolaZ <katolaz@yahoo.it>2015-10-19 16:23:00 +0100
commitdf8386f75b0538075d72d52693836bb8878f505b (patch)
tree704c2a0836f8b9fd9f470c12b6ae05637c431468 /structure/metrics
parent363274e79eade464247089c105260bc34940da07 (diff)
First commit of MAMMULT code
Diffstat (limited to 'structure/metrics')
-rw-r--r--structure/metrics/aggregate_layers_w.py37
-rw-r--r--structure/metrics/avg_edge_overlap.py47
-rw-r--r--structure/metrics/cartography_from_columns.py44
-rw-r--r--structure/metrics/cartography_from_deg_vectors.py37
-rw-r--r--structure/metrics/cartography_from_layers.py54
-rw-r--r--structure/metrics/edge_overlap.py43
-rw-r--r--structure/metrics/intersect_layers.py47
-rw-r--r--structure/metrics/overlap_degree.py47
8 files changed, 356 insertions, 0 deletions
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 <file1> <file2> [<file3>....]" % 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 <layer1> [<layer2>...]" % 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 <filein> <col1> <col2> [<col3> <col4>...]" % 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 <node_deg_vectors>" % 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 <layer1> <layer2> [<layer3>...]" % 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 <layer1> [<layer2>...]" % 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 <file1> <file2> [<file3>....]" % 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 <layer1> <layer2> [<layer3>...]" % 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