graph_community.cc 5.1 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-2015 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4
5
6
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
Tiago Peixoto's avatar
Tiago Peixoto committed
7
// as published by the Free Software Foundation; either version 3
Tiago Peixoto's avatar
Tiago Peixoto committed
8
9
10
11
12
13
14
15
// 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
16
17
// along with this program. If not, see <http://www.gnu.org/licenses/>.

18
#include "graph_python_interface.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
19
#include "graph_filtering.hh"
20
#include "graph.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
21
22
23
#include "graph_selectors.hh"
#include "graph_properties.hh"

24
#include <boost/mpl/push_back.hpp>
25
#include <boost/python.hpp>
26
27
28

#include "graph_community.hh"

Tiago Peixoto's avatar
Tiago Peixoto committed
29
30
using namespace std;
using namespace boost;
31

Tiago Peixoto's avatar
Tiago Peixoto committed
32
33
using namespace graph_tool;

34
35
36

void community_structure(GraphInterface& g, double gamma, string corr_name,
                         size_t n_iter, double Tmin, double Tmax, size_t Nspins,
37
                         rng_t& rng, bool verbose, string history_file,
38
                         boost::any weight, boost::any property)
Tiago Peixoto's avatar
Tiago Peixoto committed
39
{
Tiago Peixoto's avatar
Tiago Peixoto committed
40
    typedef property_map_types::apply<boost::mpl::vector<int32_t,int64_t>,
41
                                      GraphInterface::vertex_index_map_t,
Tiago Peixoto's avatar
Tiago Peixoto committed
42
                                      boost::mpl::bool_<false> >::type
43
        allowed_spin_properties;
Tiago Peixoto's avatar
Tiago Peixoto committed
44

45
    if (!belongs<allowed_spin_properties>()(property))
Tiago Peixoto's avatar
Tiago Peixoto committed
46
        throw ValueException("vertex property is not of integer type int32_t "
47
48
49
50
                             "or int64_t");

    typedef DynamicPropertyMapWrap<double,GraphInterface::edge_t> weight_map_t;
    typedef ConstantPropertyMap<double,GraphInterface::edge_t> no_weight_map_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
51
    typedef boost::mpl::vector<weight_map_t,no_weight_map_t> weight_properties;
Tiago Peixoto's avatar
Tiago Peixoto committed
52

53
54
55
56
57
58
59
60
61
62
63
64
    if (weight.empty())
        weight = no_weight_map_t(1.0);
    else
        weight = weight_map_t(weight, edge_scalar_properties());

    comm_corr_t corr;
    if (corr_name ==  "erdos")
        corr = ERDOS_REYNI;
    else if (corr_name == "uncorrelated")
        corr = UNCORRELATED;
    else if (corr_name == "correlated")
        corr = CORRELATED;
Tiago Peixoto's avatar
Tiago Peixoto committed
65
    else
Tiago Peixoto's avatar
Tiago Peixoto committed
66
        throw ValueException("invalid correlation type: " + corr_name);
67
68

    run_action<graph_tool::detail::never_directed>()
Tiago Peixoto's avatar
Tiago Peixoto committed
69
70
71
72
        (g, std::bind(get_communities_selector(corr, g.GetVertexIndex()),
                      placeholders::_1, placeholders::_2, placeholders::_3, gamma, n_iter,
                      make_pair(Tmin, Tmax), Nspins,
                      std::ref(rng), make_pair(verbose,history_file)),
73
74
         weight_properties(), allowed_spin_properties())
        (weight, property);
75
76
}

Tiago Peixoto's avatar
Tiago Peixoto committed
77

78
double modularity(GraphInterface& g, boost::any weight, boost::any property)
Tiago Peixoto's avatar
Tiago Peixoto committed
79
80
81
{
    double modularity = 0;

82
    typedef ConstantPropertyMap<int32_t,GraphInterface::edge_t> weight_map_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
83
    typedef boost::mpl::push_back<edge_scalar_properties, weight_map_t>::type
84
        edge_props_t;
85

86
87
    if(weight.empty())
        weight = weight_map_t(1);
88

89
    run_action<graph_tool::detail::never_directed>()
Tiago Peixoto's avatar
Tiago Peixoto committed
90
91
        (g, std::bind(get_modularity(), placeholders::_1, placeholders::_2,
                      placeholders::_3, std::ref(modularity)),
92
         edge_props_t(), vertex_scalar_properties())
93
        (weight, property);
Tiago Peixoto's avatar
Tiago Peixoto committed
94
95
    return modularity;
}
96
97
98

using namespace boost::python;

99
100
101

extern void community_network(GraphInterface& gi, GraphInterface& cgi,
                              boost::any community_property,
102
                              boost::any condensed_community_property,
103
104
105
                              boost::any vertex_count, boost::any edge_count,
                              boost::any vweight, boost::any eweight,
                              bool self_loops, bool parallel_edges);
106

107
108
109
110
void community_network_vavg(GraphInterface& gi, GraphInterface& cgi,
                            boost::any community_property,
                            boost::any condensed_community_property,
                            boost::any vweight,
Tiago Peixoto's avatar
Tiago Peixoto committed
111
                            boost::python::list avprops);
112
113
114
115
116

void community_network_eavg(GraphInterface& gi, GraphInterface& cgi,
                            boost::any community_property,
                            boost::any condensed_community_property,
                            boost::any eweight,
Tiago Peixoto's avatar
Tiago Peixoto committed
117
                            boost::python::list aeprops,
118
119
120
121
                            bool self_loops);


extern void export_blockmodel();
122
extern void export_blockmodel_overlap();
123
extern void export_blockmodel_covariate();
124

125
126
127
128
BOOST_PYTHON_MODULE(libgraph_tool_community)
{
    def("community_structure", &community_structure);
    def("modularity", &modularity);
129
    def("community_network", &community_network);
130
131
132
133
    def("community_network_vavg", &community_network_vavg);
    def("community_network_eavg", &community_network_eavg);

    export_blockmodel();
134
    export_blockmodel_overlap();
135
    export_blockmodel_covariate();
136
}