-
Notifications
You must be signed in to change notification settings - Fork 82
Fix saving/loading ProphetModel
#1019
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from copy import deepcopy | ||
from datetime import datetime | ||
from typing import Dict | ||
from typing import Iterable | ||
|
@@ -16,6 +17,8 @@ | |
|
||
if SETTINGS.prophet_required: | ||
from prophet import Prophet | ||
from prophet.serialize import model_from_dict | ||
from prophet.serialize import model_to_dict | ||
|
||
|
||
class _ProphetAdapter(BaseAdapter): | ||
|
@@ -62,11 +65,16 @@ def __init__( | |
self.stan_backend = stan_backend | ||
self.additional_seasonality_params = additional_seasonality_params | ||
|
||
self.model = Prophet( | ||
self.model = self._create_model() | ||
|
||
self.regressor_columns: Optional[List[str]] = None | ||
|
||
def _create_model(self) -> "Prophet": | ||
model = Prophet( | ||
growth=self.growth, | ||
changepoints=changepoints, | ||
n_changepoints=n_changepoints, | ||
changepoint_range=changepoint_range, | ||
changepoints=self.changepoints, | ||
n_changepoints=self.n_changepoints, | ||
changepoint_range=self.changepoint_range, | ||
yearly_seasonality=self.yearly_seasonality, | ||
weekly_seasonality=self.weekly_seasonality, | ||
daily_seasonality=self.daily_seasonality, | ||
|
@@ -84,7 +92,7 @@ def __init__( | |
for seasonality_params in self.additional_seasonality_params: | ||
self.model.add_seasonality(**seasonality_params) | ||
|
||
self.regressor_columns: Optional[List[str]] = None | ||
return model | ||
|
||
def fit(self, df: pd.DataFrame, regressors: List[str]) -> "_ProphetAdapter": | ||
""" | ||
|
@@ -154,6 +162,33 @@ def get_model(self) -> Prophet: | |
""" | ||
return self.model | ||
|
||
def __getstate__(self): | ||
state = self.__dict__.copy() | ||
try: | ||
model_dict = model_to_dict(self.model) | ||
is_fitted = True | ||
except ValueError: | ||
is_fitted = False | ||
model_dict = {} | ||
del state["model"] | ||
state["_is_fitted"] = is_fitted | ||
state["_model_dict"] = model_dict | ||
return state | ||
|
||
def __setstate__(self, state): | ||
local_state = deepcopy(state) | ||
is_fitted = local_state["_is_fitted"] | ||
model_dict = local_state["_model_dict"] | ||
del local_state["_is_fitted"] | ||
del local_state["_model_dict"] | ||
|
||
self.__dict__.update(local_state) | ||
|
||
if is_fitted: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be we can move model creation logic to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really think that it is a good idea. Now |
||
self.model = model_from_dict(model_dict) | ||
else: | ||
self.model = self._create_model() | ||
|
||
|
||
class ProphetModel( | ||
PerSegmentModelMixin, PredictionIntervalContextIgnorantModelMixin, PredictionIntervalContextIgnorantAbstractModel | ||
|
@@ -165,11 +200,6 @@ class ProphetModel( | |
Original Prophet can use features 'cap' and 'floor', | ||
they should be added to the known_future list on dataset initialization. | ||
|
||
Warning | ||
------- | ||
Currently, pickle is used in ``save`` and ``load`` methods. | ||
It can work unreliably according to `documentation <https://facebook.github.io/prophet/docs/additional_topics.html>`_. | ||
|
||
Examples | ||
-------- | ||
>>> from etna.datasets import generate_periodic_df | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add description to the method