graph_average.cc 3.02 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-2017 Tiago de Paula Peixoto <tiago@skewed.de>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//
// 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/>.

#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_properties.hh"

#include "graph_average.hh"

#include <boost/python.hpp>

26
27
#include <boost/mpl/joint_view.hpp>

28
29
30
31
32
33
using namespace std;
using namespace boost;
using namespace graph_tool;

// this will return the vertex average of degrees or scalar properties
python::object
34
get_vertex_average(GraphInterface& gi, GraphInterface::deg_t deg)
35
{
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    python::object a, dev;
    size_t count;

    typedef mpl::joint_view<vertex_scalar_properties,
                            vertex_scalar_vector_properties>
        vertex_numeric_properties_c;

    typedef property_map_types::apply<mpl::vector<python::object>,
                                      GraphInterface::vertex_index_map_t>::type
        python_properties;

    typedef mpl::joint_view<vertex_numeric_properties_c,
                            python_properties>
        vertex_numeric_properties;

    typedef mpl::transform<vertex_numeric_properties,
                           scalar_selector_type,
                           mpl::back_inserter<degree_selectors> >::type
        numeric_selectors;

    run_action<>()(gi, get_average<VertexAverageTraverse>(a,dev,count),
                   numeric_selectors())(degree_selector(deg));
    return python::make_tuple(a, dev, count);
59
60
61
62
63
64
}

// this will return the edge average of scalar properties
python::object
get_edge_average(GraphInterface& gi, boost::any prop)
{
65
66
67
68
69
70
71
    typedef mpl::joint_view<edge_scalar_properties,
                            edge_scalar_vector_properties>
        edge_numeric_properties_c;

    typedef property_map_types::apply<mpl::vector<python::object>,
                                      GraphInterface::edge_index_map_t>::type
        python_properties;
72

73
74
75
76
77
78
79
80
81
82
    typedef mpl::joint_view<edge_numeric_properties_c,
                            python_properties>
        edge_numeric_properties;

    python::object a, dev;
    size_t count;
    run_action<graph_tool::detail::always_directed>()
        (gi, get_average<EdgeAverageTraverse>(a, dev, count),
         edge_numeric_properties())(prop);
    return python::make_tuple(a, dev, count);
83
84
85
86
87
88
89
90
91
}

using namespace boost::python;

void export_average()
{
    def("get_vertex_average", &get_vertex_average);
    def("get_edge_average", &get_edge_average);
}