Skip to content

Commit

Permalink
protocols for Labels and SGVector
Browse files Browse the repository at this point in the history
  • Loading branch information
gsomix committed Aug 10, 2012
1 parent 8cebdf6 commit 6c5b634
Show file tree
Hide file tree
Showing 8 changed files with 539 additions and 267 deletions.

This file was deleted.

100 changes: 0 additions & 100 deletions examples/undocumented/python_modular/features_director_dot_modular.py

This file was deleted.

8 changes: 7 additions & 1 deletion src/interfaces/modular/Features.i
Expand Up @@ -50,8 +50,14 @@
%rename(CombinedFeatures) CCombinedFeatures;
%rename(CombinedDotFeatures) CCombinedDotFeatures;
%rename(Labels) CLabels;

PYPROTO_DENSELABELS(CDenseLabels, DenseLabels, float64_t, "d\0", NPY_FLOAT64)
%rename(DenseLabels) CDenseLabels;

PYPROTO_DENSELABELS(CBinaryLabels, BinaryLabels, float64_t, "d\0", NPY_FLOAT64)
%rename(BinaryLabels) CBinaryLabels;

PYPROTO_DENSELABELS(CMulticlassLabels, MulticlassLabels, float64_t, "d\0", NPY_FLOAT64)
%rename(MulticlassLabels) CMulticlassLabels;

PYPROTO_DENSELABELS(CRegressionLabels, RegressionLabels, float64_t, "d\0", NPY_FLOAT64)
Expand Down Expand Up @@ -350,7 +356,7 @@ namespace shogun
#endif

#ifdef USE_FLOAT32
PYPROTO_DENSEFEATURES(ShortRealFeatures, float64_t, "f\0", NPY_FLOAT32)
PYPROTO_DENSEFEATURES(ShortRealFeatures, float32_t, "f\0", NPY_FLOAT32)
%template(ShortRealFeatures) CDenseFeatures<float32_t>;
#endif

Expand Down
5 changes: 5 additions & 0 deletions src/interfaces/modular/Library.i
Expand Up @@ -241,6 +241,9 @@ namespace shogun
SERIALIZABLE_DUMMY(SGString<floatmax_t>);
#endif

%ignore SGVector<float64_t>::operator+=;
%ignore SGVector<float64_t>::operator+;

#ifdef USE_BOOL
%template(BoolVector) SGVector<bool>;
SERIALIZABLE_DUMMY(SGVector<bool>);
Expand Down Expand Up @@ -278,10 +281,12 @@ namespace shogun
SERIALIZABLE_DUMMY(SGVector<uint64_t>);
#endif
#ifdef USE_FLOAT32
PYPROTO_SGVECTOR(ShortRealVector, float32_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
62 changes: 29 additions & 33 deletions src/interfaces/python_modular/DenseFeatures_protocols.i
Expand Up @@ -16,9 +16,9 @@

PyObject* class_name ## _inplace ## operator_name ## (PyObject *self, PyObject *o2)
{
CDenseFeatures< type_name > * arg1=0; // self in c++ repr
CDenseFeatures< type_name >* arg1=0; // self in c++ repr

void *argp1=0; // pointer to self
void* argp1=0; // pointer to self
int res1=0; // result for self's casting
int res2=0; // result for checking buffer
int res3=0; // result for getting buffer
Expand All @@ -31,8 +31,8 @@ PyObject* class_name ## _inplace ## operator_name ## (PyObject *self, PyObject *
Py_ssize_t shape[2];
Py_ssize_t strides[2];

type_name *lhs;
char *rhs;
type_name* lhs;
char* rhs;

res1=SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures<type_name>"), 0 | 0 );
arg1=reinterpret_cast< CDenseFeatures< type_name > * >(argp1);
Expand Down Expand Up @@ -76,7 +76,7 @@ PyObject* class_name ## _inplace ## operator_name ## (PyObject *self, PyObject *
if (view.len!=(shape[0]*shape[1])*view.itemsize)
SWIG_exception_fail(SWIG_ArgError(view.len), "bad buffer");

// result calculation
// calculation
buf=arg1->get_feature_matrix();
num_feat=arg1->get_num_features();
num_vec=arg1->get_num_vectors();
Expand All @@ -88,7 +88,7 @@ PyObject* class_name ## _inplace ## operator_name ## (PyObject *self, PyObject *
{
for (int j=0; j<num_feat; j++)
{
lhs[num_feat*i + j] ## operator ## = (*(type_name*) (rhs + strides[0]*i + strides[1]*j));
lhs[num_feat*i + j] ## operator ## = (*(type_name*) (rhs + strides[0]*j + strides[1]*i));
}
}

Expand All @@ -113,8 +113,8 @@ fail:
/* used by PyObject_GetBuffer */
static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags)
{
CDenseFeatures< type_name > * arg1=(CDenseFeatures< type_name > *) 0; // self in c++ repr
void *argp1=0; // pointer to self
CDenseFeatures< type_name >* arg1=(CDenseFeatures< type_name > *) 0; // self in c++ repr
void* argp1=0; // pointer to self
int res1=0; // result for self's casting

int num_feat=0, num_vec=0;
Expand Down Expand Up @@ -169,7 +169,7 @@ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags)
view->ndim=2;

view->format=(char*) format_str;
view->itemsize=strides[0];
view->itemsize=sizeof( type_name );

view->len=(shape[0]*shape[1])*view->itemsize;
view->shape=shape;
Expand Down Expand Up @@ -234,14 +234,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);
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);
idx=get_idx_in_bounds(idx, num_feat);
if (idx < 0)
{
goto fail;
Expand Down Expand Up @@ -280,16 +279,16 @@ static int class_name ## _setitem(PyObject *self, Py_ssize_t idx, PyObject *v)

if (v==NULL)
{
// error
return -1;
// TODO error message
goto fail;
}

tmp = (PyArrayObject *) class_name ## _getitem(self, idx);
if(tmp == NULL)
tmp=(PyArrayObject *) class_name ## _getitem(self, idx);
if(tmp==NULL)
{
return -1;
goto fail;
}
ret = PyArray_CopyObject(tmp, v);
ret=PyArray_CopyObject(tmp, v);
Py_DECREF(tmp);
return ret;

Expand All @@ -306,7 +305,7 @@ static PyObject* class_name ## _getslice(PyObject *self, Py_ssize_t ilow, Py_ssi
int res1=0 ; // result for self's casting

int num_feat=0, num_vec=0;
char* data = 0; // internal data of self
char* data=0; // internal data of self

SGMatrix< type_name > temp;

Expand All @@ -324,8 +323,7 @@ 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);

temp=arg1->get_feature_matrix();
num_feat=arg1->get_num_features();
num_vec=arg1->get_num_vectors();
Expand Down Expand Up @@ -369,14 +367,14 @@ static int class_name ## _setslice(PyObject *self, Py_ssize_t ilow, Py_ssize_t i

if (v==NULL)
{
// error
return -1;
// TODO error message
goto fail;
}

tmp = (PyArrayObject *) class_name ## _getslice(self, ilow, ihigh);
if(tmp == NULL)
tmp=(PyArrayObject *) class_name ## _getslice(self, ilow, ihigh);
if(tmp==NULL)
{
return -1;
goto fail;
}
ret = PyArray_CopyObject(tmp, v);
Py_DECREF(tmp);
Expand Down Expand Up @@ -423,7 +421,7 @@ static PyObject* class_name ## _getsubscript(PyObject *self, PyObject *key, bool
Py_ssize_t feat_slicelength=0;
Py_ssize_t vec_slicelength=0;

PyObject *tmp; // temporary object for tuple's item
PyObject* tmp; // temporary object for tuple's item

res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures<type_name>"), 0 | 0 );
if (!SWIG_IsOK(res1))
Expand All @@ -433,8 +431,7 @@ 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);

temp=arg1->get_feature_matrix();
num_feat=arg1->get_num_features();
num_vec=arg1->get_num_vectors();
Expand Down Expand Up @@ -513,7 +510,7 @@ static PyObject* class_name ## _getsubscript(PyObject *self, PyObject *key, bool

if (ret==NULL)
{
// error here
// TODO error message
goto fail;
}

Expand Down Expand Up @@ -553,14 +550,14 @@ static int class_name ## _setsubscript(PyObject *self, PyObject *key, PyObject*

if (v==NULL)
{
// error
return -1;
// TODO error message
goto fail;
}

tmp = (PyArrayObject *) class_name ## _getsubscript(self, key, false);
if(tmp == NULL)
{
return -1;
goto fail;
}
ret = PyArray_CopyObject(tmp, v);
Py_DECREF(tmp);
Expand All @@ -579,7 +576,6 @@ static long class_name ## _flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFE

%init
%{
/* hack! */
SwigPyBuiltin__shogun__CDenseFeaturesT_ ## type_name ## _t_type.ht_type.tp_flags = class_name ## _flags;
%}

Expand Down

0 comments on commit 6c5b634

Please sign in to comment.