Skip to content

Commit

Permalink
Check data was anomalized
Browse files Browse the repository at this point in the history
  • Loading branch information
mdancho84 committed Oct 21, 2023
1 parent e6e42a8 commit 84bf5b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/pytimetk/plot/plot_anomalies.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from pytimetk.plot.plot_timeseries import plot_timeseries
from pytimetk.utils.plot_helpers import hex_to_rgba, rgba_to_hex, parse_rgba
from pytimetk.utils.checks import check_dataframe_or_groupby, check_date_column
from pytimetk.utils.checks import check_dataframe_or_groupby, check_date_column, check_anomalize_data
from pytimetk.plot.theme import theme_timetk


Expand Down Expand Up @@ -247,6 +247,9 @@ def plot_anomalies(
check_dataframe_or_groupby(data)
check_date_column(data, date_column)

# Check data was anomalized first
check_anomalize_data(data)

# Handle line_size
if line_size is None:
if engine == 'plotnine':
Expand Down
24 changes: 24 additions & 0 deletions src/pytimetk/utils/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@

from typing import Union, List

def check_anomalize_data(data: Union[pd.DataFrame, pd.core.groupby.generic.DataFrameGroupBy]) -> None:

if isinstance(data, pd.core.groupby.generic.DataFrameGroupBy):
data = data.obj

expected_colnames = [
'observed',
'seasonal',
'seasadj',
'trend',
'remainder',
'anomaly',
'anomaly_score',
'anomaly_direction',
'recomposed_l1',
'recomposed_l2',
'observed_clean'
]

if not all([column in data.columns for column in expected_colnames]):
raise ValueError(f"data does not have required colnames: {expected_colnames}. Did you run `anomalize()`?")

return None

def check_dataframe_or_groupby(data: Union[pd.DataFrame, pd.core.groupby.generic.DataFrameGroupBy]) -> None:

if not isinstance(data, pd.DataFrame):
Expand Down

0 comments on commit 84bf5b7

Please sign in to comment.