"returns : - contexte_df : the real data, around of the point of interest - forecast_array : the predictions of the neural network - start_date: the starting date of predictions of the model.
(
date_of_pred: str,
plot_context_size: int,
target_list: List[pd.DataFrame],
pred_length: int,
trained_model: PyTorchPredictor,
num_samples: int = 1000,
is_mcl=True,
)
| 24 | |
| 25 | |
| 26 | def plotting_from_a_date( |
| 27 | date_of_pred: str, |
| 28 | plot_context_size: int, |
| 29 | target_list: List[pd.DataFrame], |
| 30 | pred_length: int, |
| 31 | trained_model: PyTorchPredictor, |
| 32 | num_samples: int = 1000, |
| 33 | is_mcl=True, |
| 34 | ) -> SampleForecast: |
| 35 | """ "returns : |
| 36 | - contexte_df : the real data, around of the point of interest |
| 37 | - forecast_array : the predictions of the neural network |
| 38 | - start_date: the starting date of predictions of the model.""" |
| 39 | |
| 40 | assert len(target_list) == 1, "the data_test must be of length 1" |
| 41 | |
| 42 | for df in target_list: |
| 43 | |
| 44 | df_context = df.loc[:date_of_pred] |
| 45 | n_total = len( |
| 46 | df_context |
| 47 | ) # creating the dataset_test from scracth. The main idea is taking data from df |
| 48 | dataset_test = {} |
| 49 | dataset_test["target"] = df_context.values.transpose() # the good dimensions |
| 50 | dataset_test["start"] = df_context.index[0] |
| 51 | dataset_test["feat_static_cat"] = np.array([0], dtype=int) |
| 52 | |
| 53 | window_length = pred_length |
| 54 | _, test_template = split([dataset_test], offset=-window_length) |
| 55 | test_data = test_template.generate_instances(window_length) |
| 56 | pred = trained_model.predict( |
| 57 | test_data.input, num_samples=num_samples |
| 58 | ) # it unrolls on the past and then it sample. |
| 59 | pred = next(iter(pred)) # we extract the prediction |
| 60 | |
| 61 | forecast_array = pred.samples |
| 62 | probabilities = None # if there is no probabilities. |
| 63 | if is_mcl: |
| 64 | forecast_array, probabilities = extract_unique_forecasts(forecast_array) |
| 65 | |
| 66 | start_date = df_context.index[ |
| 67 | n_total - pred_length |
| 68 | ] # we start predicting at this date. |
| 69 | assert pred.start_date == start_date |
| 70 | contexte_df = df.iloc[ |
| 71 | n_total - 1 - pred_length - plot_context_size : n_total |
| 72 | ] # the context we want to plot |
| 73 | |
| 74 | return contexte_df, forecast_array, start_date, probabilities |
| 75 | |
| 76 | |
| 77 | def plot_forecasts_for_dimension( |
no test coverage detected