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

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

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

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

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

30 31
#include <iostream>

Tiago Peixoto's avatar
Tiago Peixoto committed
32 33 34 35 36
using namespace std;
using namespace boost;
using namespace boost::lambda;
using namespace graph_tool;

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

45

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

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

59 60 61 62 63 64 65 66
    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;

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

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

88 89 90 91 92 93 94 95
    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
96
}