graph_community.cc 4.03 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
3
// Copyright (C) 2007-2010 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
#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
69
70

    bool directed = g.GetDirected();
    g.SetDirected(false);
    run_action<graph_tool::detail::never_directed>()
71
        (g, bind<void>(get_communities_selector(corr, g.GetVertexIndex()),
72
                       _1, _2, _3, gamma, n_iter,
73
                       make_pair(Tmin, Tmax), Nspins,
74
75
76
77
                       seed, make_pair(verbose,history_file)),
         weight_properties(), allowed_spin_properties())
        (weight, property);
    g.SetDirected(directed);
78
79
}

Tiago Peixoto's avatar
Tiago Peixoto committed
80

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

85
    typedef ConstantPropertyMap<int32_t,GraphInterface::edge_t> weight_map_t;
86
87
    typedef mpl::push_back<edge_scalar_properties, weight_map_t>::type
        edge_props_t;
88

89
90
    if(weight.empty())
        weight = weight_map_t(1);
91

92
93
94
    bool directed = g.GetDirected();
    g.SetDirected(false);
    run_action<graph_tool::detail::never_directed>()
95
        (g, bind<void>(get_modularity(), _1, _2, _3, ref(modularity)),
96
         edge_props_t(), vertex_scalar_properties())
97
98
        (weight, property);
    g.SetDirected(directed);
Tiago Peixoto's avatar
Tiago Peixoto committed
99
100
101

    return modularity;
}
102
103
104

using namespace boost::python;

105
106
107
108
109
110

extern void community_network(GraphInterface& gi, GraphInterface& cgi,
                              boost::any community_property,
                              boost::any vertex_count,
                              boost::any edge_count, boost::any weight);

111
112
113
114
BOOST_PYTHON_MODULE(libgraph_tool_community)
{
    def("community_structure", &community_structure);
    def("modularity", &modularity);
115
    def("community_network", &community_network);
116
}