From b20bd1b55f6635db24de4114371cb4e9d00b9ec3 Mon Sep 17 00:00:00 2001 From: Mandeep Singh Date: Thu, 15 Apr 2021 11:21:41 -0400 Subject: [PATCH 1/2] Update forecast.py Here I applied Creational Design Builder pattern- Methods are moved to classes with their implementations --- src/covidify/forecast.py | 53 ++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/src/covidify/forecast.py b/src/covidify/forecast.py index e96ad00..ff7a25c 100644 --- a/src/covidify/forecast.py +++ b/src/covidify/forecast.py @@ -26,6 +26,8 @@ from datetime import datetime, date, time from sklearn.metrics import mean_squared_error from covidify.config import PERC_SPLIT, FIG_SIZE +from abc import ABCMeta, abstractclassmethod + font = {'weight' : 'bold', 'size' : 22} @@ -61,7 +63,35 @@ os.system('mkdir -p ' + image_dir) -def plot_forecast(tmp_df, train, index_forecast, forecast, confint): + + + +class Createbuilder(metaclass = ABCMeta): + @staticmethod + def plot_forecast(): + #plot - forecast into file + + @staticmethod + def forecast(): + #saving data + + + + + +if __name__ == '__main__': + print('Training forecasting model...') + + train = trend_df[trend_df.date.isin(train_period)].cumulative_cases + index_forecast = [x for x in range(train.index[-1]+1, train.index[-1] + days_in_future+1)] + forecast(trend_df, train, index_forecast, days_in_future) + + +class ForcastBuilder(Createbuilder): + def __init__(self): + self.result = Result() + + def plot_forecast(tmp_df, train, index_forecast, forecast, confint): ''' Plot the values of train and test, the predictions from ARIMA and the shadowing for the confidence interval. @@ -89,7 +119,7 @@ def plot_forecast(tmp_df, train, index_forecast, forecast, confint): fig.savefig(os.path.join(image_dir, 'cumulative_forecasts.png')) -def forecast(tmp_df, train, index_forecast, days_in_future): + def forecast(tmp_df, train, index_forecast, days_in_future): # Fit model with training data model = auto_arima(train, trace=False, error_action='ignore', suppress_warnings=True) @@ -108,9 +138,18 @@ def forecast(tmp_df, train, index_forecast, days_in_future): plot_forecast(forecast_df, train, index_forecast, forecast, confint) -if __name__ == '__main__': - print('Training forecasting model...') - train = trend_df[trend_df.date.isin(train_period)].cumulative_cases - index_forecast = [x for x in range(train.index[-1]+1, train.index[-1] + days_in_future+1)] - forecast(trend_df, train, index_forecast, days_in_future) + +class Result(): + def __init__(self): + #this will reset + self.forecast = pd.DataFrame() + + +class Main(): + @staticmethod + def construct(); + return ForcastBuilder(). + /forecast(pd.DataFrame(),train, index_forecast, days_in_future) + +Result = Main.construct(): \ No newline at end of file From d509da959ae7f71c1fa5d64cf68ddb0e2f53eccb Mon Sep 17 00:00:00 2001 From: Mandeep Singh Date: Thu, 15 Apr 2021 13:18:16 -0400 Subject: [PATCH 2/2] Applying structural pattern to data_prep.py introduce lazy initialization and caching to wait set wait time --- src/covidify/forecast.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/covidify/forecast.py b/src/covidify/forecast.py index ff7a25c..5ed5c4a 100644 --- a/src/covidify/forecast.py +++ b/src/covidify/forecast.py @@ -86,6 +86,10 @@ def forecast(): index_forecast = [x for x in range(train.index[-1]+1, train.index[-1] + days_in_future+1)] forecast(trend_df, train, index_forecast, days_in_future) +# Using the Builder pattern makes sense only when your products +# are quite complex and require extensive configuration. The +# following two products are related, although they don't have +# a common interface. class ForcastBuilder(Createbuilder): def __init__(self):