Files
OpenBB/openbb_terminal/forecast/forecast_view.py
Jeroen Bouma 0ee2ebcf1e Refractor stocks/fa by incorporating stocks/dd and making the menu less reliant on Yahoo Finance (#4055)
* I missed some sheet_name ones :)

* Move quote to main menu and combine yahoo finance commands

* Update stocks for quote

* First update to move yfinance away from main functionality

* Small updates

* Update tests to reflect changes

* Fix some little things

* Update docs a little too

* Remove the Due Diligence menu

* Update tests for new change

* Update fa and stocks with new text and move some stuff around

* Bye bye dd

* Pls work

* work..

* work..

* work..

* Work..|

* Add tests back in

* Remove tests

* Fix fix fix

* Remove tests to fix conflicts

* Improve the `stocks/search` command (#4084)

* Improve search command

* Make it more intuitive

* Fix tests

* Update the docs

* Ok done

* Ok done 2

* Ok done 3

* Git update tests that failed

* Update commands with --sheet-name that didn't work (#4088)

* Update commands with --sheet-name that didn't work

* Fix test

* oops

* Adds auto completion to `news` (#4089)

* Fix plot look when using eval and add query to choices (#3881)

* convert index to datetime and update choices

* uncomment economy integration test

* fix treasury concat bug

* fix datasets concat on duplciates

* Lock ruff version so that new lints dont break our CI (#3905)

* Lock ruff version so that new lints dont break our CI

* Bumped pre-commit ruff version

* returning always a dataframe

* Revert "returning always a dataframe"

This reverts commit d5dbf3132f.

* quick fix to auto complete args

---------

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>

* Hotfix/Windows-CLI (#4087)

* init

* Update windows10_build.yml

* Update windows10_build.yml

* Update windows10_build.yml

* Update windows10_build.yml

* Update windows10_build.yml

* Update windows10_build.yml

* oops

---------

Co-authored-by: James Maslek <jmaslek11@gmail.com>

* Adds end date to `forex/load` (#4077)

* Fix plot look when using eval and add query to choices (#3881)

* convert index to datetime and update choices

* uncomment economy integration test

* fix treasury concat bug

* fix datasets concat on duplciates

* Lock ruff version so that new lints dont break our CI (#3905)

* Lock ruff version so that new lints dont break our CI

* Bumped pre-commit ruff version

* returning always a dataframe

* Revert "returning always a dataframe"

This reverts commit d5dbf3132f.

* adding end date to the forex load

* added some tests

* added example

* always show msg

* rewriting test

* updating tests

* updating tests

* Redo load tests

---------

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>

* Update tests to reflect changes

* work..

* Remove tests

* Fix fix fix

* Remove tests

* Update the whole test folder

* Small updates

* Revert change

* Update eodhd to reflect it requires a paid plan and update tests

* Fix all the tests

* Update tests

* Update integration tests

* Removing tests so no whining happens

* ADD ALL THE TESTS

* Fix linting

* gitignore.  thanks jer

* Add back dd and redirect to fa

* Update to just display a message

* Update OpenBB SDK commands

* Fixing issues with exporting

* Fix ruff messing up

* Fix tests

* Fix the tests by disabling isort to run on tests folder

---------

Co-authored-by: Jeroen Bouma <jeroenbouma@Jeroens-MacBook-Pro.local>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
Co-authored-by: Henrique Joaquim <h.joaquim@campus.fct.unl.pt>
Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
Co-authored-by: teh_coderer <me@tehcoderer.com>
2023-02-06 23:24:15 +01:00

309 lines
8.0 KiB
Python

"""Forecast View"""
__docformat__ = "numpy"
import logging
import os
from typing import Dict, List, Optional
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from darts.utils.statistics import plot_acf
from pandas.plotting import register_matplotlib_converters
from openbb_terminal.config_plot import PLOT_DPI
from openbb_terminal.config_terminal import theme
from openbb_terminal.decorators import log_start_end
from openbb_terminal.forecast import forecast_model, helpers
from openbb_terminal.helper_funcs import export_data, plot_autoscale, print_rich_table
from openbb_terminal.rich_config import console
logger = logging.getLogger(__name__)
register_matplotlib_converters()
@log_start_end(log=logger)
def show_options(
datasets: Dict[str, pd.DataFrame],
dataset_name: str = None,
export: str = "",
sheet_name: str = None,
):
"""Plot custom data
Parameters
----------
datasets: dict
The loaded in datasets
dataset_name: str
The name of the dataset you wish to show options for
sheet_name: str
Optionally specify the name of the sheet the data is exported to.
export: str
Format to export image
"""
if not datasets:
console.print(
"Please load in a dataset by using the 'load' command before using this feature."
)
else:
option_tables = forecast_model.get_options(datasets, dataset_name)
for dataset, data_values in option_tables.items():
print_rich_table(
data_values,
headers=list(data_values.columns),
show_index=False,
title=f"Options for dataset: '{dataset}'",
)
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
f"{dataset}_options",
data_values.set_index("column"),
sheet_name,
)
@log_start_end(log=logger)
def display_plot(
data: pd.DataFrame,
columns: List[str],
export: str = "",
sheet_name: str = None,
external_axes: Optional[List[plt.axes]] = None,
):
"""Plot data from a dataset
Parameters
----------
data: pd.DataFrame
The dataframe to plot
columns: List[str]
The columns to show
sheet_name: str
Optionally specify the name of the sheet the data is exported to.
export: str
Format to export image
external_axes:Optional[List[plt.axes]]
External axes to plot on
"""
# Check that there's at least a valid dataframe
if external_axes is None:
_, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
else:
ax = external_axes[0]
# Only do if data is not plotted, otherwise an error will occur
if "date" in data.columns and "date" not in columns:
data = data.set_index("date")
for column in columns:
ax.plot(data[column], label=column)
theme.style_primary_axis(ax)
if external_axes is None:
theme.visualize_output()
ax.legend()
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
"plot",
sheet_name,
)
@log_start_end(log=logger)
def display_seasonality(
data: pd.DataFrame,
column: str = "close",
export: str = "",
sheet_name: str = None,
m: Optional[int] = None,
max_lag: int = 24,
alpha: float = 0.05,
external_axes: Optional[List[plt.axes]] = None,
):
"""Plot seasonality from a dataset
Parameters
----------
data: pd.DataFrame
The dataframe to plot
column: str
The column of the dataframe to analyze
sheet_name: str
Optionally specify the name of the sheet the data is exported to.
export: str
Format to export image
m: Optional[int]
Optionally, a time lag to highlight on the plot. Default is none.
max_lag: int
The maximal lag order to consider. Default is 24.
alpha: float
The confidence interval to display. Default is 0.05.
external_axes:Optional[List[plt.axes]]
External axes to plot on
"""
if not data.empty:
_, series = helpers.get_series(data, column)
if external_axes is None:
_, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
else:
ax = external_axes[0]
# TODO: Add darts check_seasonality here
plot_acf(
series, m=m, max_lag=max_lag, alpha=alpha, axis=ax, default_formatting=False
)
theme.style_primary_axis(ax)
if external_axes is None:
theme.visualize_output()
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
"plot",
sheet_name,
)
@log_start_end(log=logger)
def display_corr(
dataset: pd.DataFrame,
export: str = "",
sheet_name: str = None,
external_axes: Optional[List[plt.axes]] = None,
):
"""Plot correlation coefficients for dataset features
Parameters
----------
dataset : pd.DataFrame
The dataset fore calculating correlation coefficients
sheet_name: str
Optionally specify the name of the sheet the data is exported to.
export: str
Format to export image
external_axes:Optional[List[plt.axes]]
External axes to plot on
"""
if external_axes is None:
_, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
else:
ax = external_axes[0]
# correlation
correlation = forecast_model.corr_df(dataset)
sns.heatmap(
correlation,
vmin=correlation.values.min(),
vmax=1,
square=True,
linewidths=0.1,
annot=True,
annot_kws={"size": 8},
cbar_kws=dict(use_gridspec=True, location="left"),
)
ax.set_title("Correlation Matrix")
for t in ax.get_yticklabels():
t.set_fontsize(7)
t.set_fontweight("bold")
t.set_horizontalalignment("left")
for t in ax.get_xticklabels():
t.set_fontsize(7)
t.set_fontweight("bold")
t.set_rotation(60)
t.set_horizontalalignment("right")
theme.style_primary_axis(ax)
if external_axes is None:
theme.visualize_output()
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
"plot",
sheet_name,
)
@log_start_end(log=logger)
def show_df(
data: pd.DataFrame,
limit: int = 15,
limit_col: int = 10,
name: str = "",
export: str = "",
sheet_name: str = None,
):
console.print(
f"[green]{name} dataset has shape (row, column): {data.shape}\n[/green]"
)
if len(data.columns) > limit_col:
console.print(
f"[red]Dataframe has more than {limit_col} columns."
" If you have extra screen space, consider increasing the `limit_col`,"
" else export to see all of the data.[/red]\n"
)
data = data.iloc[:, :limit_col]
print_rich_table(
data.head(limit),
headers=list(data.columns),
show_index=True,
title=f"Dataset {name} | Showing {limit} of {len(data)} rows",
)
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
f"{name}_show",
data,
sheet_name,
)
@log_start_end(log=logger)
def describe_df(
data: pd.DataFrame, name: str = "", export: str = "", sheet_name: str = None
):
new_df = forecast_model.describe_df(data)
print_rich_table(
new_df,
headers=list(data.describe().columns),
show_index=True,
title=f"Showing Descriptive Statistics for Dataset {name}",
)
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
f"{name}_show",
sheet_name,
)
@log_start_end(log=logger)
def export_df(
data: pd.DataFrame, export: str, name: str = "", sheet_name: str = None
) -> None:
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
f"{name}_show",
data,
sheet_name,
)