diff --git a/src/graph/community/graph_blockmodel.hh b/src/graph/community/graph_blockmodel.hh index e5651c0e9d839709c0542f6b1fc57be0137b2b21..6f05f6c1eedcd6b5bf194374680cf649659d816e 100644 --- a/src/graph/community/graph_blockmodel.hh +++ b/src/graph/community/graph_blockmodel.hh @@ -94,7 +94,12 @@ inline double safelog(Type x) __attribute__((always_inline)) inline double safelog(size_t x) { - assert(x < __safelog_cache.size()); + if (x >= __safelog_cache.size()) + { + if (x == 0) + return 0; + return log(x); + } return __safelog_cache[x]; } @@ -102,8 +107,7 @@ __attribute__((always_inline)) inline double xlogx(size_t x) { if (x >= __xlogx_cache.size()) - cout << x << " " << __xlogx_cache.size() << endl; - assert(x < __xlogx_cache.size()); + return x * safelog(x); return __xlogx_cache[x]; } @@ -1654,7 +1658,8 @@ struct egroups_manage template static void build(Vprop b, boost::any& oegroups, VEprop esrcpos, VEprop etgtpos, Eprop eweight, Graph& g, - VertexIndex vertex_index, size_t B, bool weighted, bool empty) + VertexIndex vertex_index, size_t B, bool weighted, + bool empty) { if (weighted) { @@ -1666,8 +1671,7 @@ struct egroups_manage if (empty) return; build_dispatch(b, egroups_checked.get_unchecked(B), - esrcpos, etgtpos, eweight, g, vertex_index, B, - mpl::true_()); + esrcpos, etgtpos, eweight, g, vertex_index, B); } else { @@ -1679,27 +1683,29 @@ struct egroups_manage if (empty) return; build_dispatch(b, egroups_checked.get_unchecked(B), - esrcpos, etgtpos, eweight, g, vertex_index, B, - mpl::true_()); + esrcpos, etgtpos, eweight, g, vertex_index, B); } } - template + template static void build_dispatch(Vprop b, Egroups egroups, VEprop esrcpos, VEprop etgtpos, Eprop eweight, Graph& g, - VertexIndex, size_t, mpl::true_) + VertexIndex, size_t) { for (auto e : edges_range(g)) { size_t r = b[get_source(e, g)]; assert (r < B); auto& r_elist = egroups[r]; - esrcpos[e] = insert_edge(std::make_tuple(e, true), r_elist, eweight[e]); + esrcpos[e] = insert_edge(std::make_tuple(e, true), r_elist, + eweight[e]); size_t s = b[get_target(e, g)]; assert (s < B); auto& s_elist = egroups[s]; - etgtpos[e] = insert_edge(std::make_tuple(e, false), s_elist, eweight[e]); + etgtpos[e] = insert_edge(std::make_tuple(e, false), s_elist, + eweight[e]); } } @@ -1720,31 +1726,31 @@ struct egroups_manage template static void remove_edge(size_t pos, Epos& esrcpos, Epos& etgtpos, - DynamicSampler& elist) + vector& elist) { typedef typename property_traits::value_type val_t; + if (get<1>(elist.back())) + esrcpos[get<0>(elist.back())] = pos; + else + etgtpos[get<0>(elist.back())] = pos; if (get<1>(elist[pos])) esrcpos[get<0>(elist[pos])] = numeric_limits::max(); else etgtpos[get<0>(elist[pos])] = numeric_limits::max(); - elist.remove(pos); + elist[pos] = elist.back(); + elist.pop_back(); } template static void remove_edge(size_t pos, Epos& esrcpos, Epos& etgtpos, - vector& elist) + DynamicSampler& elist) { typedef typename property_traits::value_type val_t; - if (get<1>(elist.back())) - esrcpos[get<0>(elist.back())] = pos; - else - etgtpos[get<0>(elist.back())] = pos; if (get<1>(elist[pos])) esrcpos[get<0>(elist[pos])] = numeric_limits::max(); else etgtpos[get<0>(elist[pos])] = numeric_limits::max(); - elist[pos] = elist.back(); - elist.pop_back(); + elist.remove(pos); } template diff --git a/src/graph_tool/community/blockmodel.py b/src/graph_tool/community/blockmodel.py index 9a6e79e4c511b2d39ac69541ab56b5e7c1759873..5feb7eecdb84792fc7a0102f5ba9b7322022d368 100644 --- a/src/graph_tool/community/blockmodel.py +++ b/src/graph_tool/community/blockmodel.py @@ -97,7 +97,6 @@ class BlockState(object): 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 @@ -205,9 +204,11 @@ class BlockState(object): self.edges_dl = False # computation cache - libcommunity.init_safelog(int(5 * max(self.E, self.N))) - libcommunity.init_xlogx(int(5 * max(self.E, self.N))) - libcommunity.init_lgamma(int(3 * max(self.E, self.N))) + E = g.num_edges() + N = g.num_vertices() + libcommunity.init_safelog(int(5 * max(E, N))) + libcommunity.init_xlogx(int(5 * max(E, N))) + libcommunity.init_lgamma(int(3 * max(E, N))) def __del__(self): try: