Plot forecasts from either timeMCL or TimeGrad models. Args: ... (existing args) ... is_mcl: Whether the forecasts come from timeMCL model (with scores) or TimeGrad
(
target_df,
hypothesis_forecasts,
forecast_length: int,
rows=4,
cols=4,
plot_mean=True,
context_points=None,
freq_type="H",
fname="Predictions_plot.png",
extract_unique=True,
save_path=None,
is_mcl=True, # Parameter to distinguish between timeMCL and TimeGrad
)
| 44 | |
| 45 | |
| 46 | def plot_mcl( |
| 47 | target_df, |
| 48 | hypothesis_forecasts, |
| 49 | forecast_length: int, |
| 50 | rows=4, |
| 51 | cols=4, |
| 52 | plot_mean=True, |
| 53 | context_points=None, |
| 54 | freq_type="H", |
| 55 | fname="Predictions_plot.png", |
| 56 | extract_unique=True, |
| 57 | save_path=None, |
| 58 | is_mcl=True, # Parameter to distinguish between timeMCL and TimeGrad |
| 59 | ): |
| 60 | """ |
| 61 | Plot forecasts from either timeMCL or TimeGrad models. |
| 62 | |
| 63 | Args: |
| 64 | ... (existing args) ... |
| 65 | is_mcl: Whether the forecasts come from timeMCL model (with scores) or TimeGrad |
| 66 | """ |
| 67 | import pandas as pd |
| 68 | import numpy as np |
| 69 | import matplotlib.pyplot as plt |
| 70 | import matplotlib.dates as mdates |
| 71 | |
| 72 | # Handle forecasts based on model type |
| 73 | if is_mcl: |
| 74 | # For timeMCL, extract unique forecasts if requested |
| 75 | if extract_unique: |
| 76 | hypothesis_forecasts, probabilities = extract_unique_forecasts( |
| 77 | hypothesis_forecasts |
| 78 | ) |
| 79 | else: |
| 80 | # Extract scores for non-unique forecasts |
| 81 | scores = hypothesis_forecasts[:, :, -1] |
| 82 | hypothesis_forecasts = hypothesis_forecasts[:, :, :-1] |
| 83 | probabilities = scores / scores.sum(axis=0, keepdims=True) |
| 84 | else: |
| 85 | # For TimeGrad, use equal probabilities for all samples |
| 86 | probabilities = ( |
| 87 | np.ones_like(hypothesis_forecasts) / hypothesis_forecasts.shape[0] |
| 88 | ) |
| 89 | |
| 90 | # 1) check PeriodIndex |
| 91 | if isinstance(target_df.index, pd.PeriodIndex): |
| 92 | target_df.index = target_df.index.to_timestamp() |
| 93 | |
| 94 | # entire data |
| 95 | time_index = target_df.index |
| 96 | values = target_df.values |
| 97 | full_len, target_dim = values.shape |
| 98 | |
| 99 | k, fcst_len, d_ = hypothesis_forecasts.shape |
| 100 | assert fcst_len == forecast_length, "forecast_length mismatch" |
| 101 | |
| 102 | # 2) define how many context points we want before the 'end of training' |
| 103 | # if not given, default to 2 * forecast_length |
no test coverage detected