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) 2006-2014 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/>.

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_corr_hist.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
Tiago Peixoto's avatar
Tiago Peixoto committed
37 38
void graph_correlations_imp1(GraphInterface& g, boost::python::object& hist,
                             boost::python::object& ret_bins,
39
                             boost::any deg1, boost::any deg2,
40
                             boost::any weight,
Tiago Peixoto's avatar
Tiago Peixoto committed
41
                             const std::array<vector<long double>,2>& bins);
42

43

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

Tiago Peixoto's avatar
Tiago Peixoto committed
46
boost::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
{
Tiago Peixoto's avatar
Tiago Peixoto committed
54 55
    boost::python::object hist;
    boost::python::object ret_bins;
Tiago Peixoto's avatar
Tiago Peixoto committed
56

Tiago Peixoto's avatar
Tiago Peixoto committed
57
    std::array<vector<long double>,2> bins;
58 59 60 61 62 63 64
    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(),
Tiago Peixoto's avatar
Tiago Peixoto committed
77
                       boost::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

Tiago Peixoto's avatar
Tiago Peixoto committed
86
    return boost::python::make_tuple(hist, ret_bins);
87 88 89 90 91 92 93
}

using namespace boost::python;

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