graph_blockmodel.cc 7.14 KB
Newer Older
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2017 Tiago de Paula Peixoto <tiago@skewed.de>
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
//
// 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"

using namespace boost;
using namespace graph_tool;

GEN_DISPATCH(block_state, BlockState, BLOCK_STATE_params)

python::object make_block_state(boost::python::object ostate,
32
                                rng_t& rng);
33

34
35
degs_map_t get_block_degs(GraphInterface& gi, boost::any ab, boost::any aweight,
                          size_t B)
36
37
38
39
{
    degs_map_t degs;
    vmap_t b = boost::any_cast<vmap_t>(ab);
    run_action<>()(gi,
40
                   [&](auto& g, auto& eweight)
41
42
                   {
                       std::vector<gt_hash_map<std::tuple<size_t, size_t>,
43
                                               size_t>> hist(B);
44
45
46
47
48
                       for (auto v : vertices_range(g))
                       {
                           size_t r = b[v];
                           if (r >= hist.size())
                               hist.resize(r + 1);
49
50
                           size_t kin = in_degreeS()(v, g, eweight);
                           size_t kout = out_degreeS()(v, g, eweight);
51
52
53
                           hist[r][std::make_tuple(kin, kout)]++;
                       }

54
                       for (size_t r = 0; r < B; ++r)
55
56
57
58
59
60
61
                       {
                           auto& deg = degs[r];
                           for (auto& kn : hist[r])
                               deg.emplace_back(get<0>(kn.first),
                                                get<1>(kn.first),
                                                kn.second);
                       }
62
63
                   },
                   eweight_tr())(aweight);
64
65
66
67
    return degs;
}

degs_map_t get_weighted_block_degs(GraphInterface& gi, degs_map_t& degs,
68
                                   boost::any ab, size_t B)
69
70
71
72
73
74
75
{
    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>,
76
                                               size_t>> hist(B);
77
78
79
80
81
82
83
84
85
86
87
                       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);
                       }

88
                       for (size_t r = 0; r < B; ++r)
89
90
91
92
93
94
95
96
97
98
99
                       {
                           auto& deg = ndegs[r];
                           for (auto& kn : hist[r])
                               deg.emplace_back(get<0>(kn.first),
                                                get<1>(kn.first),
                                                kn.second);
                       }
                   })();
    return ndegs;
}

100
101
102
103
104
degs_map_t get_empty_degs(GraphInterface& gi)
{
    return degs_map_t(gi.get_num_vertices(false));
}

105
106
107
108

template <class Prop>
boost::any get_any(Prop& p)
{
109
    return boost::any(p);
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
}

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;
}

137
138
void export_sbm_state();

139
140
double spence(double);

141
142
143
144
void export_blockmodel_state()
{
    using namespace boost::python;

145
    export_sbm_state();
146
147
148
149

    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>);

150
151
152
153
154
    class_<entropy_args_t>("entropy_args")
        .def_readwrite("exact", &entropy_args_t::exact)
        .def_readwrite("dense", &entropy_args_t::dense)
        .def_readwrite("multigraph", &entropy_args_t::multigraph)
        .def_readwrite("adjacency", &entropy_args_t::adjacency)
155
        .def_readwrite("deg_entropy", &entropy_args_t::deg_entropy)
156
        .def_readwrite("recs", &entropy_args_t::recs)
157
158
159
        .def_readwrite("partition_dl", &entropy_args_t::partition_dl)
        .def_readwrite("degree_dl", &entropy_args_t::degree_dl)
        .def_readwrite("degree_dl_kind", &entropy_args_t::degree_dl_kind)
160
        .def_readwrite("edges_dl", &entropy_args_t::edges_dl)
161
162
        .def_readwrite("recs_dl", &entropy_args_t::recs_dl)
        .def_readwrite("beta_dl", &entropy_args_t::beta_dl);
163
164
165
166
167
168
169
170

    enum_<deg_dl_kind>("deg_dl_kind")
        .value("ent", deg_dl_kind::ENT)
        .value("uniform", deg_dl_kind::UNIFORM)
        .value("dist", deg_dl_kind::DIST);

    enum_<weight_type>("rec_type")
        .value("none", weight_type::NONE)
171
        .value("count", weight_type::COUNT)
172
173
        .value("real_exponential", weight_type::REAL_EXPONENTIAL)
        .value("real_normal", weight_type::REAL_NORMAL)
174
175
        .value("discrete_geometric", weight_type::DISCRETE_GEOMETRIC)
        .value("discrete_poisson", weight_type::DISCRETE_POISSON)
176
        .value("discrete_binomial", weight_type::DISCRETE_BINOMIAL)
177
        .value("delta_t", weight_type::DELTA_T);
178

179
    def("make_block_state", &make_block_state);
180
181
182

    def("get_block_degs", &get_block_degs);
    def("get_weighted_block_degs", &get_weighted_block_degs);
183
    def("get_empty_degs", &get_empty_degs);
184
185
    class_<degs_map_t>("degs_map_t")
        .def("print", &print_degs)
186
187
        .def("copy", &copy_degs)
        .def("_get_any", &get_any<degs_map_t>);
188
    class_<simple_degs_t>("simple_degs_t")
189
190
        .def("copy", &copy_simple_degs)
        .def("_get_any", &get_any<simple_degs_t>);
191

192
193
194
195
196
197
198
    def("init_q_cache", init_q_cache);
    def("log_q", log_q<size_t>);
    def("q_rec", q_rec);
    def("q_rec_memo", q_rec_memo);
    def("log_q_approx", log_q_approx);
    def("log_q_approx_big", log_q_approx_big);
    def("log_q_approx_small", log_q_approx_small);
199
    def("spence", spence);
200
201
202
203
204
205

    def("positive_w_log_P", positive_w_log_P<size_t>);
    def("signed_w_log_P", signed_w_log_P<size_t>);
    def("geometric_w_log_P", geometric_w_log_P<size_t>);
    def("binomial_w_log_P", binomial_w_log_P<size_t>);
    def("poisson_w_log_P", poisson_w_log_P<size_t>);
206
}