graph_blockmodel_covariates.cc 34.8 KB
Newer Older
 Tiago Peixoto committed Apr 10, 2015 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 ``````// graph-tool -- a general graph modification and manipulation thingy // // Copyright (C) 2006-2016 Tiago de Paula Peixoto // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 3 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . #define BOOST_PYTHON_MAX_ARITY 46 #include #include #include #include #include "numpy_bind.hh" #include #include #include "graph_filtering.hh" #include "graph.hh" #include "graph_selectors.hh" #include "graph_properties.hh" #include "graph_util.hh" #include "graph_python_interface.hh" #include "random.hh" #include "config.h" #include "graph_blockmodel_covariates.hh" #include "graph_blockmodel.hh" #include "graph_blockmodel_overlap.hh" using namespace graph_tool; using namespace std; template struct cov_move_sweep_dispatch { cov_move_sweep_dispatch(Eprop ce, VVprop cv, VVprop vmap, vector>& eweight, vector>& vweight, vector>& egroups, vector>& esrcpos, vector>& etgtpos, `````` Tiago Peixoto committed Jul 06, 2015 58 `````` Vprop& label, vector& vlist, `````` Tiago Peixoto committed Aug 25, 2015 59 `````` vector& block_list, `````` Tiago Peixoto committed Jul 06, 2015 60 `````` vector& target_blocks, bool deg_corr, `````` Tiago Peixoto committed Apr 10, 2015 61 62 63 64 `````` bool dense, bool multigraph, double beta, bool sequential, bool parallel, bool random_move, bool node_coherent, bool confine_layers, double c, bool verbose, size_t meindex, `````` Tiago Peixoto committed Nov 05, 2015 65 `````` vector edge_index_range, size_t nmerges, `````` Tiago Peixoto committed Apr 10, 2015 66 67 68 69 70 71 72 73 74 75 76 77 78 `````` size_t niter, Vprop merge_map, vector>& partition_stats, vector>& overlap_partition_stats, vector>& overlap_stats, vector& master, vector& slave, rng_t& rng, double& S, size_t& nmoves, vector>& bgi, BMap& block_map, vector>& block_rmap, vector>>& free_blocks, size_t B) : ce(ce), cv(cv), vmap(vmap), eweight(eweight), vweight(vweight), `````` Tiago Peixoto committed Jul 06, 2015 79 `````` oegroups(egroups), esrcpos(esrcpos), etgtpos(etgtpos), label(label), `````` Tiago Peixoto committed Aug 25, 2015 80 `````` vlist(vlist), block_list(block_list), target_blocks(target_blocks), `````` Tiago Peixoto committed Apr 10, 2015 81 82 83 84 `````` deg_corr(deg_corr), dense(dense), multigraph(multigraph), beta(beta), sequential(sequential), parallel(parallel), random_move(random_move), node_coherent(node_coherent), confine_layers(confine_layers), c(c), verbose(verbose), meindex(meindex), `````` Tiago Peixoto committed Nov 05, 2015 85 `````` edge_index_range(edge_index_range), `````` Tiago Peixoto committed Apr 10, 2015 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 `````` nmerges(nmerges), niter(niter), merge_map(merge_map), partition_stats(partition_stats), overlap_partition_stats(overlap_partition_stats), overlap_stats(overlap_stats), master(master), slave(slave), rng(rng), S(S), nmoves(nmoves), bgi(bgi), block_map(block_map), block_rmap(block_rmap), free_blocks(free_blocks), B(B) {} Eprop ce; VVprop cv; VVprop vmap; vector>& eweight; vector>& vweight; vector> oegroups; vector>& esrcpos; vector>& etgtpos; Vprop& label; size_t n; `````` Tiago Peixoto committed Jul 06, 2015 105 `````` vector& vlist; `````` Tiago Peixoto committed Aug 25, 2015 106 `````` vector& block_list; `````` Tiago Peixoto committed Jul 06, 2015 107 `````` vector& target_blocks; `````` Tiago Peixoto committed Apr 10, 2015 108 109 110 111 112 113 114 115 116 117 118 119 `````` bool deg_corr; bool dense; bool multigraph; double beta; bool sequential; bool parallel; bool random_move; bool node_coherent; bool confine_layers; double c; bool verbose; size_t meindex; `````` Tiago Peixoto committed Nov 05, 2015 120 `````` vector edge_index_range; `````` Tiago Peixoto committed Apr 10, 2015 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 `````` size_t nmerges; size_t niter; Vprop merge_map; vector>& partition_stats; vector>& overlap_partition_stats; vector>& overlap_stats; vector master; vector slave; rng_t& rng; double& S; size_t& nmoves; vector>& bgi; BMap& block_map; vector>& block_rmap; vector>>& free_blocks; size_t B; template void operator()(vector>& mrs, vector>& mrp, vector>& mrm, vector>& wr, Vprop& b, vector>& bs, `````` Tiago Peixoto committed Jul 17, 2015 144 145 `````` vector>& bgis, Graph& g, `````` Tiago Peixoto committed Apr 10, 2015 146 147 148 149 150 151 152 153 `````` vector>& ags, vector>& emat, vector>& sampler, vector>& cavity_sampler, bool weighted) const { vector> gs; for (GraphInterface& ag : ags) `````` Tiago Peixoto committed Jan 13, 2016 154 `````` gs.push_back(any_cast>(ag.get_graph_view())); `````` Tiago Peixoto committed Apr 10, 2015 155 156 157 158 159 `````` if (is_directed::apply::type::value) { vector> bgs; for (GraphInterface& bgi : bgis) `````` Tiago Peixoto committed Sep 13, 2015 160 `````` bgs.push_back(bgi.get_graph()); `````` Tiago Peixoto committed Apr 10, 2015 161 162 163 164 165 166 167 `````` dispatch(mrs, mrp, mrm, wr, b, bs, g, gs, emat, sampler, cavity_sampler, bgs, weighted); } else { vector> ubgs; for (GraphInterface& bgi : bgis) `````` Tiago Peixoto committed Sep 13, 2015 168 `````` ubgs.push_back(UndirectedAdaptor(bgi.get_graph())); `````` Tiago Peixoto committed Apr 10, 2015 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 `````` vector>> rubgs; for (auto& bg : ubgs) rubgs.push_back(bg); dispatch(mrs, mrp, mrm, wr, b, bs, g, gs, emat, sampler, cavity_sampler, rubgs, weighted); } } template void dispatch(vector>& mrs, vector>& mrp, vector>& mrm, vector>& wr, Vprop& b, vector>& bs, Graph& g, vector>& gs, vector>& aemat, vector>& asampler, vector>& acavity_sampler, vector>& bg, bool weighted) const { if (weighted) { typedef typename property_map_type::apply::edge_descriptor, bool> >, GraphInterface::vertex_index_map_t>::type vemap_t; vector> egroups; for (auto& eg : oegroups) egroups.push_back(any_cast(eg)); try { typedef typename get_emat_t::apply::type emat_t; vector> emat; for (auto& m : aemat) emat.push_back(any_cast(m)); size_t B = num_vertices(bg[0].get()); size_t max_BE = is_directed::apply::type::value ? B * B : (B * (B + 1)) / 2; vector umrs; for (auto& m : mrs) umrs.push_back(m.get().get_unchecked(max_BE)); vector> rumrs; for (auto& m : umrs) rumrs.push_back(m); dispatch(rumrs, mrp, mrm, wr, b, bs, g, gs, asampler, acavity_sampler, bg, egroups, emat); } catch (bad_any_cast) { typedef typename get_ehash_t::apply::type emat_t; vector> emat; for (auto& m : aemat) emat.push_back(any_cast(m)); dispatch(mrs, mrp, mrm, wr, b, bs, g, gs, asampler, acavity_sampler, bg, egroups, emat); } } else { typedef typename property_map_type::apply::edge_descriptor, bool> >, GraphInterface::vertex_index_map_t>::type vemap_t; vector> egroups; for (auto& eg : oegroups) egroups.push_back(any_cast(eg)); try { typedef typename get_emat_t::apply::type emat_t; vector> emat; for (auto& m : aemat) emat.push_back(any_cast(m)); dispatch(mrs, mrp, mrm, wr, b, bs, g, gs, asampler, acavity_sampler, bg, egroups, emat); } catch (bad_any_cast) { typedef typename get_ehash_t::apply::type emat_t; vector> emat; for (auto& m : aemat) emat.push_back(any_cast(m)); dispatch(mrs, mrp, mrm, wr, b, bs, g, gs, asampler, acavity_sampler, bg, egroups, emat); } } } template void dispatch(vector>& mrs, vector>& mrp, vector>& mrm, vector>& wr, Vprop& b, vector>& bs, Graph& g, vector>& gs, vector>& asampler, vector>& acavity_sampler, vector>& bgs, vector>& egroups, vector>& emat) const { typedef typename graph_traits::vertex_descriptor vertex_t; typedef typename property_map::type vindex_map_t; typedef typename property_map_type::apply, vindex_map_t>::type::unchecked_t sampler_map_t; vector> sampler; for (auto& s : asampler) sampler.push_back(any_cast(s.get())); vector> cavity_sampler; for (auto& s : acavity_sampler) cavity_sampler.push_back(any_cast(s.get())); bool overlap = overlap_stats[0].get().is_enabled(); if (!overlap) { typedef BlockState state_t; vector states; vector> m_entries; overlap_stats_t ostats; for (size_t i = 0; i < mrs.size(); ++i) { `````` Tiago Peixoto committed Nov 05, 2015 303 `````` size_t eidx = random_move ? 1 : edge_index_range[i]; `````` Tiago Peixoto committed Apr 10, 2015 304 305 `````` state_t state = make_block_state(gs[i].get(), `````` Tiago Peixoto committed Nov 05, 2015 306 `````` eweight[i].get().get_unchecked(edge_index_range[i]), `````` Tiago Peixoto committed Apr 10, 2015 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 `````` vweight[i].get().get_unchecked(num_vertices(gs[i].get())), bs[i].get().get_unchecked(num_vertices(gs[i].get())), bgs[i].get(), emat[i].get(), mrs[i].get(), mrp[i].get().get_unchecked(num_vertices(bgs[i].get())), mrm[i].get().get_unchecked(num_vertices(bgs[i].get())), wr[i].get().get_unchecked(num_vertices(bgs[i].get())), egroups[i].get().get_unchecked(num_vertices(bgs[i].get())), esrcpos[i].get().get_unchecked(eidx), etgtpos[i].get().get_unchecked(eidx), sampler[i].get(), cavity_sampler[i].get(), partition_stats[i].get(), ostats, block_map[i], block_rmap[i].get().get_unchecked(num_vertices(bgs[i].get())), free_blocks[i].get(), master[i], slave[i], false); states.push_back(state); m_entries.emplace_back(num_vertices(bgs[i].get())); } `````` Tiago Peixoto committed Jul 06, 2015 330 331 332 333 334 335 336 337 `````` if (target_blocks.empty()) { move_sweep(states, m_entries, wr[0].get().get_unchecked(B), b.get_unchecked(num_vertices(g)), cv.get_unchecked(num_vertices(g)), vmap.get_unchecked(num_vertices(g)), label.get_unchecked(B), `````` Tiago Peixoto committed Aug 25, 2015 338 `````` vlist, block_list, deg_corr, `````` Tiago Peixoto committed Jul 06, 2015 339 `````` dense, multigraph, beta, `````` Tiago Peixoto committed Nov 05, 2015 340 `````` eweight[0].get().get_unchecked(edge_index_range[0]), `````` Tiago Peixoto committed Jul 06, 2015 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 `````` vweight[0].get().get_unchecked(num_vertices(g)), g, sequential, parallel, random_move, c, nmerges, merge_map.get_unchecked(num_vertices(g)), niter, B, verbose, rng, S, nmoves); } else { auto ub = b.get_unchecked(num_vertices(g)); for (size_t i = 0; i < vlist.size(); ++i) { size_t v = vlist[i]; size_t s = target_blocks[i]; S += virtual_move(v, s, ub, cv, vmap, states, m_entries, dense, deg_corr, multigraph); move_vertex(v, s, ub, cv, vmap, deg_corr, states, not random_move); nmoves++; } } `````` Tiago Peixoto committed Apr 10, 2015 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 `````` } else { typedef BlockState state_t; vector states; for (size_t i = 0; i < mrs.size(); ++i) { `````` Tiago Peixoto committed Nov 05, 2015 378 `````` size_t eidx = random_move ? 1 : edge_index_range[i]; `````` Tiago Peixoto committed Apr 10, 2015 379 380 `````` state_t state = make_block_state(gs[i].get(), `````` Tiago Peixoto committed Nov 05, 2015 381 `````` eweight[i].get().get_unchecked(edge_index_range[i]), `````` Tiago Peixoto committed Apr 10, 2015 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 `````` vweight[i].get().get_unchecked(num_vertices(gs[i].get())), bs[i].get().get_unchecked(num_vertices(gs[i].get())), bgs[i].get(), emat[i].get(), mrs[i].get(), mrp[i].get().get_unchecked(num_vertices(bgs[i].get())), mrm[i].get().get_unchecked(num_vertices(bgs[i].get())), wr[i].get().get_unchecked(num_vertices(bgs[i].get())), egroups[i].get().get_unchecked(num_vertices(bgs[i].get())), esrcpos[i].get().get_unchecked(eidx), etgtpos[i].get().get_unchecked(eidx), sampler[i].get(), cavity_sampler[i].get(), overlap_partition_stats[i].get(), overlap_stats[i].get(), block_map[i], block_rmap[i].get().get_unchecked(num_vertices(bgs[i].get())), free_blocks[i].get(), master[i], slave[i], false); states.push_back(state); } vector> m_entries(states.size()); `````` Tiago Peixoto committed Jul 06, 2015 406 `````` if (target_blocks.empty()) `````` Tiago Peixoto committed Apr 10, 2015 407 `````` { `````` Tiago Peixoto committed Jul 06, 2015 408 `````` if (nmerges == 0) `````` Tiago Peixoto committed Apr 10, 2015 409 `````` { `````` Tiago Peixoto committed Jul 06, 2015 410 411 `````` if (!node_coherent) { `````` Tiago Peixoto committed Aug 25, 2015 412 413 `````` move_sweep_overlap(states, m_entries, overlap_stats[0].get(), `````` Tiago Peixoto committed Jul 06, 2015 414 `````` wr[0].get().get_unchecked(B), `````` Tiago Peixoto committed Aug 25, 2015 415 416 417 418 419 420 421 422 `````` b.get_unchecked(num_vertices(g)), cv, vmap, label.get_unchecked(B), vlist, block_list, deg_corr, dense, multigraph, beta, vweight[0].get().get_unchecked(num_vertices(g)), g, sequential, parallel, random_move, c, niter, B, verbose, rng, S, nmoves); `````` Tiago Peixoto committed Jul 06, 2015 423 424 425 426 427 428 `````` } else { vector> m_entries; for (auto& state : states) m_entries.emplace_back(num_vertices(state.bg)); `````` Tiago Peixoto committed Aug 25, 2015 429 430 `````` coherent_move_sweep_overlap(states, m_entries, overlap_stats[0].get(), `````` Tiago Peixoto committed Jul 06, 2015 431 `````` wr[0].get().get_unchecked(B), `````` Tiago Peixoto committed Aug 25, 2015 432 433 434 435 436 437 438 439 440 `````` b.get_unchecked(num_vertices(g)), cv, vmap, label.get_unchecked(B), vlist, block_list, deg_corr, dense, multigraph, beta, vweight[0].get().get_unchecked(num_vertices(g)), g, sequential, random_move, c, confine_layers, niter, B, rng, S, nmoves); `````` Tiago Peixoto committed Jul 06, 2015 441 `````` } `````` Tiago Peixoto committed Apr 10, 2015 442 443 444 `````` } else { `````` Tiago Peixoto committed Jul 06, 2015 445 446 447 448 `````` merge_sweep_overlap(states, m_entries, overlap_stats[0].get(), wr[0].get().get_unchecked(B), b.get_unchecked(num_vertices(g)), ce, cv, vmap, label.get_unchecked(B), vlist, `````` Tiago Peixoto committed Aug 25, 2015 449 `````` block_list, deg_corr, dense, multigraph, g, `````` Tiago Peixoto committed Jul 06, 2015 450 451 `````` random_move, confine_layers, nmerges, niter, B, rng, S, nmoves); `````` Tiago Peixoto committed Apr 10, 2015 452 453 454 455 `````` } } else { `````` Tiago Peixoto committed Jul 06, 2015 456 457 458 459 460 461 462 463 464 465 466 467 `````` half_edge_neighbour_policy npolicy(g); auto ub = b.get_unchecked(num_vertices(g)); for (size_t i = 0; i < vlist.size(); ++i) { size_t v = vlist[i]; size_t s = target_blocks[i]; S += virtual_move(v, s, ub, cv, vmap, states, m_entries, dense, deg_corr, multigraph, npolicy); move_vertex(v, s, ub, cv, vmap, deg_corr, states, not random_move, npolicy); nmoves++; } `````` Tiago Peixoto committed Apr 10, 2015 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 `````` } } } }; template vector from_list(boost::python::object list) { vector v; for (int i = 0; i < boost::python::len(list); ++i) v.push_back(boost::python::extract(list[i])()); return v; }; template vector> from_rlist(boost::python::object list) { vector> v; for (int i = 0; i < boost::python::len(list); ++i) v.emplace_back(boost::python::extract(list[i])()); return v; }; template vector> from_any_list(boost::python::object list) { vector> v; for (int i = 0; i < boost::python::len(list); ++i) v.emplace_back(any_cast(boost::python::extract(list[i])())); return v; }; boost::python::object do_cov_move_sweep(GraphInterface& gi, boost::any& oce, boost::any& ocv, boost::any& ovmap, boost::python::object& ogis, boost::python::object& obgi, boost::python::object& oemat, boost::python::object& osampler, boost::python::object& ocavity_sampler, boost::python::object& omrs, boost::python::object& omrp, boost::python::object& omrm, boost::python::object& owr, boost::any& ob, boost::python::object& obs, bmap_t& bmap, boost::python::object& obrmap, boost::python::object& ofree_blocks, boost::python::object& omaster, boost::python::object& oslave, `````` Tiago Peixoto committed Jul 06, 2015 520 521 `````` boost::any& olabel, boost::python::object& ovlist, `````` Tiago Peixoto committed Apr 10, 2015 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 `````` bool deg_corr, bool dense, bool multigraph, boost::python::object& oeweight, boost::python::object& ovweight, boost::python::object& oegroups, boost::python::object& oesrcpos, boost::python::object& oetgtpos, double beta, bool sequential, bool parallel, bool random_move, boost::python::object onode_coherent, double c, bool weighted, size_t nmerges, boost::any omerge_map, size_t niter, size_t B, boost::python::object& opartition_stats, boost::python::object& ooverlap_partition_stats, boost::python::object& ooverlap_stats, bool verbose, rng_t& rng) { typedef property_map_type::apply::type vmap_t; typedef property_map_type::apply::type emap_t; typedef property_map_type::apply, GraphInterface::vertex_index_map_t>::type vvmap_t; auto mrs = from_any_list(omrs); auto mrp = from_any_list(omrp); auto mrm = from_any_list(omrm); auto wr = from_any_list(owr); vmap_t b = any_cast(ob); auto bs = from_any_list(obs); auto brmap = from_any_list(obrmap); auto free_blocks = from_rlist>(ofree_blocks); auto eweight = from_any_list(oeweight); auto vweight = from_any_list(ovweight); auto egroups = from_rlist(oegroups); auto esrcpos = from_any_list(oesrcpos); auto etgtpos = from_any_list(oetgtpos); vmap_t label = any_cast(olabel); emap_t ce = any_cast(oce); vvmap_t cv = any_cast(ocv); vvmap_t vmap = any_cast(ovmap); double S = 0; size_t nmoves = 0; vmap_t merge_map = any_cast(omerge_map); auto gis = from_rlist(ogis); vector eidx; for (GraphInterface& g : gis) `````` Tiago Peixoto committed Nov 05, 2015 580 `````` eidx.push_back(g.get_edge_index_range()); `````` Tiago Peixoto committed Apr 10, 2015 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 `````` auto bgi = from_rlist(obgi); auto partition_stats = from_rlist(opartition_stats); auto overlap_partition_stats = from_rlist(ooverlap_partition_stats); auto overlap_stats = from_rlist(ooverlap_stats); auto emat = from_rlist(oemat); auto sampler = from_rlist(osampler); auto cavity_sampler = from_rlist(ocavity_sampler); auto master = from_list(omaster); auto slave = from_list(oslave); `````` Tiago Peixoto committed Jul 06, 2015 596 `````` vector& vlist = boost::python::extract&>(ovlist[0]); `````` Tiago Peixoto committed Aug 25, 2015 597 598 `````` vector& block_list = boost::python::extract&>(ovlist[1]); vector& target_blocks = boost::python::extract&>(ovlist[2]); `````` Tiago Peixoto committed Apr 10, 2015 599 600 601 602 `````` bool node_coherent = python::extract(onode_coherent[0]); bool confine_layers = python::extract(onode_coherent[1]); `````` Tiago Peixoto committed Jan 12, 2016 603 `````` run_action() `````` Tiago Peixoto committed Apr 10, 2015 604 605 `````` (gi, std::bind(cov_move_sweep_dispatch (ce, cv, vmap, eweight, vweight, egroups, esrcpos, etgtpos, `````` Tiago Peixoto committed Aug 25, 2015 606 607 608 `````` label, vlist, block_list, target_blocks, deg_corr, dense, multigraph, beta, sequential, parallel, random_move, node_coherent, confine_layers, c, verbose, `````` Tiago Peixoto committed Nov 05, 2015 609 `````` gi.get_edge_index_range(), eidx, nmerges, niter, merge_map, `````` Tiago Peixoto committed Apr 10, 2015 610 611 612 `````` partition_stats, overlap_partition_stats, overlap_stats, master, slave, rng, S, nmoves, bgi, bmap, brmap, free_blocks, B), std::ref(mrs), std::ref(mrp), std::ref(mrm), std::ref(wr), `````` Tiago Peixoto committed Dec 24, 2015 613 `````` std::ref(b), std::ref(bs), std::ref(bgi), std::placeholders::_1, std::ref(gis), `````` Tiago Peixoto committed Apr 10, 2015 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 `````` std::ref(emat), std::ref(sampler), std::ref(cavity_sampler), weighted))(); return boost::python::make_tuple(S, nmoves); } struct covariate_entropy { template void operator()(Graph& bg, Emap mrs, double& S) const { for (auto e : edges_range(bg)) S -= lgamma_fast(mrs[e] + 1); } }; double do_covariate_entropy(GraphInterface& gi, boost::any omrs) { typedef property_map_type::apply::type emap_t; emap_t mrs = any_cast(omrs); double S = 0; run_action<>() (gi, std::bind(covariate_entropy(), `````` Tiago Peixoto committed Dec 24, 2015 639 `````` std::placeholders::_1, mrs, std::ref(S)))(); `````` Tiago Peixoto committed Apr 10, 2015 640 641 642 643 644 645 `````` return S; } // void do_create_echash(GraphInterface& gi, size_t l, size_t L, // boost::any& emat_orig, boost::any& emat) // { `````` Tiago Peixoto committed Dec 24, 2015 646 ``````// run_action<>()(gi, std::bind(create_echash(), std::placeholders::_1, l, L, `````` Tiago Peixoto committed Apr 10, 2015 647 648 649 650 651 652 653 654 655 ``````// std::ref(emat_orig), std::ref(emat)))(); // } void do_ec_hist(GraphInterface& gi, boost::any& aevc, boost::any& aec) { typedef property_map_type::apply::type emap_t; typename emap_t::unchecked_t ec = `````` Tiago Peixoto committed Nov 05, 2015 656 `````` any_cast(aec).get_unchecked(gi.get_edge_index_range()); `````` Tiago Peixoto committed Dec 24, 2015 657 658 `````` run_action<>()(gi, std::bind(ec_hist(), std::placeholders::_1, std::placeholders::_2, std::ref(ec)), `````` Tiago Peixoto committed Apr 10, 2015 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 `````` edge_properties())(aevc); } void do_split_graph(GraphInterface& gi, boost::any& aec, boost::any& ab, boost::any& aeweight, boost::any& avweight, boost::any& avc, boost::any& avmap, boost::python::object& ous, boost::python::object& oub, boost::python::object& oueweight, boost::python::object& ouvweight, bmap_t& bmap, boost::python::object& obrmap, boost::python::object& ouvmap) { typedef property_map_type::apply::type vmap_t; typedef property_map_type::apply, GraphInterface::vertex_index_map_t>::type vvmap_t; typedef property_map_type::apply::type emap_t; emap_t& ec = any_cast(aec); vmap_t& b = any_cast(ab); vmap_t& vweight = any_cast(avweight); emap_t& eweight = any_cast(aeweight); vvmap_t& vc = any_cast(avc); vvmap_t& vmap = any_cast(avmap); auto us = from_rlist(ous); auto ub = from_any_list(oub); auto uvweight = from_any_list(ouvweight); auto ueweight = from_any_list(oueweight); auto rbmap = from_any_list(obrmap); auto uvmap = from_any_list(ouvmap); `````` Tiago Peixoto committed Dec 24, 2015 698 `````` run_action<>()(gi, std::bind(split_graph(), std::placeholders::_1, `````` Tiago Peixoto committed Jul 17, 2015 699 700 `````` std::ref(ec), std::ref(b), std::ref(eweight), `````` Tiago Peixoto committed Apr 10, 2015 701 702 703 704 705 706 707 708 `````` std::ref(vweight), std::ref(vc), std::ref(vmap), std::ref(us), std::ref(ub), std::ref(uvweight), std::ref(ueweight), std::ref(bmap), std::ref(rbmap), std::ref(uvmap)))(); } `````` Tiago Peixoto committed Apr 27, 2015 709 710 711 712 713 714 715 716 717 718 ``````bool bmap_has(const bmap_t& bmap, size_t c, size_t r) { if (c > bmap.size()) throw GraphException("invalid covariate value:" + lexical_cast(c)); auto iter = bmap[c].find(r); if (iter == bmap[c].end()) return false; return true; } `````` Tiago Peixoto committed Apr 10, 2015 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 ``````size_t bmap_get(const bmap_t& bmap, size_t c, size_t r) { if (c > bmap.size()) throw GraphException("invalid covariate value:" + lexical_cast(c)); auto iter = bmap[c].find(r); if (iter == bmap[c].end()) throw GraphException("no mapping for block " + lexical_cast(r) + " in layer " + lexical_cast(c)); return iter->second; } void bmap_set(bmap_t& bmap, size_t c, size_t r, size_t r_u) { if (c > bmap.size()) throw GraphException("invalid covariate value:" + lexical_cast(c)); bmap[c][r] = r_u; } void bmap_del_c(bmap_t& bmap, size_t c) { if (c > bmap.size()) throw GraphException("invalid covariate value:" + lexical_cast(c)); bmap.erase(bmap.begin() + c); } bmap_t bmap_copy(const bmap_t& bmap) { return bmap; } void export_blockmodel_covariate() { boost::python::class_("bmap_t") `````` Tiago Peixoto committed Apr 27, 2015 753 `````` .def("has", bmap_has) `````` Tiago Peixoto committed Apr 10, 2015 754 755 756 757 758 759 760 761 762 763 764 `````` .def("get", bmap_get) .def("set", bmap_set) .def("del_c", bmap_del_c) .def("copy", bmap_copy); boost::python::def("cov_move_sweep", do_cov_move_sweep); boost::python::def("covariate_entropy", do_covariate_entropy); // boost::python::def("create_echash", do_create_echash); boost::python::def("ec_hist", do_ec_hist); boost::python::def("split_graph", do_split_graph); }``````