From 76cfe15102ca5060d845c5e3afaafe3df39d92ac Mon Sep 17 00:00:00 2001 From: didier Date: Sat, 6 Mar 2021 14:58:37 +0000 Subject: [PATCH] Improve pred files, and non-blocking plot --- .../prediction_techniques/arima.py | 16 +++++---- .../prediction_techniques/fbprophet.py | 14 ++++---- .../prediction_techniques/knn.py | 16 +++++---- .../prediction_techniques/neural_networks.py | 34 ++++++++----------- .../prediction_techniques/pred_menu.py | 6 +++- .../prediction_techniques/regression.py | 15 ++++---- .../prediction_techniques/sma.py | 14 ++++---- 7 files changed, 60 insertions(+), 55 deletions(-) diff --git a/gamestonk_terminal/prediction_techniques/arima.py b/gamestonk_terminal/prediction_techniques/arima.py index a66cbf847ae..6ce2736423a 100644 --- a/gamestonk_terminal/prediction_techniques/arima.py +++ b/gamestonk_terminal/prediction_techniques/arima.py @@ -4,11 +4,15 @@ import pandas as pd from pandas.plotting import register_matplotlib_converters import pmdarima from statsmodels.tsa.arima.model import ARIMA -from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days +from gamestonk_terminal.helper_funcs import ( + check_positive, + get_next_stock_market_days, + parse_known_args_and_warn, +) + register_matplotlib_converters() -# -------------------------------------------------- ARIMA -------------------------------------------------- # pylint: disable=unused-argument def arima(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( @@ -71,11 +75,7 @@ def arima(l_args, s_ticker, s_interval, df_stock): ) try: - (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) - - if l_unknown_args: - print(f"The following args couldn't be interpreted: {l_unknown_args}\n") - return + ns_parser = parse_known_args_and_warn(parser, l_args) # Machine Learning model if ns_parser.s_order: @@ -115,6 +115,7 @@ def arima(l_args, s_ticker, s_interval, df_stock): print("") # Plotting + plt.figure() plt.plot(df_stock.index, df_stock["5. adjusted close"], lw=2) if ns_parser.s_order: plt.title( @@ -147,6 +148,7 @@ def arima(l_args, s_ticker, s_interval, df_stock): plt.vlines( df_stock.index[-1], ymin, ymax, linewidth=1, linestyle="--", color="k" ) + plt.ion() plt.show() # Print prediction data diff --git a/gamestonk_terminal/prediction_techniques/fbprophet.py b/gamestonk_terminal/prediction_techniques/fbprophet.py index c92ae42f704..dee891ae9b8 100644 --- a/gamestonk_terminal/prediction_techniques/fbprophet.py +++ b/gamestonk_terminal/prediction_techniques/fbprophet.py @@ -4,13 +4,16 @@ import matplotlib.pyplot as plt import pandas as pd from pandas.plotting import register_matplotlib_converters from fbprophet import Prophet -from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days +from gamestonk_terminal.helper_funcs import ( + check_positive, + get_next_stock_market_days, + parse_known_args_and_warn, +) register_matplotlib_converters() warnings.simplefilter("ignore") -# ----------------------------------------------------- FBPROPHET ----------------------------------------------------- # pylint: disable=unused-argument def fbprophet(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( @@ -33,11 +36,7 @@ def fbprophet(l_args, s_ticker, s_interval, df_stock): ) try: - (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) - - if l_unknown_args: - print(f"The following args couldn't be interpreted: {l_unknown_args}\n") - return + ns_parser = parse_known_args_and_warn(parser, l_args) df_stock = df_stock.sort_index(ascending=True) df_stock.reset_index(level=0, inplace=True) @@ -77,6 +76,7 @@ def fbprophet(l_args, s_ticker, s_interval, df_stock): df_stock["ds"].values[0], get_next_stock_market_days(l_pred_days[-1], 1)[-1] ) plt.title(f"Fb Prophet on {s_ticker} - {ns_parser.n_days} days prediction") + plt.ion() plt.show() print("") diff --git a/gamestonk_terminal/prediction_techniques/knn.py b/gamestonk_terminal/prediction_techniques/knn.py index e1ae266a72a..8ea9a579158 100644 --- a/gamestonk_terminal/prediction_techniques/knn.py +++ b/gamestonk_terminal/prediction_techniques/knn.py @@ -4,11 +4,15 @@ import pandas as pd from pandas.plotting import register_matplotlib_converters from sklearn import neighbors from TimeSeriesCrossValidation import splitTrain -from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days +from gamestonk_terminal.helper_funcs import ( + check_positive, + get_next_stock_market_days, + parse_known_args_and_warn, +) register_matplotlib_converters() -# ----------------------------------------------------- kNN ----------------------------------------------------- + # pylint: disable=unused-argument def k_nearest_neighbors(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( @@ -58,11 +62,7 @@ def k_nearest_neighbors(l_args, s_ticker, s_interval, df_stock): ) try: - (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) - - if l_unknown_args: - print(f"The following args couldn't be interpreted: {l_unknown_args}\n") - return + ns_parser = parse_known_args_and_warn(parser, l_args) # Split training data stock_x, stock_y = splitTrain.split_train( @@ -87,6 +87,7 @@ def k_nearest_neighbors(l_args, s_ticker, s_interval, df_stock): df_pred = pd.Series(l_predictions, index=l_pred_days, name="Price") # Plotting + plt.figure() plt.plot(df_stock.index, df_stock["5. adjusted close"], lw=2) plt.title( f"{ns_parser.n_neighbors}-Nearest Neighbors on {s_ticker} - {ns_parser.n_days} days prediction" @@ -114,6 +115,7 @@ def k_nearest_neighbors(l_args, s_ticker, s_interval, df_stock): plt.vlines( df_stock.index[-1], ymin, ymax, linewidth=1, linestyle="--", color="k" ) + plt.ion() plt.show() # Print prediction data diff --git a/gamestonk_terminal/prediction_techniques/neural_networks.py b/gamestonk_terminal/prediction_techniques/neural_networks.py index 49b79bbb668..58e22f4df8e 100644 --- a/gamestonk_terminal/prediction_techniques/neural_networks.py +++ b/gamestonk_terminal/prediction_techniques/neural_networks.py @@ -18,7 +18,12 @@ from tensorflow.keras.layers import ( # TimeDistributed, ) -from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days +from gamestonk_terminal.helper_funcs import ( + check_positive, + get_next_stock_market_days, + parse_known_args_and_warn, +) + from gamestonk_terminal import config_neural_network_models as cfg_nn_models @@ -77,7 +82,6 @@ def build_neural_network_model(Recurrent_Neural_Network, n_inputs, n_days): return model -# -------------------------------------------------- MLP -------------------------------------------------- # pylint: disable=unused-argument def mlp(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( @@ -159,11 +163,7 @@ def mlp(l_args, s_ticker, s_interval, df_stock): ) try: - (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) - - if l_unknown_args: - print(f"The following args couldn't be interpreted: {l_unknown_args}\n") - return + ns_parser = parse_known_args_and_warn(parser, l_args) # Pre-process data if ns_parser.s_preprocessing == "standardization": @@ -227,6 +227,7 @@ def mlp(l_args, s_ticker, s_interval, df_stock): df_pred = pd.Series(y_pred_test_t[0].tolist(), index=l_pred_days, name="Price") # Plotting + plt.figure() plt.plot(df_stock.index, df_stock["5. adjusted close"], lw=3) plt.title(f"MLP on {s_ticker} - {ns_parser.n_days} days prediction") plt.xlim( @@ -258,6 +259,7 @@ def mlp(l_args, s_ticker, s_interval, df_stock): linestyle="--", color="k", ) + plt.ion() plt.show() # Print prediction data @@ -271,7 +273,6 @@ def mlp(l_args, s_ticker, s_interval, df_stock): print("") -# -------------------------------------------------- RNN -------------------------------------------------- # pylint: disable=unused-argument def rnn(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( @@ -353,11 +354,7 @@ def rnn(l_args, s_ticker, s_interval, df_stock): ) try: - (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) - - if l_unknown_args: - print(f"The following args couldn't be interpreted: {l_unknown_args}\n") - return + ns_parser = parse_known_args_and_warn(parser, l_args) # Pre-process data if ns_parser.s_preprocessing == "standardization": @@ -421,6 +418,7 @@ def rnn(l_args, s_ticker, s_interval, df_stock): df_pred = pd.Series(y_pred_test_t[0].tolist(), index=l_pred_days, name="Price") # Plotting + plt.figure() plt.plot(df_stock.index, df_stock["5. adjusted close"], lw=3) plt.title(f"RNN on {s_ticker} - {ns_parser.n_days} days prediction") plt.xlim( @@ -452,6 +450,7 @@ def rnn(l_args, s_ticker, s_interval, df_stock): linestyle="--", color="k", ) + plt.ion() plt.show() # Print prediction data @@ -465,7 +464,6 @@ def rnn(l_args, s_ticker, s_interval, df_stock): print("") -# -------------------------------------------------- LSTM -------------------------------------------------- # pylint: disable=unused-argument def lstm(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( @@ -547,11 +545,7 @@ def lstm(l_args, s_ticker, s_interval, df_stock): ) try: - (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) - - if l_unknown_args: - print(f"The following args couldn't be interpreted: {l_unknown_args}\n") - return + ns_parser = parse_known_args_and_warn(parser, l_args) # Pre-process data if ns_parser.s_preprocessing == "standardization": @@ -615,6 +609,7 @@ def lstm(l_args, s_ticker, s_interval, df_stock): df_pred = pd.Series(y_pred_test_t[0].tolist(), index=l_pred_days, name="Price") # Plotting + plt.figure() plt.plot(df_stock.index, df_stock["5. adjusted close"], lw=3) plt.title(f"LSTM on {s_ticker} - {ns_parser.n_days} days prediction") plt.xlim( @@ -646,6 +641,7 @@ def lstm(l_args, s_ticker, s_interval, df_stock): linestyle="--", color="k", ) + plt.ion() plt.show() # Print prediction data diff --git a/gamestonk_terminal/prediction_techniques/pred_menu.py b/gamestonk_terminal/prediction_techniques/pred_menu.py index c8d3ddb34c6..4a1918e439b 100644 --- a/gamestonk_terminal/prediction_techniques/pred_menu.py +++ b/gamestonk_terminal/prediction_techniques/pred_menu.py @@ -6,8 +6,9 @@ from gamestonk_terminal.prediction_techniques import regression from gamestonk_terminal.prediction_techniques import arima from gamestonk_terminal.prediction_techniques import fbprophet from gamestonk_terminal.prediction_techniques import neural_networks +import matplotlib.pyplot as plt + -# ----------------------------------------------------------------------------------------------------------------------- def print_prediction(s_ticker, s_start, s_interval): """ Print help """ @@ -69,6 +70,9 @@ def pred_menu(df_stock, s_ticker, s_start, s_interval): # Get input command from user as_input = input("> ") + # Images are non blocking - allows to close them if we type other command + plt.close() + # Parse prediction techniques command of the list of possible commands try: (ns_known_args, l_args) = pred_parser.parse_known_args(as_input.split()) diff --git a/gamestonk_terminal/prediction_techniques/regression.py b/gamestonk_terminal/prediction_techniques/regression.py index 8fa1cfb4d94..2af7272173c 100644 --- a/gamestonk_terminal/prediction_techniques/regression.py +++ b/gamestonk_terminal/prediction_techniques/regression.py @@ -7,7 +7,11 @@ from sklearn import linear_model from sklearn import pipeline from sklearn import preprocessing -from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days +from gamestonk_terminal.helper_funcs import ( + check_positive, + get_next_stock_market_days, + parse_known_args_and_warn, +) register_matplotlib_converters() @@ -16,7 +20,6 @@ LINEAR = 1 QUADRATIC = 2 CUBIC = 3 -# -------------------------------------------------- REGRESSION -------------------------------------------------- # pylint: disable=unused-argument def regression(l_args, s_ticker, s_interval, df_stock, polynomial): parser = argparse.ArgumentParser( @@ -69,11 +72,7 @@ def regression(l_args, s_ticker, s_interval, df_stock, polynomial): ) try: - (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) - - if l_unknown_args: - print(f"The following args couldn't be interpreted: {l_unknown_args}\n") - return + ns_parser = parse_known_args_and_warn(parser, l_args) # Split training data stock_x, stock_y = splitTrain.split_train( @@ -106,6 +105,7 @@ def regression(l_args, s_ticker, s_interval, df_stock, polynomial): df_pred = pd.Series(l_predictions, index=l_pred_days, name="Price") # Plotting + plt.figure() plt.plot(df_stock.index, df_stock["5. adjusted close"], lw=2) plt.title( f"Regression (polynomial {polynomial}) on {s_ticker} - {ns_parser.n_days} days prediction" @@ -133,6 +133,7 @@ def regression(l_args, s_ticker, s_interval, df_stock, polynomial): plt.vlines( df_stock.index[-1], ymin, ymax, linewidth=1, linestyle="--", color="k" ) + plt.ion() plt.show() # Print prediction data diff --git a/gamestonk_terminal/prediction_techniques/sma.py b/gamestonk_terminal/prediction_techniques/sma.py index 834c61ce7cb..ad8fae16212 100644 --- a/gamestonk_terminal/prediction_techniques/sma.py +++ b/gamestonk_terminal/prediction_techniques/sma.py @@ -4,11 +4,14 @@ import matplotlib.pyplot as plt import pandas as pd from pandas.plotting import register_matplotlib_converters -from gamestonk_terminal.helper_funcs import check_positive, get_next_stock_market_days +from gamestonk_terminal.helper_funcs import ( + check_positive, + get_next_stock_market_days, + parse_known_args_and_warn, +) register_matplotlib_converters() -# ----------------------------------------------------- SMA ----------------------------------------------------- # pylint: disable=unused-argument def simple_moving_average(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( @@ -44,11 +47,7 @@ def simple_moving_average(l_args, s_ticker, s_interval, df_stock): ) try: - (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) - - if l_unknown_args: - print(f"The following args couldn't be interpreted: {l_unknown_args}\n") - return + ns_parser = parse_known_args_and_warn(parser, l_args) # Prediction data l_predictions = list() @@ -97,6 +96,7 @@ def simple_moving_average(l_args, s_ticker, s_interval, df_stock): plt.vlines( df_stock.index[-1], ymin, ymax, linewidth=1, linestyle="--", color="k" ) + plt.ion() plt.show() # Print prediction data