graph_correlations.cc 3.01 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/>.

18
#include "graph_filtering.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
19

20
#include <boost/python.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
21 22 23 24 25

#include "graph.hh"
#include "histogram.hh"
#include "graph_selectors.hh"
#include "graph_properties.hh"
26 27

#include "graph_correlations.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
28

29 30
#include <iostream>

Tiago Peixoto's avatar
Tiago Peixoto committed
31 32 33 34
using namespace std;
using namespace boost;
using namespace graph_tool;

35 36
// implementations spread across different compile units to minimize memory
// usage during compilation
37
void graph_correlations_imp1(GraphInterface& g, python::object& hist,
38
                             python::object& ret_bins,
39
                             boost::any deg1, boost::any deg2,
40 41 42
                             boost::any weight,
                             const array<vector<long double>,2>& bins);

43

44
typedef ConstantPropertyMap<int,GraphInterface::edge_t> cweight_map_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
45

46
python::object
47
get_vertex_correlation_histogram(GraphInterface& gi,
48 49
                                 GraphInterface::deg_t deg1,
                                 GraphInterface::deg_t deg2,
50
                                 boost::any weight,
51 52
                                 const vector<long double>& xbin,
                                 const vector<long double>& ybin)
Tiago Peixoto's avatar
Tiago Peixoto committed
53
{
54 55
    python::object hist;
    python::object ret_bins;
Tiago Peixoto's avatar
Tiago Peixoto committed
56

57 58 59 60 61 62 63 64
    array<vector<long double>,2> bins;
    bins[0] = xbin;
    bins[1] = ybin;

    any weight_prop;
    typedef DynamicPropertyMapWrap<long double, GraphInterface::edge_t>
        wrapped_weight_t;

65
    if (!weight.empty())
66
    {
67
        weight_prop = wrapped_weight_t(weight, edge_scalar_properties());
68 69 70
    }
    else
        weight_prop = cweight_map_t(1);
71

72 73 74 75
    try
    {
        run_action<>()(gi, get_correlation_histogram<GetNeighboursPairs>
                       (hist, bins, ret_bins),
76
                       scalar_selectors(), scalar_selectors(),
77
                       mpl::vector<cweight_map_t>())
78
            (degree_selector(deg1), degree_selector(deg2), weight_prop);
Tiago Peixoto's avatar
Tiago Peixoto committed
79
    }
80
    catch (ActionNotFound&)
Tiago Peixoto's avatar
Tiago Peixoto committed
81
    {
82 83
        graph_correlations_imp1(gi, hist, ret_bins, degree_selector(deg1),
                                degree_selector(deg2), weight_prop, bins);
Tiago Peixoto's avatar
Tiago Peixoto committed
84
    }
85

86 87 88 89 90 91 92 93
    return python::make_tuple(hist, ret_bins);
}

using namespace boost::python;

void export_vertex_correlations()
{
    def("vertex_correlation_histogram", &get_vertex_correlation_histogram);
Tiago Peixoto's avatar
Tiago Peixoto committed
94
}