// graph-tool -- a general graph modification and manipulation thingy // // Copyright (C) 2007-2012 Tiago de Paula Peixoto // // 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 . #ifndef GRAPH_CORRELATIONS_HH #define GRAPH_CORRELATIONS_HH #include #include #include #include #include #include #include "histogram.hh" #include "numpy_bind.hh" #include "shared_map.hh" namespace graph_tool { using namespace std; using namespace boost; // get degrees pairs from source and of neighbours class GetNeighboursPairs { public: template void operator()(typename graph_traits::vertex_descriptor v, Deg1& deg1, Deg2& deg2, Graph& g, WeightMap& weight, Hist& hist) { typename Hist::point_t k; k[0] = deg1(v, g); typename graph_traits::out_edge_iterator e, e_end; for (tie(e,e_end) = out_edges(v, g); e != e_end; ++e) { k[1] = deg2(target(*e,g),g); hist.PutValue(k, get(weight, *e)); } } template void operator()(typename graph_traits::vertex_descriptor v, Deg1& deg1, Deg2& deg2, Graph& g, WeightMap& weight, Sum& sum, Sum& sum2, Count& count) { typename Sum::point_t k1; k1[0] = deg1(v, g); typename Sum::count_type k2; typename graph_traits::out_edge_iterator e, e_end; for (tie(e,e_end) = out_edges(v, g); e != e_end; ++e) { k2 = deg2(target(*e,g),g)*get(weight, *e); sum.PutValue(k1, k2); sum2.PutValue(k1, k2*k2); count.PutValue(k1, get(weight, *e)); } } }; // get degrees pairs from one single vertex class GetCombinedPair { public: template void operator()(typename graph_traits::vertex_descriptor v, Deg1& deg1, Deg2& deg2, Graph& g, const Dummy&, Hist& hist) { typename Hist::point_t k; k[0] = deg1(v, g); k[1] = deg2(v, g); hist.PutValue(k); } template void operator()(typename graph_traits::vertex_descriptor v, Deg1& deg1, Deg2& deg2, Graph& g, WeightMap& weight, Sum& sum, Sum& sum2, Count& count) { typename Sum::point_t k1; k1[0] = deg1(v, g); typename Sum::count_type k2; k2 = deg2(v, g); sum.PutValue(k1, k2); sum2.PutValue(k1, k2*k2); count.PutValue(k1, 1); } }; namespace detail { struct select_larger_type { template struct apply { typedef typename mpl::if_< typename mpl::greater::type, typename mpl::sizeof_::type>::type, Type1, Type2>::type type; }; }; struct select_float_and_larger { template struct apply { typedef typename mpl::if_< typename mpl::and_, is_floating_point >::type, typename select_larger_type::apply::type, typename mpl::if_, Type1, Type2>::type>::type type; }; }; } template void clean_bins(const vector& obins, vector& rbins) { typedef Value val_type; rbins.resize(obins.size()); for (size_t j = 0; j < rbins.size(); ++j) { // we'll attempt to recover from out of bounds conditions try { rbins[j] = numeric_cast(obins[j]); } catch (boost::numeric::negative_overflow&) { rbins[j] = boost::numeric::bounds::lowest(); } catch (boost::numeric::positive_overflow&) { rbins[j] = boost::numeric::bounds::highest(); } } // sort the bins sort(rbins.begin(), rbins.end()); // clean bins of zero size vector temp_bin(1); temp_bin[0] = rbins[0]; for (size_t j = 1; j < rbins.size(); ++j) { if (rbins[j] > rbins[j-1]) temp_bin.push_back(rbins[j]); } rbins = temp_bin; } } // graph_tool namespace #endif