graph_bind.cc 10.4 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006  Tiago de Paula Peixoto <tiago@forked.de>
//
// 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 2
// 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 <boost/python.hpp>
#include <boost/tuple/tuple.hpp>
#include "graph.hh"

using namespace std;
using namespace graph_tool;
using namespace boost;
using namespace boost::python;

// some conversions...

template <class T1, class T2>
struct pair_to_tuple
{
    static PyObject* convert(const pair<T1,T2>& p)
    {
	boost::python::tuple t = boost::python::make_tuple(p.first,p.second);
	return incref(t.ptr());
    }
};

template <class T1, class T2, class T3>
struct tuple_to_tuple
{
    static PyObject* convert(const boost::tuple<T1,T2,T3>& p)
    {
	boost::python::tuple t = boost::python::make_tuple(get<0>(p),get<1>(p),get<2>(p));
	return incref(t.ptr());
    }
};


template <class T1, class T2, class T3>
struct tuple_from_tuple
{
    tuple_from_tuple()
    {
	converter::registry::push_back(&convertible, &construct,  boost::python::type_id<boost::tuple<T1,T2,T3> >());
    }

    static void* convertible(PyObject* obj_ptr)
    {
	handle<> x(borrowed(obj_ptr));
	object o(x);
	extract<T1> first(o[0]);
	extract<T2> second(o[1]);
	extract<T2> 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<T1,T2,T3> value;
	get<0>(value) = extract<T1>(o[0]);
	get<1>(value) = extract<T2>(o[1]);
	get<2>(value) = extract<T2>(o[2]);
	void* storage = ( (boost::python::converter::rvalue_from_python_storage<boost::tuple<T1,T2,T3> >*) data)->storage.bytes;
	new (storage) boost::tuple<T1,T2,T3>(value);
	data->convertible = storage;
    }
};

template <class T1, class T2>
struct pair_from_tuple
{
    pair_from_tuple()
    {
	converter::registry::push_back(&convertible, &construct,  boost::python::type_id<pair<T1,T2> >());
    }

    static void* convertible(PyObject* obj_ptr)
    {
	handle<> x(borrowed(obj_ptr));
	object o(x);
	extract<T1> first(o[0]);
	extract<T2> 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<T1,T2> value;
	value.first = extract<T1>(o[0]);
	value.second = extract<T2>(o[1]);
	void* storage = ( (boost::python::converter::rvalue_from_python_storage<pair<T1,T2> >*) data)->storage.bytes;
	new (storage) pair<T1,T2>(value);
	data->convertible = storage;
    }
};

template <class ValueType> 
struct variant_from_python
{
    variant_from_python()
    {
	converter::registry::push_back(&convertible, &construct, boost::python::type_id<GraphInterface::deg_t>());
    }

    static void* convertible(PyObject* obj_ptr)
    {
	handle<> x(borrowed(obj_ptr));
	object o(x);
	extract<ValueType> 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<ValueType>(o);
	GraphInterface::deg_t deg = value;
	void* storage = ( (boost::python::converter::rvalue_from_python_storage<GraphInterface::deg_t>*) data)->storage.bytes;
	new (storage) GraphInterface::deg_t(deg);
	data->convertible = storage;
    }
};



template <class Hist>
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());
    }
};


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<double>(_o(j,k));
	}
	double operator()(size_t jl, size_t kl, size_t j, size_t k)
	{
	    return extract<double>(_o(jl,kl,j,k));
	}
	pair<size_t,size_t> operator()(double r1, double r2)
	{
	    object retval = _o(r1,r2);
190
	    return make_pair(size_t(max(int(extract<int>(retval[0])),0)), size_t(max(int(extract<int>(retval[1])),0)));
Tiago Peixoto's avatar
Tiago Peixoto committed
191
192
193
194
	}
	pair<size_t,size_t> operator()(double r1, double r2, size_t j, size_t k)
	{
	    object retval = _o(r1,r2,j,k);
195
	    return make_pair(size_t(max(int(extract<int>(retval[0])),0)), size_t(max(int(extract<int>(retval[1])),0)));
Tiago Peixoto's avatar
Tiago Peixoto committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
	}
	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;}
};

BOOST_PYTHON_MODULE(libgraph_tool)
{
    class_<GraphInterfaceWrap>("GraphInterface")
	.def("GenerateCorrelatedConfigurationalModel", &GraphInterfaceWrap::GenerateCorrelatedConfigurationalModel)
	.def("GetNumberOfVertices", &GraphInterfaceWrap::GetNumberOfVertices)
	.def("GetNumberOfEdges", &GraphInterfaceWrap::GetNumberOfEdges)
216
217
	.def("GetVertexHistogram", &GraphInterfaceWrap::GetVertexHistogram)
	.def("GetEdgeHistogram", &GraphInterfaceWrap::GetEdgeHistogram)
Tiago Peixoto's avatar
Tiago Peixoto committed
218
219
	.def("GetComponentSizeHistogram", &GraphInterfaceWrap::GetComponentSizeHistogram)
	.def("GetCombinedDegreeHistogram", &GraphInterfaceWrap::GetCombinedDegreeHistogram)	
220
221
222
223
	.def("GetVertexCorrelationHistogram", &GraphInterfaceWrap::GetVertexCorrelationHistogram)
	.def("GetEdgeVertexCorrelationHistogram", &GraphInterfaceWrap::GetEdgeVertexCorrelationHistogram)
	.def("GetVertexScalarCorrelationHistogram", &GraphInterfaceWrap::GetVertexScalarCorrelationHistogram)
	.def("GetAverageNearestNeighboursCorrelation", &GraphInterfaceWrap::GetAverageNearestNeighboursCorrelation)
Tiago Peixoto's avatar
Tiago Peixoto committed
224
	.def("GetAssortativityCoefficient", &GraphInterfaceWrap::GetAssortativityCoefficient)
225
	.def("GetScalarAssortativityCoefficient", &GraphInterfaceWrap::GetScalarAssortativityCoefficient)
Tiago Peixoto's avatar
Tiago Peixoto committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
	.def("GetGlobalClustering", &GraphInterfaceWrap::GetGlobalClustering)
	.def("SetLocalClusteringToProperty", &GraphInterfaceWrap::SetLocalClusteringToProperty)
	.def("GetAverageDistance", &GraphInterfaceWrap::GetAverageDistance)
	.def("GetAverageHarmonicDistance", &GraphInterfaceWrap::GetAverageHarmonicDistance)
	.def("SetDirected", &GraphInterfaceWrap::SetDirected)
	.def("GetDirected", &GraphInterfaceWrap::GetDirected)
	.def("SetReversed", &GraphInterfaceWrap::SetReversed)
	.def("GetReversed", &GraphInterfaceWrap::GetReversed)
	.def("SetVertexFilterProperty", &GraphInterfaceWrap::SetVertexFilterProperty)
	.def("GetVertexFilterProperty", &GraphInterfaceWrap::GetVertexFilterProperty)
	.def("SetVertexFilterRange", &GraphInterfaceWrap::SetVertexFilterRange)
	.def("GetVertexFilterRange", &GraphInterfaceWrap::GetVertexFilterRange)
	.def("IsVertexFilterActive", &GraphInterfaceWrap::IsVertexFilterActive)
	.def("SetGenericVertexFilter",  &GraphInterfaceWrap::SetGenericVertexFilter)
	.def("SetEdgeFilterProperty", &GraphInterfaceWrap::SetEdgeFilterProperty)
	.def("GetEdgeFilterProperty", &GraphInterfaceWrap::GetEdgeFilterProperty)
	.def("SetEdgeFilterRange", &GraphInterfaceWrap::SetEdgeFilterRange)
	.def("GetEdgeFilterRange", &GraphInterfaceWrap::GetEdgeFilterRange)
	.def("IsEdgeFilterActive", &GraphInterfaceWrap::IsEdgeFilterActive)
	.def("SetGenericEdgeFilter",  &GraphInterfaceWrap::SetGenericEdgeFilter)
	.def("RemoveEdgeProperty",  &GraphInterfaceWrap::RemoveEdgeProperty)
	.def("RemoveVertexProperty",  &GraphInterfaceWrap::RemoveVertexProperty)
	.def("InsertEdgeIndexProperty",  &GraphInterfaceWrap::InsertEdgeIndexProperty)
	.def("InsertVertexIndexProperty",  &GraphInterfaceWrap::InsertVertexIndexProperty)
	.def("RemoveParallelEdges",  &GraphInterfaceWrap::RemoveParallelEdges)
	.def("ComputeGraphLayoutGursoy", &GraphInterfaceWrap::ComputeGraphLayoutGursoy)
	.def("ComputeGraphLayoutSpringBlock", &GraphInterfaceWrap::ComputeGraphLayoutSpringBlock)
	.def("WriteToFile", &GraphInterfaceWrap::WriteToFile)
	.def("ReadFromFile", &GraphInterfaceWrap::ReadFromFile)
	.def("InitSignalHandling", &GraphInterfaceWrap::InitSignalHandling);
	
    enum_<GraphInterfaceWrap::degree_t>("Degree")
	.value("In", GraphInterfaceWrap::IN_DEGREE)
	.value("Out", GraphInterfaceWrap::OUT_DEGREE)
	.value("Total", GraphInterfaceWrap::TOTAL_DEGREE);

    variant_from_python<string>();
    variant_from_python<GraphInterfaceWrap::degree_t>();
    to_python_converter<pair<double,double>, pair_to_tuple<double,double> >();
    pair_from_tuple<double,double>();
    to_python_converter<boost::tuple<double,double,double>, tuple_to_tuple<double,double,double> >();
    tuple_from_tuple<double,double,double>();
    to_python_converter<GraphInterfaceWrap::hist_t, hist_to_dict<GraphInterfaceWrap::hist_t> >();
    to_python_converter<GraphInterfaceWrap::hist2d_t, hist_to_dict<GraphInterfaceWrap::hist2d_t> >();
    to_python_converter<GraphInterfaceWrap::hist3d_t, hist_to_dict<GraphInterfaceWrap::hist3d_t> >();
    to_python_converter<GraphInterfaceWrap::avg_corr_t, hist_to_dict<GraphInterfaceWrap::avg_corr_t> >();

    class_<LibInfo>("mod_info")
	.add_property("name", &LibInfo::GetName)
	.add_property("author", &LibInfo::GetAuthor)
	.add_property("copyright", &LibInfo::GetCopyright)
	.add_property("version", &LibInfo::GetVersion);
}