In [1]:
import graph_tool.all as gt
import math
import numpy as np


/home/cheyulin/anaconda2/lib/python2.7/site-packages/graph_tool/draw/cairo_draw.py:1480: RuntimeWarning: Error importing Gtk module: No module named gi; GTK+ drawing will not work.
  warnings.warn(msg, RuntimeWarning)

In [2]:
pv = None

In [9]:
g = gt.collection.data["karate"]

state = gt.BlockState(g, B=20)

print('num v:', g.num_vertices())
state = state.copy(B=g.num_vertices())
dS, nmoves = state.mcmc_sweep(niter=1000)
print("Change in description length:", dS)
print("Number of accepted vertex moves:", nmoves)
gt.mcmc_equilibrate(state, wait=1000, mcmc_args=dict(niter=10))


def collect_marginals(s):
    global pv
    pv = s.collect_vertex_marginals(pv)


gt.mcmc_equilibrate(state, force_niter=10000, mcmc_args=dict(niter=10),
                    callback=collect_marginals)
print(g.vp.pos)
# Now the node marginals are stored in property map pv. We can
# visualize them as pie charts on the nodes:
state.draw(pos=g.vp.pos, vertex_text=g.vertex_index, vertex_shape="pie", vertex_pie_fractions=pv, 
           edge_gradient=None, output="karate-sbm-marginals.pdf")


('num v:', 34)
('Change in description length:', -82.64494213810974)
('Number of accepted vertex moves:', 5909)
<PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x7fa634eb7950, at 0x7fa634eb7b90>
Out[9]:
<PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x7fa634eb7950, at 0x7fa634eb7b90>

In [ ]: