graph_correlations.cc 3.16 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 40
void graph_correlations_imp1(const GraphInterface& g, python::object& hist,
                             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 49 50 51 52 53 54
python::object
get_vertex_correlation_histogram(const GraphInterface& gi,
                                 GraphInterface::deg_t deg1,
                                 GraphInterface::deg_t deg2,
                                 string weight,
                                 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 67
    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;

    if (weight != "")
68
    {
69 70 71 72 73 74
        dynamic_property_map* map =
            any_cast<dynamic_property_map*>(edge_prop(weight, gi, true));
        weight_prop = wrapped_weight_t(*map);
    }
    else
        weight_prop = cweight_map_t(1);
75

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

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