code: def backtest_metrics(
      test_dataset: Dataset,
      predictor: Predictor,
      evaluator=Evaluator(
          quantiles=(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9)
      ),
      num_samples: int = 100,
      logging_file: Optional[str] = None,
  ) -> Tuple[dict, pd.DataFrame]:
      """
      Parameters
      ----------
      test_dataset
          Dataset to use for testing.
      predictor
          The predictor to test.
      evaluator
          Evaluator to use.
      num_samples
          Number of samples to use when generating sample-based forecasts. Only
          sampling-based models will use this.
      logging_file
          If specified, information of the backtest is redirected to this file.
  
      Returns
      -------
      Tuple[dict, pd.DataFrame]
          A tuple of aggregate metrics and metrics per time series obtained by
          training `forecaster` on `train_dataset` and evaluating the resulting
          `evaluator` provided on the `test_dataset`.
      """
  
      if logging_file is not None:
          log_formatter = logging.Formatter(
              "[%(asctime)s %(levelname)s %(thread)d] %(message)s",
              datefmt="%m/%d/%Y %H:%M:%S",
          )
          logger = logging.getLogger(__name__)
          handler = logging.FileHandler(logging_file)
          handler.setFormatter(log_formatter)
          logger.addHandler(handler)
      else:
          logger = logging.getLogger(__name__)
  
      test_statistics = calculate_dataset_statistics(test_dataset)
      serialize_message(logger, test_dataset_stats_key, test_statistics)
  
      forecast_it, ts_it = make_evaluation_predictions(
          test_dataset, predictor=predictor, num_samples=num_samples
      )
  
      agg_metrics, item_metrics = evaluator(
          ts_it, forecast_it, num_series=maybe_len(test_dataset)
      )
  
      # we only log aggregate metrics for now as item metrics may be very large
      for name, value in agg_metrics.items():
          serialize_message(logger, f"metric-{name}", value)
  
      if logging_file is not None:
          # Close the file handler to avoid letting the file open.
          # https://stackoverflow.com/questions/24816456/python-logging-wont-shutdown
          logger.removeHandler(handler)
          del logger, handler
  
      return agg_metrics, item_metrics
  
file_path: gluonts/src/gluonts/evaluation/backtest.py
imports: import logging
  from typing import Optional, Tuple, Iterator
  import numpy
  import pandas
  from gluonts.dataset.common import DataEntry, Dataset
  from gluonts.dataset.field_names import FieldName
  from gluonts.dataset.split import split
  from gluonts.dataset.stat import calculate_dataset_statistics
  from gluonts.dataset.util import period_index
  from gluonts.evaluation import Evaluator
  from gluonts.model.forecast import Forecast
  from gluonts.model.predictor import Predictor
  from gluonts.itertools import maybe_len
label_desc: Use image node_id 4 for calling a global function with example usage: backtest_metrics(test_dataset, predictor, evaluator, num_samples, logging_file) and returns: agg_metrics,  item_metrics