graph_histograms.cc 2.44 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
// Copyright (C) 2008  Tiago de Paula Peixoto <tiago@skewed.de>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// 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
// along with this program. If not, see <http://www.gnu.org/licenses/>.

16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_properties.hh"

#include "graph_histograms.hh"

#include <boost/python.hpp>

using namespace std;
using namespace boost;
using namespace graph_tool;

// this will return the vertex histogram of degrees or scalar properties
python::object
30
get_vertex_histogram(GraphInterface& gi, GraphInterface::deg_t deg,
31
32
                     const vector<long double>& bins)
{
33
34
35
36
37
38
39
    try
    {
        if (!belongs<vertex_scalar_properties>()(get<boost::any>(deg)))
            throw ValueException("Vertex property must be of scalar type.");
    }
    catch (boost::bad_get&){}

40
41
42
43
44
    python::object hist;
    python::object ret_bins;

    run_action<>()(gi, get_histogram<VertexHistogramFiller>(hist, bins,
                                                            ret_bins),
45
         scalar_selectors())(degree_selector(deg));
46
47
48
49
50
    return python::make_tuple(hist, ret_bins);
}

// this will return the vertex histogram of degrees or scalar properties
python::object
51
get_edge_histogram(GraphInterface& gi, boost::any prop,
52
53
                   const vector<long double>& bins)
{
54
55
56
    if (!belongs<edge_scalar_properties>()(prop))
        throw ValueException("Edge property must be of scalar type.");

57
58
59
60
    python::object hist;
    python::object ret_bins;

    bool directed = gi.GetDirected();
61
    gi.SetDirected(true);
62
63
    run_action<graph_tool::detail::always_directed>()
        (gi, get_histogram<EdgeHistogramFiller>(hist, bins, ret_bins),
64
         edge_scalar_properties())(prop);
65
66
67
68
69
70
71
72
73
74
75
76
    gi.SetDirected(directed);

    return python::make_tuple(hist, ret_bins);
}

using namespace boost::python;

void export_histograms()
{
    def("get_vertex_histogram", &get_vertex_histogram);
    def("get_edge_histogram", &get_edge_histogram);
}