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

3
4
from __future__ import print_function

5
verbose = __name__ == "__main__"
Tiago Peixoto's avatar
Tiago Peixoto committed
6

Tiago Peixoto's avatar
Tiago Peixoto committed
7
8
9
import os
import sys
if not verbose:
Tiago Peixoto's avatar
Tiago Peixoto committed
10
11
12
    out = open(os.devnull, 'w')
else:
    out = sys.stdout
13
14
from collections import OrderedDict
import itertools
15
16
from graph_tool.all import *
import numpy.random
17
from numpy.random import randint, normal, random
18
19

numpy.random.seed(42)
Tiago Peixoto's avatar
Tiago Peixoto committed
20
seed_rng(42)
21
22
23
24
25
26

graph_tool.inference.set_test(True)

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

27
28
29
30
rec_p = g.new_ep("double", random(g.num_edges()))
rec_s = g.new_ep("double", normal(0, 10, g.num_edges()))


Tiago Peixoto's avatar
Tiago Peixoto committed
31
def gen_state(directed, deg_corr, layers, overlap, rec, rec_type, allow_empty):
32
33
34
35
36
    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(),
Tiago Peixoto's avatar
Tiago Peixoto committed
37
38
                                                       recs=[rec] if rec is not None else [],
                                                       rec_types=[rec_type] if rec is not None else [],
39
                                                       overlap=overlap,
40
41
                                                       layers=layers == True,
                                                       allow_empty=allow_empty)
42
43
    elif overlap:
        state = graph_tool.inference.OverlapBlockState(u, B=2 * u.num_edges(),
Tiago Peixoto's avatar
Tiago Peixoto committed
44
45
                                                       recs=[rec] if rec is not None else [],
                                                       rec_types=[rec_type] if rec is not None else [],
46
47
48
                                                       deg_corr=deg_corr)
    else:
        state = graph_tool.inference.BlockState(u, B=u.num_vertices(),
Tiago Peixoto's avatar
Tiago Peixoto committed
49
50
                                                recs=[rec] if rec is not None else [],
                                                rec_types=[rec_type] if rec is not None else [],
51
52
                                                deg_corr=deg_corr,
                                                allow_empty=allow_empty)
53
54
55
    return state


56
57
58
pranges = [("directed", [False, True]),
           ("overlap", [False, True]),
           ("layered", [False, "covariates", True]),
Tiago Peixoto's avatar
Tiago Peixoto committed
59
           ("rec", [None, "real-exponential", "real-normal"]),
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
           ("deg_corr", [False, True]),
           ("dl", [False, True]),
           ("degree_dl_kind", ["uniform", "distributed", "entropy"]),
           ("allow_empty", [False, True]),
           ("exact", [True, False])]

pranges = OrderedDict(pranges)

def iter_ranges(ranges):
    for vals in itertools.product(*[v for k, v in ranges.items()]):
        yield zip(ranges.keys(), vals)


for pvals in iter_ranges(pranges):
    params = OrderedDict(pvals)

    locals().update(params)

    if not deg_corr and degree_dl_kind != "uniform":
        continue

Tiago Peixoto's avatar
Tiago Peixoto committed
81
82
83
    if overlap and degree_dl_kind != "distributed":
        continue

84
85
86
87
88
89
    if (rec is not None or layered != False) and not exact:
        continue

    print(params, file=out)

    rec_ = None
Tiago Peixoto's avatar
Tiago Peixoto committed
90
    if rec == "real-exponential":
91
        rec_ = rec_p
Tiago Peixoto's avatar
Tiago Peixoto committed
92
    elif rec == "real-normal":
93
94
95
        rec_ = rec_s

    print("\t mcmc (unweighted)", file=out)
Tiago Peixoto's avatar
Tiago Peixoto committed
96
    state = gen_state(directed, deg_corr, layered, overlap, rec_, rec, allow_empty)
97
98
99
100
101

    print("\t\t",
          state.mcmc_sweep(beta=0,
                           entropy_args=dict(dl=dl,
                                             degree_dl_kind=degree_dl_kind,
102
                                             exact=exact, beta_dl=0.95)),
103
104
105
106
107
108
109
          state.get_nonempty_B(), file=out)

    if overlap:
        print("\t\t",
              state.mcmc_sweep(beta=0, bundled=True,
                               entropy_args=dict(dl=dl,
                                                 degree_dl_kind=degree_dl_kind,
110
                                                 exact=exact, beta_dl=0.95)),
111
112
              state.get_nonempty_B(), file=out)

Tiago Peixoto's avatar
Tiago Peixoto committed
113
    state = gen_state(directed, deg_corr, layered, overlap, rec_, rec, allow_empty)
114
115
116
117
118
119
120
121
122

    if not overlap:
        print("\t mcmc", file=out)
        bstate = state.get_block_state(vweight=True,  deg_corr=deg_corr)

        print("\t\t",
              bstate.mcmc_sweep(beta=0,
                                entropy_args=dict(dl=dl,
                                                  degree_dl_kind=degree_dl_kind,
123
                                                  exact=exact, beta_dl=0.95)),
124
125
126
127
128
129
              bstate.get_nonempty_B(), file=out)

        print("\t\t",
              bstate.mcmc_sweep(beta=0,
                                entropy_args=dict(dl=dl,
                                                  degree_dl_kind=degree_dl_kind,
130
                                                  exact=exact, beta_dl=0.95)),
131
132
133
134
135
136
              bstate.get_nonempty_B(), file=out)

        print("\t\t",
              bstate.gibbs_sweep(beta=0,
                                 entropy_args=dict(dl=dl,
                                                   degree_dl_kind=degree_dl_kind,
137
                                                   exact=exact, beta_dl=0.95)),
138
139
140
141
              bstate.get_nonempty_B(), file=out)

    print("\t merge", file=out)

Tiago Peixoto's avatar
Tiago Peixoto committed
142
    state = gen_state(directed, deg_corr, layered, overlap, rec_, rec, allow_empty)
143
144
145
146
147
148
149
150
151

    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,
                                                   degree_dl_kind=degree_dl_kind,
                                                   multigraph=False,
152
                                                   exact=exact, beta_dl=0.95)),
153
154
155
156
157
158
159
160
              file=out)

        bstate = bstate.copy()

        print("\t\t",
              bstate.mcmc_sweep(beta=0,
                                entropy_args=dict(dl=dl,
                                                  degree_dl_kind=degree_dl_kind,
161
                                                  exact=exact, beta_dl=0.95)),
162
163
164
165
166
              file=out)
        print("\t\t",
              bstate.gibbs_sweep(beta=0,
                                 entropy_args=dict(dl=dl,
                                                   degree_dl_kind=degree_dl_kind,
167
                                                   exact=exact, beta_dl=0.95)),
168
169
170
171
172
173
174
              file=out)
    else:
        print("\t\t",
              state.merge_sweep(50,
                                entropy_args=dict(dl=dl,
                                                  degree_dl_kind=degree_dl_kind,
                                                  multigraph=False,
175
                                                  exact=exact, beta_dl=0.95)),
176
              file=out)
177

178
    print("\t shrink", file=out)
179

Tiago Peixoto's avatar
Tiago Peixoto committed
180
    state = gen_state(directed, deg_corr, layered, overlap, rec_, rec, allow_empty)
181
182
183
    state = state.shrink(B=5, entropy_args=dict(dl=dl,
                                                degree_dl_kind=degree_dl_kind,
                                                multigraph=False,
184
                                                exact=exact, beta_dl=0.95))
185
    print("\t\t", state.B, "\n", file=out)
186
187


188
189
190
pranges = [("directed", [False, True]),
           ("overlap", [False, True]),
           ("layered", [False, "covariates", True]),
Tiago Peixoto's avatar
Tiago Peixoto committed
191
           ("rec", [None, "real-exponential", "real-normal"]),
192
193
194
           ("deg_corr", [False, True]),
           ("degree_dl_kind", ["uniform", "distributed", "entropy"]),
           ("exact", [True])]
195

196
pranges = OrderedDict(pranges)
197

198
199
for pvals in iter_ranges(pranges):
    params = OrderedDict(pvals)
200

201
    locals().update(params)
202

203
204
    if not deg_corr and degree_dl_kind != "uniform":
        continue
205

Tiago Peixoto's avatar
Tiago Peixoto committed
206
207
208
    if overlap and degree_dl_kind != "distributed":
        continue

209
    print(params, file=out)
210

Tiago Peixoto's avatar
Tiago Peixoto committed
211
212
    rec_ = []
    if rec == "real-exponential":
213
214
        rec_ = [rec_p]
        rec = [rec]
Tiago Peixoto's avatar
Tiago Peixoto committed
215
    elif rec == "real-normal":
216
217
218
219
        rec_ = [rec_s]
        rec = [rec]
    else:
        rec = []
220

221
    if layered != False:
222
223
        state_args = dict(ec=ec, layers=(layered == True), recs=rec_,
                          rec_types=rec)
224
    else:
225
        state_args = dict(recs=rec_, rec_types=rec)
226

227
    entropy_args = dict(exact=exact, beta_dl=0.95)
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

    state = minimize_blockmodel_dl(GraphView(g, directed=directed),
                                   verbose=(1, "\t") if verbose else False,
                                   deg_corr=deg_corr,
                                   overlap=overlap,
                                   layers=layered != False,
                                   state_args=state_args,
                                   mcmc_args=dict(entropy_args=entropy_args))

    print(state.B, state.entropy(), file=out)

    state = minimize_nested_blockmodel_dl(GraphView(g, directed=directed),
                                          verbose=(2, "\t") if verbose else False,
                                          deg_corr=deg_corr,
                                          overlap=overlap,
                                          layers=layered != False,
                                          state_args=state_args,
                                          mcmc_args=dict(entropy_args=entropy_args))
    if verbose:
        state.print_summary()

    print(state.entropy(), "\n", file=out)
250

251
graph_tool.inference.set_test(False)
252

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