test_inference.py 8.8 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
import itertools
14
15
from graph_tool.all import *
import numpy.random
16
from numpy.random import randint, normal, random
17
18

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

graph_tool.inference.set_test(True)

g = collection.data["football"]
24

25
# # add self-loops
26
27
28
# for i in range(10):
#     v = numpy.random.randint(g.num_vertices())
#     g.add_edge(v, v)
29

30
# # add parallel edges
31
32
# for e in list(g.edges())[:10]:
#     g.add_edge(e.source(), e.target())
33

34
35
ec = g.new_ep("int", randint(0, 10, g.num_edges()))

36
37
38
39
rec_p = g.new_ep("double", random(g.num_edges()))
rec_s = g.new_ep("double", normal(0, 10, g.num_edges()))


40
def _gen_state(directed, deg_corr, layers, overlap, rec, rec_type, dense_bg):
41
42
    u = GraphView(g, directed=directed)
    if layers != False:
43
44
45
46
47
48
49
        base_type = graph_tool.inference.LayeredBlockState
        state_args = dict(B=u.num_vertices(),
                          deg_corr=deg_corr,
                          ec=ec.copy(),
                          recs=[rec] if rec is not None else [],
                          rec_types=[rec_type] if rec is not None else [],
                          overlap=overlap,
50
                          layers=layers == True, dense_bg=dense_bg)
51
    elif overlap:
52
53
54
55
        base_type = graph_tool.inference.OverlapBlockState
        state_args = dict(B=2 * u.num_edges(),
                          recs=[rec] if rec is not None else [],
                          rec_types=[rec_type] if rec is not None else [],
56
                          deg_corr=deg_corr, dense_bg=dense_bg)
57
    else:
58
59
60
61
        base_type = graph_tool.inference.BlockState
        state_args = dict(B=u.num_vertices(),
                          recs=[rec] if rec is not None else [],
                          rec_types=[rec_type] if rec is not None else [],
62
                          deg_corr=deg_corr, dense_bg=dense_bg)
63
64
65
66
67
68
69
70
    return u, base_type, state_args


def gen_state(*args):
    u, base_type, state_args = _gen_state(*args)
    return base_type(u, **state_args)

def gen_nested_state(*args):
71
    u, base_type, state_args = _gen_state(*args)
72
73
74
    B = state_args.pop("B")
    return NestedBlockState(u,
                            bs=[numpy.arange(B)] + [numpy.zeros(1)] * 6,
Tiago Peixoto's avatar
Tiago Peixoto committed
75
                            base_type=base_type, state_args=state_args)
76
77


Alex Henrie's avatar
Alex Henrie committed
78
79
80
81
82
83
84
85
pranges = {"directed": [False, True],
           "overlap": [False, True],
           "layered": [False, "covariates", True],
           "rec": [None, "real-exponential", "real-normal"],
           "deg_corr": [False, True],
           "dl": [False, True],
           "degree_dl_kind": ["uniform", "distributed", "entropy"],
           "dense_bg": [True, False]}
86
87
88
89
90
91
92

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):
Alex Henrie's avatar
Alex Henrie committed
93
    params = dict(pvals)
94
95
96
97
98
99

    locals().update(params)

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

100
    if overlap and deg_corr and degree_dl_kind != "distributed":      # FIXME
Tiago Peixoto's avatar
Tiago Peixoto committed
101
102
        continue

103
104
105
    print(params, file=out)

    rec_ = None
Tiago Peixoto's avatar
Tiago Peixoto committed
106
    if rec == "real-exponential":
107
        rec_ = rec_p
Tiago Peixoto's avatar
Tiago Peixoto committed
108
    elif rec == "real-normal":
109
110
111
        rec_ = rec_s

    print("\t mcmc (unweighted)", file=out)
112
    state = gen_state(directed, deg_corr, layered, overlap, rec_, rec, dense_bg)
113

Tiago Peixoto's avatar
Tiago Peixoto committed
114
115
    eargs = dict(dl=dl, degree_dl_kind=degree_dl_kind, beta_dl=0.95)

116
    print("\t\t",
Tiago Peixoto's avatar
Tiago Peixoto committed
117
          state.mcmc_sweep(beta=0, entropy_args=eargs),
118
119
120
121
122
123
124
          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,
125
                                                 beta_dl=0.95)),
126
127
              state.get_nonempty_B(), file=out)

128
    print("\t mcmc (unweighted, multiflip)", file=out)
129
    state = gen_state(directed, deg_corr, layered, overlap, rec_, rec, dense_bg)
130
    print("\t\t",
Tiago Peixoto's avatar
Tiago Peixoto committed
131
          state.multiflip_mcmc_sweep(beta=0, entropy_args=eargs),
132
133
          state.get_nonempty_B(), file=out)

Tiago Peixoto's avatar
Tiago Peixoto committed
134
135
136
    print("\t mcmc (unweighted, multilevel)", file=out)
    state = gen_state(directed, deg_corr, layered, overlap, rec_, rec, dense_bg)
    print("\t\t",
Tiago Peixoto's avatar
Tiago Peixoto committed
137
          state.multilevel_mcmc_sweep(beta=0, M=3, entropy_args=eargs),
Tiago Peixoto's avatar
Tiago Peixoto committed
138
139
          state.get_nonempty_B(), file=out)

140
    print("\t gibbs (unweighted)", file=out)
141
    state = gen_state(directed, deg_corr, layered, overlap, rec_, rec, dense_bg)
142

143
    print("\t\t",
Tiago Peixoto's avatar
Tiago Peixoto committed
144
          state.gibbs_sweep(beta=0, entropy_args=eargs),
145
146
          state.get_nonempty_B(), file=out)

147
    if not overlap:
148
        state = gen_state(directed, deg_corr, layered, overlap, rec_, rec, dense_bg)
149

150
151
152
153
        print("\t mcmc", file=out)
        bstate = state.get_block_state(vweight=True,  deg_corr=deg_corr)

        print("\t\t",
Tiago Peixoto's avatar
Tiago Peixoto committed
154
              bstate.mcmc_sweep(beta=0, entropy_args=eargs),
155
156
157
              bstate.get_nonempty_B(), file=out)

        print("\t\t",
Tiago Peixoto's avatar
Tiago Peixoto committed
158
              bstate.mcmc_sweep(beta=0, entropy_args=eargs),
159
160
161
              bstate.get_nonempty_B(), file=out)

        print("\t\t",
Tiago Peixoto's avatar
Tiago Peixoto committed
162
              bstate.gibbs_sweep(beta=0, entropy_args=eargs),
163
164
              bstate.get_nonempty_B(), file=out)

165

Tiago Peixoto's avatar
Tiago Peixoto committed
166
167
168
169
170
171
172
pranges = {"directed": [False, True],
           "overlap": [False],
           "layered": [False, "covariates", True],
           "rec": [None, "real-exponential", "real-normal"],
           "deg_corr": [True, False],
           "degree_dl_kind": ["distributed"],
           "dense_bg": [True, False]}
173
174
175
176
177
178

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):
Alex Henrie's avatar
Alex Henrie committed
179
    params = dict(pvals)
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

    locals().update(params)

    if overlap and deg_corr and degree_dl_kind != "distributed":      # FIXME
        continue

    print(params, file=out)

    rec_ = None
    if rec == "real-exponential":
        rec_ = rec_p
    elif rec == "real-normal":
        rec_ = rec_s

    print("\t mcmc (single flip)", file=out)
195
    state = gen_nested_state(directed, deg_corr, layered, overlap, rec_, rec, dense_bg)
196

Tiago Peixoto's avatar
Tiago Peixoto committed
197
    eargs = dict(degree_dl_kind=degree_dl_kind, beta_dl=0.95)
Tiago Peixoto's avatar
Tiago Peixoto committed
198

199
    for i in range(5):
Tiago Peixoto's avatar
Tiago Peixoto committed
200
        print("\t\t", state.mcmc_sweep(beta=0, d=0.5, entropy_args=eargs),
201
202
203
              [s.get_nonempty_B() for s in state.levels], file=out)

    print("\n\t mcmc (multiple flip)", file=out)
204
    state = gen_nested_state(directed, deg_corr, layered, overlap, rec_, rec, dense_bg)
205
206
207

    for i in range(5):
        print("\t\t",
Tiago Peixoto's avatar
Tiago Peixoto committed
208
              state.multiflip_mcmc_sweep(beta=0, d=0.5, entropy_args=eargs),
209
              [s.get_nonempty_B() for s in state.levels], file=out)
210

Alex Henrie's avatar
Alex Henrie committed
211
212
213
214
215
216
217
pranges = {"directed": [False, True],
           "overlap": [False, True],
           "layered": [False, "covariates", True],
           "rec": [None, "real-exponential", "real-normal"],
           "deg_corr": [False, True],
           "degree_dl_kind": ["uniform", "distributed", "entropy"],
           "dense_bg": [True, False]}
218

219
for pvals in iter_ranges(pranges):
Alex Henrie's avatar
Alex Henrie committed
220
    params = dict(pvals)
221

222
    locals().update(params)
223

224
225
    if not deg_corr and degree_dl_kind != "uniform":
        continue
226

227
    if overlap and deg_corr and degree_dl_kind != "distributed":    # FIXME
Tiago Peixoto's avatar
Tiago Peixoto committed
228
229
        continue

230
    print(params, file=out)
231

Tiago Peixoto's avatar
Tiago Peixoto committed
232
233
    rec_ = []
    if rec == "real-exponential":
234
235
        rec_ = [rec_p]
        rec = [rec]
Tiago Peixoto's avatar
Tiago Peixoto committed
236
    elif rec == "real-normal":
237
238
239
240
        rec_ = [rec_s]
        rec = [rec]
    else:
        rec = []
241

242
    if layered != False:
Tiago Peixoto's avatar
Tiago Peixoto committed
243
        state_t = LayeredBlockState
244
        state_args = dict(ec=ec, layers=(layered == True), recs=rec_,
Tiago Peixoto's avatar
Tiago Peixoto committed
245
                          rec_types=rec, deg_corr=deg_corr, overlap=overlap)
246
    else:
Tiago Peixoto's avatar
Tiago Peixoto committed
247
248
249
250
251
        if overlap:
            state_t = OverlapBlockState
        else:
            state_t = BlockState
        state_args = dict(recs=rec_, rec_types=rec, deg_corr=deg_corr)
252

253
    entropy_args = dict(beta_dl=0.95)
254
255

    state = minimize_blockmodel_dl(GraphView(g, directed=directed),
Tiago Peixoto's avatar
Tiago Peixoto committed
256
                                   state=state_t,
257
                                   state_args=state_args,
Tiago Peixoto's avatar
Tiago Peixoto committed
258
259
260
261
262
263
264
265
266
267
                                   multilevel_mcmc_args=dict(entropy_args=entropy_args))

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

    state_args = dict(state_args=state_args)
    if layered != False:
        state_args["base_state"] = LayeredBlockState
        state_args["overlap"] = overlap
    elif overlap:
            state_args["base_state"] = OverlapBlockState
268
269
270
271


    state = minimize_nested_blockmodel_dl(GraphView(g, directed=directed),
                                          state_args=state_args,
Tiago Peixoto's avatar
Tiago Peixoto committed
272
                                          multilevel_mcmc_args=dict(entropy_args=entropy_args))
273
274
275
276
    if verbose:
        state.print_summary()

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

Alex Henrie's avatar
Alex Henrie committed
278
print("OK")