B_max doesn't set the maximum number of blocks in minimize_blockmodel_dl
For a particular project, I need minimize_blockmodel_dl
to return a partition with exactly 2 blocks. (I need to compare the calculated partition with a known partition of the network that has 2 communities.) As far as I can tell, modifying the value of B_max
doesn't do anything.
MWE:
import graph_tool.all as gt
g = gt.collection.data["polbooks"]
state = gt.minimize_blockmodel_dl(g, verbose = True, B_max = 2)
state.draw(pos=g.vp["pos"], vertex_shape=state.get_blocks())
Expected behavior: The network is partitioned into no more than two blocks.
Observed behavior: The network has 3 blocks. Setting B_min
> 3 affects this, but changing B_max
doesn't seem to make any difference.
I'm using MacPorts, so I'm on gt 2.16; maybe this has been fixed and I just need to wait a week or two until 2.19 is up on MacPorts? But I didn't see any differences between 2.16 and 2.18 when I dug into the code for minimize_blockmodel_dl
or the working functions that it calls.
I was struck by two things in the code, which might or might not point to the problem.
-
minimize_blockmodel_dl
, lines 300-308, modifiesb_cache
usingB_min
andB_max
immediately before callingbisection_minimize
. Butbisection_minimize
constructs its own local version ofb_cache
from scratch. -
In
get_states
, lines 111-112,min_state
is modified depending onB_min
:
if min_state.B > B_min:
min_state = mcmc_multilevel(min_state, B=B_min, **mcmc_multilevel_args)
Nothing similar is done for max_state
. Adding the following at lines 113-114 seems to make the MWE work on this particular test network.
if max_state.B > B_max:
max_state = mcmc_multilevel(max_state, B=B_max, **mcmc_multilevel_args)
But since I don't understand how the MCMC sampler works, these lines could easily be pure gibberish.