Spaces:
Runtime error
Runtime error
Update core/plot.py
Browse files- core/plot.py +40 -14
core/plot.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import matplotlib.pyplot as plt
|
| 2 |
import seaborn as sns
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def plot_forecast(result):
|
|
@@ -32,18 +33,15 @@ def plot_forecast(result):
|
|
| 32 |
|
| 33 |
def plot_future_forecast(df, result, future_df):
|
| 34 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 35 |
-
# Plot historical data
|
| 36 |
ax.plot(df['Date'], df['value'], label="Historical Data", color="blue", linewidth=2)
|
| 37 |
|
| 38 |
if "latest_prediction" in result:
|
| 39 |
last_date = df['Date'].iloc[-1]
|
| 40 |
horizon = len(result["latest_prediction"])
|
| 41 |
future_dates = pd.date_range(start=last_date + pd.Timedelta(days=1), periods=horizon, freq='B')
|
| 42 |
-
# Plot predictions
|
| 43 |
ax.plot(future_dates, result["latest_prediction"], label="Forecast", color="orange", linestyle="--", linewidth=2)
|
| 44 |
for i, val in enumerate(result["latest_prediction"]):
|
| 45 |
ax.text(future_dates[i], val, f"{val:.2f}", color="orange", fontsize=8, ha='center', va='bottom')
|
| 46 |
-
# Plot actual future values if available
|
| 47 |
if not future_df.empty and "future_actuals" in result:
|
| 48 |
actual_future_dates = future_df['Date']
|
| 49 |
actual_future_values = future_df['value']
|
|
@@ -60,23 +58,51 @@ def plot_future_forecast(df, result, future_df):
|
|
| 60 |
return fig
|
| 61 |
|
| 62 |
|
| 63 |
-
def
|
| 64 |
-
fig, ax = plt.subplots(figsize=(
|
| 65 |
-
metrics = {k: v for k, v in result['metrics'].items() if k in ['R2', '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
sns.barplot(x=list(metrics.keys()), y=list(metrics.values()), ax=ax, palette="Blues_d")
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
plt.tight_layout()
|
| 71 |
return fig
|
| 72 |
|
| 73 |
|
| 74 |
-
def
|
| 75 |
-
fig, ax = plt.subplots(figsize=(
|
| 76 |
-
metrics = {k: v for k, v in result['metrics'].items() if k in ['RMSE', 'MAE']}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
sns.barplot(x=list(metrics.keys()), y=list(metrics.values()), ax=ax, palette="Reds_d")
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
plt.tight_layout()
|
| 81 |
return fig
|
| 82 |
|
|
|
|
| 1 |
import matplotlib.pyplot as plt
|
| 2 |
import seaborn as sns
|
| 3 |
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
|
| 7 |
def plot_forecast(result):
|
|
|
|
| 33 |
|
| 34 |
def plot_future_forecast(df, result, future_df):
|
| 35 |
fig, ax = plt.subplots(figsize=(10, 6))
|
|
|
|
| 36 |
ax.plot(df['Date'], df['value'], label="Historical Data", color="blue", linewidth=2)
|
| 37 |
|
| 38 |
if "latest_prediction" in result:
|
| 39 |
last_date = df['Date'].iloc[-1]
|
| 40 |
horizon = len(result["latest_prediction"])
|
| 41 |
future_dates = pd.date_range(start=last_date + pd.Timedelta(days=1), periods=horizon, freq='B')
|
|
|
|
| 42 |
ax.plot(future_dates, result["latest_prediction"], label="Forecast", color="orange", linestyle="--", linewidth=2)
|
| 43 |
for i, val in enumerate(result["latest_prediction"]):
|
| 44 |
ax.text(future_dates[i], val, f"{val:.2f}", color="orange", fontsize=8, ha='center', va='bottom')
|
|
|
|
| 45 |
if not future_df.empty and "future_actuals" in result:
|
| 46 |
actual_future_dates = future_df['Date']
|
| 47 |
actual_future_values = future_df['value']
|
|
|
|
| 58 |
return fig
|
| 59 |
|
| 60 |
|
| 61 |
+
def plot_metrics_precision(result):
|
| 62 |
+
fig, ax = plt.subplots(figsize=(8, 5))
|
| 63 |
+
metrics = {k: v for k, v in result['metrics'].items() if k in ['R2', 'Explained Variance', 'MDA (%)'] and v is not None}
|
| 64 |
+
if not metrics:
|
| 65 |
+
ax.text(0.5, 0.5, "No valid precision metrics available", ha='center', va='center')
|
| 66 |
+
ax.set_title("Precision Metrics (Model Accuracy)")
|
| 67 |
+
return fig
|
| 68 |
+
|
| 69 |
sns.barplot(x=list(metrics.keys()), y=list(metrics.values()), ax=ax, palette="Blues_d")
|
| 70 |
+
# Add labels on bars
|
| 71 |
+
for i, v in enumerate(metrics.values()):
|
| 72 |
+
ax.text(i, v + 0.01 * max(metrics.values(), default=1), f"{v:.4f}", ha='center', va='bottom', fontsize=10)
|
| 73 |
+
|
| 74 |
+
# Dynamic y-axis scaling
|
| 75 |
+
max_val = max(metrics.values(), default=1)
|
| 76 |
+
min_val = min(metrics.values(), default=0)
|
| 77 |
+
ax.set_ylim(min(min_val - 0.1 * abs(min_val), -0.1), max_val + 0.2 * max_val)
|
| 78 |
+
|
| 79 |
+
ax.set_title("Precision Metrics (Model Accuracy)")
|
| 80 |
+
ax.set_ylabel("Value")
|
| 81 |
+
ax.grid(True, axis='y')
|
| 82 |
plt.tight_layout()
|
| 83 |
return fig
|
| 84 |
|
| 85 |
|
| 86 |
+
def plot_metrics_risk(result):
|
| 87 |
+
fig, ax = plt.subplots(figsize=(8, 5))
|
| 88 |
+
metrics = {k: v for k, v in result['metrics'].items() if k in ['RMSE', 'MAE', 'MAPE (%)', 'MASE'] and v is not None}
|
| 89 |
+
if not metrics:
|
| 90 |
+
ax.text(0.5, 0.5, "No valid risk metrics available", ha='center', va='center')
|
| 91 |
+
ax.set_title("Risk Metrics (Error Magnitude)")
|
| 92 |
+
return fig
|
| 93 |
+
|
| 94 |
sns.barplot(x=list(metrics.keys()), y=list(metrics.values()), ax=ax, palette="Reds_d")
|
| 95 |
+
# Add labels on bars
|
| 96 |
+
for i, v in enumerate(metrics.values()):
|
| 97 |
+
ax.text(i, v + 0.01 * max(metrics.values(), default=1), f"{v:.4f}", ha='center', va='bottom', fontsize=10)
|
| 98 |
+
|
| 99 |
+
# Dynamic y-axis scaling
|
| 100 |
+
max_val = max(metrics.values(), default=1)
|
| 101 |
+
ax.set_ylim(0, max_val + 0.2 * max_val)
|
| 102 |
+
|
| 103 |
+
ax.set_title("Risk Metrics (Error Magnitude)")
|
| 104 |
+
ax.set_ylabel("Value")
|
| 105 |
+
ax.grid(True, axis='y')
|
| 106 |
plt.tight_layout()
|
| 107 |
return fig
|
| 108 |
|