source: src/graph/correlations/graph_correlations.hh @ f60d65

Revision f60d65, 5.3 KB checked in by Tiago de Paula Peixoto <tiago@…>, 3 years ago (diff)
Split correlation/histogram code in different files
  • Property mode set to 100644
Line 
1// graph-tool -- a general graph modification and manipulation thingy
2//
3// Copyright (C) 2007-2010 Tiago de Paula Peixoto <tiago@forked.de>
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 3
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18#ifndef GRAPH_CORRELATIONS_HH
19#define GRAPH_CORRELATIONS_HH
20
21#include <algorithm>
22#include <boost/numeric/conversion/bounds.hpp>
23#include <boost/numeric/conversion/cast.hpp>
24#include <boost/python/object.hpp>
25#include <boost/python/list.hpp>
26#include <boost/python/extract.hpp>
27#include "histogram.hh"
28#include "numpy_bind.hh"
29#include "shared_map.hh"
30
31namespace graph_tool
32{
33using namespace std;
34using namespace boost;
35
36// get degrees pairs from source and of neighbours
37class GetNeighboursPairs
38{
39public:
40
41    template <class Graph, class Deg1, class Deg2, class Hist, class WeightMap>
42    void operator()(typename graph_traits<Graph>::vertex_descriptor v,
43                    Deg1& deg1, Deg2& deg2, Graph& g, WeightMap& weight,
44                    Hist& hist)
45    {
46        typename Hist::point_t k;
47        k[0] = deg1(v, g);
48        typename graph_traits<Graph>::out_edge_iterator e, e_end;
49        for (tie(e,e_end) = out_edges(v, g); e != e_end; ++e)
50        {
51            k[1] = deg2(target(*e,g),g);
52            hist.PutValue(k, get(weight, *e));
53        }
54    }
55
56    template <class Graph, class Deg1, class Deg2, class Sum, class Count,
57              class WeightMap>
58    void operator()(typename graph_traits<Graph>::vertex_descriptor v,
59                    Deg1& deg1, Deg2& deg2, Graph& g, WeightMap& weight,
60                    Sum& sum, Sum& sum2, Count& count)
61    {
62        typename Sum::point_t k1;
63        k1[0] = deg1(v, g);
64        typename Sum::count_type k2;
65        typename graph_traits<Graph>::out_edge_iterator e, e_end;
66        for (tie(e,e_end) = out_edges(v, g); e != e_end; ++e)
67        {
68            k2 = deg2(target(*e,g),g)*get(weight, *e);
69            sum.PutValue(k1, k2);
70            sum2.PutValue(k1, k2*k2);
71            count.PutValue(k1, get(weight, *e));
72        }
73    }
74};
75
76// get degrees pairs from one single vertex
77class GetCombinedPair
78{
79public:
80
81    template <class Graph, class Deg1, class Deg2, class Hist, class Dummy>
82    void operator()(typename graph_traits<Graph>::vertex_descriptor v,
83                    Deg1& deg1, Deg2& deg2, Graph& g, const Dummy&,
84                    Hist& hist)
85    {
86        typename Hist::point_t k;
87        k[0] = deg1(v, g);
88        k[1] = deg2(v, g);
89        hist.PutValue(k);
90    }
91
92    template <class Graph, class Deg1, class Deg2, class Sum, class Count,
93              class WeightMap>
94    void operator()(typename graph_traits<Graph>::vertex_descriptor v,
95                    Deg1& deg1, Deg2& deg2, Graph& g, WeightMap& weight,
96                    Sum& sum, Sum& sum2, Count& count)
97    {
98        typename Sum::point_t k1;
99        k1[0] = deg1(v, g);
100        typename Sum::count_type k2;
101        k2 = deg2(v, g);
102        sum.PutValue(k1, k2);
103        sum2.PutValue(k1, k2*k2);
104        count.PutValue(k1, 1);
105    }
106};
107
108
109namespace detail
110{
111struct select_larger_type
112{
113    template <class Type1, class Type2>
114    struct apply
115    {
116        typedef typename mpl::if_<
117            typename mpl::greater<typename mpl::sizeof_<Type1>::type,
118                                  typename mpl::sizeof_<Type2>::type>::type,
119            Type1,
120            Type2>::type type;
121    };
122};
123
124struct select_float_and_larger
125{
126    template <class Type1, class Type2>
127    struct apply
128    {
129        typedef typename mpl::if_<
130            typename mpl::and_<is_floating_point<Type1>,
131                               is_floating_point<Type2> >::type,
132            typename select_larger_type::apply<Type1,Type2>::type,
133            typename mpl::if_<is_floating_point<Type1>,
134                              Type1,
135                              Type2>::type>::type type;
136    };
137};
138
139}
140
141template <class Value>
142void clean_bins(const vector<long double>& obins, vector<Value>& rbins)
143{
144    typedef Value val_type;
145    rbins.resize(obins.size());
146    for (size_t j = 0; j < rbins.size(); ++j)
147    {
148        // we'll attempt to recover from out of bounds conditions
149        try
150        {
151            rbins[j] = numeric_cast<val_type,long double>(obins[j]);
152        }
153        catch (boost::numeric::negative_overflow&)
154        {
155            rbins[j] = boost::numeric::bounds<val_type>::lowest();
156        }
157        catch (boost::numeric::positive_overflow&)
158        {
159            rbins[j] = boost::numeric::bounds<val_type>::highest();
160        }
161    }
162    // sort the bins
163    sort(rbins.begin(), rbins.end());
164    // clean bins of zero size
165    vector<val_type> temp_bin(1);
166    temp_bin[0] = rbins[0];
167    for (size_t j = 1; j < rbins.size(); ++j)
168    {
169        if (rbins[j] > rbins[j-1])
170                    temp_bin.push_back(rbins[j]);
171    }
172    rbins = temp_bin;
173}
174
175} // graph_tool namespace
176
177#endif
Note: See TracBrowser for help on using the repository browser.