def update_metrics(metric_to_predictions_all,
                    metric_to_predictions_forecast, location, name,
                    predictions, ground_truth, num_forecast_steps,
                    use_quantiles):
   gt_data = ground_truth[location] if ground_truth is not None else None
   metric_to_predictions_all = model_utils.update_metric_to_predictions(
       metric=name,
       values=(predictions[location]
               [eval_train_start_idx:train_window_end_index +
                num_forecast_steps]),
       metric_to_predictions=metric_to_predictions_all,
       train_end_of_window=eval_train_start_idx,
       gt_data=gt_data,
       time_horizon_offset=time_horizon_offset,
       quantiles=quantiles if use_quantiles else None,
       quantiles_output=quantiles_output if use_quantiles else None,
       metric_string_format=metric_string_format)
   metric_to_predictions_forecast = model_utils.update_metric_to_predictions(
       metric=name,
       values=(predictions[location]
               [train_window_end_index:train_window_end_index +
                num_forecast_steps]),
       metric_to_predictions=metric_to_predictions_forecast,
       train_end_of_window=train_window_end_index,
       gt_data=gt_data,
       quantiles=quantiles if use_quantiles else None,
       quantiles_output=quantiles_output if use_quantiles else None,
       metric_string_format=metric_string_format)
   return metric_to_predictions_all, metric_to_predictions_forecast
Example #2
0
    def test_update_metric_to_predictions_offset(self):
        metric_to_predictions = {
            "Infected": [
                model.Prediction(time_horizon=1,
                                 predicted_value=20,
                                 ground_truth=12)
            ]
        }
        gt_data = [7, 2, 5, 7]

        got = model_utils.update_metric_to_predictions(
            metric="recovered_cases",
            values=np.array([[12], [23]], np.float32),
            metric_to_predictions=metric_to_predictions,
            train_end_of_window=2,
            gt_data=gt_data,
            time_horizon_offset=2)

        wanted = {
            "Infected": [
                model.Prediction(time_horizon=1,
                                 predicted_value=20,
                                 ground_truth=12)
            ],
            "recovered_cases": [
                model.Prediction(time_horizon=-1,
                                 predicted_value=12,
                                 ground_truth=5),
                model.Prediction(time_horizon=0,
                                 predicted_value=23,
                                 ground_truth=7)
            ],
        }
        self.assertEqual(wanted, got)
Example #3
0
    def test_update_metric_to_predictions_with_quantiles(self):
        metric_to_predictions = {}
        gt_data = [7, 2, 5, 7]

        got = model_utils.update_metric_to_predictions(
            metric="recovered_cases",
            values=np.array([[12, 14], [23, 25]], np.float32),
            metric_to_predictions=metric_to_predictions,
            train_end_of_window=2,
            gt_data=gt_data,
            quantiles=[0.1, 0.9],
            metric_string_format="{metric}_{quantile}_quantile")

        wanted = {
            "recovered_cases_0.1_quantile": [
                model.Prediction(time_horizon=1,
                                 predicted_value=12,
                                 ground_truth=5),
                model.Prediction(time_horizon=2,
                                 predicted_value=23,
                                 ground_truth=7)
            ],
            "recovered_cases_0.9_quantile": [
                model.Prediction(time_horizon=1,
                                 predicted_value=14,
                                 ground_truth=5),
                model.Prediction(time_horizon=2,
                                 predicted_value=25,
                                 ground_truth=7)
            ],
        }
        self.assertEqual(wanted, got)