-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_functions.py
More file actions
66 lines (59 loc) · 1.77 KB
/
Copy pathdata_functions.py
File metadata and controls
66 lines (59 loc) · 1.77 KB
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
## Read a binary datafile
## Data file should be strings of 0s and 1s without spacing
def read_data(filename, n, flip = False):
Kset = {}
N = 0
with open(filename, 'r') as file:
for line in file.readlines():
s = int(line[:n],2)
if flip:
s ^= 2**n - 1
N += 1
if s in Kset:
Kset[s] += 1
else:
Kset[s] = 1
return Kset, N
## Basis transformation
def transform(Nset, basis):
Kset = {}
for s, ks in Nset.items():
Op = 1
sn = 0
for nr, op in basis.items():
if not bin(s & op).count("1")%2:
sn += Op
Op <<= 1
if sn in Kset:
Kset[sn] += ks
else:
Kset[sn] = ks
return Kset
## Transform a partition to the c1_c_2_..._c_n format
def rewrite_partition(MCM, n):
string = ""
for i in range(n):
for key, val in MCM.items():
binary = bin(val).replace("0b", "")
binary = '0' * (n - len(binary)) + binary
if binary[i] == '1':
string += str(key) + "_"
break
return string[:-1]
## Transform a partition to a dict of binary representations of the communities
def restore_partition(MCM, n):
MCM_rewritten = {}
Op = 1 << n - 1
for label in MCM.split('_'):
if label in MCM_rewritten:
MCM_rewritten[label] += Op
else:
MCM_rewritten[label] = Op
Op >>= 1
return MCM_rewritten
## Print a partition in terms of its binary representation
def print_partition(MCM, n):
for com,ICC in MCM.items():
binary = bin(ICC).replace("0b", "")
missing = n - len(binary)
print(f'com {com}\t' + '0'*missing + binary)