setup.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import os.path
  2. import pickle
  3. import platform
  4. import sys
  5. from pkg_resources import (
  6. normalize_path,
  7. working_set,
  8. add_activation_listener,
  9. require,
  10. )
  11. from setuptools import setup
  12. from setuptools.command.build_py import build_py
  13. from setuptools.command.develop import develop
  14. from setuptools.command.test import test as test_command
  15. PLATFORM = 'unix'
  16. if platform.platform().startswith('Win'):
  17. PLATFORM = 'win'
  18. SETUP_DIR = os.path.dirname(os.path.abspath(__file__))
  19. MODELS_DIR = os.path.join(SETUP_DIR, 'stan', PLATFORM)
  20. MODELS_TARGET_DIR = os.path.join('fbprophet', 'stan_models')
  21. def build_stan_models(target_dir, models_dir=MODELS_DIR):
  22. from pystan import StanModel
  23. for model_type in ['linear', 'logistic']:
  24. model_name = 'prophet_{}_growth.stan'.format(model_type)
  25. target_name = '{}_growth.pkl'.format(model_type)
  26. with open(os.path.join(models_dir, model_name)) as f:
  27. model_code = f.read()
  28. sm = StanModel(model_code=model_code)
  29. with open(os.path.join(target_dir, target_name), 'wb') as f:
  30. pickle.dump(sm, f, protocol=pickle.HIGHEST_PROTOCOL)
  31. class BuildPyCommand(build_py):
  32. """Custom build command to pre-compile Stan models."""
  33. def run(self):
  34. if not self.dry_run:
  35. target_dir = os.path.join(self.build_lib, MODELS_TARGET_DIR)
  36. self.mkpath(target_dir)
  37. build_stan_models(target_dir)
  38. build_py.run(self)
  39. class DevelopCommand(develop):
  40. """Custom develop command to pre-compile Stan models in-place."""
  41. def run(self):
  42. if not self.dry_run:
  43. target_dir = os.path.join(self.setup_path, MODELS_TARGET_DIR)
  44. self.mkpath(target_dir)
  45. build_stan_models(target_dir)
  46. develop.run(self)
  47. class TestCommand(test_command):
  48. """We must run tests on the build directory, not source."""
  49. def with_project_on_sys_path(self, func):
  50. # Ensure metadata is up-to-date
  51. self.reinitialize_command('build_py', inplace=0)
  52. self.run_command('build_py')
  53. bpy_cmd = self.get_finalized_command("build_py")
  54. build_path = normalize_path(bpy_cmd.build_lib)
  55. # Build extensions
  56. self.reinitialize_command('egg_info', egg_base=build_path)
  57. self.run_command('egg_info')
  58. self.reinitialize_command('build_ext', inplace=0)
  59. self.run_command('build_ext')
  60. ei_cmd = self.get_finalized_command("egg_info")
  61. old_path = sys.path[:]
  62. old_modules = sys.modules.copy()
  63. try:
  64. sys.path.insert(0, normalize_path(ei_cmd.egg_base))
  65. working_set.__init__()
  66. add_activation_listener(lambda dist: dist.activate())
  67. require('%s==%s' % (ei_cmd.egg_name, ei_cmd.egg_version))
  68. func()
  69. finally:
  70. sys.path[:] = old_path
  71. sys.modules.clear()
  72. sys.modules.update(old_modules)
  73. working_set.__init__()
  74. setup(
  75. name='fbprophet',
  76. version='0.1.post1',
  77. description='Automatic Forecasting Procedure',
  78. url='https://facebookincubator.github.io/prophet/',
  79. author='Sean J. Taylor <sjt@fb.com>, Ben Letham <bletham@fb.com>',
  80. author_email='sjt@fb.com',
  81. license='BSD',
  82. packages=['fbprophet', 'fbprophet.tests'],
  83. setup_requires=[
  84. 'Cython>=0.22',
  85. 'pystan>=2.14',
  86. ],
  87. install_requires=[
  88. 'matplotlib',
  89. 'numpy',
  90. 'pandas>=0.18.1',
  91. 'pystan>=2.14',
  92. ],
  93. zip_safe=False,
  94. include_package_data=True,
  95. cmdclass={
  96. 'build_py': BuildPyCommand,
  97. 'develop': DevelopCommand,
  98. 'test': TestCommand,
  99. },
  100. test_suite='fbprophet.tests.test_prophet',
  101. long_description="""
  102. Implements a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly and weekly seasonality, plus holidays. It works best with daily periodicity data with at least one year of historical data. Prophet is robust to missing data, shifts in the trend, and large outliers.
  103. """
  104. )