Skip to content

Commit

Permalink
FrameT_to_Frame
Browse files Browse the repository at this point in the history
  • Loading branch information
elephaint committed Nov 13, 2024
1 parent 80ebb0f commit 915719e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 50 deletions.
26 changes: 13 additions & 13 deletions hierarchicalforecast/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from .methods import HReconciler
from inspect import signature
from narwhals.typing import FrameT
from narwhals.typing import Frame
from scipy.stats import norm
from scipy import sparse
from typing import Dict, List, Optional
Expand Down Expand Up @@ -39,7 +39,7 @@ def _build_fn_name(fn) -> str:
return fn_name

# %% ../nbs/src/core.ipynb 10
def _reverse_engineer_sigmah(Y_hat_df: FrameT,
def _reverse_engineer_sigmah(Y_hat_df: Frame,
y_hat: np.ndarray,
model_name: str,
id_col: str = "unique_id",
Expand Down Expand Up @@ -102,9 +102,9 @@ def __init__(self,
self.insample = any([method.insample for method in reconcilers])

def _prepare_fit(self,
Y_hat_df: FrameT,
S_df: FrameT,
Y_df: Optional[FrameT],
Y_hat_df: Frame,
S_df: Frame,
Y_df: Optional[Frame],
tags: Dict[str, np.ndarray],
level: Optional[List[int]] = None,
intervals_method: str = 'normality',
Expand Down Expand Up @@ -198,8 +198,8 @@ def _prepare_fit(self,
return Y_hat_df_nw, S_df_nw, Y_df_nw, model_names

def _prepare_Y(self,
Y_df: FrameT,
S_df: FrameT,
Y_df: Frame,
S_df: Frame,
is_balanced: bool = True,
id_col: str = "unique_id",
time_col: str = "ds",
Expand All @@ -225,10 +225,10 @@ def _prepare_Y(self,


def reconcile(self,
Y_hat_df: FrameT,
S: FrameT,
Y_hat_df: Frame,
S: Frame,
tags: Dict[str, np.ndarray],
Y_df: Optional[FrameT] = None,
Y_df: Optional[Frame] = None,
level: Optional[List[int]] = None,
intervals_method: str = 'normality',
num_samples: int = -1,
Expand Down Expand Up @@ -407,10 +407,10 @@ def reconcile(self,
return Y_tilde_df.to_native()

def bootstrap_reconcile(self,
Y_hat_df: FrameT,
S_df: FrameT,
Y_hat_df: Frame,
S_df: Frame,
tags: Dict[str, np.ndarray],
Y_df: Optional[FrameT] = None,
Y_df: Optional[Frame] = None,
level: Optional[List[int]] = None,
intervals_method: str = 'normality',
num_samples: int = -1,
Expand Down
11 changes: 5 additions & 6 deletions hierarchicalforecast/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import narwhals as nw
import numpy as np

from .core import pivot
from narwhals.typing import FrameT
from narwhals.typing import Frame
from scipy.stats import multivariate_normal

# %% ../nbs/src/evaluation.ipynb 6
Expand Down Expand Up @@ -341,10 +340,10 @@ def __init__(self,
self.evaluators = evaluators

def evaluate(self,
Y_hat_df: FrameT,
Y_test_df: FrameT,
Y_hat_df: Frame,
Y_test_df: Frame,
tags: Dict[str, np.ndarray],
Y_df: Optional[FrameT] = None,
Y_df: Optional[Frame] = None,
benchmark: Optional[str] = None,
id_col: str = "unique_id",
time_col: str = "ds",
Expand Down Expand Up @@ -396,7 +395,7 @@ def evaluate(self,
y_test_cats = Y_h_cats[target_col].to_numpy().reshape(-1, h)

if has_y_insample and Y_df is not None:
y_insample = pivot(Y_df_nw, index = id_col, columns = time_col, values = target_col)
y_insample = Y_df_nw.pivot(on=time_col, index = id_col, values = target_col, sort_columns=True).sort(by=id_col)
y_insample = y_insample.filter(nw.col(id_col).is_in(cats))
y_insample = y_insample.drop(id_col).to_numpy()

Expand Down
12 changes: 6 additions & 6 deletions hierarchicalforecast/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import numpy as np
import pandas as pd

from narwhals.typing import FrameT
from narwhals.typing import Frame
from numba import njit, prange
from sklearn.preprocessing import OneHotEncoder

Expand Down Expand Up @@ -89,7 +89,7 @@ def join_upper(bottom_value):

# %% ../nbs/src/utils.ipynb 13
def aggregate(
df: FrameT,
df: Frame,
spec: List[List[str]],
exog_vars: Optional[Dict[str, Union[str, List[str]]]] = None,
sparse_s: bool = False,
Expand Down Expand Up @@ -237,7 +237,7 @@ class HierarchicalPlot:
each key is a level and its value contains tags associated to that level.<br><br>
"""
def __init__(self,
S: FrameT,
S: Frame,
tags: Dict[str, np.ndarray],
):

Expand All @@ -258,7 +258,7 @@ def plot_summing_matrix(self):

def plot_series(self,
series: str,
Y_df: FrameT,
Y_df: Frame,
models: Optional[List[str]] = None,
level: Optional[List[int]] = None,
id_col: str = "unique_id",
Expand Down Expand Up @@ -322,7 +322,7 @@ def plot_series(self,

def plot_hierarchically_linked_series(self,
bottom_series: str,
Y_df: FrameT,
Y_df: Frame,
models: Optional[List[str]] = None,
level: Optional[List[int]] = None,
id_col: str = "unique_id",
Expand Down Expand Up @@ -394,7 +394,7 @@ def plot_hierarchically_linked_series(self,
fig.legend(handles, labels, **kwargs)

def plot_hierarchical_predictions_gap(self,
Y_df: FrameT,
Y_df: Frame,
models: Optional[List[str]] = None,
xlabel: Optional[str] = None,
ylabel: Optional[str] = None,
Expand Down
26 changes: 13 additions & 13 deletions nbs/src/core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"\n",
"from hierarchicalforecast.methods import HReconciler\n",
"from inspect import signature\n",
"from narwhals.typing import FrameT\n",
"from narwhals.typing import Frame\n",
"from scipy.stats import norm\n",
"from scipy import sparse\n",
"from typing import Dict, List, Optional\n",
Expand Down Expand Up @@ -144,7 +144,7 @@
"outputs": [],
"source": [
"#| exporti\n",
"def _reverse_engineer_sigmah(Y_hat_df: FrameT, \n",
"def _reverse_engineer_sigmah(Y_hat_df: Frame, \n",
" y_hat: np.ndarray, \n",
" model_name: str,\n",
" id_col: str = \"unique_id\",\n",
Expand Down Expand Up @@ -214,9 +214,9 @@
" self.insample = any([method.insample for method in reconcilers])\n",
" \n",
" def _prepare_fit(self,\n",
" Y_hat_df: FrameT,\n",
" S_df: FrameT,\n",
" Y_df: Optional[FrameT],\n",
" Y_hat_df: Frame,\n",
" S_df: Frame,\n",
" Y_df: Optional[Frame],\n",
" tags: Dict[str, np.ndarray],\n",
" level: Optional[List[int]] = None,\n",
" intervals_method: str = 'normality',\n",
Expand Down Expand Up @@ -310,8 +310,8 @@
" return Y_hat_df_nw, S_df_nw, Y_df_nw, model_names\n",
"\n",
" def _prepare_Y(self, \n",
" Y_df: FrameT, \n",
" S_df: FrameT, \n",
" Y_df: Frame, \n",
" S_df: Frame, \n",
" is_balanced: bool = True,\n",
" id_col: str = \"unique_id\",\n",
" time_col: str = \"ds\", \n",
Expand All @@ -337,10 +337,10 @@
"\n",
"\n",
" def reconcile(self, \n",
" Y_hat_df: FrameT,\n",
" S: FrameT,\n",
" Y_hat_df: Frame,\n",
" S: Frame,\n",
" tags: Dict[str, np.ndarray],\n",
" Y_df: Optional[FrameT] = None,\n",
" Y_df: Optional[Frame] = None,\n",
" level: Optional[List[int]] = None,\n",
" intervals_method: str = 'normality',\n",
" num_samples: int = -1,\n",
Expand Down Expand Up @@ -519,10 +519,10 @@
" return Y_tilde_df.to_native()\n",
"\n",
" def bootstrap_reconcile(self,\n",
" Y_hat_df: FrameT,\n",
" S_df: FrameT,\n",
" Y_hat_df: Frame,\n",
" S_df: Frame,\n",
" tags: Dict[str, np.ndarray],\n",
" Y_df: Optional[FrameT] = None,\n",
" Y_df: Optional[Frame] = None,\n",
" level: Optional[List[int]] = None,\n",
" intervals_method: str = 'normality',\n",
" num_samples: int = -1,\n",
Expand Down
11 changes: 5 additions & 6 deletions nbs/src/evaluation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
"import narwhals as nw\n",
"import numpy as np\n",
"\n",
"from hierarchicalforecast.core import pivot\n",
"from narwhals.typing import FrameT\n",
"from narwhals.typing import Frame\n",
"from scipy.stats import multivariate_normal"
]
},
Expand Down Expand Up @@ -533,10 +532,10 @@
" self.evaluators = evaluators\n",
"\n",
" def evaluate(self, \n",
" Y_hat_df: FrameT,\n",
" Y_test_df: FrameT,\n",
" Y_hat_df: Frame,\n",
" Y_test_df: Frame,\n",
" tags: Dict[str, np.ndarray],\n",
" Y_df: Optional[FrameT] = None,\n",
" Y_df: Optional[Frame] = None,\n",
" benchmark: Optional[str] = None,\n",
" id_col: str = \"unique_id\",\n",
" time_col: str = \"ds\", \n",
Expand Down Expand Up @@ -588,7 +587,7 @@
" y_test_cats = Y_h_cats[target_col].to_numpy().reshape(-1, h)\n",
"\n",
" if has_y_insample and Y_df is not None:\n",
" y_insample = pivot(Y_df_nw, index = id_col, columns = time_col, values = target_col)\n",
" y_insample = Y_df_nw.pivot(on=time_col, index = id_col, values = target_col, sort_columns=True).sort(by=id_col)\n",
" y_insample = y_insample.filter(nw.col(id_col).is_in(cats))\n",
" y_insample = y_insample.drop(id_col).to_numpy()\n",
"\n",
Expand Down
12 changes: 6 additions & 6 deletions nbs/src/utils.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from narwhals.typing import FrameT\n",
"from narwhals.typing import Frame\n",
"from numba import njit, prange\n",
"from sklearn.preprocessing import OneHotEncoder\n",
"\n",
Expand Down Expand Up @@ -323,7 +323,7 @@
"source": [
"#| export\n",
"def aggregate(\n",
" df: FrameT,\n",
" df: Frame,\n",
" spec: List[List[str]],\n",
" exog_vars: Optional[Dict[str, Union[str, List[str]]]] = None,\n",
" sparse_s: bool = False,\n",
Expand Down Expand Up @@ -871,7 +871,7 @@
" each key is a level and its value contains tags associated to that level.<br><br>\n",
" \"\"\"\n",
" def __init__(self,\n",
" S: FrameT,\n",
" S: Frame,\n",
" tags: Dict[str, np.ndarray],\n",
" ):\n",
"\n",
Expand All @@ -892,7 +892,7 @@
"\n",
" def plot_series(self,\n",
" series: str,\n",
" Y_df: FrameT,\n",
" Y_df: Frame,\n",
" models: Optional[List[str]] = None,\n",
" level: Optional[List[int]] = None,\n",
" id_col: str = \"unique_id\",\n",
Expand Down Expand Up @@ -956,7 +956,7 @@
" \n",
" def plot_hierarchically_linked_series(self,\n",
" bottom_series: str,\n",
" Y_df: FrameT,\n",
" Y_df: Frame,\n",
" models: Optional[List[str]] = None,\n",
" level: Optional[List[int]] = None,\n",
" id_col: str = \"unique_id\",\n",
Expand Down Expand Up @@ -1028,7 +1028,7 @@
" fig.legend(handles, labels, **kwargs)\n",
"\n",
" def plot_hierarchical_predictions_gap(self,\n",
" Y_df: FrameT,\n",
" Y_df: Frame,\n",
" models: Optional[List[str]] = None,\n",
" xlabel: Optional[str] = None,\n",
" ylabel: Optional[str] = None,\n",
Expand Down

0 comments on commit 915719e

Please sign in to comment.