test_inference.py 7.11 KB
Newer Older
1
2
#!/bin/env python

3
4
from __future__ import print_function

Tiago Peixoto's avatar
Tiago Peixoto committed
5
6
verbose=False

Tiago Peixoto's avatar
Tiago Peixoto committed
7
8
9
if __name__ == "__main__":
    verbose = True

Tiago Peixoto's avatar
Tiago Peixoto committed
10
11
12
import os
import sys
if not verbose:
Tiago Peixoto's avatar
Tiago Peixoto committed
13
14
15
    out = open(os.devnull, 'w')
else:
    out = sys.stdout
Tiago Peixoto's avatar
Tiago Peixoto committed
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
from graph_tool.all import *
import numpy.random
from numpy.random import randint

seed_rng(42)
numpy.random.seed(42)

graph_tool.inference.set_test(True)

g = collection.data["football"]
ec = g.new_ep("int", randint(0, 10, g.num_edges()))

def gen_state(directed, deg_corr, layers, overlap):
    u = GraphView(g, directed=directed)
    if layers != False:
        state = graph_tool.inference.LayeredBlockState(u, B=u.num_vertices(),
                                                       deg_corr=deg_corr,
                                                       ec=ec.copy(),
                                                       overlap=overlap,
                                                       layers=layers == True)
    elif overlap:
        state = graph_tool.inference.OverlapBlockState(u, B=2 * u.num_edges(),
                                                       deg_corr=deg_corr)
    else:
        state = graph_tool.inference.BlockState(u, B=u.num_vertices(),
                                                deg_corr=deg_corr)
    return state


for directed in [True, False]:
    for overlap in [False, True]:
        for layered in [False, "covariates", True]:
            for deg_corr in [False, True]:
                for dl in [False, True]:

                    print("\ndirected:", directed, "overlap:", overlap,
Tiago Peixoto's avatar
Tiago Peixoto committed
53
                          "layered:", layered, "deg-corr:", deg_corr, "dl:", dl,
Tiago Peixoto's avatar
Tiago Peixoto committed
54
                          file=out)
55
56


Tiago Peixoto's avatar
Tiago Peixoto committed
57
                    print("\t mcmc (unweighted)", file=out)
58
59
60
61
                    state = gen_state(directed, deg_corr, layered, overlap)

                    print("\t\t", state.mcmc_sweep(beta=0, allow_empty=True,
                                                   entropy_args=dict(dl=dl)),
Tiago Peixoto's avatar
Tiago Peixoto committed
62
                          (state.wr.a > 0).sum(), file=out)
63
64
65
66
                    if overlap:
                        print("\t\t", state.mcmc_sweep(beta=0, bundled=True,
                                                       allow_empty=True,
                                                       entropy_args=dict(dl=dl)),
Tiago Peixoto's avatar
Tiago Peixoto committed
67
                              (state.wr.a > 0).sum(),
Tiago Peixoto's avatar
Tiago Peixoto committed
68
                              file=out)
69
70
71
72

                    state = gen_state(directed, deg_corr, layered, overlap)

                    if not overlap:
Tiago Peixoto's avatar
Tiago Peixoto committed
73
                        print("\t mcmc", file=out)
74
75
76
77
78
79
80
81
                        bstate = state.get_block_state(vweight=True,
                                                       deg_corr=deg_corr)

                        print("\t\t",
                              bstate.mcmc_sweep(beta=0,
                                                allow_empty=True,
                                                entropy_args=dict(dl=dl,
                                                                  multigraph=False)),
Tiago Peixoto's avatar
Tiago Peixoto committed
82
                              (bstate.wr.a > 0).sum(), file=out)
83
84
85
86
87

                        print("\t\t",
                              bstate.mcmc_sweep(beta=0, allow_empty=True,
                                                entropy_args=dict(dl=dl,
                                                                  multigraph=False)),
Tiago Peixoto's avatar
Tiago Peixoto committed
88
                              (bstate.wr.a > 0).sum(), file=out)
89
90
91
92
93

                        print("\t\t",
                              bstate.gibbs_sweep(beta=0, allow_empty=True,
                                                 entropy_args=dict(dl=dl,
                                                                   multigraph=False)),
Tiago Peixoto's avatar
Tiago Peixoto committed
94
                              (bstate.wr.a > 0).sum(), file=out)
95

Tiago Peixoto's avatar
Tiago Peixoto committed
96
                    print("\t merge", file=out)
97
98
99
100
101
102
103
104
105
106

                    state = gen_state(directed, deg_corr, layered, overlap)

                    if not overlap:
                        bstate = state.get_block_state(vweight=True,
                                                       deg_corr=deg_corr)

                        print("\t\t",
                              bstate.merge_sweep(50,
                                                 entropy_args=dict(dl=dl,
Tiago Peixoto's avatar
Tiago Peixoto committed
107
                                                                   multigraph=False)),
Tiago Peixoto's avatar
Tiago Peixoto committed
108
                              file=out)
109
110
111
112
113
114

                        bstate = bstate.copy()

                        print("\t\t",
                              bstate.mcmc_sweep(beta=0, allow_empty=True,
                                                entropy_args=dict(dl=dl,
Tiago Peixoto's avatar
Tiago Peixoto committed
115
                                                                  multigraph=False)),
Tiago Peixoto's avatar
Tiago Peixoto committed
116
                              file=out)
117
118
119
                        print("\t\t",
                              bstate.gibbs_sweep(beta=0, allow_empty=True,
                                                 entropy_args=dict(dl=dl,
Tiago Peixoto's avatar
Tiago Peixoto committed
120
                                                                multigraph=False)),
Tiago Peixoto's avatar
Tiago Peixoto committed
121
                              file=out)
122
123
124
125
                    else:
                        print("\t\t",
                              state.merge_sweep(50,
                                                entropy_args=dict(dl=dl,
Tiago Peixoto's avatar
Tiago Peixoto committed
126
                                                                  multigraph=False)),
Tiago Peixoto's avatar
Tiago Peixoto committed
127
                              file=out)
128

Tiago Peixoto's avatar
Tiago Peixoto committed
129
                    print("\t shrink", file=out)
130
131
132
133

                    state = gen_state(directed, deg_corr, layered, overlap)
                    state = state.shrink(B=5, entropy_args=dict(dl=dl,
                                                                multigraph=False))
Tiago Peixoto's avatar
Tiago Peixoto committed
134
                    print("\t\t", state.B, file=out)
135
136
137
138
139
140

for directed in [True, False]:
    for overlap in [False, True]:
        for layered in [False, "covariates", True]:
            for deg_corr in [False, True]:
                print("\ndirected:", directed, "overlap:", overlap,
Tiago Peixoto's avatar
Tiago Peixoto committed
141
                      "layered:", layered, "deg-corr:", deg_corr, file=out)
142
143

                state = minimize_blockmodel_dl(GraphView(g, directed=directed),
Tiago Peixoto's avatar
Tiago Peixoto committed
144
                                               verbose=(1, "\t") if verbose else False,
145
146
147
148
149
                                               deg_corr=deg_corr,
                                               overlap=overlap,
                                               layers=layered != False,
                                               state_args=dict(ec=ec,
                                                               layers=(layered == True)))
Tiago Peixoto's avatar
Tiago Peixoto committed
150
                print(state.B, state.entropy(), file=out)
151
152

                state = minimize_nested_blockmodel_dl(GraphView(g, directed=directed),
Tiago Peixoto's avatar
Tiago Peixoto committed
153
                                                      verbose=(1, "\t") if verbose else False,
154
155
156
157
158
                                                      deg_corr=deg_corr,
                                                      overlap=overlap,
                                                      layers=layered != False,
                                                      state_args=dict(ec=ec,
                                                                      layers=(layered == True)))
Tiago Peixoto's avatar
Tiago Peixoto committed
159
160
                if verbose:
                    state.print_summary()
Tiago Peixoto's avatar
Tiago Peixoto committed
161
                print(state.entropy(), file=out)
162

Tiago Peixoto's avatar
Tiago Peixoto committed
163
print("OK")