Skip to content

Commit

Permalink
minor fixes for buffer protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
gsomix committed Jul 14, 2012
1 parent b3303c6 commit 4ef2573
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
Expand Up @@ -14,15 +14,18 @@ def features_dense_real_modular(in_data=data):

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

mem_real=memoryview(f_real)
mem_long=memoryview(f_long)
try:
mem_real=memoryview(f_real)
mem_long=memoryview(f_long)
except NameError:
# print "Python2.7 is needed for memoryview class"
pass

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

print ret_real
print ret_long
# print ret_real
# print ret_long

if __name__=='__main__':
print('dense_real')
Expand Down
28 changes: 14 additions & 14 deletions src/interfaces/modular/Features.i
Expand Up @@ -289,75 +289,75 @@ namespace shogun
namespace shogun
{
#ifdef USE_BOOL
#ifdef HAVE_PYTHON
BUFFER_DENSEFEATURES(BoolFeatures, bool, bool, "?\0")
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(BoolFeatures, bool, "?\0")
#endif

%template(BoolFeatures) CDenseFeatures<bool>;
#endif

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

%template(ULongIntFeatures) CDenseFeatures<uint64_t>;
%template(ULongIntFeatures) CDenseFeatures<uint64_t>;
#endif

#ifdef USE_FLOATMAX
Expand All @@ -366,15 +366,15 @@ namespace shogun

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

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

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

%template(RealFeatures) CDenseFeatures<float64_t>;
Expand Down
14 changes: 7 additions & 7 deletions src/interfaces/python_modular/swig_typemaps.i
Expand Up @@ -771,14 +771,14 @@ static bool spvector_to_numpy(PyObject* &obj, SGSparseVector<type> sg_vector, in
%}

/* Buffer protocol stuff for DenseFeatures */
%define BUFFER_DENSEFEATURES(class_name, type_name, type, format_str)
%define BUFFER_DENSEFEATURES(class_name, type_name, format_str)

%wrapper
%{

static int class_name ## _getbuffer(PyObject *exporter, Py_buffer *view, int flags)
{
CDenseFeatures< type > * self = (CDenseFeatures< type > *) 0;
CDenseFeatures< type_name > * self = (CDenseFeatures< type_name > *) 0;
void *argp1 = 0 ;
int res1 = 0 ;

Expand All @@ -788,13 +788,13 @@ static int class_name ## _getbuffer(PyObject *exporter, Py_buffer *view, int fla

static char* format = (char *) format_str;

res1 = SWIG_ConvertPtr(exporter, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures<type>"), 0 | 0 );
res1 = SWIG_ConvertPtr(exporter, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures<type_name>"), 0 | 0 );
if (!SWIG_IsOK(res1))
{
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" " class_name _getbuffer" "', argument " "1"" of type '" "CDenseFeatures< type > *""'");
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" " class_name _getbuffer" "', argument " "1"" of type '" "CDenseFeatures< type_name > *""'");
}

self = reinterpret_cast< CDenseFeatures < type > * >(argp1);
self = reinterpret_cast< CDenseFeatures < type_name > * >(argp1);

view->buf = self->get_feature_matrix(num_feat, num_vec);

Expand All @@ -803,8 +803,8 @@ static int class_name ## _getbuffer(PyObject *exporter, Py_buffer *view, int fla
shape[1] = num_vec;

stride = new Py_ssize_t[2];
stride[0] = sizeof( type );
stride[1] = sizeof( type ) * num_feat;
stride[0] = sizeof( type_name );
stride[1] = sizeof( type_name ) * num_feat;

view->len = shape[0]*stride[0];
view->itemsize = stride[0];
Expand Down

0 comments on commit 4ef2573

Please sign in to comment.