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/activity/degs_to_activity_overlap.py | 36 ++++++++++++++ structure/activity/degs_to_binary.py | 45 +++++++++++++++++ structure/activity/hamming_dist.py | 41 ++++++++++++++++ structure/activity/layer_activity.py | 27 ++++++++++ structure/activity/layer_activity_vectors.py | 44 +++++++++++++++++ structure/activity/multiplexity.py | 41 ++++++++++++++++ structure/activity/node_activity.py | 51 +++++++++++++++++++ structure/activity/node_activity_vectors.py | 68 ++++++++++++++++++++++++++ structure/activity/node_degree_vectors.py | 68 ++++++++++++++++++++++++++ 9 files changed, 421 insertions(+) create mode 100644 structure/activity/degs_to_activity_overlap.py create mode 100644 structure/activity/degs_to_binary.py create mode 100644 structure/activity/hamming_dist.py create mode 100644 structure/activity/layer_activity.py create mode 100644 structure/activity/layer_activity_vectors.py create mode 100644 structure/activity/multiplexity.py create mode 100644 structure/activity/node_activity.py create mode 100644 structure/activity/node_activity_vectors.py create mode 100644 structure/activity/node_degree_vectors.py (limited to 'structure/activity') diff --git a/structure/activity/degs_to_activity_overlap.py b/structure/activity/degs_to_activity_overlap.py new file mode 100644 index 0000000..c96959b --- /dev/null +++ b/structure/activity/degs_to_activity_overlap.py @@ -0,0 +1,36 @@ +#### +## +## Take a file which contains, on the n-th line, the degrees at each +## layer of the n-th node, and print on output the corresponding node +## multi-activity (i.e., the number of layers in which the node is +## active) and the overlapping degree (i.e., the total number of edges +## incident on the node) +## +## + + +import sys + +def to_binary(l): + s = 0 + e = 0 + for v in l: + s += v * pow(2,e) + e +=1 + return s + +if len(sys.argv) < 2: + print "Usage: %s " % sys.argv[0] + sys.exit(1) + +distr = {} + +with open(sys.argv[1]) as f: + for l in f: + elems = [int(x) for x in l.strip(" \n").split(" ")] + ov = sum(elems) + new_list = [1 if x>0 else 0 for x in elems] + multi_act = sum(new_list) + if multi_act and ov: + print ov, multi_act + diff --git a/structure/activity/degs_to_binary.py b/structure/activity/degs_to_binary.py new file mode 100644 index 0000000..cb7aef5 --- /dev/null +++ b/structure/activity/degs_to_binary.py @@ -0,0 +1,45 @@ +#### +## +## Take a file which contains, on the n-th line, the degrees at each +## layer of the n-th node, and print on output the corresponding node +## participation bit-strings, i.e. the string which contains "1" if on +## that layer the node is connected, and zero otherwise +## +## on the stderr, we also dump the distribution of each bit-string +## + + +import sys + +def to_binary(l): + s = 0 + e = 0 + for v in l: + s += v * pow(2,e) + e +=1 + return s + +if len(sys.argv) < 2: + print "Usage: %s " % sys.argv[0] + sys.exit(1) + +distr = {} + +with open(sys.argv[1]) as f: + for l in f: + elems = [int(x) for x in l.strip(" \n").split(" ")] + new_list = [1 if x>0 else 0 for x in elems] + val = to_binary(new_list) + if val in distr: + distr[val] += 1 + else: + distr[val] = 1 + for i in new_list: + print i, + print + +for k in distr: + bin_list = bin(k) + bin_num = sum([int(x) if x=='1' else 0 for x in bin_list[2:]]) + sys.stderr.write("%d %028s %d \n" % (bin_num, bin_list[2:], distr[k])) + diff --git a/structure/activity/hamming_dist.py b/structure/activity/hamming_dist.py new file mode 100644 index 0000000..2a98e64 --- /dev/null +++ b/structure/activity/hamming_dist.py @@ -0,0 +1,41 @@ +#### +## +## Take as input the layers of a multiplex, and provide as output a +## file where the n-th line contains the activity of the n-th +## layer. +## +## + + +import sys + +if len(sys.argv) < 3: + print "Usage: %s [...]" % sys.argv[0] + sys.exit(1) + +max_N = -1 + +layers = [] + + +for layer in sys.argv[1:]: + active = [] + 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 > max_N: + max_N = s + if d > max_N: + max_N = d + active.extend([s,d]) + active = set(active) + layers.append(active) + +for i in range(len(layers)): + for j in range(i+1, len(layers)): + s = layer[i] ^ layer[j] + print i, j, len(s)*1.0 / min(len(layer[i]) + len(layer[j]), max_N+1) + diff --git a/structure/activity/layer_activity.py b/structure/activity/layer_activity.py new file mode 100644 index 0000000..bea0416 --- /dev/null +++ b/structure/activity/layer_activity.py @@ -0,0 +1,27 @@ +#### +## +## Take as input the layers of a multiplex, and provide as output a +## file where the n-th line contains the activity of the n-th +## layer. +## +## + + +import sys + +if len(sys.argv) < 2: + print "Usage: %s [...]" % sys.argv[0] + sys.exit(1) + + +for layer in sys.argv[1:]: + active = [] + 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]] + active.extend([s,d]) + active = set(active) + print len(active) diff --git a/structure/activity/layer_activity_vectors.py b/structure/activity/layer_activity_vectors.py new file mode 100644 index 0000000..f32418d --- /dev/null +++ b/structure/activity/layer_activity_vectors.py @@ -0,0 +1,44 @@ +#### +## +## Take as input the layers of a multiplex, and provide as output a +## file where the n-th line contains the activity of the n-th +## layer. +## +## + + +import sys + +if len(sys.argv) < 2: + print "Usage: %s [...]" % sys.argv[0] + sys.exit(1) + +max_N = -1 + +layers = [] + + +for layer in sys.argv[1:]: + active = [] + 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 > max_N: + max_N = s + if d > max_N: + max_N = d + active.extend([s,d]) + active = set(active) + layers.append(active) + +for l in layers: + for n in range(max_N+1): + if n in l: + print 1, + else: + print 0, + print + diff --git a/structure/activity/multiplexity.py b/structure/activity/multiplexity.py new file mode 100644 index 0000000..9e038c9 --- /dev/null +++ b/structure/activity/multiplexity.py @@ -0,0 +1,41 @@ +#### +## +## Take as input the layers of a multiplex, and provide as output a +## file where the n-th line contains the activity of the n-th +## layer. +## +## + + +import sys + +if len(sys.argv) < 3: + print "Usage: %s [...]" % sys.argv[0] + sys.exit(1) + +max_N = -1 + +layers = [] + + +for layer in sys.argv[1:]: + active = [] + 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 > max_N: + max_N = s + if d > max_N: + max_N = d + active.extend([s,d]) + active = set(active) + layers.append(active) + +for i in range(len(layers)): + for j in range(i+1, len(layers)): + s = layer[i] & layer[j] + print i, j, len(s)*1.0 / (max_N+1) + diff --git a/structure/activity/node_activity.py b/structure/activity/node_activity.py new file mode 100644 index 0000000..6410a68 --- /dev/null +++ b/structure/activity/node_activity.py @@ -0,0 +1,51 @@ +#### +## +## Take as input the layers of a multiplex, and provide as output a +## file where the n-th line contains the activity of the n-th node. We +## assume that nodes are numbered sequentially, starting from 0, with +## no gaps (i.e., missing nodes are treated as isolated nodes) +## +## +## + + +import sys + +if len(sys.argv) < 2: + print "Usage: %s [...]" % sys.argv[0] + sys.exit(1) + +node_activity = {} + +max_N = -1 + +for layer in sys.argv[1:]: + active = [] + 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]] + active.extend([s,d]) + if s > max_N: + max_N = s + if d > max_N: + max_N = d + active = set(active) + for n in active: + if n in node_activity: + node_activity[n] += 1 + else: + node_activity[n] = 1 + + +for n in range(max_N+1): + if n in node_activity: + print node_activity[n] + else: + print 0 + + + + diff --git a/structure/activity/node_activity_vectors.py b/structure/activity/node_activity_vectors.py new file mode 100644 index 0000000..9839334 --- /dev/null +++ b/structure/activity/node_activity_vectors.py @@ -0,0 +1,68 @@ +#### +## +## Take as input the layers of a multiplex, and provide as output a +## file where the n-th line contains the degrees of the n-th node at +## each layer, separated by a space, in the format: +## +## node1_deglay1 node1_deglay2 .... node1_deglayM +## node2_deglay1 node2_deglay2 .... node2_deglayM +## .............................................. +## nodeN_deglay1 nodeN_deglay2 .... nodeN_deglayM +## +## + +import sys + +if len(sys.argv) < 2: + print "Usage: %s [...]" % sys.argv[0] + sys.exit(1) + +node_degrees = {} + +max_N = -1 + +num_layer = 0 + +for layer in sys.argv[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 > max_N: + max_N = s + if d > max_N: + max_N = d + + if s in node_degrees: + if num_layer in node_degrees[s]: + node_degrees[s][num_layer] += 1 + else: + node_degrees[s][num_layer] = 1 + else: + node_degrees[s] = {} + node_degrees[s][num_layer] = 1 + + if d in node_degrees: + if num_layer in node_degrees[d]: + node_degrees[d][num_layer] += 1 + else: + node_degrees[d][num_layer] = 1 + else: + node_degrees[d] = {} + node_degrees[d][num_layer] = 1 + num_layer += 1 + + +for n in range(max_N+1): + for i in range(num_layer): + if n in node_degrees: + if i in node_degrees[n]: + print 1, + else: + print 0, + else: + print 0, + print diff --git a/structure/activity/node_degree_vectors.py b/structure/activity/node_degree_vectors.py new file mode 100644 index 0000000..8863daa --- /dev/null +++ b/structure/activity/node_degree_vectors.py @@ -0,0 +1,68 @@ +#### +## +## Take as input the layers of a multiplex, and provide as output a +## file where the n-th line contains the degrees of the n-th node at +## each layer, separated by a space, in the format: +## +## node1_deglay1 node1_deglay2 .... node1_deglayM +## node2_deglay1 node2_deglay2 .... node2_deglayM +## .............................................. +## nodeN_deglay1 nodeN_deglay2 .... nodeN_deglayM +## +## + +import sys + +if len(sys.argv) < 2: + print "Usage: %s [...]" % sys.argv[0] + sys.exit(1) + +node_degrees = {} + +max_N = -1 + +num_layer = 0 + +for layer in sys.argv[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 > max_N: + max_N = s + if d > max_N: + max_N = d + + if s in node_degrees: + if num_layer in node_degrees[s]: + node_degrees[s][num_layer] += 1 + else: + node_degrees[s][num_layer] = 1 + else: + node_degrees[s] = {} + node_degrees[s][num_layer] = 1 + + if d in node_degrees: + if num_layer in node_degrees[d]: + node_degrees[d][num_layer] += 1 + else: + node_degrees[d][num_layer] = 1 + else: + node_degrees[d] = {} + node_degrees[d][num_layer] = 1 + num_layer += 1 + + +for n in range(max_N+1): + for i in range(num_layer): + if n in node_degrees: + if i in node_degrees[n]: + print node_degrees[n][i], + else: + print 0, + else: + print 0, + print -- cgit v1.2.3