Saving and loading `ModeClusterState` object

Hi Prof. Peixoto,

My graph-tool version is 2.70
Operating system is ubuntu 22

I wanted to load and save an object of gt.ModeClusterState(), I am able to save it to a pickle file, but I get the following error while loading it back:

TypeError: __init__() got an unexpected keyword argument 'entropy_args'

My minimal working example is the following:

import pickle
import graph_tool.all as gt

with open(f'bs.npy', 'rb') as f:
    bs = pickle.load(f)

pmode = gt.ModeClusterState(bs, nested=True)
gt.mcmc_equilibrate(pmode, wait=1, mcmc_args=dict(niter=1, beta=np.inf))

with open(f'pmode.npy', 'wb') as f:
    pickle.dump([pmode], f)

with open(f'pmode.npy', 'rb') as f:
    [pmode] = pickle.load(f)

Many thanks,
Govinda

Govinda,

The minimal working example you gave is not complete because it does not run without the bs.npy file. Could you please either provide this file or modify the example? Many thanks.

Best,
Tiago

bs.npy (173.4 KB)

My bad, I’m sorry.

If I use this file I get a completely different error:

AttributeError                            Traceback (most recent call last)
Cell In[6], line 7
      4 with open(f'bs.npy', 'rb') as f:
      5     bs = pickle.load(f)
----> 7 pmode = gt.ModeClusterState(bs, nested=True)
      8 gt.mcmc_equilibrate(pmode, wait=1, mcmc_args=dict(niter=1, beta=np.inf))
     10 with open(f'pmode.npy', 'wb') as f:

File ~/.local/lib/python3.12/site-packages/graph_tool/inference/partition_modes.py:299, in ModeClusterState.__init__(self, bs, b, B, nested, relabel)
    297 for bv in bs:
    298     if self.nested:
--> 299         bv = [Vector_int32_t(init=x) for x in bv]
    300     else:
    301         bv = [Vector_int32_t(init=bv)]

File ~/.local/lib/python3.12/site-packages/graph_tool/__init__.py:4147, in _vt_init(self, n, init)
   4145     self.resize(n)
   4146 if init is not None:
-> 4147     _vt_setstate(self, init)

File ~/.local/lib/python3.12/site-packages/graph_tool/__init__.py:4129, in _vt_setstate(self, state)
   4128 def _vt_setstate(self, state):
-> 4129     self.resize(len(state))
   4130     if self.a is not None:
   4131         self.a = state

File ~/.local/lib/python3.12/site-packages/graph_tool/__init__.py:938, in VertexPropertyMap.__len__(self)
    936 def __len__(self):
    937     g = self.get_graph()
--> 938     return g.num_vertices()

AttributeError: 'NoneType' object has no attribute 'num_vertices'

This is happening because you pickled a property map without its graph. Could you please revise your MWE, so I can investigate?

Hi Prof. Peixoto,

This is the revised MWE:

import graph_tool.all as gt
import pickle
import numpy as np

with open('bs.npy', 'rb') as f:
    [state, bs] = pickle.load(f)
    
pmode = gt.ModeClusterState(bs, nested=True)
gt.mcmc_equilibrate(pmode, wait=1, mcmc_args=dict(niter=1, beta=np.inf))

with open('pmode.npy', 'wb') as f:
    pickle.dump([state, bs, pmode], f)
    
with open('pmode.npy', 'rb') as f:
    [state, bs, pmode] = pickle.load(f)

Thanks,
Govinda
bs.npy (201.5 KB)