[Patrick Pletscher] Added experimental SWIG Python wrapper code
authorJoris Mooij <joris.mooij@tuebingen.mpg.de>
Tue, 22 Sep 2009 13:27:08 +0000 (15:27 +0200)
committerJoris Mooij <joris.mooij@tuebingen.mpg.de>
Tue, 22 Sep 2009 13:28:24 +0000 (15:28 +0200)
Makefile.CYGWIN
Makefile.LINUX
Makefile.MACOSX
Makefile.WINDOWS
python/Makefile [new file with mode: 0644]
python/dai.i [new file with mode: 0644]
python/test.py [new file with mode: 0644]

index 562155e..91ed494 100644 (file)
@@ -77,3 +77,11 @@ MATLABDIR=/agbs/share/sw/matlab
 MEX=$(MATLABDIR)/bin/mex
 # Specify the same C++ compiler and flags to mex
 MEXFLAGS=CXX\#$(CC) CXXFLAGS\#'$(CCFLAGS)'
+
+# SWIG PYTHON INTERFACE
+# The following should resolve to the SWIG command
+SWIG=swig -classic
+# Location of Python header files
+INCLUDE_PYTHON=/usr/include/python2.5
+# Location of Boost C++ library header files
+INCLUDE_BOOST=/usr/local/include/boost-1_37
index 64dcaab..2255db5 100644 (file)
@@ -76,3 +76,11 @@ MATLABDIR=/agbs/share/sw/matlab
 MEX=$(MATLABDIR)/bin/mex
 # Specify the same C++ compiler and flags to mex
 MEXFLAGS=CXX\#$(CC) CXXFLAGS\#'$(CCFLAGS)'
+
+# SWIG PYTHON INTERFACE
+# The following should resolve to the SWIG command
+SWIG=swig -classic
+# Location of Python header files
+INCLUDE_PYTHON=/usr/include/python2.5
+# Location of Boost C++ library header files
+INCLUDE_BOOST=/usr/include/boost
index 4f9f4dc..6dfe05e 100644 (file)
@@ -76,3 +76,11 @@ MATLABDIR=/agbs/share/sw/matlab
 MEX=$(MATLABDIR)/bin/mex
 # Specify the same C++ compiler and flags to mex
 MEXFLAGS=CXX\#$(CC) CXXFLAGS\#'$(CCFLAGS)'
+
+# SWIG PYTHON INTERFACE
+# The following should resolve to the SWIG command
+SWIG=swig -classic
+# Location of Python header files
+INCLUDE_PYTHON=/usr/include/python2.5
+# Location of Boost C++ library header files
+INCLUDE_BOOST=/usr/include/boost
index 79c5419..0f09919 100644 (file)
@@ -78,3 +78,11 @@ MATLABDIR=c:\matlab
 MEX=$(MATLABDIR)\bin\mex
 # Specify the same C++ compiler and flags to mex
 MEXFLAGS=CXX\#$(CC) CXXFLAGS\#"$(CCFLAGS)"
+
+# SWIG PYTHON INTERFACE
+# The following should resolve to the SWIG command
+SWIG=swig -classic
+# Location of Python header files
+INCLUDE_PYTHON=C:\python2.5
+# Location of Boost C++ library header files
+INCLUDE_BOOST=C:\boost_1_36_0
diff --git a/python/Makefile b/python/Makefile
new file mode 100644 (file)
index 0000000..fef22fb
--- /dev/null
@@ -0,0 +1,15 @@
+include ../Makefile.conf
+
+.PHONY:        all
+
+all:                   _dai.so
+
+_dai.so:               ../lib/libdai.a dai.i
+                       $(SWIG) -python -c++ dai.i
+                       g++ -Wall -c -O3 -g -fPIC dai_wrap.cxx -I$(INCLUDE_PYTHON) -I$(INCLUDE_BOOST) -I../include
+                       g++ -shared dai_wrap.o -o _dai.so ../lib/libdai.a
+
+.PHONY: clean
+
+clean:         
+                       rm -f dai_wrap.cxx dai_wrap.o _dai.so dai.py dai.pyc
diff --git a/python/dai.i b/python/dai.i
new file mode 100644 (file)
index 0000000..3fe7db5
--- /dev/null
@@ -0,0 +1,71 @@
+%module dai
+
+        struct Neighbor {
+            size_t iter;
+            size_t node;
+            size_t dual;
+
+            Neighbor() {}
+            Neighbor( size_t iter, size_t node, size_t dual ) : iter(iter), node(node), dual(dual) {}
+
+            operator size_t () const { return node; }
+        };
+
+%{
+#include "../include/dai/var.h"
+#include "../include/dai/smallset.h"
+#include "../include/dai/varset.h"
+#include "../include/dai/prob.h"
+#include "../include/dai/factor.h"
+#include "../include/dai/bipgraph.h"
+#include "../include/dai/factorgraph.h"
+#include "../include/dai/util.h"
+%}
+
+%ignore dai::TProb::operator[];
+%ignore dai::TFactor::operator[];
+
+%include "../include/dai/util.h"
+%include "../include/dai/var.h"
+%include "../include/dai/smallset.h"
+%template(SmallSetVar) dai::SmallSet< dai::Var >;
+%include "../include/dai/varset.h"
+%include "../include/dai/prob.h"
+%template(Prob) dai::TProb<Real>;
+%extend dai::TProb<Real> {
+        inline Real __getitem__(int i) const {return (*self)[i];}
+        inline void __setitem__(int i,Real d) {(*self)[i] = d;}
+        %template(TProbRealConstructor) TProb<double *>;
+        %template(TProbIntConstructor)  TProb<size_t *>;
+};
+%include "../include/dai/factor.h"
+%extend dai::TFactor<Real> {
+        inline Real __getitem__(int i) const {return (*self)[i];}
+        inline void __setitem__(int i,Real d) {(*self)[i] = d;}
+};
+
+%template(Factor) dai::TFactor<Real>;
+%include "../include/dai/bipgraph.h"
+%include "../include/dai/factorgraph.h"
+%include "std_vector.i"
+// TODO: typemaps for the vectors (input/output python arrays)
+%inline{
+typedef std::vector<dai::Factor> VecFactor;
+typedef std::vector< VecFactor > VecVecFactor;
+}
+%template(VecFactor) std::vector< dai::Factor >;
+%template(VecVecFactor) std::vector< VecFactor >;
+
+%{
+typedef dai::BipartiteGraph::Neighbor Neighbor;
+%}
+
+%include "../include/dai/index.h"
+%extend dai::MultiFor {
+    inline size_t __getitem__(int i) const {
+        return (*self)[i];
+    }
+    inline void next() {
+        return (*self)++;
+    }
+};
diff --git a/python/test.py b/python/test.py
new file mode 100644 (file)
index 0000000..f8d9365
--- /dev/null
@@ -0,0 +1,10 @@
+import dai
+
+v1 = dai.Var(0,4)
+v2 = dai.Var(1,3)
+vs = dai.VarSet(v1,v2)
+f = dai.Factor(vs)
+fVec = dai.VecFactor()
+fVec.append(f)
+fg = dai.FactorGraph(fVec)
+print fg.nrVars()