graph_inference.cc 8.29 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-2020 Tiago de Paula Peixoto <tiago@skewed.de>
4
//
5 6 7 8
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
9
//
10 11 12 13
// 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 Lesser General Public License for more
// details.
14
//
15
// You should have received a copy of the GNU Lesser General Public License
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
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "graph_tool.hh"

#include <boost/python.hpp>
#include "numpy_bind.hh"
#include "hash_map_wrap.hh"

using namespace std;
using namespace boost;
using namespace graph_tool;

template <class Value>
void vector_map(boost::python::object ovals, boost::python::object omap)
{
    multi_array_ref<Value,1> vals = get_array<Value,1>(ovals);
    multi_array_ref<Value,1> map = get_array<Value,1>(omap);

    size_t pos = 0;
    for (size_t i = 0; i < vals.size(); ++i)
    {
        Value v = vals[i];
        if (map[v] == -1)
            map[v] = pos++;
        vals[i] = map[v];
    }
}

template <class Value>
void vector_continuous_map(boost::python::object ovals)
{
    multi_array_ref<Value,1> vals = get_array<Value,1>(ovals);
    gt_hash_map<Value, size_t> map;

    for (size_t i = 0; i < vals.size(); ++i)
    {
        Value v = vals[i];
        auto iter = map.find(v);
        if (iter == map.end())
            iter = map.insert(make_pair(v, map.size())).first;
        vals[i] = iter->second;
    }
}

template <class Value>
void vector_rmap(boost::python::object ovals, boost::python::object omap)
{
    multi_array_ref<Value,1> vals = get_array<Value,1>(ovals);
    multi_array_ref<Value,1> map = get_array<Value,1>(omap);

    for (size_t i = 0; i < vals.size(); ++i)
    {
        map[vals[i]] = i;
    }
}

extern void export_blockmodel_state();
extern void export_blockmodel_mcmc();
extern void export_blockmodel_multicanonical();
75
extern void export_blockmodel_multicanonical_multiflip();
76
extern void export_blockmodel_multiflip_mcmc();
77 78 79 80 81 82
extern void export_blockmodel_merge();
extern void export_blockmodel_gibbs();
extern void export_overlap_blockmodel_state();
extern void export_overlap_blockmodel_mcmc();
extern void export_overlap_blockmodel_mcmc_bundled();
extern void export_overlap_blockmodel_multicanonical();
83
extern void export_overlap_blockmodel_multicanonical_multiflip();
84
extern void export_overlap_blockmodel_multiflip_mcmc();
85 86 87 88 89 90 91
extern void export_overlap_blockmodel_gibbs();
extern void export_overlap_blockmodel_vacate();
extern void export_layered_blockmodel_state();
extern void export_layered_blockmodel_mcmc();
extern void export_layered_blockmodel_merge();
extern void export_layered_blockmodel_gibbs();
extern void export_layered_blockmodel_multicanonical();
92
extern void export_layered_blockmodel_multicanonical_multiflip();
93
extern void export_layered_blockmodel_multiflip_mcmc();
94 95 96 97 98
extern void export_layered_overlap_blockmodel_state();
extern void export_layered_overlap_blockmodel_mcmc();
extern void export_layered_overlap_blockmodel_bundled_mcmc();
extern void export_layered_overlap_blockmodel_gibbs();
extern void export_layered_overlap_blockmodel_multicanonical();
99
extern void export_layered_overlap_blockmodel_multicanonical_multiflip();
100
extern void export_layered_overlap_blockmodel_multiflip_mcmc();
101
extern void export_layered_overlap_blockmodel_vacate();
102
extern void export_em_blockmodel_state();
103 104 105 106
extern void export_blockmodel_exhaustive();
extern void export_overlap_blockmodel_exhaustive();
extern void export_layered_blockmodel_exhaustive();
extern void export_layered_overlap_blockmodel_exhaustive();
107 108 109 110
extern void export_uncertain_state();
extern void export_uncertain_mcmc();
extern void export_measured_state();
extern void export_measured_mcmc();
111 112 113 114 115 116 117
extern void export_epidemics_state();
extern void export_epidemics_mcmc();
extern void export_epidemics_mcmc_r();
extern void export_cising_glauber_state();
extern void export_cising_glauber_mcmc();
extern void export_ising_glauber_state();
extern void export_ising_glauber_mcmc();
118
extern void export_marginals();
119
extern void export_modularity();
120
extern void export_latent_multigraph();
121 122 123 124 125 126
extern void export_pseudo_cising_state();
extern void export_pseudo_cising_mcmc();
extern void export_pseudo_cising_mcmc_h();
extern void export_pseudo_ising_state();
extern void export_pseudo_ising_mcmc();
extern void export_pseudo_ising_mcmc_h();
Tiago Peixoto's avatar
Tiago Peixoto committed
127 128 129
extern void export_vi_center_state();
extern void export_vi_center_mcmc();
extern void export_vi_multiflip_mcmc();
130 131 132
extern void export_rmi_center_state();
extern void export_rmi_center_mcmc();
extern void export_rmi_multiflip_mcmc();
133 134 135 136
extern void export_partition_mode();
extern void export_mode_cluster_state();
extern void export_mode_cluster_mcmc();
extern void export_mode_cluster_multiflip_mcmc();
137 138 139
extern void export_pp_state();
extern void export_pp_mcmc();
extern void export_pp_multiflip_mcmc();
140 141 142
extern void export_modularity_state();
extern void export_modularity_mcmc();
extern void export_modularity_multiflip_mcmc();
143 144 145 146

BOOST_PYTHON_MODULE(libgraph_tool_inference)
{
    using namespace boost::python;
147
    docstring_options dopt(true, false);
148 149 150
    export_blockmodel_state();
    export_blockmodel_mcmc();
    export_blockmodel_multicanonical();
151
    export_blockmodel_multicanonical_multiflip();
152
    export_blockmodel_multiflip_mcmc();
153 154 155 156 157 158
    export_blockmodel_merge();
    export_blockmodel_gibbs();
    export_overlap_blockmodel_state();
    export_overlap_blockmodel_mcmc();
    export_overlap_blockmodel_mcmc_bundled();
    export_overlap_blockmodel_multicanonical();
159
    export_overlap_blockmodel_multicanonical_multiflip();
160
    export_overlap_blockmodel_multiflip_mcmc();
161 162 163 164
    export_overlap_blockmodel_gibbs();
    export_overlap_blockmodel_vacate();
    export_layered_blockmodel_state();
    export_layered_blockmodel_mcmc();
165
    export_layered_blockmodel_multiflip_mcmc();
166 167 168
    export_layered_blockmodel_merge();
    export_layered_blockmodel_gibbs();
    export_layered_blockmodel_multicanonical();
169
    export_layered_blockmodel_multicanonical_multiflip();
170 171 172 173 174
    export_layered_overlap_blockmodel_state();
    export_layered_overlap_blockmodel_mcmc();
    export_layered_overlap_blockmodel_bundled_mcmc();
    export_layered_overlap_blockmodel_gibbs();
    export_layered_overlap_blockmodel_multicanonical();
175
    export_layered_overlap_blockmodel_multicanonical_multiflip();
176
    export_layered_overlap_blockmodel_multiflip_mcmc();
177
    export_layered_overlap_blockmodel_vacate();
178
    export_em_blockmodel_state();
179 180 181 182
    export_blockmodel_exhaustive();
    export_overlap_blockmodel_exhaustive();
    export_layered_blockmodel_exhaustive();
    export_layered_overlap_blockmodel_exhaustive();
183 184 185 186
    export_uncertain_state();
    export_uncertain_mcmc();
    export_measured_state();
    export_measured_mcmc();
187 188 189 190 191 192 193
    export_epidemics_state();
    export_epidemics_mcmc();
    export_epidemics_mcmc_r();
    export_cising_glauber_state();
    export_cising_glauber_mcmc();
    export_ising_glauber_state();
    export_ising_glauber_mcmc();
194
    export_marginals();
195
    export_modularity();
196
    export_latent_multigraph();
197 198 199 200 201 202
    export_pseudo_cising_state();
    export_pseudo_cising_mcmc();
    export_pseudo_cising_mcmc_h();
    export_pseudo_ising_state();
    export_pseudo_ising_mcmc();
    export_pseudo_ising_mcmc_h();
Tiago Peixoto's avatar
Tiago Peixoto committed
203 204 205
    export_vi_center_state();
    export_vi_center_mcmc();
    export_vi_multiflip_mcmc();
206 207 208
    export_rmi_center_state();
    export_rmi_center_mcmc();
    export_rmi_multiflip_mcmc();
209 210 211 212
    export_partition_mode();
    export_mode_cluster_state();
    export_mode_cluster_mcmc();
    export_mode_cluster_multiflip_mcmc();
213 214 215
    export_pp_state();
    export_pp_mcmc();
    export_pp_multiflip_mcmc();
216 217 218
    export_modularity_state();
    export_modularity_mcmc();
    export_modularity_multiflip_mcmc();
219 220 221 222 223 224 225 226

    def("vector_map", vector_map<int32_t>);
    def("vector_map64", vector_map<int64_t>);
    def("vector_rmap", vector_rmap<int32_t>);
    def("vector_rmap64", vector_rmap<int64_t>);
    def("vector_continuous_map", vector_continuous_map<int32_t>);
    def("vector_continuous_map64", vector_continuous_map<int64_t>);
}