Proper way to depickle NestedState?

I'm saving NestedState with pickle and then loading it later to use
get_edges_prob.

    nested_state = pickle.load(
        open(f"output/graphtool_output/pickle/{name}_nested_blockstate.p",
             "rb"))

I realized that NestedState needs a reference to the underlying graph for
get_edges_prob to work. So, I try this.

    G = gt.load_graph(f'output/graphtool_output/{name}.graphml')
    nested_state.g = G

Now, get_edges_prob is working if I use the base level of the state, like
so:

    def collect_edge_probs(s):
        s = s.levels[0]
        for i, non_edge in enumerate(non_edges):
            p = s.get_edges_prob([non_edge], [],
                                 entropy_args=dict(partition_dl=False))
            non_edges_probs[i].append(p)

If I remove this " s = s.levels[0]" I'm getting an error about "invalid
edge descriptor". Is there some other reference I need to recover for this
to work correctly?

Any help appreciated!

attachment.html (1.26 KB)

I'm saving NestedState with pickle and then loading it later to use
get_edges_prob.

nested\_state = pickle\.load\(
    open\(f"output/graphtool\_output/pickle/\{name\}\_nested\_blockstate\.p",
         "rb"\)\)

I realized that NestedState needs a reference to the underlying graph
for get_edges_prob to work. So, I try this.

G = gt\.load\_graph\(f'output/graphtool\_output/\{name\}\.graphml'\)
nested\_state\.g = G

I'm utterly puzzled by what you are trying to do.

First, unpickling should just work, there is no need for you to "fix" it.

Second, you should never ever try to modify an object like this, in
graph-tool and otherwise, just by randomly changing an internal
variable. In languages such as C++ this kind of meddling can be
forbidden, but in Python is unfortunatelly always allowed. If you
replace the graph inside a NestedBlockState, the object becomes
inconsistent and corrupted. There is no need ever to try to do this, and
it will never work.

Now, get_edges_prob is working if I use the base level of the state,
like so:

def collect\_edge\_probs\(s\):
    s = s\.levels\[0\]
    for i, non\_edge in enumerate\(non\_edges\):
        p = s\.get\_edges\_prob\(\[non\_edge\], \[\],
                             entropy\_args=dict\(partition\_dl=False\)\)
        non\_edges\_probs\[i\]\.append\(p\)

If I remove this " s = s.levels[0]" I'm getting an error about "invalid
edge descriptor". Is there some other reference I need to recover for
this to work correctly?

See above. In order for this to "work correctly" it must never be
attempted to begin with.

Best,
Tiago

Looking back at this I'm not totally sure what I was thinking... I think I
saw the tiny filesize of the pickled State and assumed it was missing a
reference to the graph. Anyway, I got it now. Apologies for the utter
puzzlement.

Thanks as always

attachment.html (3.08 KB)