Skip to content

Commit

Permalink
preliminary design of protocols for Labels
Browse files Browse the repository at this point in the history
  • Loading branch information
gsomix committed Aug 10, 2012
1 parent 796f3f0 commit 1604259
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 42 deletions.
4 changes: 4 additions & 0 deletions src/interfaces/modular/Features.i
Expand Up @@ -33,6 +33,7 @@

#ifndef SWIGPYTHON
#define PYPROTO_DENSEFEATURES(class_name, type_name, format_str, typecode)
#define PYPROTO_DENSELABELS(class_type, class_name, type_name, format_str, typecode)
#endif

/* Remove C Prefix */
Expand All @@ -52,7 +53,10 @@
%rename(DenseLabels) CDenseLabels;
%rename(BinaryLabels) CBinaryLabels;
%rename(MulticlassLabels) CMulticlassLabels;

PYPROTO_DENSELABELS(CRegressionLabels, RegressionLabels, float64_t, "d\0", NPY_FLOAT64)
%rename(RegressionLabels) CRegressionLabels;

%rename(StructuredLabels) CStructuredLabels;
%rename(MulticlassMultipleOutputLabels) CMulticlassMultipleOutputLabels;
%rename(RealFileFeatures) CRealFileFeatures;
Expand Down
11 changes: 0 additions & 11 deletions src/interfaces/modular/Library.i
Expand Up @@ -242,57 +242,46 @@ namespace shogun
#endif

#ifdef USE_BOOL
PYPROTO_SGVECTOR(BoolVector, bool, "?\0", NPY_BOOL)
%template(BoolVector) SGVector<bool>;
SERIALIZABLE_DUMMY(SGVector<bool>);
#endif
#ifdef USE_CHAR
PYPROTO_SGVECTOR(CharVector, char, "c\0", NPY_STRING)
%template(CharVector) SGVector<char>;
SERIALIZABLE_DUMMY(SGVector<char>);
#endif
#ifdef USE_UINT8
PYPROTO_SGVECTOR(ByteVector, uint8_t, "B\0", NPY_UINT8)
%template(ByteVector) SGVector<uint8_t>;
SERIALIZABLE_DUMMY(SGVector<uint8_t>);
#endif
#ifdef USE_UINT16
PYPROTO_SGVECTOR(WordVector, uint16_t, "H\0", NPY_UINT16)
%template(WordVector) SGVector<uint16_t>;
SERIALIZABLE_DUMMY(SGVector<uint16_t>);
#endif
#ifdef USE_INT16
PYPROTO_SGVECTOR(ShortVector, int16_t, "h\0", NPY_INT16)
%template(ShortVector) SGVector<int16_t>;
SERIALIZABLE_DUMMY(SGVector<int16_t>);
#endif
#ifdef USE_INT32
PYPROTO_SGVECTOR(IntVector, int32_t, "i\0", NPY_INT32)
%template(IntVector) SGVector<int32_t>;
SERIALIZABLE_DUMMY(SGVector<int32_t>);
#endif
#ifdef USE_UINT32
PYPROTO_SGVECTOR(UIntVector, uint32_t, "I\0", NPY_UINT32)
%template(UIntVector) SGVector<uint32_t>;
SERIALIZABLE_DUMMY(SGVector<uint32_t>);
#endif
#ifdef USE_INT64
PYPROTO_SGVECTOR(LongIntVector, int64_t, "l\0", NPY_INT64)
%template(LongIntVector) SGVector<int64_t>;
SERIALIZABLE_DUMMY(SGVector<int64_t>);
#endif
#ifdef USE_UINT64
PYPROTO_SGVECTOR(ULongIntVector, uint64_t, "L\0", NPY_UINT64)
%template(ULongIntVector) SGVector<uint64_t>;
SERIALIZABLE_DUMMY(SGVector<uint64_t>);
#endif
#ifdef USE_FLOAT32
PYPROTO_SGVECTOR(ShortRealVector, float64_t, "f\0", NPY_FLOAT32)
%template(ShortRealVector) SGVector<float32_t>;
SERIALIZABLE_DUMMY(SGVector<float32_t>);
#endif
#ifdef USE_FLOAT64
PYPROTO_SGVECTOR(RealVector, float64_t, "d\0", NPY_FLOAT64)
%template(RealVector) SGVector<float64_t>;
SERIALIZABLE_DUMMY(SGVector<float64_t>);
#endif
Expand Down
75 changes: 58 additions & 17 deletions src/interfaces/python_modular/ProtoDenseFeatures.i
Expand Up @@ -29,6 +29,8 @@ PyObject* class_name ## _inplace ## operator_name ## (PyObject *self, PyObject *
int num_feat, num_vec;
int shape[2];

SGMatrix< type_name > temp;

type_name *lhs;
type_name *buf;

Expand All @@ -50,29 +52,34 @@ PyObject* class_name ## _inplace ## operator_name ## (PyObject *self, PyObject *
// checking that buffer is right
if (view.ndim != 2)
{
SWIG_exception_fail(SWIG_ArgError(res1), "same dimension is needed");
SWIG_exception_fail(SWIG_ArgError(res1), "wrong dimension");
}

if (view.itemsize != sizeof(type_name))
{
SWIG_exception_fail(SWIG_ArgError(res1), "same type is needed");
SWIG_exception_fail(SWIG_ArgError(res1), "wrong type");
}

if (view.shape == NULL)
{
SWIG_exception_fail(SWIG_ArgError(res1), "same shape is needed");
SWIG_exception_fail(SWIG_ArgError(res1), "wrong shape");
}

shape[0] = view.shape[0];
shape[1] = view.shape[1];
if (shape[0] != arg1->get_num_features() || shape[1] != arg1->get_num_vectors())
SWIG_exception_fail(SWIG_ArgError(res1), "same size is needed");
SWIG_exception_fail(SWIG_ArgError(res1), "wrong size");

if (view.len != (shape[0]*shape[1])*view.itemsize)
SWIG_exception_fail(SWIG_ArgError(res1), "bad buffer length");
SWIG_exception_fail(SWIG_ArgError(res1), "bad buffer");

// result calculation
lhs = arg1->get_feature_matrix(num_feat, num_vec);
//lhs = arg1->get_feature_matrix(num_feat, num_vec);
temp=arg1->get_feature_matrix();
num_feat=arg1->get_num_features();
num_vec=arg1->get_num_vectors();

lhs=temp.matrix;

// TODO strides support!
buf = (type_name*) view.buf;
Expand Down Expand Up @@ -110,8 +117,10 @@ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags)
int res1=0; // result for self's casting

int num_feat=0, num_vec=0;
Py_ssize_t* shape;
Py_ssize_t* strides;
Py_ssize_t* shape=NULL;
Py_ssize_t* strides=NULL;

SGMatrix< type_name > temp;

static char* format=(char *) format_str; // http://docs.python.org/dev/library/struct.html#module-struct

Expand All @@ -137,7 +146,12 @@ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags)

arg1=reinterpret_cast< CDenseFeatures < type_name >* >(argp1);

view->buf=arg1->get_feature_matrix(num_feat, num_vec);
//view->buf=arg1->get_feature_matrix(num_feat, num_vec);
temp=arg1->get_feature_matrix();
num_feat=arg1->get_num_features();
num_vec=arg1->get_num_vectors();

view->buf=temp.matrix;

shape=new Py_ssize_t[2];
shape[0]=num_feat;
Expand Down Expand Up @@ -166,17 +180,21 @@ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags)
return 0;

fail:
view->obj=NULL;
return -1;
}

/* used by PyBuffer_Release */
static void class_name ## _releasebuffer(PyObject *exporter, Py_buffer *view)
static void class_name ## _releasebuffer(PyObject *self, Py_buffer *view)
{
if (view->shape!=NULL)
delete[] view->shape;
if (view->obj!=NULL)
{
if (view->shape!=NULL)
delete[] view->shape;

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

/* used by PySequence_GetItem */
Expand All @@ -189,6 +207,8 @@ static PyObject* class_name ## _getitem(PyObject *self, Py_ssize_t idx)
char* data=0; // internal data of self
int num_feat=0, num_vec=0;

SGMatrix< type_name > temp;

Py_ssize_t* shape;
Py_ssize_t* strides;

Expand All @@ -203,7 +223,13 @@ static PyObject* class_name ## _getitem(PyObject *self, Py_ssize_t idx)
}

arg1=reinterpret_cast< CDenseFeatures< type_name >* >(argp1);
data=(char*) arg1->get_feature_matrix(num_feat, num_vec);

//data=(char*) arg1->get_feature_matrix(num_feat, num_vec);
temp=arg1->get_feature_matrix();
num_feat=arg1->get_num_features();
num_vec=arg1->get_num_vectors();

data=(char*) temp.matrix;

idx = get_idx_in_bounds(idx, num_feat);
if (idx < 0)
Expand Down Expand Up @@ -272,6 +298,8 @@ static PyObject* class_name ## _getslice(PyObject *self, Py_ssize_t ilow, Py_ssi
int num_feat=0, num_vec=0;
char* data = 0; // internal data of self

SGMatrix< type_name > temp;

Py_ssize_t* shape;
Py_ssize_t* strides;

Expand All @@ -286,7 +314,13 @@ static PyObject* class_name ## _getslice(PyObject *self, Py_ssize_t ilow, Py_ssi
}

arg1=reinterpret_cast< CDenseFeatures< type_name >* >(argp1);
data=(char*) arg1->get_feature_matrix(num_feat, num_vec);

//data=(char*) arg1->get_feature_matrix(num_feat, num_vec);
temp=arg1->get_feature_matrix();
num_feat=arg1->get_num_features();
num_vec=arg1->get_num_vectors();

data=(char*) temp.matrix;

get_slice_in_bounds(&ilow, &ihigh, num_feat);
if (ilow < ihigh)
Expand Down Expand Up @@ -360,6 +394,7 @@ static PyObject* class_name ## _getsubscript(PyObject *self, PyObject *key, bool
Py_ssize_t* shape;
Py_ssize_t* strides;

SGMatrix< type_name > temp;

PyObject* ret;
PyArray_Descr* descr=PyArray_DescrFromType(typecode);
Expand Down Expand Up @@ -388,7 +423,13 @@ static PyObject* class_name ## _getsubscript(PyObject *self, PyObject *key, bool
}

arg1=reinterpret_cast< CDenseFeatures< type_name >* >(argp1);
data=(char*) arg1->get_feature_matrix(num_feat, num_vec);

//data=(char*) arg1->get_feature_matrix(num_feat, num_vec);
temp=arg1->get_feature_matrix();
num_feat=arg1->get_num_features();
num_vec=arg1->get_num_vectors();

data=(char*) temp.matrix;

feat_high=num_feat;
vec_high=num_vec;
Expand Down
7 changes: 7 additions & 0 deletions src/interfaces/python_modular/ProtoHelper.i
@@ -1,6 +1,13 @@
/* Helper functions */
%wrapper
%{

struct buffer_info
{
Py_ssize_t* shape;
Py_ssize_t* strides;
};

void get_slice_in_bounds(Py_ssize_t* ilow, Py_ssize_t* ihigh, Py_ssize_t max_idx)
{
if (*ilow<0)
Expand Down
36 changes: 23 additions & 13 deletions src/interfaces/python_modular/ProtoSGVector.i
Expand Up @@ -42,6 +42,9 @@ PyObject* class_name ## _inplace ## operator_name ## (PyObject *self, PyObject *
}

res3 = PyObject_GetBuffer(o2, &view, PyBUF_F_CONTIGUOUS | PyBUF_ND | PyBUF_STRIDES | 0);

printf("inplace %p %d\n", &view, view.ndim);

if (res3 != 0 || view.buf==NULL)
{
SWIG_exception_fail(SWIG_ArgError(res1), "bad buffer");
Expand All @@ -50,25 +53,25 @@ PyObject* class_name ## _inplace ## operator_name ## (PyObject *self, PyObject *
// checking that buffer is right
if (view.ndim != 1)
{
SWIG_exception_fail(SWIG_ArgError(res1), "same dimension is needed");
SWIG_exception_fail(SWIG_ArgError(res1), "wrong dimension");
}

if (view.itemsize != sizeof(type_name))
{
SWIG_exception_fail(SWIG_ArgError(res1), "same type is needed");
SWIG_exception_fail(SWIG_ArgError(res1), "wrong type");
}

if (view.shape == NULL)
{
SWIG_exception_fail(SWIG_ArgError(res1), "same shape is needed");
SWIG_exception_fail(SWIG_ArgError(res1), "wrong shape");
}

shape[0] = view.shape[0];
if (shape[0] != arg1->vlen)
SWIG_exception_fail(SWIG_ArgError(res1), "same size is needed");
SWIG_exception_fail(SWIG_ArgError(res1), "wrong size");

if (view.len != shape[0]*view.itemsize)
SWIG_exception_fail(SWIG_ArgError(res1), "bad buffer length");
SWIG_exception_fail(SWIG_ArgError(res1), "bad buffer");

// result calculation
lhs=arg1->vector;
Expand Down Expand Up @@ -107,8 +110,8 @@ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags)
int res1=0; // result for self's casting

int vlen=0;
Py_ssize_t* shape;
Py_ssize_t* strides;
Py_ssize_t* shape=NULL;
Py_ssize_t* strides=NULL;

static char* format=(char *) format_str; // http://docs.python.org/dev/library/struct.html#module-struct

Expand Down Expand Up @@ -158,20 +161,27 @@ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags)
view->obj=(PyObject*) self;
Py_INCREF(self);

printf("getbuffer %p %p %p %p %p\n", self, view, view->obj, view->shape, view->strides, view->ndim);

return 0;

fail:
view->obj=NULL;
return -1;
}

/* used by PyBuffer_Release */
static void class_name ## _releasebuffer(PyObject *exporter, Py_buffer *view)
static void class_name ## _releasebuffer(PyObject *self, Py_buffer *view)
{
if (view->shape!=NULL)
delete[] view->shape;
printf("releasebuffer %p %p %p %p %p\n", self, view, view->obj, view->shape, view->strides);
if (view->obj!=NULL)
{
if (view->shape!=NULL)
delete[] view->shape;

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

/* used by PySequence_GetItem */
Expand Down Expand Up @@ -202,7 +212,7 @@ static PyObject* class_name ## _getitem(PyObject *self, Py_ssize_t idx, bool get
vlen=arg1->vlen;

shape=new Py_ssize_t[1];
shape[0]=0;
shape[0]=1;

strides=new Py_ssize_t[1];
strides[0]=sizeof( type_name );
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/python_modular/swig_typemaps.i
Expand Up @@ -18,7 +18,7 @@
*/

%include "ProtoDenseFeatures.i"
%include "ProtoSGVector.i"
%include "ProtoDenseLabels.i"

#ifdef HAVE_PYTHON
%{
Expand Down

0 comments on commit 1604259

Please sign in to comment.