summaryrefslogtreecommitdiff
path: root/common.py
blob: da8fe79780144df8f20f4db1ec6bb0f2e38a5c5f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
from itertools import combinations
from sys import argv

def increment(dictionary, key):
    dictionary[key] = dictionary.get(key, 0) + 1

def print_dict(dictionary):
    keys = sorted(list(dictionary.keys()))
    for key in keys:
        print(key, dictionary[key])

def distribution(counts, capset, num):
    combs = tuple(combinations(range(counts["dim"]), num))
    num_i = len(combs)
    num_j = 3**num
    dists = [[0 for j in range(num_j)]
                for i in range(num_i)]
    for card in capset:
        for i, indices in enumerate(combs):
            substr = "".join(card[indices[n]] for n in range(num))
            j = int(substr, 3)
            dists[i][j] += 1
    metadist = {}
    for i in range(num_i):
        dists[i] = "".join(map(str, sorted(dists[i])))
        name = "dist" + str(num) + ":" \
             + str(len(capset))  + ":" + dists[i]
        increment(counts, name)
        increment(metadist, name)
    if counts["premeta" + str(num)]:
        z = tuple(zip(*counts["premeta" + str(num)].items()))
    else:
        z = ([],[])
    if metadist in z[1]:
        key = z[0][z[1].index(metadist)]
    else:
        key = len(z[0])
        counts["premeta" + str(num)][key] = metadist
    name = "premeta" + str(num) + ":" + str(key)
    increment(counts, name)

def metadistribution(counts, num):
    dists = tuple(sorted(key for key in counts.keys()
                             if key.startswith("dist" + str(num))))
    for key in counts["premeta" + str(num)]:
        count = counts["premeta" + str(num) + ":" + str(key)]
        metadist = counts["premeta" + str(num)][key]
        newstr = "".join(str(metadist.get(dist, 0)) for dist in dists)
        counts["meta" + str(num) + ":" + newstr] = count
    for key in counts.copy():
        if key.startswith("premeta" + str(num)):
            del counts[key]

def main():
    if len(argv) == 1:
        infile = "capset.out"
    elif len(argv) == 2:
        infile = argv[1]
    else:
        print("Usage: common.py [INFILE]")
        exit()
    capsets = []
    capset_file = open(infile, "r")
    for line in capset_file:
        capsets.append(line[1:-2].split(" "))
    capset_file.close()

    counts = {"count" : 0,
              "dim"   : len(capsets[0][0])}
    nums = range(1, counts["dim"])
    for num in nums:
        counts["premeta" + str(num)] = {}
    for capset in capsets:
        counts["count"] += 1
        for num in nums:
            distribution(counts, capset, num)
    for num in nums:
        metadistribution(counts, num)
    print_dict(counts)

if __name__ == "__main__": main()