Skip to content

Commit

Permalink
Support polars
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeix committed Jan 10, 2025
1 parent c8957f4 commit 639c7e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
52 changes: 30 additions & 22 deletions nbs/core.ipynb
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "0ebe1274",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"env: PYTORCH_ENABLE_MPS_FALLBACK=1\n"
]
}
],
"source": [
"%set_env PYTORCH_ENABLE_MPS_FALLBACK=1"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -62,8 +44,8 @@
"text": [
"/Users/marcopeix/miniconda3/envs/neuralforecast/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"2025-01-10 10:22:44,112\tINFO util.py:154 -- Missing packages: ['ipywidgets']. Run `pip install -U ipywidgets`, then restart the notebook server for rich notebook output.\n",
"2025-01-10 10:22:44,153\tINFO util.py:154 -- Missing packages: ['ipywidgets']. Run `pip install -U ipywidgets`, then restart the notebook server for rich notebook output.\n"
"2025-01-10 14:12:15,552\tINFO util.py:154 -- Missing packages: ['ipywidgets']. Run `pip install -U ipywidgets`, then restart the notebook server for rich notebook output.\n",
"2025-01-10 14:12:15,609\tINFO util.py:154 -- Missing packages: ['ipywidgets']. Run `pip install -U ipywidgets`, then restart the notebook server for rich notebook output.\n"
]
}
],
Expand Down Expand Up @@ -1395,7 +1377,10 @@
" trimmed_datasets.append(trimmed_series)\n",
"\n",
" # Combine all series forecasts DataFrames\n",
" fcsts_df = pd.concat(fcsts_dfs, axis=0, ignore_index=True)\n",
" if isinstance(fcsts_dfs[0], pl.DataFrame):\n",
" fcsts_df = pl.concat(fcsts_dfs, how='vertical')\n",
" else:\n",
" fcsts_df = pd.concat(fcsts_dfs, axis=0, ignore_index=True)\n",
" \n",
" # Generate predictions for each model\n",
" fcsts_list = []\n",
Expand Down Expand Up @@ -3530,7 +3515,30 @@
"execution_count": null,
"id": "7502d18c-62a5-4381-bfdb-bba5300c5290",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Seed set to 1\n"
]
},
{
"ename": "Exception",
"evalue": "{'zeros'} historical exogenous variables not found in input dataset",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mException\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[17], line 7\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m# Pandas\u001b[39;00m\n\u001b[1;32m 6\u001b[0m nf \u001b[38;5;241m=\u001b[39m NeuralForecast(models\u001b[38;5;241m=\u001b[39mmodels, freq\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mM\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 7\u001b[0m \u001b[43mnf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mAirPassengersPanel_train\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstatic_df\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mAirPassengersStatic\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 8\u001b[0m insample_preds \u001b[38;5;241m=\u001b[39m nf\u001b[38;5;241m.\u001b[39mpredict_insample()\n\u001b[1;32m 9\u001b[0m preds \u001b[38;5;241m=\u001b[39m nf\u001b[38;5;241m.\u001b[39mpredict()\n",
"Cell \u001b[0;32mIn[10], line 329\u001b[0m, in \u001b[0;36mNeuralForecast.fit\u001b[0;34m(self, df, static_df, val_size, use_init_models, verbose, id_col, time_col, target_col, distributed_config, prediction_intervals)\u001b[0m\n\u001b[1;32m 326\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_reset_models()\n\u001b[1;32m 328\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i, model \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodels):\n\u001b[0;32m--> 329\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodels[i] \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 330\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdataset\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mval_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mval_size\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdistributed_config\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdistributed_config\u001b[49m\n\u001b[1;32m 331\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 333\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_fitted \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
"File \u001b[0;32m~/miniconda3/envs/neuralforecast/lib/python3.10/site-packages/neuralforecast/common/_base_recurrent.py:535\u001b[0m, in \u001b[0;36mBaseRecurrent.fit\u001b[0;34m(self, dataset, val_size, test_size, random_seed, distributed_config)\u001b[0m\n\u001b[1;32m 506\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfit\u001b[39m(\n\u001b[1;32m 507\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 508\u001b[0m dataset,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 512\u001b[0m distributed_config\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 513\u001b[0m ):\n\u001b[1;32m 514\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Fit.\u001b[39;00m\n\u001b[1;32m 515\u001b[0m \n\u001b[1;32m 516\u001b[0m \u001b[38;5;124;03m The `fit` method, optimizes the neural network's weights using the\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 533\u001b[0m \u001b[38;5;124;03m `random_seed`: int=None, random_seed for pytorch initializer and numpy generators, overwrites model.__init__'s.<br>\u001b[39;00m\n\u001b[1;32m 534\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 535\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_fit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 536\u001b[0m \u001b[43m \u001b[49m\u001b[43mdataset\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdataset\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 537\u001b[0m \u001b[43m \u001b[49m\u001b[43mbatch_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbatch_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 538\u001b[0m \u001b[43m \u001b[49m\u001b[43mvalid_batch_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalid_batch_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 539\u001b[0m \u001b[43m \u001b[49m\u001b[43mval_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mval_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 540\u001b[0m \u001b[43m \u001b[49m\u001b[43mtest_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtest_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 541\u001b[0m \u001b[43m \u001b[49m\u001b[43mrandom_seed\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrandom_seed\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 542\u001b[0m \u001b[43m \u001b[49m\u001b[43mdistributed_config\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdistributed_config\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 543\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/neuralforecast/lib/python3.10/site-packages/neuralforecast/common/_base_model.py:325\u001b[0m, in \u001b[0;36mBaseModel._fit\u001b[0;34m(self, dataset, batch_size, valid_batch_size, val_size, test_size, random_seed, shuffle_train, distributed_config)\u001b[0m\n\u001b[1;32m 314\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_fit\u001b[39m(\n\u001b[1;32m 315\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 316\u001b[0m dataset,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 323\u001b[0m distributed_config\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 324\u001b[0m ):\n\u001b[0;32m--> 325\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_check_exog\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdataset\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 326\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_restart_seed(random_seed)\n\u001b[1;32m 328\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mval_size \u001b[38;5;241m=\u001b[39m val_size\n",
"File \u001b[0;32m~/miniconda3/envs/neuralforecast/lib/python3.10/site-packages/neuralforecast/common/_base_model.py:201\u001b[0m, in \u001b[0;36mBaseModel._check_exog\u001b[0;34m(self, dataset)\u001b[0m\n\u001b[1;32m 199\u001b[0m missing_stat \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mset\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstat_exog_list) \u001b[38;5;241m-\u001b[39m static_cols\n\u001b[1;32m 200\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m missing_hist:\n\u001b[0;32m--> 201\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\n\u001b[1;32m 202\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmissing_hist\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m historical exogenous variables not found in input dataset\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 203\u001b[0m )\n\u001b[1;32m 204\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m missing_futr:\n\u001b[1;32m 205\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\n\u001b[1;32m 206\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmissing_futr\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m future exogenous variables not found in input dataset\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 207\u001b[0m )\n",
"\u001b[0;31mException\u001b[0m: {'zeros'} historical exogenous variables not found in input dataset"
]
}
],
"source": [
"#| hide\n",
"#| polars\n",
Expand Down
15 changes: 9 additions & 6 deletions neuralforecast/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# %% auto 0
__all__ = ['NeuralForecast']

# %% ../nbs/core.ipynb 5
# %% ../nbs/core.ipynb 4
import pickle
import warnings
from copy import deepcopy
Expand Down Expand Up @@ -70,7 +70,7 @@
from .common._base_auto import BaseAuto, MockTrial
from .utils import PredictionIntervals, get_prediction_interval_method

# %% ../nbs/core.ipynb 6
# %% ../nbs/core.ipynb 5
# this disables warnings about the number of workers in the dataloaders
# which the user can't control
warnings.filterwarnings("ignore", category=pl.utilities.warnings.PossibleUserWarning)
Expand Down Expand Up @@ -128,7 +128,7 @@ def _insample_times(
out = ufp.assign_columns(out, "cutoff", actual_cutoffs)
return out

# %% ../nbs/core.ipynb 8
# %% ../nbs/core.ipynb 7
MODEL_FILENAME_DICT = {
"autoformer": Autoformer,
"autoautoformer": Autoformer,
Expand Down Expand Up @@ -195,7 +195,7 @@ def _insample_times(
"autormok": RMoK,
}

# %% ../nbs/core.ipynb 9
# %% ../nbs/core.ipynb 8
_type2scaler = {
"standard": LocalStandardScaler,
"robust": lambda: LocalRobustScaler(scale="mad"),
Expand All @@ -204,7 +204,7 @@ def _insample_times(
"boxcox": lambda: LocalBoxCoxScaler(method="loglik", lower=0.0),
}

# %% ../nbs/core.ipynb 10
# %% ../nbs/core.ipynb 9
class NeuralForecast:

def __init__(
Expand Down Expand Up @@ -1349,7 +1349,10 @@ def predict_insample(self, step_size: int = 1):
trimmed_datasets.append(trimmed_series)

# Combine all series forecasts DataFrames
fcsts_df = pd.concat(fcsts_dfs, axis=0, ignore_index=True)
if isinstance(fcsts_dfs[0], pl.DataFrame):
fcsts_df = pl.concat(fcsts_dfs, how="vertical")
else:
fcsts_df = pd.concat(fcsts_dfs, axis=0, ignore_index=True)

# Generate predictions for each model
fcsts_list = []
Expand Down

0 comments on commit 639c7e9

Please sign in to comment.