Skip to content

Commit

Permalink
Update docstring example code
Browse files Browse the repository at this point in the history
  • Loading branch information
rosericazondekon committed May 2, 2023
1 parent 4cdd6e4 commit 6f5eeed
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 51 deletions.
12 changes: 6 additions & 6 deletions pynssp/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down
1 change: 1 addition & 0 deletions pynssp/detectors/ewma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
1 change: 1 addition & 0 deletions pynssp/detectors/nbinom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
1 change: 1 addition & 0 deletions pynssp/detectors/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
1 change: 1 addition & 0 deletions pynssp/detectors/serfling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
1 change: 1 addition & 0 deletions pynssp/detectors/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
45 changes: 0 additions & 45 deletions pynssp/detectors/trend.py
Original file line number Diff line number Diff line change
@@ -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
})
1 change: 1 addition & 0 deletions pynssp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 6f5eeed

Please sign in to comment.