// 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, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include "graph.hh" using namespace std; using namespace graph_tool; using namespace boost; using namespace boost::python; // some conversions... template struct pair_to_tuple { static PyObject* convert(const pair& p) { boost::python::tuple t = boost::python::make_tuple(p.first,p.second); return incref(t.ptr()); } }; template struct tuple_to_tuple { static PyObject* convert(const boost::tuple& p) { boost::python::tuple t = boost::python::make_tuple(get<0>(p),get<1>(p),get<2>(p)); return incref(t.ptr()); } }; template struct tuple_from_tuple { tuple_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]); extract third(o[3]); if (!first.check() || !second.check() || !third.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); boost::tuple value; get<0>(value) = extract(o[0]); get<1>(value) = extract(o[1]); get<2>(value) = extract(o[2]); void* storage = ( (boost::python::converter::rvalue_from_python_storage >*) data)->storage.bytes; new (storage) boost::tuple(value); data->convertible = storage; } }; 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; } }; template struct hist_to_dict { static PyObject* convert(const Hist& h) { dict hist; for(typeof(h.begin()) iter = h.begin(); iter != h.end(); ++iter) hist[iter->first] = iter->second; return incref(hist.ptr()); } }; struct pos_t_to_tuple { static PyObject* convert(const pos_t& p) { boost::python::tuple t = boost::python::make_tuple(p.x,p.y); return incref(t.ptr()); } }; struct pos_t_from_tuple { pos_t_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); pos_t value; value.x = extract(o[0]); value.y = extract(o[1]); void* storage = ( (boost::python::converter::rvalue_from_python_storage*) data)->storage.bytes; new (storage) pos_t(value); data->convertible = storage; } }; class GraphInterfaceWrap: public GraphInterface { public: void GenerateCorrelatedConfigurationalModel(size_t N, object pjk, object ceil_pjk, object inv_ceil_pjk, double ceil_pjk_bound, object corr, object ceil_corr, object inv_ceil_corr, double ceil_corr_bound, bool undirected_corr, size_t seed, bool verbose) { GraphInterface& base = *this; base.GenerateCorrelatedConfigurationalModel(N, pjk_t(python_function(pjk)), pjk_t(python_function(ceil_pjk)), inv_ceil_t(python_function(inv_ceil_pjk)), ceil_pjk_bound, corr_t(python_function(corr)), corr_t(python_function(ceil_corr)), inv_corr_t(python_function(inv_ceil_corr)), ceil_corr_bound, undirected_corr, seed, verbose); } struct python_function { python_function(object o): _o(o) {} double operator()(size_t j, size_t k) { return extract(_o(j,k)); } double operator()(size_t jl, size_t kl, size_t j, size_t k) { return extract(_o(jl,kl,j,k)); } pair operator()(double r1, double r2) { object retval = _o(r1,r2); return make_pair(size_t(max(int(extract(retval[0])),0)), size_t(max(int(extract(retval[1])),0))); } pair operator()(double r1, double r2, size_t j, size_t k) { object retval = _o(r1,r2,j,k); return make_pair(size_t(max(int(extract(retval[0])),0)), size_t(max(int(extract(retval[1])),0))); } object _o; }; }; struct LibInfo { string GetName() const {return PACKAGE_NAME;} string GetAuthor() const {return AUTHOR;} string GetCopyright() const {return COPYRIGHT;} string GetVersion() const {return VERSION " (commit " GIT_COMMIT ", " GIT_COMMIT_DATE ")";} string GetLicense() const {return "GPL version 3 or above";} }; // overloads void (GraphInterfaceWrap::*ReadFromFile1) (string) = &GraphInterfaceWrap::ReadFromFile; void (GraphInterfaceWrap::*ReadFromFile2) (string, string) = &GraphInterfaceWrap::ReadFromFile; void (GraphInterfaceWrap::*WriteToFile1) (string) = &GraphInterfaceWrap::WriteToFile; void (GraphInterfaceWrap::*WriteToFile2) (string, string) = &GraphInterfaceWrap::WriteToFile; BOOST_PYTHON_MODULE(libgraph_tool) { class_("GraphInterface") .def("GenerateCorrelatedConfigurationalModel", &GraphInterfaceWrap::GenerateCorrelatedConfigurationalModel) .def("GetNumberOfVertices", &GraphInterfaceWrap::GetNumberOfVertices) .def("GetNumberOfEdges", &GraphInterfaceWrap::GetNumberOfEdges) .def("GetVertexHistogram", &GraphInterfaceWrap::GetVertexHistogram) .def("GetEdgeHistogram", &GraphInterfaceWrap::GetEdgeHistogram) .def("LabelComponents", &GraphInterfaceWrap::LabelComponents) .def("LabelParallelEdges", &GraphInterfaceWrap::LabelParallelEdges) .def("GetCombinedVertexHistogram", &GraphInterfaceWrap::GetCombinedVertexHistogram) .def("GetAverageCombinedVertexCorrelation", &GraphInterfaceWrap::GetAverageCombinedVertexCorrelation) .def("GetVertexCorrelationHistogram", &GraphInterfaceWrap::GetVertexCorrelationHistogram) .def("GetEdgeVertexCorrelationHistogram", &GraphInterfaceWrap::GetEdgeVertexCorrelationHistogram) .def("GetAverageNearestNeighboursCorrelation", &GraphInterfaceWrap::GetAverageNearestNeighboursCorrelation) .def("GetAssortativityCoefficient", &GraphInterfaceWrap::GetAssortativityCoefficient) .def("GetScalarAssortativityCoefficient", &GraphInterfaceWrap::GetScalarAssortativityCoefficient) .def("GetGlobalClustering", &GraphInterfaceWrap::GetGlobalClustering) .def("SetLocalClusteringToProperty", &GraphInterfaceWrap::SetLocalClusteringToProperty) .def("SetExtendedClusteringToProperty", &GraphInterfaceWrap::SetExtendedClusteringToProperty) .def("GetDistanceHistogram", &GraphInterfaceWrap::GetDistanceHistogram) .def("GetSampledDistanceHistogram", &GraphInterfaceWrap::GetSampledDistanceHistogram) .def("GetReciprocity", &GraphInterfaceWrap::GetReciprocity) .def("GetMinimumSpanningTree", &GraphInterfaceWrap::GetMinimumSpanningTree) .def("GetLineGraph", &GraphInterfaceWrap::GetLineGraph) .def("GetBetweenness", &GraphInterfaceWrap::GetBetweenness) .def("GetCentralPointDominance", &GraphInterfaceWrap::GetCentralPointDominance) .def("GetCommunityStructure", &GraphInterfaceWrap::GetCommunityStructure) .def("GetCommunityNetwork", &GraphInterfaceWrap::GetCommunityNetwork) .def("GetModularity", &GraphInterfaceWrap::GetModularity) .def("RandomRewire", &GraphInterfaceWrap::RandomRewire) .def("SetDirected", &GraphInterfaceWrap::SetDirected) .def("GetDirected", &GraphInterfaceWrap::GetDirected) .def("SetReversed", &GraphInterfaceWrap::SetReversed) .def("GetReversed", &GraphInterfaceWrap::GetReversed) .def("SetVertexFilterProperty", &GraphInterfaceWrap::SetVertexFilterProperty) .def("IsVertexFilterActive", &GraphInterfaceWrap::IsVertexFilterActive) .def("SetEdgeFilterProperty", &GraphInterfaceWrap::SetEdgeFilterProperty) .def("SetEdgeFilterRange", &GraphInterfaceWrap::SetEdgeFilterRange) .def("IsEdgeFilterActive", &GraphInterfaceWrap::IsEdgeFilterActive) .def("EditEdgeProperty", &GraphInterfaceWrap::EditEdgeProperty) .def("EditVertexProperty", &GraphInterfaceWrap::EditVertexProperty) .def("EditGraphProperty", &GraphInterfaceWrap::EditGraphProperty) .def("RemoveEdgeProperty", &GraphInterfaceWrap::RemoveEdgeProperty) .def("RemoveVertexProperty", &GraphInterfaceWrap::RemoveVertexProperty) .def("RemoveGraphProperty", &GraphInterfaceWrap::RemoveGraphProperty) .def("ListProperties", &GraphInterfaceWrap::ListProperties) .def("InsertEdgeIndexProperty", &GraphInterfaceWrap::InsertEdgeIndexProperty) .def("InsertVertexIndexProperty", &GraphInterfaceWrap::InsertVertexIndexProperty) .def("ComputeGraphLayoutGursoy", &GraphInterfaceWrap::ComputeGraphLayoutGursoy) .def("ComputeGraphLayoutSpringBlock", &GraphInterfaceWrap::ComputeGraphLayoutSpringBlock) .def("WriteToFile", WriteToFile1) .def("WriteToFile", WriteToFile2) .def("ReadFromFile", ReadFromFile1) .def("ReadFromFile", ReadFromFile2) .def("InitSignalHandling", &GraphInterfaceWrap::InitSignalHandling); enum_("Degree") .value("In", GraphInterfaceWrap::IN_DEGREE) .value("Out", GraphInterfaceWrap::OUT_DEGREE) .value("Total", GraphInterfaceWrap::TOTAL_DEGREE); enum_("CommCorr") .value("ErdosReyni", GraphInterfaceWrap::ERDOS_REYNI) .value("Uncorrelated", GraphInterfaceWrap::UNCORRELATED) .value("Correlated", GraphInterfaceWrap::CORRELATED); enum_("RewireStrat") .value("Uncorrelated", GraphInterfaceWrap::UNCORRELATED_STRAT) .value("Correlated", GraphInterfaceWrap::CORRELATED_STRAT); variant_from_python(); variant_from_python(); to_python_converter, pair_to_tuple >(); pair_from_tuple(); to_python_converter, tuple_to_tuple >(); tuple_from_tuple(); to_python_converter(); pos_t_from_tuple(); pair_from_tuple(); to_python_converter >(); to_python_converter >(); to_python_converter >(); to_python_converter >(); class_("mod_info") .add_property("name", &LibInfo::GetName) .add_property("author", &LibInfo::GetAuthor) .add_property("copyright", &LibInfo::GetCopyright) .add_property("version", &LibInfo::GetVersion) .add_property("license", &LibInfo::GetLicense); }