graph_blockmodel.cc 9.83 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
140
141
142
143
144
// 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_state.hh"

using namespace boost;
using namespace graph_tool;

GEN_DISPATCH(block_state, BlockState, BLOCK_STATE_params)

python::object make_block_state(boost::python::object ostate,
                                rng_t& rng)
{
    python::object state;
    block_state::make_dispatch(ostate,
                               [&](auto& s){state = python::object(s);},
                               rng);
    return state;
}

degs_map_t get_block_degs(GraphInterface& gi, boost::any ab)
{
    degs_map_t degs;
    vmap_t b = boost::any_cast<vmap_t>(ab);
    run_action<>()(gi,
                   [&](auto& g)
                   {
                       std::vector<gt_hash_map<std::tuple<size_t, size_t>,
                                               size_t>> hist;
                       for (auto v : vertices_range(g))
                       {
                           size_t r = b[v];
                           if (r >= hist.size())
                               hist.resize(r + 1);
                           size_t kin = in_degreeS()(v, g);
                           size_t kout = out_degreeS()(v, g);
                           hist[r][std::make_tuple(kin, kout)]++;
                       }

                       for (size_t r = 0; r < hist.size(); ++r)
                       {
                           auto& deg = degs[r];
                           for (auto& kn : hist[r])
                               deg.emplace_back(get<0>(kn.first),
                                                get<1>(kn.first),
                                                kn.second);
                       }
                   })();
    return degs;
}

degs_map_t get_weighted_block_degs(GraphInterface& gi, degs_map_t& degs,
                                   boost::any ab)
{
    degs_map_t ndegs;
    vmap_t b = boost::any_cast<vmap_t>(ab);
    run_action<>()(gi,
                   [&](auto& g)
                   {
                       std::vector<gt_hash_map<std::tuple<size_t, size_t>,
                                               size_t>> hist;
                       for (auto v : vertices_range(g))
                       {
                           size_t r = b[v];
                           if (r >= hist.size())
                               hist.resize(r + 1);
                           auto& h = hist[r];
                           auto& ks = degs[v];
                           for (auto& k : ks)
                               h[std::make_tuple(get<0>(k), get<1>(k))] += get<2>(k);
                       }

                       for (size_t r = 0; r < hist.size(); ++r)
                       {
                           auto& deg = ndegs[r];
                           for (auto& kn : hist[r])
                               deg.emplace_back(get<0>(kn.first),
                                                get<1>(kn.first),
                                                kn.second);
                       }
                   })();
    return ndegs;
}


template <class Prop>
boost::any get_any(Prop& p)
{
    return any(p);
}

void print_degs(degs_map_t& degs, size_t B)
{
    for (size_t r = 0; r < B; ++r)
    {
        cout << r << ":: ";
        auto& ks = degs[r];
        for (auto& k : ks)
        {
            cout << "(" << get<0>(k) << ", " << get<1>(k) << "): "
                 << get<2>(k) << "  ";
        }
        cout << endl;
    }
}

degs_map_t copy_degs(degs_map_t& degs)
{
    return degs.copy();
}

simple_degs_t copy_simple_degs(simple_degs_t& degs)
{
    return degs;
}

boost::python::tuple bethe_entropy(GraphInterface& gi, size_t B, boost::any op,
                                   boost::any opv)
{
    typedef vprop_map_t<vector<double>>::type vmap_t;
    vmap_t pv = any_cast<vmap_t>(opv);

    double H=0, sH=0, Hmf=0, sHmf=0;
145
    run_action<>()
146
        (gi,
147
         [&](auto& g, auto p)
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
         {
             for (auto v : vertices_range(g))
             {
                 pv[v].resize(B);
                 for (size_t i = 0; i < B; ++i)
                     pv[v][i] = 0;
             }

             H = Hmf = sH = sHmf =  0;

             for (auto e : edges_range(g))
             {
                 auto u = min(source(e, g), target(e, g));
                 auto v = max(source(e, g), target(e, g));

                 double sum = 0;
                 for (size_t r = 0; r < B; ++r)
                     for (size_t s = 0; s < B; ++s)
                     {
                         size_t i = r + B * s;
                         pv[u][r] += p[e][i];
                         pv[v][s] += p[e][i];
                         sum += p[e][i];
                     }

                 for (size_t i = 0; i < B * B; ++i)
                 {
                     if (p[e][i] == 0)
                         continue;
                     double pi = double(p[e][i]) / sum;
                     H -= pi * log(pi);
                     sH += pow((log(pi) + 1) * sqrt(pi / sum), 2);
                 }
             }

             for (auto v : vertices_range(g))
             {
                 double sum = 0;
                 for (size_t i = 0; i < B; ++i)
                     sum += pv[v][i];
                 for (size_t i = 0; i < B; ++i)
                 {
                     if (pv[v][i] == 0)
                         continue;
                     pv[v][i] /= sum;
                     double pi = pv[v][i];
                     double kt = (1 - double(in_degreeS()(v, g)) - double(out_degree(v, g)));
                     if (kt != 0)
                     {
                         H -= kt * (pi * log(pi));
                         sH += pow(kt * (log(pi) + 1) * sqrt(pi / sum), 2);
                     }

                     Hmf -= pi * log(pi);
                     sHmf += pow((log(pi) + 1) * sqrt(pi / sum), 2);
                 }
             }
205
206
         },
         edge_scalar_vector_properties())(op);
207
208
209
210
211
212
213
214
215
216
217
218
219

    return boost::python::make_tuple(H, sH, Hmf, sHmf);
}


void export_blockmodel_state()
{
    using namespace boost::python;

    block_state::dispatch
        ([&](auto* s)
         {
             typedef typename std::remove_reference<decltype(*s)>::type state_t;
220
221
222
223
224
225
226
227
             void (state_t::*remove_vertex)(size_t) =
                 &state_t::remove_vertex;
             void (state_t::*add_vertex)(size_t, size_t) =
                 &state_t::add_vertex;
             void (state_t::*remove_vertices)(python::object) =
                 &state_t::remove_vertices;
             void (state_t::*add_vertices)(python::object, python::object) =
                 &state_t::add_vertices;
228
229
230
231
             double (state_t::*virtual_move)(size_t, size_t, bool, bool, bool,
                                             bool, bool) =
                 &state_t::virtual_move;
             size_t (state_t::*sample_block)(size_t, double, vector<size_t>&,
232
233
                                             rng_t&) =
                 &state_t::sample_block;
234
             double (state_t::*get_move_prob)(size_t, size_t, size_t, double,
235
236
237
238
239
240
                                              bool) =
                 &state_t::get_move_prob;
             void (state_t::*merge_vertices)(size_t, size_t) =
                 &state_t::merge_vertices;
             void (state_t::*set_partition)(boost::any&) =
                 &state_t::set_partition;
241

242
243
             class_<state_t> c(name_demangle(typeid(state_t).name()).c_str(),
                               no_init);
244
245
246
247
             c.def("remove_vertex", remove_vertex)
                 .def("add_vertex", add_vertex)
                 .def("remove_vertices", remove_vertices)
                 .def("add_vertices", add_vertices)
248
                 .def("move_vertex", &state_t::move_vertex)
249
                 .def("set_partition", set_partition)
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
                 .def("virtual_move", virtual_move)
                 .def("merge_vertices", merge_vertices)
                 .def("sample_block", sample_block)
                 .def("entropy", &state_t::entropy)
                 .def("get_partition_dl", &state_t::get_partition_dl)
                 .def("get_deg_dl", &state_t::get_deg_dl)
                 .def("get_move_prob", get_move_prob)
                 .def("enable_partition_stats",
                      &state_t::enable_partition_stats)
                 .def("disable_partition_stats",
                      &state_t::disable_partition_stats)
                 .def("is_partition_stats_enabled",
                      &state_t::is_partition_stats_enabled);
         });

    class_<vcmap_t>("unity_vprop_t").def("_get_any", &get_any<vcmap_t>);
    class_<ecmap_t>("unity_eprop_t").def("_get_any", &get_any<ecmap_t>);

    def("make_block_state", &make_block_state);

    class_<std::true_type>("true_type");
    class_<std::false_type>("false_type");

    def("get_block_degs", &get_block_degs);
    def("get_weighted_block_degs", &get_weighted_block_degs);
    class_<degs_map_t>("degs_map_t")
        .def("print", &print_degs)
        .def("copy", &copy_degs);
    class_<simple_degs_t>("simple_degs_t")
        .def("copy", &copy_simple_degs);

    def("bethe_entropy", &bethe_entropy);
}