graph_community.cc 3.93 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  Tiago de Paula Peixoto <tiago@forked.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 24
#include <boost/lambda/bind.hpp>
#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 31
using namespace std;
using namespace boost;
using namespace boost::lambda;
32

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

35 36 37 38 39

void community_structure(GraphInterface& g, double gamma, string corr_name,
                         size_t n_iter, double Tmin, double Tmax, size_t Nspins,
                         bool new_spins, size_t seed, bool verbose, string history_file,
                         boost::any weight, boost::any property)
Tiago Peixoto's avatar
Tiago Peixoto committed
40
{
41 42 43 44 45
    using boost::lambda::bind;
    using boost::lambda::_1;
    using boost::lambda::_2;
    using boost::lambda::_3;

46
    typedef property_map_types::apply<mpl::vector<int32_t,int64_t>,
47
                                      GraphInterface::vertex_index_map_t,
48
                                      mpl::bool_<false> >::type
49
        allowed_spin_properties;
Tiago Peixoto's avatar
Tiago Peixoto committed
50

51 52 53 54 55 56
    if (!belongs<allowed_spin_properties>()(property))
        throw GraphException("vertex property is not of integer type int32_t "
                             "or int64_t");

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

59 60 61 62 63 64 65 66 67 68 69 70
    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
71
    else
72 73 74 75 76 77 78 79 80 81 82 83 84
        throw GraphException("invalid correlation type: " + corr_name);

    bool directed = g.GetDirected();
    g.SetDirected(false);
    run_action<graph_tool::detail::never_directed>()
        (g, bind<void>(get_communities_selector(corr),
                       _1, _2, _3, gamma, n_iter,
                       make_pair(Tmin, Tmax),
                       make_pair(Nspins, new_spins),
                       seed, make_pair(verbose,history_file)),
         weight_properties(), allowed_spin_properties())
        (weight, property);
    g.SetDirected(directed);
85 86
}

Tiago Peixoto's avatar
Tiago Peixoto committed
87

88
double modularity(GraphInterface& g, boost::any weight, boost::any property)
Tiago Peixoto's avatar
Tiago Peixoto committed
89
{
90 91 92 93 94
    using boost::lambda::bind;
    using boost::lambda::_1;
    using boost::lambda::_2;
    using boost::lambda::_3;

Tiago Peixoto's avatar
Tiago Peixoto committed
95 96
    double modularity = 0;

97
    typedef ConstantPropertyMap<int32_t,GraphInterface::edge_t> weight_map_t;
98

99 100
    if(weight.empty())
        weight = weight_map_t(1);
101

102 103 104 105 106 107 108
    bool directed = g.GetDirected();
    g.SetDirected(false);
    run_action<graph_tool::detail::never_directed>()
        (g, bind<void>(get_modularity(), _1, _2, _3, var(modularity)),
         edge_scalar_properties(), vertex_scalar_properties())
        (weight, property);
    g.SetDirected(directed);
Tiago Peixoto's avatar
Tiago Peixoto committed
109 110 111

    return modularity;
}
112 113 114 115 116 117 118 119

using namespace boost::python;

BOOST_PYTHON_MODULE(libgraph_tool_community)
{
    def("community_structure", &community_structure);
    def("modularity", &modularity);
}