From 6f5eeed754dbc69d67d53e4bef4fa4d255543a06 Mon Sep 17 00:00:00 2001 From: Azondekon Date: Tue, 2 May 2023 11:56:54 -0400 Subject: [PATCH] Update docstring example code --- pynssp/data.py | 12 ++++----- pynssp/detectors/ewma.py | 1 + pynssp/detectors/nbinom.py | 1 + pynssp/detectors/regression.py | 1 + pynssp/detectors/serfling.py | 1 + pynssp/detectors/switch.py | 1 + pynssp/detectors/trend.py | 45 ---------------------------------- pynssp/utils.py | 1 + 8 files changed, 12 insertions(+), 51 deletions(-) diff --git a/pynssp/data.py b/pynssp/data.py index 200f4b5..ba5463a 100644 --- a/pynssp/data.py +++ b/pynssp/data.py @@ -6,13 +6,13 @@ def load_simulated_ts(): """Return a dataframe of simulated time series. Contains the following fields: - # Column Non-Null Count Dtype + # Column Non-Null Count Dtype --- ------ -------------- ----- - 0 date 626 non-null object - 1 week 626 non-null int64 - 2 year 626 non-null int64 - 3 cases 626 non-null int64 - 4 id 626 non-null object + 0 date 626 non-null object + 1 week 626 non-null int64 + 2 year 626 non-null int64 + 3 cases 626 non-null int64 + 4 id 626 non-null object dtypes: int64(3), object(2) memory usage: 24.6+ KB """ diff --git a/pynssp/detectors/ewma.py b/pynssp/detectors/ewma.py index f740952..8d3ccb4 100644 --- a/pynssp/detectors/ewma.py +++ b/pynssp/detectors/ewma.py @@ -171,6 +171,7 @@ def alert_ewma(df, t="date", y="count", B=28, g=2, w1=0.4, w2=0.9): Defaults to 0.9 to match ESSENCE implementation and approximate the C2 algorithm. :returns: Original pandas data frame with detection results. :examples: + >>> import pandas as pd >>> import numpy as np >>> from pynssp.detectors.ewma import * diff --git a/pynssp/detectors/nbinom.py b/pynssp/detectors/nbinom.py index 3326f5c..3b83e6c 100644 --- a/pynssp/detectors/nbinom.py +++ b/pynssp/detectors/nbinom.py @@ -127,6 +127,7 @@ def alert_nbinom(df, baseline_end, t="date", y="count", include_time=True): a binary alarm indicator field, and a binary indicator field of whether or not a time term was included. :examples: + >>> import pandas as pd >>> import numpy as np >>> df = pd.DataFrame({ diff --git a/pynssp/detectors/regression.py b/pynssp/detectors/regression.py index 66f7532..1ffbc21 100644 --- a/pynssp/detectors/regression.py +++ b/pynssp/detectors/regression.py @@ -158,6 +158,7 @@ def alert_regression(df, t="date", y="count", B=28, g=2): Defaults to 2. :returns: Original pandas data frame with detection results. :examples: + >>> import pandas as pd >>> import numpy as np >>> from pynssp.detectors.regression import * diff --git a/pynssp/detectors/serfling.py b/pynssp/detectors/serfling.py index bf4b5d5..107b35d 100644 --- a/pynssp/detectors/serfling.py +++ b/pynssp/detectors/serfling.py @@ -79,6 +79,7 @@ def alert_serfling(df, baseline_end, t="date", y="count"): :returns: Original pandas dataframe with model estimates, upper prediction interval bounds, a binary alarm indicator field, and a binary indicator :examples: + >>> import pandas as pd >>> import numpy as np >>> df = pd.DataFrame({ diff --git a/pynssp/detectors/switch.py b/pynssp/detectors/switch.py index 383a7fb..aa4afdf 100644 --- a/pynssp/detectors/switch.py +++ b/pynssp/detectors/switch.py @@ -32,6 +32,7 @@ def alert_switch(df, t="date", y="count", B=28, g=2, w1=0.4, w2=0.9): Defaults to 0.9 to match NSSP-ESSENCE implementation and approximate the C2 algorithm. :returns: A dataframe containing the results of the analysis. :examples: + >>> import pandas as pd >>> import numpy as np >>> from pynssp.detectors.switch import * diff --git a/pynssp/detectors/trend.py b/pynssp/detectors/trend.py index 65565ff..e69de29 100644 --- a/pynssp/detectors/trend.py +++ b/pynssp/detectors/trend.py @@ -1,45 +0,0 @@ -import pandas as pd -import numpy as np -import statsmodels.api as sm - -def classify_trend(df, t='date', data_count='dataCount', all_count='allCount', B=12): - t = df[t] - data_count = df[data_count] - all_count = df[all_count] - - trend_analysis = pd.DataFrame({'t': t, 'data_count': data_count, 'all_count': all_count})\ - .rolling(window=B)\ - .apply(lambda x: np.nan \ - if np.sum(x['data_count']) <= 10 \ - else sm.GLM(np.column_stack([x['data_count'], x['all_count'] - x['data_count']]), - sm.add_constant(x['t']), - family=sm.families.Binomial()).fit().params[1], - raw=True - ) - - p_value = pd.DataFrame({'t': t, 'data_count': data_count, 'all_count': all_count})\ - .rolling(window=B)\ - .apply(lambda x: np.nan \ - if np.sum(x['data_count']) <= 10 \ - else sm.GLM(np.column_stack([x['data_count'], x['all_count'] - x['data_count']]), - sm.add_constant(x['t']), - family=sm.families.Binomial()).fit().pvalues[1], - raw=True - ) - - trend_classification = np.select( - condlist=[p_value < 0.01, p_value < 0.01, np.isnan(p_value)], - choicelist=['Significant Increase', 'Significant Decrease', 'Insufficient Data'], - default='Stable' - ) - - trend_classification = pd.Categorical( - trend_classification, - categories=['Significant Increase', 'Significant Decrease', 'Stable', 'Insufficient Data'] - ) - - return pd.DataFrame({ - 'trend_analysis': trend_analysis, - 'p.value': p_value, - 'trend_classification': trend_classification - }) diff --git a/pynssp/utils.py b/pynssp/utils.py index a81dc6f..0a01340 100644 --- a/pynssp/utils.py +++ b/pynssp/utils.py @@ -14,6 +14,7 @@ def change_dates(url, start_date=None, end_date=None): :param end_date: str): A new end date to replace the existing end date in the URL. (Default value = None) :returns: The modified URL with the new start and end dates. :examples: + >>> from pynssp.utils import * >>> url = "https://example.com/data?startDate=01Jan2022&endDate=31Dec2022" >>> change_dates(url, start_date="01Jan2021", end_date="31Dec2021")