diff --git a/python/fbprophet/forecaster.py b/python/fbprophet/forecaster.py index ed4ecf7..2086b18 100644 --- a/python/fbprophet/forecaster.py +++ b/python/fbprophet/forecaster.py @@ -97,8 +97,9 @@ class Prophet(object): ): self.growth = growth - self.changepoints = pd.to_datetime(changepoints) + self.changepoints = changepoints if self.changepoints is not None: + self.changepoints = pd.Series(pd.to_datetime(self.changepoints), name='ds') self.n_changepoints = len(self.changepoints) self.specified_changepoints = True else: @@ -405,7 +406,7 @@ class Prophet(object): ) else: # set empty changepoints - self.changepoints = [] + self.changepoints = pd.Series(pd.to_datetime([]), name='ds') if len(self.changepoints) > 0: self.changepoints_t = np.sort(np.array( (self.changepoints - self.start) / self.t_scale)) diff --git a/python/fbprophet/serialize.py b/python/fbprophet/serialize.py new file mode 100644 index 0000000..64f6c79 --- /dev/null +++ b/python/fbprophet/serialize.py @@ -0,0 +1,149 @@ +# -*- coding: utf-8 -*- +# Copyright (c) Facebook, Inc. and its affiliates. + +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from __future__ import absolute_import, division, print_function + +from collections import OrderedDict +import json + +import numpy as np +import pandas as pd + +from fbprophet.forecaster import Prophet +from fbprophet import __version__ + + +SIMPLE_ATTRIBUTES = [ + 'growth', 'n_changepoints', 'specified_changepoints', 'changepoint_range', + 'yearly_seasonality', 'weekly_seasonality', 'daily_seasonality', + 'seasonality_mode', 'seasonality_prior_scale', 'changepoint_prior_scale', + 'holidays_prior_scale', 'mcmc_samples', 'interval_width', 'uncertainty_samples', + 'y_scale', 'logistic_floor', 'country_holidays', 'component_modes', 'fit_kwargs' +] + +PD_SERIES = ['changepoints', 'history_dates', 'train_holiday_names'] + +PD_TIMESTAMP = ['start'] + +PD_TIMEDELTA = ['t_scale'] + +PD_DATAFRAME = ['holidays', 'history', 'train_component_cols'] + +NP_ARRAY = ['changepoints_t'] + +ORDEREDDICT = ['seasonalities', 'extra_regressors'] + + +def model_to_json(model): + """Serialize a Prophet model to json string. + + Model must be fitted. Skips Stan objects that are not needed for predict. + + Can be deserialized with model_from_json. + + Parameters + ---------- + model: Prophet model object. + + Returns + ------- + json string that can be deserialized into a Prophet model. + """ + if model.history is None: + raise ValueError( + "This can only be used to serialize models that have already been fit." + ) + + model_json = { + attribute: getattr(model, attribute) for attribute in SIMPLE_ATTRIBUTES + } + # Handle attributes of non-core types + for attribute in PD_SERIES: + if getattr(model, attribute) is None: + model_json[attribute] = None + else: + model_json[attribute] = getattr(model, attribute).to_json( + orient='split', date_format='iso' + ) + for attribute in PD_TIMESTAMP: + model_json[attribute] = getattr(model, attribute).timestamp() + for attribute in PD_TIMEDELTA: + model_json[attribute] = getattr(model, attribute).total_seconds() + for attribute in PD_DATAFRAME: + if getattr(model, attribute) is None: + model_json[attribute] = None + else: + model_json[attribute] = getattr(model, attribute).to_json(orient='table', index=False) + for attribute in NP_ARRAY: + model_json[attribute] = getattr(model, attribute).tolist() + for attribute in ORDEREDDICT: + model_json[attribute] = [ + list(getattr(model, attribute).keys()), + getattr(model, attribute), + ] + # Params (Dict[str, np.ndarray]) + model_json['params'] = {k: v.tolist() for k, v in model.params.items()} + # Attributes that are skipped: stan_fit, stan_backend + model_json['__fbprophet_version'] = __version__ + return json.dumps(model_json) + + +def model_from_json(model_json): + """Deserialize a Prophet model from json string. + + Deserializes models that were serialized with model_to_json. + + Parameters + ---------- + model_json: Serialized model string + + Returns + ------- + Prophet model. + """ + attr_dict = json.loads(model_json) + model = Prophet() # We will overwrite all attributes set in init anyway + # Simple types + for attribute in SIMPLE_ATTRIBUTES: + setattr(model, attribute, attr_dict[attribute]) + for attribute in PD_SERIES: + if attr_dict[attribute] is None: + setattr(model, attribute, None) + else: + s = pd.read_json(attr_dict[attribute], typ='series', orient='split') + if s.name == 'ds': + if len(s) == 0: + s = pd.to_datetime(s) + s = s.dt.tz_localize(None) + setattr(model, attribute, s) + for attribute in PD_TIMESTAMP: + setattr(model, attribute, pd.Timestamp.utcfromtimestamp(attr_dict[attribute])) + for attribute in PD_TIMEDELTA: + setattr(model, attribute, pd.Timedelta(seconds=attr_dict[attribute])) + for attribute in PD_DATAFRAME: + if attr_dict[attribute] is None: + setattr(model, attribute, None) + else: + df = pd.read_json(attr_dict[attribute], typ='frame', orient='table', convert_dates=['ds']) + if attribute == 'train_component_cols': + # Special handling because of named index column + df.columns.name = 'component' + df.index.name = 'col' + setattr(model, attribute, df) + for attribute in NP_ARRAY: + setattr(model, attribute, np.array(attr_dict[attribute])) + for attribute in ORDEREDDICT: + key_list, unordered_dict = attr_dict[attribute] + od = OrderedDict() + for key in key_list: + od[key] = unordered_dict[key] + setattr(model, attribute, od) + # Params (Dict[str, np.ndarray]) + model.params = {k: np.array(v) for k, v in attr_dict['params'].items()} + # Skipped attributes + model.stan_backend = None + model.stan_fit = None + return model diff --git a/python/fbprophet/tests/test_diagnostics.py b/python/fbprophet/tests/test_diagnostics.py index a635218..7d143ff 100644 --- a/python/fbprophet/tests/test_diagnostics.py +++ b/python/fbprophet/tests/test_diagnostics.py @@ -288,7 +288,10 @@ class TestDiagnostics(TestCase): self.assertEqual(m1.growth, m2.growth) self.assertEqual(m1.n_changepoints, m2.n_changepoints) self.assertEqual(m1.changepoint_range, m2.changepoint_range) - self.assertEqual(m1.changepoints, m2.changepoints) + if m1.changepoints is None: + self.assertEqual(m1.changepoints, m2.changepoints) + else: + self.assertTrue(m1.changepoints.equals(m2.changepoints)) self.assertEqual(False, m2.yearly_seasonality) self.assertEqual(False, m2.weekly_seasonality) self.assertEqual(False, m2.daily_seasonality) diff --git a/python/fbprophet/tests/test_serialize.py b/python/fbprophet/tests/test_serialize.py new file mode 100644 index 0000000..22ac341 --- /dev/null +++ b/python/fbprophet/tests/test_serialize.py @@ -0,0 +1,139 @@ +# Copyright (c) Facebook, Inc. and its affiliates. + +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +import json +import os +import sys +from unittest import TestCase, skipUnless + +import numpy as np +import pandas as pd +from fbprophet import Prophet +from fbprophet.serialize import model_to_json, model_from_json, PD_SERIES, PD_DATAFRAME + + +DATA = pd.read_csv( + os.path.join(os.path.dirname(__file__), 'data.csv'), + parse_dates=['ds'], +) + + +class TestSerialize(TestCase): + + def test_simple_serialize(self): + m = Prophet() + days = 30 + N = DATA.shape[0] + df = DATA.head(N - days) + m.fit(df) + + future = m.make_future_dataframe(2, include_history=False) + fcst = m.predict(future) + + model_str = model_to_json(m) + # Make sure json doesn't get too large in the future + self.assertTrue(len(model_str) < 200000) + z = json.loads(model_str) + self.assertEqual(z['__fbprophet_version'], '0.6') + + m2 = model_from_json(model_str) + + # Check that m and m2 are equal + self.assertEqual(m.__dict__.keys(), m2.__dict__.keys()) + for k, v in m.__dict__.items(): + if k in ['stan_fit', 'stan_backend']: + continue + if k == 'params': + self.assertEqual(v.keys(), m2.params.keys()) + for kk, vv in v.items(): + self.assertTrue(np.array_equal(vv, m2.params[kk])) + elif k in PD_SERIES and v is not None: + self.assertTrue(v.equals(m2.__dict__[k])) + elif k in PD_DATAFRAME and v is not None: + pd.testing.assert_frame_equal(v, m2.__dict__[k]) + elif k == 'changepoints_t': + self.assertTrue(np.array_equal(v, m.__dict__[k])) + else: + self.assertEqual(v, m2.__dict__[k]) + self.assertTrue(m2.stan_fit is None) + self.assertTrue(m2.stan_backend is None) + + # Check that m2 makes the same forecast + future2 = m2.make_future_dataframe(2, include_history=False) + fcst2 = m2.predict(future2) + + self.assertTrue(np.array_equal(fcst['yhat'].values, fcst2['yhat'].values)) + + def test_full_serialize(self): + # Construct a model with all attributes + holidays = pd.DataFrame({ + 'ds': pd.to_datetime(['2012-06-06', '2013-06-06']), + 'holiday': ['seans-bday'] * 2, + 'lower_window': [0] * 2, + 'upper_window': [1] * 2, + }) + # Test with holidays and country_holidays + m = Prophet( + holidays=holidays, + seasonality_mode='multiplicative', + changepoints=['2012-07-01', '2012-10-01', '2013-01-01'], + ) + m.add_country_holidays(country_name='US') + m.add_seasonality(name='conditional_weekly', period=7, fourier_order=3, + prior_scale=2., condition_name='is_conditional_week') + m.add_seasonality(name='normal_monthly', period=30.5, fourier_order=5, + prior_scale=2.) + df = DATA.copy() + df['is_conditional_week'] = [0] * 255 + [1] * 255 + m.add_regressor('binary_feature', prior_scale=0.2) + m.add_regressor('numeric_feature', prior_scale=0.5) + m.add_regressor( + 'numeric_feature2', prior_scale=0.5, mode='multiplicative' + ) + m.add_regressor('binary_feature2', standardize=True) + df['binary_feature'] = ['0'] * 255 + ['1'] * 255 + df['numeric_feature'] = range(510) + df['numeric_feature2'] = range(510) + df['binary_feature2'] = [1] * 100 + [0] * 410 + + train = df.head(400) + test = df.tail(100) + + m.fit(train) + future = m.make_future_dataframe(periods=100, include_history=False) + fcst = m.predict(test) + # Serialize! + m2 = model_from_json(model_to_json(m)) + + # Check that m and m2 are equal + self.assertEqual(m.__dict__.keys(), m2.__dict__.keys()) + for k, v in m.__dict__.items(): + if k in ['stan_fit', 'stan_backend']: + continue + if k == 'params': + self.assertEqual(v.keys(), m2.params.keys()) + for kk, vv in v.items(): + self.assertTrue(np.array_equal(vv, m2.params[kk])) + elif k in PD_SERIES and v is not None: + self.assertTrue(v.equals(m2.__dict__[k])) + elif k in PD_DATAFRAME and v is not None: + pd.testing.assert_frame_equal(v, m2.__dict__[k]) + elif k == 'changepoints_t': + self.assertTrue(np.array_equal(v, m.__dict__[k])) + else: + self.assertEqual(v, m2.__dict__[k]) + self.assertTrue(m2.stan_fit is None) + self.assertTrue(m2.stan_backend is None) + + # Check that m2 makes the same forecast + future = m2.make_future_dataframe(periods=100, include_history=False) + fcst2 = m2.predict(test) + + self.assertTrue(np.array_equal(fcst['yhat'].values, fcst2['yhat'].values))