Commit 38e2952a authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Keep track of (Overlap)BlockState instances to free cache resources when possible

parent 149e15c1
......@@ -84,10 +84,14 @@ class BlockState(object):
memory,
"""
_state_ref_count = 0
def __init__(self, g, eweight=None, vweight=None, b=None,
B=None, clabel=None, deg_corr=True,
max_BE=1000, **kwargs):
BlockState._state_ref_count += 1
# initialize weights to unity, if necessary
if eweight is None:
eweight = g.new_edge_property("int")
......@@ -193,6 +197,14 @@ class BlockState(object):
libcommunity.init_xlogx(int(5 * max(self.E, self.N)))
libcommunity.init_lgamma(int(3 * max(self.E, self.N)))
def __del__(self):
BlockState._state_ref_count -= 1
if BlockState._state_ref_count == 0:
libcommunity.clear_safelog()
libcommunity.clear_xlogx()
libcommunity.clear_lgamma()
def __repr__(self):
return "<BlockState object with %d blocks,%s for graph %s, at 0x%x>" % \
(self.B, " degree corrected," if self.deg_corr else "", str(self.g),
......
......@@ -68,6 +68,8 @@ class OverlapBlockState(BlockState):
def __init__(self, g, b=None, B=None, clabel=None, deg_corr=True,
max_BE=1000, **kwargs):
BlockState._state_ref_count += 1
# determine if there is a base graph, and overlapping structure
self.base_g = kwargs.get("base_g", None)
......@@ -221,6 +223,9 @@ class OverlapBlockState(BlockState):
libcommunity.init_xlogx(int(5 * max(self.E, self.N)))
libcommunity.init_lgamma(int(3 * max(self.E, self.N)))
def __del__(self):
BlockState.__del__(self)
def __repr__(self):
return "<OverlapBlockState object with %d blocks,%s for graph %s, at 0x%x>" % \
(self.B, " degree corrected," if self.deg_corr else "",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment