graph_blockmodel_exhaustive.cc 4.67 KB
Newer Older
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006-2016 Tiago de Paula Peixoto <tiago@skewed.de>
//
// 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 <http://www.gnu.org/licenses/>.

#include "graph_tool.hh"
#include "random.hh"

#include <boost/python.hpp>

#include "graph_blockmodel_util.hh"
#include "graph_blockmodel.hh"
#include "graph_blockmodel_exhaustive.hh"
#include "exhaustive_loop.hh"

#include "numpy_bind.hh"

#ifdef HAVE_BOOST_COROUTINE
#include "graph_python_interface.hh"
#include <boost/coroutine/all.hpp>
#endif // HAVE_BOOST_COROUTINE

using namespace boost;
using namespace graph_tool;

GEN_DISPATCH(block_state, BlockState, BLOCK_STATE_params)

template <class State>
GEN_DISPATCH(exhaustive_block_state,
             Exhaustive<State>::template ExhaustiveBlockState,
             EXHAUSTIVE_BLOCK_STATE_params(State))

void do_exhaustive_sweep(python::object oexhaustive_state,
                         python::object oblock_state,
                         python::object callback)
{
    auto dispatch = [&](auto& block_state)
    {
        typedef typename std::remove_reference<decltype(block_state)>::type
            state_t;

        exhaustive_block_state<state_t>::make_dispatch
           (oexhaustive_state,
            [&](auto& s)
            {
                exhaustive_sweep(s,
                                 [&](auto& state)
                                 {
                                     callback(state._S,
                                              state._S_min);
                                 });
            });
    };
    block_state::dispatch(oblock_state, dispatch);
}


python::object do_exhaustive_sweep_iter(python::object oexhaustive_state,
                                        python::object oblock_state)
{
#ifdef HAVE_BOOST_COROUTINE
    auto coro_dispatch = [&](auto& yield)
        {
            auto dispatch = [&](auto& block_state)
            {
                typedef typename std::remove_reference<decltype(block_state)>::type
                    state_t;

                exhaustive_block_state<state_t>::make_dispatch
                   (oexhaustive_state,
                    [&](auto& s)
                    {
                        exhaustive_sweep
                            (s,
                             [&](auto& state)
                             {
                                 yield(python::make_tuple(state._S,
                                                          state._S_min));
                             });
                    });
            };
            block_state::dispatch(oblock_state, dispatch);
        };
    return python::object(CoroGenerator(coro_dispatch));
#else
    throw GraphException("This functionality is not available because boost::coroutine was not found at compile-time");
#endif // HAVE_BOOST_COROUTINE
}

void do_exhaustive_dens(python::object oexhaustive_state,
                        python::object oblock_state,
                        double S_min, double S_max,
                        python::object ohist)
{
    multi_array_ref<uint64_t, 1> hist = get_array<uint64_t, 1>(ohist);
    int N = hist.shape()[0];
    double dS = S_max - S_min;
    auto dispatch = [&](auto& block_state)
    {
        typedef typename std::remove_reference<decltype(block_state)>::type
            state_t;

        exhaustive_block_state<state_t>::make_dispatch
           (oexhaustive_state,
            [&](auto& s)
            {
                exhaustive_sweep(s,
                                 [&](auto& state)
                                 {
                                     auto S = state._S;
                                     int i = round((N - 1) * (S - S_min) / dS);
                                     if (i >= 0 && i < N)
                                         hist[i]++;
                                 });
            });
    };
    block_state::dispatch(oblock_state, dispatch);
}


void export_blockmodel_exhaustive()
{
    using namespace boost::python;
    def("exhaustive_sweep", &do_exhaustive_sweep);
    def("exhaustive_sweep_iter", &do_exhaustive_sweep_iter);
    def("exhaustive_dens", &do_exhaustive_dens);
}