Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Comments

Python wrappers for libsdca#2

Merged
mlapin merged 2 commits intomlapin:masterfrom
121onto:python-wrappers
Dec 23, 2015
Merged

Python wrappers for libsdca#2
mlapin merged 2 commits intomlapin:masterfrom
121onto:python-wrappers

Conversation

@121onto
Copy link
Contributor

@121onto 121onto commented Dec 21, 2015

I wrote some wrappers that make it easy to use the libsdca library with Python 3.5. I tried to stick with your coding conventions. One exception is that I use distutils and Cython to build things (not too familiar with cmake).

The wrappers could use a user-friendly interface, to help with trouble-shooting potential bugs. Wouldn't be too difficult to write a nice python class for this purpose.

Would love to hear any feedback or thoughts on this.

mlapin added a commit that referenced this pull request Dec 23, 2015
@mlapin mlapin merged commit b2848bf into mlapin:master Dec 23, 2015
@mlapin
Copy link
Owner

mlapin commented Dec 23, 2015

Hey @121onto, thanks a lot for the contribution!

I cannot really test it with python 3.5 right now, but I decided to go ahead with the merge anyway - we can fix things after the holidays.

With Python 2.7.3, there is a compilation issue:

Error compiling Cython file:
------------------------------------------------------------
...

    cdef DATASET* dat = new DATASET()
    dat.num_dimensions = features.shape[0]
    dat.num_examples = features.shape[1]
    dat.num_classes = num_classes
    dat.labels.assign(&labels[0], &labels[0] + features.shape[1])
                     ^
------------------------------------------------------------

libsdca.pyx:84:22: Cannot assign type 'int *' to 'size_t'

@121onto 121onto deleted the python-wrappers branch December 23, 2015 17:23
@121onto 121onto restored the python-wrappers branch December 23, 2015 17:23
@121onto
Copy link
Contributor Author

121onto commented Dec 23, 2015

Interesting. I downloaded the latest anaconda release for python 2.7 and was able to compile things. Here is my version info:

$ python -c 'import sys, cython; print sys.version, "\n", cython.__version__'
2.7.11...
0.23.4

Are you willing to install the latest Anaconda build and try again? Its super easy to do. Everything gets placed in ~/anaconda and so uninstalling it is as easy as deleting that directory. Find install instructions here.

If it is a version issue, we should make that explicit in setup.py. Here is an updated setup.py that accomplishes this:

Edited on Wed Dec 23 10:36:42 PST 2015
Edited on Wed Dec 23 22:30:43 PST 2015

import sys, os                                                                                                          
from Cython.Build import cythonize                                                                                      
from distutils.version import LooseVersion                                                                              

import numpy as np                                                                                                      

try:                                                                                                                    
    from setuptools import setup, Extension                                                                             
    _have_setuptools = True                                                                                             
except ImportError:                                                                                                     
    # no setuptools installed                                                                                           
    from distutils.core import setup, Extension                                                                         
    _have_setuptools = False                                                                                            

setuptools_kwargs = {}                                                                                                  
min_cython_ver = '0.23.4'                                                                                               
try:                                                                                                                    
    import Cython                                                                                                       
except ImportError:                                                                                                     
    sys.exit('You are missing Cython.  Install with:'                                                                   
             '\n$ pip install cython')                                                                                  


try:                                                                                                                    
    cython_ver = Cython.__version__                                                                                     
    assert(cython_ver >= LooseVersion(min_cython_ver))                                                                  
except AssertionError:                                                                                                  
    sys.exit('Your version of Cython is not supported.  '                                                               
             'Upgrade  with: '                                                                                          
             '\n$ pip install cython --upgrade')                                                                                

# create build dir if it doesn't already exist                                                                          
try:                                                                                                                    
    os.makedirs('libsdca')                                                                                              
except OSError:                                                                                                         
    if not os.path.isdir('libsdca'):                                                                                    
        raise                                                                                                           


# Modifiy this if BLAS and LAPACK libraries are not in /usr/lib.                                                        
BLAS_LIB_DIR = '/usr/lib'                                                                                               
BLAS_LIB = ['blas']                                                                                                     
LAPACK_LIB = ['lapack']                                                                                                 

BLAS_COMPILE_ARGS = ['-DBLAS_DEFAULT_LOCAL_HEADER']                                                                     
BASE_COMPILE_ARGS = ['-std=c++11', '-lstdc++']                                                                          
BASE_LINK_ARGS = ['-std=c++11', '-lstdc++']                                                                             

INCLUDE_DIRS = [np.get_include(), '../']                                                                                

extension = Extension(name = 'libsdca',                                                                                 
                 language = 'c++',                                                                                      
                 libraries = LAPACK_LIB + BLAS_LIB,                                                                     
                 include_dirs = INCLUDE_DIRS,                                                                           
                 library_dirs = [ BLAS_LIB_DIR ],                                                                       
                 extra_compile_args = BASE_COMPILE_ARGS + BLAS_COMPILE_ARGS,                                            
                 extra_link_args = BASE_LINK_ARGS,                                                                      
                 sources = ['libsdca.pyx'])                                                                             

setup(name = 'libsdca',                                                                                                 
      description = 'Python wrappers for libsdca.',                                                                     
      version = '0.0.1',                                                                                                
      long_description = '''                                                                                            
      Python wrappers for libsdca.                                                                                      
      libsdca is a library for multiclass classification                                                                
      based on stochastic dual coordinate ascent (SDCA).                                                                
      ''',                                                                                                              
      author = '121onto',                                                                                               
      author_email = '121onto@gmail.com',                                                                               
      ext_package = 'libsdca',                                                                                          
      ext_modules = cythonize(extension),                                                                               
      packages = 'libsdca',                                                                                             
      classifiers = [                                                                                                   
          'Development Status :: 2 - Pre-Alpha',                                                                        
          'Intended Audience :: Science/Research',                                                                      
          'Operating System :: OS Independent',                                                                         
          'Programming Language :: C++',                                                                                
          'Programming Language :: Python',                                                                             
          'Programming Language :: Python :: 3',                                                                        
          'Topic :: Scientific/Engineering',                                                                            
      ],                                                                                                                
      **setuptools_kwargs                                                                                               
)  

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants