Skip to content

Commit

Permalink
Merge pull request #646 from gsomix/buffer_protocol
Browse files Browse the repository at this point in the history
Fixes for buffer protocol
  • Loading branch information
Soeren Sonnenburg committed Jul 14, 2012
2 parents a6d49a5 + cc22b2c commit 5068d3e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 47 deletions.

This file was deleted.

@@ -0,0 +1,29 @@
from shogun.Features import RealFeatures
from shogun.Features import LongIntFeatures

from numpy import array, float64, int64

# create dense matrice
data=[[1,2,3],[4,0,0],[0,0,0],[0,5,0],[0,0,6],[9,9,9]]

parameter_list = [[data]]

def features_dense_real_modular(in_data=data):
m_real=array(in_data, dtype=float64)
m_long=array(in_data, dtype=int64)

f_real=RealFeatures(m_real)
f_long=LongIntFeatures(m_long)

mem_real=memoryview(f_real)
mem_long=memoryview(f_long)

ret_real=array(f_real)
ret_long=array(f_long)

print ret_real
print ret_long

if __name__=='__main__':
print('dense_real')
features_dense_real_modular(*parameter_list[0])
20 changes: 10 additions & 10 deletions src/interfaces/modular/Features.i
Expand Up @@ -297,63 +297,63 @@ namespace shogun
#endif

#ifdef USE_CHAR
#ifdef HAVE_PYTHON
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(CharFeatures, char, char, "c\0")
#endif

%template(CharFeatures) CDenseFeatures<char>;
#endif

#ifdef USE_UINT8
#ifdef HAVE_PYTHON
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(ByteFeatures, uint8_t, unsigned char, "B\0")
#endif

%template(ByteFeatures) CDenseFeatures<uint8_t>;
#endif

#ifdef USE_UINT16
#ifdef HAVE_PYTHON
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(WordFeatures, uint16_t, unsigned short, "H\0")
#endif

%template(WordFeatures) CDenseFeatures<uint16_t>;
#endif

#ifdef USE_INT16
#ifdef HAVE_PYTHON
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(ShortFeatures, int16_t, short, "h\0")
#endif

%template(ShortFeatures) CDenseFeatures<int16_t>;
#endif

#ifdef USE_INT32
#ifdef HAVE_PYTHON
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(IntFeatures, int32_t, int, "i\0")
#endif

%template(IntFeatures) CDenseFeatures<int32_t>;
#endif

#ifdef USE_UINT32
#ifdef HAVE_PYTHON
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(UIntFeatures, uint32_t, unsigned int, "I\0")
#endif

%template(UIntFeatures) CDenseFeatures<uint32_t>;
#endif

#ifdef USE_INT64
#ifdef HAVE_PYTHON
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(LongIntFeatures, int64_t, long, "l\0")
#endif

%template(LongIntFeatures) CDenseFeatures<int64_t>;
#endif

#ifdef USE_UINT64
#ifdef HAVE_PYTHON
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(ULongIntFeatures, uint64_t, unsigned long , "L\0")
#endif

Expand All @@ -365,15 +365,15 @@ namespace shogun
#endif

#ifdef USE_FLOAT32
#ifdef HAVE_PYTHON
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(ShortRealFeatures, float64_t, double, "f\0")
#endif

%template(ShortRealFeatures) CDenseFeatures<float32_t>;
#endif

#ifdef USE_FLOAT64
#ifdef HAVE_PYTHON
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(RealFeatures, float64_t, double, "d\0")
#endif

Expand Down
8 changes: 3 additions & 5 deletions src/interfaces/modular/modshogun.i
Expand Up @@ -28,6 +28,9 @@
static int print_sgobject(PyObject *pyobj, FILE *f, int flags);
%}

/* Include helper functions for python buffer protocol */
%include "python_buffer_protocol.i"

%feature("python:slot", "tp_str", functype="reprfunc") shogun::CSGObject::__str__;
%feature("python:slot", "tp_repr", functype="reprfunc") shogun::CSGObject::__repr__;
/*%feature("python:slot", "tp_hash", functype="hashfunc") shogun::CSGObject::myHashFunc;*/
Expand All @@ -44,11 +47,6 @@

%include "modshogun_ignores.i"

/* Include helper functions for python buffer protocol */
#ifdef HAVE_PYTHON
%include "python_buffer_protocol.i"
#endif

%include "Classifier_includes.i"
%include "Clustering_includes.i"
%include "Distance_includes.i"
Expand Down
4 changes: 1 addition & 3 deletions src/interfaces/python_modular/python_buffer_protocol.i
Expand Up @@ -15,7 +15,7 @@
Py_ssize_t* shape;
Py_ssize_t* stride;

static char* format = format_str;
static char* format = (char *) format_str;

res1 = SWIG_ConvertPtr(exporter, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures<type>"), 0 | 0 );
if (!SWIG_IsOK(res1))
Expand All @@ -38,8 +38,6 @@
view->len = shape[0]*stride[0];
view->itemsize = stride[0];
view->readonly = 0;

/* TODO fix warnings related to const char* -> char* */
view->format = format;
view->ndim = 2;
view->shape = shape;
Expand Down

0 comments on commit 5068d3e

Please sign in to comment.