summaryrefslogtreecommitdiff
path: root/models/nullmodels/model_MDM.py
blob: 5b0a9693ff300a0e46abf81e3e1dcb318e003153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
####
##
##
## This is the vertical participation model. For each node i, we use
## exactly the same value of B_i as in the original network, but we
## choose at random the layers in which node i will be active. This
## breaks down intra-layer correlations. 
##
## We get as input a file which reports, for each value of B_i, the
## number of nodes in the original network which have that value, i the format:
##
## B_i N(B_i)
##
##
##
## The output is the obtained distribution of bit-strings.
##
##

import sys
import random


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) < 3:
    print "Usage: %s <Bi_file> <M>" % sys.argv[0]
    sys.exit(1)

M = int(sys.argv[2])

layers = range(M)

distr = {}

with open(sys.argv[1], "r") as f:
    for l in f:
        if l[0] == "#":
            continue
        val, num = [int(x) for x in l.strip(" \n").split(" ")]
        for j in range(num):
            node_layers = random.sample(layers, val)
            node_bitstring = [0 for x in range(M)]
            #print node_bitstring, node_layers
            for i in node_layers:
                #print i,
                node_bitstring[i] = 1
                #print node_bitstring
                
            bs = to_binary(node_bitstring)
            if bs in distr:
                distr[bs] += 1
            else:
                distr[bs] = 1

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 %0175s %d \n" % (bin_num, bin_list[2:], distr[k]))