From 360a3395d6f7cdd4d2fc39f6f8a9e75b764d3766 Mon Sep 17 00:00:00 2001 From: Tiago de Paula Peixoto Date: Thu, 10 Apr 2008 14:35:35 +0200 Subject: [PATCH] Correlations algorithms refactoring The whole histogram code has been redone, and the code has been simplified. The three-point vertex-edge-vertex correlation has been scrapped, since it's not frequently used, and would make compilation even more expensive. This also adds some missing files to the generation routine. --- configure.in | 1 + src/graph/Makefile.am | 2 +- src/graph/correlations/Makefile.am | 29 +-- src/graph/correlations/graph_assortativity.cc | 54 +++--- src/graph/correlations/graph_assortativity.hh | 6 +- src/graph/correlations/graph_correlations.cc | 81 ++++---- src/graph/correlations/graph_correlations.hh | 178 ++++++++++++++++-- ...orr_imp1.cc => graph_correlations_bind.cc} | 31 +-- .../graph_correlations_combined.cc | 63 ++++--- .../graph_correlations_combined.hh | 107 ----------- .../graph_correlations_combined_corr.cc | 64 ------- .../graph_correlations_combined_imp1.cc | 44 ----- .../correlations/graph_correlations_imp1.cc | 19 +- .../correlations/graph_correlations_imp2.cc | 49 ----- .../correlations/graph_correlations_imp3.cc | 49 ----- .../graph_correlations_neighbours.cc | 114 ----------- .../graph_correlations_neighbours.hh | 100 ---------- .../graph_correlations_neighbours_imp1.cc | 48 ----- .../graph_correlations_neighbours_imp2.cc | 48 ----- .../graph_correlations_neighbours_imp3.cc | 48 ----- .../graph_correlations_neighbours_imp4.cc | 48 ----- .../graph_correlations_neighbours_imp5.cc | 48 ----- .../graph_correlations_neighbours_imp6.cc | 44 ----- .../correlations/graph_edge_correlations.cc | 56 ------ .../correlations/graph_edge_correlations.hh | 75 -------- .../graph_edge_correlations_imp1.cc | 48 ----- .../graph_edge_correlations_imp2.cc | 48 ----- .../graph_edge_correlations_imp3.cc | 48 ----- .../graph_edge_correlations_imp4.cc | 48 ----- .../graph_edge_correlations_imp5.cc | 48 ----- src/graph/graph.cc | 1 - src/graph/graph.hh | 14 +- src/graph/graph_bind.cc | 88 +++++++++ src/graph/graph_io.cc | 1 - src/graph/graph_properties.cc | 17 +- src/graph/graph_properties.hh | 160 ++++++++-------- src/graph/graph_python_interface.cc | 82 ++++++-- src/graph/graph_python_interface.hh | 9 +- src/graph/histogram.hh | 44 ++++- src/graph/numpy_bind.hh | 49 +++-- src/graph_tool/Makefile.am | 11 +- src/graph_tool/core.py | 51 +++-- src/graph_tool/correlations/__init__.py | 100 ++++++++++ src/graph_tool/generation/__init__.py | 61 ++++++ 44 files changed, 832 insertions(+), 1502 deletions(-) rename src/graph/correlations/{graph_correlations_combined_corr_imp1.cc => graph_correlations_bind.cc} (52%) delete mode 100644 src/graph/correlations/graph_correlations_combined.hh delete mode 100644 src/graph/correlations/graph_correlations_combined_corr.cc delete mode 100644 src/graph/correlations/graph_correlations_combined_imp1.cc delete mode 100644 src/graph/correlations/graph_correlations_imp2.cc delete mode 100644 src/graph/correlations/graph_correlations_imp3.cc delete mode 100644 src/graph/correlations/graph_correlations_neighbours.cc delete mode 100644 src/graph/correlations/graph_correlations_neighbours.hh delete mode 100644 src/graph/correlations/graph_correlations_neighbours_imp1.cc delete mode 100644 src/graph/correlations/graph_correlations_neighbours_imp2.cc delete mode 100644 src/graph/correlations/graph_correlations_neighbours_imp3.cc delete mode 100644 src/graph/correlations/graph_correlations_neighbours_imp4.cc delete mode 100644 src/graph/correlations/graph_correlations_neighbours_imp5.cc delete mode 100644 src/graph/correlations/graph_correlations_neighbours_imp6.cc delete mode 100644 src/graph/correlations/graph_edge_correlations.cc delete mode 100644 src/graph/correlations/graph_edge_correlations.hh delete mode 100644 src/graph/correlations/graph_edge_correlations_imp1.cc delete mode 100644 src/graph/correlations/graph_edge_correlations_imp2.cc delete mode 100644 src/graph/correlations/graph_edge_correlations_imp3.cc delete mode 100644 src/graph/correlations/graph_edge_correlations_imp4.cc delete mode 100644 src/graph/correlations/graph_edge_correlations_imp5.cc create mode 100644 src/graph_tool/correlations/__init__.py create mode 100644 src/graph_tool/generation/__init__.py diff --git a/configure.in b/configure.in index 37c009eb..cfff954a 100644 --- a/configure.in +++ b/configure.in @@ -205,6 +205,7 @@ Makefile src/Makefile src/graph/Makefile src/graph/correlations/Makefile +src/graph/generation/Makefile src/graph_tool/Makefile ]) diff --git a/src/graph/Makefile.am b/src/graph/Makefile.am index b6d71f2d..6963e724 100644 --- a/src/graph/Makefile.am +++ b/src/graph/Makefile.am @@ -1,6 +1,6 @@ ## Process this file with automake to produce Makefile.in -#SUBDIRS = correlations +SUBDIRS = generation correlations AM_CPPFLAGS =\ -I. -I.. \ diff --git a/src/graph/correlations/Makefile.am b/src/graph/correlations/Makefile.am index 30bf29d3..fc0b7675 100644 --- a/src/graph/correlations/Makefile.am +++ b/src/graph/correlations/Makefile.am @@ -2,6 +2,7 @@ AM_CPPFLAGS =\ -I. -I.. \ + -I $(pythondir)/numpy/core/include/numpy/ \ -I../boost-workaround \ -DHAVE_CONFIG_H @@ -12,7 +13,7 @@ AM_CXXFLAGS =\ AM_CFLAGS=$(AM_CXXFLAGS) -libgraph_tool_correlationsdir = $(pythondir)/graph_tool +libgraph_tool_correlationsdir = $(pythondir)/graph_tool/correlations libgraph_tool_correlations_LTLIBRARIES = libgraph_tool_correlations.la @@ -21,33 +22,13 @@ libgraph_tool_correlations_la_includedir = $(pythondir)/graph_tool/include libgraph_tool_correlations_la_SOURCES = \ graph_assortativity.cc \ graph_correlations.cc \ - graph_correlations_combined.cc \ - graph_correlations_combined_imp1.cc \ - graph_correlations_combined_corr.cc \ - graph_correlations_combined_corr_imp1.cc \ graph_correlations_imp1.cc \ - graph_correlations_imp2.cc \ - graph_correlations_imp3.cc \ - graph_correlations_neighbours.cc \ - graph_correlations_neighbours_imp1.cc \ - graph_correlations_neighbours_imp2.cc \ - graph_correlations_neighbours_imp3.cc \ - graph_correlations_neighbours_imp4.cc \ - graph_correlations_neighbours_imp5.cc \ - graph_correlations_neighbours_imp6.cc \ - graph_edge_correlations.cc \ - graph_edge_correlations_imp1.cc \ - graph_edge_correlations_imp2.cc \ - graph_edge_correlations_imp3.cc \ - graph_edge_correlations_imp4.cc \ - graph_edge_correlations_imp5.cc + graph_correlations_combined.cc \ + graph_correlations_bind.cc libgraph_tool_correlations_la_include_HEADERS = \ graph_assortativity.hh \ - graph_correlations_combined.hh \ - graph_correlations.hh \ - graph_correlations_neighbours.hh \ - graph_edge_correlations.hh + graph_correlations.hh libgraph_tool_correlations_la_LIBADD = \ $(PYTHON_LDFLAGS) \ diff --git a/src/graph/correlations/graph_assortativity.cc b/src/graph/correlations/graph_assortativity.cc index 7767f085..3cf71fb8 100644 --- a/src/graph/correlations/graph_assortativity.cc +++ b/src/graph/correlations/graph_assortativity.cc @@ -15,7 +15,6 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . - #include "graph_filtering.hh" #include "graph_selectors.hh" #include "graph_properties.hh" @@ -24,48 +23,39 @@ #include "graph_assortativity.hh" using namespace std; -using namespace boost; -using namespace boost::lambda; using namespace graph_tool; - pair -GraphInterface::GetAssortativityCoefficient(GraphInterface::deg_t deg) const +assortativity_coefficient(const GraphInterface& gi, + GraphInterface::deg_t deg) { + using namespace boost::lambda; double a, a_err; - try - { - run_action<>()(*this, - bind(get_assortativity_coefficient(), _1, _2, - var(a), var(a_err)), all_selectors()) - (degree_selector(deg, _properties)); - } - catch (dynamic_get_failure &e) - { - throw GraphException("error getting scalar property: " + - string(e.what())); - } - + run_action<>()(gi,bind(get_assortativity_coefficient(), _1, _2, + var(a), var(a_err)), all_selectors()) + (degree_selector(deg, gi)); return make_pair(a, a_err); } pair -GraphInterface::GetScalarAssortativityCoefficient(GraphInterface::deg_t deg) - const +scalar_assortativity_coefficient(const GraphInterface& gi, + GraphInterface::deg_t deg) { + using namespace boost::lambda; double a, a_err; - try - { - run_action<>()(*this, bind(get_scalar_assortativity_coefficient(), - _1, _2, var(a), var(a_err)), + run_action<>()(gi, bind(get_scalar_assortativity_coefficient(), + _1, _2, var(a), var(a_err)), all_selectors()) - (degree_selector(deg, _properties)); - } - catch (dynamic_get_failure &e) - { - throw GraphException("error getting scalar property: " + - string(e.what())); - } - + (degree_selector(deg, gi)); return make_pair(a, a_err); } + +#include + +using namespace boost::python; + +void export_assortativity() +{ + def("assortativity_coefficient", &assortativity_coefficient); + def("scalar_assortativity_coefficient", &scalar_assortativity_coefficient); +} diff --git a/src/graph/correlations/graph_assortativity.hh b/src/graph/correlations/graph_assortativity.hh index e91cf8bc..91395bf3 100644 --- a/src/graph/correlations/graph_assortativity.hh +++ b/src/graph/correlations/graph_assortativity.hh @@ -20,6 +20,7 @@ #include #include "shared_map.hh" +#include "histogram.hh" namespace graph_tool { @@ -144,9 +145,8 @@ struct get_scalar_assortativity_coefficient sb.Gather(); double t1 = e_xy/n_edges; - double avg_a = GetHistogramMean(a), avg_b = GetHistogramMean(b); - double da = GetHistogramDeviation(a,avg_a), db = - GetHistogramDeviation(b,avg_b); + double avg_a = GetMapMean(a), avg_b = GetMapMean(b); + double da = GetMapDeviation(a,avg_a), db = GetMapDeviation(b,avg_b); if (da*db > 0) r = (t1 - avg_a*avg_b)/(da*db); diff --git a/src/graph/correlations/graph_correlations.cc b/src/graph/correlations/graph_correlations.cc index 64d40bb2..32e23a00 100644 --- a/src/graph/correlations/graph_correlations.cc +++ b/src/graph/correlations/graph_correlations.cc @@ -18,6 +18,7 @@ #include "graph_filtering.hh" #include +#include #include "graph.hh" #include "histogram.hh" @@ -26,6 +27,8 @@ #include "graph_correlations.hh" +#include + using namespace std; using namespace boost; using namespace boost::lambda; @@ -33,51 +36,63 @@ using namespace graph_tool; // implementations spread across different compile units to minimize memory // usage during compilation -void graph_correlations_imp1(const GraphInterface& g, hist2d_t& hist, - boost::any deg1, boost::any deg2, - boost::any weight); -void graph_correlations_imp2(const GraphInterface& g, hist2d_t& hist, +void graph_correlations_imp1(const GraphInterface& g, python::object& hist, + python::object& ret_bins, boost::any deg1, boost::any deg2, - boost::any weight); -void graph_correlations_imp3(const GraphInterface& g, hist2d_t& hist, - boost::any deg1, boost::any deg2, - boost::any weight); + boost::any weight, + const array,2>& bins); + typedef ConstantPropertyMap cweight_map_t; -hist2d_t -GraphInterface::GetVertexCorrelationHistogram(GraphInterface::deg_t deg1, - GraphInterface::deg_t deg2, - string weight) const +python::object +get_vertex_correlation_histogram(const GraphInterface& gi, + GraphInterface::deg_t deg1, + GraphInterface::deg_t deg2, + string weight, + const vector& xbin, + const vector& ybin) { - hist2d_t hist; + python::object hist; + python::object ret_bins; - try + array,2> bins; + bins[0] = xbin; + bins[1] = ybin; + + any weight_prop; + typedef DynamicPropertyMapWrap + wrapped_weight_t; + + if (weight != "") { - any weight_prop; - if (weight != "") - weight_prop = prop(weight, _edge_index, _properties); - else - weight_prop = cweight_map_t(1); + dynamic_property_map* map = + any_cast(edge_prop(weight, gi, true)); + weight_prop = wrapped_weight_t(*map); + } + else + weight_prop = cweight_map_t(1); - run_action<>()(*this, get_correlation_histogram(hist), + try + { + run_action<>()(gi, get_correlation_histogram + (hist, bins, ret_bins), all_selectors(), all_selectors(), mpl::vector()) - (degree_selector(deg1, _properties), - degree_selector(deg2, _properties), weight); - graph_correlations_imp1(*this, hist, degree_selector(deg1, _properties), - degree_selector(deg2, _properties), weight); - graph_correlations_imp2(*this, hist, degree_selector(deg1, _properties), - degree_selector(deg2, _properties), weight); - graph_correlations_imp3(*this, hist, degree_selector(deg1, _properties), - degree_selector(deg2, _properties), weight); - + (degree_selector(deg1, gi), degree_selector(deg2, gi), weight_prop); } - catch (dynamic_get_failure &e) + catch (ActionNotFound&) { - throw GraphException("error getting scalar property: " + - string(e.what())); + graph_correlations_imp1(gi, hist, ret_bins, degree_selector(deg1, gi), + degree_selector(deg2, gi), weight_prop, bins); } - return hist; + return python::make_tuple(hist, ret_bins); +} + +using namespace boost::python; + +void export_vertex_correlations() +{ + def("vertex_correlation_histogram", &get_vertex_correlation_histogram); } diff --git a/src/graph/correlations/graph_correlations.hh b/src/graph/correlations/graph_correlations.hh index e1ba9bdf..da925966 100644 --- a/src/graph/correlations/graph_correlations.hh +++ b/src/graph/correlations/graph_correlations.hh @@ -19,7 +19,12 @@ #define GRAPH_CORRELATIONS_HH #include -#include +#include +#include +#include +#include +#include +#include "numpy_bind.hh" #include "shared_map.hh" namespace graph_tool @@ -28,12 +33,85 @@ using namespace std; using namespace boost; using namespace boost::lambda; -// retrieves the generalized vertex-vertex correlation histogram +// 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)); + } + } +}; + +// 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); + } +}; + + +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_< + mpl::equal_to, + is_floating_point >, + typename select_larger_type::apply::type, + typename mpl::if_, + Type1, + Type2>::type>::type type; + }; +}; + +} -template +// retrieves the generalized vertex-vertex correlation histogram +template struct get_correlation_histogram { - get_correlation_histogram(Hist& hist): _hist(hist) {} + get_correlation_histogram(python::object& hist, + const array,2>& bins, + python::object& ret_bins) + : _hist(hist), _bins(bins), _ret_bins(ret_bins) {} template @@ -41,7 +119,75 @@ struct get_correlation_histogram WeightMap weight) const { Graph& g = *gp; - SharedMap s_hist(_hist); + GetDegreePair put_point; + + typedef typename DegreeSelector1::value_type type1; + typedef typename DegreeSelector2::value_type type2; + + typedef typename graph_tool::detail:: + select_float_and_larger::apply::type + val_type; + typedef typename property_traits::value_type count_type; + typedef Histogram hist_t; + + array,2> bins; + for (size_t i = 0; i < bins.size(); ++i) + { + bins[i].resize(_bins[i].size()); + for (size_t j = 0; j < bins[i].size(); ++j) + { + // we'll attempt to recover from out of bounds conditions + try + { + bins[i][j] = + numeric_cast(_bins[i][j]); + } + catch (boost::numeric::negative_overflow&) + { + bins[i][j] = boost::numeric::bounds::lowest(); + } + catch (boost::numeric::positive_overflow&) + { + bins[i][j] = boost::numeric::bounds::highest(); + } + } + // sort the bins + sort(bins[i].begin(), bins[i].end()); + // clean bins of zero size + vector temp_bin(1); + temp_bin[0] = bins[i][0]; + for (size_t j = 1; j < bins[i].size(); ++j) + { + if (bins[i][j] > bins[i][j-1]) + temp_bin.push_back(bins[i][j]); + } + bins[i] = temp_bin; + } + + // find the data range + pair range1; + pair range2; + typename graph_traits::vertex_iterator vi,vi_end; + range1.first = boost::numeric::bounds::highest(); + range1.second = boost::numeric::bounds::lowest(); + range2.first = boost::numeric::bounds::highest(); + range2.second = boost::numeric::bounds::lowest(); + for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) + { + type1 v1 = deg1(*vi, g); + type2 v2 = deg2(*vi, g); + range1.first = min(range1.first, v1); + range1.second = max(range1.second, v1); + range2.first = min(range2.first, v2); + range2.second = max(range2.second, v2); + } + + boost::array, 2> data_range; + data_range[0] = range1; + data_range[1] = range2; + + hist_t hist(bins, data_range); + SharedHistogram s_hist(hist); int i, N = num_vertices(g); #pragma omp parallel for default(shared) private(i) \ @@ -51,20 +197,20 @@ struct get_correlation_histogram typename graph_traits::vertex_descriptor v = vertex(i, g); if (v == graph_traits::null_vertex()) continue; - - typename Hist::key_type key; - key.first = 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) - { - key.second = deg2(target(*e,g),g); - s_hist[key] += - typename Hist::value_type::second_type(get(weight, *e)); - } + put_point(v, deg1, deg2, g, weight, s_hist); } s_hist.Gather(); + + bins = hist.GetBins(); + python::list ret_bins; + ret_bins.append(wrap_vector_owned(bins[0])); + ret_bins.append(wrap_vector_owned(bins[1])); + _ret_bins = ret_bins; + _hist = wrap_multi_array_owned(hist.GetArray()); } - Hist& _hist; + python::object& _hist; + const array,2>& _bins; + python::object& _ret_bins; }; diff --git a/src/graph/correlations/graph_correlations_combined_corr_imp1.cc b/src/graph/correlations/graph_correlations_bind.cc similarity index 52% rename from src/graph/correlations/graph_correlations_combined_corr_imp1.cc rename to src/graph/correlations/graph_correlations_bind.cc index c41d51e4..bf28b596 100644 --- a/src/graph/correlations/graph_correlations_combined_corr_imp1.cc +++ b/src/graph/correlations/graph_correlations_bind.cc @@ -15,30 +15,17 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#include "graph_filtering.hh" -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" -#include "shared_map.hh" +#include -#include - -#include "graph_correlations_combined.hh" - -using namespace std; using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; -void graph_correlations_combined_corr_imp1 - (const GraphInterface& g, avg_corr_t& avg_corr, - boost::any deg1, boost::any deg2) +void export_assortativity(); +void export_vertex_correlations(); +void export_combined_vertex_correlations(); + +BOOST_PYTHON_MODULE(libgraph_tool_correlations) { - run_action<>()(g, bind(get_average_combined_degree_correlation(), - _1, _2, _3, var(avg_corr)), - all_selectors(), - graph_tool::detail::split::apply::type - ::second()) - (deg1, deg2); + export_assortativity(); + export_vertex_correlations(); + export_combined_vertex_correlations(); } diff --git a/src/graph/correlations/graph_correlations_combined.cc b/src/graph/correlations/graph_correlations_combined.cc index 0953e866..9592f7af 100644 --- a/src/graph/correlations/graph_correlations_combined.cc +++ b/src/graph/correlations/graph_correlations_combined.cc @@ -16,45 +16,52 @@ // along with this program. If not, see . #include "graph_filtering.hh" + +#include +#include + #include "graph.hh" #include "histogram.hh" #include "graph_selectors.hh" #include "graph_properties.hh" -#include "shared_map.hh" -#include - -#include "graph_correlations_combined.hh" +#include "graph_correlations.hh" using namespace std; using namespace boost; using namespace boost::lambda; using namespace graph_tool; -void graph_correlations_combined_imp1(const GraphInterface& g, - hist2d_t& hist, - boost::any deg1, boost::any deg2); +typedef ConstantPropertyMap dummy_weight; + +python::object +get_vertex_combined_correlation_histogram(const GraphInterface& gi, + GraphInterface::deg_t deg1, + GraphInterface::deg_t deg2, + const vector& xbin, + const vector& ybin) +{ + python::object hist; + python::object ret_bins; + + array,2> bins; + bins[0] = xbin; + bins[1] = ybin; + + run_action<>()(gi, lambda::bind + (get_correlation_histogram(hist, bins, + ret_bins), + lambda::_1, lambda::_2, lambda::_3, dummy_weight(0)), + all_selectors(), all_selectors()) + (degree_selector(deg1, gi), degree_selector(deg2, gi)); + + return python::make_tuple(hist, ret_bins); +} + +using namespace boost::python; -hist2d_t -GraphInterface::GetCombinedVertexHistogram(deg_t deg1, deg_t deg2) const +void export_combined_vertex_correlations() { - hist2d_t hist; - try - { - run_action<>()(*this, bind(get_combined_degree_histogram(), - _1, _2, _3, var(hist)), - all_selectors(), - detail::split::apply::type::first()) - (degree_selector(deg1, _properties), - degree_selector(deg2, _properties)); - graph_correlations_combined_imp1(*this, hist, - degree_selector(deg1, _properties), - degree_selector(deg2, _properties)); - } - catch (dynamic_get_failure &e) - { - throw GraphException("error getting scalar property: " + - string(e.what())); - } - return hist; + def("vertex_combined_correlation_histogram", + &get_vertex_combined_correlation_histogram); } diff --git a/src/graph/correlations/graph_correlations_combined.hh b/src/graph/correlations/graph_correlations_combined.hh deleted file mode 100644 index 83de2a1d..00000000 --- a/src/graph/correlations/graph_correlations_combined.hh +++ /dev/null @@ -1,107 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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_COMBINED_HH -#define GRAPH_CORRELATIONS_COMBINED_HH - -#include -#include "shared_map.hh" - -namespace graph_tool -{ -using namespace std; -using namespace boost; - -// retrieves the distribution of combined (deg1,deg2) degrees -struct get_combined_degree_histogram -{ - template - void operator()(const Graph* gp, DegreeSelector1 deg1, DegreeSelector2 deg2, - Hist &hist) const - { - const Graph& g = *gp; - SharedMap s_hist(hist); - - typedef typename Hist::key_type::first_type first_type; - typedef typename Hist::key_type::second_type second_type; - - int i, N = num_vertices(g); - #pragma omp parallel for default(shared) private(i) \ - firstprivate(s_hist) schedule(dynamic) - for (i = 0; i < N; ++i) - { - typename graph_traits::vertex_descriptor v = vertex(i, g); - if (v == graph_traits::null_vertex()) - continue; - - s_hist[make_pair(first_type(deg1(v,g)), - second_type(deg2(v,g)))]++; - } - - s_hist.Gather(); - } -}; - - -// retrieves the average of deg2 in function of deg1 - -struct get_average_combined_degree_correlation -{ - template - void operator()(const Graph* gp, DegreeSelector1 deg1, DegreeSelector2 deg2, - AvgDeg& avg_deg) const - { - const Graph& g = *gp; - tr1::unordered_map count; - - typename graph_traits::vertex_iterator v, v_begin, v_end; - tie(v_begin, v_end) = vertices(g); - for(v = v_begin; v != v_end; ++v) - { - typename AvgDeg::key_type d1 = deg1(*v,g); - typename AvgDeg::value_type::second_type::first_type d2 = - deg2(*v, g); - avg_deg[d1].first += d2; - avg_deg[d1].second += d2*d2; - count[d1]++; - } - - for (typeof(avg_deg.begin()) iter = avg_deg.begin(); - iter != avg_deg.end(); ++iter) - { - size_t N = count[iter->first]; - iter->second.first /= N; - if (N > 1) - { - double err = (iter->second.second - N*iter->second.first * - iter->second.first)/(N*(N-1)); - iter->second.second = (err<0.0)?0.0:sqrt(err); - } - else - { - iter->second.second = 0.0; - } - } - } -}; - -} // graph_tool namespace - -#endif // GRAPH_CORRELATIONS_COMBINED_HH - diff --git a/src/graph/correlations/graph_correlations_combined_corr.cc b/src/graph/correlations/graph_correlations_combined_corr.cc deleted file mode 100644 index 8ac9867d..00000000 --- a/src/graph/correlations/graph_correlations_combined_corr.cc +++ /dev/null @@ -1,64 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" -#include "shared_map.hh" - -#include - -#include "graph_correlations_combined.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -void graph_correlations_combined_corr_imp1 - (const GraphInterface& g, avg_corr_t& avg_corr, - boost::any deg1, boost::any deg2); - -avg_corr_t -GraphInterface::GetAverageCombinedVertexCorrelation(deg_t deg1, deg_t deg2) -const -{ - avg_corr_t avg_corr; - - try - { - run_action<>()(*this, - bind(get_average_combined_degree_correlation(), - _1, _2, _3, var(avg_corr)), - all_selectors(), - detail::split::apply::type::first()) - (degree_selector(deg1, _properties), - degree_selector(deg2, _properties)); - graph_correlations_combined_corr_imp1 - (*this, avg_corr, degree_selector(deg1, _properties), - degree_selector(deg2, _properties)); - - } - catch (dynamic_get_failure &e) - { - throw GraphException("error getting scalar property: " + - string(e.what())); - } - return avg_corr; -} diff --git a/src/graph/correlations/graph_correlations_combined_imp1.cc b/src/graph/correlations/graph_correlations_combined_imp1.cc deleted file mode 100644 index a188cdcf..00000000 --- a/src/graph/correlations/graph_correlations_combined_imp1.cc +++ /dev/null @@ -1,44 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" -#include "shared_map.hh" - -#include - -#include "graph_correlations_combined.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -void graph_correlations_combined_imp1(const GraphInterface& g, - hist2d_t& hist, - boost::any deg1, boost::any deg2) -{ - run_action<>()(g, bind(get_combined_degree_histogram(), - _1, _2, _3, var(hist)), - all_selectors(), - graph_tool::detail::split::apply::type - ::second()) - (deg1, deg2); -} diff --git a/src/graph/correlations/graph_correlations_imp1.cc b/src/graph/correlations/graph_correlations_imp1.cc index 5db36538..3e52d337 100644 --- a/src/graph/correlations/graph_correlations_imp1.cc +++ b/src/graph/correlations/graph_correlations_imp1.cc @@ -31,20 +31,19 @@ using namespace boost; using namespace boost::lambda; using namespace graph_tool; -typedef mpl::vector weight_scalar_types; -struct weight_map_types: property_map_types::apply< - weight_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_correlations_imp1(const GraphInterface& g, hist2d_t& hist, +void graph_correlations_imp1(const GraphInterface& g, python::object& hist, + python::object& ret_bins, boost::any deg1, boost::any deg2, - boost::any weight) + boost::any weight, + const array,2>& bins) { - run_action<>()(g, get_correlation_histogram(hist), + typedef DynamicPropertyMapWrap + wrapped_weight_t; + run_action<>()(g, get_correlation_histogram + (hist, bins, ret_bins), all_selectors(), all_selectors(), - weight_map_types()) + mpl::vector()) (deg1, deg2, weight); } diff --git a/src/graph/correlations/graph_correlations_imp2.cc b/src/graph/correlations/graph_correlations_imp2.cc deleted file mode 100644 index cd617bcd..00000000 --- a/src/graph/correlations/graph_correlations_imp2.cc +++ /dev/null @@ -1,49 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_correlations.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector weight_scalar_types; - -struct weight_map_types: property_map_types::apply< - weight_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_correlations_imp2(const GraphInterface& g, hist2d_t& hist, - boost::any deg1, boost::any deg2, - boost::any weight) -{ - run_action<>()(g, get_correlation_histogram(hist), - all_selectors(), all_selectors(), - weight_map_types()) - (deg1, deg2, weight); -} diff --git a/src/graph/correlations/graph_correlations_imp3.cc b/src/graph/correlations/graph_correlations_imp3.cc deleted file mode 100644 index e67b52ad..00000000 --- a/src/graph/correlations/graph_correlations_imp3.cc +++ /dev/null @@ -1,49 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_correlations.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector weight_scalar_types; - -struct weight_map_types: property_map_types::apply< - weight_scalar_types, - GraphInterface::edge_index_map_t>::type {}; // this includes the edge index - // map - -void graph_correlations_imp3(const GraphInterface& g, hist2d_t& hist, - boost::any deg1, boost::any deg2, - boost::any weight) -{ - run_action<>()(g, get_correlation_histogram(hist), - all_selectors(), all_selectors(), - weight_map_types()) - (deg1, deg2, weight); -} diff --git a/src/graph/correlations/graph_correlations_neighbours.cc b/src/graph/correlations/graph_correlations_neighbours.cc deleted file mode 100644 index 706c1998..00000000 --- a/src/graph/correlations/graph_correlations_neighbours.cc +++ /dev/null @@ -1,114 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_correlations_neighbours.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -// implementations spread across different compile units to minimize memory -// usage during compilation -void graph_correlations_neighbours_imp1(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight); -void graph_correlations_neighbours_imp2(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight); -void graph_correlations_neighbours_imp3(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight); -void graph_correlations_neighbours_imp4(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight); -void graph_correlations_neighbours_imp5(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight); -void graph_correlations_neighbours_imp6(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight); - - -avg_corr_t -GraphInterface::GetAverageNearestNeighboursCorrelation(deg_t origin_deg, - deg_t neighbours_deg, - std::string weight) - const -{ - avg_corr_t avg_corr; - - typedef ConstantPropertyMap cweight_map_t; - - try - { - any weight_prop; - if (weight != "") - weight_prop = prop(weight, _edge_index, _properties); - else - weight_prop = cweight_map_t(1); - - run_action<>() - (*this, get_average_nearest_neighbours_correlation - (avg_corr), all_selectors(), all_selectors(), - mpl::vector()) - (degree_selector(origin_deg, _properties), - degree_selector(neighbours_deg, _properties), weight_prop); - graph_correlations_neighbours_imp1 - (*this, avg_corr, degree_selector(origin_deg, _properties), - degree_selector(neighbours_deg, _properties), weight_prop); - graph_correlations_neighbours_imp2 - (*this, avg_corr, degree_selector(origin_deg, _properties), - degree_selector(neighbours_deg, _properties), weight_prop); - graph_correlations_neighbours_imp3 - (*this, avg_corr, degree_selector(origin_deg, _properties), - degree_selector(neighbours_deg, _properties), weight_prop); - graph_correlations_neighbours_imp4 - (*this, avg_corr, degree_selector(origin_deg, _properties), - degree_selector(neighbours_deg, _properties), weight_prop); - graph_correlations_neighbours_imp5 - (*this, avg_corr, degree_selector(origin_deg, _properties), - degree_selector(neighbours_deg, _properties), weight_prop); - graph_correlations_neighbours_imp6 - (*this, avg_corr, degree_selector(origin_deg, _properties), - degree_selector(neighbours_deg, _properties), weight_prop); - } - catch (dynamic_get_failure &e) - { - throw GraphException("error getting scalar property: " + - string(e.what())); - } - return avg_corr; -} diff --git a/src/graph/correlations/graph_correlations_neighbours.hh b/src/graph/correlations/graph_correlations_neighbours.hh deleted file mode 100644 index 16e994e4..00000000 --- a/src/graph/correlations/graph_correlations_neighbours.hh +++ /dev/null @@ -1,100 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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_NEIGHBOURS_HH -#define GRAPH_CORRELATIONS_NEIGHBOURS_HH - -#include "graph_filtering.hh" -#include "shared_map.hh" - -#include - -namespace graph_tool -{ -using namespace std; -using namespace boost; - -// return generalized average nearest neighbours correlation - -template -struct get_average_nearest_neighbours_correlation -{ - get_average_nearest_neighbours_correlation(AvgDeg& avg_deg) - : _avg_deg(avg_deg) {} - - template - void operator()(const Graph* gp, DegreeSelectorOrigin& origin_deg, - DegreeSelectorNeighbours& neighbours_deg, WeightMap weight) - const - { - const Graph& g = *gp; - tr1::unordered_map count; - SharedMap > s_count(count); - SharedMap s_avg_deg(_avg_deg); - - int i, N = num_vertices(g); - #pragma omp parallel for default(shared) private(i) \ - firstprivate(s_count, s_avg_deg) schedule(dynamic) - for (i = 0; i < N; ++i) - { - typename graph_traits::vertex_descriptor v = vertex(i, g); - if (v == graph_traits::null_vertex()) - continue; - - typename AvgDeg::key_type orig_deg = origin_deg(v,g); - - typename graph_traits::out_edge_iterator e, e_begin, e_end; - tie(e_begin,e_end) = out_edges(v,g); - for(e = e_begin; e != e_end; ++e) - { - typename AvgDeg::value_type::second_type::first_type deg = - neighbours_deg(target(*e,g),g); - s_avg_deg[orig_deg].first += deg*get(weight, *e); - s_avg_deg[orig_deg].second += deg*deg; - s_count[orig_deg] += get(weight,*e); - } - } - - s_count.Gather(); - s_avg_deg.Gather(); - - for (typeof(_avg_deg.begin()) iter = _avg_deg.begin(); - iter != _avg_deg.end(); ++iter) - { - double N = count[iter->first]; - iter->second.first /= N; - if (N > 1) - iter->second.second = - sqrt((iter->second.second - N*iter->second.first * - iter->second.first)/(N*(N-1))); - else - iter->second.second = 0.0; - } - } - AvgDeg& _avg_deg; -}; - -inline void operator+=(pair&a, const pair&b) -{ - a.first += b.first; - a.second += b.second; -} - -} // graph_tool namespace - -#endif // GRAPH_CORRELATIONS_NEIGHBOURS_HH diff --git a/src/graph/correlations/graph_correlations_neighbours_imp1.cc b/src/graph/correlations/graph_correlations_neighbours_imp1.cc deleted file mode 100644 index 7affb138..00000000 --- a/src/graph/correlations/graph_correlations_neighbours_imp1.cc +++ /dev/null @@ -1,48 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_correlations_neighbours.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector weight_scalar_types; - -struct weight_map_types: property_map_types::apply< - weight_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_correlations_neighbours_imp1(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight) -{ - run_action<>()(g, get_average_nearest_neighbours_correlation - (avg_corr), all_selectors(), all_selectors(), - weight_map_types())(origin_deg, neighbours_deg, weight); -} diff --git a/src/graph/correlations/graph_correlations_neighbours_imp2.cc b/src/graph/correlations/graph_correlations_neighbours_imp2.cc deleted file mode 100644 index a5891c15..00000000 --- a/src/graph/correlations/graph_correlations_neighbours_imp2.cc +++ /dev/null @@ -1,48 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_correlations_neighbours.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector weight_scalar_types; - -struct weight_map_types: property_map_types::apply< - weight_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_correlations_neighbours_imp2(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight) -{ - run_action<>()(g, get_average_nearest_neighbours_correlation - (avg_corr), all_selectors(), all_selectors(), - weight_map_types())(origin_deg, neighbours_deg, weight); -} diff --git a/src/graph/correlations/graph_correlations_neighbours_imp3.cc b/src/graph/correlations/graph_correlations_neighbours_imp3.cc deleted file mode 100644 index c2e5b8ba..00000000 --- a/src/graph/correlations/graph_correlations_neighbours_imp3.cc +++ /dev/null @@ -1,48 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_correlations_neighbours.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector weight_scalar_types; - -struct weight_map_types: property_map_types::apply< - weight_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_correlations_neighbours_imp3(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight) -{ - run_action<>()(g, get_average_nearest_neighbours_correlation - (avg_corr), all_selectors(), all_selectors(), - weight_map_types())(origin_deg, neighbours_deg, weight); -} diff --git a/src/graph/correlations/graph_correlations_neighbours_imp4.cc b/src/graph/correlations/graph_correlations_neighbours_imp4.cc deleted file mode 100644 index 984876aa..00000000 --- a/src/graph/correlations/graph_correlations_neighbours_imp4.cc +++ /dev/null @@ -1,48 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_correlations_neighbours.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector weight_scalar_types; - -struct weight_map_types: property_map_types::apply< - weight_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_correlations_neighbours_imp4(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight) -{ - run_action<>()(g, get_average_nearest_neighbours_correlation - (avg_corr), all_selectors(), all_selectors(), - weight_map_types())(origin_deg, neighbours_deg, weight); -} diff --git a/src/graph/correlations/graph_correlations_neighbours_imp5.cc b/src/graph/correlations/graph_correlations_neighbours_imp5.cc deleted file mode 100644 index 76425dac..00000000 --- a/src/graph/correlations/graph_correlations_neighbours_imp5.cc +++ /dev/null @@ -1,48 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_correlations_neighbours.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector weight_scalar_types; - -struct weight_map_types: property_map_types::apply< - weight_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_correlations_neighbours_imp5(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight) -{ - run_action<>()(g, get_average_nearest_neighbours_correlation - (avg_corr), all_selectors(), all_selectors(), - weight_map_types())(origin_deg, neighbours_deg, weight); -} diff --git a/src/graph/correlations/graph_correlations_neighbours_imp6.cc b/src/graph/correlations/graph_correlations_neighbours_imp6.cc deleted file mode 100644 index b6a5d362..00000000 --- a/src/graph/correlations/graph_correlations_neighbours_imp6.cc +++ /dev/null @@ -1,44 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_correlations_neighbours.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -struct weight_map_types: - mpl::vector {}; - -void graph_correlations_neighbours_imp6(const GraphInterface& g, - avg_corr_t& avg_corr, - boost::any origin_deg, - boost::any neighbours_deg, - boost::any weight) -{ - run_action<>()(g, get_average_nearest_neighbours_correlation - (avg_corr), all_selectors(), all_selectors(), - weight_map_types())(origin_deg, neighbours_deg, weight); -} diff --git a/src/graph/correlations/graph_edge_correlations.cc b/src/graph/correlations/graph_edge_correlations.cc deleted file mode 100644 index 1b1bb5e6..00000000 --- a/src/graph/correlations/graph_edge_correlations.cc +++ /dev/null @@ -1,56 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include - -#include "graph_edge_correlations.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -hist3d_t -GraphInterface::GetEdgeVertexCorrelationHistogram(GraphInterface::deg_t deg1, - string edge_scalar, - GraphInterface::deg_t deg2) - const -{ - hist3d_t hist; - - try - { - run_action<>()(*this, get_edge_correlation_histogram(hist), - all_selectors(), mpl::vector(), - all_selectors()) - (degree_selector(deg1, _properties), - prop(edge_scalar, _edge_index, _properties), - degree_selector(deg2, _properties)); - } - catch (dynamic_get_failure& e) - { - throw GraphException("error getting scalar property: " + - string(e.what())); - } - return hist; -} diff --git a/src/graph/correlations/graph_edge_correlations.hh b/src/graph/correlations/graph_edge_correlations.hh deleted file mode 100644 index 366605f5..00000000 --- a/src/graph/correlations/graph_edge_correlations.hh +++ /dev/null @@ -1,75 +0,0 @@ - -// -// Copyright (C) 2007 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_EDGE_CORRELATIONS_HH -#define GRAPH_EDGE_CORRELATIONS_HH - -#include -#include -#include "shared_map.hh" - -namespace graph_tool -{ - -using namespace std; -using namespace boost; - -// retrieves the generalized vertex-edge-vertex correlation histogram - -template -struct get_edge_correlation_histogram -{ - get_edge_correlation_histogram(Hist& hist): _hist(hist) {} - - template - void operator()(const Graph* gp, DegreeSelector1 deg1, - EdgeProperty edge_prop, DegreeSelector2 deg2) const - { - const Graph& g = *gp; - SharedMap s_hist(_hist); - - int i, N = num_vertices(g); - #pragma omp parallel for default(shared) private(i) \ - firstprivate(s_hist) schedule(dynamic) - for (i = 0; i < N; ++i) - { - typename graph_traits::vertex_descriptor v = vertex(i, g); - if (v == graph_traits::null_vertex()) - continue; - - typename Hist::key_type key; - tuples::get<0>(key) = deg1(v,g); - - typename graph_traits::out_edge_iterator e, e_begin, e_end; - tie(e_begin,e_end) = out_edges(v,g); - for(e = e_begin; e != e_end; ++e) - { - tuples::get<1>(key) = get(edge_prop, *e); - tuples::get<2>(key) = deg2(target(*e,g),g); - s_hist[key]++; - } - } - s_hist.Gather(); - } - - Hist& _hist; -}; - -} // graph_tool namespace - -#endif // GRAPH_EDGE_CORRELATIONS_HH diff --git a/src/graph/correlations/graph_edge_correlations_imp1.cc b/src/graph/correlations/graph_edge_correlations_imp1.cc deleted file mode 100644 index 1c312684..00000000 --- a/src/graph/correlations/graph_edge_correlations_imp1.cc +++ /dev/null @@ -1,48 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_edge_correlations.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector edge_scalar_types; - -struct edge_map_types: property_map_types::apply< - edge_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_edge_correlations_imp1(const GraphInterface& g, - hist3d_t& hist, - boost::any deg1, - boost::any edge_prop, - boost::any deg2) -{ - run_action<>()(g, get_edge_correlation_histogram(hist), - all_selectors(), edge_map_types(), all_selectors()) - (deg1, edge_prop, deg2); -} diff --git a/src/graph/correlations/graph_edge_correlations_imp2.cc b/src/graph/correlations/graph_edge_correlations_imp2.cc deleted file mode 100644 index e96cf429..00000000 --- a/src/graph/correlations/graph_edge_correlations_imp2.cc +++ /dev/null @@ -1,48 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_edge_correlations.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector edge_scalar_types; - -struct edge_map_types: property_map_types::apply< - edge_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_edge_correlations_imp2(const GraphInterface& g, - hist3d_t& hist, - boost::any deg1, - boost::any edge_prop, - boost::any deg2) -{ - run_action<>()(g, get_edge_correlation_histogram(hist), - all_selectors(), edge_map_types(), all_selectors()) - (deg1, edge_prop, deg2); -} diff --git a/src/graph/correlations/graph_edge_correlations_imp3.cc b/src/graph/correlations/graph_edge_correlations_imp3.cc deleted file mode 100644 index 70928e73..00000000 --- a/src/graph/correlations/graph_edge_correlations_imp3.cc +++ /dev/null @@ -1,48 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_edge_correlations.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector edge_scalar_types; - -struct edge_map_types: property_map_types::apply< - edge_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_edge_correlations_imp3(const GraphInterface& g, - hist3d_t& hist, - boost::any deg1, - boost::any edge_prop, - boost::any deg2) -{ - run_action<>()(g, get_edge_correlation_histogram(hist), - all_selectors(), edge_map_types(), all_selectors()) - (deg1, edge_prop, deg2); -} diff --git a/src/graph/correlations/graph_edge_correlations_imp4.cc b/src/graph/correlations/graph_edge_correlations_imp4.cc deleted file mode 100644 index 6bfaba03..00000000 --- a/src/graph/correlations/graph_edge_correlations_imp4.cc +++ /dev/null @@ -1,48 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_edge_correlations.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector edge_scalar_types; - -struct edge_map_types: property_map_types::apply< - edge_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_edge_correlations_imp4(const GraphInterface& g, - hist3d_t& hist, - boost::any deg1, - boost::any edge_prop, - boost::any deg2) -{ - run_action<>()(g, get_edge_correlation_histogram(hist), - all_selectors(), edge_map_types(), all_selectors()) - (deg1, edge_prop, deg2); -} diff --git a/src/graph/correlations/graph_edge_correlations_imp5.cc b/src/graph/correlations/graph_edge_correlations_imp5.cc deleted file mode 100644 index 3409cf15..00000000 --- a/src/graph/correlations/graph_edge_correlations_imp5.cc +++ /dev/null @@ -1,48 +0,0 @@ -// graph-tool -- a general graph modification and manipulation thingy -// -// Copyright (C) 2007 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 . - -#include "graph_filtering.hh" - -#include "graph.hh" -#include "histogram.hh" -#include "graph_selectors.hh" -#include "graph_properties.hh" - -#include "graph_edge_correlations.hh" - -using namespace std; -using namespace boost; -using namespace boost::lambda; -using namespace graph_tool; - -typedef mpl::vector edge_scalar_types; - -struct edge_map_types: property_map_types::apply< - edge_scalar_types, - GraphInterface::edge_index_map_t, - mpl::bool_ >::type {}; - -void graph_edge_correlations_imp5(const GraphInterface& g, - hist3d_t& hist, - boost::any deg1, - boost::any edge_prop, - boost::any deg2) -{ - run_action<>()(g, get_edge_correlation_histogram(hist), - all_selectors(), edge_map_types(), all_selectors()) - (deg1, edge_prop, deg2); -} diff --git a/src/graph/graph.cc b/src/graph/graph.cc index 0d590770..36195ea2 100644 --- a/src/graph/graph.cc +++ b/src/graph/graph.cc @@ -24,7 +24,6 @@ #include /* signal name macros, and the signal() prototype */ #include "graph.hh" -#include "histogram.hh" #include "graph_filtering.hh" #include "graph_properties.hh" diff --git a/src/graph/graph.hh b/src/graph/graph.hh index 70f12949..4ea581e8 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -127,6 +127,7 @@ public: python::dict GetVertexProperties() const; python::dict GetEdgeProperties() const; python::dict GetGraphProperties() const; + void PutPropertyMap(string name, const python::object& map); // used for graph properties graph_property_tag GetDescriptor() const { return graph_property_tag(); } @@ -168,13 +169,16 @@ private: // Arbitrary code execution template - friend void RunAction(GraphInterface &g, const Action& a); + friend void RunAction(GraphInterface& g, const Action& a); - friend boost::any degree_selector(deg_t deg, const GraphInterface&gi); + friend boost::any degree_selector(deg_t deg, const GraphInterface& gi); - friend boost::any vertex_prop(const string& name, const GraphInterface& gi); - friend boost::any edge_prop(const string& name, const GraphInterface& gi); - friend boost::any graph_prop(const string& name, const GraphInterface& gi); + friend boost::any vertex_prop(const string& name, const GraphInterface& gi, + bool dynamic); + friend boost::any edge_prop(const string& name, const GraphInterface& gi, + bool dynamic); + friend boost::any graph_prop(const string& name, const GraphInterface& gi, + bool dynamic); // python interface friend class PythonVertex; diff --git a/src/graph/graph_bind.cc b/src/graph/graph_bind.cc index ee153bdf..1c9fa6f9 100644 --- a/src/graph/graph_bind.cc +++ b/src/graph/graph_bind.cc @@ -18,6 +18,8 @@ #include "graph.hh" #include "graph_python_interface.hh" #include "graph_util.hh" +#define NUMPY_EXPORT +#include "numpy_bind.hh" #include #include @@ -152,6 +154,77 @@ struct pair_to_tuple } }; +template +struct pair_from_tuple +{ + pair_from_tuple() + { + converter::registry::push_back(&convertible, &construct, + boost::python::type_id >()); + } + + static void* convertible(PyObject* obj_ptr) + { + handle<> x(borrowed(obj_ptr)); + object o(x); + extract first(o[0]); + extract second(o[1]); + if (!first.check() || !second.check()) + return 0; + return obj_ptr; + } + + static void construct(PyObject* obj_ptr, + converter::rvalue_from_python_stage1_data* data) + { + handle<> x(borrowed(obj_ptr)); + object o(x); + pair value; + value.first = extract(o[0]); + value.second = extract(o[1]); + void* storage = + ( (boost::python::converter::rvalue_from_python_storage + >*) data)->storage.bytes; + new (storage) pair(value); + data->convertible = storage; + } +}; + +template +struct variant_from_python +{ + variant_from_python() + { + converter::registry::push_back + (&convertible, &construct, + boost::python::type_id()); + } + + static void* convertible(PyObject* obj_ptr) + { + handle<> x(borrowed(obj_ptr)); + object o(x); + extract str(o); + if (!str.check()) + return 0; + return obj_ptr; + } + + static void construct(PyObject* obj_ptr, + converter::rvalue_from_python_stage1_data* data) + { + handle<> x(borrowed(obj_ptr)); + object o(x); + ValueType value = extract(o); + GraphInterface::deg_t deg = value; + void* storage = + ( (boost::python::converter::rvalue_from_python_storage + *) data)->storage.bytes; + new (storage) GraphInterface::deg_t(deg); + data->convertible = storage; + } +}; + // persistent python object IO namespace graph_tool { @@ -171,6 +244,9 @@ void set_unpickler(python::object o) BOOST_PYTHON_MODULE(libgraph_tool_core) { + // numpy + import_array(); + GraphInterface().ExportPythonInterface(); PyModule_AddObject(python::detail::current_scope, "GraphError", pyex); @@ -224,9 +300,21 @@ BOOST_PYTHON_MODULE(libgraph_tool_core) .def("GetVertexProperties", &GraphInterface::GetVertexProperties) .def("GetEdgeProperties", &GraphInterface::GetEdgeProperties) .def("GetGraphProperties", &GraphInterface::GetGraphProperties) + .def("PutPropertyMap", &GraphInterface::PutPropertyMap) .def("InitSignalHandling", &GraphInterface::InitSignalHandling); + enum_("Degree") + .value("In", GraphInterface::IN_DEGREE) + .value("Out", GraphInterface::OUT_DEGREE) + .value("Total", GraphInterface::TOTAL_DEGREE); + + + variant_from_python(); + variant_from_python(); to_python_converter, pair_to_tuple >(); + to_python_converter, pair_to_tuple >(); + pair_from_tuple(); + pair_from_tuple(); class_("IStream", no_init).def("Read", &IStream::Read); class_("OStream", no_init).def("Write", &OStream::Write). diff --git a/src/graph/graph_io.cc b/src/graph/graph_io.cc index 92c6d3ed..77819d66 100644 --- a/src/graph/graph_io.cc +++ b/src/graph/graph_io.cc @@ -17,7 +17,6 @@ #include "graph_filtering.hh" #include "graph.hh" -#include "histogram.hh" #include "graph_properties.hh" #include "graph_util.hh" diff --git a/src/graph/graph_properties.cc b/src/graph/graph_properties.cc index aaee33a5..45b4ba78 100644 --- a/src/graph/graph_properties.cc +++ b/src/graph/graph_properties.cc @@ -104,7 +104,7 @@ PropertyNotFound::PropertyNotFound(const string& name, const type_info& key, // this function gets the dynamic property map inside dp which matches the given // name and key type dynamic_property_map& -find_property_map(const dynamic_properties& dp, string name, +find_property_map(const dynamic_properties& dp, const string& name, const type_info& key_type) { for(typeof(dp.begin()) iter = dp.begin(); iter != dp.end(); ++iter) @@ -114,19 +114,22 @@ find_property_map(const dynamic_properties& dp, string name, throw PropertyNotFound(name, key_type); } -boost::any vertex_prop(const string& name, const GraphInterface& gi) +boost::any vertex_prop(const string& name, const GraphInterface& gi, + bool dynamic) { - return prop(name, gi._vertex_index, gi._properties); + return prop(name, gi._vertex_index, gi._properties, dynamic); } -boost::any edge_prop(const string& name, const GraphInterface& gi) +boost::any edge_prop(const string& name, const GraphInterface& gi, + bool dynamic) { - return prop(name, gi._edge_index, gi._properties); + return prop(name, gi._edge_index, gi._properties, dynamic); } -boost::any graph_prop(const string& name, const GraphInterface& gi) +boost::any graph_prop(const string& name, const GraphInterface& gi, + bool dynamic) { ConstantPropertyMap graph_index(0); - return prop(name, graph_index, gi._properties); + return prop(name, graph_index, gi._properties, dynamic); } void GraphInterface::RemoveVertexProperty(string property) diff --git a/src/graph/graph_properties.hh b/src/graph/graph_properties.hh index 667f6ceb..2e78227a 100644 --- a/src/graph/graph_properties.hh +++ b/src/graph/graph_properties.hh @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -176,14 +177,14 @@ PropertyMap* get_static_property_map(dynamic_property_map* map) // this function gets the dynamic property map inside dp which matches the given // name and key type. It throws PropertyNotFound in case of failure dynamic_property_map& -find_property_map(const dynamic_properties& dp, string name, +find_property_map(const dynamic_properties& dp, const string& name, const type_info& key_type); // convenience function which finds and returns the appropriate static property // from the dynamic properties template PropertyMap& find_static_property_map(const dynamic_properties& dp, - string name) + const string& name) { typedef typename property_traits::key_type key_type; dynamic_property_map& dmap = find_property_map(dp, name, @@ -196,25 +197,32 @@ PropertyMap& find_static_property_map(const dynamic_properties& dp, struct get_static_prop; // forward decl. template boost::any prop(const string& name, IndexMap, - const dynamic_properties& dp) + const dynamic_properties& dp, bool dynamic = false) { using namespace lambda; typedef typename property_traits::key_type key_t; dynamic_property_map& dmap = find_property_map(dp, name, typeid(key_t)); boost::any prop; - typedef typename property_map_types::apply::type - properties_t; - bool found = false; - mpl::for_each(lambda::bind(get_static_prop(), - lambda::_1, - &dmap, var(prop), - var(found))); - if (!found) - throw PropertyNotFound(name, typeid(key_t), - "This is a graph-tool bug. :-( " - "Please follow but report instructions at " - PACKAGE_BUGREPORT); + if (dynamic) + { + prop = &dmap; + } + else + { + typedef typename property_map_types::apply::type + properties_t; + bool found = false; + mpl::for_each(lambda::bind(get_static_prop(), + lambda::_1, + &dmap, var(prop), + var(found))); + if (!found) + throw PropertyNotFound(name, typeid(key_t), + "This is a graph-tool bug. :-( " + "Please follow but report instructions at " + PACKAGE_BUGREPORT); + } return prop; } @@ -234,9 +242,12 @@ struct get_static_prop } }; -boost::any vertex_prop(const string& name, const GraphInterface& gi); -boost::any edge_prop(const string& name, const GraphInterface& gi); -boost::any graph_prop(const string& name, const GraphInterface& gi); +boost::any vertex_prop(const string& name, const GraphInterface& gi, + bool dynamic = false); +boost::any edge_prop(const string& name, const GraphInterface& gi, + bool dynamic = false); +boost::any graph_prop(const string& name, const GraphInterface& gi, + bool dynamic = false); // this functor tests whether or not a given boost::any object holds a type // contained in a given type Sequence @@ -352,64 +363,10 @@ struct dynamic_properties_copy: public dynamic_properties } }; -// this function gets the value in the map corresponding to the given key, -// converted to ConvertedType, or throws bad_lexical_cast. - -template -struct AttemptAnyConversion; // forward declaration - -template -ConvertedType get_converted_scalar_value(dynamic_property_map& dmap, - const Key& key) -{ - ConvertedType target = ConvertedType(); - const boost::any& source = dmap.get(key); - bool success; - if (dmap.value() == typeid(ConvertedType)) - { - target = any_cast(dmap.get(key)); - success = true; - } - else - { - mpl::for_each - (AttemptAnyConversion(target, source, success)); - } - if (!success) - throw bad_lexical_cast(); - return target; -} - -template -struct AttemptAnyConversion -{ - AttemptAnyConversion(T& value, const boost::any& source, bool& success) - :_value(value), _source(source), _success(success) - { - _success = false; - } - - template - void operator()(Source) - { - try - { - _value = lexical_cast(any_cast(_source)); - _success = true; - } - catch (bad_any_cast){} - catch (bad_lexical_cast){} - } - - T& _value; - const boost::any& _source; - bool& _success; -}; - // the following class wraps a dynamic_property_map, so it can be used as a // regular property map. The values are converted to the desired Value type, -// which may cause a performance impact. Should be used only when property map -// access time is not crucial +// which may cause a performance impact, since virtual functions are +// used. Should be used only when property map access time is not crucial template class DynamicPropertyMapWrap { @@ -419,12 +376,23 @@ public: typedef Key key_type; typedef read_write_property_map_tag category; - DynamicPropertyMapWrap(dynamic_property_map& dmap):_dmap(&dmap) {} + DynamicPropertyMapWrap(dynamic_property_map& dmap) + :_dmap(&dmap) + { + ValueConverter* converter = 0; + mpl::for_each + (lambda::bind(choose_converter(), lambda::_1, + lambda::var(_dmap), lambda::var(converter))); + if (converter == 0) + throw bad_lexical_cast(); + else + _converter = tr1::shared_ptr(converter); + } DynamicPropertyMapWrap() {} Value get(const Key& k) const { - return get_converted_scalar_value(*_dmap, k); + return (*_converter)(*_dmap, k); } void put(const Key& k, const Value& val) @@ -433,19 +401,49 @@ public: } private: + class ValueConverter + { + public: + virtual Value operator()(dynamic_property_map& map, const Key& k) = 0; + }; + + template + class ValueConverterImp: public ValueConverter + { + public: + virtual Value operator()(dynamic_property_map& dmap, const Key& k) + { + OrigValue val = any_cast(dmap.get(k)); + return lexical_cast(val); + } + }; + + struct choose_converter + { + template + void operator()(Type, dynamic_property_map* dmap, + ValueConverter*& converter) const + { + if (typeid(Type) == dmap->value()) + converter = new ValueConverterImp(); + } + }; + dynamic_property_map* _dmap; + tr1::shared_ptr _converter; }; -template +template Value get(const graph_tool::DynamicPropertyMapWrap& pmap, - const Key& k) + ConvKey k) { - return pmap.get(k); + Key key = k; + return pmap.get(key); } template -void put(graph_tool::DynamicPropertyMapWrap pmap, - const Key& k, const Value& val) +void put(graph_tool::DynamicPropertyMapWrap& pmap, + Key k, const Value& val) { pmap.put(k, val); } diff --git a/src/graph/graph_python_interface.cc b/src/graph/graph_python_interface.cc index 04e60e39..ea6e6ca3 100644 --- a/src/graph/graph_python_interface.cc +++ b/src/graph/graph_python_interface.cc @@ -236,6 +236,28 @@ void GraphInterface::RemoveEdge(const python::object& e) remove_edge(de, _mg); } +// +// Property map manipulation +// +// useful typedefs: + +typedef property_map_types::apply< + value_types, + GraphInterface::vertex_index_map_t, + mpl::bool_ + >::type vertex_property_maps; + +typedef property_map_types::apply< + value_types, + GraphInterface::edge_index_map_t, + mpl::bool_ + >::type edge_property_maps; + +typedef property_map_types::apply< + value_types, + ConstantPropertyMap + >::type graph_property_maps; + struct get_property_map { get_property_map(const string& name, dynamic_property_map& dp, @@ -266,11 +288,6 @@ python::dict GraphInterface::GetVertexProperties() const { typedef graph_traits::vertex_descriptor vertex_t; - typedef property_map_types::apply< - value_types, - vertex_index_map_t, - mpl::bool_ - >::type vertex_property_maps; python::dict props; for(typeof(_properties.begin()) iter = _properties.begin(); iter != _properties.end(); ++iter) @@ -288,11 +305,6 @@ python::dict GraphInterface::GetEdgeProperties() const { typedef graph_traits::edge_descriptor edge_t; - typedef property_map_types::apply< - value_types, - edge_index_map_t, - mpl::bool_ - >::type edge_property_maps; python::dict props; for(typeof(_properties.begin()) iter = _properties.begin(); iter != _properties.end(); ++iter) @@ -309,10 +321,6 @@ GraphInterface::GetEdgeProperties() const python::dict GraphInterface::GetGraphProperties() const { - typedef property_map_types::apply< - value_types, - ConstantPropertyMap - >::type graph_property_maps; python::dict props; for(typeof(_properties.begin()) iter = _properties.begin(); iter != _properties.end(); ++iter) @@ -326,6 +334,52 @@ GraphInterface::GetGraphProperties() const return props; } +struct put_property_map +{ + template + void operator()(PropertyMap, const string& name, dynamic_properties& dp, + const python::object& pmap, bool& found) const + { + python::extract > map(pmap); + if (map.check()) + { + PythonPropertyMap python_map = map(); + try + { + typedef typename property_traits::key_type key_t; + find_property_map(dp, name, typeid(key_t)); + throw GraphException("cannot put property: property of name " + + name + " of the same key type already" + " exists"); + } + catch (const PropertyNotFound&) + { + dp.property(name, python_map.GetMap()); + found = true; + } + } + } +}; + +void GraphInterface::PutPropertyMap(string name, const python::object& map) +{ + bool found = false; + mpl::for_each + (lambda::bind(put_property_map(), lambda::_1, lambda::var(name), + lambda::var(_properties), lambda::var(map), + lambda::var(found))); + mpl::for_each + (lambda::bind(put_property_map(), lambda::_1, lambda::var(name), + lambda::var(_properties), lambda::var(map), + lambda::var(found))); + mpl::for_each + (lambda::bind(put_property_map(), lambda::_1, lambda::var(name), + lambda::var(_properties), lambda::var(map), + lambda::var(found))); + if (!found) + throw GraphException("cannot put property: property map not recognized"); +} + // // Below are the functions with will properly register all the types to python, // for every filter, type, etc. diff --git a/src/graph/graph_python_interface.hh b/src/graph/graph_python_interface.hh index d66bc401..c8a89418 100644 --- a/src/graph/graph_python_interface.hh +++ b/src/graph/graph_python_interface.hh @@ -358,7 +358,7 @@ template class PythonPropertyMap { public: - PythonPropertyMap(const std::string& name, PropertyMap& pmap) + PythonPropertyMap(const std::string& name, const PropertyMap& pmap) : _name(name), _pmap(pmap) {} typedef typename property_traits::value_type value_type; @@ -409,9 +409,14 @@ public: return type_names[mpl::find::type::pos::value]; } + PropertyMap GetMap() const + { + return _pmap; + } + private: const std::string& _name; - PropertyMap& _pmap; + PropertyMap _pmap; // hold an internal copy, since it's cheap }; diff --git a/src/graph/histogram.hh b/src/graph/histogram.hh index 998a51b1..fde6c69c 100644 --- a/src/graph/histogram.hh +++ b/src/graph/histogram.hh @@ -51,17 +51,22 @@ public: typedef typename boost::mpl::if_, ValueType, double>::type mean_t; - Histogram(const boost::array, 2>& bin_sizes, + Histogram(const boost::array, Dim>& bins, const boost::array,Dim>& data_range) - : _bin_sizes(bin_sizes), _data_range(data_range) + : _bins(bins), _data_range(data_range) { bin_t new_shape; for (size_t j = 0; j < Dim; ++j) - if (_bin_sizes[j].size() == 1) - new_shape[j] = _bin_sizes[j][0]; + if (_bins[j].size() == 1) // constant bin width + { + new_shape[j] = floor((_data_range[j].second - + _data_range[j].first)/_bins[j][0]) + 1; + } else - new_shape[j] = _bin_sizes[j].size(); + { + new_shape[j] = _bins[j].size(); + } _counts.resize(new_shape); } @@ -70,13 +75,13 @@ public: bin_t bin; for (size_t i = 0; i < Dim; ++i) { - if (_bin_sizes[i].size() == 1) // constant bin width + if (_bins[i].size() == 1) // constant bin width { - bin[i] = (v[i] - _data_range[i].first)/_bin_sizes[i][0]; + bin[i] = (v[i] - _data_range[i].first)/_bins[i][0]; } else // arbitrary bins. do a binary search { - std::vector& bins = _bin_sizes[i]; + std::vector& bins = _bins[i]; typeof(bins.begin()) iter = upper_bound(bins.begin(), bins.end(), v[i]); if (iter == bins.end()) // larger than any bin, thus belongs to @@ -98,9 +103,28 @@ public: boost::multi_array& GetArray() { return _counts; } + boost::array, Dim> GetBins() + { + boost::array, Dim> bins; + for (size_t j = 0; j < Dim; ++j) + if (_bins[j].size() == 1) // constant bin width + { + for (ValueType i = _data_range[j].first; + i <= _data_range[j].second; i += _bins[j][0]) + { + bins[j].push_back(i); + } + } + else + { + bins[j] = _bins[j]; + } + return bins; + } + protected: boost::multi_array _counts; - boost::array, Dim> _bin_sizes; + boost::array, Dim> _bins; boost::array,Dim> _data_range; }; @@ -132,7 +156,7 @@ public: _sum->GetArray().resize(shape); for (size_t i = 0; i < this->_counts.num_elements(); ++i) { - _sum->GetArray()[i][0] += this->_counts[i][0]; + _sum->GetArray().data()[i] += this->_counts.data()[i]; } } _sum = 0; diff --git a/src/graph/numpy_bind.hh b/src/graph/numpy_bind.hh index fb77f695..fbec6521 100644 --- a/src/graph/numpy_bind.hh +++ b/src/graph/numpy_bind.hh @@ -15,9 +15,18 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef NUMPY_BIND_HH +#define NUMPY_BIND_HH + #include #include -#include "ndarrayobject.h" + +// numpy unique symbol weirdness +#define PY_ARRAY_UNIQUE_SYMBOL graph_tool_numpy +#ifndef NUMPY_EXPORT +#define NO_IMPORT_ARRAY +#endif +#include "arrayobject.h" #include #define BOOST_DISABLE_ASSERTS @@ -47,13 +56,21 @@ typedef mpl::map< template python::object wrap_vector_owned(vector& vec) { - ValueType* new_data = new ValueType[vec.size()]; - memcpy(new_data, &vec[0], vec.size()*sizeof(ValueType)); - int size = vec.size(); int val_type = mpl::at::type::value; - PyArrayObject* ndarray = - (PyArrayObject*) PyArray_SimpleNewFromData(1, &size, val_type, - new_data); + npy_intp size[1]; + size[0] = vec.size(); + PyArrayObject* ndarray; + if (vec.empty()) + { + ndarray = (PyArrayObject*) PyArray_SimpleNew(1, size, val_type); + } + else + { + ValueType* new_data = new ValueType[vec.size()]; + memcpy(new_data, &vec[0], vec.size()*sizeof(ValueType)); + ndarray = (PyArrayObject*) PyArray_SimpleNewFromData(1, size, val_type, + new_data); + } ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_OWNDATA | NPY_WRITEABLE; handle<> x(borrowed((PyObject*) ndarray)); @@ -64,10 +81,14 @@ python::object wrap_vector_owned(vector& vec) template python::object wrap_vector_not_owned(vector& vec) { - int size = vec.size(); + PyArrayObject* ndarray; int val_type = mpl::at::type::value; - PyArrayObject* ndarray = - (PyArrayObject*) PyArray_SimpleNewFromData(1, &size, val_type, &vec[0]); + int size = vec.size(); + if (vec.empty()) + ndarray = (PyArrayObject*) PyArray_SimpleNew(1, &size, val_type); + else + ndarray = (PyArrayObject*) PyArray_SimpleNewFromData(1, &size, val_type, + &vec[0]); ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_WRITEABLE; handle<> x(borrowed((PyObject*) ndarray)); object o(x); @@ -79,7 +100,7 @@ template python::object wrap_multi_array_owned(multi_array& array) { ValueType* new_data = new ValueType[array.num_elements()]; - memcpy(new_data, array.origin(), array.num_elements()*sizeof(ValueType)); + memcpy(new_data, array.data(), array.num_elements()*sizeof(ValueType)); int val_type = mpl::at::type::value; npy_intp shape[Dim]; for (int i = 0; i < Dim; ++i) @@ -87,8 +108,8 @@ python::object wrap_multi_array_owned(multi_array& array) PyArrayObject* ndarray = (PyArrayObject*) PyArray_SimpleNewFromData(Dim, shape, val_type, new_data); - ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_OWNDATA | - NPY_WRITEABLE; +// ndarray->flags = NPY_ALIGNED | NPY_C_CONTIGUOUS | NPY_OWNDATA | +// NPY_WRITEABLE; handle<> x(borrowed((PyObject*) ndarray)); object o(x); return o; @@ -106,3 +127,5 @@ python::object wrap_multi_array_not_owned(multi_array& array) object o(x); return o; } + +#endif // NUMPY_BIND_HH diff --git a/src/graph_tool/Makefile.am b/src/graph_tool/Makefile.am index 85588b5f..4845ace4 100644 --- a/src/graph_tool/Makefile.am +++ b/src/graph_tool/Makefile.am @@ -2,7 +2,8 @@ graph_tool_PYTHON = \ __init__.py \ - core.py + core.py \ + io.py graph_tooldir = $(pythondir)/graph_tool graph_tool_run_action_PYTHON = \ @@ -16,3 +17,11 @@ graph_tool_test_PYTHON = \ test/basic.py \ test/properties.py graph_tool_testdir = $(pythondir)/graph_tool/test + +graph_tool_generation_PYTHON = \ + generation/__init__.py +graph_tool_generationdir = $(pythondir)/graph_tool/generation + +graph_tool_correlations_PYTHON = \ + correlations/__init__.py +graph_tool_correlationsdir = $(pythondir)/graph_tool/correlations diff --git a/src/graph_tool/core.py b/src/graph_tool/core.py index e405d8a1..dc2c27ff 100644 --- a/src/graph_tool/core.py +++ b/src/graph_tool/core.py @@ -31,12 +31,12 @@ except ImportError: _orig_dlopen_flags = sys.getdlopenflags() sys.setdlopenflags(RTLD_LAZY|RTLD_GLOBAL) -import libgraph_tool_core +import libgraph_tool_core as libcore sys.setdlopenflags(_orig_dlopen_flags) # reset it to normal case to avoid # unnecessary symbol collision -__version__ = libgraph_tool_core.mod_info().version +__version__ = libcore.mod_info().version -import io # sets up libgraph_tool_core io routines +import io # sets up libcore io routines import os, os.path, re, struct, fcntl, termios, gzip, bz2, string,\ textwrap, time, signal, traceback, shutil, time, math, inspect, \ @@ -51,11 +51,11 @@ def _degree(name): """Retrieve the degree type from string""" deg = name if name == "in-degree" or name == "in": - deg = libgraph_tool_core.Degree.In + deg = libcore.Degree.In if name == "out-degree" or name == "out": - deg = libgraph_tool_core.Degree.Out + deg = libcore.Degree.Out if name == "total-degree" or name == "total": - deg = libgraph_tool_core.Degree.Total + deg = libcore.Degree.Total return deg def _parse_range(range): @@ -135,7 +135,7 @@ def _handle_exceptions(func): try: return func(*args, **kwargs) except (IOError, RuntimeError), e: - libgraph_tool_core.raise_error(str(e)) + libcore.raise_error(str(e)) return wrap def _limit_args(allowed_vals): @@ -171,15 +171,15 @@ class Graph(object): def __init__(self, g = None): if g == None: - self.__graph = libgraph_tool_core.GraphInterface() + self.__graph = libcore.GraphInterface() else: - self.__graph = libgraph_tool_core.GraphInterface(g.__graph) + self.__graph = libcore.GraphInterface(g.__graph) @_handle_exceptions def copy(self): """Returns a deep copy of self""" new_graph = Graph() - new_graph.__graph = libgraph_tool_core.GraphInterface(self.__graph) + new_graph.__graph = libcore.GraphInterface(self.__graph) return new_graph # Graph access @@ -240,7 +240,8 @@ class Graph(object): def __get_vertex_properties(self): return PropertyDict(self, self.__graph.GetVertexProperties(), lambda g, key: g.get_vertex_property(key), - None, + lambda g, key, value: g.set_vertex_property(key, + value), lambda g, key: g.remove_vertex_property(key), lambda g, key: g.__graph.GetVertexProperties()[key]\ .value_type()) @@ -251,7 +252,8 @@ class Graph(object): def __get_edge_properties(self): return PropertyDict(self, self.__graph.GetEdgeProperties(), lambda g, key: g.get_edge_property(key), - None, + lambda g, key, value: g.set_edge_property(key, + value), lambda g, key: g.remove_edge_property(key), lambda g, key: g.__graph.GetEdgeProperties()[key]\ .value_type()) @@ -266,7 +268,8 @@ class Graph(object): lambda g, key: g.get_graph_property(key), lambda g, key, val: g.set_graph_property(key, val), lambda g, key: g.remove_graph_property(key), - lambda g, key: g.__graph.GetGraphProperties()[key].value_type()) + lambda g, key: g.__graph.GetGraphProperties()[key]\ + .value_type()) @_handle_exceptions def __set_graph_properties(self, val): @@ -308,7 +311,7 @@ class Graph(object): filename.endswith(".dot.bz2"): format = "dot" else: - libgraph_tool_core.raise_error\ + libcore.raise_error\ ("cannot determine file format of: " + filename ) elif format == "auto": format = "xml" @@ -330,7 +333,7 @@ class Graph(object): filename.endswith(".dot.bz2"): format = "dot" else: - libgraph_tool_core.raise_error\ + libcore.raise_error\ ("cannot determine file format of: " + filename ) elif format == "auto": format = "xml" @@ -472,6 +475,22 @@ class Graph(object): if val != None: self.set_graph_property(name, val) + @_handle_exceptions + def set_vertex_property(self, name, pmap): + """Insert or replaces a vertex property map object 'map' with a give + name""" + if name in self.vertex_properties.keys(): + del self.vertex_properties[name] + self.__graph.PutPropertyMap(name, pmap) + + @_handle_exceptions + def set_edge_property(self, name, pmap): + """Insert or replaces an edge property map object 'map' with a give + name""" + if name not in self.edge_properties.keys(): + del self.edge_properties[name] + self.__graph.PutPropertyMap(name, pmap) + @_attrs(opt_group=__groups[-1]) @_handle_exceptions def set_graph_property(self, property, val): @@ -624,4 +643,4 @@ class PropertyDict(dict): def value_types(): """Return a list of possible properties value types""" - return libgraph_tool_core.get_property_types() + return libcore.get_property_types() diff --git a/src/graph_tool/correlations/__init__.py b/src/graph_tool/correlations/__init__.py new file mode 100644 index 00000000..0190fede --- /dev/null +++ b/src/graph_tool/correlations/__init__.py @@ -0,0 +1,100 @@ +#! /usr/bin/env python +# graph_tool.py -- a general graph manipulation python module +# +# Copyright (C) 2007 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 . + +import sys +# RTLD_GLOBAL needs to be set in dlopen() if we want typeinfo and friends to +# work properly across DSO boundaries. See http://gcc.gnu.org/faq.html#dso + +# The "except" is because the dl module raises a system error on ia64 and x86_64 +# systems because "int" and addresses are different sizes. +try: + from dl import RTLD_LAZY, RTLD_NOW, RTLD_GLOBAL +except ImportError: + RTLD_LAZY = 1 + RTLD_NOW = 2 + RTLD_GLOBAL = 256 +_orig_dlopen_flags = sys.getdlopenflags() + +sys.setdlopenflags(RTLD_LAZY|RTLD_GLOBAL) +import libgraph_tool_correlations +sys.setdlopenflags(_orig_dlopen_flags) # reset it to normal case to avoid + # unnecessary symbol collision + +from .. core import _degree +from numpy import * + +__all__ = ["assortativity", "scalar_assortativity", + "corr_hist", "combined_corr_hist", "avg_neighbour_corr"] + +def assortativity(g, deg): + return libgraph_tool_correlations.\ + assortativity_coefficient(g.underlying_graph(), _degree(deg)) + +def scalar_assortativity(g, deg): + return libgraph_tool_correlations.\ + scalar_assortativity_coefficient(g.underlying_graph(), + _degree(deg)) + +def corr_hist(g, deg1, deg2, weight="", bins=[[1],[1]]): + ret = libgraph_tool_correlations.\ + vertex_correlation_histogram(g.underlying_graph(), _degree(deg1), + _degree(deg2), weight, bins[0], bins[1]) + return [ret[0], [ret[1][0], ret[1][1]]] + +def combined_corr_hist(g, deg1, deg2, bins=[[1],[1]]): + ret = libgraph_tool_correlations.\ + vertex_combined_correlation_histogram(g.underlying_graph(), + _degree(deg1), _degree(deg2), + bins[0], bins[1]) + return [ret[0], [ret[1][0], ret[1][1]]] + +def avg_neighbour_corr(g, deg1, deg2, weight="", bins=[[1],[1]]): + ret = corr_hist(g, deg1, deg2, weight, bins) + xbins = ret[1][0] + ybins = ret[1][1] + counts = ret[0] + avg = empty((counts.shape[0])) + dev = empty((counts.shape[0])) + mask = empty((counts.shape[0]), dtype=dtype('bool')) + n_masked = 0 + for i in xrange(0, len(ret[0])): + N = counts[i,:].sum() + if N > 0: + avg[i] = average(ybins, weights=counts[i,:]) + dev[i] = sqrt(average((ybins-avg[i])**2, + weights=counts[i,:]))/sqrt(N) + mask[i] = False + else: + mask[i] = True + n_masked += 1 + if n_masked > 0: # remove empty bins + navg = empty(len(avg) - n_masked) + ndev = empty(len(dev) - n_masked) + nxbins = empty(len(xbins) - n_masked) + cum = 0 + for i in xrange(0, len(avg)): + if not mask[i]: + navg[i-cum] = avg[i] + ndev[i-cum] = dev[i] + nxbins[i-cum] = xbins[i] + else: + cum += 1 + avg = navg + dev = ndev + xbins = nxbins + return [avg, dev, xbins] diff --git a/src/graph_tool/generation/__init__.py b/src/graph_tool/generation/__init__.py new file mode 100644 index 00000000..45f2c562 --- /dev/null +++ b/src/graph_tool/generation/__init__.py @@ -0,0 +1,61 @@ +#! /usr/bin/env python +# graph_tool.py -- a general graph manipulation python module +# +# Copyright (C) 2007 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 . + +import sys +# RTLD_GLOBAL needs to be set in dlopen() if we want typeinfo and friends to +# work properly across DSO boundaries. See http://gcc.gnu.org/faq.html#dso + +# The "except" is because the dl module raises a system error on ia64 and x86_64 +# systems because "int" and addresses are different sizes. +try: + from dl import RTLD_LAZY, RTLD_NOW, RTLD_GLOBAL +except ImportError: + RTLD_LAZY = 1 + RTLD_NOW = 2 + RTLD_GLOBAL = 256 +_orig_dlopen_flags = sys.getdlopenflags() + +sys.setdlopenflags(RTLD_LAZY|RTLD_GLOBAL) +import libgraph_tool_generation +sys.setdlopenflags(_orig_dlopen_flags) # reset it to normal case to avoid + # unnecessary symbol collision + +from .. core import Graph +import random + +__all__ = ["random_graph"] + + + +def random_graph(N, deg_sampler, deg_corr=None, directed=True, + parallel=False, self_loops=False, + seed=0, verbose=False): + if seed != 0: + seed = random.randint(0, sys.maxint) + g = Graph() + if deg_corr == None: + uncorrelated = True + else: + uncorrelated = False + libgraph_tool_generation.gen_random_graph(g.underlying_graph(), N, + deg_sampler, deg_corr, + uncorrelated, not parallel, + not self_loops, not directed, + seed, verbose) + g.set_directed(directed) + return g -- GitLab