Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tags to hier datasets #2

Merged
merged 1 commit into from
Jun 29, 2022
Merged
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
1 change: 0 additions & 1 deletion datasetsforecast/_nbdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
__all__ = ["index", "modules", "custom_doc_links", "git_url"]

index = {"Labour": "hierarchical.ipynb",
"Tourism": "hierarchical.ipynb",
"TourismLarge": "hierarchical.ipynb",
"TourismSmall": "hierarchical.ipynb",
"Traffic": "hierarchical.ipynb",
Expand Down
71 changes: 54 additions & 17 deletions datasetsforecast/hierarchical.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/hierarchical.ipynb (unless otherwise specified).

__all__ = ['Labour', 'Tourism', 'TourismLarge', 'TourismSmall', 'Traffic', 'Wiki2', 'HierarchicalInfo',
'HierarchicalData']
__all__ = ['Labour', 'TourismLarge', 'TourismSmall', 'Traffic', 'Wiki2', 'HierarchicalInfo', 'HierarchicalData']

# Cell
from dataclasses import dataclass
from pathlib import Path
from typing import Tuple

import numpy as np
import pandas as pd

from .utils import download_file, Info
Expand All @@ -18,46 +18,74 @@ class Labour:
freq: str = 'M'
horizon: int = 8
seasonality: int = 12

# Cell
@dataclass
class Tourism:
freq: str = 'Q'
horizon: int = 4
seasonality: int = 4
tags_names: Tuple[str] = (
'Country',
'Country/Region',
'Country/Gender/Region',
'Country/Employment/Gender/Region',
)

# Cell
@dataclass
class TourismLarge:
freq: str = 'M'
horizon: int = 12
seasonality: int = 12
tags_names: Tuple[str] = (
'Country',
'Country/State',
'Country/State/Zone',
'Country/State/Zone/Region',
'Country/Purpose',
'Country/State/Purpose',
'Country/State/Zone/Purpose',
'Country/State/Zone/Region/Purpose',
)

# Cell
@dataclass
class TourismSmall:
freq: str = 'Q'
horizon: int = 4
seasonality: int = 4
horizon: int = 8
seasonality: int = 8
tags_names: Tuple[str] = (
'Country',
'Country/Purpose',
'Country/Purpose/State',
'Country/Purpose/State/CityNonCity',
)

# Cell
@dataclass
class Traffic:
freq: str = 'D'
horizon: int = 14
seasonality: int = 7
tags_names: Tuple[str] = (
'Level0',
'Level1',
'Level2',
'Level2',
)

# Cell
@dataclass
class Wiki2:
freq: str = 'D'
horizon: int = 1
seasonality: int = 7
tags_names: Tuple[str] = (
'Views',
'Views/Country',
'Views/Country/Access',
'Views/Country/Access/Agent',
'Views/Country/Access/Topic'
)

# Cell
HierarchicalInfo = Info(
(
Labour, Tourism, TourismLarge,
Labour, TourismLarge,
TourismSmall,
Traffic, Wiki2
)
Expand Down Expand Up @@ -99,9 +127,9 @@ def load(directory: str,
file_cache = Path(f'{path}/{group}.p')

if file_cache.is_file() and cache:
Y_df, S = pd.read_pickle(file_cache)
Y_df, S, tags = pd.read_pickle(file_cache)

return Y_df, S
return Y_df, S, tags

HierarchicalData.download(directory)
path = Path(f'{path}/{group}')
Expand All @@ -115,11 +143,20 @@ def load(directory: str,
if not all(Y_df['unique_id'].unique() == S.index):
raise Exception('mismatch order between `Y_df` and `S`')

if cache:
pd.to_pickle((Y_df, S), file_cache)
def get_levels_from_S(S):
cut_idxs, = np.where(S.sum(axis=1).cumsum() % S.shape[1] == 0.)
levels = [S.iloc[(cut_idxs[i] + 1):(cut_idxs[i+1] + 1)].index.values for i in range(cut_idxs.size-1)]
levels = [S.iloc[[0]].index.values] + levels
assert sum([len(lv) for lv in levels]) == S.shape[0]
return levels

cls_group = HierarchicalInfo[group]
tags = dict(zip(cls_group.tags_names, get_levels_from_S(S)))

return Y_df, S
if cache:
pd.to_pickle((Y_df, S, tags), file_cache)

return Y_df, S, tags

@staticmethod
def download(directory: str) -> None:
Expand Down
87 changes: 60 additions & 27 deletions nbs/hierarchical.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"from pathlib import Path\n",
"from typing import Tuple\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from datasetsforecast.utils import download_file, Info"
Expand All @@ -45,21 +46,13 @@
"class Labour:\n",
" freq: str = 'M'\n",
" horizon: int = 8\n",
" seasonality: int = 12"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"@dataclass\n",
"class Tourism:\n",
" freq: str = 'Q'\n",
" horizon: int = 4\n",
" seasonality: int = 4"
" seasonality: int = 12\n",
" tags_names: Tuple[str] = (\n",
" 'Country',\n",
" 'Country/Region',\n",
" 'Country/Gender/Region',\n",
" 'Country/Employment/Gender/Region',\n",
" )"
]
},
{
Expand All @@ -73,7 +66,17 @@
"class TourismLarge:\n",
" freq: str = 'M'\n",
" horizon: int = 12\n",
" seasonality: int = 12"
" seasonality: int = 12\n",
" tags_names: Tuple[str] = (\n",
" 'Country',\n",
" 'Country/State',\n",
" 'Country/State/Zone',\n",
" 'Country/State/Zone/Region',\n",
" 'Country/Purpose',\n",
" 'Country/State/Purpose',\n",
" 'Country/State/Zone/Purpose',\n",
" 'Country/State/Zone/Region/Purpose',\n",
" )"
]
},
{
Expand All @@ -86,8 +89,14 @@
"@dataclass\n",
"class TourismSmall:\n",
" freq: str = 'Q'\n",
" horizon: int = 4\n",
" seasonality: int = 4"
" horizon: int = 8\n",
" seasonality: int = 8\n",
" tags_names: Tuple[str] = (\n",
" 'Country',\n",
" 'Country/Purpose',\n",
" 'Country/Purpose/State',\n",
" 'Country/Purpose/State/CityNonCity',\n",
" )"
]
},
{
Expand All @@ -101,7 +110,13 @@
"class Traffic:\n",
" freq: str = 'D'\n",
" horizon: int = 14\n",
" seasonality: int = 7"
" seasonality: int = 7\n",
" tags_names: Tuple[str] = (\n",
" 'Level0',\n",
" 'Level1',\n",
" 'Level2',\n",
" 'Level2',\n",
" )"
]
},
{
Expand All @@ -115,7 +130,14 @@
"class Wiki2:\n",
" freq: str = 'D'\n",
" horizon: int = 1\n",
" seasonality: int = 7"
" seasonality: int = 7\n",
" tags_names: Tuple[str] = (\n",
" 'Views',\n",
" 'Views/Country',\n",
" 'Views/Country/Access',\n",
" 'Views/Country/Access/Agent',\n",
" 'Views/Country/Access/Topic'\n",
" )"
]
},
{
Expand All @@ -127,7 +149,7 @@
"#export\n",
"HierarchicalInfo = Info(\n",
" (\n",
" Labour, Tourism, TourismLarge, \n",
" Labour, TourismLarge, \n",
" TourismSmall,\n",
" Traffic, Wiki2\n",
" )\n",
Expand Down Expand Up @@ -176,9 +198,9 @@
" file_cache = Path(f'{path}/{group}.p')\n",
"\n",
" if file_cache.is_file() and cache:\n",
" Y_df, S = pd.read_pickle(file_cache)\n",
" Y_df, S, tags = pd.read_pickle(file_cache)\n",
"\n",
" return Y_df, S\n",
" return Y_df, S, tags\n",
"\n",
" HierarchicalData.download(directory)\n",
" path = Path(f'{path}/{group}')\n",
Expand All @@ -191,12 +213,21 @@
" \n",
" if not all(Y_df['unique_id'].unique() == S.index):\n",
" raise Exception('mismatch order between `Y_df` and `S`')\n",
" \n",
" def get_levels_from_S(S):\n",
" cut_idxs, = np.where(S.sum(axis=1).cumsum() % S.shape[1] == 0.)\n",
" levels = [S.iloc[(cut_idxs[i] + 1):(cut_idxs[i+1] + 1)].index.values for i in range(cut_idxs.size-1)]\n",
" levels = [S.iloc[[0]].index.values] + levels\n",
" assert sum([len(lv) for lv in levels]) == S.shape[0]\n",
" return levels\n",
" \n",
" cls_group = HierarchicalInfo[group]\n",
" tags = dict(zip(cls_group.tags_names, get_levels_from_S(S)))\n",
" \n",
" if cache:\n",
" pd.to_pickle((Y_df, S), file_cache)\n",
"\n",
" return Y_df, S\n",
" pd.to_pickle((Y_df, S, tags), file_cache)\n",
"\n",
" return Y_df, S, tags\n",
" \n",
" @staticmethod\n",
" def download(directory: str) -> None:\n",
Expand All @@ -219,8 +250,10 @@
"metadata": {},
"outputs": [],
"source": [
"#hide\n",
"for group, _ in HierarchicalInfo:\n",
" Y_df, S = HierarchicalData.load('./data', group)"
" Y_df, S, tags = HierarchicalData.load('./data', group)\n",
" assert all(S.loc[cats].values.sum() == S.shape[1] for _, cats in tags.items())"
]
}
],
Expand Down