graph_generation.cc 7.34 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
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>
Tiago Peixoto's avatar
Tiago Peixoto committed
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.
Tiago Peixoto's avatar
Tiago Peixoto committed
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.
Tiago Peixoto's avatar
Tiago Peixoto committed
14
//
15
// You should have received a copy of the GNU Lesser General Public License
16 17
// along with this program. If not, see <http://www.gnu.org/licenses/>.

18
#define BOOST_PYTHON_MAX_ARITY 20
19

Tiago Peixoto's avatar
Tiago Peixoto committed
20
#include "graph.hh"
21 22 23
#include "graph_util.hh"
#include "graph_filtering.hh"
#include "graph_generation.hh"
24
#include "sampler.hh"
25
#include "dynamic_sampler.hh"
26
#include <boost/python.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
27 28 29 30 31

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

32
class PythonFuncWrap
Tiago Peixoto's avatar
Tiago Peixoto committed
33
{
34
public:
Tiago Peixoto's avatar
Tiago Peixoto committed
35
    PythonFuncWrap(boost::python::object o): _o(o) {}
36

37
    pair<size_t, size_t> operator()(size_t i) const
Tiago Peixoto's avatar
Tiago Peixoto committed
38
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
39 40
        boost::python::object ret = _o(i);
        return boost::python::extract<pair<size_t,size_t> >(ret);
Tiago Peixoto's avatar
Tiago Peixoto committed
41 42
    }

43
    size_t operator()(size_t i, bool) const
Tiago Peixoto's avatar
Tiago Peixoto committed
44
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
45 46
        boost::python::object ret = _o(i);
        return boost::python::extract<size_t>(ret);
47 48
    }

49
private:
Tiago Peixoto's avatar
Tiago Peixoto committed
50
    boost::python::object _o;
51 52
};

53 54 55 56
void generate_graph(GraphInterface& gi, size_t N,
                    boost::python::object deg_sample, bool no_parallel,
                    bool no_self_loops, bool undirected, rng_t& rng,
                    bool verbose, bool verify)
Tiago Peixoto's avatar
Tiago Peixoto committed
57
{
58
    typedef graph_tool::detail::get_all_graph_views::apply<
59
    graph_tool::detail::filt_scalar_type, boost::mpl::bool_<false>,
Tiago Peixoto's avatar
Tiago Peixoto committed
60 61
        boost::mpl::bool_<false>, boost::mpl::bool_<false>,
        boost::mpl::bool_<true>, boost::mpl::bool_<true> >::type graph_views;
62 63

    if (undirected)
64
        gi.set_directed(false);
65

66
    run_action<graph_views>()
67 68 69 70 71 72 73 74
        (gi,
         [&](auto&& graph)
         {
             return gen_graph()
                 (std::forward<decltype(graph)>(graph), N,
                  PythonFuncWrap(deg_sample), no_parallel, no_self_loops, rng,
                  verbose, verify);
         })();
75
}
76

77 78
void generate_sbm(GraphInterface& gi, boost::any ab, boost::python::object ors,
                  boost::python::object oss, boost::python::object oprobs,
79 80
                  boost::any ain_deg, boost::any aout_deg, bool micro_ers,
                  bool micro_degs, rng_t& rng);
81

82 83 84 85 86 87 88
void generate_knn(GraphInterface& gi, boost::python::object om, size_t k,
                  double r, double epsilon, bool cache, boost::any aw,
                  rng_t& rng);

void generate_knn_exact(GraphInterface& gi, boost::python::object om, size_t k,
                        boost::any aw);

89 90
size_t random_rewire(GraphInterface& gi, string strat, size_t niter,
                     bool no_sweep, bool self_loops, bool parallel_edges,
91 92
                     bool configuration, bool traditional, bool micro,
                     bool persist, boost::python::object corr_prob,
93 94
                     boost::any apin, boost::any block, bool cache, rng_t& rng,
                     bool verbose);
Tiago Peixoto's avatar
Tiago Peixoto committed
95 96
void predecessor_graph(GraphInterface& gi, GraphInterface& gpi,
                       boost::any pred_map);
97 98
void line_graph(GraphInterface& gi, GraphInterface& lgi,
                boost::any edge_index);
Tiago Peixoto's avatar
Tiago Peixoto committed
99
boost::python::tuple graph_union(GraphInterface& ugi, GraphInterface& gi,
100
                          boost::any avprop);
Tiago Peixoto's avatar
Tiago Peixoto committed
101 102 103 104 105 106
void vertex_property_union(GraphInterface& ugi, GraphInterface& gi,
                           boost::any p_vprop, boost::any p_eprop,
                           boost::any uprop, boost::any prop);
void edge_property_union(GraphInterface& ugi, GraphInterface& gi,
                         boost::any p_vprop, boost::any p_eprop,
                         boost::any uprop, boost::any prop);
107 108
void triangulation(GraphInterface& gi, boost::python::object points,
                   boost::any pos, string type, bool periodic);
Tiago Peixoto's avatar
Tiago Peixoto committed
109 110 111
void lattice(GraphInterface& gi, boost::python::object oshape, bool periodic);
void geometric(GraphInterface& gi, boost::python::object opoints, double r,
               boost::python::object orange, bool periodic, boost::any pos);
112
void price(GraphInterface& gi, size_t N, double gamma, double c, size_t m,
113
           rng_t& rng);
Tiago Peixoto's avatar
Tiago Peixoto committed
114
void complete(GraphInterface& gi, size_t N, bool directed, bool self_loops);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
void circular(GraphInterface& gi, size_t N, size_t k, bool directed,
              bool self_loops);

void community_network(GraphInterface& gi, GraphInterface& cgi,
                       boost::any community_property,
                       boost::any condensed_community_property,
                       boost::any vertex_count, boost::any edge_count,
                       boost::any vweight, boost::any eweight, bool self_loops,
                       bool parallel_edges);

void community_network_vavg(GraphInterface& gi, GraphInterface& cgi,
                            boost::any community_property,
                            boost::any condensed_community_property,
                            boost::any vweight, boost::python::list avprops);

void community_network_eavg(GraphInterface& gi, GraphInterface& cgi,
                            boost::any community_property,
                            boost::any condensed_community_property,
133 134
                            boost::any eweight, boost::python::list aeprops,
                            bool self_loops, bool parallel_edges);
135

136 137
void export_maxent_sbm();

138 139
using namespace boost::python;

140 141
BOOST_PYTHON_MODULE(libgraph_tool_generation)
{
142
    docstring_options dopt(true, false);
143
    def("gen_graph", &generate_graph);
144
    def("gen_sbm", &generate_sbm);
145 146
    def("gen_knn", &generate_knn);
    def("gen_knn_exact", &generate_knn_exact);
147
    def("random_rewire", &random_rewire);
Tiago Peixoto's avatar
Tiago Peixoto committed
148
    def("predecessor_graph", &predecessor_graph);
149
    def("line_graph", &line_graph);
Tiago Peixoto's avatar
Tiago Peixoto committed
150 151 152
    def("graph_union", &graph_union);
    def("vertex_property_union", &vertex_property_union);
    def("edge_property_union", &edge_property_union);
153
    def("triangulation", &triangulation);
154 155
    def("lattice", &lattice);
    def("geometric", &geometric);
156
    def("price", &price);
Tiago Peixoto's avatar
Tiago Peixoto committed
157
    def("complete", &complete);
Tiago Peixoto's avatar
Tiago Peixoto committed
158
    def("circular", &circular);
159 160 161
    def("community_network", &community_network);
    def("community_network_vavg", &community_network_vavg);
    def("community_network_eavg", &community_network_eavg);
162
    export_maxent_sbm();
163 164 165 166 167

    class_<Sampler<int, boost::mpl::false_>>("Sampler",
                                             init<const vector<int>&, const vector<double>&>())
        .def("sample", &Sampler<int, boost::mpl::false_>::sample<rng_t>,
             return_value_policy<copy_const_reference>());
168 169 170 171 172 173 174 175

    class_<DynamicSampler<int>>("DynamicSampler",
                                init<const vector<int>&,
                                     const vector<double>&>())
        .def("sample", &DynamicSampler<int>::sample<rng_t>,
             return_value_policy<copy_const_reference>())
        .def("insert", &DynamicSampler<int>::insert)
        .def("remove", &DynamicSampler<int>::remove)
176
        .def("clear", &DynamicSampler<int>::clear)
177
        .def("rebuild", &DynamicSampler<int>::rebuild);
Tiago Peixoto's avatar
Tiago Peixoto committed
178
}