Skip to content

Commit

Permalink
ENH: Improve setup to try to build cython
Browse files Browse the repository at this point in the history
Try to build with extension module and then bail out and go without
if missing
  • Loading branch information
bashtage committed Mar 22, 2019
1 parent 154b7a4 commit 93136ec
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 61 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -137,6 +137,7 @@ is possible that older versions work.
* pandas (0.20+)
* statsmodels (0.8+)
* xarray (0.9+, optional)
* cython (optional)

### Testing

Expand Down
8 changes: 7 additions & 1 deletion README.rst
@@ -1,7 +1,8 @@
Linear Models
=============

|Build Status| |codecov| |Codacy Badge| |codebeat badge|
|Build Status| |codecov| |Codacy Badge| |codebeat badge| |Code Quality:
Python| |Total Alerts|

Linear (regression) models for Python. Extends
`statsmodels <http://www.statsmodels.org>`__ with Panel regression,
Expand Down Expand Up @@ -142,6 +143,7 @@ is possible that older versions work.
- pandas (0.20+)
- statsmodels (0.8+)
- xarray (0.9+, optional)
- cython (optional)

Testing
~~~~~~~
Expand Down Expand Up @@ -169,3 +171,7 @@ Documentation
:target: https://www.codacy.com/app/bashtage/linearmodels?utm_source=github.com&utm_medium=referral&utm_content=bashtage/linearmodels&utm_campaign=Badge_Grade
.. |codebeat badge| image:: https://codebeat.co/badges/aaae2fb4-72b5-4a66-97cd-77b93488f243
:target: https://codebeat.co/projects/github-com-bashtage-linearmodels-master
.. |Code Quality: Python| image:: https://img.shields.io/lgtm/grade/python/g/bashtage/linearmodels.svg?logo=lgtm&logoWidth=18
:target: https://lgtm.com/projects/g/bashtage/linearmodels/context:python
.. |Total Alerts| image:: https://img.shields.io/lgtm/alerts/g/bashtage/linearmodels.svg?logo=lgtm&logoWidth=18
:target: https://lgtm.com/projects/g/bashtage/linearmodels/alerts
4 changes: 2 additions & 2 deletions requirements.txt
@@ -1,5 +1,5 @@
numpy>=1.11
pandas>= 0.19
numpy>=1.12
pandas>=0.20
scipy>=0.18
patsy
statsmodels>=0.8
Expand Down
135 changes: 77 additions & 58 deletions setup.py
Expand Up @@ -2,17 +2,24 @@
import os
import sys

from distutils.errors import CCompilerError, DistutilsExecError, DistutilsPlatformError
from setuptools import Extension, find_packages, setup

try:
from Cython.Build import cythonize
import numpy
import versioneer

HAS_CYTHON = True
except ImportError:
HAS_CYTHON = False
FAILED_COMPILER_ERROR = """
******************************************************************************
* WARNING *
******************************************************************************
import versioneer
Unable to build binary modules for linearmodels. These are not required
to run any code in the package, and only provide speed-ups for a small subset
of models.
******************************************************************************
* WARNING *
******************************************************************************
"""

if sys.version_info < (3, 5):
sys.exit('Requires Python 3.5 or later due to use of @ operator.')
Expand All @@ -34,7 +41,7 @@
windows_line_ending = '\r\n'
linux_line_ending = '\n'

description = pypandoc.convert('README.md', 'rst')
description = pypandoc.convert_file('README.md', 'rst')
description = description.replace(windows_line_ending, linux_line_ending)
description = description.replace(osx_line_ending, linux_line_ending)
with open('README.rst', 'w') as rst:
Expand Down Expand Up @@ -77,61 +84,73 @@
additional_files.append(filename.replace('./linearmodels/', ''))

for filename in glob.iglob('./linearmodels/tests/**', recursive=True):
if '.txt' in filename:
additional_files.append(filename.replace('./linearmodels/', ''))
if '.csv' in filename:
if '.txt' in filename or '.csv' in filename or '.dta' in filename:
additional_files.append(filename.replace('./linearmodels/', ''))

for filename in glob.iglob('./examples/**', recursive=True):
if '.png' in filename:
additional_files.append(filename)

if HAS_CYTHON:
macros = [('NPY_NO_DEPRECATED_API', '1'),
('NPY_1_7_API_VERSION', '1')]
# macros.append(('CYTHON_TRACE', '1'))
directives = {} # {'linetrace': True, 'binding':True}
extensions = [Extension('linearmodels.panel._utility',
['linearmodels/panel/_utility.pyx'],
define_macros=macros,
include_dirs=[numpy.get_include()])]
extensions = cythonize(extensions, compiler_directives=directives, force=True)
else:

def run_setup(binary=True):
extensions = []
if binary:
from Cython.Build import cythonize
import numpy
macros = [('NPY_NO_DEPRECATED_API', '1'),
('NPY_1_7_API_VERSION', '1')]
# macros.append(('CYTHON_TRACE', '1'))
directives = {} # {'linetrace': True, 'binding':True}
extension = Extension('linearmodels.panel._utility',
['linearmodels/panel/_utility.pyx'],
define_macros=macros,
include_dirs=[numpy.get_include()])
extensions.append(extension)
extensions = cythonize(extensions, compiler_directives=directives, force=True)

setup(cmdclass=versioneer.get_cmdclass(),
name='linearmodels',
license='NCSA',
description='Instrumental Variable and Linear Panel models for Python',
version=versioneer.get_version(),
packages=find_packages(),
package_dir={'linearmodels': './linearmodels'},
author='Kevin Sheppard',
author_email='kevin.k.sheppard@gmail.com',
url='http://github.com/bashtage/linearmodels',
long_description=description,
install_requires=open('requirements.txt').read().split('\n'),
include_package_data=True,
package_data={'linearmodels': additional_files},
keywords=['linear models', 'regression', 'instrumental variables', 'IV',
'panel', 'fixed effects', 'clustered', 'heteroskedasticity',
'endogeneity', 'instruments', 'statistics',
'statistical inference', 'econometrics'],
zip_safe=False,
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: End Users/Desktop',
'Intended Audience :: Financial and Insurance Industry',
'Intended Audience :: Science/Research',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'License :: OSI Approved',
'Operating System :: MacOS :: MacOS X',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Programming Language :: Python',
'Topic :: Scientific/Engineering',
],
ext_modules=extensions
)


try:
run_setup(binary=True)
except (CCompilerError, DistutilsExecError, DistutilsPlatformError, IOError, ValueError,
ImportError):
run_setup(binary=False)
import warnings

setup(
cmdclass=versioneer.get_cmdclass(),
name='linearmodels',
license='NCSA',
description='Instrumental Variable and Linear Panel models for Python',
version=versioneer.get_version(),
packages=find_packages(),
package_dir={'linearmodels': './linearmodels'},
author='Kevin Sheppard',
author_email='kevin.k.sheppard@gmail.com',
url='http://github.com/bashtage/linearmodels',
long_description=description,
install_requires=open('requirements.txt').read().split('\n'),
include_package_data=True,
package_data={'linearmodels': additional_files},
keywords=['linear models', 'regression', 'instrumental variables', 'IV',
'panel', 'fixed effects', 'clustered', 'heteroskedasticity',
'endogeneity', 'instruments', 'statistics',
'statistical inference', 'econometrics'],
zip_safe=False,
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: End Users/Desktop',
'Intended Audience :: Financial and Insurance Industry',
'Intended Audience :: Science/Research',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'License :: OSI Approved',
'Operating System :: MacOS :: MacOS X',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Programming Language :: Python',
'Topic :: Scientific/Engineering',
],
ext_modules=extensions
)
warnings.warn(FAILED_COMPILER_ERROR, UserWarning)

0 comments on commit 93136ec

Please sign in to comment.