graph_bind.cc 12.3 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2007  Tiago de Paula Peixoto <tiago@forked.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4
5
6
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
Tiago Peixoto's avatar
Tiago Peixoto committed
7
// as published by the Free Software Foundation; either version 3
Tiago Peixoto's avatar
Tiago Peixoto committed
8
9
10
11
12
13
14
15
// 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
16
17
// along with this program. If not, see <http://www.gnu.org/licenses/>.

Tiago Peixoto's avatar
Tiago Peixoto committed
18

19
#include "graph_filtering.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
20
21
22
#include <boost/python.hpp>
#include <boost/tuple/tuple.hpp>
#include "graph.hh"
23
#include "graph_python_interface.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
24
25
26
27
28
29
30
31
32
33
34
35
36

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)
    {
37
38
        boost::python::tuple t = boost::python::make_tuple(p.first,p.second);
        return incref(t.ptr());
Tiago Peixoto's avatar
Tiago Peixoto committed
39
40
41
42
43
44
45
46
    }
};

template <class T1, class T2, class T3>
struct tuple_to_tuple
{
    static PyObject* convert(const boost::tuple<T1,T2,T3>& p)
    {
47
        boost::python::tuple t =
48
            boost::python::make_tuple(get<0>(p),get<1>(p),get<2>(p));
49
        return incref(t.ptr());
Tiago Peixoto's avatar
Tiago Peixoto committed
50
51
52
53
54
55
56
57
58
    }
};


template <class T1, class T2, class T3>
struct tuple_from_tuple
{
    tuple_from_tuple()
    {
59
        converter::registry::push_back
60
            (&convertible, &construct,
61
             boost::python::type_id<boost::tuple<T1,T2,T3> >());
Tiago Peixoto's avatar
Tiago Peixoto committed
62
63
64
65
    }

    static void* convertible(PyObject* obj_ptr)
    {
66
67
68
69
70
        handle<> x(borrowed(obj_ptr));
        object o(x);
        extract<T1> first(o[0]);
        extract<T2> second(o[1]);
        extract<T2> third(o[3]);
71
        if (!first.check() || !second.check() || !third.check())
72
73
            return 0;
        return obj_ptr;
Tiago Peixoto's avatar
Tiago Peixoto committed
74
75
    }

76
    static void construct(PyObject* obj_ptr,
77
                          converter::rvalue_from_python_stage1_data* data)
78
    {
79
80
81
82
83
84
        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]);
85
        void* storage =
86
87
            ((boost::python::converter::rvalue_from_python_storage
              <boost::tuple<T1,T2,T3> >*) data)->storage.bytes;
88
89
        new (storage) boost::tuple<T1,T2,T3>(value);
        data->convertible = storage;
Tiago Peixoto's avatar
Tiago Peixoto committed
90
91
92
93
94
95
96
97
    }
};

template <class T1, class T2>
struct pair_from_tuple
{
    pair_from_tuple()
    {
98
        converter::registry::push_back(&convertible, &construct,
99
                                       boost::python::type_id<pair<T1,T2> >());
Tiago Peixoto's avatar
Tiago Peixoto committed
100
101
102
103
    }

    static void* convertible(PyObject* obj_ptr)
    {
104
105
106
107
        handle<> x(borrowed(obj_ptr));
        object o(x);
        extract<T1> first(o[0]);
        extract<T2> second(o[1]);
108
        if (!first.check() || !second.check())
109
110
            return 0;
        return obj_ptr;
Tiago Peixoto's avatar
Tiago Peixoto committed
111
112
    }

113
    static void construct(PyObject* obj_ptr,
114
                          converter::rvalue_from_python_stage1_data* data)
115
    {
116
117
118
119
120
        handle<> x(borrowed(obj_ptr));
        object o(x);
        pair<T1,T2> value;
        value.first = extract<T1>(o[0]);
        value.second = extract<T2>(o[1]);
121
        void* storage =
122
123
            ( (boost::python::converter::rvalue_from_python_storage
               <pair<T1,T2> >*) data)->storage.bytes;
124
125
        new (storage) pair<T1,T2>(value);
        data->convertible = storage;
Tiago Peixoto's avatar
Tiago Peixoto committed
126
127
128
    }
};

129
template <class ValueType>
Tiago Peixoto's avatar
Tiago Peixoto committed
130
131
132
133
struct variant_from_python
{
    variant_from_python()
    {
134
        converter::registry::push_back
135
            (&convertible, &construct,
136
             boost::python::type_id<GraphInterface::deg_t>());
Tiago Peixoto's avatar
Tiago Peixoto committed
137
138
139
140
    }

    static void* convertible(PyObject* obj_ptr)
    {
141
142
143
144
145
146
        handle<> x(borrowed(obj_ptr));
        object o(x);
        extract<ValueType> str(o);
        if (!str.check())
            return 0;
        return obj_ptr;
Tiago Peixoto's avatar
Tiago Peixoto committed
147
148
    }

149
    static void construct(PyObject* obj_ptr,
150
                          converter::rvalue_from_python_stage1_data* data)
151
    {
152
153
154
155
        handle<> x(borrowed(obj_ptr));
        object o(x);
        ValueType value = extract<ValueType>(o);
        GraphInterface::deg_t deg = value;
156
        void* storage =
157
158
            ( (boost::python::converter::rvalue_from_python_storage
               <GraphInterface::deg_t>*) data)->storage.bytes;
159
160
        new (storage) GraphInterface::deg_t(deg);
        data->convertible = storage;
Tiago Peixoto's avatar
Tiago Peixoto committed
161
162
163
164
165
166
167
168
169
170
    }
};



template <class Hist>
struct hist_to_dict
{
    static PyObject* convert(const Hist& h)
    {
171
172
173
174
        dict hist;
        for(typeof(h.begin()) iter = h.begin(); iter != h.end(); ++iter)
            hist[iter->first] = iter->second;
        return incref(hist.ptr());
Tiago Peixoto's avatar
Tiago Peixoto committed
175
176
177
178
179
180
181
182
    }
};

struct LibInfo
{
    string GetName()      const {return PACKAGE_NAME;}
    string GetAuthor()    const {return AUTHOR;}
    string GetCopyright() const {return COPYRIGHT;}
183
184
    string GetVersion()   const {return VERSION " (commit " GIT_COMMIT 
                                        ", " GIT_COMMIT_DATE ")";}
185
    string GetLicense()   const {return "GPL version 3 or above";}
186
187
188
    string GetCXXFLAGS()  const {return CXXFLAGS;}
    string GetInstallPrefix() const {return INSTALL_PREFIX;}
    string GetPythonDir() const {return PYTHON_DIR;}
Tiago Peixoto's avatar
Tiago Peixoto committed
189
190
};

Tiago Peixoto's avatar
Tiago Peixoto committed
191
// overloads
192
193
194
195
196
197
198
199
void  (GraphInterface::*ReadFromFile1) (string) =
    &GraphInterface::ReadFromFile;
void  (GraphInterface::*ReadFromFile2) (string, string) =
    &GraphInterface::ReadFromFile;
void  (GraphInterface::*WriteToFile1)  (string) =
    &GraphInterface::WriteToFile;
void  (GraphInterface::*WriteToFile2)  (string, string)  =
    &GraphInterface::WriteToFile;
Tiago Peixoto's avatar
Tiago Peixoto committed
200

Tiago Peixoto's avatar
Tiago Peixoto committed
201
202
BOOST_PYTHON_MODULE(libgraph_tool)
{
203
204
205
206
207
208
209
210
211
212
213
    GraphInterface().ExportPythonInterface();

    class_<GraphInterface>("GraphInterface")
        .def("GenerateCorrelatedConfigurationalModel",
             &GraphInterface::GenerateCorrelatedConfigurationalModel)
        .def("GetNumberOfVertices", &GraphInterface::GetNumberOfVertices)
        .def("GetNumberOfEdges", &GraphInterface::GetNumberOfEdges)
        .def("GetVertexHistogram", &GraphInterface::GetVertexHistogram)
        .def("GetEdgeHistogram", &GraphInterface::GetEdgeHistogram)
        .def("LabelComponents", &GraphInterface::LabelComponents)
        .def("LabelParallelEdges", &GraphInterface::LabelParallelEdges)
214
        .def("GetCombinedVertexHistogram",
215
216
217
218
219
220
221
222
223
224
225
226
227
228
             &GraphInterface::GetCombinedVertexHistogram)
        .def("GetAverageCombinedVertexCorrelation",
             &GraphInterface::GetAverageCombinedVertexCorrelation)
        .def("GetVertexCorrelationHistogram",
             &GraphInterface::GetVertexCorrelationHistogram)
        .def("GetEdgeVertexCorrelationHistogram",
             &GraphInterface::GetEdgeVertexCorrelationHistogram)
        .def("GetAverageNearestNeighboursCorrelation",
             &GraphInterface::GetAverageNearestNeighboursCorrelation)
        .def("GetAssortativityCoefficient",
             &GraphInterface::GetAssortativityCoefficient)
        .def("GetScalarAssortativityCoefficient",
             &GraphInterface::GetScalarAssortativityCoefficient)
        .def("GetGlobalClustering", &GraphInterface::GetGlobalClustering)
229
        .def("SetLocalClusteringToProperty",
230
             &GraphInterface::SetLocalClusteringToProperty)
231
        .def("SetExtendedClusteringToProperty",
232
233
234
235
236
237
238
239
240
241
242
             &GraphInterface::SetExtendedClusteringToProperty)
        .def("GetDistanceHistogram", &GraphInterface::GetDistanceHistogram)
        .def("GetSampledDistanceHistogram",
             &GraphInterface::GetSampledDistanceHistogram)
        .def("GetReciprocity", &GraphInterface::GetReciprocity)
        .def("GetMinimumSpanningTree",
             &GraphInterface::GetMinimumSpanningTree)
        .def("GetLineGraph", &GraphInterface::GetLineGraph)
        .def("GetBetweenness", &GraphInterface::GetBetweenness)
        .def("GetCentralPointDominance",
             &GraphInterface::GetCentralPointDominance)
243
        .def("GetCommunityStructure",
244
245
246
             &GraphInterface::GetCommunityStructure)
        .def("GetCommunityNetwork", &GraphInterface::GetCommunityNetwork)
        .def("GetModularity", &GraphInterface::GetModularity)
Tiago Peixoto's avatar
Tiago Peixoto committed
247
        .def("RandomRewire", &GraphInterface::RandomRewire)
248
249
250
251
252
253
254
        .def("SetDirected", &GraphInterface::SetDirected)
        .def("GetDirected", &GraphInterface::GetDirected)
        .def("SetReversed", &GraphInterface::SetReversed)
        .def("GetReversed", &GraphInterface::GetReversed)
        .def("SetVertexFilterProperty",
             &GraphInterface::SetVertexFilterProperty)
        .def("IsVertexFilterActive", &GraphInterface::IsVertexFilterActive)
255
        .def("SetEdgeFilterProperty",
256
257
             &GraphInterface::SetEdgeFilterProperty)
        .def("IsEdgeFilterActive", &GraphInterface::IsEdgeFilterActive)
258
259
260
        .def("AddEdgeProperty",  &GraphInterface::AddEdgeProperty)
        .def("AddVertexProperty",  &GraphInterface::AddVertexProperty)
        .def("AddGraphProperty",  &GraphInterface::AddGraphProperty)
261
262
263
264
265
266
267
268
269
270
271
272
273
        .def("RemoveEdgeProperty",  &GraphInterface::RemoveEdgeProperty)
        .def("RemoveVertexProperty",  &GraphInterface::RemoveVertexProperty)
        .def("RemoveGraphProperty",  &GraphInterface::RemoveGraphProperty)
        .def("PurgeVertices",  &GraphInterface::PurgeVertices)
        .def("PurgeEdges",  &GraphInterface::PurgeEdges)
        .def("InsertEdgeIndexProperty",
             &GraphInterface::InsertEdgeIndexProperty)
        .def("InsertVertexIndexProperty",
             &GraphInterface::InsertVertexIndexProperty)
        .def("ComputeGraphLayoutGursoy",
             &GraphInterface::ComputeGraphLayoutGursoy)
        .def("ComputeGraphLayoutSpringBlock",
             &GraphInterface::ComputeGraphLayoutSpringBlock)
274
275
276
277
        .def("WriteToFile", WriteToFile1)
        .def("WriteToFile", WriteToFile2)
        .def("ReadFromFile", ReadFromFile1)
        .def("ReadFromFile", ReadFromFile2)
278
        .def("Vertices", &GraphInterface::Vertices)
279
        .def("Vertex", &GraphInterface::Vertex)
280
281
282
283
284
        .def("Edges", &GraphInterface::Edges)
        .def("AddVertex", &GraphInterface::AddVertex)
        .def("AddEdge", &GraphInterface::AddEdge)
        .def("RemoveVertex", &GraphInterface::RemoveVertex)
        .def("RemoveEdge", &GraphInterface::RemoveEdge)
285
        .def("Clear", &GraphInterface::Clear)
286
287
288
        .def("GetVertexProperties", &GraphInterface::GetVertexProperties)
        .def("GetEdgeProperties", &GraphInterface::GetEdgeProperties)
        .def("GetGraphProperties", &GraphInterface::GetGraphProperties)
289
290
291
292
293
294
        .def("InitSignalHandling", &GraphInterface::InitSignalHandling);

    enum_<GraphInterface::degree_t>("Degree")
        .value("In", GraphInterface::IN_DEGREE)
        .value("Out", GraphInterface::OUT_DEGREE)
        .value("Total", GraphInterface::TOTAL_DEGREE);
Tiago Peixoto's avatar
Tiago Peixoto committed
295

296
297
298
299
    enum_<GraphInterface::comm_corr_t>("CommCorr")
        .value("ErdosReyni", GraphInterface::ERDOS_REYNI)
        .value("Uncorrelated", GraphInterface::UNCORRELATED)
        .value("Correlated", GraphInterface::CORRELATED);
Tiago Peixoto's avatar
Tiago Peixoto committed
300

Tiago Peixoto's avatar
Tiago Peixoto committed
301
    variant_from_python<string>();
302
    variant_from_python<GraphInterface::degree_t>();
Tiago Peixoto's avatar
Tiago Peixoto committed
303
304
    to_python_converter<pair<double,double>, pair_to_tuple<double,double> >();
    pair_from_tuple<double,double>();
305
    to_python_converter<boost::tuple<double,double,double>,
306
                        tuple_to_tuple<double,double,double> >();
Tiago Peixoto's avatar
Tiago Peixoto committed
307
    tuple_from_tuple<double,double,double>();
308
    pair_from_tuple<bool,bool>();
309
310
311
312
    to_python_converter<hist_t,hist_to_dict<hist_t> >();
    to_python_converter<hist2d_t,hist_to_dict<hist2d_t> >();
    to_python_converter<hist3d_t,hist_to_dict<hist3d_t> >();
    to_python_converter<avg_corr_t,hist_to_dict<avg_corr_t> >();
Tiago Peixoto's avatar
Tiago Peixoto committed
313
314

    class_<LibInfo>("mod_info")
315
316
317
        .add_property("name", &LibInfo::GetName)
        .add_property("author", &LibInfo::GetAuthor)
        .add_property("copyright", &LibInfo::GetCopyright)
318
        .add_property("version", &LibInfo::GetVersion)
319
320
321
322
        .add_property("license", &LibInfo::GetLicense)
        .add_property("cxxflags", &LibInfo::GetCXXFLAGS)
        .add_property("install_prefix", &LibInfo::GetInstallPrefix)
        .add_property("python_dir", &LibInfo::GetPythonDir);
Tiago Peixoto's avatar
Tiago Peixoto committed
323
}