import streamlit as st import matplotlib.pyplot as plt from math import sqrt import numpy as np import pandas as pd class DataVisualizer: def __init__(self, datasets, load_options): self.datasets = datasets self.load_options = load_options self.plots = dict() self.tables = dict() def visualise_data(self): fig_main, ax_main = plt.subplots(1, 1, figsize=(15, 5)) if self.load_options["evaluate"]: tmp = self.datasets["train_df"].append(self.datasets["test_df"].iloc[0], ignore_index=True) ax_main.plot(tmp["date"], tmp["food_amount"]) ax_main.plot(self.datasets["test_df"]["date"], self.datasets["test_df"]["food_amount"]) else: ax_main.plot(self.datasets["food_df"]["date"], self.datasets["food_df"]["food_amount"]) self.plots["visualise_data"] = fig_main st.write(fig_main) def visualise_evaluation(self, results): tab1, tab2 = st.tabs(["Graph", "Table"]) with tab1: x = np.arange(len(results["evaluation_real"])) f, ax = plt.subplots(1) f.set_figheight(5) f.set_figwidth(15) ax.plot(x, results["evaluation_real"], marker='o', markersize=10, linestyle='', color='black', label='Actual data') ax.plot(x, results["evaluation_results"], marker='o', markersize=10, linestyle='', color='orange', label='Forecast') ax.grid() plt.xlabel('Time', fontsize=16) plt.ylabel(self.load_options["frequency"] + ' Demand', fontsize=16) ax.legend(loc='best') plt.suptitle(self.load_options["frequency"] + ' forecast', y=1) ax.set_xticks(x) ax.set_xticklabels(x.astype(int)) self.plots["evaluation_graph"] = f st.write(f) with tab2: test_dates = self.datasets["test_df"]["date"] evaluation_results = results["evaluation_results"] evaluation_table = pd.DataFrame({'Date': test_dates, 'Evaluation_Result': evaluation_results}) self.tables["evaluation_table"] = evaluation_table st.dataframe(evaluation_table, use_container_width=True) def visualise_forecast(self, results): tab1, tab2 = st.tabs(["Graph", "Table"]) forecast_results = results["forecast_results"] with tab1: x = np.arange(len(forecast_results)) f, ax = plt.subplots(1) f.set_figheight(5) f.set_figwidth(15) ax.plot(x, forecast_results, marker='o', markersize=10, linestyle='', color='orange', label='Forecast') ax.grid() plt.xlabel('Time', fontsize=16) plt.ylabel(self.load_options["frequency"] + ' Demand', fontsize=16) ax.legend(loc='best') plt.suptitle(self.load_options["frequency"] + ' forecast', y=1) ax.set_xticks(x) ax.set_xticklabels(x.astype(int)) self.plots["visualise_forecast"] = f st.write(f) with tab2: start_date = self.datasets["forecast_df"]["date"].max() date_offset = pd.DateOffset(**{self.load_options["freq_label"]: 1}) date_range = pd.date_range(start_date + date_offset, periods=self.load_options["duration"], freq= self.load_options["freq"] ) forecast_table = pd.DataFrame({'Date': date_range, 'Forecast_Result': forecast_results}) self.tables["forecast_table"] = forecast_table st.dataframe(forecast_table, use_container_width=True) def display_evaluation(self, results): eval = results["evaluation_metrics"] evaluation = pd.DataFrame( [[eval["MAE"], eval["MAPE"], sqrt(eval["MSE"])]], columns = ["MAE", "MAPE", "RMSE"] ) self.tables["evaluation_metrics"] = evaluation st.table(evaluation)