graph_community.cc 3.97 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) 2007-2012 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/>.

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

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

#include "graph_community.hh"

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

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

33 34 35

void community_structure(GraphInterface& g, double gamma, string corr_name,
                         size_t n_iter, double Tmin, double Tmax, size_t Nspins,
36 37 38
                         bool new_spins, size_t seed, bool verbose,
                         string history_file, boost::any weight,
                         boost::any property)
Tiago Peixoto's avatar
Tiago Peixoto committed
39
{
40
    typedef property_map_types::apply<mpl::vector<int32_t,int64_t>,
41
                                      GraphInterface::vertex_index_map_t,
42
                                      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;
51
    typedef 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>()
69
        (g, bind<void>(get_communities_selector(corr, g.GetVertexIndex()),
70
                       _1, _2, _3, gamma, n_iter,
71
                       make_pair(Tmin, Tmax), Nspins,
72 73 74
                       seed, make_pair(verbose,history_file)),
         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;
83 84
    typedef mpl::push_back<edge_scalar_properties, weight_map_t>::type
        edge_props_t;
85

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

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

using namespace boost::python;

98 99 100

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

106 107 108 109
BOOST_PYTHON_MODULE(libgraph_tool_community)
{
    def("community_structure", &community_structure);
    def("modularity", &modularity);
110
    def("community_network", &community_network);
111
}