Skip to content

Commit

Permalink
Merge pull request #666 from gsomix/buffer_protocol
Browse files Browse the repository at this point in the history
Python Buffer Protocol support for DenseFeatures
  • Loading branch information
Soeren Sonnenburg committed Jul 23, 2012
2 parents f606639 + 05d66a2 commit b2eabfc
Show file tree
Hide file tree
Showing 4 changed files with 690 additions and 240 deletions.
Expand Up @@ -5,53 +5,78 @@
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]]
data=[[1,2,3],[4,5,6],[7,8,9],[-1,-2,-3]]

parameter_list = [[data]]

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

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

print m_real
print f_real

print f_real[-1]
print f_real[1, 2]
print f_real[-1:3]
print f_real[2, 0:2]
print f_real[0:3, 1]
print f_real[0:3, 1:2]
print f_real[:,1]
print f_real[1,:]

print m_real[-2]
f_real[-1]=m_real[-2]
print f_real[-1]

print m_real[0, 1]
f_real[1,2]=m_real[0,1]
print f_real[1, 2]

print m_real[0:2]
f_real[1:3]=m_real[0:2]
print f_real[1:3]

print m_real[0, 0:2]
f_real[2, 0:2]=m_real[0,0:2]
print f_real[2, 0:2]

print m_real[0:3, 2]
f_real[0:3,1]=m_real[0:3, 2]
print f_real[0:3, 1]

print m_real[0:3, 0:1]
f_real[0:3,1:2]=m_real[0:3,0:1]
print f_real[0:3, 1:2]

f_real[:,0]=0
print f_real.get_feature_matrix()

f_real[:,0]=(f_real+m_real)[:,0]
print f_real.get_feature_matrix()

if numpy.__version__ >= '1.5':
f_real+=m_real
f_long+=m_long

f_real*=m_real
f_long*=m_long

f_real-=m_real
f_long-=m_long
else:
print "numpy version >= 1.5 is needed"

f_real+=f_real
f_long+=f_long

f_real*=f_real
f_long*=f_long

f_real-=f_real
f_long-=f_long

# print f_real
# print f_long
print f_real
print f_real.get_feature_matrix()

try:
mem_real=memoryview(f_real)
mem_long=memoryview(f_long)
except NameError:
# print "Python2.7 is needed for memoryview class"
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

if __name__=='__main__':
print('dense_real')
Expand Down
55 changes: 11 additions & 44 deletions src/interfaces/modular/Features.i
Expand Up @@ -293,74 +293,47 @@ namespace shogun
namespace shogun
{
#ifdef USE_BOOL
#ifdef SWIGPYTHON
BUFFER_DENSEFEATURES(BoolFeatures, bool, "?\0")
#endif

PYPROTO_DENSEFEATURES(BoolFeatures, bool, "?\0", NPY_BOOL)
%template(BoolFeatures) CDenseFeatures<bool>;
#endif

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

PYPROTO_DENSEFEATURES(CharFeatures, char, "c\0", NPY_STRING)
%template(CharFeatures) CDenseFeatures<char>;
#endif

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

PYPROTO_DENSEFEATURES(ByteFeatures, uint8_t, "B\0", NPY_UINT8)
%template(ByteFeatures) CDenseFeatures<uint8_t>;
#endif

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

PYPROTO_DENSEFEATURES(WordFeatures, uint16_t, "H\0", NPY_UINT16)
%template(WordFeatures) CDenseFeatures<uint16_t>;
#endif

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

PYPROTO_DENSEFEATURES(ShortFeatures, int16_t, "h\0", NPY_INT16)
%template(ShortFeatures) CDenseFeatures<int16_t>;
#endif

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

PYPROTO_DENSEFEATURES(IntFeatures, int32_t, "i\0", NPY_INT32)
%template(IntFeatures) CDenseFeatures<int32_t>;
#endif

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

PYPROTO_DENSEFEATURES(UIntFeatures, uint32_t, "I\0", NPY_UINT32)
%template(UIntFeatures) CDenseFeatures<uint32_t>;
#endif

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

PYPROTO_DENSEFEATURES(LongIntFeatures, int64_t, "l\0", NPY_INT64)
%template(LongIntFeatures) CDenseFeatures<int64_t>;
#endif

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

PYPROTO_DENSEFEATURES(ULongIntFeatures, uint64_t, "L\0", NPY_UINT64)
%template(ULongIntFeatures) CDenseFeatures<uint64_t>;
#endif

Expand All @@ -369,18 +342,12 @@ namespace shogun
#endif

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

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

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

PYPROTO_DENSEFEATURES(RealFeatures, float64_t, "d\0", NPY_FLOAT64)
%template(RealFeatures) CDenseFeatures<float64_t>;
#endif
}
Expand Down

0 comments on commit b2eabfc

Please sign in to comment.