mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-28 21:12:16 +00:00
* Gave forecasting memory * Fixed scripts, refactored * FIxed poetry lock * edge case check for forecast target * Improved combine and load functionality * Cleaned up translations * Fixed issue with covariates * Fixed issue checking covariates * Another covariates check fix * Ignored regr and linregr warnings * Fixed covariate issues * switched from forecasting to forecast * Finished transition to forecast * Can add entire dataset with one command * Improved combine description * Removed naming covariates * Created new installation * typo * Make plot show dates if available * Added better handling or users without the menu * Removed unused file * Fix * Better handling for nontraditional datasets * Fixed black and pylint * Fixed tests * Added darts install to main tests * Working on darts with CI * Added back test file * Made large tables print better * naive baseline * typo * Finished naive * no dollar on prediction * fixed positive MAPE bug * quick refactoring * Fixed two different args for same thing * added extra patience * linreg mape fix * info fix * Refactored API, bumped to Darts 0.21.0 * Added fixes * Increased verbosity for wrong column * Updated dependencies * Hid warnings * Fixed importing * Fixed tests * Fixed ugly seasonal plotting * Fixed forecast line color * Switched chart output to blue * Simplified lambda_price_prediction_color * fixed residuals * Chnage * Removed darts from CI per Chavi * Added fixes to tests * Added knnfix * Fixed issue where n!= o * Added changes * Added changes * Imrpoved forecast dash * Added Theo notebook * Added enhancements to dash * Added notebook * Added fix for jupyter lab * Added debug stuff * Change * Updated docs * Fixed formatting * Fixed formatting * Removed prints * Filtered some info * Added button to run model * Improved api * Added secret feautr (no peeking Martin) * Cleaned code * Fixed tests * Added test fixes * Added fixes * Fixes * FIxes for pres * Remove bad tests * Removed knn * Fixed issues with removing mc * doc for conda * Added forecast improvements * Added streamlit support * Fixed issues * fix expo with streamlit due to quantile() * fixed performance issues with streamlit for now.. * clean up historical forecast with new trainer * quick fix for regression trainer params * Added fixes * quick fix for other fix for regression trainer params * table formatting for timestamp * potential fix for inf in feature engineered datasets * Basic working in new format * dw * Trying * Fixed issues * Improved graphing * fixing trainer for LR and formatting * doge and linting * page break * automatic cleaning of datasets * automatic cleaning of datasets- fix * Fixed forecast dates * Made dashboard prettier * Added fixes * Added fixes * Added options * Fixed error * remove caching * adding in spinner * Added vairable n_predict in streamlit * Added mypy fix * renaming and range change * new index for n predict * check positive float for window size * Update _index.md * Update _index.md * Update _index.md * Update _index.md * Update _index.md * Update _index.md * Update _index.md * Update _index.md * Update _index.md * renaming * reorg files * Update _index.md * hidden which command for versions * Update _index.md * Update _index.md * which: ns parser * hugo for: which * hugo for: forecasting fix * formatting black * update stock controller test * Lay groundwork for better residual plotting * improved delete to allow for periods in title * improved automatic cleaning of inf * Added new API * Added new API * Added new API * formatting for black * Updated our testing CI * Reverted changes * Added forecast docs * Fixed mypy issues * Fixes tests * Did some refactoring, added a report * new api in streamlit * Added integrated tests * Update _index.md * improved loading in custom dataset * menu spacing * installer fixes * Added docs fixes * Adding comments to test if commit working * Fixed report * naming conventions * formatting * removing unused var * Made last report imporvements * Update README.md * Added fix * Switched to warning * Added fixes * Added fixes * Added fixes * Added fixes * Update economy av view test * Remove forgotten print statement * Update depencencies * Added verbosity to pytest * Added fixes * Fixed pylint * Fixed actions checkout * Added fixes Co-authored-by: colin99d <colin99delahunty@gmail.com> Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com> Co-authored-by: James Simmons <simmonsj330@gmail.com> Co-authored-by: minhhoang1023 <40023817+minhhoang1023@users.noreply.github.com> Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com>
427 lines
12 KiB
Python
427 lines
12 KiB
Python
from typing import List
|
|
import argparse
|
|
import pandas as pd
|
|
import pytest
|
|
from prompt_toolkit.completion import NestedCompleter
|
|
|
|
try:
|
|
from openbb_terminal.forecast import forecast_controller as fc
|
|
except ImportError:
|
|
pytest.skip(allow_module_level=True)
|
|
|
|
# pylint: disable=E1121
|
|
base = "openbb_terminal.forecast.forecast_controller."
|
|
df = pd.DataFrame([[1, 2, 3], [2, 3, 4], [3, 4, 5]], columns=["first", "date", "close"])
|
|
|
|
|
|
class Thing:
|
|
help = True
|
|
|
|
def format_help(self):
|
|
return "You need help"
|
|
|
|
|
|
def mock_func(_):
|
|
return (Thing(), 2)
|
|
|
|
|
|
def test_check_greater_than_one():
|
|
assert fc.check_greater_than_one(2) == 2
|
|
|
|
|
|
def test_check_greater_than_one_invalid():
|
|
with pytest.raises(argparse.ArgumentTypeError):
|
|
fc.check_greater_than_one(-2)
|
|
|
|
|
|
def test_forecast_controller(mocker):
|
|
mocker.patch(base + "session", True)
|
|
mocker.patch(base + "obbff.USE_PROMPT_TOOLKIT", True)
|
|
cont = fc.ForecastController(data=df, ticker="TSLA")
|
|
assert isinstance(cont.files, List)
|
|
|
|
|
|
def test_fc_update_runtime_choices(mocker):
|
|
mocker.patch(base + "session", True)
|
|
mocker.patch(base + "obbff.USE_PROMPT_TOOLKIT", True)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"stonks": df}
|
|
cont.update_runtime_choices()
|
|
assert isinstance(cont.completer, NestedCompleter)
|
|
|
|
|
|
def test_fc_refresh_datasets_on_menu():
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"stonks": df}
|
|
cont.files = ["file.csv", "fiile.csv"]
|
|
cont.refresh_datasets_on_menu()
|
|
assert "stonks.first" in cont.list_dataset_cols
|
|
|
|
|
|
def test_fc_print_help(capsys):
|
|
cont = fc.ForecastController()
|
|
cont.print_help()
|
|
captured = capsys.readouterr()
|
|
assert "forecast" in captured.out
|
|
|
|
|
|
def test_fc_custom_reset():
|
|
cont = fc.ForecastController()
|
|
val = cont.custom_reset()
|
|
assert not val
|
|
|
|
|
|
def test_fc_custom_reset_with_files():
|
|
cont = fc.ForecastController()
|
|
cont.files_full = [["file1", "file1"]]
|
|
val = cont.custom_reset()
|
|
assert val == ["forecast", "'load file1 -a file1'"]
|
|
|
|
|
|
def test_fc_parse_known_args_and_warn(mocker):
|
|
mock = mocker.Mock()
|
|
mock2 = mocker.Mock()
|
|
mock.parse_known_args = mock_func
|
|
mock.format_help = Thing().format_help
|
|
mock.add_argument = mock2
|
|
cont = fc.ForecastController()
|
|
cont.parse_known_args_and_warn(
|
|
parser=mock,
|
|
other_args=[],
|
|
export_allowed=0,
|
|
raw=True,
|
|
limit=1,
|
|
target_dataset=True,
|
|
target_column=True,
|
|
period=5,
|
|
n_days=True,
|
|
seasonal="weekly",
|
|
periods=True,
|
|
window=True,
|
|
train_split=True,
|
|
input_chunk_length=True,
|
|
output_chunk_length=True,
|
|
force_reset=True,
|
|
save_checkpoints=True,
|
|
model_save_name="the_models",
|
|
n_epochs=True,
|
|
model_type=True,
|
|
dropout=0.05,
|
|
batch_size=3,
|
|
learning_rate=True,
|
|
past_covariates=True,
|
|
lags=True,
|
|
hidden_size=4,
|
|
n_jumps=True,
|
|
end=True,
|
|
residuals=True,
|
|
forecast_only=True,
|
|
)
|
|
assert mock2.call_count == 28
|
|
|
|
|
|
def test_fc_call_load(mocker):
|
|
mocker.patch(base + "forecast_model.load", return_value=df)
|
|
cont = fc.ForecastController()
|
|
cont.call_load(["data.csv"])
|
|
|
|
|
|
def test_fc_call_load_alias():
|
|
cont = fc.ForecastController()
|
|
cont.files = ["the_data"]
|
|
cont.call_load(["data.csv", "-a", "the_data"])
|
|
|
|
|
|
def test_fc_call_load_alias_no_dot():
|
|
cont = fc.ForecastController()
|
|
cont.call_load(["datacsv", "-a", "the_data"])
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"name,loc_df", [("base", df), ("base", pd.DataFrame()), ("", df)]
|
|
)
|
|
def test_fc_call_show(mocker, name, loc_df):
|
|
mock = mocker.MagicMock()
|
|
mock.name = name
|
|
mock.limit = 4
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"base": loc_df}
|
|
cont.call_show(["data"])
|
|
|
|
|
|
@pytest.mark.parametrize("dataset", ["base", None])
|
|
def test_call_desc(mocker, dataset):
|
|
mock = mocker.MagicMock()
|
|
mock.target_dataset = dataset
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"base": df}
|
|
cont.call_desc(["data"])
|
|
|
|
|
|
def test_call_plot(mocker):
|
|
mock = mocker.MagicMock()
|
|
mock.values = ["data.first"]
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.choices = {"plot": ["data.first", "data.second", "data.third"]}
|
|
cont.datasets = {"data": df}
|
|
cont.call_plot(["data.first"])
|
|
|
|
|
|
def test_call_plot_no_parser(mocker):
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=None
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.choices = {"plot": ["data.first", "data.second", "data.third"]}
|
|
cont.datasets = {"data": df}
|
|
cont.call_plot(["data.first"])
|
|
|
|
|
|
def test_call_plot_no_values(mocker):
|
|
mock = mocker.MagicMock()
|
|
mock.values = None
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.choices = {"plot": ["data.first", "data.second", "data.third"]}
|
|
cont.datasets = {"data": df}
|
|
cont.call_plot(["data.first"])
|
|
|
|
|
|
def test_call_season(tsla_csv):
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": tsla_csv}
|
|
cont.call_season(["data.close", "--max_lag", "1"])
|
|
|
|
|
|
def test_call_season_error():
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.call_season(["datafirst", "--max_lag", "1"])
|
|
|
|
|
|
def test_call_season_no_parser(mocker):
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=None
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.call_season(["data.first", "--max_lag", "1"])
|
|
|
|
|
|
def test_call_season_no_values(mocker):
|
|
mock = mocker.MagicMock()
|
|
mock.values = None
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.call_season(["data.first", "--max_lag", "1"])
|
|
|
|
|
|
def test_call_corr():
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.call_corr(["data"])
|
|
|
|
|
|
def test_call_corr_target(mocker):
|
|
mock = mocker.MagicMock()
|
|
mock.target_dataset = None
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.call_corr(["data"])
|
|
|
|
|
|
def test_call_comb_not_in(mocker):
|
|
mock = mocker.MagicMock()
|
|
mock.values = ["data.first"]
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.choices = {
|
|
"combine": {"data.first": 1, "data.second": 2, "data.third": 3},
|
|
"delete": {"data.first": 1, "data.second": 2, "data.third": 3},
|
|
}
|
|
cont.call_combine(["data"])
|
|
|
|
|
|
def test_call_comb(mocker):
|
|
mock = mocker.MagicMock()
|
|
mock.dataset = "data"
|
|
mock.columns = ["data.first", "data.second"]
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.choices = {
|
|
"combine": {"data.first": 1, "data.second": 2, "data.third": 3},
|
|
"delete": {"data.first": 1, "data.second": 2, "data.third": 3},
|
|
}
|
|
cont.call_combine(["data"])
|
|
|
|
|
|
def test_call_clean():
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.call_clean(["data"])
|
|
|
|
|
|
def test_call_clean_bad(mocker):
|
|
mock = mocker.MagicMock()
|
|
mock.target_dataset = None
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.call_clean(["data"])
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"feature", ["ema", "sto", "rsi", "roc", "mom", "delta", "atr", "signal", "delete"]
|
|
)
|
|
def test_call_feat_eng_invalid(feature):
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.files = ["file.csv", "fiile.csv"]
|
|
cont.choices = {
|
|
"combine": {"data.first": 1, "data.second": 2, "data.third": 3},
|
|
"delete": {"data.first": 1, "data.second": 2, "data.third": 3},
|
|
}
|
|
the_list = ["data"]
|
|
if feature == "rsi":
|
|
the_list.append("--period")
|
|
the_list.append("2")
|
|
getattr(cont, f"call_{feature}")(the_list)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"feature",
|
|
["ema", "sto", "rsi", "roc", "mom", "delta", "atr", "signal", "delete", "export"],
|
|
)
|
|
def test_call_feat_eng_invalid_parser(feature, mocker):
|
|
mocker.patch(base + "helpers.check_parser_input", return_value=None)
|
|
mock = mocker.MagicMock()
|
|
mock.target_dataset = None
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.files = ["file.csv", "fiile.csv"]
|
|
cont.choices = {
|
|
"combine": {"data.first": 1, "data.second": 2, "data.third": 3},
|
|
"delete": {"data.first": 1, "data.second": 2, "data.third": 3},
|
|
}
|
|
the_list = ["data"]
|
|
if feature == "rsi":
|
|
the_list.append("--period")
|
|
the_list.append("2")
|
|
getattr(cont, f"call_{feature}")(the_list)
|
|
|
|
|
|
def test_call_ema(mocker):
|
|
mock = mocker.MagicMock()
|
|
mock.target_dataset = "data"
|
|
mock.columns = ["data.first", "data.second"]
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.call_ema(["data"])
|
|
|
|
|
|
@pytest.mark.parametrize("datasets", [[], ["data"], ["bad"]])
|
|
def test_call_delete(mocker, datasets):
|
|
cont = fc.ForecastController()
|
|
mock = mocker.MagicMock()
|
|
if datasets == ["bad"]:
|
|
mock.delete = ["data.lose"]
|
|
else:
|
|
mock.delete = ["data.close"]
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
if datasets == ["data"]:
|
|
cont.datasets = {"data": df}
|
|
cont.choices = {
|
|
"combine": ["data.first", "data.second", "data.third"],
|
|
"delete": ["data.first", "data.second", "data.third"],
|
|
}
|
|
cont.call_delete([])
|
|
|
|
|
|
def test_call_export(mocker):
|
|
mock = mocker.MagicMock()
|
|
mock.target_dataset = "data"
|
|
mock.columns = ["data.first", "data.second"]
|
|
mocker.patch(
|
|
base + "ForecastController.parse_known_args_and_warn", return_value=mock
|
|
)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
cont.call_export(["data"])
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"opt, func",
|
|
[
|
|
("expo", "expo_view.display_expo_forecast"),
|
|
("theta", "theta_view.display_theta_forecast"),
|
|
("rnn", "rnn_view.display_rnn_forecast"),
|
|
("nbeats", "nbeats_view.display_nbeats_forecast"),
|
|
("tcn", "tcn_view.display_tcn_forecast"),
|
|
("regr", "regr_view.display_regression"),
|
|
("linregr", "linregr_view.display_linear_regression"),
|
|
("brnn", "brnn_view.display_brnn_forecast"),
|
|
("trans", "trans_view.display_trans_forecast"),
|
|
("tft", "tft_view.display_tft_forecast"),
|
|
],
|
|
)
|
|
def test_models(mocker, opt, func):
|
|
mocker.patch(base + "helpers.check_parser_input", return_value=True)
|
|
mocker.patch(base + func)
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
getattr(cont, f"call_{opt}")(["data"])
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"opt",
|
|
[
|
|
"expo",
|
|
"theta",
|
|
"rnn",
|
|
"nbeats",
|
|
"tcn",
|
|
"regr",
|
|
"linregr",
|
|
"brnn",
|
|
"trans",
|
|
"tft",
|
|
],
|
|
)
|
|
def test_models_bad(opt):
|
|
cont = fc.ForecastController()
|
|
cont.datasets = {"data": df}
|
|
getattr(cont, f"call_{opt}")(["data"])
|