Perhaps not a bug: NameError: name 'log' is not defined in mcmc_equilibrate

Hi there,

I cannot reproduce the issue on a ReprEx with in-built data, so it may not be a bug. But I can reproduce it on a bunch of data (e.g., sharetot.graphml) that used to work fine until I ran some updates on my Ubuntu 22.04.4 LTS system. I tried removing and re-installing graph-tool, but to no avail.

As per the title, running a mcmc_equilibrate on a LayeredBlockState produces the error:

Traceback (most recent call last):

  Cell In[7], line 1
    mcmc_equilibrate(res_sharetot_lT_dF_oF, wait=1000, mcmc_args=dict(niter=10),

  File /usr/local/lib/python3.10/dist-packages/graph_tool/inference/mcmc.py:117 in mcmc_equilibrate
    S = state.entropy(**mcmc_args.get("entropy_args", {}))

  File /usr/local/lib/python3.10/dist-packages/graph_tool/inference/base_states.py:49 in wrapper
    S = func(self, *args, **kwargs)

  File /usr/local/lib/python3.10/dist-packages/graph_tool/inference/nested_blockmodel.py:374 in entropy
    S += kwargs.get("beta_dl", 1.) * self._Lrecdx_entropy()

  File /usr/local/lib/python3.10/dist-packages/graph_tool/inference/nested_blockmodel.py:343 in _Lrecdx_entropy
    S_D[j] -= log(B_E_D)

NameError: name 'log' is not defined

Work flow:

from graph_tool.all import *
import os
import re
import matplotlib
import numpy
import math
import pickle

sharetot = load_graph('./sharetot.graphml')

# Verify the property names
sharetot.list_properties()
recs = (sharetot.ep['Base metals'],
        sharetot.ep['Bulk of trade'],
        sharetot.ep['Machinery'],
        sharetot.ep['Processed food'],
        sharetot.ep['Stones'])
rec_types = ['real-normal', 'real-normal', 'real-normal',
             'real-normal', 'real-normal']

sharetot.vp.period = sharetot.vp.period.coerce_type()
sharetot.ep.layer = sharetot.ep.layer.coerce_type()

state_sharetot_lF_dF_oF = LayeredBlockState(g = sharetot, ec = sharetot.ep.layer,
                                 recs = recs, rec_types = rec_types,
                                 pclabel = sharetot.vp.period.get_array(),
                                 layers = False, deg_corr = False,
                                 overlap = False)

res_sharetot_lT_dF_oF = minimize_nested_blockmodel_dl(sharetot,
                              state_args = dict(
                                  base_type=LayeredBlockState,
                                  state_args=dict(
                                      ec = sharetot.ep.layer,
                                    recs = recs, rec_types = rec_types,
                                    pclabel = sharetot.vp.period.get_array(),
                                    layers = True, deg_corr = False,
                                    overlap = False
                                    )
                                  )
                              )

dS, nmoves=0, 0
for i in range(100):
    ret=res_sharetot_lT_dF_oF.multiflip_mcmc_sweep(niter=10)
    dS+=ret[0]
    nmoves+=ret[1]

print("Change in description length:", dS)
print("Number of accepted vertex moves:", nmoves)

# Equilibrate a MCMC to improve the partition
# Efficient Monte Carlo heuristic for the inference of stochastic block models
mcmc_equilibrate(res_sharetot_lT_dF_oF, wait=1000, mcmc_args=dict(niter=10),
                 verbose=True)

Please open an issue in the issue tracker at https://git.skewed.de/count0/graph-tool/-/issues and it will be tracked from there.

Please post the link to the issue here for future reference.

Thank you. The report is here: NameError: name ‘log’ is not defined in mcmc_equilibrate (#783) · Issues · Tiago Peixoto / graph-tool · GitLab

I did not create an issue, initially, because I could not reproduce the error with built-in data, I apologise for the mis-posting

For anyone still getting the same issue, it was fixed with commit 5ef5aabd.

Either install the whole package directly from the Git repo. Or, for a quick fix :face_with_peeking_eye:, just download the file nested_blockmodel.py from the depository on Git and replace the version in the installation folder (on Ubuntu 24 it is: /usr/local/lib/python3.10/dist-packages/graph_tool/inference/).

No need for this “quick fix”, since the actual fix was already incorporated in release 2.76.