Skip to content

Commit

Permalink
preliminary design of buffer protocol stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
gsomix committed Jul 13, 2012
1 parent fdd7763 commit a184880
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
@@ -0,0 +1,29 @@
from shogun.Features import RealFeatures
from numpy import array, float64, all

# create dense matrices A,B,C
matrix=array([[1,2,3],[4,0,0],[0,0,0],[0,5,0],[0,0,6],[9,9,9]], dtype=float64)

parameter_list = [[matrix]]

# ... of type LongInt
def features_dense_real_modular(A=matrix):

# ... of type Real, LongInt and Byte
a=RealFeatures(A)
a.set_feature_vector(array([1,4,0,0,0,9], dtype=float64), 0)

# get matrix
a_out = a.get_feature_matrix()

b = array(a) + a

print b
print a_out

assert(all(a_out==A))
return a_out

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

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

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

int num_feat = 0, num_vec = 0;

Py_ssize_t* shape;
Py_ssize_t* stride;

// TODO validate argp1
res1 = SWIG_ConvertPtr(exporter, &argp1, SWIGTYPE_p_shogun__CDenseFeaturesT_ ## type ## _t, 0 | 0 );
self = reinterpret_cast< CDenseFeatures < type > * >(argp1);

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

shape = new Py_ssize_t[2];
shape[0]=num_feat;
shape[1]=num_vec;

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

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

// TODO add formats for more types
view->format="d\0";

view->ndim = 2;
view->shape = shape;
view->strides = stride;
view->suboffsets = NULL;
view->internal = NULL;

view->obj=(PyObject*) exporter;
Py_INCREF(exporter);

return 0;
}

static void name ## _releasebuffer(PyObject *exporter, Py_buffer *view)
{
if(view->shape != NULL)
delete[] view->shape;

if(view->strides != NULL)
delete[] view->strides;
}

static long name ## _flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE;
%}

%init
%{
SwigPyBuiltin__shogun__CDenseFeaturesT_ ## type_name ## _t_type.ht_type.tp_flags = name ## _flags;
%}

%feature("python:bf_getbuffer") CDenseFeatures< type_name > #name "_getbuffer"
%feature("python:bf_releasebuffer") CDenseFeatures< type_name > #name "_releasebuffer"

%enddef

/* Templated Class DenseFeatures */
%include <shogun/features/DenseFeatures.h>
namespace shogun
Expand Down Expand Up @@ -322,6 +393,7 @@ namespace shogun
%template(ShortRealFeatures) CDenseFeatures<float32_t>;
#endif
#ifdef USE_FLOAT64
BUFFER_DENSEFEATURES(RealFeatures, float64_t, double)
%template(RealFeatures) CDenseFeatures<float64_t>;
#endif
}
Expand Down

0 comments on commit a184880

Please sign in to comment.