Commit a2189994 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

blockmodel: cleanup partition_stats usage

parent 02203e8e
......@@ -197,6 +197,8 @@ public:
out_degreeS()(v, _g, _eweight)};
}
}
init_partition_stats();
}
// =========================================================================
......@@ -417,10 +419,9 @@ public:
_wr[r] -= _vweight[v];
if (is_partition_stats_enabled())
get_partition_stats(v).remove_vertex(v, r, _deg_corr, _g,
_vweight, _eweight,
_degs);
get_partition_stats(v).remove_vertex(v, r, _deg_corr, _g,
_vweight, _eweight,
_degs);
}
void add_partition_node(size_t v, size_t r)
......@@ -429,9 +430,8 @@ public:
_wr[r] += _vweight[v];
if (is_partition_stats_enabled())
get_partition_stats(v).add_vertex(v, r, _deg_corr, _g, _vweight,
_eweight, _degs);
get_partition_stats(v).add_vertex(v, r, _deg_corr, _g, _vweight,
_eweight, _degs);
if (_vweight[v] > 0 && _wr[r] == _vweight[v])
{
......@@ -483,16 +483,13 @@ public:
size_t r = _b[u];
size_t s = _b[v];
if (is_partition_stats_enabled())
{
get_partition_stats(u).remove_vertex(u, r, _deg_corr, _g,
get_partition_stats(u).remove_vertex(u, r, _deg_corr, _g,
_vweight, _eweight,
_degs);
if (u != v)
get_partition_stats(v).remove_vertex(v, s, _deg_corr, _g,
_vweight, _eweight,
_degs);
if (u != v)
get_partition_stats(v).remove_vertex(v, s, _deg_corr, _g,
_vweight, _eweight,
_degs);
}
auto me = _emat.get_me(r, s);
if constexpr (Add)
......@@ -539,17 +536,14 @@ public:
modify_edge<Add, Deplete>(u, v, e, _is_weighted);
if (is_partition_stats_enabled())
{
get_partition_stats(u).add_vertex(u, r, _deg_corr, _g,
get_partition_stats(u).add_vertex(u, r, _deg_corr, _g,
_vweight, _eweight,
_degs);
if (u != v)
get_partition_stats(v).add_vertex(v, s, _deg_corr, _g,
_vweight, _eweight,
_degs);
if (u != v)
get_partition_stats(v).add_vertex(v, s, _deg_corr, _g,
_vweight, _eweight,
_degs);
get_partition_stats(u).change_E(Add ? 1 : -1); // FIXME: wrong for multiple partition stats
}
get_partition_stats(u).change_E(Add ? 1 : -1); // FIXME: wrong for multiple partition stats
if (_coupled_state != nullptr)
{
......@@ -1002,7 +996,6 @@ public:
if (ea.degree_dl || ea.edges_dl)
{
enable_partition_stats();
auto& ps = get_partition_stats(v);
if (_deg_corr && ea.degree_dl)
dS_dl += ps.get_delta_deg_dl(v, r, nr, _vweight, _eweight,
......@@ -1242,7 +1235,6 @@ public:
if (r != s && dr + ds != 0 && ea.edges_dl)
{
size_t actual_B = 0;
enable_partition_stats();
for (auto& ps : _partition_stats)
actual_B += ps.get_actual_B();
size_t E = _partition_stats.front().get_E();
......@@ -1278,7 +1270,6 @@ public:
if (ea.partition_dl)
{
enable_partition_stats();
auto& ps = get_partition_stats(v);
dS += ps.get_delta_partition_dl(v, r, nr, _vweight);
}
......@@ -1665,7 +1656,6 @@ public:
if (ea.edges_dl)
{
enable_partition_stats();
size_t actual_B = 0;
for (auto& ps : _partition_stats)
actual_B += ps.get_actual_B();
......@@ -1705,7 +1695,6 @@ public:
double get_partition_dl()
{
enable_partition_stats();
double S = 0;
for (auto& ps : _partition_stats)
S += ps.get_partition_dl();
......@@ -1714,7 +1703,6 @@ public:
double get_deg_dl(int kind)
{
enable_partition_stats();
double S = 0;
for (auto& ps : _partition_stats)
S += ps.get_deg_dl(kind);
......@@ -1775,8 +1763,6 @@ public:
if (ea.degree_dl && _deg_corr)
{
enable_partition_stats();
if (r != s || u == v)
{
std::array<std::pair<size_t, size_t>, 2> degs;
......@@ -1938,7 +1924,6 @@ public:
{
if (ea.edges_dl)
{
enable_partition_stats();
size_t actual_B = 0;
for (auto& psi : _partition_stats)
actual_B += psi.get_actual_B();
......@@ -1967,65 +1952,55 @@ public:
return dS;
}
void enable_partition_stats()
void init_partition_stats()
{
if (_partition_stats.empty())
{
size_t E = 0;
for (auto e : edges_range(_g))
E += _eweight[e];
size_t B = num_vertices(_bg);
reset_partition_stats();
size_t E = 0;
for (auto e : edges_range(_g))
E += _eweight[e];
size_t B = num_vertices(_bg);
// Clang 8.0 fails to correctly recognize these as ForwardIterators,
// triggering a static_assert in std::max_element(). See #576.
#ifndef __clang__
auto vi = std::max_element(
auto vi = std::max_element(
#else
auto vi = boost::first_max_element(
auto vi = boost::first_max_element(
#endif
vertices(_g).first, vertices(_g).second,
[&](auto u, auto v)
{ return (this->_pclabel[u] <
this->_pclabel[v]); });
vertices(_g).first, vertices(_g).second,
[&](auto u, auto v)
{ return (this->_pclabel[u] <
this->_pclabel[v]); });
size_t C = _pclabel[*vi] + 1;
size_t C = _pclabel[*vi] + 1;
vector<vector<size_t>> vcs(C);
vector<size_t> rc(num_vertices(_bg));
for (auto v : vertices_range(_g))
{
vcs[_pclabel[v]].push_back(v);
rc[_b[v]] = _pclabel[v];
}
vector<vector<size_t>> vcs(C);
vector<size_t> rc(num_vertices(_bg));
for (auto v : vertices_range(_g))
{
vcs[_pclabel[v]].push_back(v);
rc[_b[v]] = _pclabel[v];
}
for (size_t c = 0; c < C; ++c)
_partition_stats.emplace_back(_g, _b, vcs[c], E, B,
_vweight, _eweight, _degs);
for (size_t c = 0; c < C; ++c)
_partition_stats.emplace_back(_g, _b, vcs[c], E, B,
_vweight, _eweight, _degs);
for (auto r : vertices_range(_bg))
_partition_stats[rc[r]].get_r(r);
}
for (auto r : vertices_range(_bg))
_partition_stats[rc[r]].get_r(r);
}
void disable_partition_stats()
void reset_partition_stats()
{
_partition_stats.clear();
_partition_stats.shrink_to_fit();
}
bool is_partition_stats_enabled() const
{
return !_partition_stats.empty();
}
partition_stats_t& get_partition_stats(size_t v)
{
size_t r = _pclabel[v];
if (r >= _partition_stats.size())
{
disable_partition_stats();
enable_partition_stats();
}
init_partition_stats();
return _partition_stats[r];
}
......@@ -2033,11 +2008,6 @@ public:
void init_mcmc(MCMCState& state)
{
auto c = state._c;
auto& entropy_args = state._entropy_args;
bool dl = (entropy_args.partition_dl ||
entropy_args.degree_dl ||
entropy_args.edges_dl);
if (!std::isinf(c))
{
_egroups.clear();
......@@ -2047,11 +2017,6 @@ public:
{
_egroups.clear();
}
if (dl)
enable_partition_stats();
else
disable_partition_stats();
}
void couple_state(BlockStateVirtualBase& s, const entropy_args_t& ea)
......
......@@ -85,7 +85,6 @@ struct Layers
using BaseState::_m_entries;
using BaseState::_emat;
using BaseState::_partition_stats;
using BaseState::is_partition_stats_enabled;
using BaseState::get_move_entries;
typedef vprop_map_t<int32_t>::type block_rmap_t;
......@@ -229,7 +228,6 @@ struct Layers
_layers(other._layers),
_actual_B(other._actual_B),
_N(other._N),
_is_partition_stats_enabled(other._is_partition_stats_enabled),
_lcoupled_state(other._lcoupled_state),
_vc_c(_vc.get_checked()),
_vmap_c(_vmap.get_checked())
......@@ -242,7 +240,6 @@ struct Layers
std::vector<LayerState> _layers;
size_t _actual_B = 0;
size_t _N = 0;
bool _is_partition_stats_enabled = false;
typedef entropy_args_t _entropy_args_t;
LayeredBlockStateVirtualBase* _lcoupled_state = nullptr;
typename vc_t::checked_t _vc_c;
......@@ -641,25 +638,6 @@ struct Layers
double edge_entropy_term(size_t, size_t, const entropy_args_t&) { return 0; }
void enable_partition_stats()
{
if (!_is_partition_stats_enabled)
{
BaseState::enable_partition_stats();
for (auto& state : _layers)
state.enable_partition_stats();
_is_partition_stats_enabled = true;
}
}
void disable_partition_stats()
{
BaseState::disable_partition_stats();
for (auto& state : _layers)
state.disable_partition_stats();
_is_partition_stats_enabled = false;
}
template <class MCMCState>
void init_mcmc(MCMCState& state)
{
......
......@@ -167,8 +167,7 @@ public:
_overlap_stats(other._overlap_stats),
_coupled_state(nullptr)
{
if (other.is_partition_stats_enabled())
enable_partition_stats();
init_partition_stats();
}
template <bool Add>
......@@ -273,10 +272,7 @@ public:
}
}
if (is_partition_stats_enabled())
get_partition_stats(v).move_vertex(v, r, nr, _g);
get_partition_stats(v).move_vertex(v, r, nr, _g);
}
void add_edge(size_t, size_t, GraphInterface::edge_t&,
......@@ -421,7 +417,6 @@ public:
dS_dl += get_delta_partition_dl(v, r, nr, ea);
if (ea.partition_dl || ea.degree_dl || ea.edges_dl)
{
enable_partition_stats();
auto& ps = get_partition_stats(v);
if (_deg_corr && ea.degree_dl)
dS_dl += ps.get_delta_deg_dl(v, r, nr, _eweight, _g);
......@@ -496,7 +491,6 @@ public:
if (ea.partition_dl)
{
enable_partition_stats();
auto& ps = get_partition_stats(v);
dS += ps.get_delta_partition_dl(v, r, nr, _g);
}
......@@ -848,7 +842,6 @@ public:
if (ea.edges_dl)
{
enable_partition_stats();
size_t actual_B = 0;
for (auto& ps : _partition_stats)
actual_B += ps.get_actual_B();
......@@ -870,8 +863,6 @@ public:
double get_partition_dl()
{
if (!is_partition_stats_enabled())
enable_partition_stats();
double S = 0;
for (auto& ps : _partition_stats)
S += ps.get_partition_dl();
......@@ -880,8 +871,6 @@ public:
double get_deg_dl(int kind)
{
if (!is_partition_stats_enabled())
enable_partition_stats();
double S = 0;
for (auto& ps : _partition_stats)
S += ps.get_deg_dl(kind);
......@@ -930,49 +919,45 @@ public:
{
}
void enable_partition_stats()
void reset_partition_stats()
{
if (_partition_stats.empty())
{
size_t E = num_vertices(_g) / 2;
size_t B = num_vertices(_bg);
auto vi = std::max_element(vertices(_g).first, vertices(_g).second,
[&](auto u, auto v)
{ return this->_pclabel[u] < this->_pclabel[v];});
size_t C = _pclabel[*vi] + 1;
_partition_stats.clear();
_partition_stats.shrink_to_fit();
}
vector<gt_hash_set<size_t>> vcs(C);
vector<size_t> rc(num_vertices(_bg));
for (auto v : vertices_range(_g))
{
vcs[_pclabel[v]].insert(_overlap_stats.get_node(v));
rc[_b[v]] = _pclabel[v];
}
void init_partition_stats()
{
reset_partition_stats();
size_t E = num_vertices(_g) / 2;
size_t B = num_vertices(_bg);
for (size_t c = 0; c < C; ++c)
_partition_stats.emplace_back(_g, _b, vcs[c], E, B,
_eweight, _overlap_stats);
auto vi = std::max_element(vertices(_g).first, vertices(_g).second,
[&](auto u, auto v)
{ return this->_pclabel[u] < this->_pclabel[v];});
size_t C = _pclabel[*vi] + 1;
for (size_t r = 0; r < num_vertices(_bg); ++r)
_partition_stats[rc[r]].get_r(r);
vector<gt_hash_set<size_t>> vcs(C);
vector<size_t> rc(num_vertices(_bg));
for (auto v : vertices_range(_g))
{
vcs[_pclabel[v]].insert(_overlap_stats.get_node(v));
rc[_b[v]] = _pclabel[v];
}
}
void disable_partition_stats()
{
_partition_stats.clear();
}
for (size_t c = 0; c < C; ++c)
_partition_stats.emplace_back(_g, _b, vcs[c], E, B,
_eweight, _overlap_stats);
bool is_partition_stats_enabled() const
{
return !_partition_stats.empty();
for (size_t r = 0; r < num_vertices(_bg); ++r)
_partition_stats[rc[r]].get_r(r);
}
overlap_partition_stats_t& get_partition_stats(size_t v)
{
return _partition_stats[_pclabel[v]];
size_t r = _pclabel[v];
if (r >= _partition_stats.size())
init_partition_stats();
return _partition_stats[r];
}
void couple_state(BlockStateVirtualBase& s, const entropy_args_t& ea)
......@@ -1172,10 +1157,6 @@ public:
void init_mcmc(MCMCState& state)
{
auto c = state._c;
auto& entropy_args = state._entropy_args;
bool dl = (entropy_args.partition_dl ||
entropy_args.degree_dl ||
entropy_args.edges_dl);
if (!std::isinf(c))
{
if (_egroups.empty())
......@@ -1185,11 +1166,6 @@ public:
{
_egroups.clear();
}
if (dl)
enable_partition_stats();
else
disable_partition_stats();
}
bool check_edge_counts(bool emat=true)
......
......@@ -89,7 +89,6 @@ struct Dynamics
get_u_edge<true>(source(e, _u), target(e, _u)) = e;
_E += _eweight[e];
}
_block_state.enable_partition_stats();
}
DynamicsState(const DynamicsState& other)
......@@ -102,9 +101,7 @@ struct Dynamics
_E(other._E),
_dstate(*this, _params),
_xc(_x.get_checked())
{
_block_state.enable_partition_stats();
}
{}
typedef BlockState block_state_t;
BlockState& _block_state;
......
......@@ -199,8 +199,6 @@ struct LatentClosure
// assert(_M[i] > 0);
// }
}
if (_l == 0)
_pbstate.enable_partition_stats();
}
size_t _l;
......@@ -698,10 +696,6 @@ struct LatentClosure
}
}
void enable_partition_stats()
{
}
bool check()
{
typename vprop_map_t<int32_t>::type::unchecked_t M(num_vertices(_gc));
......
......@@ -58,8 +58,6 @@ struct DummyBlockState
DummyBlockState(Graph& g, EW& eweight)
:_g(g), _eweight(eweight){}
constexpr void enable_partition_stats() {}
template <bool Add, class E, class R, class EA>
constexpr double modify_edge_dS(size_t, size_t, E&&, R&&, EA&&)
{
......@@ -124,7 +122,6 @@ struct LatentLayers
_E += x;
_Es[l] += x;
}
bstate.enable_partition_stats();
}
_uea.latent_edges = true;
......
......@@ -100,8 +100,6 @@ struct Measured
_N += (_NP - gE) * _n_default;
_X += (_NP - gE) * _x_default;
_block_state.enable_partition_stats();
}
MeasuredState(const MeasuredState& other)
......@@ -116,9 +114,7 @@ struct Measured
_X(other._X),
_T(other._T),
_M(other._M)
{
_block_state.enable_partition_stats();
}
{}
typedef BlockState block_state_t;
BlockState& _block_state;
......
......@@ -68,7 +68,6 @@ struct Uncertain
_edges.resize(num_vertices(_g));
for (auto e : edges_range(_g))
get_edge<true>(source(e, _g), target(e, _g)) = e;
_block_state.enable_partition_stats();
}
UncertainState(const UncertainState& other)
......@@ -78,9 +77,7 @@ struct Uncertain
_edges(other._edges),
_pe(other._pe),
_E(other._E)
{
_block_state.enable_partition_stats();
}
{}
typedef BlockState block_state_t;
BlockState& _block_state;
......
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