Skip to content
This repository has been archived by the owner on Oct 7, 2023. It is now read-only.

feature(NeuralProphet): added WrapNeuralProphet class #17

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/test-neuralprophet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: test-neuralprophet

on: push

jobs:

run-tests:
runs-on: ubuntu-latest

steps:
- name: checkout
uses: actions/checkout@v3

- name: setup-python
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: install-dependencies
run: |
python -m pip install --upgrade pip
pip install ".[tests]"
pip install "neuralprophet>=0.5.2"
- name: run-tests
run: pytest tests/test_neuralprophet.py -s --durations 0
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ dmypy.json

# Pyre type checker
.pyre/

lightning_logs/*
1 change: 1 addition & 0 deletions src/fold_models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .neuralprophet import WrapNeuralProphet
from .sktime import WrapSktime
from .statsforecast import WrapStatsForecast
from .statsmodels import WrapStatsModels
Expand Down
51 changes: 51 additions & 0 deletions src/fold_models/neuralprophet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from typing import Any, Optional, Union

import pandas as pd
from fold.models.base import Model


class WrapNeuralProphet(Model):
properties = Model.Properties(
model_type=Model.Properties.ModelType.regressor,
)

fitted = False

def __init__(self, model: Any) -> None:
self.model = model
from neuralprophet import set_random_seed

set_random_seed(0)
self.name = "NeuralProphet"

def fit(
self, X: pd.DataFrame, y: pd.Series, sample_weights: Optional[pd.Series] = None
) -> None:
data = pd.DataFrame(
{"ds": X.index, "y": y.values},
index=range(0, len(y)),
)
self.training_metrics = self.model.fit(data, epochs=40)

def predict(self, X: pd.DataFrame) -> Union[pd.Series, pd.DataFrame]:
data = pd.DataFrame(
{"ds": X.index, "y": 0.0},
index=range(0, len(X)),
)
future = self.model.make_future_dataframe(data, periods=len(X))
predictions = self.model.predict(future)["yhat1"]
return pd.Series(
predictions.values, index=X.index, name=self.name + "_predictions"
)

def __deepcopy__(self, memo):
from io import BytesIO

from neuralprophet.utils import load, save

buff = BytesIO()

save(self.model, buff)
buff.seek(0)
model = load(buff)
return NeuralProphetWrapper(model)
3 changes: 1 addition & 2 deletions src/fold_models/xgboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,4 @@ def update(
def predict(self, X: pd.DataFrame) -> Union[pd.Series, pd.DataFrame]:
return pd.Series(self.model.predict(X), index=X.index)

def predict_in_sample(self, X: pd.DataFrame) -> Union[pd.Series, pd.DataFrame]:
return pd.Series(self.model.predict(X), index=X.index)
predict_in_sample = predict
21 changes: 21 additions & 0 deletions tests/test_neuralprophet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from fold.loop import backtest, train
from fold.splitters import ExpandingWindowSplitter
from fold.utils.tests import generate_sine_wave_data
from neuralprophet import NeuralProphet

from fold_models.neuralprophet import NeuralProphetWrapper


def test_neuralprophet() -> None:
X = generate_sine_wave_data(resolution=400)
y = X.shift(-1).squeeze()

splitter = ExpandingWindowSplitter(train_window_size=300, step=50)
transformations = NeuralProphetWrapper(NeuralProphet(yearly_seasonality=False))

transformations_over_time = train(transformations, X, y, splitter)
_, pred = backtest(transformations_over_time, X, y, splitter)
assert (y.squeeze()[pred.index][:-1] - pred.squeeze()[:-1]).abs().sum() < 20


test_neuralprophet()